I am trying to get the mail information and do some actions based on the values when the user hits the send button in Outlook. Therefore I use this function:
VOID WINAPI CConnect::ItemSend(IDispatch * Item, bool Cancel)
In the OnConnection event handler I call
DispEventAdvise((IDispatch*)m_Application, &__uuidof(Outlook::ApplicationEvents));
It is implemented in the Header-File like this:
public IDispEventSimpleImpl<1, CConnect, &__uuidof(Outlook::ItemEvents)>
public:
VOID WINAPI ItemSend(IDispatch * Item, bool Cancel);
BEGIN_SINK_MAP(CConnect)
SINK_ENTRY_INFO(1, __uuidof(Outlook::ItemEvents), 0x0000F002, ItemSend, &fiMailItemEvents)
END_SINK_MAP()
This is working just like it should, but inside the function I try to get the mail item I always get an exception. This is my code for accessing the item:
CComPtr<Outlook::_MailItem> mail;
Item->QueryInterface(IID__MailItem, (void**)&mail);
What am I doing wrong? Thanks in advance
There are a couple of caveats in your code, which could lead to problems:
ItemSend()
method differs from the one in Outlook's type library. It should be declared asItemSend(IDispatch* Item, VARIANT_BOOL* Cancel)
.IDispEventSimpleImpl
template declaration points toOutlook::ItemEvents
. However, you are interested in handling events fromOutlook::ApplicationEvents
.DispEventAdvise()
casts the application interface pointer toIDispatch*
, whereas the function expects anIUnknown*
parameter. You may also omit the second parameter.The following class demonstrates how to handle the
ItemSend
event accordingly. Since you are implementing theIDTExtensibility2
interface, you'll need to move the initialization and cleanup routines to itsOnConnection
andOnDisconnection
methods respectively.