J
JL
Hi,
I'm writing a COM add-in for Outlook in Visual C++/ATL.
In my add-in I use among other things the OnFolderSwitch event of the
dispinterface ExplorerEvents and OnUpdate event of the dispinterface
_CommandBars
in order to add items to the main menu and the context menu of
outlook.
My add-in works perfectly but only for the first "instance" of Outlook
(the window that's opened when Outlook opens). My goal is that it
works for each new explorer window. For that I use the OnNewExplorers
event to get the new explorer and connect it to the OnfolderSwitch,
OnClose, etc... events with DispEventAdvise(). Before I have naturally
to disconnect the "old" explorer from these events (if I don't
disconnect it the add-in crashes):
void __stdcall CSMSAddin::OnNewExplorers(IDispatch* pDisp)
{
CComQIPtr<Outlook::_Explorer> spNewExplorer(pDisp);
ATLASSERT(spNewExplorer);
pDisp->Release();
HRESULT hr;
// Unadvise to stop notification of OnFolderSwitch,
OnSelectionChange, OnActivate, OnDeactivate, OnClose events for the
"old" explorer.
hr = ExpEvents:ispEventUnadvise((IDispatch*)m_spExplorer);
ATLASSERT(hr == S_OK);
hr = ExpEvents3:ispEventUnadvise((IDispatch*)m_spExplorer);
ATLASSERT(hr == S_OK);
hr = ExpEvents4:ispEventUnadvise((IDispatch*)m_spExplorer);
ATLASSERT(hr == S_OK);
hr = ExpEvents5:ispEventUnadvise((IDispatch*)m_spExplorer);
ATLASSERT(hr == S_OK);
hr = ExpEvents6:ispEventUnadvise((IDispatch*)m_spExplorer);
ATLASSERT(hr == S_OK);
m_spExplorer = spNewExplorer;
// Advise for the OnFolderSwitch, OnSelectionChange, OnActivate,
OnDeactivate, OnClose events for the new explorer.
hr = ExpEvents:ispEventAdvise((IDispatch*)m_spExplorer);
ATLASSERT(hr == S_OK);
hr = ExpEvents3:ispEventAdvise((IDispatch*)m_spExplorer);
ATLASSERT(hr == S_OK);
hr = ExpEvents4:ispEventAdvise((IDispatch*)m_spExplorer);
ATLASSERT(hr == S_OK);
hr = ExpEvents5:ispEventAdvise((IDispatch*)m_spExplorer);
ATLASSERT(hr == S_OK);
hr = ExpEvents6:ispEventAdvise((IDispatch*)m_spExplorer);
ATLASSERT(hr == S_OK);
}
Then I disconnect the explorer in the OnClose event.
void __stdcall CSMSAddin::OnExplorerClose()
{
HRESULT hr = ExpEvents:ispEventUnadvise((IDispatch*)m_spExplorer);
ATLASSERT(hr == S_OK);
hr = ExpEvents3:ispEventUnadvise((IDispatch*)m_spExplorer);
ATLASSERT(hr == S_OK);
hr = ExpEvents4:ispEventUnadvise((IDispatch*)m_spExplorer);
ATLASSERT(hr == S_OK);
hr = ExpEvents5:ispEventUnadvise((IDispatch*)m_spExplorer);
ATLASSERT(hr == S_OK);
hr = ExpEvents6:ispEventUnadvise((IDispatch*)m_spExplorer);
ATLASSERT(hr == S_OK);
MessageBox(NULL,"close", "outlook",MB_OK);
}
That's work !....but if I close the new explorer and go back to the
"old" window my add-in don't work anymore
It's logical since my "old" explorer is now disconnected... I
understand the problem but I don't see the solution.
How to connect again the "old" explorer to the events ?
Is there another way to solve this problem ?
In my searches I've seen the ItemsCB COM add-in example
(www.microeye.com) but it's in VB and I'm programming in C++...
I try to find a solution for a week. Please help me !
Thank you very much
Robert
I'm writing a COM add-in for Outlook in Visual C++/ATL.
In my add-in I use among other things the OnFolderSwitch event of the
dispinterface ExplorerEvents and OnUpdate event of the dispinterface
_CommandBars
in order to add items to the main menu and the context menu of
outlook.
My add-in works perfectly but only for the first "instance" of Outlook
(the window that's opened when Outlook opens). My goal is that it
works for each new explorer window. For that I use the OnNewExplorers
event to get the new explorer and connect it to the OnfolderSwitch,
OnClose, etc... events with DispEventAdvise(). Before I have naturally
to disconnect the "old" explorer from these events (if I don't
disconnect it the add-in crashes):
void __stdcall CSMSAddin::OnNewExplorers(IDispatch* pDisp)
{
CComQIPtr<Outlook::_Explorer> spNewExplorer(pDisp);
ATLASSERT(spNewExplorer);
pDisp->Release();
HRESULT hr;
// Unadvise to stop notification of OnFolderSwitch,
OnSelectionChange, OnActivate, OnDeactivate, OnClose events for the
"old" explorer.
hr = ExpEvents:ispEventUnadvise((IDispatch*)m_spExplorer);
ATLASSERT(hr == S_OK);
hr = ExpEvents3:ispEventUnadvise((IDispatch*)m_spExplorer);
ATLASSERT(hr == S_OK);
hr = ExpEvents4:ispEventUnadvise((IDispatch*)m_spExplorer);
ATLASSERT(hr == S_OK);
hr = ExpEvents5:ispEventUnadvise((IDispatch*)m_spExplorer);
ATLASSERT(hr == S_OK);
hr = ExpEvents6:ispEventUnadvise((IDispatch*)m_spExplorer);
ATLASSERT(hr == S_OK);
m_spExplorer = spNewExplorer;
// Advise for the OnFolderSwitch, OnSelectionChange, OnActivate,
OnDeactivate, OnClose events for the new explorer.
hr = ExpEvents:ispEventAdvise((IDispatch*)m_spExplorer);
ATLASSERT(hr == S_OK);
hr = ExpEvents3:ispEventAdvise((IDispatch*)m_spExplorer);
ATLASSERT(hr == S_OK);
hr = ExpEvents4:ispEventAdvise((IDispatch*)m_spExplorer);
ATLASSERT(hr == S_OK);
hr = ExpEvents5:ispEventAdvise((IDispatch*)m_spExplorer);
ATLASSERT(hr == S_OK);
hr = ExpEvents6:ispEventAdvise((IDispatch*)m_spExplorer);
ATLASSERT(hr == S_OK);
}
Then I disconnect the explorer in the OnClose event.
void __stdcall CSMSAddin::OnExplorerClose()
{
HRESULT hr = ExpEvents:ispEventUnadvise((IDispatch*)m_spExplorer);
ATLASSERT(hr == S_OK);
hr = ExpEvents3:ispEventUnadvise((IDispatch*)m_spExplorer);
ATLASSERT(hr == S_OK);
hr = ExpEvents4:ispEventUnadvise((IDispatch*)m_spExplorer);
ATLASSERT(hr == S_OK);
hr = ExpEvents5:ispEventUnadvise((IDispatch*)m_spExplorer);
ATLASSERT(hr == S_OK);
hr = ExpEvents6:ispEventUnadvise((IDispatch*)m_spExplorer);
ATLASSERT(hr == S_OK);
MessageBox(NULL,"close", "outlook",MB_OK);
}
That's work !....but if I close the new explorer and go back to the
"old" window my add-in don't work anymore
It's logical since my "old" explorer is now disconnected... I
understand the problem but I don't see the solution.
How to connect again the "old" explorer to the events ?
Is there another way to solve this problem ?
In my searches I've seen the ItemsCB COM add-in example
(www.microeye.com) but it's in VB and I'm programming in C++...
I try to find a solution for a week. Please help me !
Thank you very much
Robert