The code in this example illustrates the use of error objects in OLE DB error handling.
//////////////////////////////////////////////////////////////////////// // myHandleResult // // This function is called as part of the XCHECK_HR macro; it takes an // HRESULT, which is returned by the method called in the XCHECK_HR // macro, and the file and line number where the method call was made. // If the method call failed, this function attempts to get and display // the extended error information for the call from the IErrorInfo, // IErrorRecords, and ISQLErrorInfo interfaces. // //////////////////////////////////////////////////////////////////////// HRESULT myHandleResult ( HRESULT hrReturned, LPCWSTR pwszFile, ULONG ulLine) { HRESULT hr; IErrorInfo * pIErrorInfo = NULL; IErrorRecords * pIErrorRecords = NULL; ULONG cRecords; ULONG iErr; // If the method called as part of the XCHECK_HR macro failed, // we will attempt to get extended error information for the call. if( FAILED(hrReturned) ) { // Obtain the current error object, if any, by using the // Automation GetErrorInfo function, which will give // us back an IErrorInfo interface pointer if successful. hr = GetErrorInfo(0, &pIErrorInfo); // We've got the IErrorInfo interface pointer on the error object. if( SUCCEEDED(hr) && pIErrorInfo ) { // OLE DB extends the Automation error model by allowing // error objects to support the IErrorRecords interface. This // interface can expose information on multiple errors. hr = pIErrorInfo->QueryInterface(IID_IErrorRecords, (void**)&pIErrorRecords); if( SUCCEEDED(hr) ) { // Get the count of error records from the object. CHECK_HR(hr = pIErrorRecords->GetRecordCount(&cRecords)); // Loop through the set of error records, and // display the error information for each one. for( iErr = 0; iErr < cRecords; iErr++ ) { myDisplayErrorRecord(hrReturned, iErr, pIErrorRecords, pwszFile, ulLine); } } // The object didn't support IErrorRecords. Display // the error information for this single error. else { myDisplayErrorInfo(hrReturned, pIErrorInfo, pwszFile, ulLine); } } // There was no error object, so just display the HRESULT to the // user. else { wprintf(L"\nNo Error Info posted; HResult: 0x%08x\n" L"File: %s, Line: %d\n", hrReturned, pwszFile, ulLine); } } CLEANUP: if( pIErrorInfo ) pIErrorInfo->Release(); if( pIErrorRecords ) pIErrorRecords->Release(); return hrReturned; }