borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (menu handle in vb)Next Topic (Advantage 7.1 Connects) New Topic New Poll Post Reply
AndreaVB Forum : VB General : Run as admin
Poster Message
humberto
Level: VB Lord

Registered: 13-01-2005
Posts: 246

icon Run as admin

How can i start vb application in Administrative right something like runas

01-03-2005 at 02:26 PM
View Profile Send Email to User Show All Posts | Quote Reply
steve_w
Level: Moderator


Registered: 18-04-2003
Posts: 1156
icon Re: Run as admin

Hi

Here's some code I copied off msdn a while back.



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



to use it just see below
Private Sub cmdRun_Click()
    RunAsUser txtUser.Text, txtpwd.Text, txtDomain.Text, txtProg.Text, "c:\"


End Sub
Steve
    


[Edited by steve_w on 01-03-2005 at 02:45 PM GMT]

01-03-2005 at 02:43 PM
View Profile Send Email to User Show All Posts | Quote Reply
humberto
Level: VB Lord

Registered: 13-01-2005
Posts: 246
icon Re: Run as admin

Thanks steve
but can i run the same exe file as admin same project
or need to hve to files..?

01-03-2005 at 03:03 PM
View Profile Send Email to User Show All Posts | Quote Reply
steve_w
Level: Moderator


Registered: 18-04-2003
Posts: 1156
icon Re: Run as admin

Hi

Not sure quite what you mean, you can use the above code to run another program under a different account but it won't change the user it was launched from. If you want to run something in vb as admin then launch vb6 as admin.


Steve  

01-03-2005 at 03:25 PM
View Profile Send Email to User Show All Posts | Quote Reply
humberto
Level: VB Lord

Registered: 13-01-2005
Posts: 246
icon Re: Run as admin

Steve,

Is there are some possibilities  to run the same  program under a different account ..?

For example :
if i have a file (project.exe)
i like that it runs it self as admin; from the same file (project.exe)


[Edited by humberto on 01-03-2005 at 06:22 PM GMT]

01-03-2005 at 05:21 PM
View Profile Send Email to User Show All Posts | Quote Reply
steve_w
Level: Moderator


Registered: 18-04-2003
Posts: 1156
icon Re: Run as admin

Sorry m8 don't know, I created my own RunAs program with the code I posted, from there you can launch your project.exe.
Why would this be different to the project.exe putting itself into an admin account.

Steve

01-03-2005 at 07:38 PM
View Profile Send Email to User Show All Posts | Quote Reply
Goran
Level: Moderator

Registered: 16-05-2002
Posts: 1681
icon Re: Run as admin

AS far as I know, a user will have to know administrator password (which usually is not the case) and has to be prompted to enter it. You can, for instance, when creating a shortcut, add runas syntax in it, and after a user enters correct administrator info, will be able to execute application with administrator rights. Type runas /? and see how this command works.

____________________________
If you find the answer helpful, please mark this topic as solved.

02-03-2005 at 11:55 AM
View Profile Send Email to User Show All Posts | Quote Reply
humberto
Level: VB Lord

Registered: 13-01-2005
Posts: 246
icon Re: Run as admin

why can i  not run a the  exe file

RunAsUser "Humberto", "Password", Computername, "Project.exe", "c:\test\"



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

02-03-2005 at 12:17 PM
View Profile Send Email to User Show All Posts | Quote Reply
humberto
Level: VB Lord

Registered: 13-01-2005
Posts: 246
icon Re: Run as admin

runas can't be run from the server
some one can tell my why..?

02-03-2005 at 03:58 PM
View Profile Send Email to User Show All Posts | Quote Reply
steve_w
Level: Moderator


Registered: 18-04-2003
Posts: 1156
icon Re: Run as admin

Hi

Will the exe in the attached zip work on the server. If it does itw ill be easy to create a runas type program by parsing in the parameters instead of getting them from text boxes.


Steve  

____________________________
Attached:
sw_su.zip 9 KB (Downloads: 24)

02-03-2005 at 04:14 PM
View Profile Send Email to User Show All Posts | Quote Reply
humberto
Level: VB Lord

Registered: 13-01-2005
Posts: 246
icon Re: Run as admin

i have a err missing project.exe path if i run from the server
it work fine loca
also i have Createprocesswithlogonw() failed with error 1326

[Edited by humberto on 02-03-2005 at 05:26 PM GMT]

02-03-2005 at 04:22 PM
View Profile Send Email to User Show All Posts | Quote Reply
steve_w
Level: Moderator


Registered: 18-04-2003
Posts: 1156
icon Re: Run as admin

I found this on a 1326 error

System error 1326 has occurred - Logon failure: unknown user name or bad password.

Sure they're right ?

I know it maybe a different meaning but it is kind of in the right area if you now what I mean.

Steve  

02-03-2005 at 04:49 PM
View Profile Send Email to User Show All Posts | Quote Reply
humberto
Level: VB Lord

Registered: 13-01-2005
Posts: 246
icon Re: Run as admin

yes password and user name are right
but this ocurred if i run the file from the server

02-03-2005 at 05:20 PM
View Profile Send Email to User Show All Posts | Quote Reply
steve_w
Level: Moderator


Registered: 18-04-2003
Posts: 1156
icon Re: Run as admin

Okay found this

quote:
Windows XP SP2 and Windows Server 2003:  You cannot call CreateProcessWithLogonW from a process that is running under the LocalSystem account, because the function uses the logon SID in the caller token, and the token for the LocalSystem account does not contain this SID. As an alternative, use the CreateProcessAsUser and LogonUser functions


http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/createprocessasuser.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/secauthn/security/logonuser.asp

Hope it helps

Steve  
02-03-2005 at 05:35 PM
View Profile Send Email to User Show All Posts | Quote Reply
humberto
Level: VB Lord

Registered: 13-01-2005
Posts: 246
icon Re: Run as admin

thanks Steve

Option Explicit

'//
'// Logon Support APIs
'//

Private Const LOGON32 LOGON INTERACTIVE = 2
Private Const LOGON32 LOGON NETWORK = 3
Private Const LOGON32 LOGON BATCH = 4
Private Const LOGON32 LOGON SERVICE = 5

Private Const LOGON32 PROVIDER DEFAULT = 0
Private Const LOGON32 PROVIDER WINNT35 = 1
'#if( WIN32 WINNT >= 0x0400)
Private Const LOGON32 PROVIDER WINNT40 = 2
'#endif /*  WIN32 WINNT >= 0x0400 */
'#if( WIN32 WINNT >= 0x0500)
Private Const LOGON32 PROVIDER WINNT50 = 3
'#endif // ( WIN32 WINNT >= 0x0500)

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 CloseHandle Lib "kernel32" (ByVal hObject As
Long) As Long

Sub main()
    Dim res As Long
    Dim hToken As Long
    res = LogonUser("User", "DomainName", "Password",
LOGON32 LOGON NETWORK, LOGON32 PROVIDER DEFAULT, hToken)
    Debug.Print res
    Debug.Print hToken
    Call CloseHandle(hToken)
End Sub

02-03-2005 at 05:57 PM
View Profile Send Email to User Show All Posts | Quote Reply
humberto
Level: VB Lord

Registered: 13-01-2005
Posts: 246
icon Re: Run as admin

i need more help
my problem is i try to run one file from the serve
with a local admin account.
i need to run one of this files from the server
file1 run file2 als runas admin
file1 location server
file2 = c:\temp

1) attempt was to run the one file from the server
     and with this file run another from the local drv
     but this one give my path err. it's like not have mapping


03-03-2005 at 06:55 PM
View Profile Send Email to User Show All Posts | Quote Reply
steve_w
Level: Moderator


Registered: 18-04-2003
Posts: 1156
icon Re: Run as admin

I'm quite sure that it won't have the mapping 'cos you logged on as local admin doesn't mean that you will have your logged on user settings available. Is it possible for you to log onto the server as local admin and try running the program.

Out of interest have you used DCOM, it maybe another alternative for you.

Steve  

04-03-2005 at 08:47 AM
View Profile Send Email to User Show All Posts | Quote Reply
humberto
Level: VB Lord

Registered: 13-01-2005
Posts: 246
icon Re: Run as admin

quote:
Out of interest have you used DCOM, it maybe another alternative for you.


Can you explanat my more about DCOM  
04-03-2005 at 05:11 PM
View Profile Send Email to User Show All Posts | Quote Reply
steve_w
Level: Moderator


Registered: 18-04-2003
Posts: 1156
icon Re: Run as admin

Your probably better off looking on the net for an explanation than me giving you one. Look for com or dcom or activex exes.


But I will tell you how to create it.

To create a dcom project you need to have the enterprise edition of vb but not for com. The difference being dcom allows you to run an activexe another computer. But com will do what we want anyway.

Create an Activex exe project
Call the project DCOMExample (or whatever you want, but something original)
Call the class clsgetTime and add the following code
Public Function GetLocaltime() As Date

    GetLocaltime = Now()
    
End Function


In the project properties tab set the unattended execution box ticked. This will send any errors to the event viewer and not the screen.

Make this exe.

Now run the following program

dcomcnfg
Depending on your os you will get a list of the com objects on your pc or you may have to go Component Services(XP), My computer ,DCOM Config.

Find your appliction and right click to get properties. Go to the identity tab and by selecting "This user" enter the local system account (btw I have not tried this but I have for other users)

Now create another new project and add a reference to your com object and you should be able to run your class but under another user

If you have got the enterprise edition you can create a vbr file which you can install on a pc using clireg32 and tell it what computer your dcom is on, and it will then run from there.

Post back if you need more help

Steve  

04-03-2005 at 08:41 PM
View Profile Send Email to User Show All Posts | Quote Reply
waughp
Level: Trainee

Registered: 26-04-2005
Posts: 1
icon Re: Run as admin

Hey Steve,

You seem to be pretty knowledgable with this stuff. Your first post, which had the example on how to run an external program as an administrator is EXACTLY what I need. The private sub on the click is exactly what I've been looking for, for months now! However, I need this exact example to work in VB.net! Any chance you can help me out? This thing is driving me nuts!

Thanks!!!

Pat

26-04-2005 at 03:33 AM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : VB General : Run as admin
Previous Topic (menu handle in vb)Next Topic (Advantage 7.1 Connects) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder