You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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.
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.
Reacted by Alovchin91, 7mile and Muhammad Mominul Huque
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!
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:
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?
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.)
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.
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 👍
Alternatively, having a generated associated GetRuntimeClassName function on authored runtime classes would’ve already been a huge improvement for manual authoring support.
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.
Alternatively, having a generated associated GetRuntimeClassName function on authored runtime classes would’ve already been a huge improvement for manual authoring support.
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!
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.
Activity
Alovchin91 commentedon Dec 8, 2021
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!
kennykerr commentedon Dec 8, 2021
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.
Alovchin91 commentedon Dec 8, 2021
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
IActivationFactoryandDllGetActivationFactoryof course.kennykerr commentedon Dec 8, 2021
Yep, that's it. It's a bit error-prone which is also why I'm planning on automating much of that.
Alovchin91 commentedon Dec 8, 2021
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 🙂
kennykerr commentedon Dec 8, 2021
Sounds good!
Alovchin91 commentedon Dec 30, 2021
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-bindgenand how in my opinion it could be improved 🙂kennykerr commentedon Mar 14, 2022
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.
huiminyan2017 commentedon Apr 15, 2022
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!
kennykerr commentedon Apr 19, 2022
@hmyan90 take a look at #1705 - that should address your immediate need.
kennykerr commentedon Apr 26, 2022
0.36.0 has been released and includes the new
Event<T>type.nerocui commentedon Nov 3, 2022
Any update to this task? Looking forward to this so much!
kennykerr commentedon Nov 3, 2022
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
Ciantic commentedon Feb 4, 2023
Trying to understand this, I'm coming from
com-rscrate, which was deprecated in favor ofwindows-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
and implementing class for it
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.
13 remaining items
lesderid commentedon May 1, 2023
For WebView2 host objects, you can skip implementing
GetTypeInfoand just returnOk(0)fromGetTypeInfoCount. This only leaves implementingGetIDsOfNamesandInvoke, which is trivial for most cases, only theVARIANTs 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.)
[-]Support WinRT and COM class authoring[/-][+]Simplify WinRT and COM class authoring[/+]defims commentedon Mar 18, 2024
I wrote a macro called
wvwasi_macro::create_type_info_cratethat automatically generates a trait for gettingITypeInfo, which simplifies the implementation ofGetTypeInfo.Ciantic commentedon May 1, 2024
Something changed between 0.53 and 0.56, this code
Began to give an error that it requires a "windows_core" crate, I added
windows-corecrate and it works again.kennykerr commentedon May 1, 2024
#2917
Alovchin91 commentedon Sep 9, 2024
#3266
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 👍
Alovchin91 commentedon Sep 9, 2024
Alternatively, having a generated associated
GetRuntimeClassNamefunction on authored runtime classes would’ve already been a huge improvement for manual authoring support.kennykerr commentedon Sep 10, 2024
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 supportDllCanUnloadNowas unloading proved to be very unreliable in general, quite costly at run time, and of little value.kennykerr commentedon Sep 10, 2024
What about using the
RuntimeNametrait?Alovchin91 commentedon Sep 10, 2024
Oh! I didn't realise it was available. Great, thanks! 👍
kennykerr commentedon Feb 28, 2025
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!
Alovchin91 commentedon Feb 28, 2025
Kenny Kerr (@kennykerr) Just checking if I've missed it: is there now a reference counter for
DllCanUnloadNow, as mentioned in #2472 (comment)?kennykerr commentedon Feb 28, 2025
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.
Alovchin91 commentedon Feb 28, 2025
Okay, so I didn't miss anything 😄 Sounds fair to me, thanks!