Archived

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

Static callback WndProc question.

This topic is 5113 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
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
A calling convention can''t be Microsoft-specific. A compiler can support whatever calling conventions it wants.


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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Its me too lazy to type name and pass.

---------------------------------------------
A calling convention can''t be Microsoft-specific. A compiler can support whatever calling conventions it wants.
---------------------------------------------

Yeah they seem to be an over sensitive bunch when it comes to anything that even smells of OS, API, or implmentation specific.

Share this post


Link to post
Share on other sites