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;