我想为我的VB 6.0程序创建一个清单文件,这样当我启动应用程序时,操作系统将要求用户提供管理员权限。
我还想知道如何将其嵌入应用程序中?
在VB中,您实际上不需要创建清单文件。Windows应用程序清单是一个标准文本文档,格式为XML。您可以在记事本中创建它,并将其保存在应用程序目录中,文件名为YourAppName.exe.manifest
。
Microsoft在应用程序清单中提供了更多信息。它甚至包括一个示例清单,您可以将其简单地复制并粘贴到空白文本文件中以开始使用。
重要的是,如果您希望应用程序提示用户进行升级,则需要将requestedExecutionLevel
设置为requireAdministrator
,而不是asInvoker
。有关使用UAC与清单的具体信息,请参阅此处。
因此,完整的示例可能如下所示:
<?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>
将清单嵌入可执行文件的传统方法是使用Windows SDK中的mt.exe
实用程序。
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 must also 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.
但是,如果你想在从VB 6 IDE构建应用程序时自动嵌入清单文件,那么你需要面对一些困难。VB 6 IDE不支持后构建步骤,因此你不能简单地在命令行上运行mt.exe
来完成这项工作。我看到过一些声称可以自动嵌入清单文件的实用工具,但我认为其中大多数是旧的实用工具,只能处理请求v6的ComCtl32.dll。我不确定它们是否容易扩展以包括UAC权限,但值得一试。以下是一些要查看的链接:
以下是一种在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.
在过去的几个月中,我一直在使用它,并没有遇到任何问题,这是为了运行在Windows 7上的两个旧的遗留应用程序。不过你可能需要尝试一下如何保存你的清单文件,因为只有将其保存为UTF8而不带BOM才能正常工作。
为了检查以这种方式嵌入的清单文件的确切内容,您还可以使用mt.exe
从已编译的exe / dll中提取清单。这就是帮助我找到BOM问题的方法...
mt.exe
。 - Cody Gray