我试图调用CreateProcessWithTokenW并且我已经完成了它,但我无法设法传递lpCommandLine值。它采用称为LPWSTR的格式。我尝试过使用字符串变量和字符串构建器变量(如建议的here),但都没有用。
非常感谢任何支持。
(PS:我已经看到了这个post和post,但他们没有帮助 - 如果可以的话,我正在寻找一个vb.net解决方案找到一个 - )。
已修改为包含代码:
以下是程序中调用CreateProcessWithTokenW:
的代码Private Sub LowerPriviledgeStart(ByVal ProgramName As String, ByVal CommandLine As String, ByVal WorkingDirectory As String)
Try
Dim currentProcess As Process = Process.GetCurrentProcess
'Enable SeIncreaseQuotaPrivilege in this process. (This requires administrative privileges.)
Dim hProcessToken As IntPtr = Nothing
OpenProcessToken(currentProcess.Handle, TOKEN_ADJUST_PRIVILEGES, hProcessToken)
Dim tkp As TOKEN_PRIVILEGES
tkp.PrivilegeCount = 1
LookupPrivilegeValue(Nothing, SE_INCREASE_QUOTA_NAME, tkp.TheLuid)
tkp.Attributes = SE_PRIVILEGE_ENABLED
AdjustTokenPrivileges(hProcessToken, False, tkp, 0, Nothing, Nothing)
'Get window handle representing the desktop shell. This might not work if there is no shell window, or when
'using a custom shell. Also note that we're assuming that the shell is not running elevated.
Dim hShellWnd As IntPtr = GetShellWindow()
'Get the ID of the desktop shell process.
Dim dwShellPID As IntPtr
GetWindowThreadProcessId(hShellWnd, dwShellPID)
'Open the desktop shell process in order to get the process token.
Dim hShellProcess As IntPtr = OpenProcess(PROCESS_QUERY_INFORMATION, False, dwShellPID)
Dim hShellProcessToken As IntPtr = Nothing
Dim hPrimaryToken As IntPtr = Nothing
'Get the process token of the desktop shell.
OpenProcessToken(hShellProcess, TOKEN_DUPLICATE, hShellProcessToken)
'Duplicate the shell's process token to get a primary token.
Dim dwTokenRights As Integer = TOKEN_QUERY Or TOKEN_ASSIGN_PRIMARY Or TOKEN_DUPLICATE Or TOKEN_ADJUST_DEFAULT Or TOKEN_ADJUST_SESSIONID
DuplicateTokenEx(hShellProcessToken, dwTokenRights, Nothing, SecurityImpersonation, TokenPrimary, hPrimaryToken)
Dim si As STARTUPINFO = Nothing
Dim pi As PROCESS_INFORMATION = Nothing
si.cb = Marshal.SizeOf(si)
Dim ptrWorkingDirectory As IntPtr = Marshal.StringToHGlobalAuto(WorkingDirectory)
Dim sbCommandLine As New StringBuilder(256)
sbCommandLine.Append(CommandLine)
CreateProcessWithTokenW(hPrimaryToken, 0, ProgramName, sbCommandLine, 0, Nothing, ptrWorkingDirectory, si, pi)
Catch ex As Exception
End Try
End Sub
<DllImport("advapi32", SetLastError:=True, CharSet:=CharSet.Unicode)>
Public Shared Function CreateProcessWithTokenW(hToken As IntPtr,
dwLogonFlags As Integer,
lpApplicationName As String,
lpCommandLine As StringBuilder,
dwCreationFlags As Integer,
lpEnvironment As IntPtr,
lpCurrentDirectory As IntPtr,
ByRef lpStartupInfo As STARTUPINFO,
ByRef lpProcessInformation As PROCESS_INFORMATION) As Boolean
End Function
上述代码需要在具有提升权限的进程中运行。
使用上面的内容,确实会启动要启动的程序。但是,似乎没有传递Commandline值。
在启动的程序中,我使用以下代码行来获取命令行
Dim CommandLine As String = Microsoft.VisualBasic.Command
正常工作正常。我也试过
Dim CommandLine As String = Microsoft.VisualBasic.Interaction.Command()