VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Interprocess Communication via Semaphores

by Joe Jordan (2 Submissions)
Category: Windows API Call/Explanation
Difficulty: Advanced
Date Added: Wed 3rd February 2021
Rating: (9 Votes)

New version 1.1


+ Added IsSemaphore function to quickly test for the existence of a semaphore

+ Added HandleSecurity feature which prevents the closing of the semaphore handle via CloseHandle

+ Added caching of ValidateDLL result to improve speed

+ Started Adding QueryHandleCount function; if anyone can fix it please let me know

- Fixed logic error in getting the semaphore global state if we opened an existing semaphore on initialize call



---

Ah, semaphores, the staple of any good operating system. I searched for a VB6 example implementing the semaphore functions and came up mostly empty-handed. This class attempts to fill that missing gap in the world of VB6 examples.



In developing this class, I initially thought it would be simple to create a global semaphore that all users would have access to. After all, the documentation states that: "The semaphore name can have a "Global\" or "Local\" prefix to explicitly create the object in the global or session name space." Little did I know that I would have to delve into the depths of Windows security functions in order to actually provide *true* Global semaphore functionality. After many failed attempts, approaches and cryptic error messages (The revision level is unknown. WTF?), I believe that the infamous ACL dragon has, for our intentions at least, been slayed.



I took the advice of one Anne Gunn and implemented some additional security for our global semaphore, so rogue applications can't steal our lunch money completely.



The majority of the trial and error took place in finding the proper way to call and declare the security APIs. I thought I was 98% done, so I tested on XP to see if it worked there, as I figured if I could get it to work on Windows 7-64 bit, surely it would work in the UAC-less environment of XP. Well, it worked fine in the IDE, but spit out an invalid memory access error when compiled. I had gotten a similar error while testing in Windows 7 and tracked it down to using the actual struct when calling CreateSemaphore rather than the pointer. So I had to go back through each call and test to see which one needed the actual struct instead of the pointer. Turns out it was SetSecurityDescriptorDacl that needed to accept an actual SECURITY_DESCRIPTOR rather than a pointer to one. After the 2nd such discovery, I went back and used the actual structs whenever possible as a precaution.



The class was lightly tested on Windows 2000, XP, Vista and Windows 7. If you come across any issues or have any improvements or suggestions please let me know.



Credits:

https://undocumented.ntinternals.net/">https://undocumented.ntinternals.net/">https://undocumented.ntinternals.net/ for information on the undocumented NtQuerySemaphore API function and the (also undocumented) SEMAPHORE_QUERY_STATE permission constant.



Anne Gunn for her excellent, thorough and well written article and accompanying code on creating a not-quite-null dacl, and explaining the benefits of doing so.

https://www.codeguru.com/cpp/w-p/win32/tutorials/article.php/c4545



Matts_User_Name">https://www.codeguru.com/cpp/w-p/win32/tutorials/article.php/c4545">https://www.codeguru.com/cpp/w-p/win32/tutorials/article.php/c4545



Matts_User_Name of the SysInternals forums for the QueryName function.

https://forum.sysinternals.com/handle-name-help-ntqueryObject_topic14435_page2.html



IrfanAhmad">https://forum.sysinternals.com/handle-name-help-ntqueryObject_topic14435_page2.html">https://forum.sysinternals.com/handle-name-help-ntqueryObject_topic14435_page2.html



IrfanAhmad on the MSDN forums for his thread on how to share a semaphore:

https://social.msdn.microsoft.com/Forums/en/windowssdk/thread/335db156-b1f7-45e2-b3d1-f0e79e386744">https://social.msdn.microsoft.com/Forums/en/windowssdk/thread/335db156-b1f7-45e2-b3d1-f0e79e386744">https://social.msdn.microsoft.com/Forums/en/windowssdk/thread/335db156-b1f7-45e2-b3d1-f0e79e386744

Rate Interprocess Communication via Semaphores

Download Interprocess Communication via Semaphores

Download Interprocess Communication via Semaphores (10 KB)

Interprocess Communication via Semaphores Comments

No comments have been posted about Interprocess Communication via Semaphores. Why not be the first to post a comment about Interprocess Communication via Semaphores.

Post your comment

Subject:
Message:
0/1000 characters