I am creating a semaphore in a program and checking for GetLastError set to ERROR_ALREADY_EXISTS. It does not return this on a second instance of the program. Why?
HANDLE hSemaphore = CreateSemaphore( NULL, 1, 1, "MySemaphore" );
if ( GetLastError() == ERROR_ALREADY_EXISTS )
// Display a message to the user and exit
// Run the program
It does not return ERROR_ALREADY_EXISTS on the second instance of the program. If I add a messagbox in the "run the program" then the first instance pops that messsagebox and the second does get a return of ERROR_ALREADY_EXISTS and pops it's messagenox to tell me so. I don't see why this happens.
Same thing HAPPENS VB with a waitable timer:
Private Sub Form_Load()
Dim ft As FILETIME
TimerObjectHandle = CreateWaitableTimer(0, True, "MyTimer")
If Err.LastDllError = ERROR_ALREADY_EXISTS Then
' Another app already created the timer,
' we may need to go to sleep now
MsgBox "crWT already exists", vbOKOnly + vbMsgBoxSetForeground, "mdo"
' MsgBox "crWT did not exist", vbOKOnly + vbMsgBoxSetForeground, "mdo"
If I run this as is above twice at the same time no message pops up. If I remove the comment on the second message box then both mesage boxes pop up. So what gives, why does this happen?
Thanks for assistance, I need this resolved asap.