Confdump Agent
1.4.0
|
00001 /* 00002 * Confdump-Agent - Dump static and runtime system configuration 00003 * Copyright (C) 2009-2012 Straton IT, SAS 00004 * 00005 * This program is free software: you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License version 3 as 00007 * published by the Free Software Foundation. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00016 */ 00017 00018 #include <windows.h> 00019 00020 #include "boost/shared_ptr.hpp" 00021 #include "Confdump/System/Win32/errors.hpp" 00022 00023 namespace Confdump 00024 { 00025 namespace System 00026 { 00027 namespace Win32 00028 { 00029 00030 SystemError::SystemError( DWORD rc, QString action ) 00031 : RuntimeError( "Unspecified win32 error: "+action ), // overwritten 00032 errorCode_( rc ), action_( action ) 00033 { 00034 setMessage( QString( "Failed to %1: %2 (%3)" ) 00035 .arg( action ) 00036 .arg( errorDescription() ) 00037 .arg( errorCodeAsHex() ) ); 00038 } 00039 00040 SystemError::~SystemError() 00041 { 00042 } 00043 00044 QString SystemError::errorCodeAsHex() const 00045 { 00046 return "0x" + QString::number( errorCode_, 16 ); 00047 } 00048 00049 QString SystemError::errorDescription() const 00050 { 00051 TCHAR *raw_buffer = 0; 00052 DWORD rc = FormatMessage( 00053 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 00054 NULL, 00055 errorCode_, 00056 MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), // Default language 00057 reinterpret_cast<LPTSTR>( &raw_buffer ), 00058 0, // buffer initial size 00059 NULL ); // arguments 00060 if ( !rc ) 00061 return "Unknown (could not get the system error message)"; 00062 boost::shared_ptr<TCHAR> buffer( raw_buffer, LocalFree ); 00063 00064 return QString::fromWCharArray( buffer.get() ); 00065 } 00066 00067 00068 void throwOnWin32Error( DWORD rc, QString action ) 00069 { 00070 if ( rc != NO_ERROR ) 00071 throw SystemError( rc, action ); 00072 } 00073 00074 00075 } 00076 } 00077 }