C#Byte Array to struct

时间:2011-02-19 19:03:41

标签: c# .net memory data-structures bytearray

我想使用ReadProcessMemory函数来填充此结构。

    [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;
}

我知道如何读取字节数组,int,string,short等。 我想知道如何将字节数组转换为此结构。

2 个答案:

答案 0 :(得分:8)

这篇文章为您提供了一些信息。

Reading a C/C++ data structure in C# from a byte array

答案 1 :(得分:-1)

哇...有趣的问题。

您可以查看二进制格式器... http://msdn.microsoft.com/en-us/library/b85344hz.aspx

据说你看起来有一些二进制数组并不完全匹配你的对象定义。在这种情况下,我认为你必须阅读你感兴趣的每个数组,并将该chunck反序列化为你想要的类型,即int32等...

请参阅此页面,了解如何从byte []键入单个字段。 enter link description here