This is an explanation of the video content.
 用技术延续对ACG的热爱
57
 | 
go windows程序使用管理员权限运行

1. 获取rsrc

go install github.com/akavel/rsrc@latest

在项目根目录中创建*.manifest文件(文件名没有过多局限,不过按照要求来也没错) 文件内容如下:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
    <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
        <security>
            <requestedPrivileges>
                <requestedExecutionLevel level="requireAdministrator" />
            </requestedPrivileges>
        </security>
    </trustInfo>
</assembly>

网上部分转载摘抄的文章中有的少抄了最后一行,会导致程序抛出异常,报错:应用程序无法启动,因为应用程序的并行配置不正确。有关详细信息,请参阅应用程序事件日志,或使用命令行 sxstrace.exe 工具。

2. 编译 *.syso

rsrc -manifest .\test.manifest -o test.syso

3. go编译打包

go build

然后测试运行一下,看是否提示申请管理员权限。

4. 优化

如果需要做到windows全系列兼容,可以在标签下面增加以下内容:

<!-- OSVersion -->
    <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">   
        <application>   
            <!-- Windows 10 -->   
            <supportedos id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"></supportedos>  
            <!-- Windows 8.1/Windows Blue/Server 2012 R2 -->  
            <supportedos id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"></supportedos>  
            <!-- Windows Vista/Server 2008 -->  
            <supportedos id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"></supportedos>   
            <!-- Windows 7/Server 2008 R2 -->  
            <supportedos id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"></supportedos>  
            <!-- Windows 8/Server 2012 -->  
            <supportedos id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"></supportedos>  
        </application>   
    </compatibility>  

支持Windows 6.0界面库、支持管理员权限、兼容WIN8/WIN10下取系统版本、兼容DPI Aware,完整manifest可以参考https://blog.csdn.net/cometnet/article/details/52995192



使用rsrc库实现go开发windows程序使用管理员权限运行

57 服务端 ↦ Go开发技巧 __ 107 字
 Go开发技巧 #11