Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
551 views
in Technique[技术] by (71.8m points)

windows - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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_USERSoftwareMicrosoftWindows NTCurrentVersionAppCompatFlagsLayers

or

HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindows NTCurrentVersionAppCompatFlagsLayers

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 "HKLMSoftwareMicrosoftWindows NTCurrentVersionAppCompatFlagsLayers" /v "C:Program FilesMyAppTest.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 "HKLMSOFTWAREMicrosoftWindows NTCurrentVersionAppCompatFlagsLayers" /v "C:empcompatmodeliconsext.exe" /t REG_SZ /d "WINXPSP3 RUNASADMIN" /f

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...