Monthly Archives: July 2009

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?

Below the code:

C#

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

usingSystem;

usingSystem.Management;

namespaceTestDettachedProcess

{

classProgram

{

staticvoidMain(string[]args)

{

ManagementBaseObject inArgs=null;

ManagementBaseObject outArgs=null;

using(ManagementClass mc=newManagementClass("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",inArgs,null);

uintret=

Convert.ToUInt32(outArgs.Properties["ReturnValue"].Value);

if(ret!=0)

Console.WriteLine("Failed : {0}",ret);

else

Console.WriteLine("Success : {0}",

outArgs.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.