windows 如何以编程方式设置“以管理员身份运行此程序”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2313045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set "Run this program as an administrator" programmatically
提问by Patrick
I'm having a problem with good ol' bdeadmin.exe in Vista. First, let's get the predictable responses out of the way:
我在 Vista 中遇到了很好的 bdeadmin.exe 问题。首先,让我们排除可预测的响应:
"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).
“你需要嵌入一个清单文件。”
它已经编译了,已经有很多年了,创建它的公司无意再次这样做,并且它是从合并模块(MSM 文件)安装的。
"BDE is obsolete, you should be using dbExpress"
One and a half million lines of code. 'Nuff said.
“BDE 已过时,您应该使用 dbExpress”
一五十万行代码。'纳夫说。
"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.
“在 EXE 旁边放置一个清单文件。”
试过了,什么也没做。作为测试,同一个清单文件能够使其他几个 EXE 文件需要提升,只是不是我想要的。里面的东西阻止了外部清单被读取。
"Create a shortcut and set SLDF_RUNAS_USER."
Can't do that, it's a Control Panel applet.
“创建一个快捷方式并设置 SLDF_RUNAS_USER。”
不能这样做,它是一个控制面板小程序。
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.
唯一有效的是在其“属性”窗口的“兼容性”选项卡下设置“以管理员身份运行此程序”。我不应该告诉用户这样做。不利于生意。我需要让安装程序执行此操作。MSM 文件使用静态路径。
回答by Allon Guralnek
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):
您可以通过设置一个简单的注册表项,以编程方式设置“以管理员身份运行此程序”标志(您可以在 EXE 属性的“兼容性”选项卡中找到该选项)。您需要在这些键之一下创建一个字符串值 (REG_SZ)(如果您希望设置分别针对每个用户或每台机器):
HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers
HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers
or
或者
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers
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
.
值的名称需要是可执行文件的完整路径(如果路径包含空格,请不要用引号将路径括起来)并且值的数据必须包含字符串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
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
SPINS1000 SP100
Privilege Level
权限级别
RUNASADMIN Run program as an administrator
RUNASADMIN 以管理员身份运行程序
REG ADD "HKLM\SOFTWARE\Microsoft\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
References: http://www.verboon.info/2011/03/running-an-application-as-administrator-or-in-compatibility-mode/
参考资料:http: //www.verboon.info/2011/03/running-an-application-as-administrator-or-in-compatibility-mode/
回答by Allon Guralnek
This is a long shot, but if you have the word "setup" or "install" in the name of the EXE, Windows will prompt for elevation when running it. I don't know if that'll work with a control panel applet, though.
这是一个长镜头,但如果您在 EXE 的名称中包含“设置”或“安装”一词,Windows 将在运行时提示提升。不过,我不知道这是否适用于控制面板小程序。
回答by Aaron Klotz
Have you tried Microsoft's Application Compatibility Toolkit? It analyses your app and provides compatibility shims that might be able to help resolve your problem.
您是否尝试过 Microsoft 的应用程序兼容性工具包?它会分析您的应用程序并提供可能有助于解决您的问题的兼容性垫片。
回答by Barrypp.zzx
Use a wrap program which uses ShellExcute that uses "runas" as its "verb" to run the program you want.
使用使用“runas”作为其“动词”的 ShellExcute 的包装程序来运行您想要的程序。
回答by Barrypp.zzx
I have found that the .manifest
file method doesn't work if the .exe
is under C:\Program files\...
and the .exe
has previously been run without the .manifest
file. Windows remembers the .manifest
from the first time the .exe
is run. This means you can't just send the manifest when users complain that their installations don't run. The manifest file has to be placed before or during the same installation that places the .exe
.
我发现如果.manifest
文件.exe
是在下面C:\Program files\...
并且.exe
以前在没有.manifest
文件的情况下运行过,则文件方法不起作用。Windows 会记住.manifest
从第一次.exe
运行开始。这意味着当用户抱怨他们的安装无法运行时,您不能只发送清单。清单文件必须在放置.exe
.
Windows rechecks the .manifest
if the .exe
changes (e.g. new release or different number of bytes)
Windows 重新检查.manifest
是否有.exe
更改(例如新版本或不同的字节数)
回答by Benji
Pack your app into WinRar SFX with silent mode + admin request mode.
使用静默模式 + 管理请求模式将您的应用程序打包到 WinRar SFX 中。
Much simpler than messing with .MSI variables.
比弄乱 .MSI 变量要简单得多。
回答by Jay
I'd be surprised if this was possible. It would be an ideal way for malicious code to abuse the system. You're probably going to have tell the user the administrator must install or they must have admin rights (like all the other programs on windows do).
如果这是可能的,我会感到惊讶。这将是恶意代码滥用系统的理想方式。您可能会告诉用户管理员必须安装或他们必须具有管理员权限(就像 Windows 上的所有其他程序一样)。