*★已知某一个进程句柄,如何得到此进程下所有打开窗体的句柄? ( 积分: 200 )

没有程序?<br>有个RAR压缩包呀,如果不能下载....那真是RPWT了
 
唉,GetProcessID的代码你上面不是有吗?<br>uses<br> &nbsp;TlHelp32;<br><br>function GetProcessID(lpszProcName: PChar): DWORD ;<br>var<br> &nbsp;hSnapShot:THandle;<br> &nbsp;ProcessEntry:pROCESSENTRY32;<br> &nbsp;bFlag:BOOL;<br>begin<br> &nbsp;Result:=0;<br> &nbsp;hSnapshot:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);<br> &nbsp;if(hSnapshot&amp;lt;&amp;gt;INVALID_HANDLE_VALUE) then<br> &nbsp;try<br> &nbsp; &nbsp;FillChar(ProcessEntry,Sizeof(PROCESSENTRY32),0);<br> &nbsp; &nbsp;ProcessEntry.dwSize:=Sizeof(PROCESSENTRY32);<br> &nbsp; &nbsp;bFlag:=Process32First(hSnapshot,ProcessEntry);<br> &nbsp; &nbsp;while bFlag do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;if Pos(UpperCase(lpszProcName),UpperCase(ProcessEntry.szExeFile)) &amp;gt; 0 then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Result:=ProcessEntry.th32ProcessID;<br> &nbsp; &nbsp; &nbsp; &nbsp;Break;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;ProcessEntry.dwSize:=sizeof(PROCESSENTRY32);<br> &nbsp; &nbsp; &nbsp;bFlag:=Process32Next(hSnapshot,ProcessEntry);<br> &nbsp; &nbsp;end;<br> &nbsp;finally<br> &nbsp; &nbsp;CloseHandle(hSnapshot);<br> &nbsp;end;<br>end;<br>
 
呵呵 &nbsp; 你看我这样写 还是不行的 &nbsp;我的系统是2003<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>function EnumWindowsProc(hwnd:HWND;lParam:LPARAM):BOOL;stdcall;<br>var<br> &nbsp;ProcID:DWORD;<br> &nbsp;szCaption:array[0..255] of Char;<br>begin<br> &nbsp;Result:=True;<br> &nbsp;GetWindowThreadProcessID(hwnd,ProcID);<br> &nbsp;if ProcID=dwProcessID then<br> &nbsp;begin<br> &nbsp; &nbsp;SendMessage(hwnd,WM_GETTEXT,255,Integer(@szCaption));<br> &nbsp; &nbsp;Form1.Memo1.Lines.Add(Format('Handle:%.5d &nbsp;Caption:%s',[hwnd,szCaption]));<br> &nbsp;end;<br>end;<br>Var<br> &nbsp;lppe: TProcessEntry32;<br> &nbsp;found: boolean;<br> &nbsp;Hand: THandle;<br>Begin<br> &nbsp;Hand := CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);<br> &nbsp;found := Process32First(Hand, lppe);<br> &nbsp;While found Do<br> &nbsp; &nbsp;Begin<br> &nbsp; &nbsp; &nbsp;ListBox1.Items.Add(StrPas(lppe.szExeFile)); //列出所有进程。<br> &nbsp; &nbsp; &nbsp;If StrPas(lppe.szExeFile) = 'delphi32.exe' Then<br> &nbsp; &nbsp; &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dwProcessID:=Hand;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EnumWindows(@EnumWindowsProc,0);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit;<br> &nbsp; &nbsp; &nbsp; &nbsp; end;<br> &nbsp; &nbsp; &nbsp;found := Process32Next(Hand, lppe);<br> &nbsp; &nbsp;End;<br>End;<br>
 
基本搞定 &nbsp;散
 
根据进程句柄,不能列举进程的窗口!!<br>根据进程ID列举进程所有窗口句柄的方法:用CreateToolhelp32Snapshot列举系统线程(一个进程有多个线程);对每个线程,如果其进程ID=规定的进程ID,则用EnumThreadWindows等列举线程窗口。<br>下面的程序运行结果与spyxx对照过,完全正确。<br>==============================<br>根据进程ID列举进程所有窗口句柄<br>==============================<br>unit Unit1;<br>interface<br>uses<br> &nbsp;Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> &nbsp;StdCtrls, TLHelp32;<br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;Button1: TButton;<br> &nbsp; &nbsp;ListBox1: TListBox;<br> &nbsp; &nbsp;Edit1: TEdit;<br> &nbsp; &nbsp;procedure Button1Click(Sender: TObject);<br> &nbsp; &nbsp;procedure MyENumChildWindows(h: HWND);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br>var<br> &nbsp;Form1: TForm1;<br>implementation<br>{$R *.DFM}<br>procedure TForm1.MyENumChildWindows(h: HWND);<br>var<br> &nbsp;h1: HWND;<br>begin<br> &nbsp;h1:=GetWindow(h,GW_CHILD);<br> &nbsp;While(h1&amp;gt;0) do<br> &nbsp;begin<br> &nbsp; &nbsp;ListBox1.Items.Add(IntToHex(h1,8));<br> &nbsp; &nbsp;if GetWindow(h1,GW_CHILD)&amp;gt;0 then MyENumChildWindows(h1);<br> &nbsp; &nbsp;h1:=GetWindow(h1,GW_HWNDNEXT);<br> &nbsp;end;<br>end;<br><br>function WNDENUMPROC(h: HWND; l: lParam): bool; StdCall;<br>begin<br> &nbsp;Form1.ListBox1.Items.Add(IntToHex(h,8));<br> &nbsp;Form1.MyENumChildWindows(h);<br> &nbsp;result:=true;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> &nbsp;Lpte: TThreadEntry32;<br> &nbsp;Found: boolean;<br> &nbsp;Handle: THandle;<br>begin<br> &nbsp;Handle:=CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD,0);<br> &nbsp;lpte.dwSize:=Sizeof(TThreadEntry32);<br> &nbsp;Found:=Thread32First(Handle,Lpte);<br> &nbsp;while Found do<br> &nbsp;begin<br> &nbsp; &nbsp;//如果线程的进程ID=规定的进程ID<br> &nbsp; &nbsp;if Lpte.th32OwnerProcessID=strtoint(edit1.text) then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;EnumThreadWindows(Lpte.th32ThreadID,@WNDENUMPROC,0);<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;Found:=Thread32Next(Handle,Lpte);<br> &nbsp;end;<br> &nbsp;CloseHandle(Handle);<br>end;<br><br>end.
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
顶部