From 3ce8d53c33df134d77564040d9912c308582c6d4 Mon Sep 17 00:00:00 2001 From: Alexander Grund Date: Mon, 26 Aug 2024 12:57:23 +0200 Subject: [PATCH] Allow direct cast of `get_proc_address` result (#95) 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. --- include/boost/winapi/get_proc_address.hpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/include/boost/winapi/get_proc_address.hpp b/include/boost/winapi/get_proc_address.hpp index 2240930..bd723d9 100644 --- a/include/boost/winapi/get_proc_address.hpp +++ b/include/boost/winapi/get_proc_address.hpp @@ -72,6 +72,19 @@ BOOST_FORCEINLINE FARPROC_ get_proc_address(HMODULE_ hModule, LPCSTR_ lpProcName #endif } +template +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(get_proc_address(hModule, lpProcName)); +#if defined(BOOST_GCC) && BOOST_GCC >= 80000 +#pragma GCC diagnostic pop +#endif +} + } // namespace winapi } // namespace boost