在windows中使用Qt Creator编译出需要管理员权限来运行的程序,分为3个步骤:
1. 新建一个xml文档,例如命名为uac.manifest
<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<!-- UAC Manifest Options
If you want to change the Windows User Account Control level replace the
requestedExecutionLevel node with one of the following.
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
If you want to utilize File and Registry Virtualization for backward
compatibility then delete the requestedExecutionLevel node.
-->
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
</asmv1:assembly>
注:level的取值,requireAdministrator、asInvoker或highestAvailable
2. 新建一个Resource File,例如命名为uac.rc
#include <windows.h> //这句不能少
CREATEPROCESS_MANIFEST_RESOURCE_ID RT_MANIFEST "uac.manifest"
3. 在Qt project中(即pro文件)添加以下内容:
RC_FILE = uac.rc
然后编译出的程序,就需要管理员权限来运行了。