The windows-link crate provides the link macro that
simplifies linking. The link macro uses raw-dylib and thus does not require import lib files.
Start by adding the following to your Cargo.toml file:
[dependencies.windows-link]
version = "0.100"Use the link macro to define the external functions you wish to call:
windows_link::link!("kernel32.dll" "system" fn SetLastError(code: u32));
windows_link::link!("kernel32.dll" "system" fn GetLastError() -> u32);
unsafe {
SetLastError(1234);
assert_eq!(GetLastError(), 1234);
}In addition to declaring the function, the macro also emits a pub type alias of the same name
describing the function's signature. This is useful when you need to store or pass around the
function pointer (for example, after resolving the symbol at runtime via GetProcAddress):
windows_link::link!("kernel32.dll" "system" fn GetTickCount() -> u32);
// `GetTickCount` is also available as a function-pointer type:
let f: GetTickCount = GetTickCount;