For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic > March 2004 > CAN I USE THE RUNAS COMMAND IN VB 6.0????









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author CAN I USE THE RUNAS COMMAND IN VB 6.0????
Jim Carlock

2004-03-30, 6:30 am

"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.

--
Jim Carlock
http://www.microcosmotalk.com/
Post replies to the newsgroup.


Matt Williamson

2004-03-30, 10:30 am

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
"Jim Carlock" <anonymous@127.0.0.1> wrote in message
news:uUg328jFEHA.2944@TK2MSFTNGP12.phx.gbl...
> "Confused about Run As" asked:
>
> 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.
>
> --
> Jim Carlock
> http://www.microcosmotalk.com/
> Post replies to the newsgroup.
>
>



Confused about Run As

2004-03-30, 2:30 pm

I have 2 DLL's that I am registering using DLLRegisterServer in a small exe which serves as a StartUp exe. I have 2 servers that the users can connect to, one is for Testing and another is for Production. So, there are always 2 different versions of the
DLL's and the main app on each server.

The startup exe serves to switch the user from one machine to another depending upon what environment they want to access and it unregisters and registers the DLL's from the server that they chose onto their machine. My company wants to take away the use
rs Admin rights, so I need to know what code would I need to add to my Startup.exe to call DLLRegisterServer with RunAs and give an admin user and password, so that the user is unaware.
Please help!

It also inserts an MSI file registry setting. Does the user need Admin right in order for the exe to continue inserting the msi file setting?
J French

2004-03-31, 5:30 am

On Tue, 30 Mar 2004 10:36:09 -0800,
=?Utf-8?B?Q29uZnVzZWQgYWJvdXQgUnVuIEFz?=
<anonymous@discussions.microsoft.com> wrote:

>I have 2 DLL's that I am registering using DLLRegisterServer in a small exe which serves as a StartUp exe. I have 2 servers that the users can connect to, one is for Testing and another is for Production. So, there are always 2 different versions of th

e DLL's and the main app on each server.
>
>The startup exe serves to switch the user from one machine to another depending upon what environment they want to access and it unregisters and registers the DLL's from the server that they chose onto their machine. My company wants to take away the us

ers Admin rights, so I need to know what code would I need to add to my Startup.exe to call DLLRegisterServer with RunAs and give an admin user and password, so that the user is unaware.
>Please help!


Register both DLLs
Let your startup EXE load the one you want using Late Binding

>It also inserts an MSI file registry setting. Does the user need Admin right in order for the exe to continue inserting the msi file setting?


Don't know - probably
Please Help I need specific Code Ex.

2004-03-31, 10:30 am

have 2 DLL's that I am registering using DLLRegisterServer in a small exe which serves as a StartUp exe. I have 2 servers that the users can connect to, one is for Testing and another is for Production. So, there are always 2 different versions of the
DLL's and the main app on each server.

The startup exe serves to switch the user from one machine to another depending upon what environment they want to access and it unregisters and registers the DLL's from the server that they chose onto their machine. My company wants to take away the use
rs Admin rights, so I need to know what code would I need to add to my Startup.exe to call DLLRegisterServer with RunAs and give an admin user and password, so that the user is unaware.
Please help!

It also inserts an MSI file registry setting. Does the user need Admin right in order for the exe to continue inserting the msi file setting?






Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com