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

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:

Please see this web page for how to implement CreateProcessWithLogonW and some sample code.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DFPITCAuthor Commented:
Thanks, I tried looking for VB code for this for ages.
Fernando SotoRetiredCommented:
Not a problem DFPITC, glad to be able to help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.