Skip to content

Commit 8c4c76b

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 1c90ee6 commit 8c4c76b

File tree

3 files changed

+50
-22
lines changed

3 files changed

+50
-22
lines changed

compat/mingw.c

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1978,24 +1978,18 @@ int mingw_raise(int sig)
19781978

19791979
int link(const char *oldpath, const char *newpath)
19801980
{
1981-
typedef BOOL (WINAPI *T)(LPCWSTR, LPCWSTR, LPSECURITY_ATTRIBUTES);
1982-
static T create_hard_link = NULL;
1981+
DECLARE_PROC_ADDR(kernel32.dll, BOOL, CreateHardLinkW,
1982+
LPCWSTR, LPCWSTR, LPSECURITY_ATTRIBUTES);
19831983
wchar_t woldpath[MAX_LONG_PATH], wnewpath[MAX_LONG_PATH];
1984+
1985+
if (!INIT_PROC_ADDR(CreateHardLinkW))
1986+
return -1;
1987+
19841988
if (xutftowcs_long_path(woldpath, oldpath) < 0 ||
19851989
xutftowcs_long_path(wnewpath, newpath) < 0)
19861990
return -1;
19871991

1988-
if (!create_hard_link) {
1989-
create_hard_link = (T) GetProcAddress(
1990-
GetModuleHandle("kernel32.dll"), "CreateHardLinkW");
1991-
if (!create_hard_link)
1992-
create_hard_link = (T)-1;
1993-
}
1994-
if (create_hard_link == (T)-1) {
1995-
errno = ENOSYS;
1996-
return -1;
1997-
}
1998-
if (!create_hard_link(wnewpath, woldpath, NULL)) {
1992+
if (!CreateHardLinkW(wnewpath, woldpath, NULL)) {
19991993
errno = err_win_to_posix(GetLastError());
20001994
return -1;
20011995
}

compat/win32.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,42 @@ 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+
HANDLE hnd;
70+
proc->initialized = 1;
71+
hnd = LoadLibraryA(proc->dll);
72+
if (hnd)
73+
proc->pfunction = GetProcAddress(hnd, proc->function);
74+
}
75+
/* set ENOSYS if DLL or function was not found */
76+
if (!proc->pfunction)
77+
errno = ENOSYS;
78+
return proc->pfunction;
79+
}
80+
4381
#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)