Dim ms As New CMemStruct, errMsg As String, b() as byte
If Not ms.AddFields("byte1*b,int1*i,lng1*l,cur1*c,blob1*15", errMsg) Then
Text1 = errMsg
Exit Sub
End If
ms.LoadFromFile , App.Path & "\test.bin"
b() = ms.toBytes()
addText ms.dump(True)
If Not ms.field("blob1").SetValue("new blob2!", errMsg) Then
addText "Error setting new blob1 in test2.bin: " & errMsg
End If
ms.field("cur1").SetValue 3.14
ms.SaveToFile , App.Path & "\test3.bin"
Interface:
Code:
'Variables
Public filePath As String
Public name As String 'in case instances get held in a collection or want to be named
'Properties
Property size() As Long
Property fieldCount() As Long
'Methods
Function AddFields(ByVal formatString As String, Optional errMsg, Optional reset As Boolean = True, Optional ByVal name As String) As Boolean
Sub AddField(ByVal name As String, ft As ms_fieldType, Optional size As Long = 0)
Function dump(Optional asHex As Boolean = True) As String
Function fromBytes(buf() As Byte) As Boolean
Function toBytes() As Byte()
Function fromHexString(str, Optional errMsg) As Boolean
Function toHexString() As String
Function SaveToFile(Optional ByVal offset As Long, Optional ByVal filePath As String)
Function LoadFromFile(Optional ByVal offset As Long, Optional ByVal filePath As String)
Function offsetOf(fieldIndexOrName) As Long
Sub CloseFile()
Sub reset()
Function CurOffset() As Long
Function field(fieldIndexOrName) As CStructField
Another similar class that may be of interest is CMemBuf that lets you do read and writes to a memory buffer in a very similar way tha tyou can use get/put to write different types to disk. buffer grows dynamically:
Dim mb As New CMemBuf
Dim i As Integer
Dim l As Long
Dim ii(4) As Integer
dim ss() as string
i = &H1122
l = &H66554433
ii(0) = &H1122
ii(1) = &H3344
ii(2) = &H5566
ii(3) = &H7788
ii(4) = &H99AA
ss = Split("this is my string", " ")
mb.write_ i
mb.write_ l
mb.write_ ii
strStart = mb.curOffset
mb.writeStr "testing!"
mb.writeStr ss, , mb_bstr
debug.print mb.HexDump
mb.read l, 2
debug.print Hex(l)
debug.print mb.readStr(strStart)
debug.print mb.readStr(, mb_bstr)
Interface:
Code:
'Variables
Public lastErr As String
'Properties
Property curOffset() As Long
Property Buffer() As Byte()
'Methods
Function AryIsEmpty(ary) As Boolean
Sub push(ary, Value) 'this modifies parent ary object
Function rc4(ByVal password As Variant, Optional overWriteBuf As Boolean = True) As Byte()
Function WriteLong(ByVal X As Long, Optional offset As Long = -1) As Boolean
Function ReadByte(ByRef X As Byte, Optional offset As Long = -1) As Boolean
Function ReadLong(ByRef X As Long, Optional offset As Long = -1) As Boolean
Function read(ByRef data As Variant, Optional offset As Long = -1) As Boolean
Function write_(data As Variant, Optional offset As Long = -1) As Boolean
Function fromFile(ByVal fPath As String, Optional fileOffset As Long = 1, Optional ByVal dataSize As Long = -1) As Boolean
Function toFile(ByVal fPath As String, Optional fileOffset As Long = 1) As Boolean
Function saveToMem(memAddress As Long, Optional size As Long, Optional offset As Long = -1) As Boolean
Function writeFromMem(memAddress As Long, size As Long, Optional offset As Long = -1) As Boolean
Function writeStr(data As Variant, Optional offset As Long = -1, Optional writeType As mb_StrTypes = mb_cstr) As Boolean
Function readStr(Optional offset As Long = -1, Optional readType As mb_StrTypes = mb_cstr, Optional fixedWidth As Long = 0) As String
Function HexDump(Optional ByVal Length As Long = -1, Optional ByVal startAt As Long = 1, Optional hexFormat As hexOutFormats = hoDump) As String
Function sizeInBounds(size As Long, Optional offset As Long = -1) As Boolean
Function ensureSize(ByVal size As Long, Optional offset As Long = -1) As Boolean
Sub clear()
What's about IRecordInfo? The advantages are the assignment that dynamic structure to a variant variable. Then you can pass it into the methods of the interfaces, proper marshaling etc.
What's about IRecordInfo? The advantages are the assignment that dynamic structure to a variant variable. Then you can pass it into the methods of the interfaces, proper marshaling etc.
Hi The trick, I want to use VB6 to simulate the dynamic structure classes of JavaScript. Could you give some suggestions? Thanks!