struct USMC_DEVICES_st 将转换为类似于:
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential)]
struct USMC_DEVICES_st
{
public UInt32 NOD;
public IntPtr serial;
public IntPtr Version;
}
可以使用以下代码访问序列号和版本:
IntPtr ser;
for (var i = 0;
(ser = Marshal.ReadIntPtr(um.serial, i)) != IntPtr.Zero;
i += Marshal.SizeOf(ser))
{
var serial = Marshal.PtrToStringAnsi(ser);
}
编辑- 在您的评论之后是访问结构成员的更详细示例:
static void PrintDevices(USMC_DEVICES_st um)
{
const int serialSize = 16;
const int verSize = 4;
int j = 0;
for (var i= 0; i < um.NOD; i++, j+= IntPtr.Size)
{
var ser = Marshal.ReadIntPtr(um.serial, j);
var ver = Marshal.ReadIntPtr(um.Version, j);
// ensure we check for null pointers - just in case
if (ver == IntPtr.Zero || ser == IntPtr.Zero) break;
Console.WriteLine("Device {0}, \tSerial number {1}",
Marshal.PtrToStringAnsi(ser, serialSize),
Marshal.PtrToStringAnsi(ver, verSize));
}
}