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