Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6618095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:47:05+00:00 2026-05-25T20:47:05+00:00

I want to use class member functions as callbacks, I don’t use libsigc, because

  • 0

I want to use class member functions as callbacks, I don’t use libsigc, because it’s slow.
In ATL, we can use member function for C-style callback(http://www.codeproject.com/KB/cpp/SoloGenericCallBack.aspx), so can we implement c++ thunk in linux?

The code below will crash:

#include <assert.h>
#include <stdio.h>
#include <sys/mman.h>

typedef char BYTE;
typedef int DWORD;
typedef int* DWORD_PTR;
typedef int* INT_PTR;
typedef bool BOOL;
typedef unsigned long ULONG;
typedef unsigned long* ULONG_PTR;
#define PtrToUlong( p ) ((ULONG)(ULONG_PTR) (p) )
#define __stdcall __attribute__((__stdcall__))

//#pragma pack( push, 1 )
struct  MemFunToStdCallThunk
{
    BYTE          m_mov;
    DWORD      m_this; 
    BYTE          m_pushEax;
    BYTE          m_jmp;
    DWORD      m_relproc;

    void  Init( DWORD_PTR proc, void* pThis )
    {
        printf("proc=%x\n", proc);
        m_mov = 0xB8;   // mov eax
        m_this = PtrToUlong(pThis);
        m_pushEax = 0xc3;// push eax
        m_jmp = 0xe9;          //jmp
        m_relproc = DWORD((INT_PTR)proc - ((INT_PTR)this+sizeof(MemFunToStdCallThunk)));
        printf("m_relproc = %x\n", m_relproc);
        mprotect(this, sizeof(MemFunToStdCallThunk), PROT_READ|PROT_WRITE|PROT_EXEC);
    }

    void* GetCodeAddress()
    {
        return this;
    }
}__attribute__ ((packed));
//#pragma  pack( pop )

template< typename TDst, typename TSrc >
TDst  UnionCastType( TSrc src )
{
    union
    {
        struct
        {
            int* pfn;  //function,index
            long delta; // offset, 
        }funcPtr;
        TSrc  uSrc;
    }uMedia;
    uMedia.uSrc  = src;
    return uMedia.funcPtr.pfn;
}




typedef  int  ( __stdcall *StdCallFun)(int, int);
class  CTestClass
{
public:
    int  m_nBase;
    MemFunToStdCallThunk  m_thunk;

    int  memFun( int m, int n )
    {
        int  nSun = m_nBase + m + n;
        printf("m=%d,n=%d,nSun=%d\n", m, n, nSun);
        return 1234;
    }

public:
    CTestClass()
    {
        m_nBase  = 10;
    }

    void  Test()
    {
        printf("%x\n", &CTestClass::memFun);
        m_thunk.Init(UnionCastType<DWORD_PTR>(&CTestClass::memFun), this );
        StdCallFun fun = (StdCallFun)m_thunk.GetCodeAddress();
        assert( fun != NULL );

        int ret =   fun( 9, 3 );
        printf("ret = %x\n", ret);


    }
};



int main()
{
    CTestClass test;
    test.Test();
    return 0;
}

EDIT:
Thanks to user786653, I get the right answer:

#include <assert.h>
#include <stdio.h>
#include <sys/mman.h>
#include <unistd.h>

typedef char BYTE;
typedef int DWORD;
typedef int* DWORD_PTR;
typedef int* INT_PTR;
typedef bool BOOL;
typedef unsigned long ULONG;
typedef unsigned long* ULONG_PTR;
#define PtrToUlong(p) ((ULONG)(ULONG_PTR) (p) )
#define __stdcall __attribute__((__stdcall__))

struct  MemFunToStdCallThunk
{
    BYTE          m_repairStack[10];
    DWORD      m_mov;
    DWORD      m_this;
    BYTE          m_jmp;
    DWORD      m_relproc;

    void  Init( DWORD_PTR proc, void* pThis )
    {
        printf("proc=%p\n", proc);
        m_repairStack[0] = 0x83; //sub esp, 0x4
        m_repairStack[1] = 0xec;
        m_repairStack[2] = 0x04;
        m_repairStack[3] = 0x8b; //mov eax,[esp + 0x4]
        m_repairStack[4] = 0x44;
        m_repairStack[5] = 0x24;
        m_repairStack[6] = 0x04;
        m_repairStack[7] = 0x89;//mov [esp], eax
        m_repairStack[8] = 0x04;
        m_repairStack[9] = 0x24;
        m_mov = 0x042444C7;   // mov   dword   ptr   [esp+0x4], 
        m_this = PtrToUlong(pThis);
        m_jmp = 0xe9;          //jmp
        m_relproc = (DWORD)proc - ((DWORD)this+sizeof(MemFunToStdCallThunk));
        printf("m_relproc = %d\n", m_relproc);
        //long page_size = sysconf(_SC_PAGE_SIZE);
        //mprotect((void*)(PtrToUlong(this) & -page_size), 2*page_size, PROT_READ|PROT_WRITE|PROT_EXEC);
    }

    void* GetCodeAddress()
    {
        return this;
    }
}__attribute__ ((packed));


template< typename TDst, typename TSrc >
TDst  UnionCastType( TSrc src )
{
    union
    {
        struct
        {
            int* pfn;  //function or index
            long delta; // offset
        }funcPtr;
        TSrc  uSrc;
    }uMedia;
    uMedia.uSrc  = src;
    return uMedia.funcPtr.pfn;
}




typedef  int  ( __stdcall *StdCallFun)(int, int);
class  CTestClass
{
public:
    int  m_nBase;
    MemFunToStdCallThunk  m_thunk;

    int  memFun( int m, int n )
    {
    printf("this=%p\n", this);
        int  nSun = m_nBase + m + n;
        printf("m=%d,n=%d,nSun=%d\n", m, n, nSun);
        return nSun;
    }

public:
    CTestClass()
    {
        m_nBase  = 10;
    }

    void  Test()
    {
        int (CTestClass::*abc)(int, int);
        printf("sizeof(MemFunToStdCallThunk)=%d,sizeof(abc)=%d\n", sizeof(MemFunToStdCallThunk), sizeof(abc));
        printf("memFun=%p\n", &CTestClass::memFun);
        m_thunk.Init(UnionCastType<DWORD_PTR>(&CTestClass::memFun), this );
        StdCallFun fun = (StdCallFun)m_thunk.GetCodeAddress();
        assert( fun != NULL );

        int ret = memFun(2, 3);
        printf("ret 1= %d\n", ret);
        ret =  fun( 9, 3 );
        printf("ret 2= %d\n", ret);
    }
};


int main()
{
    CTestClass test;
    test.Test();
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:47:06+00:00Added an answer on May 25, 2026 at 8:47 pm

    Yes, but I wouldn’t recommend it. It will (obviously) make your code a lot less portable and you’re potentially opening a security hole if you’re not careful.

    You will need to make the code executable with mprotect(2). Something like mprotect(&thunk_struct, sizeof(struct _CallBackProcThunk), PROT_READ|PROT_WRITE|PROT_EXEC).

    Also the normal GCC syntax for structure packing is struct S { /* ... */ } __attribute__ ((packed)) though newer versions might support the #pragma pack syntax.

    You will probably also want to substitute DWORD with uint32_t from stdint.h and BYTE with uint8_t (or just stick a typedef in there).

    EDIT:

    From the man page on mprotect “[..]addr must be aligned to a page boundary”. You should check the return value. Try doing something like this instead:

    long page_size = sysconf(_SC_PAGE_SIZE);
    uintptr_t addr = ((uintptr_t)this) & -page_size;
    if (mprotect((void*)addr, 2*page_size, PROT_READ|PROT_WRITE|PROT_EXEC)) {
        perror("mprotect"); 
        /* handle error */
    }
    

    The following calculation is wrong:

    DWORD((INT_PTR)proc - ((INT_PTR)this+sizeof(MemFunToStdCallThunk)))
    

    It’s doing its calculations on int*‘s.

    (DWORD)proc - ((DWORD)this+sizeof(MemFunToStdCallThunk)
    

    should be sufficient here.

    A very ugly (non-portable etc. etc.), but small and self-contained example follows:

    #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    #include <unistd.h>
    #include <sys/mman.h>
    
    struct thunk {
        uint32_t mov;
        uint32_t this_ptr;
        uint8_t jmp;
        uint32_t rel;
    } __attribute__((packed));
    
    class Test {
    public:
        virtual int foo(void) {
            printf("foo! %p\n", (void*)this);
            return 42;
        }
    };
    
    int main()
    {
        Test test;
        printf("%d\n", test.foo());
    
        thunk t;
        t.mov = 0x042444C7;
        t.this_ptr = (uint32_t)&test;
        t.jmp = 0xe9;
        t.rel = ((uint32_t)(void*)&Test::foo) - ((uint32_t)&t + sizeof(thunk));
    
        uint32_t addr = (uint32_t)&t;
        long page_size = sysconf(_SC_PAGE_SIZE);
        if (mprotect((void*)(addr & -page_size), 2*page_size, PROT_READ|PROT_WRITE|PROT_EXEC)) {
            perror("mprotect");
            return 1;
        }
    
        union {
            void* p;
            int (*foo)(int);
        } u;
        u.p = &t;
        printf("%d\n", u.foo(0));
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use gcd function of the Integer class. Using the example from
In a Android application I want to use Scanner class to read a list
I want to use the DbProvider class to construct a generic DAL component. This
I want to use the UIGestureRecognizer class inside the iPhone (not the iPad), is
So I want to use memoir document class and the ntheorem package with the
The singleton is explained here: http://en.wikipedia.org/wiki/Singleton_pattern#PHP_5 . I want to use the singleton class
I want to use the Saxon-B XQuery class in my ASP.Net website. But my
I want to use the Html Helper class to build, for example, an Html.ActionLink
I want to use mootools and SqueezBox class to handle a request to a
I have a class that I want to use to store properties for another

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.