关于DllImport 调用c++动态链接库 (代码)

首先 .net 只能通过反射和添加引用的方式来调用.net生成的dll(他是一个程序集),不能通过dllimport来进行调用

下面是代码:

(首次运行的时候可能会报一个异常,设置一下就行了)。

(选择入口点的时候可能名称会有所变化,用exescope软件进行查看就行)

 (也可以使用dumpbin命令进行查看)

关于DllImport 调用c++动态链接库 (代码)

//C++代码
// Dll1.cpp : 定义 DLL 应用程序的导出函数。
//

#include "stdafx.h"

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

__declspec(dllexport) int subtract(int a, int b) {
	return a - b;
}








//C# 代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace test_DLL
{
   
    class Program
    {
        [DllImport("Dll1.dll",EntryPoint ="[email protected]@[email protected]")]
        private static extern int add(int a,int b);
        
        static void Main(string[] args)
        {
            int a=add(1,2);
            Console.WriteLine(a);
            Console.ReadKey();
        }
    }
}