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 wxPanel,
public IDispEventSimpleImpl<1, AutoSync, &__uuidof(Outlook::ItemsEvents)>,
public IDispEventSimpleImpl<2, AutoSync, &__uuidof(Outlook::ItemsEvents)>,
public IDispEventSimpleImpl<3, AutoSync, &__uuidof(Outlook::ItemsEvents)>
{
public:
AutoSync();
~AutoSync();
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));
}
somewhere else defined:
extern _ATL_FUNC_INFO OnItemsAddInfo;
extern _ATL_FUNC_INFO OnItemsChangeInfo;
extern _ATL_FUNC_INFO OnItemsRemoveInfo;
_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};
The problems comes in the
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));
It gives exception 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<IConnectionPoint> pCP;
HRESULT hRes = pUnkCP->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;
}
I can't manage to understand why. Any sugestion here? Everything seems to be fine, but obviously is not. I've been stucked here for quite a long time. Need your help, thanks.