日期:2014-05-18 浏览次数:20770 次
typedef struct { unsigned char ucOp; unsigned char ucHType; unsigned char ucHLen; unsigned char ucHops; unsigned long ulXID; unsigned short usSecs; unsigned short usFlags; unsigned long ulCIAddr; unsigned long ulYIAddr; unsigned long ulSIAddr; unsigned long ulGIAddr; unsigned char pucCHAddr[16]; char pcSName[64]; char pcFile[128]; unsigned char pucVend[64]; } tBOOTPPacket; tBOOTPPacket *pPacket; unsigned char pcPacketData[700]; pPacket = (tBOOTPPacket *)pcPacketData;
[StructLayoutAttribute(LayoutKind.Sequential)] public struct tBOOTPPacket { byte ucOp; byte ucHType; byte ucHLen; byte ucHops; uint ulXID; ushort usSecs; ushort usFlags; uint ulCIAddr; uint ulYIAddr; uint ulSIAddr; uint ulGIAddr; [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 16)] byte[] pucCHAdd; [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 64)] byte[] pcSName; [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 128)] byte[] pcFile; [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 64)] byte[] pucVend; } //字节数组转结构 byte[] bytes = new byte[300]; Type type = typeof(tBOOTPPacket); int size = Marshal.SizeOf(type); IntPtr p = Marshal.AllocHGlobal(size); Marshal.Copy(bytes, 0, p, size); tBOOTPPacket obj = (tBOOTPPacket)Marshal.PtrToStructure(p, type); Marshal.FreeHGlobal(p);