Jump to content
  • Advertisement

Archived

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

hellz

Static callback WndProc question.

This topic is 5333 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

Hey all. I have a question regarding the use of a static WndProc function. Basically, why does it have to be static to compile? I thought I understood, but the question has been plaguing my mind for a few days now, so I did a search and came up with the following: MSDN - Using Callback Functions GameDev threads: OOP window design Qs. WindowProc in a class........ Are #define good to use? <-- Good read, incidentally. Really constructive thread. Anyway, from one of the threads is the following quote:
quote:
Original post by groby Class member functions cannot simply be used as callbacks. The reason for this is that member functions expect an implicit parameter to the object they are operating on - the 'this' pointer. What I usually do is have a static member function in the class. Either just make the WndProc static, or, if you want to have multiple instances, make it a forwarder.
So how come the this pointer wouldn't be passed implicitly as an argument to a class member callback function and how does making the function static, solve this? Thanks for your time. -hellz [edited by - hellz on December 15, 2003 1:08:04 PM]

Share this post


Link to post
Share on other sites
Advertisement
The "this" pointer won''t be passed because windows doesn''t know to pass the "this" pointer. On the machine level, it treats callback functions as expecting arguments to be passed to it in a specific way, and return values returned a specific way. That set of behaviors is called a "calling convention". nonstatic member functions use a different calling convention from all other functions, so it isn''t possible to use them as callbacks in windows; it would be like hiring a secretary who doesn''t speak english.


"Sneftel is correct, if rather vulgar." --Flarelocke

Share this post


Link to post
Share on other sites
Thanks for the reply. That certainly clears up why the this pointer isn''t passed, but why exactly does making the function, static, solve that? I didn''t quite follow that part. Is that down to a different calling convention as well?

Thanks again.

-hellz

Share this post


Link to post
Share on other sites
Exactly. static member functions use the same calling convention as global functions, the calling convention that Windows is expecting.


"Sneftel is correct, if rather vulgar." --Flarelocke

Share this post


Link to post
Share on other sites
If you''re curious, that''s what "cdecl" means when it occasionally occurs, usually in linker errors. It''s a calling convention.


"Sneftel is correct, if rather vulgar." --Flarelocke

Share this post


Link to post
Share on other sites
Heh, funny you should say that. That was my next question. Does that mean, __stdcall (think that''s it), is also a calling convention? In fact, same with any words you see after the return type of a function signature, which come before the function''s name and parameter list?

-hellz

Share this post


Link to post
Share on other sites
Yup, __stdcall is another calling convention. Read more about them here.


"Sneftel is correct, if rather vulgar." --Flarelocke

Share this post


Link to post
Share on other sites
Since the first question has been answered....

I was told in alt.comp.lang.learn.c-c++ that __stdcall is Microsoft specific.

If that is so was is the ANSI C or C++ way do doing __stdcall??

Thanks.
(If anyone is listening)

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!