VB6 array TYPE bug - Can anyone explain this please ???

  • 9 years ago

    Run following code to see the problem as .BAS module.

    'Word' should be same as an Integer - i.e. 2 bytes, but it actually takes 3 bytes in the boot_fat16_bad Type structure!

    To fix it, uncomment the res type in both structures.

    Using the IDE Watch feature, it is easy to see an extra byte has been declared at the end of the 8 byte array.

    Why is this happening???

     

     


    Option Explicit


    Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

    Public Type Word
      lsb As Byte
      msb As Byte
    End Type

    'note: using Word type here as using integers seems to sometimes us 3 bytes instead of 2!!!

    Private Type boot_fat16
        jmp(2) As Byte          'Must be 0xEB, 0x3C, 0x90
        sys_id(7) As Byte       '3 "MSDOS5.0"
    '    res As Byte            'this fixes the bug
        Sect_Size As Word       '0bh *Sector size in bytes (512)
    End Type


    Private Type boot_fat16_bad
        jmp(2) As Byte          'Must be 0xEB, 0x3C, 0x90
        sys_id(7) As Byte       '3 "MSDOS5.0"
    '    res As Byte            'this fixed the bug
        Sect_Size As Integer    '0bh *Sector size in bytes (512)
    End Type


    Sub Main()
    Dim b As boot_fat16         'good structure
    Dim bb As boot_fat16_bad    'bad structure

    b.Sect_Size.lsb = &H3
    b.Sect_Size.msb = &H2
    bb.Sect_Size = &H203
    Dim SectorBuff1() As Byte
    Dim SectorBuff2() As Byte
    ReDim SectorBuff1(15)
    ReDim SectorBuff2(15)
    Call CopyMemory(SectorBuff1(0), b.jmp(0), Len(b))     'dest, source, no bytes
    Call CopyMemory(SectorBuff2(0), bb.jmp(0), Len(bb))   'dest, source, no bytes
    MsgBox SectorBuff2(12) & " should be same as " & SectorBuff1(12)


    End Sub


     

     

Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Better train people and risk they leave – than do nothing and risk they stay.” - Anonymous