Skip to content

Commit 687f099

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

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

src/rt/mod.rs

+28-7
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,15 @@ pub trait RtNamedClass {
136136
fn name() -> &'static [u16];
137137
}
138138

139+
pub enum RegistrationStatus {
140+
Ok,
141+
NotRegistered,
142+
Error(u32),
143+
}
144+
139145
pub trait RtActivatable<Interface> : RtNamedClass {
140-
/// Returns a factory object to create instances of this class or to call static methods.
141146
#[inline]
142-
fn get_activation_factory() -> Interface where Interface: RtInterface + ComIid + RtType, Interface: RtType<Abi=*mut <Interface as ComInterface>::TAbi> {
147+
fn can_be_registered() -> RegistrationStatus {
143148
let mut res: <Interface as RtType>::Abi = unsafe { <Interface as RtType>::uninitialized() };
144149
let class_id = unsafe { HStringReference::from_utf16_unchecked(Self::name()) };
145150
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 +154,29 @@ pub trait RtActivatable<Interface> : RtNamedClass {
149154
hr = unsafe { RoGetActivationFactory(class_id.get(), <Interface as ComIid>::iid().as_ref(), &mut res as *mut *mut _ as *mut *mut VOID) };
150155
}
151156
if hr == S_OK {
152-
unsafe { <Interface as RtType>::wrap_nonnull(res) }
157+
RegistrationStatus::Ok
153158
} 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]))
159+
RegistrationStatus::NotRegistered
156160
} else {
157-
panic!("RoGetActivationFactory failed with error code 0x{:X}", hr as u32)
158-
}
161+
RegistrationStatus::Error(hr as u32)
162+
}
163+
}
164+
165+
/// Returns a factory object to create instances of this class or to call static methods.
166+
#[inline]
167+
fn get_activation_factory() -> Interface where Interface: RtInterface + ComIid + RtType, Interface: RtType<Abi=*mut <Interface as ComInterface>::TAbi> {
168+
match RtActivatable::can_be_registered() {
169+
RegistrationStatus::Ok => {
170+
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)) };
171+
}
172+
RegistrationStatus::NotRegistered => {
173+
let name = Self::name();
174+
panic!("WinRT class \"{}\" not registered", String::from_utf16_lossy(&name[0..name.len()-1]))
175+
}
176+
RegistrationStatus::Error(hr) => {
177+
panic!("RoGetActivationFactory failed with error code 0x{:X}", hr)
178+
}
179+
}
159180
}
160181
}
161182

0 commit comments

Comments
 (0)