Click here to Skip to main content
15,886,518 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.
#include <Thunk/StdToStd.h>

#define THUNK_MACHINE_CODE_IMPLEMENT
#include <Thunk/MachineCodeMacro.h>

namespace Thunk
{
	dword_ptr StdToStd::GetObject() const
	{
		return m_this;
	}

	void StdToStd::SetObject(dword_ptr newObj)
	{
		m_this = newObj;
		Helper::FlushInstructionCache(&m_this,sizeof(m_this));
	}

	dword StdToStd::GetMethod() const
	{
		return Helper::GetTransferDST(&JMP);
	}

	void StdToStd::SetMethod(dword newMethod)
	{
		Helper::SetTransferDST(&JMP,newMethod);
		Helper::FlushInstructionCache(&m_method,sizeof(m_method));
	}

	StdToStd::~StdToStd()
	{}

	StdToStd::StdToStd(dword_ptr Obj,dword method)
		STD_TO_STD_CODES()
	{
		SetObject(Obj);
		SetMethod(method);
	}
	
	StdToStd::StdToStd(const StdToStd &src)
		STD_TO_STD_CODES()
	{
		*this = src;
	}

	StdToStd& StdToStd::operator =(const Thunk::StdToStd &rhs)
	{
		SetObject( rhs.GetObject() );
		SetMethod( rhs.GetMethod() );
		return *this;
	}

	bool StdToStd::operator == (const StdToStd &rhs) const
	{
		return (GetObject()==rhs.GetObject() && GetMethod()==rhs.GetMethod());
	}

	bool StdToStd::operator != (const StdToStd &rhs) const
	{
		return !(*this==rhs);
	}

	dword_ptr StdToStd::Attach(dword_ptr newObj)
	{
		dword_ptr oldObj = GetObject();
		SetObject(newObj);
		return oldObj;
	}

	dword StdToStd::Attach(dword newMethod)
	{
		dword oldMetod = GetMethod();
		SetMethod(newMethod);
		return oldMetod;
	}
}

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