Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Native C++ and C# interop

Tags:

c++

c#

.net

interop

So I'm architecting an application that does necessarily C++ work, but MFC/ATL is too messy for my liking, so I had this brilliant idea of doing all the "thinking" code in native C++ and all the pretty UI code in C#. The problem, though, is interoperability between the two of them. Before I get too carried away with this, I was wondering if this is a solved problem, and there's a really good way to do this. Note that I don't want to mix logic and display in the same module, as it gives rise to annoyingly high coupling.

Here's what I have so far:

enter image description here

So tell me, can it be done better?

like image 435
Clark Gaebel Avatar asked Dec 31 '09 22:12

Clark Gaebel


People also ask

What does native C mean?

Native code is computer programming (code) that is compiled to run with a particular processor(such as an Intel *86-class processor) and its set of inctruction. If the same program is run on a computer with a different processor, software can be provided so that the computer emulates the original processor.

What is native C C++?

The Native Development Kit (NDK) is a set of tools that allows you to use C and C++ code with Android, and provides platform libraries you can use to manage native activities and access physical device components, such as sensors and touch input.

Is C++ a native language?

C++ is considered a native language because it compiles directly into machine code that can be understood by the underlying system. C# must first compile into Microsoft Intermediate Language (MSIL) before the just-in-time (JIT) compiler generates machine code. For this reason, C++ is typically faster than C#.

What is the difference between C and C+?

In a nutshell, the main difference between C and C++ is that C is function-driven procedural language with no support for objects and classes, whereas C++ is a combination of procedural and object-oriented programming languages.


1 Answers

The easiest way to handle this is to use C++/CLI, and expose your logic as .NET types.

It's very easy to wrap a native C++ class in a ref class that's usuable directly from a C# user interface.

That being said - this was my plan, originally, in my current project. My thinking was that I'd need the native code for some of the heavy math work we typically do. I've found, however, that it's been easier, faster, and nicer to just move most of my logic directly into C# (separated from the UI code, but still in a C# assembly) rather than try to implement it in C++.

My experience has been that speed has not been an issue - unsafe C# code has nearly always managed to be as fast or faster than the equivelent C++ when tuned, and it's easier to profile and tune the C# code.

like image 122
Reed Copsey Avatar answered Sep 19 '22 02:09

Reed Copsey