From f6b9dcdfd58efb2067b2de5ad4bc02ecced90529 Mon Sep 17 00:00:00 2001 From: Kenny Kerr Date: Mon, 21 Jul 2025 08:55:46 -0500 Subject: [PATCH 1/5] extern abi --- crates/libs/link/src/lib.rs | 2 +- crates/libs/targets/src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/libs/link/src/lib.rs b/crates/libs/link/src/lib.rs index feee0da270..dbecf9f3b5 100644 --- a/crates/libs/link/src/lib.rs +++ b/crates/libs/link/src/lib.rs @@ -20,7 +20,7 @@ macro_rules! link { macro_rules! link { ($library:literal $abi:literal $($link_name:literal)? fn $($function:tt)*) => ( #[link(name = $library, kind = "raw-dylib", modifiers = "+verbatim")] - extern "C" { + extern $abi { $(#[link_name=$link_name])? pub fn $($function)*; } diff --git a/crates/libs/targets/src/lib.rs b/crates/libs/targets/src/lib.rs index b1ddc313c5..5af6bc56cc 100644 --- a/crates/libs/targets/src/lib.rs +++ b/crates/libs/targets/src/lib.rs @@ -20,7 +20,7 @@ macro_rules! link { macro_rules! link { ($library:literal $abi:literal $($link_name:literal)? fn $($function:tt)*) => ( #[link(name = $library, kind = "raw-dylib", modifiers = "+verbatim")] - extern "C" { + extern $abi { $(#[link_name=$link_name])? pub fn $($function)*; } From 731057ba8b5d76855fee2de8adb5d66d09938623 Mon Sep 17 00:00:00 2001 From: Kenny Kerr Date: Mon, 21 Jul 2025 09:30:26 -0500 Subject: [PATCH 2/5] windows_link --- .../tests/misc/calling_convention/Cargo.toml | 3 ++ .../misc/calling_convention/tests/link.rs | 33 +++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 crates/tests/misc/calling_convention/tests/link.rs diff --git a/crates/tests/misc/calling_convention/Cargo.toml b/crates/tests/misc/calling_convention/Cargo.toml index 2f61794c6e..98c20096da 100644 --- a/crates/tests/misc/calling_convention/Cargo.toml +++ b/crates/tests/misc/calling_convention/Cargo.toml @@ -25,3 +25,6 @@ features = [ "Win32_System_SystemInformation", "Win32_UI_WindowsAndMessaging", ] + +[dependencies.windows-link] +workspace = true diff --git a/crates/tests/misc/calling_convention/tests/link.rs b/crates/tests/misc/calling_convention/tests/link.rs new file mode 100644 index 0000000000..9edebda174 --- /dev/null +++ b/crates/tests/misc/calling_convention/tests/link.rs @@ -0,0 +1,33 @@ +use windows_sys::{ + core::{s, PCSTR, PSTR}, + Win32::Foundation::ERROR_BUSY, + Win32::Networking::Ldap::LDAP_BUSY, +}; + +windows_link::link!("wldap32.dll" "C" fn LdapMapErrorToWin32(ldaperror: u32) -> u32); +windows_link::link!("kernel32.dll" "system" fn GetTickCount() -> u32); +windows_link::link!("user32.dll" "C" fn wsprintfA(param0: PSTR, param1: PCSTR, ...) -> i32); + +#[test] +fn calling_convention() { + unsafe { + // This function requires cdecl on x86. + assert_eq!(LdapMapErrorToWin32(LDAP_BUSY as u32), ERROR_BUSY); + + // This function requires stdcall on x86. + GetTickCount(); + } +} + +#[test] +fn variadic() { + unsafe { + let mut buffer = vec![0u8; 1024]; + let len = wsprintfA(buffer.as_mut_ptr(), s!("test-%d-%d!"), 123u32, 456u32); + let result = std::str::from_utf8_unchecked(std::slice::from_raw_parts( + buffer.as_ptr(), + len as usize, + )); + assert_eq!(result, "test-123-456!"); + } +} From e03a65de71723b45116353a8bf730e6bfc97ec0e Mon Sep 17 00:00:00 2001 From: Kenny Kerr Date: Mon, 21 Jul 2025 11:04:15 -0500 Subject: [PATCH 3/5] compat --- .cargo/config.toml | 2 -- crates/libs/link/src/lib.rs | 2 +- crates/libs/targets/src/lib.rs | 2 +- .../misc/calling_convention/tests/ptr.rs | 20 +++++++++++++++++++ 4 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 crates/tests/misc/calling_convention/tests/ptr.rs diff --git a/.cargo/config.toml b/.cargo/config.toml index c55bc79763..0887da4668 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,7 +1,5 @@ [build] rustflags = [ - "-D", "warnings", - # "--cfg", "windows_raw_dylib", # "--cfg", "windows_slim_errors", # "-C", "target-feature=+crt-static", diff --git a/crates/libs/link/src/lib.rs b/crates/libs/link/src/lib.rs index dbecf9f3b5..feee0da270 100644 --- a/crates/libs/link/src/lib.rs +++ b/crates/libs/link/src/lib.rs @@ -20,7 +20,7 @@ macro_rules! link { macro_rules! link { ($library:literal $abi:literal $($link_name:literal)? fn $($function:tt)*) => ( #[link(name = $library, kind = "raw-dylib", modifiers = "+verbatim")] - extern $abi { + extern "C" { $(#[link_name=$link_name])? pub fn $($function)*; } diff --git a/crates/libs/targets/src/lib.rs b/crates/libs/targets/src/lib.rs index 5af6bc56cc..b1ddc313c5 100644 --- a/crates/libs/targets/src/lib.rs +++ b/crates/libs/targets/src/lib.rs @@ -20,7 +20,7 @@ macro_rules! link { macro_rules! link { ($library:literal $abi:literal $($link_name:literal)? fn $($function:tt)*) => ( #[link(name = $library, kind = "raw-dylib", modifiers = "+verbatim")] - extern $abi { + extern "C" { $(#[link_name=$link_name])? pub fn $($function)*; } diff --git a/crates/tests/misc/calling_convention/tests/ptr.rs b/crates/tests/misc/calling_convention/tests/ptr.rs new file mode 100644 index 0000000000..5ecd1abe5d --- /dev/null +++ b/crates/tests/misc/calling_convention/tests/ptr.rs @@ -0,0 +1,20 @@ +// This is a test specifically for the breaking change discussed in https://github.com/microsoft/windows-rs/pull/3669 +// and https://github.com/microsoft/windows-rs/issues/3626 where a function pointer is stored. All of this can be +// removed if the hardcoding of "C" is removed in a future breaking change. + +windows_link::link!("kernel32.dll" "system" fn GetTickCount() -> u32); + +#[cfg(target_arch = "x86")] +type GetTickCountType = unsafe extern "system" fn() -> u32; + +#[cfg(target_arch = "x86_64")] +type GetTickCountType = unsafe extern "C" fn() -> u32; + +static GET_TICK_COUNT: GetTickCountType = GetTickCount; + +#[test] +fn store_ptr() { + unsafe { + GET_TICK_COUNT(); + } +} From a5c1210d6ef7d956186dc6e64f716907b8b30613 Mon Sep 17 00:00:00 2001 From: Kenny Kerr Date: Mon, 21 Jul 2025 11:05:28 -0500 Subject: [PATCH 4/5] revert config change --- .cargo/config.toml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.cargo/config.toml b/.cargo/config.toml index 0887da4668..c55bc79763 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,5 +1,7 @@ [build] rustflags = [ + "-D", "warnings", + # "--cfg", "windows_raw_dylib", # "--cfg", "windows_slim_errors", # "-C", "target-feature=+crt-static", From 663b563420456838f1a6b92072e0b1dac373fdd3 Mon Sep 17 00:00:00 2001 From: Kenny Kerr Date: Mon, 21 Jul 2025 11:30:42 -0500 Subject: [PATCH 5/5] not --- crates/tests/misc/calling_convention/tests/ptr.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/tests/misc/calling_convention/tests/ptr.rs b/crates/tests/misc/calling_convention/tests/ptr.rs index 5ecd1abe5d..52bf1138ce 100644 --- a/crates/tests/misc/calling_convention/tests/ptr.rs +++ b/crates/tests/misc/calling_convention/tests/ptr.rs @@ -7,7 +7,7 @@ windows_link::link!("kernel32.dll" "system" fn GetTickCount() -> u32); #[cfg(target_arch = "x86")] type GetTickCountType = unsafe extern "system" fn() -> u32; -#[cfg(target_arch = "x86_64")] +#[cfg(not(target_arch = "x86"))] type GetTickCountType = unsafe extern "C" fn() -> u32; static GET_TICK_COUNT: GetTickCountType = GetTickCount;