This repository has been archived by the owner on Jun 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ComSession.cpp
166 lines (141 loc) · 5.04 KB
/
ComSession.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*!
* \brief The file contains COM session abstruction implementation
* \author \verbatim
Created by: Alexander Egorov
\endverbatim
* \date \verbatim
Creation date: 2010-04-11
\endverbatim
* Copyright 2008-2012 Alexander Egorov <egoroff@gmail.com> (http://www.egoroff.spb.ru)
*/
#include "StdAfx.h"
#include "ComSession.h"
#include "wexception.h"
inline void WriteErrorMessage(
CComPtr<IErrorInfo> pIErrorInfo,
std::wostream* pOstr)
{
// IErrorInfo values.
CComBSTR bstrDescription;
CComBSTR bstrSource;
pIErrorInfo->GetSource(&bstrSource);
pIErrorInfo->GetDescription(&bstrDescription);
if (bstrSource != NULL) {
*pOstr << L"SOURCE:\t\t" << bstrSource.m_str << std::endl;
}
if (bstrDescription != NULL) {
*pOstr
<< L"ERROR MESSAGE:\t"
<< bstrDescription.m_str
<< std::endl
<< std::endl;
}
}
// DumpErrorInfo queries SQLOLEDB error interfaces, retrieving available
// status or error information.
inline void ComSession::DumpErrorInfo(
std::wostream* pOstr,
IUnknown* pObjectWithError,
REFIID rErrorInterface) const
{
// Interfaces used in the example.
CComPtr<IErrorInfo> pIErrorInfoAll;
CComPtr<IErrorRecords> pIErrorRecords;
CComPtr<ISupportErrorInfo> pISupportErrorInfo;
// Only ask for error information if the interface supports
// it.
if (pObjectWithError == NULL || FAILED(pObjectWithError->QueryInterface(IID_ISupportErrorInfo,
reinterpret_cast<void**>(&pISupportErrorInfo)))) {
*pOstr << L"SupportErrorErrorInfo interface not supported" << std::endl;
return;
}
if (FAILED(pISupportErrorInfo->InterfaceSupportsErrorInfo(rErrorInterface))) {
*pOstr << L"InterfaceWithError interface not supported" << std::endl;
return;
}
// Do not test the return of GetErrorInfo. It can succeed and return
// a NULL pointer in pIErrorInfoAll. Simply test the pointer.
HRESULT r = GetErrorInfo(0, &pIErrorInfoAll);
if ((pIErrorInfoAll == NULL) || FAILED(r)) {
*pOstr << L"GetErrorInfo failed." << std::endl;
return;
}
// Test to see if it's a valid OLE DB IErrorInfo interface
// exposing a list of records.
if (FAILED(pIErrorInfoAll->QueryInterface(IID_IErrorRecords,
reinterpret_cast<void**>(&pIErrorRecords))) ) {
// IErrorInfo is valid; get the source and
// description to see what it is.
WriteErrorMessage(pIErrorInfoAll, pOstr);
return;
}
// Basic error information from GetBasicErrorInfo.
ERRORINFO errorinfo;
// Number of error records.
ULONG nRecs;
ULONG nRec;
// ISQLErrorInfo parameters.
CComBSTR bstrSQLSTATE;
LONG lNativeError;
pIErrorRecords->GetRecordCount(&nRecs);
// Within each record, retrieve information from each
// of the defined interfaces.
for (nRec = 0; nRec < nRecs; ++nRec) {
// From IErrorRecords, get the HRESULT and a reference
// to the ISQLErrorInfo interface.
pIErrorRecords->GetBasicErrorInfo(nRec, &errorinfo);
CComPtr<ISQLErrorInfo> pISQLErrorInfo;
CComPtr<IErrorInfo> pIErrorInfoRecord;
pIErrorRecords->GetCustomErrorObject(
nRec,
IID_ISQLErrorInfo,
reinterpret_cast<IUnknown**>(&pISQLErrorInfo));
if (pISQLErrorInfo != NULL) {
pISQLErrorInfo->GetSQLInfo(&bstrSQLSTATE, &lNativeError);
// Display the SQLSTATE and native error values.
*pOstr << L"SQLSTATE:\t" << bstrSQLSTATE.m_str << std::endl;
}
if (SUCCEEDED(pIErrorRecords->GetErrorInfo(nRec, ::GetSystemDefaultLCID(), &pIErrorInfoRecord))) {
WriteErrorMessage(pIErrorInfoRecord, pOstr);
}
}
}
// DumpErrorInfo queries SQLOLEDB error interfaces, retrieving available
// status or error information.
inline void ComSession::DumpErrorInfo(
IUnknown* pObjectWithError,
REFIID rErrorInterface) const
{
DumpErrorInfo(&std::wcout, pObjectWithError, rErrorInterface);
}
ComSession::ComSession()
{
HRESULT hr = CoInitializeEx(0, COINIT_MULTITHREADED);
ThrowIfError(hr, L"Failed to initialize COM library");
}
ComSession::~ComSession()
{
Clear();
ATLASSERT(_CrtCheckMemory());
}
void ComSession::Clear()
{
CoUninitialize();
}
void ComSession::ThrowIfError(HRESULT result, LPCWSTR pMessage) const
{
if (FAILED(result)) {
throw wexception(pMessage);
}
}
void ComSession::ThrowIfError(
HRESULT result,
IUnknown* pObjectWithError,
REFIID rErrorInterface) const
{
if (FAILED(result)) {
std::auto_ptr<std::wostringstream> msg(new std::wostringstream);
DumpErrorInfo(msg.get(), pObjectWithError, rErrorInterface);
throw wexception(msg->str().c_str());
}
}