{"thread":{"id":"22371","subject":"[PATCH] Windows: avoid static dependency on advapi32.dll","startedAt":"2010-01-24T18:53:51Z","lastAt":"2010-01-24T19:20:08Z","messageCount":2,"participants":["Michael Lukashov","Johannes Sixt"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"132549","messageId":"1264359231-4672-1-git-send-email-michael.lukashov@gmail.com","threadId":"22371","inReplyTo":null,"subject":"[PATCH] Windows: avoid static dependency on advapi32.dll","fromName":"Michael Lukashov","fromEmail":"michael.lukashov@gmail.com","sentAt":"2010-01-24T18:53:51Z","receivedAt":"2010-01-24T18:53:51Z","isPatch":true,"sender":{"key":"michael.lukashov@gmail.com","avatar":"https://avatars.githubusercontent.com/u/890439?v=4"},"body":"This DLL is used to get default user name. By looking\nup the only function that we need at runtime, we can\navoid the startup costs of this DLL.\n\nSigned-off-by: Michael Lukashov <michael.lukashov@gmail.com>\n---\n compat/mingw.c |   31 ++++++++++++++++++++++++++++++-\n 1 files changed, 30 insertions(+), 1 deletions(-)\n\ndiff --git a/compat/mingw.c b/compat/mingw.c\nindex ab65f77..6ce398c 100644\n--- a/compat/mingw.c\n+++ b/compat/mingw.c\n@@ -1176,13 +1176,42 @@ int mingw_getpagesize(void)\n \treturn si.dwAllocationGranularity;\n }\n \n+static HMODULE advapi32_dll = NULL;\n+static BOOL (WINAPI *advapi32_get_user_name)(char *, DWORD *);\n+\n+static void advapi32_cleanup(void)\n+{\n+\tif (advapi32_dll)\n+\t\tFreeLibrary(advapi32_dll);\n+\tadvapi32_dll = NULL;\n+\tadvapi32_get_user_name = NULL;\n+}\n+\n struct passwd *getpwuid(int uid)\n {\n \tstatic char user_name[100];\n \tstatic struct passwd p;\n+\tstatic int advapi32_initialized = 0;\n \n \tDWORD len = sizeof(user_name);\n-\tif (!GetUserName(user_name, &len))\n+\n+\tif (!advapi32_initialized)\n+\t{\n+\t\tadvapi32_dll = LoadLibrary(\"advapi32.dll\");\n+\t\tif (!advapi32_dll)\n+\t\t\tdie(\"cannot load advapi32.dll\");\n+\t\tadvapi32_get_user_name = (BOOL (WINAPI *)(char *, DWORD *))\n+\t\t\tGetProcAddress(advapi32_dll, \"GetUserNameA\");\n+\t\tif (!advapi32_get_user_name) {\n+\t\t\tFreeLibrary(advapi32_dll);\n+\t\t\tadvapi32_dll = NULL;\n+\t\t\tdie(\"cannot find GetUserNameA\");\n+\t\t}\n+\t\tatexit(advapi32_cleanup);\n+\t\tadvapi32_initialized = 1;\n+\t}\n+\n+\tif (!advapi32_get_user_name(user_name, &len))\n \t\treturn NULL;\n \tp.pw_name = user_name;\n \tp.pw_gecos = \"unknown\";\n-- \n1.6.6.1599.gaed1a\n"},{"id":"132554","messageId":"201001242020.08534.j6t@kdbg.org","threadId":"22371","inReplyTo":"1264359231-4672-1-git-send-email-michael.lukashov@gmail.com","subject":"Re: [PATCH] Windows: avoid static dependency on advapi32.dll","fromName":"Johannes Sixt","fromEmail":"j6t@kdbg.org","sentAt":"2010-01-24T19:20:08Z","receivedAt":"2010-01-24T19:20:08Z","isPatch":true,"sender":{"key":"j6t@kdbg.org","avatar":"https://avatars.githubusercontent.com/u/14810926?v=4"},"body":"On Sonntag, 24. Januar 2010, Michael Lukashov wrote:\n> This DLL is used to get default user name. By looking\n> up the only function that we need at runtime, we can\n> avoid the startup costs of this DLL.\n\nSorry, no. This doesn't work. Even though git.exe does no longer depend on \nadvapi32.dll directly, it does so indirectly (via ws_32.dll). This patch is \nnot an improvement, therefore: NAK.\n\n-- Hannes\n"}]}