Skip to content

Commit 817a3f3

Browse files
committed
Prototyping windows-sys fn ptrs
1 parent f6eba59 commit 817a3f3

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

crates/libs/bindgen/src/types/cpp_fn.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,35 @@ impl CppFn {
2727
self.type_name().write(config, &[])
2828
}
2929

30+
pub fn write_fn_ptr(&self, config: &Config<'_>, underlying_types: bool) -> TokenStream {
31+
let ptr_name = self.method.name().to_string() + "Fn";
32+
let name = to_ident(&ptr_name);
33+
let abi = self.method.calling_convention();
34+
let signature = self.method.signature(self.namespace, &[]);
35+
36+
let params = signature.params.iter().map(|param| {
37+
let name = param.write_ident();
38+
let ty = if underlying_types {
39+
param.underlying_type().write_abi(config)
40+
} else {
41+
param.write_abi(config)
42+
};
43+
quote! { #name: #ty }
44+
});
45+
46+
let return_sig = config.write_return_sig(self.method, &signature, underlying_types);
47+
48+
let vararg = if config.sys && signature.call_flags.contains(MethodCallAttributes::VARARG) {
49+
quote! { , ... }
50+
} else {
51+
quote! {}
52+
};
53+
54+
link_fmt(quote! {
55+
pub type #name = unsafe extern #abi fn(#(#params),* #vararg) #return_sig;
56+
})
57+
}
58+
3059
pub fn write_link(&self, config: &Config<'_>, underlying_types: bool) -> TokenStream {
3160
let library = self.method.module_name().to_lowercase();
3261
let symbol = self.method.import_name();
@@ -71,6 +100,7 @@ impl CppFn {
71100
let name = to_ident(self.method.name());
72101
let signature = self.method.signature(self.namespace, &[]);
73102

103+
let ptr = self.write_fn_ptr(config, false);
74104
let link = self.write_link(config, false);
75105
let arches = write_arches(self.method);
76106
let cfg = self.write_cfg(config);
@@ -79,6 +109,9 @@ impl CppFn {
79109

80110
if config.sys {
81111
return quote! {
112+
#cfg
113+
#ptr
114+
#window_long
82115
#cfg
83116
#link
84117
#window_long

crates/samples/services/time/src/bindings.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
clippy::all
99
)]
1010

11+
pub type FileTimeToLocalFileTimeFn =
12+
unsafe extern "system" fn(lpfiletime: *const FILETIME, lplocalfiletime: *mut FILETIME) -> BOOL;
1113
windows_link::link!("kernel32.dll" "system" fn FileTimeToLocalFileTime(lpfiletime : *const FILETIME, lplocalfiletime : *mut FILETIME) -> BOOL);
14+
pub type FileTimeToSystemTimeFn =
15+
unsafe extern "system" fn(lpfiletime: *const FILETIME, lpsystemtime: *mut SYSTEMTIME) -> BOOL;
1216
windows_link::link!("kernel32.dll" "system" fn FileTimeToSystemTime(lpfiletime : *const FILETIME, lpsystemtime : *mut SYSTEMTIME) -> BOOL);
1317
pub type BOOL = i32;
1418
#[repr(C)]

0 commit comments

Comments
 (0)