From efaefe9e490c1d3cf5071ab8093abb59526a5002 Mon Sep 17 00:00:00 2001 From: mitchell Date: Wed, 17 Jun 2020 19:38:48 -0400 Subject: Backport: Encapsulate GetProcAddress in a way that avoids undefined and conditionally defined behaviour. Backport of changeset 8243:09cccd3f867b. --- win32/PlatWin.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'win32/PlatWin.h') diff --git a/win32/PlatWin.h b/win32/PlatWin.h index 3f1152522..be99b8e55 100644 --- a/win32/PlatWin.h +++ b/win32/PlatWin.h @@ -34,6 +34,20 @@ inline HWND HwndFromWindow(const Window &w) noexcept { void *PointerFromWindow(HWND hWnd) noexcept; void SetWindowPointer(HWND hWnd, void *ptr) noexcept; +/// Find a function in a DLL and convert to a function pointer. +/// This avoids undefined and conditionally defined behaviour. +template +T DLLFunction(HMODULE hModule, LPCSTR lpProcName) noexcept { + if (!hModule) { + return nullptr; + } + FARPROC function = ::GetProcAddress(hModule, lpProcName); + static_assert(sizeof(T) == sizeof(function), "size mismatch"); + T fp; + memcpy(&fp, &function, sizeof(T)); + return fp; +} + #if defined(USE_D2D) extern bool LoadD2D(); extern ID2D1Factory *pD2DFactory; -- cgit v1.2.3