Article ID: 169498
Article Last Modified on 3/2/2005
// Obtain the error message for a given HRESULT
CString LogCrackHR( HRESULT hr )
{
LPVOID lpMsgBuf;
CString strTmp;
::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
hr,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0,
NULL );
// STR_TMP is defined within LOG.CPP to provide safe format string
// for both ANSI and UNICODE
strTmp.Format( "%s", (char *) lpMsgBuf );
// Free the buffer.
::LocalFree( lpMsgBuf );
return strTmp;
}
// Obtain information from the Errors Collection.
HRESULT LogAdoErrorImport(_ConnectionPtr pConn)
{
ErrorsPtr pErrors = NULL;
ErrorPtr pError = NULL;
CString strTmp;
HRESULT hr = (HRESULT) 0L;
long nCount;
// Don't have an un-handled exception in the handler that
// handles exceptions!
try
{
pErrors = pConn->GetErrors();
nCount = pErrors->GetCount();
for( long i = 0; (!FAILED(hr)) && (i < nCount); i++ )
{
TRACE( "\t Dumping ADO Error %d of %d", i+1, nCount );
hr = pErrors->get_Item((_variant_t)((long)i), &pError );
_bstr_t bstrSource ( pError->GetSource() );
_bstr_t bstrDescription( pError->GetDescription() );
_bstr_t bstrHelpFile ( pError->GetHelpFile() );
_bstr_t bstrSQLState ( pError->GetSQLState() );
TRACE( "\t\t Number = %ld", pError->GetNumber() );
TRACE( "\t\t Source = %s", (LPCTSTR) bstrSource );
TRACE( "\t\t Description = %s", (LPCTSTR) bstrDescription );
TRACE( "\t\t HelpFile = %s", (LPCTSTR) bstrHelpFile );
TRACE( "\t\t HelpContext = %ld", pError->GetHelpContext() );
TRACE( "\t\t SQLState = %s", (LPCTSTR) bstrSQLState );
TRACE( "\t\t HelpContext = %ld", pError->GetHelpContext() );
TRACE( "\t\t NativeError = %ld", pError->GetNativeError() );
}
}
catch( CException *e )
{
TRACE( "*** UNABLE TO LOG EXCEPTION ***" );
e->Delete();
}
catch(...)
{
TRACE( "*** UNABLE TO LOG EXCEPTION ***" );
}
if( pErrors ) pErrors->Release();
if( pError ) pError->Release();
return hr;
}
void CAdoDemoDlg::OnAdoTest()
{
HRESULT hr = S_OK;
_ConnectionPtr pConn;
hr = ::CoInitialize( NULL );
if( !FAILED( hr ) )
hr = pConn.CreateInstance( __uuidof( Connection ) );
// The following exception handling assumes a valid Connection
// object, so drop out if we couldn't create one for any reason.
if ( FAILED(hr) )
return;
try
{
// ... Your code goes here.
pConn->Close();
// For any error condition, dump results to TRACE.
// You may get a failure that does not raise an exception.
// The ADO Errors collection will likely be empty, but
// check anyway.
if( FAILED( hr ) )
{
TRACE( "*** HRESULT ***" );
TRACE( LogCrackHR( hr ) );
LogAdoErrorImport( pConn );
}
}
catch( CException *e )
{
TRACE( "*** Unhandled MFC Exception ***" );
e->Delete();
}
catch( _com_error &e )
{
// Crack _com_error
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
TRACE( "Exception thrown for classes generated by #import" );
TRACE( "\tCode = %08lx\n", e.Error());
TRACE( "\tCode meaning = %s\n", e.ErrorMessage());
TRACE( "\tSource = %s\n", (LPCTSTR) bstrSource);
TRACE( "\tDescription = %s\n", (LPCTSTR) bstrDescription);
// Errors Collection may not always be populated.
if( FAILED( hr ) )
{
TRACE( "*** HRESULT ***" );
TRACE( LogCrackHR( hr ) );
}
// Crack Errors Collection.
LogAdoErrorImport(pConn);
}
catch(...)
{
TRACE( "*** Unhandled Exception ***" );
}
}
167802 SAMPLE: EXCEPTEX Traps MFC and Win32 Structured Exceptions
168354 INFO: Underlying OLE and OLEDB Provider Errors Are Exposed Through ADO
Keywords: kbinfo kbdatabase kbcode KB169498