尝试设置服务创建的事件时在应用程序中访问被拒绝

Access denied in application when trying to set event created by service

我在 C++ 中创建了一个 Windows 服务,它创建一个事件,然后等待桌面应用程序向它发送信号。

这是事件创建代码(来自 windows 服务):

CUpdateMonitor::CUpdateMonitor()
{
    PSECURITY_DESCRIPTOR psd = (PSECURITY_DESCRIPTOR)LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
    InitializeSecurityDescriptor(psd, SECURITY_DESCRIPTOR_REVISION);
    SetSecurityDescriptorDacl(psd, TRUE, NULL, FALSE);

    SECURITY_ATTRIBUTES sa = { 0 };
    sa.nLength = sizeof(sa);
    sa.lpSecurityDescriptor = psd;
    sa.bInheritHandle = FALSE;

    m_hUpdateAvailableEvent = CreateEvent(&sa, TRUE, FALSE, UPDATE_AVAILABLE_EVENT);
    LocalFree(psd);

    if (m_hUpdateAvailableEvent == NULL)
        throw Win32Exception(GetLastError(), _T("CUpdateMonitor: Failed to create update available event"));
}

这是桌面应用程序代码:

void CClientDlg::OnNotifyUpdate()
{
    HANDLE hEvent = OpenEvent(SYNCHRONIZE, FALSE, UPDATE_AVAILABLE_EVENT); // <-- this works fine
    if (hEvent == NULL)
        MLOGE(_T("Could not open event. Err code: %d"), GetLastError());
    else
    {
        MLOGD(_T("Setting event/Notifying update service"));
        if (!SetEvent(hEvent)) // <-- this return ACCESS_DENIED
        {
            MLOGE(_T("Failed to set update event. Error code: %d"), GetLastError());
            return;
        }

        try
        {
            CSimpleMessageSender msgSender(UPDATE_PIPE_NAME);
            MLOGD(_T("Sending message to update service"));
            msgSender.SendSimpleMessage(_T("stuff"));
        }
        catch (const Win32Exception& w32Ex)
        {
            MLOGE(_T("Could not send message ot update service. Win32Exception:\n%s"), w32Ex.GetErrorMessage().c_str());
        }
    }
}

我设法打开了事件,但是当我尝试设置它时,我得到 ACCESS_DENIED。

我做错了什么?

您不应该使用 EVENT_MODIFY_STATE 作为访问权限吗? MSND 表示 SetEvent() 需要 EVENT_MODIFY_STATE