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

.
Summary
This function creates a new process. Only necessary params are the Application Name and the process Information

C# Signature:

[DllImport("coredll.dll",CallingConvention = CallingConvention.Winapi,CharSet = CharSet.Auto)]
static extern bool CreateProcess(
    string lpApplicationName,
    string lpCommandLine,
    IntPtr lpProcessAttributes,
    IntPtr lpThreadAttributes,
    bool bInheritHandles,
    uint dwCreationFlags,
    IntPtr lpEnvironment,
    string lpCurrentDirectory,
    IntPtr lpStartupInfo,
    out ProcessInfo lpProcessInformation
);

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#

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct ProcessInfo
{
    public IntPtr hProcess;
    public IntPtr hThread;
    public Int32 ProcessId;
    public Int32 ThreadId;
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct SecurityAttributes
{
    public int length;
    public IntPtr lpSecurityDescriptor;
    public bool bInheritHandle;
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
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;
}

Notes:

None.

Tips & Tricks:

You will need the above ProcessInfo class and may also want to use GetLastError and WaitForSingleObject.

Sample Code:

C# Sample usage:

StartupInfo si; // no new required since StartupInfo is a value type (struct) - StartupInfo not supported by the .NET Compact Framework

ProcessInfo pi; // no new required since ProcessInfo is a value type (struct)

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

Alternative Managed API:

Do you know one? Please contribute it!

Documentation