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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include "APNDataObject.hxx"
#include <osl/diagnose.h>
#include <systools/win32/comtools.hxx>
#include <assert.h>
#define FREE_HGLOB_ON_RELEASE TRUE
#define KEEP_HGLOB_ON_RELEASE FALSE
// ctor
CAPNDataObject::CAPNDataObject( IDataObjectPtr rIDataObject ) :
m_rIDataObjectOrg( rIDataObject ),
m_hGlobal( nullptr ),
m_nRefCnt( 0 )
{
OSL_ENSURE( m_rIDataObjectOrg.get( ), "constructing CAPNDataObject with empty data object" );
// we marshal the IDataObject interface pointer here so
// that it can be unmarshalled multiple times when this
// class will be used from another apartment
sal::systools::COMReference<IStream> pStm;
HRESULT hr = CreateStreamOnHGlobal( nullptr, KEEP_HGLOB_ON_RELEASE, &pStm );
OSL_ENSURE( E_INVALIDARG != hr, "invalid args passed to CreateStreamOnHGlobal" );
if ( SUCCEEDED( hr ) )
{
HRESULT hr_marshal = CoMarshalInterface(
pStm,
__uuidof(IDataObject),
m_rIDataObjectOrg,
MSHCTX_LOCAL,
nullptr,
MSHLFLAGS_TABLEWEAK );
OSL_ENSURE( CO_E_NOTINITIALIZED != hr_marshal, "COM is not initialized" );
// marshalling may fail if COM is not initialized
// for the calling thread which is a program time
// error or because of stream errors which are runtime
// errors for instance E_OUTOFMEMORY etc.
hr = GetHGlobalFromStream(pStm, &m_hGlobal );
OSL_ENSURE( E_INVALIDARG != hr, "invalid stream passed to GetHGlobalFromStream" );
// if the marshalling failed we free the
// global memory again and set m_hGlobal
// to a defined value
if (FAILED(hr_marshal))
{
OSL_FAIL("marshalling failed");
HGLOBAL hGlobal =
GlobalFree(m_hGlobal);
OSL_ENSURE(nullptr == hGlobal, "GlobalFree failed");
m_hGlobal = nullptr;
}
}
}
CAPNDataObject::~CAPNDataObject( )
{
if (m_hGlobal)
{
sal::systools::COMReference<IStream> pStm;
HRESULT hr = CreateStreamOnHGlobal(m_hGlobal, FREE_HGLOB_ON_RELEASE, &pStm);
OSL_ENSURE( E_INVALIDARG != hr, "invalid args passed to CreateStreamOnHGlobal" );
if (SUCCEEDED(hr))
{
hr = CoReleaseMarshalData(pStm);
OSL_ENSURE(SUCCEEDED(hr), "CoReleaseMarshalData failed");
}
}
}
// IUnknown->QueryInterface
STDMETHODIMP CAPNDataObject::QueryInterface( REFIID iid, void** ppvObject )
{
OSL_ASSERT( nullptr != ppvObject );
if ( nullptr == ppvObject )
return E_INVALIDARG;
HRESULT hr = E_NOINTERFACE;
*ppvObject = nullptr;
if ( ( __uuidof( IUnknown ) == iid ) || ( __uuidof( IDataObject ) == iid ) )
{
*ppvObject = static_cast< IUnknown* >( this );
AddRef( );
hr = S_OK;
}
return hr;
}
// IUnknown->AddRef
STDMETHODIMP_(ULONG) CAPNDataObject::AddRef( )
{
return static_cast< ULONG >( InterlockedIncrement( &m_nRefCnt ) );
}
// IUnknown->Release
STDMETHODIMP_(ULONG) CAPNDataObject::Release( )
{
// we need a helper variable because it's not allowed to access
// a member variable after an object is destroyed
ULONG nRefCnt = static_cast< ULONG >( InterlockedDecrement( &m_nRefCnt ) );
if ( 0 == nRefCnt )
delete this;
return nRefCnt;
}
// IDataObject->GetData
STDMETHODIMP CAPNDataObject::GetData( FORMATETC * pFormatetc, STGMEDIUM * pmedium )
{
HRESULT hr = m_rIDataObjectOrg->GetData( pFormatetc, pmedium );
if (RPC_E_WRONG_THREAD == hr)
{
IDataObjectPtr pIDOTmp;
hr = MarshalIDataObjectIntoCurrentApartment(&pIDOTmp);
if (SUCCEEDED(hr))
hr = pIDOTmp->GetData(pFormatetc, pmedium);
}
return hr;
}
// IDataObject->EnumFormatEtc
STDMETHODIMP CAPNDataObject::EnumFormatEtc( DWORD dwDirection, IEnumFORMATETC** ppenumFormatetc )
{
HRESULT hr = m_rIDataObjectOrg->EnumFormatEtc(dwDirection, ppenumFormatetc);
if (RPC_E_WRONG_THREAD == hr)
{
IDataObjectPtr pIDOTmp;
hr = MarshalIDataObjectIntoCurrentApartment(&pIDOTmp);
if (SUCCEEDED(hr))
hr = pIDOTmp->EnumFormatEtc(dwDirection, ppenumFormatetc);
}
return hr;
}
// IDataObject->QueryGetData
STDMETHODIMP CAPNDataObject::QueryGetData( FORMATETC * pFormatetc )
{
HRESULT hr = m_rIDataObjectOrg->QueryGetData( pFormatetc );
if (RPC_E_WRONG_THREAD == hr)
{
IDataObjectPtr pIDOTmp;
hr = MarshalIDataObjectIntoCurrentApartment( &pIDOTmp );
if (SUCCEEDED(hr))
hr = pIDOTmp->QueryGetData(pFormatetc);
}
return hr;
}
// IDataObject->GetDataHere
STDMETHODIMP CAPNDataObject::GetDataHere( FORMATETC * pFormatetc, STGMEDIUM * pmedium )
{
HRESULT hr = m_rIDataObjectOrg->GetDataHere(pFormatetc, pmedium);
if (RPC_E_WRONG_THREAD == hr)
{
IDataObjectPtr pIDOTmp;
hr = MarshalIDataObjectIntoCurrentApartment(&pIDOTmp);
if (SUCCEEDED(hr))
hr = pIDOTmp->GetDataHere(pFormatetc, pmedium);
}
return hr;
}
// IDataObject->GetCanonicalFormatEtc
STDMETHODIMP CAPNDataObject::GetCanonicalFormatEtc(FORMATETC * pFormatectIn, FORMATETC * pFormatetcOut)
{
HRESULT hr = m_rIDataObjectOrg->GetCanonicalFormatEtc( pFormatectIn, pFormatetcOut );
if (RPC_E_WRONG_THREAD == hr)
{
IDataObjectPtr pIDOTmp;
hr = MarshalIDataObjectIntoCurrentApartment(&pIDOTmp);
if (SUCCEEDED(hr))
hr = pIDOTmp->GetCanonicalFormatEtc(pFormatectIn, pFormatetcOut);
}
return hr;
}
// IDataObject->SetData
STDMETHODIMP CAPNDataObject::SetData( FORMATETC * pFormatetc, STGMEDIUM * pmedium, BOOL fRelease )
{
HRESULT hr = m_rIDataObjectOrg->SetData( pFormatetc, pmedium, fRelease );
if (RPC_E_WRONG_THREAD == hr)
{
IDataObjectPtr pIDOTmp;
hr = MarshalIDataObjectIntoCurrentApartment(&pIDOTmp);
if (SUCCEEDED(hr))
hr = pIDOTmp->SetData(pFormatetc, pmedium, fRelease);
}
return hr;
}
// IDataObject->DAdvise
STDMETHODIMP CAPNDataObject::DAdvise( FORMATETC * pFormatetc, DWORD advf, IAdviseSink * pAdvSink, DWORD * pdwConnection )
{
HRESULT hr = m_rIDataObjectOrg->DAdvise(pFormatetc, advf, pAdvSink, pdwConnection);
if (RPC_E_WRONG_THREAD == hr)
{
IDataObjectPtr pIDOTmp;
hr = MarshalIDataObjectIntoCurrentApartment(&pIDOTmp);
if (SUCCEEDED(hr))
hr = pIDOTmp->DAdvise(pFormatetc, advf, pAdvSink, pdwConnection);
}
return hr;
}
// IDataObject->DUnadvise
STDMETHODIMP CAPNDataObject::DUnadvise( DWORD dwConnection )
{
HRESULT hr = m_rIDataObjectOrg->DUnadvise( dwConnection );
if (RPC_E_WRONG_THREAD == hr)
{
IDataObjectPtr pIDOTmp;
hr = MarshalIDataObjectIntoCurrentApartment(&pIDOTmp);
if (SUCCEEDED(hr))
hr = pIDOTmp->DUnadvise(dwConnection);
}
return hr;
}
// IDataObject->EnumDAdvise
STDMETHODIMP CAPNDataObject::EnumDAdvise( IEnumSTATDATA ** ppenumAdvise )
{
HRESULT hr = m_rIDataObjectOrg->EnumDAdvise(ppenumAdvise);
if (RPC_E_WRONG_THREAD == hr)
{
IDataObjectPtr pIDOTmp;
hr = MarshalIDataObjectIntoCurrentApartment(&pIDOTmp);
if (SUCCEEDED(hr))
hr = pIDOTmp->EnumDAdvise(ppenumAdvise);
}
return hr;
}
// helper function
HRESULT CAPNDataObject::MarshalIDataObjectIntoCurrentApartment( IDataObject** ppIDataObj )
{
assert(ppIDataObj);
*ppIDataObj = nullptr;
HRESULT hr = E_FAIL;
if (m_hGlobal)
{
sal::systools::COMReference<IStream> pStm;
hr = CreateStreamOnHGlobal(m_hGlobal, KEEP_HGLOB_ON_RELEASE, &pStm);
OSL_ENSURE(E_INVALIDARG != hr, "CreateStreamOnHGlobal with invalid args called");
if (SUCCEEDED(hr))
{
hr = CoUnmarshalInterface(pStm, IID_PPV_ARGS(ppIDataObj));
OSL_ENSURE(CO_E_NOTINITIALIZED != hr, "COM is not initialized");
}
}
return hr;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|