This article mainly introduces C + + and C # intermodulation DLL implementation steps, DLL dynamic link library sharing in some large projects have some application value, the need for friends can refer to the next
This example shows the implementation steps of C + + and C # intermodulation DLLs, which can be used in a large project shared DLL dynamic link library. Here's how:
One, C # calls C + + DLL steps (only export methods):
1. Add CPP files and functions to the Source files folder, create an empty project with C + +
2. In C + + property settings, the configuration type is set to dynamic library DLL, and the common language runtime support is changed to/clr
3. C # Reference DLL for C + +
4. C # declares the C + + method and adds the DllImport attribute
5. C # project Property set to: Target platform x86
6. Note the type matching of the method
7. Throw pInvokeStackImbalance Exception: note: C + + "_declspec" and "Callingconvention=callingconvention.cdecl" in C #
In addition, you can cancel the detection of the pinvokestackimbalance exception by using the exception window of VS :
Click on the "Debug-exception" of VS, open the Exception window, expand select "Managed Debugging Assistants\pinvokestackimbalance", remove the corresponding "throw" optional box.
Second, C + + calls the C # DLL's steps (you can use the C # class directly):
1. Create a C + + console Application
2. Copy the C # DLL to the C + + engineering root directory
3. Common language Runtime Support--general--configuration---project Properties---->CLR
Project properties, Configuration->c/c++ General, debug Information Format->zi
Configuration->c/c++ General--Common language Runtime support->CLR, project properties
#using"CSharpDllProject.dll"usingnamespace Csharpdllproject;
C # calls C + + class steps (c + +/CLI, can be used directly from the + + Class)
Introduction to C + +/CLI: C + +/CLI Standard is a technology based on Microsoft-submitted standard C + + and Common Language Infrastructure (Common Language Infrastructure)
1. Wrapping standard C + + classes using C + +/CLI syntax (aggregation mode, reference to standard C + + classes, implementation of all standard C + + methods)
2.c# reference C + + DLLs, you can directly new a managed class object created by C + +/CLI
I hope the method described in this article is helpful to everyone.
Citation links;
Steps for implementing C + + and C # intermodulation DLLs
Methods for calling unmanaged DLLs from a C # program
Reference book: Proficient. NET interop Pinvoke,c++interop and COM Interop
Steps for implementing C + + and C # intermodulation DLLs