Semaphores values

I have a question regarding using Semaphores

HANDLE WINAPI CreateSemaphore(...);

Is there anyway I can get the current value of the semaphore?


No, and that's intentional. Even if you could get the "current" value, that value might very well have changed before you could do anything with it. The only way to do anything with it is to get and set the value atomically -- e.g., wait for the semaphore to be free and set it to "owned" (by that piece of code) in the same operation.

Here the solution using Native Api, documented by I ommited a few thing because these would make the code even longer. The code is easy, when you understand the syntax of the native api. For example (nearly) all native api function, these exported by ntdll and not documented well by microsoft, return NTSTATUS, and dont use a functions simliar to get Set/GetLastError (). If NtQuerySemaphore fails (Status != STATUS_SUCCESS) you might want to look up the error code here:

Okay lets get to the code, its pretty straight forward, first defining some structs you can get from Then getting the address of NtQuerySemaphore in the ntdll.dll. You dont need to use LoadLibrary, because every process has ntdll.dll loaded.

NtQuerySemaphore is easy too, first parameter is the handle to the semaphore, second the information class you want to retrive (in our case SemaphoreBasicInformation = 0x0). Thee third parameter is an pointer to an struct, wich revieces the informations. Fourth paramwter is the sizeof the struct. Fifth would be the the ReturnLength, for example if you could recieve the name of the sempahore with this function, this parameter could hold the need buffer size after the first call with an incorrect SemaphoreInformationLength.

Enough! Code :)

#include <windows.h>
#include <stdio.h>


typedef NTSTATUS (NTAPI *_NtQuerySemaphore)(
    HANDLE SemaphoreHandle, 
    DWORD SemaphoreInformationClass, /* Would be SEMAPHORE_INFORMATION_CLASS */
    PVOID SemaphoreInformation,      /* but this is to much to dump here     */
    ULONG SemaphoreInformationLength, 
    PULONG ReturnLength OPTIONAL

    ULONG CurrentCount; 
    ULONG MaximumCount;

int main (int argc, char *argv[])
    _NtQuerySemaphore NtQuerySemaphore;
    HANDLE Semaphore;
    NTSTATUS Status;

    Semaphore = CreateSemaphore (NULL, 50, 100, "Test");

    NtQuerySemaphore = (_NtQuerySemaphore)GetProcAddress (GetModuleHandle ("ntdll.dll"), "NtQuerySemaphore");

    if (NtQuerySemaphore)

        Status = NtQuerySemaphore (Semaphore, 0 /*SemaphoreBasicInformation*/, 
            &BasicInfo, sizeof (SEMAPHORE_BASIC_INFORMATION), NULL);

        if (Status == ERROR_SUCCESS)
            printf ("CurrentCount: %lu", BasicInfo.CurrentCount);

    CloseHandle (Semaphore);

One last thing, be warned, microsoft could remove or change the way this function works.

Cheers evilpie

Can be solved by undocumented NtQuerySemaphore


You can always use:

//ReleaseSemaphore(Last Parameter- Previous Count)

If you want your Main thread to wait till all threads complete its execution.

//Assign threads
//WaitForSingleObject( hEvent, INFINITE) ;

//When you call the threads function and after releasing Semaphore.
//Check for the previousCount, if it is equal to MAX_SEMAPHORES-1, SetEvent()...

Hope it would help.

I think you need to call

DWORD WINAPI WaitForSingleObject

with a second argument set to 0 (immediate result requested).

and take actions depending on the result.

I hope this will help you, Jerom Wagner

Need Your Help

CSS - Push footer to bottom of page, not screen

html css footer

I have this setup on my page, this works fine if the head + content + foot fits the screen height. But when the content grows, it will grow under the foot, instead of pushing the foot to the bottom...

AWS SQS Java. Not all messages are retrieved from the SQS queue

java amazon-web-services amazon-sqs

I have been trying several approaches to retrieve all messages from the SQS queue by using AWS SDK for Java to no avail. I have read about the distributed nature of the AWS SQS and that messages are