user12
user12

Reputation: 55

Capturing Outlook events

I'm trying to capture some outlook events using a MFC application. I started from this Code Project sample. I managed to capture email send and receive events but I couldn't capture ObjectSync.Start/End events. I believe I'm missing something.

Here is the QueryInterface method:

STDMETHODIMP CAppEventListener::QueryInterface(REFIID riid, 
                                           void ** ppvObj)
{
   if (riid == IID_IUnknown){*ppvObj = static_cast<IUnknown*>(this);}
   else 
       if (riid == IID_IDispatch){*ppvObj = static_cast<IDispatch*>(this);}
       else 
           if (riid == IID_ApplicationEvents){*ppvObj = static_cast<IDispatch*>(this);}
           else {*ppvObj = NULL;return E_NOINTERFACE;}
   static_cast<IUnknown*>(*ppvObj)->AddRef();
   return S_OK;
}

Here is the Invoke method

STDMETHODIMP CAppEventListener::Invoke(DISPID dispIdMember, 
                       REFIID riid, LCID lcid,
                       WORD wFlags, DISPPARAMS* pDispParams,
                       VARIANT* pVarResult, EXCEPINFO* pExcepInfo,
                       UINT* puArgErr)
{   
    //declaratin of l_date and l_time               
   switch(dispIdMember)
   {       
      case 0x0000fba7: HandleItemLoad(dispIdMember,l_date ,l_time);
               break;

      case 0x0000fab5: HandleOnReceive(riid,pDispParams,l_date ,l_time);
                   break;

      case 0x0000f002: HandleOnSend(riid,pDispParams,l_date ,l_time);
               break;

      case 0x0000fb40: HandleContextMenuDis(dispIdMember,l_date ,l_time);
               break;

      case 0x0000fba6: HandleContextMenuClo(dispIdMember,l_date ,l_time);
                   break;

      case 0x0000f006: HandleStartup(dispIdMember,l_date ,l_time);
               break;

      case 0x0000f007: HandleQuit(dispIdMember,l_date ,l_time);
               break;

      default:         HandleUnknown(dispIdMember,l_date ,l_time);
                   break;
   }
    return S_OK;
 }

And here is the AttachToSource method

STDMETHODIMP CAppEventListener::AttachToSource
                                 ( IUnknown* pEventSource )
{
     HRESULT hr = S_OK;

     IConnectionPointContainer* pCPC = NULL;
     hr = pEventSource->QueryInterface( IID_IConnectionPointContainer, 
                                  (void**)&pCPC );
     if (SUCCEEDED(hr))
     {
         hr = pCPC->FindConnectionPoint( IID_ApplicationEvents, 
                               &m_pConnectionPoint );
         if (SUCCEEDED(hr))
         {
             hr = m_pConnectionPoint->Advise( this, &m_dwConnection );
         }
         pCPC->Release();
     }  
     return hr;
}

Any help would be appreciated.


UPDATE 1:

Now I have a wrapper class for SyncObjectEvents

class CSyncObjectEventListener : public IDispatch

Here is my AttachToSource Method :

HRESULT STDMETHODCALLTYPE CSyncObjectEventListener::AttachToSource( IUnknown* pEventSource )
{
    HRESULT hr = S_OK;

   IConnectionPointContainer* pCPC = NULL;
   hr = pEventSource->QueryInterface( IID_IConnectionPointContainer, 
      (void**)&pCPC );
   if (SUCCEEDED(hr))
   {
      HRESULT hr = pCPC->FindConnectionPoint(IID_SyncObjectEvents, &m_pConnectionPoint);              
      //Failure
      if (SUCCEEDED(hr) )
      {
         hr = m_pConnectionPoint->Advise( this, &m_dwConnection );
      }
      pCPC->Release();
   }

   return hr;
}

IID_SyncObjectEvents is defined this way:

const IID IID_SyncObjectEvents  =   {0x00063005,0x0000,0x0000,{0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};

The FindConnectionPoint method fails to connect.


UPDATE 2 :

I have another class called ObjectSync : class CObjectSync : public COleDispatchDriver And in CSyncObjectEventListener class I have this attribute public : CObjectSync m_pObjectSync;

And here is the code with which I'm trying to connect.

m_pSyncObjectEventListener = new CSyncObjectEventListener();
if(!m_pSyncObjectEventListener->m_pObjectSync.CreateDispatch( _T("Outlook.SyncObjects" ),&l_oleExcep))
{
    return 0;
}

m_pSyncObjectEventListener->AddRef();
m_pSyncObjectEventListener->AttachToSource( m_pSyncObjectEventListener->m_pObjectSync.m_lpDispatch);

Upvotes: 4

Views: 835

Answers (1)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66235

Your code handles Application events only. You need to retrieve the SyncObject object and connect to its events (SyncObjectEvents), not _ApplicationEvents.

Upvotes: 4

Related Questions