Click here to Skip to main content
15,073,462 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
I have that code:

C++:
C++
class testfunction {
  public:
	virtual double operator()(double) = 0;
};


and i want use this class in C#, example

C#
C#
class myCSharpClass: testfunction
{
}


How i can do it?

What I have tried:

C++
extern "C"
{
  class testfunction {
  public:
	virtual double operator()(double) = 0;
  };
}
Posted
Updated 30-Jun-16 10:15am
v2
Comments
Philippe Mori 30-Jun-16 10:04am
   
You cannot derive from an unmanaged class in C#. So if this is what you want to do, then you have to write a managed class in C++/CLI...

Also, as far as I know, C# do not allows to override () operator (and I'm not sure if it is possible for managed C++ classes) so you might have to rename that function...

One solution would be to use Bridge pattern.
Software design pattern - Wikipedia, the free encyclopedia[^]
Bridge pattern - Wikipedia, the free encyclopedia[^]

Thus you create a bridge that would allow you to simulate a managed C# class deriving from an unmanaged C++ class. Of course, you cannot directly do that. But it is not hard to create a managed interface in C++/CLI that mimic your original interface and then have also a class that derive from your original interface and forward all calls to the managed interface.

As it required a few lines of code, I won't write it. If you know how to hold managed data inside a native class, then it should be relatively trivial to implement.

On the other hand, do you really need to implement a native C++ abstract class using managed C# class? In fact, mixing managed and native code does required some knowledge about interop.

By the way a book like this one might be very useful if you really want to write such code: Manning | C++/CLI in Action[^]

P.S. See also my comments under question and second solution.
   
v3
Take a look at this example: How to create dll in C++ for using in C# - Stack Overflow[^]

C++ Code (DLL), eg. math.cpp, compiled to HighSpeedMath.dll:
C++
extern "C"
{
    __declspec(dllexport) int __stdcall math_add(int a, int b)
    {
        return a + b;
    }
}

C# Code, eg. Program.cs:

C#
namespace HighSpeedMathTest
{
    using System.Runtime.InteropServices;

    class Program
    {
        [DllImport("HighSpeedMath.dll", EntryPoint="math_add", CallingConvention=CallingConvention.StdCall)]
        static extern int Add(int a, int b);

        static void Main(string[] args)
        {
            int result = Add(27, 28);
        }
    }
}
   
Comments
lorenzo todeschi 30-Jun-16 13:22pm
   
that not a solution for my problem, because. testfunction is an interface, so how I can extend the C# class with the C++ interface?
Philippe Mori 30-Jun-16 16:16pm
   
You cannot directly do that since your C++ class is not managed. See my solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900