{ 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
windows,
SysUtils,
Classes,
hookform1 in 'hookform1.pas' {Form1};
{$R *.res}
var
gamehook:HHOOK ; // 定义钩子全局变量
Function huidiaohanshu(xiaoxi,sky,dao:integer):DWORD;stdcall; //回调函数
//拦截键盘消息。。。
begin
if (xiaoxi=HC_ACTION) and ((1 shl 31)and dao=0) then
begin
if (sky=VK_HOME) then // 判断按下HOME键时呼出窗口
begin
if form1=nil then Form1:=Tform1.Create(nil);
form1.Visible:=not form1.Visible;
end;
end;
huidiaohanshu:=CallNextHookEx(gamehook,xiaoxi,sky,dao); //将函数的返回值返回给回调函数
end;
var
tid:THandle;
Function WndThreadHook(hwndx:HWND):boolean;stdcall; //构建线程钩子函数
begin
Result:=false;
tid:=GetWindowThreadProcessId(hwndx); //返回的就是这个线程的id号
gamehook:=SetWindowsHookEx(WH_KEYBOARD,@huidiaohanshu,GetModuleHandle('hook.dll'),Tid); //安装线程勾子
if gamehook>0 then Result:=true;
end;
var
ckCaption:Pchar;
Function EnumWinproc(hwndx:HWND ; lParam:LPARAM ):boolean; stdcall;
//枚举所有窗口函数获取所有顶层窗口的句柄
begin
if (StrLComp(ckCaption,youxibiaoti,13)=0) then //比较窗口标题是否为游戏窗口标题
begin
WndThreadHook(hwndx);//执行钩子函数。。。
SetWindowText(hwndx,pchar(intTostr(hwndx)));//修改安装后钩子的窗口标题。。
end;
result:=true; //返回真
end;
Function diaoyongchuangkou():boolean;stdcall;
begin
//调用枚举所有窗口
ckCaption:=Allocmem(256);//分配内存空间。。。为256
result:=windows.EnumWindows(@EnumWinproc,1); //返回枚举窗口
end;
procedure tuichuchuli(reason:integer); //构建处理函数
begin
case reason of
windows.DLL_PROCESS_ATTACH: begin end;
windows.DLL_PROCESS_DETACH:
begin Form1.Free;form1:=nil; end;
end; //释放内存并赋值为空
end;
exports //套出函数
diaoyongchuangkou;