Click here to Skip to main content
15,175,716 members
Articles / Programming Languages / C++
Posted 12 Mar 2002


26 bookmarked

Pass Class Method as Static function to API Calls

Rate me:
Please Sign up or sign in to vote.
1.86/5 (4 votes)
12 Mar 2002
Pass Class Methods to API Calls where Static Functions are required using CThunk class


A number of times we need to define static functions in a class to pass them to API Calls like Window Proc, Thread Proc, etc. Typically we define a static/global function and pass this as one parameter and recast that parameter into object again. In these cases, we can define a thunk that generates the Static Procedure at runtime, and pass the generated Proc to the API Function. All we need to set this correctly at runtime and jump to required address.

Code listing

The following class CThunk does the same thing.

Thunk Class 

F1:   Static Proc
F2:   Class function
T1 :  Class

// Function Types F1 & F2 should have same parameters 
// and return type otherwise stack corruption will occur


#if defined(_M_IX86)
#pragma pack(push,1)

template <typename F1,typename F2, typename T1>
class CThunk
    struct _ProcThunk
        BYTE m_movcx;          
        DWORD m_pThis;
        BYTE    m_jmp;          // jmp WndProc
        DWORD   m_relproc;      // relative jmp

    _ProcThunk thunk;

    void Init(F2 proc, T1 * pClass)
        //make sure that 'this' is in ecx pointer
        // otherwsie code does not work
#ifdef _DEBUG
        long  pthis;
        _asm  { mov         dword ptr [pthis],ecx }; 
        assert(pthis == (long) this);
        // put values
        thunk.m_movcx = 0xB9;  //B9
        thunk.m_pThis= (DWORD)pClass;
        thunk.m_jmp = 0xe9;
        thunk.m_relproc = 
            *((int *) &proc) - ((int)this+sizeof(_ProcThunk));

        // write block from data cache and 
        // flush from instruction cache
            &thunk, sizeof(thunk));

    F1 GetStaticProc()
        return reinterpret_cast<F1> (&thunk);
#pragma pack(pop)
#error Only X86 supported


The following code shows the usage of CThunk class for threads Proc. This class can be used in other places where Static procs are required. In this code CThunk class is used to create a thunk for the _beginthread API Call.

//Test Thread class

typedef void( __cdecl *THREADPROC )( void * );

class CThreadTest
    typedef LRESULT (CThreadTest::*FTN)
        (LPVOID lpThreadParameter);

    // THREADPROC & FTN have same signature
    CThunk<THREADPROC,FTN,CThreadTest> m_th; 

    HANDLE m_hThread;

    int m_y;
        //Intialize the Thunk

    LRESULT ThreadFunction(LPVOID lpThreadParameter)
        int ntime =(int) lpThreadParameter;
        printf("%d",m_y); // this pointer is set correctly
        return 0;

    BOOL Create (int nSleeptime)
        //Get Static Proc
        THREADPROC tp =m_th.GetStaticProc();

        // pass proc to  _beginthread
        m_hThread = (HANDLE)_beginthread(tp, 0,
        return TRUE;

    void WaitForThread()



int main(int argc, char* argv[])
    CThreadTest t1;



    return 0;


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Deepak Khajuria
Web Developer
United States United States
Programming in MFC/ATL for 2-3 years.

Comments and Discussions

GeneralWay to steal code from ATL! Pin
Darrin W. Cullop8-Apr-05 16:40
MemberDarrin W. Cullop8-Apr-05 16:40 
GeneralRe: Way to steal code from ATL! Pin
Ismail Ufuk PAZARBASI20-Aug-05 5:04
MemberIsmail Ufuk PAZARBASI20-Aug-05 5:04 

As a matter of fact, in many cases, I don't believe thunks have any advantage over other trivial solutions. For example, using a class method for thread callback. Thunk's are not panacea.

Thunks that WOW layer uses to provide transition between 16-bit and 32-bit worlds are where thunks become panacea and, in turn, inevitable.
GeneralTrouble with more than a program Pin
dna27-Jan-03 13:05
Memberdna27-Jan-03 13:05 
QuestionWhat the clue Pin
KarstenK10-Apr-02 5:20
mveKarstenK10-Apr-02 5:20 
QuestionOriginal article? Pin
Todd Smith27-Mar-02 8:27
MemberTodd Smith27-Mar-02 8:27 
QuestionDoes this code safe? Pin
Zhuofei Wang19-Mar-02 15:59
MemberZhuofei Wang19-Mar-02 15:59 
AnswerRe: Does this code safe? Pin
Deepak Khajuria19-Mar-02 18:55
MemberDeepak Khajuria19-Mar-02 18:55 
QuestionIs there any real performance/size benefit to this? Pin
Felix Cho18-Mar-02 4:07
MemberFelix Cho18-Mar-02 4:07 
Generalsomething similar Pin
12-Mar-02 23:14
suss12-Mar-02 23:14 
Questionincomplete code? Pin
12-Mar-02 23:14
suss12-Mar-02 23:14 
AnswerRe: incomplete code? Pin
Andreas Saurwein13-Mar-02 4:16
MemberAndreas Saurwein13-Mar-02 4:16 
GeneralRe: incomplete code? Pin
13-Mar-02 4:23
suss13-Mar-02 4:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.