C# Byte Array to struct [duplicate]
I want to use ReadProcessMemory function to fill this struct.
[StructLayout(LayoutKind.Explicit)]
public struct WinMineGameStruct
{
[FieldOffset(0x118)]
public Int32 xPressed;
[FieldOffset(0x118)]
public Int32 yPressed;
[FieldOffset(0x140)]
public Int32 MouseDown;
[FieldOffset(0x160)]
public Int32 GameStatus;
[FieldOffset(0x164)]
public Int32 IsInGame;
[FieldOffset(0x194)]
public Int32 MinesLeft;
[FieldOffset(0x330)]
public Int32 LevelMines;
[FieldOffset(0x334)]
public Int32 Colls;
[FieldOffset(0x338)]
public Int32 Rows;
[FieldOffset(0x6a0)]
public Int32 GameType;
[FieldOffset(0x6cc)]
public Int32 EasyBestScore;
[FieldOffset(0x6d0)]
public Int32 MediumBestScore;
[FieldOffset(0x6d4)]
public Int32 HardBestScore;
[FieldOffset(0x6d8)]
[MarshalAs(UnmanagedType.ByValArra开发者_如何学Cy, SizeConst = 64)]
public Char[] PlayerEasyName;
[FieldOffset(0x718)]
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
public Char[] PlayerMediumName;
[FieldOffset(0x758)]
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
public Char[] PlayerHardName;
[FieldOffset(0x798)]
public Int32 NumLastHitDiscovered;
[FieldOffset(0x79c)]
public Int32 TimePlayed;
[FieldOffset(0x7a4)]
public Int32 DiscoveredFields;
}
I know how to read byte array, int, string, short, and so on. I want to know how to convert byte array to this struct.
Wow... interesting question.
You might have a look at the binaryformatter... http://msdn.microsoft.com/en-us/library/b85344hz.aspx
That being said it looks like you have some binary array that doesn't exactly match your object definition. In this case I think you would have to read each piece of the array you are interested in and deserialize that chunck into the type you want ie int32 etc...
See this page on how to do the individual field from byte[] to type.. enter link description here
精彩评论