[ADVANCED] - Passing structures via WM_COPYDATA [SOLVED]
Hi everybody ::- D. Does anybody know how to get the size of a structure in Visual Basic? I need this to pass a structure to VC++ via WM_COPYDATA ::- ). The structure is this:
VB Code:
Public Type SomeStructure
xx As Long
yy As Long
End Type
Of course, I use COPYDATASTRUCT like this:
VB Code:
Public Type CopyDataStruct
dwData As Long
cbData As Long
lpData As Long
End Type
And the variables are:
VB Code:
Public cpy As CopyDataStruct
Public some As SomeStructure
And the call is:
VB Code:
some.xx = 4
some.yy = 6
cpy.cbData = 8
cpy.dwData = 101
cpy.lpData = VarPtr(nu)
SendMessage 131198, WM_COPYDATA, Me.hwnd, cpy
And of course, the error is: OVERFLOW.
It's probably 'cause of that cbData = 8. I am not sure if the size of the structure is 8 bytes. It's two longs so it should be, right? But isn't there some command to find that out?
Re: [ADVANCED] - Passing structures via WM_COPYDATA
If it is 8 bytes then try using a Double which is an 8 byte floating point instead of a Long.
Re: [ADVANCED] - Passing structures via WM_COPYDATA
LenB(UDT Variable) returns the number of bytes required by the UDT.
What is the variable nu in your sample code?
Re: [ADVANCED] - Passing structures via WM_COPYDATA
And keep the size of 8? Or put 16? Hm. Let's see....
Tried both. Both give the same "overflow" error. So I should understand that there's no way of measuring the size of a structure in VB? No already existing function? I use VarPtr to send a pointer of the structure via WM_COPYDATA but I also need that size *sigh*.
Re: [ADVANCED] - Passing structures via WM_COPYDATA
The good news is that indeed LenB is good ::- D. Thanks brucevde. The bad news is that I still get that Overflow error. Crap.
Re: [ADVANCED] - Passing structures via WM_COPYDATA
Quote:
Originally Posted by Axonn
And keep the size of 8? Or put 16? Hm. Let's see....
Tried both. Both give the same "overflow" error. So I should understand that there's no way of measuring the size of a structure in VB? No already existing function? I use VarPtr to send a pointer of the structure via WM_COPYDATA but I also need that size *sigh*.
In theory, the LenB function should give the size of a structure. But, I tried it out on the structure you posted above and it gave me 8 too :ehh: . One thing you might want to look into is seeing how the variable is being stored in memory. There's a module over on CodeGuru that can create memory maps from VB variables, and this might be helpful. Interestingly, it almost looks like there are 4 extra null bytes tacked onto the end of the struct.
http://www.codeguru.com/vb/gen/vb_mi...Printing%20Mem
Re: [ADVANCED] - Passing structures via WM_COPYDATA
Quote:
The bad news is that I still get that Overflow error. Crap.
Well for one thing you cannot pass pointers across process boundaries.
Again, what is the variable nu in your sample code.
Re: [ADVANCED] - Passing structures via WM_COPYDATA
Yep, think that's the case. Here are 2 memory dumps with 2 different structs. I read out LenB(uType) + 8 in each case and set all the bits in the variables.
VB Code:
Public Type SomeStructure2
xx As Long
yy As Long
zz As Long
End Type
Public Type SomeStructure
xx As Long
yy As Long
End Type
And the Output:
Code:
Memory Dump For uTest:
----------------------
0 (12F718) >> ÿ FFh (255) [11111111]
1 (12F719) >> ÿ FFh (255) [11111111]
2 (12F71A) >> ÿ FFh (255) [11111111]
3 (12F71B) >> ÿ FFh (255) [11111111]
4 (12F71C) >> ÿ FFh (255) [11111111]
5 (12F71D) >> ÿ FFh (255) [11111111]
6 (12F71E) >> ÿ FFh (255) [11111111]
7 (12F71F) >> ÿ FFh (255) [11111111]
8 (12F720) >> . 0h (0) [00000000]
9 (12F721) >> . 0h (0) [00000000]
10 (12F722) >> . 0h (0) [00000000]
11 (12F723) >> . 0h (0) [00000000]
12 (12F724) >> ´ B4h (180) [10110100]
13 (12F725) >> ÷ F7h (247) [11110111]
14 (12F726) >> . 12h (18) [00010010]
15 (12F727) >> . 0h (0) [00000000]
Memory Dump For uTest2:
-----------------------
0 (12F714) >> ÿ FFh (255) [11111111]
1 (12F715) >> ÿ FFh (255) [11111111]
2 (12F716) >> ÿ FFh (255) [11111111]
3 (12F717) >> ÿ FFh (255) [11111111]
4 (12F718) >> ÿ FFh (255) [11111111]
5 (12F719) >> ÿ FFh (255) [11111111]
6 (12F71A) >> ÿ FFh (255) [11111111]
7 (12F71B) >> ÿ FFh (255) [11111111]
8 (12F71C) >> ÿ FFh (255) [11111111]
9 (12F71D) >> ÿ FFh (255) [11111111]
10 (12F71E) >> ÿ FFh (255) [11111111]
11 (12F71F) >> ÿ FFh (255) [11111111]
12 (12F720) >> . 0h (0) [00000000]
13 (12F721) >> . 0h (0) [00000000]
14 (12F722) >> . 0h (0) [00000000]
15 (12F723) >> . 0h (0) [00000000]
16 (12F724) >> ´ B4h (180) [10110100]
17 (12F725) >> ÷ F7h (247) [11110111]
18 (12F726) >> . 12h (18) [00010010]
19 (12F727) >> . 0h (0) [00000000]
Re: [ADVANCED] - Passing structures via WM_COPYDATA
brucevde: sorry, the "nu" variable is a copy paste error. It's originally "some". So that's ok. It's the pointer to the structure to copy. I just made an error in the post.
As for process boundaries, brucevde, check this out:
http://www.vbforums.com/showpost.php...2&postcount=20
My particular situation is this: the VB Application loads a DLL. The DLL hooks Explorer.exe. I send the message to a window of Explorer.exe and I catch it in the hook back in VC++. Tricky huh? ::- ).
Comintern: I tried putting the size of 12 but still the same problem *sigh*.
Re: [ADVANCED] - Passing structures via WM_COPYDATA
The size of your some UDT is 8-bytes so LenB is giving the correct size.
The code looks OK to me, where are you getting the overflow error?
Certainly not in the code you presented... perhaps in your hook code?
Re: [ADVANCED] - Passing structures via WM_COPYDATA
Negative Moeur. The hook is all the way in VC++. In VB there is no hook, only a subclass which doesn't have anything to do with sending the message anyway. Can YOU make it work between two VB Applications for example? Maybe it ain't workin' 'cause I'm sending the message to the TaskBar window ('cause that's where I'm sending them) - although this doesn't have any sense. I think there's something wrong in the VB side. This time I'll paste more details:
In a module:
VB Code:
'Structures
Public Type SomeStructure
xx As Long
yy As Long
End Type
Public Type CopyDataStruct
dwData As Long
cbData As Long
lpData As Long
End Type
'API Declares:
Public Const WM_COPYDATA = &H4A
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByRef lParam As CopyDataStruct) As Long
'Required variables.
Public cpy As CopyDataStruct
Public some As SomeStructure
In the form:
VB Code:
some.xx = 4
some.yy = 6
cpy.cbData = LenB(some)
cpy.dwData = 101
cpy.lpData = VarPtr(some)
SendMessage &H30032, WM_COPYDATA, Me.hwnd, cpy
'Where &H30032 is the handle of a valid window.
Re: [ADVANCED] - Passing structures via WM_COPYDATA
Quote:
Can YOU make it work between two VB Applications for example?
Yes.
Just Alter the code that you posted a link to above.
In Receiver
VB Code:
Public Function NewWndProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim cbData As Long
Dim dwData As Long
Dim lpData As Long
Dim some As SomeStructure
If uMsg = WM_COPYDATA Then
'get dwData
CopyMemory dwData, ByVal lParam, 4
'get len of structure
CopyMemory cbData, ByVal (lParam + 4), 4
'get pointer to structure
CopyMemory lpData, ByVal (lParam + 8), 4
'get structure itself
CopyMemory some, ByVal lpData, LenB(some)
Form1.Label1 = CStr(some.xx) & " " & CStr(some.yy)
End If
NewWndProc = CallWindowProc(lOrigWndProc, hwnd, uMsg, wParam, lParam)
End Function
In Sender
VB Code:
Private Sub cmdSend_Click()
Dim hWndREC As Long
Dim MyData As CopyDataStruct
Dim some As SomeStructure
'get handle of receiver
hWndREC = FindWindow(vbNullString, "Receiver")
While hWndREC = 0
If MsgBox("Start Receiver Program", vbOKCancel) = vbCancel Then Exit Sub
hWndREC = FindWindow(vbNullString, "Receiver")
Wend
some.xx = 4
some.yy = 6
MyData.cbData = LenB(some)
MyData.dwData = 101
MyData.lpData = VarPtr(some)
SendMessage hWndREC, WM_COPYDATA, Me.hwnd, MyData
End Sub
Again I ask, what line in your code is giving you the Overflow error???
Re: [ADVANCED] - Passing structures via WM_COPYDATA
Check the SendMessage declaration. You seem to have ByVal wParam As Integer, but it should be As Long :)
Re: [ADVANCED] - Passing structures via WM_COPYDATA