Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with writing byte array to a file

Tags:

excel

vba

Under windows, when Evernote is installed, an api is also installed, which can be accessed through vba (for example).

Each notes can show its "Resources" (attached files and images), and the actual Resources can be retrieved as Byte Arrays.

I have trouble writing the byte arrays to actual file.

Declaration of variables :

Dim fileByte() As Byte
Dim nt As enapiLib.Note

Fetch data :

fileByte = nt.Resources.Item(i).Data

Write byte array to a file :

Function WriteByteArray(vData As Variant, sFileName As String, Optional bAppendToFile As Boolean = False) As Boolean
Dim iFileNum As Integer, lWritePos As Long

Debug.Print " --> Entering WriteByteArray function with " & sFileName & " file to write."
On Error GoTo ErrFailed
If bAppendToFile = False Then
    If Len(Dir$(sFileName)) > 0 And Len(sFileName) > 0 Then
        'Delete the existing file
        VBA.Kill sFileName
    End If
End If

iFileNum = FreeFile
Debug.Print "iFileNum = " & iFileNum
'Open sFileName For Binary Access Write As #iFileNum
Open sFileName For Binary Lock Read Write As #iFileNum

If bAppendToFile = False Then
    'Write to first byte
    lWritePos = 1
Else
    'Write to last byte + 1
    lWritePos = LOF(iFileNum) + 1
End If

Put #iFileNum, lWritePos, vData
Close #iFileNum

WriteByteArray = True
Exit Function

ErrFailed:
Debug.Print "################################"
Debug.Print "Error handling of WriteByteArray"
Debug.Print "################################"
FileWriteBinary = False
Close iFileNum
Debug.Print Err.Description & "(" & Err.Number & ")"
End Function

I tried with an exe file

By debug.printing each byte value, I know that my byte array starts with 4D 5A as every other exe file

Resource (1) : 
ClickToSetup.0.9.8.1416.exe
application/x-msdownload
Le fichier C:\Dropbox\TestEvernote\ClickToSetup.0.9.8.1416.exe doit être créé.
Lbound(fileByte) = 0
Ubound(fileByte) = 5551919
i = 0
filebyte(i) = 4D
i = 1
filebyte(i) = 5A

By reading back the exe file created to a byte array, I know that the newly created array starts with byte 4D 5A as wished

But the exe file present on the hard drive is _corrupted_, and _does not start_ with the correct bytes_ :

Here are the first binary values of the stored file on the harddrive : (got from VBinDiff tool ) (I cannot post image, I am a newbie here... ) : VBinDiff output of exe

Why is there these 12 bytes in front of the actual data ??

like image 868
user1683620 Avatar asked Sep 19 '12 16:09

user1683620


2 Answers

I had the same problem - some 12 byte header thrown at the top of every file written. It turns out that the PUT command doesn't quite know how to handle data of type Variant. I'm not sure of the exact cause, but my work around was to simply replace the PUT line:

    Put #iFileNum, lWritePos, vData

with this:

    Dim buffer() As Byte
    buffer = vData
    Put #iFileNum, lWritePos, buffer

Problem solved.

like image 161
CarlSteffen Avatar answered Nov 14 '22 14:11

CarlSteffen


In VB6, 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.

You need to "copy" the Variant content to Byte array, for example:

ReDim arrByte(0 To UBound(varBuffer) - LBound(varBuffer))

For i = 0 To UBound(varBuffer) - LBound(varBuffer)
  arrByte = varBuffer(i + LBound(varBuffer))
Next i
like image 32
Cristian C. Bittel Avatar answered Nov 14 '22 12:11

Cristian C. Bittel