-
Notifications
You must be signed in to change notification settings - Fork 2
/
Connect.cpp
224 lines (179 loc) · 6.04 KB
/
Connect.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*!-----------------------------------------------------------------------
connect.cpp
The main implementation of the addin. It includes the implementation
for IDTExtensibility2, IRibbonExtensibility, ICustomTaskPaneConsumer,
and FormRegionStartup.
-----------------------------------------------------------------------!*/
#include "connect.h"
#include "FormRegionWrapper.h"
#include "MAPIX.h"
#include "MAPI/TestMAPI.h"
/*!-----------------------------------------------------------------------
CConnect implementation
-----------------------------------------------------------------------!*/
STDMETHODIMP CConnect::OnConnection(
IDispatch *pApplication,
ext_ConnectMode /* ConnectMode */,
IDispatch* /* pAddInInst */,
SAFEARRAY ** /* custom */)
{
if (!pApplication)
return E_POINTER;
if (!m_bMAPIInitialized)
{
const auto hRes = MAPIInitialize(nullptr);
if (SUCCEEDED(hRes))
{
m_bMAPIInitialized = true;
//TestMAPI::TestInbox(L"OnConnection", false);
}
}
m_pApplication = pApplication;
MessageBoxW(NULL, L"OnConnection fired", L"Sample Add-In", MB_OK | MB_ICONINFORMATION);
m_ApplicationEventSink = new ApplicationEventsSink(m_pApplication);
m_pApplication->ActiveExplorer(&m_pExplorer);
m_ExplorerEventsSink = new ExplorerEventsSink(m_pExplorer);
return S_OK;
}
STDMETHODIMP CConnect::OnDisconnection(ext_DisconnectMode /*RemoveMode*/, SAFEARRAY ** /*custom*/)
{
delete m_ExplorerEventsSink;
delete m_ApplicationEventSink;
if (m_pExplorer)
{
m_pExplorer.Release();
}
if (m_pApplication)
{
m_pApplication.Release();
}
if (m_bMAPIInitialized)
{
MAPIUninitialize();
m_bMAPIInitialized = false;
}
return S_OK;
}
STDMETHODIMP CConnect::OnAddInsUpdate(SAFEARRAY ** /*custom*/)
{
return S_OK;
}
STDMETHODIMP CConnect::OnStartupComplete(SAFEARRAY ** /*custom*/)
{
return S_OK;
}
STDMETHODIMP CConnect::OnBeginShutdown(SAFEARRAY ** /*custom*/)
{
return S_OK;
}
STDMETHODIMP CConnect::Invoke(
DISPID dispidMember,
const IID &riid,
LCID lcid,
WORD wFlags,
DISPPARAMS *pdispparams,
VARIANT *pvarResult,
EXCEPINFO *pexceptinfo,
UINT *puArgErr)
{
// Currently the CConnect object can get away with only one implementation
// of Invoke because the only interfaces that Outlook calls Invoke on are
// the ribbon callbacks and the form region startup. The other interfaces
// IRibbonExtensibility, IDTExtensibility, and ICustomTaskPaneConsumer are
// currently called via the virtual table and not the automation invoke
// method although this could potentially change in the future. The key
// thing to remember about using a common Invoke for multiple interfaces
// is to ensure that dispids for the different interfaces don't overlap.
// This is assuming the ribbon callback dispids are low and they not
// intersect with any of the form region startup dispids
auto hr = IRibbonCallbackImpl::Invoke(dispidMember, riid, lcid, wFlags, pdispparams, pvarResult, pexceptinfo, puArgErr);
if (DISP_E_MEMBERNOTFOUND == hr)
hr = FormRegionStartupImpl::Invoke(dispidMember, riid, lcid, wFlags, pdispparams, pvarResult, pexceptinfo, puArgErr);
if (DISP_E_MEMBERNOTFOUND == hr)
hr = IDTExtensibilityImpl::Invoke(dispidMember, riid, lcid, wFlags, pdispparams, pvarResult, pexceptinfo, puArgErr);
// There is no use trying to call Invoke on IRibbonExtensiblilityImpl and
// ICustomTaskPaneConsumerImpl because they only contain one method with
// dispid 1 and the other interfaces above most likely already cover dispid
// 1, namely IDTExtensibility2, plus as already mention they are usually
// called via the virtual table instead of IDispatch::Invoke.
return hr;
}
/*!-----------------------------------------------------------------------
FormRegionStartup interface implementation
-----------------------------------------------------------------------!*/
STDMETHODIMP CConnect::GetFormRegionStorage(
BSTR /* bstrFormRegionName */,
IDispatch * /* pDispItem */,
long /* LCID */,
OlFormRegionMode /* formRegionMode */,
OlFormRegionSize /* formRegionSize */,
__out VARIANT * pVarStorage)
{
const auto pSafeArray = GetOFSResource(IDS_FORMREGIONSTORAGE);
if (!pSafeArray)
return E_UNEXPECTED;
V_VT(pVarStorage) = VT_ARRAY | VT_UI1;
V_ARRAY(pVarStorage) = pSafeArray;
return S_OK;
}
STDMETHODIMP CConnect::BeforeFormRegionShow(_FormRegion *pFormRegion)
{
return FormRegionWrapper::Setup(pFormRegion);
}
STDMETHODIMP CConnect::GetFormRegionManifest(
BSTR /* bstrFormRegionName */,
long /* LCID */,
__out VARIANT * pvarManifest)
{
const auto bstr = GetXMLResource(IDS_FORMREGIONMANIFEST);
if (!bstr)
return E_UNEXPECTED;
V_VT(pvarManifest) = VT_BSTR;
V_BSTR(pvarManifest) = bstr;
return S_OK;
}
STDMETHODIMP CConnect::GetFormRegionIcon(
BSTR /* bstrFormRegionName */,
long /* LCID */,
OlFormRegionIcon /* formRegionIcon */,
__out VARIANT* /* pvarIcon */)
{
return S_OK;
}
/*!-----------------------------------------------------------------------
ICustomTaskPaneConsumer interface implementation
-----------------------------------------------------------------------!*/
HRESULT CConnect::CTPFactoryAvailable(ICTPFactory *CTPFactoryInst)
{
m_pCTPFactory = CTPFactoryInst;
return HrCreateSampleTaskPane();
}
/*!-----------------------------------------------------------------------
IRibbonExtensibility interface implementation
-----------------------------------------------------------------------!*/
HRESULT CConnect::GetCustomUI(BSTR /* ribbonID */, BSTR *ribbonXml)
{
if (!ribbonXml)
return E_POINTER;
// Get the same ribbon xml for every ribbonID
*ribbonXml = GetXMLResource(IDS_CUSTOMRIBBON);
return S_OK;
}
HRESULT CConnect::Button1Clicked(IDispatch* /* ribbonControl */)
{
MessageBoxW(nullptr,
L"Going to create a task pane now!",
L"Message from ribbon button.",
MB_OK | MB_ICONINFORMATION);
return HrCreateSampleTaskPane();
}
HRESULT CConnect::HrCreateSampleTaskPane()
{
if (!m_pCTPFactory)
return E_POINTER;
_CustomTaskPanePtr ctp;
const auto hr = m_pCTPFactory->CreateCTP(bstr_t(SAMPLECONTROL_PROGID), bstr_t("Sample Task Pane"), vtMissing, &ctp);
if (SUCCEEDED(hr))
ctp->put_Visible(VARIANT_TRUE);
return hr;
}