diff --git a/crates/libs/bindgen/src/constants.rs b/crates/libs/bindgen/src/constants.rs index 729b1fbda8..903d269209 100644 --- a/crates/libs/bindgen/src/constants.rs +++ b/crates/libs/bindgen/src/constants.rs @@ -39,12 +39,15 @@ pub fn gen(gen: &Gen, def: Field) -> TokenStream { } else { let kind = gen.type_default_name(&ty); let value = gen.value(&gen.reader.constant_value(constant)); + let underlying_type = gen.reader.type_underlying_type(&ty); - let value = if gen.reader.type_underlying_type(&ty) == constant_type { + let value = if underlying_type == constant_type { value // TODO: workaround for https://github.com/microsoft/win32metadata/issues/1029 } else if ty == Type::PCWSTR && value.0.starts_with('-') { quote! { #value as u16 as _ } + } else if gen.std && underlying_type == Type::ISize { + quote! { ::core::ptr::invalid_mut(#value as _) } } else { quote! { #value as _ } }; diff --git a/crates/libs/bindgen/src/functions.rs b/crates/libs/bindgen/src/functions.rs index 892223d41d..5ba2214e15 100644 --- a/crates/libs/bindgen/src/functions.rs +++ b/crates/libs/bindgen/src/functions.rs @@ -16,12 +16,7 @@ fn gen_sys_function(gen: &Gen, def: MethodDef) -> TokenStream { let features = gen.cfg_features(&cfg); let return_type = gen.return_sig(&signature); let abi = gen.reader.method_def_extern_abi(def); - let impl_map = gen - .reader - .method_def_impl_map(def) - .expect("ImplMap not found"); - let scope = gen.reader.impl_map_scope(impl_map); - let link = gen.reader.module_ref_name(scope).to_lowercase(); + let link = gen.reader.method_def_module_name(def); // TODO: skip inline functions for now. if link == "forceinline" { @@ -35,14 +30,26 @@ fn gen_sys_function(gen: &Gen, def: MethodDef) -> TokenStream { }); let mut tokens = features; - tokens.combine(&gen_link( - &link, - abi, - doc.as_str(), - name.as_str(), - params, - return_type.as_str(), - )); + + if gen.std { + let link = link.trim_end_matches(".dll"); + tokens.combine("e! { + #[link(name = #link)] + extern #abi { + pub fn #name(#(#params),*) #return_type; + } + }); + } else { + tokens.combine(&gen_link( + &link, + abi, + doc.as_str(), + name.as_str(), + params, + return_type.as_str(), + )); + } + tokens } @@ -96,12 +103,7 @@ fn gen_win_function(gen: &Gen, def: MethodDef) -> TokenStream { } } } else { - let impl_map = gen - .reader - .method_def_impl_map(def) - .expect("ImplMap not found"); - let scope = gen.reader.impl_map_scope(impl_map); - let link = gen.reader.module_ref_name(scope).to_lowercase(); + let link = gen.reader.method_def_module_name(def); // TODO: skip inline functions for now. if link == "forceinline" { diff --git a/crates/libs/bindgen/src/gen.rs b/crates/libs/bindgen/src/gen.rs index 0813c5a58a..9c365a1266 100644 --- a/crates/libs/bindgen/src/gen.rs +++ b/crates/libs/bindgen/src/gen.rs @@ -8,6 +8,7 @@ pub struct Gen<'a> { pub doc: bool, pub component: bool, pub standalone: bool, + pub std: bool, } impl<'a> Gen<'a> { @@ -20,6 +21,7 @@ impl<'a> Gen<'a> { doc: false, component: false, standalone: false, + std: false, } } diff --git a/crates/libs/bindgen/src/handles.rs b/crates/libs/bindgen/src/handles.rs index dadbb7b749..9ace4a1b64 100644 --- a/crates/libs/bindgen/src/handles.rs +++ b/crates/libs/bindgen/src/handles.rs @@ -10,10 +10,23 @@ pub fn gen(gen: &Gen, def: TypeDef) -> TokenStream { pub fn gen_sys_handle(gen: &Gen, def: TypeDef) -> TokenStream { let ident = to_ident(gen.reader.type_def_name(def)); - let signature = gen.type_default_name(&gen.reader.type_def_underlying_type(def)); + match gen.reader.type_def_underlying_type(def) { + Type::ISize if gen.std => quote! { + pub type #ident = *mut ::core::ffi::c_void; + }, + Type::USize if gen.std => quote! { + #[cfg(target_pointer_width = "32")] + pub type #ident = u32; + #[cfg(target_pointer_width = "64")] + pub type #ident = u64; + }, + underlying_type => { + let signature = gen.type_default_name(&underlying_type); - quote! { - pub type #ident = #signature; + quote! { + pub type #ident = #signature; + } + } } } diff --git a/crates/libs/bindgen/src/standalone.rs b/crates/libs/bindgen/src/standalone.rs index 63e362e84e..311d6a402d 100644 --- a/crates/libs/bindgen/src/standalone.rs +++ b/crates/libs/bindgen/src/standalone.rs @@ -1,22 +1,27 @@ use super::*; -/// Generates standalone bindings for Windows APIs. +/// Generates standalone Windows bindings. pub fn standalone(names: &[&str]) -> String { let files = &File::with_default(&[]).unwrap(); let reader = &Reader::new(files); let mut gen = &mut Gen::new(reader); gen.standalone = true; gen.sys = true; - let mut tokens: TokenStream = format!( - r#"// Bindings generated by `windows-bindgen` {} - -"#, - std::env!("CARGO_PKG_VERSION") - ) - .into(); + standalone_imp(gen, names) +} - tokens.combine(&allow()); +/// Generates standalone Windows bindings for the Rust Standard Library. +pub fn standalone_std(names: &[&str]) -> String { + let files = &File::with_default(&[]).unwrap(); + let reader = &Reader::new(files); + let mut gen = &mut Gen::new(reader); + gen.standalone = true; + gen.sys = true; + gen.std = true; + standalone_imp(gen, names) +} +fn standalone_imp(gen: &Gen, names: &[&str]) -> String { let mut type_names = BTreeSet::new(); let mut core_types = BTreeSet::new(); let mut enums = BTreeSet::new(); @@ -25,13 +30,13 @@ pub fn standalone(names: &[&str]) -> String { let type_name = TypeName::parse(name); let mut found = false; - if let Some(def) = reader.get(type_name).next() { + if let Some(def) = gen.reader.get(type_name).next() { found = true; type_names.insert(type_name); - let mut cfg = reader.type_def_cfg(def, &[]); + let mut cfg = gen.reader.type_def_cfg(def, &[]); core_types.append(&mut cfg.core_types); for def in cfg.types.values().flatten() { - type_names.insert(reader.type_def_type_name(*def)); + type_names.insert(gen.reader.type_def_type_name(*def)); } if gen.reader.type_def_kind(def) == TypeKind::Struct && gen.reader.type_def_fields(def).next().is_none() @@ -42,7 +47,8 @@ pub fn standalone(names: &[&str]) -> String { } if !found { - if let Some(def) = reader + if let Some(def) = gen + .reader .get(TypeName::new(type_name.namespace, "Apis")) .next() { @@ -54,11 +60,12 @@ pub fn standalone(names: &[&str]) -> String { if name == type_name.name { found = true; type_names.insert(type_name); - let mut cfg = - reader.signature_cfg(&reader.method_def_signature(method, &[])); + let mut cfg = gen + .reader + .signature_cfg(&gen.reader.method_def_signature(method, &[])); core_types.append(&mut cfg.core_types); for def in cfg.types.values().flatten() { - type_names.insert(reader.type_def_type_name(*def)); + type_names.insert(gen.reader.type_def_type_name(*def)); } } } @@ -70,10 +77,10 @@ pub fn standalone(names: &[&str]) -> String { if name == type_name.name { found = true; type_names.insert(type_name); - let mut cfg = reader.field_cfg(field); + let mut cfg = gen.reader.field_cfg(field); core_types.append(&mut cfg.core_types); for def in cfg.types.values().flatten() { - type_names.insert(reader.type_def_type_name(*def)); + type_names.insert(gen.reader.type_def_type_name(*def)); } } } @@ -81,7 +88,10 @@ pub fn standalone(names: &[&str]) -> String { } if !found { - for def in reader.namespace_types(type_name.namespace, &Default::default()) { + for def in gen + .reader + .namespace_types(type_name.namespace, &Default::default()) + { if found { break; } @@ -103,43 +113,50 @@ pub fn standalone(names: &[&str]) -> String { } } + let mut sorted = SortedTokens::default(); + for ty in core_types { match ty { - Type::HRESULT => tokens.combine("e! { pub type HRESULT = i32; }), - Type::String => { - tokens.combine("e! { pub type HSTRING = *mut ::core::ffi::c_void; }) - } - Type::IUnknown => { - tokens.combine("e! { pub type IUnknown = *mut ::core::ffi::c_void; }) - } - Type::IInspectable => { - tokens.combine("e! { pub type IInspectable = *mut ::core::ffi::c_void; }) - } - Type::PSTR => tokens.combine("e! { pub type PSTR = *mut u8; }), - Type::PWSTR => tokens.combine("e! { pub type PWSTR = *mut u16; }), - Type::PCSTR => tokens.combine("e! { pub type PCSTR = *const u8; }), - Type::PCWSTR => tokens.combine("e! { pub type PCWSTR = *const u16; }), - Type::BSTR => tokens.combine("e! { pub type BSTR = *const u16; }), - Type::GUID => tokens.combine("e! { - #[repr(C)] - pub struct GUID { - pub data1: u32, - pub data2: u16, - pub data3: u16, - pub data4: [u8; 8], - } - impl GUID { - pub const fn from_u128(uuid: u128) -> Self { - Self { data1: (uuid >> 96) as u32, data2: (uuid >> 80 & 0xffff) as u16, data3: (uuid >> 64 & 0xffff) as u16, data4: (uuid as u64).to_be_bytes() } + Type::HRESULT => sorted.insert("HRESULT", quote! { pub type HRESULT = i32; }), + Type::String => sorted.insert( + "HSTRING", + quote! { pub type HSTRING = *mut ::core::ffi::c_void; }, + ), + Type::IUnknown => sorted.insert( + "IUnknown", + quote! { pub type IUnknown = *mut ::core::ffi::c_void; }, + ), + Type::IInspectable => sorted.insert( + "IInspectable", + quote! { pub type IInspectable = *mut ::core::ffi::c_void; }, + ), + Type::PSTR => sorted.insert("PSTR", quote! { pub type PSTR = *mut u8; }), + Type::PWSTR => sorted.insert("PWSTR", quote! { pub type PWSTR = *mut u16; }), + Type::PCSTR => sorted.insert("PCSTR", quote! { pub type PCSTR = *const u8; }), + Type::PCWSTR => sorted.insert("PCWSTR", quote! { pub type PCWSTR = *const u16; }), + Type::BSTR => sorted.insert("BSTR", quote! { pub type BSTR = *const u16; }), + Type::GUID => { + sorted.insert("GUID", quote! { + #[repr(C)] + pub struct GUID { + pub data1: u32, + pub data2: u16, + pub data3: u16, + pub data4: [u8; 8], } - } - impl ::core::marker::Copy for GUID {} - impl ::core::clone::Clone for GUID { - fn clone(&self) -> Self { - *self + impl GUID { + pub const fn from_u128(uuid: u128) -> Self { + Self { data1: (uuid >> 96) as u32, data2: (uuid >> 80 & 0xffff) as u16, data3: (uuid >> 64 & 0xffff) as u16, data4: (uuid as u64).to_be_bytes() } + } } - } - }), + impl ::core::marker::Copy for GUID {} + impl ::core::clone::Clone for GUID { + fn clone(&self) -> Self { + *self + } + } + }); + } _ => {} } } @@ -147,34 +164,41 @@ pub fn standalone(names: &[&str]) -> String { for type_name in type_names { let mut found = false; - for def in reader.get(type_name) { + for def in gen.reader.get(type_name) { found = true; let kind = gen.reader.type_def_kind(def); match kind { TypeKind::Class | TypeKind::Interface => unimplemented!(), - TypeKind::Enum => tokens.combine(&enums::gen(gen, def)), + TypeKind::Enum => { + sorted.insert(gen.reader.type_def_name(def), enums::gen(gen, def)); + } TypeKind::Struct => { if gen.reader.type_def_fields(def).next().is_none() { if let Some(guid) = gen.reader.type_def_guid(def) { let ident = to_ident(type_name.name); let value = gen.guid(&guid); let guid = gen.type_name(&Type::GUID); - let constant = quote! { - pub const #ident: #guid = #value; - }; - tokens.combine(&constant); + sorted.insert( + type_name.name, + quote! { + pub const #ident: #guid = #value; + }, + ); continue; } } - tokens.combine(&structs::gen(gen, def)); + sorted.insert(gen.reader.type_def_name(def), structs::gen(gen, def)); + } + TypeKind::Delegate => { + sorted.insert(gen.reader.type_def_name(def), delegates::gen(gen, def)); } - TypeKind::Delegate => tokens.combine(&delegates::gen(gen, def)), } } if !found { - if let Some(def) = reader + if let Some(def) = gen + .reader .get(TypeName::new(type_name.namespace, "Apis")) .next() { @@ -185,7 +209,10 @@ pub fn standalone(names: &[&str]) -> String { let name = gen.reader.method_def_name(method); if name == type_name.name { found = true; - tokens.combine(&functions::gen(gen, method)); + sorted.insert( + &format!(".{}.{name}", gen.reader.method_def_module_name(method)), + functions::gen(gen, method), + ); } } for field in gen.reader.type_def_fields(def) { @@ -195,7 +222,7 @@ pub fn standalone(names: &[&str]) -> String { let name = gen.reader.field_name(field); if name == type_name.name { found = true; - tokens.combine(&constants::gen(gen, field)); + sorted.insert(name, constants::gen(gen, field)); } } } @@ -203,17 +230,20 @@ pub fn standalone(names: &[&str]) -> String { } for (enum_type, field_name) in enums { - if let Some(def) = reader.get(enum_type).next() { + if let Some(def) = gen.reader.get(enum_type).next() { for field in gen.reader.type_def_fields(def) { if gen.reader.field_name(field) == field_name { - let field_name = to_ident(field_name); - let ident = to_ident(enum_type.name); + let ident = to_ident(field_name); + let ty = to_ident(enum_type.name); let constant = gen.reader.field_constant(field).unwrap(); let value = gen.value(&gen.reader.constant_value(constant)); - tokens.combine("e! { - pub const #field_name: #ident = #value; - }); + sorted.insert( + field_name, + quote! { + pub const #ident: #ty = #value; + }, + ); break; } @@ -221,6 +251,20 @@ pub fn standalone(names: &[&str]) -> String { } } + let mut tokens: TokenStream = format!( + r#"// Bindings generated by `windows-bindgen` {} + +"#, + std::env!("CARGO_PKG_VERSION") + ) + .into(); + + tokens.combine(&allow()); + + for value in sorted.0.values() { + tokens.combine(value); + } + try_format(tokens.into_string()) } @@ -255,3 +299,12 @@ fn try_format(tokens: String) -> String { tokens } } + +#[derive(Default)] +struct SortedTokens(BTreeMap); + +impl SortedTokens { + fn insert(&mut self, key: &str, tokens: TokenStream) { + self.0.entry(key.to_string()).or_default().combine(&tokens); + } +} diff --git a/crates/libs/metadata/src/reader/mod.rs b/crates/libs/metadata/src/reader/mod.rs index 5860af85fc..7bd44fea8b 100644 --- a/crates/libs/metadata/src/reader/mod.rs +++ b/crates/libs/metadata/src/reader/mod.rs @@ -568,6 +568,14 @@ impl<'a> Reader<'a> { pub fn method_def_impl_map(&self, row: MethodDef) -> Option { self.row_equal_range(row.0, TABLE_IMPLMAP, 1, MemberForwarded::MethodDef(row).encode()).map(ImplMap).next() } + pub fn method_def_module_name(&self, row: MethodDef) -> String { + if let Some(impl_map) = self.method_def_impl_map(row) { + let scope = self.impl_map_scope(impl_map); + self.module_ref_name(scope).to_lowercase() + } else { + String::new() + } + } pub fn method_def_signature(&self, row: MethodDef, generics: &[Type]) -> Signature { let mut blob = self.row_blob(row.0, 4); blob.read_usize(); diff --git a/crates/tests/standalone/build.rs b/crates/tests/standalone/build.rs index d40ac38450..9f48a72048 100644 --- a/crates/tests/standalone/build.rs +++ b/crates/tests/standalone/build.rs @@ -65,6 +65,18 @@ fn main() { ], ); + write_std( + "src/b_std.rs", + &[ + "Windows.Win32.Foundation.CloseHandle", + "Windows.Win32.Foundation.GetLastError", + "Windows.Win32.Foundation.HMODULE", + "Windows.Win32.Networking.WinSock.socket", + "Windows.Win32.Security.Cryptography.BCRYPT_ALG_HANDLE", + "Windows.Win32.Storage.FileSystem.FindFileHandle", + ], + ); + write( "src/b_test.rs", &[ @@ -84,3 +96,8 @@ fn write(filename: &str, apis: &[&str]) { let bindings = windows_bindgen::standalone(apis); std::fs::write(filename, bindings).unwrap(); } + +fn write_std(filename: &str, apis: &[&str]) { + let bindings = windows_bindgen::standalone_std(apis); + std::fs::write(filename, bindings).unwrap(); +} diff --git a/crates/tests/standalone/src/b_arch.rs b/crates/tests/standalone/src/b_arch.rs index 2f195962bd..0e42950795 100644 --- a/crates/tests/standalone/src/b_arch.rs +++ b/crates/tests/standalone/src/b_arch.rs @@ -7,8 +7,11 @@ dead_code, clippy::all )] -pub type PSTR = *mut u8; +#[cfg(any(target_arch = "aarch64", target_arch = "x86_64"))] +::windows_targets::link!("user32.dll" "system" fn GetWindowLongPtrW(hwnd : HWND, nindex : WINDOW_LONG_PTR_INDEX) -> isize); pub type HWND = isize; +pub type PSTR = *mut u8; +pub type WINDOW_LONG_PTR_INDEX = i32; #[repr(C)] #[cfg(any(target_arch = "aarch64", target_arch = "x86_64"))] pub struct WSADATA { @@ -47,6 +50,3 @@ impl ::core::clone::Clone for WSADATA { *self } } -#[cfg(any(target_arch = "aarch64", target_arch = "x86_64"))] -::windows_targets::link!("user32.dll" "system" fn GetWindowLongPtrW(hwnd : HWND, nindex : WINDOW_LONG_PTR_INDEX) -> isize); -pub type WINDOW_LONG_PTR_INDEX = i32; diff --git a/crates/tests/standalone/src/b_bstr.rs b/crates/tests/standalone/src/b_bstr.rs index ebb4cc3934..9c8e41ce1a 100644 --- a/crates/tests/standalone/src/b_bstr.rs +++ b/crates/tests/standalone/src/b_bstr.rs @@ -7,6 +7,6 @@ dead_code, clippy::all )] -pub type PCWSTR = *const u16; -pub type BSTR = *const u16; ::windows_targets::link!("oleaut32.dll" "system" fn SysAllocString(psz : PCWSTR) -> BSTR); +pub type BSTR = *const u16; +pub type PCWSTR = *const u16; diff --git a/crates/tests/standalone/src/b_depends.rs b/crates/tests/standalone/src/b_depends.rs index f01e5dd415..023c43935f 100644 --- a/crates/tests/standalone/src/b_depends.rs +++ b/crates/tests/standalone/src/b_depends.rs @@ -7,9 +7,8 @@ dead_code, clippy::all )] -pub type PSTR = *mut u8; -pub type HANDLE = isize; pub type ADDRESS_FAMILY = u16; +pub type HANDLE = isize; pub type LPWSAOVERLAPPED_COMPLETION_ROUTINE = ::core::option::Option< unsafe extern "system" fn( dwerror: u32, @@ -19,6 +18,42 @@ pub type LPWSAOVERLAPPED_COMPLETION_ROUTINE = ::core::option::Option< ) -> (), >; #[repr(C)] +pub struct OVERLAPPED { + pub Internal: usize, + pub InternalHigh: usize, + pub Anonymous: OVERLAPPED_0, + pub hEvent: HANDLE, +} +impl ::core::marker::Copy for OVERLAPPED {} +impl ::core::clone::Clone for OVERLAPPED { + fn clone(&self) -> Self { + *self + } +} +#[repr(C)] +pub union OVERLAPPED_0 { + pub Anonymous: OVERLAPPED_0_0, + pub Pointer: *mut ::core::ffi::c_void, +} +impl ::core::marker::Copy for OVERLAPPED_0 {} +impl ::core::clone::Clone for OVERLAPPED_0 { + fn clone(&self) -> Self { + *self + } +} +#[repr(C)] +pub struct OVERLAPPED_0_0 { + pub Offset: u32, + pub OffsetHigh: u32, +} +impl ::core::marker::Copy for OVERLAPPED_0_0 {} +impl ::core::clone::Clone for OVERLAPPED_0_0 { + fn clone(&self) -> Self { + *self + } +} +pub type PSTR = *mut u8; +#[repr(C)] pub struct SOCKADDR { pub sa_family: ADDRESS_FAMILY, pub sa_data: [u8; 14], @@ -69,38 +104,3 @@ impl ::core::clone::Clone for WSASENDMSG { *self } } -#[repr(C)] -pub struct OVERLAPPED { - pub Internal: usize, - pub InternalHigh: usize, - pub Anonymous: OVERLAPPED_0, - pub hEvent: HANDLE, -} -impl ::core::marker::Copy for OVERLAPPED {} -impl ::core::clone::Clone for OVERLAPPED { - fn clone(&self) -> Self { - *self - } -} -#[repr(C)] -pub union OVERLAPPED_0 { - pub Anonymous: OVERLAPPED_0_0, - pub Pointer: *mut ::core::ffi::c_void, -} -impl ::core::marker::Copy for OVERLAPPED_0 {} -impl ::core::clone::Clone for OVERLAPPED_0 { - fn clone(&self) -> Self { - *self - } -} -#[repr(C)] -pub struct OVERLAPPED_0_0 { - pub Offset: u32, - pub OffsetHigh: u32, -} -impl ::core::marker::Copy for OVERLAPPED_0_0 {} -impl ::core::clone::Clone for OVERLAPPED_0_0 { - fn clone(&self) -> Self { - *self - } -} diff --git a/crates/tests/standalone/src/b_enumerator.rs b/crates/tests/standalone/src/b_enumerator.rs index 96be6122d4..9788b60a8f 100644 --- a/crates/tests/standalone/src/b_enumerator.rs +++ b/crates/tests/standalone/src/b_enumerator.rs @@ -7,6 +7,6 @@ dead_code, clippy::all )] -pub type WIN32_ERROR = u32; pub const WAIT_IO_COMPLETION: WIN32_ERROR = 192u32; pub const WAIT_TIMEOUT: WIN32_ERROR = 258u32; +pub type WIN32_ERROR = u32; diff --git a/crates/tests/standalone/src/b_guid.rs b/crates/tests/standalone/src/b_guid.rs index d53726351a..951da3e079 100644 --- a/crates/tests/standalone/src/b_guid.rs +++ b/crates/tests/standalone/src/b_guid.rs @@ -7,6 +7,7 @@ dead_code, clippy::all )] +::windows_targets::link!("ole32.dll" "system" fn CoCreateGuid(pguid : *mut GUID) -> HRESULT); #[repr(C)] pub struct GUID { pub data1: u32, @@ -31,4 +32,3 @@ impl ::core::clone::Clone for GUID { } } pub type HRESULT = i32; -::windows_targets::link!("ole32.dll" "system" fn CoCreateGuid(pguid : *mut GUID) -> HRESULT); diff --git a/crates/tests/standalone/src/b_hresult.rs b/crates/tests/standalone/src/b_hresult.rs index 1bf9785dc9..0d9072b3a0 100644 --- a/crates/tests/standalone/src/b_hresult.rs +++ b/crates/tests/standalone/src/b_hresult.rs @@ -7,5 +7,5 @@ dead_code, clippy::all )] -pub type HRESULT = i32; ::windows_targets::link!("ole32.dll" "system" fn CoInitialize(pvreserved : *const ::core::ffi::c_void) -> HRESULT); +pub type HRESULT = i32; diff --git a/crates/tests/standalone/src/b_hstring.rs b/crates/tests/standalone/src/b_hstring.rs index ffa00a35f4..632732cea6 100644 --- a/crates/tests/standalone/src/b_hstring.rs +++ b/crates/tests/standalone/src/b_hstring.rs @@ -7,5 +7,5 @@ dead_code, clippy::all )] -pub type HSTRING = *mut ::core::ffi::c_void; ::windows_targets::link!("api-ms-win-core-winrt-string-l1-1-0.dll" "system" fn WindowsGetStringLen(string : HSTRING) -> u32); +pub type HSTRING = *mut ::core::ffi::c_void; diff --git a/crates/tests/standalone/src/b_inspectable.rs b/crates/tests/standalone/src/b_inspectable.rs index aec3bbb96f..2215e89a17 100644 --- a/crates/tests/standalone/src/b_inspectable.rs +++ b/crates/tests/standalone/src/b_inspectable.rs @@ -7,7 +7,7 @@ dead_code, clippy::all )] +::windows_targets::link!("api-ms-win-core-winrt-l1-1-0.dll" "system" fn RoActivateInstance(activatableclassid : HSTRING, instance : *mut IInspectable) -> HRESULT); +pub type HRESULT = i32; pub type HSTRING = *mut ::core::ffi::c_void; pub type IInspectable = *mut ::core::ffi::c_void; -pub type HRESULT = i32; -::windows_targets::link!("api-ms-win-core-winrt-l1-1-0.dll" "system" fn RoActivateInstance(activatableclassid : HSTRING, instance : *mut IInspectable) -> HRESULT); diff --git a/crates/tests/standalone/src/b_pcstr.rs b/crates/tests/standalone/src/b_pcstr.rs index 2281ab9df5..8ccd5a1fa4 100644 --- a/crates/tests/standalone/src/b_pcstr.rs +++ b/crates/tests/standalone/src/b_pcstr.rs @@ -7,5 +7,5 @@ dead_code, clippy::all )] -pub type PCSTR = *const u8; ::windows_targets::link!("kernel32.dll" "system" fn lstrlenA(lpstring : PCSTR) -> i32); +pub type PCSTR = *const u8; diff --git a/crates/tests/standalone/src/b_pcwstr.rs b/crates/tests/standalone/src/b_pcwstr.rs index 23d17e4be4..566ad8fa71 100644 --- a/crates/tests/standalone/src/b_pcwstr.rs +++ b/crates/tests/standalone/src/b_pcwstr.rs @@ -7,5 +7,5 @@ dead_code, clippy::all )] -pub type PCWSTR = *const u16; ::windows_targets::link!("kernel32.dll" "system" fn lstrlenW(lpstring : PCWSTR) -> i32); +pub type PCWSTR = *const u16; diff --git a/crates/tests/standalone/src/b_pstr.rs b/crates/tests/standalone/src/b_pstr.rs index a725013083..50ebf41855 100644 --- a/crates/tests/standalone/src/b_pstr.rs +++ b/crates/tests/standalone/src/b_pstr.rs @@ -7,6 +7,6 @@ dead_code, clippy::all )] +::windows_targets::link!("oleaut32.dll" "system" fn VarI1FromDate(datein : f64, pcout : PSTR) -> HRESULT); pub type HRESULT = i32; pub type PSTR = *mut u8; -::windows_targets::link!("oleaut32.dll" "system" fn VarI1FromDate(datein : f64, pcout : PSTR) -> HRESULT); diff --git a/crates/tests/standalone/src/b_pwstr.rs b/crates/tests/standalone/src/b_pwstr.rs index 2ed8e10704..9687ff2c00 100644 --- a/crates/tests/standalone/src/b_pwstr.rs +++ b/crates/tests/standalone/src/b_pwstr.rs @@ -7,7 +7,6 @@ dead_code, clippy::all )] -pub type PWSTR = *mut u16; #[repr(C)] pub struct CALPOLESTR { pub cElems: u32, @@ -19,3 +18,4 @@ impl ::core::clone::Clone for CALPOLESTR { *self } } +pub type PWSTR = *mut u16; diff --git a/crates/tests/standalone/src/b_std.rs b/crates/tests/standalone/src/b_std.rs new file mode 100644 index 0000000000..1f87f11609 --- /dev/null +++ b/crates/tests/standalone/src/b_std.rs @@ -0,0 +1,32 @@ +// Bindings generated by `windows-bindgen` 0.48.0 + +#![allow( + non_snake_case, + non_upper_case_globals, + non_camel_case_types, + dead_code, + clippy::all +)] +#[link(name = "kernel32")] +extern "system" { + pub fn CloseHandle(hobject: HANDLE) -> BOOL; +} +#[link(name = "kernel32")] +extern "system" { + pub fn GetLastError() -> WIN32_ERROR; +} +#[link(name = "ws2_32")] +extern "system" { + pub fn socket(af: i32, r#type: WINSOCK_SOCKET_TYPE, protocol: i32) -> SOCKET; +} +pub type BCRYPT_ALG_HANDLE = *mut ::core::ffi::c_void; +pub type BOOL = i32; +pub type FindFileHandle = *mut ::core::ffi::c_void; +pub type HANDLE = *mut ::core::ffi::c_void; +pub type HMODULE = *mut ::core::ffi::c_void; +#[cfg(target_pointer_width = "32")] +pub type SOCKET = u32; +#[cfg(target_pointer_width = "64")] +pub type SOCKET = u64; +pub type WIN32_ERROR = u32; +pub type WINSOCK_SOCKET_TYPE = i32; diff --git a/crates/tests/standalone/src/b_test.rs b/crates/tests/standalone/src/b_test.rs index ada7822d3e..0fd53d2d20 100644 --- a/crates/tests/standalone/src/b_test.rs +++ b/crates/tests/standalone/src/b_test.rs @@ -7,6 +7,14 @@ dead_code, clippy::all )] +::windows_targets::link!("kernel32.dll" "system" fn CloseHandle(hobject : HANDLE) -> BOOL); +::windows_targets::link!("kernel32.dll" "system" fn CreateEventW(lpeventattributes : *const SECURITY_ATTRIBUTES, bmanualreset : BOOL, binitialstate : BOOL, lpname : PCWSTR) -> HANDLE); +::windows_targets::link!("kernel32.dll" "system" fn SetEvent(hevent : HANDLE) -> BOOL); +::windows_targets::link!("kernel32.dll" "system" fn WaitForSingleObject(hhandle : HANDLE, dwmilliseconds : u32) -> WIN32_ERROR); +::windows_targets::link!("ole32.dll" "system" fn CoCreateInstance(rclsid : *const GUID, punkouter : IUnknown, dwclscontext : CLSCTX, riid : *const GUID, ppv : *mut *mut ::core::ffi::c_void) -> HRESULT); +pub type BOOL = i32; +pub type CLSCTX = u32; +pub const CLSCTX_ALL: CLSCTX = 23u32; #[repr(C)] pub struct GUID { pub data1: u32, @@ -30,13 +38,10 @@ impl ::core::clone::Clone for GUID { *self } } -pub type IUnknown = *mut ::core::ffi::c_void; +pub type HANDLE = isize; pub type HRESULT = i32; +pub type IUnknown = *mut ::core::ffi::c_void; pub type PCWSTR = *const u16; -pub type BOOL = i32; -::windows_targets::link!("kernel32.dll" "system" fn CloseHandle(hobject : HANDLE) -> BOOL); -pub type HANDLE = isize; -pub type WIN32_ERROR = u32; #[repr(C)] pub struct SECURITY_ATTRIBUTES { pub nLength: u32, @@ -49,11 +54,6 @@ impl ::core::clone::Clone for SECURITY_ATTRIBUTES { *self } } -pub type CLSCTX = u32; -::windows_targets::link!("ole32.dll" "system" fn CoCreateInstance(rclsid : *const GUID, punkouter : IUnknown, dwclscontext : CLSCTX, riid : *const GUID, ppv : *mut *mut ::core::ffi::c_void) -> HRESULT); pub const STGTY_REPEAT: i32 = 256i32; -::windows_targets::link!("kernel32.dll" "system" fn CreateEventW(lpeventattributes : *const SECURITY_ATTRIBUTES, bmanualreset : BOOL, binitialstate : BOOL, lpname : PCWSTR) -> HANDLE); -::windows_targets::link!("kernel32.dll" "system" fn SetEvent(hevent : HANDLE) -> BOOL); -::windows_targets::link!("kernel32.dll" "system" fn WaitForSingleObject(hhandle : HANDLE, dwmilliseconds : u32) -> WIN32_ERROR); pub const UIAnimationManager: GUID = GUID::from_u128(0x4c1fc63a_695c_47e8_a339_1a194be3d0b8); -pub const CLSCTX_ALL: CLSCTX = 23u32; +pub type WIN32_ERROR = u32; diff --git a/crates/tests/standalone/src/b_unknown.rs b/crates/tests/standalone/src/b_unknown.rs index 3d68d1f420..4db660dcc3 100644 --- a/crates/tests/standalone/src/b_unknown.rs +++ b/crates/tests/standalone/src/b_unknown.rs @@ -7,6 +7,6 @@ dead_code, clippy::all )] -pub type IUnknown = *mut ::core::ffi::c_void; -pub type BOOL = i32; ::windows_targets::link!("ole32.dll" "system" fn CoIsHandlerConnected(punk : IUnknown) -> BOOL); +pub type BOOL = i32; +pub type IUnknown = *mut ::core::ffi::c_void; diff --git a/crates/tests/standalone/src/lib.rs b/crates/tests/standalone/src/lib.rs index 679986d90c..d36132a645 100644 --- a/crates/tests/standalone/src/lib.rs +++ b/crates/tests/standalone/src/lib.rs @@ -1,5 +1,6 @@ #![cfg(test)] #![cfg_attr(windows_raw_dylib, feature(raw_dylib))] +#![allow(clashing_extern_declarations)] mod b_arch; mod b_bstr; @@ -15,6 +16,7 @@ mod b_pcstr; mod b_pcwstr; mod b_pstr; mod b_pwstr; +mod b_std; mod b_test; mod b_unknown; @@ -103,6 +105,13 @@ fn enumerator() { assert_eq!(b_enumerator::WAIT_TIMEOUT, 258); } +#[test] +fn std() { + unsafe { + assert_eq!(b_std::CloseHandle(std::ptr::null_mut()), 0); + } +} + #[test] fn test() { use b_test::*; diff --git a/crates/tools/lib/src/lib.rs b/crates/tools/lib/src/lib.rs index 983d8b2bba..d8ca21ce34 100644 --- a/crates/tools/lib/src/lib.rs +++ b/crates/tools/lib/src/lib.rs @@ -45,12 +45,11 @@ fn combine(files: &[metadata::reader::File], libraries: &mut BTreeMap