-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathException.cpp
88 lines (66 loc) · 2.33 KB
/
Exception.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
////////////////////////////////////////////////////////////////////////////////
//! \file Exception.cpp
//! \brief The Exception class definition.
//! \author Chris Oldwood
#include "Common.hpp"
#include "Exception.hpp"
#include <WCL/ComPtr.hpp>
#include <WCL/ComStr.hpp>
#include <wbemidl.h>
#include <WCL/StrCvt.hpp>
#include <Core/StringUtils.hpp>
#ifndef _MSC_VER
WCL_DECLARE_IFACETRAITS(IWbemStatusCodeText, IID_IWbemStatusCodeText);
#endif
namespace WMI
{
////////////////////////////////////////////////////////////////////////////////
//! Construction from a non-IErrorInfo supported error.
Exception::Exception(HRESULT result, const tchar* operation)
: WCL::ComException(result, operation)
{
}
////////////////////////////////////////////////////////////////////////////////
//! Destructor.
Exception::~Exception() throw()
{
}
////////////////////////////////////////////////////////////////////////////////
//! Format the error using the WMI or IErrorInfo details.
void Exception::formatError(HRESULT result, IUnknown* object, const IID& iid, const tchar* operation)
{
tstring wmiText(formatWmiError(result));
if (!wmiText.empty())
{
m_details = Core::fmt(TXT("%s [0x%08lX - %s]"), operation, result, wmiText.c_str());
}
else
{
tstring source;
tstring description;
// Query for IErrorInfo details.
extractErrorInfo(object, iid, source, description);
tstring resultCode(CStrCvt::FormatError(result));
// Format the error string.
if (!source.empty() || !description.empty())
m_details = Core::fmt(TXT("%s [0x%08lX - %s] {%s : %s}"), operation, result, resultCode.c_str(), source.c_str(), description.c_str());
else
m_details = Core::fmt(TXT("%s [0x%08lX - %s]"), operation, result, resultCode.c_str());
}
}
////////////////////////////////////////////////////////////////////////////////
//! Format the WMI specific HRESULT error.
tstring Exception::formatWmiError(HRESULT error)
{
typedef WCL::ComPtr<IWbemStatusCodeText> IWbemStatusCodeTextPtr;
WCL::ComStr text;
// Use the WMI error lookup first.
IWbemStatusCodeTextPtr converter(CLSID_WbemStatusCodeText);
HRESULT result = converter->GetErrorCodeText(error, 0, 0, AttachTo(text));
if (SUCCEEDED(result))
return Core::trimCopy(W2T(text.Get()));
// Fall-back to using the standard error formatter.
return tstring(CStrCvt::FormatError(error));
}
//namespace WMI
}