Results 1 to 6 of 6

Thread: Sending a variable of UDT via TCP

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2000
    Location
    Australia
    Posts
    82

    Post

    I am trying to send a variable of UDT via a TCP connection. However, when I try and run the project I get the following message.

    "Compile Error

    Only user-defined types defined in public object modules can be coerced to or from a variant or passed to late-bound functions."

    This is all very well, but what does this actually mean?

    Appreciate any advice.

    Tks
    Adrian

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    That probably means you have these three somewhere in your project:

    Type YourUDT

    Dim something as YourUDT
    something = YourFunction


    Function YourFunction() as YourUDT
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2000
    Location
    Australia
    Posts
    82
    Thanks Kedaman, but still unclear.

    What I have in my code is this.
    ----- Module Code ------
    Type typeHrt
    First As Byte
    Second As Byte
    Third As Byte
    Fourth As Byte
    Fifth As Byte
    End Type
    Public HeartBeat As typeHrt

    ------Form Code-------
    Public Sub cmdSend_Click()

    If tcpClient.State = sckConnected Then
    tcpClient.SendData HeartBeat 'Problem

    end sub
    The HeartBeat Type is loaded with byte values prior to the cmdSend call.

    If the UDT has to be in a "Public Object Model" then how do I accomplish this?

    Tks
    Adrian.

  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Well, when you declare an variable as an UDT and trying to send it (I assume you're using the WinSock control) with the SendData method, the WinSock control tries to convert the UDT into a Variant.
    This convertion is not allowed if the UDT isn't declared in a public class module (i.e a class module in an ActiveX DLL or ActiveX EXE file).

    What you should do is to copy the UDT into a byte array using the CopyMemory API function and then send the byte array.
    On the other side you should do the reverse. That is copy the byte array back to the UDT.

    Here's an quick example:
    Code:
    Private Declare Sub CopyMemory _
     Lib "kernel32" Alias "RtlMoveMemory" ( _
     Destination As Any, _
     Source As Any, _
     ByVal Length As Long)
    
    Private Type MyUDT
        FName As String * 15
        LName As String * 15
        Age As Integer
    End Type
    
    Private Sub cmdSend_Click()
        Dim udt As MyUDT
        Dim bArr() As Byte
        
        With udt
            .FName = "John"
            .LName = "Doe"
            .Age = 32
        End With
        ReDim bArr(1 To Len(udt)) As Byte
        CopyMemory bArr(1), udt, Len(udt)
        'code to send the byte array with the
        'WinSock control goes here
    End Sub
    Good luck!

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2000
    Location
    Australia
    Posts
    82
    Thanks Joacim. This does exactly what I need.

    Regards
    Adrian

  6. #6
    New Member
    Join Date
    Dec 2000
    Location
    Cincinnati, OH
    Posts
    4

    Thumbs up Thank you!

    Thank You, Thank You, Thank You, Thank You, Thank You!

    This advice got me through a big hang up. In fact it was precisely what I was trying to do.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width