捕获Outlook事件

时间:2013-10-24 09:15:56

标签: c++ outlook mfc outlook-addin

我正在尝试使用MFC应用程序捕获一些Outlook事件。我从这个Code Project样本开始。 我设法捕获电子邮件发送和接收事件,但我无法捕获 ObjectSync.Start / End 事件。我相信我错过了什么。

以下是 QueryInterface 方法:

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;
}

以下是 调用 方法

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;
 }

这是 AttachToSource 方法

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;
}

任何帮助都将不胜感激。


更新1:

现在我有一个SyncObjectEvents的包装类

class CSyncObjectEventListener : public IDispatch

这是我的AttachToSource方法:

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以这种方式定义:

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

FindConnectionPoint方法无法连接。


更新2:

我有另一个名为ObjectSync的类:class CObjectSync : public COleDispatchDriver 在CSyncObjectEventListener类中,我有这个属性public : CObjectSync m_pObjectSync;

这是我正在尝试连接的代码。

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);

1 个答案:

答案 0 :(得分:4)

您的代码仅处理应用程序事件。您需要检索SyncObject对象并连接到其事件(SyncObjectEvents),而不是_ApplicationEvents。