Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

CreateProcess (kernel32)
 
.
Summary
Creates a new process and its primary thread.

C# Signature:

[DllImport("kernel32.dll")]
static extern bool CreateProcess(string lpApplicationName,
   string lpCommandLine, ref SECURITY_ATTRIBUTES lpProcessAttributes,
   ref SECURITY_ATTRIBUTES lpThreadAttributes, bool bInheritHandles,
   uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory,
   [In] ref STARTUPINFO lpStartupInfo,
   out PROCESS_INFORMATION lpProcessInformation);

VB Signature:

<DllImport("kernel32.dll")>
Shared Sub CreateProcess(lpApplicationName As String,
   lpCommandLine As String, ByRef lpProcessAttributes As SECURITY_ATTRIBUTES,
   ByRef lpThreadAttributes As SECURITY_ATTRIBUTES,bInheritHandles As Boolean,
   dwCreationFlags As Uint, lpEnvironment As IntPtr, lpCurrentDirectory As String,
   <In> ByRef lpStartupInfo As STARTUPINFO,
   <Out> ByRef lpProcessInformation As PROCESS_INFORMATION) As Boolean

User-Defined Types:

STARTUPINFO, PROCESS_INFORMATION, SECURITY_ATTRIBUTES

Notes:

This is great for starting an external app and then use the PID and handle for calls to functions such as WaitForSingleObject and all window message functions.

It should also be considered when wanting to start a process without all the diagnostic capability. When starting a process, about 3 MB of DLLs are loaded as well as several extra threads started. Doing this via Win32 API avoids this overhead.

Tips & Tricks:

Add this in constructor of a class to logically treat the app as an object.

Sample Code:

public static void StartupNotepad()
{
     const uint NORMAL_PRIORITY_CLASS = 0x0020;

     bool retValue;
     string Application = Environment.GetEnvironmentVariable("windir") + @"\Notepad.exe";
     string CommandLine = @" c:\boot.ini";
     PROCESS_INFORMATION pInfo = new PROCESS_INFORMATION();
     STARTUPINFO sInfo = new STARTUPINFO();
     SECURITY_ATTRIBUTES pSec = new SECURITY_ATTRIBUTES();
     SECURITY_ATTRIBUTES tSec = new SECURITY_ATTRIBUTES();
     pSec.nLength = Marshal.SizeOf(pSec);
     tSec.nLength = Marshal.SizeOf(tSec);

     //Open Notepad
     retValue = CreateProcess(Application,CommandLine,
     ref pSec,ref tSec,false,NORMAL_PRIORITY_CLASS,
     IntPtr.Zero,null,ref sInfo,out pInfo);

     Console.WriteLine("Process ID (PID): " + pInfo.dwProcessId);
     Console.WriteLine("Process Handle : " + pInfo.hProcess);
}

Alternative Managed API:

System.Diagnostics.Process.Start

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Find References
Show Printable Version
Revisions