Since C# is written in managed code and compiled into Microsoft intermediate, while C++ code is compiled into native machine code (also known as native C++ or unmanaged C++, VC6.0 is the platform for unmanaged C++ code), mixing programming between the two languages is somewhat difficult. A common approach is to use DllImport’s method, which is widely available on the web but won’t be described in detail here. But anyone who has used this method knows that it works fine for exporting functions but does not export unmanaged C++ classes! Very deadly.

However, in addition to C# and unmanaged C++, there is another language in the C series called managed C++. This language has almost the same syntax as unmanaged C++, but is compiled into Microsoft Intermediate language like C#, so that managed C++ can communicate well with C#, that is, managed C++ classes can be used in C#. In addition, managed C++ has two extremely important features: the ability to call unmanaged C++ classes and functions! An assembly that manages C++ can nest unmanaged C++ compiled machine code! What a powerful mix. So our technical path is clear: C# mediates unmanaged C++ classes and functions. In other words, a shell for unmanaged C++ code to be called by C#.

Environment: VS2017, Win10;

Step 1: create a CLR C++ class library. Name it Test_DLL



Step 2: Create an unmanaged method FunctionAdd class

FunctionAdd. H:

// The C function used to export is defined here

int Add(int a, int b);

FunctionAdd. CPP:

# include “stdafx. H”

# include “FunctionAdd. H”

Int Add(int a, int b) {return a + b; H: #pragma once class Native {public: Native(void); ~Native(void);

int menber; Int menderFuncSub(int a, int b); // Use the exported member function to implement arithmetic subtractionCopy the code

};

Native.cpp: #include “stdafx.h” #include “native.h”

// menber = 1; // menber = 1; }

Native::~Native(void)

{

}

Int Native::menderFuncSub(int a, int b) {return a-b; } create a new managed class clrClass clrclass.h: Pragma once #include “natively. H” // this is a managed C++ class that encapsulate C++ Native code classes and functions to use in C#. Otherwise, the class is not visible in the assembly. The keyword ref indicates that the class is a managed class and will be compiled into an intermediate language {public: clrClass(void);

int menber; Int menderFuncSub(int a, int b); // This member calls the public members of the unmanaged CClassNative class. // This member function wraps the unmanaged CClassNative public member function int menberFuncAdd(int a, int b); Int Add(int a,int b);Copy the code

private: Native * native; // Create an unmanaged class instance (instantiated in the constructor to “inherit” CClassNative public members and methods in clrClass)}; Clrclass.cpp: #include “stdafx.h” #include “clrclass.h” #include “functionadd.h”

using namespace System;

clrClass::clrClass(void) { native = new Native(); // Make sure you create objects! menber = native->menber; // this is a simple example. It is best to use the property method to read and write CClassNative members, similar to C#.

Int clrClass::menderFuncSub(int a, int b) {return native->menderFuncSub(a, b); }

// Implement arithmetic addition by calling C functions

int clrClass::menberFuncAdd(int a, int b)

{

return Add(a, b);

}

This implements clrClass managed wrappers for Native classes and C functions int Add(inta,int B). The generated test_dll. DLL “can be used directly in C#

Step 4: Create a new WinForm program and reference test_DL.dll



Code:

public partial class Form1 : Form

{

clrClass clr = null;

public Form1()

{

InitializeComponent();

clr = new clrClass();

}

private void button1_Click(object sender, EventArgs e) { int a = Convert.ToInt32(txtA.Text); int b = Convert.ToInt32(txtB.Text); Txtc.text = cl.menderFuncSub (a, b).toString (); } private void btnAdd_Click(object sender, EventArgs e) { int a = Convert.ToInt32(txtA.Text); int b = Convert.ToInt32(txtB.Text); Txtc.text = cl.menberfuncadd (a, b).tostring (); // Add by hosting C++ to call C function int Add(int a,int b). }}Copy the code