Skip to content

Commit 7766237

Browse files
committed
mingw: factor out lazy-loading functions from .dll files
This code is very useful and needed also in compat/poll/poll.c, so let's factor out those macros and functions into their own header file. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 7db5cb8 commit 7766237

File tree

2 files changed

+44
-38
lines changed

2 files changed

+44
-38
lines changed

compat/win32.h

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <windows.h>
77
#endif
88

9+
#include "compat/win32/lazyload.h"
10+
911
static inline int file_attr_to_st_mode (DWORD attr, DWORD tag)
1012
{
1113
int fMode = S_IREAD;
@@ -40,42 +42,4 @@ static inline int get_file_attr(const char *fname, WIN32_FILE_ATTRIBUTE_DATA *fd
4042
}
4143
}
4244

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-
8145
#endif

compat/win32/lazyload.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#ifndef LAZYLOAD_H
2+
#define LAZYLOAD_H
3+
4+
/* simplify loading of DLL functions */
5+
6+
struct proc_addr {
7+
const char *const dll;
8+
const char *const function;
9+
FARPROC pfunction;
10+
unsigned initialized : 1;
11+
};
12+
13+
/* Declares a function to be loaded dynamically from a DLL. */
14+
#define DECLARE_PROC_ADDR(dll, rettype, function, ...) \
15+
static struct proc_addr proc_addr_##function = \
16+
{ #dll, #function, NULL, 0 }; \
17+
static rettype (WINAPI *function)(__VA_ARGS__)
18+
19+
/*
20+
* Loads a function from a DLL (once-only).
21+
* Returns non-NULL function pointer on success.
22+
* Returns NULL + errno == ENOSYS on failure.
23+
*/
24+
#define INIT_PROC_ADDR(function) (function = get_proc_addr(&proc_addr_##function))
25+
26+
static inline void *get_proc_addr(struct proc_addr *proc)
27+
{
28+
/* only do this once */
29+
if (!proc->initialized) {
30+
HANDLE hnd;
31+
proc->initialized = 1;
32+
hnd = LoadLibraryA(proc->dll);
33+
if (hnd)
34+
proc->pfunction = GetProcAddress(hnd, proc->function);
35+
}
36+
/* set ENOSYS if DLL or function was not found */
37+
if (!proc->pfunction)
38+
errno = ENOSYS;
39+
return proc->pfunction;
40+
}
41+
42+
#endif

0 commit comments

Comments
 (0)