来自Windows服务应用程序的RegisterHotKey

时间:2015-04-19 23:29:49

标签: service delphi-xe

如果我从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;

1 个答案:

答案 0 :(得分:1)

你不能

  

为了补充Ken所说的,在Vista中引入Session 0 Isolation时,Interactive Services已被淘汰。在此之前,如果在对CreateService()的调用中指定了SERVICE_INTERACTIVE_PROCESS标志,则服务可以与用户桌面交互(但仅限第一个用户的桌面登录)。该标志不再受支持,服务无法再与任何用户桌面交互。 - 雷米·勒博(Remy Lebeau)