to:品梅小哥<br>实在不好意思,我是新手没什么分,等下我用朋友的号再给100分,请你帮看一下下面的代码<br>以下是我的程序<br>dll部分:<br>library HookApi;<br><br>uses<br> SysUtils,<br> windows,<br> Messages;<br><br>type<br> PData = ^TData;<br> TData = record<br> Hook: THandle;<br> Hooked: Boolean;<br> end;<br> <br>var<br> DLLData: PData;<br> h:thandle;<br>{------------------------------------}<br>{过程名:HookProc<br>{过程功能:HOOK过程<br>{过程参数:nCode, wParam, lParam消息的相<br>{ 关参数<br>{------------------------------------}<br>procedure HookProc(nCode, wParam, lParam: LongWORD);stdcall;<br>var<br> Msg : PMSG;<br>begin<br> if ncode=HC_ACTION then<br> begin<br> SetWindowLong(h,GWL_STYLE,GetWindowLong(h,GWL_STYLE) or WS_CAPTION);//修改窗体的exstyle属性<br> SetWindowLong(h,GWL_EXSTYLE,GetWindowLong(h,GWL_EXSTYLE)<br> or WS_EX_APPWINDOW or WS_EX_WINDOWEDGE);<br> //设置窗体的位置,取消其最前端显示,为图简单807,632是我自己随便设的//当然最好是先用AdjustWindowRect函数调整一下大小<br> SetWindowPos(h,HWND_NOTOPMOST,0,0,640,480,SWP_SHOWWINDOW);<br> ShowWindow(h,SW_SHOWNORMAL);<br> Msg := PMSG(lParam);<br> ///////////////////////////这里加入你要钩的消息s<br> if Msg.wparam=integer($3e9) then<br> KillTimer(h,Msg.wparam);<br> DLLData^.Hooked := True;<br> end;<br> //调用下一个Hook<br> CallNextHookEx(DLLData^.Hook, nCode, wParam, lParam);<br>end;<br><br><br>{------------------------------------}<br>{函数名:InstallHook<br>{函数功能:在指定窗口上安装HOOK<br>{函数参数:sWindow:要安装HOOK的窗口<br>{返回值:成功返回TRUE,失败返回FALSE<br>{------------------------------------}<br>function InstallHook(SWindow: LongWORD):Boolean;stdcall;<br>var<br> ThreadID: LongWORD;<br>begin<br> Result := False;<br> DLLData^.Hook := 0;<br> h:=swindow;<br> ThreadID := GetWindowThreadProcessId(sWindow, nil);<br> //给指定窗口挂上钩子<br> DLLData^.Hook := SetWindowsHookEx(WH_GETMESSAGE, @HookProc, Hinstance, ThreadID);<br> if DLLData^.Hook > 0 then<br> Result := True //是否成功HOOK<br> else<br> exit;<br>end;<br><br>{------------------------------------}<br>{过程名:UnHook<br>{过程功能:卸载HOOK<br>{过程参数:无<br>{------------------------------------}<br>procedure UnHook;stdcall;<br>begin<br> //卸载Hook<br> UnhookWindowsHookEx(DLLData^.Hook);<br>end;<br><br>{------------------------------------}<br>{过程名
LL入口函数<br>{过程功能:进行DLL初始化,释放等<br>{过程参数
LL状态<br>{------------------------------------}<br>procedure MyDLLHandler(Reason: Integer);<br>var<br> FHandle: LongWORD;<br>begin<br> case Reason of<br> DLL_PROCESS_ATTACH:<br> begin //建立文件映射,以实现DLL中的全局变量<br> FHandle := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE, 0, $ffff, 'MYDLLDATA');<br> if FHandle = 0 then<br> if GetLastError = ERROR_ALREADY_EXISTS then<br> begin<br> FHandle := OpenFileMapping(FILE_MAP_ALL_ACCESS, False, 'MYDLLDATA');<br> if FHandle = 0 then Exit;<br> end else Exit;<br> DLLData := MapViewOfFile(FHandle, FILE_MAP_ALL_ACCESS, 0, 0, 0);<br> if DLLData = nil then<br> CloseHandle(FHandle);<br> end;<br> DLL_PROCESS_DETACH:<br> begin<br> if Assigned(DLLData) then<br> begin<br> UnmapViewOfFile(DLLData);<br> DLLData := nil;<br> h:=0;<br> end;<br> end;<br> end;<br>end;<br><br>{$R *.res}<br>exports<br> InstallHook, UnHook, HookProc;<br><br>begin<br> DLLProc := @MyDLLHandler;<br> MyDLLhandler(DLL_PROCESS_ATTACH);<br> DLLData^.Hooked := False;<br>end.<br><br>主程序部分<br>var<br> Form1: TForm1;<br> InstallHook: function(SWindow:LongWORD):boolean;stdcall;<br> UnHook: procedure;stdcall;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> @InstallHook := GetProcAddress(hw, 'InstallHook');<br> @UnHook := GetProcAddress(hw, 'UnHook');<br> if InstallHook(h) then<br> caption:='hook';<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br> Unhook;<br>end;<br>不知道为什么勾不到.<br><br><br>