启动Mozilla Firefox时如何获取进程ID?

问题描述 投票:-1回答:1

获取我要启动的进程的PID的任务。CreateProcess() ProcessInformation.dwProcessId 做得很好,但在我的例子中,我启动的进程打开了子进程,然后关闭,我需要得到所有创建我正在打开的进程的PID。

我找到了这段代码,它接收到了子进程的PID,但它们与最终的Firefox窗口不匹配,我到底做错了什么?

Source.CreateProcess返回与启动的Chrome.exe不同的句柄。CreateProcess返回的句柄与启动的Chrome.exe不同。

更新1在Drake Wu - MSFT评论之后,我使用了以下代码。

int test(const wchar_t* programPath) {
    HANDLE Job = CreateJobObject(nullptr, nullptr);
    if (!Job) {
        std::cout << "CreateJobObject, error " << GetLastError() << std::endl;
        return 0;
    }

    HANDLE IOPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE, nullptr, 0, 1);
    if (!IOPort) {
        std::cout << "CreateIoCompletionPort, error " << GetLastError() << std::endl;
        return 0;
    }

    JOBOBJECT_ASSOCIATE_COMPLETION_PORT Port;
    Port.CompletionKey = Job;
    Port.CompletionPort = IOPort;
    if (!SetInformationJobObject(Job,
        JobObjectAssociateCompletionPortInformation,
        &Port, sizeof(Port))) {
        std::cout << "SetInformation, error " << GetLastError() << std::endl;
        return 0;
    }

    PROCESS_INFORMATION ProcessInformation;
    STARTUPINFOW StartupInfo = { sizeof(StartupInfo) };
    LPWSTR szCmdline = const_cast<LPWSTR>(programPath);

    if (!CreateProcessW(
        programPath,
        nullptr,
        nullptr,
        nullptr,
        FALSE,
        CREATE_SUSPENDED,
        nullptr,
        nullptr,
        &StartupInfo,
        &ProcessInformation))
    {
        std::cout << "CreateProcess, error " << GetLastError() << std::endl;
        return 0;
    }
    std::cout << "PID: " << ProcessInformation.dwProcessId << std::endl;
    if (!AssignProcessToJobObject(Job, ProcessInformation.hProcess)) {
        std::cout << "Assign, error " << GetLastError() << std::endl;
        return 0;
    }

    ResumeThread(ProcessInformation.hThread);
    CloseHandle(ProcessInformation.hThread);
    CloseHandle(ProcessInformation.hProcess);

    DWORD CompletionCode;
    ULONG_PTR CompletionKey;
    LPOVERLAPPED Overlapped;
    while (GetQueuedCompletionStatus(IOPort, &CompletionCode, &CompletionKey, &Overlapped, INFINITE))
    {
        switch (CompletionCode)
        {
        case JOB_OBJECT_MSG_NEW_PROCESS:
            std::cout << "New PID: " << (int)Overlapped << std::endl;
            break;
        case JOB_OBJECT_MSG_EXIT_PROCESS:
            std::cout << "Exit PID: " << (int)Overlapped << std::endl;
            break;
        case JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO:
            std::cout << "JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO" << std::endl;
            break;
        default:
            break;
        }
        if (CompletionCode == JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO)
            break;
    }
    std::cout << "All done" << std::endl;
}

而我得到了以下结果。

标准的Firefox

test(L"C:\\Program Files\\Mozilla Firefox\\firefox.exe");

便携版火狐

test(L"D:\\FirefoxPortable\\FirefoxPortable.exe");

和以前一样,PIDs被错误地返回。在便携版的情况下,进程在while循环中挂起,在标准版的firefox的情况下。GetQueuedCompletionStatus() 返回 JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO. 为什么我得到了错误的结果?

更新2

我以管理员身份运行Visual Studio,但在标准启动时,一切都显示正确。

c++ winapi pid createprocess
1个回答
0
投票

我测试了Firefox的进程不是新的,并按顺序退出(由ProcessInformation.dwProcessId获得的PID)。CreateProcess 将会退出),如果后来创建了任何新的进程,你的代码将不会收到新的Firefox进程。

你可以使用 swtich-case 语句,下面为我示例工作。

int openProgram(const wchar_t* programPath) {
    HANDLE Job = CreateJobObject(nullptr, nullptr);
    if (!Job) {
        std::cout << "CreateJobObject, error " << GetLastError() << std::endl;
        return 0;
    }

    HANDLE IOPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE, nullptr, 0, 1);
    if (!IOPort) {
        std::cout << "CreateIoCompletionPort, error " << GetLastError() << std::endl;
        return 0;
    }

    JOBOBJECT_ASSOCIATE_COMPLETION_PORT Port;
    Port.CompletionKey = Job;
    Port.CompletionPort = IOPort;
    if (!SetInformationJobObject(Job,
        JobObjectAssociateCompletionPortInformation,
        &Port, sizeof(Port))) {
        std::cout << "SetInformation, error " << GetLastError() << std::endl;
        return 0;
    }

    PROCESS_INFORMATION ProcessInformation;
    STARTUPINFO StartupInfo = { sizeof(StartupInfo) };
    LPTSTR szCmdline = _tcsdup(programPath);
    if (!CreateProcessW(
        nullptr,
        szCmdline,
        nullptr,
        nullptr,
        FALSE,
        CREATE_SUSPENDED,
        nullptr,
        nullptr,
        &StartupInfo,
        &ProcessInformation))
    {
        std::cout << "CreateProcess, error " << GetLastError() << std::endl;
        return 0;
    }
    std::cout << "PID: " << ProcessInformation.dwProcessId << std::endl;
    if (!AssignProcessToJobObject(Job, ProcessInformation.hProcess)) {
        std::cout << "Assign, error " << GetLastError() << std::endl;
        return 0;
    }

    ResumeThread(ProcessInformation.hThread);
    CloseHandle(ProcessInformation.hThread);
    CloseHandle(ProcessInformation.hProcess);

    DWORD CompletionCode;
    ULONG_PTR CompletionKey;
    LPOVERLAPPED Overlapped;
    while (GetQueuedCompletionStatus(IOPort, &CompletionCode, &CompletionKey, &Overlapped, INFINITE))
    {
        switch (CompletionCode)
        {
        case JOB_OBJECT_MSG_NEW_PROCESS:
            std::cout << "New PID: " << (int)Overlapped << std::endl;
            break;
        case JOB_OBJECT_MSG_EXIT_PROCESS:
            std::cout << "Exit PID: " << (int)Overlapped << std::endl;
            break;
        case JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO:
            std::cout << "JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO" << std::endl;
            break;
        default:
            break;
        }
        if (CompletionCode == JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO)
            break;
    }
    std::cout << "All done" << std::endl;
}

结果:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.