Link to home
Start Free TrialLog in
Avatar of DFPITC
DFPITCFlag for Australia

asked on

VB.Net convert string to LPCWSTR

I'm trying to use CreateProcessWithLogonW from ASP.Net (VB) as i'm having no end of difficulty just using process.start with impersonation - see:
https://www.experts-exchange.com/questions/28640451/Process-Start-not-working-correctly.html

and i've created a class, however, i'm stuck trying to convert my strings to LPCWSTR (i'm guessing a unicode pointer?) as the function in VB6 - StrConv(UserName + Chr(0), VBUNICODE) does not work in vb.net

I'm assuming there is an equivalent function in vb.net, could someone please let me know?

https://msdn.microsoft.com/en-us/library/windows/desktop/ms682431%28v=vs.85%29.aspx

Here's my class code if anyone wants to see it for reference:
(see the section under "this part isn't working yet")

'RunAsUser
	Public Class RunAsUser
		'Taken from https://support.microsoft.com/en-us/kb/285879
		'Info
			'LogonUser() requires that the caller has the following permission
			'Permission                        Display Name
			'--------------------------------------------------------------------
			'SE_TCB_NAME                      Act as part of the operating system
			
			'CreateProcessAsUser() requires that the caller has the following permissions
			'Permission                        Display Name
			'---------------------------------------------------------------
			'SE_ASSIGNPRIMARYTOKEN_NAME       Replace a process level token
			'SE_INCREASE_QUOTA_NAME           Increase quotas
		'Constants
			Private Const CREATE_DEFAULT_ERROR_MODE = &H4000000
			Private Const LOGON_WITH_PROFILE = &H1
			Private Const LOGON_NETCREDENTIALS_ONLY = &H2
			Private Const LOGON32_LOGON_INTERACTIVE = 2
			Private Const LOGON32_PROVIDER_DEFAULT = 0
			Private Const VBUNICODE = 64
		'Structures
			'Startup Info
				Private Structure STARTUPINFO
					Public cb As Long
					Public lpReserved As Long ' !!! must be Long for Unicode string
					Public lpDesktop As Long  ' !!! must be Long for Unicode string
					Public lpTitle As Long    ' !!! must be Long for Unicode string
					Public dwX As Long
					Public dwY As Long
					Public dwXSize As Long
					Public dwYSize As Long
					Public dwXCountChars As Long
					Public dwYCountChars As Long
					Public dwFillAttribute As Long
					Public dwFlags As Long
					Public wShowWindow As Integer
					Public cbReserved2 As Integer
					Public lpReserved2 As Long
					Public hStdInput As Long
					Public hStdOutput As Long
					Public hStdError As Long
				End Structure
			'Process Information
				Private Structure PROCESS_INFORMATION
					Public hProcess As Long
					Public hThread As Long
					Public dwProcessId As Long
					Public dwThreadId As Long
				End Structure
		'DLL Functions
			'Logon User
				Private Declare Function LogonUser Lib "advapi32.dll" Alias "LogonUserA" ( _
					ByVal lpszUsername As String, _
					ByVal lpszDomain As String, _
					ByVal lpszPassword As String, _
					ByVal dwLogonType As Long, _
					ByVal dwLogonProvider As Long, _
					phToken As Long _
				) As Long
			'Create Process As User
				Private Declare Function CreateProcessAsUser Lib "advapi32.dll" Alias "CreateProcessAsUserA" ( _
					ByVal hToken As Long, _
					ByVal lpApplicationName As Long, _
					ByVal lpCommandLine As String, _
					ByVal lpProcessAttributes As Long, _
					ByVal lpThreadAttributes As Long, _
					ByVal bInheritHandles As Long, _
					ByVal dwCreationFlags As Long, _
					ByVal lpEnvironment As Long, _
					ByVal lpCurrentDirectory As String, _
					lpStartupInfo As STARTUPINFO, _
					lpProcessInformation As PROCESS_INFORMATION _
				) As Long
			'Create Process With Logon W (Only available on W2K +)
				Private Declare Function CreateProcessWithLogonW Lib "advapi32.dll" ( _
					ByVal lpUsername As String, _
					ByVal lpDomain As String, _
					ByVal lpPassword As String, _
					ByVal dwLogonFlags As Long, _
					ByVal lpApplicationName As Long, _
					ByVal lpCommandLine As String, _
					ByVal dwCreationFlags As Long, _
					ByVal lpEnvironment As Long, _
					ByVal lpCurrentDirectory As String, _
					ByRef lpStartupInfo As STARTUPINFO, _
					ByRef lpProcessInformation As PROCESS_INFORMATION _
				) As Long
			'Close Handle
				Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
			'Set Error Mode
				Declare Function SetErrorMode Lib "kernel32.dll" (ByVal uMode As Long) As Long
		
		'Functions
			'Run Process As User
				Public Function RunProcessAsUser(ByVal UserName As String, ByVal Password As String, ByVal DomainName As String, ByVal CommandLine As String, ByVal CurrentDirectory As String) As Long
					'Create Objects
						Dim si As STARTUPINFO
						Dim pi As PROCESS_INFORMATION
					'Set Structure Size
						si.cb = Len(si)
					'Convert Strings to Unicode
						'This part isn't working yet..
						Dim wUser As String = StrConv(UserName + Chr(0), VBUNICODE)
						Dim wDomain As String = StrConv(DomainName + Chr(0), VBUNICODE)
						Dim wPassword As String = StrConv(Password + Chr(0), VBUNICODE)
						Dim wCommandLine As String = StrConv(CommandLine + Chr(0), VBUNICODE)
						Dim wCurrentDir As String = StrConv(CurrentDirectory + Chr(0), VBUNICODE)
					'Create Process
						Dim Result As Long = CreateProcessWithLogonW(wUser, wDomain, wPassword, LOGON_NETCREDENTIALS_ONLY, 0&, wCommandLine, CREATE_DEFAULT_ERROR_MODE, 0&, wCurrentDir, si, pi)
					'Check for Error
						If Result = 0 Then
							Return Err.LastDllError
						End If
					'Cleanup
						CloseHandle(pi.hThread)
						CloseHandle(pi.hProcess)
					'Return
						Return 0
				End Function
	End Class

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DFPITC

ASKER

Thanks, I tried looking for VB code for this for ages.
Not a problem DFPITC, glad to be able to help.