SHARE
TWEET

runas reverse Admin Unelevated UAC ShellExecute Delphi

a guest Nov 18th, 2013 377 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. unit ShellExecuteUnelevated;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows;
  7.  
  8. // This is a reverse to ShellExecute("runas")
  9. // This function will perform ShellExecute in the context of original (unelevated) user
  10. // The user account is taken from running Explorer
  11. procedure ShellExecuteFromExplorer(const AFile: WideString; const AParameters: WideString = ''; const ADirectory: WideString = ''; const AOperation: WideString = ''; const AShowCmd: Cardinal = SW_SHOWNORMAL);
  12.  
  13. implementation
  14.  
  15. uses
  16.   SysUtils, ComObj, ActiveX, ShellAPI, ShlObj, SHDocVw;
  17.  
  18. procedure FindDesktopFolderView(const RIID: TGUID; var PPV);
  19. var
  20.   ShellWindows: IShellWindows;
  21.   Browser: IShellBrowser;
  22.   Disp: IDispatch;
  23.   ServiceProvider: IServiceProvider;
  24.   View: IShellView;
  25.   Loc: OleVariant;
  26.   Empty: OleVariant;
  27.   Wnd: Integer;
  28. begin
  29.   OleCheck(CoCreateInstance(CLASS_ShellWindows, nil, CLSCTX_ALL, IShellWindows, ShellWindows));
  30.  
  31.   Loc := CSIDL_DESKTOP;
  32.   VarClear(Empty);
  33.   Disp := ShellWindows.FindWindowSW(Loc, Empty, SWC_DESKTOP, Wnd, SWFO_NEEDDISPATCH);
  34.  
  35.   OleCheck(Disp.QueryInterface(IServiceProvider, ServiceProvider));
  36.   OleCheck(ServiceProvider.QueryService(SID_STopLevelBrowser, IShellBrowser, Browser));
  37.   OleCheck(Browser.QueryActiveShellView(View));
  38.   OleCheck(View.QueryInterface(RIID, PPV));
  39. end;
  40.  
  41. procedure GetDesktopAutomationObject(const RIID: TGUID; var PPV);
  42. var
  43.   SV: IShellView;
  44.   DispView: IDispatch;
  45. begin
  46.   FindDesktopFolderView(IShellView, SV);
  47.   OleCheck(SV.GetItemObject(SVGIO_BACKGROUND, IDispatch, Pointer(DispView)));
  48.   OleCheck(DispView.QueryInterface(RIID, PPV));
  49. end;
  50.  
  51. procedure ShellExecuteFromExplorer(const AFile: WideString; const AParameters: WideString = ''; const ADirectory: WideString = ''; const AOperation: WideString = ''; const AShowCmd: Cardinal = SW_SHOWNORMAL);
  52. var
  53.   FolderView: IShellFolderViewDual;
  54.   DispShell: IDispatch;
  55.   ShellDispatch: IShellDispatch2;
  56. begin
  57.   GetDesktopAutomationObject(IShellFolderViewDual, FolderView);
  58.   OleCheck(FolderView.get_Application(DispShell));
  59.   OleCheck(DispShell.QueryInterface(IShellDispatch2, ShellDispatch));
  60.   OleCheck(ShellDispatch.ShellExecute(PWideChar(AFile), AParameters, ADirectory, AOperation, AShowCmd));
  61. end;
  62.  
  63. end.
  64.  
  65. Usage:
  66.  
  67. uses
  68.   ShellExecuteUnelevated;
  69.  
  70. ShellExecuteFromExplorer('http://www.microsoft.com/');
  71. ShellExecuteFromExplorer('iexplore.exe', 'http://www.microsoft.com/');
  72. ShellExecuteFromExplorer(ParamStr(0), '/PostInstall');
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top