如何解决RegisterHotKey在服务中导致错误1459此操作需要交互式窗口站
我在SO上阅读了几个类似的主题,讨论了启动另一个进程并在该子进程中注册热键,但是我的情况略有不同。
我正在构建一个执行许多不同功能的Windows服务,并希望将其保留为服务,因为我需要我的代码在用户登录之前以及在系统关闭之前一直在系统启动时运行。
我也投入了大量的精力[strong] ,使其其余的功能都可以正常工作,因此必须将整个项目重新构建为控制台应用,然后找出如何在系统上运行该项目启动远非理想。
这里是执行大部分实际工作的线程回调。我已经验证到目前一切正常,但是在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,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:-)
解决方法
与交互式桌面交互的代码将需要在该交互式桌面中运行。您的服务在会话0中隔离运行。
您将不得不分离出系统中的交互式部分,并在交互式过程中运行它们。您可以保留该服务来管理这些交互过程,并使用IPC进行通信。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。