Welcome to MSDN Blogs Sign in | Join | Help

Accessing CreateProcess from C# and VB.NET

I came across this issue recently. I was trying to access CreateProcess using DLLImport from managed code using C# and VB.Net. Here is my code from C#:

 

using System;

using System.Diagnostics;

using System.Runtime.InteropServices;

 

public struct PROCESS_INFORMATION

{

    public IntPtr hProcess;

    public IntPtr hThread;

    public uint dwProcessId;

    public uint dwThreadId;

}

 

public struct STARTUPINFO

{

    public uint cb;

    public string lpReserved;

    public string lpDesktop;

    public string lpTitle;

    public uint dwX;

    public uint dwY;

    public uint dwXSize;

    public uint dwYSize;

    public uint dwXCountChars;

    public uint dwYCountChars;

    public uint dwFillAttribute;

    public uint dwFlags;

    public short wShowWindow;

    public short cbReserved2;

    public IntPtr lpReserved2;

    public IntPtr hStdInput;

    public IntPtr hStdOutput;

    public IntPtr hStdError;

}

 

public struct SECURITY_ATTRIBUTES

{

    public int length;

    public IntPtr lpSecurityDescriptor;

    public bool bInheritHandle;

}

 

public class Program

{

    public static void Main()

    {

        STARTUPINFO si = new STARTUPINFO();

        PROCESS_INFORMATION pi = new PROCESS_INFORMATION();

        CreateProcess("C:\\WINDOWS\\SYSTEM32\\Calc.exe", null, IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, null, ref si, out pi);

        Console.ReadLine();

    }

 

[DllImport("kernel32.dll")]

static extern bool CreateProcess(string lpApplicationName, string lpCommandLine, IntPtr lpProcessAttributes, IntPtr lpThreadAttributes,

                        bool bInheritHandles, uint dwCreationFlags, IntPtr lpEnvironment,

                        string lpCurrentDirectory, ref STARTUPINFO lpStartupInfo,out PROCESS_INFORMATION lpProcessInformation);

}

 

 

This works like a charm. I initially had problems with this VB code. I looked at the IL code for both and figured out that I had constructed structures in C# for Process_Information, Security_Attricutes etc, but had marked them as classes in VB. This was mking the call to fail. I figured this out after looking into the IL from VB and C# to see what was different between the two.  I changed the VB to struct and it works like a charm in VB too. Check the VB code below!

 

 

Imports System.Runtime.InteropServices

Imports System.Security.Permissions

Imports System.Reflection

 

Module Module1

    Sub Main()

        StartupNotepad()

        Console.ReadLine()

    End Sub

    Sub StartupNotepad()

        Dim retValue As Boolean

        Dim pInfo As PROCESS_INFORMATION = New PROCESS_INFORMATION()

        Dim sInfo As STARTUPINFO = New STARTUPINFO()

 

        retValue = CreateProcess("c:\\windows\\system32\\NotePad.exe", Nothing, IntPtr.Zero, IntPtr.Zero, False, 0, IntPtr.Zero, Nothing, sInfo, pInfo)

 

    End Sub

    <StructLayout(LayoutKind.Sequential)> _

    Public Structure PROCESS_INFORMATION

        Public hProcess As IntPtr

        Public hThread As IntPtr

        Public dwProcessID As UInteger

        Public dwThreadID As UInteger

    End Structure 'PROCESS_INFORMATION

 

    <StructLayout(LayoutKind.Sequential)> _

    Public Structure SECURITY_ATTRIBUTES

        Public nLength As Integer

        Public lpSecurityDescriptor As IntPtr

        Public bInheritHandle As Boolean

    End Structure 'SECURITY_ATTRIBUTES

 

    <StructLayout(LayoutKind.Sequential)> _

    Public Structure STARTUPINFO

        Public cb As UInteger

        Public lpReserved As String

        Public lpDesktop As String

        Public lpTitle As String

        Public dwX As UInteger

        Public dwY As UInteger

        Public dwXSize As UInteger

        Public dwYSize As UInteger

        Public dwXCountChars As UInteger

        Public dwYCountChars As UInteger

        Public dwFillAttribute As UInteger

        Public dwFlags As UInteger

        Public wShowWindow As Short

        Public cbReserved2 As Short

        Public lpReserved2 As IntPtr

        Public hStdInput As IntPtr

        Public hStdOutput As IntPtr

        Public hStdError As IntPtr

    End Structure 'STARTINFO

    <DllImport("kernel32.dll")> _

    Function CreateProcess( _

    ByVal lpApplicationName As String, _

    ByVal lpCommandLine As String, _

    ByVal lpProcessAttributes As IntPtr, _

    ByVal lpThreadAttributes As IntPtr, _

    ByVal bInheritHandles As Boolean, _

    ByVal dwCreationFlags As UInteger, _

    ByVal lpEnvironment As IntPtr, _

    ByVal lpCurrentDirectory As String, _

    ByRef lpStartupInfo As STARTUPINFO, _

    ByRef lpProcessInformation As PROCESS_INFORMATION) As Boolean

    End Function

End Module

 

 

Published Friday, August 11, 2006 11:56 PM by thottams@microsoft.com

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Interesting Finds: August 10 part 2 and August 11, 2006

Friday, August 11, 2006 11:24 PM by Jason Haley

# re: Accessing CreateProcess from C# and VB.NET

a. Any reason why the Process class is not enough?
b. are you aware of http://www.pinvoke.net/default.aspx/coredll/CreateProcess.html
Saturday, August 12, 2006 10:43 AM by ShayEr

# re: Accessing CreateProcess from C# and VB.NET

Could you explain a little bit more about why structs are compiled as classes and what are the implications of that? What about that attribute??

Thanks
Monday, August 14, 2006 7:59 AM by Andrés

# re: Accessing CreateProcess from C# and VB.NET

Using porocess is another alternative. I am aware of pinvoke.net. The sample for CreateProcess from there did not work for me.

It was my mistake on the wording. I had authored the struct as classes in VB and hence the problem. I changed them to structs and it worked fine.
Monday, August 14, 2006 12:31 PM by thottams@microsoft.com

# re: Accessing CreateProcess from C# and VB.NET

Did you update the sample (and anything else you found to be incorrect) for the pinvoke entry?  :)
Monday, August 14, 2006 2:24 PM by RonO

# re: Accessing CreateProcess from C# and VB.NET

The sample represented PROCESS_INFORMATION and SECURITY_ATTRIBUTES and STARTUPINFO as classes in VB. This made the call to fail. I changed them to struct in VB and I could get notepad up and running from the VB code! The working sample is the code above.
Monday, August 14, 2006 5:11 PM by Thottam Sriram

# re: Accessing CreateProcess from C# and VB.NET

I don't understand the desire to use P/Invoke for this.  The managed world already has a Process class for doing all these sorts of things.  It'll not only be more performant (by avoiding P/Invoke), but also be easier for future maintainers of any code to understand and modify, and less error-prone.

Can you explain why you would ever want to use this over the normal Process class of the .NET Base Class Library?
Wednesday, August 16, 2006 10:19 AM by Austin

# re: Accessing CreateProcess from C# and VB.NET

I totally agree with you and would think that to be a better decision.

There were a few questions on the web relating to this not working and I just wanted to aaddress that. Regardig using this over existing Process class, I agree with you that it will be a better choice.

Question to users:
If you are using it, can you explain the scenario that force you to using this?
Wednesday, August 16, 2006 7:02 PM by Thottam Sriram

# re: Accessing CreateProcess from C# and VB.NET

One reason for wanting to do this is:

Open a socket for listening in the parent process.

Create a child process using Process class.

Kill parent.

Socket is still open for listening and doesn't close until the child process exits. I cannot see anyway of just using the Process class from stopping this from working. I suspect the call to CreateProcess has inherit handle set to true. Which seems like a bug to me.

Tuesday, August 22, 2006 9:24 AM by Andy

# re: Accessing CreateProcess from C# and VB.NET

Here's one reason why one would prefer to use CreateProcess this way as opposed to the managed code of System.Diagnostics.Process is that the Process class quite simply does not exist in the .NET Compact Framework 1.0

So while Microst assumes that the minute hey shipped the newer version of the compact framework all older devices stopped existing, a few unlucy souls such as myself still have to develop for devices running the CF 1.0

I am very grateful to have stmbled across this article for that very reason.

Friday, December 08, 2006 2:12 AM by Dewald Swaneoel

# re: Accessing CreateProcess from C# and VB.NET

Sorry to be a month late on this>  you also have to pInvoke to create a process if you are trying to run the process in a different user context (some WTS applications).

Wednesday, January 31, 2007 7:54 AM by jon nolen

# re: Accessing CreateProcess from C# and VB.NET

These are some good scenarios for this.

Can't you use System.Diagnostics.Process to create a process after you impersonate an user? I have not tried it, but would be interesting to know.

Wednesday, February 07, 2007 12:55 PM by thottams@microsoft.com

# re: Accessing CreateProcess from C# and VB.NET

Great article!!! Thanks for help :)

Vlada, jelovac.vladimir@cont.co.yu

Monday, May 07, 2007 6:29 AM by Vladimir Jelovac

# re: Accessing CreateProcess from C# and VB.NET

Another reason would be that you cannot create a process in suspended mode with the .NET Process class.

Tuesday, July 03, 2007 7:27 AM by SuspendedUser

# re: Accessing CreateProcess from C# and VB.NET

Beauty lies in utility & simplicity.

Thank you!

Wednesday, July 25, 2007 5:14 AM by Er. Kartikay Malhotra

# re: Accessing CreateProcess from C# and VB.NET

One reason to use this instead of Process.Start is that Process.Start has a upper limit of maximum 2048 characters passed as parameter to the process.

This method has a maximum of 32000-something

Tuesday, September 23, 2008 2:56 AM by Reasonator

# re: Accessing CreateProcess from C# and VB.NET

thanks very useful in creating a new Process group with C#,

i think System.Diagnostics.Process lacks this

Tuesday, November 25, 2008 3:33 AM by doboloh

# Window's Handle

Thank you, that was very very useful.

Tell me, how do you find the Window's handle ? (HWND) from createprocess ?

Thursday, December 11, 2008 8:54 AM by Yaniv

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker