c++windowswinapi

Set child process name in Windows?


I have process, that run multiple times child processes (each without GUI), and it needs to set to all child processes different "names" and "description" for Task Manager. Is it possible using Win API? I can't find solution for Windows family.

I look to WriteProcessMemory, but it looks like form of cheating, even if there is possible to change name. Solution with copying .exe file, run it, and after process finished - deleting it - is even more cheating. There may be exist solution using start process from memory (so, I load exe file to memory, then start it from there), but it also looks bad and I'm not sure I will be able to change name.

I hope there have to be solution to set process name to my own child process, isn't it?


Solution

  • You can't change the "Image Name" that appears in Task Manager. As you found, Windows obtains that from the name of the file that is actually executing and the only way to have something different appear is to actually run a file with a different name (such as by copying your executable).

    If you are targeting newer versions of Windows, Task Manager can display a "Command Line" column (View -> Select Columns). You can make your processes distinguishable by including a particular flag in the command line, or by setting the first token of the lpCommandLine argument to something unique - you just have to also set the lpApplicationName argument to the actual executable you want to run so the loader can find it.

    For example:

    BOOL ret1 = CreateProcess(
                   "c:\\program\\worker.exe",
                   "worker1.exe infile outfile",
                   ...);
    
    BOOL ret2 = CreateProcess(
                   "c:\\program\\worker.exe",
                   "worker2.exe infile outfile",
                   ...);
    

    These will have identical Image Name values in Task Manager, but they will be distinguishable by the Command Line values.