Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Win32 dll in rust #65

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions win32/dll/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "dll"
version = "0.1.0"
build = "build.rs"
edition = "2021"

[workspace]
resolver = "2"

[[example]]
name = "advapi32"
crate-type = ["cdylib"]
path = "advapi32.rs"

[lib]
path = "lib.rs"

[profile.release]
panic = "abort"
# Disabling debug appears to do nothing on Windows :(
debug = false
strip = true
48 changes: 48 additions & 0 deletions win32/dll/advapi32.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#![no_std]
#![no_main]

#[panic_handler]
fn panic(_: &::core::panic::PanicInfo) -> ! {
loop {}
}

#[link(
name = "retrowin32",
kind = "raw-dylib",
import_name_type = "undecorated"
)]
extern "stdcall" {
fn retrowin32_syscall();
}

#[no_mangle]
pub unsafe extern "stdcall" fn RegCloseKey(_: u32) {
retrowin32_syscall();
}

#[no_mangle]
pub unsafe extern "stdcall" fn RegOpenKey(_: u32) {
retrowin32_syscall();
}

// Have to wrap the pointers in a struct to impl Sync.
// Have to impl Sync to use in a static.
// The other option is a "static mut" but that creates a .data section we don't otherwise need.
#[repr(transparent)]
pub struct VTableEntry(*const fn());
unsafe impl Sync for VTableEntry {}

#[no_mangle]
pub static vtab: [VTableEntry; 4] = [
VTableEntry(core::ptr::null()),
VTableEntry(RegCloseKey as _),
VTableEntry(core::ptr::null()),
VTableEntry(RegOpenKey as _),
];

// core::arch::global_asm!(
// ".globl _IDirectSound",
// "_IDirectSound:",
// ".long _RegCloseKey",
// ".long 0",
// );
20 changes: 20 additions & 0 deletions win32/dll/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#[cfg(target_family = "unix")]
fn main() {
println!("cargo:rerun-if-env-changed=XWIN");
if let Ok(xwin) = std::env::var("XWIN") {
for dir in ["crt/lib/x86", "sdk/lib/ucrt/x86", "sdk/lib/um/x86"] {
println!(r"cargo:rustc-link-search={xwin}/{dir}");
}
}

println!("cargo::rustc-link-arg=/Brepro");
println!("cargo::rustc-link-arg=/noentry");
println!("cargo::rustc-link-arg=/nodefaultlib");
// This doesn't help, we need -Zmerge-functions=disabled
// println!("cargo::rustc-link-arg=/OPT:NOICF");
// This doesn't disable debug info, turns out /Brepro causes it
// println!("cargo::rustc-link-arg=/DEBUG:NONE");
}

#[cfg(target_family = "windows")]
fn main() {}
1 change: 1 addition & 0 deletions win32/dll/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@