钩子的问题(150分)

  • 主题发起人 主题发起人 akalun
  • 开始时间 开始时间
A

akalun

Unregistered / Unconfirmed
GUEST, unregistred user!
在主程序中捕获qq的句柄传给dll中的安装钩子函数<br>procedure TForm1.Button1Click(Sender: TObject);<br>var hwnd:Thandle;<br>begin<br>&nbsp; &nbsp; &nbsp;hwnd:=Findwindow(nil,'231002446');<br>&nbsp; &nbsp; &nbsp;showmessage(inttostr(hwnd));<br>&nbsp; &nbsp; &nbsp;EnableHook(hwnd);<br>end;<br>hwnd为dll中的全局变量<br><br>function EnableHook(shwnd:Thandle):Boolean;stdcall;export;<br>var ThreadID:Thandle;<br>begin<br>&nbsp; &nbsp; &nbsp;result:=false;<br>&nbsp; &nbsp; &nbsp;if hhk=0 then<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; hwnd:=shwnd;<br>&nbsp; &nbsp; &nbsp; &nbsp; showmessage(inttostr(hwnd));<br>&nbsp; &nbsp; &nbsp; &nbsp; ThreadId:=GetWindowThreadProcessId(hwnd,nil);<br>&nbsp; &nbsp; &nbsp; &nbsp; hhk:=SetWindowsHookEx(WH_MOUSE,@HookProc,Hinstance,ThreadID);<br>&nbsp; &nbsp; &nbsp; &nbsp; result:=true;<br>&nbsp; &nbsp; &nbsp;end;<br>end;<br><br>function HookProc(ncode:integer;wparam:Wparam;lparam:Lparam):LRESULT;stdcall;<br>var tf:textfile;<br>begin<br>&nbsp; &nbsp; &nbsp;if ncode&lt;0 then<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp;result:=CallNextHookEx(hhk,ncode,wparam,lparam);<br>&nbsp; &nbsp; &nbsp; &nbsp;exit;<br>&nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp;if wparam=WM_MOUSEMOVE then<br>&nbsp; &nbsp; &nbsp;begin &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>&nbsp; &nbsp; &nbsp;sendmessage(hwnd,WM_CLOSE,0,0);//不起作用????<br>&nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp;result:=0;<br>end;<br><br>我想安装一个鼠标钩子,当鼠标在qq上移动时关掉qq<br>为什么sendmessage(hwnd,WM_CLOSE,0,0)会不起作用,如果把这句放在主程序中可以关掉qq<br>我检查过hwnd和主程序传过来的值一样
 
那是因为你的Function定义错误,不是因为语句错误。<br>function HookProc(ncode:integer;wparam:Wparam;lparam:Lparam):LRESULT;stdcall;<br>var tf:textfile;<br>改为:<br>function 你的Tform类.HookProc(ncode:integer;wparam:Wparam;lparam:Lparam):LRESULT;stdcall;<br>var tf:textfile;<br><br>这样就欧开了吧。具体的你要看你的程序。<br>
 
应为我要监视其他的进程,HookProc函数是要放在dll中的,不是主程序的,不管这么样感谢你的回答
 
&nbsp; 强制结束<br>&nbsp;GetWindowThreadProcessId(hwnd,@P);<br>&nbsp; &nbsp; if P&lt;&gt;0 then<br>&nbsp; &nbsp; &nbsp;TerminateProcess(OpenProcess(PROCESS_TERMINATE,False,P),$FFFFFFFF);<br>
 
感谢各位的回答,问题我已经解决了,是dll全局变量共享的问题,只要添加一个文件映射函数就行了
 
可以贴出你的文件映射函数吗,我也遇到相同的问题<br>http://www.delphibbs.com/delphibbs/dispq.asp?lid=2492079<br>我是想把用钩子记录下的窗口标题栏名称保存到dll所在的文件夹<br>一开始,我在shellproc用path获得dll所在的文件夹的路径,path := GetCurrentDir;<br>结果当我打开一个网页时, &nbsp; &nbsp;<br>path:= H:/program files/internet explorer/IEXPLORE.EXE<br><br>为此我到EnableHotKeyHook:中,全局变量path := GetCurrentDir;<br>的确获得dll所在的文件夹的路径,但是通过全局变量path传不进shellproc<br>在shellproc中path 未改变,仍为空串<br><br>unit HKProc;<br><br>interface<br><br>uses<br>&nbsp;Windows, Messages, Dialogs, SysUtils, TLHelp32;<br><br>var<br>&nbsp;hNextHookProc: HHook;<br>&nbsp;procSaveExit: Pointer;<br>&nbsp;userName, path : string;<br>&nbsp;lpBuffer :array[1..64] of Char;<br>&nbsp;nSize :Cardinal;<br>&nbsp;myfile : textfile;<br><br>function shellproc(iCode: Integer;<br>&nbsp;wParam: WPARAM;<br>&nbsp;lParam: LPARAM): LRESULT; stdcall; export;<br>function EnableHotKeyHook: BOOL; export;<br>function DisableHotKeyHook: BOOL; export;<br>procedure HotKeyHookExit; far;<br><br><br>implementation<br><br>function shellproc(iCode: Integer;<br>&nbsp;wParam: WPARAM;<br>&nbsp;lParam: LPARAM): LRESULT; stdcall; export;<br>var<br>&nbsp;szTitle:string; &nbsp; &nbsp; //当前窗口名称<br>&nbsp;temp : TDateTime;<br>&nbsp;processPath:string;<br>&nbsp;Handler : THandle;<br>begin<br>&nbsp;Result := 0;<br>&nbsp;If iCode &lt; 0 Then<br>&nbsp;begin<br>&nbsp; &nbsp;Result := CallNextHookEx(hNextHookProc, iCode, wParam, lParam);<br>&nbsp; &nbsp;Exit;<br>&nbsp;end<br>&nbsp;else if iCode=HSHELL_WINDOWCREATED &nbsp; then<br>&nbsp;begin<br>&nbsp; &nbsp;SetLength(szTitle, 255);<br>&nbsp; &nbsp;SetLength(szTitle, GetWindowText(wParam,pchar(szTitle),254));<br><br>{ &nbsp; &nbsp;nSize := 255;<br>&nbsp; &nbsp;GetUserName(@lpBuffer, nSize);<br>&nbsp; &nbsp;userName := Copy(lpBuffer, 1, nSize-1);<br>&nbsp; &nbsp;path := GetCurrentDir;<br>&nbsp; &nbsp;path:=path + '/' + userName + DateToStr(now()) +'.txt';<br>&nbsp; &nbsp;AssignFile(myfile, 'E:/delphiProgram/managerComputer/windowtext.txt');<br>&nbsp; &nbsp;append(myfile);<br>&nbsp; &nbsp;Writeln(myfile, Path );<br>&nbsp; &nbsp;CloseFile(myfile);<br>}<br><br>&nbsp; &nbsp;if length(path) &lt;= 0 then<br>&nbsp; &nbsp; &nbsp;path := 'null';<br>&nbsp; &nbsp;AssignFile(myfile, 'E:/delphiProgram/managerComputer/windowtext.txt');<br>&nbsp; &nbsp;append(myfile);<br>&nbsp; &nbsp;Writeln(myfile, Path );<br>&nbsp; &nbsp;CloseFile(myfile);<br><br>&nbsp;end;<br>end;<br><br><br>function EnableHotKeyHook: BOOL; export;<br>begin<br>&nbsp; &nbsp;nSize := 255;<br>&nbsp; &nbsp;GetUserName(@lpBuffer, nSize);<br>&nbsp; &nbsp;userName := Copy(lpBuffer, 1, nSize-1);<br>&nbsp; &nbsp;path := GetCurrentDir;<br>&nbsp; &nbsp;path:=path + '/' + userName + DateToStr(now()) +'.txt';<br>&nbsp; &nbsp;AssignFile(myfile, 'E:/delphiProgram/managerComputer/windowtext.txt');<br>&nbsp; &nbsp;append(myfile);<br>&nbsp; &nbsp;Writeln(myfile, Path );<br>&nbsp; &nbsp;CloseFile(myfile);<br><br>&nbsp;Result := False;<br>&nbsp;if hNextHookProc &lt;&gt; 0 then Exit;<br>&nbsp;// 挂上 WH_KEYBOARD 这型的 HOOK, 同时, 传回值必须保留下<br>&nbsp;// 来, 免得 HOOK 呼叫链结断掉<br>&nbsp;hNextHookProc := SetWindowsHookEx(WH_SHELL,<br>&nbsp; &nbsp;shellproc,<br>&nbsp; &nbsp;HInstance,<br>&nbsp; &nbsp;0);<br>&nbsp;Result := hNextHookProc &lt;&gt; 0;<br>end;<br><br><br>function DisableHotKeyHook: BOOL; export;<br>begin<br>&nbsp;if hNextHookProc &lt;&gt; 0 then<br>&nbsp;begin<br>&nbsp; &nbsp;UnhookWindowshookEx(hNextHookProc); &nbsp;// 解除 Keyboard Hook<br>&nbsp; &nbsp;hNextHookProc := 0;<br>&nbsp; &nbsp;MessageBeep(0);<br>&nbsp; &nbsp;MessageBeep(0);<br>&nbsp;end;<br>&nbsp;Result := hNextHookProc = 0;<br>end;<br><br>procedure HotKeyHookExit;<br>begin<br>&nbsp;// 如果忘了解除 HOOK, 自动代理解除的动作<br>&nbsp;if hNextHookProc &lt;&gt; 0 then DisableHotKeyHook;<br>&nbsp;ExitProc := procSaveExit;<br>end;<br><br>end.<br><br><br><br>
 
procedure MyDLLHandler(Reason: Integer);<br>var<br>&nbsp; FHandle: LongWORD;<br>begin<br>&nbsp; case Reason of<br>&nbsp; &nbsp; DLL_PROCESS_ATTACH:<br>&nbsp; &nbsp; begin &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//建立文件映射,以实现DLL中的全局变量<br>&nbsp; &nbsp; &nbsp; FHandle := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE, 0, $ffff, 'MYDLLDATA');<br>&nbsp; &nbsp; &nbsp; if FHandle = 0 then<br>&nbsp; &nbsp; &nbsp; if GetLastError = ERROR_ALREADY_EXISTS then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; FHandle := OpenFileMapping(FILE_MAP_ALL_ACCESS, False, 'MYDLLDATA');<br>&nbsp; &nbsp; &nbsp; &nbsp; if FHandle = 0 then Exit;<br>&nbsp; &nbsp; &nbsp; end else Exit;<br>&nbsp; &nbsp; &nbsp; hwnd := MapViewOfFile(FHandle, FILE_MAP_ALL_ACCESS, 0, 0, 0);<br>&nbsp; &nbsp; &nbsp; if hwnd = nil then<br>&nbsp; &nbsp; &nbsp; &nbsp; CloseHandle(FHandle);<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; DLL_PROCESS_DETACH:<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; if Assigned(hwnd) then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; UnmapViewOfFile(hwnd);<br>&nbsp; &nbsp; &nbsp; &nbsp; hwnd := nil;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;<br>这段代码我是从CSDN上看来的,只要把你需要的全局变量包装成一个结构再把结构的<br>指针代替hwnd就行了,很方便希望对你有帮助
 
用钩子记录下打开过的所有的网页窗口标题栏名称 <br>这是我的代码,有问题,可以截获WM_SETTEXT消息,但是不知如何进一步得到窗口标题栏名称<br>function CallWndProc(iCode: Integer;<br>wParam: WPARAM;<br>lParam: LPARAM): LRESULT; stdcall; export;<br>var<br>Wnd: hWnd;<br>WndText: string;<br>szTitle:string; &nbsp; &nbsp; //当前窗口名称<br>data : TModuleEntry32;<br>myfile : textfile;<br>temp : TDateTime;<br>processPath:string;<br>processID:integer;<br>Handler : THandle;<br>msg:Teventmsg;<br>str:PChar;<br>i : integer;<br>begin<br>Result := 0;<br>If iCode &lt; 0 Then<br>begin<br>&nbsp; Result := CallNextHookEx(hNextHookProc, iCode, wParam, lParam);<br>&nbsp; Exit;<br>end<br>&nbsp;else if iCode=HC_ACTION then<br>&nbsp;begin<br>&nbsp; &nbsp;msg:=peventmsg(lParam)^;<br>&nbsp; &nbsp;if msg.message=WM_SETTEXT then<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp;str := pchar(msg.paramL);<br>&nbsp; &nbsp; &nbsp;i := 1;<br>&nbsp; &nbsp; &nbsp;while str &lt;&gt; #0 do<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp;szTitle := str;<br>&nbsp; &nbsp; &nbsp; &nbsp;i := i+1;<br>&nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp;szTitle := #0;<br>&nbsp; &nbsp; &nbsp;if length(szTitle)&lt;=0 then<br>&nbsp; &nbsp; &nbsp; szTitle := 'null';<br>&nbsp; &nbsp; AssignFile(myfile, 'E:/delphiProgram/managerComputer/windowtext.txt');<br>&nbsp; &nbsp; append(myfile);<br>&nbsp; &nbsp; Writeln(myfile, FormatDateTime('c', now()) +' '+ szTitle +' ');// + processPath<br>&nbsp; &nbsp; Writeln(myfile, #13);<br>&nbsp; &nbsp; CloseFile(myfile);<br>end;<br>end;<br><br><br>function EnableHotKeyHook: BOOL; export;<br>begin<br>Result := False;<br>if hNextHookProc &lt;&gt; 0 then Exit;<br>// 挂上 WH_KEYBOARD 这型的 HOOK, 同时, 传回值必须保留下<br>// 来, 免得 HOOK 呼叫链结断掉<br>hNextHookProc := SetWindowsHookEx(WH_CALLWNDPROC,<br>&nbsp; CallWndProc,<br>&nbsp; HInstance,<br>&nbsp; 0);<br>Result := hNextHookProc &lt;&gt; 0;<br>end;<br><br>可以指点一下吗<br>
 
str := pchar(msg.paramL);??<br>应该是msg,Lparam<br>另外不用把pchar转换成string,可以直接写入textfile的<br><br><br><br>&nbsp; &nbsp;
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=2532183
 
后退
顶部