本文介绍如何重定向从标准输入句柄接收输入或将输出发送到标准输出句柄的子进程的输入和输出。
原始产品版本: Visual Basic
原始 KB 数: 285879
总结
本文介绍如何以编程方式以 Microsoft Visual Basic 中的其他用户身份启动进程。 为此,可以在运行 Windows NT 4.0 的计算机上使用 LogonUser Win32 CreateProcessAsUser API,也可以在运行 Windows 2000 或更高版本的计算机上使用 CreateProcessWithLogonW Win32 API。 CreateProcessWithLogonW 无法从 LocalSystem 帐户下的进程调用。
详细信息
本文包含检测操作系统版本的示例 Visual Basic 代码。 然后,它使用相应的 API 以其他用户身份启动进程。
Windows NT 4.0
若要使用
LogonUser,CreateProcessAsUser并且调用用户帐户必须具有特定权限。若要使用
LogonUser(),呼叫用户帐户必须具有以下权限:权限显示名称
SE_TCB_NAME作为操作系统的一部分若要使用
CreateProcessAsUser(),调用用户帐户必须具有以下两个权限:权限显示名称
SE_ASSIGNPRIMARYTOKEN_NAME替换进程级别令牌SE_INCREASE_QUOTA_NAME增加配额
如果调用用户帐户没有“充当操作系统的一部分”的权限,API LogonUser() 将失败并生成返回值零。 如果调用 Err.LastDllError,将收到错误消息 1314。 此消息表示客户端不保留所需的权限。 同样,如果调用用户帐户没有两个权限来“替换进程级别令牌”和“增加配额”,API CreateProcessAsUser() 将失败并生成错误消息 1314。
如果以其他用户身份启动交互式应用程序,则必须有权访问名为 winsta0\default 的交互式窗口工作站和桌面。 如果应用程序是交互式的,则调用方需要以编程方式向 winsta0\default 添加所需的权限。 之后,调用方可以在下面的示例 Visual Basic 代码中调用 RunAsUser 帮助程序函数。
必须向指定的 LogonUser() 用户帐户授予足够的权限,以便交互式应用程序能够成功启动。 以下知识库文章包含示例 Visual Basic 代码,可用于更新窗口工作站和桌面的权限。
Windows 2000 及更高版本
API CreateProcessWithLogonW() 已在 Windows 2000 中引入。 调用 CreateProcessWithLogonW() 不需要为调用用户帐户授予权限,就像使用 LogonUser 和 CreateProcessAsUser API 一样。
将来,请使用 CreateProcessWithLogonW() API。 它处理与继承的窗口工作站和桌面关联的权限。 在此方案中,应用程序只需调用 RunAsUser 以下示例 Visual Basic 代码中的帮助程序函数。
Option Explicit
Private Const CREATE_DEFAULT_ERROR_MODE = &H4000000
Private Const LOGON_WITH_PROFILE = &H1
Private Const LOGON_NETCREDENTIALS_ONLY = &H2
Private Const LOGON32_LOGON_INTERACTIVE = 2
Private Const LOGON32_PROVIDER_DEFAULT = 0
Private Type STARTUPINFO
cb As Long
lpReserved As Long ' !!! must be Long for Unicode string
lpDesktop As Long ' !!! must be Long for Unicode string
lpTitle As Long ' !!! must be Long for Unicode string
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
' LogonUser() requires that the caller has the following permission
' Permission Display Name
' --------------------------------------------------------------------
' SE_TCB_NAME Act as part of the operating system
' CreateProcessAsUser() requires that the caller has the following permissions
' Permission Display Name
' ---------------------------------------------------------------
' SE_ASSIGNPRIMARYTOKEN_NAME Replace a process level token
' SE_INCREASE_QUOTA_NAME Increase quotas
Private Declare Function LogonUser Lib "advapi32.dll" 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 CreateProcessAsUser Lib "advapi32.dll" _Alias "CreateProcessAsUserA" _(ByVal hToken As Long, _ByVal lpApplicationName As Long, _ByVal lpCommandLine As String, _ByVal lpProcessAttributes As Long, _ByVal lpThreadAttributes As Long, _ByVal bInheritHandles As Long, _ByVal dwCreationFlags As Long, _ByVal lpEnvironment As Long, _ByVal lpCurrentDirectory As String, _lpStartupInfo As STARTUPINFO, _lpProcessInformation As PROCESS_INFORMATION) As Long
' CreateProcessWithLogonW API is available only on Windows 2000 and later.
Private Declare Function CreateProcessWithLogonW Lib "advapi32.dll" _(ByVal lpUsername As String, _ByVal lpDomain As String, _ByVal lpPassword As String, _ByVal dwLogonFlags As Long, _ByVal lpApplicationName As Long, _ByVal lpCommandLine As String, _ByVal dwCreationFlags As Long, _ByVal lpEnvironment As Long, _ByVal lpCurrentDirectory As String, _ByRef lpStartupInfo As STARTUPINFO, _ByRef lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function CloseHandle Lib "kernel32.dll" _(ByVal hObject As Long) As Long
Private Declare Function SetErrorMode Lib "kernel32.dll" _(ByVal uMode As Long) As Long
Private Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128
End Type
' Version Checking APIs
Private Declare Function GetVersionExA Lib "kernel32.dll" _(lpVersionInformation As OSVERSIONINFO) As Integer
Private Const VER_PLATFORM_WIN32_NT = &H2
'********************************************************************' RunAsUser for Windows 2000 and Later
'********************************************************************
Public Function W2KRunAsUser(ByVal UserName As String, _ByVal Password As String, _ByVal DomainName As String, _ByVal CommandLine As String, _ByVal CurrentDirectory As String) As Long
Dim si As STARTUPINFO
Dim pi As PROCESS_INFORMATION
Dim wUser As String
Dim wDomain As String
Dim wPassword As String
Dim wCommandLine As String
Dim wCurrentDir As String
Dim Result As Long
si.cb = Len(si)
wUser = StrConv(UserName + Chr$(0), vbUnicode)
wDomain = StrConv(DomainName + Chr$(0), vbUnicode)
wPassword = StrConv(Password + Chr$(0), vbUnicode)
wCommandLine = StrConv(CommandLine + Chr$(0), vbUnicode)
wCurrentDir = StrConv(CurrentDirectory + Chr$(0), vbUnicode)
Result = CreateProcessWithLogonW(wUser, wDomain, wPassword, _LOGON_WITH_PROFILE, 0&, wCommandLine, _CREATE_DEFAULT_ERROR_MODE, 0&, wCurrentDir, si, pi)' CreateProcessWithLogonW() does not
If Result <> 0 Then
CloseHandle pi.hThread
CloseHandle pi.hProcess
W2KRunAsUser = 0
Else
W2KRunAsUser = Err.LastDllError
MsgBox "CreateProcessWithLogonW() failed with error " & Err.LastDllError, vbExclamation
End If
End Function
'********************************************************************' RunAsUser for Windows NT 4.0
'********************************************************************
Public Function NT4RunAsUser(ByVal UserName As String, _ByVal Password As String, _ByVal DomainName As String, _ByVal CommandLine As String, _ByVal CurrentDirectory As String) As Long
Dim Result As Long
Dim hToken As Long
Dim si As STARTUPINFO
Dim pi As PROCESS_INFORMATION
Result = LogonUser(UserName, DomainName, Password, LOGON32_LOGON_INTERACTIVE, _
LOGON32_PROVIDER_DEFAULT, hToken)
If Result = 0 Then
NT4RunAsUser = Err.LastDllError
' LogonUser will fail with 1314 error code, if the user account associated
' with the calling security context does not have
' "Act as part of the operating system" permission
MsgBox "LogonUser() failed with error " & Err.LastDllError, vbExclamation
Exit Function
End If
si.cb = Len(si)
Result = CreateProcessAsUser(hToken, 0&, CommandLine, 0&, 0&, False, _
CREATE_DEFAULT_ERROR_MODE, _
0&, CurrentDirectory, si, pi)
If Result = 0 Then
NT4RunAsUser = Err.LastDllError
' CreateProcessAsUser will fail with 1314 error code, if the user
' account associated with the calling security context does not have
' the following two permissions
' "Replace a process level token"
' "Increase Quotoas"
MsgBox "CreateProcessAsUser() failed with error " & Err.LastDllError, vbExclamation
CloseHandle hToken
Exit Function
End If
CloseHandle hToken
CloseHandle pi.hThread
CloseHandle pi.hProcess
NT4RunAsUser = 0
End Function
Public Function RunAsUser(ByVal UserName As String, _ByVal Password As String, _ByVal DomainName As String, _ByVal CommandLine As String, _ByVal CurrentDirectory As String) As Long
Dim w2kOrAbove As Boolean
Dim osinfo As OSVERSIONINFO
Dim Result As Long
Dim uErrorMode As Long
' Determine if system is Windows 2000 or later
osinfo.dwOSVersionInfoSize = Len(osinfo)
osinfo.szCSDVersion = Space$(128)
GetVersionExA osinfo
w2kOrAbove = _
(osinfo.dwPlatformId = VER_PLATFORM_WIN32_NT And _
osinfo.dwMajorVersion >= 5)
If (w2kOrAbove) Then
Result = W2KRunAsUser(UserName, Password, DomainName, _
CommandLine, CurrentDirectory)
Else
Result = NT4RunAsUser(UserName, Password, DomainName, _
CommandLine, CurrentDirectory)
End If
RunAsUser = Result
End Function