From 8b10d639ba30d210dd70d01f260c7f8b47a72532 Mon Sep 17 00:00:00 2001 From: Kenny Kerr Date: Sat, 30 Nov 2024 07:35:26 -0600 Subject: [PATCH] samples --- .../components/json_validator_winrt/build.rs | 10 +- .../json_validator_winrt/src/bindings.rs | 170 +++++++++--------- .../json_validator_winrt_client/build.rs | 7 +- .../src/bindings.rs | 79 ++++---- .../windows-sys/task_dialog/src/main.rs | 4 +- crates/samples/windows/core_app/src/main.rs | 1 - crates/samples/windows/wmi/src/main.rs | 11 +- 7 files changed, 143 insertions(+), 139 deletions(-) diff --git a/crates/samples/components/json_validator_winrt/build.rs b/crates/samples/components/json_validator_winrt/build.rs index c04917f40b..1695cf0dd7 100644 --- a/crates/samples/components/json_validator_winrt/build.rs +++ b/crates/samples/components/json_validator_winrt/build.rs @@ -21,7 +21,7 @@ fn main() { panic!("Failed to run midlrt"); } - if let Err(error) = windows_bindgen::bindgen([ + windows_bindgen::bindgen([ "--in", "sample.winmd", &metadata_dir, @@ -29,9 +29,7 @@ fn main() { "src/bindings.rs", "--filter", "Sample", - "--config", - "implement", - ]) { - panic!("{error}"); - } + "--flat", + "--implement", + ]); } diff --git a/crates/samples/components/json_validator_winrt/src/bindings.rs b/crates/samples/components/json_validator_winrt/src/bindings.rs index f94a20373b..8b78210a8f 100644 --- a/crates/samples/components/json_validator_winrt/src/bindings.rs +++ b/crates/samples/components/json_validator_winrt/src/bindings.rs @@ -7,6 +7,63 @@ dead_code, clippy::all )] + +#[repr(transparent)] +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct JsonValidator(windows_core::IUnknown); +windows_core::imp::interface_hierarchy!( + JsonValidator, + windows_core::IUnknown, + windows_core::IInspectable +); +impl JsonValidator { + pub fn Validate( + &self, + value: &windows_core::HSTRING, + ) -> windows_core::Result { + let this = self; + unsafe { + let mut result__ = core::mem::zeroed(); + (windows_core::Interface::vtable(this).Validate)( + windows_core::Interface::as_raw(this), + core::mem::transmute_copy(value), + &mut result__, + ) + .map(|| core::mem::transmute(result__)) + } + } + pub fn CreateInstance(schema: &windows_core::HSTRING) -> windows_core::Result { + Self::IJsonValidatorFactory(|this| unsafe { + let mut result__ = core::mem::zeroed(); + (windows_core::Interface::vtable(this).CreateInstance)( + windows_core::Interface::as_raw(this), + core::mem::transmute_copy(schema), + &mut result__, + ) + .and_then(|| windows_core::Type::from_abi(result__)) + }) + } + fn IJsonValidatorFactory windows_core::Result>( + callback: F, + ) -> windows_core::Result { + static SHARED: windows_core::imp::FactoryCache = + windows_core::imp::FactoryCache::new(); + SHARED.call(callback) + } +} +impl windows_core::RuntimeType for JsonValidator { + const SIGNATURE: windows_core::imp::ConstBuffer = + windows_core::imp::ConstBuffer::for_class::(); +} +unsafe impl windows_core::Interface for JsonValidator { + type Vtable = ::Vtable; + const IID: windows_core::GUID = ::IID; +} +impl windows_core::RuntimeName for JsonValidator { + const NAME: &'static str = "Sample.JsonValidator"; +} +unsafe impl Send for JsonValidator {} +unsafe impl Sync for JsonValidator {} windows_core::imp::define_interface!( IJsonValidator, IJsonValidator_Vtbl, @@ -16,30 +73,21 @@ impl windows_core::RuntimeType for IJsonValidator { const SIGNATURE: windows_core::imp::ConstBuffer = windows_core::imp::ConstBuffer::for_interface::(); } -#[repr(C)] -pub struct IJsonValidator_Vtbl { - pub base__: windows_core::IInspectable_Vtbl, - pub Validate: unsafe extern "system" fn( - *mut core::ffi::c_void, - core::mem::MaybeUninit, - *mut core::mem::MaybeUninit, - ) -> windows_core::HRESULT, +impl windows_core::RuntimeName for IJsonValidator { + const NAME: &'static str = "Sample.IJsonValidator"; } -pub trait IJsonValidator_Impl: Sized + windows_core::IUnknownImpl { +pub trait IJsonValidator_Impl: windows_core::IUnknownImpl { fn Validate( &self, value: &windows_core::HSTRING, ) -> windows_core::Result; } -impl windows_core::RuntimeName for IJsonValidator { - const NAME: &'static str = "Sample.IJsonValidator"; -} impl IJsonValidator_Vtbl { - pub const fn new() -> IJsonValidator_Vtbl { + pub const fn new() -> Self { unsafe extern "system" fn Validate( this: *mut core::ffi::c_void, - value: core::mem::MaybeUninit, - result__: *mut core::mem::MaybeUninit, + value: *mut core::ffi::c_void, + result__: *mut *mut core::ffi::c_void, ) -> windows_core::HRESULT { let this: &Identity = &*((this as *const *const ()).offset(OFFSET) as *const Identity); match IJsonValidator_Impl::Validate(this, core::mem::transmute(&value)) { @@ -60,6 +108,15 @@ impl IJsonValidator_Vtbl { iid == &::IID } } +#[repr(C)] +pub struct IJsonValidator_Vtbl { + pub base__: windows_core::IInspectable_Vtbl, + pub Validate: unsafe extern "system" fn( + *mut core::ffi::c_void, + *mut core::ffi::c_void, + *mut *mut core::ffi::c_void, + ) -> windows_core::HRESULT, +} windows_core::imp::define_interface!( IJsonValidatorFactory, IJsonValidatorFactory_Vtbl, @@ -69,31 +126,21 @@ impl windows_core::RuntimeType for IJsonValidatorFactory { const SIGNATURE: windows_core::imp::ConstBuffer = windows_core::imp::ConstBuffer::for_interface::(); } -#[repr(C)] -pub struct IJsonValidatorFactory_Vtbl { - pub base__: windows_core::IInspectable_Vtbl, - pub CreateInstance: unsafe extern "system" fn( - *mut core::ffi::c_void, - core::mem::MaybeUninit, - *mut *mut core::ffi::c_void, - ) -> windows_core::HRESULT, +impl windows_core::RuntimeName for IJsonValidatorFactory { + const NAME: &'static str = "Sample.IJsonValidatorFactory"; } -pub trait IJsonValidatorFactory_Impl: Sized + windows_core::IUnknownImpl { +pub trait IJsonValidatorFactory_Impl: windows_core::IUnknownImpl { fn CreateInstance(&self, schema: &windows_core::HSTRING) -> windows_core::Result; } -impl windows_core::RuntimeName for IJsonValidatorFactory { - const NAME: &'static str = "Sample.IJsonValidatorFactory"; -} impl IJsonValidatorFactory_Vtbl { - pub const fn new( - ) -> IJsonValidatorFactory_Vtbl { + pub const fn new() -> Self { unsafe extern "system" fn CreateInstance< Identity: IJsonValidatorFactory_Impl, const OFFSET: isize, >( this: *mut core::ffi::c_void, - schema: core::mem::MaybeUninit, + schema: *mut core::ffi::c_void, result__: *mut *mut core::ffi::c_void, ) -> windows_core::HRESULT { let this: &Identity = &*((this as *const *const ()).offset(OFFSET) as *const Identity); @@ -116,59 +163,12 @@ impl IJsonValidatorFactory_Vtbl { iid == &::IID } } -#[repr(transparent)] -#[derive(PartialEq, Eq, Debug, Clone)] -pub struct JsonValidator(windows_core::IUnknown); -windows_core::imp::interface_hierarchy!( - JsonValidator, - windows_core::IUnknown, - windows_core::IInspectable -); -impl JsonValidator { - pub fn Validate( - &self, - value: &windows_core::HSTRING, - ) -> windows_core::Result { - let this = self; - unsafe { - let mut result__ = core::mem::zeroed(); - (windows_core::Interface::vtable(this).Validate)( - windows_core::Interface::as_raw(this), - core::mem::transmute_copy(value), - &mut result__, - ) - .and_then(|| windows_core::Type::from_abi(result__)) - } - } - pub fn CreateInstance(schema: &windows_core::HSTRING) -> windows_core::Result { - Self::IJsonValidatorFactory(|this| unsafe { - let mut result__ = core::mem::zeroed(); - (windows_core::Interface::vtable(this).CreateInstance)( - windows_core::Interface::as_raw(this), - core::mem::transmute_copy(schema), - &mut result__, - ) - .and_then(|| windows_core::Type::from_abi(result__)) - }) - } - fn IJsonValidatorFactory windows_core::Result>( - callback: F, - ) -> windows_core::Result { - static SHARED: windows_core::imp::FactoryCache = - windows_core::imp::FactoryCache::new(); - SHARED.call(callback) - } -} -impl windows_core::RuntimeType for JsonValidator { - const SIGNATURE: windows_core::imp::ConstBuffer = - windows_core::imp::ConstBuffer::for_class::(); -} -unsafe impl windows_core::Interface for JsonValidator { - type Vtable = IJsonValidator_Vtbl; - const IID: windows_core::GUID = ::IID; -} -impl windows_core::RuntimeName for JsonValidator { - const NAME: &'static str = "Sample.JsonValidator"; +#[repr(C)] +pub struct IJsonValidatorFactory_Vtbl { + pub base__: windows_core::IInspectable_Vtbl, + pub CreateInstance: unsafe extern "system" fn( + *mut core::ffi::c_void, + *mut core::ffi::c_void, + *mut *mut core::ffi::c_void, + ) -> windows_core::HRESULT, } -unsafe impl Send for JsonValidator {} -unsafe impl Sync for JsonValidator {} diff --git a/crates/samples/components/json_validator_winrt_client/build.rs b/crates/samples/components/json_validator_winrt_client/build.rs index 9f902f0324..5712ee01ba 100644 --- a/crates/samples/components/json_validator_winrt_client/build.rs +++ b/crates/samples/components/json_validator_winrt_client/build.rs @@ -1,5 +1,5 @@ fn main() { - if let Err(error) = windows_bindgen::bindgen([ + windows_bindgen::bindgen([ "--in", "../json_validator_winrt/sample.winmd", &format!("{}\\System32\\WinMetadata", env!("windir")), @@ -7,7 +7,6 @@ fn main() { "src/bindings.rs", "--filter", "Sample", - ]) { - panic!("{error}"); - } + "--flat", + ]); } diff --git a/crates/samples/components/json_validator_winrt_client/src/bindings.rs b/crates/samples/components/json_validator_winrt_client/src/bindings.rs index 37e8bd51ae..96829f9d38 100644 --- a/crates/samples/components/json_validator_winrt_client/src/bindings.rs +++ b/crates/samples/components/json_validator_winrt_client/src/bindings.rs @@ -7,44 +7,9 @@ dead_code, clippy::all )] -windows_core::imp::define_interface!( - IJsonValidator, - IJsonValidator_Vtbl, - 0xe09cb12b_b13c_5139_8c99_6140bf80deb9 -); -impl windows_core::RuntimeType for IJsonValidator { - const SIGNATURE: windows_core::imp::ConstBuffer = - windows_core::imp::ConstBuffer::for_interface::(); -} -#[repr(C)] -pub struct IJsonValidator_Vtbl { - pub base__: windows_core::IInspectable_Vtbl, - pub Validate: unsafe extern "system" fn( - *mut core::ffi::c_void, - core::mem::MaybeUninit, - *mut core::mem::MaybeUninit, - ) -> windows_core::HRESULT, -} -windows_core::imp::define_interface!( - IJsonValidatorFactory, - IJsonValidatorFactory_Vtbl, - 0x1cf4464e_ae9e_55d5_9539_0af4d8fc35aa -); -impl windows_core::RuntimeType for IJsonValidatorFactory { - const SIGNATURE: windows_core::imp::ConstBuffer = - windows_core::imp::ConstBuffer::for_interface::(); -} -#[repr(C)] -pub struct IJsonValidatorFactory_Vtbl { - pub base__: windows_core::IInspectable_Vtbl, - pub CreateInstance: unsafe extern "system" fn( - *mut core::ffi::c_void, - core::mem::MaybeUninit, - *mut *mut core::ffi::c_void, - ) -> windows_core::HRESULT, -} + #[repr(transparent)] -#[derive(PartialEq, Eq, Debug, Clone)] +#[derive(Clone, Debug, Eq, PartialEq)] pub struct JsonValidator(windows_core::IUnknown); windows_core::imp::interface_hierarchy!( JsonValidator, @@ -64,7 +29,7 @@ impl JsonValidator { core::mem::transmute_copy(value), &mut result__, ) - .and_then(|| windows_core::Type::from_abi(result__)) + .map(|| core::mem::transmute(result__)) } } pub fn CreateInstance(schema: &windows_core::HSTRING) -> windows_core::Result { @@ -91,7 +56,7 @@ impl windows_core::RuntimeType for JsonValidator { windows_core::imp::ConstBuffer::for_class::(); } unsafe impl windows_core::Interface for JsonValidator { - type Vtable = IJsonValidator_Vtbl; + type Vtable = ::Vtable; const IID: windows_core::GUID = ::IID; } impl windows_core::RuntimeName for JsonValidator { @@ -99,3 +64,39 @@ impl windows_core::RuntimeName for JsonValidator { } unsafe impl Send for JsonValidator {} unsafe impl Sync for JsonValidator {} +windows_core::imp::define_interface!( + IJsonValidator, + IJsonValidator_Vtbl, + 0xe09cb12b_b13c_5139_8c99_6140bf80deb9 +); +impl windows_core::RuntimeType for IJsonValidator { + const SIGNATURE: windows_core::imp::ConstBuffer = + windows_core::imp::ConstBuffer::for_interface::(); +} +#[repr(C)] +pub struct IJsonValidator_Vtbl { + pub base__: windows_core::IInspectable_Vtbl, + pub Validate: unsafe extern "system" fn( + *mut core::ffi::c_void, + *mut core::ffi::c_void, + *mut *mut core::ffi::c_void, + ) -> windows_core::HRESULT, +} +windows_core::imp::define_interface!( + IJsonValidatorFactory, + IJsonValidatorFactory_Vtbl, + 0x1cf4464e_ae9e_55d5_9539_0af4d8fc35aa +); +impl windows_core::RuntimeType for IJsonValidatorFactory { + const SIGNATURE: windows_core::imp::ConstBuffer = + windows_core::imp::ConstBuffer::for_interface::(); +} +#[repr(C)] +pub struct IJsonValidatorFactory_Vtbl { + pub base__: windows_core::IInspectable_Vtbl, + pub CreateInstance: unsafe extern "system" fn( + *mut core::ffi::c_void, + *mut core::ffi::c_void, + *mut *mut core::ffi::c_void, + ) -> windows_core::HRESULT, +} diff --git a/crates/samples/windows-sys/task_dialog/src/main.rs b/crates/samples/windows-sys/task_dialog/src/main.rs index 3b1abf0fdb..37583d4025 100644 --- a/crates/samples/windows-sys/task_dialog/src/main.rs +++ b/crates/samples/windows-sys/task_dialog/src/main.rs @@ -35,8 +35,8 @@ fn main() { } } -extern "system" fn callback(_: HWND, notification: i32, _: WPARAM, _: LPARAM, _: isize) -> HRESULT { - if notification == TDN_BUTTON_CLICKED { +extern "system" fn callback(_: HWND, notification: u32, _: WPARAM, _: LPARAM, _: isize) -> HRESULT { + if notification == TDN_BUTTON_CLICKED as _ { println!("button clicked"); } diff --git a/crates/samples/windows/core_app/src/main.rs b/crates/samples/windows/core_app/src/main.rs index 66a152e91b..d8c6bf58d5 100644 --- a/crates/samples/windows/core_app/src/main.rs +++ b/crates/samples/windows/core_app/src/main.rs @@ -16,7 +16,6 @@ struct CoreApp(); #[allow(non_snake_case)] impl IFrameworkViewSource_Impl for CoreApp_Impl { fn CreateView(&self) -> Result { - // TODO: need self query `self.into()` to support implementing both IFrameworkViewSource and IFrameworkView on the same object. Ok(CoreAppView().into()) } } diff --git a/crates/samples/windows/wmi/src/main.rs b/crates/samples/windows/wmi/src/main.rs index 4a7be64c4e..6d74818cc1 100644 --- a/crates/samples/windows/wmi/src/main.rs +++ b/crates/samples/windows/wmi/src/main.rs @@ -18,8 +18,15 @@ fn main() -> Result<()> { let locator: IWbemLocator = CoCreateInstance(&WbemLocator, None, CLSCTX_INPROC_SERVER)?; - let server = - locator.ConnectServer(&BSTR::from("root\\cimv2"), None, None, None, 0, None, None)?; + let server = locator.ConnectServer( + &BSTR::from("root\\cimv2"), + &BSTR::new(), + &BSTR::new(), + &BSTR::new(), + 0, + &BSTR::new(), + None, + )?; // // ExecQuery example