1 //===-- MIUtilSystemWindows.cpp ---------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 // File: MIUtilSystemWindows.cpp
13 // Overview: CMIUtilSystemWindows implementation.
15 // Environment: Compilers: Visual C++ 12.
16 // gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
17 // Libraries: See MIReadmetxt.
24 // Third party headers
25 #include <memory> // std::unique_ptr
27 #include <WinBase.h> // ::FormatMessage()
30 #include "MIUtilSystemWindows.h"
31 #include "MICmnResources.h"
33 //++ ------------------------------------------------------------------------------------
34 // Details: CMIUtilSystemWindows constructor.
40 CMIUtilSystemWindows::CMIUtilSystemWindows(void)
44 //++ ------------------------------------------------------------------------------------
45 // Details: CMIUtilSystemWindows destructor.
51 CMIUtilSystemWindows::~CMIUtilSystemWindows(void)
55 //++ ------------------------------------------------------------------------------------
56 // Details: Retrieve the OS system error message for the given system error code.
58 // Args: vError - (R) OS error code value.
59 // vrwErrorMsg - (W) The error message.
60 // Return: MIstatus::success - Functional succeeded.
61 // MIstatus::failure - Functional failed.
65 CMIUtilSystemWindows::GetOSErrorMsg(const MIint vError, CMIUtilString &vrwErrorMsg) const
70 const MIuint nBufLen = 1024;
71 std::unique_ptr<char[]> pBuffer;
72 pBuffer.reset(new char[nBufLen]);
74 // CMIUtilString Format is not used as cannot replicate the behavior of ::FormatMessage which
75 // can take into account locality while retrieving the error message from the system.
76 const int nLength = ::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, (DWORD)vError,
77 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), reinterpret_cast<LPTSTR>(&pBuffer[0]), nBufLen, nullptr);
78 bool bOk = MIstatus::success;
80 vrwErrorMsg = &pBuffer[0];
82 bOk = MIstatus::failure;
87 //++ ------------------------------------------------------------------------------------
88 // Details: Retrieve if possible the OS last error description.
91 // Return: CMIUtilString - Error description.
95 CMIUtilSystemWindows::GetOSLastError(void) const
97 CMIUtilString errorMsg;
98 const DWORD dwLastError = ::GetLastError();
101 if (!GetOSErrorMsg(dwLastError, errorMsg))
102 errorMsg = MIRSRC(IDE_OS_ERR_RETRIEVING);
105 errorMsg = MIRSRC(IDE_OS_ERR_UNKNOWN);
110 //++ ------------------------------------------------------------------------------------
111 // Details: Retrieves the fully qualified path for the this application. If the function
112 // fails the string is filled with the error message.
114 // Args: vrwFileNamePath - (W) The excutable's name and path or last error description.
115 // Return: MIstatus::success - Functional succeeded.
116 // MIstatus::failure - Functional failed.
120 CMIUtilSystemWindows::GetExecutablesPath(CMIUtilString &vrwFileNamePath) const
122 bool bOk = MIstatus::success;
123 HMODULE hModule = ::GetModuleHandle(nullptr);
124 char pPath[MAX_PATH];
125 const DWORD nLen = ::GetModuleFileName(hModule, &pPath[0], MAX_PATH);
126 const CMIUtilString strLastErr(GetOSLastError());
127 if ((nLen != 0) && (strLastErr == "Unknown OS error"))
128 vrwFileNamePath = &pPath[0];
131 bOk = MIstatus::failure;
132 vrwFileNamePath = strLastErr;
138 //++ ------------------------------------------------------------------------------------
139 // Details: Retrieves the fully qualified path for the Log file for this application.
140 // If the function fails the string is filled with the error message.
142 // Args: vrwFileNamePath - (W) The Log file's name and path or last error description.
143 // Return: MIstatus::success - Functional succeeded.
144 // MIstatus::failure - Functional failed.
148 CMIUtilSystemWindows::GetLogFilesPath(CMIUtilString &vrwFileNamePath) const
150 return GetExecutablesPath(vrwFileNamePath);
153 #endif // #if defined( _MSC_VER )