Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update component sample to use static factory #3154

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}