From 9c23522b739029c12bc2331d654b11289488fabb Mon Sep 17 00:00:00 2001 From: Steve Gates Date: Tue, 27 May 2014 12:28:13 -0700 Subject: [PATCH] Changes to make Boost System work on the Windows Runtime. Basically just use FormatMessageW without FORMAT_MESSAGE_FROM_SYSTEM. --- include/boost/system/detail/error_code.ipp | 59 ++++++++++++++-------- test/system_error_test.cpp | 4 +- 2 files changed, 39 insertions(+), 24 deletions(-) diff --git a/include/boost/system/detail/error_code.ipp b/include/boost/system/detail/error_code.ipp index c5a2068..e5b13c2 100644 --- a/include/boost/system/detail/error_code.ipp +++ b/include/boost/system/detail/error_code.ipp @@ -1,7 +1,7 @@ // error_code support implementation file ----------------------------------// // Copyright Beman Dawes 2002, 2006 - +// Copyright (c) Microsoft Corporation 2014 // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) @@ -22,7 +22,7 @@ # if defined( BOOST_WINDOWS_API ) # include -# if !defined(WINAPI_FAMILY) || ((WINAPI_FAMILY & WINAPI_PARTITION_DESKTOP) != 0) +# if BOOST_PLAT_WINDOWS_DESKTOP # include # endif # ifndef ERROR_INCORRECT_SIZE @@ -368,7 +368,7 @@ namespace std::string system_error_category::message( int ev ) const { -# if defined(WINAPI_FAMILY) && ((WINAPI_FAMILY & WINAPI_PARTITION_DESKTOP) == 0) +#if BOOST_PLAT_WINDOWS_DESKTOP std::string str( 128, char() ); for (;;) { @@ -415,27 +415,42 @@ namespace return std::string("Unknown error"); std::string str( static_cast(lpMsgBuf) ); -# else // WinCE workaround - LPVOID lpMsgBuf = 0; - DWORD retval = ::FormatMessageW( - FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, - ev, - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language - (LPWSTR) &lpMsgBuf, - 0, - NULL - ); - detail::local_free_on_destruction lfod(lpMsgBuf); - if (retval == 0) - return std::string("Unknown error"); - - int num_chars = (wcslen( static_cast(lpMsgBuf) ) + 1) * 2; +# else // WinCE and Windows Runtime workaround + std::wstring buf(128, wchar_t()); + for (;;) + { + DWORD retval = ::FormatMessageW( + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + ev, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language + &buf[0], + buf.size(), + NULL + ); + + if (retval > 0) + { + buf.resize(retval); + break; + } + else if ( ::GetLastError() != ERROR_INSUFFICIENT_BUFFER ) + { + return std::string("Unknown error"); + } + else + { + buf.resize(buf.size() + buf.size() / 2); + } + } + + int num_chars = (buf.size() + 1) * 2; LPSTR narrow_buffer = (LPSTR)_alloca( num_chars ); - if (::WideCharToMultiByte(CP_ACP, 0, static_cast(lpMsgBuf), -1, narrow_buffer, num_chars, NULL, NULL) == 0) + if (::WideCharToMultiByte(CP_ACP, 0, buf.c_str(), -1, narrow_buffer, num_chars, NULL, NULL) == 0) + { return std::string("Unknown error"); + } std::string str( narrow_buffer ); # endif diff --git a/test/system_error_test.cpp b/test/system_error_test.cpp index 9f7c996..fe88cd7 100644 --- a/test/system_error_test.cpp +++ b/test/system_error_test.cpp @@ -1,7 +1,7 @@ // system_error_test.cpp ---------------------------------------------------// // Copyright Beman Dawes 2006 - +// Copyright (c) Microsoft Corporation 2014 // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) @@ -40,7 +40,7 @@ namespace BOOST_TEST( ex.code().category() == system_category() ); # ifdef BOOST_WINDOWS_API LANGID language_id; -# if !defined(__MINGW32__) && !defined(__CYGWIN__) +# if !defined(__MINGW32__) && !defined(__CYGWIN__) && !BOOST_PLAT_WINDOWS_RUNTIME language_id = ::GetUserDefaultUILanguage(); # else language_id = 0x0409; // Assume US English