Access denied on WaitForSingleObject on an event created from managed code

假如想象 提交于 2021-02-16 19:36:10

问题


How do I create a named event object in C# so that I can wait on it in separate C++ process?

My simplified C# code of process A:

EventWaitHandle evt = new EventWaitHandle(false, EventResetMode.AutoReset, "eventName");
EventWaitHandle.SignalAndWait(evt , <other event>);

And simplified C++ code of process B:

HANDLE hEvt = OpenEvent(
    EVENT_MODIFY_STATE | SYNCHRONIZE,  // access
    FALSE,               // do not inherit handle
    TEXT("eventName")
);

if (hEvt == NULL)
{
    printf("CreateEvent failed (%d)\n", GetLastError());
    //error out
}

DWORD dwWaitResult = WaitForSingleObject(hEvt, INFINITE); 
switch (dwWaitResult)
{
    case WAIT_OBJECT_0:
        //Signalled  - all good
        break;

    // An error occurred
    default:
        printf("Wait error (%d)\n", GetLastError());
        //error out
}

When I run (or debug) this code, the event is created (C#) and opened (C++) just fine. However I get error 5 (Access Denied) during WaitForSingleObject. Adding 'Global\' prefix does not help (nor does adding EventWaitHandleSecurity argument in C# code that allows full control to all build in users).

What I am doing wrong? Is this supposed to work at all?

Edit Fixed the C++ by adding SYNCHRONIZE desired access. Credit for pointing this out goes to Hans Passant - Access denied on WaitForSingleObject on an event created from managed code


回答1:


(Answering myself as there is no other answer to accept, but it is not unanswered question any more).

As pointed out by Hans Passant - Issue was in incomplete desired access flags of OpenEvent call. The code in question is already accordingly fixed and works fine.



来源:https://stackoverflow.com/questions/43791666/access-denied-on-waitforsingleobject-on-an-event-created-from-managed-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!