Hi, Johannes Schindelin wrote: > Dynamic loading of DLL functions is duplicated in several places in Git > for Windows' source code. > > This patch adds a pair of macros to simplify the process: the > DECLARE_PROC_ADDR(<dll>, <return-type>, <function-name>, > ...<function-parameter-types>...) macro to be used at the beginning of a > code block, and the INIT_PROC_ADDR(<function-name>) macro to call before > using the declared function. The return value of the INIT_PROC_ADDR() > call has to be checked; If it is NULL, the function was not found in the > specified DLL. > > Example: > > DECLARE_PROC_ADDR(kernel32.dll, BOOL, CreateHardLinkW, > LPCWSTR, LPCWSTR, LPSECURITY_ATTRIBUTES); > > if (!INIT_PROC_ADDR(CreateHardLinkW)) > return error("Could not find CreateHardLinkW() function"; > > if (!CreateHardLinkW(source, target, NULL)) > return error("could not create hardlink from %S to %S", > source, target); > return 0; nit: whitespace is a bit strange here (mixture of tabs and spaces). Could this example go near the top of the header instead? That way, it's easier for people reading the header to see how to use it. > Signed-off-by: Karsten Blees <blees@xxxxxxx> > Signed-off-by: Johannes Schindelin <johannes.schindelin@xxxxxx> > --- Just curious: what was Karsten's contribution? (I ask mostly because I'm interested in kinds of collaboration git metadata is failing to capture correctly --- e.g. pair programming.) > So far, there are no users (except in Git for Windows). Ben > promised to make use of it in his fsmonitor patch series. > > compat/win32/lazyload.h | 44 ++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 44 insertions(+) > create mode 100644 compat/win32/lazyload.h Are any of the Git for Windows users something that could go upstream along with this patch? That would help illustrate what a good caller looks like, which should help with reviewing future patches that use this code. > --- /dev/null > +++ b/compat/win32/lazyload.h > @@ -0,0 +1,44 @@ [...] > +/* Declares a function to be loaded dynamically from a DLL. */ > +#define DECLARE_PROC_ADDR(dll, rettype, function, ...) \ > + static struct proc_addr proc_addr_##function = \ > + { #dll, #function, NULL, 0 }; \ > + static rettype (WINAPI *function)(__VA_ARGS__) > + > +/* > + * Loads a function from a DLL (once-only). > + * Returns non-NULL function pointer on success. > + * Returns NULL + errno == ENOSYS on failure. > + */ > +#define INIT_PROC_ADDR(function) \ > + (function = get_proc_addr(&proc_addr_##function)) Probably worth mentioning in the doc comment that this is not thread safe, so a caller that wants to lazy-init in a threaded context is responsible for doing their own locking. > + > +static inline void *get_proc_addr(struct proc_addr *proc) > +{ > + /* only do this once */ > + if (!proc->initialized) { > + HANDLE hnd; > + proc->initialized = 1; > + hnd = LoadLibraryExA(proc->dll, NULL, > + LOAD_LIBRARY_SEARCH_SYSTEM32); > + if (hnd) > + proc->pfunction = GetProcAddress(hnd, proc->function); > + } > + /* set ENOSYS if DLL or function was not found */ > + if (!proc->pfunction) > + errno = ENOSYS; > + return proc->pfunction; > +} strerror(ENOSYS) is "Function not implemented". Cute. Reviewed-by: Jonathan Nieder <jrnieder@xxxxxxxxx> Thanks, Jonathan