Binary File Read

M

Mike Iacovou

Hi all,

I'm trying to read in a binary file... I found a VB6 script that 'should'
work, but get an error using it in Access 2003. I get a 458 error 'Variable
uses an automation type not supported in visual basic' at the 'Get #iFileNum,
, vThisBlock' line... The file path is correct...
I am happy to abandon this procedure for any that will read in a file
binary... the file is infact a DLL that is required by some db functions...
it will be extracted and saved to hard drive if it is not found - thus
ensuring the system will run... thus a binary load / save is required...
any guidance appreciated...

Function FileReadBinary(sFileName As String) As Variant
Dim iFileNum As Integer, lFileLen As Long
Dim vThisBlock As Variant, lThisBlock As Long, vFileData As Variant

'On Error GoTo ErrFailed

If Len(Dir$(sFileName)) > 0 And Len(sFileName) > 0 Then
iFileNum = FreeFile
Open sFileName For Binary Access Read As #iFileNum

lFileLen = LOF(iFileNum)

Do
lThisBlock = lThisBlock + 1
Get #iFileNum, , vThisBlock
If IsEmpty(vThisBlock) = False Then
If lThisBlock = 1 Then
ReDim vFileData(1 To 1)
Else
ReDim Preserve vFileData(1 To lThisBlock)
End If
vFileData(lThisBlock) = vThisBlock
End If
Loop While EOF(iFileNum) = False
Close iFileNum

FileReadBinary = vFileData
End If

Exit Function

ErrFailed:
Close iFileNum
MsgBox Err.Description
End Function
 
S

Stephen Lebans

Here's code to read a file from disk.

Private Function fReadFile(fname As String) As String


If Len(fname & vbNullString) = 0 Then
MsgBox "You Must Select a File!", vbCritical
fReadFile = ""
Exit Function
End If


Dim InFileData() As Byte
Dim FileNumIn As Long


FileNumIn = FreeFile


Open fname For Binary Access Read As FileNumIn


Dim FileLengthExtra As Long
' Get length of file.
FileLength = LOF(FileNumIn)


ReDim InFileData(1 To (FileLength )) as Byte
' fills array with contents of original file
Get FileNumIn, , InFileData
Close FileNumIn


'Me!NameOfYourField = InFileData
fReadFile = fname


' End of code snippet



--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.
 
M

Mike Iacovou

many thanks stephen

I tried this, and it seems to create a byte array of the appropriate size on
ubound testing... but the returned string size is approx. half this size.
Writing the bytearray from your routine using 'Put' creates an identical
file... the issue is in the return conversion from the routine:

'Me!NameOfYourField = InFileData
fReadFile = InFileData 'changed from fname

The returned string is approx. half the length of the original binary... I
imagine this is specific to reading application binaries ie EXE / DLLs... and
possibly the binary size / max string size (I tested with a 44Kb DLL and
returned string of 22Kb, so occurs even within max. string size)

If I can convert the byte array to a string, and then convert from that
string to a byte array, the system will work to store this required file...
Any further tips appreciated.
 
M

Mike Iacovou

OK. Converted the bytearray to base64 string within file read function, and
works fine.
thanks again
 
S

Stephen Lebans

You are working with an array of Byte values. Where does string conversion
come into this?

--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top