2012-09-17

Convert char** (string array) to C# string[]

底下範例是將 C/C++ 的字串陣列指標 (char**) 轉成 C# 內部使用的 string[] :

C/C++ 端的函數指標為 :
typedef void (* pCBFunc)(int, char**);

對應的 C# 端的函數為 :
public static void callback(int nArrsz, IntPtr pStringArray)
{
    string[] data = new string[nArrsz];
    IntPtr tmpPtr = pStringArray;
 
    for (int i = 0; i < nArrsz; ++i)
    {
        IntPtr strPtr = 
            (IntPtr)Marshal.PtrToStructure(tmpPtr, typeof(IntPtr));
        data[i] = Marshal.PtrToStringAuto(strPtr);
 
        tmpPtr = new IntPtr(tmpPtr.ToInt64() + IntPtr.Size);
    }
}
其中的 int nArrsz 為字串數目,IntPtr pStringArray 為字串陣列 ( 對應至 C 的 char** ) ,string[] data 是轉型後的資料。

1 則留言 :

  1. The phase development additionally be|may also be|can be} attributed 온라인카지노 to the increased spending capability of the global population on gambling. The rising gross sales of smartphones across the globe are also encouraging vendors to supply casino games through online platforms. The Sports Betting phase accounts for the largest market share and is estimated to develop at a CAGR of 12.5% over the forecast period. The use of smartphones and computers for efficiently placing bets on sports is driving the expansion of the sports betting phase. The proliferation of sports betting advertisements globally is predicted to drive phase development over the forecast period.

    回覆刪除