Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than advapi32, prefix the name with the module name and a period.
<DllImport("advapi32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)> _
Public Function CreateProcessWithLogonW(ByVal userName As String, ByVal domain As String, ByVal password As String, ByVal logonFlags As UInt32, ByVal applicationName As String, ByVal commandLine As String, ByVal creationFlags As UInt32, ByVal environment As UInt32, ByVal currentDirectory As String, ByRef startupInfo As StartupInfo, ByRef processInformation As ProcessInformation) As Boolean
End Function
Sample code for VB
Imports System
Imports System.Runtime.InteropServices
'2005.09.21
'translation in VB of the C# code of
'Chris Hand cj_hand@hotmail.com
Module Module1
Public Infinite As System.UInt32 = Convert.ToUInt32(&HFFFFFFF)
Public Startf_UseStdHandles As Int32 = &H100
Public StdOutputHandle As Int32 = -11
Public StdErrorHandle As Int32 = -12
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Public Structure StartupInfo
Public cb As Integer
Public reserved As String
Public desktop As String
Public title As String
Public x As Integer
Public y As Integer
Public xSize As Integer
Public ySize As Integer
Public xCountChars As Integer
Public yCountChars As Integer
Public fillAttribute As Integer
Public flags As Integer
Public showWindow As UInt16
Public reserved2 As UInt16
Public reserved3 As Byte
Public stdInput As IntPtr
Public stdOutput As IntPtr
Public stdError As IntPtr
End Structure 'StartupInfo
Friend Structure ProcessInformation
Public process As IntPtr
Public thread As IntPtr
Public processId As Integer
Public threadId As Integer
End Structure 'ProcessInformation
<DllImport("advapi32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)> _
Public Function CreateProcessWithLogonW(ByVal userName As String, ByVal domain As String, ByVal password As String, ByVal logonFlags As UInt32, ByVal applicationName As String, ByVal commandLine As String, ByVal creationFlags As UInt32, ByVal environment As UInt32, ByVal currentDirectory As String, ByRef startupInfo As StartupInfo, ByRef processInformation As ProcessInformation) As Boolean
End Function
<DllImport("kernel32.dll", SetLastError:=True)> _
Public Function GetExitCodeProcess(ByVal process As IntPtr, ByRef exitCode As UInt32) As Boolean
End Function
<DllImport("Kernel32.dll", SetLastError:=True)> _
Public Function WaitForSingleObject(ByVal handle As IntPtr, ByVal milliseconds As UInt32) As UInt32
End Function
<DllImport("Kernel32.dll", SetLastError:=True)> _
Public Function GetStdHandle(ByVal handle As IntPtr) As IntPtr
End Function
<DllImport("Kernel32.dll", SetLastError:=True)> _
Public Function CloseHandle(ByVal handle As IntPtr) As Boolean
End Function
<STAThread()> _
Overloads Sub Main(ByVal args() As String)
Dim MyPointer As IntPtr = Marshal.AllocHGlobal(4)
Marshal.WriteInt32(MyPointer, StdOutputHandle)
Dim MyErrorPointer As IntPtr = Marshal.AllocHGlobal(4)
Marshal.WriteInt32(MyErrorPointer, StdErrorHandle)
Dim startupInfo As New StartupInfo
startupInfo.reserved = Nothing
startupInfo.flags = startupInfo.flags And Startf_UseStdHandles
startupInfo.stdOutput = MyPointer
startupInfo.stdError = MyErrorPointer
Dim exitCode As System.UInt32 = Convert.ToUInt32(123456)
Dim processInfo As New ProcessInformation
Dim command As String = "c:\windows\Notepad.exe"
Dim user As String = "administrator"
Dim domain As String = System.Environment.MachineName
Dim password As String = "blank"
Dim currentDirectory As String = System.IO.Directory.GetCurrentDirectory()
Try
CreateProcessWithLogonW(user, domain, password, Convert.ToUInt32(1), _
command, command, Convert.ToUInt32(0), Convert.ToUInt32(0), _
currentDirectory, startupInfo, processInfo)
Catch e As Exception
Console.WriteLine(e.ToString())
End Try
Console.WriteLine("Running ...")
WaitForSingleObject(processInfo.process, Infinite)
GetExitCodeProcess(processInfo.process, exitCode)
Console.WriteLine("Exit code: {0}", exitCode)
CloseHandle(processInfo.process)
CloseHandle(processInfo.thread)
End Sub
using System;
using System.Runtime.InteropServices;
//Chris Hand cj_hand@hotmail.com
//2005.03.13
namespace RunAs
{
class Class1
{
public const UInt32 Infinite = 0xffffffff;
public const Int32 Startf_UseStdHandles = 0x00000100;
public const Int32 StdOutputHandle = -11;
public const Int32 StdErrorHandle = -12;
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public struct StartupInfo
{
public int cb;
public String reserved;
public String desktop;
public String title;
public int x;
public int y;
public int xSize;
public int ySize;
public int xCountChars;
public int yCountChars;
public int fillAttribute;
public int flags;
public UInt16 showWindow;
public UInt16 reserved2;
public byte reserved3;
public IntPtr stdInput;
public IntPtr stdOutput;
public IntPtr stdError;
}
internal struct ProcessInformation
{
public IntPtr process;
public IntPtr thread;
public int processId;
public int threadId;
}
For .NET 2.0, System.Diagnostics.Process.Start has new overloads that allows you to run under different user accounts.
TODO - a short description
3/16/2007 7:34:26 AM - anonymous
TODO - a short description
3/16/2007 7:34:26 AM - anonymous
Passed in place of STARTUPINFO to extend CreateProcess
7/8/2019 11:50:55 AM - dahall-72.24.140.51
The '''PROCESS_INFORMATION''' structure is filled in by either the CreateProcess, CreateProcessAsUser, CreateProcessWithLogonW, or CreateProcessWithTokenW function with information about the newly created process and its primary thread.
8/9/2010 12:13:12 PM - -97.79.160.250
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).