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 !!!!