diff --git a/runtime/bin/builtin_impl_sources.gypi b/runtime/bin/builtin_impl_sources.gypi index 9d9d8acb8f5..d62c091be75 100644 --- a/runtime/bin/builtin_impl_sources.gypi +++ b/runtime/bin/builtin_impl_sources.gypi @@ -61,5 +61,6 @@ 'utils_linux.cc', 'utils_macos.cc', 'utils_win.cc', + 'utils_win.h', ], } diff --git a/runtime/bin/process_win.cc b/runtime/bin/process_win.cc index d3efd4cc5f6..c83afabdb78 100644 --- a/runtime/bin/process_win.cc +++ b/runtime/bin/process_win.cc @@ -13,6 +13,7 @@ #include "bin/log.h" #include "bin/thread.h" #include "bin/utils.h" +#include "bin/utils_win.h" namespace dart { @@ -305,21 +306,7 @@ static int SetOsErrorMessage(char** os_error_message) { int error_code = GetLastError(); static const int kMaxMessageLength = 256; wchar_t message[kMaxMessageLength]; - DWORD message_size = - FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, - error_code, - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - message, - kMaxMessageLength, - NULL); - if (message_size == 0) { - if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) { - Log::PrintErr("FormatMessage failed %d\n", GetLastError()); - } - _snwprintf(message, kMaxMessageLength, L"OS Error %d", error_code); - } - message[kMaxMessageLength - 1] = '\0'; + FormatMessageIntoBuffer(error_code, message, kMaxMessageLength); *os_error_message = StringUtils::WideToUtf8(message); return error_code; } diff --git a/runtime/bin/utils_win.cc b/runtime/bin/utils_win.cc index b926dd767de..dab73552daa 100644 --- a/runtime/bin/utils_win.cc +++ b/runtime/bin/utils_win.cc @@ -9,15 +9,14 @@ #include // NOLINT #include "bin/utils.h" +#include "bin/utils_win.h" #include "bin/log.h" namespace dart { namespace bin { -static void FormatMessageIntoBuffer(DWORD code, - wchar_t* buffer, - int buffer_length) { +void FormatMessageIntoBuffer(DWORD code, wchar_t* buffer, int buffer_length) { DWORD message_size = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, @@ -28,11 +27,14 @@ static void FormatMessageIntoBuffer(DWORD code, NULL); if (message_size == 0) { if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) { - Log::PrintErr("FormatMessage failed %d\n", GetLastError()); + Log::PrintErr("FormatMessage failed for error code %d (error %d)\n", + code, + GetLastError()); } _snwprintf(buffer, buffer_length, L"OS Error %d", code); } - buffer[buffer_length - 1] = '\0'; + // Ensure string termination. + buffer[buffer_length - 1] = 0; } diff --git a/runtime/bin/utils_win.h b/runtime/bin/utils_win.h new file mode 100644 index 00000000000..a0447604cfe --- /dev/null +++ b/runtime/bin/utils_win.h @@ -0,0 +1,18 @@ +// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +#ifndef BIN_UTILS_WIN_H_ +#define BIN_UTILS_WIN_H_ + +#include "platform/globals.h" + +namespace dart { +namespace bin { + +void FormatMessageIntoBuffer(DWORD code, wchar_t* buffer, int buffer_length); + +} // namespace bin +} // namespace dart + +#endif // BIN_UTILS_WIN_H_