Jump to content
  • Advertisement
Sign in to follow this  
mooreaa

boost function and GetProcAddress

This topic is 4112 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

Hello. I am having a bit of a problem combining a boost function pointer with a specific mem address. I am trying to get to a function stored in a dll, and safe the function to a boost function: boost::function<CBase* (std::string)> fn_myFunction; fn_myFunction= GetProcAddress( hModule, fn_name.c_str()); i get a error C2197: 'int (__stdcall *)(void)' : too many arguments for call and a bunch of template issues... what can i try?

Share this post


Link to post
Share on other sites
Advertisement
The problem is that GetProcAddress returns a FARPROC which is actually "int (__stdcall *)(void)" so you need to cast the result to the appropriate type of function pointer. Try using:
fn_myFunction= reinterpret_cast< CBase* (*)(std::string) >
(GetProcAddress( hModule, fn_name.c_str()));

I haven't tested it, but it should work.

EDIT: Or:
typedef CBase* (*funcptr)(std::string);
fn_myFunction = reinterpret_cast<funcptr>( GetProcAddress( hModule, fn_name.c_str()) );

If you would like something a bit more readable.

Share this post


Link to post
Share on other sites
Oh great! Now what does the (*) do in the reinterpret_cast? I am just trying to understand how reinterpret_cast works.

Share this post


Link to post
Share on other sites
Quote:
Original post by mooreaa
Oh great! Now what does the (*) do in the reinterpret_cast? I am just trying to understand how reinterpret_cast works.


That is just a result of the ugly syntax in C++ for function pointers. "CBase* (*)(std::string)" Simply means a type of function pointer that returns a CBase pointer and takes one std::string argument.

In general the type of a function pointer returning R and taking arguments A1, A2, ..., An is written:
R (*)(A1, A2, ..., An)

If we want to declare a variable with the same type we simply put the name right after the *.
reinterpret_cast is simply a way to reinterpret the meaning of some data, in our case a function pointer. The stuff inside < and > is simply the target type, so the weird syntax has nothing to do with reinterpret_cast, but rather with the syntax for function pointers.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!