很可能您必須使用Win32 API創建自己的「shell」函數。
使用CreateProcessWithLogonW API,您可以在不同憑據下創建新進程,並可以選擇加載用戶配置文件信息。
在下面的代碼片段,如果你更換
- 用戶名 - 與您的用戶名
- 域 - 與您的域名或 「vbNullString」
- 密碼 - 與您的密碼
- 參數4 - 取代0,'LOGON WITH PROFILE'加載指定的用戶配置文件。
有關更多詳細信息,請參閱CreateProcessWithLogonW API的文檔。通過這條路線,您可以完全控制並全面負責啓動應用程序。
再次,這只是一個示例,你可能需要玩一點它來做你想做的。
Imports System.Runtime.InteropServices
Public Module modShell
<StructLayout(LayoutKind.Sequential)> _
Public 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)> _
Public Structure PROCESS_INFORMATION
Public hProcess As IntPtr
Public hThread As IntPtr
Public dwProcessId As Integer
Public dwThreadId As Integer
End Structure
Public Declare Unicode Function CreateProcessWithLogonW Lib "Advapi32" (ByVal lpUsername As String, ByVal lpDomain As String, ByVal lpPassword As String, ByVal dwLogonFlags As Int32, ByVal lpApplicationName As String, ByVal lpCommandLine As String, ByVal dwCreationFlags As Int32, ByVal lpEnvironment As IntPtr, ByVal lpCurrentDirectory As String, ByRef si As STARTUPINFO, ByRef pi As PROCESS_INFORMATION) As Integer
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As IntPtr) As Integer
Public Const LOGON_WITH_PROFILE As Int32 = &H1
Public Const NORMAL_PRIORITY_CLASS As Int32 = &H20&
Public Const STARTF_USESHOWWINDOW As Int32 = &H1
Public Const SW_HIDE As Int16 = 0
Public Const SW_SHOW As Int16 = 5
Public Function Shell(ByVal strCmdLine As String, ByVal strCurrentDirectory As String) As Boolean
Dim pi As PROCESS_INFORMATION
Dim si As New STARTUPINFO
si.cb = Marshal.SizeOf(si)
si.dwFlags = STARTF_USESHOWWINDOW
si.wShowWindow = SW_SHOW
Dim result As Integer = CreateProcessWithLogonW("username", "domain", "password", 0, vbNullString, strCmdLine, NORMAL_PRIORITY_CLASS, IntPtr.Zero, strCurrentDirectory, si, pi)
If result <> 0 Then
Call CloseHandle(pi.hThread)
Call CloseHandle(pi.hProcess)
Else
Return False
End If
Return True
End Function
End Module
我會試試看看會發生什麼。如果我可以做到這一點,而不必使用第二個EXE作爲存根,那麼我會很高興,如果UAC啓用時不會發生,我會處理提示。我嘗試過之後我會成爲bakc。 – SqlRyan 2009-01-26 20:08:08
@SqlRyan,並與啓用UAC工作? – Jet 2013-03-31 13:59:57