About Delphi KeyBoard Hook question,I need your help !
-
I program on Delphi 7 . First I Create a DLL Code :
library HookDLL;
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }uses
SysUtils,Classes,Dialogs,windows;{$R *.res}
var
hookHandle:HHOOK;
hookCode:string;function KeyBoardHook(code:Integer;
wparaw:Cardinal;
lparaw:Cardinal):Cardinal;stdcall;
begin
if code >0 then
begin
// hookCode := hookCode + IntToStr(code);
ShowMessage(IntToStr(code));
CallNextHookEx(hookHandle,code,wparaw,lparaw);
end;
end;function StartHook(instance:Cardinal;SysType:Integer):Boolean;stdcall;
begin
hookHandle := SetWindowsHookEx(WH_KEYBOARD,@KeyBoardHook,instance,SysType);
if hookHandle <> 0 then
begin
ShowMessage('install Success!');
Result := True;
end
else
begin
ShowMessage('install Error!');
Result := False;
end;
end;function UnSetHook:Boolean;stdcall;
begin
Result := UnhookWindowsHookEx(hookHandle);
end;function GetPass:string;stdcall;
begin
Result := hookCode;
end;exports
StartHook,GetPass,UnSetHook;begin
end.
and use code
unit SetHook;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;type
TForm1 = class(TForm)
btn1: TButton;
btn2: TButton;
btn3: TButton;
tmr1: TTimer;
procedure btn1Click(Sender: TObject);
procedure btn2Click(Sender: TObject);
procedure btn3Click(Sender: TObject);
procedure tmr1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }end;
var
Form1: TForm1;function UnSetHook:Boolean; external 'HookDLL.dll';
function GetPass:string; external 'HookDLL.dll';
function StartHook(instance:Car -
I program on Delphi 7 . First I Create a DLL Code :
library HookDLL;
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }uses
SysUtils,Classes,Dialogs,windows;{$R *.res}
var
hookHandle:HHOOK;
hookCode:string;function KeyBoardHook(code:Integer;
wparaw:Cardinal;
lparaw:Cardinal):Cardinal;stdcall;
begin
if code >0 then
begin
// hookCode := hookCode + IntToStr(code);
ShowMessage(IntToStr(code));
CallNextHookEx(hookHandle,code,wparaw,lparaw);
end;
end;function StartHook(instance:Cardinal;SysType:Integer):Boolean;stdcall;
begin
hookHandle := SetWindowsHookEx(WH_KEYBOARD,@KeyBoardHook,instance,SysType);
if hookHandle <> 0 then
begin
ShowMessage('install Success!');
Result := True;
end
else
begin
ShowMessage('install Error!');
Result := False;
end;
end;function UnSetHook:Boolean;stdcall;
begin
Result := UnhookWindowsHookEx(hookHandle);
end;function GetPass:string;stdcall;
begin
Result := hookCode;
end;exports
StartHook,GetPass,UnSetHook;begin
end.
and use code
unit SetHook;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;type
TForm1 = class(TForm)
btn1: TButton;
btn2: TButton;
btn3: TButton;
tmr1: TTimer;
procedure btn1Click(Sender: TObject);
procedure btn2Click(Sender: TObject);
procedure btn3Click(Sender: TObject);
procedure tmr1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }end;
var
Form1: TForm1;function UnSetHook:Boolean; external 'HookDLL.dll';
function GetPass:string; external 'HookDLL.dll';
function StartHook(instance:Car -
I program on Delphi 7 . First I Create a DLL Code :
library HookDLL;
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }uses
SysUtils,Classes,Dialogs,windows;{$R *.res}
var
hookHandle:HHOOK;
hookCode:string;function KeyBoardHook(code:Integer;
wparaw:Cardinal;
lparaw:Cardinal):Cardinal;stdcall;
begin
if code >0 then
begin
// hookCode := hookCode + IntToStr(code);
ShowMessage(IntToStr(code));
CallNextHookEx(hookHandle,code,wparaw,lparaw);
end;
end;function StartHook(instance:Cardinal;SysType:Integer):Boolean;stdcall;
begin
hookHandle := SetWindowsHookEx(WH_KEYBOARD,@KeyBoardHook,instance,SysType);
if hookHandle <> 0 then
begin
ShowMessage('install Success!');
Result := True;
end
else
begin
ShowMessage('install Error!');
Result := False;
end;
end;function UnSetHook:Boolean;stdcall;
begin
Result := UnhookWindowsHookEx(hookHandle);
end;function GetPass:string;stdcall;
begin
Result := hookCode;
end;exports
StartHook,GetPass,UnSetHook;begin
end.
and use code
unit SetHook;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;type
TForm1 = class(TForm)
btn1: TButton;
btn2: TButton;
btn3: TButton;
tmr1: TTimer;
procedure btn1Click(Sender: TObject);
procedure btn2Click(Sender: TObject);
procedure btn3Click(Sender: TObject);
procedure tmr1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }end;
var
Form1: TForm1;function UnSetHook:Boolean; external 'HookDLL.dll';
function GetPass:string; external 'HookDLL.dll';
function StartHook(instance:CarIt's recommended that you not put your email address in the message. No one is going to emil you an answer - you'll have to read the forum to see if there is any help. As suggested, use the debugger and find out where the error is occurring. Once you know that, then you see how to fix it.
CQ de W5ALT
Walt Fair, Jr., P. E. Comport Computing Specializing in Technical Engineering Software
-
I program on Delphi 7 . First I Create a DLL Code :
library HookDLL;
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }uses
SysUtils,Classes,Dialogs,windows;{$R *.res}
var
hookHandle:HHOOK;
hookCode:string;function KeyBoardHook(code:Integer;
wparaw:Cardinal;
lparaw:Cardinal):Cardinal;stdcall;
begin
if code >0 then
begin
// hookCode := hookCode + IntToStr(code);
ShowMessage(IntToStr(code));
CallNextHookEx(hookHandle,code,wparaw,lparaw);
end;
end;function StartHook(instance:Cardinal;SysType:Integer):Boolean;stdcall;
begin
hookHandle := SetWindowsHookEx(WH_KEYBOARD,@KeyBoardHook,instance,SysType);
if hookHandle <> 0 then
begin
ShowMessage('install Success!');
Result := True;
end
else
begin
ShowMessage('install Error!');
Result := False;
end;
end;function UnSetHook:Boolean;stdcall;
begin
Result := UnhookWindowsHookEx(hookHandle);
end;function GetPass:string;stdcall;
begin
Result := hookCode;
end;exports
StartHook,GetPass,UnSetHook;begin
end.
and use code
unit SetHook;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;type
TForm1 = class(TForm)
btn1: TButton;
btn2: TButton;
btn3: TButton;
tmr1: TTimer;
procedure btn1Click(Sender: TObject);
procedure btn2Click(Sender: TObject);
procedure btn3Click(Sender: TObject);
procedure tmr1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }end;
var
Form1: TForm1;function UnSetHook:Boolean; external 'HookDLL.dll';
function GetPass:string; external 'HookDLL.dll';
function StartHook(instance:CarYeah... what the other two people said... Just out of curiosity though, why don't you just use a combination of the OnKeyPress, OnKeyUp and OnKeyDown events to handle what you need to do rather than the schlep of hooking into the keyboard? Also, remember to propogate meaningful error messages rather than just a one liner that won't tell you what the actual problem is. Use the GetLastError function (in the Windows.pas unit if I'm not mistaken) at the very least to see exactly what went wrong. Hope this helps you a little :)
Cheers, Glen Vlotman QFTD: "You cannot code around stupidity..."
-
I program on Delphi 7 . First I Create a DLL Code :
library HookDLL;
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }uses
SysUtils,Classes,Dialogs,windows;{$R *.res}
var
hookHandle:HHOOK;
hookCode:string;function KeyBoardHook(code:Integer;
wparaw:Cardinal;
lparaw:Cardinal):Cardinal;stdcall;
begin
if code >0 then
begin
// hookCode := hookCode + IntToStr(code);
ShowMessage(IntToStr(code));
CallNextHookEx(hookHandle,code,wparaw,lparaw);
end;
end;function StartHook(instance:Cardinal;SysType:Integer):Boolean;stdcall;
begin
hookHandle := SetWindowsHookEx(WH_KEYBOARD,@KeyBoardHook,instance,SysType);
if hookHandle <> 0 then
begin
ShowMessage('install Success!');
Result := True;
end
else
begin
ShowMessage('install Error!');
Result := False;
end;
end;function UnSetHook:Boolean;stdcall;
begin
Result := UnhookWindowsHookEx(hookHandle);
end;function GetPass:string;stdcall;
begin
Result := hookCode;
end;exports
StartHook,GetPass,UnSetHook;begin
end.
and use code
unit SetHook;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;type
TForm1 = class(TForm)
btn1: TButton;
btn2: TButton;
btn3: TButton;
tmr1: TTimer;
procedure btn1Click(Sender: TObject);
procedure btn2Click(Sender: TObject);
procedure btn3Click(Sender: TObject);
procedure tmr1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }end;
var
Form1: TForm1;function UnSetHook:Boolean; external 'HookDLL.dll';
function GetPass:string; external 'HookDLL.dll';
function StartHook(instance:CarTo: 1102043058@qq.com To: Mr. #6352956 at CodeProject: I was wondering if you're still doing anything with that .DLL code you had up on the Delphi forum at CodeProject. I looked at it, and was trying to figure out whether there might be a thread-access problem (even a security block) that kept the hook from being set. I would have called GetLastError to find out why SetWindowsHookEx returned null. Did you ever do that? I'm curious to know what you found out if you did. In case you didn't get around to it, you can look up the parameter info on MSDN, which will tell you the same thing Delphi's documentation should have. (I haven't looked at it yet to see what it says about SetWindowsHookEx.) Here are the links: GetLastError function: http://msdn.microsoft.com/en-us/library/ms679360 SetWindowsHookEx function: http://msdn.microsoft.com/en-us/library/ms644990 GAMerritt 01-18-11