Home > Net >  COM Interop with .NET 6
COM Interop with .NET 6

Time:10-08

We have a .NET class library written in C# that we're trying to upgrade to .NET 6 from .NET Framework v2.0. With .NET 2.0, the library is instantiated/consumed via C code which imports the library's .TLB.

In order to consume .NET 6 libraries using COM, you now have to add attributes to your C# interfaces/classes yourself, as well as the .csproj, so that an extra DLL called .comhost.dll is created at compile time, which you then register using regsvr32 as per https://learn.microsoft.com/en-us/dotnet/core/native-interop/expose-components-to-com. Using type libraries is no longer supported out of the box but I tried generating a TLB using the "dscom" utility here https://github.com/dspace-group/dscom. Unfortunately it kept failing with an error saying System.Windows.Forms could not be found and having spent a couple of days trying to get it work, I've given up.

Microsoft's tutorial doesn't suggest the best way of instantiating the newly created COM object, so I'm wondering if it's a case of going back to the old methods of calling CoCreateInstance or LoadLibrary, or if there's a better way? It would've been nice if the TLB-based approach still worked.

CodePudding user response:

Just an update on this. I managed to generate a type library using the dscom utility. I had to provide an additional argument called "--asmpath" to point to the location of the .NET runtime assemblies (in this case C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App\6.0.0) that my assembly depended on, and then it worked.

  • Related