日期:2014-05-18 浏览次数:20750 次
[StructLayout(LayoutKind.Sequential)] public struct GE_MSGEvent { public Int32 msgtype; public Int32 msglen; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1024 * 64)] public Byte[] data; } //...得到msg GCHandle gch = GCHandle.Alloc(msg.data, GCHandleType.Pinned); //固定数组 IntPtr ptr = Marshal.UnsafeAddrOfPinnedArrayElement(msg.data, 0); //得到数组首地址 //转化至GE_DevListMsg GE_DevListMsg devListMsg = (GE_DevListMsg)Marshal.PtrToStructure(ptr, typeof(GE_DevListMsg)); gch.Free();
------解决方案--------------------
byte是基本数据类型,任何数据类型都可以转换为byte的。另外上述定义的结构体其实可以也最好这样定义:
[StructLayout(LayoutKind.Sequential)]
public struct GE_MSGEvent
{
public Int32 msgtype;
public Int32 msglen;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024 * 64)]
public string data;
}