3

I am trying to display memory address of a function in a MessageBox, but it doesn't display it as I want.

I want to pass a function address of a callback function to another function, so I tried to get its address.

I looked at this example and tried to show it in a MessageBox first instead printing to console, before using it.

How I tried it:

char ** fun()
{
    static char * z = (char*)"Merry Christmas :)";
    return &z;
}
int main()
{
    char ** ptr = NULL;

    char ** (*fun_ptr)(); //declaration of pointer to the function
    fun_ptr = &fun;

    ptr = fun();

    char C[256];

    snprintf(C, sizeof(C), "\n %s \n Address of function: [%p]", *ptr, fun_ptr);
    MessageBoxA(nullptr, C, "Hello World!", MB_ICONINFORMATION);

    snprintf(C, sizeof(C), "\n Address of first variable created in fun() = [%p]", (void*)ptr);
    MessageBoxA(nullptr, C, "Hello World!", MB_ICONINFORMATION);

    return 0;
}

But, these messageboxes display very large numbers and they seems null.

I like to display them in a messagebox exactly like in linked post's example outputs.

Thanks in Advance.

Nipun
  • 2,217
  • 5
  • 23
  • 38
Blueeyes789
  • 543
  • 6
  • 18
  • 2
    the cast in `(char*)"Merry Christmas :)"` is useless and dangerous because string literal is already `const char*` – phuclv Aug 18 '17 at 06:43
  • 1
    What do you expect the value of a function pointer to look like? I'd expect a "very large number". What do you mean by "and they seems null", that seems to contradict "very large number". – Yunnosch Aug 18 '17 at 06:48
  • 1
    *What do you expect the value of a function pointer to look like?* Like this: `0xxxxxxx`. :-) And **I like to display them in a messagebox exactly like in linked post's example outputs.** – Blueeyes789 Aug 18 '17 at 06:49
  • 1
    Btw, why use `snprintf`, when you tag this [tag:C++]? It looks like [tag:C] code to me. When your code works, post it at https://codereview.stackexchange.com/ and ask for comments – Mawg says reinstate Monica Aug 18 '17 at 07:30
  • Does the message both print wrong (with `snprintf`) and display wrong in the `MessageBox`, or is the problem only the message box? – pingul Aug 18 '17 at 09:50
  • @pingul no, both prints wrong. Perhaps `[%p]` pointer format specifier won't work with `sntprintf`? – Blueeyes789 Aug 18 '17 at 09:51
  • 1
    If your OS is 64 bit, then you'll see addresses twice larger than in linked post – borisbn Aug 18 '17 at 10:03

1 Answers1

2

I made some changes in the code to make it more c++-y, and now it seems to work:

  1. I'm using std::cout to print instead of snprintf.
  2. I'm converting a pointer address to a std::string via the std::stringstream. This should work without problem for your MessageBox.
  3. I changed the function signature to const char** to avoid any problems.

Final code:

#include <iostream>
#include <sstream>

const char** fun()
{
    static const char* z = "Merry Christmas :)";
    return &z;
}
int main()
{
    const char** (*fun_ptr)() = fun; 
    const char** ptr = fun();

    std::cout << "Address of function: [" << (void*)fun_ptr  << "]" << std::endl;
    std::cout << "Address of first variable created in fun() = [" << (void*)ptr  << "]" << std::endl;

    std::stringstream ss;
    ss << (void*)fun_ptr;
    std::cout << "Address as std::string = [" << ss.str() << "]" << std::endl;

    return 0;
}

Outputs:

Address of function: [0x106621520]
Address of first variable created in fun() = [0x1066261b0]
Address as std::string = [0x106621520]
pingul
  • 3,351
  • 3
  • 25
  • 43