首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

怎么指定以某用户(非当前登录的用户)执行某程序

2012-01-09 
如何指定以某用户(非当前登录的用户)执行某程序?如何指定以某用户(非当前登录的用户)执行某程序?如:当前登

如何指定以某用户(非当前登录的用户)执行某程序?
如何指定以某用户(非当前登录的用户)执行某程序?
如:当前登录的用户普通用户(user1),没有安装权.
现在下载了一个文件了setup.exe,想用(Administrator)执行安装(密码:abcd),应该如何去做呢?

[解决办法]
Option Explicit


Private Const LOGON_WITH_PROFILE = &H1&
Private Const CREATE_DEFAULT_ERROR_MODE = &H4000000
Private Const CREATE_NEW_CONSOLE = &H10&
Private Const CREATE_NEW_PROCESS_GROUP = &H200&

Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessId As Long
dwThreadId As Long
End Type

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 Byte
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type

Private Declare Function CreateProcessWithLogon Lib "advapi32 " Alias "CreateProcessWithLogonW " (ByVal lpUsername As Long, ByVal lpDomain As Long, ByVal lpPassword As Long, ByVal dwLogonFlags As Long, ByVal lpApplicationName As Long, ByVal lpCommandLine As Long, ByVal dwCreationFlags As Long, ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, lpStartupInfo As STARTUPINFO, lpProcessInfo As PROCESS_INFORMATION) As Long
Private Declare Function CloseHandle Lib "kernel32 " (ByVal hObject As Long) As Long
'以下函数用制定名和密码调用了应用程序
Private Function AnShell(Username As String, Domain As String, Password As String, ApplicationName As String) As Long
Dim lpUsername As String, lpDomain As String, lpPassword As String, lpApplicationName As String, lpCommandLine As String, lpCurrentDirectory As String
Dim StartInfo As STARTUPINFO, ProcessInfo As PROCESS_INFORMATION
lpUsername = Username
lpDomain = Domain
lpPassword = Password
lpApplicationName = ApplicationName
lpCommandLine = vbNullString
lpCurrentDirectory = vbNullString
StartInfo.cb = LenB(StartInfo)
StartInfo.dwFlags = 0&
CreateProcessWithLogon StrPtr(lpUsername), StrPtr(lpDomain), StrPtr(lpPassword), LOGON_WITH_PROFILE, StrPtr(lpApplicationName), StrPtr(lpCommandLine), CREATE_DEFAULT_ERROR_MODE Or CREATE_NEW_CONSOLE Or CREATE_NEW_PROCESS_GROUP, ByVal 0&, StrPtr(lpCurrentDirectory), StartInfo, ProcessInfo

CloseHandle ProcessInfo.hThread
CloseHandle ProcessInfo.hProcess
AnShell = ProcessInfo.dwProcessId
End Function

Private Sub Form_Click()
'用刚刚建立的管理员运行一个程序,这里是记事本,参数1为用户名,参数2为域(一般为空就可以了,因为是本机),参数3为要运行的程序路径和名字(这里调用的是WINDOWS下的程序,所以WINDOWS会自己找到的,程序运行时的几个PATH是WINDOWS,SYSTEM,SYSTEM32,APP.PATH,当然可以通过修改WINDOWS的PATH来改变)
AnShell "mAdmin ", " ", "mAdmin@ ", "mstsc.exe "
'打开任务管理器,以便查看notepad.exe进程的所有者
End Sub

热点排行