Platform SDK: DLLs, Processes, and Threads |
The OpenSemaphore function opens an existing named semaphore object.
HANDLE OpenSemaphore( DWORD dwDesiredAccess, // access BOOL bInheritHandle, // inheritance option LPCTSTR lpName // object name );
This parameter can be any combination of the following values.
Access | Description |
---|---|
SEMAPHORE_ALL_ACCESS | Specifies all possible access flags for the semaphore object. |
SEMAPHORE_MODIFY_STATE | Enables use of the semaphore handle in the ReleaseSemaphore function to modify the semaphore's count. |
SYNCHRONIZE | Windows NT/2000: Enables use of the semaphore handle in any of the wait functions to wait for the semaphore's state to be signaled. |
Terminal Services: The name can have a "Global\" or "Local\" prefix to explicitly open an object in the global or session name space. The remainder of the name can contain any character except the backslash character (\). For more information, see Kernel Object Name Spaces.
Windows 2000: On Windows 2000 systems without Terminal Services running, the "Global\" and "Local\" prefixes are ignored. The remainder of the name can contain any character except the backslash character.
Windows NT 4.0 and earlier, Windows 95/98: The name can contain any character except the backslash character.
If the function succeeds, the return value is a handle to the semaphore object.
If the function fails, the return value is NULL. To get extended error information, call GetLastError.
The OpenSemaphore function enables multiple processes to open handles of the same semaphore object. The function succeeds only if some process has already created the semaphore by using the CreateSemaphore function. The calling process can use the returned handle in any function that requires a handle to a semaphore object, such as the wait functions, subject to the limitations of the access specified in the dwDesiredAccess parameter.
The handle can be duplicated by using the DuplicateHandle function. Use the CloseHandle function to close the handle. The system closes the handle automatically when the process terminates. The semaphore object is destroyed when its last handle has been closed.
Windows NT/2000: Requires Windows NT 3.1 or later.
Windows 95/98: Requires Windows 95 or later.
Header: Declared in Winbase.h; include Windows.h.
Library: Use Kernel32.lib.
Unicode: Implemented as Unicode and ANSI versions on Windows NT/2000.
Synchronization Overview, Synchronization Functions, CloseHandle, CreateSemaphore, DuplicateHandle, ReleaseSemaphore, Object Names