Search code examples
c#.netcomvisual-studio-2022.net-8.0

Can I use a .NET 4.8 assembly that has COM support from a .NET 8.0 assembly


I have a .NET framework 4.8 assembly that supports COM, it has a typelib.

I tried to add it as a COM reference to my .NET 8.0 assembly, but got this error:

C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets(3010,5): warning MSB3290:
Failed to create the wrapper assembly for type library "{ce9f57a9-86a1-49a2-8aa9-63ec6d8a7afb}". Type library 'LMF_Tracker_Connection' was exported from a CLR assembly and cannot be re-imported as a CLR assembly.

I was hoping that I could use COM to use the .NET assembly's classes in my .NET 8.0 assembly.

Has anyone done anything like this before?

I registered my assembly using a bat file like this

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\regasm /u /tlb LMF.Tracker.Connection.dll
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\regasm /codebase LMF.Tracker.Connection.dll /tlb

I right clicked on my project dependencies and picked Add COM Reference then picked my typelib.

The reference had an exclamation mark against it and I saw the error message that I quoted above.

I haven't tried this before but I expected to see some classes added to my project that I could use to drive the other assembly via COM.

PS: I am going to ask the manufacturer for an updated assembly, my attempt to use COM is just a holding action until they get round to making a new assembly.


Solution

  • As Simon Mourier said:

    Calling a .NET Framework COM object from .NET 8 works fine, but no .NET version can import a TLB created by .NET Framework. What you can do is rewrite manually (in a .cs) the interfaces that were defined in the COM object (or decompile it with a tool like ILSpy), ie: you cannot "reference" the .NET Framework project. Once you've done that you can instantiate it like this var obj = (IMyInterface)Activator.CreateInstance(Type.GetTypeFromCLSID (yourClsid));