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.
22 #if defined( _MSC_VER )
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.
64 bool CMIUtilSystemWindows::GetOSErrorMsg( const MIint vError, CMIUtilString & vrwErrorMsg ) const
69 const MIuint nBufLen = 1024;
70 std::unique_ptr< char[] > pBuffer;
71 pBuffer.reset( new char[ nBufLen ] );
73 // CMIUtilString Format is not used as cannot replicate the behavior of ::FormatMessage which
74 // can take into account locality while retrieving the error message from the system.
75 const int nLength = ::FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
76 nullptr, (DWORD) vError, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
77 reinterpret_cast< LPTSTR >( &pBuffer[ 0 ] ),
79 bool bOk = MIstatus::success;
81 vrwErrorMsg = &pBuffer[ 0 ];
83 bOk = MIstatus::failure;
88 //++ ------------------------------------------------------------------------------------
89 // Details: Retrieve if possible the OS last error description.
92 // Return: CMIUtilString - Error description.
95 CMIUtilString CMIUtilSystemWindows::GetOSLastError( void ) const
97 CMIUtilString errorMsg;
98 const DWORD dwLastError = ::GetLastError();
99 if( dwLastError != 0 )
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.
119 bool CMIUtilSystemWindows::GetExecutablesPath( CMIUtilString & vrwFileNamePath ) const
121 bool bOk = MIstatus::success;
122 HMODULE hModule = ::GetModuleHandle( nullptr );
123 char pPath[ MAX_PATH ];
124 const DWORD nLen = ::GetModuleFileName( hModule, &pPath[ 0 ], MAX_PATH );
125 const CMIUtilString strLastErr( GetOSLastError() );
126 if( (nLen != 0) && (strLastErr == "Unknown OS error") )
127 vrwFileNamePath = &pPath[ 0 ];
130 bOk = MIstatus::failure;
131 vrwFileNamePath = strLastErr;
137 //++ ------------------------------------------------------------------------------------
138 // Details: Retrieves the fully qualified path for the Log file for this application.
139 // If the function fails the string is filled with the error message.
141 // Args: vrwFileNamePath - (W) The Log file's name and path or last error description.
142 // Return: MIstatus::success - Functional succeeded.
143 // MIstatus::failure - Functional failed.
146 bool CMIUtilSystemWindows::GetLogFilesPath( CMIUtilString & vrwFileNamePath ) const
148 return GetExecutablesPath( vrwFileNamePath );
151 #endif // #if defined( _MSC_VER )