This is an extension of Microsofts basic example of providing a managed COM server in .NET Core/5+. Documentation on the inner workings of activation can be found here.
My goal was to make MS Access work with .net 6 code. Main pain-points where that back then it wasn't documented that this requires the build to target x86
and how to create a fitting .tlb
-file.
Both are addressed in this small sample. The trick for the tlb-issue is that you have a dummy implementation of your original .net 6 code class in the contract project that is used to generate the .tlb-file directly. The contract project is a netstandard2
project, allowing it to be run with (tlbexp.exe)[(https://docs.microsoft.com/en-us/dotnet/framework/tools/tlbexp-exe-type-library-exporter]
Run the following commands from the developer powershell
dotnet build
& 'C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools\TlbExp.exe' .\Contract\bin\x86\Debug\netstandard2.0\Contract.dll
move -force .\Contract.tlb .\ComServer\bin\x86\Debug\net6\
- Register the .dll from an admin cmd/Powershell from:
regsvr32.exe ComServer\bin\x86\Debug\net6\COMServer.comhost.dll
- The included
Access2003.mdb
can be used to import the .tlb-file (there is a link which is most likely wrong for your machine, or you copy the contents from step 3 to the folderc:\ComSample
) and the code should run.
The project will only build and run on the Windows platform.
Remember to unregister the COM server when the demo is complete regsvr32.exe /u ComServer\bin\x86\Debug\net6\COMServer.comhost.dll