// Test.cpp: implementation of the CTest class. // //////////////////////////////////////////////////////////////////////
#include "stdafx.h" #include "Test.h"
////////////////////////////////////////////////////////////////////// // Construction/Destruction //////////////////////////////////////////////////////////////////////
#define CLASS_MEMBER_FUNCTION_SUPPORT_CALLBACK( ThisPointerAddress ) \ __asm mov eax , (ThisPointerAddress)\ __asm mov [ebp-4] , eax
CTest::CTest() { typedef void ( CTest::* pf )(int ); pf p = &CTest::TestFunction; m_iTheadID = 0X99; m_iTheadID1 = 0X12; m_sChar = 'A'; ULONG * pTID = &this->m_iTheadID;
__asm { push pTID push 0 push this push p push 0 push 0 // eax是作为函数的返回值,所以改变没有关系 mov eax , CreateThread call eax } }
CTest::~CTest() {
}
void CTest::TestFunction(int ThisPointer) { CLASS_MEMBER_FUNCTION_SUPPORT_CALLBACK(ThisPointer) int x = 1000;
// 下面来分析程序走到这里栈的结构: // // [ebp+8] -> ThisPointer的值,因为是传值 // [ebp+4] -> CTest::TestFunction(int a)函数的 call [CTest::TestFunction] 该语句的地址加4 // [ebp] -> 作为局部变量与外部传进变量的分界线,存入的是ebp原来的值 // [ebp-4] -> 指向类的 this 指针 , 这是由编译器加入代码中的,如果函数不是成员函数, // 这个地址指向第一个分配的局部变量,而 this 指针是通过 ecx 进入传递的, // 也就是说成员函数和全局函数的本质是一样的(它们在栈里面的参数个数是相同的), // 只是编译器在语法分析的时候会对成员函数进行一些附加的工作, // 即如果是成员函数的时候,在函数的 '{' // 之前通过:mov [ebp-4],ecx 对 this 指针进行分配,以后每次出现非局部变 // 量和非传入变量都会以[ebp-4]为基进行内存的读写 // [ebp-8] -> 指向局部变量 x 的地址 // // // esp -> 永远指向栈顶,当函数退出时会以 mov esp,ebp 让所有局部变量消失 printf("Hello World! I am Inside a class , and Called by OS\n"); printf("m_iTheadID1:0x%x \t %c \n" , m_iTheadID1 , m_sChar); }
// Test.h: interface for the CTest class. // //////////////////////////////////////////////////////////////////////
#if !defined(AFX_TEST_H__F30756C2_DE72_48F2_BBA2_A416B7ED3E28__INCLUDED_) #define AFX_TEST_H__F30756C2_DE72_48F2_BBA2_A416B7ED3E28__INCLUDED_
#if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000
class CTest { public: void TestFunction(int a); CTest(); ~CTest();
private: ULONG m_iTheadID1; ULONG m_iTheadID; CHAR m_sChar; };
#endif // !defined(AFX_TEST_H__F30756C2_DE72_48F2_BBA2_A416B7ED3E28__INCLUDED_)
// TestMemberFunction.cpp : Defines the entry point for the console application. //
#include "stdafx.h" #include "Test.h"
int main(int argc, char* argv[]) { CTest testobject; Sleep(100); return 0; }
上面是整个实现的代码,都有注释,慢慢看吧! 
|