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

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 on other sites
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 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 on other sites
Quote:
 Original post by mooreaaOh 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.