Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit ShellExecuteUnelevated;
- interface
- uses
- Windows;
- // This is a reverse to ShellExecute("runas")
- // This function will perform ShellExecute in the context of original (unelevated) user
- // The user account is taken from running Explorer
- procedure ShellExecuteFromExplorer(const AFile: WideString; const AParameters: WideString = ''; const ADirectory: WideString = ''; const AOperation: WideString = ''; const AShowCmd: Cardinal = SW_SHOWNORMAL);
- implementation
- uses
- SysUtils, ComObj, ActiveX, ShellAPI, ShlObj, SHDocVw;
- procedure FindDesktopFolderView(const RIID: TGUID; var PPV);
- var
- ShellWindows: IShellWindows;
- Browser: IShellBrowser;
- Disp: IDispatch;
- ServiceProvider: IServiceProvider;
- View: IShellView;
- Loc: OleVariant;
- Empty: OleVariant;
- Wnd: Integer;
- begin
- OleCheck(CoCreateInstance(CLASS_ShellWindows, nil, CLSCTX_ALL, IShellWindows, ShellWindows));
- Loc := CSIDL_DESKTOP;
- VarClear(Empty);
- Disp := ShellWindows.FindWindowSW(Loc, Empty, SWC_DESKTOP, Wnd, SWFO_NEEDDISPATCH);
- OleCheck(Disp.QueryInterface(IServiceProvider, ServiceProvider));
- OleCheck(ServiceProvider.QueryService(SID_STopLevelBrowser, IShellBrowser, Browser));
- OleCheck(Browser.QueryActiveShellView(View));
- OleCheck(View.QueryInterface(RIID, PPV));
- end;
- procedure GetDesktopAutomationObject(const RIID: TGUID; var PPV);
- var
- SV: IShellView;
- DispView: IDispatch;
- begin
- FindDesktopFolderView(IShellView, SV);
- OleCheck(SV.GetItemObject(SVGIO_BACKGROUND, IDispatch, Pointer(DispView)));
- OleCheck(DispView.QueryInterface(RIID, PPV));
- end;
- procedure ShellExecuteFromExplorer(const AFile: WideString; const AParameters: WideString = ''; const ADirectory: WideString = ''; const AOperation: WideString = ''; const AShowCmd: Cardinal = SW_SHOWNORMAL);
- var
- FolderView: IShellFolderViewDual;
- DispShell: IDispatch;
- ShellDispatch: IShellDispatch2;
- begin
- GetDesktopAutomationObject(IShellFolderViewDual, FolderView);
- OleCheck(FolderView.get_Application(DispShell));
- OleCheck(DispShell.QueryInterface(IShellDispatch2, ShellDispatch));
- OleCheck(ShellDispatch.ShellExecute(PWideChar(AFile), AParameters, ADirectory, AOperation, AShowCmd));
- end;
- end.
- Usage:
- uses
- ShellExecuteUnelevated;
- ShellExecuteFromExplorer('http://www.microsoft.com/');
- ShellExecuteFromExplorer('iexplore.exe', 'http://www.microsoft.com/');
- ShellExecuteFromExplorer(ParamStr(0), '/PostInstall');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement