Create DLLs in C + + in VS2015 with C # calls and implement debugging on DLLs at the same time

Source: Internet
Author: User

Ext: 51075023

The advantage of creating a C # project in VS2015 and then creating a dynamic library DLL project to write is that the entire solution's programming environment is C # mode, so that there are many smart hints or quick programming options available throughout the solution.

One: Create a C # Console application: To invoke a DLL written in C + +

(1) Start VS2015 "file" new "project, in the Popup New Project dialog box to fill in the selection, first fill in the name of the project, and then modify the solution name.

(2) After the project is created, the overall effect is as follows:

Two: Create DLLs in C + +

The following is just the creation of a DLL, specifically to create a DLL, please refer to the "write in C + + in the VS2015 can be called by other languages Dynamic Library DLL"

(1) On the solution, right-click to select Add > New Project

(2) In the Add New Item dialog box, press to select Fill in

(3) In the Create Wizard dialog box, make the following selections

(4) In the Create Wizard dialog box, make the following selections

(5) After the project is created, the overall effect is as follows:

(6) Right click on "header file" to make the following selections

(7) In the Add New Item dialog box, select Fill in the following

(8) Add a reference to the CreateDLL.h in the CreateDLL.cpp file and add the test code shown

(9) Add the test code shown in the CreateDLL.h file

(10) Right click on "source file" to make the following selections

(11) In the Add New Item dialog box, select Fill in the following

(12) Add the test code shown in the Createdll.def file

(13) First switch the solution to release mode, and then right-click on the Createdll project name to select "Build" or "rebuild"

Note: DLL generated in release mode is final, DLL generated in debug mode sometimes problems

(14) Open the Release folder in the directory where the solution resides to see the generated DLL

(15) Use Dependency Walker to see if the exported function name is correct, drag CreateDLL.dll directly to the Dependency Walker software interface, as

(16) using the DLL function viewer (3.5) to see if the export function name and the number of parameters are correct, drag CreateDLL.dll directly to the DLL function viewer (3.5) software interface, as

Three: calling C + + to create a DLL with C # projects

(1) Set the C # project as the startup project and set the solution to debug mode

Write the following code in the C # project's Program.cs file, the code in the path of the DLL in the slash, the test found that the left oblique, right oblique can be, the relative path in the code refers to: the compiled EXE program with respect to the path of the DLL

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Runtime.InteropServices;usingSystem.Text;usingSystem.Threading.Tasks;namespacecsharpcalldll{classProgram {[DllImport (@".. /.. /.. /debug/createdll.dll", EntryPoint ="test01", SetLastError =true, CharSet = charset.ansi, exactspelling =false, CallingConvention =Callingconvention.stdcall)]extern Static intTEST01 (intAintBintc); [DllImport (@".. /.. /.. /debug/createdll.dll", EntryPoint ="test02", SetLastError =true, CharSet = charset.ansi, exactspelling =false, CallingConvention =Callingconvention.stdcall)]extern Static inttest02 (intAintb); Static voidMain (string[] args) {            intR1 = test01 (1,2,3); intr2 = test02 (5,2); Console.WriteLine ("test01 Results:"+R1.            ToString ()); Console.WriteLine ("test02 Results:"+R2.            ToString ());        Console.readkey (); }    }}

(3) Run C # project output as follows


(4) To implement a breakpoint in C + + project settings when the C # call DLL is implemented, select "Start native code debugging" in the C # project right-click Properties, Debug, start debugger

(5) Set breakpoints in the source code of C + + project, run C # program in debug mode will automatically jump to the breakpoint, for example, you can see that the values in the C # project have been uploaded to the C + + project.

(6) Source code: Create a DLL in C + + in VS2015 and call it in C # and implement debug source for DLL at the same time

(7) Dependency Walker software: Dependency Walker dynamic Library export function Viewer

(8) DLL function viewer (3.5) Software: DLL function viewer (3.5)

Create DLLs in C + + in VS2015 with C # calls and implement debugging on DLLs at the same time

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.