Win32: simplify loading of DLL functions
commitdb2f7c48cb30f8fdcb2933aa7d9890b34e0c4e94
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Mon, 25 Sep 2017 16:06:24 +0000 (25 18:06 +0200)
committerJunio C Hamano <gitster@pobox.com>
Tue, 26 Sep 2017 02:02:49 +0000 (26 11:02 +0900)
tree10fafc47a8017524f7f106c176329b15598db2ae
parent94c9fd268d4287f6fbfef84793288479905a7e48
Win32: simplify loading of DLL functions

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;

Signed-off-by: Karsten Blees <blees@dcon.de>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
compat/win32/lazyload.h [new file with mode: 0644]