M
m8858
Unregistered / Unconfirmed
GUEST, unregistred user!
///////////////EXE////////////////unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls, ExtCtrls, XPMan;typeTForm1 = class(TForm) Button1: TButton; Button2: TButton; Edit1: TEdit; Label1: TLabel;procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject);private{ Private declarations }public{ Public declarations }end;var Form1: TForm1; hhk:Longint;function HookOn(lpHwnd:HWND):Longint;stdcall;external 'Hook.dll' name 'HookOn';function HookOff:Boolean;stdcall;external 'Hook.dll' name 'HookOff';implementationfunction UninstallHook(hk:HHOOK):Boolean;stdcall;export; //卸载钩子begin if hk<>0 then begin UnHookWindowsHookEx(hHk); hk :=0; Result :=true; end else Result :=false;end;//------------------------------------------------------------------------------{$R *.dfm}procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);begin hookoff;end;procedure TForm1.Button1Click(Sender: TObject);var h1:HWND; txt:string;begin if Edit1.Text<>'' then begin txt:=edit1.text; h1:=FindWindow(nil,pchar(txt)); if h1>0 then begin hhk:=HookOn(h1); showmessage('找到进程,已注入!'); Caption:=IntToStr(hhk); end else showmessage('没找到进程!'); edit1.setfocus; end else begin showmessage('窗口标题不能为空!'); edit1.setfocus; end; end;procedure TForm1.Button2Click(Sender: TObject);begin UninstallHook(hhk); showmessage('注入已被取消!');end;end.////////////////////////HookUnit/////////////////////////////////unit HookUnit;interfaceuseswindows,Dialogs;var hTimer : Thandle = 0;procedure TimerProc(hWnd: HWND; uMsg: UINT; idEvent: UINT; dwTime: DWORD); stdcall;procedure ThreadPro(X: Integer); stdcall;procedure TimerOff();procedure TimerOn();implementationprocedure TimerProc(hWnd: HWND; uMsg: UINT; idEvent: UINT; dwTime: DWORD); stdcall;beginTimerOff();sleep(5000);showmessage('aaa'); //这里是我要做的事 先用showmessage代替TimerOn();end;procedure TimerOff();begin if (hTimer <> 0) then begin KillTimer(0, hTimer); hTimer := 0; end;end;procedure TimerOn();begin if (hTimer <> 0) then TimerOff(); TimerProc(0, 0 , 0, 0); // 先执行一次end;procedure ThreadPro(X: Integer); stdcall;var Msg: TMsg;begin TimerOn(); while GetMessage(Msg, 0, 0, 0) do DispatchMessage(Msg); TimerOff();end;end.////////////////////Hook///////////////////////library Hook;uses Windows, Messages, HookUnit;var hmodointer; xx :integer=0; hThread1:Thandle; hThread :Thandle; hHk:HHOOK=0;function HookProc(nCode:Integer;WParam: WPARAM;LParam:LPARAM):LRESULT;stdcall;label theExit;var ThreadIDWord;begin if xx = 0 then begin xx := xx+1; hthread1:=CreateThread(nil,0,@ThreadPro,nil,0,ThreadID); end; result := CallNextHookEx(hHk, nCode, wParam, lParam);end;//------------------------------------------------------------------------------function HookOn(lpHwnd:HWND):Longint;stdcall;export;//安装钩子begin hThread := GetWindowThreadProcessId(lpHwnd,hmod); if (lpHwnd <> 0) and (xx = 0) then hHk := SetWindowsHookEx(WH_MOUSE,@HookProc,hInstance,hThread); xx := xx+1; 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;exportsHookOn,HookOff;beginend./////////////////////////////////////////////////////////////以上是一个注入钩子的程序,程序注入 运行 都没问题,但只要是目标程序退出,就立刻会出错,自身主程序不从新启动是注入不了其他窗口的,这事什么原因呢? 如何修正这个错误? 大家可以用WIN自带的计算器来测试