如何在 Win32 中创建 child 个进程,以便它们显示为嵌套在任务管理器中?

How do I make child processes in Win32 so that they show up as nested in Task Manager?

我有一个 Win32 C++ 应用程序。我正在尝试使用 CreateProcess 启动一个或多个 child 进程。我希望 children 在 parent 关闭时关闭。

我通过创建工作并启用 JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE:

实现了这一点
HANDLE hJob = CreateJobObject(NULL, NULL);

JOBOBJECT_EXTENDED_LIMIT_INFORMATION extendedInfo;
ZeroMemory(&extendedInfo, sizeof(extendedInfo));
extendedInfo.BasicLimitInformation.LimitFlags =
    JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;

SetInformationJobObject(
        hJob, JOBOBJECTINFOCLASS::JobObjectExtendedLimitInformation,
        &extendedInfo, sizeof(extendedInfo));

然后将当前(parent)和创建(child)进程添加到此作业:

// assign parent to job
AssignProcessToJobObject(hJob, GetCurrentProcess());

// launch child with no inherited handles
PROCESS_INFORMATION procInfo;
ZeroMemory(&procInfo, sizeof(procInfo));
STARTUPINFOA startInfo;
ZeroMemory(&startInfo, sizeof(startInfo));
startInfo.cb = sizeof(startInfo);
startInfo.dwFlags |= STARTF_USESTDHANDLES;
bool success = CreateProcessA(NULL,
                              "test.exe",  // command line
                              NULL,     // process security attributes
                              NULL,   // primary thread security attributes
                              FALSE,  // handles are inherited
                              0,      // creation flags
                              NULL,   // use parent's environment
                              NULL,   // use parent's current directory
                              &startInfo,  // STARTUPINFO pointer
                              &procInfo);  // receives PROCESS_INFORMATION
// assign child to job
AssignProcessToJobObject(hJob, procInfo.hProcess);

这有效,但是 parent 应用程序和 child 应用程序(main.exetest.exe)在任务管理器中显示为两个不相关的进程:

(即使关闭 main.exe 也会关闭 test.exe)。

我做的事情与 Microsoft Teams 或 Chrome 有什么不同,它们都有嵌套进程?

没有记录任务管理器的具体操作。

在 Windows 8 中,它不对 child 进程进行分组,它仅根据具有 window 或“特殊”的进程进行组织。

How does Task Manager categorize processes as App, Background Process, or Windows Process?:

These are terms that Task Manager simply made up. The system itself doesn’t really care what kind of processes they are.

If the process has a visible window, then Task Manager calls it an “App”.

If the process is marked as critical, then Task Manager calls it a “Windows Process”.

Otherwise, Task Manager calls it a “Background Process”.

(我不相信这是 100% 准确的,它清楚地知道服务,我怀疑它可能 hard-code 一些名字)

在 Windows 10 中,它更努力地将事物组合在一起,但我不完全知道它在做什么。

  • 通常(但不总是)能够将 conhost.exe child 与其 parent 控制台应用程序联系起来。

  • 新奇特的 store/packaged 版本的记事本和画图将所有进程都放在一个组中。即使 Notepad2 设置了应用程序模型 ID,也不会发生同样的情况。它也不适用于写字板(即使一个是另一个的child)。我还尝试在一个小测试应用程序中设置 AMUI,但进程范围的 AMUI 和 per-HWND AMUI 似乎都不会触发分组。

  • 一个工作object似乎没有启用分组。

  • 根据您的版本,Edge 可能会使用特殊的 API 到 tell Task manager about its processes

总之,我不知道它到底在寻找什么,但打包应用程序和应用程序容器似乎经常触发它。