bat-man
asked on
executing SHELL but running under a different user!
Hi all,
I would like to know how to run another application from VB but under a different user!
Lets say i am logged in as "BOB"
i want my VB app to launch "Explorer.exe" but running under "administrator" not "bob"
does anyone know how to do this!
I would like to know how to run another application from VB but under a different user!
Lets say i am logged in as "BOB"
i want my VB app to launch "Explorer.exe" but running under "administrator" not "bob"
does anyone know how to do this!
You can do this with Shell, but it will prompt for a password. Try this:
Shell("runas /user:administrator explorer.exe")
Shell("runas /user:administrator explorer.exe")
Wow crazycomputers that is good to know. I never knew that, but it still requires a password.
-Brian
-Brian
Of course it requires a password. If it didn't, Windows would be the easiest OS to hack! (Not that it isn't already, but...)
So true, so true, but you can easily register a program as a system process to run under any account.
ASKER
damn, there must be a better way, im doing this because i am tired of typing in passwords!
ASKER
ok, so how do we do this system process thingy?
Maybe you can automate the password process:
Shell ("runas /user:administrator explorer.exe")
AppActivate "C:\WINDOWS\System32\runas .exe", 2000
SendKeys "Password", 2000
SendKeys "{ENTER}", 2000
Shell ("runas /user:administrator explorer.exe")
AppActivate "C:\WINDOWS\System32\runas
SendKeys "Password", 2000
SendKeys "{ENTER}", 2000
If you're tired of typing in passwords, set all your passwords to blank.
No Brian, you cannot use SendKeys to send keystrokes to a DOS box.
give me one minute and ill get a sample together.,
Check out this site, particularily the section on SANUR:
http://is-it-true.org/nt/nt2000/atips/atips12.shtml
http://is-it-true.org/nt/nt2000/atips/atips12.shtml
I cant find my samples on how to send keys to a dos window, but it can be done by copying the text to the clipboard first.
ASKER
i can't set my passwords to nothing :), its the company where i work at, i need to use other accounts to run different processes!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Private Const LOGON_WITH_PROFILE = &H1&
Private Const LOGON_NETCREDENTIALS_ONLY = &H2&
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_CLAS S = &H8000&
Private Const BELOW_NORMAL_PRIORITY_CLAS S = &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&
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 Declare Function GetLastError Lib "kernel32" () As Long
Sub Main()
Dim lpUsername As String, lpDomain As String, lpPassword As String, lpApplicationName As String
Dim lpCommandLine As String, lpCurrentDirectory As String
Dim StartInfo As STARTUPINFO, ProcessInfo As PROCESS_INFORMATION
ParseCommandLine Command(), lpUsername, lpDomain, lpPassword, lpApplicationName
lpCommandLine = vbNullString 'use the same as lpApplicationName
lpCurrentDirectory = vbNullString 'use standard directory
StartInfo.cb = LenB(StartInfo) 'initialize structure
StartInfo.dwFlags = 0&
StartInfo.lpDesktop = StrPtr("winsta0\default")
ret = CreateProcessWithLogon(Str Ptr(lpUser name), StrPtr(lpDomain), StrPtr(lpPassword), 0&, StrPtr(lpApplicationName), StrPtr(lpCommandLine), CREATE_DEFAULT_ERROR_MODE Or CREATE_NEW_CONSOLE Or CREATE_NEW_PROCESS_GROUP, ByVal 0&, StrPtr(lpCurrentDirectory) , StartInfo, ProcessInfo)
r = GetLastError()
CloseHandle ProcessInfo.hThread 'close the handle to the main thread, since we don't use it
CloseHandle ProcessInfo.hProcess 'close the handle to the process, since we don't use it
End Sub
Private Sub ParseCommandLine(ByVal CmdLine As String, ByRef UserName As String, ByRef Domain As String, ByRef Password As String, ByRef ApplicationName As String)
Dim aryParams() As String
aryParams = Split(CmdLine, "/")
UserName = GetParam("USER", aryParams)
Domain = GetParam("DOMAIN", aryParams)
Password = GetParam("PASSWORD", aryParams)
ApplicationName = GetParam("APPLICATION", aryParams)
End Sub
Private Function GetParam(ByVal ParameterName As String, ByRef Params() As String) As String
Dim intElement As Integer
Dim aryElements() As String
GetParam = vbNullString
For intElement = LBound(Params) + 1 To UBound(Params)
aryElements = Split(Params(intElement), "=")
If UCase(aryElements(0)) = ParameterName Then
GetParam = Trim(aryElements(1))
Exit For
End If
Next
End Function
Paste this into a module, compile it to LaunchAsUser.exe (or whatever) then use this to send the parameters e.g.,:
LaunchAsUser /Domain=MyDomain /User=Arnold /Password=Scwharzenegger /Application="c:\myfolder\ myapp.exe param1,param2,param3"
From the command line, or you could use a shell command to do the same thing.
Private Const LOGON_NETCREDENTIALS_ONLY = &H2&
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
Private Const ABOVE_NORMAL_PRIORITY_CLAS
Private Const BELOW_NORMAL_PRIORITY_CLAS
Private Const HIGH_PRIORITY_CLASS = &H80&
Private Const IDLE_PRIORITY_CLASS = &H40&
Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const REALTIME_PRIORITY_CLASS = &H100&
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 Declare Function GetLastError Lib "kernel32" () As Long
Sub Main()
Dim lpUsername As String, lpDomain As String, lpPassword As String, lpApplicationName As String
Dim lpCommandLine As String, lpCurrentDirectory As String
Dim StartInfo As STARTUPINFO, ProcessInfo As PROCESS_INFORMATION
ParseCommandLine Command(), lpUsername, lpDomain, lpPassword, lpApplicationName
lpCommandLine = vbNullString 'use the same as lpApplicationName
lpCurrentDirectory = vbNullString 'use standard directory
StartInfo.cb = LenB(StartInfo) 'initialize structure
StartInfo.dwFlags = 0&
StartInfo.lpDesktop = StrPtr("winsta0\default")
ret = CreateProcessWithLogon(Str
r = GetLastError()
CloseHandle ProcessInfo.hThread 'close the handle to the main thread, since we don't use it
CloseHandle ProcessInfo.hProcess 'close the handle to the process, since we don't use it
End Sub
Private Sub ParseCommandLine(ByVal CmdLine As String, ByRef UserName As String, ByRef Domain As String, ByRef Password As String, ByRef ApplicationName As String)
Dim aryParams() As String
aryParams = Split(CmdLine, "/")
UserName = GetParam("USER", aryParams)
Domain = GetParam("DOMAIN", aryParams)
Password = GetParam("PASSWORD", aryParams)
ApplicationName = GetParam("APPLICATION", aryParams)
End Sub
Private Function GetParam(ByVal ParameterName As String, ByRef Params() As String) As String
Dim intElement As Integer
Dim aryElements() As String
GetParam = vbNullString
For intElement = LBound(Params) + 1 To UBound(Params)
aryElements = Split(Params(intElement), "=")
If UCase(aryElements(0)) = ParameterName Then
GetParam = Trim(aryElements(1))
Exit For
End If
Next
End Function
Paste this into a module, compile it to LaunchAsUser.exe (or whatever) then use this to send the parameters e.g.,:
LaunchAsUser /Domain=MyDomain /User=Arnold /Password=Scwharzenegger /Application="c:\myfolder\
From the command line, or you could use a shell command to do the same thing.
ASKER
cool, that works great , thanks
-Brian