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:
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_28640451.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

DFPITCAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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:
Hi DFPITC;

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

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.
0
Fernando SotoRetiredCommented:
Not a problem DFPITC, glad to be able to help.
0
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
ASP.NET

From novice to tech pro — start learning today.