mirror of
https://github.com/git/git.git
synced 2026-01-27 02:48:32 +00:00
Win32: simplify loading of DLL functions
Dynamic loading of DLL functions is duplicated in several places. Add a set of macros to simplify the process. Signed-off-by: Karsten Blees <blees@dcon.de>
This commit is contained in:
committed by
Johannes Schindelin
parent
0d3fadb798
commit
9250b01892
@@ -2088,24 +2088,18 @@ int mingw_raise(int sig)
|
||||
|
||||
int link(const char *oldpath, const char *newpath)
|
||||
{
|
||||
typedef BOOL (WINAPI *T)(LPCWSTR, LPCWSTR, LPSECURITY_ATTRIBUTES);
|
||||
static T create_hard_link = NULL;
|
||||
DECLARE_PROC_ADDR(kernel32.dll, BOOL, CreateHardLinkW,
|
||||
LPCWSTR, LPCWSTR, LPSECURITY_ATTRIBUTES);
|
||||
wchar_t woldpath[MAX_LONG_PATH], wnewpath[MAX_LONG_PATH];
|
||||
|
||||
if (!INIT_PROC_ADDR(CreateHardLinkW))
|
||||
return -1;
|
||||
|
||||
if (xutftowcs_long_path(woldpath, oldpath) < 0 ||
|
||||
xutftowcs_long_path(wnewpath, newpath) < 0)
|
||||
return -1;
|
||||
|
||||
if (!create_hard_link) {
|
||||
create_hard_link = (T) GetProcAddress(
|
||||
GetModuleHandle("kernel32.dll"), "CreateHardLinkW");
|
||||
if (!create_hard_link)
|
||||
create_hard_link = (T)-1;
|
||||
}
|
||||
if (create_hard_link == (T)-1) {
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
if (!create_hard_link(wnewpath, woldpath, NULL)) {
|
||||
if (!CreateHardLinkW(wnewpath, woldpath, NULL)) {
|
||||
errno = err_win_to_posix(GetLastError());
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -40,4 +40,42 @@ static inline int get_file_attr(const char *fname, WIN32_FILE_ATTRIBUTE_DATA *fd
|
||||
}
|
||||
}
|
||||
|
||||
/* simplify loading of DLL functions */
|
||||
|
||||
struct proc_addr {
|
||||
const char *const dll;
|
||||
const char *const function;
|
||||
FARPROC pfunction;
|
||||
unsigned initialized : 1;
|
||||
};
|
||||
|
||||
/* 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))
|
||||
|
||||
static inline void *get_proc_addr(struct proc_addr *proc)
|
||||
{
|
||||
/* only do this once */
|
||||
if (!proc->initialized) {
|
||||
HANDLE hnd;
|
||||
proc->initialized = 1;
|
||||
hnd = LoadLibraryA(proc->dll);
|
||||
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;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include "../git-compat-util.h"
|
||||
#include <wingdi.h>
|
||||
#include <winreg.h>
|
||||
#include "win32.h"
|
||||
|
||||
/*
|
||||
ANSI codes used by git: m, K
|
||||
@@ -35,26 +36,21 @@ typedef struct _CONSOLE_FONT_INFOEX {
|
||||
#endif
|
||||
#endif
|
||||
|
||||
typedef BOOL (WINAPI *PGETCURRENTCONSOLEFONTEX)(HANDLE, BOOL,
|
||||
PCONSOLE_FONT_INFOEX);
|
||||
|
||||
static void warn_if_raster_font(void)
|
||||
{
|
||||
DWORD fontFamily = 0;
|
||||
PGETCURRENTCONSOLEFONTEX pGetCurrentConsoleFontEx;
|
||||
DECLARE_PROC_ADDR(kernel32.dll, BOOL, GetCurrentConsoleFontEx,
|
||||
HANDLE, BOOL, PCONSOLE_FONT_INFOEX);
|
||||
|
||||
/* don't bother if output was ascii only */
|
||||
if (!non_ascii_used)
|
||||
return;
|
||||
|
||||
/* GetCurrentConsoleFontEx is available since Vista */
|
||||
pGetCurrentConsoleFontEx = (PGETCURRENTCONSOLEFONTEX) GetProcAddress(
|
||||
GetModuleHandle("kernel32.dll"),
|
||||
"GetCurrentConsoleFontEx");
|
||||
if (pGetCurrentConsoleFontEx) {
|
||||
if (INIT_PROC_ADDR(GetCurrentConsoleFontEx)) {
|
||||
CONSOLE_FONT_INFOEX cfi;
|
||||
cfi.cbSize = sizeof(cfi);
|
||||
if (pGetCurrentConsoleFontEx(console, 0, &cfi))
|
||||
if (GetCurrentConsoleFontEx(console, 0, &cfi))
|
||||
fontFamily = cfi.FontFamily;
|
||||
} else {
|
||||
/* pre-Vista: check default console font in registry */
|
||||
|
||||
Reference in New Issue
Block a user