CreateProcess (coredll)
Last changed: hfr@hfrmobile.com-80.109.238.224

.
Summary
This function creates a new process.

C# Signature:

[DllImport("kernel32.dll", SetLastError = true)]

static extern bool CreateProcess(String imageName,

            String cmdLine,
            IntPtr lpProcessAttributes,
            IntPtr lpThreadAttributes,
            bool boolInheritHandles,
            Int32 dwCreationFlags,
            IntPtr lpEnvironment,
            IntPtr lpszCurrentDir,
            byte[] si,
            IntPtr pi);

VB Signature:

Declare Function CreateProcess Lib "coredll.dll" (ByVal imageName As String, ByVal cmdLine As String, ByVal lpProcessAttributes As IntPtr, ByVal lpThreadAttributes As IntPtr, ByVal boolInheritHandles As Int32, ByVal dwCreationFlags As Int32, ByVal lpEnvironment As IntPtr, ByVal lpszCurrentDir As IntPtr, ByVal si As Byte(), ByVal pi As IntPtr) As Integer

User-Defined Types:

C#

public struct ProcessInfo
{
    public IntPtr hProcess;
    public IntPtr hThread;
    public Int32 ProcessId;
    public Int32 ThreadId;
}

Notes:

None.

Tips & Tricks:

You will need the above ProcessInfo class and may also want to use GetLastError and WaitForSingleObject. COMMENT: What about filling in "si.cb = sizeof(si);". No such code in the example.

Sample Code:

C# Sample usage:

ProcessInfo pi = new ProcessInfo();

IntPtr ptr_pi = Marshal.AllocHGlobal(Marshal.SizeOf(pi)); ;

Marshal.StructureToPtr(pi, ptr_pi, true);

byte[] si = new byte[128];

bool result = CreateProcess(openFileDialog1.FileName,

    " ",
    IntPtr.Zero,
    IntPtr.Zero,
    false,
    0,
    IntPtr.Zero,
    IntPtr.Zero,
    si,
    ptr_pi);

    pi = (ProcessInfo)Marshal.PtrToStructure(ptr_pi, typeof(ProcessInfo));

Alternative Managed API:

Do you know one? Please contribute it!

Documentation