Article ID: 190901
Article Last Modified on 3/2/2005
119591 How to Obtain Microsoft Support Files from Online Services
********
#include <adoint.h>
class CADOConnectionEvents : public ConnectionEventsVt
{
public :
ULONG m_ulRefCount;
CADOConnectionEvents():m_ulRefCount(1){}
STDMETHOD(QueryInterface)(REFIID iid, LPVOID * ppvObject)
{
if (IsEqualIID(__uuidof(IUnknown), iid) ||
IsEqualIID(__uuidof(ConnectionEventsVt), iid))
{
*ppvObject = this;
return S_OK;
}
else
return E_NOINTERFACE;
}
STDMETHOD_(ULONG, AddRef)()
{
return m_ulRefCount++;
}
STDMETHOD_(ULONG, Release)()
{
if (--m_ulRefCount == 0)
{
delete this;
return 0;
}
else
return m_ulRefCount;
}
STDMETHOD(InfoMessage)(
ADOError * pError,
EventStatusEnum * adStatus,
_ADOConnection * pConnection)
{
*adStatus = adStatusUnwantedEvent;
return S_OK;
}
STDMETHOD(BeginTransComplete)(
LONG TransactionLevel,
ADOError * pError,
EventStatusEnum * adStatus,
_ADOConnection * pConnection)
{
*adStatus = adStatusUnwantedEvent;
return S_OK;
}
STDMETHOD(CommitTransComplete)(
ADOError * pError,
EventStatusEnum * adStatus,
_ADOConnection * pConnection)
{
*adStatus = adStatusUnwantedEvent;
return S_OK;
}
STDMETHOD(RollbackTransComplete)(
ADOError * pError,
EventStatusEnum * adStatus,
_ADOConnection * pConnection)
{
*adStatus = adStatusUnwantedEvent;
return S_OK;
}
STDMETHOD(WillExecute)(
BSTR * Source,
CursorTypeEnum * CursorType,
LockTypeEnum * LockType,
long * Options,
EventStatusEnum * adStatus,
_ADOCommand * pCommand,
_ADORecordset * pRecordset,
_ADOConnection * pConnection)
{
*adStatus = adStatusUnwantedEvent;
return S_OK;
}
STDMETHOD(ExecuteComplete)(
LONG RecordsAffected,
ADOError * pError,
EventStatusEnum * adStatus,
_ADOCommand * pCommand,
_ADORecordset * pRecordset,
_ADOConnection * pConnection)
{
*adStatus = adStatusUnwantedEvent;
return S_OK;
}
STDMETHOD(WillConnect)(
BSTR * ConnectionString,
BSTR * UserID,
BSTR * Password,
long * Options,
EventStatusEnum * adStatus,
_ADOConnection *pConnection)
{
*adStatus = adStatusUnwantedEvent;
return S_OK;
}
STDMETHOD(ConnectComplete)(
ADOError * pError,
EventStatusEnum * adStatus,
_ADOConnection * pConnection)
{
*adStatus = adStatusUnwantedEvent;
return S_OK;
}
STDMETHOD(Disconnect)(
EventStatusEnum * adStatus,
_ADOConnection * pConnection)
{
*adStatus = adStatusUnwantedEvent;
return S_OK;
}
};
********
Notice that you must implement Iunknown. Also, notice that all of the
methods return adStatusUnwantedEvent. This tells ADO to not call the event
handler again. This gives you a generic class that you can derive from and
then only override the events that you want to handle. All other events are
only be called once because of the adStatusUnwantedEvent return value. You
can see in ADOEvents.cpp that there are 2 classes derived from these
classes: CCnEvents and CRSEvents. These are the classes that you will use
to create the event objects. Their methods contain an OutputDebugString
message for each event so you can view the events in the output window of
the debugger.
*****
int main(int argc, char* argv[])
{
CoInitialize(NULL);
ADODB::_ConnectionPtr spConn(__uuidof(ADODB::Connection));
ADODB::_RecordsetPtr spRS(__uuidof(ADODB::Recordset));
SetConnectionEvents(spConn);
spConn->Open(L"DSN=AccessTest", L"", L"", -1L);
SetRSEvents(spRS);
spRS->Open(L"Select * from Table1", spConn.GetInterfacePtr(),
ADODB::adOpenKeyset, ADODB::adLockOptimistic, -1);
spRS->MoveFirst();
spRS->MoveNext();
spRS->Close();
spConn->Close();
ClearRSEvents(spRS);
ClearConnectionEvents(spConn);
return 0;
}
*****
In the code above you can see that an interface pointer to the connection
and recordset objects is passed to SetConnectionEvents and SetRSEvents
respectively. These functions create a connection event or recordset event
object and connect it to the ADO object.
*****
DWORD gdwCnEvents;
HRESULT SetConnectionEvents(ADODB::_ConnectionPtr & spConn)
{
IConnectionPointContainer *pCPC = NULL;
IConnectionPoint *pCP = NULL;
HRESULT hr;
hr = spConn->QueryInterface(IID_IConnectionPointContainer,
(LPVOID *) &pCPC);
if (FAILED(hr))
return hr;
hr = pCPC->FindConnectionPoint(__uuidof(ConnectionEvents), &pCP);
pCPC->Release();
if (FAILED(hr))
return hr;
hr = pCP->Advise(new CCnEvents, &gdwCnEvents);
pCP->Release();
return hr;
}
*****
Additional query words: ADOEVTS
Keywords: kbinfo kbdownload kbdatabase kbfile kbsample KB190901