Archived

This topic is now archived and is closed to further replies.

function pointer to external function....

This topic is 5112 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

www.function-pointer.org was only mildly helpful in my figuring this out.
extern "C" __stdcall GetModuleHandleA(int a);
int (__stdcall *fptr)(int a);  ****

int main() {

int i;

fptr  GetModuleHandleA;

i = fptr(0);

i = GetModuleHandleA(0);

return 0;
};
Anything look funny? **** fp org and some books use void for tist line: int (__stdcall *fptr)(int a); Using int works but I don''t know why. Thanks for help and critisizems.

Share this post


Link to post
Share on other sites
quote:
Original post by ThoughtCriminal
**** fp org and some books use void for tist line:
int (__stdcall *fptr)(int a);
Using int works but I don''t know why.


a void pointer can point to anything, ints, floats, strings..

I guess that a void function pointer must then be able to return any type..

by changing it to int, you limited it to only int pointers...

Just a though..

Share this post


Link to post
Share on other sites
Ahhh but if I change it from int to void:

main.cpp(9) : error C2146: syntax error : missing '';'' before identifier ''GetModuleHandleA''
main.cpp(9) : warning C4551: function call missing argument list
main.cpp(11) : error C2440: ''='' : cannot convert from ''void'' to ''int''

I''m thinking I''m missing something here:
extern "C" __stdcall GetModuleHandleA(int a);


extern "C" __stdcall GetModuleHandleA(int a);
void (__stdcall *fptr)(int a);

int main() {

int i;

fptr = (void)GetModuleHandleA;****looking ugly already.

i = fptr(0);****error C2440: ''='' : cannot convert from ''void'' to ''int''

i = GetModuleHandleA(0);

return 0;
};

The externalnis of all this is whats confusing me. What is the type of the function? Or is the in the return type?

Share this post


Link to post
Share on other sites
quote:

I''m sorry but what exactly are you trying to do?


Hmmm good question.

I''m using Net 2003 C++ compiler. This is a cpp console project.

I''m trying to figure out 2 things:

1. Linking with C style functions in libs.

2. Making a function pointer to call said function.

I kinnda have it figured out. What bothers me now is why use ''int''? Randomly using types and cast etc until it compiles is generally a bad idea. So why ''int'' or why ''void''? Does the first code look correct? Its there a better way?

Thanks.

Share this post


Link to post
Share on other sites