diff --git a/src/aws-cpp-sdk-core/source/platform/windows/FileSystem.cpp b/src/aws-cpp-sdk-core/source/platform/windows/FileSystem.cpp index 2ea82de6f89..b6678e1cc9c 100644 --- a/src/aws-cpp-sdk-core/source/platform/windows/FileSystem.cpp +++ b/src/aws-cpp-sdk-core/source/platform/windows/FileSystem.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -180,14 +181,20 @@ Aws::String GetHomeDirectory() Aws::String GetExecutableDirectory() { - static const unsigned long long bufferSize = 256; - WCHAR buffer[bufferSize]; + Aws::Vector buffer(MAX_PATH + 1, NULL); + DWORD written = GetModuleFileNameW(nullptr, buffer.data(), static_cast(buffer.size())); - memset(buffer, 0, sizeof(buffer)); + if (ERROR_INSUFFICIENT_BUFFER == GetLastError()) + { + /* Max unicode path size is 2^15 + 1 additional byte for null terminator. */ + const DWORD unicode_max_path = 0x8000 + 1; + buffer.resize(unicode_max_path, NULL); + written = GetModuleFileNameW(nullptr, buffer.data(), static_cast(buffer.size())); + } - if (GetModuleFileNameW(nullptr, buffer, static_cast(sizeof(buffer)))) + if (written > 0) { - Aws::String bufferStr(Aws::Utils::StringUtils::FromWString(buffer)); + Aws::String bufferStr(Aws::Utils::StringUtils::FromWString(buffer.data())); auto fileNameStart = bufferStr.find_last_of(PATH_DELIM); if (fileNameStart != std::string::npos) {