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

ComWrappers: are there any (Roslyn) Source Generators planned? #59651

Closed
obiwanjacobi opened this issue Sep 27, 2021 · 7 comments
Closed

ComWrappers: are there any (Roslyn) Source Generators planned? #59651

obiwanjacobi opened this issue Sep 27, 2021 · 7 comments

Comments

@obiwanjacobi
Copy link

I am dipping my toes into COM interop with the ComWrappers API.
The necessary plumbing code involved in any serious COM interop project is considerable.
My project has almost a 100 interfaces or structs.

So a logical question: (How) can we generate this code and are there any plans to provide this feature?

cc: @AaronRobinsonMSFT

@dotnet-issue-labeler dotnet-issue-labeler bot added area-Interop-coreclr untriaged New issue has not been triaged by the area owner labels Sep 27, 2021
@AaronRobinsonMSFT AaronRobinsonMSFT removed the untriaged New issue has not been triaged by the area owner label Sep 27, 2021
@AaronRobinsonMSFT
Copy link
Member

AaronRobinsonMSFT commented Sep 27, 2021

So a logical question: (How) can we generate this code and are there any plans to provide this feature?

@obiwanjacobi We are actively investigating options for .NET 7. Using Roslyn source generators is an option, another is to follow the C#/WinRT pattern which also has benefits. As we move further into the .NET 7 planning phase issues will be created and the community consulted on their opinion as well. @jkoritzinsky is going to be investigating options and starting that conversation when appropriate.

/cc @elinor-fung @jeffschwMSFT

@obiwanjacobi
Copy link
Author

Thanks @AaronRobinsonMSFT - So that will take a year at least..?
What if I can't wait that long and want to create something that at least covers my use case?
Is there any source code available (as inspiration) of the old COM interop (CCW/RCW) and how it handled marshaling?
I am mainly looking for guidance how to handle all those MarshalAs and StructLayout code attributes.

BTW: I am unfamiliar with C#/WinRT and did not understand its benefits from looking at the code...
/cc @jkoritzinsky

@AaronRobinsonMSFT
Copy link
Member

So that will take a year at least..?

That is a good estimate for an official release. We would likely have Preview releases much earlier though.

What if I can't wait that long and want to create something that at least covers my use case?
Is there any source code available (as inspiration) of the old COM interop (CCW/RCW) and how it handled marshaling?

That is entirely possible. I think at this point there are two main options.

  1. Manually write the interop code. This seems like a lot but once you start it is actually relatively quick going. I'd say about 2 weeks for ~100 interfaces and types. It could be faster depending on experience though.

  2. Use something like SharpGenTools to start with.

I am unfamiliar with C#/WinRT and did not understand its benefits from looking at the code...

The benefits are it is a standalone tool that reads an independent source of truth (i.e., WinMD) as opposed to C# which requires at least an initial definition be written in C#. There are composability approaches that could make a Roslyn source generator work like a standalone tool or a simpler tool that goes from WinMD, TLB, or IDL to C# and then use a Roslyn source generator to fill in the interop goo – lots of options. My intention was to point out that there the approach isn't obvious or clear given the complexity of UX with the Roslyn source generator nor is it great with a standalone tool. We also need to weight how the experience with work in non-Windows.

@obiwanjacobi
Copy link
Author

obiwanjacobi commented Sep 29, 2021

Thanks @AaronRobinsonMSFT for the pointers.

I just spent the better part of the day with SharpGenTools and although it looks very promising, I don't think it can generate the code I am looking for. For instance: I cannot get it to generate real C# interfaces from the C++ COM code. I have tried several mappings, bindings and what not but it's not doing it. Also, I cannot find how to put the generated code somewhere else than buried in the obj folder and how to actually use the generated code...

SharpGenTools is my preferred option for that actual source of what I am doing is a 3rd party Cpp Sdk. I imagine just having to run my generator project when a new Sdk version comes out... (would be nice)

Using ComWrapper -with or without code gen- seems a lot more work, provided I can massage SharpGenTools a bit more to do what I want...

I would be interested to know if the future plans for ComWrapper source gen includes generating from Cpp source code?

@AaronRobinsonMSFT
Copy link
Member

I would be interested to know if the future plans for ComWrapper source gen includes generating from Cpp source code?

@obiwanjacobi That would likely be a P2 since the C++ is very tricky to make cross-platform successfully and it is substantially more difficult to generate than canonical C#. It is something that is on our list of nice to haves but I am skeptical it would be a P0 or P1. I could see a potential P1 being generation of TLBs, from which C/C++ headers could be generated using Windows tooling though.

Keep the questions/comments/requests coming; these types of issues help to drive priorities.

@jkotas
Copy link
Member

jkotas commented Oct 8, 2021

Proposed plan: #60143

@AaronRobinsonMSFT
Copy link
Member

Closing this now that the tracking issue is available. See #66674.

@ghost ghost locked as resolved and limited conversation to collaborators Jun 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants