docs icon indicating copy to clipboard operation
docs copied to clipboard

More detail needed on "Expose .NET Core components to COM"

Open chrisjbreisch opened this issue 3 years ago • 3 comments

The page says:

Unlike in .NET Framework, there is no support in .NET Core or .NET 5+ for generating a COM Type Library (TLB) from a .NET assembly. The guidance is to either manually write an IDL file or a C/C++ header for the native declarations of the COM interfaces. If you decide to write an IDL file, you can compile it with the Visual C++ SDK's MIDL compiler to produce a TLB.

Great. How do I manually create an IDL file? How would I manually create the C/C++ header file (probably my preference unless there's a good reason not to do it this way). And if I use an IDL file, how do I compile with the MIDL compiler? And once I do that, what do I do with the TLB?

I'm sure all of this is documented elsewhere. COM interop has been around for ages. But without knowing more specifics, particularly in how it relates to .NET Core, this paragraph is frustratingly useless.

It would be great to see a .h file or a .IDL file for the example shown earlier on the page (btw, the class definition, not just the interface would be useful also).


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

chrisjbreisch avatar Jul 17 '22 18:07 chrisjbreisch

@chrisjbreisch A bit late to the party, but did you find a way to manually create the IDL file? I'm currently having the same issue. In regards to your other comments:

I'm sure all of this is documented elsewhere. COM interop has been around for ages. But without knowing more specifics, particularly in how it relates to .NET Core, this paragraph is frustratingly useless.

It would be great to see a .h file or a .IDL file for the example shown earlier on the page (btw, the class definition, not just the interface would be useful also).

I couldn't agree more.

Resources

https://learn.microsoft.com/en-us/previous-versions/windows/desktop/automat/contents-of-a-type-library

https://learn.microsoft.com/en-us/windows/win32/midl/com-dcom-and-type-libraries#type-library

kevinetore avatar Feb 09 '24 17:02 kevinetore

@kevinetore - I ended up not using COM for this. I don't recall what I actually did, and I can't find the code. It was for a project for myself, not for work. I may have deleted it later. There is a good response on options for this on StackOverflow here: https://stackoverflow.com/questions/778590/calling-c-sharp-code-from-c-but-executeindefaultappdomain-is-too-limited

I think I used what is described as the Reverse P/Invoke. If I can find the code I used, I'll post it here.

chrisjbreisch avatar Feb 12 '24 12:02 chrisjbreisch

@chrisjbreisch Thanks for your response. I looked into pinvoke and also dllexport. In both cases (from my understanding) I was required to provide my c# as static methods. I could accomplish this by creating a static function which creates a new instance of the object whose member functions I need it to call. That does however feel like nasty workarounds.

I think I'll research COM a bit more, and try to get it to compile. I think via COM we're a bit more flexible when it comes to the whole static/instance stuff.

kevinetore avatar Feb 12 '24 13:02 kevinetore