如何使用清单在 Windows 版 Rust 中进行编译?

问题描述 投票:0回答:1

我目前正在开发一个适用于 Windows x86 的 Rust 应用程序,需要以管理员身份运行。因此,我编写了一个 Windows 清单,允许您请求管理员模式。

这是

app.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" uiAccess="false"/>
            </requestedPrivileges>
        </security>
    </trustInfo>
</assembly>

这是

Cargo.toml
文件

[package]
name = "wrapper"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
named-lock = "0.3.0"
rand = "0.8.5"
reqwest = "0.11.24"
tokio = { version = "1.36.0", features = ["rt", "rt-multi-thread"] }
winapi = { version = "0.3.9", features = ["processthreadsapi", "securitybaseapi", "winnt", "fileapi", "minwinbase", "iphlpapi", "winsock2", "ws2def"] }
windows-service = "0.6.0"

[profile.release]
opt-level = 3

[package.metadata.win]
manifest = "app.manifest"

问题是,当我通过Cargo(

cargo build --release --target=i686-pc-windows-gnu
)编译时,清单似乎没有被考虑在内。

rust rust-cargo
1个回答
0
投票

您可以使用以下方式在构建过程中设置清单:

# Cargo.toml
[target.'cfg(target_os = "windows")'.build-dependencies]
winres = "0.1.12"

# build.rs
pub const PROCESS_NAME: &str = "PROCESS NAME";

#[cfg(all(target_os = "windows"))]
fn main() {
    let mut res = winres::WindowsResource::new();
    res.set("FileDescription", PROCESS_NAME);
    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>
"#,
    );
    if let Err(error) = res.compile() {
        eprint!("{error}");
        std::process::exit(1);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.