Results 1 to 1 of 1

Thread: [VB6] Code snippet: Run unelevated app from elevated app

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    7,054

    [VB6, twinBASIC] Code snippet: Run unelevated app from elevated app

    Surprised I didn't see an example of this, so wanted to post it.

    Here's a quick implementation of a method to run unelevated apps from your elevated app by routing it through Explorer, as outlined by Raymond Chen.

    Requirements
    -VB6: oleexp.tlb v5.01 or higher, with included addon mIID.bas (released the same day as this snippet... I had a partial set of the shell automation objects in oleeximp.tlb, not sure why it was complete, or not in oleexp.tlb, so for convenience I put out a quick new version with a complete set in oleexp.tlb. So you only need oleexp.tlb 5.01+ (and mIID.bas) if you get the new version. Otherwise that, oleexpimp.tlb, and shell32).

    -twinBASIC: Windows Development Library for twinBASIC (WinDevLib). References->Available Packages

    -Windows XP or newer

    Code

    Code:
    Public Sub LaunchUnelevated(sPath As String, Optional sArgs As String = "")
    Dim pShWin As ShellWindows
    Set pShWin = New ShellWindows
    #If TWINBASIC Then
    Dim pDispView As IDispatch
    #Else
    Dim pDispView As oleexp.IDispatch 'VB6 has a built in hidden version that will cause an error if you try to use it. Specify oleexp's unrestricted version.
    #End If
    Dim pServ As IServiceProvider
    Dim pSB As IShellBrowser
    Dim pDual As IShellFolderViewDual
    Dim pView As IShellView
    Dim hwnd As LongPtr 
    Dim vrEmpty As Variant
     
    
    Set pServ = pShWin.FindWindowSW(CVar(CSIDL_DESKTOP), vrEmpty, SWC_DESKTOP, hwnd, SWFO_NEEDDISPATCH)
    
    pServ.QueryService SID_STopLevelBrowser, IID_IShellBrowser, pSB
    
    pSB.QueryActiveShellView pView
    
    pView.GetItemObject SVGIO_BACKGROUND, IID_IDispatch, pDispView
    Set pDual = pDispView
     
    Dim pDispShell As IShellDispatch2
    Set pDispShell = pDual.Application
    
    If sArgs <> "" Then
        pDispShell.ShellExecute sPath, CVar(sArgs)
    Else
        pDispShell.ShellExecute sPath
    End If
    End Sub
    And it's that simple. Just call LaunchUnelevated with a path to the exe.
    Last edited by fafalone; Apr 25th, 2025 at 01:53 PM. Reason: Update for tB/WDL/x64

Tags for this Thread

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