Function Pointer

This is a discussion on Function Pointer within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm a pickle at the moment. I have been playing around with import table hijacking, well now I want ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    8

    Function Pointer

    Hello,
    I'm a pickle at the moment. I have been playing around with import table hijacking, well now I want to rewrite a function and to avoid having the program malfunction, I need to use the old function in the new function; that's besides the point.
    I have something like this, which would pointer function
    typedef int (__cdecl * oldfunct)(SOCKET s, const char *buf, int len, int flags);
    I however do not know how to assign oldfunct a value, in other words, I don't know how to cast it;
    oldfunct = pThunk2->Function; //pThunk2->Function == the pointer
    doesn't work and neither do
    oldfunct = (int __cdecl) pThunk2->Function;
    oldfunct = (int __cdecl *)(SOCKET , const char *, int , int ) pThunk2->Function;
    oldfunct = (int __cdecl) pThunk2->Function;
    oldfunct = (int __cdecl *) (SOCKET , const char *, int , int )pThunk2->Function;
    Any advice is appreciated, thanks.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I however do not know how to assign oldfunct a value,
    oldfunct = someFunctionName;

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Here's an example:
    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    
    int oldfunct(int n, double d)
    {
    	cout<<n<<endl<<d<<endl;
    	
    	return 1;
    }
    
    void newfunct(int n, double d, string name, int(*pfunc)(int, double) )
    {
    	pfunc(n, d);
    	cout<<name<<endl;
    }
    
    
    int main()
    {
    	newfunct(10, 12.5, "Betty", oldfunct);
    
    	return 0;
    }

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    8
    Oh sorry for the ambiguity, but the new value is a DWORD. This really shouldn't be C++, but I'm using MS VC++ 2003. Ok, I'm trying this with a simpler situation
    typedef int (__cdecl * oldfunct)(const char * ez, ...);
    oldfunct = &printf;
    (*oldfunct)("test %d %d %d", 1, 2, 3);
    yet when I do that, I get an error
    projectile.cpp(46): error C2513: 'int (__cdecl *)(const char *,...)' : no variable declared before '='

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Oh sorry for the ambiguity, but the new value is a DWORD.
    What the heck does that mean? I don't have any idea what "the new value" is.

    yet when I do that, I get an error
    projectile.cpp(46): error C2513: 'int (__cdecl *)(const char *,...)' : no variable declared before '='
    You are getting that error because a typedef creates a synonym for a type, i.e. something that is easier to type in than the actual type. So, oldfunct is a synonym for a type, and you can't do this:
    Code:
    int = 6;
    which is equivalent to what you are doing, and which I did in my first post.

    Instead, you have to create a variable of type oldfunct and then assign it a value:
    Code:
    oldfunct pfunc;
    pfunc = someFunc;
    
    //or combining the statements:
    
    oldfunct pfunc = someFunc;
    Note the name of a function is an address, so you don't use the address-of operator on the function name. To call the function, you would do this:
    Code:
    pfunc(p1, p2...);
    Here's a complete example:
    Code:
    #include<iostream>
    using namespace std;
    
    int someFunc(int n, double d)
    {
    	cout<<n<<endl<<d;
    	
    	return 10;
    }
    
    typedef int(*IntIntDp)(int, double);
    
    int main()
    {
    	IntIntDp pfunc = someFunc;
    
    	int result = pfunc(3, 10.1);
    	cout<<endl;
    	
    
    	return 0;
    }
    Last edited by 7stud; 11-22-2005 at 06:08 PM.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Code:
    cout<<endl;
    Did you mean
    Code:
    cout << result << endl;
    ? Otherwise result is used only once.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Did you mean

    cout << result << endl;

    ?
    No.
    Otherwise result is used only once.
    Do you think the example was a demonstration of how to display an int returned by a function?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  3. Function Pointer help
    By Skydt in forum C Programming
    Replies: 5
    Last Post: 12-02-2005, 08:13 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21