Release 1.3.7.
[wine/gsoc-2012-control.git] / dlls / ole32 / defaulthandler.c
blob026df3a694e8ca6b307e96e3d0d37907900ed8f8
1 /*
2 * OLE 2 default object handler
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 default object handler supports a whole whack of
23 * interfaces including:
24 * IOleObject, IDataObject, IPersistStorage, IViewObject2,
25 * IRunnableObject, IOleCache2, IOleCacheControl and much more.
27 * All the implementation details are taken from: Inside OLE
28 * second edition by Kraig Brockschmidt,
30 * TODO
31 * - This implementation of the default handler does not launch the
32 * server in the DoVerb, Update, GetData, GetDataHere and Run
33 * methods. When it is fixed to do so, all the methods will have
34 * to be revisited to allow delegating to the running object
36 * - All methods in the class that use the class ID should be
37 * aware that it is possible for a class to be treated as
38 * another one and go into emulation mode. Nothing has been
39 * done in this area.
41 * - Some functions still return E_NOTIMPL they have to be
42 * implemented. Most of those are related to the running of the
43 * actual server.
45 * - All the methods related to notification and advise sinks are
46 * in place but no notifications are sent to the sinks yet.
48 #include <assert.h>
49 #include <stdarg.h>
50 #include <string.h>
52 #define COBJMACROS
54 #include "windef.h"
55 #include "winbase.h"
56 #include "winuser.h"
57 #include "winerror.h"
58 #include "ole2.h"
60 #include "compobj_private.h"
62 #include "wine/unicode.h"
63 #include "wine/debug.h"
65 WINE_DEFAULT_DEBUG_CHANNEL(ole);
67 enum storage_state
69 storage_state_uninitialised,
70 storage_state_initialised,
71 storage_state_loaded
74 enum object_state
76 object_state_not_running,
77 object_state_running
80 /****************************************************************************
81 * DefaultHandler
84 struct DefaultHandler
86 const IOleObjectVtbl* lpVtbl;
87 const IUnknownVtbl* lpvtblIUnknown;
88 const IDataObjectVtbl* lpvtblIDataObject;
89 const IRunnableObjectVtbl* lpvtblIRunnableObject;
90 const IAdviseSinkVtbl *lpvtblIAdviseSink;
91 const IPersistStorageVtbl *lpvtblIPersistStorage;
93 /* Reference count of this object */
94 LONG ref;
96 /* IUnknown implementation of the outer object. */
97 IUnknown* outerUnknown;
99 /* Class Id that this handler object represents. */
100 CLSID clsid;
102 /* IUnknown implementation of the datacache. */
103 IUnknown* dataCache;
104 /* IPersistStorage implementation of the datacache. */
105 IPersistStorage* dataCache_PersistStg;
107 /* Client site for the embedded object. */
108 IOleClientSite* clientSite;
111 * The IOleAdviseHolder maintains the connections
112 * on behalf of the default handler.
114 IOleAdviseHolder* oleAdviseHolder;
117 * The IDataAdviseHolder maintains the data
118 * connections on behalf of the default handler.
120 IDataAdviseHolder* dataAdviseHolder;
122 /* Name of the container and object contained */
123 LPWSTR containerApp;
124 LPWSTR containerObj;
126 /* IOleObject delegate */
127 IOleObject *pOleDelegate;
128 /* IPersistStorage delegate */
129 IPersistStorage *pPSDelegate;
130 /* IDataObject delegate */
131 IDataObject *pDataDelegate;
132 enum object_state object_state;
134 /* connection cookie for the advise on the delegate OLE object */
135 DWORD dwAdvConn;
137 /* storage passed to Load or InitNew */
138 IStorage *storage;
139 enum storage_state storage_state;
141 /* optional class factory for object */
142 IClassFactory *pCFObject;
143 /* TRUE if acting as an inproc server instead of an inproc handler */
144 BOOL inproc_server;
147 typedef struct DefaultHandler DefaultHandler;
149 static inline DefaultHandler *impl_from_IOleObject( IOleObject *iface )
151 return (DefaultHandler *)((char*)iface - FIELD_OFFSET(DefaultHandler, lpVtbl));
154 static inline DefaultHandler *impl_from_NDIUnknown( IUnknown *iface )
156 return (DefaultHandler *)((char*)iface - FIELD_OFFSET(DefaultHandler, lpvtblIUnknown));
159 static inline DefaultHandler *impl_from_IDataObject( IDataObject *iface )
161 return (DefaultHandler *)((char*)iface - FIELD_OFFSET(DefaultHandler, lpvtblIDataObject));
164 static inline DefaultHandler *impl_from_IRunnableObject( IRunnableObject *iface )
166 return (DefaultHandler *)((char*)iface - FIELD_OFFSET(DefaultHandler, lpvtblIRunnableObject));
169 static inline DefaultHandler *impl_from_IAdviseSink( IAdviseSink *iface )
171 return (DefaultHandler *)((char*)iface - FIELD_OFFSET(DefaultHandler, lpvtblIAdviseSink));
174 static inline DefaultHandler *impl_from_IPersistStorage( IPersistStorage *iface )
176 return (DefaultHandler *)((char*)iface - FIELD_OFFSET(DefaultHandler, lpvtblIPersistStorage));
179 static void DefaultHandler_Destroy(DefaultHandler* This);
181 static inline BOOL object_is_running(DefaultHandler *This)
183 return IRunnableObject_IsRunning((IRunnableObject*)&This->lpvtblIRunnableObject);
186 /*********************************************************
187 * Method implementation for the non delegating IUnknown
188 * part of the DefaultHandler class.
191 /************************************************************************
192 * DefaultHandler_NDIUnknown_QueryInterface (IUnknown)
194 * See Windows documentation for more details on IUnknown methods.
196 * This version of QueryInterface will not delegate its implementation
197 * to the outer unknown.
199 static HRESULT WINAPI DefaultHandler_NDIUnknown_QueryInterface(
200 IUnknown* iface,
201 REFIID riid,
202 void** ppvObject)
204 DefaultHandler *This = impl_from_NDIUnknown(iface);
206 if (!ppvObject)
207 return E_INVALIDARG;
209 *ppvObject = NULL;
211 if (IsEqualIID(&IID_IUnknown, riid))
212 *ppvObject = iface;
213 else if (IsEqualIID(&IID_IOleObject, riid))
214 *ppvObject = &This->lpVtbl;
215 else if (IsEqualIID(&IID_IDataObject, riid))
216 *ppvObject = &This->lpvtblIDataObject;
217 else if (IsEqualIID(&IID_IRunnableObject, riid))
218 *ppvObject = &This->lpvtblIRunnableObject;
219 else if (IsEqualIID(&IID_IPersist, riid) ||
220 IsEqualIID(&IID_IPersistStorage, riid))
221 *ppvObject = &This->lpvtblIPersistStorage;
222 else if (IsEqualIID(&IID_IViewObject, riid) ||
223 IsEqualIID(&IID_IViewObject2, riid) ||
224 IsEqualIID(&IID_IOleCache, riid) ||
225 IsEqualIID(&IID_IOleCache2, riid))
227 HRESULT hr = IUnknown_QueryInterface(This->dataCache, riid, ppvObject);
228 if (FAILED(hr)) FIXME("interface %s not implemented by data cache\n", debugstr_guid(riid));
229 return hr;
231 else if (This->inproc_server && This->pOleDelegate)
233 return IUnknown_QueryInterface(This->pOleDelegate, riid, ppvObject);
236 /* Check that we obtained an interface. */
237 if (*ppvObject == NULL)
239 WARN( "() : asking for un supported interface %s\n", debugstr_guid(riid));
240 return E_NOINTERFACE;
244 * Query Interface always increases the reference count by one when it is
245 * successful.
247 IUnknown_AddRef((IUnknown*)*ppvObject);
249 return S_OK;
252 /************************************************************************
253 * DefaultHandler_NDIUnknown_AddRef (IUnknown)
255 * See Windows documentation for more details on IUnknown methods.
257 * This version of QueryInterface will not delegate its implementation
258 * to the outer unknown.
260 static ULONG WINAPI DefaultHandler_NDIUnknown_AddRef(
261 IUnknown* iface)
263 DefaultHandler *This = impl_from_NDIUnknown(iface);
264 return InterlockedIncrement(&This->ref);
267 /************************************************************************
268 * DefaultHandler_NDIUnknown_Release (IUnknown)
270 * See Windows documentation for more details on IUnknown methods.
272 * This version of QueryInterface will not delegate its implementation
273 * to the outer unknown.
275 static ULONG WINAPI DefaultHandler_NDIUnknown_Release(
276 IUnknown* iface)
278 DefaultHandler *This = impl_from_NDIUnknown(iface);
279 ULONG ref;
281 ref = InterlockedDecrement(&This->ref);
283 if (!ref) DefaultHandler_Destroy(This);
285 return ref;
288 /*********************************************************
289 * Methods implementation for the IOleObject part of
290 * the DefaultHandler class.
293 /************************************************************************
294 * DefaultHandler_QueryInterface (IUnknown)
296 * See Windows documentation for more details on IUnknown methods.
298 static HRESULT WINAPI DefaultHandler_QueryInterface(
299 IOleObject* iface,
300 REFIID riid,
301 void** ppvObject)
303 DefaultHandler *This = impl_from_IOleObject(iface);
305 return IUnknown_QueryInterface(This->outerUnknown, riid, ppvObject);
308 /************************************************************************
309 * DefaultHandler_AddRef (IUnknown)
311 * See Windows documentation for more details on IUnknown methods.
313 static ULONG WINAPI DefaultHandler_AddRef(
314 IOleObject* iface)
316 DefaultHandler *This = impl_from_IOleObject(iface);
318 return IUnknown_AddRef(This->outerUnknown);
321 /************************************************************************
322 * DefaultHandler_Release (IUnknown)
324 * See Windows documentation for more details on IUnknown methods.
326 static ULONG WINAPI DefaultHandler_Release(
327 IOleObject* iface)
329 DefaultHandler *This = impl_from_IOleObject(iface);
331 return IUnknown_Release(This->outerUnknown);
334 /************************************************************************
335 * DefaultHandler_SetClientSite (IOleObject)
337 * The default handler's implementation of this method only keeps the
338 * client site pointer for future reference.
340 * See Windows documentation for more details on IOleObject methods.
342 static HRESULT WINAPI DefaultHandler_SetClientSite(
343 IOleObject* iface,
344 IOleClientSite* pClientSite)
346 DefaultHandler *This = impl_from_IOleObject(iface);
347 HRESULT hr = S_OK;
349 TRACE("(%p, %p)\n", iface, pClientSite);
351 if (object_is_running(This))
352 hr = IOleObject_SetClientSite(This->pOleDelegate, pClientSite);
355 * Make sure we release the previous client site if there
356 * was one.
358 if (This->clientSite)
359 IOleClientSite_Release(This->clientSite);
361 This->clientSite = pClientSite;
363 if (This->clientSite)
364 IOleClientSite_AddRef(This->clientSite);
366 return hr;
369 /************************************************************************
370 * DefaultHandler_GetClientSite (IOleObject)
372 * The default handler's implementation of this method returns the
373 * last pointer set in IOleObject_SetClientSite.
375 * See Windows documentation for more details on IOleObject methods.
377 static HRESULT WINAPI DefaultHandler_GetClientSite(
378 IOleObject* iface,
379 IOleClientSite** ppClientSite)
381 DefaultHandler *This = impl_from_IOleObject(iface);
383 if (!ppClientSite)
384 return E_POINTER;
386 *ppClientSite = This->clientSite;
388 if (This->clientSite)
389 IOleClientSite_AddRef(This->clientSite);
391 return S_OK;
394 /************************************************************************
395 * DefaultHandler_SetHostNames (IOleObject)
397 * The default handler's implementation of this method just stores
398 * the strings and returns S_OK.
400 * See Windows documentation for more details on IOleObject methods.
402 static HRESULT WINAPI DefaultHandler_SetHostNames(
403 IOleObject* iface,
404 LPCOLESTR szContainerApp,
405 LPCOLESTR szContainerObj)
407 DefaultHandler *This = impl_from_IOleObject(iface);
409 TRACE("(%p, %s, %s)\n",
410 iface,
411 debugstr_w(szContainerApp),
412 debugstr_w(szContainerObj));
414 if (object_is_running(This))
415 IOleObject_SetHostNames(This->pOleDelegate, szContainerApp, szContainerObj);
417 /* Be sure to cleanup before re-assigning the strings. */
418 HeapFree( GetProcessHeap(), 0, This->containerApp );
419 This->containerApp = NULL;
420 HeapFree( GetProcessHeap(), 0, This->containerObj );
421 This->containerObj = NULL;
423 if (szContainerApp)
425 if ((This->containerApp = HeapAlloc( GetProcessHeap(), 0,
426 (lstrlenW(szContainerApp) + 1) * sizeof(WCHAR) )))
427 strcpyW( This->containerApp, szContainerApp );
430 if (szContainerObj)
432 if ((This->containerObj = HeapAlloc( GetProcessHeap(), 0,
433 (lstrlenW(szContainerObj) + 1) * sizeof(WCHAR) )))
434 strcpyW( This->containerObj, szContainerObj );
436 return S_OK;
439 static void release_delegates(DefaultHandler *This)
441 if (This->pDataDelegate)
443 IDataObject_Release(This->pDataDelegate);
444 This->pDataDelegate = NULL;
446 if (This->pPSDelegate)
448 IPersistStorage_Release(This->pPSDelegate);
449 This->pPSDelegate = NULL;
451 if (This->pOleDelegate)
453 IOleObject_Release(This->pOleDelegate);
454 This->pOleDelegate = NULL;
458 /* undoes the work done by DefaultHandler_Run */
459 static void DefaultHandler_Stop(DefaultHandler *This)
461 if (!object_is_running(This))
462 return;
464 IOleObject_Unadvise(This->pOleDelegate, This->dwAdvConn);
466 /* FIXME: call IOleCache_OnStop */
468 if (This->dataAdviseHolder)
469 DataAdviseHolder_OnDisconnect(This->dataAdviseHolder);
471 This->object_state = object_state_not_running;
474 /************************************************************************
475 * DefaultHandler_Close (IOleObject)
477 * The default handler's implementation of this method is meaningless
478 * without a running server so it does nothing.
480 * See Windows documentation for more details on IOleObject methods.
482 static HRESULT WINAPI DefaultHandler_Close(
483 IOleObject* iface,
484 DWORD dwSaveOption)
486 DefaultHandler *This = impl_from_IOleObject(iface);
487 HRESULT hr;
489 TRACE("(%d)\n", dwSaveOption);
491 if (!object_is_running(This))
492 return S_OK;
494 hr = IOleObject_Close(This->pOleDelegate, dwSaveOption);
496 DefaultHandler_Stop(This);
497 release_delegates(This);
499 return hr;
502 /************************************************************************
503 * DefaultHandler_SetMoniker (IOleObject)
505 * The default handler's implementation of this method does nothing.
507 * See Windows documentation for more details on IOleObject methods.
509 static HRESULT WINAPI DefaultHandler_SetMoniker(
510 IOleObject* iface,
511 DWORD dwWhichMoniker,
512 IMoniker* pmk)
514 DefaultHandler *This = impl_from_IOleObject(iface);
516 TRACE("(%p, %d, %p)\n",
517 iface,
518 dwWhichMoniker,
519 pmk);
521 if (object_is_running(This))
522 return IOleObject_SetMoniker(This->pOleDelegate, dwWhichMoniker, pmk);
524 return S_OK;
527 /************************************************************************
528 * DefaultHandler_GetMoniker (IOleObject)
530 * Delegate this request to the client site if we have one.
532 * See Windows documentation for more details on IOleObject methods.
534 static HRESULT WINAPI DefaultHandler_GetMoniker(
535 IOleObject* iface,
536 DWORD dwAssign,
537 DWORD dwWhichMoniker,
538 IMoniker** ppmk)
540 DefaultHandler *This = impl_from_IOleObject(iface);
542 TRACE("(%p, %d, %d, %p)\n",
543 iface, dwAssign, dwWhichMoniker, ppmk);
545 if (object_is_running(This))
546 return IOleObject_GetMoniker(This->pOleDelegate, dwAssign, dwWhichMoniker,
547 ppmk);
549 /* FIXME: dwWhichMoniker == OLEWHICHMK_CONTAINER only? */
550 if (This->clientSite)
552 return IOleClientSite_GetMoniker(This->clientSite,
553 dwAssign,
554 dwWhichMoniker,
555 ppmk);
559 return E_FAIL;
562 /************************************************************************
563 * DefaultHandler_InitFromData (IOleObject)
565 * This method is meaningless if the server is not running
567 * See Windows documentation for more details on IOleObject methods.
569 static HRESULT WINAPI DefaultHandler_InitFromData(
570 IOleObject* iface,
571 IDataObject* pDataObject,
572 BOOL fCreation,
573 DWORD dwReserved)
575 DefaultHandler *This = impl_from_IOleObject(iface);
577 TRACE("(%p, %p, %d, %d)\n",
578 iface, pDataObject, fCreation, dwReserved);
580 if (object_is_running(This))
581 return IOleObject_InitFromData(This->pOleDelegate, pDataObject, fCreation,
582 dwReserved);
583 return OLE_E_NOTRUNNING;
586 /************************************************************************
587 * DefaultHandler_GetClipboardData (IOleObject)
589 * This method is meaningless if the server is not running
591 * See Windows documentation for more details on IOleObject methods.
593 static HRESULT WINAPI DefaultHandler_GetClipboardData(
594 IOleObject* iface,
595 DWORD dwReserved,
596 IDataObject** ppDataObject)
598 DefaultHandler *This = impl_from_IOleObject(iface);
600 TRACE("(%p, %d, %p)\n",
601 iface, dwReserved, ppDataObject);
603 if (object_is_running(This))
604 return IOleObject_GetClipboardData(This->pOleDelegate, dwReserved,
605 ppDataObject);
607 return OLE_E_NOTRUNNING;
610 static HRESULT WINAPI DefaultHandler_DoVerb(
611 IOleObject* iface,
612 LONG iVerb,
613 struct tagMSG* lpmsg,
614 IOleClientSite* pActiveSite,
615 LONG lindex,
616 HWND hwndParent,
617 LPCRECT lprcPosRect)
619 DefaultHandler *This = impl_from_IOleObject(iface);
620 IRunnableObject *pRunnableObj = (IRunnableObject *)&This->lpvtblIRunnableObject;
621 HRESULT hr;
623 TRACE("(%d, %p, %p, %d, %p, %s)\n", iVerb, lpmsg, pActiveSite, lindex, hwndParent, wine_dbgstr_rect(lprcPosRect));
625 hr = IRunnableObject_Run(pRunnableObj, NULL);
626 if (FAILED(hr)) return hr;
628 return IOleObject_DoVerb(This->pOleDelegate, iVerb, lpmsg, pActiveSite,
629 lindex, hwndParent, lprcPosRect);
632 /************************************************************************
633 * DefaultHandler_EnumVerbs (IOleObject)
635 * The default handler implementation of this method simply delegates
636 * to OleRegEnumVerbs
638 * See Windows documentation for more details on IOleObject methods.
640 static HRESULT WINAPI DefaultHandler_EnumVerbs(
641 IOleObject* iface,
642 IEnumOLEVERB** ppEnumOleVerb)
644 DefaultHandler *This = impl_from_IOleObject(iface);
645 HRESULT hr = OLE_S_USEREG;
647 TRACE("(%p, %p)\n", iface, ppEnumOleVerb);
649 if (object_is_running(This))
650 hr = IOleObject_EnumVerbs(This->pOleDelegate, ppEnumOleVerb);
652 if (hr == OLE_S_USEREG)
653 return OleRegEnumVerbs(&This->clsid, ppEnumOleVerb);
654 else
655 return hr;
658 static HRESULT WINAPI DefaultHandler_Update(
659 IOleObject* iface)
661 DefaultHandler *This = impl_from_IOleObject(iface);
662 TRACE("(%p)\n", iface);
664 if (!object_is_running(This))
666 FIXME("Should run object\n");
667 return E_NOTIMPL;
669 return IOleObject_Update(This->pOleDelegate);
672 /************************************************************************
673 * DefaultHandler_IsUpToDate (IOleObject)
675 * This method is meaningless if the server is not running
677 * See Windows documentation for more details on IOleObject methods.
679 static HRESULT WINAPI DefaultHandler_IsUpToDate(
680 IOleObject* iface)
682 DefaultHandler *This = impl_from_IOleObject(iface);
683 TRACE("(%p)\n", iface);
685 if (object_is_running(This))
686 return IOleObject_IsUpToDate(This->pOleDelegate);
688 return OLE_E_NOTRUNNING;
691 /************************************************************************
692 * DefaultHandler_GetUserClassID (IOleObject)
694 * TODO: Map to a new class ID if emulation is active.
696 * See Windows documentation for more details on IOleObject methods.
698 static HRESULT WINAPI DefaultHandler_GetUserClassID(
699 IOleObject* iface,
700 CLSID* pClsid)
702 DefaultHandler *This = impl_from_IOleObject(iface);
704 TRACE("(%p, %p)\n", iface, pClsid);
706 if (object_is_running(This))
707 return IOleObject_GetUserClassID(This->pOleDelegate, pClsid);
709 if (!pClsid)
710 return E_POINTER;
712 *pClsid = This->clsid;
714 return S_OK;
717 /************************************************************************
718 * DefaultHandler_GetUserType (IOleObject)
720 * The default handler implementation of this method simply delegates
721 * to OleRegGetUserType
723 * See Windows documentation for more details on IOleObject methods.
725 static HRESULT WINAPI DefaultHandler_GetUserType(
726 IOleObject* iface,
727 DWORD dwFormOfType,
728 LPOLESTR* pszUserType)
730 DefaultHandler *This = impl_from_IOleObject(iface);
732 TRACE("(%p, %d, %p)\n", iface, dwFormOfType, pszUserType);
733 if (object_is_running(This))
734 return IOleObject_GetUserType(This->pOleDelegate, dwFormOfType, pszUserType);
736 return OleRegGetUserType(&This->clsid, dwFormOfType, pszUserType);
739 /************************************************************************
740 * DefaultHandler_SetExtent (IOleObject)
742 * This method is meaningless if the server is not running
744 * See Windows documentation for more details on IOleObject methods.
746 static HRESULT WINAPI DefaultHandler_SetExtent(
747 IOleObject* iface,
748 DWORD dwDrawAspect,
749 SIZEL* psizel)
751 DefaultHandler *This = impl_from_IOleObject(iface);
753 TRACE("(%p, %x, (%d x %d))\n", iface,
754 dwDrawAspect, psizel->cx, psizel->cy);
756 if (object_is_running(This))
757 return IOleObject_SetExtent(This->pOleDelegate, dwDrawAspect, psizel);
759 return OLE_E_NOTRUNNING;
762 /************************************************************************
763 * DefaultHandler_GetExtent (IOleObject)
765 * The default handler's implementation of this method returns uses
766 * the cache to locate the aspect and extract the extent from it.
768 * See Windows documentation for more details on IOleObject methods.
770 static HRESULT WINAPI DefaultHandler_GetExtent(
771 IOleObject* iface,
772 DWORD dwDrawAspect,
773 SIZEL* psizel)
775 DVTARGETDEVICE* targetDevice;
776 IViewObject2* cacheView = NULL;
777 HRESULT hres;
779 DefaultHandler *This = impl_from_IOleObject(iface);
781 TRACE("(%p, %x, %p)\n", iface, dwDrawAspect, psizel);
783 if (object_is_running(This))
784 return IOleObject_GetExtent(This->pOleDelegate, dwDrawAspect, psizel);
786 hres = IUnknown_QueryInterface(This->dataCache, &IID_IViewObject2, (void**)&cacheView);
787 if (FAILED(hres))
788 return E_UNEXPECTED;
791 * Prepare the call to the cache's GetExtent method.
793 * Here we would build a valid DVTARGETDEVICE structure
794 * but, since we are calling into the data cache, we
795 * know its implementation and we'll skip this
796 * extra work until later.
798 targetDevice = NULL;
800 hres = IViewObject2_GetExtent(cacheView,
801 dwDrawAspect,
803 targetDevice,
804 psizel);
806 IViewObject2_Release(cacheView);
808 return hres;
811 /************************************************************************
812 * DefaultHandler_Advise (IOleObject)
814 * The default handler's implementation of this method simply
815 * delegates to the OleAdviseHolder.
817 * See Windows documentation for more details on IOleObject methods.
819 static HRESULT WINAPI DefaultHandler_Advise(
820 IOleObject* iface,
821 IAdviseSink* pAdvSink,
822 DWORD* pdwConnection)
824 HRESULT hres = S_OK;
825 DefaultHandler *This = impl_from_IOleObject(iface);
827 TRACE("(%p, %p, %p)\n", iface, pAdvSink, pdwConnection);
829 /* Make sure we have an advise holder before we start. */
830 if (!This->oleAdviseHolder)
831 hres = CreateOleAdviseHolder(&This->oleAdviseHolder);
833 if (SUCCEEDED(hres))
834 hres = IOleAdviseHolder_Advise(This->oleAdviseHolder,
835 pAdvSink,
836 pdwConnection);
838 return hres;
841 /************************************************************************
842 * DefaultHandler_Unadvise (IOleObject)
844 * The default handler's implementation of this method simply
845 * delegates to the OleAdviseHolder.
847 * See Windows documentation for more details on IOleObject methods.
849 static HRESULT WINAPI DefaultHandler_Unadvise(
850 IOleObject* iface,
851 DWORD dwConnection)
853 DefaultHandler *This = impl_from_IOleObject(iface);
855 TRACE("(%p, %d)\n", iface, dwConnection);
858 * If we don't have an advise holder yet, it means we don't have
859 * a connection.
861 if (!This->oleAdviseHolder)
862 return OLE_E_NOCONNECTION;
864 return IOleAdviseHolder_Unadvise(This->oleAdviseHolder,
865 dwConnection);
868 /************************************************************************
869 * DefaultHandler_EnumAdvise (IOleObject)
871 * The default handler's implementation of this method simply
872 * delegates to the OleAdviseHolder.
874 * See Windows documentation for more details on IOleObject methods.
876 static HRESULT WINAPI DefaultHandler_EnumAdvise(
877 IOleObject* iface,
878 IEnumSTATDATA** ppenumAdvise)
880 DefaultHandler *This = impl_from_IOleObject(iface);
882 TRACE("(%p, %p)\n", iface, ppenumAdvise);
884 if (!ppenumAdvise)
885 return E_POINTER;
887 *ppenumAdvise = NULL;
889 if (!This->oleAdviseHolder)
890 return S_OK;
892 return IOleAdviseHolder_EnumAdvise(This->oleAdviseHolder, ppenumAdvise);
895 /************************************************************************
896 * DefaultHandler_GetMiscStatus (IOleObject)
898 * The default handler's implementation of this method simply delegates
899 * to OleRegGetMiscStatus.
901 * See Windows documentation for more details on IOleObject methods.
903 static HRESULT WINAPI DefaultHandler_GetMiscStatus(
904 IOleObject* iface,
905 DWORD dwAspect,
906 DWORD* pdwStatus)
908 HRESULT hres;
909 DefaultHandler *This = impl_from_IOleObject(iface);
911 TRACE("(%p, %x, %p)\n", iface, dwAspect, pdwStatus);
913 if (object_is_running(This))
914 return IOleObject_GetMiscStatus(This->pOleDelegate, dwAspect, pdwStatus);
916 hres = OleRegGetMiscStatus(&This->clsid, dwAspect, pdwStatus);
918 if (FAILED(hres))
919 *pdwStatus = 0;
921 return hres;
924 /************************************************************************
925 * DefaultHandler_SetColorScheme (IOleObject)
927 * This method is meaningless if the server is not running
929 * See Windows documentation for more details on IOleObject methods.
931 static HRESULT WINAPI DefaultHandler_SetColorScheme(
932 IOleObject* iface,
933 struct tagLOGPALETTE* pLogpal)
935 DefaultHandler *This = impl_from_IOleObject(iface);
937 TRACE("(%p, %p))\n", iface, pLogpal);
939 if (object_is_running(This))
940 return IOleObject_SetColorScheme(This->pOleDelegate, pLogpal);
942 return OLE_E_NOTRUNNING;
945 /*********************************************************
946 * Methods implementation for the IDataObject part of
947 * the DefaultHandler class.
950 /************************************************************************
951 * DefaultHandler_IDataObject_QueryInterface (IUnknown)
953 * See Windows documentation for more details on IUnknown methods.
955 static HRESULT WINAPI DefaultHandler_IDataObject_QueryInterface(
956 IDataObject* iface,
957 REFIID riid,
958 void** ppvObject)
960 DefaultHandler *This = impl_from_IDataObject(iface);
962 return IUnknown_QueryInterface(This->outerUnknown, riid, ppvObject);
965 /************************************************************************
966 * DefaultHandler_IDataObject_AddRef (IUnknown)
968 * See Windows documentation for more details on IUnknown methods.
970 static ULONG WINAPI DefaultHandler_IDataObject_AddRef(
971 IDataObject* iface)
973 DefaultHandler *This = impl_from_IDataObject(iface);
975 return IUnknown_AddRef(This->outerUnknown);
978 /************************************************************************
979 * DefaultHandler_IDataObject_Release (IUnknown)
981 * See Windows documentation for more details on IUnknown methods.
983 static ULONG WINAPI DefaultHandler_IDataObject_Release(
984 IDataObject* iface)
986 DefaultHandler *This = impl_from_IDataObject(iface);
988 return IUnknown_Release(This->outerUnknown);
991 /************************************************************************
992 * DefaultHandler_GetData
994 * Get Data from a source dataobject using format pformatetcIn->cfFormat
995 * See Windows documentation for more details on GetData.
996 * Default handler's implementation of this method delegates to the cache.
998 static HRESULT WINAPI DefaultHandler_GetData(
999 IDataObject* iface,
1000 LPFORMATETC pformatetcIn,
1001 STGMEDIUM* pmedium)
1003 IDataObject* cacheDataObject = NULL;
1004 HRESULT hres;
1006 DefaultHandler *This = impl_from_IDataObject(iface);
1008 TRACE("(%p, %p, %p)\n", iface, pformatetcIn, pmedium);
1010 hres = IUnknown_QueryInterface(This->dataCache,
1011 &IID_IDataObject,
1012 (void**)&cacheDataObject);
1014 if (FAILED(hres))
1015 return E_UNEXPECTED;
1017 hres = IDataObject_GetData(cacheDataObject,
1018 pformatetcIn,
1019 pmedium);
1021 IDataObject_Release(cacheDataObject);
1023 if (FAILED(hres) && This->pDataDelegate)
1024 hres = IDataObject_GetData(This->pDataDelegate, pformatetcIn, pmedium);
1026 return hres;
1029 static HRESULT WINAPI DefaultHandler_GetDataHere(
1030 IDataObject* iface,
1031 LPFORMATETC pformatetc,
1032 STGMEDIUM* pmedium)
1034 FIXME(": Stub\n");
1035 return E_NOTIMPL;
1038 /************************************************************************
1039 * DefaultHandler_QueryGetData (IDataObject)
1041 * The default handler's implementation of this method delegates to
1042 * the cache.
1044 * See Windows documentation for more details on IDataObject methods.
1046 static HRESULT WINAPI DefaultHandler_QueryGetData(
1047 IDataObject* iface,
1048 LPFORMATETC pformatetc)
1050 IDataObject* cacheDataObject = NULL;
1051 HRESULT hres;
1053 DefaultHandler *This = impl_from_IDataObject(iface);
1055 TRACE("(%p, %p)\n", iface, pformatetc);
1057 hres = IUnknown_QueryInterface(This->dataCache,
1058 &IID_IDataObject,
1059 (void**)&cacheDataObject);
1061 if (FAILED(hres))
1062 return E_UNEXPECTED;
1064 hres = IDataObject_QueryGetData(cacheDataObject,
1065 pformatetc);
1067 IDataObject_Release(cacheDataObject);
1069 if (FAILED(hres) && This->pDataDelegate)
1070 hres = IDataObject_QueryGetData(This->pDataDelegate, pformatetc);
1072 return hres;
1075 /************************************************************************
1076 * DefaultHandler_GetCanonicalFormatEtc (IDataObject)
1078 * This method is meaningless if the server is not running
1080 * See Windows documentation for more details on IDataObject methods.
1082 static HRESULT WINAPI DefaultHandler_GetCanonicalFormatEtc(
1083 IDataObject* iface,
1084 LPFORMATETC pformatetcIn,
1085 LPFORMATETC pformatetcOut)
1087 DefaultHandler *This = impl_from_IDataObject(iface);
1089 TRACE("(%p, %p, %p)\n", iface, pformatetcIn, pformatetcOut);
1091 if (!This->pDataDelegate)
1092 return OLE_E_NOTRUNNING;
1094 return IDataObject_GetCanonicalFormatEtc(This->pDataDelegate, pformatetcIn, pformatetcOut);
1097 /************************************************************************
1098 * DefaultHandler_SetData (IDataObject)
1100 * The default handler's implementation of this method delegates to
1101 * the cache.
1103 * See Windows documentation for more details on IDataObject methods.
1105 static HRESULT WINAPI DefaultHandler_SetData(
1106 IDataObject* iface,
1107 LPFORMATETC pformatetc,
1108 STGMEDIUM* pmedium,
1109 BOOL fRelease)
1111 DefaultHandler *This = impl_from_IDataObject(iface);
1112 IDataObject* cacheDataObject = NULL;
1113 HRESULT hres;
1115 TRACE("(%p, %p, %p, %d)\n", iface, pformatetc, pmedium, fRelease);
1117 hres = IUnknown_QueryInterface(This->dataCache,
1118 &IID_IDataObject,
1119 (void**)&cacheDataObject);
1121 if (FAILED(hres))
1122 return E_UNEXPECTED;
1124 hres = IDataObject_SetData(cacheDataObject,
1125 pformatetc,
1126 pmedium,
1127 fRelease);
1129 IDataObject_Release(cacheDataObject);
1131 return hres;
1134 /************************************************************************
1135 * DefaultHandler_EnumFormatEtc (IDataObject)
1137 * The default handler's implementation of This method simply delegates
1138 * to OleRegEnumFormatEtc.
1140 * See Windows documentation for more details on IDataObject methods.
1142 static HRESULT WINAPI DefaultHandler_EnumFormatEtc(
1143 IDataObject* iface,
1144 DWORD dwDirection,
1145 IEnumFORMATETC** ppenumFormatEtc)
1147 DefaultHandler *This = impl_from_IDataObject(iface);
1149 TRACE("(%p, %x, %p)\n", iface, dwDirection, ppenumFormatEtc);
1151 return OleRegEnumFormatEtc(&This->clsid, dwDirection, ppenumFormatEtc);
1154 /************************************************************************
1155 * DefaultHandler_DAdvise (IDataObject)
1157 * The default handler's implementation of this method simply
1158 * delegates to the DataAdviseHolder.
1160 * See Windows documentation for more details on IDataObject methods.
1162 static HRESULT WINAPI DefaultHandler_DAdvise(
1163 IDataObject* iface,
1164 FORMATETC* pformatetc,
1165 DWORD advf,
1166 IAdviseSink* pAdvSink,
1167 DWORD* pdwConnection)
1169 HRESULT hres = S_OK;
1170 DefaultHandler *This = impl_from_IDataObject(iface);
1172 TRACE("(%p, %p, %d, %p, %p)\n",
1173 iface, pformatetc, advf, pAdvSink, pdwConnection);
1175 /* Make sure we have a data advise holder before we start. */
1176 if (!This->dataAdviseHolder)
1178 hres = CreateDataAdviseHolder(&This->dataAdviseHolder);
1179 if (SUCCEEDED(hres) && This->pDataDelegate)
1180 DataAdviseHolder_OnConnect(This->dataAdviseHolder, This->pDataDelegate);
1183 if (SUCCEEDED(hres))
1184 hres = IDataAdviseHolder_Advise(This->dataAdviseHolder,
1185 iface,
1186 pformatetc,
1187 advf,
1188 pAdvSink,
1189 pdwConnection);
1191 return hres;
1194 /************************************************************************
1195 * DefaultHandler_DUnadvise (IDataObject)
1197 * The default handler's implementation of this method simply
1198 * delegates to the DataAdviseHolder.
1200 * See Windows documentation for more details on IDataObject methods.
1202 static HRESULT WINAPI DefaultHandler_DUnadvise(
1203 IDataObject* iface,
1204 DWORD dwConnection)
1206 DefaultHandler *This = impl_from_IDataObject(iface);
1208 TRACE("(%p, %d)\n", iface, dwConnection);
1211 * If we don't have a data advise holder yet, it means that
1212 * we don't have any connections..
1214 if (!This->dataAdviseHolder)
1215 return OLE_E_NOCONNECTION;
1217 return IDataAdviseHolder_Unadvise(This->dataAdviseHolder,
1218 dwConnection);
1221 /************************************************************************
1222 * DefaultHandler_EnumDAdvise (IDataObject)
1224 * The default handler's implementation of this method simply
1225 * delegates to the DataAdviseHolder.
1227 * See Windows documentation for more details on IDataObject methods.
1229 static HRESULT WINAPI DefaultHandler_EnumDAdvise(
1230 IDataObject* iface,
1231 IEnumSTATDATA** ppenumAdvise)
1233 DefaultHandler *This = impl_from_IDataObject(iface);
1235 TRACE("(%p, %p)\n", iface, ppenumAdvise);
1237 if (!ppenumAdvise)
1238 return E_POINTER;
1240 *ppenumAdvise = NULL;
1242 /* If we have a data advise holder object, delegate. */
1243 if (This->dataAdviseHolder)
1244 return IDataAdviseHolder_EnumAdvise(This->dataAdviseHolder,
1245 ppenumAdvise);
1247 return S_OK;
1250 /*********************************************************
1251 * Methods implementation for the IRunnableObject part
1252 * of the DefaultHandler class.
1255 /************************************************************************
1256 * DefaultHandler_IRunnableObject_QueryInterface (IUnknown)
1258 * See Windows documentation for more details on IUnknown methods.
1260 static HRESULT WINAPI DefaultHandler_IRunnableObject_QueryInterface(
1261 IRunnableObject* iface,
1262 REFIID riid,
1263 void** ppvObject)
1265 DefaultHandler *This = impl_from_IRunnableObject(iface);
1267 return IUnknown_QueryInterface(This->outerUnknown, riid, ppvObject);
1270 /************************************************************************
1271 * DefaultHandler_IRunnableObject_AddRef (IUnknown)
1273 * See Windows documentation for more details on IUnknown methods.
1275 static ULONG WINAPI DefaultHandler_IRunnableObject_AddRef(
1276 IRunnableObject* iface)
1278 DefaultHandler *This = impl_from_IRunnableObject(iface);
1280 return IUnknown_AddRef(This->outerUnknown);
1283 /************************************************************************
1284 * DefaultHandler_IRunnableObject_Release (IUnknown)
1286 * See Windows documentation for more details on IUnknown methods.
1288 static ULONG WINAPI DefaultHandler_IRunnableObject_Release(
1289 IRunnableObject* iface)
1291 DefaultHandler *This = impl_from_IRunnableObject(iface);
1293 return IUnknown_Release(This->outerUnknown);
1296 /************************************************************************
1297 * DefaultHandler_GetRunningClass (IRunnableObject)
1299 * See Windows documentation for more details on IRunnableObject methods.
1301 static HRESULT WINAPI DefaultHandler_GetRunningClass(
1302 IRunnableObject* iface,
1303 LPCLSID lpClsid)
1305 FIXME("()\n");
1306 return S_OK;
1309 static HRESULT WINAPI DefaultHandler_Run(
1310 IRunnableObject* iface,
1311 IBindCtx* pbc)
1313 DefaultHandler *This = impl_from_IRunnableObject(iface);
1314 HRESULT hr;
1316 FIXME("(%p): semi-stub\n", pbc);
1318 /* already running? if so nothing to do */
1319 if (object_is_running(This))
1320 return S_OK;
1322 release_delegates(This);
1324 hr = CoCreateInstance(&This->clsid, NULL, CLSCTX_LOCAL_SERVER,
1325 &IID_IOleObject, (void **)&This->pOleDelegate);
1326 if (FAILED(hr))
1327 return hr;
1329 This->object_state = object_state_running;
1331 hr = IOleObject_Advise(This->pOleDelegate,
1332 (IAdviseSink *)&This->lpvtblIAdviseSink,
1333 &This->dwAdvConn);
1335 if (SUCCEEDED(hr) && This->clientSite)
1336 hr = IOleObject_SetClientSite(This->pOleDelegate, This->clientSite);
1338 if (SUCCEEDED(hr))
1340 IOleObject_QueryInterface(This->pOleDelegate, &IID_IPersistStorage,
1341 (void **)&This->pPSDelegate);
1342 if (This->pPSDelegate)
1344 if(This->storage_state == storage_state_initialised)
1345 hr = IPersistStorage_InitNew(This->pPSDelegate, This->storage);
1346 else if(This->storage_state == storage_state_loaded)
1347 hr = IPersistStorage_Load(This->pPSDelegate, This->storage);
1351 if (SUCCEEDED(hr) && This->containerApp)
1352 hr = IOleObject_SetHostNames(This->pOleDelegate, This->containerApp,
1353 This->containerObj);
1355 /* FIXME: do more stuff here:
1356 * - IOleObject_GetMiscStatus
1357 * - IOleObject_GetMoniker
1358 * - IOleCache_OnRun
1361 if (SUCCEEDED(hr))
1362 hr = IOleObject_QueryInterface(This->pOleDelegate, &IID_IDataObject,
1363 (void **)&This->pDataDelegate);
1365 if (SUCCEEDED(hr) && This->dataAdviseHolder)
1366 hr = DataAdviseHolder_OnConnect(This->dataAdviseHolder, This->pDataDelegate);
1368 if (FAILED(hr))
1370 DefaultHandler_Stop(This);
1371 release_delegates(This);
1374 return hr;
1377 /************************************************************************
1378 * DefaultHandler_IsRunning (IRunnableObject)
1380 * See Windows documentation for more details on IRunnableObject methods.
1382 static BOOL WINAPI DefaultHandler_IsRunning(
1383 IRunnableObject* iface)
1385 DefaultHandler *This = impl_from_IRunnableObject(iface);
1387 TRACE("()\n");
1389 if (This->object_state == object_state_running)
1390 return TRUE;
1391 else
1392 return FALSE;
1395 /************************************************************************
1396 * DefaultHandler_LockRunning (IRunnableObject)
1398 * See Windows documentation for more details on IRunnableObject methods.
1400 static HRESULT WINAPI DefaultHandler_LockRunning(
1401 IRunnableObject* iface,
1402 BOOL fLock,
1403 BOOL fLastUnlockCloses)
1405 FIXME("()\n");
1406 return S_OK;
1409 /************************************************************************
1410 * DefaultHandler_SetContainedObject (IRunnableObject)
1412 * See Windows documentation for more details on IRunnableObject methods.
1414 static HRESULT WINAPI DefaultHandler_SetContainedObject(
1415 IRunnableObject* iface,
1416 BOOL fContained)
1418 FIXME("()\n");
1419 return S_OK;
1422 static HRESULT WINAPI DefaultHandler_IAdviseSink_QueryInterface(
1423 IAdviseSink *iface,
1424 REFIID riid,
1425 void **ppvObject)
1427 if (IsEqualIID(riid, &IID_IUnknown) ||
1428 IsEqualIID(riid, &IID_IAdviseSink))
1430 *ppvObject = iface;
1431 IAdviseSink_AddRef(iface);
1432 return S_OK;
1435 return E_NOINTERFACE;
1438 static ULONG WINAPI DefaultHandler_IAdviseSink_AddRef(
1439 IAdviseSink *iface)
1441 DefaultHandler *This = impl_from_IAdviseSink(iface);
1443 return IUnknown_AddRef((IUnknown *)&This->lpvtblIUnknown);
1446 static ULONG WINAPI DefaultHandler_IAdviseSink_Release(
1447 IAdviseSink *iface)
1449 DefaultHandler *This = impl_from_IAdviseSink(iface);
1451 return IUnknown_Release((IUnknown *)&This->lpvtblIUnknown);
1454 static void WINAPI DefaultHandler_IAdviseSink_OnDataChange(
1455 IAdviseSink *iface,
1456 FORMATETC *pFormatetc,
1457 STGMEDIUM *pStgmed)
1459 FIXME(": stub\n");
1462 static void WINAPI DefaultHandler_IAdviseSink_OnViewChange(
1463 IAdviseSink *iface,
1464 DWORD dwAspect,
1465 LONG lindex)
1467 FIXME(": stub\n");
1470 static void WINAPI DefaultHandler_IAdviseSink_OnRename(
1471 IAdviseSink *iface,
1472 IMoniker *pmk)
1474 DefaultHandler *This = impl_from_IAdviseSink(iface);
1476 TRACE("(%p)\n", pmk);
1478 if (This->oleAdviseHolder)
1479 IOleAdviseHolder_SendOnRename(This->oleAdviseHolder, pmk);
1482 static void WINAPI DefaultHandler_IAdviseSink_OnSave(
1483 IAdviseSink *iface)
1485 DefaultHandler *This = impl_from_IAdviseSink(iface);
1487 TRACE("()\n");
1489 if (This->oleAdviseHolder)
1490 IOleAdviseHolder_SendOnSave(This->oleAdviseHolder);
1493 static void WINAPI DefaultHandler_IAdviseSink_OnClose(
1494 IAdviseSink *iface)
1496 DefaultHandler *This = impl_from_IAdviseSink(iface);
1498 TRACE("()\n");
1500 if (This->oleAdviseHolder)
1501 IOleAdviseHolder_SendOnClose(This->oleAdviseHolder);
1503 DefaultHandler_Stop(This);
1507 /************************************************************************
1508 * DefaultHandler_IPersistStorage_QueryInterface
1511 static HRESULT WINAPI DefaultHandler_IPersistStorage_QueryInterface(
1512 IPersistStorage* iface,
1513 REFIID riid,
1514 void** ppvObject)
1516 DefaultHandler *This = impl_from_IPersistStorage(iface);
1518 return IUnknown_QueryInterface(This->outerUnknown, riid, ppvObject);
1521 /************************************************************************
1522 * DefaultHandler_IPersistStorage_AddRef
1525 static ULONG WINAPI DefaultHandler_IPersistStorage_AddRef(
1526 IPersistStorage* iface)
1528 DefaultHandler *This = impl_from_IPersistStorage(iface);
1530 return IUnknown_AddRef(This->outerUnknown);
1533 /************************************************************************
1534 * DefaultHandler_IPersistStorage_Release
1537 static ULONG WINAPI DefaultHandler_IPersistStorage_Release(
1538 IPersistStorage* iface)
1540 DefaultHandler *This = impl_from_IPersistStorage(iface);
1542 return IUnknown_Release(This->outerUnknown);
1545 /************************************************************************
1546 * DefaultHandler_IPersistStorage_GetClassID
1549 static HRESULT WINAPI DefaultHandler_IPersistStorage_GetClassID(
1550 IPersistStorage* iface,
1551 CLSID* clsid)
1553 DefaultHandler *This = impl_from_IPersistStorage(iface);
1554 HRESULT hr;
1556 TRACE("(%p)->(%p)\n", iface, clsid);
1558 if(object_is_running(This))
1559 hr = IPersistStorage_GetClassID(This->pPSDelegate, clsid);
1560 else
1561 hr = IPersistStorage_GetClassID(This->dataCache_PersistStg, clsid);
1563 return hr;
1566 /************************************************************************
1567 * DefaultHandler_IPersistStorage_IsDirty
1570 static HRESULT WINAPI DefaultHandler_IPersistStorage_IsDirty(
1571 IPersistStorage* iface)
1573 DefaultHandler *This = impl_from_IPersistStorage(iface);
1574 HRESULT hr;
1576 TRACE("(%p)\n", iface);
1578 hr = IPersistStorage_IsDirty(This->dataCache_PersistStg);
1579 if(hr != S_FALSE) return hr;
1581 if(object_is_running(This))
1582 hr = IPersistStorage_IsDirty(This->pPSDelegate);
1584 return hr;
1587 /***********************************************************************
1588 * init_ole_stream
1590 * Creates the '\1Ole' stream.
1591 * The format of this stream is as follows:
1593 * DWORD Version == 0x02000001
1594 * DWORD Flags - low bit set indicates the object is a link otherwise it's embedded.
1595 * DWORD LinkupdateOption - [MS-OLEDS describes this as an implementation specific hint
1596 * supplied by the app that creates the data structure. May be
1597 * ignored on processing].
1599 * DWORD Reserved == 0
1600 * DWORD MonikerStreamSize - size of the rest of the data (ie CLSID + moniker stream data).
1601 * CLSID clsid - class id of object capable of processing the moniker
1602 * BYTE data[] - moniker data for a link
1605 static const WCHAR OleStream[] = {1,'O','l','e',0};
1606 typedef struct
1608 DWORD version;
1609 DWORD flags;
1610 DWORD link_update_opt;
1611 DWORD res;
1612 DWORD moniker_size;
1613 } ole_stream_header_t;
1614 static const DWORD ole_stream_version = 0x02000001;
1616 static void init_ole_stream(IStorage *storage)
1618 HRESULT hr;
1619 IStream *stream;
1621 hr = IStorage_CreateStream(storage, OleStream, STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stream);
1622 if(SUCCEEDED(hr))
1624 DWORD written;
1625 ole_stream_header_t header;
1627 header.version = ole_stream_version;
1628 header.flags = 0;
1629 header.link_update_opt = 0;
1630 header.res = 0;
1631 header.moniker_size = 0;
1633 IStream_Write(stream, &header, sizeof(header), &written);
1634 IStream_Release(stream);
1636 return;
1639 static HRESULT load_ole_stream(DefaultHandler *This, IStorage *storage)
1641 IStream *stream;
1642 HRESULT hr;
1644 hr = IStorage_OpenStream(storage, OleStream, NULL, STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stream);
1646 if(SUCCEEDED(hr))
1648 DWORD read;
1649 ole_stream_header_t header;
1651 hr = IStream_Read(stream, &header, sizeof(header), &read);
1652 if(hr == S_OK && read == sizeof(header) && header.version == ole_stream_version)
1654 if(header.flags & 1)
1656 /* FIXME: Read the moniker and deal with the link */
1657 FIXME("Linked objects are not supported yet\n");
1660 else
1662 WARN("Incorrect OleStream header\n");
1663 hr = DV_E_CLIPFORMAT;
1665 IStream_Release(stream);
1667 else
1669 init_ole_stream(storage);
1670 hr = S_OK;
1672 return hr;
1675 /************************************************************************
1676 * DefaultHandler_IPersistStorage_InitNew
1679 static HRESULT WINAPI DefaultHandler_IPersistStorage_InitNew(
1680 IPersistStorage* iface,
1681 IStorage* pStg)
1683 DefaultHandler *This = impl_from_IPersistStorage(iface);
1684 HRESULT hr;
1686 TRACE("(%p)->(%p)\n", iface, pStg);
1687 init_ole_stream(pStg);
1689 hr = IPersistStorage_InitNew(This->dataCache_PersistStg, pStg);
1691 if(SUCCEEDED(hr) && object_is_running(This))
1692 hr = IPersistStorage_InitNew(This->pPSDelegate, pStg);
1694 if(SUCCEEDED(hr))
1696 IStorage_AddRef(pStg);
1697 This->storage = pStg;
1698 This->storage_state = storage_state_initialised;
1701 return hr;
1705 /************************************************************************
1706 * DefaultHandler_IPersistStorage_Load
1709 static HRESULT WINAPI DefaultHandler_IPersistStorage_Load(
1710 IPersistStorage* iface,
1711 IStorage* pStg)
1713 DefaultHandler *This = impl_from_IPersistStorage(iface);
1714 HRESULT hr;
1716 TRACE("(%p)->(%p)\n", iface, pStg);
1718 hr = load_ole_stream(This, pStg);
1720 if(SUCCEEDED(hr))
1721 hr = IPersistStorage_Load(This->dataCache_PersistStg, pStg);
1723 if(SUCCEEDED(hr) && object_is_running(This))
1724 hr = IPersistStorage_Load(This->pPSDelegate, pStg);
1726 if(SUCCEEDED(hr))
1728 IStorage_AddRef(pStg);
1729 This->storage = pStg;
1730 This->storage_state = storage_state_loaded;
1732 return hr;
1736 /************************************************************************
1737 * DefaultHandler_IPersistStorage_Save
1740 static HRESULT WINAPI DefaultHandler_IPersistStorage_Save(
1741 IPersistStorage* iface,
1742 IStorage* pStgSave,
1743 BOOL fSameAsLoad)
1745 DefaultHandler *This = impl_from_IPersistStorage(iface);
1746 HRESULT hr;
1748 TRACE("(%p)->(%p, %d)\n", iface, pStgSave, fSameAsLoad);
1750 hr = IPersistStorage_Save(This->dataCache_PersistStg, pStgSave, fSameAsLoad);
1751 if(SUCCEEDED(hr) && object_is_running(This))
1752 hr = IPersistStorage_Save(This->pPSDelegate, pStgSave, fSameAsLoad);
1754 return hr;
1758 /************************************************************************
1759 * DefaultHandler_IPersistStorage_SaveCompleted
1762 static HRESULT WINAPI DefaultHandler_IPersistStorage_SaveCompleted(
1763 IPersistStorage* iface,
1764 IStorage* pStgNew)
1766 DefaultHandler *This = impl_from_IPersistStorage(iface);
1767 HRESULT hr;
1769 TRACE("(%p)->(%p)\n", iface, pStgNew);
1771 hr = IPersistStorage_SaveCompleted(This->dataCache_PersistStg, pStgNew);
1773 if(SUCCEEDED(hr) && object_is_running(This))
1774 hr = IPersistStorage_SaveCompleted(This->pPSDelegate, pStgNew);
1776 if(pStgNew)
1778 IStorage_AddRef(pStgNew);
1779 if(This->storage) IStorage_Release(This->storage);
1780 This->storage = pStgNew;
1781 This->storage_state = storage_state_loaded;
1784 return hr;
1788 /************************************************************************
1789 * DefaultHandler_IPersistStorage_HandsOffStorage
1792 static HRESULT WINAPI DefaultHandler_IPersistStorage_HandsOffStorage(
1793 IPersistStorage* iface)
1795 DefaultHandler *This = impl_from_IPersistStorage(iface);
1796 HRESULT hr;
1798 TRACE("(%p)\n", iface);
1800 hr = IPersistStorage_HandsOffStorage(This->dataCache_PersistStg);
1802 if(SUCCEEDED(hr) && object_is_running(This))
1803 hr = IPersistStorage_HandsOffStorage(This->pPSDelegate);
1805 if(This->storage) IStorage_Release(This->storage);
1806 This->storage = NULL;
1807 This->storage_state = storage_state_uninitialised;
1809 return hr;
1814 * Virtual function tables for the DefaultHandler class.
1816 static const IOleObjectVtbl DefaultHandler_IOleObject_VTable =
1818 DefaultHandler_QueryInterface,
1819 DefaultHandler_AddRef,
1820 DefaultHandler_Release,
1821 DefaultHandler_SetClientSite,
1822 DefaultHandler_GetClientSite,
1823 DefaultHandler_SetHostNames,
1824 DefaultHandler_Close,
1825 DefaultHandler_SetMoniker,
1826 DefaultHandler_GetMoniker,
1827 DefaultHandler_InitFromData,
1828 DefaultHandler_GetClipboardData,
1829 DefaultHandler_DoVerb,
1830 DefaultHandler_EnumVerbs,
1831 DefaultHandler_Update,
1832 DefaultHandler_IsUpToDate,
1833 DefaultHandler_GetUserClassID,
1834 DefaultHandler_GetUserType,
1835 DefaultHandler_SetExtent,
1836 DefaultHandler_GetExtent,
1837 DefaultHandler_Advise,
1838 DefaultHandler_Unadvise,
1839 DefaultHandler_EnumAdvise,
1840 DefaultHandler_GetMiscStatus,
1841 DefaultHandler_SetColorScheme
1844 static const IUnknownVtbl DefaultHandler_NDIUnknown_VTable =
1846 DefaultHandler_NDIUnknown_QueryInterface,
1847 DefaultHandler_NDIUnknown_AddRef,
1848 DefaultHandler_NDIUnknown_Release,
1851 static const IDataObjectVtbl DefaultHandler_IDataObject_VTable =
1853 DefaultHandler_IDataObject_QueryInterface,
1854 DefaultHandler_IDataObject_AddRef,
1855 DefaultHandler_IDataObject_Release,
1856 DefaultHandler_GetData,
1857 DefaultHandler_GetDataHere,
1858 DefaultHandler_QueryGetData,
1859 DefaultHandler_GetCanonicalFormatEtc,
1860 DefaultHandler_SetData,
1861 DefaultHandler_EnumFormatEtc,
1862 DefaultHandler_DAdvise,
1863 DefaultHandler_DUnadvise,
1864 DefaultHandler_EnumDAdvise
1867 static const IRunnableObjectVtbl DefaultHandler_IRunnableObject_VTable =
1869 DefaultHandler_IRunnableObject_QueryInterface,
1870 DefaultHandler_IRunnableObject_AddRef,
1871 DefaultHandler_IRunnableObject_Release,
1872 DefaultHandler_GetRunningClass,
1873 DefaultHandler_Run,
1874 DefaultHandler_IsRunning,
1875 DefaultHandler_LockRunning,
1876 DefaultHandler_SetContainedObject
1879 static const IAdviseSinkVtbl DefaultHandler_IAdviseSink_VTable =
1881 DefaultHandler_IAdviseSink_QueryInterface,
1882 DefaultHandler_IAdviseSink_AddRef,
1883 DefaultHandler_IAdviseSink_Release,
1884 DefaultHandler_IAdviseSink_OnDataChange,
1885 DefaultHandler_IAdviseSink_OnViewChange,
1886 DefaultHandler_IAdviseSink_OnRename,
1887 DefaultHandler_IAdviseSink_OnSave,
1888 DefaultHandler_IAdviseSink_OnClose
1891 static const IPersistStorageVtbl DefaultHandler_IPersistStorage_VTable =
1893 DefaultHandler_IPersistStorage_QueryInterface,
1894 DefaultHandler_IPersistStorage_AddRef,
1895 DefaultHandler_IPersistStorage_Release,
1896 DefaultHandler_IPersistStorage_GetClassID,
1897 DefaultHandler_IPersistStorage_IsDirty,
1898 DefaultHandler_IPersistStorage_InitNew,
1899 DefaultHandler_IPersistStorage_Load,
1900 DefaultHandler_IPersistStorage_Save,
1901 DefaultHandler_IPersistStorage_SaveCompleted,
1902 DefaultHandler_IPersistStorage_HandsOffStorage
1905 /*********************************************************
1906 * Methods implementation for the DefaultHandler class.
1908 static DefaultHandler* DefaultHandler_Construct(
1909 REFCLSID clsid,
1910 LPUNKNOWN pUnkOuter,
1911 DWORD flags,
1912 IClassFactory *pCF)
1914 DefaultHandler* This = NULL;
1915 HRESULT hr;
1917 This = HeapAlloc(GetProcessHeap(), 0, sizeof(DefaultHandler));
1919 if (!This)
1920 return This;
1922 This->lpVtbl = &DefaultHandler_IOleObject_VTable;
1923 This->lpvtblIUnknown = &DefaultHandler_NDIUnknown_VTable;
1924 This->lpvtblIDataObject = &DefaultHandler_IDataObject_VTable;
1925 This->lpvtblIRunnableObject = &DefaultHandler_IRunnableObject_VTable;
1926 This->lpvtblIAdviseSink = &DefaultHandler_IAdviseSink_VTable;
1927 This->lpvtblIPersistStorage = &DefaultHandler_IPersistStorage_VTable;
1929 This->inproc_server = (flags & EMBDHLP_INPROC_SERVER) ? TRUE : FALSE;
1932 * Start with one reference count. The caller of this function
1933 * must release the interface pointer when it is done.
1935 This->ref = 1;
1938 * Initialize the outer unknown
1939 * We don't keep a reference on the outer unknown since, the way
1940 * aggregation works, our lifetime is at least as large as its
1941 * lifetime.
1943 if (!pUnkOuter)
1944 pUnkOuter = (IUnknown*)&This->lpvtblIUnknown;
1946 This->outerUnknown = pUnkOuter;
1949 * Create a datacache object.
1950 * We aggregate with the datacache. Make sure we pass our outer
1951 * unknown as the datacache's outer unknown.
1953 hr = CreateDataCache(This->outerUnknown,
1954 clsid,
1955 &IID_IUnknown,
1956 (void**)&This->dataCache);
1957 if(SUCCEEDED(hr))
1959 hr = IUnknown_QueryInterface(This->dataCache, &IID_IPersistStorage, (void**)&This->dataCache_PersistStg);
1960 /* keeping a reference to This->dataCache_PersistStg causes us to keep a
1961 * reference on the outer object */
1962 if (SUCCEEDED(hr))
1963 IUnknown_Release(This->outerUnknown);
1964 else
1965 IUnknown_Release(This->dataCache);
1967 if(FAILED(hr))
1969 ERR("Unexpected error creating data cache\n");
1970 HeapFree(GetProcessHeap(), 0, This);
1971 return NULL;
1974 This->clsid = *clsid;
1975 This->clientSite = NULL;
1976 This->oleAdviseHolder = NULL;
1977 This->dataAdviseHolder = NULL;
1978 This->containerApp = NULL;
1979 This->containerObj = NULL;
1980 This->pOleDelegate = NULL;
1981 This->pPSDelegate = NULL;
1982 This->pDataDelegate = NULL;
1983 This->object_state = object_state_not_running;
1985 This->dwAdvConn = 0;
1986 This->storage = NULL;
1987 This->storage_state = storage_state_uninitialised;
1989 if (This->inproc_server && !(flags & EMBDHLP_DELAYCREATE))
1991 HRESULT hr;
1992 This->pCFObject = NULL;
1993 if (pCF)
1994 hr = IClassFactory_CreateInstance(pCF, NULL, &IID_IOleObject, (void **)&This->pOleDelegate);
1995 else
1996 hr = CoCreateInstance(&This->clsid, NULL, CLSCTX_INPROC_SERVER,
1997 &IID_IOleObject, (void **)&This->pOleDelegate);
1998 if (SUCCEEDED(hr))
1999 hr = IOleObject_QueryInterface(This->pOleDelegate, &IID_IPersistStorage, (void **)&This->pPSDelegate);
2000 if (SUCCEEDED(hr))
2001 hr = IOleObject_QueryInterface(This->pOleDelegate, &IID_IDataObject, (void **)&This->pDataDelegate);
2002 if (SUCCEEDED(hr))
2003 This->object_state = object_state_running;
2004 if (FAILED(hr))
2005 WARN("object creation failed with error %08x\n", hr);
2007 else
2009 This->pCFObject = pCF;
2010 if (pCF) IClassFactory_AddRef(pCF);
2013 return This;
2016 static void DefaultHandler_Destroy(
2017 DefaultHandler* This)
2019 TRACE("(%p)\n", This);
2021 /* AddRef/Release may be called on this object during destruction.
2022 * Prevent the object being destroyed recursively by artificially raising
2023 * the reference count. */
2024 This->ref = 10000;
2026 /* release delegates */
2027 DefaultHandler_Stop(This);
2028 release_delegates(This);
2030 HeapFree( GetProcessHeap(), 0, This->containerApp );
2031 This->containerApp = NULL;
2032 HeapFree( GetProcessHeap(), 0, This->containerObj );
2033 This->containerObj = NULL;
2035 if (This->dataCache)
2037 /* to balance out the release of dataCache_PersistStg which will result
2038 * in a reference being released from the outer unknown */
2039 IUnknown_AddRef(This->outerUnknown);
2040 IPersistStorage_Release(This->dataCache_PersistStg);
2041 IUnknown_Release(This->dataCache);
2042 This->dataCache_PersistStg = NULL;
2043 This->dataCache = NULL;
2046 if (This->clientSite)
2048 IOleClientSite_Release(This->clientSite);
2049 This->clientSite = NULL;
2052 if (This->oleAdviseHolder)
2054 IOleAdviseHolder_Release(This->oleAdviseHolder);
2055 This->oleAdviseHolder = NULL;
2058 if (This->dataAdviseHolder)
2060 IDataAdviseHolder_Release(This->dataAdviseHolder);
2061 This->dataAdviseHolder = NULL;
2064 if (This->storage)
2066 IStorage_Release(This->storage);
2067 This->storage = NULL;
2070 if (This->pCFObject)
2072 IClassFactory_Release(This->pCFObject);
2073 This->pCFObject = NULL;
2076 HeapFree(GetProcessHeap(), 0, This);
2079 /******************************************************************************
2080 * OleCreateEmbeddingHelper [OLE32.@]
2082 HRESULT WINAPI OleCreateEmbeddingHelper(
2083 REFCLSID clsid,
2084 LPUNKNOWN pUnkOuter,
2085 DWORD flags,
2086 IClassFactory *pCF,
2087 REFIID riid,
2088 LPVOID* ppvObj)
2090 DefaultHandler* newHandler = NULL;
2091 HRESULT hr = S_OK;
2093 TRACE("(%s, %p, %08x, %p, %s, %p)\n", debugstr_guid(clsid), pUnkOuter, flags, pCF, debugstr_guid(riid), ppvObj);
2095 if (!ppvObj)
2096 return E_POINTER;
2098 *ppvObj = NULL;
2101 * If This handler is constructed for aggregation, make sure
2102 * the caller is requesting the IUnknown interface.
2103 * This is necessary because it's the only time the non-delegating
2104 * IUnknown pointer can be returned to the outside.
2106 if (pUnkOuter && !IsEqualIID(&IID_IUnknown, riid))
2107 return CLASS_E_NOAGGREGATION;
2110 * Try to construct a new instance of the class.
2112 newHandler = DefaultHandler_Construct(clsid, pUnkOuter, flags, pCF);
2114 if (!newHandler)
2115 return E_OUTOFMEMORY;
2118 * Make sure it supports the interface required by the caller.
2120 hr = IUnknown_QueryInterface((IUnknown*)&newHandler->lpvtblIUnknown, riid, ppvObj);
2123 * Release the reference obtained in the constructor. If
2124 * the QueryInterface was unsuccessful, it will free the class.
2126 IUnknown_Release((IUnknown*)&newHandler->lpvtblIUnknown);
2128 return hr;
2132 /******************************************************************************
2133 * OleCreateDefaultHandler [OLE32.@]
2135 HRESULT WINAPI OleCreateDefaultHandler(REFCLSID clsid, LPUNKNOWN pUnkOuter,
2136 REFIID riid, LPVOID* ppvObj)
2138 TRACE("(%s, %p, %s, %p)\n", debugstr_guid(clsid), pUnkOuter,debugstr_guid(riid), ppvObj);
2139 return OleCreateEmbeddingHelper(clsid, pUnkOuter, EMBDHLP_INPROC_HANDLER | EMBDHLP_CREATENOW,
2140 NULL, riid, ppvObj);
2143 typedef struct HandlerCF
2145 const IClassFactoryVtbl *lpVtbl;
2146 LONG refs;
2147 CLSID clsid;
2148 } HandlerCF;
2150 static HRESULT WINAPI
2151 HandlerCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid, LPVOID *ppv)
2153 *ppv = NULL;
2154 if (IsEqualIID(riid,&IID_IUnknown) ||
2155 IsEqualIID(riid,&IID_IClassFactory))
2157 *ppv = iface;
2158 IClassFactory_AddRef(iface);
2159 return S_OK;
2161 return E_NOINTERFACE;
2164 static ULONG WINAPI HandlerCF_AddRef(LPCLASSFACTORY iface)
2166 HandlerCF *This = (HandlerCF *)iface;
2167 return InterlockedIncrement(&This->refs);
2170 static ULONG WINAPI HandlerCF_Release(LPCLASSFACTORY iface)
2172 HandlerCF *This = (HandlerCF *)iface;
2173 ULONG refs = InterlockedDecrement(&This->refs);
2174 if (!refs)
2175 HeapFree(GetProcessHeap(), 0, This);
2176 return refs;
2179 static HRESULT WINAPI
2180 HandlerCF_CreateInstance(LPCLASSFACTORY iface, LPUNKNOWN pUnk,
2181 REFIID riid, LPVOID *ppv)
2183 HandlerCF *This = (HandlerCF *)iface;
2184 return OleCreateDefaultHandler(&This->clsid, pUnk, riid, ppv);
2187 static HRESULT WINAPI HandlerCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
2189 FIXME("(%d), stub!\n",fLock);
2190 return S_OK;
2193 static const IClassFactoryVtbl HandlerClassFactoryVtbl = {
2194 HandlerCF_QueryInterface,
2195 HandlerCF_AddRef,
2196 HandlerCF_Release,
2197 HandlerCF_CreateInstance,
2198 HandlerCF_LockServer
2201 HRESULT HandlerCF_Create(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
2203 HRESULT hr;
2204 HandlerCF *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
2205 if (!This) return E_OUTOFMEMORY;
2206 This->lpVtbl = &HandlerClassFactoryVtbl;
2207 This->refs = 0;
2208 This->clsid = *rclsid;
2210 hr = IUnknown_QueryInterface((IUnknown *)&This->lpVtbl, riid, ppv);
2211 if (FAILED(hr))
2212 HeapFree(GetProcessHeap(), 0, This);
2214 return hr;