您好,登錄后才能下訂單哦!
本篇內容主要講解“如何實現c++返回char**值傳給C#”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“如何實現c++返回char**值傳給C#”吧!
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),最終實驗也是成功的。
到此,相信大家對“如何實現c++返回char**值傳給C#”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。