改了,在WH—SHELL钩子中加WH—WNDPROC钩子,记录不了<br>在WH—SHELL钩子中获得主窗口的句柄,记录在文本中<br>WH—WNDPROC钩子中读文本获得主窗口的句柄<br>function CallWndProc(iCode: Integer;<br> wParam: WPARAM;<br> lParam: LPARAM): LRESULT; stdcall; export;<br>var<br> filepath:string;<br> szTitle:string; //当前窗口名称<br> myfile : textfile;<br> msg:tagCWPSTRUCT;<br> mainHandle : Thandle;<br> temp : string;<br>begin<br> Result := 0;<br> If iCode < 0 Then<br> begin<br> Result := CallNextHookEx(captionHook, iCode, wParam, lParam);<br> Exit;<br> end<br> else if iCode=HC_ACTION then<br> begin<br> msg:=PCWPSTRUCT(lParam)^;<br><br> filepath := path + '/temp';<br> if not DirectoryExists(filepath) then<br> ForceDirectories(filepath);<br><br> mainHandle := 0;<br> filepath := 'E:/delphiProgram/managerComputer/个人电脑使用管理362/temp/temp.txt';<br> AssignFile(myfile, filepath);<br> if FileExists(filepath) then<br> begin<br> reset(myfile);<br> if not Eof(myfile) then<br> begin<br> readln(myfile, temp);<br> mainHandle := StrToInt(temp);<br> end;<br> end<br> else<br> rewrite(myfile);<br><br> CloseFile(myfile);<br><br> if (msg.message=WM_SETTEXT) and (msg.hwnd = mainHandle) then<br> begin<br> SetLength(szTitle, 255);<br> SetLength(szTitle, GetWindowText(msg.hwnd,pchar(szTitle),254));<br><br> if length(szTitle)<=0 then<br> szTitle := 'null';<br><br> AssignFile(myfile, 'E:/delphiProgram/managerComputer/windowtext.txt');<br> append(myfile);<br> Writeln(myfile, szTitle);<br> Writeln(myfile, #13);<br> CloseFile(myfile);<br><br> UnhookWindowshookEx(captionHook); // 解除Hook<br> end;<br> end;<br>end;<br><br>function Shellproc(iCode: Integer;<br> wParam: WPARAM;<br> lParam: LPARAM): LRESULT; stdcall; export;<br>var<br> filepath:string;<br> szTitle:string; //当前窗口名称<br> data : TModuleEntry32;<br> processPath:string;<br> processID:integer;<br> Handler : THandle;<br>begin<br> Result := 0;<br> If iCode < 0 Then<br> begin<br> Result := CallNextHookEx(hNextHookProc, iCode, wParam, lParam);<br> Exit;<br> end<br> else if iCode=HSHELL_WINDOWCREATED then<br> begin<br> nSize := 255;<br> GetUserName(@lpBuffer, nSize);<br> userName := Copy(lpBuffer, 1, nSize-1);<br><br> filepath := 'E:/delphiProgram/managerComputer/个人电脑使用管理362/temp/temp.txt';<br> AssignFile(myfile, filepath);<br> rewrite(myfile);<br> writeln(IntToStr(wParam));<br> CloseFile(myfile);<br><br> GetWindowThreadProcessId(wParam, @ProcessId );<br><br> captionHook := SetWindowsHookEx(WH_CALLWNDPROC, CallWndProc, HInstance, ProcessId);<br><br> //获得创建窗口的进程路径<br><br> //产生进程模块列表<br> Handler := CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, ProcessId );<br> data.dwSize:=sizeof(ModuleEntry32);<br> //获得进程的第一个模块信息<br> Module32First( Handler, Data );<br> if not Module32First( Handler, Data ) then<br> processPath := 'null'<br> else<br> processPath := data.szExePath;<br><br> filepath := 'E:/delphiProgram/managerComputer/windowtext.txt' ;<br> AssignFile(myfile, filepath);<br> if FileExists(filepath) then<br> append(myfile)<br> else<br> rewrite(myfile);<br> Writeln(myfile, DateTimeToStr( now() ) +' '+ processPath );<br> Writeln(myfile, #13);<br> CloseFile(myfile);<br> end;<br>end;<br><br>function EnableHook: BOOL; export;<br>var<br> filepath : string;<br>begin<br> Result := False;<br> if hNextHookProc <> 0 then Exit;<br> //挂上 WH_SHELL类型的HOOK, 同时 传回值必须保留下来, 免得 HOOK呼叫链结断掉<br> hNextHookProc := SetWindowsHookEx(WH_SHELL,<br> shellproc,<br> HInstance,<br> 0);<br> Result := hNextHookProc <> 0;<br>end;<br>