我在上面读过几个类似的线程,以便讨论启动另一个进程并在子进程中注册一个热键,但我的情况略有不同。
我正在构建一个Windows服务,它执行许多不同的功能,并且希望将它作为一个服务,因为我需要我的代码在系统启动时运行,在用户登录之前,并一直运行到系统关闭。
我还投入了大量的精力让的其余部分都能正常工作,所以必须将整个项目重建为一个控制台应用程序,然后想出如何在系统启动时运行该应用程序是远远不够理想的。
下面是执行大部分实际工作的线程回调。到目前为止,我已经验证了一切正常运行,但是在GetLastError
调用RegisterHotKey
之后,我得到了1459个消息:
DWORD WINAPI SvcWorkerThread(LPVOID param) {
// Register a hot key to perform an internet search of the selected text
// in the focused application, whenever the user presses Ctrl+Alt+S.
if (!RegisterHotKey(nullptr, 1, MOD_CONTROL | MOD_ALT | MOD_NOREPEAT, 0x53)) {
// LogError() calls GetLastError() and FormatMessage() to get the system-defined error
// message from the last unsuccessful API call, and logs it to a file. The parameter is
// the name of the function that failed.
return LogError("RegisterHotKey");
}
// Repeatedly poll our stop event and process any WM_HOTKEY
// messages that are sent to this thread while its running.
MSG msg;
while (WaitForSingleObject(svcStopEvent, 0) != WAIT_OBJECT_0) {
if ((PeekMessage(&msg, nullptr, WM_HOTKEY, WM_HOTKEY, PM_REMOVE)) != 0) {
// LogMessage() is basically the same as LogError(), but
// doesn't bother with the whole error collecting process.
LogMessage("User action", "Pressed hot key Ctrl+Alt+S");
}
}
// Service has been stopped; allow thread to exit normally.
return ERROR_SUCCESS;
}
除了启动另一个进程之外,绝对没有办法避免这个错误吗?MTIA :-)
发布于 2020-10-09 13:25:10
与交互式桌面交互的代码将需要在该交互式桌面中运行。您的服务在会话0中隔离运行。
您将不得不分离出系统中具有交互性的部分,并在交互过程中运行它们。您可以保留服务来管理这些交互式进程,并使用IPC进行通信。
https://stackoverflow.com/questions/64280889
复制相似问题