C + + Write DLL file called in C #

Source: Internet
Author: User

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 #

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.