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[])

      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;


      return 0;


Comments (11)

  1. 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!

  2. shiannji says:

    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:


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

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


         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;


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


  3. SecureW2 says:

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



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

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

  6. milindsm says:

    Can a process elevate itself programmatically?

  7. kiran.kashalkar@live.com says:

    Please point to the appropriate place for programmatic elevation.