1

Heres a sample:

Imports System.Runtime.InteropServices

<StructLayout(LayoutKind.Explicit)>
Public Structure TimeStamp32
    <FieldOffset(0)>
    Public I32 As Int32
    <FieldOffset(0)>
    <VBFixedArray(3)> Public Bytes() As Byte
End Structure

It gives me an error saying "Could not load type TimeStamp32 ..blah blah.. because it contains an object field at offset 0 that is incorrectly aligned or overlapped by a non-object field." While i could overlap the int with a single or whatever other 4 byte variable no problem.

I don't actually need this for that structure but it would greatly help me with managing my 128bit ID structure and potentially others if theres a way to make it work.

Jake Freelander
  • 1,471
  • 1
  • 19
  • 26
  • possible duplicate of [Struct memory hack to overlap object reference - Is it possible?](http://stackoverflow.com/questions/17771902/struct-memory-hack-to-overlap-object-reference-is-it-possible) – Bjørn-Roger Kringsjå May 27 '14 at 19:55

1 Answers1

0

I've flagged this as a duplicate but I'll share a "workaround".

<StructLayout(LayoutKind.Explicit)>
Public Structure TimeStamp32

    <FieldOffset(0)> Public Value As Integer
    <FieldOffset(0)> Public Byte1 As Byte
    <FieldOffset(1)> Public Byte2 As Byte
    <FieldOffset(2)> Public Byte3 As Byte
    <FieldOffset(3)> Public Byte4 As Byte

    Public Sub New(value As Integer)
        Me.Value = value
    End Sub

    Public Sub New(byte1 As Byte, byte2 As Byte, byte3 As Byte, byte4 As Byte)
        Me.Byte1 = byte1
        Me.Byte2 = byte2
        Me.Byte3 = byte3
        Me.Byte4 = byte4
    End Sub

    Public ReadOnly Property Bytes() As Byte()
        Get
            Return New Byte() {Me.Byte1, Me.Byte2, Me.Byte3, Me.Byte4}
        End Get
    End Property

End Structure
Bjørn-Roger Kringsjå
  • 9,849
  • 6
  • 36
  • 64