|
-
Jan 5th, 2003, 06:31 PM
#1
Thread Starter
Fanatic Member
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:
Option Explicit
Declare Function test Lib "H:\Microsoft Visual Studio\MyProjects\callback\Debug\callback.dll" (ByVal cbkCallbackProc As Long, ByRef wParam As Any) As Long
Public Function CB(ByVal uCalled As Long, ByVal uReturn As Long, wParam As [b]Variant[/b]) As Long
Debug.Print "uCalled" & vbTab & uCalled
Debug.Print "uReturn" & vbTab & uReturn
Debug.Print "wParam" & vbTab & wParam
CB = 54321
End Function
Sub main()
Dim a As [b]Variant[/b]
a = "hello world"
Debug.Print "Return" & vbTab & test(AddressOf CB, a)
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
-
Jan 5th, 2003, 07:33 PM
#2
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.
-
Jan 5th, 2003, 08:03 PM
#3
Thread Starter
Fanatic Member
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:
Option Explicit
Declare Function test Lib "H:\Microsoft Visual Studio\MyProjects\callback\Debug\callback.dll" (ByVal cbkCallbackProc As Long, ByRef wParam As Any) As Long
Public Function CB(ByVal uCalled As Long, ByVal uReturn As Long, wParam As [b]String[/b]) As Long
Debug.Print "uCalled" & vbTab & uCalled
Debug.Print "uReturn" & vbTab & uReturn
Debug.Print "wParam" & vbTab & wParam
CB = 54321
End Function
Sub main()
Dim a As [b]String[/b]
a = "hello world"
Debug.Print "Return" & vbTab & test(AddressOf CB, a)
End Sub
the c++ code posted in the previous thread did not seem to change the behaivior.
-
Jan 5th, 2003, 08:17 PM
#4
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.
-
Jan 6th, 2003, 02:00 AM
#5
Guru
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:
Option Explicit
Declare Function test Lib "H:\Microsoft Visual Studio\MyProjects\callback\Debug\callback.dll" (ByVal cbkCallbackProc As Long, ByVal wParam As String) As Long
Public Function CB(ByVal uCalled As Long, ByVal uReturn As Long, ByVal wParam As String) As Long
Debug.Print "uCalled" & vbTab & uCalled
Debug.Print "uReturn" & vbTab & uReturn
Debug.Print "wParam" & vbTab & wParam
CB = 54321
End Function
Sub main()
Dim a As String
a = "hello world"
Debug.Print "Return" & vbTab & test(AddressOf CB, a)
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.
-
Jan 7th, 2003, 01:24 AM
#6
Thread Starter
Fanatic Member
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.
-
Jan 7th, 2003, 06:52 AM
#7
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.
-
Jan 7th, 2003, 08:36 AM
#8
Guru
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:
Option Explicit
Declare Function test Lib "H:\Microsoft Visual Studio\MyProjects\callback\Debug\callback.dll" (ByVal cbkCallbackProc As Long, ByVal wParam As Any) As Long
Public Function CB(ByVal uCalled As Long, ByVal uReturn As Long, ByVal wParam As String) As Long
Debug.Print "uCalled" & vbTab & uCalled
Debug.Print "uReturn" & vbTab & uReturn
Debug.Print "wParam" & vbTab & wParam
CB = 54321
End Function
Sub main()
Dim a As String
a = "hello world"
Debug.Print "Return" & vbTab & test(AddressOf CB, a)
End Sub
Should work.
-
Jan 8th, 2003, 02:29 PM
#9
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|