From dfbe7384b309c20d7733b0d18e6819302a483d95 Mon Sep 17 00:00:00 2001 From: Christian Ullrich Date: Tue, 6 Sep 2016 10:02:06 +0200 Subject: [PATCH] Pin any DLL as soon as seen when looking for _putenv() --- src/port/win32env.c | 54 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/src/port/win32env.c b/src/port/win32env.c index 3f56ba8..7417b60 100644 --- a/src/port/win32env.c +++ b/src/port/win32env.c @@ -43,36 +43,37 @@ pgwin32_putenv(const char *envval) char *modulename; HMODULE hmodule; PUTENVPROC putenvFunc; + BOOL pinned; } rtmodules[] = { /* Visual Studio 6.0 / mingw */ - {"msvcrt", NULL, NULL}, - {"msvcrtd", NULL, NULL}, + {"msvcrt", NULL, NULL, FALSE}, + {"msvcrtd", NULL, NULL, FALSE}, /* Visual Studio 2002 */ - {"msvcr70", NULL, NULL}, - {"msvcr70d", NULL, NULL}, + {"msvcr70", NULL, NULL, FALSE}, + {"msvcr70d", NULL, NULL, FALSE}, /* Visual Studio 2003 */ - {"msvcr71", NULL, NULL}, - {"msvcr71d", NULL, NULL}, + {"msvcr71", NULL, NULL, FALSE}, + {"msvcr71d", NULL, NULL, FALSE}, /* Visual Studio 2005 */ - {"msvcr80", NULL, NULL}, - {"msvcr80d", NULL, NULL}, + {"msvcr80", NULL, NULL, FALSE}, + {"msvcr80d", NULL, NULL, FALSE}, /* Visual Studio 2008 */ - {"msvcr90", NULL, NULL}, - {"msvcr90d", NULL, NULL}, + {"msvcr90", NULL, NULL, FALSE}, + {"msvcr90d", NULL, NULL, FALSE}, /* Visual Studio 2010 */ - {"msvcr100", NULL, NULL}, - {"msvcr100d", NULL, NULL}, + {"msvcr100", NULL, NULL, FALSE}, + {"msvcr100d", NULL, NULL, FALSE}, /* Visual Studio 2012 */ - {"msvcr110", NULL, NULL}, - {"msvcr110d", NULL, NULL}, + {"msvcr110", NULL, NULL, FALSE}, + {"msvcr110d", NULL, NULL, FALSE}, /* Visual Studio 2013 */ - {"msvcr120", NULL, NULL}, - {"msvcr120d", NULL, NULL}, + {"msvcr120", NULL, NULL, FALSE}, + {"msvcr120d", NULL, NULL, FALSE}, /* Visual Studio 2015 and later */ - {"ucrtbase", NULL, NULL}, - {"ucrtbased", NULL, NULL}, - {NULL, NULL, NULL} + {"ucrtbase", NULL, NULL, FALSE}, + {"ucrtbased", NULL, NULL, FALSE}, + {NULL, NULL, NULL, FALSE} }; int i; @@ -90,6 +91,21 @@ pgwin32_putenv(const char *envval) } else { + /* + * Pin this DLL handle as soon as possible to avoid it + * to be unloaded until the process terminates. + */ + if (!rtmodules[i].pinned) + { + HMODULE tmp; + BOOL res = GetModuleHandleEx( + GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS + | GET_MODULE_HANDLE_EX_FLAG_PIN, + (LPCTSTR)rtmodules[i].hmodule, + &tmp); + rtmodules[i].pinned = res; + } + rtmodules[i].putenvFunc = (PUTENVPROC) GetProcAddress(rtmodules[i].hmodule, "_putenv"); if (rtmodules[i].putenvFunc == NULL) { -- 2.10.0.windows.1