CallBack - class function - DirectPlay
When setting up directPlay you have to specify the call back function like:
g_pDPClient->Initialize(NULL, DirectPlayMessageHandler, 0 )
Where DirectPlayMessageHandler is a function usually defined like:
HRESULT WINAPI DirectPlayMessageHandler(PVOID pvUserContext, DWORD dwMessageId, PVOID pMsgBuffer)
But, what if you wanted DirectPlayMessageHandler to be a class function defined in a class called Multiplayer? I tried doing this and got an error message as follows:
error C2664: ''Initialize'' : cannot convert parameter 2 from ''long (void *,unsigned long,void *)'' to ''long (__stdcall *const )(void *,unsigned long,void *)''
Any ideas on what I''m doing wrong? I tried making DirectPlayMessageHandler a static method. That got rid of this particular error but introduced others due to the fact that it is now static. Am I on the right track? What should I do?
If the class is only going to be used once, you could define a function outside of the class that only calls the method for the class, like:
HRESULT WINAPI MsgHandler(...){
return yourClass->MsgProc(...);
}
The D3D app wizard uses a similar setup. For this to work tho, "yourClass" must be declared as a global variable...
I hope this helps.
---Dan
HRESULT WINAPI MsgHandler(...){
return yourClass->MsgProc(...);
}
The D3D app wizard uses a similar setup. For this to work tho, "yourClass" must be declared as a global variable...
I hope this helps.
---Dan
Thanks, that will work for what I''m doing.
For future reference, does anyone know if this can actually be done without having outside functions?
For future reference, does anyone know if this can actually be done without having outside functions?
pointers to member functions cannot be transformed into ''ordinary'' pointers in Standard C++. Some (BC++, g++...) compilers let you do it as an extension to the language.
If the callback mechanism allows for a user parameter (usually void*), you can try to pass the this pointer in there.
Otherwise, within this framework, I am afraid there is no easy general solution.
Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
If the callback mechanism allows for a user parameter (usually void*), you can try to pass the this pointer in there.
Otherwise, within this framework, I am afraid there is no easy general solution.
Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
Yes, and no... You can build a class that contains a callback, but you have to declare it as static, meaning that every instance of the class will use it. This is fine, however, as you can specify a pointer to the calling class instance when it is called.
The reason you can''t do it as you were trying is because C++ adds a pointer to the calling class instance as the first parameter. For instance, if you type:
cMyClass->Function1(10);
The compiler will convert it into:
Function1(cMyClass, 10);
Which is why you get the convert errors. Using static gets rid of the class instance pointer, so what you type is what you get, but as a side effect, you are loosing the class instance pointer.
Jim Adams
home.att.net/~rpgbook
Author, Programming Role-Playing Games with DirectX
The reason you can''t do it as you were trying is because C++ adds a pointer to the calling class instance as the first parameter. For instance, if you type:
cMyClass->Function1(10);
The compiler will convert it into:
Function1(cMyClass, 10);
Which is why you get the convert errors. Using static gets rid of the class instance pointer, so what you type is what you get, but as a side effect, you are loosing the class instance pointer.
Jim Adams
home.att.net/~rpgbook
Author, Programming Role-Playing Games with DirectX
Ergo, know your C++.
The function doesn't have to be external. You can package both the object on which the callback is being executed as well as necessary execution data using std:: pair. Take a look at the Graphics.cpp file in this archive for examples (D3D).
[Edit: Damn smileys.]
[edited by - Oluseyi on May 30, 2002 2:50:17 PM]
The function doesn't have to be external. You can package both the object on which the callback is being executed as well as necessary execution data using std:: pair. Take a look at the Graphics.cpp file in this archive for examples (D3D).
[Edit: Damn smileys.]
[edited by - Oluseyi on May 30, 2002 2:50:17 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement