[英]How to wait for child process when CreateProcess creates 2 process viz. rundll32 process which creates internally windows photo viewer Process
我已經使用 CreateProcess() 創建了使用 Windows 照片查看器打開圖像的過程。 由於 Windows 照片查看器不是 .exe,它使用 rundll32.exe 運行,因此創建了 2 個進程。 所以 rundll32 成為父進程,windows photo viewer 是子進程。 現在我想等待子進程被創建。 如何等待子進程。
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
CString appStr = L"rundll32 \"C:\\Program Files\\Windows Photo Viewer\\PhotoViewer.dll\" ImageView_Fullscreen D:\\\\Results\\1.png";
CreateProcess(NULL, // Name of program to execute
CT2W(appStr), // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
TRUE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi);
WaitForSingleObject(pi.hProcess, INFINITE); //its waiting for infinite time.
它正在等待無限時間,如果我以毫秒為單位給出一些時間(WaitForSingleObject(pi.hProcess, 500);),那么它會返回 WAIT_TIMEOUT。
我已經附加了圖像,每當我調用 create process 時,它都會為每個圖像創建兩個進程,如附件中所示。 從任務管理器關閉 rundll32.exe 會同時關閉進程和圖像窗口,而 rundll32.exe *32 既不會關閉 rundll32.exe,也不會關閉 Windows 圖像查看器。
DWORD GetChildProcessID(DWORD dwProcessID)
{
DWORD dwChildProcessID = -1;
HANDLE hProcessSnapshot;
PROCESSENTRY32 processEntry32;
hProcessSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcessSnapshot != INVALID_HANDLE_VALUE)
{
processEntry32.dwSize = sizeof(PROCESSENTRY32);
if (Process32First(hProcessSnapshot, &processEntry32))
{
do
{
if (dwProcessID == processEntry32.th32ParentProcessID)
{
dwChildProcessID = processEntry32.th32ProcessID;
break;
}
} while (Process32Next(hProcessSnapshot, &processEntry32));
CloseHandle(hProcessSnapshot);
}
}
return dwChildProcessID;
}
此代碼返回正確的 childProcess ID 為 12504。但是從創建過程中,從 pi.dwProcessId 檢索到的 ID 是 12132。
現在我的要求是等待進程 ID 12504 直到它沒有被創建。 我已經使用下面寫的代碼嘗試過這個。
while (1)
{
dwChldProcessID = GetChildProcessID(pi.dwProcessId);
hwnd = GetWindowHandle(dwChldProcessID);
if (IsWindow(hwnd))
break;
else
Sleep(100);
}
它正在工作,但我正在搜索的任何替代方式。
基本上 run32dll.exe 是窗口宿主進程可執行文件,用於托管任何類型的 DLL 應用程序。 我從未見過 run32dll 托管可執行文件(這里可能是錯誤的)。 您的示例完全基於 run32dll 可執行文件上的窗口 dll 托管。 因此首先更正的是 run32dll 不是父進程,並且窗口圖像視圖不是子進程。 您可以在流程瀏覽器中對此進行交叉驗證。
此鏈接中的代碼還在 run32dll.exe 下返回零個子進程。
現在讓我們來回答您的問題,您想檢查天氣窗口照片查看器是否打開。 在這種情況下,您自己的代碼將幫助您解決相同的問題。
以下是幾個例子:
CString appStr = L"rundll32 \"C:\\Program Files (x86)\\Windows Photo Viewer\\PhotoViewer.dll\" ImageView_Fullscreen C:\\download.png";
BOOL result = CreateProcess(NULL, // Name of program to execute
CT2W(appStr), // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
TRUE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi);
WaitForSingleObject(pi.hProcess, INFINITE); //it will wait until you are not closing photo view since image is loaded successfully.
由於未打開圖像查看器,以下帶有錯誤圖像路徑的代碼將不會保持執行:
CString appStr = L"rundll32 \"C:\\Program Files (x86)\\Windows Photo Viewer\\PhotoViewer.dll\" ImageView_Fullscreen C:\\dowdsdsdnload.dspng";
BOOL result = CreateProcess(NULL, // Name of program to execute
CT2W(appStr), // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
TRUE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi);
WaitForSingleObject(pi.hProcess, INFINITE); //it will wait until you are not closing photo view since image is loaded successfully.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.