VB.NET 使用其他用户启动程序
来源:互联网 发布:怎样才可以成为程序员 编辑:程序博客网 时间:2024/04/28 21:46
Imports System.Runtime.InteropServicesImports System.TextPublic Class RunAs Private Const LOGON_WITH_PROFILE = 1 Private Const LOGON_NETCREDENTIALS_ONLY = 2 Private Const CREATE_DEFAULT_ERROR_MODE = &H4000000 Private Const CREATE_NEW_CONSOLE = &H10 Private Const CREATE_NEW_PROCESS_GROUP = &H200 Private Const CREATE_SEPARATE_WOW_VDM = &H800 Private Const CREATE_SUSPENDED = &H4 Private Const CREATE_UNICODE_ENVIRONMENT = &H400 Private Const ABOVE_NORMAL_PRIORITY_CLASS = &H8000& Private Const BELOW_NORMAL_PRIORITY_CLASS = &H4000& Private Const HIGH_PRIORITY_CLASS = &H80& Private Const IDLE_PRIORITY_CLASS = &H40& Private Const NORMAL_PRIORITY_CLASS = &H20& Private Const REALTIME_PRIORITY_CLASS = &H100& <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _ Private Structure STARTUPINFO Public cb As Integer Public lpReserved As String Public lpDesktop As String Public lpTitle As String Public dwX As Integer Public dwY As Integer Public dwXSize As Integer Public dwYSize As Integer Public dwXCountChars As Integer Public dwYCountChars As Integer Public dwFillAttribute As Integer Public dwFlags As Integer Public wShowWindow As Short Public cbReserved2 As Short Public lpReserved2 As Integer Public hStdInput As Integer Public hStdOutput As Integer Public hStdError As Integer End Structure <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _ Private Structure PROCESS_INFORMATION Public hProcess As IntPtr Public hThread As IntPtr Public dwProcessId As Integer Public dwThreadId As Integer End Structure <DllImport("Advapi32", _ SetLastError:=True, _ CharSet:=CharSet.Unicode, _ EntryPoint:="CreateProcessWithLogonW", _ CallingConvention:=CallingConvention.StdCall)> _ Private Shared Function CreateProcessWithLogon( _ ByVal lpUsername As String, _ ByVal lpDomain As String, _ ByVal lpPassword As String, _ ByVal dwLogonFlags As Integer, _ ByVal lpApplicationName As String, _ ByVal lpCommandLine As String, _ ByVal dwCreationFlags As UInteger, _ ByVal lpEnvironment As Integer, _ ByVal lpCurrentDirectory As String, _ ByRef lpStartupInfo As STARTUPINFO, _ <Out()> ByRef lpProcessInfo As PROCESS_INFORMATION) As Boolean End Function <DllImport("kernel32.dll", _ SetLastError:=True, _ CharSet:=CharSet.Ansi)> _ Private Shared Function CloseHandle(ByVal handle As Integer) As Boolean End Function Public Shared Sub BootUpToMcad(ByVal strExeFilepath As String) Try 'CommandLine Dim strCommandLine As String = Environment.CommandLine ' If strCommandLine <> "" Then strCommandLine = " " & strCommandLine End If Dim lResult As Boolean Dim StartInfo As STARTUPINFO = New STARTUPINFO Dim ProcessInfo As PROCESS_INFORMATION = New PROCESS_INFORMATION StartInfo.cb = Marshal.SizeOf(StartInfo) StartInfo.dwFlags = 0 lResult = CreateProcessWithLogon("UserAccount", "", "UserPassword", _ LOGON_WITH_PROFILE, strExeFilepath, strCommandLine, _ CREATE_DEFAULT_ERROR_MODE Or CREATE_NEW_CONSOLE Or CREATE_NEW_PROCESS_GROUP, _ 0, Nothing, StartInfo, ProcessInfo) If (Not lResult) Then Throw New System.ComponentModel.Win32Exception() Else CloseHandle(ProcessInfo.hThread) CloseHandle(ProcessInfo.hProcess) End If Catch ex As Exception Throw ex End Try End SubEnd Class