【C#学习笔记】调用C++生成的DLL

首先用vs2010建立win32项目,选择dll和空项目。

头文件add.h

extern "C" __declspec(dllexport) int add(int a,int b);

源文件add.cpp

#include "add.h"

int add(int a,int b)
{
    return a+b;
}

编译生成add.dll。

C#调用:

复制代码
using System;
using System.Runtime.InteropServices;

namespace ConsoleApplication2
{
    class Program
    {
        [DllImport("add.dll")]
        public static extern int add(int a, int b);
        static void Main(string[] args)
        {

            Console.Write(add(1, 2));
            Console.Read();
        }
    }
}
复制代码

调试报错,执行没有错。

原文地址:https://www.cnblogs.com/tiandsp/p/7440475.html