From 26b2e5dbb6301756644d6e4cf6ca9c49c00513d3 Mon Sep 17 00:00:00 2001 From: Nicolas Noble Date: Mon, 1 Nov 2021 12:09:59 -0700 Subject: [PATCH] win: fix build for mingw32 (#3340) Commit e9c524aa from pull request https://github.com/libuv/libuv/pull/3149 introduced a hard dependency on `GetHostnameW`, which isn't declared yet in mingw distributions (see https://github.com/msys2/MINGW-packages/issues/9667). This prevents the current version of libuv from building on many mingw distributions, until such time the next version of mingw is released with the correct definition for `GetHostnameW`, preventing a lot of projects depending on libuv from building on mingw properly, as not all distributions will update to head immediately anyway. Instead of waiting, let's find the definition ourselves using `GetProcAddress` and use the pointer instead. PR-URL: https://github.com/libuv/libuv/pull/3340 --- src/win/util.c | 5 ++++- src/win/winapi.c | 10 ++++++++++ src/win/winapi.h | 7 +++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/win/util.c b/src/win/util.c index 88602c7e..33e874ac 100644 --- a/src/win/util.c +++ b/src/win/util.c @@ -1674,7 +1674,10 @@ int uv_os_gethostname(char* buffer, size_t* size) { uv__once_init(); /* Initialize winsock */ - if (GetHostNameW(buf, UV_MAXHOSTNAMESIZE) != 0) + if (pGetHostNameW == NULL) + return UV_ENOSYS; + + if (pGetHostNameW(buf, UV_MAXHOSTNAMESIZE) != 0) return uv_translate_sys_error(WSAGetLastError()); convert_result = uv__convert_utf16_to_utf8(buf, -1, &utf8_str); diff --git a/src/win/winapi.c b/src/win/winapi.c index bb86ec8c..bf306cd8 100644 --- a/src/win/winapi.c +++ b/src/win/winapi.c @@ -45,12 +45,15 @@ sPowerRegisterSuspendResumeNotification pPowerRegisterSuspendResumeNotification; /* User32.dll function pointer */ sSetWinEventHook pSetWinEventHook; +/* ws2_32.dll function pointer */ +uv_sGetHostNameW pGetHostNameW; void uv_winapi_init(void) { HMODULE ntdll_module; HMODULE powrprof_module; HMODULE user32_module; HMODULE kernel32_module; + HMODULE ws2_32_module; ntdll_module = GetModuleHandleA("ntdll.dll"); if (ntdll_module == NULL) { @@ -134,4 +137,11 @@ void uv_winapi_init(void) { pSetWinEventHook = (sSetWinEventHook) GetProcAddress(user32_module, "SetWinEventHook"); } + + ws2_32_module = LoadLibraryA("ws2_32.dll"); + if (ws2_32_module != NULL) { + pGetHostNameW = (uv_sGetHostNameW) GetProcAddress( + ws2_32_module, + "GetHostNameW"); + } } diff --git a/src/win/winapi.h b/src/win/winapi.h index 0b66b563..d380bda4 100644 --- a/src/win/winapi.h +++ b/src/win/winapi.h @@ -4759,4 +4759,11 @@ extern sPowerRegisterSuspendResumeNotification pPowerRegisterSuspendResumeNotifi /* User32.dll function pointer */ extern sSetWinEventHook pSetWinEventHook; +/* ws2_32.dll function pointer */ +/* mingw doesn't have this definition, so let's declare it here locally */ +typedef int (WINAPI *uv_sGetHostNameW) + (PWSTR, + int); +extern uv_sGetHostNameW pGetHostNameW; + #endif /* UV_WIN_WINAPI_H_ */