Skip to content

Commit 02c4f29

Browse files
authored
Update component sample to use static factory (#3154)
1 parent 7cec74f commit 02c4f29

File tree

1 file changed

+11
-22
lines changed
  • crates/samples/components/json_validator_winrt/src

1 file changed

+11
-22
lines changed

crates/samples/components/json_validator_winrt/src/lib.rs

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -63,31 +63,20 @@ fn json_from_hstring(value: &HSTRING) -> Result<serde_json::Value> {
6363
serde_json::from_str(&value).map_err(|error| Error::new(E_INVALIDARG, format!("{error}")))
6464
}
6565

66+
static JSON_VALIDATOR_FACTORY: StaticComObject<JsonValidatorFactory> =
67+
JsonValidatorFactory.into_static();
68+
6669
#[no_mangle]
67-
unsafe extern "system" fn DllGetActivationFactory(
70+
extern "system" fn DllGetActivationFactory(
6871
name: Ref<HSTRING>,
69-
result: *mut *mut std::ffi::c_void,
72+
factory: OutRef<IActivationFactory>,
7073
) -> HRESULT {
71-
if result.is_null() {
72-
return E_POINTER;
73-
}
74-
75-
let mut factory: Option<IActivationFactory> = None;
76-
7774
if *name == "Sample.JsonValidator" {
78-
factory = Some(JsonValidatorFactory.into());
79-
}
80-
81-
// Dereferencing `result` is safe because we've validated that the pointer is not null and
82-
// transmuting `factory` is safe because `DllGetActivationFactory` is expected to return an
83-
// `IActivationFactory` pointer and that's what `factory` contains.
84-
unsafe {
85-
if let Some(factory) = factory {
86-
*result = factory.into_raw();
87-
S_OK
88-
} else {
89-
*result = std::ptr::null_mut();
90-
CLASS_E_CLASSNOTAVAILABLE
91-
}
75+
factory
76+
.write(Some(JSON_VALIDATOR_FACTORY.to_interface()))
77+
.into()
78+
} else {
79+
_ = factory.write(None);
80+
CLASS_E_CLASSNOTAVAILABLE
9281
}
9382
}

0 commit comments

Comments
 (0)