can anyone explain the following code-snippet (found on codeproject - program 75 http://www.codeproject.com/atl/atl_u...asp#xx321175xx)
the line that I am having difficulty with is:Code:#include <iostream> #include <windows.h> using namespace std; class C; C* g_pC = NULL; typedef void(*pFUN)(); #pragma pack(push,1) // structure to store the machine code struct Thunk { BYTE m_jmp; // op code of jmp instruction DWORD m_relproc; // relative jmp }; #pragma pack(pop) class C { public: Thunk m_thunk; void Init(pFUN pFun, void* pThis) { // op code of jump instruction m_thunk.m_jmp = 0xe9; // address of the appripriate function m_thunk.m_relproc = (int)pFun - ((int)this+sizeof(Thunk)); FlushInstructionCache(GetCurrentProcess(), &m_thunk, sizeof(m_thunk)); } // this is cour call back function static void CallBackFun() { C* pC = g_pC; // initilize the thunk pC->Init(StaticFun, pC); // get the address of thunk code pFUN pFun = (pFUN)&(pC->m_thunk); // start executing thunk code which will call StaticFun pFun(); cout << "C::CallBackFun" << endl; } static void StaticFun() { cout << "C::StaticFun" << endl; } }; int main() { C objC; g_pC = &objC; C::CallBackFun(); return 0; }
What I don't understand is why you can't use (int)pFun as the address of the function, why the need for the math???Code:m_thunk.m_relproc = (int)pFun - ((int)this+sizeof(Thunk));
any help would be appreciate.. thanks
Zameer




Reply With Quote