This example shows the implementation steps for C + + and C # intermodulation DLLs that can be used in large project sharing DLL dynamic-link libraries. The specific methods are as follows:
C # calls C + + DLL steps (only methods can be exported):
1. C + + Establish empty project-> source files folder add CPP files and functions
2. C + + property settings, the configuration type is set to a dynamic library DLL, and common language runtime support is changed to/clr
3. C # DLL referencing C + +
4. C # declares C + + methods and adds DllImport attributes
5. C # Engineering properties are set to: Target platform x86
6. Note the type matching of the method
7. Throw pInvokeStackImbalance Exception: note:C + + "_declspec" and C # "CALLINGCONVENTION=CALLINGCONVENTION.CDECL"
In addition, you can cancel the detection of the pinvokestackimbalance exception by using the VS exception window :
Click on vs "Debug-exception", open the Exception window, expand select "Managed Debugging Assistants\pinvokestackimbalance", remove the corresponding "throw" optional box.
Second, C + + to invoke the C # DLL (you can use the C # class directly):
1. Create a C + + console Application
2. Copy C # DLL to C + + engineering root directory
3. Engineering Properties-> Configuration-> General-> Common Language Runtime support->CLR
Engineering Properties-> Configuration->c/c++ General-> debug Information Format->zi
Engineering Properties-> Configuration->c/c++ General-> Common Language Runtime support->CLR
#using "CSharpDllProject.dll"
using namespace Csharpdllproject;
C # Call C + + class steps (C++/CLI, you can use C + + classes directly)
C++/CLI Introduction: C++/CLI standard is based on Microsoft-submitted standard C + + and the Common Language Infrastructure (Common Language infrastructure) technology
1. Use C++/CLI syntax to package standard C + + classes (use aggregation mode, reference standard C + + classes, implement all standard C + + methods)
2.c# referencing a C + + DLL, you can directly new a managed class object created by C++/cli
I hope the method described in this article will be helpful to everyone.