开发者

Different behaviour of an event object in Windows XP and Windows 7

开发者 https://www.devze.com 2023-03-09 02:23 出处:网络
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.

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 m开发者_JAVA百科aybe 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.


I think you're running into security issues here. Generally speaking, one app cannot modify objects created by another app that's running as another user, so if one app creates the event object, an app running as another user should not be able to open it - even it already exits.

There are some subtle changes to this on Vista onwards; the concept of running something 'elevated' introduces an extra wrinkle; it's similar, but not exactly the same, as running as another user. Some resources are allowed through for compat reasons - I can't remember offhand if events/mutexes are in this category.

Either way, as currently written, your code likely is incorrect: part of the problem is that OpenEvent can fail for multiple reasons: your code assumes it's failing because the event is not created, but it could be failing because the event is created but that the calling code does not have permission to modify the object.

As mentioned in one of the other replies, if you want to do this on a per-user basis (but still within the same session), you need to give your events per-user names: add the username to the event's base name, and you should be done.

0

精彩评论

暂无评论...
验证码 换一张
取 消