git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] Windows: avoid static dependency on advapi32.dll

From
Michael Lukashov <michael.lukashov@gmail.com>
Date
Jan 24, 2010, 18:53 UTC
Message-ID
<1264359231-4672-1-git-send-email-michael.lukashov@gmail.com>

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.

Signed-off-by: Michael Lukashov <michael.lukashov@gmail.com>
---
 compat/mingw.c |   31 ++++++++++++++++++++++++++++++-
 1 files changed, 30 insertions(+), 1 deletions(-)
diff --git a/compat/mingw.c b/compat/mingw.c
index ab65f77..6ce398c 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -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";
-- 
1.6.6.1599.gaed1a
Next: Johannes Sixt
Message 1 of 2 in “Windows: avoid static dependency on advapi32.dll”
  1. Windows: avoid static dependency on advapi32.dllMichael Lukashov, Jan 24, 2010
  2. Johannes SixtJan 24, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.