在服务程序中CreateEvent,当LPSECURITY_ATTRIBUTES这个参数传NULL的时候,将使用默认访问控制。
服务程序中的默认控制就是不允许桌面程序访问这些对象,所以服务中打开的Event,桌面无法打开,GetLastError()的值为5(ERROR_ACCESS_DENIED) 返回访问拒绝。
服务中创建的内核对象要想在普通应用程序中使用,必须指定安全描述符。
OpenEvent就按一般的写法即可,例:
服务程序中的默认控制就是不允许桌面程序访问这些对象,所以服务中打开的Event,桌面无法打开,GetLastError()的值为5(ERROR_ACCESS_DENIED) 返回访问拒绝。
服务中创建的内核对象要想在普通应用程序中使用,必须指定安全描述符。
- SECURITY_DESCRIPTOR SecurityDescriptor = { 0 };
- ::InitializeSecurityDescriptor(&SecurityDescriptor, 1);
- ::SetSecurityDescriptorDacl(&SecurityDescriptor, TRUE, NULL, FALSE);
- SECURITY_ATTRIBUTES SecurityAttribute = { 0 };
- SecurityAttribute.nLength = sizeof(SecurityAttribute);
- SecurityAttribute.lpSecurityDescriptor = &SecurityDescriptor;
- SecurityAttribute.bInheritHandle = TRUE;
- m_hEventForStop = ::CreateEvent(&SecurityAttribute, TRUE, FALSE, _defStopEventName);
- HANDLE hStopEvent = ::OpenEvent(EVENT_MODIFY_STATE, FALSE, _defStopEventName);
- if (hStopEvent)
- {
- ::SetEvent(hStopEvent);
- ::CloseHandle(hStopEvent);
- }