如何设置“以管理员身份运行此程序”以编程方式
在Vista中,我的bdeadmin.exe很好。首先,让我们获得可预测的响应:
I'm having a problem with good ol' bdeadmin.exe in Vista. First, let's get the predictable responses out of the way:
您不应该要求提升您的应用程序。
C'est la vie。
"You should not require your application to be elevated."
This one does. C'est la vie.
您需要嵌入清单文件。
已经编译,已经有很多年了,创建它的公司无意再次执行此操作,而是从合并模块(MSM文件)安装它。
"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已过时,您应该使用dbExpress
一百五十万行代码。
"BDE is obsolete, you should be using dbExpress"
One and a half million lines of code. 'Nuff said.
将清单文件拖放到EXE旁边。
尝试过,什么也没做。作为测试,该清单文件能够使其他几个EXE文件需要提升,但不是我想要的。
"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.
创建快捷方式并设置SLDF_RUNAS_USER。
不能这样做,它是
"Create a shortcut and set SLDF_RUNAS_USER."
Can't do that, it's a Control Panel applet.
唯一可行的方法是在属性窗口的兼容性选项卡下设置以管理员身份运行该程序。我不必告诉用户这样做。对企业不利。我需要安装程序来执行此操作。 MSM文件使用静态路径。
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.
您可以通过编程方式设置以管理员身份运行该程序标志(该选项您可以通过设置简单的注册表项在EXE属性的兼容性选项卡中找到)。您需要在这些键之一下创建一个字符串值(REG_SZ)(如果您希望该设置分别针对每个用户或每台计算机):
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\软件\Microsoft\Windows
NT\CurrentVersion\AppCompatFlags\Layers
HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers
或
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows
NT\CurrentVersion\AppCompatFlags\Layers
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers
值的名称必须是可执行文件的完整路径(如果路径包含空格,请不要在路径两边加上引号)并且值的数据必须包含字符串 RUNASADMIN
。
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
.
对于示例:
reg.exe Add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /v "C:\Program Files\MyApp\Test.exe" /d "PUT__VALUE__HERE"
兼容模式
WI N95 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
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
特权级别
RUNASADMIN以管理员身份运行程序
RUNASADMIN Run program as an administrator
注册添加 HKLM\软件WMicrosoft\Windows
NT\CurrentVersion\AppCompatFlags\Layers / v
C:\temp\compatmodel\iconsext.exe / t REG_SZ / d WINXPSP3 RUNASADMIN
/ f
REG ADD "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /v "C:\temp\compatmodel\iconsext.exe" /t REG_SZ /d "WINXPSP3 RUNASADMIN" /f
参考文献:
http://www.verboon.info/2011/03/running-an-application-as-administrator -or-in-compatibility-mode /