aboutsummaryrefslogtreecommitdiffhomepage
path: root/win32/PlatWin.h
diff options
context:
space:
mode:
authorNeil <nyamatongwe@gmail.com>2020-05-19 15:32:06 +1000
committerNeil <nyamatongwe@gmail.com>2020-05-19 15:32:06 +1000
commit05498786fcc7fd6ef18be4b842df16769f4a29cd (patch)
treecfab70c3b0a28b0faddebca6046e1f87ab378b0a /win32/PlatWin.h
parent24b9ab288ebb48fb4cd7efc0fd6a409f50964664 (diff)
downloadscintilla-mirror-05498786fcc7fd6ef18be4b842df16769f4a29cd.tar.gz
Encapsulate GetProcAddress in a way that avoids undefined and conditionally
defined behaviour.
Diffstat (limited to 'win32/PlatWin.h')
-rw-r--r--win32/PlatWin.h14
1 files changed, 14 insertions, 0 deletions
diff --git a/win32/PlatWin.h b/win32/PlatWin.h
index d7e544da6..9d17dff69 100644
--- a/win32/PlatWin.h
+++ b/win32/PlatWin.h
@@ -38,6 +38,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<typename T>
+T DLLFunction(HMODULE hModule, LPCSTR lpProcName) noexcept {
+ if (!hModule) {
+ return nullptr;
+ }
+ FARPROC function = ::GetProcAddress(hModule, lpProcName);
+ static_assert(sizeof(T) == sizeof(function));
+ T fp;
+ memcpy(&fp, &function, sizeof(T));
+ return fp;
+}
+
#if defined(USE_D2D)
extern bool LoadD2D();
extern ID2D1Factory *pD2DFactory;