Click here to Skip to main content
15,896,201 members
Articles / Desktop Programming / Win32

dbAx: a C++ Library for ActiveX Data Objects (ADO)

Rate me:
Please Sign up or sign in to vote.
4.93/5 (24 votes)
21 Jan 2009CPOL4 min read 104.4K   2.4K   98  
C++ class wrapper for ADO
#include "StdAfx.h"
#include "AxLib.h"

using namespace dbAx;

CAxException::CAxException()
{
	m_pErrors = NULL;
	m_pError = NULL;
	m_nErrorCount = 0;
  m_scode = S_OK;
}

CAxException::~CAxException()
{
}

void CAxException::GetErrorsCollection()
{
  ADOConnection *pCn = NULL;
  _AxConnectionsT::iterator i = m_AxConnections.begin();

  for (; i != m_AxConnections.end(); i++ )
  {
    pCn = (*i)->_GetActiveConnection();
    pCn->get_Errors(&m_pErrors);
    m_pErrors->get_Count(&m_nErrorCount);
    if ( m_nErrorCount > 0 )
      break;
  }
}

void CAxException::GetErrorInfo()
{
	BSTR bstrDesc;

	GetErrorsCollection();

	if ( m_pErrors )
	{
		for ( long i = 0L; i < m_nErrorCount; i++ )
		{
			m_pErrors->get_Item(_variant_t(i), &m_pError);
			m_pError->get_Number(&m_nErrorNo);
			m_pError->get_Description(&bstrDesc);
      m_szErrorDesc += _T("\n");
      m_szErrorDesc += bstrDesc;

      if ( i < m_nErrorCount - 1 )
				m_szErrorDesc +=_T("\n");
		}
	}

  //Is there a COM error involved
  if ( GetAScode() != S_OK )
  {
    _com_error comErr(GetAScode());
    m_szErrorDesc += _T("\nCOM Error: ");
    m_szErrorDesc += comErr.ErrorMessage();
  }

  if ( m_pErrors != NULL )
		m_pErrors->Clear();
}

void dbAx::ThrowAxException(int nAxError, LPCTSTR lpszMsg, HRESULT hr)
{
  CAxException *pAxException = new CAxException;
  pAxException->SetAScode(hr);

	if ( nAxError == AXLIB_ERROR_NONE )
  {
    pAxException->m_szErrorDesc = lpszMsg;
    pAxException->GetErrorInfo();
  }
  else
  {
    pAxException->m_nErrorNo = nAxError;
    switch ( nAxError )
    {
		case AXLIB_ERROR_INIT :
      pAxException->m_szErrorDesc = _T("The dbAx Library failed to initialize");
			break;

		case AXLIB_ERROR_DXBIND :
      pAxException->m_szErrorDesc = _T("The specified field in the database was not found");
			break;

    case AXLIB_ERROR_OBJECT_NOTOPEN :
      pAxException->m_szErrorDesc = _T("The dbAx object is not open");
			break;

    case AXLIB_ERROR_BUF_SIZE :
      pAxException->m_szErrorDesc = _T("The specified buffer size is too small");
      break;

    case AXLIB_ERROR_NULL_PTR :
      pAxException->m_szErrorDesc = _T("Bad or NULL pointer");
      break;

    case AXLIB_ERROR_ENUM :
      pAxException->m_szErrorDesc = _T("Invalid enumeration value");
      break;

    case AXLIB_ERROR_INVALID_POS :
      pAxException->m_szErrorDesc = _T("Invalid page or record position");
      break;

    default :
       pAxException->m_szErrorDesc = _T("Unknown error");
      break;
   }
    
   if ( pAxException->GetAScode() != S_OK )
   {
     _com_error comErr(pAxException->GetAScode());
     pAxException->m_szErrorDesc += _T("\nCOM Error: ");
     pAxException->m_szErrorDesc += comErr.ErrorMessage();
   }

    if ( lpszMsg )
    {
      pAxException->m_szErrorDesc += _T("\nSource: ");
      pAxException->m_szErrorDesc += lpszMsg;
    }
  }
  throw pAxException;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Unknown
I work almost exclusively with C++ and database applications and manage the IT concerns for the engineering department of a large food processing company. As such, I get involved with add-ons to existing commercial software, SCADA, interfacing with industrial components (PLC’s, automated weight checking machines, etc.) and other pet projects (currently working on v4.0 of my own document management app).

I get totally fed-up, frustrated and thoroughly delighted with programming and computers, but always remember the sage words of an old lab tech from my Air Force days; “There was never a statue erected to the man who left well enough alone.” I’ll be at it again tomorrow!

Comments and Discussions