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

CreateProcessWithLogonW (advapi32)
 
.
Summary
Starts a new process, opens an application in that proces, and uses a passed UserID and Password. The application opened is running under the credentials and authority fo the UserID passed.

C# Signature:

[DllImport("advapi32.dll", SetLastError=true, CharSet=CharSet.Unicode, ExactSpelling=true)]
static extern bool CreateProcessWithLogonW(
    string lpUsername,
    string lpDomain,
    string lpPassword,
    uint dwLogonFlags,
    string lpApplicationName,
    string lpCommandLine,
    uint dwCreationFlags,
    IntPtr lpEnvironment,
    string lpCurrentDirectory,
    ref STARTUPINFO lpStartupInfo,
    out PROCESS_INFORMATION lpProcessInformation);

VB .NET Signature:

<DllImport("advapi32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
Public Shared Function CreateProcessWithLogonW( _
   ByVal lpUsername As String, _
   ByVal lpDomain As String, _
   ByVal lpPassword As String, _
   ByVal dwLogonFlags As Integer, _
   ByVal lpApplicationName As String, _
   ByVal lpCommandLine As String, _
   ByVal dwCreationFlags As Integer, _
   ByVal lpEnvironment As IntPtr, _
   ByVal lpCurrentDirectory As String, _
   ByRef lpStartupInfo As STARTUPINFO, _
   ByRef lpProcessInformation As PROCESS_INFORMATION _
) As Integer
End Function

C# User-Defined Types

        [Flags]
        enum LogonFlags
        {
            LOGON_WITH_PROFILE     = 0x00000001,
            LOGON_NETCREDENTIALS_ONLY  = 0x00000002    
        }

        [Flags]
        enum CreationFlags
        {
            CREATE_SUSPENDED       = 0x00000004,
            CREATE_NEW_CONSOLE     = 0x00000010,
            CREATE_NEW_PROCESS_GROUP   = 0x00000200,
            CREATE_UNICODE_ENVIRONMENT = 0x00000400,
            CREATE_SEPARATE_WOW_VDM    = 0x00000800,
            CREATE_DEFAULT_ERROR_MODE  = 0x04000000,
        }

        [StructLayout (LayoutKind.Sequential)]
        struct ProcessInfo
        {
            public IntPtr hProcess;
            public IntPtr hThread;
            public uint dwProcessId;
            public uint dwThreadId;
        }

        [StructLayout (LayoutKind.Sequential, CharSet=CharSet.Unicode)]
        struct StartupInfo
        {
            public int     cb;
            public string  reserved1;
            public string  desktop;
            public string  title;
            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 ushort  wShowWindow;
            public short   reserved2;
            public int     reserved3;
            public IntPtr  hStdInput;
            public IntPtr  hStdOutput;
            public IntPtr  hStdError;
        }

VB.NET User-Defined Types:

Private Const LOGON_WITH_PROFILE = &H1

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)>  
Public Structure STARTUPINFO
  Public cb As Integer
  Public lpReserved As String
  Public lpDesktop As String
  Public lpTitle As String
  Public dwX As Integer
  Public dwY As Integer
  Public dwXSize As Integer
  Public dwYSize As Integer
  Public dwXCountChars As Integer
  Public dwYCountChars As Integer
  Public dwFillAttribute As Integer
  Public dwFlags As Integer
  Public wShowWindow As Short
  Public cbReserved2 As Short
  Public lpReserved2 As Integer
  Public hStdInput As Integer
  Public hStdOutput As Integer
  Public hStdError As Integer
End Structure

<StructLayout(LayoutKind.Sequential)>
Public Structure PROCESS_INFORMATION
  Public hProcess As Integer
  Public hThread As Integer
  Public dwProcessId As Integer
  Public dwThreadId As Integer
End Structure

Notes:

You need the following imports [VB.NET]:

Imports System.runtime.InteropServices

Imports SR = System.Reflection

Imports System.Windows.Forms ' add System.Windows.Forms.dll reference

Also, the sample code uses the Exception Management Application Block (MAE) - but you can comment the lines in the else-branch of the if-statement 'if ret=1326' out and provide a messagebox to run it without usage of the MAE.

Tips & Tricks:

Please add some!

Links

The Code Project - Sample Project: http://www.thecodeproject.com/csharp/RunAs.asp

Sample Code:

Public Shared Function OpenAppAsUser(ByVal username As String, ByVal domainname As String, ByVal password As String) As Boolean

'The Windows NT user token.
Dim lpAppName As String
Dim appExe As SR.[Assembly]
Dim pi As PROCESS_INFORMATION
Dim si As STARTUPINFO
Dim fResult As Boolean
Dim dwCreationFlags As Integer = Nothing
Dim lpCommandLine As String = Nothing
Dim lpEnvironment As IntPtr
Dim lpCurrentDirectory As String = Nothing
Dim ret As Integer

appExe = SR.Assembly.GetEntryAssembly()
lpAppName = appExe.Location
si.cb = Len(si)
si.lpTitle = appExe.GetName().Name

' call the Win32 API to open the application under the userID passed in
' Implicit Integer to Boolean conversion.  CreateProcessWithLogonW returns  0 (False) when it fails.
fResult = CreateProcessWithLogonW(username, domainname, password,    LOGON_WITH_PROFILE, lpAppName, lpCommandLine, dwCreationFlags,   lpEnvironment, lpCurrentDirectory, si, pi)

' if the logon fails then we need to generate an error
If Not fResult Then
   ' get the last error from Windows
   ret = Marshal.GetLastWin32Error

   ' if the userid, password, or domain is bad we can display a friendly message
   If ret = 1326 Then
     MessageBox.Show("Invalid UserID or Password entered.  Please try again.", "Logon Error")
     Return False
   Else
     ' the error code is unexpected so throw an exception to be logged.
     Dim retMsg = GetErrorMessage(ret)
     Throw New MAE.BaseApplicationException("Error occurred: " + ret.ToString() + " - " + retMsg)
     Return False
   End If
End If

Return True
End Function

Alternative Managed API:

TODO

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