Creating a new process that's not a child of the creating process

后端 未结 1 664
孤街浪徒
孤街浪徒 2021-01-26 23:39

I am developing an application in which a number of instances of a process, A, depend on a single instance of a process, B. The idea is that one of the instances of process A st

相关标签:
1条回答
  • 2021-01-26 23:50

    You can try to use the ManagementClass to launch a process and pass some CreateFlags, more specifically, the DETACHED_PROCESS flag. (You'll need to reference System.Management.)

    private static void Main()
    {
        using (var managementClass = new ManagementClass("Win32_Process"))
        {
            var processInfo = new ManagementClass("Win32_ProcessStartup");
            processInfo.Properties["CreateFlags"].Value = 0x00000008;
    
            var inParameters = managementClass.GetMethodParameters("Create");
            inParameters["CommandLine"] = "notepad.exe";
            inParameters["ProcessStartupInformation"] = processInfo;
    
            var result = managementClass.InvokeMethod("Create", inParameters, null);
            if ((result != null) && ((uint)result.Properties["ReturnValue"].Value != 0))
            {
                Console.WriteLine("Process ID: {0}", result.Properties["ProcessId"].Value);
            }
        }
    
        Console.ReadKey();
    }
    

    At least on my machine notepad is not considered a child process of my console test application.

    0 讨论(0)
提交回复
热议问题