From dabeb6e2ba40f9b1141ff4e8a718542b66114d4b Mon Sep 17 00:00:00 2001 From: Bryan Roe Date: Thu, 29 Aug 2019 09:56:12 -0700 Subject: [PATCH] Updated Windows, so GetHostNameW is dynamically bound, so that it can fallback to gethostname on platforms that lack support, such as Windows Server 2008 R2 --- meshcore/agentcore.c | 29 ++++++++++++++++++++++++++--- meshcore/agentcore.h | 1 + 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/meshcore/agentcore.c b/meshcore/agentcore.c index da4d7cd..51d1ef4 100644 --- a/meshcore/agentcore.c +++ b/meshcore/agentcore.c @@ -3681,10 +3681,33 @@ MeshAgentHostContainer* MeshAgent_Create(MeshCommand_AuthInfo_CapabilitiesMask c retVal->capabilities = capabilities | MeshCommand_AuthInfo_CapabilitiesMask_CONSOLE | MeshCommand_AuthInfo_CapabilitiesMask_JAVASCRIPT; #ifdef WIN32 - WCHAR whostname[MAX_PATH]; - if (GetHostNameW(whostname, MAX_PATH) == 0) + + HMODULE wsCORE = LoadLibraryExA((LPCSTR)"Ws2_32.dll", NULL, LOAD_LIBRARY_SEARCH_SYSTEM32); + GetHostNameWFunc ghnw = NULL; + if (wsCORE != NULL) { - WideCharToMultiByte(CP_UTF8, 0, whostname, -1, retVal->hostname, (int)sizeof(retVal->hostname), NULL, NULL); + if ((ghnw = (GetHostNameWFunc)GetProcAddress(wsCORE, (LPCSTR)"GetHostNameW")) == NULL) + { + FreeLibrary(wsCORE); + wsCORE = NULL; + } + } + if (ghnw != NULL) + { + WCHAR whostname[MAX_PATH]; + if (ghnw(whostname, MAX_PATH) == 0) + { + WideCharToMultiByte(CP_UTF8, 0, whostname, -1, retVal->hostname, (int)sizeof(retVal->hostname), NULL, NULL); + } + } + else + { + gethostname(retVal->hostname, (int)sizeof(retVal->hostname)); + } + if (wsCORE != NULL) + { + FreeLibrary(wsCORE); + wsCORE = NULL; } #else gethostname(retVal->hostname, (int)sizeof(retVal->hostname)); diff --git a/meshcore/agentcore.h b/meshcore/agentcore.h index d284b9c..b193d62 100644 --- a/meshcore/agentcore.h +++ b/meshcore/agentcore.h @@ -141,6 +141,7 @@ extern char* MeshAgentHost_BatteryInfo_STRINGS[]; #ifdef WIN32 typedef HRESULT (__stdcall *DpiAwarenessFunc)(PROCESS_DPI_AWARENESS); +typedef int (WSAAPI *GetHostNameWFunc)(PWSTR name, int namelen); #endif typedef struct MeshAgentHostContainer