Results 1 to 6 of 6

Thread: Call external procedure by address

  1. #1

    Thread Starter
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Arrow Call external procedure by address

    What is the C++ command to call a procedure (residing in a calling app) by its address, in a function that accepts the address (of the function to be called) as an argument?

    Usage: To call a VB procedure by its address using a C++ DLL.
    e.g.

    VB Code:
    1. Private declare sub CallByProcAddress Lib "xxx" (ByVal lpfnProcAddress as Long)
    2.  
    3. CallByProcAddress AddressOf TheProc

    Sorry, I'm a C++ n00b.

  2. #2
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    Re: Call external procedure by address

    You can do something like this: (assuming the function you want to call is int __stdcall somefunc(int a, int b) )

    Code:
    typdef int (__stdcall* myfunctionptr)(int a, int b);
    
    int call_myfunc(void* ptr, int arga, int argb)
    {
       ((myfunctionptr) _ptr = (myfunctionptr)ptr;
       return _ptr(arga, argb);
    }
    basically you cast the address to a function pointer.
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  3. #3

    Thread Starter
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Call external procedure by address

    Couldn't get it to work... it said _ptr was an undefined identifier.

    Could you please possibly explain to me how to do it.. then I will understand a bit better?

    Sorry for being such a newb

  4. #4
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    Re: Call external procedure by address

    Not your fault -- looks like I messed up

    Give this a try; I'm not sure what I was thinking when I wrote that.

    Code:
    typdef int (__stdcall* myfunctionptr)(int a, int b);
    
    int call_myfunc(void* ptr, int arga, int argb)
    {
       myfunctionptr _ptr = (myfunctionptr)ptr;
       return _ptr(arga, argb);
    }
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  5. #5

    Thread Starter
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Call external procedure by address

    I built it, it failed...
    I changed typdef to typedef , added an export file, built it again, and it made a DLL file fine... but then when I tried to call it from VB the IDE crashed

    Does void* ptr mean I pass the address of the function as a Long (from VB)?
    I'm not sure how to declare it.

  6. #6

    Thread Starter
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Call external procedure by address

    I changed it a bit, not sure what did the trick, but it works now

    Here is the code I finished with:
    Code:
    int __stdcall CallByProcAddress(void* lpfnProcAddress, long arga, long argb)
    {
       ProcAddress _lpfnProcAddress = (ProcAddress)lpfnProcAddress;
       return _lpfnProcAddress(arga, argb);
    }
    VB Code:
    1. Public Declare Function CallByProcAddress Lib "xxxx" _
    2.     (ByVal lpfnProcAddress As Long, _
    3.      ByVal arga As Long, _
    4.      ByVal argb As Long) _
    5.     As Integer

    Thanks, I kinda understand it now

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width