在Vista下提升应用程序的执行权限在vista系统应用程序执行时默认不会使用超级用户权限,这会使一些创建文件和目录的IO操作失败,出现访问错误.这种现象是因为Vista的UAC特性引起的.
要避免这种情况,可以强制用户在运行应用程序时以超级用户权限执行程序.具体做法:
1.编辑manifest文件. 在manifest文件中配置好应用程序执行时需要的参数和执行环境.下面是一个实际例子:
| <?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="X86"
name="IsUserAdmin"
type="win32"/>
<description>Description of your application</description>
<!-- Identify the application security requirements. -->
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges>
<requestedExecutionLevel
level="requireAdministrator"
uiAccess="false"/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
|
2.编辑应用程序的资源文件(rc后缀的文件),在其中加入下面两行代码:
#define MANIFEST_RESOURCE_ID 1
MANIFEST_RESOURCE_ID RT_MANIFEST "{Application.exe}.manifest"
|
其中Application.exe替换成要编译生成的可执行文件名.
3.编译链接程序,生成可执行应用程序.
4.运行mt.exe命令,把manifest文件和生成的可执行程序链接起来.把manifest内容写入可执行文件.
命令行为:
mt.exe –manifest temp.manifest –outputresource:YourApp.exe;#1 |
到这一步,生成的可执行文件就可以在vista下运行时弹出UAC确认框,让用户确认以超级用户权限执行应用程序.
参考:
1.
Designing UAC Applications for Windows Vista
2.
AC: User Account Control
阅读(801) | 评论(0) | 转发(0) |