Query the Value of a Semaphore

Have you ever wanted to take a look at the current value of a semaphore whilst debugging your code, but don't want to alter the value of the semaphore by ReleaseSemaphore?

Well, Win32 does not give you the possibilty to get the value, but there is a call in ntdll.dll which allows to query the semaphore. So the 'QuerySemaphore.lib' simply provides a small static lib, which wraps the NtQuerySemaphore call in ntdll.dll.

QuerySemaphore.lib is selfcontained, so your program does not need ntdll.lib to resolve the call to NtQuerySemaphore.

A simple example

	#include "QuerySemaphore.h"

	LONG	Value, v1;

	// Create a semaphore
	HANDLE	Handle = CreateSemaphore(NULL, 4, 10, NULL);

	// Query the content of a semaphore
	QuerySemaphore(Handle, &Value);

	// Acquire Semaphore
	WaitForSingleObject(Handle, INFINITE);
	
	// Query again
	QuerySemaphore(Handle, &Value);

So all you need is to include 'QuerySemaphore.h' in your sourcecode and add 'QuerySemaphore.lib' to the libraries you are linking against.

Download demo project - 2 KB

Download source - 8 KB



Comments

  • NtQuerySemaphore.lib

    Posted by Brad on 06/11/2012 07:07pm

    First of all, nice work! 13 years after your post this code is still very useful! Where did you get NtQuerySemaphore.lib from? Did you build it? If so, can you post that source or provide some instructions? I'm trying to link it to a 64 bit application and it is failing... (I'm assuming I need a 64 bit version of NtQuerySemaphore.lib) Thank you!

    Reply
  • RE: What about W95/W98?

    Posted by Legacy on 03/14/1999 12:00am

    Originally posted by: Hermann Schinagl

    > And also, does this function provide proper locking on multiprocessor
    > systems ??

    Yes it does, because it is a System call in Windows NT.

    > What Abot Win95/98 ??? Is there any way to query semaphore value
    > in 95/98 ?

    Uhh, I don't know.. But I guess it it is very different, because
    the architecture of W95/W98 is different to Windows NT.

    Ciao Hermann



    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • The first phase of API management was about realizing the business value of APIs. This next wave of API management enables the hyper-connected enterprise to drive and scale their businesses as API models become more complex and sophisticated. Today, real world product launches begin with an API program and strategy in mind. This API-first approach to development will only continue to increase, driven by an increasingly interconnected web of devices, organizations, and people. To support this rapid growth, …

  • Live Event Date: September 17, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Another day, another end-of-support deadline. You've heard enough about the hazards of not migrating to Windows Server 2008 or 2012. What you may not know is that there's plenty in it for you and your business, like increased automation and performance, time-saving technical features, and a lower total cost of ownership. Check out this upcoming eSeminar and join Rich Holmes, Pomeroy's practice director of virtualization, as he discusses the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds