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

Dynamic casting to COM implementation #3055

Merged
merged 1 commit into from
May 25, 2024

Conversation

sivadeilra
Copy link
Collaborator

@sivadeilra sivadeilra commented May 23, 2024

This provides a new feature for COM developers using the windows-rs crate. It allows for safe dynamic casting from IUnknown to an implementation object. It is based on Rust's Any trait.

Any type that is marked with #[implement], except for those that contain non-static lifetimes, can be used with dynamic casting.

Example:

#[implement(IFoo)]
struct MyApp { ... }

fn main() {
    let my_app = ComObject::new(MyApp { ... });
    let iunknown: IUnknown = my_app.to_interface();
    do_stuff(&iunknown);
}

fn do_stuff(unknown: &IUnknown) -> Result<()> {
    let my_app: ComObject<MyApp> = unknown.cast_object()?;
    my_app.internal_method();
    Ok(())
}

@sivadeilra sivadeilra marked this pull request as ready for review May 23, 2024 23:16
@kennykerr
Copy link
Collaborator

As discussed offline, we can extend the identity vtable pointer such that IDynamicCast inherits from IInspectable so that the instance size remains the same and ideally optimize interlocked cost as appropriate.

identity: &'static ::windows_core::IInspectable_Vtbl,

@kennykerr kennykerr changed the title Dynamic casting to implementation Dynamic casting to COM implementation May 24, 2024
dpaoliello
dpaoliello previously approved these changes May 24, 2024
This provides a new feature for COM developers using the windows-rs crate.
It allows for safe dynamic casting from IUnknown to an implementation object.
It is based on Rust's Any trait.

Any type that is marked with #[implement], except for those that contain
non-static lifetimes, can be used with dynamic casting.

Example:

```rust
struct MyApp { ... }

fn main() {
    let my_app = ComObject::new(MyApp { ... });
    let iunknown: IUnknown = my_app.to_interface();
    do_stuff(&iunknown);
}

fn do_stuff(unknown: &IUnknown) -> Result<()> {
    let my_app: ComObject<MyApp> = unknown.cast_object()?;
    my_app.internal_method();
    Ok(())
}
```
@riverar riverar merged commit ccd334f into microsoft:master May 25, 2024
90 checks passed
mati865 pushed a commit to mati865/windows-rs that referenced this pull request Jun 15, 2024
This provides a new feature for COM developers using the windows-rs crate.
It allows for safe dynamic casting from IUnknown to an implementation object.
It is based on Rust's Any trait.

Any type that is marked with #[implement], except for those that contain
non-static lifetimes, can be used with dynamic casting.

Example:

```rust
struct MyApp { ... }

fn main() {
    let my_app = ComObject::new(MyApp { ... });
    let iunknown: IUnknown = my_app.to_interface();
    do_stuff(&iunknown);
}

fn do_stuff(unknown: &IUnknown) -> Result<()> {
    let my_app: ComObject<MyApp> = unknown.cast_object()?;
    my_app.internal_method();
    Ok(())
}
```

Co-authored-by: Arlie Davis <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants