Avatar of DFPITC
Flag 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:

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?


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

	Public Class RunAsUser
		'Taken from https://support.microsoft.com/en-us/kb/285879
			'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
			Private Const CREATE_DEFAULT_ERROR_MODE = &H4000000
			Private Const LOGON_WITH_PROFILE = &H1
			Private Const LOGON32_LOGON_INTERACTIVE = 2
			Private Const LOGON32_PROVIDER_DEFAULT = 0
			Private Const VBUNICODE = 64
			'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
			'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
					'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
						Return 0
				End Function
	End Class

Open in new window

ASP.NETVisual Basic.NET.NET Programming

Avatar of undefined
Last Comment
Fernando Soto

8/22/2022 - Mon
Fernando Soto

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.

Thanks, I tried looking for VB code for this for ages.
Fernando Soto

Not a problem DFPITC, glad to be able to help.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.