I have created process using CreateProcess() to open image using Windows Photo Viewer. Since Windows Photo Viewer is not .exe, it is invoked via rundll32.exe, hence creating 2 process, so rundll32.exe becomes the parent process and Windows Photo Viewer is the child process. Now I want to wait for the child process to be created. How to wait for the child process?
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
CString appStr = L"rundll32.exe \"%ProgramFiles%\\Windows Photo Viewer\\PhotoViewer.dll\" ImageView_Fullscreen C:\\\\img\\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.
It is waiting for infinite time, and in case if I am giving some time in milisecond WaitForSingleObject(pi.hProcess, 500) then it is returning WAIT_TIMEOUT.
I have attached the image, whenever I am calling CreateProcess, it creates two processes per image, as you can see in the attachment. Closing rundll32.exe from Task Manager closes both the process as well as image window also, whereas rundll32.exe *32 neither closes the rundll32.exe nor the windows image viewer.
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;
}
This code returns the proper child ProcessID as 12504, but from CreateProcess the ID retrieved from pi.dwProcessId is 12132.
Now my requirement is to wait for the Process ID 12504 until it is not created. I have tried this using the code written below:
while (1)
{
dwChldProcessID = GetChildProcessID(pi.dwProcessId);
hwnd = GetWindowHandle(dwChldProcessID);
if (IsWindow(hwnd))
break;
else
Sleep(100);
}
It is working, but any alternatives?

Basically run32dll.exe is window host process executable used to host any kind of DLL application. I never saw run32dll hosting executable (Might be wrong here). Your example is completely based on window dll hosting on run32dll executable. Hence first correction is run32dll is not parent and window image view is not child process. you can cross verify this in process explorer.
code present in this link also return zero child processes under run32dll.exe.
Now lets come to your question, you want to check weather window photo viewer is opened or not. in this case, your own code will help you with the same.
Following are few example:
Following code with wrong image path will not hold execution since image viewer is not opened: