Convert user defined type or variant to byte array (and back) 
Author Message
 Convert user defined type or variant to byte array (and back)

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



Sat, 22 Dec 2001 03:00:00 GMT  
 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



Sat, 22 Dec 2001 03:00:00 GMT  
 Convert user defined type or variant to byte array (and back)
Awesome; Thanks!

The only other thing that I think I will ever need to send is a RecordSet,
but from a quick test, it looks like I can send that directly.

Thanks again, with this block passed, I will finally be able to start
writting the graphical/rules part of my RTS game again (which I liked doing
a whole lot more).


Quote:
> 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........

> > 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



Sat, 22 Dec 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. how to convert user-defined TYPE to variant?

2. VARIANT array to user defined type mapping

3. Casting from a Byte Array to a User Defined type

4. Q: variant data type with user defined data type

5. Variant to Byte Array, and back

6. Variant to Byte Array, and back

7. Converting a Variant To an array of bytes!

8. Convert variant to array of byte

9. Convert variant to array of byte

10. Convert byte array to variant

11. Convert variant to array of byte

12. Convert Strings to Byte arrays and back

 

 
Powered by phpBB® Forum Software