Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define an interface in C++ that needs to be implemented in C# and C++

I have an interface that I have defined in C++ which now needs to be implemented in C#. What is the best way to go about this? I don't want to use COM at all in my interface definition. The way I have solved this right now is to to have two interface definitions, one in C++ and one in C#. I then expose the C# interfaces as a COM server. This was my application which is written in C++ can call into C#. Is there anyway I can avoid having to define my implementation in C++ as well as C#?

like image 744
Juba Avatar asked Sep 15 '08 16:09

Juba


People also ask

What is an interface in C?

What Does Interface Mean? Interface, in C#, is a code structure that defines a contract between an object and its user. It contains a collection of semantically similar properties and methods that can be implemented by a class or a struct that adheres to the contract.

How do you define and implement an interface?

The implements keyword is used to implement an interface . The interface keyword is used to declare a special type of class that only contains abstract methods. To access the interface methods, the interface must be "implemented" (kinda like inherited) by another class with the implements keyword (instead of extends ).

What is the need of implementing interfaces?

By using interfaces, you can, for example, include behavior from multiple sources in a class. That capability is important in C# because the language doesn't support multiple inheritance of classes.

What is interface define with example?

An interface is a description of the actions that an object can do... for example when you flip a light switch, the light goes on, you don't care how, just that it does. In Object Oriented Programming, an Interface is a description of all functions that an object must have in order to be an "X".


2 Answers

If you are willing to use C++/CLI for your managed code instead of C#, then you can just consume the native C++ interface definition directly via the header file. How easy this will be will depend on exactly what is in your interface - simplest case is something that you could use from C.

Take a look at Marcus Heege's Expert C++/CLI: .NET for Visual C++ Programmers, for a lot of helpful information on mixing native and managed C++ in .NET.

like image 159
McKenzieG1 Avatar answered Oct 03 '22 17:10

McKenzieG1


Swig is a great tool for wrapping C++ classes in other languages like C#.

like image 29
Jeremy Avatar answered Oct 03 '22 17:10

Jeremy