CAN I USE THE RUNAS COMMAND IN VB 6.0????

J

Jim Carlock

"Confused about Run As" asked:
Please help me. Can the RUNAS command be used within
VB 6.0 ???? If so, please give example.

Yes you can. What is it that you'd like to do exactly? I'm
moving this over to the VB 6 newsgroup to help you get
some more help.

I'm thinking that you'd use the ShellExecute command to
get RunAs.exe working for you. I'm not familiar with the
RunAs parameterization and I'm thinking there might be
a better way to get things done.

If you can, please explain what it is that you wish to
accomplish.
 
M

Matt Williamson

Here is a code snip I found in the groups awhile ago. I've never tested it
though, I just stashed it away in my code database for a rainy day.


Private Const AdminUser = "<enter admin account here>"
Private Const AdminPwd = "<enter admin password here>"

Public Type STARTUPINFOW
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

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

Public Const LOGON_WITH_PROFILE As Long = &H1&
Public Const LOGON_NETCREDENTIALS_ONLY As Long = &H2&
Public Const WAIT_TIMEOUT = 258&
Public Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000

Public Declare Function GetCommandLine Lib "kernel32" Alias _
"GetCommandLineA" () As Long
Public Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" ( _
ByVal lpString1 As String, ByVal lpString2 As Long) As Long
Public Declare Function GetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Public Declare Function CreateProcessWithLogonW Lib "advapi32" ( _
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, lpEnvironment As Any, _
ByVal lpCurrentDirectory As Long, lpStartupInfo As STARTUPINFOW, _
lpProcessInfo As PROCESS_INFORMATION) As Long
Public Declare Function GetComputerName Lib "kernel32" Alias _
"GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Public Declare Function FormatMessage Lib "kernel32" Alias _
"FormatMessageA" (ByVal dwFlags As Long, lpSource As Any, _
ByVal dwMessageId As Long, ByVal dwLanguageId As Long, _
ByVal lpBuffer As String, ByVal nSize As Long, _
Arguments As Long) As Long
Public Declare Function WaitForSingleObject Lib "kernel32" ( _
ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Public Declare Function TerminateProcess Lib "kernel32" ( _
ByVal hProcess As Long, ByVal uExitCode As Long) As Long

Public Function AppPath() As String
Dim lpStr As Long, i As Long
Dim Buffer As String
Dim exePath As String

lpStr = GetCommandLine()
Buffer = Space$(512)
lstrcpy Buffer, lpStr
Buffer = Left$(Buffer, InStr(Buffer & vbNullChar, vbNullChar) - 1)
If Left$(Buffer, 1) = """" Then
i = InStr(2, Buffer, """")
exePath = Mid$(Buffer, 2, i - 2)
Else
i = InStr(Buffer, " ")
exePath = Left$(Buffer, i - 1)
End If
AppPath = Left(exePath, Len(exePath) - InStr(1, StrReverse(exePath), _
"\"))
End Function

Public Function AppExeName() As String
Dim lpStr As Long, i As Long
Dim Buffer As String
Dim exePath As String

lpStr = GetCommandLine()
Buffer = Space$(512)
lstrcpy Buffer, lpStr
Buffer = Left$(Buffer, InStr(Buffer & vbNullChar, vbNullChar) - 1)
If Left$(Buffer, 1) = """" Then
i = InStr(2, Buffer, """")
exePath = Mid$(Buffer, 2, i - 2)
Else
i = InStr(Buffer, " ")
exePath = Left$(Buffer, i - 1)
End If
AppExeName = Mid(exePath, Len(exePath) - InStr(1, _
StrReverse(exePath), "\") + 2)
End Function

Public Function CommandLine() As String
Dim lpStr As Long, i As Long
Dim Buffer As String
Dim cmdLine As String

lpStr = GetCommandLine()
Buffer = Space$(512)
lstrcpy Buffer, lpStr
Buffer = Left$(Buffer, InStr(Buffer & vbNullChar, vbNullChar) - 1)
If Left$(Buffer, 1) = """" Then
i = InStr(2, Buffer, """")
cmdLine = LTrim$(Mid$(Buffer, i + 1))
Else
i = InStr(Buffer, " ")
cmdLine = LTrim$(Mid$(Buffer, i))
End If
CommandLine = cmdLine
End Function

Function UserName() As String
Dim lpBuffer As String
Dim nSize As Long
Dim lError As Long
lpBuffer = Space(255)
nSize = Len(lpBuffer)
Call GetUserName(lpBuffer, nSize)
UserName = Left(lpBuffer, InStr(1, lpBuffer, Chr(0)) - 1)
End Function

Public Function GetErrorMessage(Error As Long) As String
Dim Buffer As String
Dim lBuffer As Long
Buffer = String(1024, 0)
lBuffer = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, ByVal 0&, Error, _
0, Buffer, 200, ByVal 0&)
GetErrorMessage = Left(Buffer, lBuffer)
End Function

Public Function ComputerName() As String
Dim lpBuffer As String
Dim nSize As Long
Dim lError As Long
lpBuffer = Space(255)
nSize = Len(lpBuffer)
Call GetComputerName(lpBuffer, nSize)
ComputerName = Left(lpBuffer, nSize)
End Function

Public Function RunAs(sUser As String, sPwd As String, _
sCmdLine As String, Optional Parameters As String = "", _
Optional Directory As String = "", _
Optional WindowStyle As VbAppWinStyle = vbNormalFocus, _
Optional Wait As Boolean = False, Optional Timeout As Long = -1, _
Optional Terminate As Boolean = False, _
Optional hProcess As Long) As Long
Dim SInfo As STARTUPINFOW
Dim PInfo As PROCESS_INFORMATION
Dim aUser() As String
Dim sDomain As String
Dim sUsername As String
Dim sDir As String
Dim sCmd As String
Dim Res As Long

aUser = Split(sUser, "\")
If UBound(aUser) = 1 Then
sDomain = aUser(0)
sUsername = aUser(1)
Else
sDomain = ComputerName
sUsername = sUser
End If

SInfo.dwFlags = STARTF_USESHOWWINDOW
SInfo.wShowWindow = WindowStyle

If Directory = "" Then
sDir = CurDir
Else
sDir = Directory
End If

If Parameters <> "" Then
sCmd = sCmdLine & " " & Parameters
Else
sCmd = sCmdLine
End If

Res = CreateProcessWithLogonW(StrPtr(sUsername), StrPtr(sDomain), _
StrPtr(sPwd), LOGON_WITH_PROFILE, 0&, StrPtr(sCmd), 0&, ByVal 0&, _
StrPtr(sDir), SInfo, PInfo)

If Res <> 0 Then
hProcess = PInfo.hProcess
If Wait Then
If Timeout > 0 Then Timeout = Timeout * 1000
If WaitForSingleObject(PInfo.hProcess, _
Timeout) = WAIT_TIMEOUT Then
RunAs = WAIT_TIMEOUT
If Terminate Then
If TerminateProcess(PInfo.hProcess, 0) = 0 Then
RunAs = Err.LastDllError
End If
End If
End If
End If
Else
RunAs = Err.LastDllError
hProcess = 0
End If
End Function

Sub Main()
Dim Res As Long

If LCase(UserName) <> LCase(AdminUser) Then
Res = RunAs(AdminUser, AdminPwd, AppPath & "\" & AppExeName, _
CommandLine)
If Res <> 0 Then MsgBox GetErrorMessage(Res)
Exit Sub
Else
MsgBox "Now running in the context of " & UserName & vbNewLine _
& "Add your own processing code here!"
End If
End Sub

HTH

Matt
 
G

Guest

I need to be able to Register 2 Dll's developed in VB 6.0 on our users machines which utilize XP without the users having Admin rights. I have an .exe called StartUp.exe which uses DLLRegisterServer to register the DLL's. This .exe resides on 2 different servers, one is a Test server and another is a Production server. The purpose of the Startup.exe is to determine which server (Test or Prod) that the user is trying to login to, register the dlls, insert the msi file info and call the main application .exe.

Because we have 2 diferent environments on which there could be a different version of the DLL's and the application on one server as opposed to the other, every time the user accesses the application we must unregister and reregister the DLL's

Please tell me what code I would need to add to my StartUp.exe to allow DLLRegisterServer to run as an Admin??
Also, is inserting an MSI file registry entry still allowable in XP without Admin Rights?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top