如何设置Windows执行级别以要求用户提供Rust程序的管理员特权?

时间:2018-12-19 07:23:35

标签: windows rust

我正在编写Windows CLI应用程序,需要以管理员身份运行它。在C#中,我将此行添加到app.manifest:

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> 

我如何在Rust中做到这一点?

2 个答案:

答案 0 :(得分:3)

为时已晚,但仍要回答。 :-)

请查看winres库。它包含以下示例:

以下清单将把exe品牌标记为请求管理员 特权。因此,每次执行时,Windows UAC对话框都会 出现。

let mut res = winres::WindowsResource::new();
res.set_manifest(r#"
<assembly xmlns="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>
"#);

完整的示例已记录在案,并且可用here

我一直在一个包含图标且需要管理员权限的项目中使用此库,其使用build.rs的{​​{1}}托管在here中。查看生成的可执行文件:enter image description here

HTH

答案 1 :(得分:1)

有一个open RFC #721 for manifest support in Rust

除了讨论添加本机支持的方法外,这些帖子还包含使用链接器开关或其他工具的各种解决方法的链接。当前没有通过链接器开关的好方法。您必须将rustflags选项放入Cargo config file中,并将参数通过rustc传递到["-C", "link-args=/exoticlinkerswitch"]。这显然不是很方便。

对于工具,您可以use mt.exe from the Windows SDK to add a manifest to your program after it has been compiled

请注意,Cargo当前尚无法自动执行构建后步骤。但是,有一个货运扩展名cargo-make支持此类构建过程。您可以通过cargo install cargo-make安装它。

相关问题