gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:12:35 27 4
gpt4 key购买 nike

我用 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

关于c++ - 尝试设置服务创建的事件时在应用程序中访问被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33830099/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com