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 //===----------------------------------------------------------------------===//
12 // Third party headers
13 #include <memory> // std::unique_ptr
15 #include <WinBase.h> // ::FormatMessage()
18 #include "MIUtilSystemWindows.h"
19 #include "MICmnResources.h"
20 #include "MIUtilFileStd.h"
22 //++ ------------------------------------------------------------------------------------
23 // Details: CMIUtilSystemWindows constructor.
29 CMIUtilSystemWindows::CMIUtilSystemWindows(void)
33 //++ ------------------------------------------------------------------------------------
34 // Details: CMIUtilSystemWindows destructor.
40 CMIUtilSystemWindows::~CMIUtilSystemWindows(void)
44 //++ ------------------------------------------------------------------------------------
45 // Details: Retrieve the OS system error message for the given system error code.
47 // Args: vError - (R) OS error code value.
48 // vrwErrorMsg - (W) The error message.
49 // Return: MIstatus::success - Functional succeeded.
50 // MIstatus::failure - Functional failed.
54 CMIUtilSystemWindows::GetOSErrorMsg(const MIint vError, CMIUtilString &vrwErrorMsg) const
59 const MIuint nBufLen = 1024;
60 std::unique_ptr<char[]> pBuffer;
61 pBuffer.reset(new char[nBufLen]);
63 // CMIUtilString Format is not used as cannot replicate the behavior of ::FormatMessage which
64 // can take into account locality while retrieving the error message from the system.
65 const int nLength = ::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, (DWORD)vError,
66 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), reinterpret_cast<LPTSTR>(&pBuffer[0]), nBufLen, nullptr);
67 bool bOk = MIstatus::success;
69 vrwErrorMsg = &pBuffer[0];
71 bOk = MIstatus::failure;
76 //++ ------------------------------------------------------------------------------------
77 // Details: Retrieve if possible the OS last error description.
80 // Return: CMIUtilString - Error description.
84 CMIUtilSystemWindows::GetOSLastError(void) const
86 CMIUtilString errorMsg;
87 const DWORD dwLastError = ::GetLastError();
90 if (!GetOSErrorMsg(dwLastError, errorMsg))
91 errorMsg = MIRSRC(IDE_OS_ERR_RETRIEVING);
94 errorMsg = MIRSRC(IDE_OS_ERR_UNKNOWN);
99 //++ ------------------------------------------------------------------------------------
100 // Details: Retrieves the fully qualified path for the this application. If the function
101 // fails the string is filled with the error message.
103 // Args: vrwFileNamePath - (W) The executable's name and path or last error description.
104 // Return: MIstatus::success - Functional succeeded.
105 // MIstatus::failure - Functional failed.
109 CMIUtilSystemWindows::GetExecutablesPath(CMIUtilString &vrwFileNamePath) const
111 bool bOk = MIstatus::success;
112 HMODULE hModule = ::GetModuleHandle(nullptr);
113 char pPath[MAX_PATH];
114 if (!::GetModuleFileName(hModule, &pPath[0], MAX_PATH))
116 bOk = MIstatus::failure;
117 vrwFileNamePath = GetOSLastError();
120 vrwFileNamePath = &pPath[0];
125 //++ ------------------------------------------------------------------------------------
126 // Details: Retrieves the fully qualified path for the Log file for this application.
127 // If the function fails the string is filled with the error message.
129 // Args: vrwFileNamePath - (W) The Log file's name and path or last error description.
130 // Return: MIstatus::success - Functional succeeded.
131 // MIstatus::failure - Functional failed.
135 CMIUtilSystemWindows::GetLogFilesPath(CMIUtilString &vrwFileNamePath) const
137 vrwFileNamePath = CMIUtilString(".");
138 return MIstatus::success;
141 #endif // #if defined( _MSC_VER )