
Convert user defined type or variant to byte array (and back)
Howard,
If the UDT does not contain dynamic strings or dynamic arrays you can use the
Windows API CopyMemory function to copy it to a byte array, or a byte array back
to a UDT. Sample code follows.
John........
-------------------------------
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(Destination As Any, _
Source As Any, _
ByVal Length As Long)
Private Type MyType
a As String * 12
x As Long
s As Single
End Type
Private Sub Form_Load()
Dim i As Integer
Dim MyUDT1 As MyType
Dim MyUDT2 As MyType
Dim ab() As Byte
MyUDT1.a = "abcdefghijkl"
MyUDT1.x = 123456
MyUDT1.s = 345.678
ReDim ab(Len(MyUDT1))
' Copy UDT to byte array
Call CopyMemory(ab(0), MyUDT1, Len(MyUDT1))
For i = 0 To UBound(ab)
Debug.Print ab(i)
Next
' Copy byte array to UDT
Call CopyMemory(MyUDT2, ab(0), UBound(ab) + 1)
Debug.Print MyUDT2.a
Debug.Print MyUDT2.x
Debug.Print MyUDT2.s
End Sub
---------------------------------
Quote:
> Hello,
> I'm still trying to beam my user defined type across the Winsock control.
> Someone suggest that I try to beam a variant across instead of messing with
> the UDT. Unfortunately, it gave the same error as trying to beam the UDT
> directly: Unsupported variant types.
> So anyway, here is my question:
> How do I convert my UDT or variant into a byte array? I know I can transmit
> byte arrays with the Winsock control. Also, an associated question, how do
> I convert a byte array into a variant or UDT?
> Thanks in advance.
> --
> Howard Henry 'Gawyn Ballpeen' Schlunder
> Gawyn Developments; Core developer