Skip to content

Commit 2498109

Browse files
Add RtActivatable::can_be_registered method
1 parent 0dfa3de commit 2498109

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

src/rt/mod.rs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,16 @@ pub trait RtNamedClass {
136136
fn name() -> &'static [u16];
137137
}
138138

139+
#[derive(Clone, Copy, Debug, PartialEq)]
140+
pub enum RegistrationStatus {
141+
Ok,
142+
NotRegistered,
143+
Error(u32),
144+
}
145+
139146
pub trait RtActivatable<Interface> : RtNamedClass {
140-
/// Returns a factory object to create instances of this class or to call static methods.
141147
#[inline]
142-
fn get_activation_factory() -> Interface where Interface: RtInterface + ComIid + RtType, Interface: RtType<Abi=*mut <Interface as ComInterface>::TAbi> {
148+
fn can_be_registered() -> RegistrationStatus {
143149
let mut res: <Interface as RtType>::Abi = unsafe { <Interface as RtType>::uninitialized() };
144150
let class_id = unsafe { HStringReference::from_utf16_unchecked(Self::name()) };
145151
let mut hr = unsafe { RoGetActivationFactory(class_id.get(), <Interface as ComIid>::iid().as_ref(), &mut res as *mut *mut _ as *mut *mut VOID) };
@@ -149,13 +155,29 @@ pub trait RtActivatable<Interface> : RtNamedClass {
149155
hr = unsafe { RoGetActivationFactory(class_id.get(), <Interface as ComIid>::iid().as_ref(), &mut res as *mut *mut _ as *mut *mut VOID) };
150156
}
151157
if hr == S_OK {
152-
unsafe { <Interface as RtType>::wrap_nonnull(res) }
158+
RegistrationStatus::Ok
153159
} else if hr == REGDB_E_CLASSNOTREG {
154-
let name = Self::name();
155-
panic!("WinRT class \"{}\" not registered", String::from_utf16_lossy(&name[0..name.len()-1]))
160+
RegistrationStatus::NotRegistered
156161
} else {
157-
panic!("RoGetActivationFactory failed with error code 0x{:X}", hr as u32)
158-
}
162+
RegistrationStatus::Error(hr as u32)
163+
}
164+
}
165+
166+
/// Returns a factory object to create instances of this class or to call static methods.
167+
#[inline]
168+
fn get_activation_factory() -> Interface where Interface: RtInterface + ComIid + RtType, Interface: RtType<Abi=*mut <Interface as ComInterface>::TAbi> {
169+
match RtActivatable::can_be_registered() {
170+
RegistrationStatus::Ok => {
171+
unsafe { <Interface as RtType>::wrap_nonnull(RoGetActivationFactory(class_id.get(), <Interface as ComIid>::iid().as_ref(), &mut res as *mut *mut _ as *mut *mut VOID)) };
172+
}
173+
RegistrationStatus::NotRegistered => {
174+
let name = Self::name();
175+
panic!("WinRT class \"{}\" not registered", String::from_utf16_lossy(&name[0..name.len()-1]))
176+
}
177+
RegistrationStatus::Error(hr) => {
178+
panic!("RoGetActivationFactory failed with error code 0x{:X}", hr)
179+
}
180+
}
159181
}
160182
}
161183

0 commit comments

Comments
 (0)