{"id":103,"date":"2008-11-20T13:00:06","date_gmt":"2008-11-20T20:00:06","guid":{"rendered":"http:\/\/cknotes.com\/?p=103"},"modified":"2008-11-20T13:01:56","modified_gmt":"2008-11-20T20:01:56","slug":"vb6-variant-vs-byte-array","status":"publish","type":"post","link":"https:\/\/cknotes.com\/vb6-variant-vs-byte-array\/","title":{"rendered":"VB6 Variant vs Byte Array"},"content":{"rendered":"<p>In Visual Basic 6.0, a Variant containing a byte array is different than a byte array.<br \/>\nFor example, examine this code:<\/p>\n<pre>\r\n    ' Assume <strong>mime<\/strong> is a ChilkatMime object...\r\n    Set mimePart = mime.GetPart(1)\r\n    \r\n    thefile = FreeFile()\r\n    'Get the attachment filename\r\n    FileName = mimePart.FileName\r\n    'Get the attachment\r\n    Dim mBody As Variant\r\n    mBody = mimePart.GetBodyBinary\r\n    If Len(mBody) > 0 Then\r\n        Open FileName For Binary As #thefile\r\n            Put #thefile, , mBody\r\n        Close #thefile\r\n    End If\r\n<\/pre>\n<p> The result is not what you expected.  There&#8217;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.<br \/>\nThis is what you really wanted:<\/p>\n<pre>\r\n    ' Assume <strong>mime<\/strong> is a ChilkatMime object...\r\n    Set mimePart = mime.GetPart(1)\r\n    \r\n    thefile = FreeFile()\r\n    'Get the attachment filename\r\n    FileName = mimePart.FileName\r\n    'Get the attachment\r\n    Dim mBody() As Byte\r\n    mBody = mimePart.GetBodyBinary\r\n    If UBound(mBody) > 0 Then\r\n        Open FileName For Binary As #thefile\r\n            Put #thefile, , mBody\r\n        Close #thefile\r\n    End If\r\n<\/pre>\n<p>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.<\/p>\n<p>Note: There is an implicit conversion happening in this statement:<\/p>\n<pre>\r\nmBody = mimePart.GetBodyBinary\r\n<\/pre>\n<p>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&#8217;s removing the Variant wrapping so-to-speak&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Visual Basic 6.0, a Variant containing a byte array is different than a byte array. For example, examine this code: &#8216; Assume mime is a ChilkatMime object&#8230; Set mimePart = mime.GetPart(1) thefile = FreeFile() &#8216;Get the attachment filename FileName = mimePart.FileName &#8216;Get the attachment Dim mBody As Variant mBody = mimePart.GetBodyBinary If Len(mBody) > [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[76,151,474,109],"class_list":["post-103","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-byte-array","tag-variant","tag-vb6","tag-visual-basic"],"_links":{"self":[{"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/posts\/103","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/comments?post=103"}],"version-history":[{"count":0,"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/posts\/103\/revisions"}],"wp:attachment":[{"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/media?parent=103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/categories?post=103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/tags?post=103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}