我在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。
我做错了什么?