1. 程式人生 > >使用stdcall模擬thiscall(調用成員函數)

使用stdcall模擬thiscall(調用成員函數)

dea sys UNC spec name this x86 color nbsp

 1 #include <iostream>
 2 using namespace std;
 3 
 4 __declspec(naked) void* get_addr(...)
 5 {
 6     __asm
 7     {
 8         mov eax, dword ptr[esp + 4]
 9         ret
10     }
11 }
12 
13 class A
14 {
15 public:
16     char *name = "dearfuture";
17     void print()
18     {
19         cout << name << "
: hello world\n"; 20 } 21 }; 22 23 int main() 24 { 25 A a; 26 A *pa = &a; 27 void *addr = get_addr(&A::print); 28 __asm 29 { 30 mov ecx, pa 31 } 32 void(_stdcall *f)() = (void(_stdcall *)())addr; 33 f(); 34 system("pause"); 35 return 0; 36 }
 1
#include <iostream> 2 using namespace std; 3 4 class A 5 { 6 public: 7 char *name = "dearfuture"; 8 void print() 9 { 10 cout << name << ": hello world\n"; 11 } 12 }; 13 14 int main() 15 { 16 auto pFunc = &A::print; 17 void *pAddr = static_cast<void
*>(&pFunc); 18 void *addr = *static_cast<void **>(pAddr); 19 void(_stdcall *f)() = (void(_stdcall *)())addr; 20 21 A a; 22 A *pa = &a; 23 __asm 24 { 25 mov ecx, pa 26 } 27 f(); 28 29 system("pause"); 30 return 0; 31 }

在msvc x86下測試通過

使用stdcall模擬thiscall(調用成員函數)