From 687f0998002fbc274dbaa08ec0f83ee0cb970a2b Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 6 Apr 2020 11:06:09 +0200 Subject: [PATCH] Add RtActivatable::can_be_registered method --- src/rt/mod.rs | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/src/rt/mod.rs b/src/rt/mod.rs index 76b073f..5544269 100644 --- a/src/rt/mod.rs +++ b/src/rt/mod.rs @@ -136,10 +136,15 @@ pub trait RtNamedClass { fn name() -> &'static [u16]; } +pub enum RegistrationStatus { + Ok, + NotRegistered, + Error(u32), +} + pub trait RtActivatable : RtNamedClass { - /// Returns a factory object to create instances of this class or to call static methods. #[inline] - fn get_activation_factory() -> Interface where Interface: RtInterface + ComIid + RtType, Interface: RtType::TAbi> { + fn can_be_registered() -> RegistrationStatus { let mut res: ::Abi = unsafe { ::uninitialized() }; let class_id = unsafe { HStringReference::from_utf16_unchecked(Self::name()) }; let mut hr = unsafe { RoGetActivationFactory(class_id.get(), ::iid().as_ref(), &mut res as *mut *mut _ as *mut *mut VOID) }; @@ -149,13 +154,29 @@ pub trait RtActivatable : RtNamedClass { hr = unsafe { RoGetActivationFactory(class_id.get(), ::iid().as_ref(), &mut res as *mut *mut _ as *mut *mut VOID) }; } if hr == S_OK { - unsafe { ::wrap_nonnull(res) } + RegistrationStatus::Ok } else if hr == REGDB_E_CLASSNOTREG { - let name = Self::name(); - panic!("WinRT class \"{}\" not registered", String::from_utf16_lossy(&name[0..name.len()-1])) + RegistrationStatus::NotRegistered } else { - panic!("RoGetActivationFactory failed with error code 0x{:X}", hr as u32) - } + RegistrationStatus::Error(hr as u32) + } + } + + /// Returns a factory object to create instances of this class or to call static methods. + #[inline] + fn get_activation_factory() -> Interface where Interface: RtInterface + ComIid + RtType, Interface: RtType::TAbi> { + match RtActivatable::can_be_registered() { + RegistrationStatus::Ok => { + unsafe { ::wrap_nonnull(RoGetActivationFactory(class_id.get(), ::iid().as_ref(), &mut res as *mut *mut _ as *mut *mut VOID)) }; + } + RegistrationStatus::NotRegistered => { + let name = Self::name(); + panic!("WinRT class \"{}\" not registered", String::from_utf16_lossy(&name[0..name.len()-1])) + } + RegistrationStatus::Error(hr) => { + panic!("RoGetActivationFactory failed with error code 0x{:X}", hr) + } + } } }