aboutsummaryrefslogtreecommitdiffhomepage
path: root/win32/PlatWin.h
diff options
context:
space:
mode:
authormitchell <unknown>2020-06-17 19:38:48 -0400
committermitchell <unknown>2020-06-17 19:38:48 -0400
commitefaefe9e490c1d3cf5071ab8093abb59526a5002 (patch)
tree7ef412f6c4dc353cc5d931a299f49b8601c5ec09 /win32/PlatWin.h
parent9b9c12efef67a4f327f298e36a75b19d97a4735a (diff)
downloadscintilla-mirror-efaefe9e490c1d3cf5071ab8093abb59526a5002.tar.gz
Backport: Encapsulate GetProcAddress in a way that avoids undefined and conditionally defined behaviour.
Backport of changeset 8243:09cccd3f867b.
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 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<typename T>
+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;