diff --git a/src/core/utils.hpp b/src/core/utils.hpp index 8a7ca3ccb..7e76f1d6b 100644 --- a/src/core/utils.hpp +++ b/src/core/utils.hpp @@ -147,12 +147,12 @@ std::wstring ExpandUserFromString(const wchar_t* path, size_t path_len); bool GetExecutablePath(std::string* exe_path); #ifdef _WIN32 -bool GetExecutablePathW(std::wstring* exe_path); +bool GetExecutablePath(std::wstring* exe_path); #endif void SetExecutablePath(const std::string& exe_path); #ifdef _WIN32 -void SetExecutablePathW(const std::wstring& exe_path); +void SetExecutablePath(const std::wstring& exe_path); #endif bool Net_ipv6works(); @@ -166,7 +166,7 @@ ssize_t ReadFileToBuffer(const std::string& path, char* buf, size_t buf_len); ssize_t WriteFileWithBuffer(const std::string& path, const char* buf, size_t buf_len); PlatformFile OpenReadFile(const std::string &path); #ifdef _WIN32 -PlatformFile OpenReadFileW(const std::wstring& path); +PlatformFile OpenReadFile(const std::wstring& path); #endif #ifdef HAVE_TCMALLOC diff --git a/src/core/utils_win.cpp b/src/core/utils_win.cpp index 6686b34b9..ba4b4f170 100644 --- a/src/core/utils_win.cpp +++ b/src/core/utils_win.cpp @@ -658,29 +658,17 @@ bool IsWindowsVersionBNOrGreater(int wMajorVersion, static std::string main_exe_path = "UNKNOWN"; bool GetExecutablePath(std::string* exe_path) { - DWORD len; + std::wstring wexe_path; exe_path->clear(); - // Windows XP: The string is truncated to nSize characters and is not - // null-terminated. - exe_path->resize(_MAX_PATH + 1, '\0'); - len = ::GetModuleFileNameA(nullptr, const_cast(exe_path->data()), - _MAX_PATH); - exe_path->resize(len); - - // A zero return value indicates a failure other than insufficient space. - - // Insufficient space is determined by a return value equal to the size of - // the buffer passed in. - if (len == 0 || len == _MAX_PATH) { - PLOG(WARNING) << "Internal error: GetModuleFileNameA failed"; - *exe_path = main_exe_path; + if (!GetExecutablePath(&wexe_path)) { return false; } + *exe_path = SysWideToUTF8(wexe_path); return true; } -bool GetExecutablePathW(std::wstring* exe_path) { +bool GetExecutablePath(std::wstring* exe_path) { DWORD len; exe_path->clear(); // Windows XP: The string is truncated to nSize characters and is not @@ -688,6 +676,9 @@ bool GetExecutablePathW(std::wstring* exe_path) { exe_path->resize(_MAX_PATH + 1, L'\0'); len = ::GetModuleFileNameW(nullptr, const_cast(exe_path->data()), _MAX_PATH); + // If the function succeeds, the return value is the length of the string + // that is copied to the buffer, in characters, + // not including the terminating null character. exe_path->resize(len); // A zero return value indicates a failure other than insufficient space. @@ -711,7 +702,7 @@ void SetExecutablePath(const std::string& exe_path) { absl::flags_internal::SetProgramInvocationName(new_exe_path); } -void SetExecutablePathW(const std::wstring& exe_path) { +void SetExecutablePath(const std::wstring& exe_path) { main_exe_path = SysWideToUTF8(exe_path); std::string new_exe_path; @@ -763,7 +754,7 @@ PlatformFile OpenReadFile(const std::string &path) { FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr); } -PlatformFile OpenReadFileW(const std::wstring &path) { +PlatformFile OpenReadFile(const std::wstring &path) { return ::CreateFileW(path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr); } diff --git a/src/i18n/icu_util.cpp b/src/i18n/icu_util.cpp index e648143c4..27710327e 100644 --- a/src/i18n/icu_util.cpp +++ b/src/i18n/icu_util.cpp @@ -45,7 +45,11 @@ wchar_t g_debug_icu_pf_filename[_MAX_PATH]; // No need to change the filename in multiple places (gyp files, windows // build pkg configurations, etc). 'l' stands for Little Endian. // This variable is exported through the header file. +#ifdef _WIN32 +const wchar_t kIcuDataFileName[] = L"icudtl.dat"; +#else const char kIcuDataFileName[] = "icudtl.dat"; +#endif // Time zone data loading. @@ -111,16 +115,12 @@ void LazyInitIcuDataFile() { #endif // !defined(__APPLE__) #else // 0 #ifdef _WIN32 - std::wstring exe_path; - CHECK(GetExecutablePathW(&exe_path)); - std::filesystem::path exe_dir = std::filesystem::path(exe_path).parent_path(); - std::wstring data_path = exe_dir / SysUTF8ToWide(kIcuDataFileName); - PlatformFile pf = OpenReadFileW(data_path); + std::wstring exe_path, data_path; #else // _WIN32 - std::string exe_path; + std::string exe_path, data_path; +#endif // _WIN32 CHECK(GetExecutablePath(&exe_path)); std::filesystem::path exe_dir = std::filesystem::path(exe_path).parent_path(); - std::string data_path; PlatformFile pf = kInvalidPlatformFile; #ifdef __APPLE__ data_path = exe_dir.parent_path() / "Resources" / kIcuDataFileName; @@ -134,7 +134,6 @@ void LazyInitIcuDataFile() { data_path = exe_dir / kIcuDataFileName; pf = OpenReadFile(data_path); } -#endif // _WIN32 #endif // 0 if (pf != kInvalidPlatformFile) { // TODO(brucedawson): http://crbug.com/445616. diff --git a/src/win32/utils_win.cpp b/src/win32/utils_win.cpp index 319253671..4fcf6f84f 100644 --- a/src/win32/utils_win.cpp +++ b/src/win32/utils_win.cpp @@ -761,8 +761,8 @@ static std::wstring GetAutoStartCmdline() { std::wstring cmdline; /* turn on auto start */ - if (!GetExecutablePathW(&cmdline)) { - LOG(FATAL) << "GetExecutablePathW failed"; + if (!GetExecutablePath(&cmdline)) { + LOG(FATAL) << "GetExecutablePath failed"; } ss << L"\"" << cmdline << L"\" --background";