不安全的代码可以吗?
public unsafe struct UnmanagedStruct
{
public short size;
public ushort** shortValues;
}
[DllImport("example.dll")]
public static extern void GetUnmanagedStruct(out UnmanagedStruct s);
如果你有一个指向 s 数组的指针ushort
:
public static unsafe void WriteValues()
{
UnmanagedStruct s;
GetUnmanagedStruct(out s);
for (var i = 0; i < s.size; i++)
{
ushort x = (*s.shortValues)[i];
Console.WriteLine(x);
}
}
如果您有一个以空结尾的 s 数组数组ushort
:
public static unsafe void WriteValues()
{
UnmanagedStruct s;
GetUnmanagedStruct(out s);
for (var i = 0; i < s.size; i++)
{
for (ushort* p = s.shortValues[i]; p != null; p++)
{
ushort x = *p;
Console.WriteLine(x);
}
}
}