如何确定使用 ShellExecuteEx 创建的进程是否拥有 window?
How to find out if process created with ShellExecuteEx owns a window?
我正在使用 ShellExecuteEx
到 运行 外部应用程序:
SHELLEXECUTEINFO shExInfo = { 0 };
shExInfo.cbSize = sizeof(shExInfo);
shExInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
shExInfo.hwnd = 0;
shExInfo.lpVerb = L"runas"; // Operation to perform
shExInfo.lpFile = windowStringContainingAppPath.c_str(); // Application to start
shExInfo.lpParameters = windowStringContainingAppParameters.c_str(); // Additional parameters
shExInfo.lpDirectory = 0;
shExInfo.nShow = SW_SHOW;
shExInfo.hInstApp = 0;
if(ShellExecuteEx(&shExInfo))
{
WaitForSingleObject(shExInfo.hProcess, INFINITE);
DeleteFile(wsMesh3dx64Parameter.c_str());
CloseHandle(shExInfo.hProcess);
}
一切正常,但此外部应用程序存在意外行为,即在关闭其主要 window 后,其进程仍然处于活动状态。
这可以防止 WaitForSingleObject(shExInfo.hProcess, INFINITE);
返回,我必须手动终止进程。
相反,我正在寻找一种方法来用一个循环替换 WaitForSingleObject(shExInfo.hProcess, INFINITE);
,该循环检查外部进程是否拥有 window,如果没有则终止它。
这是我想到的,如果有更好的方法请指点一下。
更新:
感谢 Robson 的回答,我成功地完成了我的计划:
struct Porcess_ID_HWND
{
DWORD processID;
HWND processhWnd;
};
BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam)
{
Porcess_ID_HWND*info = (Porcess_ID_HWND*)lParam;
DWORD processID;
GetWindowThreadProcessId(hWnd, &processID);
if (processID == info->processID){
info->processhWnd = hWnd;
return FALSE;
}
return TRUE;
}
我的循环:
if(ShellExecuteEx(&shExInfo))
{
DWORD dwProcessID = GetProcessId(shExInfo.hProcess);
Porcess_ID_HWND info;
info.processID = dwProcessID;
// wait for window to appear
do
{
info.processhWnd = NULL;
EnumWindows(EnumWindowsProc, (LPARAM)&info);
} while (!info.processhWnd);
// wait for window to close
do
{
info.processhWnd = NULL;
EnumWindows(EnumWindowsProc, (LPARAM)&info);
} while (info.processhWnd);
//WaitForSingleObject(shExInfo.hProcess, INFINITE);
DeleteFile(wsMesh3dx64Parameter.c_str());
CloseHandle(shExInfo.hProcess);
}
找到了一个很好的答案
1)
HAVE: Process ID, NEED: Process handle
Solution: OpenProcess()
2)
HAVE: Process handle, NEED: Process ID
Solution: GetProcessId()
3)
HAVE: Window handle, NEED: Process ID
Solution: GetWindowThreadProcessId()
4)
HAVE: Window handle, NEED: Process handle
Solution: Use 3) and then 1)
5)
HAVE: Process ID, NEED: Window handle
Solution: EnumWindows(), then in the callback function do 3) and check if it matches your process ID.
6)
HAVE: Process handle, NEED: Window handle
Solution: 2) and then 5)
所以你属于情况 6。那么如果没有 window 句柄的进程 ID 与你的 shExInfo.hProcess 的进程 ID 匹配,那么 shExInfo.hProcess 不拥有 window
我正在使用 ShellExecuteEx
到 运行 外部应用程序:
SHELLEXECUTEINFO shExInfo = { 0 };
shExInfo.cbSize = sizeof(shExInfo);
shExInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
shExInfo.hwnd = 0;
shExInfo.lpVerb = L"runas"; // Operation to perform
shExInfo.lpFile = windowStringContainingAppPath.c_str(); // Application to start
shExInfo.lpParameters = windowStringContainingAppParameters.c_str(); // Additional parameters
shExInfo.lpDirectory = 0;
shExInfo.nShow = SW_SHOW;
shExInfo.hInstApp = 0;
if(ShellExecuteEx(&shExInfo))
{
WaitForSingleObject(shExInfo.hProcess, INFINITE);
DeleteFile(wsMesh3dx64Parameter.c_str());
CloseHandle(shExInfo.hProcess);
}
一切正常,但此外部应用程序存在意外行为,即在关闭其主要 window 后,其进程仍然处于活动状态。
这可以防止 WaitForSingleObject(shExInfo.hProcess, INFINITE);
返回,我必须手动终止进程。
相反,我正在寻找一种方法来用一个循环替换 WaitForSingleObject(shExInfo.hProcess, INFINITE);
,该循环检查外部进程是否拥有 window,如果没有则终止它。
这是我想到的,如果有更好的方法请指点一下。
更新:
感谢 Robson 的回答,我成功地完成了我的计划:
struct Porcess_ID_HWND
{
DWORD processID;
HWND processhWnd;
};
BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam)
{
Porcess_ID_HWND*info = (Porcess_ID_HWND*)lParam;
DWORD processID;
GetWindowThreadProcessId(hWnd, &processID);
if (processID == info->processID){
info->processhWnd = hWnd;
return FALSE;
}
return TRUE;
}
我的循环:
if(ShellExecuteEx(&shExInfo))
{
DWORD dwProcessID = GetProcessId(shExInfo.hProcess);
Porcess_ID_HWND info;
info.processID = dwProcessID;
// wait for window to appear
do
{
info.processhWnd = NULL;
EnumWindows(EnumWindowsProc, (LPARAM)&info);
} while (!info.processhWnd);
// wait for window to close
do
{
info.processhWnd = NULL;
EnumWindows(EnumWindowsProc, (LPARAM)&info);
} while (info.processhWnd);
//WaitForSingleObject(shExInfo.hProcess, INFINITE);
DeleteFile(wsMesh3dx64Parameter.c_str());
CloseHandle(shExInfo.hProcess);
}
1) HAVE: Process ID, NEED: Process handle Solution: OpenProcess() 2) HAVE: Process handle, NEED: Process ID Solution: GetProcessId() 3) HAVE: Window handle, NEED: Process ID Solution: GetWindowThreadProcessId() 4) HAVE: Window handle, NEED: Process handle Solution: Use 3) and then 1) 5) HAVE: Process ID, NEED: Window handle Solution: EnumWindows(), then in the callback function do 3) and check if it matches your process ID. 6) HAVE: Process handle, NEED: Window handle Solution: 2) and then 5)
所以你属于情况 6。那么如果没有 window 句柄的进程 ID 与你的 shExInfo.hProcess 的进程 ID 匹配,那么 shExInfo.hProcess 不拥有 window