Wednesday, October 13, 2010

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;
}

No comments:

Post a Comment