1
[編輯]好的傢伙,謝謝你的答案,這就是我需要知道的!Windows服務應用程序的RegisterHotKey? [解決] - 你不能
如果我從ServiceStart過程調用RegisterHotKey(),它將失敗,並返回ERROR_REQUIRES_INTERACTIVE_WINDOWSTATION。我找不到這方面的很多信息,所以我創建了一個線程,創建了一個窗口(CreateWindow),並從這個上下文中調用了RegisterHotKey()。但是,它返回相同的錯誤,從服務應用程序註冊熱鍵的正確方法是什麼?
Function Makewnd(): integer;
Var
Hwnd: THandle;
uMsg: TMsg;
Begin
Hwnd := CreateWindow('STATIC', 'DummyWindow', 0, 0, 0, 100, 100, HWND_MESSAGE, 0, HInstance, Nil);
Writelog(pchar('CreateWindow HWND->'+inttohex(hwnd,8)));
If (RegisterHotKey(Hwnd, 7000, MOD_CONTROL or MOD_ALT, VK_F12) = TRUE) Then
writelog('hotkey set: MOD_CONTROL or MOD_ALT, VK_F12')
Else begin
Writelog(PWideChar('Error: '+inttostr(getlasterror())));
End;
while (GetMessage(uMsg, Hwnd, 0, 0) = TRUE) do
case uMsg.message of
WM_HOTKEY:
Begin
Writelog(PWideChar('Hotkey! ID-> ' + inttostr(uMsg.wParam)));
End;
end;
Writelog('GetMessage=false');
Result := 0;
End;
procedure ServiceController(CtrlCode: DWord); stdcall;
begin
Service4.Controller(CtrlCode);
end;
function TService4.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;
procedure TService4.ServiceExecute(Sender: TService);
begin
Writelog('ServiceExecuteing');
while not Terminated do
Begin
ServiceThread.ProcessRequests(TRUE);
End;
end;
procedure TService4.ServiceStart(Sender: TService; var Started: Boolean);
Var
TID: DWORD;
Handle: THandle;
begin
writelog('ServiceStart');
Handle := CreateThread(Nil, 0, @makewnd, Nil, 0, TID);
//not using handle right now
end;
你不能,因爲服務不與用戶桌面交互;他們無法獲得按鍵。錯誤值清楚地表明瞭這一點,所以我不確定你會期望我們提供什麼。 'ERROR_REQUIRES_INTERACTIVE_WINDOWSTATION'似乎對我來說很容易理解。 –
要添加到Ken所說的內容,[Interactive Services](https://msdn.microsoft.com/en-us/library/windows/desktop/ms683502.aspx)在[Session 0 Isolation](https:// msdn.microsoft.com/en-us/library/windows/hardware/dn653293.aspx)在Vista中引入。在此之前,如果在調用CreateService()時指定了SERVICE_INTERACTIVE_PROCESS標誌,那麼服務*可以與用戶桌面進行交互(但只有第一個用戶的桌面才能登錄)。該標誌不再受支持,並且服務不能再與任何用戶桌面交互。 –
謝謝,這就是我需要知道的一切! – cracksman