• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "flutter/fml/platform/win/errors_win.h"
6 
7 // ACE PC preivew.
8 #ifdef WINDOWS_PLATFORM
9 #include <windows.h>
10 #else
11 #include <Windows.h>
12 #endif
13 
14 #include <sstream>
15 
16 #include "flutter/fml/platform/win/wstring_conversion.h"
17 
18 namespace fml {
19 
GetLastErrorMessage()20 std::string GetLastErrorMessage() {
21   DWORD last_error = ::GetLastError();
22   if (last_error == 0) {
23     return {};
24   }
25 
26   const DWORD flags = FORMAT_MESSAGE_ALLOCATE_BUFFER |
27                       FORMAT_MESSAGE_FROM_SYSTEM |
28                       FORMAT_MESSAGE_IGNORE_INSERTS;
29 
30   wchar_t* buffer = nullptr;
31   size_t size = ::FormatMessage(
32       flags,                                      // dwFlags
33       NULL,                                       // lpSource
34       last_error,                                 // dwMessageId
35       MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),  // dwLanguageId
36       (LPWSTR)&buffer,                            // lpBuffer
37       0,                                          // nSize
38       NULL                                        // Arguments
39   );
40 
41   std::wstring message(buffer, size);
42 
43   ::LocalFree(buffer);
44 
45   std::wstringstream stream;
46   stream << message << " (" << last_error << ").";
47 
48   return WideStringToString(stream.str());
49 }
50 
51 }  // namespace fml
52