Results 1 to 7 of 7

Thread: ShellExecute RunAS

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2015
    Posts
    11

    ShellExecute RunAS

    Hello,
    Can you tell me how to pass as a parameter the user and password in a RunAs ShellExecute?
    I tried this, but does not.

    Code:
    ShellExecute hWnd, "runas /profile /user:" & txtdom.Text & "\" & txtuser.Text & " " & App.Path & "\Project1.exe", "", "", "", vbNormalFocus

  2. #2
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: ShellExecute RunAS

    You may want to try a different API instead - CreateProcessWithLogonW:

    Code:
    Private Enum CreationFlags
        CREATE_SUSPENDED = &H4
        CREATE_NEW_CONSOLE = &H10
        CREATE_NEW_PROCESS_GROUP = &H200
        CREATE_UNICODE_ENVIRONMENT = &H400
        CREATE_SEPARATE_WOW_VDM = &H800
        EXTENDED_STARTUPINFO_PRESENT = &H80000
        CREATE_DEFAULT_ERROR_MODE = &H4000000
    End Enum
    #If False Then
        Dim CREATE_SUSPENDED, CREATE_NEW_CONSOLE, CREATE_NEW_PROCESS_GROUP, CREATE_UNICODE_ENVIRONMENT, _
            CREATE_SEPARATE_WOW_VDM, EXTENDED_STARTUPINFO_PRESENT, CREATE_DEFAULT_ERROR_MODE
    #End If
    
    Private Enum LogonFlags
        LOGON_WITH_PROFILE = &H1
        LOGON_NETCREDENTIALS_ONLY = &H2
    End Enum
    #If False Then
        Dim LOGON_WITH_PROFILE, LOGON_NETCREDENTIALS_ONLY
    #End If
    
    Private Enum STARTUPINFO_Flags
        STARTF_USESHOWWINDOW = &H1
        STARTF_USESIZE = &H2
        STARTF_USEPOSITION = &H4
        STARTF_USECOUNTCHARS = &H8
        STARTF_USEFILLATTRIBUTE = &H10
        STARTF_RUNFULLSCREEN = &H20
        STARTF_FORCEONFEEDBACK = &H40
        STARTF_FORCEOFFFEEDBACK = &H80
        STARTF_USESTDHANDLES = &H100
        STARTF_USEHOTKEY = &H200
        STARTF_TITLEISLINKNAME = &H800
        STARTF_TITLEISAPPID = &H1000
        STARTF_PREVENTPINNING = &H2000
    End Enum
    #If False Then
        Dim STARTF_USESHOWWINDOW, STARTF_USESIZE, STARTF_USEPOSITION, _
        STARTF_USECOUNTCHARS, STARTF_USEFILLATTRIBUTE, STARTF_RUNFULLSCREEN, _
        STARTF_FORCEONFEEDBACK, STARTF_FORCEOFFFEEDBACK, STARTF_USESTDHANDLES, _
        STARTF_USEHOTKEY, STARTF_TITLEISLINKNAME, STARTF_TITLEISAPPID, STARTF_PREVENTPINNING
    #End If
    
    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 STARTUPINFO_Flags
        wShowWindow     As Integer
        cbReserved2     As Integer
        lpReserved2     As Long
        hStdInput       As Long
        hStdOutput      As Long
        hStdError       As Long
    End Type
    
    Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
    Private Declare Function CreateProcessWithLogonW Lib "advapi32.dll" ( _
        ByVal lpUsername As Long, _
        ByVal lpDomain As Long, _
        ByVal lpPassword As Long, _
        ByVal dwLogonFlags As LogonFlags, _
        ByVal lpApplicationName As Long, _
        ByVal lpCommandLine As Long, _
        ByVal dwCreationFlags As CreationFlags, _
        ByVal lpEnvironment As Long, _
        ByVal lpCurrentDirectory As Long, _
        ByRef lpStartupInfo As STARTUPINFO, _
        ByRef lpProcessInfo As PROCESS_INFORMATION _
    ) As Long
    
    Private Function CreateProcessWithLogon(ByRef UserName As String, _
                                            ByRef Domain As String, _
                                            ByRef Password As String, _
                                            ByRef AppFileName As String, _
                                   Optional ByRef CmdLine As String) As Boolean
        Dim PI As PROCESS_INFORMATION, SI As STARTUPINFO
    
        SI.cb = LenB(SI)
    
        CreateProcessWithLogon = CreateProcessWithLogonW(StrPtr(UserName), _
                                                         StrPtr(Domain), _
                                                         StrPtr(Password), 0&, _
                                                         StrPtr(AppFileName), _
                                                         StrPtr(CmdLine), 0&, 0&, 0&, _
                                                         SI, PI)
        If CreateProcessWithLogon Then
            PI.hThread = CloseHandle(PI.hThread):   Debug.Assert PI.hThread
            PI.hProcess = CloseHandle(PI.hProcess): Debug.Assert PI.hProcess
        End If
    End Function
    Code:
    CreateProcessWithLogon txtUser.Text, txtDom.Text, txtPW.Text, App.Path & "\Project1.exe"
    Please read the given documentation link very carefully in order to understand how to use that function.



    EDIT

    I just found out that Microsoft had a KB article for that API: How to start a process as another user from Visual Basic.
    Last edited by Bonnie West; Jun 8th, 2015 at 02:13 PM. Reason: Changed Strings in STARTUPINFO to Longs per the KB article.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2015
    Posts
    11

    Re: ShellExecute RunAS

    Could you give me an example of using your code?
    I did not understand are operating.

  4. #4
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: ShellExecute RunAS

    Quote Originally Posted by tonton View Post
    Could you give me an example of using your code?
    I already did. Did you not notice this line?

    Quote Originally Posted by Bonnie West View Post
    Code:
    CreateProcessWithLogon txtUser.Text, txtDom.Text, txtPW.Text, App.Path & "\Project1.exe"
    In that example, I was assuming that the password is entered in a TextBox named "txtPW". If your TextBox is named differently, change it as appropriate.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2015
    Posts
    11

    Re: ShellExecute RunAS

    I do not understand the "If False Then" outside the code.

  6. #6
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: ShellExecute RunAS

    Quote Originally Posted by tonton View Post
    I do not understand the "If False Then" outside the code.
    See this post.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  7. #7

    Thread Starter
    New Member
    Join Date
    May 2015
    Posts
    11

    Re: ShellExecute RunAS

    Ok, very well, thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width