8

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.ByValArray, 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.

David Hoerster
  • 28,421
  • 8
  • 67
  • 102
Hooch
  • 28,817
  • 29
  • 102
  • 161
  • Looking for a [tutorial](http://geekswithblogs.net/taylorrich/archive/2006/08/21/88665.aspx) on this perhaps? (or maybe [this one](http://bytes.com/topic/c-sharp/answers/249770-byte-structure) suites you better?) – Brad Christie Feb 19 '11 at 19:07

1 Answers1

-1

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

John Sobolewski
  • 4,512
  • 1
  • 20
  • 26
  • 3
    BinaryFormatter probably won't work, because it is used for serialization. You cannot throw a BinaryFormatter at something you didn't serialize and expect it to work. – R. Martinho Fernandes Feb 19 '11 at 19:36