Use thunk to implement non-static class functions as Callback Functions

Source: Internet
Author: User

First, create a project based on the MFC dialog box. Insert the following code in the DLG class:

Class ctesttimer01dlg: Public cdialog

{

//...........

PRIVATE:
Byte m_codecmd [10]; // code command
// Mov address
// JMP address
Void _ initcodecmd (DWORD dwpthis, DWORD dwprocptr );
DWORD _ getmessageprocptr ();
Protected:

// Construction
Int m_itestshow;
// Call the rule with the default value _ thiscall
// Other call rules are not acceptable (possible _ fastcall is acceptable, but do not use it as much as possible)
Virtual void _ ontimersink (hwnd, DWORD dwmsg, wparam WPA, lparam LPA );

//...........

}
 

//////////////////////////////////////// ////////////////////////////////////
// Ctesttimer01dlg message handlers
Void ctesttimer01dlg: _ ontimersink (hwnd, DWORD dwmsg, wparam WPA, lparam LPA)
{
++ M_itestshow;
Invalidaterect (null );
}

Void ctesttimer01dlg: _ initcodecmd (DWORD dwpthis, DWORD dwprocptr)
{
 
/*
This machine code is equivalent to the following Assembly:
----------------------------------------------------------------
B9 ?? ?? ?? ?? MoV ECx, dwpthis; load ECx with this pointer
E9 ?? ?? ?? ?? JMP dwprocptr; jump to target message handler
*/
M_codecmd [0] = 0xb9; // mov command, responsible for passing this pointer
// The member functions are generally
// Use ECx to pass the this pointer, that is, _ thiscall. The member functions are called by default.
// _ Fastcall call: the compiler will try to use ECx to pass Parameters

// Use the stack to pass this pointer, that is, _ stdcall or _ cdecl call
// _ Stdcall indicates that the called function restores the call stack.
// _ Cdecl is a function called to restore the call stack. It can be used to implement variable length parameters.
M_codecmd [5] = 0xe9; // JMP command
* (DWORD *) & m_codecmd [1]) = (DWORD) dwpthis;
// Jump offset
DWORD dwdistance = (DWORD) dwprocptr-(DWORD) & m_codecmd [0]-10;
* (DWORD *) & m_codecmd [6]) = dwdistance;
 
}

DWORD ctesttimer01dlg: _ getmessageprocptr ()
{
/*
Typedef void (ctesttimer01dlg: * PMF) (hwnd, DWORD dwmsg, wparam WPA, lparam LPA );
Pmf p = & ctesttimer01dlg: _ ontimersink;
DWORD off = 0;
_ ASM
{
MoV eax, P
MoV [off], eax
}
Return off;
// Vc6 cannot be compiled
 
DWORD dwprocaddr = 0;
_ ASM
{
MoV eax, offset ctesttimer01dlg: _ ontimersink
MoV dword ptr [dwprocaddr], eax
}

Return dwprocaddr ;*/
// Get the timer callback pointer
// The Compiler does not allow forced conversion from ctesttimer01dlg: _ ontimersink to DWORD
// Use union_cast to implement conversion for compatibility
Return union_cast <DWORD> (& ctesttimer01dlg: _ ontimersink );
}

Bool ctesttimer01dlg: oninitdialog ()
{
Cdialog: oninitdialog ();
_ Initcodecmd (DWORD) This, _ getmessageprocptr ());
: Settimer (null, 1,500, (timerproc) (void *) m_codecmd );

// The following code is omitted

}

 

Here we use the type conversion function union_cast, which is defined as follows:

Template <class totype, class fromtype>
Totype union_cast (fromtype F)
{
Union
{
Fromtype _ F;
Totype _ t;
} Ut;

Memset (& ut, 0, sizeof (UT ));
Ut. _ f = F;
Return ut. _ t;
};

PS: wtl uses this method to implement member functions to process messages.

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.