|
-
Dec 19th, 2002, 02:35 PM
#1
Thread Starter
Addicted Member
CALLBACK functions in Classes
How do I have a CALLBACK function in a Class, so that it can access the member variables? I'm trying to write a DirectInput class, but the 'Enum' Callback functions will require access to the classes' variables.
If you need more info, please ask.
Last edited by Barguast; Dec 19th, 2002 at 02:42 PM.
Using Visual Studio .NET 2005
-
Dec 19th, 2002, 03:48 PM
#2
Member
The callback would have to have some way of looking up the current object and then call a method on that (I'm pretty sure this is how MFC does it or something similar).
Like if there is a User defined parameter for the callback you could set that to the address of the current object.
Other than that there really isn't anything you can do.
Class methods (except static methods) use a calling convention all by itself (that's how the this pointer gets passed in). The CALLBACK macro is defined as __stdcall, which is another calling convention. And last time I checked one function cannot have two calling conventions.
I'm not familiar with what a DirectInput class is, so I can't help you out... until I have a little more information.
-
Dec 21st, 2002, 09:09 AM
#3
Actually class methods use an extended calling convention: thiscall can extend stdcall, fastcall and cdecl. But no extended version can be used instead of a non-extended version.
Every callback in windows allows you to pass a user-defined parameter, usually of type LPARAM or LPVOID (void *). You can simply pass the this pointer there.
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.
-
Dec 28th, 2002, 10:30 AM
#4
Thread Starter
Addicted Member
I'm gonna need a little more help with this I think.
This is the line which calls the Callback function:
m_pDirectInput->EnumDevices(DI8DEVCLASS_GAMECTRL, EnumJoysticksCallback, this, DIEDFL_ATTACHEDONLY)))
The second parameter is the Callback function in question:
int CALLBACK EnumJoysticksCallback(const DIDEVICEINSTANCE* pdidInstance, VOID* pContext) {
....
}
[This passes a this pointer into the Callback with the second parameter]
How do I then use pContext as a 'this' pointer?
Last edited by Barguast; Dec 28th, 2002 at 10:57 AM.
Using Visual Studio .NET 2005
-
Dec 29th, 2002, 05:09 AM
#5
Code:
// class declaration:
static int CALLBACK EnumJoysticksCallbackStub(const DIDEVICEINSTANCE* pdidInstance, VOID* pContext);
int EnumJoysticksCallback(const DIDEVICEINSTANCE* pdidInstance);
// Implementation file
int CALLBACK CYourClass::EnumJoysticksCallbackStub(const DIDEVICEINSTANCE* pdidInstance, VOID* pContext)
{
return (reinterpret_cast<CYourClass*>(pContext))->
EnumJoysticksCallback(pdidInstance);
}
int CYourClass::EnumJoysticksCallback(const DIDEVICEINSTANCE* pdidInstance)
{
....
}
Pass the stub as the callback function.
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.
-
Dec 29th, 2002, 08:37 AM
#6
Thread Starter
Addicted Member
It's working! Yey!
It was the "reinterpret_cast<CDirectInput*>(pContext) " I missed out...
Using Visual Studio .NET 2005
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
|