Outlook Addin: Capturing events with IDispEventSimpleImpl. Adviseexception


P

Paco

Hello, I want to creating an addin that captures when a {contact,
calendar, task, note} is {created, edited, removed}. I have the
following code, to make it shorter I removed all the code but the
related to contact, since all types will be the same I guess.

AutoSync.h
class ATL_NO_VTABLE AutoSync :
....
public IDispEventSimpleImpl<1, AutoSync,
&__uuidof(Outlook::ItemsEvents)>,
public IDispEventSimpleImpl<2, AutoSync,
&__uuidof(Outlook::ItemsEvents)>,
public IDispEventSimpleImpl<3, AutoSync,
&__uuidof(Outlook::ItemsEvents)>
{
public:
....
void __stdcall OnItemAdd(IDispatch* Item); /* 0xf001 */
void __stdcall OnItemChange(IDispatch* Item); /* 0xf002 */
void __stdcall OnItemRemove(); /* 0xf003 */

BEGIN_SINK_MAP(AutoSync)
SINK_ENTRY_INFO(1, __uuidof(Outlook::ItemsEvents), 0xf001, OnItemAdd,
&OnItemsAddInfo)
SINK_ENTRY_INFO(2, __uuidof(Outlook::ItemsEvents), 0xf002,
OnItemChange, &OnItemsChangeInfo)
SINK_ENTRY_INFO(3, __uuidof(Outlook::ItemsEvents), 0xf003,
OnItemRemove, &OnItemsRemoveInfo)
END_SINK_MAP()

typedef IDispEventSimpleImpl<1, AutoSync,
&__uuidof(Outlook::ItemsEvents)> ItemAddEvents;
typedef IDispEventSimpleImpl<2, AutoSync,
&__uuidof(Outlook::ItemsEvents)> ItemChangeEvents;
typedef IDispEventSimpleImpl<3, AutoSync,
&__uuidof(Outlook::ItemsEvents)> ItemRemoveEvents;

private:
CComPtr<Outlook::_Items> m_contacts;
};


AutoSync.cpp
_NameSpacePtr pMAPI = OutlookWorker::GetInstance()->GetNameSpacePtr();


MAPIFolderPtr pContactsFolder = NULL;
HRESULT hr = NULL;

//get folders
if(pMAPI != NULL) {
pMAPI->GetDefaultFolder(olFolderContacts, &pContactsFolder);
}

//get items
if(pContactsFolder != NULL) pContactsFolder->get_Items(&m_contacts);

//dispatch events
if(m_contacts != NULL) {
//HERE COMES THE EXCEPTION
hr =
ItemAddEvents::DispEventAdvise((IDispatch*)m_contacts,&__uuidof(Outlook::ItemsEvents));
hr =
ItemChangeEvents::DispEventAdvise((IDispatch*)m_contacts,&__uuidof(Outlook::ItemsEvents));
hr =
ItemRemoveEvents::DispEventAdvise((IDispatch*)m_contacts,&__uuidof(Outlook::ItemsEvents));
}


and this defined somewhere else:
_ATL_FUNC_INFO OnItemsAddInfo = {CC_STDCALL,VT_EMPTY,1,{VT_DISPATCH}};
_ATL_FUNC_INFO OnItemsChangeInfo = {CC_STDCALL,VT_EMPTY,1,
{VT_DISPATCH}};
_ATL_FUNC_INFO OnItemsRemoveInfo = {CC_STDCALL,VT_EMPTY,0};

When the instructions:
hr =
ItemAddEvents::DispEventAdvise((IDispatch*)m_contacts,&__uuidof(Outlook::ItemsEvents));
hr =
ItemChangeEvents::DispEventAdvise((IDispatch*)m_contacts,&__uuidof(Outlook::ItemsEvents));
hr =
ItemRemoveEvents::DispEventAdvise((IDispatch*)m_contacts,&__uuidof(Outlook::ItemsEvents));

are executed, an excetion comes in atlbase.inl' when executes method
'Advise':

ATLINLINE ATLAPI AtlAdvise(IUnknown* pUnkCP, IUnknown* pUnk, const
IID& iid, LPDWORD pdw)
{
if(pUnkCP == NULL)
return E_INVALIDARG;

CComPtr<IConnectionPointContainer> pCPC;
CComPtr said:
QueryInterface(__uuidof(IConnectionPointContainer), (void**)&pCPC);
if (SUCCEEDED(hRes))
hRes = pCPC->FindConnectionPoint(iid, &pCP);
if (SUCCEEDED(hRes))
//HERE GIVES EXCEPTION
//Unhandled exception at 0x2fe913e3 in OUTLOOK.EXE: 0xC0000005:
//Access violation reading location 0xcdcdcdcd.
hRes = pCP->Advise(pUnk, pdw);
return hRes;
}

Any idea or suggestion here? Thank you.
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top