Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Windows 7 Services - Hotkey implement - Do not run

hotkey

This topic has been archived. This means that you cannot reply to this topic.
No replies to this topic

#1 elioliveira

elioliveira

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 19 April 2015 - 07:59 AM

Hi guys..!

So, i'd like to implement a hot-key in may Windows Service app. In windows XP this code run fine, but in Windows 7 no! What's going on?

 

Look my code:

 

*********************************************************************
unit Inventary;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils,
System.Classes, Vcl.Graphics, Vcl.Controls,
Vcl.SvcMgr, Vcl.Dialogs;

type
TMInventary = class(TService)
    procedure ServiceStart(Sender: TService; var Started: Boolean);
    procedure ServiceStop(Sender: TService; var Stopped: Boolean);
    procedure ServiceExecute(Sender: TService);
private
    procedure WMHotKey(var Msg: TWMHotKey); message WM_HOTKEY;
public
    function GetServiceController: TServiceController; override;
end;

var
MInventary: TMInventary;

implementation

{$R *.dfm}

procedure TMInventary.WMHotKey(var Msg: TWMHotKey);
const
    FileName = 'C:\Hotkey.log';
var
    F: TextFile;
begin
    inherited;
       Beep;
      AssignFile(f,FileName);
     if FileExists(FileName) then Append(f)
     else
       Rewrite(f);
    writeln(f,DateTimeToStr(Now)+'Hotkey is running ok');
   CloseFile(f);
end;

procedure ServiceController(CtrlCode: DWord); stdcall;
begin
    MInventary.Controller(CtrlCode);
end;

function TMInventary.GetServiceController: TServiceController;
begin
     Result := ServiceController;
end;

procedure TMInventary.ServiceExecute(Sender: TService);
begin
      while not Terminated do
            ServiceThread.ProcessRequests(True);
end;

procedure TMInventary.ServiceStart(Sender: TService; var Started: Boolean);
begin
   RegisterHotKey(0, 7000, MOD_CONTROL or MOD_ALT, VK_F12);
end;

procedure TMInventary.ServiceStop(Sender: TService; var Stopped: Boolean);
begin
    UnRegisterHotKey(0, 7000);
end;

end.
******************************************************************************

 

So, Hotkey does not work at all..!

Windows 7 x32 - Delphi XE7 - Update 1.

 

Could you give any help about that? I've been playing with it and found that RegisterHotKey() returns false, and GetlastError() returns ERROR_REQUIRES_INTERACTIVE_WINDOWSTATION.

 

How can I implement this?

Thank you. rolleyes.png