Question

I'm trying to create a map of function names and function pointers using __stdcall. Here is how I currently get my function pointers:

typedef int (CALLBACK* InitializeDLL)(int,int);
InitializeDLL initializeDLL = (InitializeDLL)GetProcAddress(hInstanceLibrary, "InitializeDLL");

and now my Map:

map<string, int *__stdcall> mapInt;
mapInt["InitializeDLL"] = initializeDLL; //throws error for "InitializeDLL cannot be assigned to entity of type int*"

That error is exactly what I expected, but I need to add a type apparently in front of __stdcall. If I remove the "int" in the front then it complains with:

Error: expected a type specifier

If I try to compile it after only creating the map object with the "int" included in the front, it throws the error:

error C2059: syntax error : '>'

Which does not make much sense to me. So what is the correct way to use __stdcall as a type for a map? Adding the int in front of it seemed suspicious to me, but if I don't add it then it complains that it needs a type specified.

Also, CALLBACK is a #define for __stdcall if that is confusing.

Was it helpful?

Solution

A function pointer type is not limited to a single function. For instance, your InitializeDLL function pointer type can hold a pointer to any function that has this signature: int foo(int,int).

Perhaps CallbackFunction would be a better name for that typedef, since it encompasses all functions having a signature that can used for a callback:

typedef int (CALLBACK* CallbackFunction)(int,int);
CallbackFunction initializeDLL =
    (InitializeDLL)GetProcAddress(hInstanceLibrary, "InitializeDLL");
map<string, CallbackFunction> mapInt;
mapInt["InitializeDLL"] = initializeDLL;

There are more generic ways to handle functions (and even member functions). If you're capable/willing to use new C++11 features, you can use std::function and std::bind. If not, then you can use boost::function and boost::bind. The Boost and C++11 versions of those facilities are used in exactly the same way. The Boost documentation will be a lot more helpful than cppreference.

If you show us how you intend to invoke the callback functions in your map (including cases where the function signatures are different), I might be able to show you examples on how to use function and bind for your use case.


You might consider having all your command callbacks have the same function signature, then have each callback be responsible for extracting whatever parameters they need to do their work. The object that contains the command parameters can be passed as an argument to the callback function.

You can also consider passing a list of parameters to your callback functions in the form of std::vector<boost::any> or std::vector<boost::variant>. The callback function then converts each boost::any or boost::variant to the specific type associated with that particular parameter. If the command parameter types are of simple built-in types, you can even use std:vector<UnionOfPossibleParameterTypes>, where UnionOfPossibleParameterTypes is a plain old C-style union.

OTHER TIPS

You are using __stdcall like a type, but it isn’t one. You need to use your function pointer type in the map definition.

map<string, InitializeDLL> mapInt;

I’m not sure why you tried to do something else in the first place, since you already used this type elsewhere, shouldn’t it have been obvious that it was needed here as well?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top