Steps for implementing C + + and C # intermodulation DLLs

Source: Internet
Author: User

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

Related Article

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.