unit mydll;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls , ComCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
function HookProc(nCode:Integer;WParam: WPARAM;LParam:LPARAM):LRESULT;stdcall;
function HookOn(lpHwnd:HWND;lpType:Longint):Longint;stdcall;export;
function HookOff:Boolean;stdcall;export;
implementation
var
hHk: HHOOK=0; //钩子的句柄
hThread: Cardinal; //长整形数据,窗口线程的标示符
hmod: Pointer; //进程标示符的指针
//以下就是呼出封装在dll文件中窗口的代码
{$R *.dfm}
function HookProc(nCode:Integer;WParam: WPARAM;LParam:LPARAM):LRESULT;stdcall;
begin
if (wParam=VK_END) and ((LParam and $40000000) <> 0) then
begin
Form1:=TForm1.Create(Application);
Form1.show;
end;
Result :=0 //CallNextHookEx(hHk,nCode,WParam,LParam);
end;
function HookOn(lpHwnd:HWND;lpType:Longint): Longint;stdcall; export;
begin
hThread :=GetWindowThreadProcessId(lpHwnd,hmod);
//注入开始
hHk :=SetWindowsHookEx(lpType,@HookProc,hInstance,hThread); // WH_KEYBOARD
Result :=hHk
end;
function HookOff:Boolean;stdcall; export;
begin
if hHk<>0 then
begin
//移除挂钩
UnHookWindowsHookEx(hHk);
hHk :=0;
Result :=true;
end
else
Result :=false;
end;
end.