CodeBlocks 创建 test.dll
test.h
#ifndef TEST_H
#define TEST_H
#ifdef BUILD_DLL
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif // BUILD_DLL
int DLL_EXPORT mul(int,int);
int DLL_EXPORT add(int,int);
void DLL_EXPORT order(int[],int);
#endif // TEST_H
test.c
#include "test.h"
int DLL_EXPORT mul(int x,int y)
{
return x*y;
}
int DLL_EXPORT add(int x,int y)
{
return x+y;
}
void DLL_EXPORT order(int x[],int count)
{
int tmp,i,j;
for(i=1; i<count; i++)
{
for(j=count-1; j>=i; j--)
{
if(x[j]<x[j-1])
{
tmp=x[j-1];
x[j-1]=x[j];
x[j]=tmp;
}
}
}
}
C#中调用
[DllImport("test.dll", CallingConvention = CallingConvention.Cdecl)]
static extern int mul(int x, int y);
[DllImport("test.dll", CallingConvention = CallingConvention.Cdecl)]
static extern int add(int x, int y);
[DllImport("test.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void order(int[] x, int count);
static void Main(string[] args)
{
Console.WriteLine(mul(3, 5));
Console.WriteLine(add(3, 5));
int[] y = { 3, 2, 5, 9, 8, 1 };
order(y, y.Length);
foreach (int i in y)
{
Console.Write(i + " ");
}
Console.Read();
}