views:

209

answers:

2

I have two windows application, one is a windows service which create EventWaitHandle and wait for it. Second application is a windows gui which open it by calling EventWaitHandle.OpenExisting() and try to Set the event. But I am getting an exception in OpenExisting. The Exception is "Access to the path is denied".

windows Service code

EventWaitHandle wh = new EventWaitHandle(false, EventResetMode.AutoReset, "MyEventName");
wh.WaitOne();

Windows GUI code

try
{
    EventWaitHandle wh = EventWaitHandle.OpenExisting("MyEventName");
    wh.Set();
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

I tried the same code with two sample console application, it was working fine.

A: 

This might be caused by the service process running at an elevated privilege level, but the GUI process is not. If you put the same code into two console apps, they'll both be running at user level and won't have any trouble accessing each other's named shared objects.

Try running the GUI app with the "Run as administrator" flag from the Windows start menu. If that solves the issue, you need to read up on how to request elevation within your code. (I haven't done that)

dthorpe
i logged in as administrator only and i am using windows XP
Navaneeth
Good thinking, but I don't think Run as Administrator will help since the other process is running in an isolated session. For example, I don't believe Mutexes can be shared between a service in "session 0" and a logged on interactive session. I am no expert in this regard however.
Josh Einstein
Just found the following document which describes the session 0 impact in Vista. As Codeka mentions, you can share events this way if you explicitly use the Global\ scope. Here's the URL: http://www.microsoft.com/whdc/system/sysinternals/Session0Changes.mspx
Josh Einstein
Navaneeth: Ok, XP disqualifies process elevation. That only exists in Vista and Win7. (In Vista and Win7, you can be logged in as an admin user but your GUI apps still will not run with full admin privilege unless you go through the elevation confirmation security question)
dthorpe
+5  A: 

You need to use the version of the EventWaitHandle constructor that takes an EventWaitHandleSecurity instance. For example, the following code should work (it's not tested, but hopefully will get you started):

// create a rule that allows anybody in the "Users" group to synchronise with us
var users = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null);
var rule = new EventWaitHandleAccessRule(users, EventWaitHandleRights.Synchronize | EventWaitHandleRights.Modify,
                          AccessControlType.Allow);
var security = new EventWaitHandleSecurity();
security.AddAccessRule(rule);

bool created;
var wh = new EventWaitHandle(false, EventResetMode.AutoReset, "MyEventName", out created, security);
...

Also, if you're running on Vista or later, you need to create the event in the global namespace (that is, prefix the name with "Global\"). You'd also have to do this on Windows XP if you use the "Fast User Switching" feature.

Dean Harding
thanks. let me test first ....
Navaneeth
hey thank you very much.....its working fine
Navaneeth