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 9197209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:01:16+00:00 2026-06-17T22:01:16+00:00

I’m trying to get function addresses which are hidden behind structures. Unfortunately, the void*

  • 0

I’m trying to get function addresses which are hidden behind structures. Unfortunately, the void* basic C++ conversion doesn’t work, so I used C++ template instead.

1. Basic void* C++ conversion doesn’t work with functions inside structures, why?

void * lpfunction;
lpfunction = scanf; //OK
lpfunction = MessageBoxA; //OK

I made a simple structure :

struct FOO{

    void PRINT(void){printf("bla bla bla");}

    void SETA(int){} //nothing you can see
    void SETB(int){} //nothing you can see

    int GETA(void){} //nothing you can see
    int GETB(void){} //nothing you can see
};
///////////////////////////////////////////
void *lpFunction = FOO::PRINT;

And the compiling error :

error C2440: 'initializing' : 
cannot convert from 'void (__thiscall FOO::*)(void)' to 'void *'

2. Is getting function member addresses impossible?

Then, I made a template function which is able to convert a function member to address. Then I will call it by assembly. It should be something like this:

template <class F,void (F::*Function)()>  
void * GetFunctionAddress() {

    union ADDRESS  
    { 
        void (F::*func)();  
        void * lpdata;  
    }address_data;  

    address_data.func = Function;  
    return address_data.lpdata; //Address found!!!  

}  

And here is the code :

int main()
{
    void * address = GetFunctionAddress<FOO,&FOO::PRINT>();

    FOO number;
    number.PRINT(); //Template call
    void * lpdata = &number;

    __asm mov ecx, lpdata //Attach "number" structure address
    __asm call address //Call FOO::PRINT with assembly using __thiscall

printf("Done.\n");
system("pause");
return 0;
}

But, I see it is extremely specific. It looks like LOCK – KEY, and I have to make a new template for every set of argument types.

Original (OK) :

void PRINT(); //void FOO::PRINT();

Modify a bit :

void PRINT(int); //void FOO::PRINT(int);

Immediately with old template code the compiler shows :

//void (F::*func)();
//address_data.func = Function;

error C2440: '=' : cannot convert from 
'void (__thiscall FOO::*)(int)' to 'void (__thiscall FOO::*)(void)'

Why? They are only addresses.

69:       address_data.func = Function;
00420328  mov  dword ptr [ebp-4],offset @ILT+2940(FOO::PRINT) (00401b81)

…

EDIT3 : I know the better solution :

void(NUMBER::*address_PRINT)(void) = FOO::PRINT;
int(NUMBER::*address_GETA)(void) = FOO::GETA;
int(NUMBER::*address_GETB)(void) = FOO::GETB;
void(NUMBER::*address_SETA)(int) = FOO::SETA;
void(NUMBER::*address_SETA)(int) = FOO::SETB;

It’s much better than template. And by the way I want to achieve the goal :

<special_definition> lpfunction;
lpfunction = FOO::PRINT; //OK
lpfunction = FOO::GETA; //OK
lpfunction = FOO::GETB; //OK
lpfunction = FOO::SETA; //OK
lpfunction = FOO::SETB; //OK

Is this possible?

  • 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-06-17T22:01:17+00:00Added an answer on June 17, 2026 at 10:01 pm

    Pointers to member functions are nothing like pointers to global functions or static member functions. There are many reasons for this, but I’m not sure how much you know about how C++ works, and so I’m not sure what reasons will make sense.

    I do know that what you are trying in assembly simply won’t work in the general case. It seems like you have a fundamental misunderstanding about the purpose of member functions and function pointers.

    The thing is, you are doing some things that you would generally not do in C++. You don’t generally build up tables of function pointers in C++ because the things you would use that sort of thing for are what virtual functions are for.

    If you are determined to use this approach, I would suggest you not use C++ at all, and only use C.

    To prove these pointer types are completely incompatible, here is a program for you:

    #include <cstdio>
    
    struct Foo {
       int a;
       int b;
       int addThem() { return a + b; }
    };
    
    struct Bar {
       int c;
       int d;
       int addThemAll() { return c + d; }
    };
    
    struct Qux : public Foo, public Bar {
       int e;
       int addAllTheThings() { return Foo::addThem() + Bar::addThemAll() + e; }
    };
    
    int addThemGlobal(Foo *foo)
    {
       return foo->a + foo->b;
    }
    
    int main()
    {
       int (Qux::*func)();
    
       func = &Bar::addThemAll;
       printf("sizeof(Foo::addThem) == %u\n", sizeof(&Foo::addThem));
       printf("sizeof(Bar::addThemAll) == %u\n", sizeof(&Bar::addThemAll));
       printf("sizeof(Qux::addAllTheThings) == %u\n", sizeof(&Qux::addAllTheThings));
       printf("sizeof(func) == %u\n", sizeof(func));
       printf("sizeof(addThemGlobal) == %u\n", sizeof(&addThemGlobal));
       printf("sizeof(void *) == %u\n", sizeof(void *));
       return 0;
    }
    

    On my system this program yields these results:

    $ /tmp/a.out 
    sizeof(Foo::addThem) == 16
    sizeof(Bar::addThemAll) == 16
    sizeof(Qux::addAllTheThings) == 16
    sizeof(func) == 16
    sizeof(addThemGlobal) == 8
    sizeof(void *) == 8
    

    Notice how the member function pointer is 16 bytes long. It won’t fit into a void *. It isn’t a pointer in the normal sense. Your code and union work purely by accident.

    The reason for this is that a member function pointer often needs extra data stored in it related to fixing up the object pointer it’s passed in order to be correct for the function that’s called. In my example, when called Bar::addThemAll on a Qux object (which is perfectly valid because of inheritance) the pointer to the Qux object needs to be adjusted to point at the Bar sub-object before the function is called. So Qux::*s to member functions must have this adjustment encoded in them. After all, saying func = &Qux::addAllTheThings is perfectly valid, and if that function were called no pointer adjustment would be necessary. So the pointer adjustment is a part of the function pointer’s value.

    And that’s just an example. Compilers are permitted to implement member function pointers in any way they see fit (within certain constraints). Many compilers (like the GNU C++ compiler on a 64-bit platform like I was using) will implement them in a way that do not permit any member function pointer to be treated as at all equivalent to normal function pointers.

    There are ways to deal with this. The swiss-army knife of dealing with member function pointers is the ::std::function template in C++11 or C++ TR1.

    An example:

     #include <functional>
    
     // .... inside main
        ::std::function<int(Qux *)> funcob = func;
    

    funcob can point at absolutely anything that can be called like a function and needs a Qux *. Member functions, global functions, static member functions, functors… funcob can point at it.

    That example only works on a C++11 compiler though. But if your compiler is reasonably recent, but still not a C++11 compiler, this may work instead:

     #include <tr1/functional>
    
     // .... inside main
        ::std::tr1::function<int(Qux *)> funcob = func;
    

    If worse comes to worse, you can use the Boost libraries, which is where this whole concept came from.

    But I would rethink your design. I suspect that you will get a lot more milage out of having a well thought out inheritance hierarchy and using virtual functions than you will out of whatever it is you’re doing now. With an interpreter I would have a top level abstract ‘expression’ class that is an abstract class for anything that can be evaluated. I would give it a virtual evaluate method. Then you can derive classes for different syntax elements like an addition expression a variable or a constant. Each of them will overload the evaluate method for their specific case. Then you can build up expression trees.

    Not knowing details though, that’s just a vague suggestion about your design.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to select an H1 element which is the second-child in its group
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Basically, what I'm trying to create is a page of div tags, each has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.