Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1475

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

$
0
0
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
-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).

-Windows XP or newer

Code

Code:

Public Sub LaunchUnelevated(sPath As String, Optional sArgs As String = "")
Dim pShWin As ShellWindows
Set pShWin = New ShellWindows

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.
Dim pServ As IServiceProvider
Dim pSB As IShellBrowser
Dim pDual As IShellFolderViewDual
Dim pView As IShellView

Dim vrEmpty As Variant
Dim hwnd As Long

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.

Viewing all articles
Browse latest Browse all 1475

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>