win_util.cc (1195B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style license that can be 5 // found in the LICENSE file. 6 7 #include "base/win_util.h" 8 9 #include <sddl.h> 10 11 #include "base/logging.h" 12 #include "base/string_util.h" 13 14 namespace win_util { 15 16 std::wstring FormatMessage(unsigned messageid) { 17 wchar_t* string_buffer = NULL; 18 ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | 19 FORMAT_MESSAGE_IGNORE_INSERTS, 20 NULL, messageid, 0, 21 reinterpret_cast<wchar_t*>(&string_buffer), 0, NULL); 22 23 std::wstring formatted_string; 24 if (string_buffer) { 25 formatted_string = string_buffer; 26 LocalFree(reinterpret_cast<HLOCAL>(string_buffer)); 27 } else { 28 // The formating failed. simply convert the message value into a string. 29 SStringPrintf(&formatted_string, L"message number %d", messageid); 30 } 31 return formatted_string; 32 } 33 34 std::wstring FormatLastWin32Error() { return FormatMessage(GetLastError()); } 35 36 } // namespace win_util