Hey,
Is there a way to create a new process that will not be my child
process?
Meaning, that after opening the new process, if I'll open TaskManager
and do "End Process Tree" on the parent process, it will not terminate
the child process as well?
Yes there is: PInvoke CreateProcess, but the easiest is by using using
System.Management
Here is a small snip to get you started:
using System.Management;
...
ManagementBaseObject inArgs = null;
ManagementBaseObject outArgs = null;
using(ManagementClass mc = new ManagementClass("Win32_Process"))
{
ManagementClass si = new
ManagementClass("Win32_ProcessStartup");
si.Properties["CreateFlags"].Value = 8;
si.Properties["ShowWindow"].Value = 1;
inArgs = mc.GetMethodParameters("Create");
inArgs["CommandLine"] = "notepad.exe";
inArgs["ProcessStartupInformation"] = si;
outArgs = mc.InvokeMethod("Create", inputArgs, null);
uint ret =
Convert.ToUInt32(outParams.Properties["ReturnValue"].Value);
if(ret != 0)
Console.WriteLine("Failed : {0}", ret);
else
Console.WriteLine("Success : {0}",
outParams.Properties["ProcessId"].Value);
}
Here I create a new process (starting notepad) detached from any other
process (CreateFlags = 8).
You can also set other values, for instance if you want the new process to
become a new process group parent the you have to set CreateFlags = 512 + 8.
Search the WMI docs in MSDN for the details about the possible Win32_Process
and Win32_ProcessStartup properties.
Willy.