Sign in to follow this  
OniLink10

Win32 Window Wrapper WndProc Problems

Recommended Posts

I'm trying to write a Win32 Window Wrapper, and I'm having a problem with WndProc. WndProc needs to be a non-static member function of the wrapper due to my requirements, but I cannot use member functions for WndProc. Is there any way to convert it to non-member when passing it to the WNDCLASS or do SOMETHING so it can remain a non-static member? If someone needs code, I'll be glad to post it. I have been googling for hours and have yet to find a solution.

Share this post


Link to post
Share on other sites
This is actually a well-known problem that has been solved at least 3 different ways (that I know of). Since I'm bored I'm going to tell you all 3, but if you're impatient just skip to #3.

1. Allocate some memory in Thread Local Storage, and stash a pointer to the window wrapper class in there. This is the approach taken by Microsoft's MFC library.

2. Create a thunk using bits of assembly that changes the call to your WndProc from a __stdcall to a __thiscall (member function call) by sticking the "this" pointer for your window wrapper class in the ECX register. This is the approach taken by Microsoft's ATL library.

3. Stick a pointer to your window wrapper class in the GWL_USERDATA portion of the window data. Then every time your static WndProc gets a message, you retrieve the pointer and use it to call the message processing function of your wrapper class. This is the most common approach, and it's described quite nicely in this article.

Share this post


Link to post
Share on other sites
Quote:
Original post by MJP
This is actually a well-known problem that has been solved at least 3 different ways (that I know of). Since I'm bored I'm going to tell you all 3, but if you're impatient just skip to #3.

1. Allocate some memory in Thread Local Storage, and stash a pointer to the window wrapper class in there. This is the approach taken by Microsoft's MFC library.

2. Create a thunk using bits of assembly that changes the call to your WndProc from a __stdcall to a __thiscall (member function call) by sticking the "this" pointer for your window wrapper class in the ECX register. This is the approach taken by Microsoft's ATL library.

3. Stick a pointer to your window wrapper class in the GWL_USERDATA portion of the window data. Then every time your static WndProc gets a message, you retrieve the pointer and use it to call the message processing function of your wrapper class. This is the most common approach, and it's described quite nicely in this article.

Of course the solution happens to be simpler than expected! I'll be using solution 3 as it is the simplest. Thank you so much!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this