Reputation: 1144
I'm trying to use COM interop, specifically with .net core and C++. I have a .net core library which I'm trying to expose to a C++ codebase.
The dotnet documentation mentions:
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.
I tried creating my own IDL file that declared the interface I wanted to create:
import "oaidl.idl";
import "ocidl.idl";
[object, uuid(9526A66B-200B-4157-AEDC-56F084962031), pointer_default(unique)]
interface IMyInterface : IUnknown {
HRESULT StartMethodA();
HRESULT StartMethodB();
};
Which I'm compiling with midl.exe test.idl
(I've also played around with extra tlb
params). However, this compilation step only creates .h
and .c
files, but not a .tlb
.
I've also looked into reverse 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'm quite new to COM and writing a 'bridge' between C++ and .net core, hopefully someone can give me some pointers that would allow this to work.
Resources
How do I expose a .netstandard2.0 library with COM for use in VB6?
https://learn.microsoft.com/en-us/windows/win32/midl/com-dcom-and-type-libraries#type-library
https://mark-borg.github.io/blog/2017/interop/
Calling C# code from C++, but ExecuteInDefaultAppDomain() is too limited
What do you do when MIDL can't create a tlb?
idl with library block
I've also wrapper the interface with a library
block, however, that did not result in a .tlb either (just like it's mentioned here).
[
uuid(123123V-200B-4157-AEDC-56F084962031),
version(1.0)
]
library grpcLibrary {
importlib("stdole2.tlb");
[object, uuid(9526A66B-200B-4157-AEDC-56F084962031)]
interface IKioskProcessor : IDispatch {
HRESULT StartAutomaticProcess();
HRESULT StartManualProcess();
};
};
PS C:\Program Files (x86)\Windows Kits\10\bin\10.0.22621.0\arm64> .\midl.exe C:\Users\kevin\grpc\GrpcLibrary.idl /tlb Foo.tlb
Microsoft (R) 32b/64b MIDL Compiler Version 8.01.0628
Copyright (c) Microsoft Corporation. All rights reserved.
64 bit Processing C:\Users\kevin\grpc\GrpcLibrary.idl
GrpcLibrary.idl
64 bit Processing C:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\um\oaidl.idl
oaidl.idl
64 bit Processing C:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\um\objidl.idl
objidl.idl
64 bit Processing C:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\um\unknwn.idl
unknwn.idl
64 bit Processing C:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\shared\wtypes.idl
wtypes.idl
64 bit Processing C:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\shared\wtypesbase.idl
wtypesbase.idl
64 bit Processing C:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\shared\basetsd.h
basetsd.h
64 bit Processing C:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\shared\guiddef.h
guiddef.h
64 bit Processing C:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\um\ocidl.idl
ocidl.idl
64 bit Processing C:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\um\oleidl.idl
oleidl.idl
64 bit Processing C:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\um\servprov.idl
servprov.idl
64 bit Processing C:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\um\urlmon.idl
urlmon.idl
64 bit Processing C:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\um\msxml.idl
msxml.idl
64 bit Processing C:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\um\oaidl.acf
oaidl.acf
64 bit Processing C:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\um\ocidl.acf
ocidl.acf
Upvotes: 1
Views: 370
Reputation: 15172
An IDL file with only interface declarations isn't going to generate a TLB, you need to declare a library containing the interfaces. It might also need coclass declarations as well, depending on your exact requirements.
Upvotes: 0