Win32 Window Wrapper WndProc Problems

Started by
1 comment, last by OniLink10 14 years, 2 months ago
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.
Advertisement
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.
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!

This topic is closed to new replies.

Advertisement