Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set "Run this program as an administrator" programmatically

I'm having a problem with good ol' bdeadmin.exe in Vista. First, let's get the predictable responses out of the way:

"You should not require your application to be elevated."
This one does. C'est la vie.

"You need to embed a manifest file."
It is already compiled, it is many years old, the company that created it has no intention of doing it again, and it is installed from a Merge Module (MSM file).

"BDE is obsolete, you should be using dbExpress"
One and a half million lines of code. 'Nuff said.

"Drop a manifest file next to the EXE."
Tried that, did nothing. As a test, that same manifest file was able to make several other EXE files require elevation, just not the one I wanted. Something in there is preventing the external manifest from being read.

"Create a shortcut and set SLDF_RUNAS_USER."
Can't do that, it's a Control Panel applet.

The only thing that worked was setting "Run this program as an administrator" under the Compatibility tab of its Properties window. I shouldn't have to tell users to do this. Bad for business. I need to have the installer do this. The MSM file uses a static path.

like image 677
Patrick Avatar asked Feb 22 '10 18:02

Patrick


People also ask

How do I run a program as administrator without prompt?

Hold down both the Ctrl and the Shift keys on your keyboard and then click or tap on that program's shortcut. You can also use the "Ctrl + Shift + Click/Tap" shortcut on an app's Start Menu tile to run it with administrator permissions in Windows 10.


1 Answers

You can programmatically set the "Run this program as an administrator" flag (the option you find in the Compatibility tab of an EXE's properties), by setting a simple registry key. You need to create a string value (REG_SZ) under one of these keys (if you want the setting to be per user or per machine, respectively):

HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers

or

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers

The name of the value needs to be the full path to your executable (if the path contains spaces, do not surround the path with quotes) and the data of the value must contain the string RUNASADMIN.

For sample:

reg.exe Add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /v "C:\Program Files\MyApp\Test.exe" /d "PUT__VALUE__HERE" 

Compatibility Modes

WIN95 Windows 95
WIN98 Windows 98
WIN4SP5 Windows NT 4.0 SP5
WIN2000 Windows 2000
WINXPSP2 Windows XP SP2
WINXPSP3 Windows XP SP3
VISTARTM Vista
VISTASP1 Vista SP1
VISTASP2 Vista SP2
WIN7RTM Windows 7
WINSRV03SP1 Windows Server 2003 SP1
WINSRV08SP1 Windows Server 2008 SP1

Privilege Level

RUNASADMIN Run program as an administrator

REG ADD "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /v "C:\temp\compatmodel\iconsext.exe" /t REG_SZ /d "WINXPSP3 RUNASADMIN" /f

References: http://www.verboon.info/2011/03/running-an-application-as-administrator-or-in-compatibility-mode/

like image 200
Allon Guralnek Avatar answered Oct 11 '22 11:10

Allon Guralnek