>>>>以下转添:<br>----------这是*.dll中的单元---------------<br>unit HookProc;<br><br><br>interface<br><br>uses windows,messages,sysutils;<br><br>const<br> HTName:array[1..13] of pchar=(<br> 'CALLWNDPROC','CALLWNDPROCRET','CBT','DEBUG','GETMESSAGE','JOURNALPLAYBACK',<br> 'JOURNALRECORD','KEYBOARD','MOUSE','MSGFILTER','SHELL','SYSMSGFILTER','FOREGROUNDIDLE'<br> );<br><br><br>function CallWndProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>function CallWndRetProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>function CBTProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>function DebugProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>function GetMsgProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>function JournalPlaybackProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>function JournalRecordProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>function KeyboardProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>function MouseProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>function MessageProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>function ShellProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>function SysMsgProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>function ForegroundIdleProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br><br>implementation<br><br>procedure SaveInfo(k:integer;str:string);stdcall;<br>var<br> f:textfile;<br> WorkPath:string;<br>begin<br> WorkPath:=ExtractFilePath(ParamStr(0));<br> assignfile(f,WorkPath+'Records.txt');<br> if fileexists(WorkPath+'Records.txt')=false then rewrite(f)<br> else append(f);<br> //if strcomp(pchar(str),pchar('#13#10'))=0 then writeln(f,'')<br> //else write(f,str);<br> writeln(f,HTName[k]+'----'+str);<br> closefile(f);<br>end;<br><br><br><br>function CallWndProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>var<br> pcs:TCWPSTRUCT;<br>begin<br> pcs:=TCWPSTRUCT(PCWPSTRUCT(lParam)^);<br> if nCode>=0 then<br> begin<br> if pcs.message=wm_lbuttonup then<br> SaveInfo(1,format('hwnd=%x',[pcs.hwnd]));<br> end;<br> Result:=CallNextHookEx(0,nCode,wParam,lParam);<br>end;<br>//<br>function CallWndRetProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>begin<br> Result:=CallNextHookEx(0,nCode,wParam,lParam);<br>end;<br>//<br>function CBTProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>begin<br> Result:=CallNextHookEx(0,nCode,wParam,lParam);<br>end;<br>//<br>function DebugProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>begin<br> Result:=CallNextHookEx(0,nCode,wParam,lParam);<br>end;<br>//<br>function GetMsgProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>var<br> pcs:TMSG;<br>begin<br> pcs:=TMSG(PMSG(lParam)^);<br> if nCode>=0 then<br> begin<br> if pcs.message=wm_lbuttonup then<br> SaveInfo(5,format('hwnd=%x',[pcs.hwnd]));<br> end;<br> Result:=CallNextHookEx(0,nCode,wParam,lParam);<br>end;<br>//<br>function JournalPlaybackProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>begin<br> Result:=CallNextHookEx(0,nCode,wParam,lParam);<br>end;<br>//<br>function JournalRecordProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>begin<br> Result:=CallNextHookEx(0,nCode,wParam,lParam);<br>end;<br>//<br>function KeyboardProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>begin<br> Result:=CallNextHookEx(0,nCode,wParam,lParam);<br>end;<br>//<br>function MouseProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>begin<br> Result:=CallNextHookEx(0,nCode,wParam,lParam);<br>end;<br>//<br>function MessageProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>begin<br> Result:=CallNextHookEx(0,nCode,wParam,lParam);<br>end;<br>//<br>function ShellProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>begin<br> Result:=CallNextHookEx(0,nCode,wParam,lParam);<br>end;<br>//<br>function SysMsgProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>begin<br> Result:=CallNextHookEx(0,nCode,wParam,lParam);<br>end;<br>//<br>function ForegroundIdleProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>begin<br> Result:=CallNextHookEx(0,nCode,wParam,lParam);<br>end;<br><br><br>end.<br><br><br><br>--------这是*.dll主程序------------------<br>library DemoHook;<br><br>uses<br> windows,messages,sysutils,<br> HookProc in 'HookProc.pas';<br><br>{$r *.res}<br><br>const<br><br> HookMemFileName='DllHookMemFile.DTA';<br> HTName:array[1..13] of pchar=(<br> 'CALLWNDPROC','CALLWNDPROCRET','CBT','DEBUG','GETMESSAGE','JOURNALPLAYBACK',<br> 'JOURNALRECORD','KEYBOARD','MOUSE','MSGFILTER','SHELL','SYSMSGFILTER','FOREGROUNDIDLE'<br> );<br><br>type<br> THookProc = function(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br> PShared=^TShared;<br> THook = record<br> HookHand:HHook;<br> HookType:integer;<br> HookProc:THookProc;<br> end;<br> TShared = record<br> Hook:array [0..16] of THook;<br> Father,Self:integer;<br> Count:integer;<br> hinst:integer;<br> end;<br> TWin = record<br> Msg:TMsg;<br> wClass:TWndClass;<br> hMain:integer;<br> end;<br>var<br> MemFile:THandle;<br> Shared
Shared;<br> Win:TWin;<br> wmhook:integer;<br><br>procedure SaveInfo(k:integer;str:string);stdcall;<br>var<br> f:textfile;<br> WorkPath:string;<br>begin<br> WorkPath:=ExtractFilePath(ParamStr(0));<br> assignfile(f,WorkPath+'Records.txt');<br> if fileexists(WorkPath+'Records.txt')=false then rewrite(f)<br> else append(f);<br> //if strcomp(pchar(str),pchar('#13#10'))=0 then writeln(f,'')<br> //else write(f,str);<br> writeln(f,HTName[k]+'----'+str);<br> closefile(f);<br>end;<br><br><br>procedure InitHookData;<br>var k:integer;<br>begin<br> with Shared^ do<br> begin<br> for k:=0 to 14 do Hook[k].HookHand:=0;<br> //<br> Hook[0].HookType:=WH_CALLWNDPROC;<br> Hook[0].HookProc:=@CallWndProc;<br> //<br> Hook[1].HookType:=WH_CALLWNDPROCRET;<br> Hook[1].HookProc:=@CallWndRetProc;<br> //<br> Hook[2].HookType:=WH_CBT;<br> Hook[2].HookProc:=@CBTProc;<br> //<br> Hook[3].HookType:=WH_DEBUG;<br> Hook[3].HookProc:=@DebugProc;<br> //<br> Hook[4].HookType:=WH_GETMESSAGE;<br> Hook[4].HookProc:=@GetMsgProc;<br> //<br> Hook[5].HookType:=WH_JOURNALPLAYBACK;<br> Hook[5].HookProc:=@JournalPlaybackProc;<br> //<br> Hook[6].HookType:=WH_JOURNALRECORD;<br> Hook[6].HookProc:=@JournalRecordProc;<br> //<br> Hook[7].HookType:=WH_KEYBOARD;<br> Hook[7].HookProc:=@KeyboardProc;<br> //<br> Hook[8].HookType:=WH_MOUSE;<br> Hook[8].HookProc:=@MouseProc;<br> //<br> Hook[9].HookType:=WH_MSGFILTER;<br> Hook[9].HookProc:=@MessageProc;<br> //<br> Hook[10].HookType:=WH_SHELL ;<br> Hook[10].HookProc:=@ShellProc;<br> //<br> Hook[11].HookType:=WH_SYSMSGFILTER;<br> Hook[11].HookProc:=@SysMsgProc;<br> //<br> Hook[12].HookType:=WH_FOREGROUNDIDLE;<br> Hook[12].HookProc:=@ForegroundIdleProc;<br><br> end;<br>end;<br><br>function SetHook(fSet:boolean;HookId:integer):bool;stdcall;<br>begin<br> with shared^ do<br> if fSet=true then<br> begin<br> if Hook[HookId].HookHand=0 then<br> begin<br> Hook[HookId].HookHand:=SetWindowsHookEx(Hook[HookId].HookType,Hook[HookId].HookProc,hinstance,0);<br> if Hook[HookId].HookHand<>0 then Result:=true<br> else Result:=false;<br> end else Result:=true;<br> end else<br> begin<br> if Hook[HookId].HookHand<>0 then<br> begin<br> if UnhookWindowsHookEx(Hook[HookId].HookHand)=true then<br> begin<br> Hook[HookId].HookHand:=0;<br> Result:=true;<br> end else Result:=false;<br> end else Result:=true;<br> end;<br>end;<br><br>procedure Extro;<br>begin<br> UnmapViewOfFile(Shared);<br> CloseHandle(MemFile);<br>end;<br><br><br>function WindowProc(hWnd,Msg,wParam,lParam:longint):LRESULT; stdcall;<br>var k:integer;<br>begin<br> Result:=DefWindowProc(hWnd,Msg,wParam,lParam);<br> case Msg of<br> wm_destroy:<br> begin<br> for k:=0 to 12 do SetHook(False,k);<br> postmessage(findwindow('WinHook',nil),wm_destroy,0,0);<br> ExitThread(0);<br> end;<br> end;<br> if msg=wmhook then<br> begin<br> if wparam>0 then<br> begin<br> if sethook(true,wparam-1)=true then postmessage(findwindow('WinHook',nil),wmhook,wparam,0);<br> end else<br> begin<br> if sethook(false,-wparam-1)=true then postmessage(findwindow('WinHook',nil),wmhook,wparam,0);<br> end;<br> end;<br>end;<br><br>procedure run;stdcall;<br>//var k:integer;<br>begin<br> win.wClass.lpfnWndProc:= @WindowProc;<br> win.wClass.hInstance:= hInstance;<br> win.wClass.lpszClassName:='WideHook';<br> RegisterClass(win.wClass);<br> win.hmain:=CreateWindowEx(ws_ex_toolwindow,win.wClass.lpszClassName,'WideHook',WS_CAPTION,0,0,1,1,0,0,hInstance,nil);<br> FillChar(Shared^,SizeOf(TShared),0);<br> shared^.self:=win.hmain;<br> shared^.hinst:=hinstance;<br> InitHookData;<br> wmhook:=registerwindowmessage(pchar('wm_hook'));<br> while(GetMessage(win.Msg,win.hmain,0,0))do<br> begin<br> TranslateMessage(win.Msg);<br> DispatchMessage(win.Msg);<br> end;<br>end;<br><br>procedure DllEntryPoint(fdwReason
WORD);<br>begin<br> case fdwReason of<br> DLL_PROCESS_DETACH:<br> Extro;<br> end;<br>end;<br><br>exports run;<br><br>begin<br> //建立内存映象文件,用来保存全局变量<br> MemFile:=CreateFileMapping($FFFFFFFF,nil,PAGE_READWRITE,0,SizeOf(TShared),HookMemFileName);<br> Shared:=MapViewOfFile(MemFile,FILE_MAP_WRITE,0,0,0);<br> DLLProc:=@DllEntryPoint;<br>end.<br><br>====================================================================================<br>procedure Search(Strings:TStrings); <br>implementation <br><br>{$R *.dfm} <br>procedure Search(Strings:TStrings); <br>var <br> Snap:THandle; <br> RB:Boolean; <br> PE:TProcessEntry32; <br>begin <br> if Strings=nil then <br> Exit; <br> snap:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); <br> if snap = -1 then Exit; <br> try <br> PE.dwSize:=SizeOf(TProcessEntry32); <br> RB:=Process32First(snap,PE); <br> while RB do <br> begin <br> Strings.AddObject(PE.szExeFile,Pointer(PE.th32ProcessID)); <br> PE.dwSize:=SizeOf(TProcessEntry32); <br> RB:=Process32Next(snap,PE); <br> end; <br> finally <br> CloseHandle(snap); <br> end; <br>end; <br>procedure TForm1.BitBtn1Click(Sender: TObject); <br>begin <br> ListBox1.Items.Clear; <br> Search(ListBox1.Items); <br>end; <br><br>procedure TForm1.Button1Click(Sender: TObject); <br>var <br> H:THandle; <br> R:Cardinal; <br>begin <br> H:=OpenProcess(PROCESS_TERMINATE,True,LongInt(ListBox1.Items.Objects[ListBox1.ItemIndex])); <br> R:=0; <br> TerminateProcess(H,R); <br> CloseHandle(H); <br>end; <br><br>end. <br>关键的: <br>procedure TForm1.Button1Click(Sender: TObject); <br>var <br> H:THandle; <br> R:Cardinal; <br>begin <br> H:=OpenProcess(PROCESS_TERMINATE,True,LongInt(ListBox1.Items.Objects[ListBox1.ItemIndex])); <br> R:=0; <br> TerminateProcess(H,R); <br> CloseHandle(H); <br>end; <br>其中ListBox1.Items.Objects[ListBox1.ItemIndex]是在遍历进程时存放的ProcessID