ntdll: More compatible exception information for protection faults.
[wine/multimedia.git] / dlls / ole32 / datacache.c
bloba3beacab4ee9ffa1a0c6512c2e1f827aad64b3c6
1 /*
2 * OLE 2 Data cache
4 * Copyright 1999 Francis Beaudet
5 * Copyright 2000 Abey George
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * NOTES:
22 * The OLE2 data cache supports a whole whack of
23 * interfaces including:
24 * IDataObject, IPersistStorage, IViewObject2,
25 * IOleCache2 and IOleCacheControl.
27 * Most of the implementation details are taken from: Inside OLE
28 * second edition by Kraig Brockschmidt,
30 * NOTES
31 * - This implementation of the datacache will let your application
32 * load documents that have embedded OLE objects in them and it will
33 * also retrieve the metafile representation of those objects.
34 * - This implementation of the datacache will also allow your
35 * application to save new documents with OLE objects in them.
36 * - The main thing that it doesn't do is allow you to activate
37 * or modify the OLE objects in any way.
38 * - I haven't found any good documentation on the real usage of
39 * the streams created by the data cache. In particular, How to
40 * determine what the XXX stands for in the stream name
41 * "\002OlePresXXX". It appears to just be a counter.
42 * - Also, I don't know the real content of the presentation stream
43 * header. I was able to figure-out where the extent of the object
44 * was stored and the aspect, but that's about it.
46 #include <assert.h>
47 #include <stdarg.h>
48 #include <string.h>
50 #define COBJMACROS
51 #define NONAMELESSUNION
52 #define NONAMELESSSTRUCT
54 #include "windef.h"
55 #include "winbase.h"
56 #include "wingdi.h"
57 #include "winuser.h"
58 #include "winerror.h"
59 #include "wine/unicode.h"
60 #include "ole2.h"
61 #include "wine/debug.h"
63 WINE_DEFAULT_DEBUG_CHANNEL(ole);
65 /****************************************************************************
66 * PresentationDataHeader
68 * This structure represents the header of the \002OlePresXXX stream in
69 * the OLE object strorage.
71 * Most fields are still unknown.
73 typedef struct PresentationDataHeader
75 DWORD unknown1; /* -1 */
76 DWORD unknown2; /* 3, possibly CF_METAFILEPICT */
77 DWORD unknown3; /* 4, possibly TYMED_ISTREAM */
78 DVASPECT dvAspect;
79 DWORD unknown5; /* -1 */
81 DWORD unknown6;
82 DWORD unknown7; /* 0 */
83 DWORD dwObjectExtentX;
84 DWORD dwObjectExtentY;
85 DWORD dwSize;
86 } PresentationDataHeader;
88 /****************************************************************************
89 * DataCache
91 struct DataCache
94 * List all interface VTables here
96 const IDataObjectVtbl* lpVtbl;
97 const IUnknownVtbl* lpvtblNDIUnknown;
98 const IPersistStorageVtbl* lpvtblIPersistStorage;
99 const IViewObject2Vtbl* lpvtblIViewObject;
100 const IOleCache2Vtbl* lpvtblIOleCache2;
101 const IOleCacheControlVtbl* lpvtblIOleCacheControl;
104 * Reference count of this object
106 LONG ref;
109 * IUnknown implementation of the outer object.
111 IUnknown* outerUnknown;
114 * This storage pointer is set through a call to
115 * IPersistStorage_Load. This is where the visual
116 * representation of the object is stored.
118 IStorage* presentationStorage;
121 * The user of this object can setup ONE advise sink
122 * connection with the object. These parameters describe
123 * that connection.
125 DWORD sinkAspects;
126 DWORD sinkAdviseFlag;
127 IAdviseSink* sinkInterface;
131 typedef struct DataCache DataCache;
134 * Here, I define utility macros to help with the casting of the
135 * "this" parameter.
136 * There is a version to accommodate all of the VTables implemented
137 * by this object.
140 static inline DataCache *impl_from_IDataObject( IDataObject *iface )
142 return (DataCache *)((char*)iface - FIELD_OFFSET(DataCache, lpVtbl));
145 static inline DataCache *impl_from_NDIUnknown( IUnknown *iface )
147 return (DataCache *)((char*)iface - FIELD_OFFSET(DataCache, lpvtblNDIUnknown));
150 static inline DataCache *impl_from_IPersistStorage( IPersistStorage *iface )
152 return (DataCache *)((char*)iface - FIELD_OFFSET(DataCache, lpvtblIPersistStorage));
155 static inline DataCache *impl_from_IViewObject2( IViewObject2 *iface )
157 return (DataCache *)((char*)iface - FIELD_OFFSET(DataCache, lpvtblIViewObject));
160 static inline DataCache *impl_from_IOleCache2( IOleCache2 *iface )
162 return (DataCache *)((char*)iface - FIELD_OFFSET(DataCache, lpvtblIOleCache2));
165 static inline DataCache *impl_from_IOleCacheControl( IOleCacheControl *iface )
167 return (DataCache *)((char*)iface - FIELD_OFFSET(DataCache, lpvtblIOleCacheControl));
172 * Prototypes for the methods of the DataCache class.
174 static DataCache* DataCache_Construct(REFCLSID clsid,
175 LPUNKNOWN pUnkOuter);
176 static HRESULT DataCache_OpenPresStream(DataCache *this,
177 DWORD drawAspect,
178 IStream **pStm);
180 static void DataCache_Destroy(
181 DataCache* ptrToDestroy)
183 TRACE("()\n");
185 if (ptrToDestroy->sinkInterface != NULL)
187 IAdviseSink_Release(ptrToDestroy->sinkInterface);
188 ptrToDestroy->sinkInterface = NULL;
191 if (ptrToDestroy->presentationStorage != NULL)
193 IStorage_Release(ptrToDestroy->presentationStorage);
194 ptrToDestroy->presentationStorage = NULL;
198 * Free the datacache pointer.
200 HeapFree(GetProcessHeap(), 0, ptrToDestroy);
203 /************************************************************************
204 * DataCache_ReadPresentationData
206 * This method will read information for the requested presentation
207 * into the given structure.
209 * Param:
210 * this - Pointer to the DataCache object
211 * drawAspect - The aspect of the object that we wish to draw.
212 * header - The structure containing information about this
213 * aspect of the object.
215 static HRESULT DataCache_ReadPresentationData(
216 DataCache* this,
217 DWORD drawAspect,
218 PresentationDataHeader* header)
220 IStream* presStream = NULL;
221 HRESULT hres;
224 * Open the presentation stream.
226 hres = DataCache_OpenPresStream(
227 this,
228 drawAspect,
229 &presStream);
231 if (FAILED(hres))
232 return hres;
235 * Read the header.
238 hres = IStream_Read(
239 presStream,
240 header,
241 sizeof(PresentationDataHeader),
242 NULL);
245 * Cleanup.
247 IStream_Release(presStream);
250 * We don't want to propagate any other error
251 * code than a failure.
253 if (hres!=S_OK)
254 hres = E_FAIL;
256 return hres;
259 /************************************************************************
260 * DataCache_FireOnViewChange
262 * This method will fire an OnViewChange notification to the advise
263 * sink registered with the datacache.
265 * See IAdviseSink::OnViewChange for more details.
267 static void DataCache_FireOnViewChange(
268 DataCache* this,
269 DWORD aspect,
270 LONG lindex)
272 TRACE("(%p, %lx, %ld)\n", this, aspect, lindex);
275 * The sink supplies a filter when it registers
276 * we make sure we only send the notifications when that
277 * filter matches.
279 if ((this->sinkAspects & aspect) != 0)
281 if (this->sinkInterface != NULL)
283 IAdviseSink_OnViewChange(this->sinkInterface,
284 aspect,
285 lindex);
288 * Some sinks want to be unregistered automatically when
289 * the first notification goes out.
291 if ( (this->sinkAdviseFlag & ADVF_ONLYONCE) != 0)
293 IAdviseSink_Release(this->sinkInterface);
295 this->sinkInterface = NULL;
296 this->sinkAspects = 0;
297 this->sinkAdviseFlag = 0;
303 /* Helper for DataCache_OpenPresStream */
304 static BOOL DataCache_IsPresentationStream(const STATSTG *elem)
306 /* The presentation streams have names of the form "\002OlePresXXX",
307 * where XXX goes from 000 to 999. */
308 static const WCHAR OlePres[] = { 2,'O','l','e','P','r','e','s' };
310 LPCWSTR name = elem->pwcsName;
312 return (elem->type == STGTY_STREAM)
313 && (elem->cbSize.u.LowPart >= sizeof(PresentationDataHeader))
314 && (strlenW(name) == 11)
315 && (strncmpW(name, OlePres, 8) == 0)
316 && (name[8] >= '0') && (name[8] <= '9')
317 && (name[9] >= '0') && (name[9] <= '9')
318 && (name[10] >= '0') && (name[10] <= '9');
321 /************************************************************************
322 * DataCache_OpenPresStream
324 * This method will find the stream for the given presentation. It makes
325 * no attempt at fallback.
327 * Param:
328 * this - Pointer to the DataCache object
329 * drawAspect - The aspect of the object that we wish to draw.
330 * pStm - A returned stream. It points to the beginning of the
331 * - presentation data, including the header.
333 * Errors:
334 * S_OK The requested stream has been opened.
335 * OLE_E_BLANK The requested stream could not be found.
336 * Quite a few others I'm too lazy to map correctly.
338 * Notes:
339 * Algorithm: Scan the elements of the presentation storage, looking
340 * for presentation streams. For each presentation stream,
341 * load the header and check to see if the aspect maches.
343 * If a fallback is desired, just opening the first presentation stream
344 * is a possibility.
346 static HRESULT DataCache_OpenPresStream(
347 DataCache *this,
348 DWORD drawAspect,
349 IStream **ppStm)
351 STATSTG elem;
352 IEnumSTATSTG *pEnum;
353 HRESULT hr;
355 if (!ppStm) return E_POINTER;
357 hr = IStorage_EnumElements(this->presentationStorage, 0, NULL, 0, &pEnum);
358 if (FAILED(hr)) return hr;
360 while ((hr = IEnumSTATSTG_Next(pEnum, 1, &elem, NULL)) == S_OK)
362 if (DataCache_IsPresentationStream(&elem))
364 IStream *pStm;
366 hr = IStorage_OpenStream(this->presentationStorage, elem.pwcsName,
367 NULL, STGM_READ | STGM_SHARE_EXCLUSIVE, 0,
368 &pStm);
369 if (SUCCEEDED(hr))
371 PresentationDataHeader header;
372 ULONG actual_read;
374 hr = IStream_Read(pStm, &header, sizeof(header), &actual_read);
376 /* can't use SUCCEEDED(hr): S_FALSE counts as an error */
377 if (hr == S_OK && actual_read == sizeof(header)
378 && header.dvAspect == drawAspect)
380 /* Rewind the stream before returning it. */
381 LARGE_INTEGER offset;
382 offset.u.LowPart = 0;
383 offset.u.HighPart = 0;
384 IStream_Seek(pStm, offset, STREAM_SEEK_SET, NULL);
386 *ppStm = pStm;
388 CoTaskMemFree(elem.pwcsName);
389 IEnumSTATSTG_Release(pEnum);
391 return S_OK;
394 IStream_Release(pStm);
398 CoTaskMemFree(elem.pwcsName);
401 IEnumSTATSTG_Release(pEnum);
403 return (hr == S_FALSE ? OLE_E_BLANK : hr);
406 /************************************************************************
407 * DataCache_ReadPresentationData
409 * This method will read information for the requested presentation
410 * into the given structure.
412 * Param:
413 * this - Pointer to the DataCache object
414 * drawAspect - The aspect of the object that we wish to draw.
416 * Returns:
417 * This method returns a metafile handle if it is successful.
418 * it will return 0 if not.
420 static HMETAFILE DataCache_ReadPresMetafile(
421 DataCache* this,
422 DWORD drawAspect)
424 LARGE_INTEGER offset;
425 IStream* presStream = NULL;
426 HRESULT hres;
427 void* metafileBits;
428 STATSTG streamInfo;
429 HMETAFILE newMetafile = 0;
432 * Open the presentation stream.
434 hres = DataCache_OpenPresStream(
435 this,
436 drawAspect,
437 &presStream);
439 if (FAILED(hres))
440 return (HMETAFILE)hres;
443 * Get the size of the stream.
445 hres = IStream_Stat(presStream,
446 &streamInfo,
447 STATFLAG_NONAME);
450 * Skip the header
452 offset.u.HighPart = 0;
453 offset.u.LowPart = sizeof(PresentationDataHeader);
455 hres = IStream_Seek(
456 presStream,
457 offset,
458 STREAM_SEEK_SET,
459 NULL);
461 streamInfo.cbSize.u.LowPart -= offset.u.LowPart;
464 * Allocate a buffer for the metafile bits.
466 metafileBits = HeapAlloc(GetProcessHeap(),
468 streamInfo.cbSize.u.LowPart);
471 * Read the metafile bits.
473 hres = IStream_Read(
474 presStream,
475 metafileBits,
476 streamInfo.cbSize.u.LowPart,
477 NULL);
480 * Create a metafile with those bits.
482 if (SUCCEEDED(hres))
484 newMetafile = SetMetaFileBitsEx(streamInfo.cbSize.u.LowPart, metafileBits);
488 * Cleanup.
490 HeapFree(GetProcessHeap(), 0, metafileBits);
491 IStream_Release(presStream);
493 if (newMetafile==0)
494 hres = E_FAIL;
496 return newMetafile;
499 /*********************************************************
500 * Method implementation for the non delegating IUnknown
501 * part of the DataCache class.
504 /************************************************************************
505 * DataCache_NDIUnknown_QueryInterface (IUnknown)
507 * See Windows documentation for more details on IUnknown methods.
509 * This version of QueryInterface will not delegate it's implementation
510 * to the outer unknown.
512 static HRESULT WINAPI DataCache_NDIUnknown_QueryInterface(
513 IUnknown* iface,
514 REFIID riid,
515 void** ppvObject)
517 DataCache *this = impl_from_NDIUnknown(iface);
520 * Perform a sanity check on the parameters.
522 if ( (this==0) || (ppvObject==0) )
523 return E_INVALIDARG;
526 * Initialize the return parameter.
528 *ppvObject = 0;
531 * Compare the riid with the interface IDs implemented by this object.
533 if (memcmp(&IID_IUnknown, riid, sizeof(IID_IUnknown)) == 0)
535 *ppvObject = iface;
537 else if (memcmp(&IID_IDataObject, riid, sizeof(IID_IDataObject)) == 0)
539 *ppvObject = (IDataObject*)&(this->lpVtbl);
541 else if ( (memcmp(&IID_IPersistStorage, riid, sizeof(IID_IPersistStorage)) == 0) ||
542 (memcmp(&IID_IPersist, riid, sizeof(IID_IPersist)) == 0) )
544 *ppvObject = (IPersistStorage*)&(this->lpvtblIPersistStorage);
546 else if ( (memcmp(&IID_IViewObject, riid, sizeof(IID_IViewObject)) == 0) ||
547 (memcmp(&IID_IViewObject2, riid, sizeof(IID_IViewObject2)) == 0) )
549 *ppvObject = (IViewObject2*)&(this->lpvtblIViewObject);
551 else if ( (memcmp(&IID_IOleCache, riid, sizeof(IID_IOleCache)) == 0) ||
552 (memcmp(&IID_IOleCache2, riid, sizeof(IID_IOleCache2)) == 0) )
554 *ppvObject = (IOleCache2*)&(this->lpvtblIOleCache2);
556 else if (memcmp(&IID_IOleCacheControl, riid, sizeof(IID_IOleCacheControl)) == 0)
558 *ppvObject = (IOleCacheControl*)&(this->lpvtblIOleCacheControl);
562 * Check that we obtained an interface.
564 if ((*ppvObject)==0)
566 WARN( "() : asking for unsupported interface %s\n", debugstr_guid(riid));
567 return E_NOINTERFACE;
571 * Query Interface always increases the reference count by one when it is
572 * successful.
574 IUnknown_AddRef((IUnknown*)*ppvObject);
576 return S_OK;
579 /************************************************************************
580 * DataCache_NDIUnknown_AddRef (IUnknown)
582 * See Windows documentation for more details on IUnknown methods.
584 * This version of QueryInterface will not delegate it's implementation
585 * to the outer unknown.
587 static ULONG WINAPI DataCache_NDIUnknown_AddRef(
588 IUnknown* iface)
590 DataCache *this = impl_from_NDIUnknown(iface);
591 return InterlockedIncrement(&this->ref);
594 /************************************************************************
595 * DataCache_NDIUnknown_Release (IUnknown)
597 * See Windows documentation for more details on IUnknown methods.
599 * This version of QueryInterface will not delegate it's implementation
600 * to the outer unknown.
602 static ULONG WINAPI DataCache_NDIUnknown_Release(
603 IUnknown* iface)
605 DataCache *this = impl_from_NDIUnknown(iface);
606 ULONG ref;
609 * Decrease the reference count on this object.
611 ref = InterlockedDecrement(&this->ref);
614 * If the reference count goes down to 0, perform suicide.
616 if (ref == 0) DataCache_Destroy(this);
618 return ref;
621 /*********************************************************
622 * Method implementation for the IDataObject
623 * part of the DataCache class.
626 /************************************************************************
627 * DataCache_IDataObject_QueryInterface (IUnknown)
629 * See Windows documentation for more details on IUnknown methods.
631 static HRESULT WINAPI DataCache_IDataObject_QueryInterface(
632 IDataObject* iface,
633 REFIID riid,
634 void** ppvObject)
636 DataCache *this = impl_from_IDataObject(iface);
638 return IUnknown_QueryInterface(this->outerUnknown, riid, ppvObject);
641 /************************************************************************
642 * DataCache_IDataObject_AddRef (IUnknown)
644 * See Windows documentation for more details on IUnknown methods.
646 static ULONG WINAPI DataCache_IDataObject_AddRef(
647 IDataObject* iface)
649 DataCache *this = impl_from_IDataObject(iface);
651 return IUnknown_AddRef(this->outerUnknown);
654 /************************************************************************
655 * DataCache_IDataObject_Release (IUnknown)
657 * See Windows documentation for more details on IUnknown methods.
659 static ULONG WINAPI DataCache_IDataObject_Release(
660 IDataObject* iface)
662 DataCache *this = impl_from_IDataObject(iface);
664 return IUnknown_Release(this->outerUnknown);
667 /************************************************************************
668 * DataCache_GetData
670 * Get Data from a source dataobject using format pformatetcIn->cfFormat
671 * See Windows documentation for more details on GetData.
672 * TODO: Currently only CF_METAFILEPICT is implemented
674 static HRESULT WINAPI DataCache_GetData(
675 IDataObject* iface,
676 LPFORMATETC pformatetcIn,
677 STGMEDIUM* pmedium)
679 HRESULT hr = 0;
680 HRESULT hrRet = E_UNEXPECTED;
681 IPersistStorage *pPersistStorage = 0;
682 IStorage *pStorage = 0;
683 IStream *pStream = 0;
684 OLECHAR name[]={ 2, 'O', 'l', 'e', 'P', 'r', 'e', 's', '0', '0', '0', 0};
685 HGLOBAL hGlobalMF = 0;
686 void *mfBits = 0;
687 PresentationDataHeader pdh;
688 METAFILEPICT *mfPict;
689 HMETAFILE hMetaFile = 0;
691 if (pformatetcIn->cfFormat == CF_METAFILEPICT)
693 /* Get the Persist Storage */
695 hr = IDataObject_QueryInterface(iface, &IID_IPersistStorage, (void**)&pPersistStorage);
697 if (hr != S_OK)
698 goto cleanup;
700 /* Create a doc file to copy the doc to a storage */
702 hr = StgCreateDocfile(NULL, STGM_CREATE | STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, &pStorage);
704 if (hr != S_OK)
705 goto cleanup;
707 /* Save it to storage */
709 hr = OleSave(pPersistStorage, pStorage, FALSE);
711 if (hr != S_OK)
712 goto cleanup;
714 /* Open the Presentation data srteam */
716 hr = IStorage_OpenStream(pStorage, name, 0, STGM_CREATE|STGM_SHARE_EXCLUSIVE|STGM_READWRITE, 0, &pStream);
718 if (hr != S_OK)
719 goto cleanup;
721 /* Read the presentation header */
723 hr = IStream_Read(pStream, &pdh, sizeof(PresentationDataHeader), NULL);
725 if (hr != S_OK)
726 goto cleanup;
728 mfBits = HeapAlloc(GetProcessHeap(), 0, pdh.dwSize);
730 /* Read the Metafile bits */
732 hr = IStream_Read(pStream, mfBits, pdh.dwSize, NULL);
734 if (hr != S_OK)
735 goto cleanup;
737 /* Create the metafile and place it in the STGMEDIUM structure */
739 hMetaFile = SetMetaFileBitsEx(pdh.dwSize, mfBits);
741 hGlobalMF = GlobalAlloc(GMEM_SHARE|GMEM_MOVEABLE, sizeof(METAFILEPICT));
742 mfPict = (METAFILEPICT *)GlobalLock(hGlobalMF);
743 mfPict->hMF = hMetaFile;
745 GlobalUnlock(hGlobalMF);
747 pmedium->u.hGlobal = hGlobalMF;
748 pmedium->tymed = TYMED_MFPICT;
749 hrRet = S_OK;
751 cleanup:
753 HeapFree(GetProcessHeap(), 0, mfBits);
755 if (pStream)
756 IStream_Release(pStream);
758 if (pStorage)
759 IStorage_Release(pStorage);
761 if (pPersistStorage)
762 IPersistStorage_Release(pPersistStorage);
764 return hrRet;
767 /* TODO: Other formats are not implemented */
769 return E_NOTIMPL;
772 static HRESULT WINAPI DataCache_GetDataHere(
773 IDataObject* iface,
774 LPFORMATETC pformatetc,
775 STGMEDIUM* pmedium)
777 FIXME("stub\n");
778 return E_NOTIMPL;
781 static HRESULT WINAPI DataCache_QueryGetData(
782 IDataObject* iface,
783 LPFORMATETC pformatetc)
785 FIXME("stub\n");
786 return E_NOTIMPL;
789 /************************************************************************
790 * DataCache_EnumFormatEtc (IDataObject)
792 * The data cache doesn't implement this method.
794 * See Windows documentation for more details on IDataObject methods.
796 static HRESULT WINAPI DataCache_GetCanonicalFormatEtc(
797 IDataObject* iface,
798 LPFORMATETC pformatectIn,
799 LPFORMATETC pformatetcOut)
801 TRACE("()\n");
802 return E_NOTIMPL;
805 /************************************************************************
806 * DataCache_IDataObject_SetData (IDataObject)
808 * This method is delegated to the IOleCache2 implementation.
810 * See Windows documentation for more details on IDataObject methods.
812 static HRESULT WINAPI DataCache_IDataObject_SetData(
813 IDataObject* iface,
814 LPFORMATETC pformatetc,
815 STGMEDIUM* pmedium,
816 BOOL fRelease)
818 IOleCache2* oleCache = NULL;
819 HRESULT hres;
821 TRACE("(%p, %p, %p, %d)\n", iface, pformatetc, pmedium, fRelease);
823 hres = IDataObject_QueryInterface(iface, &IID_IOleCache2, (void**)&oleCache);
825 if (FAILED(hres))
826 return E_UNEXPECTED;
828 hres = IOleCache2_SetData(oleCache, pformatetc, pmedium, fRelease);
830 IOleCache2_Release(oleCache);
832 return hres;
835 /************************************************************************
836 * DataCache_EnumFormatEtc (IDataObject)
838 * The data cache doesn't implement this method.
840 * See Windows documentation for more details on IDataObject methods.
842 static HRESULT WINAPI DataCache_EnumFormatEtc(
843 IDataObject* iface,
844 DWORD dwDirection,
845 IEnumFORMATETC** ppenumFormatEtc)
847 TRACE("()\n");
848 return E_NOTIMPL;
851 /************************************************************************
852 * DataCache_DAdvise (IDataObject)
854 * The data cache doesn't support connections.
856 * See Windows documentation for more details on IDataObject methods.
858 static HRESULT WINAPI DataCache_DAdvise(
859 IDataObject* iface,
860 FORMATETC* pformatetc,
861 DWORD advf,
862 IAdviseSink* pAdvSink,
863 DWORD* pdwConnection)
865 TRACE("()\n");
866 return OLE_E_ADVISENOTSUPPORTED;
869 /************************************************************************
870 * DataCache_DUnadvise (IDataObject)
872 * The data cache doesn't support connections.
874 * See Windows documentation for more details on IDataObject methods.
876 static HRESULT WINAPI DataCache_DUnadvise(
877 IDataObject* iface,
878 DWORD dwConnection)
880 TRACE("()\n");
881 return OLE_E_NOCONNECTION;
884 /************************************************************************
885 * DataCache_EnumDAdvise (IDataObject)
887 * The data cache doesn't support connections.
889 * See Windows documentation for more details on IDataObject methods.
891 static HRESULT WINAPI DataCache_EnumDAdvise(
892 IDataObject* iface,
893 IEnumSTATDATA** ppenumAdvise)
895 TRACE("()\n");
896 return OLE_E_ADVISENOTSUPPORTED;
899 /*********************************************************
900 * Method implementation for the IDataObject
901 * part of the DataCache class.
904 /************************************************************************
905 * DataCache_IPersistStorage_QueryInterface (IUnknown)
907 * See Windows documentation for more details on IUnknown methods.
909 static HRESULT WINAPI DataCache_IPersistStorage_QueryInterface(
910 IPersistStorage* iface,
911 REFIID riid,
912 void** ppvObject)
914 DataCache *this = impl_from_IPersistStorage(iface);
916 return IUnknown_QueryInterface(this->outerUnknown, riid, ppvObject);
919 /************************************************************************
920 * DataCache_IPersistStorage_AddRef (IUnknown)
922 * See Windows documentation for more details on IUnknown methods.
924 static ULONG WINAPI DataCache_IPersistStorage_AddRef(
925 IPersistStorage* iface)
927 DataCache *this = impl_from_IPersistStorage(iface);
929 return IUnknown_AddRef(this->outerUnknown);
932 /************************************************************************
933 * DataCache_IPersistStorage_Release (IUnknown)
935 * See Windows documentation for more details on IUnknown methods.
937 static ULONG WINAPI DataCache_IPersistStorage_Release(
938 IPersistStorage* iface)
940 DataCache *this = impl_from_IPersistStorage(iface);
942 return IUnknown_Release(this->outerUnknown);
945 /************************************************************************
946 * DataCache_GetClassID (IPersistStorage)
948 * The data cache doesn't implement this method.
950 * See Windows documentation for more details on IPersistStorage methods.
952 static HRESULT WINAPI DataCache_GetClassID(
953 IPersistStorage* iface,
954 CLSID* pClassID)
956 TRACE("(%p, %p)\n", iface, pClassID);
957 return E_NOTIMPL;
960 /************************************************************************
961 * DataCache_IsDirty (IPersistStorage)
963 * Until we actully connect to a running object and retrieve new
964 * information to it, we never get dirty.
966 * See Windows documentation for more details on IPersistStorage methods.
968 static HRESULT WINAPI DataCache_IsDirty(
969 IPersistStorage* iface)
971 TRACE("(%p)\n", iface);
973 return S_FALSE;
976 /************************************************************************
977 * DataCache_InitNew (IPersistStorage)
979 * The data cache implementation of IPersistStorage_InitNew simply stores
980 * the storage pointer.
982 * See Windows documentation for more details on IPersistStorage methods.
984 static HRESULT WINAPI DataCache_InitNew(
985 IPersistStorage* iface,
986 IStorage* pStg)
988 TRACE("(%p, %p)\n", iface, pStg);
990 return IPersistStorage_Load(iface, pStg);
993 /************************************************************************
994 * DataCache_Load (IPersistStorage)
996 * The data cache implementation of IPersistStorage_Load doesn't
997 * actually load anything. Instead, it holds on to the storage pointer
998 * and it will load the presentation information when the
999 * IDataObject_GetData or IViewObject2_Draw methods are called.
1001 * See Windows documentation for more details on IPersistStorage methods.
1003 static HRESULT WINAPI DataCache_Load(
1004 IPersistStorage* iface,
1005 IStorage* pStg)
1007 DataCache *this = impl_from_IPersistStorage(iface);
1009 TRACE("(%p, %p)\n", iface, pStg);
1011 if (this->presentationStorage != NULL)
1013 IStorage_Release(this->presentationStorage);
1016 this->presentationStorage = pStg;
1018 if (this->presentationStorage != NULL)
1020 IStorage_AddRef(this->presentationStorage);
1022 return S_OK;
1025 /************************************************************************
1026 * DataCache_Save (IPersistStorage)
1028 * Until we actully connect to a running object and retrieve new
1029 * information to it, we never have to save anything. However, it is
1030 * our responsability to copy the information when saving to a new
1031 * storage.
1033 * See Windows documentation for more details on IPersistStorage methods.
1035 static HRESULT WINAPI DataCache_Save(
1036 IPersistStorage* iface,
1037 IStorage* pStg,
1038 BOOL fSameAsLoad)
1040 DataCache *this = impl_from_IPersistStorage(iface);
1042 TRACE("(%p, %p, %d)\n", iface, pStg, fSameAsLoad);
1044 if ( (!fSameAsLoad) &&
1045 (this->presentationStorage!=NULL) )
1047 return IStorage_CopyTo(this->presentationStorage,
1049 NULL,
1050 NULL,
1051 pStg);
1054 return S_OK;
1057 /************************************************************************
1058 * DataCache_SaveCompleted (IPersistStorage)
1060 * This method is called to tell the cache to release the storage
1061 * pointer it's currentlu holding.
1063 * See Windows documentation for more details on IPersistStorage methods.
1065 static HRESULT WINAPI DataCache_SaveCompleted(
1066 IPersistStorage* iface,
1067 IStorage* pStgNew)
1069 TRACE("(%p, %p)\n", iface, pStgNew);
1071 if (pStgNew)
1074 * First, make sure we get our hands off any storage we have.
1077 IPersistStorage_HandsOffStorage(iface);
1080 * Then, attach to the new storage.
1083 DataCache_Load(iface, pStgNew);
1086 return S_OK;
1089 /************************************************************************
1090 * DataCache_HandsOffStorage (IPersistStorage)
1092 * This method is called to tell the cache to release the storage
1093 * pointer it's currentlu holding.
1095 * See Windows documentation for more details on IPersistStorage methods.
1097 static HRESULT WINAPI DataCache_HandsOffStorage(
1098 IPersistStorage* iface)
1100 DataCache *this = impl_from_IPersistStorage(iface);
1102 TRACE("(%p)\n", iface);
1104 if (this->presentationStorage != NULL)
1106 IStorage_Release(this->presentationStorage);
1107 this->presentationStorage = NULL;
1110 return S_OK;
1113 /*********************************************************
1114 * Method implementation for the IViewObject2
1115 * part of the DataCache class.
1118 /************************************************************************
1119 * DataCache_IViewObject2_QueryInterface (IUnknown)
1121 * See Windows documentation for more details on IUnknown methods.
1123 static HRESULT WINAPI DataCache_IViewObject2_QueryInterface(
1124 IViewObject2* iface,
1125 REFIID riid,
1126 void** ppvObject)
1128 DataCache *this = impl_from_IViewObject2(iface);
1130 return IUnknown_QueryInterface(this->outerUnknown, riid, ppvObject);
1133 /************************************************************************
1134 * DataCache_IViewObject2_AddRef (IUnknown)
1136 * See Windows documentation for more details on IUnknown methods.
1138 static ULONG WINAPI DataCache_IViewObject2_AddRef(
1139 IViewObject2* iface)
1141 DataCache *this = impl_from_IViewObject2(iface);
1143 return IUnknown_AddRef(this->outerUnknown);
1146 /************************************************************************
1147 * DataCache_IViewObject2_Release (IUnknown)
1149 * See Windows documentation for more details on IUnknown methods.
1151 static ULONG WINAPI DataCache_IViewObject2_Release(
1152 IViewObject2* iface)
1154 DataCache *this = impl_from_IViewObject2(iface);
1156 return IUnknown_Release(this->outerUnknown);
1159 /************************************************************************
1160 * DataCache_Draw (IViewObject2)
1162 * This method will draw the cached representation of the object
1163 * to the given device context.
1165 * See Windows documentation for more details on IViewObject2 methods.
1167 static HRESULT WINAPI DataCache_Draw(
1168 IViewObject2* iface,
1169 DWORD dwDrawAspect,
1170 LONG lindex,
1171 void* pvAspect,
1172 DVTARGETDEVICE* ptd,
1173 HDC hdcTargetDev,
1174 HDC hdcDraw,
1175 LPCRECTL lprcBounds,
1176 LPCRECTL lprcWBounds,
1177 BOOL (CALLBACK *pfnContinue)(ULONG_PTR dwContinue),
1178 ULONG_PTR dwContinue)
1180 PresentationDataHeader presData;
1181 HMETAFILE presMetafile = 0;
1182 HRESULT hres;
1184 DataCache *this = impl_from_IViewObject2(iface);
1186 TRACE("(%p, %lx, %ld, %p, %p, %p, %p, %p, %p, %lx)\n",
1187 iface,
1188 dwDrawAspect,
1189 lindex,
1190 pvAspect,
1191 hdcTargetDev,
1192 hdcDraw,
1193 lprcBounds,
1194 lprcWBounds,
1195 pfnContinue,
1196 dwContinue);
1199 * Sanity check
1201 if (lprcBounds==NULL)
1202 return E_INVALIDARG;
1205 * First, we need to retrieve the dimensions of the
1206 * image in the metafile.
1208 hres = DataCache_ReadPresentationData(this,
1209 dwDrawAspect,
1210 &presData);
1212 if (FAILED(hres))
1213 return hres;
1216 * Then, we can extract the metafile itself from the cached
1217 * data.
1219 * FIXME Unless it isn't a metafile. I think it could be any CF_XXX type,
1220 * particularly CF_DIB.
1222 presMetafile = DataCache_ReadPresMetafile(this,
1223 dwDrawAspect);
1226 * If we have a metafile, just draw baby...
1227 * We have to be careful not to modify the state of the
1228 * DC.
1230 if (presMetafile!=0)
1232 INT prevMapMode = SetMapMode(hdcDraw, MM_ANISOTROPIC);
1233 SIZE oldWindowExt;
1234 SIZE oldViewportExt;
1235 POINT oldViewportOrg;
1237 SetWindowExtEx(hdcDraw,
1238 presData.dwObjectExtentX,
1239 presData.dwObjectExtentY,
1240 &oldWindowExt);
1242 SetViewportExtEx(hdcDraw,
1243 lprcBounds->right - lprcBounds->left,
1244 lprcBounds->bottom - lprcBounds->top,
1245 &oldViewportExt);
1247 SetViewportOrgEx(hdcDraw,
1248 lprcBounds->left,
1249 lprcBounds->top,
1250 &oldViewportOrg);
1252 PlayMetaFile(hdcDraw, presMetafile);
1254 SetWindowExtEx(hdcDraw,
1255 oldWindowExt.cx,
1256 oldWindowExt.cy,
1257 NULL);
1259 SetViewportExtEx(hdcDraw,
1260 oldViewportExt.cx,
1261 oldViewportExt.cy,
1262 NULL);
1264 SetViewportOrgEx(hdcDraw,
1265 oldViewportOrg.x,
1266 oldViewportOrg.y,
1267 NULL);
1269 SetMapMode(hdcDraw, prevMapMode);
1271 DeleteMetaFile(presMetafile);
1274 return S_OK;
1277 static HRESULT WINAPI DataCache_GetColorSet(
1278 IViewObject2* iface,
1279 DWORD dwDrawAspect,
1280 LONG lindex,
1281 void* pvAspect,
1282 DVTARGETDEVICE* ptd,
1283 HDC hicTargetDevice,
1284 LOGPALETTE** ppColorSet)
1286 FIXME("stub\n");
1287 return E_NOTIMPL;
1290 static HRESULT WINAPI DataCache_Freeze(
1291 IViewObject2* iface,
1292 DWORD dwDrawAspect,
1293 LONG lindex,
1294 void* pvAspect,
1295 DWORD* pdwFreeze)
1297 FIXME("stub\n");
1298 return E_NOTIMPL;
1301 static HRESULT WINAPI DataCache_Unfreeze(
1302 IViewObject2* iface,
1303 DWORD dwFreeze)
1305 FIXME("stub\n");
1306 return E_NOTIMPL;
1309 /************************************************************************
1310 * DataCache_SetAdvise (IViewObject2)
1312 * This sets-up an advisory sink with the data cache. When the object's
1313 * view changes, this sink is called.
1315 * See Windows documentation for more details on IViewObject2 methods.
1317 static HRESULT WINAPI DataCache_SetAdvise(
1318 IViewObject2* iface,
1319 DWORD aspects,
1320 DWORD advf,
1321 IAdviseSink* pAdvSink)
1323 DataCache *this = impl_from_IViewObject2(iface);
1325 TRACE("(%p, %lx, %lx, %p)\n", iface, aspects, advf, pAdvSink);
1328 * A call to this function removes the previous sink
1330 if (this->sinkInterface != NULL)
1332 IAdviseSink_Release(this->sinkInterface);
1333 this->sinkInterface = NULL;
1334 this->sinkAspects = 0;
1335 this->sinkAdviseFlag = 0;
1339 * Now, setup the new one.
1341 if (pAdvSink!=NULL)
1343 this->sinkInterface = pAdvSink;
1344 this->sinkAspects = aspects;
1345 this->sinkAdviseFlag = advf;
1347 IAdviseSink_AddRef(this->sinkInterface);
1351 * When the ADVF_PRIMEFIRST flag is set, we have to advise the
1352 * sink immediately.
1354 if (advf & ADVF_PRIMEFIRST)
1356 DataCache_FireOnViewChange(this,
1357 DVASPECT_CONTENT,
1358 -1);
1361 return S_OK;
1364 /************************************************************************
1365 * DataCache_GetAdvise (IViewObject2)
1367 * This method queries the current state of the advise sink
1368 * installed on the data cache.
1370 * See Windows documentation for more details on IViewObject2 methods.
1372 static HRESULT WINAPI DataCache_GetAdvise(
1373 IViewObject2* iface,
1374 DWORD* pAspects,
1375 DWORD* pAdvf,
1376 IAdviseSink** ppAdvSink)
1378 DataCache *this = impl_from_IViewObject2(iface);
1380 TRACE("(%p, %p, %p, %p)\n", iface, pAspects, pAdvf, ppAdvSink);
1383 * Just copy all the requested values.
1385 if (pAspects!=NULL)
1386 *pAspects = this->sinkAspects;
1388 if (pAdvf!=NULL)
1389 *pAdvf = this->sinkAdviseFlag;
1391 if (ppAdvSink!=NULL)
1393 if (this->sinkInterface != NULL)
1394 IAdviseSink_QueryInterface(this->sinkInterface,
1395 &IID_IAdviseSink,
1396 (void**)ppAdvSink);
1397 else *ppAdvSink = NULL;
1400 return S_OK;
1403 /************************************************************************
1404 * DataCache_GetExtent (IViewObject2)
1406 * This method retrieves the "natural" size of this cached object.
1408 * See Windows documentation for more details on IViewObject2 methods.
1410 static HRESULT WINAPI DataCache_GetExtent(
1411 IViewObject2* iface,
1412 DWORD dwDrawAspect,
1413 LONG lindex,
1414 DVTARGETDEVICE* ptd,
1415 LPSIZEL lpsizel)
1417 PresentationDataHeader presData;
1418 HRESULT hres = E_FAIL;
1420 DataCache *this = impl_from_IViewObject2(iface);
1422 TRACE("(%p, %lx, %ld, %p, %p)\n",
1423 iface, dwDrawAspect, lindex, ptd, lpsizel);
1426 * Sanity check
1428 if (lpsizel==NULL)
1429 return E_POINTER;
1432 * Initialize the out parameter.
1434 lpsizel->cx = 0;
1435 lpsizel->cy = 0;
1438 * This flag should be set to -1.
1440 if (lindex!=-1)
1441 FIXME("Unimplemented flag lindex = %ld\n", lindex);
1444 * Right now, we support only the callback from
1445 * the default handler.
1447 if (ptd!=NULL)
1448 FIXME("Unimplemented ptd = %p\n", ptd);
1451 * Get the presentation information from the
1452 * cache.
1454 hres = DataCache_ReadPresentationData(this,
1455 dwDrawAspect,
1456 &presData);
1458 if (SUCCEEDED(hres))
1460 lpsizel->cx = presData.dwObjectExtentX;
1461 lpsizel->cy = presData.dwObjectExtentY;
1465 * This method returns OLE_E_BLANK when it fails.
1467 if (FAILED(hres))
1468 hres = OLE_E_BLANK;
1470 return hres;
1474 /*********************************************************
1475 * Method implementation for the IOleCache2
1476 * part of the DataCache class.
1479 /************************************************************************
1480 * DataCache_IOleCache2_QueryInterface (IUnknown)
1482 * See Windows documentation for more details on IUnknown methods.
1484 static HRESULT WINAPI DataCache_IOleCache2_QueryInterface(
1485 IOleCache2* iface,
1486 REFIID riid,
1487 void** ppvObject)
1489 DataCache *this = impl_from_IOleCache2(iface);
1491 return IUnknown_QueryInterface(this->outerUnknown, riid, ppvObject);
1494 /************************************************************************
1495 * DataCache_IOleCache2_AddRef (IUnknown)
1497 * See Windows documentation for more details on IUnknown methods.
1499 static ULONG WINAPI DataCache_IOleCache2_AddRef(
1500 IOleCache2* iface)
1502 DataCache *this = impl_from_IOleCache2(iface);
1504 return IUnknown_AddRef(this->outerUnknown);
1507 /************************************************************************
1508 * DataCache_IOleCache2_Release (IUnknown)
1510 * See Windows documentation for more details on IUnknown methods.
1512 static ULONG WINAPI DataCache_IOleCache2_Release(
1513 IOleCache2* iface)
1515 DataCache *this = impl_from_IOleCache2(iface);
1517 return IUnknown_Release(this->outerUnknown);
1520 static HRESULT WINAPI DataCache_Cache(
1521 IOleCache2* iface,
1522 FORMATETC* pformatetc,
1523 DWORD advf,
1524 DWORD* pdwConnection)
1526 FIXME("stub\n");
1527 return E_NOTIMPL;
1530 static HRESULT WINAPI DataCache_Uncache(
1531 IOleCache2* iface,
1532 DWORD dwConnection)
1534 FIXME("stub\n");
1535 return E_NOTIMPL;
1538 static HRESULT WINAPI DataCache_EnumCache(
1539 IOleCache2* iface,
1540 IEnumSTATDATA** ppenumSTATDATA)
1542 FIXME("stub\n");
1543 return E_NOTIMPL;
1546 static HRESULT WINAPI DataCache_InitCache(
1547 IOleCache2* iface,
1548 IDataObject* pDataObject)
1550 FIXME("stub\n");
1551 return E_NOTIMPL;
1554 static HRESULT WINAPI DataCache_IOleCache2_SetData(
1555 IOleCache2* iface,
1556 FORMATETC* pformatetc,
1557 STGMEDIUM* pmedium,
1558 BOOL fRelease)
1560 FIXME("stub\n");
1561 return E_NOTIMPL;
1564 static HRESULT WINAPI DataCache_UpdateCache(
1565 IOleCache2* iface,
1566 LPDATAOBJECT pDataObject,
1567 DWORD grfUpdf,
1568 LPVOID pReserved)
1570 FIXME("stub\n");
1571 return E_NOTIMPL;
1574 static HRESULT WINAPI DataCache_DiscardCache(
1575 IOleCache2* iface,
1576 DWORD dwDiscardOptions)
1578 FIXME("stub\n");
1579 return E_NOTIMPL;
1583 /*********************************************************
1584 * Method implementation for the IOleCacheControl
1585 * part of the DataCache class.
1588 /************************************************************************
1589 * DataCache_IOleCacheControl_QueryInterface (IUnknown)
1591 * See Windows documentation for more details on IUnknown methods.
1593 static HRESULT WINAPI DataCache_IOleCacheControl_QueryInterface(
1594 IOleCacheControl* iface,
1595 REFIID riid,
1596 void** ppvObject)
1598 DataCache *this = impl_from_IOleCacheControl(iface);
1600 return IUnknown_QueryInterface(this->outerUnknown, riid, ppvObject);
1603 /************************************************************************
1604 * DataCache_IOleCacheControl_AddRef (IUnknown)
1606 * See Windows documentation for more details on IUnknown methods.
1608 static ULONG WINAPI DataCache_IOleCacheControl_AddRef(
1609 IOleCacheControl* iface)
1611 DataCache *this = impl_from_IOleCacheControl(iface);
1613 return IUnknown_AddRef(this->outerUnknown);
1616 /************************************************************************
1617 * DataCache_IOleCacheControl_Release (IUnknown)
1619 * See Windows documentation for more details on IUnknown methods.
1621 static ULONG WINAPI DataCache_IOleCacheControl_Release(
1622 IOleCacheControl* iface)
1624 DataCache *this = impl_from_IOleCacheControl(iface);
1626 return IUnknown_Release(this->outerUnknown);
1629 static HRESULT WINAPI DataCache_OnRun(
1630 IOleCacheControl* iface,
1631 LPDATAOBJECT pDataObject)
1633 FIXME("stub\n");
1634 return E_NOTIMPL;
1637 static HRESULT WINAPI DataCache_OnStop(
1638 IOleCacheControl* iface)
1640 FIXME("stub\n");
1641 return E_NOTIMPL;
1645 * Virtual function tables for the DataCache class.
1647 static const IUnknownVtbl DataCache_NDIUnknown_VTable =
1649 DataCache_NDIUnknown_QueryInterface,
1650 DataCache_NDIUnknown_AddRef,
1651 DataCache_NDIUnknown_Release
1654 static const IDataObjectVtbl DataCache_IDataObject_VTable =
1656 DataCache_IDataObject_QueryInterface,
1657 DataCache_IDataObject_AddRef,
1658 DataCache_IDataObject_Release,
1659 DataCache_GetData,
1660 DataCache_GetDataHere,
1661 DataCache_QueryGetData,
1662 DataCache_GetCanonicalFormatEtc,
1663 DataCache_IDataObject_SetData,
1664 DataCache_EnumFormatEtc,
1665 DataCache_DAdvise,
1666 DataCache_DUnadvise,
1667 DataCache_EnumDAdvise
1670 static const IPersistStorageVtbl DataCache_IPersistStorage_VTable =
1672 DataCache_IPersistStorage_QueryInterface,
1673 DataCache_IPersistStorage_AddRef,
1674 DataCache_IPersistStorage_Release,
1675 DataCache_GetClassID,
1676 DataCache_IsDirty,
1677 DataCache_InitNew,
1678 DataCache_Load,
1679 DataCache_Save,
1680 DataCache_SaveCompleted,
1681 DataCache_HandsOffStorage
1684 static const IViewObject2Vtbl DataCache_IViewObject2_VTable =
1686 DataCache_IViewObject2_QueryInterface,
1687 DataCache_IViewObject2_AddRef,
1688 DataCache_IViewObject2_Release,
1689 DataCache_Draw,
1690 DataCache_GetColorSet,
1691 DataCache_Freeze,
1692 DataCache_Unfreeze,
1693 DataCache_SetAdvise,
1694 DataCache_GetAdvise,
1695 DataCache_GetExtent
1698 static const IOleCache2Vtbl DataCache_IOleCache2_VTable =
1700 DataCache_IOleCache2_QueryInterface,
1701 DataCache_IOleCache2_AddRef,
1702 DataCache_IOleCache2_Release,
1703 DataCache_Cache,
1704 DataCache_Uncache,
1705 DataCache_EnumCache,
1706 DataCache_InitCache,
1707 DataCache_IOleCache2_SetData,
1708 DataCache_UpdateCache,
1709 DataCache_DiscardCache
1712 static const IOleCacheControlVtbl DataCache_IOleCacheControl_VTable =
1714 DataCache_IOleCacheControl_QueryInterface,
1715 DataCache_IOleCacheControl_AddRef,
1716 DataCache_IOleCacheControl_Release,
1717 DataCache_OnRun,
1718 DataCache_OnStop
1721 /******************************************************************************
1722 * CreateDataCache [OLE32.@]
1724 HRESULT WINAPI CreateDataCache(
1725 LPUNKNOWN pUnkOuter,
1726 REFCLSID rclsid,
1727 REFIID riid,
1728 LPVOID* ppvObj)
1730 DataCache* newCache = NULL;
1731 HRESULT hr = S_OK;
1733 TRACE("(%s, %p, %s, %p)\n", debugstr_guid(rclsid), pUnkOuter, debugstr_guid(riid), ppvObj);
1736 * Sanity check
1738 if (ppvObj==0)
1739 return E_POINTER;
1741 *ppvObj = 0;
1744 * If this cache is constructed for aggregation, make sure
1745 * the caller is requesting the IUnknown interface.
1746 * This is necessary because it's the only time the non-delegating
1747 * IUnknown pointer can be returned to the outside.
1749 if ( (pUnkOuter!=NULL) &&
1750 (memcmp(&IID_IUnknown, riid, sizeof(IID_IUnknown)) != 0) )
1751 return CLASS_E_NOAGGREGATION;
1754 * Try to construct a new instance of the class.
1756 newCache = DataCache_Construct(rclsid,
1757 pUnkOuter);
1759 if (newCache == 0)
1760 return E_OUTOFMEMORY;
1763 * Make sure it supports the interface required by the caller.
1765 hr = IUnknown_QueryInterface((IUnknown*)&(newCache->lpvtblNDIUnknown), riid, ppvObj);
1768 * Release the reference obtained in the constructor. If
1769 * the QueryInterface was unsuccessful, it will free the class.
1771 IUnknown_Release((IUnknown*)&(newCache->lpvtblNDIUnknown));
1773 return hr;
1776 /*********************************************************
1777 * Method implementation for DataCache class.
1779 static DataCache* DataCache_Construct(
1780 REFCLSID clsid,
1781 LPUNKNOWN pUnkOuter)
1783 DataCache* newObject = 0;
1786 * Allocate space for the object.
1788 newObject = HeapAlloc(GetProcessHeap(), 0, sizeof(DataCache));
1790 if (newObject==0)
1791 return newObject;
1794 * Initialize the virtual function table.
1796 newObject->lpVtbl = &DataCache_IDataObject_VTable;
1797 newObject->lpvtblNDIUnknown = &DataCache_NDIUnknown_VTable;
1798 newObject->lpvtblIPersistStorage = &DataCache_IPersistStorage_VTable;
1799 newObject->lpvtblIViewObject = &DataCache_IViewObject2_VTable;
1800 newObject->lpvtblIOleCache2 = &DataCache_IOleCache2_VTable;
1801 newObject->lpvtblIOleCacheControl = &DataCache_IOleCacheControl_VTable;
1804 * Start with one reference count. The caller of this function
1805 * must release the interface pointer when it is done.
1807 newObject->ref = 1;
1810 * Initialize the outer unknown
1811 * We don't keep a reference on the outer unknown since, the way
1812 * aggregation works, our lifetime is at least as large as it's
1813 * lifetime.
1815 if (pUnkOuter==NULL)
1816 pUnkOuter = (IUnknown*)&(newObject->lpvtblNDIUnknown);
1818 newObject->outerUnknown = pUnkOuter;
1821 * Initialize the other members of the structure.
1823 newObject->presentationStorage = NULL;
1824 newObject->sinkAspects = 0;
1825 newObject->sinkAdviseFlag = 0;
1826 newObject->sinkInterface = 0;
1828 return newObject;