Compile to generate C + + exported function DLLs and test in C # project

Source: Internet
Author: User

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

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.