如何设置“以管理员身份运行此程序"以编程方式 [英] How to set "Run this program as an administrator" programmatically

查看:38
本文介绍了如何设置“以管理员身份运行此程序"以编程方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 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.

推荐答案

您可以通过设置简单的注册表项.您需要在这些键之一下创建一个字符串值 (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_USERSoftwareMicrosoftWindowsNTCurrentVersionAppCompatFlagsLayers

HKEY_CURRENT_USERSoftwareMicrosoftWindows NTCurrentVersionAppCompatFlagsLayers

HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsNTCurrentVersionAppCompatFlagsLayers

HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindows NTCurrentVersionAppCompatFlagsLayers

值的名称需要是可执行文件的完整路径(如果路径包含空格,请不要用引号将路径括起来)并且值的数据必须包含字符串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 "HKLMSoftwareMicrosoftWindows NTCurrentVersionAppCompatFlagsLayers" /v "C:Program FilesMyAppTest.exe" /d "PUT__VALUE__HERE"

兼容模式

WIN95 Windows 95
WIN98 视窗 98
WIN4SP5 Windows NT 4.0 SP5
WIN2000 视窗 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

REG ADD "HKLMSOFTWAREMicrosoftWindowsNTCurrentVersionAppCompatFlagsLayers"/v"C: empcompatmodeliconsext.exe"/t REG_SZ/d "WINXPSP3 RUNASADMIN"/f

REG ADD "HKLMSOFTWAREMicrosoftWindows NTCurrentVersionAppCompatFlagsLayers" /v "C: empcompatmodeliconsext.exe" /t REG_SZ /d "WINXPSP3 RUNASADMIN" /f

参考资料:http://www.verboon.info/2011/03/running-an-application-as-administrator-or-in-compatibility-mode/

这篇关于如何设置“以管理员身份运行此程序"以编程方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆