已知进程句柄,如何知道此进程的主窗口句柄

hcbj2002 2005-06-12 01:04:43
1.已知进程句柄,如何获得此进程的主窗口的句柄.
2.已经主窗口句柄如何获取当前进程


b := CreateProcess('c:\windows\System32\calc.exe'), //lpApplicationName: PChar
nil, //lpCommandLine: PChar
nil, //lpProcessAttributes: PSecurityAttributes
nil, //lpThreadAttributes: PSecurityAttributes
True, //bInheritHandles: BOOL
CREATE_NEW_CONSOLE,
nil,
PChar(Ddir),
StartInfo,
ProceInfo);

我现在通过Createprocess创进了一个进程,可我现在想知道calc.exe的主窗口句柄,如何操作?
...全文
992 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
柯本 2005-06-12
  • 打赏
  • 举报
回复
先枚举全部窗口,在枚举回调函数中调用GetWindowThreadProcessID()得到窗口的进程ID与以前得到的ID比较,如果不一制,不处理,如一样,循环调用GetParent()一直到返回NULL,最后的hwnd既为顶层窗口句柄。
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam )
{
unsigned long id;
HWND thwnd;

id=GetWindowThreadProcessId(hwnd,NULL);
if (id==lParam)
{
while((thwnd=GetParent(hwnd))!=NULL)
hwnd=thwnd;
CString x;
x.Format("HWND = %x",hwnd);
MessageBox(NULL,x,NULL,MB_OK);
return false;
}
return true;
}


void CMt2Dlg::OnButton1()
{
// TODO: Add your control notification handler code here
STARTUPINFO StartInfo;
PROCESS_INFORMATION ProceInfo;
ZeroMemory(&StartInfo,sizeof(StartInfo));
StartInfo.cb=sizeof(StartInfo);

CreateProcess(NULL, //lpApplicationName: PChar
"calc.exe", //lpCommandLine: PChar
NULL, //lpProcessAttributes: PSecurityAttributes
NULL, //lpThreadAttributes: PSecurityAttributes
true, //bInheritHandles: BOOL
CREATE_NEW_CONSOLE,
NULL,
NULL,
&StartInfo,
&ProceInfo);

Sleep(100); //这是必须的,因为 CreateProcess 不能马上Active windows
EnumWindows(EnumWindowsProc,ProceInfo.dwThreadId);
cnwolf 2005-06-12
  • 打赏
  • 举报
回复
1已知进程句柄,如何获得此进程的主窗口的句柄.
偶也想知道

2.已经主窗口句柄如何获取当前进程
The GetWindowThreadProcessId function retrieves the identifier of the thread that created the specified window and, optionally, the identifier of the process that created the window.

DWORD GetWindowThreadProcessId(
HWND hWnd, // handle to window
LPDWORD lpdwProcessId // process identifier
);

15,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧