簡體   English   中英

從C到C#的元數據結構

[英]Marshal struct from C to C#

編輯:我簡化了我的示例...在真實的代碼中,我沒有正確地使用wcscpy_s就將值分配給strMyStringx,所以不是分配值,我只是傳遞了指針,該指針在編組值時已超出范圍進入托管代碼...

我正在嘗試將具有三個字符串屬性的結構從C編組為C#,但是我無法在C#中正確獲取該結構的定義。 所有屬性都打印為垃圾。 我編組錯誤還是我的財產類型錯誤?

我的自定義結構:

typedef struct _MY_STRUCT_STRING {
    LPWSTR strMyString1;
    LPWSTR strMyString2;
    LPWSTR strMyString3;
}MY_STRUCT_STRING, *PMY_STRUCT_STRING;

我的C函數返回一個指向該結構的指針數組:

bool bEnumerateString(OUT LONG &i_arr_size, OUT PMY_STRUCT_STRING* &pArrStringStruct)
{
// [...] function simplified to demonstrate building a pointer to an array of struct*
long i_arr_size = 3

PMY_STRUCT_STRING *ptr_arr_string = (PMY_STRUCT_STRING *)malloc(sizeof(PMY_STRUCT_STRING)* i_arr_size);

for (int i = 0; i < i_arr_size; i++) {
    ptr_arr_string[i] = (PMY_STRUCT_STRING)malloc(sizeof(MY_STRUCT_STRING));
    ptr_arr_string[i]->strMyString1 = L"String 1"; // This would work. In the real code I was assigning values from another array and mistakenly passed the pointer rather than doing wcscpy_s
    ptr_arr_string[i]->strMyString2 = L"String 2";
    ptr_arr_string[i]->strMyString3 = L"String 3";
}

pArrStringStruct = ptr_arr_string;

return true;
}

C#:

    //Import the DLL with my function
    [DllImport("My.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "bEnumerateString")]
    internal static extern bool bEnumerateString(out long count, out IntPtr pArrStringStruct);

     // Define the C# equivalent of the C struct
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    internal struct MY_STRUCT_STRING
    {
        [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 8)]
        public string strMyString1;
        [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 8)]
        public string strMyString1;
        [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 8)]
        public string strMyString1;
    }

   [...]

    // Code to marshal (try... catch etc removed for succinctness)
   IntPtr pArrStruct = IntPtr.Zero;
   long lCount = 0;

   bool bResult = false;
   bResult = bEnumerateString(out lCount, out pArrStruct);

   if (!bResult)
   {
    // Marshal to deref pointer
       IntPtr[] pArrStructList = new IntPtr[lCount];
       for (ulong i = 0; i < (ulong)lCount; i++)
       {
            pArrStructList[i] = Marshal.ReadIntPtr(pArrStruct, Marshal.SizeOf(typeof(IntPtr)) * (int)i);
       }


    // Marshal pointers to struct
       var lstMyStringStrct = new List<MY_STRUCT_STRING>(pArrStructList.Length);

       foreach (IntPtr ptr in pArrStructList)
       {         
            lstMyStringStrct.Add((MY_STRUCT_STRING)Marshal.PtrToStructure(ptr, typeof(MY_STRUCT_STRING)));
       }

    // Enumerate struct
       foreach (MY_STRUCT_STRING myStr in lstMyStringStrct)
       {
           // All of these outputs are garbage
           Console.WriteLine("strMyString1: " + myStr.strMyString1);
           Console.WriteLine("strMyString2: " + myStr.strMyString2);
           Console.WriteLine("strMyString3: " + myStr.strMyString3);
        }

    }

我看到一個問題。 您的C ++結構使用LPWSTR(指針),而您的C#代碼則需要固定大小的char數組。

從以下位置更改您的字符串:

[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 8)]
public string strMyString1;

在定義C ++結構時將使用以下代碼:

char strMyString1[8];

至:

[MarshalAsAttribute(UnmanagedType.LPWStr)]
public string strMyString1;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM