windows 如何创建清单文件以启动具有管理员权限的应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5755426/
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 do I create a manifest file to launch application with admin privileges?
提问by nightfire001
I want to create a manifest file for my VB 6.0 program, so that when I launch my application, the OS should ask the user for administrator privilege.
我想为我的 VB 6.0 程序创建一个清单文件,这样当我启动我的应用程序时,操作系统应该要求用户提供管理员权限。
I also want to know how it can be embedded in the application?
我也想知道它如何嵌入到应用程序中?
回答by Cody Gray
You don't actually create the manifest file in VB. A Windows application manifest is a standard text document, formatted as XML. You can create it in Notepad and save it with the appropriate file name in your application's directory (YourAppName.exe.manifest
).
您实际上并没有在 VB 中创建清单文件。Windows 应用程序清单是一个标准的文本文档,格式为 XML。您可以在记事本中创建它并在应用程序目录 ( YourAppName.exe.manifest
) 中使用适当的文件名保存它。
Microsoft has more information available here: Application Manifests. It even includes a sample manifest that you can simply copy and paste into a blank text file to get started.
Microsoft 在此处提供了更多信息:应用程序清单。它甚至包括一个示例清单,您可以简单地将其复制并粘贴到空白文本文件中以开始使用。
The important thing, if you want your application to prompt the user for elevation, is to set the requestedExecutionLevel
to requireAdministrator
, rather than asInvoker
. Specific information on using manifests with UAC is available here.
重要的是,如果您希望应用程序提示用户提升权限,则将 设置requestedExecutionLevel
为requireAdministrator
,而不是asInvoker
。此处提供了有关在 UAC 中使用清单的具体信息。
So a full sample might look something like this:
所以一个完整的样本可能看起来像这样:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity
version="1.0.0.0"
processorArchitecture="*"
name="MyMagicalApplication"
type="win32"
/>
<description>Sample manifest for your super cool application</description>
<!-- Request version 6 of the common controls. -->
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="*"
publicKeyToken="6595b64144ccf1df"
language="*"
/>
</dependentAssembly>
</dependency>
<!-- Identify the application security requirements. -->
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges>
<requestedExecutionLevel
level="requireAdministrator"
uiAccess="false"
/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
The traditional way to embed a manifest into an executable is using the mt.exe
utility, available as part of the Windows SDK.
将清单嵌入到可执行文件中的传统方法是使用mt.exe
实用程序,它是 Windows SDK 的一部分。
The VBAccelerator sitealso has some information about embedding manifests in a VB 6 application. Specifically, it says:
该VBAccelerator网站也有关于在VB 6应用程序中嵌入清单的一些信息。具体来说,它说:
There are two ways to provide the manifest: the simplest (but least elegant) way is to provide the manifest on disk for an executable. Let's say your application is called TimeSlot.exe. Then if you save the manifest XML above as
TimeSlot.exe.manifest
in the same directory as the executable, TimeSlot.exe will automatically get the XP styles. VB5 and VB6 examples are provided. If you rename the .manifest file prior to running the app, you can switch off the XP styles.
A more robust method is to compile the manifest as a resource in your application. To do this, the manifest must appear as resource type
RT_MANIFEST
(24) with idCREATEPROCESS_MANIFEST_RESOURCE_ID
(1). For some bizarre reason, you mustalso ensure that the resulting XML file is an even multiple of 4 bytes long. So for example, if your file is actually 597 bytes you need to add padding spaces to make up the file size to 600 bytes before compiling. The Resource examples demonstrate how to create this resource file using a resource compiler script (.rc file) and RC.exe.
有两种方法可以提供清单:最简单(但最不优雅)的方法是在磁盘上为可执行文件提供清单。假设您的应用程序名为 TimeSlot.exe。然后,如果您将上面的清单 XML 保存为
TimeSlot.exe.manifest
在与可执行文件相同的目录中,TimeSlot.exe 将自动获取 XP 样式。提供了 VB5 和 VB6 示例。如果在运行应用程序之前重命名 .manifest 文件,则可以关闭 XP 样式。
更可靠的方法是将清单编译为应用程序中的资源。为此,清单必须显示为
RT_MANIFEST
带有 idCREATEPROCESS_MANIFEST_RESOURCE_ID
(1) 的资源类型(24 )。由于某些奇怪的原因,您还必须确保生成的 XML 文件是 4 字节长的偶数倍。因此,例如,如果您的文件实际上是 597 字节,则在编译之前,您需要添加填充空间以将文件大小补充为 600 字节。资源示例演示了如何使用资源编译器脚本(.rc 文件)和 RC.exe 创建此资源文件。
But if you want to embed the manifest automaticallywhen you build your application from the VB 6 IDE, you're in for a little more difficulty. The VB 6 IDE doesn't support post-build steps, so you can't simply run mt.exe
on the command line to do it for you. There area couple of utilities I've seen around the web that claim to automatically embed manifests for you, but I believe most of these are older utilities that only handle requesting v6 of ComCtl32.dll. I'm not sure if they're easily extensible to include the UAC permissions as well, but it's worth a shot. Here are some links to check out:
但是,如果您想在从 VB 6 IDE 构建应用程序时自动嵌入清单,则会遇到更多困难。VB 6 IDE 不支持后期构建步骤,因此您不能简单地mt.exe
在命令行上运行来为您完成。这里是我的网站,声称能自动嵌入舱单你,但我相信大多数的这些周围看到一对夫妇的公用事业旧的实用程序,只能处理请求COMCTL32.DLL的V6发动机。我不确定它们是否可以轻松扩展以包含 UAC 权限,但值得一试。以下是一些可以查看的链接:
回答by takrl
Here's a way to include the manifest at build time of your VB6 app:
这是在 VB6 应用程序的构建时包含清单的一种方法:
- Save your manifest file, maybe call it "my.manifest".
- Make sure the file size is a multiple of four, as Cody already mentioned. If necessary, pad it with blanks at the end.
See also: Embedding an application manifest into a VB6 exe Generate a resource-file "my.rc" with the following content:
#define CREATEPROCESS_MANIFEST_RESOURCE_ID 1 #define RT_MANIFEST 24 CREATEPROCESS_MANIFEST_RESOURCE_ID RT_MANIFEST "my.manifest"
You can find more information on the constants used at the msdn blogs. Seems you'd need to use
#define ISOLATIONAWARE_MANIFEST_RESOURCE_ID 2
for a dll project.
- Use
rc.exe
to compile this .rc file to a .res file. - Add the resulting .res file to your VB6 project.
- 保存您的清单文件,也许称之为“my.manifest”。
- 确保文件大小是四的倍数,正如 Cody 已经提到的。如有必要,请在末尾填充空白。
另请参阅:将应用程序清单嵌入到 VB6 exe 中 生成一个资源文件“my.rc”,内容如下:
#define CREATEPROCESS_MANIFEST_RESOURCE_ID 1 #define RT_MANIFEST 24 CREATEPROCESS_MANIFEST_RESOURCE_ID RT_MANIFEST "my.manifest"
您可以在msdn 博客中找到有关所用常量的更多信息。似乎你需要使用
#define ISOLATIONAWARE_MANIFEST_RESOURCE_ID 2
对于 dll 项目。
- 用于
rc.exe
将此 .rc 文件编译为 .res 文件。 - 将生成的 .res 文件添加到您的 VB6 项目中。
I've been using this for the last few months without problems, on two old legacy applications that needed to run on Windows 7. You may need to experiment on how you save your manifest file though, for me only saving it as UTF8 without BOM worked properly.
在过去的几个月里,我一直在没有问题的情况下使用它,在需要在 Windows 7 上运行的两个旧的遗留应用程序上。不过,您可能需要试验如何保存清单文件,对我来说只将它保存为没有 BOM 的 UTF8工作正常。
To check what exactly gets embedded as manifest this way, you can also use mt.exe
to extract the manifest from your compiled exe/dll. That was what helped me finding the BOM problem ...
要检查以这种方式作为清单嵌入的确切内容,您还可以使用mt.exe
从已编译的 exe/dll 中提取清单。这就是帮助我找到 BOM 问题的原因......