- Private Type STARTUPINFO
- cb As Long
- lpReserved As Long
- lpDesktop As Long
- lpTitle As Long
- dwX As Long
- dwY As Long
- dwXSize As Long
- dwYSize As Long
- dwXCountChars As Long
- dwYCountChars As Long
- dwFillAttribute As Long
- dwFlags As Long
- wShowWindow As Integer
- cbReserved2 As Integer
- lpReserved2 As Long
- hStdInput As Long
- hStdOutput As Long
- hStdError As Long
- End Type
- Private Type PROCESS_INFORMATION
- hProcess As Long
- hThread As Long
- dwProcessId As Long
- dwThreadId As Long
- End Type
-
- Private Declare Function LogonUser Lib "Advapi32" 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
- Private Declare Function CreateProcessAsUserA Lib "kernel32" (ByVal hToken As Long, ByVal lpApplicationName As String, ByVal lpCommandLine As String, ByVal lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, ByVal lpEnvironment As String, ByVal lpCurrentDirectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
-
- Private Const LOGON32_LOGON_INTERACTIVE = 2
- Private Const LOGON32_PROVIDER_DEFAULT = 0
- Private Const NORMAL_PRIORITY_CLASS = &H20
- Private Const INVALID_HANDLE_VALUE = -1
-
- Function CreateProcessAsUser(ByVal UserName As String, ByVal Domain As String, ByVal Password As String, ByVal CommandLine As String) As Long
- Dim hToken As Long
- Dim SI As STARTUPINFO
- Dim PI As PROCESS_INFORMATION
- SI.cb = Len(SI)
- CreateProcessAsUser = INVALID_HANDLE_VALUE
- If Not LogonUser(UserName, Domain, Password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, hToken) Then Stop
- Debug.Print Err.LastDllError
- If Not CreateProcessAsUserA(hToken, vbNullString, CommandLine, ByVal 0&, ByVal 0&, False, NORMAL_PRIORITY_CLASS, ByVal 0&, ByVal 0&, SI, PI) Then Exit Function
- CreateProcessAsUser = PI.hProcess
- End Function
-
- Private Sub Form_Load()
- CreateProcessAsUser "TestUser", "DOMKRATT_PC", "MegaPassword1", "notepad.exe"
- End Sub
А вообще проще юзать АПИ CreateProcessWithLogon
Ответить
|