From d3c8205e2da074e10727ee4e96fe9c49da9a0f6d Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 12 Nov 2024 19:38:54 -0800 Subject: [PATCH] Recognize windows-sys signatures as "C" or "system" depending on cfg --- src/dbghelp.rs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/dbghelp.rs b/src/dbghelp.rs index 82b81e1d..f8ea9192 100644 --- a/src/dbghelp.rs +++ b/src/dbghelp.rs @@ -30,12 +30,6 @@ use core::mem; use core::ptr; use core::slice; -// This is used when we're double-checking function signatures against windows-sys. -#[inline(always)] -fn assert_equal_types(a: T, _b: T) -> T { - a -} - // This macro is used to define a `Dbghelp` structure which internally contains // all the function pointers that we might load. macro_rules! dbghelp { @@ -85,14 +79,23 @@ macro_rules! dbghelp { // either read the cached function pointer or load it and return the // loaded value. Loads are asserted to succeed. $(pub fn $name(&mut self) -> Option<$name> { + // Assert that windows_sys::$name is declared to have the same + // argument types and return type as our declaration, although + // it might be either extern "C" or extern "system". + cfg_if::cfg_if! { + if #[cfg(any(target_arch = "x86", not(windows_raw_dylib)))] { + let _: unsafe extern "system" fn($($argty),*) -> $ret = super::windows_sys::$name; + } else { + let _: unsafe extern "C" fn($($argty),*) -> $ret = super::windows_sys::$name; + } + } + unsafe { if self.$name == 0 { let name = concat!(stringify!($name), "\0"); self.$name = self.symbol(name.as_bytes())?; } - let ret = mem::transmute::(self.$name); - assert_equal_types(ret, super::windows_sys::$name); - Some(ret) + Some(mem::transmute::(self.$name)) } })*