当结构包含字节变量时,如何避免多余的字节?(VB.NET)

人气:1,033 发布:2022-10-16 标签: .net vb.net byte

问题描述

VB.NET 4.5

我定义了一个只包含一个字节的结构。我需要获取要通过串口发送的字节数组。

Public Structure ExampleStructure
    Public variable1 As Byte
    Public variable2 As UInt16
    Public variable3 As UInt16

    Public Function getBytes() As Byte()
        Dim binaryBytes(5) As Byte
        Dim pointerCommand As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(Me))
        Marshal.StructureToPtr(Me, pointerCommand, False)
        Marshal.Copy(pointerCommand, binaryBytes, 0, Marshal.SizeOf(Me))
        Marshal.FreeHGlobal(pointerCommand)
        Return binaryBytes
    End Function
End Structure

问题是:

当我使用Marshal.AllocHGlobalMarshal.StructureToPtrMarshal.Copy时,返回的字节数组为6字节。.NET为Variable1创建了2个字节,因此在Variable1和Variable2数据之间有多余的字节。

我可以通过使用LayoutKind.Explicit并定义FieldOffsets来解决此问题。

<StructLayout(LayoutKind.Explicit)> _
Public Structure ExampleStructure
    <FieldOffset(0)> Public variable1 As Byte
    <FieldOffset(1)> Public variable2 As UInt16
    <FieldOffset(3)> Public variable3 As UInt16

    Public Function getBytes() As Byte()
        Dim binaryBytes(5) As Byte
        Dim pointerCommand As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(Me))
        Marshal.StructureToPtr(Me, pointerCommand, False)
        Marshal.Copy(pointerCommand, binaryBytes, 0, Marshal.SizeOf(Me))
        Marshal.FreeHGlobal(pointerCommand)
        Return binaryBytes
    End Function
End Structure

现在,当我获取字节数时,varable1和varable2之间不再有多余的字节。

尽管这似乎是一种笨拙的方式。有没有更好的选择,让我不必手动设置FieldOffsets?这个结构很简单,但它们可能会变得复杂得多。

推荐答案

修复非常简单-只需添加标记LayoutKind.Sequential,Pack:=1

<StructLayout(LayoutKind.Sequential, Pack:=1)> _
Public Structure ExampleStructure
    Public variable1 As Byte
    Public variable2 As UInt16
    Public variable3 As UInt16

    Public Function getBytes() As Byte()
        Dim binaryBytes(5) As Byte
        Dim pointerCommand As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(Me))
        Marshal.StructureToPtr(Me, pointerCommand, False)
        Marshal.Copy(pointerCommand, binaryBytes, 0, Marshal.SizeOf(Me))
        Marshal.FreeHGlobal(pointerCommand)
        Return binaryBytes
    End Function
End Structure

730