简体   繁体   English

CreateProcess 创建 2 个进程时如何等待子进程即。 rundll32 进程在内部创建 windows 照片查看器进程

[英]How to wait for child process when CreateProcess creates 2 process viz. rundll32 process which creates internally windows photo viewer Process

在此处输入图片说明 I have created process using CreateProcess() to open image using windows photo viewer.我已经使用 CreateProcess() 创建了使用 Windows 照片查看器打开图像的过程。 Since windows photo viewer is not .exe, it run with rundll32.exe, hence creating 2 process.由于 Windows 照片查看器不是 .exe,它使用 rundll32.exe 运行,因此创建了 2 个进程。 so rundll32 becomes the parent process and windows photo viewer is child process.所以 rundll32 成为父进程,windows photo viewer 是子进程。 Now I want to wait for child process to be created.现在我想等待子进程被创建。 How to wait for child process.如何等待子进程。

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.

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.它正在等待无限时间,如果我以毫秒为单位给出一些时间(WaitForSingleObject(pi.hProcess, 500);),那么它会返回 WAIT_TIMEOUT。

I have attached the image, whenever I am calling create process it create two process for each image, as you can see in attachment.我已经附加了图像,每当我调用 create process 时,它都会为每个图像创建两个进程,如附件中所示。 Closing of rundll32.exe from taskmanger closes both the process as well as image window also, where as rundll32.exe *32 neither closes the rundll32.exe nor the windows image viewer.从任务管理器关闭 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; 
}

This code return proper childProcess ID as 12504. But from create process the ID retrieved from pi.dwProcessId is 12132.此代码返回正确的 childProcess ID 为 12504。但是从创建过程中,从 pi.dwProcessId 检索到的 ID 是 12132。

Now my requirement is to wait for the Process ID 12504 until it is not created.现在我的要求是等待进程 ID 12504 直到它没有被创建。 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 alternate way I am searching.它正在工作,但我正在搜索的任何替代方式。

Basically run32dll.exe is window host process executable used to host any kind of DLL application.基本上 run32dll.exe 是窗口宿主进程可执行文件,用于托管任何类型的 DLL 应用程序。 I never saw run32dll hosting executable (Might be wrong here).我从未见过 run32dll 托管可执行文件(这里可能是错误的)。 Your example is completely based on window dll hosting on run32dll executable.您的示例完全基于 run32dll 可执行文件上的窗口 dll 托管。 Hence first correction is run32dll is not parent and window image view is not child process.因此首先更正的是 run32dll 不是父进程,并且窗口图像视图不是子进程。 you can cross verify this in process explorer.您可以在流程浏览器中对此进行交叉验证。 在此处输入图片说明

code present in this link also return zero child processes under run32dll.exe. 链接中的代码还在 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:以下是几个例子:

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.

Following code with wrong image path will not hold execution since image viewer is not opened:由于未打开图像查看器,以下带有错误图像路径的代码将不会保持执行:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM