Click here to Skip to main content
15,886,578 members
Articles / Desktop Programming / Win32

Generic Thunk with 5 combinations of Calling Conventions

Rate me:
Please Sign up or sign in to vote.
2.78/5 (8 votes)
13 Apr 2008CPOL12 min read 33.4K   398   19  
A simple and generic solution of making a member function become a callback function with the help of thunk technology.
#ifndef OWN_THUNK_H_080326
#define OWN_THUNK_H_080326


//if (this code works)
//{
//	it was written by
//	Roc King (OwnWaterloo)
//	2008-3-25 - 2008-3-27
//}	
//else
//{
//	I don't know who wrote it.
//}
//contact me : OwnWaterloo@gmail.com
//

#include <Thunk/ThunkBase.h>
#include <Thunk/ThisToStd.h>
#include <Thunk/ThisToCdecl.h>
#include <Thunk/StdToStd.h>
#include <Thunk/CdeclToCdecl.h>

/*------------------------------
		Introduction

	These source code contains some classes(all in namespace Thunk).
	Each object of these classes has two attributes: Obj, and Method(non-static).
	They can dynamically create some machine codes.
	Executing the machine codes will logically be the same as call Obj.Method(...);
	So,a non-static member function of some other objects(notice,not class!)
		can be callback function with the help of these classes
	
--------------------------------*/

/*--------------------------------
		Usage
	Using these classes needs 5 steps as follows:

Step 1 : Choose a correct class

	According to Calling Convention of both Member function's and callback function's.
	Suppose,the choice is CThunk (See last part of this file)

Step 2 : Instantiate a CThunk Object

	The object MUST EXIST until the callback is needless.
	Each Thunk:: classes has one Constructor(const void *Obj=0,int method=0)
		,can be use to attach object and member functions.(See Step 3 & 4)

	
Step 3 : Attach a object which need to be callback
		
		Method 1:
	Call const void* Attach(const void *newObj), passing the object's "THIS" pointer.
	It returns the last attached object.

		Method 2:
	Pass "this" to Constructor's first parameter.

		Notice 1:
	Must use the true "THIS" pointer to specify the Object.
	(means,the parameter's value equals to "this")
	
		Notice 2:
	If the attached object has been destroyed,the callback MUST BE STOPPED!


Step 4 : Attach the Member Function
	
	Member Function is specified by it's ENTRY POINT address.(a 32-bit integer value)
	There is a helper method:
	Call int Thunk::Helper::PointerToInt32(Pointer pointer) to get any pointer's value ����
	(including function pointer, returns 32-bit integer)

		Method 1:
	Call int Attach(int newMethod),passing the member function (int)
	It returns the last attached member function.

		Method 2:
	Pass member function to Constructor's second parameter.

		Method 3:
	Call int AttachMethod(Method newMethod).
	This function is just a combination PointerToInt32(Pointer) and int Attach(int) in one call.

		Notice 1:
	The attached function must be NON-STATIC-MEMBER-FUNCTION!


Step 5 : Convert it to a function pointer.
		
		Notice 1:
	The Callback function MUST have the same PARAMETER LIST as the Member Function attached.
		AND,Calling Convention MUST be RIGHT! (See last part of this file)

		Notice 2:
	AGAIN,either the Thunk object or other class's object has been destroyed,
		the callback MUST BE STOPPED!

		Method 1,2:
	a non-static and a static member function "MakeCallback" to do this work.
	They are both template functions,and could not deduce template argument.
	So,must pass the Callback type to it.

		Method 3:
	A cast operator.
	It's dangerous,(these Thunk classes is dangerous too) but convenience.
	
	All the 3 method returns function pointer.


---------------------------------*/



/*------------------------------

	Calling Convention

-------------------------------*/

/*
	normal function
	__cdecl (default in Visual Studio 2005)
	__stdcall
	__fastcall
	...

	non-static MemberFuntion
	__thiscall (default in Visual Studio 2005)
	__stdcall
	__cdecl
	...
*/

/*
	--------THISCALL--------

	__thiscall to __stdcall
	use Thunk::ThisToStd class

	__thiscall to __cdecl
	use Thunk::ThisToCdecl class

*/


/*
	--------CDECL(non-static MemberFuncion)--------
	__cdecl to __cdecl
	use Thunk::CdecltoCdecl class

	__cdecl to __std
	not implement
*/

/*
	--------STDCALL(non-static MemberFuncion)--------
	__stdcall to __stdcall
	use Thunk::StdToStd class

	__stdcall to __cdecl
	use Thunk::StdToCdecl class

*/

#endif	//#ifndef OWN_THUNK_H_080326

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 Code Project Open License (CPOL)


Written By
Software Developer (Junior)
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions