Thunk technology!

Source: Internet
Author: User
Tags call back

# 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;
}

 

Execution result:

C: StaticFun
C: CallBackFun

 

Keep the code in question and try again later!

Related Article

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.