阅读背景:

C++调用全局函数与类成员函数

来源:互联网 
void testfunc(void *param)
{
	printf("\n\tcall global function %s\n", param);
}

void *GetClassFuncAddr(...)
{
	DWORD address;

	__asm
	{
		lea eax,address
		mov edx, [ebp + 8]
		mov [eax], edx
	}

	return (void *)address;
}

void *callfunc(void *pfn, void *pthis, void *param)
{
	if (pthis != NULL)
	{
		unsigned long dwThis = (unsigned long)pthis;

		 typedef void* (__fastcall *memfunc)(void *,int, void*);//__fastcall调用方式会先传递两个DWORD参数(ecx与edx)
        //typedef void* (__thiscall *memfunc)(void *, void*);//__thiscall调用方式传递ecx

        /*typedef void* (__stdcall *memfunc)(void *);//__stdcall调用方式,此方式得准备this指针
        __asm mov ecx, dwThis;
        */
		 return reinterpret_cast<memfunc>(pfn)(pthis, 0, param);
	}
	else
	{
		typedef void *(*normalfunc)(void*);
		return reinterpret_cast<normalfunc>(pfn)(param);
	}
}

class CTest
{
public:
	void SimpleFunc(char *str)
	{
		printf("\n\tcall member function %s\n", str);
	}

	void SimpleCall(char *p)
	{
		callfunc(GetClassFuncAddr(&CTest::SimpleFunc), this, (void*)p);
	}
};




int _tmain(int argc, _TCHAR* argv[])
{
	CTest *pthis = new CTest();
	char *str = "test str";


	void *addr = GetClassFuncAddr(&CTest::SimpleCall);
	callfunc(addr, pthis, (void*)str);

	callfunc(&testfunc, NULL, (void*)str);
	return 0;
}void testfunc(void *param)
{
	printf("\n\tc



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: