visual c++ - C++ display address of a function in a MessageBox -
i trying display memory address of function in messagebox, doesn't display want.
i want pass function address of callback function function, tried address.
i looked @ this example , tried show in messagebox first instead printing console, before using it.
how tried it:
char ** fun() { static char * z = (char*)"merry christmas :)"; return &z; } int main() { char ** ptr = null; char ** (*fun_ptr)(); //declaration of pointer 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 large numbers , seems null.
i display them in messagebox in linked post's example outputs.
thanks in advance.
i made changes in code make more c++
-y, , seems work:
- i'm using
std::cout
print instead ofsnprintf
. - i'm converting pointer address
std::string
viastd::stringstream
. should work without problemmessagebox
. - i changed function signature
const char**
avoid 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 std::string = [" << ss.str() << "]" << std::endl; return 0; }
outputs:
address of function: [0x106621520] address of first variable created in fun() = [0x1066261b0] address std::string = [0x106621520]
Comments
Post a Comment