In my C++/winapi program I use an event object to detect if an instance of my application is already running, in order to not let the second instance start.
But in the same time I want to allow multiple instances of the program to run if they are launched using a different user account. E.g. using “runas” command.
(That is: for each user account only 1 instance can run simultaneously)
I use the code that looks like this:
HANDLE hSingleInstance=OpenEvent(EVENT_MODIFY_STATE,FALSE,
"Local\\SingleInstanceEventName");
if(hSingleInstance!=NULL) {
// there is an instance already running
SetEvent(hSingleInstance);// let know the 1st instance that we are trying to start
CloseHandle(hSingleInstance);
return 0; // exit the program
}
else {
// this is the 1st instance
hSingleInstance=CreateEvent(NULL,FALSE,FALSE, "Local\\SingleInstanceEventName");
}
It works as expected in XP – I can run only 1 instance using the same user account, and I can run multiple instances using multiple user accounts.
But in Windows 7, the OpenEvent() function always “finds” my event object, even if the event object is created using another user account. Because of this I cannot run multiple instances using different user accounts (as desired).
What should I change in my code to allow multiple instances be run using diffent user accounts in Windows 7?
Or maybe my approach is completely wrong, then what one is correct?
thank you
All you have to do is include the username in the name of the event somehow. I’m quite frankly a bit surprised that your scenario worked in XP.