首先用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#调用:

【C#学习笔记】调用C++生成的DLL
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();
        }
    }
}
【C#学习笔记】调用C++生成的DLL

调试报错,执行没有错。

相关文章:

  • 2021-12-30
  • 2022-12-23
  • 2022-12-23
  • 2021-04-10
  • 2022-03-07
  • 2022-12-23
  • 2022-12-23
  • 2021-06-11
猜你喜欢
  • 2021-09-24
  • 2022-12-23
  • 2021-04-05
  • 2022-12-23
  • 2022-01-08
  • 2022-12-23
相关资源
相似解决方案