代码改变世界

使用stdcall模拟thiscall(调用成员函数)

2018-07-22 00:55  dearfuture  阅读(404)  评论(0)    收藏  举报
 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下测试通过