Search code examples
c++winapicastingfunction-pointerscompiler-warnings

How to address C4191 warning around calls to GetProcAddress with FARPROC?


Recently I tried to use /Wall Visual C++ option to enable all warnings and found that the following code:

typedef BOOL ( WINAPI * TIsWow64ProcessFunction )( HANDLE, BOOL* );
TIsWow64ProcessFunction isWow64ProcessFunction = reinterpret_cast<TIsWow64ProcessFunction> (
    ::GetProcAddress( kernel32DllHandle, "IsWow64Process" ) );

spawned C4191:

warning C4191: 'reinterpret_cast' : unsafe conversion from 'FARPROC' to 'TIsWow64ProcessFunction'
Calling this function through the result pointer may cause your program to fail

If I use a C-style cast the same warning appears but now it mentions "type cast" instead of "reinterpret_cast".

The same warning is repeated for just any case I call GetProcAddress() and convert its return value to some usable function pointer.

How do I address these warnings? Do I need to make alterations to my code?


Solution

  • You are casting a FARPROC (function pointer with no args) to a function pointer with args. Normally this is a hugely dumb thing to do that will probably result in stack corruption.

    Now it turns out that GetProcAddress() doesn't really return a FARPROC and you do actually know what you're doing -- but the compiler doesn't know that and it feels obliged to warn you.

    The only way you can silence it is using a #pragma or a compiler switch to turn off the warning. It is ugly and messy, but that is Windows programming for you. :-)