文章目录
Qt 程序配置
在 Qt 的 pro 配置文件中添加如下配置,表示运行时请求管理员权限
CONFIG(debug, debug | release) {
DESTDIR = ./bin_debug
} else {
DESTDIR = ./bin_release
QMAKE_LFLAGS += /MANIFESTUAC:\"level=\'requireAdministrator\' uiAccess=\'false\'\"
}
在这里只配置了Release模式下的管理员身份运行,因为QtCreator运行时本身没有管理员身份,如果全加上就会导致我们开发测试时无法运行程序。如下:
23:03:31: Starting D:\qtworkspace\JmainFrame\bin_release\QuakePosition.exe ...
23:03:31: 启动程序失败,路径或者权限错误?
23:03:31: D:\qtworkspace\JmainFrame\bin_release\QuakePosition.exe exited with code -1
23:03:31: The process failed to start. Either the invoked program "D:\qtworkspace\JmainFrame\bin_release\QuakePosition.exe" is missing, or you may have insufficient permissions to invoke the program.
所以只在release模式下添加该项配置,在测试程序的时候就可以在debug模式下进行测试。
InnoSetup 配置
在Qt程序中配置了管理员身份运行后还需要在InnoSetup中配置生成的安装包的管理员身份,否则打包出来的安装包依然会有问题。
这时候就需要使用第三方的软件(reshacker)修改Innosetup的配置文件。
软件下载地址:
链接:https://pan.baidu.com/s/1LbA4vn5FmfIeNJ39215Pyg?pwd=ht97
提取码:ht97
使用reshacker打开 Innosetup 安装目录下的 SetupLdr.e32 和 Setup.e32 修改他们的清单信息
添加如下代码:保存即可。
<security>
<requestedPrivileges>
<requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
</requestedPrivileges>
</security>