aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2022-07-15 00:39:07 +0200
committerJohn Ankarström <john@ankarstrom.se>2022-07-15 00:39:07 +0200
commit0e22414d042207269f916298b236f1be341ddeea (patch)
treec7f57ee6882eda6d9a59712f9da753120086bc9e
parent31c03c4ddba65199c8c444c75b684b6cfaa629d9 (diff)
downloadEpisodeBrowser-0e22414d042207269f916298b236f1be341ddeea.tar.gz
Add wrapper for LoadLibrary, FreeLibrary.
-rw-r--r--c/common.cpp26
-rw-r--r--c/defs.h16
-rw-r--r--c/main.cpp36
3 files changed, 56 insertions, 22 deletions
diff --git a/c/common.cpp b/c/common.cpp
index 63adb53..93f97eb 100644
--- a/c/common.cpp
+++ b/c/common.cpp
@@ -1,3 +1,4 @@
+#include <memory>
#include <windows.h>
#include <SWI-Prolog.h>
@@ -27,3 +28,28 @@ TszFromSz(const char *sz, int iCp)
return tsz;
}
+
+Library::Library(const TCHAR *tszLibrary)
+{
+ m_hModule = LoadLibrary(tszLibrary);
+}
+
+Library::~Library()
+{
+ if (m_hModule)
+ FreeLibrary(m_hModule);
+}
+
+FARPROC
+Library::GetProcAddress(const char *szProc)
+{
+ return m_hModule? ::GetProcAddress(m_hModule, szProc): NULL;
+}
+
+std::unique_ptr<Library>
+Library::Load(const TCHAR *tszLibrary)
+{
+ auto upLib = std::make_unique<Library>(tszLibrary);
+ if (!upLib->m_hModule) upLib = nullptr;
+ return upLib;
+}
diff --git a/c/defs.h b/c/defs.h
index c557320..c83dd8c 100644
--- a/c/defs.h
+++ b/c/defs.h
@@ -1,12 +1,22 @@
#ifndef DEFS_H
#define DEFS_H
+#include <memory>
#include <windows.h>
#include <commctrl.h>
#include <SWI-Prolog.h>
/* common.cpp */
TCHAR *TszFromSz(const char *, int);
+class Library {
+private:
+ HMODULE m_hModule;
+public:
+ static std::unique_ptr<Library> Load(const TCHAR *);
+ Library(const TCHAR *);
+ ~Library();
+ FARPROC GetProcAddress(const char *);
+};
/* main.cpp */
void UpdateLayout();
@@ -61,6 +71,12 @@ int Plp(term_t, const char *, ...);
int Plg(term_t, const char *, ...);
/* defs.h */
+#ifdef UNICODE
+#define WA "W"
+#else
+#define WA "A"
+#endif
+
#define DLVSIKEY 0
#define DLVSIVALUE 1
#define ELVSIEPISODE 0
diff --git a/c/main.cpp b/c/main.cpp
index f2172ba..718ed85 100644
--- a/c/main.cpp
+++ b/c/main.cpp
@@ -3,8 +3,6 @@
#include <uxtheme.h>
#include <SWI-Prolog.h>
-#include <cstdio>
-
#include "resource.h"
#include "defs.h"
@@ -36,26 +34,23 @@ int WINAPI
WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, INT nCmdShow)
{
- HMODULE hModule;
LPTSTR tszErr;
/* Set constant values. */
- hModule = LoadLibrary(TEXT("uxtheme.dll"));
- if (hModule && GetProcAddress(hModule, "SetWindowTheme")) {
- g_bThemes = 1;
- FreeLibrary(hModule);
- }
+ if (auto upLib = Library::Load(TEXT("uxtheme.dll")))
+ if (upLib->GetProcAddress("SetWindowTheme")) {
+ g_bThemes = 1;
+ }
g_cxVScroll = GetSystemMetrics(SM_CXVSCROLL);
/* Setup fonts. */
- hModule = LoadLibrary(TEXT("User32.dll"));
- if (hModule && GetProcAddress(hModule, "SystemParametersInfoW")) {
- NONCLIENTMETRICS m;
- m.cbSize = sizeof(NONCLIENTMETRICS);
- SystemParametersInfo(SPI_GETNONCLIENTMETRICS,
- sizeof(NONCLIENTMETRICS), &m, 0);
- g_hfNormal = CreateFontIndirect(&m.lfMessageFont);
- FreeLibrary(hModule);
+ if (auto upLib = Library::Load(TEXT("User32.dll"))) {
+ if (upLib->GetProcAddress("SystemParametersInfo" WA)) {
+ NONCLIENTMETRICS m;
+ m.cbSize = sizeof(NONCLIENTMETRICS);
+ SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &m, 0);
+ g_hfNormal = CreateFontIndirect(&m.lfMessageFont);
+ }
} else
g_hfNormal = static_cast<HFONT>(GetStockObject(DEFAULT_GUI_FONT));
@@ -147,12 +142,9 @@ CBTProc(int nCode, WPARAM wParam, LPARAM lParam)
g_hWnd = (HWND)wParam;
/* Get DPI. */
- UINT (*GetDpiForWindow)(HWND);
- HMODULE hModule = LoadLibrary(TEXT("User32.dll"));
- if (hModule && (GetDpiForWindow = (UINT (*)(HWND))GetProcAddress(hModule, "GetDpiForWindow"))) {
- g_iDPI = GetDpiForWindow(g_hWnd);
- FreeLibrary(hModule);
- }
+ if (auto upLib = Library::Load(TEXT("User32.dll")))
+ if (auto GetDpiForWindow = (UINT (*)(HWND))upLib->GetProcAddress("GetDpiForWindow"))
+ g_iDPI = GetDpiForWindow(g_hWnd);
/* Create child windows. */
g_lpDlv = new DataListView;