C + + calls global functions and class member functions

Source: Internet
Author: User

void TestFunc (void *param) {printf ("\n\tcall global function%s\n", param);} void *getclassfuncaddr (...) {DWORD Address;__asm{lea eax,addressmov 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; Typede  F void* (__fastcall *memfunc) (void *,int, void*); The//__fastcall invocation method first passes two DWORD parameters (ECX and edx)//typedef void* (__thiscall *MEMFUNC) (void *, void*),//__thiscall call passing ECX/*typedef void* (__stdcall *memfunc) (void *),//__stdcall call mode, this method is prepared        This pointer __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;}

One thing to add: The above are all based on the format of the called function

void *func (void*);

In addition, for virtual functions of classes, this method is not applicable, because when you call a virtual function, you need to determine the virtual function table of the class, where I do not do too much analysis

As it is now, I feel it is enough for me to write thread classes;)

Actual operation depends on the specific format

If I say something wrong, please let me know;)

C + + calls global functions and class member functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.