Click here to Skip to main content
15,895,809 members
Articles / Programming Languages / C++

Thunking in Win32: Simplifying Callbacks to Non-static Member Functions

Rate me:
Please Sign up or sign in to vote.
4.33/5 (32 votes)
22 Dec 2006Apache10 min read 119.8K   673   60  
A quick introduction to thunking, as well as a demonstration of a simple library which does the work for us.
/***************************************************************************
**  Thunk32                                       December 22nd, 2006
**  Einar Otto Stangvik <einaros(at)gmail.com>, www.indev.no
**  Roger Pate <roger(at)qxxy.com>
**	
**  Enables simple, type safe thunking in win32. For more information,
**  see http://einaros.livejournal.com/#einaros523.
**
**  Changelog:
**
**    - August 17th, 2006: 
**			Initial release
**
**    - December 19th, 2006: 
**			Added instruction flushing, which is required on some 
**			architectures to avoid the execution of uninitalized (cached) 
**			thunks.
**
**	  - December 19th, 2006: 
**			Changed thunks to use __thiscall. Reduced thunk adjustments 
**			from 5 to 3 instructions.
**
**	  - December 22nd, 2006: 
**			Introduced a private thunk heap, to the avoid high memory impact
**			caused by multiple blocks allocated by VirtualAlloc.
****************************************************************************/

#pragma once

// Boost Headers
#include <boost/static_assert.hpp>
#include <boost/preprocessor/repetition.hpp>
#include <boost/preprocessor/iterate.hpp>

// Thunk32 Headers
#include "Thunk32Base.h"

namespace indev
{
	template<class T, typename Signature>
	class Thunk32;
}

#ifndef THUNK32_MAX_ARGS
#  define THUNK32_MAX_ARGS 10
#endif // THUNK32_MAX_ARGS

#define BOOST_PP_ITERATION_PARAMS_1 (3,(0,THUNK32_MAX_ARGS,"Thunk32_template.h"))
??=include BOOST_PP_ITERATE()
#undef BOOST_PP_ITERATION_PARAMS_1

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0


Written By
Software Developer
Norway Norway
My name is Einar Otto Stangvik, and I'm a programmer based in Oslo, Norway. I mainly develop applications and software architectures targetting C++ on the Windows platform, but I have also got experience doing the same on Unix and Linux. The last few years I've used C# a lot, but native C++ is still my main focus.


As of July 2008, I'm a Microsoft MVP for Visual C++.


Follow me on Twitter: @einaros
My code blog: einaros.blogspot.com
My site: www.indev.no

Comments and Discussions