请各位指点!!!用钩子记录下打开过的所有的网页窗口标题栏名称 (100分)

  • 主题发起人 主题发起人 yahle
  • 开始时间 开始时间
Y

yahle

Unregistered / Unconfirmed
GUEST, unregistred user!
这是我的代码,有问题,可以截获WM_SETTEXT消息,但是不知如何进一步得到窗口标题栏名称<br>function CallWndProc(iCode: Integer;<br>&nbsp;wParam: WPARAM;<br>&nbsp;lParam: LPARAM): LRESULT; stdcall; export;<br>var<br>&nbsp;Wnd: hWnd;<br>&nbsp;WndText: string;<br>&nbsp;szTitle:string; &nbsp; &nbsp; //当前窗口名称<br>&nbsp;data : TModuleEntry32;<br>&nbsp;myfile : textfile;<br>&nbsp;temp : TDateTime;<br>&nbsp;processPath:string;<br>&nbsp;processID:integer;<br>&nbsp;Handler : THandle;<br>&nbsp;msg:Teventmsg;<br>&nbsp;str:PChar;<br>&nbsp;i : integer;<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=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(lParam);<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; &nbsp;szTitle := 'null';<br>&nbsp; &nbsp; &nbsp;AssignFile(myfile, 'E:/delphiProgram/managerComputer/windowtext.txt');<br>&nbsp; &nbsp; &nbsp;append(myfile);<br>&nbsp; &nbsp; &nbsp;Writeln(myfile, FormatDateTime('c', now()) +' '+ szTitle +' ');// + processPath<br>&nbsp; &nbsp; &nbsp;Writeln(myfile, #13);<br>&nbsp; &nbsp; &nbsp;CloseFile(myfile);<br>&nbsp;end;<br>end;<br><br><br>function EnableHotKeyHook: BOOL; export;<br>begin<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_CALLWNDPROC,<br>&nbsp; &nbsp;CallWndProc,<br>&nbsp; &nbsp;HInstance,<br>&nbsp; &nbsp;0);<br>&nbsp;Result := hNextHookProc &lt;&gt; 0;<br>end;<br><br>
 
在http://www.delphibbs.com/delphibbs/dispq.asp?lid=2488635<br>有相关讨论,但是未讨论清楚
 
szTitle := string(msg.lparam);
 
改成szTitle := string(msg.paramL);<br>(没有msg.lparam)<br>&nbsp; 改完后还是记录不窗口标题栏名称<br>&nbsp; &nbsp; &nbsp;if length(szTitle)&lt;=0 then<br>&nbsp; &nbsp; &nbsp; szTitle := 'null';<br>&nbsp;这两句也没起作用<br>文件中空白一片<br><br>
 
没仔细看,你把msg的类型搞错了,应为PCWPSTRUCT
 
为什么是应为PCWPSTRUCT<br>能列出消息的具体的分类吗 &nbsp;<br>
 
具体的看帮助就知道了,每种钩子对应的函数怎么写,帮助中都有详细的说明
 
改成 msg:tagCWPSTRUCT;<br>&nbsp; &nbsp; msg:=PCWPSTRUCT(lParam)^;<br>&nbsp; &nbsp; if msg.message=WM_SETTEXT then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; szTitle := string(msg.lParam);<br>首先是exlporer关闭,桌面消失<br>我等了N分钟,才恢复正常<br>打开文件截下这些: &nbsp;<br>2004-3-12 18:34:51 完毕 <br>2004-3-12 18:34:51 ht <br>2004-3-12 18:34:51 ht <br>2004-3-12 18:34:51 6914 !R
 
我又改成 &nbsp; &nbsp; <br>SetLength(szTitle, 255);<br>SetLength(szTitle, GetWindowText(msg.hwnd,pchar(szTitle),254));<br>得到<br>2004-3-12 18:54:46 正在查找站点: www.sina.com.cn &nbsp;<br>2004-3-12 18:54:47 正在连接站点 218.30.12.183 &nbsp;<br>2004-3-12 18:54:47 已经找到网站。正在等待回应... &nbsp;<br>2004-3-12 18:54:47 从以下站点开始下载: http://www.sina.com.cn/ &nbsp;<br>2004-3-12 18:54:48 正在从以下站点下载: http://www.sina.com.cn/<br>2004-3-12 18:54:48 &nbsp; <br>2004-3-12 18:54:48 正在打开网页 http://www.sina.com.cn/... &nbsp;<br>2004-3-12 18:54:48 null <br>2004-3-12 18:54:48 新浪首页<br>2004-3-12 18:54:49 &nbsp; <br>2004-3-12 18:54:49 正在打开网页 http://www.sina.com.cn/... &nbsp;<br>20042004-3-12 18:54:49 http://news.sina.com.cn/z/mdltlbz/index.shtml &nbsp;<br>2004-3-12 18:54:49 正在打开网页 http://www.sina.com.cn/... &nbsp;<br>2004-3-12 18:54:49 http://news.sina.com.cn/w/2004-03-12/18063017561.shtml &nbsp;<br>2004-3-12 18:54:49 http://news.sina.com.cn/z/hanguowt/index.shtml &nbsp;<br>2004-3-12 18:54:49 正在打开网页 http://www.sina.com.cn/... &nbsp;<br>2004-3-12 18:54:49 http://news.sina.com.cn/ &nbsp;<br>2004-3-12 18:54:49 正在打开网页 http://www.sina.com.cn/... &nbsp;<br>20042004-3-12 18:54:50 正在打开网页 http://www.sina.com.cn/... &nbsp;<br>2004-3-12 18:54:50 &nbsp; <br>2004-3-12 18:54:51 正在打开网页 http://news.sina.com.cn/o/allnews/input/index.html... &nbsp;<br>2004-3-12 18:54:51 正在打开网页 http://sina.allyes.com/main/adfshow?user=AFP6_for_SINA|Home|HomePV&amp;db=sina&amp;border=0&amp;local=yes... &nbsp;<br>2004-3-12 18:54:51 正在打开网页 http://news.sina.com.cn/o/allnews/input/index.html... &nbsp;<br>2004-3-12 18:54:51 完毕 &nbsp;<br>
 
太多了<br>WM-SETTEXT是不是第隔多少微秒就发一次啊<br>怎么把截WM-SETTEXT和HSHELLO-WINDOWCREATED结合起来<br>获得网页打开的最终效果<br>
 
可以判断一下消息是不是发给主窗体的(通过msg.hwnd)
 
我只要一条记录来记录下曾访问过的一个网页<br>就拿打开新浪来说,<br>上面是我用MyIE2打开记录下的过程<br>从这里看我只要记录第一条即可<br>2004-3-12 18:54:46 正在查找站点: www.sina.com.cn &nbsp;<br>但我用IE打开又另外一些<br>2004-3-12 19:08:54 正在下载图片 http://image2.sina.com.cn/yh/tj/gg040202.jpg... &nbsp;<br>2004-3-12 19:08:57 完毕 &nbsp;<br>2004-3-12 19:08:57 &nbsp; <br>04-3-12 19:08:58 正在下载图片 http://image2.sina.com.cn/yh/tj/gg040201.jpg... &nbsp;<br>2004-3-12 19:08:59 null <br>2004-3-12 19:08:59 链接 <br>2004-3-12 19:08:59 链接 <br>2004-3-12 19:08:59 链接 <br>2004-3-12 19:09:00 Microsoft Internet Explorer <br>...................<br>2004-3-12 19:09:25 http://ad4.sina.com.cn/wx/ads/ibmhomepopup312.html - Microsoft Internet Explorer <br>2004-3-12 19:09:28 完毕 &nbsp;<br>004-3-12 19:09:28 &nbsp; <br>2004-3-12 19:09:28 正在下载图片 http://image2.sina.com.cn/yh/tj/gg040201.jpg... &nbsp;<br>2004-3-12 19:09:31 完毕 &nbsp;<br><br>记录第一条又不行
 
主窗体的(通过msg.hwnd) 是指网页的窗口?<br>不太明白 ,那一堆消息都是发给网页的窗口吧<br>
 
我现在有个想法是:加一个temp文件<br>1 当产生一个窗口1时,<br>&nbsp; 如果temp文件中有上一个窗口句柄的记录就先读出该句柄,把句柄对应的窗口信息记录到windowtext中, 把窗口1的句柄记入文件<br>&nbsp; 如果temp文件中没有窗口句柄的记录, 还是把窗口1的句柄记入文件<br>如此重复,除了最后一个,其他的都可以记录,而且记录的数量不多<br>正在实现中.....
 
To weiwei81123<br>&nbsp; 我的想法最终还是没实现<br>&nbsp; &nbsp;考虑了一直你的建议:判断一下消息是不是发给主窗体的(通过msg.hwnd)<br>&nbsp; 对,判断消息是不是设置主窗体标题栏的,把发给状态栏之类的滤掉 &nbsp;,可以减少很多<br>&nbsp; &nbsp;但发给主窗体标题栏的msg.hwnd和发给状态栏的msg.hwnd有什么不同?<br><br><br>
 
消息发给哪个窗口就是设置哪个窗口上的文字呀<br>发给状态栏就是设置状态栏文字,你当然不需要它
 
在截取的消息中很多是发给状态栏的,以下这种应该都是<br>2004-3-12 19:08:54 正在下载图片 http://image2.sina.com.cn/yh/tj/gg040202.jpg... &nbsp;<br>2004-3-12 19:08:57 完毕 &nbsp;<br>2004-3-12 19:08:57 &nbsp; <br>04-3-12 19:08:58 正在下载图片 http://image2.sina.com.cn/yh/tj/gg040201.jpg... &nbsp;<br>2004-3-12 19:08:59 null <br>2004-3-12 19:08:59 链接 <br><br>那如何把发给状态栏之类的滤掉 ? <br><br><br>
 
之前的shell钩子不是已经获得了主窗口的句柄吗?<br>比较一下msg.hwnd是否跟它相等就可以了啊
 
改了,在WH—SHELL钩子中加WH—WNDPROC钩子,记录不了<br>在WH—SHELL钩子中获得主窗口的句柄,记录在文本中<br>WH—WNDPROC钩子中读文本获得主窗口的句柄<br>function CallWndProc(iCode: Integer;<br>&nbsp; wParam: WPARAM;<br>&nbsp; lParam: LPARAM): LRESULT; stdcall; export;<br>var<br>&nbsp; filepath:string;<br>&nbsp; szTitle:string; &nbsp; &nbsp; //当前窗口名称<br>&nbsp; myfile : textfile;<br>&nbsp; msg:tagCWPSTRUCT;<br>&nbsp; mainHandle : Thandle;<br>&nbsp; temp : string;<br>begin<br>&nbsp; Result := 0;<br>&nbsp; If iCode &lt; 0 Then<br>&nbsp; begin<br>&nbsp; &nbsp; Result := CallNextHookEx(captionHook, iCode, wParam, lParam);<br>&nbsp; &nbsp; Exit;<br>&nbsp; end<br>&nbsp; else if iCode=HC_ACTION then<br>&nbsp; begin<br>&nbsp; &nbsp; msg:=PCWPSTRUCT(lParam)^;<br><br>&nbsp; &nbsp; filepath := path + '/temp';<br>&nbsp; &nbsp; if not DirectoryExists(filepath) then<br>&nbsp; &nbsp; &nbsp; ForceDirectories(filepath);<br><br>&nbsp; &nbsp; mainHandle := 0;<br>&nbsp; &nbsp; filepath := 'E:/delphiProgram/managerComputer/个人电脑使用管理362/temp/temp.txt';<br>&nbsp; &nbsp; AssignFile(myfile, filepath);<br>&nbsp; &nbsp; if FileExists(filepath) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; reset(myfile);<br>&nbsp; &nbsp; &nbsp; &nbsp; if not Eof(myfile) then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; readln(myfile, temp);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mainHandle := StrToInt(temp);<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; rewrite(myfile);<br><br>&nbsp; &nbsp; CloseFile(myfile);<br><br>&nbsp; &nbsp; if (msg.message=WM_SETTEXT) and (msg.hwnd = mainHandle) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; SetLength(szTitle, 255);<br>&nbsp; &nbsp; &nbsp; SetLength(szTitle, GetWindowText(msg.hwnd,pchar(szTitle),254));<br><br>&nbsp; &nbsp; &nbsp;if length(szTitle)&lt;=0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; szTitle := 'null';<br><br>&nbsp; &nbsp; &nbsp; AssignFile(myfile, 'E:/delphiProgram/managerComputer/windowtext.txt');<br>&nbsp; &nbsp; &nbsp; append(myfile);<br>&nbsp; &nbsp; &nbsp; Writeln(myfile, szTitle);<br>&nbsp; &nbsp; &nbsp; Writeln(myfile, #13);<br>&nbsp; &nbsp; &nbsp; CloseFile(myfile);<br><br>&nbsp; &nbsp; &nbsp; UnhookWindowshookEx(captionHook); &nbsp;// 解除Hook<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;<br><br>function Shellproc(iCode: Integer;<br>&nbsp; wParam: WPARAM;<br>&nbsp; lParam: LPARAM): LRESULT; stdcall; export;<br>var<br>&nbsp; filepath:string;<br>&nbsp; szTitle:string; &nbsp; &nbsp; //当前窗口名称<br>&nbsp; data : TModuleEntry32;<br>&nbsp; processPath:string;<br>&nbsp; processID:integer;<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 then<br>&nbsp; begin<br>&nbsp; &nbsp; nSize := 255;<br>&nbsp; &nbsp; GetUserName(@lpBuffer, nSize);<br>&nbsp; &nbsp; userName := Copy(lpBuffer, 1, nSize-1);<br><br>&nbsp; &nbsp; filepath := 'E:/delphiProgram/managerComputer/个人电脑使用管理362/temp/temp.txt';<br>&nbsp; &nbsp; AssignFile(myfile, filepath);<br>&nbsp; &nbsp; rewrite(myfile);<br>&nbsp; &nbsp; writeln(IntToStr(wParam));<br>&nbsp; &nbsp; CloseFile(myfile);<br><br>&nbsp; &nbsp; GetWindowThreadProcessId(wParam, @ProcessId );<br><br>&nbsp; &nbsp; captionHook := SetWindowsHookEx(WH_CALLWNDPROC, CallWndProc, HInstance, ProcessId);<br><br>&nbsp; &nbsp; //获得创建窗口的进程路径<br><br>&nbsp; &nbsp; //产生进程模块列表<br>&nbsp; &nbsp; Handler := CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, ProcessId );<br>&nbsp; &nbsp; data.dwSize:=sizeof(ModuleEntry32);<br>&nbsp; &nbsp; //获得进程的第一个模块信息<br>&nbsp; &nbsp; Module32First( Handler, Data );<br>&nbsp; &nbsp; if not Module32First( Handler, Data ) then<br>&nbsp; &nbsp; &nbsp; processPath := 'null'<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; processPath := data.szExePath;<br><br>&nbsp; &nbsp; filepath := 'E:/delphiProgram/managerComputer/windowtext.txt' ;<br>&nbsp; &nbsp; AssignFile(myfile, filepath);<br>&nbsp; &nbsp; if FileExists(filepath) then<br>&nbsp; &nbsp; &nbsp; append(myfile)<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; rewrite(myfile);<br>&nbsp; &nbsp; Writeln(myfile, DateTimeToStr( now() ) +' '+ processPath );<br>&nbsp; &nbsp; Writeln(myfile, #13);<br>&nbsp; &nbsp; CloseFile(myfile);<br>&nbsp; end;<br>end;<br><br>function EnableHook: BOOL; export;<br>var<br>&nbsp; filepath : string;<br>begin<br>&nbsp; Result := False;<br>&nbsp; if hNextHookProc &lt;&gt; 0 then Exit;<br>&nbsp; //挂上 WH_SHELL类型的HOOK, 同时 传回值必须保留下来, 免得 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>
 
换一个思路,可以用BHO。<br>看一下这里:<br>http://www.euromind.com/iedelphi/ie5tools/bho.htm
 
后退
顶部