[英]Dllimport char* [] from C to C#
如何使用 C# 的 Dllimport 從 C 庫中獲取字符串數組
int GetClasses (int *NumberofClasses, char *ClassNames[], int arrayLength)
我已經嘗試 StringBuilder[] 和許多 MarshalAsAttributes 來接收 ClassNames 但仍然無法正常工作並導致 memory 違規
我對這個 C 方法的解釋如下。
int GetClasses(int * NumberofClasses, char * ClassNames[], int arrayLength)
This function will get the total number of unique classes and each unique class name.
Parameters:
NumberofClasses: Total number of unique classes.
ClassNames: Array of allocated char * to return class names.
arrayLength: Length of passed ClassNames array. If the result exceeds this array length, this function will fail with an error code.
Returns: Zero for success. Non-zero for error
我的聲明是
[DllImport(@".\library.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
private static extern Int16 GetClasses(ref UInt16 NumberofClasses, StringBuilder[] ClassNames, UInt16 arrayLength);
我有通過
類數=16,
ClassNames= StringBuilder[16] 每個容量為 256
數組長度=16
接到這個電話
最后我通過修改參數類型的聲明得到了成功調用。
[DllImport(@".\library.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
private static extern Int16 GetClasses(ref UInt16 NumberofClasses, [In][Out][MarshalAs(UnmanagedType.LPArray)] string[] ClassNames, UInt16 arrayLength);
並為參數 ClassNames 分配一個字符串數組
string[] names = new string[NumberOfClasses];
for (int i = 0; i < names.Length; i++)
{
names[i] = new string(new char[256]);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.