// case 1 传递 int* /////////////////////////////////////////////
extern “C” __declspec(dllexport) int mySum(int *a2,int *b2)
{
// 改变 a1, b1
*a2=...
*b2=...
return a+b;
}
public static extern int mySum (ref int a1,ref int b1); // c# 声明
///////////////////////////////////////////////////////////////////// // case 2 DLL 传入 char* 打印 /////////////////////////////////////////////
extern “C” __declspec(dllexport) void print(const char *str)
{
printf(str);
}
public static extern void print(string str); // c# 声明
///////////////////////////////////////////////////////////////////// // case 3 传入 char* 写回 ///////////////////////////////////////////
void foo(char* bar) {
// do write some information into char* bar
} [DllImport("foobar.dll")]
private static external void foo(StringBuilder bar); public String ReadFoo() {
StringBuilder result = new StringBuilder();
foo(result);
return result.ToString();
}
// 一些字符的处理
public String ReadFoo2() {
StringBuilder strBuilder = new StringBuilder();
foo(strBuilder);
Byte[] buf = Encoding.Unicode.GetBytes(strBuilder.ToString());
String result = Encoding.ASCII.GetString(buf);
}
///////////////////////////////////////////////////////////////////// // case 4 输入数组 ////////////////////////////////////////////////
[DllImport("foobar.dll")]
private unsafe static extern void getpicture(byte* imageBuffer); private byte[] GetImage() {
// size of the picture is 1024 * 1024 at RGB color, 8 bit each color
Byte[] rc = new Byte[ * * ]; // this block contains unsafe code!!!
unsafe {
// create the pointer by disabling garbage collection and
// memory reallocation
fixed (byte* rcPrt = rc) {
this.getpicture(rcPtr);
}
// devalidate pointer and reenable memory reallocation and
// garbage collection
}
// and get safe again return rc;
}
/////////////////////////////////////////////////////////////////
参考:https://www.gadgetweb.de/programming/38-cs-and-the-char-mess.html