Results 1 to 15 of 15

Thread: [ADVANCED] - Passing structures via WM_COPYDATA [SOLVED]

  1. #1

    Thread Starter
    Lively Member Axonn's Avatar
    Join Date
    May 2005
    Posts
    64

    Resolved [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:
    1. Public Type SomeStructure
    2.       xx As Long
    3.       yy As Long
    4. End Type

    Of course, I use COPYDATASTRUCT like this:

    VB Code:
    1. Public Type CopyDataStruct
    2.       dwData As Long
    3.       cbData As Long
    4.       lpData As Long
    5. End Type

    And the variables are:
    VB Code:
    1. Public cpy As CopyDataStruct
    2. Public some As SomeStructure

    And the call is:

    VB Code:
    1. some.xx = 4
    2.    some.yy = 6
    3.    
    4.    cpy.cbData = 8
    5.    cpy.dwData = 101
    6.    cpy.lpData = VarPtr(nu)
    7.    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?
    Last edited by Axonn; Nov 12th, 2005 at 06:29 PM.
    . - = E C H Y S T T A S = - .
    The Greater Mind Balance

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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.
    Last edited by Hack; Nov 11th, 2005 at 02:48 PM.

  3. #3
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    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?

  4. #4

    Thread Starter
    Lively Member Axonn's Avatar
    Join Date
    May 2005
    Posts
    64

    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*.
    . - = E C H Y S T T A S = - .
    The Greater Mind Balance

  5. #5

    Thread Starter
    Lively Member Axonn's Avatar
    Join Date
    May 2005
    Posts
    64

    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.
    . - = E C H Y S T T A S = - .
    The Greater Mind Balance

  6. #6
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

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

  7. #7
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: [ADVANCED] - Passing structures via WM_COPYDATA

    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.

  8. #8
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    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:
    1. Public Type SomeStructure2
    2.       xx As Long
    3.       yy As Long
    4.       zz As Long
    5. End Type
    6.  
    7. Public Type SomeStructure
    8.       xx As Long
    9.       yy As Long
    10. 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]

  9. #9

    Thread Starter
    Lively Member Axonn's Avatar
    Join Date
    May 2005
    Posts
    64

    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*.
    . - = E C H Y S T T A S = - .
    The Greater Mind Balance

  10. #10
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    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?

  11. #11

    Thread Starter
    Lively Member Axonn's Avatar
    Join Date
    May 2005
    Posts
    64

    Angry 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:
    1. 'Structures
    2. Public Type SomeStructure
    3.       xx As Long
    4.       yy As Long
    5. End Type
    6.  
    7. Public Type CopyDataStruct
    8.       dwData As Long
    9.       cbData As Long
    10.       lpData As Long
    11. End Type
    12.  
    13. 'API Declares:
    14. Public Const WM_COPYDATA = &H4A
    15. 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
    16.  
    17. 'Required variables.
    18. Public cpy As CopyDataStruct
    19. Public some As SomeStructure

    In the form:

    VB Code:
    1. some.xx = 4
    2. some.yy = 6
    3.  
    4. cpy.cbData = LenB(some)
    5. cpy.dwData = 101
    6. cpy.lpData = VarPtr(some)
    7.  
    8. SendMessage &H30032, WM_COPYDATA, Me.hwnd, cpy
    9. 'Where &H30032 is the handle of a valid window.
    . - = E C H Y S T T A S = - .
    The Greater Mind Balance

  12. #12
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: [ADVANCED] - Passing structures via WM_COPYDATA

    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:
    1. Public Function NewWndProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    2. Dim cbData As Long
    3. Dim dwData As Long
    4. Dim lpData As Long
    5. Dim some As SomeStructure
    6.  
    7.     If uMsg = WM_COPYDATA Then
    8.         'get dwData
    9.         CopyMemory dwData, ByVal lParam, 4
    10.         'get len of structure
    11.         CopyMemory cbData, ByVal (lParam + 4), 4
    12.         'get pointer to structure
    13.         CopyMemory lpData, ByVal (lParam + 8), 4
    14.         'get structure itself
    15.         CopyMemory some, ByVal lpData, LenB(some)
    16.         Form1.Label1 = CStr(some.xx) & "  " & CStr(some.yy)
    17.     End If
    18.             NewWndProc = CallWindowProc(lOrigWndProc, hwnd, uMsg, wParam, lParam)
    19. End Function
    In Sender
    VB Code:
    1. Private Sub cmdSend_Click()
    2. Dim hWndREC As Long
    3. Dim MyData As CopyDataStruct
    4. Dim some As SomeStructure
    5.  
    6.  
    7.  'get handle of receiver
    8.  hWndREC = FindWindow(vbNullString, "Receiver")
    9.  While hWndREC = 0
    10.     If MsgBox("Start Receiver Program", vbOKCancel) = vbCancel Then Exit Sub
    11.     hWndREC = FindWindow(vbNullString, "Receiver")
    12.  Wend
    13.  
    14.  
    15. some.xx = 4
    16. some.yy = 6
    17.  
    18. MyData.cbData = LenB(some)
    19. MyData.dwData = 101
    20. MyData.lpData = VarPtr(some)
    21.  
    22. SendMessage hWndREC, WM_COPYDATA, Me.hwnd, MyData
    23.  
    24. End Sub
    Again I ask, what line in your code is giving you the Overflow error???

  13. #13
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    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

  14. #14
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: [ADVANCED] - Passing structures via WM_COPYDATA

    I'll bet that's it.

  15. #15

    Thread Starter
    Lively Member Axonn's Avatar
    Join Date
    May 2005
    Posts
    64

    Talking Hooray!

    Success. Indeed it was that idiotical Integer. That's what I get from pasting from the now-proven stupid API Guide! This is how the function was declared in an example and I didn't bother to change... grrrrr silly me ::- D. Thanks guys. I successfully get the message back in VC++. I didn't yet try to get the data from it but I wouldn't expect ugly surprises and simply hope that it works out ok.
    . - = E C H Y S T T A S = - .
    The Greater Mind Balance

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