OLE DB Programmer's Reference |
When a consumer wants to retrieve an error object in response to a return code it received, it must perform the following actions:
The following code shows how a consumer might retrieve and use an OLE DB error object. (In the interest of clarity, some error checks and interface releases are not shown.)
#include <oledb.h> #include <stdio.h> IUnknown * pMyObject; Extern GUID IID_IMyInterface; DWORD MYLOCALEID; BSTR bstrSourceOfError, bstrDescriptionOfError; int main() { IErrorInfo * pErrorInfo; IErrorInfo * pErrorInfoRec; IErrorRecords * pErrorRecords; ISupportErrorInfo * pSupportErrorInfo; HRESULT hr, hrErr; ULONG i, ulNumErrorRecs; ERRORINFO ErrorInfo; // (Not shown.) Error or warning occurs when calling method. if (!FAILED(hrErr)) return (hrErr); // Check that the current interface supports error objects. hr = pMyObject->QueryInterface(IID_ISupportErrorInfo, (void**) &pSupportErrorInfo); if (SUCCEEDED(hr)) { hr = pSupportErrorInfo->InterfaceSupportsErrorInfo (IID_IMyInterface); if (hr == S_OK) { // Get the current error object. Return if no // error object exists. GetErrorInfo(0,&pErrorInfo); if (!pErrorInfo) return (hrErr); // Get the IErrorRecord interface, and get the count // of error recs. pErrorInfo->QueryInterface(IID_IErrorRecords, (void**) &pErrorRecords); pErrorRecords->GetRecordCount(&ulNumErrorRecs); // Read through error records and display them. for (i = 0; i < ulNumErrorRecs; i++) { // Get basic error information. pErrorRecords->GetBasicErrorInfo(i, &ErrorInfo); // Get error description and source through the // IErrorInfo interface pointer on a particular record. pErrorRecords->GetErrorInfo(i, MYLOCALEID, &pErrorInfoRec); BSTR bstrDescriptionOfError = NULL; BSTR bstrSourceOfError = NULL; pErrorInfoRec->GetDescription(&bstrDescriptionOfError); pErrorInfoRec->GetSource(&bstrSourceOfError); // At this point, you could call GetCustomErrorObject // and query for additional interfaces to determine // what else happened. wprintf( OLESTR("HRESULT: %lx, Minor Code: %lu, Source: %s, Description: %s"), ErrorInfo.hrError, ErrorInfo.dwMinor, bstrSourceOfError, bstrDescriptionOfError); // Free the resources. SysFreeString(bstrDescriptionOfError); SysFreeString(bstrSourceOfError); pErrorInfoRec->Release(); } // Release the error object. pErrorInfo->Release(); pErrorRecords->Release(); } } return (hrErr); };
1998-2001 Microsoft Corporation. All rights reserved.