Manifest文件是为了解决“DLL Hell”问题而引入的。Vista引入了UAC机制,也是通过manifest文件让操作系统感知到exe文件的UAC状态的(在程序图标右下方有一个盾牌标识)。Manifest文件介绍请参考这篇文章,http://hi.baidu.com/emtlwemlducgixe/item/5912603a495fd120b3c0c50d。本文只介绍如何利用Manifest文件为应用程序添加UAC功能。
Visual Studio 2008和Visual Studio 2012添加UAC功能相对简单,只需要在配置属性的链接器\清单文件项里设置以下项目即可。
编译后会在exe资源里嵌入如下内容的manifest文件。
Visual Studio 2005没有提供类似的编译选项,必须通过另外的办法将manifest文件嵌入exe资源。
首先准备好manifest文件,命名为 程序名.exe.manifest,如app.exe.manifest,文件内容如下:
<?xml version='1.0' encoding='UTF-8'standalone='yes'?>
<assemblyxmlns='urn:schemas-microsoft-com:asm.v1' manifestVersion='1.0'>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level='requireAdministrator'uiAccess='false' />
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
① 若编译只兼容vista系统的程序,在资源文件里增加一个RT_MANIFEST类型的资源,资源id必须为1。将资源文件链接进exe即可。
#define MANIFEST_RESOURCE_ID 1
MANIFEST_RESOURCE_ID RT_MANIFEST "app.exe.manifest"
② 若编译xp和vista操作系统的程序,在配置属性\清单工具\输入输出\附加清单文件项中指定待添加的清单文件,重新编译即可。
Ps:只需要为exe增加manifest资源。