测试代码 dllTest.c
运行环境: Win10 Professional 、Visual Studio 2015 C# 控制台应用程序
//dllTest.c
__declspec(dllexport) int GetNumber();
int GetNumber()
{
return 20;
}
//Program.cs
using System;
using System.Runtime.InteropServices;
namespace dllImportDemo
{
class Program
{
[DllImport("dllTest.dll",CallingConvention = CallingConvention.Cdecl)]
public static extern int GetNumber(); //声明为外部函数
//函数签名需与C++签名相同(涉及到C++与C#数据类型的转换)
//函数必须要 static extern 标记
static void Main(string[] args)
{
Console.WriteLine(GetNumber());
Console.ReadKey();
}
}
}
运行结果:20