Results 1 to 9 of 9

Thread: Callback with wParam of Any type (vb and c++)

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662

    Callback with wParam of Any type (vb and c++)

    I am trying to make a callback in vc++ and vb (vc++ calls a function in vb) and one of the parameters passed to the function that calls the callback is passed to the callback, it's value ignored.

    this is the same functionality of the lParam in EnumFontFamilies.

    The declare (in vb) for the function defines wParam of my function as type Any. This allows me to pass numbers, objects, or strings to the callback function.

    Unfortunatly, my code doesn't work correctly for this. It works for numbers variables (i.e. integer and long) and it works for variants, but it doesn't work with strings (the debug window shows "?????" for the value of wParam)

    Here is the c++ code:
    Code:
    #include <windows.h>
    
    typedef int (__stdcall *STATUSCALLBACK)(int uCalled, int uReturn, WPARAM wParam);
    
    int __stdcall test(STATUSCALLBACK f, WPARAM wParam)
    {
    	return f(123, 123, wParam);
    }
    here is the vb code:
    VB Code:
    1. Option Explicit
    2. Declare Function test Lib "H:\Microsoft Visual Studio\MyProjects\callback\Debug\callback.dll" (ByVal cbkCallbackProc As Long, ByRef wParam As Any) As Long
    3.  
    4. Public Function CB(ByVal uCalled As Long, ByVal uReturn As Long, wParam As [b]Variant[/b]) As Long
    5.    
    6.     Debug.Print "uCalled" & vbTab & uCalled
    7.     Debug.Print "uReturn" & vbTab & uReturn
    8.     Debug.Print "wParam" & vbTab & wParam
    9.    
    10.     CB = 54321
    11. End Function
    12.  
    13. Sub main()
    14.     Dim a As [b]Variant[/b]
    15.    
    16.     a = "hello world"
    17.    
    18.     Debug.Print "Return" & vbTab & test(AddressOf CB, a)
    19. End Sub

    i need to be able to put any data type, object type, or user-defined type in place of the variant type i used here

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Try this C code:
    Code:
    typedef int (__stdcall *STATUSCALLBACK)(int uCalled, int uReturn, VARIANTARG * pArg);
    
    int __stdcall test(STATUSCALLBACK f, VARIANTARG * pArg)
    {
    	return f(123, 123, pArg);
    }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662
    the code i originally posted works as you would think. i should have included the code which i need to work. change everything that says variant to string as shown and the bebug window shows a bunch of "?".

    i posted the working vb code by mistake.
    VB Code:
    1. Option Explicit
    2. Declare Function test Lib "H:\Microsoft Visual Studio\MyProjects\callback\Debug\callback.dll" (ByVal cbkCallbackProc As Long, ByRef wParam As Any) As Long
    3.  
    4. Public Function CB(ByVal uCalled As Long, ByVal uReturn As Long, wParam As [b]String[/b]) As Long
    5.    
    6.     Debug.Print "uCalled" & vbTab & uCalled
    7.     Debug.Print "uReturn" & vbTab & uReturn
    8.     Debug.Print "wParam" & vbTab & wParam
    9.    
    10.     CB = 54321
    11. End Function
    12.  
    13. Sub main()
    14.     Dim a As [b]String[/b]
    15.    
    16.     a = "hello world"
    17.    
    18.     Debug.Print "Return" & vbTab & test(AddressOf CB, a)
    19. End Sub

    the c++ code posted in the previous thread did not seem to change the behaivior.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Ahhh...
    Try BSTR instead of VARIANTARG *
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    Unless working with COM, all VB Strings should be translated to char* (LPSTR) in the DLL.
    Try:
    Code:
    #include <windows.h>
    
    typedef int (__stdcall *STATUSCALLBACK)(int uCalled, int uReturn, LPSTR wParam);
    
    int __stdcall test(STATUSCALLBACK f, LPSTR wParam)
    {
    	// wParam is LPSTR - you can modify (read/write) it, but
    	// DON'T attempt to deallocate/resize it
    	return f(123, 123, wParam);
    }
    VB Code:
    1. Option Explicit
    2. Declare Function test Lib "H:\Microsoft Visual Studio\MyProjects\callback\Debug\callback.dll" (ByVal cbkCallbackProc As Long, ByVal wParam As String) As Long
    3.  
    4. Public Function CB(ByVal uCalled As Long, ByVal uReturn As Long, ByVal wParam As String) As Long
    5.    
    6.     Debug.Print "uCalled" & vbTab & uCalled
    7.     Debug.Print "uReturn" & vbTab & uReturn
    8.     Debug.Print "wParam" & vbTab & wParam
    9.    
    10.     CB = 54321
    11. End Function
    12.  
    13. Sub main()
    14.     Dim a As String
    15.    
    16.     a = "hello world"
    17.    
    18.     Debug.Print "Return" & vbTab & test(AddressOf CB, a)
    19. End Sub
    Not 100% sure it'll work (because of the callback), but try it.
    Btw, all Strings have to be ByVal since then VB passes a pointer to an ANSI string, instead of a pointer to a pointer to a BSTR.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662
    i think you guys are missing the point. i need to be able to modify the vb code so that i can pass any data type, not just string.

    with EnumFontFamilies, i can pass a form object (byref, of course) if the EnumFontFamiliesProc's lParam is defined as Form.

    the dll ignores the contents of lParam and passes it directly to EnumFontFamiliesProc.

    I think that the dll doesn't even know what type of data is stored.

    This is why I defined wParam as type Any in the Declare statement.

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Didn't you say you want to pass strings seperatly or am I missing something here?

    the code i originally posted works as you would think. i should have included the code which i need to work. change everything that says variant to string as shown and the bebug window shows a bunch of "?".

    i posted the working vb code by mistake.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    Practically same thing
    Code:
    #include <windows.h>
    
    typedef int (__stdcall *STATUSCALLBACK)(int uCalled, int uReturn, LPVOID wParam);
    
    int __stdcall test(STATUSCALLBACK f, LPVOID wParam)
    {
    	return f(123, 123, wParam);
    }
    VB Code:
    1. Option Explicit
    2. Declare Function test Lib "H:\Microsoft Visual Studio\MyProjects\callback\Debug\callback.dll" (ByVal cbkCallbackProc As Long, ByVal wParam As Any) As Long
    3.  
    4. Public Function CB(ByVal uCalled As Long, ByVal uReturn As Long, ByVal wParam As String) As Long
    5.    
    6.     Debug.Print "uCalled" & vbTab & uCalled
    7.     Debug.Print "uReturn" & vbTab & uReturn
    8.     Debug.Print "wParam" & vbTab & wParam
    9.    
    10.     CB = 54321
    11. End Function
    12.  
    13. Sub main()
    14.     Dim a As String
    15.    
    16.     a = "hello world"
    17.    
    18.     Debug.Print "Return" & vbTab & test(AddressOf CB, a)
    19. End Sub
    Should work.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662
    Got it working, thanks.

    VB converts strings to ASCII (from Unicode) when pasing them to an API call. Unfortunatly, it remains ASCII when it's passed to my callback. Solution? Uuse StrConv at some point to convert it back to Unicode.

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