Compile the build process:
1. Building a DLL project
Select the new Visual C + +
For both types of projects, the following interface will appear, where the build DLL is set:
2. Set up the project:
In the project properties, set:
3. Related code:
Because the name of the project is "Testcppdll", will automatically generate TestCPPDLL.h and TestCPPDLL.cpp two files, the. h file is the declaration file to export the content, in order to be able to clearly explain the problem, we will TestCPPDLL.h and TestCPPDLL.cpp two files of all the content is deleted, and then in Testcppdll Add the following to the. h:
Header file:
#define Testcppdll_api __declspec (dllexport)int __stdcall Add (intint b);
Source file:
int __stdcall Add (intint b) { return a + b;}
The test process is as follows:
Create a new C # empty project and modify the generated main file:
1. Add
Using System.Runtime.InteropServices;
2. Add in the static method:
[DllImport (@ "G:/vs13_project/testcppdll/release/testcppdll.dll", EntryPoint = "Add")]extern static int Add (int A, int b);
Specific as follows:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Runtime.InteropServices;//plus this line.namespacetestc{classProgram {[DllImport (@"G:/vs13_project/testcppdll/release/testcppdll.dll", EntryPoint ="ADD")] extern Static intADD (intAintb); Static voidMain (string[] args) { intc = ADD (1,2); Console.Write (c); Console.read (); } }}
Reference: http://jingyan.baidu.com/article/67508eb43f91869cca1ce49c.html
Compile to generate C + + exported function DLLs and test in C # project