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”

 

// Implement C function (arithmetic addition)

int Add(int a, int b)

{

  return a + b;

}

Step 3: Create an unmanaged Native class

Native. H:

#pragma once

class Native

{

public:

    Native(void);

    ~Native(void);

 

int menber; // A member for the export

int menderFuncSub(int a, int b); // Use the exported member function to implement arithmetic subtraction

};

 

Native. CPP:

#include “stdafx.h”

#include “Native.h”

 

Native::Native(void)

{

/ / structure

    menber = 1;

}

 

Native::~Native(void)

{

}

 

// this is arithmetic subtraction implemented by an unmanaged C++ class

int Native::menderFuncSub(int a, int b)

{

    return a – b;

}

Step 3 Create a new managed class clrClass

ClrClass. H:

 #pragma once

#include “Native.h”

// this is a managed C++ class that encapsulates C++ native code classes and functions for use in C#

public ref class clrClass

// The class must be declared public, otherwise it 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; // This member accesses the public members of the unmanaged CClassNative class. (In fact, you only need to wrap the public members and public member functions.

int menderFuncSub(int a, int b); // This member function is used to wrap public member functions of unmanaged CClassNative

int menberFuncAdd(int a, int b); Int Add(int a,int b);

 

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; // here is a simple example, it is best to use the property method to read and write CClassNative class members, similar to C#, managed C++ has property functions, please check the usage

}

 

// Implement arithmetic subtraction by calling unmanaged CClassNative subtraction functions

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);

// Add(int a,int b) by hosting C++ call C function int Add(int a,int b)

            txtC.Text = clr.menderFuncSub(a, b).ToString();

        }

 

        private void btnAdd_Click(object sender, EventArgs e)

        {

            int a = Convert.ToInt32(txtA.Text);

            int b = Convert.ToInt32(txtB.Text);

// Add(int a,int b) by hosting C++ call C function int Add(int a,int b)

            txtC.Text = clr.menberFuncAdd(a, b).ToString();

        }

    }