VB6 Variant vs Byte Array

In Visual Basic 6.0, a Variant containing a byte array is different than a byte array.
For example, examine this code:

    ' Assume mime is a ChilkatMime object...
    Set mimePart = mime.GetPart(1)
    
    thefile = FreeFile()
    'Get the attachment filename
    FileName = mimePart.FileName
    'Get the attachment
    Dim mBody As Variant
    mBody = mimePart.GetBodyBinary
    If Len(mBody) > 0 Then
        Open FileName For Binary As #thefile
            Put #thefile, , mBody
        Close #thefile
    End If

The result is not what you expected. There’s a mysterious extra 12 bytes prepended to the content that is saved. Why? Because you saved the Variant structure as well as the contents of the Variant.
This is what you really wanted:

    ' Assume mime is a ChilkatMime object...
    Set mimePart = mime.GetPart(1)
    
    thefile = FreeFile()
    'Get the attachment filename
    FileName = mimePart.FileName
    'Get the attachment
    Dim mBody() As Byte
    mBody = mimePart.GetBodyBinary
    If UBound(mBody) > 0 Then
        Open FileName For Binary As #thefile
            Put #thefile, , mBody
        Close #thefile
    End If

In the above code fragment, mBody is a byte array. There is no Variant structure encapsulating the data. The content of the MIME body is saved exactly as you expected.

Note: There is an implicit conversion happening in this statement:

mBody = mimePart.GetBodyBinary

GetBodyBinary is a function that returns a Variant. However, mBody is declared as an array of Byte. Therefore, the VB6 runtime is converting the Variant to a byte array. In other words, it’s removing the Variant wrapping so-to-speak…