Brandon
Brandon

Reputation: 23498

Is my casting wrong?

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

Upvotes: 1

Views: 666

Answers (3)

user9876
user9876

Reputation: 11102

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

Upvotes: 1

Captain Obvlious
Captain Obvlious

Reputation: 20063

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.

Upvotes: 3

jwismar
jwismar

Reputation: 12258

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.

Upvotes: 1

Related Questions