以前的帖子說到c++函數的參數是char**,這種函數在C#中用什麼方式接收(用ref IntPtr[]),但其實根據這個帖子中所說,哪方分配內存,對於另一方函數參數的類型形式是不同的,所以本次說如果返回值是char**/int**等怎麼處理呢?
char **arr;
int arrlen,arrcol;
template<class T>
void make2DArray(T** &ptr, int rows, int cols)
{
//創建二維數組,先創建行指針
ptr = new T*[rows];
//再爲每一行分配空間
for (int i = 0; i < rows; i++)
{
ptr[i] = new T[cols];
}
}
void alloc2D()
{
arrlen = 3;
arrcol = 2;
arr = new char*[arrlen];//分配3個元素的數組空間,每個元素是char*指針元素
for (int i = 0; i <= arrlen; i++)
{
arr[i] = new char[arrcol*(i+1)+1]; //其實不同行可以長度不同
}
}
void* returnpptr(int* len) //C++分配內存,返回**給C#
{
alloc2D();
*len = 3;
for (int i = 0; i < arrlen; i++)
{
for (int j = 0; j <= arrcol*(i + 1)+1; j++)
{
if (j == arrcol*(i + 1)+1)
arr[i][j] = 0;
else
arr[i][j] = (char)(0x31 + i + j % 5);
}
}
return arr;
}
[DllImport("dllfordebugdemo.dll", EntryPoint = "returnpptr", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr returnpptr(ref int len);
private void button7_Click(object sender, EventArgs e)
{
int len =0;
IntPtr p = returnpptr(ref len);
IntPtr[] ps = new IntPtr[len];
for(int i =0;i<len;i++)
{
ps[i] = Marshal.ReadIntPtr(p + Marshal.SizeOf(typeof(IntPtr)) * i);
Console.WriteLine(Marshal.PtrToStringAnsi(ps[i]));
}
}
顯示如下:
123
23456
3456734
如果C++返回的是int**,實驗如下:
int** ppcol;
int* p1;
int reflen;
void* returnpptrint(int* len)
{
reflen = 1;
*len = reflen;
arrcol = 2;
p1 = &arrcol;
if (ppcol == NULL)
{
ppcol = &p1;
}
return ppcol;
}
[DllImport("dllfordebugdemo.dll", EntryPoint = "returnpptrint", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr returnpptrint(ref int len);
private void button7_Click(object sender, EventArgs e)
{
int len =0;
IntPtr p = returnpptrint(ref len);
IntPtr[] ps = new IntPtr[len];
int[] lens = new int[len];
for (int i = 0; i < len; i++)
{
ps[i] = Marshal.ReadIntPtr(p + Marshal.SizeOf(typeof(IntPtr)) * i);
Marshal.Copy(ps[i],lens,0,1);
Console.WriteLine("lens[i]=" + lens[i]);
}
}
顯示:
lens[i]=2
總結:
C#無法接收C++返回值的char**/int**,只能先強轉爲void*,然後在C#中public static extern IntPtr fun(...),實際使用時二級指針的行數,並且通過IntPtr[]數組和Marshal.ReadIntPtr(基址+偏址),再用Marshal.Copy或PtrToString...方式獲取最終值。
到目前爲止:三級指針不管是參數還是返回值形式,都不成功(估計還是以後有時間再做實驗)。
上面統一以c# IntPtr接收C++的void*(實際是char**/int**強轉而來),雖然實驗成功,但換種思維方式,c++中還是char**/int** fun(...),而c#中以IntPtr fun(...)可不可以呢?
[DllImport("dllfordebugdemo.dll", EntryPoint = "returnpptr", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr returnpptr(ref int len);
private void button7_Click(object sender, EventArgs e)
{
int len =0;
IntPtr p = returnpptr(ref len);
IntPtr[] ps = new IntPtr[len];
for (int i = 0; i < len; i++)
{
ps[i] = Marshal.ReadIntPtr(p + Marshal.SizeOf(typeof(IntPtr)) * i);
Console.WriteLine(Marshal.PtrToStringAnsi(ps[i]));
}
}
void** returnpptr(int* len) //C++分配內存,返回**給C#
{
alloc2D();
*len = 3;
for (int i = 0; i < arrlen; i++)
{
for (int j = 0; j <= arrcol*(i + 1)+1; j++)
{
if (j == arrcol*(i + 1)+1)
arr[i][j] = 0;
else
arr[i][j] = (char)(0x31 + i + j % 5);
}
}
return (void**)arr;
}
或者將返回值改爲實際的char**:
char** returnpptr(int* len) //C++分配內存,返回**給C#
{
alloc2D();
*len = 3;
for (int i = 0; i < arrlen; i++)
{
for (int j = 0; j <= arrcol*(i + 1)+1; j++)
{
if (j == arrcol*(i + 1)+1)
arr[i][j] = 0;
else
arr[i][j] = (char)(0x31 + i + j % 5);
}
}
return (arr;
}
C#中還是IntPtr returnpptr(ref int len),最終實驗也是成功的。