1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include <embservconst.h>
25 #include "inprocembobj.h"
26 
27 namespace inprocserv
28 {
29 
30 #ifdef OWNDEBUG
31 //-------------------------------------------------------------------------------
WriteDebugInfo(DWORD pThis,char * pString,DWORD nToWrite)32 void WriteDebugInfo( DWORD pThis, char* pString, DWORD nToWrite )
33 {
34     if ( nToWrite )
35     {
36         char pNumber[12];
37         pNumber[0] = '0';
38         pNumber[1] = 'x';
39         for ( int nInd = 0; nInd < 8; nInd++ )
40             pNumber[nInd+2] = (char)( ( pThis / ( 1 << ( 7 - nInd ) ) ) % 16 ) + 48;
41         pNumber[10] = ' ';
42         pNumber[11] = 0;
43 
44         HANDLE pFile = CreateFileA( "h:\\inproc.log", GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, 0, NULL );
45         if ( pFile )
46         {
47             DWORD dwWritten = 0;
48             SetFilePointer( pFile, 0, 0, FILE_END );
49             WriteFile( pFile, pNumber, 11, &dwWritten, NULL );
50             WriteFile( pFile, pString, nToWrite - 1, &dwWritten, NULL );
51             CloseHandle( pFile );
52         }
53     }
54 }
55 #endif
56 
57 //-------------------------------------------------------------------------------
StringsEqual(LPCOLESTR pszNameFromOutside,wchar_t * pOwnName)58 BOOL StringsEqual( LPCOLESTR pszNameFromOutside, wchar_t* pOwnName )
59 {
60     BOOL bResult = TRUE;
61 
62     if ( pszNameFromOutside && pOwnName )
63     {
64         for ( int nInd = 0; pszNameFromOutside[nInd] != 0 || pOwnName[nInd] != 0; nInd++ )
65         {
66             if ( pszNameFromOutside[nInd] != pOwnName[nInd] )
67             {
68                 bResult = FALSE;
69                 break;
70             }
71         }
72     }
73     else if ( pszNameFromOutside || pOwnName )
74         bResult = FALSE;
75 
76     return bResult;
77 }
78 
79 //-------------------------------------------------------------------------------
Init()80 HRESULT InprocEmbedDocument_Impl::Init()
81 {
82 	return S_OK;
83 }
84 
85 //-------------------------------------------------------------------------------
SetName(LPCOLESTR pszNameFromOutside,wchar_t * & pOwnName)86 void InprocEmbedDocument_Impl::SetName( LPCOLESTR pszNameFromOutside, wchar_t*& pOwnName )
87 {
88     if ( !pszNameFromOutside )
89         return;
90 
91     // copy the string
92     size_t nLen = 0;
93     while( pszNameFromOutside[nLen] != 0 )
94         nLen++;
95 
96     if ( pOwnName )
97     {
98         delete[] pOwnName;
99         pOwnName = NULL;
100     }
101 
102     pOwnName = new wchar_t[nLen+1];
103     for ( size_t nInd = 0; nInd < nLen; nInd++ )
104         pOwnName[nInd] = pszNameFromOutside[nInd];
105     pOwnName[nLen] = 0;
106 }
107 
108 //-------------------------------------------------------------------------------
CheckDefHandler()109 BOOL InprocEmbedDocument_Impl::CheckDefHandler()
110 {
111     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::CheckDefHandler()" );
112     // set the own listener
113     if ( m_pOleAdvises[0] == NULL )
114     {
115         WRITEDEBUGINFO( "InprocEmbedDocument_Impl::CheckDefHandler()" );
116         m_pOleAdvises[0] = new OleWrapperAdviseSink();
117     }
118     else
119     {
120         WRITEDEBUGINFO( "InprocEmbedDocument_Impl::CheckDefHandler()" );
121         if ( m_pOleAdvises[0]->IsClosed() )
122         {
123             WRITEDEBUGINFO( "InprocEmbedDocument_Impl::CheckDefHandler()" );
124             if ( m_pDefHandler )
125             {
126                 WRITEDEBUGINFO( "InprocEmbedDocument_Impl::CheckDefHandler()" );
127                 // deregister all the listeners
128 
129                 ComSmart< IOleObject > pOleObject;
130                 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, (void**)&pOleObject );
131                 if ( SUCCEEDED( hr ) && pOleObject )
132                 {
133                     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::CheckDefHandler()" );
134                     for ( DWORD nInd = 0; nInd < DEFAULT_ARRAY_LEN; nInd++ )
135                         if ( m_pOleAdvises[nInd] )
136                         {
137                             DWORD nID = m_pOleAdvises[nInd]->GetRegID();
138                             pOleObject->Unadvise( nID );
139                             m_pOleAdvises[nInd]->SetRegID( 0 );
140                         }
141 
142                     pOleObject->SetClientSite( NULL );
143                 }
144 
145                 WRITEDEBUGINFO( "InprocEmbedDocument_Impl::CheckDefHandler()" );
146                 ComSmart< IDataObject > pIDataObject;
147                 hr = m_pDefHandler->QueryInterface( IID_IDataObject, (void**)&pIDataObject );
148                 if ( SUCCEEDED( hr ) && pIDataObject )
149                 {
150                     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::CheckDefHandler()" );
151                     for ( DWORD nInd = 0; nInd < DEFAULT_ARRAY_LEN; nInd++ )
152                         if ( m_pDataAdvises[nInd] )
153                         {
154                             DWORD nID = m_pDataAdvises[nInd]->GetRegID();
155                             pIDataObject->DUnadvise( nID );
156                             m_pDataAdvises[nInd]->SetRegID( 0 );
157                         }
158                 }
159 
160                 WRITEDEBUGINFO( "InprocEmbedDocument_Impl::CheckDefHandler()" );
161                 ComSmart< IViewObject > pIViewObject;
162                 hr = m_pDefHandler->QueryInterface( IID_IViewObject, (void**)&pIViewObject );
163                 if ( SUCCEEDED( hr ) && pIViewObject )
164                 {
165                     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::CheckDefHandler()" );
166                     if ( m_pViewAdvise )
167                         pIViewObject->SetAdvise( m_pViewAdvise->GetAspect(), m_pViewAdvise->GetViewAdviseFlag(), NULL );
168                     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::CheckDefHandler()" );
169                 }
170                 WRITEDEBUGINFO( "InprocEmbedDocument_Impl::CheckDefHandler()" );
171 
172                 ComSmart< IPersistStorage > pPersist;
173                 hr = m_pDefHandler->QueryInterface( IID_IPersistStorage, (void**)&pPersist );
174                 if ( SUCCEEDED( hr ) && pPersist )
175                 {
176                     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::CheckDefHandler()" );
177                     // disconnect the old wrapper from the storage
178                     pPersist->HandsOffStorage();
179                 }
180 
181                 m_pDefHandler = NULL;
182             }
183 
184             WRITEDEBUGINFO( "InprocEmbedDocument_Impl::CheckDefHandler()" );
185             m_pOleAdvises[0]->UnsetClosed();
186         }
187     }
188 
189     if ( m_nCallsOnStack )
190         return FALSE;
191 
192     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::CheckDefHandler()" );
193     if ( !m_pDefHandler )
194     {
195         WRITEDEBUGINFO( "InprocEmbedDocument_Impl::CheckDefHandler()" );
196         // create a new default inprocess handler
197         HRESULT hr = OleCreateDefaultHandler( m_guid, NULL, IID_IUnknown, (void**)&m_pDefHandler );
198         if ( SUCCEEDED( hr ) )
199         {
200             WRITEDEBUGINFO( "InprocEmbedDocument_Impl::CheckDefHandler()" );
201 //            // reinit the handler
202 //            ComSmart< IRunnableObject > pIRunObj;
203 //            hr = m_pDefHandler->QueryInterface( IID_IRunnableObject, (void**)&pIRunObj );
204 //
205 //            if ( SUCCEEDED( hr ) && pIRunObj )
206             {
207 //                {
208 //                    ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
209 //                    hr = pIRunObj->Run( NULL );
210 //                }
211 //
212 //                if ( SUCCEEDED( hr ) )
213                 {
214                     if ( m_nInitMode == INIT_FROM_STORAGE )
215                     {
216                         WRITEDEBUGINFO( "InprocEmbedDocument_Impl::CheckDefHandler()" );
217                         ComSmart< IPersistStorage > pPersist;
218                         hr = m_pDefHandler->QueryInterface( IID_IPersistStorage, (void**)&pPersist );
219 
220                         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
221                         if ( SUCCEEDED( hr ) && pPersist && m_pStorage )
222                             hr = pPersist->InitNew( m_pStorage );
223                     }
224                     else if ( m_nInitMode == LOAD_FROM_STORAGE )
225                     {
226                         WRITEDEBUGINFO( "InprocEmbedDocument_Impl::CheckDefHandler()" );
227                         ComSmart< IPersistStorage > pPersist;
228                         hr = m_pDefHandler->QueryInterface( IID_IPersistStorage, (void**)&pPersist );
229 
230                         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
231                         if ( SUCCEEDED( hr ) && pPersist && m_pStorage )
232                             hr = pPersist->Load( m_pStorage );
233                     }
234                     else if ( m_nInitMode == LOAD_FROM_FILE )
235                     {
236                         WRITEDEBUGINFO( "InprocEmbedDocument_Impl::CheckDefHandler()" );
237                         ComSmart< IPersistFile > pPersistFile;
238                         hr = m_pDefHandler->QueryInterface( IID_IPersistFile, (void**)&pPersistFile );
239 
240                         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
241                         if ( SUCCEEDED( hr ) && pPersistFile && m_pFileName )
242                             hr = pPersistFile->Load( m_pFileName, m_nFileOpenMode );
243                     }
244                     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::CheckDefHandler()" );
245                 }
246             }
247         }
248 
249         WRITEDEBUGINFO( "InprocEmbedDocument_Impl::CheckDefHandler()" );
250         if ( !SUCCEEDED( hr ) || !m_pDefHandler )
251         {
252             WRITEDEBUGINFO( "InprocEmbedDocument_Impl::CheckDefHandler()" );
253             m_pDefHandler = NULL;
254             return FALSE;
255         }
256 
257         // register all the listeners new
258 
259         ComSmart< IOleObject > pOleObject;
260         hr = m_pDefHandler->QueryInterface( IID_IOleObject, (void**)&pOleObject );
261         if ( SUCCEEDED( hr ) && pOleObject )
262         {
263             WRITEDEBUGINFO( "InprocEmbedDocument_Impl::CheckDefHandler()" );
264             if ( m_pClientSite )
265                 pOleObject->SetClientSite( m_pClientSite );
266 
267             WRITEDEBUGINFO( "InprocEmbedDocument_Impl::CheckDefHandler()" );
268             for ( DWORD nInd = 0; nInd < DEFAULT_ARRAY_LEN; nInd++ )
269                 if ( m_pOleAdvises[nInd] )
270                 {
271                     DWORD nRegID = 0;
272                     if ( SUCCEEDED( pOleObject->Advise( m_pOleAdvises[nInd], &nRegID ) ) && nRegID > 0 )
273                         m_pOleAdvises[nInd]->SetRegID( nRegID );
274                 }
275         }
276 
277         WRITEDEBUGINFO( "InprocEmbedDocument_Impl::CheckDefHandler()" );
278         ComSmart< IDataObject > pIDataObject;
279         hr = m_pDefHandler->QueryInterface( IID_IDataObject, (void**)&pIDataObject );
280         if ( SUCCEEDED( hr ) && pIDataObject )
281         {
282             WRITEDEBUGINFO( "InprocEmbedDocument_Impl::CheckDefHandler()" );
283             for ( DWORD nInd = 0; nInd < DEFAULT_ARRAY_LEN; nInd++ )
284                 if ( m_pDataAdvises[nInd] )
285                 {
286                     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::CheckDefHandler()" );
287                     DWORD nRegID = 0;
288                     if ( SUCCEEDED( pIDataObject->DAdvise( m_pDataAdvises[nInd]->GetFormatEtc(), m_pDataAdvises[nInd]->GetDataAdviseFlag(), m_pDataAdvises[nInd], &nRegID ) ) && nRegID > 0 )
289                         m_pDataAdvises[nInd]->SetRegID( nRegID );
290                     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::CheckDefHandler()" );
291                 }
292         }
293 
294         WRITEDEBUGINFO( "InprocEmbedDocument_Impl::CheckDefHandler()" );
295         ComSmart< IViewObject > pIViewObject;
296         hr = m_pDefHandler->QueryInterface( IID_IViewObject, (void**)&pIViewObject );
297         if ( SUCCEEDED( hr ) && pIViewObject )
298         {
299             WRITEDEBUGINFO( "InprocEmbedDocument_Impl::CheckDefHandler()" );
300             if ( m_pViewAdvise )
301                 pIViewObject->SetAdvise( m_pViewAdvise->GetAspect(), m_pViewAdvise->GetViewAdviseFlag(), m_pViewAdvise );
302             WRITEDEBUGINFO( "InprocEmbedDocument_Impl::CheckDefHandler()" );
303         }
304     }
305 
306     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::CheckDefHandler()" );
307 
308     return TRUE;
309 }
310 
311 //-------------------------------------------------------------------------------
InsertAdviseLinkToList(const ComSmart<OleWrapperAdviseSink> & pOwnAdvise,ComSmart<OleWrapperAdviseSink> pAdvises[])312 DWORD InprocEmbedDocument_Impl::InsertAdviseLinkToList( const ComSmart<OleWrapperAdviseSink>& pOwnAdvise, ComSmart< OleWrapperAdviseSink > pAdvises[] )
313 {
314     // the result should start from 1 in case of success, the element 0 can be used for own needs
315     DWORD nResult = 0;
316 
317     if ( pOwnAdvise )
318     {
319         for ( DWORD nInd = 1; nInd < DEFAULT_ARRAY_LEN && nResult == 0; nInd++ )
320         {
321             if ( pAdvises[nInd] == pOwnAdvise )
322             {
323                 nResult = nInd;
324             }
325             else if ( pAdvises[nInd] == NULL )
326             {
327                 pAdvises[nInd] = pOwnAdvise;
328                 nResult = nInd;
329             }
330         }
331     }
332 
333     return nResult;
334 }
335 
336 //-------------------------------------------------------------------------------
Clean()337 void InprocEmbedDocument_Impl::Clean()
338 {
339     m_pDefHandler = (IUnknown*)NULL;
340 
341     // no DisconnectOrigAdvise() call here, since it is no explicit disconnection
342     for ( DWORD nInd = 0; nInd < DEFAULT_ARRAY_LEN; nInd++ )
343     {
344         if ( m_pOleAdvises[nInd] )
345         {
346             ComSmart< OleWrapperAdviseSink > pAdvise = m_pOleAdvises[nInd];
347             m_pOleAdvises[nInd] = NULL;
348         }
349 
350         if ( m_pDataAdvises[nInd] )
351         {
352             ComSmart< OleWrapperAdviseSink > pAdvise = m_pDataAdvises[nInd];
353             m_pDataAdvises[nInd] = NULL;
354         }
355     }
356 
357     m_pViewAdvise = NULL;
358 
359     m_nInitMode = NOINIT;
360     m_pStorage = NULL;
361 
362     if ( m_pOleContainer )
363     {
364         m_pOleContainer->LockContainer( FALSE );
365         m_pOleContainer = NULL;
366     }
367 
368     m_pClientSite = NULL;
369 
370     m_nFileOpenMode = 0;
371     if ( m_pFileName )
372     {
373         delete m_pFileName;
374         m_pFileName = NULL;
375     }
376 }
377 
378 // IUnknown
379 //-------------------------------------------------------------------------------
QueryInterface(REFIID riid,void FAR * FAR * ppv)380 STDMETHODIMP InprocEmbedDocument_Impl::QueryInterface( REFIID riid, void FAR* FAR* ppv )
381 {
382     if(IsEqualIID(riid, IID_IUnknown))
383 	{
384 		AddRef();
385 		*ppv = (IUnknown*) (IPersistStorage*) this;
386 		return S_OK;
387     }
388     else if (IsEqualIID(riid, IID_IPersist))
389 	{
390 		AddRef();
391 		*ppv = (IPersist*) (IPersistStorage*) this;
392 		return S_OK;
393 	}
394     else if (IsEqualIID(riid, IID_IPersistStorage))
395 	{
396 		AddRef();
397 		*ppv = (IPersistStorage*) this;
398 		return S_OK;
399 	}
400     else if (IsEqualIID(riid, IID_IDataObject))
401 	{
402 		AddRef();
403 		*ppv = (IDataObject*) this;
404 		return S_OK;
405 	}
406     else if (IsEqualIID(riid, IID_IOleObject))
407 	{
408 		AddRef();
409 		*ppv = (IOleObject*) this;
410 		return S_OK;
411 	}
412     else if (IsEqualIID(riid, IID_IPersistFile))
413 	{
414 		AddRef();
415 		*ppv = (IPersistFile*) this;
416 		return S_OK;
417 	}
418     else if (IsEqualIID(riid, IID_IRunnableObject))
419 	{
420 		AddRef();
421 		*ppv = (IRunnableObject*) this;
422 		return S_OK;
423 	}
424     else if (IsEqualIID(riid, IID_IViewObject))
425     {
426         AddRef();
427         *ppv = (IViewObject*) this;
428         return S_OK;
429     }
430     else if (IsEqualIID(riid, IID_IViewObject2))
431     {
432         AddRef();
433         *ppv = (IViewObject2*) this;
434         return S_OK;
435     }
436     else if (IsEqualIID(riid, IID_IOleCache))
437     {
438         AddRef();
439         *ppv = (IOleCache*) &m_aInternalCache;
440         return S_OK;
441     }
442     else if (IsEqualIID(riid, IID_IOleCache2))
443     {
444         AddRef();
445         *ppv = (IOleCache2*) &m_aInternalCache;
446         return S_OK;
447     }
448     else if (IsEqualIID(riid, IID_IOleWindow))
449     {
450         AddRef();
451         *ppv = (IOleWindow*) this;
452         return S_OK;
453     }
454     else if (IsEqualIID(riid, IID_IOleInPlaceObject))
455     {
456         AddRef();
457         *ppv = (IOleInPlaceObject*) this;
458         return S_OK;
459     }
460     else if (IsEqualIID(riid, IID_IDispatch))
461     {
462         AddRef();
463         *ppv = (IDispatch*) this;
464         return S_OK;
465     }
466 
467     *ppv = NULL;
468     return ResultFromScode(E_NOINTERFACE);
469 }
470 
471 //-------------------------------------------------------------------------------
STDMETHODIMP_(ULONG)472 STDMETHODIMP_(ULONG) InprocEmbedDocument_Impl::AddRef()
473 {
474 	return ++m_refCount;
475 }
476 
477 //-------------------------------------------------------------------------------
STDMETHODIMP_(ULONG)478 STDMETHODIMP_(ULONG) InprocEmbedDocument_Impl::Release()
479 {
480     // unfortunately there are reentrance problems in mfc that have to be workarounded
481 	sal_Int32 nCount = m_refCount > 0 ? --m_refCount : 0;
482 	if ( nCount == 0 && !m_bDeleted )
483 	{
484         // deleting of this object can trigger deleting of mfc objects that will try to delete this object one more time
485         m_bDeleted = TRUE;
486 
487         Clean();
488 		delete this;
489 	}
490     return nCount;
491 }
492 
493 // IPersist
494 //-------------------------------------------------------------------------------
GetClassID(CLSID * pClassId)495 STDMETHODIMP InprocEmbedDocument_Impl::GetClassID( CLSID* pClassId )
496 {
497 	*pClassId = *&m_guid;
498 	return S_OK;
499 }
500 
501 // IPersistStorage
502 //-------------------------------------------------------------------------------
IsDirty()503 STDMETHODIMP InprocEmbedDocument_Impl::IsDirty()
504 {
505     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::IsDirty()1" );
506     if ( m_pDefHandler == NULL || m_pOleAdvises[0] == NULL || m_pOleAdvises[0]->IsClosed() )
507         return S_FALSE;
508 
509     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::IsDirty()2" );
510     if ( CheckDefHandler() )
511     {
512         ComSmart< IPersistStorage > pPersist;
513         HRESULT hr = m_pDefHandler->QueryInterface( IID_IPersistStorage, (void**)&pPersist );
514 
515         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
516         if ( SUCCEEDED( hr ) && pPersist )
517             return pPersist->IsDirty();
518     }
519 
520     return E_FAIL;
521 }
522 
523 //-------------------------------------------------------------------------------
InitNew(IStorage * pStg)524 STDMETHODIMP InprocEmbedDocument_Impl::InitNew( IStorage *pStg )
525 {
526     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::InitNew( IStorage *pStg )" );
527     if ( CheckDefHandler() )
528     {
529         ComSmart< IPersistStorage > pPersist;
530         HRESULT hr = m_pDefHandler->QueryInterface( IID_IPersistStorage, (void**)&pPersist );
531 
532         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
533         if ( SUCCEEDED( hr ) && pPersist )
534         {
535             hr = pPersist->InitNew( pStg );
536             if ( SUCCEEDED( hr ) )
537             {
538                 m_nInitMode = INIT_FROM_STORAGE;
539                 m_pStorage = pStg;
540 
541                 m_nFileOpenMode = 0;
542                 if ( m_pFileName )
543                 {
544                     delete[] m_pFileName;
545                     m_pFileName = NULL;
546                 }
547             }
548 
549             return hr;
550         }
551     }
552 
553     return E_FAIL;
554 }
555 
556 //-------------------------------------------------------------------------------
Load(IStorage * pStg)557 STDMETHODIMP InprocEmbedDocument_Impl::Load( IStorage *pStg )
558 {
559     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::Load( IStorage *pStg )" );
560     if ( CheckDefHandler() )
561     {
562         WRITEDEBUGINFO( "InprocEmbedDocument_Impl::Load( IStorage *pStg )" );
563         ComSmart< IPersistStorage > pPersist;
564         HRESULT hr = m_pDefHandler->QueryInterface( IID_IPersistStorage, (void**)&pPersist );
565 
566         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
567         if ( SUCCEEDED( hr ) && pPersist )
568         {
569             WRITEDEBUGINFO( "InprocEmbedDocument_Impl::Load( IStorage *pStg )" );
570             hr = pPersist->Load( pStg );
571             if ( SUCCEEDED( hr ) )
572             {
573                 WRITEDEBUGINFO( "InprocEmbedDocument_Impl::Load( IStorage *pStg )" );
574                 m_nInitMode = LOAD_FROM_STORAGE;
575                 m_pStorage = pStg;
576 
577                 m_nFileOpenMode = 0;
578                 if ( m_pFileName )
579                 {
580                     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::Load( IStorage *pStg )" );
581                     delete[] m_pFileName;
582                     m_pFileName = NULL;
583                 }
584             }
585 
586             return hr;
587         }
588     }
589 
590     return E_FAIL;
591 }
592 
593 //-------------------------------------------------------------------------------
Save(IStorage * pStgSave,BOOL fSameAsLoad)594 STDMETHODIMP InprocEmbedDocument_Impl::Save( IStorage *pStgSave, BOOL fSameAsLoad )
595 {
596     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::Save( IStorage *pStgSave, BOOL fSameAsLoad )" );
597     if ( fSameAsLoad && ( m_pDefHandler == NULL || m_pOleAdvises[0] == NULL || m_pOleAdvises[0]->IsClosed() ) )
598         return S_OK;
599 
600     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::Save( IStorage *pStgSave, BOOL fSameAsLoad )" );
601     if ( CheckDefHandler() )
602     {
603         ComSmart< IPersistStorage > pPersist;
604         HRESULT hr = m_pDefHandler->QueryInterface( IID_IPersistStorage, (void**)&pPersist );
605 
606         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
607         if ( SUCCEEDED( hr ) && pPersist )
608             return pPersist->Save( pStgSave, fSameAsLoad );
609     }
610 
611     return E_FAIL;
612 }
613 
614 //-------------------------------------------------------------------------------
SaveCompleted(IStorage * pStgNew)615 STDMETHODIMP InprocEmbedDocument_Impl::SaveCompleted( IStorage *pStgNew )
616 {
617     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::SaveCompleted( IStorage *pStgNew )" );
618     if ( m_pDefHandler == NULL || m_pOleAdvises[0] == NULL || m_pOleAdvises[0]->IsClosed() )
619     {
620         if ( pStgNew )
621             m_pStorage = pStgNew;
622 
623         return S_OK;
624     }
625 
626     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::SaveCompleted( IStorage *pStgNew )" );
627     if ( CheckDefHandler() )
628     {
629         ComSmart< IPersistStorage > pPersist;
630         HRESULT hr = m_pDefHandler->QueryInterface( IID_IPersistStorage, (void**)&pPersist );
631 
632         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
633         if ( SUCCEEDED( hr ) && pPersist )
634         {
635             hr = pPersist->SaveCompleted( pStgNew );
636             if ( SUCCEEDED( hr ) )
637             {
638                 WRITEDEBUGINFO( "InprocEmbedDocument_Impl::SaveCompleted( IStorage *pStgNew )" );
639                 m_nInitMode = LOAD_FROM_STORAGE;
640                 if ( pStgNew )
641                 {
642                     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::SaveCompleted( IStorage *pStgNew )" );
643                     m_pStorage = pStgNew;
644                 }
645 
646                 m_nFileOpenMode = 0;
647                 if ( m_pFileName )
648                 {
649                     delete[] m_pFileName;
650                     m_pFileName = NULL;
651                 }
652             }
653 
654             return hr;
655         }
656     }
657 
658     return E_FAIL;
659 }
660 
661 //-------------------------------------------------------------------------------
HandsOffStorage()662 STDMETHODIMP InprocEmbedDocument_Impl::HandsOffStorage()
663 {
664     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::HandsOffStorage()" );
665     if ( CheckDefHandler() )
666     {
667         ComSmart< IPersistStorage > pPersist;
668         HRESULT hr = m_pDefHandler->QueryInterface( IID_IPersistStorage, (void**)&pPersist );
669 
670         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
671         if ( SUCCEEDED( hr ) && pPersist )
672         {
673             hr = pPersist->HandsOffStorage();
674             if ( SUCCEEDED( hr ) )
675             {
676                 m_pStorage = NULL;
677                 WRITEDEBUGINFO( "InprocEmbedDocument_Impl::HandsOffStorage()" );
678             }
679 
680             return hr;
681         }
682     }
683 
684     return E_FAIL;
685 }
686 
687 // IPersistFile
688 //-------------------------------------------------------------------------------
Load(LPCOLESTR pszFileName,DWORD dwMode)689 STDMETHODIMP InprocEmbedDocument_Impl::Load( LPCOLESTR pszFileName, DWORD dwMode )
690 {
691     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::Load( LPCOLESTR pszFileName, DWORD dwMode )" );
692     if ( CheckDefHandler() && pszFileName )
693     {
694         ComSmart< IPersistFile > pPersist;
695         HRESULT hr = m_pDefHandler->QueryInterface( IID_IPersistFile, (void**)&pPersist );
696 
697         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
698         if ( SUCCEEDED( hr ) && pPersist )
699         {
700             hr = pPersist->Load( pszFileName, dwMode );
701             if ( SUCCEEDED( hr ) )
702             {
703                 m_nInitMode = LOAD_FROM_FILE;
704                 if ( m_pStorage )
705                     m_pStorage = NULL;
706 
707                 m_nFileOpenMode = dwMode;
708                 // copy the string
709                 SetName( pszFileName, m_pFileName );
710             }
711 
712             return hr;
713         }
714     }
715 
716     return E_FAIL;
717 }
718 
719 //-------------------------------------------------------------------------------
Save(LPCOLESTR pszFileName,BOOL fRemember)720 STDMETHODIMP InprocEmbedDocument_Impl::Save( LPCOLESTR pszFileName, BOOL fRemember )
721 {
722     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::Save( LPCOLESTR pszFileName, BOOL fRemember )" );
723     if ( CheckDefHandler() )
724     {
725         ComSmart< IPersistFile > pPersist;
726         HRESULT hr = m_pDefHandler->QueryInterface( IID_IPersistFile, (void**)&pPersist );
727 
728         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
729         if ( SUCCEEDED( hr ) && pPersist )
730             return pPersist->Save( pszFileName, fRemember );
731     }
732 
733     return E_FAIL;
734 }
735 
736 //-------------------------------------------------------------------------------
SaveCompleted(LPCOLESTR pszFileName)737 STDMETHODIMP InprocEmbedDocument_Impl::SaveCompleted( LPCOLESTR pszFileName )
738 {
739     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::SaveCompleted( LPCOLESTR pszFileName )" );
740     if ( CheckDefHandler() )
741     {
742         ComSmart< IPersistFile > pPersist;
743         HRESULT hr = m_pDefHandler->QueryInterface( IID_IPersistFile, (void**)&pPersist );
744 
745         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
746         if ( SUCCEEDED( hr ) && pPersist )
747         {
748             hr = pPersist->SaveCompleted( pszFileName );
749             if ( SUCCEEDED( hr ) )
750             {
751                 m_nInitMode = LOAD_FROM_STORAGE;
752                 if ( m_pStorage )
753                     m_pStorage = NULL;
754 
755                 m_nFileOpenMode = STGM_READWRITE; // was just written
756                 // copy the string
757                 SetName( pszFileName, m_pFileName );
758             }
759         }
760 
761     }
762 
763     return E_FAIL;
764 }
765 
766 //-------------------------------------------------------------------------------
GetCurFile(LPOLESTR * ppszFileName)767 STDMETHODIMP InprocEmbedDocument_Impl::GetCurFile( LPOLESTR *ppszFileName )
768 {
769     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::GetCurFile( LPOLESTR *ppszFileName )" );
770     if ( CheckDefHandler() )
771     {
772         ComSmart< IPersistFile > pPersist;
773         HRESULT hr = m_pDefHandler->QueryInterface( IID_IPersistFile, (void**)&pPersist );
774 
775         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
776         if ( SUCCEEDED( hr ) && pPersist )
777             return pPersist->GetCurFile( ppszFileName );
778     }
779 
780     return E_FAIL;
781 }
782 
783 // IOleObject
784 //-------------------------------------------------------------------------------
SetClientSite(IOleClientSite * pSite)785 STDMETHODIMP InprocEmbedDocument_Impl::SetClientSite( IOleClientSite* pSite )
786 {
787     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::SetClientSite( IOleClientSite* pSite )" );
788     if ( pSite == m_pClientSite )
789         return S_OK;
790 
791     if ( !pSite )
792     {
793         m_pClientSite = NULL;
794         if ( m_pOleContainer )
795         {
796             m_pOleContainer->LockContainer( FALSE );
797             m_pOleContainer = NULL;
798         }
799     }
800 
801     if ( CheckDefHandler() )
802     {
803         ComSmart< IOleObject > pOleObject;
804         HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, (void**)&pOleObject );
805 
806         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
807         if ( SUCCEEDED( hr ) && pOleObject )
808         {
809             HRESULT hr = pOleObject->SetClientSite( pSite );
810             if ( SUCCEEDED( hr ) )
811             {
812                 m_pClientSite = pSite;
813 
814                 if ( m_pOleContainer )
815                 {
816                     m_pOleContainer->LockContainer( FALSE );
817                     m_pOleContainer = NULL;
818                 }
819 
820                 m_pClientSite->GetContainer( &m_pOleContainer );
821                 if ( m_pOleContainer )
822                     m_pOleContainer->LockContainer( TRUE );
823             }
824 
825             return hr;
826         }
827     }
828 
829     return E_FAIL;
830 }
831 
832 //-------------------------------------------------------------------------------
GetClientSite(IOleClientSite ** pSite)833 STDMETHODIMP InprocEmbedDocument_Impl::GetClientSite( IOleClientSite** pSite )
834 {
835     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::GetClientSite( IOleClientSite** pSite )" );
836     if ( CheckDefHandler() )
837     {
838         ComSmart< IOleObject > pOleObject;
839         HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, (void**)&pOleObject );
840 
841         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
842         if ( SUCCEEDED( hr ) && pOleObject )
843             return pOleObject->GetClientSite( pSite );
844     }
845 
846     return E_FAIL;
847 }
848 
849 //-------------------------------------------------------------------------------
SetHostNames(LPCOLESTR szContainerApp,LPCOLESTR szContainerObj)850 STDMETHODIMP InprocEmbedDocument_Impl::SetHostNames( LPCOLESTR szContainerApp, LPCOLESTR szContainerObj )
851 {
852     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::SetHostNames( LPCOLESTR szContainerApp, LPCOLESTR szContainerObj )" );
853 
854     if ( CheckDefHandler() )
855     {
856         ComSmart< IOleObject > pOleObject;
857         HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, (void**)&pOleObject );
858 
859         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
860         if ( SUCCEEDED( hr ) && pOleObject )
861         {
862             WRITEDEBUGINFO( "InprocEmbedDocument_Impl::SetHostNames( LPCOLESTR szContainerApp, LPCOLESTR szContainerObj )" );
863             hr = pOleObject->SetHostNames( szContainerApp, szContainerObj );
864         }
865     }
866 
867     return S_OK;
868 }
869 
870 //-------------------------------------------------------------------------------
Close(DWORD dwSaveOption)871 STDMETHODIMP InprocEmbedDocument_Impl::Close( DWORD dwSaveOption )
872 {
873     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::Close( DWORD dwSaveOption )" );
874     if ( m_pDefHandler && CheckDefHandler() )
875     {
876         // no need to close if there is no default handler.
877         ComSmart< IOleObject > pOleObject;
878         HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, (void**)&pOleObject );
879 
880         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
881         if ( SUCCEEDED( hr ) && pOleObject )
882 		{
883             hr = pOleObject->Close( dwSaveOption );
884 			hr = CoDisconnectObject( (IUnknown*)(IPersistStorage*)this, 0 );
885 		}
886     }
887 
888     // if the object is closed from outside that means that it should go to uninitialized state
889     Clean();
890 
891     return S_OK;
892 }
893 
894 //-------------------------------------------------------------------------------
SetMoniker(DWORD dwWhichMoniker,IMoniker * pmk)895 STDMETHODIMP InprocEmbedDocument_Impl::SetMoniker( DWORD dwWhichMoniker, IMoniker * pmk )
896 {
897     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::SetMoniker( DWORD dwWhichMoniker, IMoniker * pmk )" );
898     if ( CheckDefHandler() )
899     {
900         ComSmart< IOleObject > pOleObject;
901         HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, (void**)&pOleObject );
902 
903         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
904         if ( SUCCEEDED( hr ) && pOleObject )
905             return pOleObject->SetMoniker( dwWhichMoniker, pmk );
906     }
907 
908     return E_FAIL;
909 }
910 
911 //-------------------------------------------------------------------------------
GetMoniker(DWORD dwAssign,DWORD dwWhichMoniker,IMoniker ** ppmk)912 STDMETHODIMP InprocEmbedDocument_Impl::GetMoniker( DWORD dwAssign, DWORD dwWhichMoniker, IMoniker ** ppmk )
913 {
914     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::GetMoniker( DWORD dwAssign, DWORD dwWhichMoniker, IMoniker ** ppmk )" );
915     if ( CheckDefHandler() )
916     {
917         ComSmart< IOleObject > pOleObject;
918         HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, (void**)&pOleObject );
919 
920         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
921         if ( SUCCEEDED( hr ) && pOleObject )
922             return pOleObject->GetMoniker( dwAssign, dwWhichMoniker, ppmk );
923     }
924 
925     return E_FAIL;
926 }
927 
928 //-------------------------------------------------------------------------------
InitFromData(IDataObject * pDataObject,BOOL fCreation,DWORD dwReserved)929 STDMETHODIMP InprocEmbedDocument_Impl::InitFromData( IDataObject * pDataObject, BOOL fCreation, DWORD dwReserved )
930 {
931     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::InitFromData( IDataObject * pDataObject, BOOL fCreation, DWORD dwReserved )" );
932     if ( CheckDefHandler() )
933     {
934         ComSmart< IOleObject > pOleObject;
935         HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, (void**)&pOleObject );
936 
937         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
938         if ( SUCCEEDED( hr ) && pOleObject )
939             return pOleObject->InitFromData( pDataObject, fCreation, dwReserved );
940     }
941 
942     return E_FAIL;
943 }
944 
945 //-------------------------------------------------------------------------------
GetClipboardData(DWORD dwReserved,IDataObject ** ppDataObject)946 STDMETHODIMP InprocEmbedDocument_Impl::GetClipboardData( DWORD dwReserved, IDataObject ** ppDataObject )
947 {
948     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::GetClipboardData( DWORD dwReserved, IDataObject ** ppDataObject )" );
949     if ( CheckDefHandler() )
950     {
951         ComSmart< IOleObject > pOleObject;
952         HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, (void**)&pOleObject );
953 
954         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
955         if ( SUCCEEDED( hr ) && pOleObject )
956             return pOleObject->GetClipboardData( dwReserved, ppDataObject );
957     }
958 
959     return E_FAIL;
960 }
961 
962 //-------------------------------------------------------------------------------
DoVerb(LONG iVerb,LPMSG pMsg,IOleClientSite * pActiveSite,LONG nLong,HWND hWin,LPCRECT pRect)963 STDMETHODIMP InprocEmbedDocument_Impl::DoVerb(
964 	LONG iVerb,
965 	LPMSG pMsg,
966 	IOleClientSite *pActiveSite,
967 	LONG nLong,
968 	HWND hWin,
969 	LPCRECT pRect )
970 {
971     WRITEDEBUGINFO( "DoVerb" );
972     if ( CheckDefHandler() )
973     {
974         WRITEDEBUGINFO( "DoVerb" MY_STRING_LINE "n" );
975         ComSmart< IOleObject > pOleObject;
976         HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, (void**)&pOleObject );
977 
978         WRITEDEBUGINFO( "DoVerb" );
979         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
980         WRITEDEBUGINFO( "DoVerb" );
981         if ( SUCCEEDED( hr ) && pOleObject )
982         {
983             WRITEDEBUGINFO( "DoVerb" );
984             hr = pOleObject->DoVerb( iVerb, pMsg, pActiveSite, nLong, hWin, pRect );
985             if ( SUCCEEDED( hr ) )
986             {
987                 WRITEDEBUGINFO( "DoVerb" );
988             }
989 
990             return hr;
991         }
992 
993         WRITEDEBUGINFO( "DoVerb" );
994     }
995 
996     return E_FAIL;
997 }
998 
999 //-------------------------------------------------------------------------------
EnumVerbs(IEnumOLEVERB ** ppEnumOleVerb)1000 STDMETHODIMP InprocEmbedDocument_Impl::EnumVerbs( IEnumOLEVERB ** ppEnumOleVerb )
1001 {
1002     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::EnumVerbs( IEnumOLEVERB ** ppEnumOleVerb )" );
1003     if ( CheckDefHandler() )
1004     {
1005         ComSmart< IOleObject > pOleObject;
1006         HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, (void**)&pOleObject );
1007 
1008         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1009         if ( SUCCEEDED( hr ) && pOleObject )
1010             return pOleObject->EnumVerbs( ppEnumOleVerb );
1011     }
1012 
1013     return E_FAIL;
1014 }
1015 
1016 //-------------------------------------------------------------------------------
Update()1017 STDMETHODIMP InprocEmbedDocument_Impl::Update()
1018 {
1019     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::Update()" );
1020 
1021     if ( m_pDefHandler && CheckDefHandler() )
1022     {
1023         ComSmart< IOleObject > pOleObject;
1024         HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, (void**)&pOleObject );
1025 
1026         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1027         if ( SUCCEEDED( hr ) && pOleObject )
1028             return pOleObject->Update();
1029     }
1030 
1031     return S_OK;
1032 }
1033 
1034 //-------------------------------------------------------------------------------
IsUpToDate()1035 STDMETHODIMP InprocEmbedDocument_Impl::IsUpToDate()
1036 {
1037     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::IsUpToDate()" );
1038     if ( CheckDefHandler() )
1039     {
1040         ComSmart< IOleObject > pOleObject;
1041         HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, (void**)&pOleObject );
1042 
1043         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1044         if ( SUCCEEDED( hr ) && pOleObject )
1045             return pOleObject->IsUpToDate();
1046     }
1047 
1048     return E_FAIL;
1049 }
1050 
1051 //-------------------------------------------------------------------------------
GetUserClassID(CLSID * pClsid)1052 STDMETHODIMP InprocEmbedDocument_Impl::GetUserClassID( CLSID *pClsid )
1053 {
1054     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::GetUserClassID( CLSID *pClsid )" );
1055     if ( pClsid )
1056         *pClsid = m_guid;
1057 
1058     return S_OK;
1059 }
1060 
1061 //-------------------------------------------------------------------------------
GetUserType(DWORD dwFormOfType,LPOLESTR * pszUserType)1062 STDMETHODIMP InprocEmbedDocument_Impl::GetUserType( DWORD dwFormOfType, LPOLESTR * pszUserType )
1063 {
1064     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::GetUserType( DWORD dwFormOfType, LPOLESTR * pszUserType )" );
1065     if ( CheckDefHandler() )
1066     {
1067         ComSmart< IOleObject > pOleObject;
1068         HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, (void**)&pOleObject );
1069 
1070         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1071         if ( SUCCEEDED( hr ) && pOleObject )
1072             return pOleObject->GetUserType( dwFormOfType, pszUserType );
1073     }
1074 
1075     return E_FAIL;
1076 }
1077 
1078 //-------------------------------------------------------------------------------
SetExtent(DWORD dwDrawAspect,SIZEL * psizel)1079 STDMETHODIMP InprocEmbedDocument_Impl::SetExtent( DWORD dwDrawAspect, SIZEL *psizel )
1080 {
1081     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::SetExtent( DWORD dwDrawAspect, SIZEL *psizel )" );
1082     if ( CheckDefHandler() )
1083     {
1084         ComSmart< IOleObject > pOleObject;
1085         HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, (void**)&pOleObject );
1086 
1087         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1088         if ( SUCCEEDED( hr ) && pOleObject )
1089             return pOleObject->SetExtent( dwDrawAspect, psizel );
1090     }
1091 
1092     return E_FAIL;
1093 }
1094 
1095 //-------------------------------------------------------------------------------
GetExtent(DWORD dwDrawAspect,SIZEL * psizel)1096 STDMETHODIMP InprocEmbedDocument_Impl::GetExtent( DWORD dwDrawAspect, SIZEL * psizel )
1097 {
1098     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::GetExtent( DWORD dwDrawAspect, SIZEL * psizel )" );
1099     if ( CheckDefHandler() )
1100     {
1101         ComSmart< IOleObject > pOleObject;
1102         HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, (void**)&pOleObject );
1103 
1104         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1105         if ( SUCCEEDED( hr ) && pOleObject )
1106             return pOleObject->GetExtent( dwDrawAspect, psizel );
1107     }
1108 
1109     return E_FAIL;
1110 }
1111 
1112 //-------------------------------------------------------------------------------
Advise(IAdviseSink * pAdvSink,DWORD * pdwConnection)1113 STDMETHODIMP InprocEmbedDocument_Impl::Advise( IAdviseSink *pAdvSink, DWORD *pdwConnection )
1114 {
1115     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::Advise( IAdviseSink *pAdvSink, DWORD *pdwConnection )" );
1116 
1117     if ( !pdwConnection )
1118         return E_FAIL;
1119 
1120 	// CheckDefHandler will set the listener, avoid reusing of old listener
1121     if ( DEFAULT_ARRAY_LEN > *pdwConnection && *pdwConnection > 0 && m_pOleAdvises[*pdwConnection] )
1122     {
1123         m_pOleAdvises[*pdwConnection]->DisconnectOrigAdvise();
1124         m_pOleAdvises[*pdwConnection] = NULL;
1125     }
1126 
1127     if ( pAdvSink && CheckDefHandler() )
1128     {
1129         ComSmart< IOleObject > pOleObject;
1130         HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, (void**)&pOleObject );
1131 
1132         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1133         if ( SUCCEEDED( hr ) && pOleObject )
1134         {
1135             ComSmart< OleWrapperAdviseSink > pOwnAdvise( new OleWrapperAdviseSink( pAdvSink ) );
1136             DWORD nRegID = 0;
1137 
1138             if ( SUCCEEDED( pOleObject->Advise( pOwnAdvise, &nRegID ) ) && nRegID > 0 )
1139             {
1140                 pOwnAdvise->SetRegID( nRegID );
1141                 *pdwConnection = InsertAdviseLinkToList( pOwnAdvise, m_pOleAdvises );
1142                 if ( *pdwConnection )
1143                     return S_OK;
1144                 else
1145                     pOleObject->Unadvise( nRegID );
1146             }
1147         }
1148     }
1149 
1150     // return success always for now
1151     return S_OK;
1152 }
1153 
1154 //-------------------------------------------------------------------------------
Unadvise(DWORD dwConnection)1155 STDMETHODIMP InprocEmbedDocument_Impl::Unadvise( DWORD dwConnection )
1156 {
1157     if ( DEFAULT_ARRAY_LEN > dwConnection && dwConnection > 0 && m_pOleAdvises[dwConnection] )
1158     {
1159         if ( m_pDefHandler )
1160         {
1161             ComSmart< IOleObject > pOleObject;
1162             HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, (void**)&pOleObject );
1163 
1164             ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1165             if ( SUCCEEDED( hr ) && pOleObject )
1166             {
1167                 DWORD nID = m_pOleAdvises[dwConnection]->GetRegID();
1168                 pOleObject->Unadvise( nID );
1169             }
1170         }
1171 
1172         m_pOleAdvises[dwConnection]->DisconnectOrigAdvise();
1173         m_pOleAdvises[dwConnection] = NULL;
1174 
1175         return S_OK;
1176     }
1177 
1178     return E_FAIL;
1179 }
1180 
1181 //-------------------------------------------------------------------------------
EnumAdvise(IEnumSTATDATA **)1182 STDMETHODIMP InprocEmbedDocument_Impl::EnumAdvise( IEnumSTATDATA ** /*ppenumAdvise*/ )
1183 {
1184     return E_NOTIMPL;
1185 
1186 //    if ( CheckDefHandler() )
1187 //    {
1188 //        ComSmart< IOleObject > pOleObject;
1189 //        HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, (void**)&pOleObject );
1190 //
1191 //        ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1192 //        if ( SUCCEEDED( hr ) && pOleObject )
1193 //            return pOleObject->EnumAdvise( ppenumAdvise );
1194 //    }
1195 //
1196 //    return E_FAIL;
1197 }
1198 
1199 //-------------------------------------------------------------------------------
GetMiscStatus(DWORD dwAspect,DWORD * pdwStatus)1200 STDMETHODIMP InprocEmbedDocument_Impl::GetMiscStatus( DWORD dwAspect, DWORD * pdwStatus )
1201 {
1202     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::GetMiscStatus( DWORD dwAspect, DWORD * pdwStatus )" );
1203     if ( CheckDefHandler() )
1204     {
1205         ComSmart< IOleObject > pOleObject;
1206         HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, (void**)&pOleObject );
1207 
1208         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1209         if ( SUCCEEDED( hr ) && pOleObject )
1210             return pOleObject->GetMiscStatus( dwAspect, pdwStatus );
1211     }
1212 
1213     return E_FAIL;
1214 }
1215 
1216 //-------------------------------------------------------------------------------
SetColorScheme(LOGPALETTE * pLogpal)1217 STDMETHODIMP InprocEmbedDocument_Impl::SetColorScheme( LOGPALETTE * pLogpal )
1218 {
1219     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::SetColorScheme( LOGPALETTE * pLogpal )" );
1220     if ( CheckDefHandler() )
1221     {
1222         ComSmart< IOleObject > pOleObject;
1223         HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, (void**)&pOleObject );
1224 
1225         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1226         if ( SUCCEEDED( hr ) && pOleObject )
1227             return pOleObject->SetColorScheme( pLogpal );
1228     }
1229 
1230     return E_FAIL;
1231 }
1232 
1233 //IDataObject
1234 //-------------------------------------------------------------------------------
GetData(FORMATETC * pFormatetc,STGMEDIUM * pMedium)1235 STDMETHODIMP InprocEmbedDocument_Impl::GetData( FORMATETC * pFormatetc, STGMEDIUM * pMedium )
1236 {
1237     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::GetData( FORMATETC * pFormatetc, STGMEDIUM * pMedium )" );
1238     if ( CheckDefHandler() )
1239     {
1240         ComSmart< IDataObject > pIDataObject;
1241         HRESULT hr = m_pDefHandler->QueryInterface( IID_IDataObject, (void**)&pIDataObject );
1242 
1243         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1244         if ( SUCCEEDED( hr ) && pIDataObject )
1245             return pIDataObject->GetData( pFormatetc, pMedium );
1246     }
1247 
1248     return E_FAIL;
1249 }
1250 
1251 //-------------------------------------------------------------------------------
GetDataHere(FORMATETC * pFormatetc,STGMEDIUM * pMedium)1252 STDMETHODIMP InprocEmbedDocument_Impl::GetDataHere( FORMATETC * pFormatetc, STGMEDIUM * pMedium )
1253 {
1254     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::GetDataHere( FORMATETC * pFormatetc, STGMEDIUM * pMedium )" );
1255     if ( CheckDefHandler() )
1256     {
1257         ComSmart< IDataObject > pIDataObject;
1258         HRESULT hr = m_pDefHandler->QueryInterface( IID_IDataObject, (void**)&pIDataObject );
1259 
1260         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1261         if ( SUCCEEDED( hr ) && pIDataObject )
1262             return pIDataObject->GetDataHere( pFormatetc, pMedium );
1263     }
1264 
1265     return E_FAIL;
1266 }
1267 
1268 //-------------------------------------------------------------------------------
QueryGetData(FORMATETC * pFormatetc)1269 STDMETHODIMP InprocEmbedDocument_Impl::QueryGetData( FORMATETC * pFormatetc )
1270 {
1271     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::QueryGetData( FORMATETC * pFormatetc )" );
1272     if ( CheckDefHandler() )
1273     {
1274         ComSmart< IDataObject > pIDataObject;
1275         HRESULT hr = m_pDefHandler->QueryInterface( IID_IDataObject, (void**)&pIDataObject );
1276 
1277         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1278         if ( SUCCEEDED( hr ) && pIDataObject )
1279             return pIDataObject->QueryGetData( pFormatetc );
1280     }
1281 
1282     return E_FAIL;
1283 }
1284 
1285 //-------------------------------------------------------------------------------
GetCanonicalFormatEtc(FORMATETC * pFormatetcIn,FORMATETC * pFormatetcOut)1286 STDMETHODIMP InprocEmbedDocument_Impl::GetCanonicalFormatEtc( FORMATETC * pFormatetcIn, FORMATETC * pFormatetcOut )
1287 {
1288     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::GetCanonicalFormatEtc( FORMATETC * pFormatetcIn, FORMATETC * pFormatetcOut )" );
1289     if ( CheckDefHandler() )
1290     {
1291         ComSmart< IDataObject > pIDataObject;
1292         HRESULT hr = m_pDefHandler->QueryInterface( IID_IDataObject, (void**)&pIDataObject );
1293 
1294         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1295         if ( SUCCEEDED( hr ) && pIDataObject )
1296             return pIDataObject->GetCanonicalFormatEtc( pFormatetcIn, pFormatetcOut );
1297     }
1298 
1299     return E_FAIL;
1300 }
1301 
1302 //-------------------------------------------------------------------------------
SetData(FORMATETC * pFormatetc,STGMEDIUM * pMedium,BOOL fRelease)1303 STDMETHODIMP InprocEmbedDocument_Impl::SetData( FORMATETC * pFormatetc, STGMEDIUM * pMedium, BOOL fRelease )
1304 {
1305     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::SetData( FORMATETC * pFormatetc, STGMEDIUM * pMedium, BOOL fRelease )" );
1306     if ( CheckDefHandler() )
1307     {
1308         ComSmart< IDataObject > pIDataObject;
1309         HRESULT hr = m_pDefHandler->QueryInterface( IID_IDataObject, (void**)&pIDataObject );
1310 
1311         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1312         if ( SUCCEEDED( hr ) && pIDataObject )
1313             return pIDataObject->SetData( pFormatetc, pMedium, fRelease );
1314     }
1315 
1316     return E_FAIL;
1317 }
1318 
1319 //-------------------------------------------------------------------------------
EnumFormatEtc(DWORD dwDirection,IEnumFORMATETC ** ppFormatetc)1320 STDMETHODIMP InprocEmbedDocument_Impl::EnumFormatEtc( DWORD dwDirection, IEnumFORMATETC ** ppFormatetc )
1321 {
1322     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::EnumFormatEtc( DWORD dwDirection, IEnumFORMATETC ** ppFormatetc )" );
1323     if ( CheckDefHandler() )
1324     {
1325         ComSmart< IDataObject > pIDataObject;
1326         HRESULT hr = m_pDefHandler->QueryInterface( IID_IDataObject, (void**)&pIDataObject );
1327 
1328         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1329         if ( SUCCEEDED( hr ) && pIDataObject )
1330             return pIDataObject->EnumFormatEtc( dwDirection, ppFormatetc );
1331     }
1332 
1333     return E_FAIL;
1334 }
1335 
1336 //-------------------------------------------------------------------------------
DAdvise(FORMATETC * pFormatetc,DWORD advf,IAdviseSink * pAdvSink,DWORD * pdwConnection)1337 STDMETHODIMP InprocEmbedDocument_Impl::DAdvise( FORMATETC * pFormatetc, DWORD advf, IAdviseSink * pAdvSink, DWORD * pdwConnection )
1338 {
1339     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::DAdvise( FORMATETC * pFormatetc, DWORD advf, IAdviseSink * pAdvSink, DWORD * pdwConnection )" );
1340 
1341     if ( !pdwConnection )
1342         return E_FAIL;
1343 
1344 	// avoid reusing of the old listener
1345 	if ( m_pDefHandler && DEFAULT_ARRAY_LEN > *pdwConnection && *pdwConnection > 0 && m_pDataAdvises[*pdwConnection] )
1346     {
1347         m_pDataAdvises[*pdwConnection]->DisconnectOrigAdvise();
1348         m_pDataAdvises[*pdwConnection] = NULL;
1349     }
1350 
1351 	if ( pAdvSink && CheckDefHandler() )
1352     {
1353         ComSmart< IDataObject > pIDataObject;
1354         HRESULT hr = m_pDefHandler->QueryInterface( IID_IDataObject, (void**)&pIDataObject );
1355 
1356         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1357         if ( SUCCEEDED( hr ) && pIDataObject )
1358         {
1359             ComSmart< OleWrapperAdviseSink > pOwnAdvise( new OleWrapperAdviseSink( ComSmart<IAdviseSink>( pAdvSink ), pFormatetc, advf ) );
1360             DWORD nRegID = 0;
1361 
1362             if ( SUCCEEDED( pIDataObject->DAdvise( pFormatetc, advf, pOwnAdvise, &nRegID ) ) && nRegID > 0 )
1363             {
1364                 pOwnAdvise->SetRegID( nRegID );
1365                 *pdwConnection = InsertAdviseLinkToList( pOwnAdvise, m_pDataAdvises );
1366                 if ( *pdwConnection )
1367                     return S_OK;
1368                 else
1369                     pIDataObject->DUnadvise( nRegID );
1370             }
1371         }
1372     }
1373 
1374     // return success always for now
1375     return S_OK;
1376 }
1377 
1378 //-------------------------------------------------------------------------------
DUnadvise(DWORD dwConnection)1379 STDMETHODIMP InprocEmbedDocument_Impl::DUnadvise( DWORD dwConnection )
1380 {
1381     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::DUnadvise( DWORD dwConnection )" );
1382     if ( m_pDefHandler && DEFAULT_ARRAY_LEN > dwConnection && dwConnection > 0 && m_pDataAdvises[dwConnection] )
1383     {
1384         if ( CheckDefHandler() )
1385         {
1386             ComSmart< IDataObject > pIDataObject;
1387             HRESULT hr = m_pDefHandler->QueryInterface( IID_IDataObject, (void**)&pIDataObject );
1388 
1389             ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1390             if ( SUCCEEDED( hr ) && pIDataObject )
1391             {
1392                 DWORD nID = m_pDataAdvises[dwConnection]->GetRegID();
1393                 pIDataObject->DUnadvise( nID );
1394             }
1395         }
1396 
1397         m_pDataAdvises[dwConnection]->DisconnectOrigAdvise();
1398         m_pDataAdvises[dwConnection] = NULL;
1399 
1400         return S_OK;
1401     }
1402 
1403     return E_FAIL;
1404 }
1405 
1406 //-------------------------------------------------------------------------------
EnumDAdvise(IEnumSTATDATA ** ppenumAdvise)1407 STDMETHODIMP InprocEmbedDocument_Impl::EnumDAdvise( IEnumSTATDATA ** ppenumAdvise )
1408 {
1409     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::EnumDAdvise( IEnumSTATDATA ** ppenumAdvise )" );
1410     if ( CheckDefHandler() )
1411     {
1412         ComSmart< IDataObject > pIDataObject;
1413         HRESULT hr = m_pDefHandler->QueryInterface( IID_IDataObject, (void**)&pIDataObject );
1414 
1415         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1416         if ( SUCCEEDED( hr ) && pIDataObject )
1417             return pIDataObject->EnumDAdvise( ppenumAdvise );
1418     }
1419 
1420     return E_FAIL;
1421 }
1422 
1423 // IRunnableObject
1424 //-------------------------------------------------------------------------------
GetRunningClass(LPCLSID lpClsid)1425 STDMETHODIMP InprocEmbedDocument_Impl::GetRunningClass( LPCLSID lpClsid )
1426 {
1427     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::GetRunningClass( LPCLSID lpClsid )" );
1428     if ( CheckDefHandler() )
1429     {
1430         ComSmart< IRunnableObject > pIRunObj;
1431         HRESULT hr = m_pDefHandler->QueryInterface( IID_IRunnableObject, (void**)&pIRunObj );
1432 
1433         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1434         if ( SUCCEEDED( hr ) && pIRunObj )
1435             return pIRunObj->GetRunningClass( lpClsid );
1436     }
1437 
1438     return E_FAIL;
1439 }
1440 
1441 //-------------------------------------------------------------------------------
Run(LPBINDCTX pbc)1442 STDMETHODIMP InprocEmbedDocument_Impl::Run( LPBINDCTX pbc )
1443 {
1444     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::Run( LPBINDCTX pbc )" );
1445     if ( CheckDefHandler() )
1446     {
1447         ComSmart< IRunnableObject > pIRunObj;
1448         HRESULT hr = m_pDefHandler->QueryInterface( IID_IRunnableObject, (void**)&pIRunObj );
1449 
1450         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1451         if ( SUCCEEDED( hr ) && pIRunObj )
1452             return pIRunObj->Run( pbc );
1453     }
1454 
1455     return E_FAIL;
1456 }
1457 
1458 //-------------------------------------------------------------------------------
IsRunning()1459 BOOL STDMETHODCALLTYPE InprocEmbedDocument_Impl::IsRunning()
1460 {
1461     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::IsRunning()" );
1462    if ( CheckDefHandler() )
1463     {
1464         ComSmart< IRunnableObject > pIRunObj;
1465         HRESULT hr = m_pDefHandler->QueryInterface( IID_IRunnableObject, (void**)&pIRunObj );
1466 
1467         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1468         if ( SUCCEEDED( hr ) && pIRunObj )
1469             return pIRunObj->IsRunning();
1470     }
1471 
1472     return E_FAIL;
1473 
1474 }
1475 
1476 //-------------------------------------------------------------------------------
LockRunning(BOOL fLock,BOOL fLastUnlockCloses)1477 STDMETHODIMP InprocEmbedDocument_Impl::LockRunning( BOOL fLock, BOOL fLastUnlockCloses )
1478 {
1479     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::LockRunning( BOOL fLock, BOOL fLastUnlockCloses )" );
1480    if ( CheckDefHandler() )
1481     {
1482         ComSmart< IRunnableObject > pIRunObj;
1483         HRESULT hr = m_pDefHandler->QueryInterface( IID_IRunnableObject, (void**)&pIRunObj );
1484 
1485         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1486         if ( SUCCEEDED( hr ) && pIRunObj )
1487             return pIRunObj->LockRunning( fLock, fLastUnlockCloses );
1488     }
1489 
1490     return E_FAIL;
1491 }
1492 
1493 //-------------------------------------------------------------------------------
SetContainedObject(BOOL fContained)1494 STDMETHODIMP InprocEmbedDocument_Impl::SetContainedObject( BOOL fContained)
1495 {
1496     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::SetContainedObject( BOOL fContained)" );
1497    if ( CheckDefHandler() )
1498     {
1499         ComSmart< IRunnableObject > pIRunObj;
1500         HRESULT hr = m_pDefHandler->QueryInterface( IID_IRunnableObject, (void**)&pIRunObj );
1501 
1502         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1503         if ( SUCCEEDED( hr ) && pIRunObj )
1504             return pIRunObj->SetContainedObject( fContained );
1505     }
1506 
1507     return E_FAIL;
1508 }
1509 
1510 
1511 // IViewObject methods
1512 //-------------------------------------------------------------------------------
Draw(DWORD dwDrawAspect,LONG lindex,void * pvAspect,DVTARGETDEVICE * ptd,HDC hdcTargetDev,HDC hdcDraw,LPCRECTL lprcBounds,LPCRECTL lprcWBounds,BOOL (STDMETHODCALLTYPE * pfnContinue)(ULONG_PTR dwContinue),ULONG_PTR dwContinue)1513 STDMETHODIMP InprocEmbedDocument_Impl::Draw( DWORD dwDrawAspect, LONG lindex, void *pvAspect, DVTARGETDEVICE *ptd, HDC hdcTargetDev, HDC hdcDraw, LPCRECTL lprcBounds, LPCRECTL lprcWBounds, BOOL ( STDMETHODCALLTYPE *pfnContinue )( ULONG_PTR dwContinue ), ULONG_PTR dwContinue )
1514 {
1515     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::Draw( DWORD dwDrawAspect, LONG lindex, void *pvAspect, DVTARGETDEVICE *ptd, HDC hdcTargetDev, HDC hdcDraw, LPCRECTL lprcBounds, LPCRECTL lprcWBounds, BOOL ( STDMETHODCALLTYPE *pfnContinue )( ULONG_PTR dwContinue ), ULONG_PTR dwContinue )" );
1516     if ( CheckDefHandler() )
1517     {
1518         ComSmart< IViewObject > pIViewObject;
1519         HRESULT hr = m_pDefHandler->QueryInterface( IID_IViewObject, (void**)&pIViewObject );
1520 
1521         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1522         if ( SUCCEEDED( hr ) && pIViewObject )
1523             return pIViewObject->Draw( dwDrawAspect, lindex, pvAspect, ptd, hdcTargetDev, hdcDraw, lprcBounds, lprcWBounds, pfnContinue, dwContinue );
1524     }
1525 
1526     return E_FAIL;
1527 }
1528 
1529 //-------------------------------------------------------------------------------
GetColorSet(DWORD dwDrawAspect,LONG lindex,void * pvAspect,DVTARGETDEVICE * ptd,HDC hicTargetDev,LOGPALETTE ** ppColorSet)1530 STDMETHODIMP InprocEmbedDocument_Impl::GetColorSet( DWORD dwDrawAspect, LONG lindex, void *pvAspect, DVTARGETDEVICE *ptd, HDC hicTargetDev, LOGPALETTE **ppColorSet )
1531 {
1532     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::GetColorSet( DWORD dwDrawAspect, LONG lindex, void *pvAspect, DVTARGETDEVICE *ptd, HDC hicTargetDev, LOGPALETTE **ppColorSet )" );
1533     if ( CheckDefHandler() )
1534     {
1535         ComSmart< IViewObject > pIViewObject;
1536         HRESULT hr = m_pDefHandler->QueryInterface( IID_IViewObject, (void**)&pIViewObject );
1537 
1538         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1539         if ( SUCCEEDED( hr ) && pIViewObject )
1540             return pIViewObject->GetColorSet( dwDrawAspect, lindex, pvAspect, ptd, hicTargetDev, ppColorSet );
1541     }
1542 
1543     return E_FAIL;
1544 }
1545 
1546 //-------------------------------------------------------------------------------
Freeze(DWORD dwDrawAspect,LONG lindex,void * pvAspect,DWORD * pdwFreeze)1547 STDMETHODIMP InprocEmbedDocument_Impl::Freeze( DWORD dwDrawAspect, LONG lindex, void *pvAspect, DWORD *pdwFreeze )
1548 {
1549     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::Freeze( DWORD dwDrawAspect, LONG lindex, void *pvAspect, DWORD *pdwFreeze )" );
1550     if ( CheckDefHandler() )
1551     {
1552         ComSmart< IViewObject > pIViewObject;
1553         HRESULT hr = m_pDefHandler->QueryInterface( IID_IViewObject, (void**)&pIViewObject );
1554 
1555         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1556         if ( SUCCEEDED( hr ) && pIViewObject )
1557             return pIViewObject->Freeze( dwDrawAspect, lindex, pvAspect, pdwFreeze );
1558     }
1559 
1560     return E_FAIL;
1561 }
1562 
1563 //-------------------------------------------------------------------------------
Unfreeze(DWORD dwFreeze)1564 STDMETHODIMP InprocEmbedDocument_Impl::Unfreeze( DWORD dwFreeze )
1565 {
1566     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::Unfreeze( DWORD dwFreeze )" );
1567     if ( CheckDefHandler() )
1568     {
1569         ComSmart< IViewObject > pIViewObject;
1570         HRESULT hr = m_pDefHandler->QueryInterface( IID_IViewObject, (void**)&pIViewObject );
1571 
1572         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1573         if ( SUCCEEDED( hr ) && pIViewObject )
1574             return pIViewObject->Unfreeze( dwFreeze );
1575     }
1576 
1577     return E_FAIL;
1578 }
1579 
1580 //-------------------------------------------------------------------------------
SetAdvise(DWORD aspects,DWORD advf,IAdviseSink * pAdvSink)1581 STDMETHODIMP InprocEmbedDocument_Impl::SetAdvise( DWORD aspects, DWORD advf, IAdviseSink *pAdvSink )
1582 {
1583     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::SetAdvise( DWORD aspects, DWORD advf, IAdviseSink *pAdvSink )" );
1584 
1585 	// CheckDefHandler will set the listener, avoid reusing of old listener
1586     if ( m_pViewAdvise )
1587     {
1588         m_pViewAdvise->DisconnectOrigAdvise();
1589 	    m_pViewAdvise = NULL;
1590     }
1591 
1592     if ( pAdvSink && CheckDefHandler() )
1593     {
1594         ComSmart< IViewObject > pIViewObject;
1595         HRESULT hr = m_pDefHandler->QueryInterface( IID_IViewObject, (void**)&pIViewObject );
1596 
1597         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1598         if ( SUCCEEDED( hr ) && pIViewObject )
1599         {
1600             ComSmart< OleWrapperAdviseSink > pOwnAdvise( new OleWrapperAdviseSink( pAdvSink, aspects, advf ) );
1601 
1602             if ( SUCCEEDED( pIViewObject->SetAdvise( aspects, advf, pOwnAdvise ) ) )
1603             {
1604                 m_pViewAdvise = pOwnAdvise;
1605                 return S_OK;
1606             }
1607         }
1608     }
1609 
1610     return S_OK;
1611 }
1612 
1613 //-------------------------------------------------------------------------------
GetAdvise(DWORD * pAspects,DWORD * pAdvf,IAdviseSink ** ppAdvSink)1614 STDMETHODIMP InprocEmbedDocument_Impl::GetAdvise( DWORD *pAspects, DWORD *pAdvf, IAdviseSink **ppAdvSink )
1615 {
1616     if ( !ppAdvSink )
1617         return E_INVALIDARG;
1618 
1619     if ( m_pViewAdvise )
1620     {
1621         if ( pAspects )
1622             *pAspects = m_pViewAdvise->GetAspect();
1623 
1624         if ( pAdvf )
1625             *pAdvf = m_pViewAdvise->GetViewAdviseFlag();
1626 
1627         *ppAdvSink = m_pViewAdvise->GetOrigAdvise();
1628         if ( *ppAdvSink )
1629             (*ppAdvSink)->AddRef();
1630     }
1631     else
1632         *ppAdvSink = NULL;
1633 
1634     return S_OK;
1635 }
1636 
1637 // IViewObject2 methods
1638 //-------------------------------------------------------------------------------
GetExtent(DWORD dwDrawAspect,LONG lindex,DVTARGETDEVICE * ptd,LPSIZEL lpsizel)1639 STDMETHODIMP InprocEmbedDocument_Impl::GetExtent( DWORD dwDrawAspect, LONG lindex, DVTARGETDEVICE *ptd, LPSIZEL lpsizel )
1640 {
1641     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::GetExtent( DWORD dwDrawAspect, LONG lindex, DVTARGETDEVICE *ptd, LPSIZEL lpsizel )" );
1642     if ( CheckDefHandler() )
1643     {
1644         ComSmart< IViewObject2 > pIViewObject2;
1645         HRESULT hr = m_pDefHandler->QueryInterface( IID_IViewObject2, (void**)&pIViewObject2 );
1646 
1647         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1648         if ( SUCCEEDED( hr ) && pIViewObject2 )
1649             return pIViewObject2->GetExtent( dwDrawAspect, lindex, ptd, lpsizel );
1650     }
1651 
1652     return E_FAIL;
1653 }
1654 
1655 
1656 
1657 // IOleWindow methods
1658 //-------------------------------------------------------------------------------
GetWindow(HWND * phwnd)1659 STDMETHODIMP InprocEmbedDocument_Impl::GetWindow( HWND *phwnd )
1660 {
1661     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::GetWindow( HWND *phwnd )" );
1662     if ( CheckDefHandler() )
1663     {
1664         ComSmart< IOleWindow > pIOleWindow;
1665         HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleWindow, (void**)&pIOleWindow );
1666 
1667         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1668         if ( SUCCEEDED( hr ) && pIOleWindow )
1669             return pIOleWindow->GetWindow( phwnd );
1670     }
1671 
1672     return E_FAIL;
1673 }
1674 
1675 //-------------------------------------------------------------------------------
ContextSensitiveHelp(BOOL fEnterMode)1676 STDMETHODIMP InprocEmbedDocument_Impl::ContextSensitiveHelp( BOOL fEnterMode )
1677 {
1678     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::ContextSensitiveHelp( BOOL fEnterMode )" );
1679     if ( CheckDefHandler() )
1680     {
1681         ComSmart< IOleWindow > pIOleWindow;
1682         HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleWindow, (void**)&pIOleWindow );
1683 
1684         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1685         if ( SUCCEEDED( hr ) && pIOleWindow )
1686             return pIOleWindow->ContextSensitiveHelp( fEnterMode );
1687     }
1688 
1689     return E_FAIL;
1690 }
1691 
1692 
1693 // IOleInPlaceObject methods
1694 //-------------------------------------------------------------------------------
InPlaceDeactivate(void)1695 STDMETHODIMP InprocEmbedDocument_Impl::InPlaceDeactivate( void )
1696 {
1697     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::InPlaceDeactivate( void )" );
1698     if ( CheckDefHandler() )
1699     {
1700         ComSmart< IOleInPlaceObject > pIOleInPlaceObject;
1701         HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleInPlaceObject, (void**)&pIOleInPlaceObject );
1702 
1703         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1704         if ( SUCCEEDED( hr ) && pIOleInPlaceObject )
1705             return pIOleInPlaceObject->InPlaceDeactivate();
1706     }
1707 
1708     return E_FAIL;
1709 }
1710 
1711 //-------------------------------------------------------------------------------
UIDeactivate(void)1712 STDMETHODIMP InprocEmbedDocument_Impl::UIDeactivate( void )
1713 {
1714     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::UIDeactivate( void )" );
1715     if ( CheckDefHandler() )
1716     {
1717         ComSmart< IOleInPlaceObject > pIOleInPlaceObject;
1718         HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleInPlaceObject, (void**)&pIOleInPlaceObject );
1719 
1720         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1721         if ( SUCCEEDED( hr ) && pIOleInPlaceObject )
1722             return pIOleInPlaceObject->UIDeactivate();
1723     }
1724 
1725     return E_FAIL;
1726 }
1727 
1728 //-------------------------------------------------------------------------------
SetObjectRects(LPCRECT lprcPosRect,LPCRECT lprcClipRect)1729 STDMETHODIMP InprocEmbedDocument_Impl::SetObjectRects( LPCRECT lprcPosRect, LPCRECT lprcClipRect )
1730 {
1731     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::SetObjectRects( LPCRECT lprcPosRect, LPCRECT lprcClipRect )" );
1732     if ( CheckDefHandler() )
1733     {
1734         ComSmart< IOleInPlaceObject > pIOleInPlaceObject;
1735         HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleInPlaceObject, (void**)&pIOleInPlaceObject );
1736 
1737         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1738         if ( SUCCEEDED( hr ) && pIOleInPlaceObject )
1739             return pIOleInPlaceObject->SetObjectRects( lprcPosRect, lprcClipRect );
1740     }
1741 
1742     return E_FAIL;
1743 }
1744 
1745 //-------------------------------------------------------------------------------
ReactivateAndUndo(void)1746 STDMETHODIMP InprocEmbedDocument_Impl::ReactivateAndUndo( void )
1747 {
1748     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::ReactivateAndUndo( void )" );
1749     if ( CheckDefHandler() )
1750     {
1751         ComSmart< IOleInPlaceObject > pIOleInPlaceObject;
1752         HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleInPlaceObject, (void**)&pIOleInPlaceObject );
1753 
1754         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1755         if ( SUCCEEDED( hr ) && pIOleInPlaceObject )
1756             return pIOleInPlaceObject->ReactivateAndUndo();
1757     }
1758 
1759     return E_FAIL;
1760 }
1761 
1762 
1763 // IDispatch methods
1764 //-------------------------------------------------------------------------------
GetTypeInfoCount(UINT * pctinfo)1765 STDMETHODIMP InprocEmbedDocument_Impl::GetTypeInfoCount( UINT *pctinfo )
1766 {
1767     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::GetTypeInfoCount( UINT *pctinfo )" );
1768     if ( CheckDefHandler() )
1769     {
1770         ComSmart< IDispatch > pIDispatch;
1771         HRESULT hr = m_pDefHandler->QueryInterface( IID_IDispatch, (void**)&pIDispatch );
1772 
1773         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1774         if ( SUCCEEDED( hr ) && pIDispatch )
1775             return pIDispatch->GetTypeInfoCount( pctinfo );
1776     }
1777 
1778     return E_FAIL;
1779 }
1780 
1781 //-------------------------------------------------------------------------------
GetTypeInfo(UINT iTInfo,LCID lcid,ITypeInfo ** ppTInfo)1782 STDMETHODIMP InprocEmbedDocument_Impl::GetTypeInfo( UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo )
1783 {
1784     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::GetTypeInfo( UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo )" );
1785     if ( CheckDefHandler() )
1786     {
1787         ComSmart< IDispatch > pIDispatch;
1788         HRESULT hr = m_pDefHandler->QueryInterface( IID_IDispatch, (void**)&pIDispatch );
1789 
1790         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1791         if ( SUCCEEDED( hr ) && pIDispatch )
1792             return pIDispatch->GetTypeInfo( iTInfo, lcid, ppTInfo );
1793     }
1794 
1795     return E_FAIL;
1796 }
1797 
1798 //-------------------------------------------------------------------------------
GetIDsOfNames(REFIID riid,LPOLESTR * rgszNames,UINT cNames,LCID lcid,DISPID * rgDispId)1799 STDMETHODIMP InprocEmbedDocument_Impl::GetIDsOfNames( REFIID riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId )
1800 {
1801     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::GetIDsOfNames( REFIID riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId )" );
1802     if ( CheckDefHandler() )
1803     {
1804         ComSmart< IDispatch > pIDispatch;
1805         HRESULT hr = m_pDefHandler->QueryInterface( IID_IDispatch, (void**)&pIDispatch );
1806 
1807         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1808         if ( SUCCEEDED( hr ) && pIDispatch )
1809             return pIDispatch->GetIDsOfNames( riid, rgszNames, cNames, lcid, rgDispId );
1810     }
1811 
1812     return E_FAIL;
1813 }
1814 
1815 //-------------------------------------------------------------------------------
Invoke(DISPID dispIdMember,REFIID riid,LCID lcid,WORD wFlags,DISPPARAMS * pDispParams,VARIANT * pVarResult,EXCEPINFO * pExcepInfo,UINT * puArgErr)1816 STDMETHODIMP InprocEmbedDocument_Impl::Invoke( DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr )
1817 {
1818     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::Invoke( DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr )" );
1819     if ( CheckDefHandler() )
1820     {
1821         ComSmart< IDispatch > pIDispatch;
1822         HRESULT hr = m_pDefHandler->QueryInterface( IID_IDispatch, (void**)&pIDispatch );
1823 
1824         ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1825         if ( SUCCEEDED( hr ) && pIDispatch )
1826             return pIDispatch->Invoke( dispIdMember, riid, lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr );
1827     }
1828 
1829     return E_FAIL;
1830 }
1831 
1832 
1833 // ====
1834 // InternalCacheWrapper
1835 // ====
1836 
1837 // IUnknown
1838 //-------------------------------------------------------------------------------
QueryInterface(REFIID riid,void FAR * FAR * ppv)1839 STDMETHODIMP InprocEmbedDocument_Impl::InternalCacheWrapper::QueryInterface( REFIID riid, void FAR* FAR* ppv )
1840 {
1841     return m_rOwnDocument.QueryInterface( riid, ppv );
1842 }
1843 
1844 //-------------------------------------------------------------------------------
STDMETHODIMP_(ULONG)1845 STDMETHODIMP_(ULONG) InprocEmbedDocument_Impl::InternalCacheWrapper::AddRef()
1846 {
1847 	return m_rOwnDocument.AddRef();
1848 }
1849 
1850 //-------------------------------------------------------------------------------
STDMETHODIMP_(ULONG)1851 STDMETHODIMP_(ULONG) InprocEmbedDocument_Impl::InternalCacheWrapper::Release()
1852 {
1853     return m_rOwnDocument.Release();
1854 }
1855 
1856 // IOleCache methods
1857 //-------------------------------------------------------------------------------
Cache(FORMATETC * pformatetc,DWORD advf,DWORD * pdwConnection)1858 STDMETHODIMP InprocEmbedDocument_Impl::InternalCacheWrapper::Cache( FORMATETC *pformatetc, DWORD advf, DWORD *pdwConnection )
1859 {
1860     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::InternalCacheWrapper::Cache( FORMATETC *pformatetc, DWORD advf, DWORD *pdwConnection )" );
1861     if ( m_rOwnDocument.CheckDefHandler() )
1862     {
1863         ComSmart< IOleCache > pIOleCache;
1864         HRESULT hr = m_rOwnDocument.GetDefHandler()->QueryInterface( IID_IOleCache, (void**)&pIOleCache );
1865 
1866         ULONGGuard aGuard( &m_rOwnDocument.m_nCallsOnStack ); // avoid reentrance problem
1867         if ( SUCCEEDED( hr ) && pIOleCache )
1868             return pIOleCache->Cache( pformatetc, advf, pdwConnection );
1869     }
1870 
1871     return E_FAIL;
1872 }
1873 
1874 //-------------------------------------------------------------------------------
Uncache(DWORD dwConnection)1875 STDMETHODIMP InprocEmbedDocument_Impl::InternalCacheWrapper::Uncache( DWORD dwConnection )
1876 {
1877     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::InternalCacheWrapper::Uncache( DWORD dwConnection )" );
1878     if ( m_rOwnDocument.CheckDefHandler() )
1879     {
1880         ComSmart< IOleCache > pIOleCache;
1881         HRESULT hr = m_rOwnDocument.GetDefHandler()->QueryInterface( IID_IOleCache, (void**)&pIOleCache );
1882 
1883         ULONGGuard aGuard( &m_rOwnDocument.m_nCallsOnStack ); // avoid reentrance problem
1884         if ( SUCCEEDED( hr ) && pIOleCache )
1885             return pIOleCache->Uncache( dwConnection );
1886     }
1887 
1888     return E_FAIL;
1889 }
1890 
1891 //-------------------------------------------------------------------------------
EnumCache(IEnumSTATDATA ** ppenumSTATDATA)1892 STDMETHODIMP InprocEmbedDocument_Impl::InternalCacheWrapper::EnumCache( IEnumSTATDATA **ppenumSTATDATA )
1893 {
1894     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::InternalCacheWrapper::EnumCache( IEnumSTATDATA **ppenumSTATDATA )" );
1895     if ( m_rOwnDocument.CheckDefHandler() )
1896     {
1897         ComSmart< IOleCache > pIOleCache;
1898         HRESULT hr = m_rOwnDocument.GetDefHandler()->QueryInterface( IID_IOleCache, (void**)&pIOleCache );
1899 
1900         ULONGGuard aGuard( &m_rOwnDocument.m_nCallsOnStack ); // avoid reentrance problem
1901         if ( SUCCEEDED( hr ) && pIOleCache )
1902             return pIOleCache->EnumCache( ppenumSTATDATA );
1903     }
1904 
1905     return E_FAIL;
1906 }
1907 
1908 //-------------------------------------------------------------------------------
InitCache(IDataObject * pDataObject)1909 STDMETHODIMP InprocEmbedDocument_Impl::InternalCacheWrapper::InitCache( IDataObject *pDataObject )
1910 {
1911     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::InternalCacheWrapper::InitCache( IDataObject *pDataObject )" );
1912     if ( m_rOwnDocument.CheckDefHandler() )
1913     {
1914         ComSmart< IOleCache > pIOleCache;
1915         HRESULT hr = m_rOwnDocument.GetDefHandler()->QueryInterface( IID_IOleCache, (void**)&pIOleCache );
1916 
1917         ULONGGuard aGuard( &m_rOwnDocument.m_nCallsOnStack ); // avoid reentrance problem
1918         if ( SUCCEEDED( hr ) && pIOleCache )
1919             return pIOleCache->InitCache( pDataObject );
1920     }
1921 
1922     return E_FAIL;
1923 }
1924 
1925 //-------------------------------------------------------------------------------
SetData(FORMATETC * pformatetc,STGMEDIUM * pmedium,BOOL fRelease)1926 STDMETHODIMP InprocEmbedDocument_Impl::InternalCacheWrapper::SetData( FORMATETC *pformatetc, STGMEDIUM *pmedium, BOOL fRelease )
1927 {
1928     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::InternalCacheWrapper::SetData( FORMATETC *pformatetc, STGMEDIUM *pmedium, BOOL fRelease )" );
1929     if ( m_rOwnDocument.CheckDefHandler() )
1930     {
1931         ComSmart< IOleCache > pIOleCache;
1932         HRESULT hr = m_rOwnDocument.GetDefHandler()->QueryInterface( IID_IOleCache, (void**)&pIOleCache );
1933 
1934         ULONGGuard aGuard( &m_rOwnDocument.m_nCallsOnStack ); // avoid reentrance problem
1935         if ( SUCCEEDED( hr ) && pIOleCache )
1936             return pIOleCache->SetData( pformatetc, pmedium, fRelease );
1937     }
1938 
1939     return E_FAIL;
1940 }
1941 
1942 // IOleCache2 methods
1943 //-------------------------------------------------------------------------------
UpdateCache(LPDATAOBJECT pDataObject,DWORD grfUpdf,LPVOID pReserved)1944 STDMETHODIMP InprocEmbedDocument_Impl::InternalCacheWrapper::UpdateCache( LPDATAOBJECT pDataObject, DWORD grfUpdf, LPVOID pReserved )
1945 {
1946     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::InternalCacheWrapper::UpdateCache( LPDATAOBJECT pDataObject, DWORD grfUpdf, LPVOID pReserved )" );
1947     if ( m_rOwnDocument.CheckDefHandler() )
1948     {
1949         ComSmart< IOleCache2 > pIOleCache2;
1950         HRESULT hr = m_rOwnDocument.GetDefHandler()->QueryInterface( IID_IOleCache2, (void**)&pIOleCache2 );
1951 
1952         ULONGGuard aGuard( &m_rOwnDocument.m_nCallsOnStack ); // avoid reentrance problem
1953         if ( SUCCEEDED( hr ) && pIOleCache2 )
1954             return pIOleCache2->UpdateCache( pDataObject, grfUpdf, pReserved );
1955     }
1956 
1957     return E_FAIL;
1958 }
1959 
1960 //-------------------------------------------------------------------------------
DiscardCache(DWORD dwDiscardOptions)1961 STDMETHODIMP InprocEmbedDocument_Impl::InternalCacheWrapper::DiscardCache( DWORD dwDiscardOptions )
1962 {
1963     WRITEDEBUGINFO( "InprocEmbedDocument_Impl::InternalCacheWrapper::DiscardCache( DWORD dwDiscardOptions )" );
1964     if ( m_rOwnDocument.CheckDefHandler() )
1965     {
1966         ComSmart< IOleCache2 > pIOleCache2;
1967         HRESULT hr = m_rOwnDocument.GetDefHandler()->QueryInterface( IID_IOleCache2, (void**)&pIOleCache2 );
1968 
1969         ULONGGuard aGuard( &m_rOwnDocument.m_nCallsOnStack ); // avoid reentrance problem
1970         if ( SUCCEEDED( hr ) && pIOleCache2 )
1971             return pIOleCache2->DiscardCache( dwDiscardOptions );
1972     }
1973 
1974     return E_FAIL;
1975 }
1976 
1977 }; // namespace inprocserv
1978 
1979