1.c++ 创建类库(无公共语言运行时)
2.添加类 mydll.h文件
#pragma once
extern "C" _declspec(dllexport) int _stdcall add(int a,int b);
mydll.cpp文件
#include "stdafx.h"
#include "mydll.h"
int _stdcall add(int a, int b)
{
return (a+b);
}
2.c#
using System;
using System.Runtime.InteropServices;
namespace ConsoleAppTemp
{
class Program
{
[DllImport("DllTest.dll")] //DllImport 必须引用using System.Runtime.InteropServices;
public static extern int add(int a, int b);
//可以通过EntryPoint特性指定函数入口,然后为函数定义别名
[DllImport("HelloWorldLib.dll", EntryPoint = "add")]
public static extern int CustomName(int a, int b);
static void Main(string[] args)
{
int a = add(1, 2);
Console.WriteLine("输出结果为:" + a.ToString());
//跟上面是一样的
int b = add(1, 2);
Console.WriteLine("输出结果为:" + b.ToString());
Console.ReadKey();
}
}
}