C # Call two methods of unmanaged C ++ DLL,

Source: Internet
Author: User

C # Call two methods of unmanaged C ++ DLL,
 

C # The code written is a cross-platform managed code. The C ++ language can write managed and native code. In mixed programming of C # And C ++, C # is often used to call native C ++ DLL. Below are two commonly used call methods for your reference.

 

The detailed process of method 1 is as follows:

 

 

(2) Add the nativeCPP. h and nativeCPP. cpp files and write code respectively, for example:

NativeCPP. h:

 

NativeCPP. cpp: (function implementation)

File Content:

 

 

(4) Add the C # application console project to the solution,

And set it to start the project (right-click the leleapplication_test project ):

 

The content in the Program. cs file is as follows:

 

The compilation and running results are as follows:

 

 

2. detailed process of method 2 is as follows:

(1)Create a C ++ win 32 project

 

(2) Create the nativeCPP. h and nativeCPP. cpp files in the project.

 

 

 

 

(3) Add a new class library project to the solution:

Then, reference the nativeCPP project in the CLRWrapper project:

Add the following content in the CLRWrapper. h and CLRWrapper. cpp files respectively:

 

 

(4) Add the test project ConsoleApplication_test in solution and set it as the start item.

Then add a reference to CLRWrapper in the C # test project, and add the following code in the Program. cs file:

 

After compilation and running, the following situations may occur:

 

This is because the C # execution file cannot directly reference the CLRWrapper dependency nativeCPP. Therefore, you need to manually import the nativeCPP produced by the nativeCPP project. dll and nativeCPP. pdb two files copy to C # project bin under the Debug file inside (can also be set in VS2015 environment automatic copy, detailed process please see http://www.cnblogs.com/qiushui127/p/7147229.html ). The running result is as follows:

 

END !!!!

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.