To create a project:
Project is the Win32 project, note that not the console, then select the DLL at the settings and tick "Export project", see
Start writing C + +:
Empty the. h file and the. cpp file with the same name as the project, and add the following code:
//Add in header file#ifdef win32project1_exports#define WIN32PROJECT1_API __declspec (dllexport)#else#define WIN32PROJECT1_API __declspec (dllimport)#endifextern"C"Win32project1_apivoid_stdcall ADD (intAintb);//Add in Source file#include "stdafx.h"#include "Win32Project1.h"#include <iostream>Win32project1_apivoid_stdcall ADD (intAintb) {Std::cout << a + b << std::endl;}
And then set the project properties to compile to C + + code
The final generation is ready.
I'm using win10, I can't see the generated DLL file in the file manager, but I can call it according to the path completely.
The code used is as follows:
namespace csharp调用Cppdll{ class Program { [DllImport(@"C:\Users\chengk\Documents\Visual Studio 2015\Projects\Win32Project1\Debug\Win32Project1.dll""Add")] externstaticvoid Add(int a,int b); staticvoid Main(string[] args) { Add(23); } }}
Operation diagram:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C + + Write DLL file called in C #