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

From: http://blogs.msdn.com/vistacompatteam/archive/2006/09/25/771232.aspx

你可能感兴趣的:(windows)