Skip to contentSkip to conversation

Windows: avoid static dependency on advapi32.dll

PATCH In review Michael Lukashov 2 participants 2 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Windows: avoid static dependency on advapi32.dll

This DLL is used to get default user name. By looking up the only function that we need at runtime, we can avoid the startup costs of this DLL.

---
 compat/mingw.c |   31 ++++++++++++++++++++++++++++++-
 1 files changed, 30 insertions(+), 1 deletions(-)
compat/mingw.c +30 -1
index ab65f77..6ce398c 100644
@@ -1176,13 +1176,42 @@ int mingw_getpagesize(void)
 	return si.dwAllocationGranularity;
 }
 
+static HMODULE advapi32_dll = NULL;
+static BOOL (WINAPI *advapi32_get_user_name)(char *, DWORD *);
+
+static void advapi32_cleanup(void)
+{
+	if (advapi32_dll)
+		FreeLibrary(advapi32_dll);
+	advapi32_dll = NULL;
+	advapi32_get_user_name = NULL;
+}
+
 struct passwd *getpwuid(int uid)
 {
 	static char user_name[100];
 	static struct passwd p;
+	static int advapi32_initialized = 0;
 
 	DWORD len = sizeof(user_name);
-	if (!GetUserName(user_name, &len))
+
+	if (!advapi32_initialized)
+	{
+		advapi32_dll = LoadLibrary("advapi32.dll");
+		if (!advapi32_dll)
+			die("cannot load advapi32.dll");
+		advapi32_get_user_name = (BOOL (WINAPI *)(char *, DWORD *))
+			GetProcAddress(advapi32_dll, "GetUserNameA");
+		if (!advapi32_get_user_name) {
+			FreeLibrary(advapi32_dll);
+			advapi32_dll = NULL;
+			die("cannot find GetUserNameA");
+		}
+		atexit(advapi32_cleanup);
+		advapi32_initialized = 1;
+	}
+
+	if (!advapi32_get_user_name(user_name, &len))
 		return NULL;
 	p.pw_name = user_name;
 	p.pw_gecos = "unknown";
Signature · 1 line
1.6.6.1599.gaed1a
#1 lore raw
Quoted 3 lines · Michael Lukashov

This DLL is used to get default user name. By looking up the only function that we need at runtime, we can avoid the startup costs of this DLL.

Sorry, no. This doesn't work. Even though git.exe does no longer depend on advapi32.dll directly, it does so indirectly (via ws_32.dll). This patch is not an improvement, therefore: NAK.

-- Hannes