I使用CreateProcess()创建了使用Windows照片查看器打开图像的过程。由于Windows照片查看器不是.exe,它与rundll32.exe一起运行,因此创建了2个进程。所以rundll32成为父进程,windows照片查看器是子进程。现在我想等待创建子进程。如何等待子进程。
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时,它会为每个图像创建两个进程,正如您在附件中看到的那样。从taskmanger关闭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不是父级,窗口图像视图不是子进程。您可以在进程资源管理器中交叉验证。
this链接中的代码也会在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.