 |
| VB How-To Ask your "How do I do this with VB?" questions in this forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the VB How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

February 5th, 2004, 12:00 PM
|
|
Registered User
|
|
Join Date: Feb 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
sending a collection of UDTs
Hello,
I need help with sending a collection of my UDTs using the winsock control.
i created a UDT with a few string types, integers and booleans
i have a function that returns a collection of my UDT.
i tried a few ways to send this collection with the winsock but the problem isnt the sending but recieving it is where my vb crash everytime!!!
it usually happens during the "CopyMemory" api function which tries to convert the variant which holds the data recieved by the "GetData" Method.
please help me, i have been battling this problem for weeks now and it is essential for my program to work that i succeed in this.
thank you.
|
|

February 5th, 2004, 12:08 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
hi there..
maybe if you put some code we can help more??
Gonzalo Bianchi
|
|

February 5th, 2004, 10:12 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Why are you using a Variant? Variant and CopyMemory are not friends...
You can read the data in an array of bytes, that are easier to manage:
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
dim myUDT(something)
dim bVal() as Byte
redim bVal(myDataLength-1)
.GetData bval, vbArray + vbByte, myDataLength
CopyMemory myUDT(0), bval(0), myDataLength
Just a thought,
Marco
|
|

February 5th, 2004, 10:19 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Sorry, I should have been more clear...
Dim myUDT(numberOfElements-1) as MyUdtType
the bVal array should be dimensioned as
dim myDataLength as Long
myDataLength = numberOfElements * len(myUdt(0))
redim bVal(myDataLength - 1)
Just be careful with GetData, it is possible that not all data is available at that time and you have to do more calls.
Quote:
quote:Originally posted by marcostraf
Why are you using a Variant? Variant and CopyMemory are not friends...
You can read the data in an array of bytes, that are easier to manage:
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
dim myUDT(something)
dim bVal() as Byte
redim bVal(myDataLength-1)
.GetData bval, vbArray + vbByte, myDataLength
CopyMemory myUDT(0), bval(0), myDataLength
Just a thought,
Marco
|
|
|

February 8th, 2004, 08:30 AM
|
|
Registered User
|
|
Join Date: Feb 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
marcostraf,
first of all thanks for the tips.
but i have just one problem with your proposal: you used "NumberOfElements" but the number is not known and changes all the time. since my udt represents users that are logged on to my system and since this number can vary from 1 to n, theres a problem with getting the number of elements, or in my case users.
please help.
|
|

February 9th, 2004, 03:17 AM
|
|
Registered User
|
|
Join Date: Feb 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
i have another problem when i pass a udt with the winsock: when i send an integer it goes through the right way but when i try sending a string it comes out in the recieving end like this - "????" or just " ", or something like that.
does anyone know what could be the problem and what can i do with it?
|
|

February 9th, 2004, 03:45 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
In your original post you said that "I have a functions that returns a collection of my UDT". Now, this is strange because UDTs cannot be added in a collection. I just presumed that you know how many users you have...
Can you give more info? What do you know in your code, from where you get the information and to where you have to send it?
Marco
Quote:
quote:Originally posted by unholly_plugin
marcostraf,
first of all thanks for the tips.
but i have just one problem with your proposal: you used "NumberOfElements" but the number is not known and changes all the time. since my udt represents users that are logged on to my system and since this number can vary from 1 to n, theres a problem with getting the number of elements, or in my case users.
please help.
|
|
|

February 9th, 2004, 03:50 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
The VB String is in reality the COM BSTR object. Before sending it to a socket you have to change it to something more of a a low level (like an array of bytes or shorts, depending if you need wide characters)
Now my very bad question: why are you using a socket communication?
Marco
Quote:
quote:Originally posted by unholly_plugin
i have another problem when i pass a udt with the winsock: when i send an integer it goes through the right way but when i try sending a string it comes out in the recieving end like this - "????" or just " ", or something like that.
does anyone know what could be the problem and what can i do with it?
|
|
|
 |