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
Comments
Anonymous
September 25, 2006
PingBack from http://weblog.pigfoot.org/pigfoot/2006/09/26/elevate-through-shellexecute/Anonymous
March 02, 2007
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!Anonymous
March 21, 2007
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.Anonymous
March 27, 2007
How would you advise transfering information between process that calls ShellExecute and the newly (elevated) process? Regards, T.Anonymous
September 18, 2007
The last step in the Vista-related improvements I outlined several months ago involves improving theAnonymous
December 06, 2007
张康宗 http://www.KZTechs.COM Level:300 在Windows Vista里面,进程是分权限级别的。Windows会根据manifest内容、Windows InstallerAnonymous
January 21, 2009
PingBack from http://www.hilpers.it/1717701-ot-windows-vista-e-uacAnonymous
April 25, 2009
PingBack from http://www.drwindows.de/vista-toolbox/11420-script-elevation-powertoys-programmstart-mit-administratorrechten.html#post93686Anonymous
May 01, 2009
PingBack from http://www.supernature-forum.de/betriebssystem-erweiterungen/90846-script-elevation-powertoys-adminrechte-fuer-anwendungen.html#post855674Anonymous
January 26, 2010
Can a process elevate itself programmatically?Anonymous
January 29, 2010
Please point to the appropriate place for programmatic elevation.