【问题标题】:Cannot convert structure in a byte array C#无法转换字节数组 C# 中的结构
【发布时间】:2016-01-02 11:06:28
【问题描述】:

我在将这些结构转换为字节数组时遇到问题:

[StructLayout(LayoutKind.Sequential)]
/// <summary>
/// Packet structure, type 1 (SEND)
/// </summary>
internal struct PACKET_SEND
{
    /// <summary>
    /// Packet Type: 0 for notifications, 1 for status, 2 for login and auth.
    /// </summary>
    public Byte Type;
    /// <summary>
    /// Packet Key: select it from Packets class.
    /// </summary>
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 61)]
    public char[] Key;
    /// <summary>
    /// Account Name.
    /// </summary>
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 30)]
    public char[] AccountName;
    /// <summary>
    /// Account Status: 0 for offline, 1 for online, 2 for absent, 3 for busy, 4 for invisible.
    /// </summary>
    public Byte Status;
}

[StructLayout(LayoutKind.Sequential)]
/// <summary>
/// Packet structure, type 2 (RECV)
/// </summary>
internal struct PACKET_RECV
{
    /// <summary>
    /// Packet Type: 0 for notifications, 1 for status, 2 for login and auth.
    /// </summary>
    public Byte Type;
    /// <summary>
    /// Packet Key: select it from Packets class.
    /// </summary>
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 61)]
    public char[] Key;
    /// <summary>
    /// Account Name.
    /// </summary>
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 30)]
    public char[] AccountName;
}

使用这些函数:

public static T ByteArrayToStructure<T>(byte[] data) where T : struct
    {
        GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
        T stuff = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(),
            typeof(T));
        handle.Free();
        return stuff;
    }

    public static byte[] StructToByteArray(object structure)
    {
        byte[] buffer = new byte[Marshal.SizeOf(structure)];
        GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
        Marshal.StructureToPtr(structure, handle.AddrOfPinnedObject(), false);//HERE GETS EXCEPTION
        handle.Free();
        return buffer;
    }

我在使用“StructToByteArray”方法时遇到了这个异常:

Type could not be marshaled because the length of an embedded array instance does not match the declared length in the layout.

想法?请不要只发布解决方案,但解释会更受欢迎:D。 问候。

【问题讨论】:

  • 确定你做对了吗?我做了一个快速测试,它对我有用。你能显示更多的代码吗?顺便说一句,我的原始代码没有显示这一点(stackoverflow.com/questions/8704161/…),但我建议将Marshal 调用包装到try ... finally

标签: c# arrays byte structure


【解决方案1】:

看...

Error : Type could not be marshaled because the length of an embedded array instance does not match the declared length in the layout

...和...

Error on size of class

...也许您没有初始化数组字段“Key”和“AccountName”,或者您没有正确初始化它们。这些值的大小必须始终为 61 和 30 个字符。

【讨论】:

  • 尝试一下。我确定密钥是 always 61 字符长度,但 AccountName 是可变的。所以去“填充”并尝试。
  • @Ciavi 您是否尝试单独转换数组?因为这应该不起作用,所以转换孔结构是有效的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-17
  • 2015-11-18
  • 1970-01-01
  • 1970-01-01
  • 2011-10-04
相关资源
最近更新 更多