C++调用全局函数与类成员函数
2015-01-18 16:05
323 查看
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 *func(void*);
另外,对于类的虚函数,此方法不适用,因为在调用虚函数时,需要先确定类的虚函数表,在此我没做过多分析
照目前这种情况对于我在写线程类上,感觉是够用了 ;)
具体格式看实际操作而定
如果我说的哪点不正确,请告知我 ;)
相关文章推荐
- c++ 如何把this指针传入成员函数 像全局函数一样调用成员函数
- [C++]static全局变量/全局变量,static函数/普通函数,函数中static变量/函数中的变量,类中的static成员变量/普通类成员变量
- 关于在C++内用指针调用类的成员函数
- C++继承时子类定义同名成员变量时的调用继承函数的问题
- C++继承类和基类之间成员函数和虚函数调用机制
- C++ - 对象模型之 成员函数调用
- 转:C++指针直接调用类成员函数探讨
- C++类中调用与成员函数同名的全局函数
- C++指针直接调用类成员函数探讨
- C++第三周任务3请在原类基础上,增加成员函数,要求前三个设计成内置函数,在main()数中增加适当的调用以展示扩充类定义后的功能(最好能一次运行)。
- C++指针直接调用类成员函数探讨
- C++指针直接调用类成员函数探讨
- C++的 成员函数指针, 静态函数指针,全局函数指针 区别
- C++ 类成员函数回调调用方法
- C++面试题__1__成员函数的调用
- 转:关于在C++内用指针调用类的成员函数
- C++中使用非静态成员函数代替全局函数
- 使用类成员函数指针调用C++成员函数的一种方式
- VC多线程中全局函数如何调用对话框类成员变量及成员函数
- 使用纯C函数指针调用C++的类成员函…