Skip to main content

windows_link/
lib.rs

1#![doc = include_str!("../readme.md")]
2#![no_std]
3
4/// Defines an external function to import.
5///
6/// Expands to an `extern` block declaring the function as well as a `pub type`
7/// alias matching the function's signature. The type alias has the same name
8/// as the function, lives in the type namespace, and is useful for storing or
9/// passing around the function pointer (for example, after resolving the
10/// symbol at runtime via `GetProcAddress`).
11#[cfg(all(windows, target_arch = "x86"))]
12#[macro_export]
13macro_rules! link {
14    ($library:literal $abi:literal $($link_name:literal)? fn $name:ident($($params:tt)*) $(-> $ret:ty)?) => (
15        #[link(name = $library, kind = "raw-dylib", modifiers = "+verbatim", import_name_type = "undecorated")]
16        unsafe extern $abi {
17            $(#[link_name=$link_name])?
18            pub fn $name($($params)*) $(-> $ret)?;
19        }
20        #[allow(non_camel_case_types)]
21        pub type $name = unsafe extern $abi fn($($params)*) $(-> $ret)?;
22    )
23}
24
25/// Defines an external function to import.
26///
27/// Expands to an `extern` block declaring the function as well as a `pub type`
28/// alias matching the function's signature. The type alias has the same name
29/// as the function, lives in the type namespace, and is useful for storing or
30/// passing around the function pointer (for example, after resolving the
31/// symbol at runtime via `GetProcAddress`).
32#[cfg(all(windows, not(target_arch = "x86")))]
33#[macro_export]
34macro_rules! link {
35    ($library:literal $abi:literal $($link_name:literal)? fn $name:ident($($params:tt)*) $(-> $ret:ty)?) => (
36        #[link(name = $library, kind = "raw-dylib", modifiers = "+verbatim")]
37        unsafe extern $abi {
38            $(#[link_name=$link_name])?
39            pub fn $name($($params)*) $(-> $ret)?;
40        }
41        #[allow(non_camel_case_types)]
42        pub type $name = unsafe extern $abi fn($($params)*) $(-> $ret)?;
43    )
44}
45
46/// Defines an external function to import.
47///
48/// Expands to an `extern` block declaring the function as well as a `pub type`
49/// alias matching the function's signature. The type alias has the same name
50/// as the function, lives in the type namespace, and is useful for storing or
51/// passing around the function pointer (for example, after resolving the
52/// symbol at runtime via `GetProcAddress`).
53#[cfg(not(windows))]
54#[macro_export]
55macro_rules! link {
56    ($library:literal $abi:literal $($link_name:literal)? fn $name:ident($($params:tt)*) $(-> $ret:ty)?) => (
57        unsafe extern $abi {
58            pub fn $name($($params)*) $(-> $ret)?;
59        }
60        #[allow(non_camel_case_types)]
61        pub type $name = unsafe extern $abi fn($($params)*) $(-> $ret)?;
62    )
63}