|
-
May 13th, 2005, 04:49 AM
#1
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:
Private declare sub CallByProcAddress Lib "xxx" (ByVal lpfnProcAddress as Long)
CallByProcAddress AddressOf TheProc
Sorry, I'm a C++ n00b.
-
May 14th, 2005, 02:12 AM
#2
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.
-
May 15th, 2005, 02:14 AM
#3
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
-
May 15th, 2005, 10:48 AM
#4
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.
-
May 15th, 2005, 11:06 AM
#5
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.
-
May 15th, 2005, 11:22 AM
#6
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:
Public Declare Function CallByProcAddress Lib "xxxx" _
(ByVal lpfnProcAddress As Long, _
ByVal arga As Long, _
ByVal argb As Long) _
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|