Search code examples
c#.netcomc++-clidcom

Creating a Managed C++ interface to use in C#


We have a collection of Native C++ business rules wrapped in C++/CLI and then by a C# layer so we can expose it via DCOM.

The Native C++ business rules are in a DLL and are easily invoked in C++/CLI which is in a COM visible DLL. We then add this DLL as a reference to the C# project.

The C# layer has a lot of classes derived from ServicedComponent and we apply regsvcs to get it registered as a DCOM server.

However a need has arisen for the C++/CLI layer to communicate to the C# layer. To do this I was thinking of using a C# style interface. The C# layer will derive from the interface and pass it down to the C++/CLI layer which can then call methods on it to get something to occur in the C# layer. This interface will need to be declared in the C++/CLI layer though for visibility. This is my problem I do not seem to be able to achieve this.

Is there a way to declare a C# style interface in C++/CLI and then use it in C#?


Solution

  • There is (https://msdn.microsoft.com/en-us/library/737cydt1.aspx):

    #pragma managed
    using namespace System;
    
    public interface class MyInterface
    {
        void MyFunction();
    }
    

    And used in C#:

    public class MyClass : MyInterface
    {
        public void MyFunction() { }
    }