Jump to content
  • Advertisement
Sign in to follow this  
lukesmith123

Non static winproc function

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

I'm trying to create a winproc function like this:


LRESULT CALLBACK Engine::WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if(bStarted)
return MsgProc(hWnd, msg, wParam, lParam);
else
return DefWindowProc (hWnd, msg, wParam, lParam);
}


So that the function checks if a bStarted boolean in the Engine class is true before returning the MsgProc function.

The problem is that when I make WindowProc a function in the Engine class I get an error that the argument list is missing when I try to create the window. But if I make the WindowProc class static I dont have access to the bStarted var or the MsgProc function.

I looked in a couple of books but the books all make the main class a global singleton to make things simpler so I'm pretty stumped on how to do this.

Share this post


Link to post
Share on other sites
Advertisement
You have to make it static or a free function. You can however pass the "this" pointer to the window when calling CreateWindow() (the lpParam)
Then when processing WM_CREATE set the pointer via SetWindowLongPtr().
When processing subsequent messages, you can get the pointer via GetWindowLongPtr().

Share this post


Link to post
Share on other sites
Basically, you can't have a non-static WndProc by design. To get around this, its common practice to have a simple static WndProc that simply forwards to the member WndProc of the owning class.

There is plenty of coverage of this online, but the gist of it is that when you create your window, there's an parameter in the window (possibly the Ex version) data structure that you can use to pass around user-defined data, and that data is provided to the first message that your WndProc receives -- If you pass a pointer to the class that has the member WndProc, then the static WndProc function can cache this pointer and use it to call the member WndProc.

EDIT - MadHead nicely fills in the rest of the details :)

Share this post


Link to post
Share on other sites
The pointer you pass to your window class (the WndProc) has to be to a free function. Member function pointers are not the same thing and are incompatible.

You can get around this by having one static WndProc that receives a pointer to your class and then re-dispatches to a second non-static WndProc inside your class instance.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!