Thread: I have the Memory Address of a function, how do I call it?

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    27

    I have the Memory Address of a function, how do I call it?

    Say I have the Memory Address of a function, and I know the return type and the paramter types, how can I call it?

    I tried something like this:
    Code:
    typedef void(*TESTFUNCTION)(void);
    
    ...
    
    TESTFUNCTION Test = (TESTFUNCTION)0x00400000 <--- not real address
    
    Test();
    Oh ya, the function would have to be called by a DLL that has been loaded by the process.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Type some parentheses after the function pointer name, and put the arguments between the parentheses:
    Code:
    #include <iostream>
    using namespace std;
    
    void display(int n, double d)
    {
    	cout<<n<<endl;
    	cout<<d<<endl;
    }
    
    int main()
    {
    	void (*myfunc)(int, double);
    	myfunc = display;
    	
    	myfunc(10, 30.5);
    
    	return 0;
    }
    Or, with a typedef:
    Code:
    #include <iostream>
    using namespace std;
    
    typedef void (*TESTFUNCTION)(int, double);
    
    void display(int n, double d)
    {
    	cout<<n<<endl;
    	cout<<d<<endl;
    }
    
    int main()
    {
    	TESTFUNCTION a = display;
    	a(100, 200.5);
    
    	return 0;
    }
    Last edited by 7stud; 05-14-2005 at 07:26 PM.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    27
    Oh sorry, I wasn't clear enough.
    I know how to do it like that. I'm saying if I only know the hexadecimal memory address of a function how can I get it to work.

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Do this
    Code:
    void display(int n, double d){....}
    cout << display <<endl;
    You'll get an hexadecimal value printed to cout, the function adress. The functions name is just a var that holds that adress.
    So explicitly writing the adress or typing the function name will do, but keep this in mind: You can never know the adress that the compiler will give to a function, therefore doing what you want can and will be erroneous.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    27
    No, you see it WON'T be erroneous because I'm using the debugger to find the memory address from the completed code.

    The function pointer code will be contained within a DLL that will be loaded into the memory, thus the address of the function will not change.

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    hum, that way yes it may fail, because when loading a dll in memory you'll never know in which memory position it'll be. That's why there is a LoadLibrary(...) function in the win api. The same for the exe.
    Last edited by xErath; 05-21-2005 at 09:28 PM.

  7. #7
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    Code:
    void (*myfunc)(int, double);
    myfunc = ( void (*)(int, double) )0xADD12355;  //my leet version of address
    I think this would work, I don't have a compiler at ready to check it out for myself. However, like xErath said, if it's coming from a DLL, you might have a bit of trouble. Unless you figured that part out of course, in that case kudos!

    PS: If you're checking through the DLL using your debugger, what you're trying to do might be illegal. A DLL has a string table to associate a normal stringified function name with the address you're looking at so that you can call it like any normal function.

  8. #8
    Registered User
    Join Date
    May 2005
    Posts
    28
    How can you know the exact memory address? It never stays the same...

    Hard coding a memory address will most likely not work

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I think this would work, I don't have a compiler at ready to check it out for myself.
    I tried something like that earlier, but I couldn't get it to work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 07:17 PM
  3. how to get function call stack
    By George2 in forum C Programming
    Replies: 18
    Last Post: 11-11-2006, 07:51 AM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM