// the code is write in c
#ifdef __cplusplus
extern "C"{
#endif__declspec(dllexport) int __cdecl add(int a, int b);#ifdef __cplusplus
}
#endifint add(int a, int b)
{return a + b;
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;namespace TestApp
{class AdderWapper{[DllImport("AdderImpl.dll", CallingConvention = CallingConvention.Cdecl)]static extern private int add(int a, int b);static public int performAdd(int a, int b){// convert c# data to c data// TODO:// call the c interfaceint ret = add(a, b);// convert result from c data to c# data// TODO:// return the resultreturn ret;}}
} Program.cs中代碼為: [csharp] view plaincopyprint?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace TestApp
{class Program{static void Main(string[] args){int a = 3; int b = 5;int c = AdderWapper.performAdd(a, b);System.Console.WriteLine(c);}}
} 生成,也就是讓dll和exe在同一文件夾下