知道进程ID,如何得到主窗口句柄? ( 积分: 90 )

  • 主题发起人 主题发起人 joephoenix
  • 开始时间 开始时间
J

joephoenix

Unregistered / Unconfirmed
GUEST, unregistred user!
知道进程ID,如何得到主窗口句柄?要代码~~~~谢谢~~~<br>另外:CreateToolHelp32Snapshot创建快照后,能不能得到指定EXE的主窗口句柄?如果可以,要怎样写吖?
 
OpenProcess啊!可以根据id得到handle
 
openprocess得到的不是窗口的handle吧?因为我要用showwindow函数,所以必需得到主窗口的句柄。。。
 
function&nbsp;EnumProc(hWnd:THandle;PID:LPARAM):BOOL;stdcall;<br>var<br>&nbsp;&nbsp;dwWndPID:DWORD;<br>begin<br>&nbsp;&nbsp;Result:=&nbsp;True;<br>&nbsp;&nbsp;GetWindowThreadProcessId(hWnd,&nbsp;dwWndPID);<br>&nbsp;&nbsp;if&nbsp;dwWndPID&nbsp;=&nbsp;PID&nbsp;then<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;Result:=&nbsp;False;<br>&nbsp;&nbsp;&nbsp;&nbsp;ShowMessage(IntToStr(hWnd));<br>&nbsp;&nbsp;end;<br>end;<br>procedure&nbsp;TForm1.Button1Click(Sender:&nbsp;TObject);<br>var<br>&nbsp;&nbsp;TargetPID:DWORD;<br>begin<br>&nbsp;&nbsp;TargetPID:=&nbsp;2040;<br>&nbsp;&nbsp;EnumWindows(@EnumProc,TargetPID);<br>end;<br>===========<br>这段代码就可以知道PID为2040的进程的主窗口的Handle,并显示出来了,楼主可自行修改
 
CreateToolHelp32Snapshot&nbsp;+&nbsp;<br>Process32First&nbsp;+<br>Process32Next<br><br>比较如下结构的结果即可,可以得到句柄,可以得到路径<br><br>typedef&nbsp;struct&nbsp;tagPROCESSENTRY32&nbsp;{&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;DWORD&nbsp;dwSize;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;DWORD&nbsp;cntUsage;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;DWORD&nbsp;th32ProcessID;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;DWORD&nbsp;th32DefaultHeapID;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;DWORD&nbsp;th32ModuleID;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;DWORD&nbsp;cntThreads;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;DWORD&nbsp;th32ParentProcessID;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;LONG&nbsp;&nbsp;pcPriClassBase;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;DWORD&nbsp;dwFlags;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;szExeFile[MAX_PATH];&nbsp;<br>}&nbsp;PROCESSENTRY32;&nbsp;<br>typedef&nbsp;PROCESSENTRY32&nbsp;*&nbsp;&nbsp;PPROCESSENTRY32;&nbsp;<br>typedef&nbsp;PROCESSENTRY32&nbsp;*&nbsp;&nbsp;LPPROCESSENTRY32;
 
ztf86781163<br>想问一下哪个是主窗口句柄???
 
接受答案了.
 
后退
顶部