小编Sni*_*000的帖子

Marshal.SizeOf给出错误的大小

下面的代码在使用Marshal.SizeOf时给出了错误的大小,但是我不确定为什么。

这是我要获取的大小的Struct:

//[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
//[Serializable]
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct BLEGenericMsg
{
    public BLEMessageHdr msg_hdr;
    public byte[] msg_body;

    public BLEGenericMsg(int messageSize)
    {
        msg_hdr = new BLEMessageHdr();
        msg_body = new byte[messageSize];
    }
};
Run Code Online (Sandbox Code Playgroud)

以下是填充该结构并调用serialize函数的代码:

    BLEGenericMsg hostKeyMsg = new BLEGenericMsg(serializedPublicBytes.Length);
    hostKeyMsg.msg_hdr.msg_id = MESSAGE_BASE_EVENT + EVENT_HOST_PUBLIC_KEY;

    hostKeyMsg.msg_body = serializedPublicBytes;

    //Only get the size of the body for the entire message, not counter or header
    hostKeyMsg.msg_hdr.msg_body_sz = (uint)hostKeyMsg.msg_body.Length;
    BluetoothLEHardwareInterface.Log("public Key Size: " + hostKeyMsg.msg_hdr.msg_body_sz + "\n");

    byte[] temp = …
Run Code Online (Sandbox Code Playgroud)

c# arrays struct sizeof marshalling

5
推荐指数
1
解决办法
949
查看次数

标签 统计

arrays ×1

c# ×1

marshalling ×1

sizeof ×1

struct ×1