From d73d6e8546dfb3ea6558d711681662cc14aa96e0 Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Thu, 20 Dec 2018 16:02:04 -0800 Subject: [PATCH] Fix GetDllDirectory on 2008 (#6066) --- .../AspNetCoreModuleV2/CommonLib/Environment.cpp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Servers/IIS/src/AspNetCoreModuleV2/CommonLib/Environment.cpp b/src/Servers/IIS/src/AspNetCoreModuleV2/CommonLib/Environment.cpp index 249884a4af..7e5ff9ca47 100644 --- a/src/Servers/IIS/src/AspNetCoreModuleV2/CommonLib/Environment.cpp +++ b/src/Servers/IIS/src/AspNetCoreModuleV2/CommonLib/Environment.cpp @@ -87,10 +87,21 @@ std::wstring Environment::GetCurrentDirectoryValue() std::wstring Environment::GetDllDirectoryValue() { + // GetDllDirectory can return 0 in both the success case and the failure case, and it only sets last error when it fails. + // This requires you to set the last error to ERROR_SUCCESS before calling it in order to detect failure. + SetLastError(ERROR_SUCCESS); + DWORD requestedSize = GetDllDirectory(0, nullptr); if (requestedSize == 0) { - throw std::system_error(GetLastError(), std::system_category(), "GetDllDirectory"); + if (GetLastError() != ERROR_SUCCESS) + { + throw std::system_error(GetLastError(), std::system_category(), "GetDllDirectory"); + } + else + { + return L""; + } } std::wstring expandedStr; @@ -99,7 +110,7 @@ std::wstring Environment::GetDllDirectoryValue() expandedStr.resize(requestedSize); requestedSize = GetDllDirectory(requestedSize, expandedStr.data()); // 0 might be returned if GetDllDirectory is empty - if (requestedSize == 0 && GetLastError() != 0) + if (requestedSize == 0 && GetLastError() != ERROR_SUCCESS) { throw std::system_error(GetLastError(), std::system_category(), "GetDllDirectory"); }