编写一个 C 函数,返回指向数组第一个元素的指针和元素个数:
const char **GetPtxEditorColumnHeaders(int *count)
{
*count = 4;//or however you want to get hold of this information
return PtxEditorColumnHeaders;
}
然后声明 p/invoke:
[DllImport(@"mydll.dll", CallingConvention=CallingConvention.Cdecl)]
static extern IntPtr GetPtxEditorColumnHeaders(out int count);
像这样调用函数:
int count;
IntPtr PtxEditorColumnHeaders = GetPtxEditorColumnHeaders(out count);
List<string> headers = new List<string>();
for (int i=0; i<count; i++)
{
IntPtr strPtr = Marshal.ReadIntPtr(PtxEditorColumnHeaders);
headers.Add(Marshal.PtrToStringAnsi(strPtr));
PtxEditorColumnHeaders += Marshal.SizeOf(typeof(IntPtr));
}
这些东西很快就会变得乏味,此时 C++/CLI 包装器开始看起来像是一个更有吸引力的选择。