-
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
-
That probably means you have these three somewhere in your project:
Type YourUDT
Dim something as YourUDT
something = YourFunction
Function YourFunction() as YourUDT
-
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.
-
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!
-
Thanks Joacim. This does exactly what I need.
Regards
Adrian
-
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.