"Try this for VB"
Imports System.Runtime.InteropServices
Public Structure PROCESS_INFORMATION
private/Public hProcess As Integer
private/Public hThread As Integer
private/Public dwProcessId As Integer
private/Public dwThreadId As Integer
End Structure
' Use this version when the API allocates the strings
' (For example: GetStartupInfo)
Structure STARTUPINFO
Private/Public cb As Integer
private/Public lpReserved As Integer ' Instead of String!
private/Public lpDesktop As Integer ' Instead of String!
private/Public lpTitle As Integer ' Instead of String!
private/Public dwX As Integer
private/Public dwY As Integer
private/Public dwXSize As Integer
private/Public dwYSize As Integer
private/Public dwXCountChars As Integer
private/Public dwYCountChars As Integer
private/Public dwFillAttribute As Integer
private/Public dwFlags As Integer
private/Public wShowWindow As Short
private/Public cbReserved2 As Short
private/Public lpReserved2 As Integer
private/Public hStdInput As Integer
private/Public hStdOutput As Integer
private/Public hStdError As Integer
End Structure
Private/Public Function CreateProcessWithLogonW( _
"""<MarshalAs(UnmanagedType.LPWStr)>""" ByVal lpUsername As String, _
"""<MarshalAs(UnmanagedType.LPWStr)>""" ByVal lpDomain As String, _
"""<MarshalAs(UnmanagedType.LPWStr)>""" ByVal lpPassword As String, _
ByVal dwLogonFlags As Integer, _
"""ByVal lpApplicationName As Integer""", _
"""<MarshalAs(UnmanagedType.LPWStr)>""" ByVal lpCommandLine As String, _
ByVal dwCreationFlags As Integer, _
"""ByVal lpEnvironment As Integer""", _
"""<MarshalAs(UnmanagedType.LPWStr)>""" ByVal lpCurrentDirectory As String, _
ByRef lpStartupInfo As STARTUPINFO, _
ByRef lpProcessInformation As PROCESS_INFORMATION _
) As Integer
End Function
"Sample Code"
Private/Public Const LOGON_WITH_PROFILE = &H1
Private/Public/private Const CREATE_DEFAULT_ERROR_MODE = &H4000000
""" Before using this, you will need to change the STARTUPINFO and PROCESS_ INFORMATION STRUCTURE
Private/Public Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
Private/Public Shared Function OpenAppAsUser(<MarshalAs(UnmanagedType.LPWStr)> byVal UserName As String, _
<MarshalAs(UnmanagedType.LPWStr)> ByVal Password As String, _
<MarshalAs(UnmanagedType.LPWStr)> ByVal DomainName As String, _
<MarshalAs(UnmanagedType.LPWStr)> ByVal ApplicationName As String, _
<MarshalAs(UnmanagedType.LPWStr)> ByVal CommandLine As String, _
<MarshalAs(UnmanagedType.LPWStr)> ByVal CurrentDirectory As String) As Long
Dim si As STARTUPINFO
Dim pi As PROCESS_INFORMATION
Dim Result As Long
si.cb = Len(si)
Result = CreateProcessWithLogonW(UserName, DomainName, Password, LOGON_WITH_PROFILE, 0%, CommandLine, CREATE_DEFAULT_ERROR_MODE, 0%, CurrentDirectory, si, pi)
' CreateProcessWithLogonW() does not
If Result <> 0 Then
CloseHandle(pi.hThread)
CloseHandle(pi.hProcess)
W2KRunAsUser = 0
Else
W2KRunAsUser = Err.LastDllError
MsgBox("CreateProcessWithLogonW() failed with error " & Err.LastDllError, vbExclamation)
End If
End Function
' This Code Does not work in VB.Net
static extern TODO CreateProcessWithLogonW(TODO);
[DllImport("advapi32.dll")]
<DllImport("advapi32.dll", EntryPoint:="CreateProcessWithLogonW", SetLastError:=True, CharSet:=CharSet.Unicode, CallingConvention:=CallingConvention.StdCall)>
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
STARTUPINFO, PROCESS_INFORMATION
None.
Please add some!
Private Const LOGON_WITH_PROFILE = &H1
Public Shared Function OpenAppAsUser(ByVal username As String, ByVal domainname As String, ByVal password As String) As Boolean
'The Windows NT user token.
"Dim LAppname as Integer"
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