BeeJ
4/26/2012 1:42:00 AM
So why does one work and the other not?
Confused because if I send the first Type (sString As String) to an
Open Binary File and read it back it works fine. I just do a Put
#lFnbr , tHdrA1 both in the IDE and as .EXE
What are the gotchas?
Googling only found .Net stuff and even then not really related to
this.
Anyway, bottom line I need to convert various Type structures to a Byte
Arrays. How to?
Note: in one case the IDE crashes after several runs and in the other
case the .EXE crashes (these code parts were put into seperate button
clicks to call multiple times as desired).
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory"
(Destination As Any, Source As Any, ByVal Length As Long)
Private Type tHeaderAType
lLong As Long
ayByte(0 To 1) As Byte
sString As String
dDouble As Double
End Type
Private Type tHeaderBType
lLong As Long
ayByte(0 To 1) As Byte
ayString() As Byte
dDouble As Double
End Type
'
Private Sub Form_Load()
'
' Part A
Debug.Print "PartA Does Not Work In THe IDE But Seems To Work As
..EXE"
Dim tHdrA1 As tHeaderAType
Dim tHdrA2 As tHeaderAType
Dim tHdrB1 As tHeaderBType
Dim tHdrB2 As tHeaderBType
Dim ayByte() As Byte
tHdrA1.lLong = &H100
tHdrA1.ayByte(0) = Asc("A")
tHdrA1.ayByte(1) = Asc("Z")
tHdrA1.sString = "ABC123"
tHdrA1.dDouble = 1.00001
ReDim ayByte(0 To LenB(tHdrA1) - 1)
' Convert to byte array
CopyMemory ayByte(0), tHdrA1, LenB(tHdrA1)
' now try to get it back
CopyMemory tHdrA2, ayByte(0), UBound(ayByte) + 1
Debug.Print tHdrA2.lLong
Debug.Print Chr$(tHdrA2.ayByte(0))
Debug.Print Chr$(tHdrA2.ayByte(1))
Debug.Print tHdrA2.sString
Debug.Print tHdrA2.dDouble
' =========================================
' Part B
Debug.Print "PartB Seems To Work In IDE Once But Not as .EXE"
tHdrB1.lLong = &H100
tHdrB1.ayByte(0) = Asc("A")
tHdrB1.ayByte(1) = Asc("Z")
tHdrB1.ayString = "ABC123"
tHdrB1.dDouble = 1.00001
ReDim ayByte(0 To LenB(tHdrB1) - 1)
CopyMemory ayByte(0), tHdrB1, LenB(tHdrB1)
' now try to get it back
CopyMemory tHdrB2, ayByte(0), UBound(ayByte) + 1
Debug.Print tHdrB2.lLong
Debug.Print Chr$(tHdrB2.ayByte(0))
Debug.Print Chr$(tHdrB2.ayByte(1))
Debug.Print tHdrB2.ayString
Debug.Print tHdrB2.dDouble
End Sub '
===================
Debug.Print Results
===================
PartA Does Not Work
256
A
Z
Form1/Form1
1.00001
PartB Seems To Work
256
A
Z
ABC123
1.00001
--
So where are we?
Not the street address.
Not the city.
Not the country.
Not the Earth.
Not the Solar System.
Not the Galaxy.
Not the Universe.
Not the Brane.
So where is the Brane?
Where are we?
Life is but a dream!