Can I export a C++ interface with C-Style methods to a C# DLL?

This is a question posted right here on the stack overflow web site. None of the answers are completely correct since they do not address the real issue of the question. I consider those answers are work-around and they are not direct solution.

With using xInterop C++ .NET Bridge, the answer of course is, YES.

In C++, there is really no concept of an interface, the OP referred to the C++ abstract class by stating C++ interface. So let’s change the question to

Can I export a C++ abstract class with C-Style methods to a C# DLL?

Stating exporting a C++ abstract (base) class is not completely correct, it is useless to export a C++ abstract base class as an interface, because most of the instance methods are pointing to the address of the pure virtual method. Remember all the instance methods except the destructor are pure virtual methods if you want to represent an interface using a C++ abstract base class. There are a lot of talks about this topic on stack overflow web site, the followings are just some of them,

http://stack overflow.com/questions/12854778/abstract-class-vs-interface-in-c

http://stackoverflow.com/questions/318064/how-do-you-declare-an-interface-in-c?noredirect=1&lq=1

The real issue of the question represents is how one can retrieve the pointer and the virtual function table of a C++ abstract class, how one can find the virtual function address and marshal it to a C# .NET delegate. Okay, let’s discuss this question and answer step by step.

Assuming we have an interface named ICalculator which defines 4 methods, Add, Subtract, Multiply and Divide respectively as shown below.

Remember we already said that we can’t export ICaculator class because all the instance methods, Add, Subtract, Multiply and Divide are pure virtual methods, they are pointing to the pure virtual methods, even you export them, you will be calling into the pure virtual methods when you call any of the methods. This means writing the following code won’t work as expected if you intend to call methods on any ICalculator instance.

So if we can’t export ICalculator, how do we get an instance of ICaculator. As the OP asked, what is desired to export a c-style method to return an instance of a class deriving from the abstract base class. But first, lets implement a new class which is derived from ICalculator.

Now let’s simply return a pointer of a global variable of an instance of Calculator.

And the last thing is to define an export entry in the .def file.

When you build the DLL, all you get from the native DLL is just one method of GetCalculator as shown in the following screen-shot. I was using Explorer Suite to analyze the PE information of the native DLL, you may download the application from here. It is free thanks to Daniel Pistelli.

Export function GetCalculator using Explorer Suite

Now that we have built the native DLL of MathLib, we would want to create a .NET wrapper assembly using xInterop C++ .NET Bridge. Let’s fire up xInterop C++ .NET Bridge. For a native DLL as simple as MathLib which has only one header file to include, I will just show you a few screen-shots demonstrating how you can create an xInterop C++ .NET bridge project. I am using xInterop C++ .NET Bridge standard version .NET to native C++ bridge. The feature of creating C# wrapper class for a C++ abstract class is currently not included in the trial/evaluation version.

image

image

image

image

By adding ICalculator as an abstract class in the preceding GUI, we are telling xInterop C++ .NET Bridge to create a C# wrapper class for ICalculator.

Now, let’s generate the source code of all the C# wrapper class and the project file and solution file.

image

Once xInterop C++ .NET Bridge completes generating the C# wrapper source code, you will see the following message in the output window.

image

On the right site of xInterop C++ .NET Bridge, it is the solution explorer. You can see the complete solution as shown below.

image

Let’s look at the static C# wrapper method for GetCalculator,

The followings are the C# wrapper method for Add, Subtract, Multiply and Divide.

Because all the four methods are not exported, but we do know the address of the virtual function table and the index of the each virtual method, we can look into the virtual function table and get the address of each function and turn them into each delegate which we can call in the proceeding code. You can download xInterop C++ .NET Bridge and try yourself, the above code was generated by a paid version, if a trial version is used, the generated source code is obfuscated, you will still be able to test each generated C# methods though.

Translate »