[DllImport("kernel32.dll", SetLastError=true, CharSet=CharSet.Auto)]
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);
<DllImport("kernel32.dll")> _
Shared Function CreateProcess( _
lpApplicationName As String, _
lpCommandLine As String, _
ByRef lpProcessAttributes As SECURITY_ATTRIBUTES, _
ByRef lpThreadAttributes As SECURITY_ATTRIBUTES, _
bInheritHandles As Boolean, _
dwCreationFlags As UInt32, _
lpEnvironment As IntPtr, _
lpCurrentDirectory As String, _
<[In]> ByRef lpStartupInfo As STARTUPINFO, _
<[Out]> ByRef lpProcessInformation As PROCESS_INFORMATION) As Boolean
End Function
[DllImport("kernel32.dll", SetLastError : true)]
def CreateProcess(
lpApplicationName as string,
lpCommandLine as string,
ref lpProcessAttributes as SECURITY_ATTRIBUTES,
ref lpThreadAttributes as SECURITY_ATTRIBUTES,
bInheritHandles as bool,
dwCreationFlags as CreationFlags,
lpEnvironment as IntPtr,
lpCurrentDirectory as string,
ref lpStartupInfo as STARTUPINFOEX,
ref lpProcessInformation as PROCESS_INFORMATION) as bool:
pass
[StructLayout(LayoutKind.Sequential)]
struct SECURITY_ATTRIBUTES:
nLength as int
lpSecurityDescriptor as IntPtr
[Flags]
enum CreationFlags:
CreateSuspended = 0x00000004
DetachedProcess = 0x00000008
CreateNoWindow = 0x08000000
ExtendedStartupInfoPresent = 0x00080000
[StructLayout(LayoutKind.Sequential)]
struct STARTUPINFO:
cb as uint
lpReserved as IntPtr
lpDesktop as IntPtr
lpTitle as IntPtr
dwX as uint
dwY as uint
dwXSize as uint
dwYSize as uint
dwXCountChars as uint
dwYCountChars as uint
dwFillAttributes as uint
dwFlags as uint
wShowWindow as ushort
cbReserved as ushort
lpReserved2 as IntPtr
hStdInput as IntPtr
hStdOutput as IntPtr
hStdErr as IntPtr
[StructLayout(LayoutKind.Sequential)]
struct STARTUPINFOEX:
StartupInfo as STARTUPINFO
lpAttributeList as IntPtr
[StructLayout(LayoutKind.Sequential)]
struct PROCESS_INFORMATION:
hProcess as IntPtr
hThread as IntPtr
dwProcessId as int
dwThreadId as int
This is great for starting an external app and then using the PID and handle for calls to functions such as WaitForSingleObject and all window message functions.
Replace STARTUPINFO with STARTUPINFOEX to use UpdateProcThreadAttribute and extend CreateProcess. Make sure you include the flag EXTENDED_STARTUPINFO_PRESENT (0x00080000)
Add this in constructor of a class to logically treat the app as an object.
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);
}
Public Shared Sub StartupNotepad()
Const NORMAL_PRIORITY_CLASS AS UInt32 = &h2
Dim retValue As Boolean
Dim Application As String = Environment.GetEnvironmentVariable("windir") & "\Notepad.exe"
Dim CommandLine As String = " c:\boot.ini"
Dim pInfo As PROCESS_INFORMATION = New PROCESS_INFORMATION()
Dim sInfo As STARTUPINFO = New STARTUPINFO()
Dim pSec As SECURITY_ATTRIBUTES = New SECURITY_ATTRIBUTES()
Dim tSec As SECURITY_ATTRIBUTES = New SECURITY_ATTRIBUTES()
pSec.nLength = Marshal.SizeOf(pSec)
tSec.nLength = Marshal.SizeOf(tSec)
retValue = CreateProcess(Application, CommandLine, pSec, tSec, False, NORMAL_PRIORITY_CLASS, IntPtr.Zero, vbNull, sInfo, pInfo)
Console.WriteLine("Process ID (PID): " + pInfo.dwProcessId)
Console.WriteLine("Process Handle : " + pInfo.hProcess)
End Sub
System.Diagnostics.Process.Start
If the approach outlined above does not work verbatim, try the variation by Thottam R. Sriram: http://blogs.msdn.com/thottams/archive/2006/08/11/696013.aspx