Skip to content

Simplify WinRT and COM class authoring #1094

Closed

Description

@kennykerr

Dedicating an issue to this topic. Originally part of #81 (huge thread).

The windows crate now supports implementing COM and WinRT interfaces, but more is required to support classes as as whole. This early sample illustrates what's possible today:

https://github.com/kennykerr/component-rs

I still have much work to do to streamline this experience.

Activity

  1. Alovchin91 commented on Dec 8, 2021

    @Alovchin91
    Contributor

    Kenny Kerr (@kennykerr) Is it possible today to author WinRT components in Rust, either using windows-rs crate or not? 🤔

    I suppose it would require the use of Midl compiler + writing COM wiring manually. Could you maybe provide a sample of what can be done today?

    Thanks!

  2. kennykerr commented on Dec 8, 2021

    @kennykerr
    CollaboratorAuthor

    You can do so today but yes you'd need to write some IDL and call the MIDL compiler to produce a winmd. Next year I'm hoping to spend most of my time on improving authoring support and being able to produce a winmd directly from Rust so that IDL will not be required.

  3. Alovchin91 commented on Dec 8, 2021

    @Alovchin91
    Contributor

    Thank you for the quick response! 😊

    I don't mind writing IDL and calling Midl compiler manually. Having an ability to author WinRT components would unblock the use of Rust for me 🙂

    Do I understand correctly that I would need to call the windows::build! macro on the resulting .winmd's interfaces, and then #[implement] them? 🤔

    And implement an IActivationFactory and DllGetActivationFactory of course.

  4. kennykerr commented on Dec 8, 2021

    @kennykerr
    CollaboratorAuthor

    Yep, that's it. It's a bit error-prone which is also why I'm planning on automating much of that.

  5. Alovchin91 commented on Dec 8, 2021

    @Alovchin91
    Contributor

    Yeah, I can imagine 😅 I'm gonna try this out and if I succeed, I could open a PR to the Samples repo, if you would be interested 🙂

  6. kennykerr commented on Dec 8, 2021

    @kennykerr
    CollaboratorAuthor

    Sounds good!

  7. Alovchin91 commented on Dec 30, 2021

    @Alovchin91
    Contributor

    Hi Kenny Kerr (@kennykerr) , you might be interested to take a look:

    https://github.com/Alovchin91/winrt-component-rs

    Please let me know if you have any suggestions, comments etc. 🙂

    I'll also open a bunch of issues to share my experience with windows-bindgen and how in my opinion it could be improved 🙂

  8. kennykerr commented on Mar 14, 2022

    @kennykerr
    CollaboratorAuthor

    Thanks Alovchin91, that's very helpful. I'm now starting to work on this and #1093 in earnest. We should get to a point where MIDL is no longer required.

  9. huiminyan2017 commented on Apr 15, 2022

    @huiminyan2017

    Synced a bit offline, just want to leave a comment here, please also consider authoring event and delegate, prototype a winrt "event" type (similar like c++/winrt winrt::event struct ) so when implement the event, we can know how to manage event handler and token. Thanks!

  10. kennykerr commented on Apr 19, 2022

    @kennykerr
    CollaboratorAuthor

    @hmyan90 take a look at #1705 - that should address your immediate need.

  11. kennykerr commented on Apr 26, 2022

    @kennykerr
    CollaboratorAuthor

    0.36.0 has been released and includes the new Event<T> type.

  12. nerocui commented on Nov 3, 2022

    @nerocui
    Member

    Any update to this task? Looking forward to this so much!

  13. kennykerr commented on Nov 3, 2022

    @kennykerr
    CollaboratorAuthor

    I'm hard at work maturing support for component authoring. You can already implement components, with some limitations. There is an example here that you can use as a starting point:

    https://github.com/microsoft/windows-rs/tree/master/crates/tests/component

  14. Ciantic commented on Feb 4, 2023

    @Ciantic

    Trying to understand this, I'm coming from com-rs crate, which was deprecated in favor of windows-rs, and it had this kind of macros:

    #[com_interface("a5cd92ff-29be-454c-8d04-d82879fb3f1b")]
    and
    #[co_class(implements(IVirtualDesktopNotification))]

    Is the test component example somehow the same thing but without macros? I can't find similar types of macro shortcuts in there, maybe it's more manual now?


    Usage examples in com-rs

    Defining interface

    #[com_interface("CD403E52-DEED-4C13-B437-B98380F2B1E8")]
    pub trait IVirtualDesktopNotification: IUnknown {
        unsafe fn virtual_desktop_created(
            &self,
            monitors: ComRc<dyn IObjectArray>,
            desktop: ComRc<dyn IVirtualDesktop>,
        ) -> HRESULT;
    
        unsafe fn virtual_desktop_destroy_begin(
            &self,
            monitors: ComRc<dyn IObjectArray>,
            desktopDestroyed: ComRc<dyn IVirtualDesktop>,
            desktopFallback: ComRc<dyn IVirtualDesktop>,
        ) -> HRESULT;
       // ...
    }

    and implementing class for it

    #[co_class(implements(IVirtualDesktopNotification))]
    struct VirtualDesktopChangeListener {
        sender: Mutex<Option<VirtualDesktopEventSender>>,
    }
    
    impl IVirtualDesktopNotification for VirtualDesktopChangeListener {
        /// On desktop creation
        unsafe fn virtual_desktop_created(
            &self,
            _monitors: ComRc<dyn IObjectArray>,
            idesktop: ComRc<dyn IVirtualDesktop>,
        ) -> HRESULT {
            HRESULT::ok()
        }
    
        /// On desktop destroy begin
        unsafe fn virtual_desktop_destroy_begin(
            &self,
            _monitors: ComRc<dyn IObjectArray>,
            _destroyed_desktop: ComRc<dyn IVirtualDesktop>,
            _fallback_desktop: ComRc<dyn IVirtualDesktop>,
        ) -> HRESULT {
    
            HRESULT::ok()
        }
        // ...
    }

    Above are snipptes from my code I have a lots of code written with com-rs crate.

    I'm trying to reimplement these in windows-rs, but I can't find similar examples as in com-rs crate had.

  15. 13 remaining items

  16. lesderid commented on May 1, 2023

    @lesderid

    For WebView2 host objects, you can skip implementing GetTypeInfo and just return Ok(0) from GetTypeInfoCount. This only leaves implementing GetIDsOfNames and Invoke, which is trivial for most cases, only the VARIANTs are a bit annoying.

    (Edit: FWIW, I'd check whether this gives better performance than other ways of communicating with your Rust code if performance is what you're aiming for. For the Tauri app that I'm working on, host objects are actually slightly slower than whatever Tauri is doing for its built-in commands, at least for basic in/out objects, but YMMV.)

  17. changed the title [-]Support WinRT and COM class authoring[/-] [+]Simplify WinRT and COM class authoring[/+] on Sep 12, 2023
  18. defims commented on Mar 18, 2024

    @defims

    I wrote a macro called wvwasi_macro::create_type_info_crate that automatically generates a trait for getting ITypeInfo, which simplifies the implementation of GetTypeInfo.

  19. Ciantic commented on May 1, 2024

    @Ciantic

    Something changed between 0.53 and 0.56, this code

    #[interface("094d70d6-5202-44b8-abb8-43860da5aca2")]
    unsafe trait IValue: IUnknown {
        fn GetValue(&self, value: *mut i32) -> HRESULT;
    }
    

    Began to give an error that it requires a "windows_core" crate, I added windows-core crate and it works again.

  20. kennykerr commented on May 1, 2024

    @kennykerr
    CollaboratorAuthor
  21. Alovchin91 commented on Sep 9, 2024

    @Alovchin91
    Contributor

    #3266

    In an attempt to streamline and focus on what's most valuable, I'm making the difficult decision to drop support for metadata generation in general. While the existing metadata generation tooling is not ideal for Rust developers (e.g. midlrt and win32metadata), I don't realistically have the bandwidth to provide a Rust version any time soon.

    I think this actually makes a lot of sense, as long as authoring WinRT components is still a supported scenario. IDL works well and aligns with what C++/WinRT developers do. If we could get metadata generation support via IDL with shim like DllGetActivationFactory, DllCanUnloadNow etc. generation instead, that would be awesome 👍

  22. Alovchin91 commented on Sep 9, 2024

    @Alovchin91
    Contributor

    DllGetActivationFactory

    Alternatively, having a generated associated GetRuntimeClassName function on authored runtime classes would’ve already been a huge improvement for manual authoring support.

  23. kennykerr commented on Sep 10, 2024

    @kennykerr
    CollaboratorAuthor

    Yes, I plan to at least bring it on par with C++/WinRT in terms of the scaffolding provided for things like DllGetActivationFactory. Note that I have no plans to support DllCanUnloadNow as unloading proved to be very unreliable in general, quite costly at run time, and of little value.

  24. kennykerr commented on Sep 10, 2024

    @kennykerr
    CollaboratorAuthor

    Alternatively, having a generated associated GetRuntimeClassName function on authored runtime classes would’ve already been a huge improvement for manual authoring support.

    What about using the RuntimeName trait?

  25. Alovchin91 commented on Sep 10, 2024

    @Alovchin91
    Contributor

    What about using the RuntimeName trait?

    Oh! I didn't realise it was available. Great, thanks! 👍

  26. kennykerr commented on Feb 28, 2025

    @kennykerr
    CollaboratorAuthor

    Going to close this old issue as at a glance everything mentioned here has now been implemented and shipped. If there are any remaining gaps you'd like to see filled or improved please open a new issue to discuss. Thanks!

  27. Alovchin91 commented on Feb 28, 2025

    @Alovchin91
    Contributor

    Kenny Kerr (@kennykerr) Just checking if I've missed it: is there now a reference counter for DllCanUnloadNow, as mentioned in #2472 (comment)?

  28. kennykerr commented on Feb 28, 2025

    @kennykerr
    CollaboratorAuthor

    This isn't directly supported and I still stand by this comment:

    #2472 (comment)

    It's just not a good idea or practically impossible to implement reliably in a non-trivial system. It also adds measurable run time cost and complexity as it interacts with things like factory caching and every single implementation path.

  29. Alovchin91 commented on Feb 28, 2025

    @Alovchin91
    Contributor

    Okay, so I didn't miss anything 😄 Sounds fair to me, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions