Implementation steps for C + + and C # intermodulation DLLs _c language

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.