1
void xGetFunctionAddress(void* FunctionDefinition, std::string FunctionName)
{
    *static_cast<FARPROC*>(FunctionDefinition) = xProcAddress(Module, FunctionName.c_str());
}

In the above code, I'm trying to get rid of that FARPROC* to make it cross-platform. However, if I cast to long long int (*)(), it gives me the error that it cannot statically cast to that. So when I typedef it, it works:

Ex:

//This works:
void xGetFunctionAddress(void* FunctionDefinition, std::string FunctionName)
{
    typedef __stdcall long long int (*Ptr)();
    *static_cast<Ptr*>(FunctionDefinition) = GetProcAddress(Module, FunctionName.c_str());
}

//This doesn't:
void xGetFunctionAddress(void* FunctionDefinition, std::string FunctionName)
{
    *static_cast<long long int(*)()>(FunctionDefinition) = GetProcAddress(Module, FunctionName.c_str());
}

What am I doing wrong in the second example? :S

Brandon
  • 22,723
  • 11
  • 93
  • 186

3 Answers3

3

Your are casting the void pointer to a function pointer and then dereferencing it. This evaluates to an assignment to a function instead of a function pointer. The following should take of the problem

*static_cast<long long int(__stdcall **)()>(FunctionDefinition) = GetProcAddress(Module, FunctionName.c_str());
                                     ^^

Notice the additional pointer level next to __stdcall.

Captain Obvlious
  • 19,754
  • 5
  • 44
  • 74
1

It would be useful to see the specific error message, but it looks like you might be missing the __stdcall dcl-specifier in your static_cast. Does it work if you add that?

Edit:

Looking further, it appears this may not be supported. Please see this answer: https://stackoverflow.com/a/1096349/279130 which seems to address the same question.

Community
  • 1
  • 1
jwismar
  • 12,164
  • 3
  • 32
  • 44
  • No I tried that too. error: invalid static_cast from type 'void*' to type 'long long int (*)() with and without the __stdcall Changing it to the typedef gives no problems at all.. – Brandon Jun 02 '13 at 01:17
  • OK. Did some additional research. Modifying answer above. – jwismar Jun 02 '13 at 01:21
1

What is GetProcAddress() defined to return?

If it's a void *, you can't portably cast that to a function pointer. See https://stackoverflow.com/a/1096349/37386

Community
  • 1
  • 1
user9876
  • 10,954
  • 6
  • 44
  • 66