Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Crunchy

CallBack - class function - DirectPlay

This topic is 5988 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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 ]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!