ATL uses thunks because the designers didn’t want to use SetWindowLongPtr(GWLP_USERDATA). Doing so would be a barrier to people porting existing code that happened to already store important data in GWLP_USERDATA. Also, another motivation for ATL thunks is that you won’t pay the price for the GetWindowLongPtr() lookup on every message processed. Any comments on how fast GetWindowLongPtr(, GWLP_USERDATA) is? If you’re unfamiliar with ATL thunks, it basically works like this: For each instance of a 'C++ window object', you create a small piece of code at runtime (called a thunk). When the window is created, the actual WndProc is replaced by the thunk by calling the SetWindowLongPtr(, GWL_WNDPROC, ) API. So, instead of calling the original WndProc, Windows will call your thunk instead.
The job of the thunk is to replace the HWND parameter passed to the WndProc with the 'this' pointer of the C++ class instance, and the thunk then jumps to the original WndProc. Because the thunk replaces the HWND with your this pointer, you code up your WndProc as accepting a 'this' pointer instead of an HWND as the first parameter. Password Guru.
Teach yourself or your students programming with Scratch, the simple, web-based visual programming language. Apr 22, 2005 I think it’s time to update the scratch program we’ve been using for the past year. I hear there’s this new language called C++ that’s going to become really popular any day now, so let’s hop on the bandwagon! #define STRICT #define UNICODE #define _UNICODE #include #include #include.
More details can be found in the article 'Thunking WndProcs in ATL' by Fritz Onion that appeared in C++ Report, March 1999: Raymond, can you comment on the merits of this technique vs. The GetWindowLongPtr(, GWLP_USERDATA) method? Thanks for another great blog! One thing that I’ve wondered about the ATL thunks is how future-proof they are.
Since they effectively step outside the bounds of the language and embed x86 ASM code into data members, aren’t they at risk for becoming obselete if x86 is ever abandoned? Although, admittedly, this is a pretty weak argument. The recent entries on the IA64 being a more strict architecture do bring these concerns back to the surface, however. I guess the nice thing about the ATL thunks is that the dirty work is done by maintainers at Microsoft, so conceivably we don’t have to worry about it as much.
'One thing that I’ve wondered about the ATL thunks is how future-proof they are. Since they effectively step outside the bounds of the language and embed x86 ASM code into data members, aren’t they at risk for becoming obselete if x86 is ever abandoned?' Uh, no ASM code being stored in data. Otherwise, you’d trip over NX protection.
All he’s doing is casting a pointer to a class (Window *) to another pointer type (void *) and storing that somewhere. He then retrieves it, casts it back, and calls a method on it, which is perfectly legal. In fact, he’s even being careful to observe the C++ Standard rule that you cannot cast a data pointer to a function pointer or visa versa. Stereo Mix Plus Safe. If you eliminate the __super extension as he mentioned, as far as I can tell it’s perfectly valid C++. Not portable, since it uses Win32, but a valid program. >For all those wondering ‘why don’t ANSI/ISO standardise __super’, think about what it would mean in a class with multiple bases. They would have to make an arbitrary decision whether to choose the first base, be illegal, or be a synonym for __super(0) which picks off the first base Raymond, I noticed that the this pointer is implicitly converted to a void * in the call to CreateWindowEx but down below you reinterpret_cast the lpCreateParams to a Window *.
- by admin