Vista Compatibility Team Blog

Elevate through ShellExecute

We often get the question how to elevate a process through ShellExecute. From the docs it is not immediately clear. The trick is passing in "runas" in the lpVerb.

Here is a snippet to run notepad elevated.

#include "stdafx.h"
#include "windows.h"
#include "shellapi.h"


int _tmain(int argc, _TCHAR* argv[])
{
      SHELLEXECUTEINFO shExecInfo;

      shExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);

      shExecInfo.fMask = NULL;
      shExecInfo.hwnd = NULL;
      shExecInfo.lpVerb = L"runas";
      shExecInfo.lpFile = L"notepad.exe";
      shExecInfo.lpParameters = NULL;
      shExecInfo.lpDirectory = NULL;
      shExecInfo.nShow = SW_MAXIMIZE;
      shExecInfo.hInstApp = NULL;

      ShellExecuteEx(&shExecInfo);

      return 0;
}

Maarten

Published Monday, September 25, 2006 11:18 PM by VistaCompatTeam

Comments

 

pigfoot’s weblog » Elevate through ShellExecute said:

September 26, 2006 3:20 AM
 

Aaron Ballman said:

For anyone else reading this, you should note that passing in NULL for the window handle is not advised since this can (and usually will) cause a dialog to be displayed.  (This assumes your application has some UI which triggers this action).  If you pass in NULL, then the dialog will be associated with the desktop window and be centered on the screen.  Instead, if you assign a valid window handle to it, the dialog will be displayed over the window whose handle you specify.  That provides a good visual correlation between the dialog which is displayed and the window which causes the action.  Just a little usability tip.  :-)

Otherwise, awesome blog topic!

March 2, 2007 4:14 PM
 

shiannji said:

Hi Maarten:

   I am having trouble with this function "ShellExecute" or "ShellExecuteEx".

   I hope to run telnet in Viata elevated thought ShellExecute.

   I have had to install the telnet client program.

   I tried the same as above syntax, but the system is passed as follow error message:

         "Windows cannot find 'telnet.exe'. Make sure you typed the name correctly, and then try again."

snippet code:

ShellExecute:

wsprintf(str, "telnet://%s", inet_ntoa(ip));

HINSTANCE ret = ShellExecute(Handle, NULL, str, 0, 0, SW_SHOWNORMAL);

ShellExecuteEx:

     SHELLEXECUTEINFO shExecInfo;

     shExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);

     shExecInfo.fMask = NULL;

     shExecInfo.hwnd = NULL;

     shExecInfo.lpVerb = "runas";

     shExecInfo.lpFile = "telnet.exe";

     shExecInfo.lpParameters = inet_ntoa(ip);

     shExecInfo.lpDirectory = "C:\\Windows\\System32";

     shExecInfo.nShow = SW_SHOWNORMAL;

     shExecInfo.hInstApp = NULL;

     ShellExecuteEx(&shExecInfo);

Would you tell me, how can i do. Thanks a lot and so sorry my English is so poor.

Frank.

March 22, 2007 2:28 AM
 

SecureW2 said:

How would you advise transfering information between process that calls ShellExecute and the newly (elevated) process?

Regards,

T.

March 27, 2007 2:28 PM
 

Re-inventing The Wheel said:

The last step in the Vista-related improvements I outlined several months ago involves improving the

September 18, 2007 5:02 PM
 

Smallfrogs' Technical BLOG said:

张康宗 http://www.KZTechs.COM Level:300 在Windows Vista里面,进程是分权限级别的。Windows会根据manifest内容、Windows Installer

December 6, 2007 7:05 PM
 

[OT] Windows Vista e UAC | hilpers said:

January 21, 2009 1:16 PM
 

Script Elevation PowerToys - Programmstart mit Administratorrechten - drwindows.de said:

April 26, 2009 4:58 AM
 

Download Script Elevation PowerToys - Adminrechte f?r Anwendungen - Supernature-Forum said:

May 2, 2009 7:59 AM
 

milindsm said:

Can a process elevate itself programmatically?

January 27, 2010 4:29 AM
 

kiran.kashalkar@live.com said:

Please point to the appropriate place for programmatic elevation.

January 29, 2010 11:00 AM
Anonymous comments are disabled

© 2010 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker