lazyload.h: fix warnings about mismatching function pointer types
[git/debian.git] / compat / win32 / lazyload.h
blob121ee24ed2d96e998fbaeccf3785e05dafb44398
1 #ifndef LAZYLOAD_H
2 #define LAZYLOAD_H
4 /*
5 * A pair of macros to simplify loading of DLL functions. Example:
7 * DECLARE_PROC_ADDR(kernel32.dll, BOOL, CreateHardLinkW,
8 * LPCWSTR, LPCWSTR, LPSECURITY_ATTRIBUTES);
10 * if (!INIT_PROC_ADDR(CreateHardLinkW))
11 * return error("Could not find CreateHardLinkW() function";
13 * if (!CreateHardLinkW(source, target, NULL))
14 * return error("could not create hardlink from %S to %S",
15 * source, target);
18 struct proc_addr {
19 const char *const dll;
20 const char *const function;
21 FARPROC pfunction;
22 unsigned initialized : 1;
25 /* Declares a function to be loaded dynamically from a DLL. */
26 #define DECLARE_PROC_ADDR(dll, rettype, function, ...) \
27 static struct proc_addr proc_addr_##function = \
28 { #dll, #function, NULL, 0 }; \
29 typedef rettype (WINAPI *proc_type_##function)(__VA_ARGS__); \
30 static proc_type_##function function
33 * Loads a function from a DLL (once-only).
34 * Returns non-NULL function pointer on success.
35 * Returns NULL + errno == ENOSYS on failure.
36 * This function is not thread-safe.
38 #define INIT_PROC_ADDR(function) \
39 (function = (proc_type_##function)get_proc_addr(&proc_addr_##function))
41 static inline FARPROC get_proc_addr(struct proc_addr *proc)
43 /* only do this once */
44 if (!proc->initialized) {
45 HANDLE hnd;
46 proc->initialized = 1;
47 hnd = LoadLibraryExA(proc->dll, NULL,
48 LOAD_LIBRARY_SEARCH_SYSTEM32);
49 if (hnd)
50 proc->pfunction = GetProcAddress(hnd, proc->function);
52 /* set ENOSYS if DLL or function was not found */
53 if (!proc->pfunction)
54 errno = ENOSYS;
55 return proc->pfunction;
58 #endif