Non static winproc function

Started by
3 comments, last by lukesmith123 12 years, 1 month ago
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.
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().
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 :)

throw table_exception("(? ???)? ? ???");

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.
Ah thats brilliant thank you!

This topic is closed to new replies.

Advertisement