var OldHook: HHOOK; OldProc:FARPROC; CriticalSection: TRTLCriticalSection;
Function WndProc(Hwnd,Msg,wParam,lParam:longint):LRESULT; stdcall; begin case Msg of //WM_PAINT: 这东西好啊 = = ,搞点什么好事都不错丫,嘻嘻嘻 WM_MOUSEMOVE: begin showmessage('s'); end; end; Result:=CallWindowProc(OldProc,Hwnd,Msg,wParam,lParam); end;
procedure HookProc(nCode, wParam, lParam: LongWORD);stdcall; var Winh:HWND; begin CallNextHookEx(OldHook, nCode, wParam, lParam); end;
function SetHook:Boolean;stdcall; begin OldHook:=SetWindowsHookEx(WH_GETMESSAGE,@HookProc, Hinstance,0); if (OldHook=0) then begin exit end else Result:=True; end;
procedure UnHook; stdcall; begin UnhookWindowsHookEx(OldHook); end;
function IdToExeDir(dwProcessId : DWORD): String; var cbNeeded : DWORD; hProcess : THandle; hModules : HMODULE; lpFilename : array [0..1024-1] of Char; begin result:=''; hProcess:=OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ,false,dwProcessId); if hProcess=0 then exit; EnumProcessModules(hProcess,@hModules,sizeof(hModule),cbNeeded); GetModuleFileNameEx(hProcess,hModules,lpFilename,1024); result:=lpFilename; CloseHandle(hProcess); end;
procedure SetWndProc; var WinStr:HWND; begin EnterCriticalSection(CriticalSection); Sleep(2000); //等两秒,嘿嘿 这回窗口出来了吧~ WinStr:=FindWindow(nil,'Windows 任务管理器'); OldProc:=FARPROC(GetWindowLong(WinStr,GWL_WNDPROC)); if WinStr<>0 then begin SetWindowLong(WinStr,GWL_WNDPROC,Longint(@WndProc)); end; LeaveCriticalSection(CriticalSection); ExitThread(4); end;
procedure FindWindows; var WinStr:HWND; ThreadId1:DWORD; begin if pos('taskmgr',IdToExeDir(GetCurrentProcessID))>0 then begin if CriticalSection.RecursionCount<>0 then DeleteCriticalSection(CriticalSection); InitializeCriticalSection(CriticalSection); CreateThread(nil,0,@SetWndProc,nil,0,ThreadId1); //虽然这时候窗口没出来find不到句柄,那我们不懂来个线程啊,嘻嘻嘻嘻 end; end;
procedure DllMain(Reason: Integer); begin case Reason of DLL_PROCESS_ATTACH: begin FindWindows; end; DLL_PROCESS_DETACH: begin
end; end; end;
exports SetHook, UnHook;
begin DLLProc:=@DllMain; DllMain(DLL_PROCESS_ATTACH); end.