c# – How to make C ++ and C # friends through COM in the simplest way?


All recipes for using COM include creating a type library, registering it in the registry, registering it in the server registry …

In this case, you need to create an object factory in the DLL.

If there is an executable file in C # and a DLL in C ++ – is it possible to do without all these steps and just load the dll ?


Yes, you can. COM technology does not require any registration in the registry for its use!

All the steps listed in the question are needed to reduce the connectivity of the modules. If these steps do not suit you, you do not need them, or you are simply too lazy to do them, you can skip them.

Here's a minimal example that works.

C #

using System;
using System.Runtime.InteropServices;

[ComVisible(true), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface ICallback
    void execute();

class Program : ICallback
    static void Main(string[] args)
        SetCallbacks(new Program());

    [DllImport("mylib", CallingConvention=CallingConvention.StdCall)]
    static extern void SetCallbacks(ICallback callback);

    void ICallback.execute()
        Console.WriteLine("Hello, world!");

C ++

#include "windows.h"

interface ICallback : IUnknown
    virtual HRESULT __stdcall execute() = 0;

extern "C" __declspec(dllexport) void __stdcall SetCallbacks(ICallback *cb) {

In the example above, the bilingual interfaces were written independently. In principle, this is a double job – so it makes sense to compose the interface only once and then import it.

If the interface is compiled in a C ++ project in the IDL language, then it is enough to then connect the type library to the C # project as a dependency. It is not necessary to register it!

If the interface is compiled in a C # project, then it is necessary to set it in addition to the above-listed Guid attribute and make it public. After that, you can pull out the type library through tlbexp and connect it to the C ++ project. Again, there is no need to register the type library in the registry.

Scroll to Top