C#调用Dll
创建c++空项目:添加.h文件与.cpp文件,并编写内容;
修改项目属性-配置类型为动态库(dll)
8470.h
#pragma once
extern "C" {
_declspec(dllexport) int add(int a, int b);
}
8470.cpp
#include "8470.h"
int add(int a, int b) {
return a + b;
}
生成动态链接库(dll)
创建C#控制台程序并编写内容
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace Console8470
{
internal class Program
{
[DllImport("8470.dll",CallingConvention = CallingConvention.Cdecl)]
public static extern int add(int a,int b);
static void Main(string[] args)
{
Console.WriteLine(add(4, 5));
Console.ReadKey();
}
}
}