Creating A New Process

S

sternr

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?

Thanks ahead

sternr
 
N

Nicholas Paldino [.NET/C# MVP]

Sternr,

Not that I know of. You could try and get around this by having another
process running already, remoting to it, and telling that process to start
the new process, so the new process is a part of the other process tree.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Not that I know of, and most probably it's not possible. It could have
security consequences associated with it. If your process is not the parent
then which process is?
 
W

Willy Denoyette [MVP]

sternr said:
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?

Thanks ahead

sternr


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

sternr

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?
Thanks ahead

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.

Once again you saved me Willy thanks alot! ;)

--Question answered--
 
A

Alvin Bruney [ASP.NET MVP]

Why do you need a new process? What's the design motivation?

--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The O.W.C. Black Book, 2nd Edition
Exclusively on www.lulu.com/owc $19.99
-------------------------------------------------------



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?
Thanks ahead

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.

Once again you saved me Willy thanks alot! ;)

--Question answered--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top