windowscodecs/tests: Use common wine_dbgstr_guid implementation from test.h.
[wine.git] / dlls / ole32 / datacache.c
blob3e5e99c94cd6dda8a606de84fcb48880f520eadf
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 <stdarg.h>
47 #include <string.h>
49 #define COBJMACROS
50 #define NONAMELESSUNION
51 #define NONAMELESSSTRUCT
53 #include "windef.h"
54 #include "winbase.h"
55 #include "wingdi.h"
56 #include "winuser.h"
57 #include "winerror.h"
58 #include "wine/unicode.h"
59 #include "ole2.h"
60 #include "wine/list.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 storage.
71 typedef struct PresentationDataHeader
73 /* clipformat:
74 * - standard clipformat:
75 * DWORD length = 0xffffffff;
76 * DWORD cfFormat;
77 * - or custom clipformat:
78 * DWORD length;
79 * CHAR format_name[length]; (null-terminated)
81 DWORD unknown3; /* 4, possibly TYMED_ISTREAM */
82 DVASPECT dvAspect;
83 DWORD lindex;
84 DWORD tymed;
85 DWORD unknown7; /* 0 */
86 DWORD dwObjectExtentX;
87 DWORD dwObjectExtentY;
88 DWORD dwSize;
89 } PresentationDataHeader;
91 typedef struct DataCacheEntry
93 struct list entry;
94 /* format of this entry */
95 FORMATETC fmtetc;
96 /* the clipboard format of the data */
97 CLIPFORMAT data_cf;
98 /* cached data */
99 STGMEDIUM stgmedium;
101 * This storage pointer is set through a call to
102 * IPersistStorage_Load. This is where the visual
103 * representation of the object is stored.
105 IStorage *storage;
106 /* connection ID */
107 DWORD id;
108 /* dirty flag */
109 BOOL dirty;
110 /* stream number (-1 if not set ) */
111 unsigned short stream_number;
112 /* sink id set when object is running */
113 DWORD sink_id;
114 /* Advise sink flags */
115 DWORD advise_flags;
116 } DataCacheEntry;
118 /****************************************************************************
119 * DataCache
121 struct DataCache
124 * List all interface here
126 IDataObject IDataObject_iface;
127 IUnknown IUnknown_iface;
128 IPersistStorage IPersistStorage_iface;
129 IViewObject2 IViewObject2_iface;
130 IOleCache2 IOleCache2_iface;
131 IOleCacheControl IOleCacheControl_iface;
133 /* The sink that is connected to a remote object.
134 The other interfaces are not available by QI'ing the sink and vice-versa */
135 IAdviseSink IAdviseSink_iface;
138 * Reference count of this object
140 LONG ref;
143 * IUnknown implementation of the outer object.
145 IUnknown* outerUnknown;
148 * The user of this object can setup ONE advise sink
149 * connection with the object. These parameters describe
150 * that connection.
152 DWORD sinkAspects;
153 DWORD sinkAdviseFlag;
154 IAdviseSink* sinkInterface;
155 IStorage *presentationStorage;
157 /* list of cache entries */
158 struct list cache_list;
159 /* last id assigned to an entry */
160 DWORD last_cache_id;
161 /* dirty flag */
162 BOOL dirty;
163 /* running object set by OnRun */
164 IDataObject *running_object;
167 typedef struct DataCache DataCache;
170 * Here, I define utility macros to help with the casting of the
171 * "this" parameter.
172 * There is a version to accommodate all of the VTables implemented
173 * by this object.
176 static inline DataCache *impl_from_IDataObject( IDataObject *iface )
178 return CONTAINING_RECORD(iface, DataCache, IDataObject_iface);
181 static inline DataCache *impl_from_IUnknown( IUnknown *iface )
183 return CONTAINING_RECORD(iface, DataCache, IUnknown_iface);
186 static inline DataCache *impl_from_IPersistStorage( IPersistStorage *iface )
188 return CONTAINING_RECORD(iface, DataCache, IPersistStorage_iface);
191 static inline DataCache *impl_from_IViewObject2( IViewObject2 *iface )
193 return CONTAINING_RECORD(iface, DataCache, IViewObject2_iface);
196 static inline DataCache *impl_from_IOleCache2( IOleCache2 *iface )
198 return CONTAINING_RECORD(iface, DataCache, IOleCache2_iface);
201 static inline DataCache *impl_from_IOleCacheControl( IOleCacheControl *iface )
203 return CONTAINING_RECORD(iface, DataCache, IOleCacheControl_iface);
206 static inline DataCache *impl_from_IAdviseSink( IAdviseSink *iface )
208 return CONTAINING_RECORD(iface, DataCache, IAdviseSink_iface);
211 static const char * debugstr_formatetc(const FORMATETC *formatetc)
213 return wine_dbg_sprintf("{ cfFormat = 0x%x, ptd = %p, dwAspect = %d, lindex = %d, tymed = %d }",
214 formatetc->cfFormat, formatetc->ptd, formatetc->dwAspect,
215 formatetc->lindex, formatetc->tymed);
218 static void DataCacheEntry_Destroy(DataCache *cache, DataCacheEntry *cache_entry)
220 list_remove(&cache_entry->entry);
221 if (cache_entry->storage)
222 IStorage_Release(cache_entry->storage);
223 HeapFree(GetProcessHeap(), 0, cache_entry->fmtetc.ptd);
224 ReleaseStgMedium(&cache_entry->stgmedium);
225 if(cache_entry->sink_id)
226 IDataObject_DUnadvise(cache->running_object, cache_entry->sink_id);
228 HeapFree(GetProcessHeap(), 0, cache_entry);
231 static void DataCache_Destroy(
232 DataCache* ptrToDestroy)
234 DataCacheEntry *cache_entry, *next_cache_entry;
236 TRACE("()\n");
238 if (ptrToDestroy->sinkInterface != NULL)
240 IAdviseSink_Release(ptrToDestroy->sinkInterface);
241 ptrToDestroy->sinkInterface = NULL;
244 LIST_FOR_EACH_ENTRY_SAFE(cache_entry, next_cache_entry, &ptrToDestroy->cache_list, DataCacheEntry, entry)
245 DataCacheEntry_Destroy(ptrToDestroy, cache_entry);
247 if (ptrToDestroy->presentationStorage != NULL)
249 IStorage_Release(ptrToDestroy->presentationStorage);
250 ptrToDestroy->presentationStorage = NULL;
254 * Free the datacache pointer.
256 HeapFree(GetProcessHeap(), 0, ptrToDestroy);
259 static DataCacheEntry *DataCache_GetEntryForFormatEtc(DataCache *This, const FORMATETC *formatetc)
261 DataCacheEntry *cache_entry;
262 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
264 /* FIXME: also compare DVTARGETDEVICEs */
265 if ((!cache_entry->fmtetc.cfFormat || !formatetc->cfFormat || (formatetc->cfFormat == cache_entry->fmtetc.cfFormat)) &&
266 (formatetc->dwAspect == cache_entry->fmtetc.dwAspect) &&
267 (formatetc->lindex == cache_entry->fmtetc.lindex) &&
268 (!cache_entry->fmtetc.tymed || !formatetc->tymed || (formatetc->tymed == cache_entry->fmtetc.tymed)))
269 return cache_entry;
271 return NULL;
274 /* checks that the clipformat and tymed are valid and returns an error if they
275 * aren't and CACHE_S_NOTSUPPORTED if they are valid, but can't be rendered by
276 * DataCache_Draw */
277 static HRESULT check_valid_clipformat_and_tymed(CLIPFORMAT cfFormat, DWORD tymed)
279 if (!cfFormat || !tymed ||
280 (cfFormat == CF_METAFILEPICT && tymed == TYMED_MFPICT) ||
281 (cfFormat == CF_BITMAP && tymed == TYMED_GDI) ||
282 (cfFormat == CF_DIB && tymed == TYMED_HGLOBAL) ||
283 (cfFormat == CF_ENHMETAFILE && tymed == TYMED_ENHMF))
284 return S_OK;
285 else if (tymed == TYMED_HGLOBAL)
286 return CACHE_S_FORMATETC_NOTSUPPORTED;
287 else
289 WARN("invalid clipformat/tymed combination: %d/%d\n", cfFormat, tymed);
290 return DV_E_TYMED;
294 static HRESULT DataCache_CreateEntry(DataCache *This, const FORMATETC *formatetc, DataCacheEntry **cache_entry)
296 HRESULT hr;
298 hr = check_valid_clipformat_and_tymed(formatetc->cfFormat, formatetc->tymed);
299 if (FAILED(hr))
300 return hr;
301 if (hr == CACHE_S_FORMATETC_NOTSUPPORTED)
302 TRACE("creating unsupported format %d\n", formatetc->cfFormat);
304 *cache_entry = HeapAlloc(GetProcessHeap(), 0, sizeof(**cache_entry));
305 if (!*cache_entry)
306 return E_OUTOFMEMORY;
308 (*cache_entry)->fmtetc = *formatetc;
309 if (formatetc->ptd)
311 (*cache_entry)->fmtetc.ptd = HeapAlloc(GetProcessHeap(), 0, formatetc->ptd->tdSize);
312 memcpy((*cache_entry)->fmtetc.ptd, formatetc->ptd, formatetc->ptd->tdSize);
314 (*cache_entry)->data_cf = 0;
315 (*cache_entry)->stgmedium.tymed = TYMED_NULL;
316 (*cache_entry)->stgmedium.pUnkForRelease = NULL;
317 (*cache_entry)->storage = NULL;
318 (*cache_entry)->id = This->last_cache_id++;
319 (*cache_entry)->dirty = TRUE;
320 (*cache_entry)->stream_number = -1;
321 (*cache_entry)->sink_id = 0;
322 (*cache_entry)->advise_flags = 0;
323 list_add_tail(&This->cache_list, &(*cache_entry)->entry);
324 return hr;
327 /************************************************************************
328 * DataCache_FireOnViewChange
330 * This method will fire an OnViewChange notification to the advise
331 * sink registered with the datacache.
333 * See IAdviseSink::OnViewChange for more details.
335 static void DataCache_FireOnViewChange(
336 DataCache* this,
337 DWORD aspect,
338 LONG lindex)
340 TRACE("(%p, %x, %d)\n", this, aspect, lindex);
343 * The sink supplies a filter when it registers
344 * we make sure we only send the notifications when that
345 * filter matches.
347 if ((this->sinkAspects & aspect) != 0)
349 if (this->sinkInterface != NULL)
351 IAdviseSink_OnViewChange(this->sinkInterface,
352 aspect,
353 lindex);
356 * Some sinks want to be unregistered automatically when
357 * the first notification goes out.
359 if ( (this->sinkAdviseFlag & ADVF_ONLYONCE) != 0)
361 IAdviseSink_Release(this->sinkInterface);
363 this->sinkInterface = NULL;
364 this->sinkAspects = 0;
365 this->sinkAdviseFlag = 0;
371 /* Helper for DataCacheEntry_OpenPresStream */
372 static BOOL DataCache_IsPresentationStream(const STATSTG *elem)
374 /* The presentation streams have names of the form "\002OlePresXXX",
375 * where XXX goes from 000 to 999. */
376 static const WCHAR OlePres[] = { 2,'O','l','e','P','r','e','s' };
378 LPCWSTR name = elem->pwcsName;
380 return (elem->type == STGTY_STREAM)
381 && (strlenW(name) == 11)
382 && (strncmpW(name, OlePres, 8) == 0)
383 && (name[8] >= '0') && (name[8] <= '9')
384 && (name[9] >= '0') && (name[9] <= '9')
385 && (name[10] >= '0') && (name[10] <= '9');
388 static HRESULT read_clipformat(IStream *stream, CLIPFORMAT *clipformat)
390 DWORD length;
391 HRESULT hr;
392 ULONG read;
394 *clipformat = 0;
396 hr = IStream_Read(stream, &length, sizeof(length), &read);
397 if (hr != S_OK || read != sizeof(length))
398 return DV_E_CLIPFORMAT;
399 if (length == -1)
401 DWORD cf;
402 hr = IStream_Read(stream, &cf, sizeof(cf), 0);
403 if (hr != S_OK || read != sizeof(cf))
404 return DV_E_CLIPFORMAT;
405 *clipformat = cf;
407 else
409 char *format_name = HeapAlloc(GetProcessHeap(), 0, length);
410 if (!format_name)
411 return E_OUTOFMEMORY;
412 hr = IStream_Read(stream, format_name, length, &read);
413 if (hr != S_OK || read != length || format_name[length - 1] != '\0')
415 HeapFree(GetProcessHeap(), 0, format_name);
416 return DV_E_CLIPFORMAT;
418 *clipformat = RegisterClipboardFormatA(format_name);
419 HeapFree(GetProcessHeap(), 0, format_name);
421 return S_OK;
424 static HRESULT write_clipformat(IStream *stream, CLIPFORMAT clipformat)
426 DWORD length;
427 HRESULT hr;
429 if (clipformat < 0xc000)
430 length = -1;
431 else
432 length = GetClipboardFormatNameA(clipformat, NULL, 0);
433 hr = IStream_Write(stream, &length, sizeof(length), NULL);
434 if (FAILED(hr))
435 return hr;
436 if (clipformat < 0xc000)
438 DWORD cf = clipformat;
439 hr = IStream_Write(stream, &cf, sizeof(cf), NULL);
441 else
443 char *format_name = HeapAlloc(GetProcessHeap(), 0, length);
444 if (!format_name)
445 return E_OUTOFMEMORY;
446 GetClipboardFormatNameA(clipformat, format_name, length);
447 hr = IStream_Write(stream, format_name, length, NULL);
448 HeapFree(GetProcessHeap(), 0, format_name);
450 return hr;
453 /************************************************************************
454 * DataCacheEntry_OpenPresStream
456 * This method will find the stream for the given presentation. It makes
457 * no attempt at fallback.
459 * Param:
460 * this - Pointer to the DataCache object
461 * drawAspect - The aspect of the object that we wish to draw.
462 * pStm - A returned stream. It points to the beginning of the
463 * - presentation data, including the header.
465 * Errors:
466 * S_OK The requested stream has been opened.
467 * OLE_E_BLANK The requested stream could not be found.
468 * Quite a few others I'm too lazy to map correctly.
470 * Notes:
471 * Algorithm: Scan the elements of the presentation storage, looking
472 * for presentation streams. For each presentation stream,
473 * load the header and check to see if the aspect matches.
475 * If a fallback is desired, just opening the first presentation stream
476 * is a possibility.
478 static HRESULT DataCacheEntry_OpenPresStream(DataCacheEntry *cache_entry, IStream **ppStm)
480 STATSTG elem;
481 IEnumSTATSTG *pEnum;
482 HRESULT hr;
484 if (!ppStm) return E_POINTER;
486 hr = IStorage_EnumElements(cache_entry->storage, 0, NULL, 0, &pEnum);
487 if (FAILED(hr)) return hr;
489 while ((hr = IEnumSTATSTG_Next(pEnum, 1, &elem, NULL)) == S_OK)
491 if (DataCache_IsPresentationStream(&elem))
493 IStream *pStm;
495 hr = IStorage_OpenStream(cache_entry->storage, elem.pwcsName,
496 NULL, STGM_READ | STGM_SHARE_EXCLUSIVE, 0,
497 &pStm);
498 if (SUCCEEDED(hr))
500 PresentationDataHeader header;
501 ULONG actual_read;
502 CLIPFORMAT clipformat;
504 hr = read_clipformat(pStm, &clipformat);
506 if (hr == S_OK)
507 hr = IStream_Read(pStm, &header, sizeof(header), &actual_read);
509 /* can't use SUCCEEDED(hr): S_FALSE counts as an error */
510 if (hr == S_OK && actual_read == sizeof(header)
511 && header.dvAspect == cache_entry->fmtetc.dwAspect)
513 /* Rewind the stream before returning it. */
514 LARGE_INTEGER offset;
515 offset.u.LowPart = 0;
516 offset.u.HighPart = 0;
517 IStream_Seek(pStm, offset, STREAM_SEEK_SET, NULL);
519 *ppStm = pStm;
521 CoTaskMemFree(elem.pwcsName);
522 IEnumSTATSTG_Release(pEnum);
524 return S_OK;
527 IStream_Release(pStm);
531 CoTaskMemFree(elem.pwcsName);
534 IEnumSTATSTG_Release(pEnum);
536 return (hr == S_FALSE ? OLE_E_BLANK : hr);
539 /************************************************************************
540 * DataCacheEntry_LoadData
542 * This method will read information for the requested presentation
543 * into the given structure.
545 * Param:
546 * This - The entry to load the data from.
548 * Returns:
549 * This method returns a metafile handle if it is successful.
550 * it will return 0 if not.
552 static HRESULT DataCacheEntry_LoadData(DataCacheEntry *cache_entry)
554 IStream* presStream = NULL;
555 HRESULT hres;
556 ULARGE_INTEGER current_pos;
557 STATSTG streamInfo;
558 void* metafileBits;
559 METAFILEPICT *mfpict;
560 HGLOBAL hmfpict;
561 PresentationDataHeader header;
562 CLIPFORMAT clipformat;
563 static const LARGE_INTEGER offset_zero;
566 * Open the presentation stream.
568 hres = DataCacheEntry_OpenPresStream(cache_entry, &presStream);
570 if (FAILED(hres))
571 return hres;
574 * Get the size of the stream.
576 hres = IStream_Stat(presStream,
577 &streamInfo,
578 STATFLAG_NONAME);
581 * Read the header.
584 hres = read_clipformat(presStream, &clipformat);
585 if (FAILED(hres))
587 IStream_Release(presStream);
588 return hres;
591 hres = IStream_Read(
592 presStream,
593 &header,
594 sizeof(PresentationDataHeader),
595 NULL);
596 if (hres != S_OK)
598 IStream_Release(presStream);
599 return E_FAIL;
602 hres = IStream_Seek(presStream, offset_zero, STREAM_SEEK_CUR, &current_pos);
604 streamInfo.cbSize.QuadPart -= current_pos.QuadPart;
606 hmfpict = GlobalAlloc(GMEM_MOVEABLE, sizeof(METAFILEPICT));
607 if (!hmfpict)
609 IStream_Release(presStream);
610 return E_OUTOFMEMORY;
612 mfpict = GlobalLock(hmfpict);
615 * Allocate a buffer for the metafile bits.
617 metafileBits = HeapAlloc(GetProcessHeap(),
619 streamInfo.cbSize.u.LowPart);
622 * Read the metafile bits.
624 hres = IStream_Read(
625 presStream,
626 metafileBits,
627 streamInfo.cbSize.u.LowPart,
628 NULL);
631 * Create a metafile with those bits.
633 if (SUCCEEDED(hres))
635 /* FIXME: get this from the stream */
636 mfpict->mm = MM_ANISOTROPIC;
637 mfpict->xExt = header.dwObjectExtentX;
638 mfpict->yExt = header.dwObjectExtentY;
639 mfpict->hMF = SetMetaFileBitsEx(streamInfo.cbSize.u.LowPart, metafileBits);
640 if (!mfpict->hMF)
641 hres = E_FAIL;
644 GlobalUnlock(hmfpict);
645 if (SUCCEEDED(hres))
647 cache_entry->data_cf = cache_entry->fmtetc.cfFormat;
648 cache_entry->stgmedium.tymed = TYMED_MFPICT;
649 cache_entry->stgmedium.u.hMetaFilePict = hmfpict;
651 else
652 GlobalFree(hmfpict);
655 * Cleanup.
657 HeapFree(GetProcessHeap(), 0, metafileBits);
658 IStream_Release(presStream);
660 return hres;
663 static HRESULT DataCacheEntry_CreateStream(DataCacheEntry *cache_entry,
664 IStorage *storage, IStream **stream)
666 WCHAR wszName[] = {2,'O','l','e','P','r','e','s',
667 '0' + (cache_entry->stream_number / 100) % 10,
668 '0' + (cache_entry->stream_number / 10) % 10,
669 '0' + cache_entry->stream_number % 10, 0};
671 /* FIXME: cache the created stream in This? */
672 return IStorage_CreateStream(storage, wszName,
673 STGM_READWRITE | STGM_SHARE_EXCLUSIVE | STGM_CREATE,
674 0, 0, stream);
677 static HRESULT DataCacheEntry_Save(DataCacheEntry *cache_entry, IStorage *storage,
678 BOOL same_as_load)
680 PresentationDataHeader header;
681 HRESULT hr;
682 IStream *pres_stream;
683 void *data = NULL;
685 TRACE("stream_number = %d, fmtetc = %s\n", cache_entry->stream_number, debugstr_formatetc(&cache_entry->fmtetc));
687 hr = DataCacheEntry_CreateStream(cache_entry, storage, &pres_stream);
688 if (FAILED(hr))
689 return hr;
691 hr = write_clipformat(pres_stream, cache_entry->data_cf);
692 if (FAILED(hr))
693 return hr;
695 if (cache_entry->fmtetc.ptd)
696 FIXME("ptd not serialized\n");
697 header.unknown3 = 4;
698 header.dvAspect = cache_entry->fmtetc.dwAspect;
699 header.lindex = cache_entry->fmtetc.lindex;
700 header.tymed = cache_entry->stgmedium.tymed;
701 header.unknown7 = 0;
702 header.dwObjectExtentX = 0;
703 header.dwObjectExtentY = 0;
704 header.dwSize = 0;
706 /* size the data */
707 switch (cache_entry->data_cf)
709 case CF_METAFILEPICT:
711 if (cache_entry->stgmedium.tymed != TYMED_NULL)
713 const METAFILEPICT *mfpict = GlobalLock(cache_entry->stgmedium.u.hMetaFilePict);
714 if (!mfpict)
716 IStream_Release(pres_stream);
717 return DV_E_STGMEDIUM;
719 header.dwObjectExtentX = mfpict->xExt;
720 header.dwObjectExtentY = mfpict->yExt;
721 header.dwSize = GetMetaFileBitsEx(mfpict->hMF, 0, NULL);
722 GlobalUnlock(cache_entry->stgmedium.u.hMetaFilePict);
724 break;
726 default:
727 break;
731 * Write the header.
733 hr = IStream_Write(pres_stream, &header, sizeof(PresentationDataHeader),
734 NULL);
735 if (FAILED(hr))
737 IStream_Release(pres_stream);
738 return hr;
741 /* get the data */
742 switch (cache_entry->data_cf)
744 case CF_METAFILEPICT:
746 if (cache_entry->stgmedium.tymed != TYMED_NULL)
748 const METAFILEPICT *mfpict = GlobalLock(cache_entry->stgmedium.u.hMetaFilePict);
749 if (!mfpict)
751 IStream_Release(pres_stream);
752 return DV_E_STGMEDIUM;
754 data = HeapAlloc(GetProcessHeap(), 0, header.dwSize);
755 GetMetaFileBitsEx(mfpict->hMF, header.dwSize, data);
756 GlobalUnlock(cache_entry->stgmedium.u.hMetaFilePict);
758 break;
760 default:
761 break;
764 if (data)
765 hr = IStream_Write(pres_stream, data, header.dwSize, NULL);
766 HeapFree(GetProcessHeap(), 0, data);
768 IStream_Release(pres_stream);
769 return hr;
772 /* helper for copying STGMEDIUM of type bitmap, MF, EMF or HGLOBAL.
773 * does no checking of whether src_stgm has a supported tymed, so this should be
774 * done in the caller */
775 static HRESULT copy_stg_medium(CLIPFORMAT cf, STGMEDIUM *dest_stgm,
776 const STGMEDIUM *src_stgm)
778 if (src_stgm->tymed == TYMED_MFPICT)
780 const METAFILEPICT *src_mfpict = GlobalLock(src_stgm->u.hMetaFilePict);
781 METAFILEPICT *dest_mfpict;
783 if (!src_mfpict)
784 return DV_E_STGMEDIUM;
785 dest_stgm->u.hMetaFilePict = GlobalAlloc(GMEM_MOVEABLE, sizeof(METAFILEPICT));
786 dest_mfpict = GlobalLock(dest_stgm->u.hMetaFilePict);
787 if (!dest_mfpict)
789 GlobalUnlock(src_stgm->u.hMetaFilePict);
790 return E_OUTOFMEMORY;
792 *dest_mfpict = *src_mfpict;
793 dest_mfpict->hMF = CopyMetaFileW(src_mfpict->hMF, NULL);
794 GlobalUnlock(src_stgm->u.hMetaFilePict);
795 GlobalUnlock(dest_stgm->u.hMetaFilePict);
797 else if (src_stgm->tymed != TYMED_NULL)
799 dest_stgm->u.hGlobal = OleDuplicateData(src_stgm->u.hGlobal, cf,
800 GMEM_MOVEABLE);
801 if (!dest_stgm->u.hGlobal)
802 return E_OUTOFMEMORY;
804 dest_stgm->tymed = src_stgm->tymed;
805 dest_stgm->pUnkForRelease = src_stgm->pUnkForRelease;
806 if (dest_stgm->pUnkForRelease)
807 IUnknown_AddRef(dest_stgm->pUnkForRelease);
808 return S_OK;
811 static HRESULT DataCacheEntry_SetData(DataCacheEntry *cache_entry,
812 const FORMATETC *formatetc,
813 const STGMEDIUM *stgmedium,
814 BOOL fRelease)
816 if ((!cache_entry->fmtetc.cfFormat && !formatetc->cfFormat) ||
817 (cache_entry->fmtetc.tymed == TYMED_NULL && formatetc->tymed == TYMED_NULL) ||
818 stgmedium->tymed == TYMED_NULL)
820 WARN("invalid formatetc\n");
821 return DV_E_FORMATETC;
824 cache_entry->dirty = TRUE;
825 ReleaseStgMedium(&cache_entry->stgmedium);
826 cache_entry->data_cf = cache_entry->fmtetc.cfFormat ? cache_entry->fmtetc.cfFormat : formatetc->cfFormat;
827 if (fRelease)
829 cache_entry->stgmedium = *stgmedium;
830 return S_OK;
832 else
833 return copy_stg_medium(cache_entry->data_cf,
834 &cache_entry->stgmedium, stgmedium);
837 static HRESULT DataCacheEntry_GetData(DataCacheEntry *cache_entry, STGMEDIUM *stgmedium)
839 if (stgmedium->tymed == TYMED_NULL && cache_entry->storage)
841 HRESULT hr = DataCacheEntry_LoadData(cache_entry);
842 if (FAILED(hr))
843 return hr;
845 if (cache_entry->stgmedium.tymed == TYMED_NULL)
846 return OLE_E_BLANK;
847 return copy_stg_medium(cache_entry->data_cf, stgmedium, &cache_entry->stgmedium);
850 static inline HRESULT DataCacheEntry_DiscardData(DataCacheEntry *cache_entry)
852 ReleaseStgMedium(&cache_entry->stgmedium);
853 cache_entry->data_cf = cache_entry->fmtetc.cfFormat;
854 return S_OK;
857 static inline void DataCacheEntry_HandsOffStorage(DataCacheEntry *cache_entry)
859 if (cache_entry->storage)
861 IStorage_Release(cache_entry->storage);
862 cache_entry->storage = NULL;
866 /*********************************************************
867 * Method implementation for the non delegating IUnknown
868 * part of the DataCache class.
871 /************************************************************************
872 * DataCache_NDIUnknown_QueryInterface (IUnknown)
874 * This version of QueryInterface will not delegate its implementation
875 * to the outer unknown.
877 static HRESULT WINAPI DataCache_NDIUnknown_QueryInterface(
878 IUnknown* iface,
879 REFIID riid,
880 void** ppvObject)
882 DataCache *this = impl_from_IUnknown(iface);
884 if ( ppvObject==0 )
885 return E_INVALIDARG;
887 *ppvObject = 0;
889 if (IsEqualIID(&IID_IUnknown, riid))
891 *ppvObject = iface;
893 else if (IsEqualIID(&IID_IDataObject, riid))
895 *ppvObject = &this->IDataObject_iface;
897 else if ( IsEqualIID(&IID_IPersistStorage, riid) ||
898 IsEqualIID(&IID_IPersist, riid) )
900 *ppvObject = &this->IPersistStorage_iface;
902 else if ( IsEqualIID(&IID_IViewObject, riid) ||
903 IsEqualIID(&IID_IViewObject2, riid) )
905 *ppvObject = &this->IViewObject2_iface;
907 else if ( IsEqualIID(&IID_IOleCache, riid) ||
908 IsEqualIID(&IID_IOleCache2, riid) )
910 *ppvObject = &this->IOleCache2_iface;
912 else if ( IsEqualIID(&IID_IOleCacheControl, riid) )
914 *ppvObject = &this->IOleCacheControl_iface;
917 if ((*ppvObject)==0)
919 WARN( "() : asking for unsupported interface %s\n", debugstr_guid(riid));
920 return E_NOINTERFACE;
923 IUnknown_AddRef((IUnknown*)*ppvObject);
925 return S_OK;
928 /************************************************************************
929 * DataCache_NDIUnknown_AddRef (IUnknown)
931 * This version of QueryInterface will not delegate its implementation
932 * to the outer unknown.
934 static ULONG WINAPI DataCache_NDIUnknown_AddRef(
935 IUnknown* iface)
937 DataCache *this = impl_from_IUnknown(iface);
938 return InterlockedIncrement(&this->ref);
941 /************************************************************************
942 * DataCache_NDIUnknown_Release (IUnknown)
944 * This version of QueryInterface will not delegate its implementation
945 * to the outer unknown.
947 static ULONG WINAPI DataCache_NDIUnknown_Release(
948 IUnknown* iface)
950 DataCache *this = impl_from_IUnknown(iface);
951 ULONG ref;
953 ref = InterlockedDecrement(&this->ref);
955 if (ref == 0) DataCache_Destroy(this);
957 return ref;
960 /*********************************************************
961 * Method implementation for the IDataObject
962 * part of the DataCache class.
965 /************************************************************************
966 * DataCache_IDataObject_QueryInterface (IUnknown)
968 static HRESULT WINAPI DataCache_IDataObject_QueryInterface(
969 IDataObject* iface,
970 REFIID riid,
971 void** ppvObject)
973 DataCache *this = impl_from_IDataObject(iface);
975 return IUnknown_QueryInterface(this->outerUnknown, riid, ppvObject);
978 /************************************************************************
979 * DataCache_IDataObject_AddRef (IUnknown)
981 static ULONG WINAPI DataCache_IDataObject_AddRef(
982 IDataObject* iface)
984 DataCache *this = impl_from_IDataObject(iface);
986 return IUnknown_AddRef(this->outerUnknown);
989 /************************************************************************
990 * DataCache_IDataObject_Release (IUnknown)
992 static ULONG WINAPI DataCache_IDataObject_Release(
993 IDataObject* iface)
995 DataCache *this = impl_from_IDataObject(iface);
997 return IUnknown_Release(this->outerUnknown);
1000 /************************************************************************
1001 * DataCache_GetData
1003 * Get Data from a source dataobject using format pformatetcIn->cfFormat
1005 static HRESULT WINAPI DataCache_GetData(
1006 IDataObject* iface,
1007 LPFORMATETC pformatetcIn,
1008 STGMEDIUM* pmedium)
1010 DataCache *This = impl_from_IDataObject(iface);
1011 DataCacheEntry *cache_entry;
1013 memset(pmedium, 0, sizeof(*pmedium));
1015 cache_entry = DataCache_GetEntryForFormatEtc(This, pformatetcIn);
1016 if (!cache_entry)
1017 return OLE_E_BLANK;
1019 return DataCacheEntry_GetData(cache_entry, pmedium);
1022 static HRESULT WINAPI DataCache_GetDataHere(
1023 IDataObject* iface,
1024 LPFORMATETC pformatetc,
1025 STGMEDIUM* pmedium)
1027 FIXME("stub\n");
1028 return E_NOTIMPL;
1031 static HRESULT WINAPI DataCache_QueryGetData(
1032 IDataObject* iface,
1033 LPFORMATETC pformatetc)
1035 FIXME("stub\n");
1036 return E_NOTIMPL;
1039 /************************************************************************
1040 * DataCache_EnumFormatEtc (IDataObject)
1042 * The data cache doesn't implement this method.
1044 static HRESULT WINAPI DataCache_GetCanonicalFormatEtc(
1045 IDataObject* iface,
1046 LPFORMATETC pformatectIn,
1047 LPFORMATETC pformatetcOut)
1049 TRACE("()\n");
1050 return E_NOTIMPL;
1053 /************************************************************************
1054 * DataCache_IDataObject_SetData (IDataObject)
1056 * This method is delegated to the IOleCache2 implementation.
1058 static HRESULT WINAPI DataCache_IDataObject_SetData(
1059 IDataObject* iface,
1060 LPFORMATETC pformatetc,
1061 STGMEDIUM* pmedium,
1062 BOOL fRelease)
1064 IOleCache2* oleCache = NULL;
1065 HRESULT hres;
1067 TRACE("(%p, %p, %p, %d)\n", iface, pformatetc, pmedium, fRelease);
1069 hres = IDataObject_QueryInterface(iface, &IID_IOleCache2, (void**)&oleCache);
1071 if (FAILED(hres))
1072 return E_UNEXPECTED;
1074 hres = IOleCache2_SetData(oleCache, pformatetc, pmedium, fRelease);
1076 IOleCache2_Release(oleCache);
1078 return hres;
1081 /************************************************************************
1082 * DataCache_EnumFormatEtc (IDataObject)
1084 * The data cache doesn't implement this method.
1086 static HRESULT WINAPI DataCache_EnumFormatEtc(
1087 IDataObject* iface,
1088 DWORD dwDirection,
1089 IEnumFORMATETC** ppenumFormatEtc)
1091 TRACE("()\n");
1092 return E_NOTIMPL;
1095 /************************************************************************
1096 * DataCache_DAdvise (IDataObject)
1098 * The data cache doesn't support connections.
1100 static HRESULT WINAPI DataCache_DAdvise(
1101 IDataObject* iface,
1102 FORMATETC* pformatetc,
1103 DWORD advf,
1104 IAdviseSink* pAdvSink,
1105 DWORD* pdwConnection)
1107 TRACE("()\n");
1108 return OLE_E_ADVISENOTSUPPORTED;
1111 /************************************************************************
1112 * DataCache_DUnadvise (IDataObject)
1114 * The data cache doesn't support connections.
1116 static HRESULT WINAPI DataCache_DUnadvise(
1117 IDataObject* iface,
1118 DWORD dwConnection)
1120 TRACE("()\n");
1121 return OLE_E_NOCONNECTION;
1124 /************************************************************************
1125 * DataCache_EnumDAdvise (IDataObject)
1127 * The data cache doesn't support connections.
1129 static HRESULT WINAPI DataCache_EnumDAdvise(
1130 IDataObject* iface,
1131 IEnumSTATDATA** ppenumAdvise)
1133 TRACE("()\n");
1134 return OLE_E_ADVISENOTSUPPORTED;
1137 /*********************************************************
1138 * Method implementation for the IDataObject
1139 * part of the DataCache class.
1142 /************************************************************************
1143 * DataCache_IPersistStorage_QueryInterface (IUnknown)
1145 static HRESULT WINAPI DataCache_IPersistStorage_QueryInterface(
1146 IPersistStorage* iface,
1147 REFIID riid,
1148 void** ppvObject)
1150 DataCache *this = impl_from_IPersistStorage(iface);
1152 return IUnknown_QueryInterface(this->outerUnknown, riid, ppvObject);
1155 /************************************************************************
1156 * DataCache_IPersistStorage_AddRef (IUnknown)
1158 static ULONG WINAPI DataCache_IPersistStorage_AddRef(
1159 IPersistStorage* iface)
1161 DataCache *this = impl_from_IPersistStorage(iface);
1163 return IUnknown_AddRef(this->outerUnknown);
1166 /************************************************************************
1167 * DataCache_IPersistStorage_Release (IUnknown)
1169 static ULONG WINAPI DataCache_IPersistStorage_Release(
1170 IPersistStorage* iface)
1172 DataCache *this = impl_from_IPersistStorage(iface);
1174 return IUnknown_Release(this->outerUnknown);
1177 /************************************************************************
1178 * DataCache_GetClassID (IPersistStorage)
1180 * The data cache doesn't implement this method.
1182 static HRESULT WINAPI DataCache_GetClassID(
1183 IPersistStorage* iface,
1184 CLSID* pClassID)
1186 DataCache *This = impl_from_IPersistStorage(iface);
1187 DataCacheEntry *cache_entry;
1189 TRACE("(%p, %p)\n", iface, pClassID);
1191 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
1193 if (cache_entry->storage != NULL)
1195 STATSTG statstg;
1196 HRESULT hr = IStorage_Stat(cache_entry->storage, &statstg, STATFLAG_NONAME);
1197 if (SUCCEEDED(hr))
1199 *pClassID = statstg.clsid;
1200 return S_OK;
1205 *pClassID = CLSID_NULL;
1207 return S_OK;
1210 /************************************************************************
1211 * DataCache_IsDirty (IPersistStorage)
1213 static HRESULT WINAPI DataCache_IsDirty(
1214 IPersistStorage* iface)
1216 DataCache *This = impl_from_IPersistStorage(iface);
1217 DataCacheEntry *cache_entry;
1219 TRACE("(%p)\n", iface);
1221 if (This->dirty)
1222 return S_OK;
1224 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
1225 if (cache_entry->dirty)
1226 return S_OK;
1228 return S_FALSE;
1231 /************************************************************************
1232 * DataCache_InitNew (IPersistStorage)
1234 * The data cache implementation of IPersistStorage_InitNew simply stores
1235 * the storage pointer.
1237 static HRESULT WINAPI DataCache_InitNew(
1238 IPersistStorage* iface,
1239 IStorage* pStg)
1241 DataCache *This = impl_from_IPersistStorage(iface);
1243 TRACE("(%p, %p)\n", iface, pStg);
1245 if (This->presentationStorage != NULL)
1246 IStorage_Release(This->presentationStorage);
1248 This->presentationStorage = pStg;
1250 IStorage_AddRef(This->presentationStorage);
1251 This->dirty = TRUE;
1253 return S_OK;
1256 /************************************************************************
1257 * DataCache_Load (IPersistStorage)
1259 * The data cache implementation of IPersistStorage_Load doesn't
1260 * actually load anything. Instead, it holds on to the storage pointer
1261 * and it will load the presentation information when the
1262 * IDataObject_GetData or IViewObject2_Draw methods are called.
1264 static HRESULT WINAPI DataCache_Load(
1265 IPersistStorage* iface,
1266 IStorage* pStg)
1268 DataCache *This = impl_from_IPersistStorage(iface);
1269 STATSTG elem;
1270 IEnumSTATSTG *pEnum;
1271 HRESULT hr;
1273 TRACE("(%p, %p)\n", iface, pStg);
1275 if (This->presentationStorage != NULL)
1276 IStorage_Release(This->presentationStorage);
1278 This->presentationStorage = pStg;
1280 hr = IStorage_EnumElements(pStg, 0, NULL, 0, &pEnum);
1281 if (FAILED(hr)) return hr;
1283 while ((hr = IEnumSTATSTG_Next(pEnum, 1, &elem, NULL)) == S_OK)
1285 if (DataCache_IsPresentationStream(&elem))
1287 IStream *pStm;
1289 hr = IStorage_OpenStream(This->presentationStorage, elem.pwcsName,
1290 NULL, STGM_READ | STGM_SHARE_EXCLUSIVE, 0,
1291 &pStm);
1292 if (SUCCEEDED(hr))
1294 PresentationDataHeader header;
1295 ULONG actual_read;
1296 CLIPFORMAT clipformat;
1298 hr = read_clipformat(pStm, &clipformat);
1300 if (hr == S_OK)
1301 hr = IStream_Read(pStm, &header, sizeof(header),
1302 &actual_read);
1304 /* can't use SUCCEEDED(hr): S_FALSE counts as an error */
1305 if (hr == S_OK && actual_read == sizeof(header))
1307 DataCacheEntry *cache_entry;
1308 FORMATETC fmtetc;
1310 fmtetc.cfFormat = clipformat;
1311 fmtetc.ptd = NULL; /* FIXME */
1312 fmtetc.dwAspect = header.dvAspect;
1313 fmtetc.lindex = header.lindex;
1314 fmtetc.tymed = header.tymed;
1316 TRACE("loading entry with formatetc: %s\n", debugstr_formatetc(&fmtetc));
1318 cache_entry = DataCache_GetEntryForFormatEtc(This, &fmtetc);
1319 if (!cache_entry)
1320 hr = DataCache_CreateEntry(This, &fmtetc, &cache_entry);
1321 if (SUCCEEDED(hr))
1323 DataCacheEntry_DiscardData(cache_entry);
1324 if (cache_entry->storage) IStorage_Release(cache_entry->storage);
1325 cache_entry->storage = pStg;
1326 IStorage_AddRef(pStg);
1327 cache_entry->dirty = FALSE;
1331 IStream_Release(pStm);
1335 CoTaskMemFree(elem.pwcsName);
1338 This->dirty = FALSE;
1340 IEnumSTATSTG_Release(pEnum);
1342 IStorage_AddRef(This->presentationStorage);
1343 return S_OK;
1346 /************************************************************************
1347 * DataCache_Save (IPersistStorage)
1349 * Until we actually connect to a running object and retrieve new
1350 * information to it, we never have to save anything. However, it is
1351 * our responsibility to copy the information when saving to a new
1352 * storage.
1354 static HRESULT WINAPI DataCache_Save(
1355 IPersistStorage* iface,
1356 IStorage* pStg,
1357 BOOL fSameAsLoad)
1359 DataCache *This = impl_from_IPersistStorage(iface);
1360 DataCacheEntry *cache_entry;
1361 BOOL dirty = FALSE;
1362 HRESULT hr = S_OK;
1363 unsigned short stream_number = 0;
1365 TRACE("(%p, %p, %d)\n", iface, pStg, fSameAsLoad);
1367 dirty = This->dirty;
1368 if (!dirty)
1370 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
1372 dirty = cache_entry->dirty;
1373 if (dirty)
1374 break;
1378 /* this is a shortcut if nothing changed */
1379 if (!dirty && !fSameAsLoad && This->presentationStorage)
1381 return IStorage_CopyTo(This->presentationStorage, 0, NULL, NULL, pStg);
1384 /* assign stream numbers to the cache entries */
1385 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
1387 if (cache_entry->stream_number != stream_number)
1389 cache_entry->dirty = TRUE; /* needs to be written out again */
1390 cache_entry->stream_number = stream_number;
1392 stream_number++;
1395 /* write out the cache entries */
1396 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
1398 if (!fSameAsLoad || cache_entry->dirty)
1400 hr = DataCacheEntry_Save(cache_entry, pStg, fSameAsLoad);
1401 if (FAILED(hr))
1402 break;
1404 cache_entry->dirty = FALSE;
1408 This->dirty = FALSE;
1409 return hr;
1412 /************************************************************************
1413 * DataCache_SaveCompleted (IPersistStorage)
1415 * This method is called to tell the cache to release the storage
1416 * pointer it's currently holding.
1418 static HRESULT WINAPI DataCache_SaveCompleted(
1419 IPersistStorage* iface,
1420 IStorage* pStgNew)
1422 TRACE("(%p, %p)\n", iface, pStgNew);
1424 if (pStgNew)
1426 IPersistStorage_HandsOffStorage(iface);
1428 DataCache_Load(iface, pStgNew);
1431 return S_OK;
1434 /************************************************************************
1435 * DataCache_HandsOffStorage (IPersistStorage)
1437 * This method is called to tell the cache to release the storage
1438 * pointer it's currently holding.
1440 static HRESULT WINAPI DataCache_HandsOffStorage(
1441 IPersistStorage* iface)
1443 DataCache *this = impl_from_IPersistStorage(iface);
1444 DataCacheEntry *cache_entry;
1446 TRACE("(%p)\n", iface);
1448 if (this->presentationStorage != NULL)
1450 IStorage_Release(this->presentationStorage);
1451 this->presentationStorage = NULL;
1454 LIST_FOR_EACH_ENTRY(cache_entry, &this->cache_list, DataCacheEntry, entry)
1455 DataCacheEntry_HandsOffStorage(cache_entry);
1457 return S_OK;
1460 /*********************************************************
1461 * Method implementation for the IViewObject2
1462 * part of the DataCache class.
1465 /************************************************************************
1466 * DataCache_IViewObject2_QueryInterface (IUnknown)
1468 static HRESULT WINAPI DataCache_IViewObject2_QueryInterface(
1469 IViewObject2* iface,
1470 REFIID riid,
1471 void** ppvObject)
1473 DataCache *this = impl_from_IViewObject2(iface);
1475 return IUnknown_QueryInterface(this->outerUnknown, riid, ppvObject);
1478 /************************************************************************
1479 * DataCache_IViewObject2_AddRef (IUnknown)
1481 static ULONG WINAPI DataCache_IViewObject2_AddRef(
1482 IViewObject2* iface)
1484 DataCache *this = impl_from_IViewObject2(iface);
1486 return IUnknown_AddRef(this->outerUnknown);
1489 /************************************************************************
1490 * DataCache_IViewObject2_Release (IUnknown)
1492 static ULONG WINAPI DataCache_IViewObject2_Release(
1493 IViewObject2* iface)
1495 DataCache *this = impl_from_IViewObject2(iface);
1497 return IUnknown_Release(this->outerUnknown);
1500 /************************************************************************
1501 * DataCache_Draw (IViewObject2)
1503 * This method will draw the cached representation of the object
1504 * to the given device context.
1506 static HRESULT WINAPI DataCache_Draw(
1507 IViewObject2* iface,
1508 DWORD dwDrawAspect,
1509 LONG lindex,
1510 void* pvAspect,
1511 DVTARGETDEVICE* ptd,
1512 HDC hdcTargetDev,
1513 HDC hdcDraw,
1514 LPCRECTL lprcBounds,
1515 LPCRECTL lprcWBounds,
1516 BOOL (CALLBACK *pfnContinue)(ULONG_PTR dwContinue),
1517 ULONG_PTR dwContinue)
1519 DataCache *This = impl_from_IViewObject2(iface);
1520 HRESULT hres;
1521 DataCacheEntry *cache_entry;
1523 TRACE("(%p, %x, %d, %p, %p, %p, %p, %p, %p, %lx)\n",
1524 iface,
1525 dwDrawAspect,
1526 lindex,
1527 pvAspect,
1528 hdcTargetDev,
1529 hdcDraw,
1530 lprcBounds,
1531 lprcWBounds,
1532 pfnContinue,
1533 dwContinue);
1535 if (lprcBounds==NULL)
1536 return E_INVALIDARG;
1538 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
1540 /* FIXME: compare ptd too */
1541 if ((cache_entry->fmtetc.dwAspect != dwDrawAspect) ||
1542 (cache_entry->fmtetc.lindex != lindex))
1543 continue;
1545 /* if the data hasn't been loaded yet, do it now */
1546 if ((cache_entry->stgmedium.tymed == TYMED_NULL) && cache_entry->storage)
1548 hres = DataCacheEntry_LoadData(cache_entry);
1549 if (FAILED(hres))
1550 continue;
1553 /* no data */
1554 if (cache_entry->stgmedium.tymed == TYMED_NULL)
1555 continue;
1557 if (pfnContinue && !pfnContinue(dwContinue)) return E_ABORT;
1559 switch (cache_entry->data_cf)
1561 case CF_METAFILEPICT:
1564 * We have to be careful not to modify the state of the
1565 * DC.
1567 INT prevMapMode;
1568 SIZE oldWindowExt;
1569 SIZE oldViewportExt;
1570 POINT oldViewportOrg;
1571 METAFILEPICT *mfpict;
1573 if ((cache_entry->stgmedium.tymed != TYMED_MFPICT) ||
1574 !((mfpict = GlobalLock(cache_entry->stgmedium.u.hMetaFilePict))))
1575 continue;
1577 prevMapMode = SetMapMode(hdcDraw, mfpict->mm);
1579 SetWindowExtEx(hdcDraw,
1580 mfpict->xExt,
1581 mfpict->yExt,
1582 &oldWindowExt);
1584 SetViewportExtEx(hdcDraw,
1585 lprcBounds->right - lprcBounds->left,
1586 lprcBounds->bottom - lprcBounds->top,
1587 &oldViewportExt);
1589 SetViewportOrgEx(hdcDraw,
1590 lprcBounds->left,
1591 lprcBounds->top,
1592 &oldViewportOrg);
1594 PlayMetaFile(hdcDraw, mfpict->hMF);
1596 SetWindowExtEx(hdcDraw,
1597 oldWindowExt.cx,
1598 oldWindowExt.cy,
1599 NULL);
1601 SetViewportExtEx(hdcDraw,
1602 oldViewportExt.cx,
1603 oldViewportExt.cy,
1604 NULL);
1606 SetViewportOrgEx(hdcDraw,
1607 oldViewportOrg.x,
1608 oldViewportOrg.y,
1609 NULL);
1611 SetMapMode(hdcDraw, prevMapMode);
1613 GlobalUnlock(cache_entry->stgmedium.u.hMetaFilePict);
1615 return S_OK;
1620 WARN("no data could be found to be drawn\n");
1622 return OLE_E_BLANK;
1625 static HRESULT WINAPI DataCache_GetColorSet(
1626 IViewObject2* iface,
1627 DWORD dwDrawAspect,
1628 LONG lindex,
1629 void* pvAspect,
1630 DVTARGETDEVICE* ptd,
1631 HDC hicTargetDevice,
1632 LOGPALETTE** ppColorSet)
1634 FIXME("stub\n");
1635 return E_NOTIMPL;
1638 static HRESULT WINAPI DataCache_Freeze(
1639 IViewObject2* iface,
1640 DWORD dwDrawAspect,
1641 LONG lindex,
1642 void* pvAspect,
1643 DWORD* pdwFreeze)
1645 FIXME("stub\n");
1646 return E_NOTIMPL;
1649 static HRESULT WINAPI DataCache_Unfreeze(
1650 IViewObject2* iface,
1651 DWORD dwFreeze)
1653 FIXME("stub\n");
1654 return E_NOTIMPL;
1657 /************************************************************************
1658 * DataCache_SetAdvise (IViewObject2)
1660 * This sets-up an advisory sink with the data cache. When the object's
1661 * view changes, this sink is called.
1663 static HRESULT WINAPI DataCache_SetAdvise(
1664 IViewObject2* iface,
1665 DWORD aspects,
1666 DWORD advf,
1667 IAdviseSink* pAdvSink)
1669 DataCache *this = impl_from_IViewObject2(iface);
1671 TRACE("(%p, %x, %x, %p)\n", iface, aspects, advf, pAdvSink);
1674 * A call to this function removes the previous sink
1676 if (this->sinkInterface != NULL)
1678 IAdviseSink_Release(this->sinkInterface);
1679 this->sinkInterface = NULL;
1680 this->sinkAspects = 0;
1681 this->sinkAdviseFlag = 0;
1685 * Now, setup the new one.
1687 if (pAdvSink!=NULL)
1689 this->sinkInterface = pAdvSink;
1690 this->sinkAspects = aspects;
1691 this->sinkAdviseFlag = advf;
1693 IAdviseSink_AddRef(this->sinkInterface);
1697 * When the ADVF_PRIMEFIRST flag is set, we have to advise the
1698 * sink immediately.
1700 if (advf & ADVF_PRIMEFIRST)
1702 DataCache_FireOnViewChange(this, aspects, -1);
1705 return S_OK;
1708 /************************************************************************
1709 * DataCache_GetAdvise (IViewObject2)
1711 * This method queries the current state of the advise sink
1712 * installed on the data cache.
1714 static HRESULT WINAPI DataCache_GetAdvise(
1715 IViewObject2* iface,
1716 DWORD* pAspects,
1717 DWORD* pAdvf,
1718 IAdviseSink** ppAdvSink)
1720 DataCache *this = impl_from_IViewObject2(iface);
1722 TRACE("(%p, %p, %p, %p)\n", iface, pAspects, pAdvf, ppAdvSink);
1725 * Just copy all the requested values.
1727 if (pAspects!=NULL)
1728 *pAspects = this->sinkAspects;
1730 if (pAdvf!=NULL)
1731 *pAdvf = this->sinkAdviseFlag;
1733 if (ppAdvSink!=NULL)
1735 if (this->sinkInterface != NULL)
1736 IAdviseSink_QueryInterface(this->sinkInterface,
1737 &IID_IAdviseSink,
1738 (void**)ppAdvSink);
1739 else *ppAdvSink = NULL;
1742 return S_OK;
1745 /************************************************************************
1746 * DataCache_GetExtent (IViewObject2)
1748 * This method retrieves the "natural" size of this cached object.
1750 static HRESULT WINAPI DataCache_GetExtent(
1751 IViewObject2* iface,
1752 DWORD dwDrawAspect,
1753 LONG lindex,
1754 DVTARGETDEVICE* ptd,
1755 LPSIZEL lpsizel)
1757 DataCache *This = impl_from_IViewObject2(iface);
1758 HRESULT hres = E_FAIL;
1759 DataCacheEntry *cache_entry;
1761 TRACE("(%p, %x, %d, %p, %p)\n",
1762 iface, dwDrawAspect, lindex, ptd, lpsizel);
1764 if (lpsizel==NULL)
1765 return E_POINTER;
1767 lpsizel->cx = 0;
1768 lpsizel->cy = 0;
1770 if (lindex!=-1)
1771 FIXME("Unimplemented flag lindex = %d\n", lindex);
1774 * Right now, we support only the callback from
1775 * the default handler.
1777 if (ptd!=NULL)
1778 FIXME("Unimplemented ptd = %p\n", ptd);
1780 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
1782 /* FIXME: compare ptd too */
1783 if ((cache_entry->fmtetc.dwAspect != dwDrawAspect) ||
1784 (cache_entry->fmtetc.lindex != lindex))
1785 continue;
1787 /* if the data hasn't been loaded yet, do it now */
1788 if ((cache_entry->stgmedium.tymed == TYMED_NULL) && cache_entry->storage)
1790 hres = DataCacheEntry_LoadData(cache_entry);
1791 if (FAILED(hres))
1792 continue;
1795 /* no data */
1796 if (cache_entry->stgmedium.tymed == TYMED_NULL)
1797 continue;
1800 switch (cache_entry->data_cf)
1802 case CF_METAFILEPICT:
1804 METAFILEPICT *mfpict;
1806 if ((cache_entry->stgmedium.tymed != TYMED_MFPICT) ||
1807 !((mfpict = GlobalLock(cache_entry->stgmedium.u.hMetaFilePict))))
1808 continue;
1810 lpsizel->cx = mfpict->xExt;
1811 lpsizel->cy = mfpict->yExt;
1813 GlobalUnlock(cache_entry->stgmedium.u.hMetaFilePict);
1815 return S_OK;
1820 WARN("no data could be found to get the extents from\n");
1823 * This method returns OLE_E_BLANK when it fails.
1825 return OLE_E_BLANK;
1829 /*********************************************************
1830 * Method implementation for the IOleCache2
1831 * part of the DataCache class.
1834 /************************************************************************
1835 * DataCache_IOleCache2_QueryInterface (IUnknown)
1837 static HRESULT WINAPI DataCache_IOleCache2_QueryInterface(
1838 IOleCache2* iface,
1839 REFIID riid,
1840 void** ppvObject)
1842 DataCache *this = impl_from_IOleCache2(iface);
1844 return IUnknown_QueryInterface(this->outerUnknown, riid, ppvObject);
1847 /************************************************************************
1848 * DataCache_IOleCache2_AddRef (IUnknown)
1850 static ULONG WINAPI DataCache_IOleCache2_AddRef(
1851 IOleCache2* iface)
1853 DataCache *this = impl_from_IOleCache2(iface);
1855 return IUnknown_AddRef(this->outerUnknown);
1858 /************************************************************************
1859 * DataCache_IOleCache2_Release (IUnknown)
1861 static ULONG WINAPI DataCache_IOleCache2_Release(
1862 IOleCache2* iface)
1864 DataCache *this = impl_from_IOleCache2(iface);
1866 return IUnknown_Release(this->outerUnknown);
1869 /*****************************************************************************
1870 * setup_sink
1872 * Set up the sink connection to the running object.
1874 static HRESULT setup_sink(DataCache *This, DataCacheEntry *cache_entry)
1876 HRESULT hr = S_FALSE;
1877 DWORD flags;
1879 /* Clear the ADVFCACHE_* bits. Native also sets the two highest bits for some reason. */
1880 flags = cache_entry->advise_flags & ~(ADVFCACHE_NOHANDLER | ADVFCACHE_FORCEBUILTIN | ADVFCACHE_ONSAVE);
1882 if(This->running_object)
1883 if(!(flags & ADVF_NODATA))
1884 hr = IDataObject_DAdvise(This->running_object, &cache_entry->fmtetc, flags,
1885 &This->IAdviseSink_iface, &cache_entry->sink_id);
1886 return hr;
1889 static HRESULT WINAPI DataCache_Cache(
1890 IOleCache2* iface,
1891 FORMATETC* pformatetc,
1892 DWORD advf,
1893 DWORD* pdwConnection)
1895 DataCache *This = impl_from_IOleCache2(iface);
1896 DataCacheEntry *cache_entry;
1897 HRESULT hr;
1899 TRACE("(%p, 0x%x, %p)\n", pformatetc, advf, pdwConnection);
1901 if (!pformatetc || !pdwConnection)
1902 return E_INVALIDARG;
1904 TRACE("pformatetc = %s\n", debugstr_formatetc(pformatetc));
1906 *pdwConnection = 0;
1908 cache_entry = DataCache_GetEntryForFormatEtc(This, pformatetc);
1909 if (cache_entry)
1911 TRACE("found an existing cache entry\n");
1912 *pdwConnection = cache_entry->id;
1913 return CACHE_S_SAMECACHE;
1916 hr = DataCache_CreateEntry(This, pformatetc, &cache_entry);
1918 if (SUCCEEDED(hr))
1920 *pdwConnection = cache_entry->id;
1921 cache_entry->advise_flags = advf;
1922 setup_sink(This, cache_entry);
1925 return hr;
1928 static HRESULT WINAPI DataCache_Uncache(
1929 IOleCache2* iface,
1930 DWORD dwConnection)
1932 DataCache *This = impl_from_IOleCache2(iface);
1933 DataCacheEntry *cache_entry;
1935 TRACE("(%d)\n", dwConnection);
1937 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
1938 if (cache_entry->id == dwConnection)
1940 DataCacheEntry_Destroy(This, cache_entry);
1941 return S_OK;
1944 WARN("no connection found for %d\n", dwConnection);
1946 return OLE_E_NOCONNECTION;
1949 static HRESULT WINAPI DataCache_EnumCache(
1950 IOleCache2* iface,
1951 IEnumSTATDATA** ppenumSTATDATA)
1953 FIXME("stub\n");
1954 return E_NOTIMPL;
1957 static HRESULT WINAPI DataCache_InitCache(
1958 IOleCache2* iface,
1959 IDataObject* pDataObject)
1961 FIXME("stub\n");
1962 return E_NOTIMPL;
1965 static HRESULT WINAPI DataCache_IOleCache2_SetData(
1966 IOleCache2* iface,
1967 FORMATETC* pformatetc,
1968 STGMEDIUM* pmedium,
1969 BOOL fRelease)
1971 DataCache *This = impl_from_IOleCache2(iface);
1972 DataCacheEntry *cache_entry;
1973 HRESULT hr;
1975 TRACE("(%p, %p, %s)\n", pformatetc, pmedium, fRelease ? "TRUE" : "FALSE");
1976 TRACE("formatetc = %s\n", debugstr_formatetc(pformatetc));
1978 cache_entry = DataCache_GetEntryForFormatEtc(This, pformatetc);
1979 if (cache_entry)
1981 hr = DataCacheEntry_SetData(cache_entry, pformatetc, pmedium, fRelease);
1983 if (SUCCEEDED(hr))
1984 DataCache_FireOnViewChange(This, cache_entry->fmtetc.dwAspect,
1985 cache_entry->fmtetc.lindex);
1987 return hr;
1989 WARN("cache entry not found\n");
1991 return OLE_E_BLANK;
1994 static HRESULT WINAPI DataCache_UpdateCache(
1995 IOleCache2* iface,
1996 LPDATAOBJECT pDataObject,
1997 DWORD grfUpdf,
1998 LPVOID pReserved)
2000 FIXME("(%p, 0x%x, %p): stub\n", pDataObject, grfUpdf, pReserved);
2001 return E_NOTIMPL;
2004 static HRESULT WINAPI DataCache_DiscardCache(
2005 IOleCache2* iface,
2006 DWORD dwDiscardOptions)
2008 DataCache *This = impl_from_IOleCache2(iface);
2009 DataCacheEntry *cache_entry;
2010 HRESULT hr = S_OK;
2012 TRACE("(%d)\n", dwDiscardOptions);
2014 if (dwDiscardOptions == DISCARDCACHE_SAVEIFDIRTY)
2015 hr = DataCache_Save(&This->IPersistStorage_iface, This->presentationStorage, TRUE);
2017 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
2019 hr = DataCacheEntry_DiscardData(cache_entry);
2020 if (FAILED(hr))
2021 break;
2024 return hr;
2028 /*********************************************************
2029 * Method implementation for the IOleCacheControl
2030 * part of the DataCache class.
2033 /************************************************************************
2034 * DataCache_IOleCacheControl_QueryInterface (IUnknown)
2036 static HRESULT WINAPI DataCache_IOleCacheControl_QueryInterface(
2037 IOleCacheControl* iface,
2038 REFIID riid,
2039 void** ppvObject)
2041 DataCache *this = impl_from_IOleCacheControl(iface);
2043 return IUnknown_QueryInterface(this->outerUnknown, riid, ppvObject);
2046 /************************************************************************
2047 * DataCache_IOleCacheControl_AddRef (IUnknown)
2049 static ULONG WINAPI DataCache_IOleCacheControl_AddRef(
2050 IOleCacheControl* iface)
2052 DataCache *this = impl_from_IOleCacheControl(iface);
2054 return IUnknown_AddRef(this->outerUnknown);
2057 /************************************************************************
2058 * DataCache_IOleCacheControl_Release (IUnknown)
2060 static ULONG WINAPI DataCache_IOleCacheControl_Release(
2061 IOleCacheControl* iface)
2063 DataCache *this = impl_from_IOleCacheControl(iface);
2065 return IUnknown_Release(this->outerUnknown);
2068 /************************************************************************
2069 * DataCache_OnRun (IOleCacheControl)
2071 static HRESULT WINAPI DataCache_OnRun(IOleCacheControl* iface, IDataObject *data_obj)
2073 DataCache *This = impl_from_IOleCacheControl(iface);
2074 DataCacheEntry *cache_entry;
2076 TRACE("(%p)->(%p)\n", iface, data_obj);
2078 if(This->running_object) return S_OK;
2080 /* No reference is taken on the data object */
2081 This->running_object = data_obj;
2083 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
2085 setup_sink(This, cache_entry);
2088 return S_OK;
2091 /************************************************************************
2092 * DataCache_OnStop (IOleCacheControl)
2094 static HRESULT WINAPI DataCache_OnStop(IOleCacheControl* iface)
2096 DataCache *This = impl_from_IOleCacheControl(iface);
2097 DataCacheEntry *cache_entry;
2099 TRACE("(%p)\n", iface);
2101 if(!This->running_object) return S_OK;
2103 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
2105 if(cache_entry->sink_id)
2107 IDataObject_DUnadvise(This->running_object, cache_entry->sink_id);
2108 cache_entry->sink_id = 0;
2112 /* No ref taken in OnRun, so no Release call here */
2113 This->running_object = NULL;
2114 return S_OK;
2117 /************************************************************************
2118 * IAdviseSink methods.
2119 * This behaves as an internal object to the data cache. QI'ing its ptr doesn't
2120 * give access to the cache's other interfaces. We don't maintain a ref count,
2121 * the object exists as long as the cache is around.
2123 static HRESULT WINAPI DataCache_IAdviseSink_QueryInterface(IAdviseSink *iface, REFIID iid, void **obj)
2125 *obj = NULL;
2126 if (IsEqualIID(&IID_IUnknown, iid) ||
2127 IsEqualIID(&IID_IAdviseSink, iid))
2129 *obj = iface;
2132 if(*obj)
2134 IAdviseSink_AddRef(iface);
2135 return S_OK;
2137 return E_NOINTERFACE;
2140 static ULONG WINAPI DataCache_IAdviseSink_AddRef(IAdviseSink *iface)
2142 return 2;
2145 static ULONG WINAPI DataCache_IAdviseSink_Release(IAdviseSink *iface)
2147 return 1;
2150 static void WINAPI DataCache_OnDataChange(IAdviseSink *iface, FORMATETC *fmt, STGMEDIUM *med)
2152 DataCache *This = impl_from_IAdviseSink(iface);
2153 TRACE("(%p)->(%s, %p)\n", This, debugstr_formatetc(fmt), med);
2154 IOleCache2_SetData(&This->IOleCache2_iface, fmt, med, FALSE);
2157 static void WINAPI DataCache_OnViewChange(IAdviseSink *iface, DWORD aspect, LONG index)
2159 FIXME("stub\n");
2162 static void WINAPI DataCache_OnRename(IAdviseSink *iface, IMoniker *mk)
2164 FIXME("stub\n");
2167 static void WINAPI DataCache_OnSave(IAdviseSink *iface)
2169 FIXME("stub\n");
2172 static void WINAPI DataCache_OnClose(IAdviseSink *iface)
2174 FIXME("stub\n");
2178 * Virtual function tables for the DataCache class.
2180 static const IUnknownVtbl DataCache_NDIUnknown_VTable =
2182 DataCache_NDIUnknown_QueryInterface,
2183 DataCache_NDIUnknown_AddRef,
2184 DataCache_NDIUnknown_Release
2187 static const IDataObjectVtbl DataCache_IDataObject_VTable =
2189 DataCache_IDataObject_QueryInterface,
2190 DataCache_IDataObject_AddRef,
2191 DataCache_IDataObject_Release,
2192 DataCache_GetData,
2193 DataCache_GetDataHere,
2194 DataCache_QueryGetData,
2195 DataCache_GetCanonicalFormatEtc,
2196 DataCache_IDataObject_SetData,
2197 DataCache_EnumFormatEtc,
2198 DataCache_DAdvise,
2199 DataCache_DUnadvise,
2200 DataCache_EnumDAdvise
2203 static const IPersistStorageVtbl DataCache_IPersistStorage_VTable =
2205 DataCache_IPersistStorage_QueryInterface,
2206 DataCache_IPersistStorage_AddRef,
2207 DataCache_IPersistStorage_Release,
2208 DataCache_GetClassID,
2209 DataCache_IsDirty,
2210 DataCache_InitNew,
2211 DataCache_Load,
2212 DataCache_Save,
2213 DataCache_SaveCompleted,
2214 DataCache_HandsOffStorage
2217 static const IViewObject2Vtbl DataCache_IViewObject2_VTable =
2219 DataCache_IViewObject2_QueryInterface,
2220 DataCache_IViewObject2_AddRef,
2221 DataCache_IViewObject2_Release,
2222 DataCache_Draw,
2223 DataCache_GetColorSet,
2224 DataCache_Freeze,
2225 DataCache_Unfreeze,
2226 DataCache_SetAdvise,
2227 DataCache_GetAdvise,
2228 DataCache_GetExtent
2231 static const IOleCache2Vtbl DataCache_IOleCache2_VTable =
2233 DataCache_IOleCache2_QueryInterface,
2234 DataCache_IOleCache2_AddRef,
2235 DataCache_IOleCache2_Release,
2236 DataCache_Cache,
2237 DataCache_Uncache,
2238 DataCache_EnumCache,
2239 DataCache_InitCache,
2240 DataCache_IOleCache2_SetData,
2241 DataCache_UpdateCache,
2242 DataCache_DiscardCache
2245 static const IOleCacheControlVtbl DataCache_IOleCacheControl_VTable =
2247 DataCache_IOleCacheControl_QueryInterface,
2248 DataCache_IOleCacheControl_AddRef,
2249 DataCache_IOleCacheControl_Release,
2250 DataCache_OnRun,
2251 DataCache_OnStop
2254 static const IAdviseSinkVtbl DataCache_IAdviseSink_VTable =
2256 DataCache_IAdviseSink_QueryInterface,
2257 DataCache_IAdviseSink_AddRef,
2258 DataCache_IAdviseSink_Release,
2259 DataCache_OnDataChange,
2260 DataCache_OnViewChange,
2261 DataCache_OnRename,
2262 DataCache_OnSave,
2263 DataCache_OnClose
2266 /*********************************************************
2267 * Method implementation for DataCache class.
2269 static DataCache* DataCache_Construct(
2270 REFCLSID clsid,
2271 LPUNKNOWN pUnkOuter)
2273 DataCache* newObject = 0;
2276 * Allocate space for the object.
2278 newObject = HeapAlloc(GetProcessHeap(), 0, sizeof(DataCache));
2280 if (newObject==0)
2281 return newObject;
2284 * Initialize the virtual function table.
2286 newObject->IDataObject_iface.lpVtbl = &DataCache_IDataObject_VTable;
2287 newObject->IUnknown_iface.lpVtbl = &DataCache_NDIUnknown_VTable;
2288 newObject->IPersistStorage_iface.lpVtbl = &DataCache_IPersistStorage_VTable;
2289 newObject->IViewObject2_iface.lpVtbl = &DataCache_IViewObject2_VTable;
2290 newObject->IOleCache2_iface.lpVtbl = &DataCache_IOleCache2_VTable;
2291 newObject->IOleCacheControl_iface.lpVtbl = &DataCache_IOleCacheControl_VTable;
2292 newObject->IAdviseSink_iface.lpVtbl = &DataCache_IAdviseSink_VTable;
2295 * Start with one reference count. The caller of this function
2296 * must release the interface pointer when it is done.
2298 newObject->ref = 1;
2301 * Initialize the outer unknown
2302 * We don't keep a reference on the outer unknown since, the way
2303 * aggregation works, our lifetime is at least as large as its
2304 * lifetime.
2306 if (pUnkOuter==NULL)
2307 pUnkOuter = &newObject->IUnknown_iface;
2309 newObject->outerUnknown = pUnkOuter;
2312 * Initialize the other members of the structure.
2314 newObject->sinkAspects = 0;
2315 newObject->sinkAdviseFlag = 0;
2316 newObject->sinkInterface = 0;
2317 newObject->presentationStorage = NULL;
2318 list_init(&newObject->cache_list);
2319 newObject->last_cache_id = 1;
2320 newObject->dirty = FALSE;
2321 newObject->running_object = NULL;
2323 return newObject;
2326 /******************************************************************************
2327 * CreateDataCache [OLE32.@]
2329 * Creates a data cache to allow an object to render one or more of its views,
2330 * whether running or not.
2332 * PARAMS
2333 * pUnkOuter [I] Outer unknown for the object.
2334 * rclsid [I]
2335 * riid [I] IID of interface to return.
2336 * ppvObj [O] Address where the data cache object will be stored on return.
2338 * RETURNS
2339 * Success: S_OK.
2340 * Failure: HRESULT code.
2342 * NOTES
2343 * The following interfaces are supported by the returned data cache object:
2344 * IOleCache, IOleCache2, IOleCacheControl, IPersistStorage, IDataObject,
2345 * IViewObject and IViewObject2.
2347 HRESULT WINAPI CreateDataCache(
2348 LPUNKNOWN pUnkOuter,
2349 REFCLSID rclsid,
2350 REFIID riid,
2351 LPVOID* ppvObj)
2353 DataCache* newCache = NULL;
2354 HRESULT hr = S_OK;
2356 TRACE("(%s, %p, %s, %p)\n", debugstr_guid(rclsid), pUnkOuter, debugstr_guid(riid), ppvObj);
2359 * Sanity check
2361 if (ppvObj==0)
2362 return E_POINTER;
2364 *ppvObj = 0;
2367 * If this cache is constructed for aggregation, make sure
2368 * the caller is requesting the IUnknown interface.
2369 * This is necessary because it's the only time the non-delegating
2370 * IUnknown pointer can be returned to the outside.
2372 if ( pUnkOuter && !IsEqualIID(&IID_IUnknown, riid) )
2373 return CLASS_E_NOAGGREGATION;
2376 * Try to construct a new instance of the class.
2378 newCache = DataCache_Construct(rclsid,
2379 pUnkOuter);
2381 if (newCache == 0)
2382 return E_OUTOFMEMORY;
2385 * Make sure it supports the interface required by the caller.
2387 hr = IUnknown_QueryInterface(&newCache->IUnknown_iface, riid, ppvObj);
2390 * Release the reference obtained in the constructor. If
2391 * the QueryInterface was unsuccessful, it will free the class.
2393 IUnknown_Release(&newCache->IUnknown_iface);
2395 return hr;