Skip to content

Commit e19d831

Browse files
kbleesdscho
authored andcommitted
Win32: simplify loading of DLL functions
Dynamic loading of DLL functions is duplicated in several places. Add a set of macros to simplify the process. Signed-off-by: Karsten Blees <[email protected]>
1 parent 95408ab commit e19d831

File tree

3 files changed

+53
-33
lines changed

3 files changed

+53
-33
lines changed

compat/mingw.c

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1975,23 +1975,16 @@ static const char *make_backslash_path(const char *path)
19751975
void mingw_open_html(const char *unixpath)
19761976
{
19771977
const char *htmlpath = make_backslash_path(unixpath);
1978-
typedef HINSTANCE (WINAPI *T)(HWND, const char *,
1979-
const char *, const char *, const char *, INT);
1980-
T ShellExecute;
1981-
HMODULE shell32;
19821978
int r;
1979+
DECLARE_PROC_ADDR(shell32.dll, HINSTANCE, ShellExecuteA,
1980+
HWND, LPCSTR, LPCSTR, LPCSTR, LPCSTR, INT);
19831981

1984-
shell32 = LoadLibrary("shell32.dll");
1985-
if (!shell32)
1982+
if (!INIT_PROC_ADDR(ShellExecuteA))
19861983
die("cannot load shell32.dll");
1987-
ShellExecute = (T)GetProcAddress(shell32, "ShellExecuteA");
1988-
if (!ShellExecute)
1989-
die("cannot run browser");
19901984

19911985
printf("Launching default browser to display HTML ...\n");
1992-
r = HCAST(int, ShellExecute(NULL, "open", htmlpath,
1986+
r = HCAST(int, ShellExecuteA(NULL, "open", htmlpath,
19931987
NULL, "\\", SW_SHOWNORMAL));
1994-
FreeLibrary(shell32);
19951988
/* see the MSDN documentation referring to the result codes here */
19961989
if (r <= 32) {
19971990
die("failed to launch browser for %.*s", MAX_PATH, unixpath);
@@ -2000,24 +1993,18 @@ void mingw_open_html(const char *unixpath)
20001993

20011994
int link(const char *oldpath, const char *newpath)
20021995
{
2003-
typedef BOOL (WINAPI *T)(LPCWSTR, LPCWSTR, LPSECURITY_ATTRIBUTES);
2004-
static T create_hard_link = NULL;
1996+
DECLARE_PROC_ADDR(kernel32.dll, BOOL, CreateHardLinkW,
1997+
LPCWSTR, LPCWSTR, LPSECURITY_ATTRIBUTES);
20051998
wchar_t woldpath[MAX_LONG_PATH], wnewpath[MAX_LONG_PATH];
1999+
2000+
if (!INIT_PROC_ADDR(CreateHardLinkW))
2001+
return -1;
2002+
20062003
if (xutftowcs_long_path(woldpath, oldpath) < 0 ||
20072004
xutftowcs_long_path(wnewpath, newpath) < 0)
20082005
return -1;
20092006

2010-
if (!create_hard_link) {
2011-
create_hard_link = (T) GetProcAddress(
2012-
GetModuleHandle("kernel32.dll"), "CreateHardLinkW");
2013-
if (!create_hard_link)
2014-
create_hard_link = (T)-1;
2015-
}
2016-
if (create_hard_link == (T)-1) {
2017-
errno = ENOSYS;
2018-
return -1;
2019-
}
2020-
if (!create_hard_link(wnewpath, woldpath, NULL)) {
2007+
if (!CreateHardLinkW(wnewpath, woldpath, NULL)) {
20212008
errno = err_win_to_posix(GetLastError());
20222009
return -1;
20232010
}

compat/win32.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,41 @@ static inline int get_file_attr(const char *fname, WIN32_FILE_ATTRIBUTE_DATA *fd
4040
}
4141
}
4242

43+
/* simplify loading of DLL functions */
44+
45+
struct proc_addr {
46+
const char *const dll;
47+
const char *const function;
48+
FARPROC pfunction;
49+
unsigned initialized : 1;
50+
};
51+
52+
/* Declares a function to be loaded dynamically from a DLL. */
53+
#define DECLARE_PROC_ADDR(dll, rettype, function, ...) \
54+
static struct proc_addr proc_addr_##function = \
55+
{ #dll, #function, NULL, 0 }; \
56+
static rettype (WINAPI *function)(__VA_ARGS__)
57+
58+
/*
59+
* Loads a function from a DLL (once-only).
60+
* Returns non-NULL function pointer on success.
61+
* Returns NULL + errno == ENOSYS on failure.
62+
*/
63+
#define INIT_PROC_ADDR(function) (function = get_proc_addr(&proc_addr_##function))
64+
65+
static inline void *get_proc_addr(struct proc_addr *proc)
66+
{
67+
/* only do this once */
68+
if (!proc->initialized) {
69+
proc->initialized = 1;
70+
HANDLE hnd = LoadLibraryA(proc->dll);
71+
if (hnd)
72+
proc->pfunction = GetProcAddress(hnd, proc->function);
73+
}
74+
/* set ENOSYS if DLL or function was not found */
75+
if (!proc->pfunction)
76+
errno = ENOSYS;
77+
return proc->pfunction;
78+
}
79+
4380
#endif

compat/winansi.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "../git-compat-util.h"
77
#include <wingdi.h>
88
#include <winreg.h>
9+
#include "win32.h"
910

1011
/*
1112
ANSI codes used by git: m, K
@@ -35,26 +36,21 @@ typedef struct _CONSOLE_FONT_INFOEX {
3536
#endif
3637
#endif
3738

38-
typedef BOOL (WINAPI *PGETCURRENTCONSOLEFONTEX)(HANDLE, BOOL,
39-
PCONSOLE_FONT_INFOEX);
40-
4139
static void warn_if_raster_font(void)
4240
{
4341
DWORD fontFamily = 0;
44-
PGETCURRENTCONSOLEFONTEX pGetCurrentConsoleFontEx;
42+
DECLARE_PROC_ADDR(kernel32.dll, BOOL, GetCurrentConsoleFontEx,
43+
HANDLE, BOOL, PCONSOLE_FONT_INFOEX);
4544

4645
/* don't bother if output was ascii only */
4746
if (!non_ascii_used)
4847
return;
4948

5049
/* GetCurrentConsoleFontEx is available since Vista */
51-
pGetCurrentConsoleFontEx = (PGETCURRENTCONSOLEFONTEX) GetProcAddress(
52-
GetModuleHandle("kernel32.dll"),
53-
"GetCurrentConsoleFontEx");
54-
if (pGetCurrentConsoleFontEx) {
50+
if (INIT_PROC_ADDR(GetCurrentConsoleFontEx)) {
5551
CONSOLE_FONT_INFOEX cfi;
5652
cfi.cbSize = sizeof(cfi);
57-
if (pGetCurrentConsoleFontEx(console, 0, &cfi))
53+
if (GetCurrentConsoleFontEx(console, 0, &cfi))
5854
fontFamily = cfi.FontFamily;
5955
} else {
6056
/* pre-Vista: check default console font in registry */

0 commit comments

Comments
 (0)