我有一些麻烦,封送指针字符串数组。 它看起来无害的像这样:
typedef struct
{
char* listOfStrings[100];
} UnmanagedStruct;
这实际上是嵌入另一个这样的结构中:
typedef struct
{
UnmanagedStruct umgdStruct;
} Outerstruct;
非托管代码回调到托管代码,返回Outerstruct与内存分配和填充值一个IntPtr。
管理世界:
[StructLayout(LayoutKind.Sequential)]
public struct UnmanagedStruct
{
[MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr, SizeConst=100)]
public string[] listOfStrings;
}
[StructLayout(LayoutKind.Sequential)]
public struct Outerstruct
{
public UnmanagedStruct ums;
}
public void CallbackFromUnmanagedLayer(IntPtr outerStruct)
{
Outerstruct os = Marshal.PtrToStructure(outerStruct, typeof(Outerstruct));
// The above line FAILS! it throws an exception complaining it cannot marshal listOfStrings field in the inner struct and that its managed representation is incorrect!
}
如果我改变listOfStrings简单地是一个IntPtr然后Marshal.PtrToStructure工作,但现在我无法翻录成listOfStrings并提取字符串一个接一个。