Skip to content

Instantly share code, notes, and snippets.

@carey
Created August 14, 2022 03:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carey/6d98c548b645bc72c3a075450fcd5221 to your computer and use it in GitHub Desktop.
Save carey/6d98c548b645bc72c3a075450fcd5221 to your computer and use it in GitHub Desktop.
Windows XML Manifest embedding in Rust

Feeling jealous of the Windows Rust developers that can embed a Windows manifest in their code with /MANIFESTINPUT? Why not embed the manifest directly into your source code using inline assembly?

#![windows_subsystem = "windows"]
use std::arch::global_asm;
use std::ffi::c_void;
use std::ptr::null;
#[link(name = "kernel32")]
extern "system" {
fn GetVersion() -> u32;
}
#[link(name = "user32")]
extern "system" {
fn MessageBoxA(hwnd: *const c_void, lpText: *const u8, lpCaption: *const u8, uType: u32) -> i32;
}
fn main() {
let version = unsafe { GetVersion() };
let major_version = version & 0x0f;
let minor_version = (version & 0xf0) >> 8;
let build = if version < 0x80000000 { version >> 16 } else { 0 };
let message = format!(
"It’s raining 🐈s and 🐕s.\n\nI think this is Windows {}.{} ({}).\0",
major_version, minor_version, build
);
unsafe {
MessageBoxA(null(), message.as_ptr(), "Manifest Test\0".as_ptr(), 0x40);
}
}
global_asm!(
r#".section .rsrc$01,"dw""#,
".p2align 2",
"2:",
".zero 14",
".short 1",
".long 24",
".long (3f - 2b) | 0x80000000",
"3:",
".zero 14",
".short 1",
".long 1",
".long (4f - 2b) | 0x80000000",
"4:",
".zero 14",
".short 1",
".long 1033",
".long 5f - 2b",
"5:",
".long MANIFEST@imgrel",
".long 1207",
".zero 8",
".p2align 2",
);
#[no_mangle]
#[link_section = ".rsrc$02"]
#[used]
static mut MANIFEST: [u8; 1207] = *br#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3" manifestVersion="1.0">
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>
<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"/>
<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>
</application>
</compatibility>
<asmv3:application>
<asmv3:windowsSettings>
<activeCodePage xmlns="http://schemas.microsoft.com/SMI/2019/WindowsSettings">UTF-8</activeCodePage>
<longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware>
</asmv3:windowsSettings>
</asmv3:application>
<asmv3:trustInfo>
<asmv3:security>
<asmv3:requestedPrivileges>
<asmv3:requestedExecutionLevel level="asInvoker" uiAccess="false"/>
</asmv3:requestedPrivileges>
</asmv3:security>
</asmv3:trustInfo>
</assembly>"#;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment