|
Moderator

| Joined: | 23 Sep 2002 | | Posts: | 949 | | Location: | St. Louis, MO |
|
 |
Posted: Fri Apr 23, 2004 12:33 pm |
|
 |
 |
 |
 |
Here is some of my code. m_pAdoCon is my ADO connection object. After I create the hADOEventHandle I pass it to WaitForSingleObject where the code waits for the connect to fire.
It would work but it yarfs on my Advise statement.
This code is based upon the ado event trapping example in MSDN.
***.CPP
DWORD dwConnEvt = NULL;
IConnectionPointContainer *pCPC = NULL;
IConnectionPoint *pCP = NULL;
IUnknown *pUnk = NULL;
CConnEvent *pConnEvent= NULL;
int rc = 0;
//ConnectComplete Event for ADO
STDMETHODIMP CConnEvent::raw_ConnectComplete(
struct Error *pError,
ADODB::EventStatusEnum *adStatus,
struct _Connection *pConnection)
{
*adStatus = ADODB::adStatusOK;
SetEvent(hADOEventHandle); //Set the event handle
return S_OK;
};
//Disconnect Event for ADO
STDMETHODIMP CConnEvent::raw_Disconnect(
ADODB::EventStatusEnum *adStatus,
struct _Connection *pConnection)
{
*adStatus = ADODB::adStatusUnwantedEvent;
return S_OK;
};
//QueryInterface Function for CConnEvent
STDMETHODIMP CConnEvent::QueryInterface(REFIID riid, void ** ppv)
{
*ppv = NULL;
if (riid == __uuidof(IUnknown) ||
riid == __uuidof(ADODB::ConnectionEventsVt)) *ppv = this;
if (*ppv == NULL)
return ResultFromScode(E_NOINTERFACE);
AddRef();
return NOERROR;
}
STDMETHODIMP_(ULONG) CConnEvent::AddRef() { return ++m_cRef; };
STDMETHODIMP_(ULONG) CConnEvent::Release()
{
if (0 != --m_cRef) return m_cRef;
delete this;
return 0;
}
......
......
//QueryInterface function is overridden and up above in this code
hr = m_pAdoCon->QueryInterface(__uuidof(IConnectionPointContainer), (void **)&pCPC);
if (FAILED(hr)) return rc;
hr = pCPC->FindConnectionPoint(__uuidof(ADODB::ConnectionEvents), &pCP);
pCPC->Release();
if (FAILED(hr)) return rc;
pConnEvent = new CConnEvent();
hr = pConnEvent->QueryInterface(__uuidof(IUnknown), (void **) &pUnk);
if (FAILED(hr)) return rc;
hr = pCP->Advise(pUnk, &dwConnEvt);
pCP->Release();
if (FAILED(hr)) return rc;
//hADOEventHandle gets set in the raw_ConnectComplete event
pConnEvent->hADOEventHandle = CreateEvent(NULL, false, false, NULL);
if(pConnEvent->hADOEventHandle == NULL)
CBUDNETStdException::BUDNETThrowException(E_FAIL,"Could not open connection.", BudNETMessage);
|
***.H
class CConnEvent :
public CComCoClass<ADODB::ConnectionEventsVt>
{
private:
ULONG m_cRef;
public:
CConnEvent() { m_cRef = 0; };
~CConnEvent() {};
HANDLE hADOEventHandle;
STDMETHODIMP QueryInterface(REFIID riid, void ** ppv);
STDMETHODIMP_(ULONG) AddRef(void);
STDMETHODIMP_(ULONG) Release(void);
STDMETHODIMP raw_ConnectComplete(struct Error *pError, ADODB::EventStatusEnum *adStatus, struct _Connection *pConnection);
STDMETHODIMP raw_Disconnect(ADODB::EventStatusEnum *adStatus, struct _Connection *pConnection);
};
|
Thanks for any help you can provide!
|
|
_________________ Sometimes you feel like a nut, and sometimes you don't.
|