Click here to Skip to main content
15,885,278 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_THIS_TO_CDECL_H_080325
#define OWN_THUNK_THIS_TO_CDECL_H_080325

#include <Thunk/ThunkBase.h>

#include <Thunk/MachineCodeMacro.h>

namespace Thunk
{
	
#define __THIS_TO__CDECL_CODES()			\
/*	MOV DWORD PTR [old_esp],ESP	*/			\
CONST	CODE_FIRST(word,MOV_ESP_TO,0x2589)	\
CONST	CODE(dword_ptr,pold_esp,&old_esp)	\
											\
/*	POP	ECX	*/								\
CONST	CODE(byte,POP_ECX,0x59)				\
											\
/*	MOV DWORD PTR [old_return],ECX	*/		\
CONST	CODE(word,MOV_POLD_R,0x0D89)		\
CONST	CODE(dword_ptr,p_old_return,&old_return)	\
											\
/*	MOV ECX,this	*/						\
CONST	CODE(byte,MOV_ECX,0xB9)				\
		CODE(dword_ptr,m_this,0)			\
											\
/*	CALL memFunc	*/						\
CONST	CODE(byte,CALL,0xE8)				\
		CODE(dword,m_memFunc,0)				\
											\
/*	MOV ESP,old_esp	*/						\
CONST	CODE(byte,MOV_ESP,0xBC)				\
CONST	CODE(dword,old_esp,0)				\
/*	MOV DWORD PTR [ESP],old_retrun	*/		\
CONST	CODE(word,MOV_P,0x04C7)				\
CONST	CODE(byte,_ESP,0x24)				\
CONST	CODE(dword,old_return,0)			\
/*	RET	*/									\
CONST	CODE(byte,RET,0xC3)


	class ThisToCdecl
	{
	public:
		~ThisToCdecl();
		explicit ThisToCdecl(const void *Obj=0,int memFunc=0);
	
		const void* Attach(const void *newObj);
		int Attach(int newFunc);

	private:

		void SetOffset(dword dst);
		dword GetDST();

#pragma pack( push , 1)

	__THIS_TO__CDECL_CODES()

#pragma pack( pop )

	};
}

#endif	//#ifndef OWN_THUNK_THIS_TO_CDECL_H_080325

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