From: Saga on

Hello all, I have the code listed below. This *was* working. The binary
file has this structure:

2 bytes marker bytes
2 bytes flags
4 bytes 32 bit time stamp
2 bytes
x bytes null terminated text string
x bytes The Data

The text string is optional, so it may be 0 bytes. The fourth header
byte tells me this. I tested it thoroughly, but now that I have revisted
this process the fileget() is reading all zeroes for the marker bytes and
flags. I opened the file in DEBUG and verified that the expected data
was there. Any suggestions or ideas on alternate methods are welcomed.
Thank you, Saga


Dim TheBytes() As Byte
Dim strName As String = ""
Dim bytHeader(3) As Byte
Dim bytByte As Byte
Dim lngTimeStamp As Integer
Dim bytExtra(1) As Byte
Dim intFH As Integer


intFH = FreeFile()

FileOpen(intFH, strDataFile, OpenMode.Binary)

FileGet(intFH, (bytHeader))

'Check for valid marker bytes
If bytHeader(0) = 225 And bytHeader(1) = 179 Then

FileGet(intFH, lngTimeStamp)

'2 more bytes.
FileGet(intFH, (bytExtra))

If (bytHeader(3) And 16) = 16 Then

'Get the name.
Do
FileGet(intFH, bytByte)

If bytByte > 0 Then
strName = strName & Chr(bytByte)
End If
Loop Until bytByte = 0
End If


ReDim TheBytes(CInt(LOF(intFH) - 1))

FileGet(intFH, (TheBytes))


From: Armin Zingler on
Am 11.05.2010 21:56, schrieb Saga:
> Hello all, I have the code listed below. This *was* working.

....until you did _what_? Enable Option Strict and put the array names
in () braces?

> The binary
> file has this structure:
>
> 2 bytes marker bytes
> 2 bytes flags
> 4 bytes 32 bit time stamp
> 2 bytes
> x bytes null terminated text string
> x bytes The Data
>
> The text string is optional, so it may be 0 bytes. The fourth header
> byte tells me this. I tested it thoroughly, but now that I have revisted
> this process the fileget() is reading all zeroes for the marker bytes and
> flags. I opened the file in DEBUG and verified that the expected data
> was there. Any suggestions or ideas on alternate methods are welcomed.
> Thank you, Saga

Did you upgrade a VB6 project? Or why do you use the old VB functions?
Use the System.IO namespace instead.


> Dim TheBytes() As Byte
> Dim strName As String = ""
> Dim bytHeader(3) As Byte
> Dim bytByte As Byte
> Dim lngTimeStamp As Integer
> Dim bytExtra(1) As Byte
> Dim intFH As Integer
>
>
> intFH = FreeFile()
>
> FileOpen(intFH, strDataFile, OpenMode.Binary)
>
> FileGet(intFH, (bytHeader))

With the "()" arround the 2nd arg, it is an expression.
The value of the expressions is stored in an internal, local variable
in order to be able to pass the reference to the variable to the
FileGet method. This is because the 2nd arg is passed ByRef.

FileGet returns a new array, and the reference is stored in that
invisible local variable. After that it's lost.

So the solution while keeping Option Strict On is:

Dim tmpArray As Array = bytHeader
FileGet(intFH, tmpArray)
bytHeader = DirectCast(tmpArray, Byte())


Still I wouldn't use the FileXY methods anymore.

--
Armin
From: Saga on
Your reply is much appreciated. To answer some of your questions:

Yes - I ported this algorithm from a VB6 project. After experimenting
with various options I found that using fileX() family of functions was
the fastest way to convert the VB6 code.

Yes - I turned on Option Strict. All successful testing was done
before I did that. Suspecting that this might have caused the change in
behavior I turned option strict OFF, but this did not solve the problem.

Yes - after the code failed I am looking for an alternative to using the
fileX() family of functions. I am aware that using the current method
is notthe best way to do this. Lack of time and ease won out this time,
but I am looking for a different way to do this.

I have made the change that you suggested and the routine is now
once again functional. Thank you for you help. Your suggestions
have not fallen on deaf ear and I will look into a way to do this not
using the fileX() family. I am currently looking into using System.IO.
FileStream. Regards, Saga

"Armin Zingler" <az.nospam(a)freenet.de> wrote in message
news:%23sHu7JW8KHA.1560(a)TK2MSFTNGP02.phx.gbl...
> Am 11.05.2010 21:56, schrieb Saga:
>> Hello all, I have the code listed below. This *was* working.
>
> ...until you did _what_? Enable Option Strict and put the array names
> in () braces?
>
>> The binary
>> file has this structure:
>>
>> 2 bytes marker bytes
>> 2 bytes flags
>> 4 bytes 32 bit time stamp
>> 2 bytes
>> x bytes null terminated text string
>> x bytes The Data
>>
>> The text string is optional, so it may be 0 bytes. The fourth header
>> byte tells me this. I tested it thoroughly, but now that I have revisted
>> this process the fileget() is reading all zeroes for the marker bytes and
>> flags. I opened the file in DEBUG and verified that the expected data
>> was there. Any suggestions or ideas on alternate methods are welcomed.
>> Thank you, Saga
>
> Did you upgrade a VB6 project? Or why do you use the old VB functions?
> Use the System.IO namespace instead.
>
>
>> Dim TheBytes() As Byte
>> Dim strName As String = ""
>> Dim bytHeader(3) As Byte
>> Dim bytByte As Byte
>> Dim lngTimeStamp As Integer
>> Dim bytExtra(1) As Byte
>> Dim intFH As Integer
>>
>>
>> intFH = FreeFile()
>>
>> FileOpen(intFH, strDataFile, OpenMode.Binary)
>>
>> FileGet(intFH, (bytHeader))
>
> With the "()" arround the 2nd arg, it is an expression.
> The value of the expressions is stored in an internal, local variable
> in order to be able to pass the reference to the variable to the
> FileGet method. This is because the 2nd arg is passed ByRef.
>
> FileGet returns a new array, and the reference is stored in that
> invisible local variable. After that it's lost.
>
> So the solution while keeping Option Strict On is:
>
> Dim tmpArray As Array = bytHeader
> FileGet(intFH, tmpArray)
> bytHeader = DirectCast(tmpArray, Byte())
>
>
> Still I wouldn't use the FileXY methods anymore.
>
> --
> Armin


From: Chris Dunaway on
On May 12, 10:46 am, "Saga" <antiS...(a)nowhere.com> wrote:
> Your reply is much appreciated. To answer some of your questions:
>
> Yes - I ported this algorithm from a VB6 project. After experimenting
> with various options I found that using fileX() family of functions was
> the fastest way to convert the VB6 code.
>
> Yes - I turned on Option Strict. All successful testing was done
> before I did that. Suspecting that this might have caused the change in
> behavior I turned option strict OFF, but this did not solve the problem.
>
> Yes - after the code failed I am looking for an alternative to using the
> fileX() family of functions. I am aware that using the current method
> is notthe best way to do this. Lack of time and ease won out this time,
> but I am looking for a different way to do this.
>
> I have made the change that you suggested and the routine is now
> once again functional. Thank you for you help. Your suggestions
> have not fallen on deaf ear and I will look into a way to do this not
> using the fileX() family. I am currently looking into using System.IO.
> FileStream. Regards, Saga
>
> "Armin Zingler" <az.nos...(a)freenet.de> wrote in message
>
> news:%23sHu7JW8KHA.1560(a)TK2MSFTNGP02.phx.gbl...
>
> > Am 11.05.2010 21:56, schrieb Saga:
> >> Hello all, I have the code listed below. This *was* working.
>
> > ...until you did _what_?  Enable Option Strict and put the array names
> > in () braces?
>
> >> The binary
> >> file has this structure:
>
> >> 2 bytes marker bytes
> >> 2 bytes flags
> >> 4 bytes 32 bit time stamp
> >> 2 bytes
> >> x bytes null terminated text string
> >> x bytes The Data
>
> >> The text string is optional, so it may be 0 bytes. The fourth header
> >> byte tells me this. I tested it thoroughly, but now that I have revisted
> >> this process the fileget() is reading all zeroes for the marker bytes and
> >> flags. I opened the file in DEBUG and verified that the expected data
> >> was there. Any suggestions or ideas on alternate methods are welcomed.
> >> Thank you, Saga
>
> > Did you upgrade a VB6 project? Or why do you use the old VB functions?
> > Use the System.IO namespace instead.
>
> >>     Dim TheBytes() As Byte
> >>     Dim strName As String = ""
> >>     Dim bytHeader(3) As Byte
> >>     Dim bytByte As Byte
> >>     Dim lngTimeStamp As Integer
> >>     Dim bytExtra(1) As Byte
> >>     Dim intFH As Integer
>
> >>     intFH = FreeFile()
>
> >>     FileOpen(intFH, strDataFile, OpenMode.Binary)
>
> >>     FileGet(intFH, (bytHeader))
>
> > With the "()" arround the 2nd arg, it is an expression.
> > The value of the expressions is stored in an internal, local variable
> > in order to be able to pass the reference to the variable to the
> > FileGet method. This is because the 2nd arg is passed ByRef.
>
> > FileGet returns a new array, and the reference is stored in that
> > invisible local variable. After that it's lost.
>
> > So the solution while keeping Option Strict On is:
>
> >      Dim tmpArray As Array = bytHeader
> >      FileGet(intFH, tmpArray)
> >      bytHeader = DirectCast(tmpArray, Byte())
>
> > Still I wouldn't use the FileXY methods anymore.
>
> > --
> > Armin

Look at the BinaryReader class. It has methods such as ReadByte,
ReadInt32, ReadString, etc. that should work for your purposes and
it's not any more difficult than FileGet.

http://msdn.microsoft.com/en-us/library/system.io.binaryreader_methods%28v=VS.100%29.aspx

Chris
From: Armin Zingler on
As Chris says. And here's the code: Untested of course! ;)

In the line "TheBytes = " I'm assuming that you want to read
only the rest of the file. In your code the array was as long
as the whole file.


imports system.io

'...

Using fs As New FileStream("a:\devicemissing", FileMode.Open, FileAccess.Read, FileShare.Read)
Dim br As New BinaryReader(fs)

Dim header As Byte()
Dim name As String
Dim TheBytes As Byte()

header = br.ReadBytes(4)

If header(0) = 225 AndAlso header(1) = 179 Then
Dim Timestamp = br.ReadInt32

br.ReadInt16()

If (header(3) And 16) = 16 Then
'Get the name.
Dim Codes As New List(Of Byte)

Do
Dim code = br.ReadByte
If code = 0 Then Exit Do
Codes.Add(code)
Loop

name = System.Text.Encoding.Default.GetString(Codes.ToArray)
End If

TheBytes = br.ReadBytes(CInt(fs.Length - fs.Position))
End If

br.Close()
End Using



--
Armin