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",
19 const char *const dll;
20 const char *const function;
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 static rettype (WINAPI *function)(__VA_ARGS__)
32 * Loads a function from a DLL (once-only).
33 * Returns non-NULL function pointer on success.
34 * Returns NULL + errno == ENOSYS on failure.
35 * This function is not thread-safe.
37 #define INIT_PROC_ADDR(function) \
38 (function = get_proc_addr(&proc_addr_##function))
40 static inline void *get_proc_addr(struct proc_addr *proc)
42 /* only do this once */
43 if (!proc->initialized) {
45 proc->initialized = 1;
46 hnd = LoadLibraryExA(proc->dll, NULL,
47 LOAD_LIBRARY_SEARCH_SYSTEM32);
49 proc->pfunction = GetProcAddress(hnd, proc->function);
51 /* set ENOSYS if DLL or function was not found */
54 return proc->pfunction;