Skip to content

Commit

Permalink
Update component sample to use static factory (#3154)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr authored Jul 9, 2024
1 parent 7cec74f commit 02c4f29
Showing 1 changed file with 11 additions and 22 deletions.
33 changes: 11 additions & 22 deletions crates/samples/components/json_validator_winrt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,31 +63,20 @@ fn json_from_hstring(value: &HSTRING) -> Result<serde_json::Value> {
serde_json::from_str(&value).map_err(|error| Error::new(E_INVALIDARG, format!("{error}")))
}

static JSON_VALIDATOR_FACTORY: StaticComObject<JsonValidatorFactory> =
JsonValidatorFactory.into_static();

#[no_mangle]
unsafe extern "system" fn DllGetActivationFactory(
extern "system" fn DllGetActivationFactory(
name: Ref<HSTRING>,
result: *mut *mut std::ffi::c_void,
factory: OutRef<IActivationFactory>,
) -> HRESULT {
if result.is_null() {
return E_POINTER;
}

let mut factory: Option<IActivationFactory> = None;

if *name == "Sample.JsonValidator" {
factory = Some(JsonValidatorFactory.into());
}

// Dereferencing `result` is safe because we've validated that the pointer is not null and
// transmuting `factory` is safe because `DllGetActivationFactory` is expected to return an
// `IActivationFactory` pointer and that's what `factory` contains.
unsafe {
if let Some(factory) = factory {
*result = factory.into_raw();
S_OK
} else {
*result = std::ptr::null_mut();
CLASS_E_CLASSNOTAVAILABLE
}
factory
.write(Some(JSON_VALIDATOR_FACTORY.to_interface()))
.into()
} else {
_ = factory.write(None);
CLASS_E_CLASSNOTAVAILABLE
}
}

0 comments on commit 02c4f29

Please sign in to comment.