C++ and c sharp intermodulation DLL implementation steps

  • 2020-04-02 02:41:20
  • OfStack

This article demonstrates the implementation steps of C++ and C# intertunable DLL, which can be used in sharing DLL dynamic link library for large projects. Specific methods are as follows:

C# call C++ DLL steps (can only export methods) :

1. C++ creates an empty project - > Add CPP files and functions to the source folder
2. In c++ property Settings, the configuration type is set to dynamic library DLL, and the common language runtime support is changed to/CLR
C# references c++ DLLS
C# declares c++ methods and adds the DllImport feature
5. The c# project property is set to: target platform x86
6. Pay attention to method type matching
7. Cause abnormal PInvokeStackImbalance: note: "_declspec" c + + and c # "CallingConvention = CallingConvention. Cdecl"

In addition, Can pass VS abnormal window, cancel to PInvokeStackImbalance anomaly detection :

Click the "debug - exception" VS, abnormal open window, a choice "Managed was Debugging Assistants \ PInvokeStackImbalance", the corresponding "trigger" optional box.  
 
Ii. C++ steps of calling c# DLL (can directly use c# class) :

1. Create a c++ console application
2. Copy c# DLL to c++ project root
3. Engineering attributes - > Configuration - > General - > Common language runtime support - > The CLR
Engineering attributes - > Configuration - > C/c + + routine - > Debug information format - > zi
Engineering attributes - > Configuration - > C/c + + routine - > Common language runtime support - > The CLR


#using "CSharpDllProject.dll"
using namespace CSharpDllProject;

 
C# call c++ class steps (c++/cli, can directly use c++ class)

About c++ / cli: the c++ / cli standard is based on the standard c++ submitted by Microsoft combined with the Common Language Infrastructure
1. Use c++/cli syntax to package standard c++ classes (can adopt the aggregation mode, reference standard c++ classes, and implement all standard c++ methods)
2. After c# references c++ DLL, you can directly new out a c++/cli created managed class object

I hope the methods described in this article are helpful to you.


Related articles: