-
Notifications
You must be signed in to change notification settings - Fork 199
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
COM interop support #306
Comments
I am making this up for grabs in case anybody is interested to work on this. We will be happy to provide guidance about details. This is a large project. |
cc: @kekekeks if you want to extend the MicroCom IDL reader you started working on. |
I had an idea on how to implement this. It involves a
I would appreciate assistance with point 1, and guidance with points 2 and 3. (Also, the “CoreCLR COM Wrappers” link in the OP is dead. You may want to fix that.) Thanks! |
@wjk |
@kekekeks Very cool! I’ll borrow use your generator code to emit the COM glue (under MIT). Thanks for the pointer! In fact, come to think of it, an IDL-like file would probably be a better choice for NativeAOT COM interop than reflecting the assemblies at build time. We would just need to manually generate default IDL files for the COM usage in Windows Forms and WPF, and include them in the package with ILC. For developer-defined COM types, they would need to write the IDL themselves. I looked at the |
I was planning to extract that code to a standalone roslyn generator. That way it should be possible to use C# interface definitions as the source too. Will probably do that during the winter holidays. |
Converting the tool to a roslyn generator is a bad idea, as some of the interfaces we need to handle are in libraries we do not control (and therefore cannot recompile with your generator). I praised the use of IDL precisely because it sidestepped this difficult issue. |
What exactly does that change? Interop bridges are registered separately, take another look at the generated code. Interfaces are plain C# interfaces. |
@kekekeks You are correct. Registration of wrappers do not reference the end-developer-visible interface definition they wrap; it is done purely via GUID. This means that a Roslyn analyzer would be a perfectly viable way to go, as long as we can point it at an already-compiled DLL and have it create wrappers for its contents as well. (I am thinking specifically of Windows Forms and WPF, which both use COM internally. One of my design goals here is to not require the recompilation of those assemblies from source. It’s certainly possible, but very inconvenient.) |
I have added more granularity above. If you only need COM interop (e.g. for WinForms), you can ignore the special WinRT lifetime management.
This is a common pain point for source generators, discussed in dotnet/roslyn#11149. There are workarounds, but none of them is pretty:
There can be more than one instance of ComWrappers alive in the process. The ComWrappers API is designed to allow libraries to carry their own local implementation of COM interop marshallers as internal implementation detail. The WPF shipping in .NET 5 is doing something similar for WinRT interop. There are a few internal implementation details in WPF that need WinRT interop, but we do not want to WPF to depend on the whole CsWinRT. The solution for that is "embedded" CsWinRT. We have internal instance of ComWrappers in WPF that handles the WinRT interop for the set of WinRT interfaces needed by WPF. WPF does not need the special WinRT lifetime management, so a local ComWrappers instance works for it just fine. There can be only one instance of ComWrappers registered for:
Notice that these two global registrations are independent, so the global WinRT lifetime registration does not prohibit you from registering your side-car assembly. |
Is IgnoresAccessChecksTo supported by NativeAOT? There is also a generator tool for creating dummy assemblies for Roslyn. |
Yes, it is. |
@jkotas is it possible that my previous PR for CoreRT tick at least some ticks? |
You can make it work either way. I think using the C++ implementation will be less new code, but it may be uphill battle to get it to build and the result will be less pretty than C# implementation. Yep, moving over the work you have done against CoreRT repo is a good way to start. Thank you! |
This should help in implementing dotnet/runtimelab#306
During previous discussion, I was pointed by @jkoritzinsky to https://github.com/microsoft/CsWinRT/blob/master/src/WinRT.Runtime/ComWrappersSupport.netstandard2.0.cs implementation which is change place from that time. I post link here, so I or anybody else who would like to pursue COM support will have this link around place where actual work happens. |
* Consolidate ComWrappers implementation across platforms This should help in implementing dotnet/runtimelab#306 * Fix incorrect and missing partial declarations * Update src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/ComWrappers.cs Co-authored-by: Jan Kotas <[email protected]>
@jkotas can you please update what's currently implemented? If something require additional work, it would be good to know about that also in such actionable list of items. |
@wjk Status quo is no more. Any issues which are in COM support can be counted as bugs in my opinion, but in general a lot things with COM will works. What I post is specifically to limitations of current ComWrappers implementation.
That's only things which I know so far. |
@kant2002 What are you using to provide the ComWrappers required by WinForms? Is it all hand-written or auto-generated?
This can be fixed by replacing |
WPF is using C++/CLI to inject code for module setup AFAIK. Unless they use module initializer to rewrite those code, I don't think WPF will work even with COM Wrapper implemented. |
WPF is using C++/CLI for more than that. The module initializer issue is a small problem. The overall WPF dependency on C++/CLI is the big problem. |
For now everything is handwritten. https://github.com/kant2002/CoreRTWinFormsTestBed/blob/master/WinFormsComInterop/WinFormsComWrappers.cs I have form which have almost all controls so more or less it's working. Right now most things would be in ComWrappers space and not in NativeAOT IMO. Code generation for ComWrappers is next thing which I will play. But this is that community can also works on. Will try replacing FileXxxRCW, but I secretly hoping that DLlImportGenerator will reach maturity soon. |
DllImportGenerator won't help you with replacing the coclass RCWs. |
@hez2010 That's bad. Anyway ball start rolling and I hope we all know how far WinForms will progress. |
@jkotas I take a look at runtime repository and usage of ComImport attribute,
Does this warrant implementation of ComImport/CoClass here, or same trick as in WinForms can be used where newing of objects can be replaced with CoCreateInstanceEx? |
We will need an overall plan for how to make libraries like this to officially work with trimming and without BuiltInComInterop dotnet/runtime#50500. You can start discussion in this issue about the plan, what libraries to address first, etc. The libraries in your list are among the ones that are not very actively maintained and the test coverage is not great either, so touching them will likely raise question about how we are ensuring that the change is not regressing anything. |
@jkotas does @MichalStrehovsky state of work on |
dotnet/runtime#72909 tracks the remaining work for |
This moves a bit towards dotnet#306 and dotnet#1453
Btw what's the status of |
|
Thanks for clarification. So if I read correctly through recent related PRs, does it mean COM interop support has been finished except some edge cases in shared generics with |
Yes, modulo bugs. |
COM source generator proposal: dotnet/runtime#60143 |
I think we can close, this was all resolved, there's even the COM source generator now. |
The Native AOT compiler does not support COM interop today. Attempt to use COM interop leads to errors like:
The right way to add support for COM interop to Native AOT runtime is combination of:
Add CoreCLR ComWrappers implementation to Native AOT build.CreateComInterfaceForObject
set of APIsCreateObjectForComInstance
set of APIsComWrappers.RegisterForMarshalling
- needed to replace built-in interopComWrappers.RegisterForTrackerSupport
- WinRT-specific lifetime managementIDynamicInterfaceCastable
Support IDynamicInterfaceCastable with shared generic code runtime#72909dotnet/corert#4219 has more discussion on this topic.
The text was updated successfully, but these errors were encountered: