在 MSDN 上花了一点时间后,我能想到的就是这些了,它与 windows 提供的“运行方式”相同
测试和工作正常
代码 :
Imports System
Imports System.Runtime.InteropServices
Module Module1
Public Infinite As System.UInt32 = Convert.ToUInt32(&HFFFFFFF)
Public Startf_UseStdHandles As Int32 = &H100
Public StdOutputHandle As Int32 = -11
Public StdErrorHandle As Int32 = -12
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Public Structure StartupInfo
Public cb As Integer
Public reserved As String
Public desktop As String
Public title As String
Public x As Integer
Public y As Integer
Public xSize As Integer
Public ySize As Integer
Public xCountChars As Integer
Public yCountChars As Integer
Public fillAttribute As Integer
Public flags As Integer
Public showWindow As UInt16
Public reserved2 As UInt16
Public reserved3 As Byte
Public stdInput As IntPtr
Public stdOutput As IntPtr
Public stdError As IntPtr
End Structure 'StartupInfo
Friend Structure ProcessInformation
Public process As IntPtr
Public thread As IntPtr
Public processId As Integer
Public threadId As Integer
End Structure 'ProcessInformation
<DllImport("advapi32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)> _
Public Function CreateProcessWithLogonW(ByVal userName As String, ByVal domain As String, ByVal password As String, ByVal logonFlags As UInt32, ByVal applicationName As String, ByVal commandLine As String, ByVal creationFlags As UInt32, ByVal environment As UInt32, ByVal currentDirectory As String, ByRef startupInfo As StartupInfo, ByRef processInformation As ProcessInformation) As Boolean
End Function
<DllImport("kernel32.dll", SetLastError:=True)> _
Public Function GetExitCodeProcess(ByVal process As IntPtr, ByRef exitCode As UInt32) As Boolean
End Function
<DllImport("Kernel32.dll", SetLastError:=True)> _
Public Function WaitForSingleObject(ByVal handle As IntPtr, ByVal milliseconds As UInt32) As UInt32
End Function
<DllImport("Kernel32.dll", SetLastError:=True)> _
Public Function GetStdHandle(ByVal handle As IntPtr) As IntPtr
End Function
<DllImport("Kernel32.dll", SetLastError:=True)> _
Public Function CloseHandle(ByVal handle As IntPtr) As Boolean
End Function
<STAThread()> _
Overloads Sub Main(ByVal args() As String)
Dim MyPointer As IntPtr = Marshal.AllocHGlobal(4)
Marshal.WriteInt32(MyPointer, StdOutputHandle)
Dim MyErrorPointer As IntPtr = Marshal.AllocHGlobal(4)
Marshal.WriteInt32(MyErrorPointer, StdErrorHandle)
Dim startupInfo As New StartupInfo
startupInfo.reserved = Nothing
startupInfo.flags = startupInfo.flags And Startf_UseStdHandles
startupInfo.stdOutput = MyPointer
startupInfo.stdError = MyErrorPointer
Dim exitCode As System.UInt32 = Convert.ToUInt32(123456)
Dim processInfo As New ProcessInformation
Dim command As String = "c:\windows\Notepad.exe"
Dim user As String = "administrator"
Dim domain As String = System.Environment.MachineName
Dim password As String = "admin acct password"
Dim currentDirectory As String = System.IO.Directory.GetCurrentDirectory()
Try
CreateProcessWithLogonW(user, domain, password, Convert.ToUInt32(1), _
command, command, Convert.ToUInt32(0), Convert.ToUInt32(0), _
currentDirectory, startupInfo, processInfo)
Catch e As Exception
Console.WriteLine(e.ToString())
End Try
Console.WriteLine("Running ...")
WaitForSingleObject(processInfo.process, Infinite)
GetExitCodeProcess(processInfo.process, exitCode)
Console.WriteLine("Exit code: {0}", exitCode)
CloseHandle(processInfo.process)
CloseHandle(processInfo.thread)
End Sub
End Module