gpt4 book ai didi

c++ - 如何确定使用 ShellExecuteEx 创建的进程是否拥有一个窗口?

转载 作者:行者123 更新时间:2023-11-28 00:16:22 29 4
gpt4 key购买 nike

我正在使用 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);
}

一切正常,但此外部应用程序存在意外行为,即在关闭其主窗口后,其进程仍处于事件状态。这可以防止 WaitForSingleObject(shExInfo.hProcess, INFINITE); 返回,我必须手动终止进程。

相反,我正在寻找一种方法来替换 WaitForSingleObject(shExInfo.hProcess, INFINITE);,用一个循环检查外部进程是否拥有一个窗口,如果没有则终止它。

这是我想到的,如果有更好的办法请指点一下。

更新:

感谢 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);
}

最佳答案

http://forums.codeguru.com/showthread.php?392273-RESOLVED-How-to-get-window-s-HWND-from-it-s-process-handle 找到了一个很好的答案

1)HAVE: Process ID, NEED: Process handleSolution: OpenProcess()2)HAVE: Process handle, NEED: Process IDSolution: GetProcessId()3)HAVE: Window handle, NEED: Process IDSolution: GetWindowThreadProcessId()4)HAVE: Window handle, NEED: Process handleSolution: Use 3) and then 1)5)HAVE: Process ID, NEED: Window handleSolution: EnumWindows(), then in the callback function do 3) and check if it matches your process ID.6)HAVE: Process handle, NEED: Window handleSolution: 2) and then 5)

所以你是第 6 种情况。如果没有窗口句柄的进程 ID 与你的 shExInfo.hProcess 的进程 ID 匹配,那么 shExInfo.hProcess 不拥有任何窗口

关于c++ - 如何确定使用 ShellExecuteEx 创建的进程是否拥有一个窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30051397/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com