Skip to content

Commit

Permalink
Allow direct cast of get_proc_address result (#95)
Browse files Browse the repository at this point in the history
GetProcAddress returns a pointer to some function. It can return
pointers to different functions, so it has to return something that is
suitable to store any pointer to function. Microsoft chose FARPROC,
which is int (WINAPI *)() on 32-bit Windows. The user is supposed to
know the signature of the function he requests and perform a cast
(which is a nop on this platform). The result is a pointer to function
with the required signature, which is bitwise equal to what
GetProcAddress returned. However, gcc >= 8 warns about that.

Add an overload with a template parameter to do the cast inside
the function and suppress the warning there.
  • Loading branch information
Flamefire authored Aug 26, 2024
1 parent dfc89a6 commit 3ce8d53
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions include/boost/winapi/get_proc_address.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,19 @@ BOOST_FORCEINLINE FARPROC_ get_proc_address(HMODULE_ hModule, LPCSTR_ lpProcName
#endif
}

template<typename Signature>
BOOST_FORCEINLINE Signature get_proc_address(HMODULE_ hModule, LPCSTR_ lpProcName)
{
#if defined(BOOST_GCC) && BOOST_GCC >= 80000
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-function-type"
#endif
return reinterpret_cast<Signature>(get_proc_address(hModule, lpProcName));
#if defined(BOOST_GCC) && BOOST_GCC >= 80000
#pragma GCC diagnostic pop
#endif
}

} // namespace winapi
} // namespace boost

Expand Down

0 comments on commit 3ce8d53

Please sign in to comment.