msi/tests: Fix a test failure on win2k.
[wine] / dlls / ole32 / defaulthandler.c
1 /*
2  *      OLE 2 default object handler
3  *
4  *      Copyright 1999  Francis Beaudet
5  *      Copyright 2000  Abey George
6  *
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.
11  *
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.
16  *
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
20  *
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.
26  *
27  *    All the implementation details are taken from: Inside OLE
28  *    second edition by Kraig Brockschmidt,
29  *
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
35  *
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.
40  *
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.
44  *
45  * - All the methods related to notification and advise sinks are
46  *   in place but no notifications are sent to the sinks yet.
47  */
48 #include <assert.h>
49 #include <stdarg.h>
50 #include <string.h>
51
52 #define COBJMACROS
53
54 #include "windef.h"
55 #include "winbase.h"
56 #include "winuser.h"
57 #include "winerror.h"
58 #include "ole2.h"
59
60 #include "compobj_private.h"
61
62 #include "wine/unicode.h"
63 #include "wine/debug.h"
64
65 WINE_DEFAULT_DEBUG_CHANNEL(ole);
66
67 enum storage_state
68 {
69     storage_state_uninitialised,
70     storage_state_initialised,
71     storage_state_loaded
72 };
73
74 enum object_state
75 {
76     object_state_not_running,
77     object_state_running
78 };
79
80 /****************************************************************************
81  * DefaultHandler
82  *
83  */
84 struct DefaultHandler
85 {
86   const IOleObjectVtbl*      lpVtbl;
87   const IUnknownVtbl*        lpvtblIUnknown;
88   const IDataObjectVtbl*     lpvtblIDataObject;
89   const IRunnableObjectVtbl* lpvtblIRunnableObject;
90   const IAdviseSinkVtbl     *lpvtblIAdviseSink;
91   const IPersistStorageVtbl *lpvtblIPersistStorage;
92
93   /* Reference count of this object */
94   LONG ref;
95
96   /* IUnknown implementation of the outer object. */
97   IUnknown* outerUnknown;
98
99   /* Class Id that this handler object represents. */
100   CLSID clsid;
101
102   /* IUnknown implementation of the datacache. */
103   IUnknown* dataCache;
104   /* IPersistStorage implementation of the datacache. */
105   IPersistStorage* dataCache_PersistStg;
106
107   /* Client site for the embedded object. */
108   IOleClientSite* clientSite;
109
110   /*
111    * The IOleAdviseHolder maintains the connections
112    * on behalf of the default handler.
113    */
114   IOleAdviseHolder* oleAdviseHolder;
115
116   /*
117    * The IDataAdviseHolder maintains the data
118    * connections on behalf of the default handler.
119    */
120   IDataAdviseHolder* dataAdviseHolder;
121
122   /* Name of the container and object contained */
123   LPWSTR containerApp;
124   LPWSTR containerObj;
125
126   /* IOleObject delegate */
127   IOleObject *pOleDelegate;
128   /* IPersistStorage delegate */
129   IPersistStorage *pPSDelegate;
130   /* IDataObject delegate */
131   IDataObject *pDataDelegate;
132   enum object_state object_state;
133
134   /* connection cookie for the advise on the delegate OLE object */
135   DWORD dwAdvConn;
136
137   /* storage passed to Load or InitNew */
138   IStorage *storage;
139   enum storage_state storage_state;
140
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;
145 };
146
147 typedef struct DefaultHandler DefaultHandler;
148
149 static inline DefaultHandler *impl_from_IOleObject( IOleObject *iface )
150 {
151     return (DefaultHandler *)((char*)iface - FIELD_OFFSET(DefaultHandler, lpVtbl));
152 }
153
154 static inline DefaultHandler *impl_from_NDIUnknown( IUnknown *iface )
155 {
156     return (DefaultHandler *)((char*)iface - FIELD_OFFSET(DefaultHandler, lpvtblIUnknown));
157 }
158
159 static inline DefaultHandler *impl_from_IDataObject( IDataObject *iface )
160 {
161     return (DefaultHandler *)((char*)iface - FIELD_OFFSET(DefaultHandler, lpvtblIDataObject));
162 }
163
164 static inline DefaultHandler *impl_from_IRunnableObject( IRunnableObject *iface )
165 {
166     return (DefaultHandler *)((char*)iface - FIELD_OFFSET(DefaultHandler, lpvtblIRunnableObject));
167 }
168
169 static inline DefaultHandler *impl_from_IAdviseSink( IAdviseSink *iface )
170 {
171     return (DefaultHandler *)((char*)iface - FIELD_OFFSET(DefaultHandler, lpvtblIAdviseSink));
172 }
173
174 static inline DefaultHandler *impl_from_IPersistStorage( IPersistStorage *iface )
175 {
176     return (DefaultHandler *)((char*)iface - FIELD_OFFSET(DefaultHandler, lpvtblIPersistStorage));
177 }
178
179 static void DefaultHandler_Destroy(DefaultHandler* This);
180
181 static inline BOOL object_is_running(DefaultHandler *This)
182 {
183     return IRunnableObject_IsRunning((IRunnableObject*)&This->lpvtblIRunnableObject);
184 }
185
186 /*********************************************************
187  * Method implementation for the  non delegating IUnknown
188  * part of the DefaultHandler class.
189  */
190
191 /************************************************************************
192  * DefaultHandler_NDIUnknown_QueryInterface (IUnknown)
193  *
194  * See Windows documentation for more details on IUnknown methods.
195  *
196  * This version of QueryInterface will not delegate its implementation
197  * to the outer unknown.
198  */
199 static HRESULT WINAPI DefaultHandler_NDIUnknown_QueryInterface(
200             IUnknown*      iface,
201             REFIID         riid,
202             void**         ppvObject)
203 {
204   DefaultHandler *This = impl_from_NDIUnknown(iface);
205
206   if (!ppvObject)
207     return E_INVALIDARG;
208
209   *ppvObject = NULL;
210
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))
226   {
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;
230   }
231   else if (This->inproc_server && This->pOleDelegate)
232   {
233     return IUnknown_QueryInterface(This->pOleDelegate, riid, ppvObject);
234   }
235
236   /* Check that we obtained an interface. */
237   if (*ppvObject == NULL)
238   {
239     WARN( "() : asking for un supported interface %s\n", debugstr_guid(riid));
240     return E_NOINTERFACE;
241   }
242
243   /*
244    * Query Interface always increases the reference count by one when it is
245    * successful.
246    */
247   IUnknown_AddRef((IUnknown*)*ppvObject);
248
249   return S_OK;
250 }
251
252 /************************************************************************
253  * DefaultHandler_NDIUnknown_AddRef (IUnknown)
254  *
255  * See Windows documentation for more details on IUnknown methods.
256  *
257  * This version of QueryInterface will not delegate its implementation
258  * to the outer unknown.
259  */
260 static ULONG WINAPI DefaultHandler_NDIUnknown_AddRef(
261             IUnknown*      iface)
262 {
263   DefaultHandler *This = impl_from_NDIUnknown(iface);
264   return InterlockedIncrement(&This->ref);
265 }
266
267 /************************************************************************
268  * DefaultHandler_NDIUnknown_Release (IUnknown)
269  *
270  * See Windows documentation for more details on IUnknown methods.
271  *
272  * This version of QueryInterface will not delegate its implementation
273  * to the outer unknown.
274  */
275 static ULONG WINAPI DefaultHandler_NDIUnknown_Release(
276             IUnknown*      iface)
277 {
278   DefaultHandler *This = impl_from_NDIUnknown(iface);
279   ULONG ref;
280
281   ref = InterlockedDecrement(&This->ref);
282
283   if (!ref) DefaultHandler_Destroy(This);
284
285   return ref;
286 }
287
288 /*********************************************************
289  * Methods implementation for the IOleObject part of
290  * the DefaultHandler class.
291  */
292
293 /************************************************************************
294  * DefaultHandler_QueryInterface (IUnknown)
295  *
296  * See Windows documentation for more details on IUnknown methods.
297  */
298 static HRESULT WINAPI DefaultHandler_QueryInterface(
299             IOleObject*      iface,
300             REFIID           riid,
301             void**           ppvObject)
302 {
303   DefaultHandler *This = impl_from_IOleObject(iface);
304
305   return IUnknown_QueryInterface(This->outerUnknown, riid, ppvObject);
306 }
307
308 /************************************************************************
309  * DefaultHandler_AddRef (IUnknown)
310  *
311  * See Windows documentation for more details on IUnknown methods.
312  */
313 static ULONG WINAPI DefaultHandler_AddRef(
314             IOleObject*        iface)
315 {
316   DefaultHandler *This = impl_from_IOleObject(iface);
317
318   return IUnknown_AddRef(This->outerUnknown);
319 }
320
321 /************************************************************************
322  * DefaultHandler_Release (IUnknown)
323  *
324  * See Windows documentation for more details on IUnknown methods.
325  */
326 static ULONG WINAPI DefaultHandler_Release(
327             IOleObject*        iface)
328 {
329   DefaultHandler *This = impl_from_IOleObject(iface);
330
331   return IUnknown_Release(This->outerUnknown);
332 }
333
334 /************************************************************************
335  * DefaultHandler_SetClientSite (IOleObject)
336  *
337  * The default handler's implementation of this method only keeps the
338  * client site pointer for future reference.
339  *
340  * See Windows documentation for more details on IOleObject methods.
341  */
342 static HRESULT WINAPI DefaultHandler_SetClientSite(
343             IOleObject*        iface,
344             IOleClientSite*    pClientSite)
345 {
346   DefaultHandler *This = impl_from_IOleObject(iface);
347   HRESULT hr = S_OK;
348
349   TRACE("(%p, %p)\n", iface, pClientSite);
350
351   if (object_is_running(This))
352     hr = IOleObject_SetClientSite(This->pOleDelegate, pClientSite);
353
354   /*
355    * Make sure we release the previous client site if there
356    * was one.
357    */
358   if (This->clientSite)
359     IOleClientSite_Release(This->clientSite);
360
361   This->clientSite = pClientSite;
362
363   if (This->clientSite)
364     IOleClientSite_AddRef(This->clientSite);
365
366   return hr;
367 }
368
369 /************************************************************************
370  * DefaultHandler_GetClientSite (IOleObject)
371  *
372  * The default handler's implementation of this method returns the
373  * last pointer set in IOleObject_SetClientSite.
374  *
375  * See Windows documentation for more details on IOleObject methods.
376  */
377 static HRESULT WINAPI DefaultHandler_GetClientSite(
378             IOleObject*        iface,
379             IOleClientSite**   ppClientSite)
380 {
381   DefaultHandler *This = impl_from_IOleObject(iface);
382
383   if (!ppClientSite)
384     return E_POINTER;
385
386   *ppClientSite = This->clientSite;
387
388   if (This->clientSite)
389     IOleClientSite_AddRef(This->clientSite);
390
391   return S_OK;
392 }
393
394 /************************************************************************
395  * DefaultHandler_SetHostNames (IOleObject)
396  *
397  * The default handler's implementation of this method just stores
398  * the strings and returns S_OK.
399  *
400  * See Windows documentation for more details on IOleObject methods.
401  */
402 static HRESULT WINAPI DefaultHandler_SetHostNames(
403             IOleObject*        iface,
404             LPCOLESTR          szContainerApp,
405             LPCOLESTR          szContainerObj)
406 {
407   DefaultHandler *This = impl_from_IOleObject(iface);
408
409   TRACE("(%p, %s, %s)\n",
410         iface,
411         debugstr_w(szContainerApp),
412         debugstr_w(szContainerObj));
413
414   if (object_is_running(This))
415     IOleObject_SetHostNames(This->pOleDelegate, szContainerApp, szContainerObj);
416
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;
422
423   if (szContainerApp)
424   {
425       if ((This->containerApp = HeapAlloc( GetProcessHeap(), 0,
426                                            (lstrlenW(szContainerApp) + 1) * sizeof(WCHAR) )))
427           strcpyW( This->containerApp, szContainerApp );
428   }
429
430   if (szContainerObj)
431   {
432       if ((This->containerObj = HeapAlloc( GetProcessHeap(), 0,
433                                            (lstrlenW(szContainerObj) + 1) * sizeof(WCHAR) )))
434           strcpyW( This->containerObj, szContainerObj );
435   }
436   return S_OK;
437 }
438
439 static void release_delegates(DefaultHandler *This)
440 {
441     if (This->pDataDelegate)
442     {
443         IDataObject_Release(This->pDataDelegate);
444         This->pDataDelegate = NULL;
445     }
446     if (This->pPSDelegate)
447     {
448         IPersistStorage_Release(This->pPSDelegate);
449         This->pPSDelegate = NULL;
450     }
451     if (This->pOleDelegate)
452     {
453         IOleObject_Release(This->pOleDelegate);
454         This->pOleDelegate = NULL;
455     }
456 }
457
458 /* undoes the work done by DefaultHandler_Run */
459 static void DefaultHandler_Stop(DefaultHandler *This)
460 {
461   if (!object_is_running(This))
462     return;
463
464   IOleObject_Unadvise(This->pOleDelegate, This->dwAdvConn);
465
466   /* FIXME: call IOleCache_OnStop */
467
468   if (This->dataAdviseHolder)
469     DataAdviseHolder_OnDisconnect(This->dataAdviseHolder);
470
471   This->object_state = object_state_not_running;
472 }
473
474 /************************************************************************
475  * DefaultHandler_Close (IOleObject)
476  *
477  * The default handler's implementation of this method is meaningless
478  * without a running server so it does nothing.
479  *
480  * See Windows documentation for more details on IOleObject methods.
481  */
482 static HRESULT WINAPI DefaultHandler_Close(
483             IOleObject*        iface,
484             DWORD              dwSaveOption)
485 {
486   DefaultHandler *This = impl_from_IOleObject(iface);
487   HRESULT hr;
488
489   TRACE("(%d)\n", dwSaveOption);
490
491   if (!object_is_running(This))
492     return S_OK;
493
494   hr = IOleObject_Close(This->pOleDelegate, dwSaveOption);
495
496   DefaultHandler_Stop(This);
497   release_delegates(This);
498
499   return hr;
500 }
501
502 /************************************************************************
503  * DefaultHandler_SetMoniker (IOleObject)
504  *
505  * The default handler's implementation of this method does nothing.
506  *
507  * See Windows documentation for more details on IOleObject methods.
508  */
509 static HRESULT WINAPI DefaultHandler_SetMoniker(
510             IOleObject*        iface,
511             DWORD              dwWhichMoniker,
512             IMoniker*          pmk)
513 {
514   DefaultHandler *This = impl_from_IOleObject(iface);
515
516   TRACE("(%p, %d, %p)\n",
517         iface,
518         dwWhichMoniker,
519         pmk);
520
521   if (object_is_running(This))
522     return IOleObject_SetMoniker(This->pOleDelegate, dwWhichMoniker, pmk);
523
524   return S_OK;
525 }
526
527 /************************************************************************
528  * DefaultHandler_GetMoniker (IOleObject)
529  *
530  * Delegate this request to the client site if we have one.
531  *
532  * See Windows documentation for more details on IOleObject methods.
533  */
534 static HRESULT WINAPI DefaultHandler_GetMoniker(
535             IOleObject*        iface,
536             DWORD              dwAssign,
537             DWORD              dwWhichMoniker,
538             IMoniker**         ppmk)
539 {
540   DefaultHandler *This = impl_from_IOleObject(iface);
541
542   TRACE("(%p, %d, %d, %p)\n",
543         iface, dwAssign, dwWhichMoniker, ppmk);
544
545   if (object_is_running(This))
546     return IOleObject_GetMoniker(This->pOleDelegate, dwAssign, dwWhichMoniker,
547                                  ppmk);
548
549   /* FIXME: dwWhichMoniker == OLEWHICHMK_CONTAINER only? */
550   if (This->clientSite)
551   {
552     return IOleClientSite_GetMoniker(This->clientSite,
553                                      dwAssign,
554                                      dwWhichMoniker,
555                                      ppmk);
556
557   }
558
559   return E_FAIL;
560 }
561
562 /************************************************************************
563  * DefaultHandler_InitFromData (IOleObject)
564  *
565  * This method is meaningless if the server is not running
566  *
567  * See Windows documentation for more details on IOleObject methods.
568  */
569 static HRESULT WINAPI DefaultHandler_InitFromData(
570             IOleObject*        iface,
571             IDataObject*       pDataObject,
572             BOOL               fCreation,
573             DWORD              dwReserved)
574 {
575   DefaultHandler *This = impl_from_IOleObject(iface);
576
577   TRACE("(%p, %p, %d, %d)\n",
578         iface, pDataObject, fCreation, dwReserved);
579
580   if (object_is_running(This))
581     return IOleObject_InitFromData(This->pOleDelegate, pDataObject, fCreation,
582                                    dwReserved);
583   return OLE_E_NOTRUNNING;
584 }
585
586 /************************************************************************
587  * DefaultHandler_GetClipboardData (IOleObject)
588  *
589  * This method is meaningless if the server is not running
590  *
591  * See Windows documentation for more details on IOleObject methods.
592  */
593 static HRESULT WINAPI DefaultHandler_GetClipboardData(
594             IOleObject*        iface,
595             DWORD              dwReserved,
596             IDataObject**      ppDataObject)
597 {
598   DefaultHandler *This = impl_from_IOleObject(iface);
599
600   TRACE("(%p, %d, %p)\n",
601         iface, dwReserved, ppDataObject);
602
603   if (object_is_running(This))
604     return IOleObject_GetClipboardData(This->pOleDelegate, dwReserved,
605                                        ppDataObject);
606
607   return OLE_E_NOTRUNNING;
608 }
609
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)
618 {
619   DefaultHandler *This = impl_from_IOleObject(iface);
620   IRunnableObject *pRunnableObj = (IRunnableObject *)&This->lpvtblIRunnableObject;
621   HRESULT hr;
622
623   TRACE("(%d, %p, %p, %d, %p, %s)\n", iVerb, lpmsg, pActiveSite, lindex, hwndParent, wine_dbgstr_rect(lprcPosRect));
624
625   hr = IRunnableObject_Run(pRunnableObj, NULL);
626   if (FAILED(hr)) return hr;
627
628   return IOleObject_DoVerb(This->pOleDelegate, iVerb, lpmsg, pActiveSite,
629                            lindex, hwndParent, lprcPosRect);
630 }
631
632 /************************************************************************
633  * DefaultHandler_EnumVerbs (IOleObject)
634  *
635  * The default handler implementation of this method simply delegates
636  * to OleRegEnumVerbs
637  *
638  * See Windows documentation for more details on IOleObject methods.
639  */
640 static HRESULT WINAPI DefaultHandler_EnumVerbs(
641             IOleObject*        iface,
642             IEnumOLEVERB**     ppEnumOleVerb)
643 {
644   DefaultHandler *This = impl_from_IOleObject(iface);
645   HRESULT hr = OLE_S_USEREG;
646
647   TRACE("(%p, %p)\n", iface, ppEnumOleVerb);
648
649   if (object_is_running(This))
650     hr = IOleObject_EnumVerbs(This->pOleDelegate, ppEnumOleVerb);
651
652   if (hr == OLE_S_USEREG)
653     return OleRegEnumVerbs(&This->clsid, ppEnumOleVerb);
654   else
655     return hr;
656 }
657
658 static HRESULT WINAPI DefaultHandler_Update(
659             IOleObject*        iface)
660 {
661     DefaultHandler *This = impl_from_IOleObject(iface);
662     TRACE("(%p)\n", iface);
663
664     if (!object_is_running(This))
665     {
666         FIXME("Should run object\n");
667         return E_NOTIMPL;
668     }
669     return IOleObject_Update(This->pOleDelegate);
670 }
671
672 /************************************************************************
673  * DefaultHandler_IsUpToDate (IOleObject)
674  *
675  * This method is meaningless if the server is not running
676  *
677  * See Windows documentation for more details on IOleObject methods.
678  */
679 static HRESULT WINAPI DefaultHandler_IsUpToDate(
680             IOleObject*        iface)
681 {
682     DefaultHandler *This = impl_from_IOleObject(iface);
683     TRACE("(%p)\n", iface);
684
685     if (object_is_running(This))
686         return IOleObject_IsUpToDate(This->pOleDelegate);
687
688     return OLE_E_NOTRUNNING;
689 }
690
691 /************************************************************************
692  * DefaultHandler_GetUserClassID (IOleObject)
693  *
694  * TODO: Map to a new class ID if emulation is active.
695  *
696  * See Windows documentation for more details on IOleObject methods.
697  */
698 static HRESULT WINAPI DefaultHandler_GetUserClassID(
699             IOleObject*        iface,
700             CLSID*             pClsid)
701 {
702   DefaultHandler *This = impl_from_IOleObject(iface);
703
704   TRACE("(%p, %p)\n", iface, pClsid);
705
706   if (object_is_running(This))
707     return IOleObject_GetUserClassID(This->pOleDelegate, pClsid);
708
709   if (!pClsid)
710     return E_POINTER;
711
712   *pClsid = This->clsid;
713
714   return S_OK;
715 }
716
717 /************************************************************************
718  * DefaultHandler_GetUserType (IOleObject)
719  *
720  * The default handler implementation of this method simply delegates
721  * to OleRegGetUserType
722  *
723  * See Windows documentation for more details on IOleObject methods.
724  */
725 static HRESULT WINAPI DefaultHandler_GetUserType(
726             IOleObject*        iface,
727             DWORD              dwFormOfType,
728             LPOLESTR*          pszUserType)
729 {
730   DefaultHandler *This = impl_from_IOleObject(iface);
731
732   TRACE("(%p, %d, %p)\n", iface, dwFormOfType, pszUserType);
733   if (object_is_running(This))
734     return IOleObject_GetUserType(This->pOleDelegate, dwFormOfType, pszUserType);
735
736   return OleRegGetUserType(&This->clsid, dwFormOfType, pszUserType);
737 }
738
739 /************************************************************************
740  * DefaultHandler_SetExtent (IOleObject)
741  *
742  * This method is meaningless if the server is not running
743  *
744  * See Windows documentation for more details on IOleObject methods.
745  */
746 static HRESULT WINAPI DefaultHandler_SetExtent(
747             IOleObject*        iface,
748             DWORD              dwDrawAspect,
749             SIZEL*             psizel)
750 {
751   DefaultHandler *This = impl_from_IOleObject(iface);
752
753   TRACE("(%p, %x, (%d x %d))\n", iface,
754         dwDrawAspect, psizel->cx, psizel->cy);
755
756   if (object_is_running(This))
757     return IOleObject_SetExtent(This->pOleDelegate, dwDrawAspect, psizel);
758
759   return OLE_E_NOTRUNNING;
760 }
761
762 /************************************************************************
763  * DefaultHandler_GetExtent (IOleObject)
764  *
765  * The default handler's implementation of this method returns uses
766  * the cache to locate the aspect and extract the extent from it.
767  *
768  * See Windows documentation for more details on IOleObject methods.
769  */
770 static HRESULT WINAPI DefaultHandler_GetExtent(
771             IOleObject*        iface,
772             DWORD              dwDrawAspect,
773             SIZEL*             psizel)
774 {
775   DVTARGETDEVICE* targetDevice;
776   IViewObject2*   cacheView = NULL;
777   HRESULT         hres;
778
779   DefaultHandler *This = impl_from_IOleObject(iface);
780
781   TRACE("(%p, %x, %p)\n", iface, dwDrawAspect, psizel);
782
783   if (object_is_running(This))
784     return IOleObject_GetExtent(This->pOleDelegate, dwDrawAspect, psizel);
785
786   hres = IUnknown_QueryInterface(This->dataCache, &IID_IViewObject2, (void**)&cacheView);
787   if (FAILED(hres))
788     return E_UNEXPECTED;
789
790   /*
791    * Prepare the call to the cache's GetExtent method.
792    *
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.
797    */
798   targetDevice = NULL;
799
800   hres = IViewObject2_GetExtent(cacheView,
801                                 dwDrawAspect,
802                                 -1,
803                                 targetDevice,
804                                 psizel);
805
806   IViewObject2_Release(cacheView);
807
808   return hres;
809 }
810
811 /************************************************************************
812  * DefaultHandler_Advise (IOleObject)
813  *
814  * The default handler's implementation of this method simply
815  * delegates to the OleAdviseHolder.
816  *
817  * See Windows documentation for more details on IOleObject methods.
818  */
819 static HRESULT WINAPI DefaultHandler_Advise(
820             IOleObject*        iface,
821             IAdviseSink*       pAdvSink,
822             DWORD*             pdwConnection)
823 {
824   HRESULT hres = S_OK;
825   DefaultHandler *This = impl_from_IOleObject(iface);
826
827   TRACE("(%p, %p, %p)\n", iface, pAdvSink, pdwConnection);
828
829   /* Make sure we have an advise holder before we start. */
830   if (!This->oleAdviseHolder)
831     hres = CreateOleAdviseHolder(&This->oleAdviseHolder);
832
833   if (SUCCEEDED(hres))
834     hres = IOleAdviseHolder_Advise(This->oleAdviseHolder,
835                                    pAdvSink,
836                                    pdwConnection);
837
838   return hres;
839 }
840
841 /************************************************************************
842  * DefaultHandler_Unadvise (IOleObject)
843  *
844  * The default handler's implementation of this method simply
845  * delegates to the OleAdviseHolder.
846  *
847  * See Windows documentation for more details on IOleObject methods.
848  */
849 static HRESULT WINAPI DefaultHandler_Unadvise(
850             IOleObject*        iface,
851             DWORD              dwConnection)
852 {
853   DefaultHandler *This = impl_from_IOleObject(iface);
854
855   TRACE("(%p, %d)\n", iface, dwConnection);
856
857   /*
858    * If we don't have an advise holder yet, it means we don't have
859    * a connection.
860    */
861   if (!This->oleAdviseHolder)
862     return OLE_E_NOCONNECTION;
863
864   return IOleAdviseHolder_Unadvise(This->oleAdviseHolder,
865                                    dwConnection);
866 }
867
868 /************************************************************************
869  * DefaultHandler_EnumAdvise (IOleObject)
870  *
871  * The default handler's implementation of this method simply
872  * delegates to the OleAdviseHolder.
873  *
874  * See Windows documentation for more details on IOleObject methods.
875  */
876 static HRESULT WINAPI DefaultHandler_EnumAdvise(
877             IOleObject*        iface,
878             IEnumSTATDATA**    ppenumAdvise)
879 {
880   DefaultHandler *This = impl_from_IOleObject(iface);
881
882   TRACE("(%p, %p)\n", iface, ppenumAdvise);
883
884   if (!ppenumAdvise)
885     return E_POINTER;
886
887   *ppenumAdvise = NULL;
888
889   if (!This->oleAdviseHolder)
890       return S_OK;
891
892   return IOleAdviseHolder_EnumAdvise(This->oleAdviseHolder, ppenumAdvise);
893 }
894
895 /************************************************************************
896  * DefaultHandler_GetMiscStatus (IOleObject)
897  *
898  * The default handler's implementation of this method simply delegates
899  * to OleRegGetMiscStatus.
900  *
901  * See Windows documentation for more details on IOleObject methods.
902  */
903 static HRESULT WINAPI DefaultHandler_GetMiscStatus(
904             IOleObject*        iface,
905             DWORD              dwAspect,
906             DWORD*             pdwStatus)
907 {
908   HRESULT hres;
909   DefaultHandler *This = impl_from_IOleObject(iface);
910
911   TRACE("(%p, %x, %p)\n", iface, dwAspect, pdwStatus);
912
913   if (object_is_running(This))
914     return IOleObject_GetMiscStatus(This->pOleDelegate, dwAspect, pdwStatus);
915
916   hres = OleRegGetMiscStatus(&This->clsid, dwAspect, pdwStatus);
917
918   if (FAILED(hres))
919     *pdwStatus = 0;
920
921   return hres;
922 }
923
924 /************************************************************************
925  * DefaultHandler_SetColorScheme (IOleObject)
926  *
927  * This method is meaningless if the server is not running
928  *
929  * See Windows documentation for more details on IOleObject methods.
930  */
931 static HRESULT WINAPI DefaultHandler_SetColorScheme(
932             IOleObject*           iface,
933             struct tagLOGPALETTE* pLogpal)
934 {
935   DefaultHandler *This = impl_from_IOleObject(iface);
936
937   TRACE("(%p, %p))\n", iface, pLogpal);
938
939   if (object_is_running(This))
940     return IOleObject_SetColorScheme(This->pOleDelegate, pLogpal);
941
942   return OLE_E_NOTRUNNING;
943 }
944
945 /*********************************************************
946  * Methods implementation for the IDataObject part of
947  * the DefaultHandler class.
948  */
949
950 /************************************************************************
951  * DefaultHandler_IDataObject_QueryInterface (IUnknown)
952  *
953  * See Windows documentation for more details on IUnknown methods.
954  */
955 static HRESULT WINAPI DefaultHandler_IDataObject_QueryInterface(
956             IDataObject*     iface,
957            REFIID           riid,
958             void**           ppvObject)
959 {
960   DefaultHandler *This = impl_from_IDataObject(iface);
961
962   return IUnknown_QueryInterface(This->outerUnknown, riid, ppvObject);
963 }
964
965 /************************************************************************
966  * DefaultHandler_IDataObject_AddRef (IUnknown)
967  *
968  * See Windows documentation for more details on IUnknown methods.
969  */
970 static ULONG WINAPI DefaultHandler_IDataObject_AddRef(
971             IDataObject*     iface)
972 {
973   DefaultHandler *This = impl_from_IDataObject(iface);
974
975   return IUnknown_AddRef(This->outerUnknown);
976 }
977
978 /************************************************************************
979  * DefaultHandler_IDataObject_Release (IUnknown)
980  *
981  * See Windows documentation for more details on IUnknown methods.
982  */
983 static ULONG WINAPI DefaultHandler_IDataObject_Release(
984             IDataObject*     iface)
985 {
986   DefaultHandler *This = impl_from_IDataObject(iface);
987
988   return IUnknown_Release(This->outerUnknown);
989 }
990
991 /************************************************************************
992  * DefaultHandler_GetData
993  *
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.
997  */
998 static HRESULT WINAPI DefaultHandler_GetData(
999             IDataObject*     iface,
1000             LPFORMATETC      pformatetcIn,
1001             STGMEDIUM*       pmedium)
1002 {
1003   IDataObject* cacheDataObject = NULL;
1004   HRESULT      hres;
1005
1006   DefaultHandler *This = impl_from_IDataObject(iface);
1007
1008   TRACE("(%p, %p, %p)\n", iface, pformatetcIn, pmedium);
1009
1010   hres = IUnknown_QueryInterface(This->dataCache,
1011                                  &IID_IDataObject,
1012                                  (void**)&cacheDataObject);
1013
1014   if (FAILED(hres))
1015     return E_UNEXPECTED;
1016
1017   hres = IDataObject_GetData(cacheDataObject,
1018                              pformatetcIn,
1019                              pmedium);
1020
1021   IDataObject_Release(cacheDataObject);
1022
1023   if (FAILED(hres) && This->pDataDelegate)
1024     hres = IDataObject_GetData(This->pDataDelegate, pformatetcIn, pmedium);
1025
1026   return hres;
1027 }
1028
1029 static HRESULT WINAPI DefaultHandler_GetDataHere(
1030             IDataObject*     iface,
1031             LPFORMATETC      pformatetc,
1032             STGMEDIUM*       pmedium)
1033 {
1034   FIXME(": Stub\n");
1035   return E_NOTIMPL;
1036 }
1037
1038 /************************************************************************
1039  * DefaultHandler_QueryGetData (IDataObject)
1040  *
1041  * The default handler's implementation of this method delegates to
1042  * the cache.
1043  *
1044  * See Windows documentation for more details on IDataObject methods.
1045  */
1046 static HRESULT WINAPI DefaultHandler_QueryGetData(
1047             IDataObject*     iface,
1048             LPFORMATETC      pformatetc)
1049 {
1050   IDataObject* cacheDataObject = NULL;
1051   HRESULT      hres;
1052
1053   DefaultHandler *This = impl_from_IDataObject(iface);
1054
1055   TRACE("(%p, %p)\n", iface, pformatetc);
1056
1057   hres = IUnknown_QueryInterface(This->dataCache,
1058                                  &IID_IDataObject,
1059                                  (void**)&cacheDataObject);
1060
1061   if (FAILED(hres))
1062     return E_UNEXPECTED;
1063
1064   hres = IDataObject_QueryGetData(cacheDataObject,
1065                                   pformatetc);
1066
1067   IDataObject_Release(cacheDataObject);
1068
1069   if (FAILED(hres) && This->pDataDelegate)
1070     hres = IDataObject_QueryGetData(This->pDataDelegate, pformatetc);
1071
1072   return hres;
1073 }
1074
1075 /************************************************************************
1076  * DefaultHandler_GetCanonicalFormatEtc (IDataObject)
1077  *
1078  * This method is meaningless if the server is not running
1079  *
1080  * See Windows documentation for more details on IDataObject methods.
1081  */
1082 static HRESULT WINAPI DefaultHandler_GetCanonicalFormatEtc(
1083             IDataObject*     iface,
1084             LPFORMATETC      pformatetcIn,
1085             LPFORMATETC      pformatetcOut)
1086 {
1087   DefaultHandler *This = impl_from_IDataObject(iface);
1088
1089   TRACE("(%p, %p, %p)\n", iface, pformatetcIn, pformatetcOut);
1090
1091   if (!This->pDataDelegate)
1092     return OLE_E_NOTRUNNING;
1093
1094   return IDataObject_GetCanonicalFormatEtc(This->pDataDelegate, pformatetcIn, pformatetcOut);
1095 }
1096
1097 /************************************************************************
1098  * DefaultHandler_SetData (IDataObject)
1099  *
1100  * The default handler's implementation of this method delegates to
1101  * the cache.
1102  *
1103  * See Windows documentation for more details on IDataObject methods.
1104  */
1105 static HRESULT WINAPI DefaultHandler_SetData(
1106             IDataObject*     iface,
1107             LPFORMATETC      pformatetc,
1108             STGMEDIUM*       pmedium,
1109             BOOL             fRelease)
1110 {
1111   DefaultHandler *This = impl_from_IDataObject(iface);
1112   IDataObject* cacheDataObject = NULL;
1113   HRESULT      hres;
1114
1115   TRACE("(%p, %p, %p, %d)\n", iface, pformatetc, pmedium, fRelease);
1116
1117   hres = IUnknown_QueryInterface(This->dataCache,
1118                                  &IID_IDataObject,
1119                                  (void**)&cacheDataObject);
1120
1121   if (FAILED(hres))
1122     return E_UNEXPECTED;
1123
1124   hres = IDataObject_SetData(cacheDataObject,
1125                              pformatetc,
1126                              pmedium,
1127                              fRelease);
1128
1129   IDataObject_Release(cacheDataObject);
1130
1131   return hres;
1132 }
1133
1134 /************************************************************************
1135  * DefaultHandler_EnumFormatEtc (IDataObject)
1136  *
1137  * The default handler's implementation of This method simply delegates
1138  * to OleRegEnumFormatEtc.
1139  *
1140  * See Windows documentation for more details on IDataObject methods.
1141  */
1142 static HRESULT WINAPI DefaultHandler_EnumFormatEtc(
1143             IDataObject*     iface,
1144             DWORD            dwDirection,
1145             IEnumFORMATETC** ppenumFormatEtc)
1146 {
1147   DefaultHandler *This = impl_from_IDataObject(iface);
1148
1149   TRACE("(%p, %x, %p)\n", iface, dwDirection, ppenumFormatEtc);
1150
1151   return OleRegEnumFormatEtc(&This->clsid, dwDirection, ppenumFormatEtc);
1152 }
1153
1154 /************************************************************************
1155  * DefaultHandler_DAdvise (IDataObject)
1156  *
1157  * The default handler's implementation of this method simply
1158  * delegates to the DataAdviseHolder.
1159  *
1160  * See Windows documentation for more details on IDataObject methods.
1161  */
1162 static HRESULT WINAPI DefaultHandler_DAdvise(
1163             IDataObject*     iface,
1164             FORMATETC*       pformatetc,
1165             DWORD            advf,
1166             IAdviseSink*     pAdvSink,
1167             DWORD*           pdwConnection)
1168 {
1169   HRESULT hres = S_OK;
1170   DefaultHandler *This = impl_from_IDataObject(iface);
1171
1172   TRACE("(%p, %p, %d, %p, %p)\n",
1173         iface, pformatetc, advf, pAdvSink, pdwConnection);
1174
1175   /* Make sure we have a data advise holder before we start. */
1176   if (!This->dataAdviseHolder)
1177   {
1178     hres = CreateDataAdviseHolder(&This->dataAdviseHolder);
1179     if (SUCCEEDED(hres) && This->pDataDelegate)
1180       DataAdviseHolder_OnConnect(This->dataAdviseHolder, This->pDataDelegate);
1181   }
1182
1183   if (SUCCEEDED(hres))
1184     hres = IDataAdviseHolder_Advise(This->dataAdviseHolder,
1185                                     iface,
1186                                     pformatetc,
1187                                     advf,
1188                                     pAdvSink,
1189                                     pdwConnection);
1190
1191   return hres;
1192 }
1193
1194 /************************************************************************
1195  * DefaultHandler_DUnadvise (IDataObject)
1196  *
1197  * The default handler's implementation of this method simply
1198  * delegates to the DataAdviseHolder.
1199  *
1200  * See Windows documentation for more details on IDataObject methods.
1201  */
1202 static HRESULT WINAPI DefaultHandler_DUnadvise(
1203             IDataObject*     iface,
1204             DWORD            dwConnection)
1205 {
1206   DefaultHandler *This = impl_from_IDataObject(iface);
1207
1208   TRACE("(%p, %d)\n", iface, dwConnection);
1209
1210   /*
1211    * If we don't have a data advise holder yet, it means that
1212    * we don't have any connections..
1213    */
1214   if (!This->dataAdviseHolder)
1215     return OLE_E_NOCONNECTION;
1216
1217   return IDataAdviseHolder_Unadvise(This->dataAdviseHolder,
1218                                     dwConnection);
1219 }
1220
1221 /************************************************************************
1222  * DefaultHandler_EnumDAdvise (IDataObject)
1223  *
1224  * The default handler's implementation of this method simply
1225  * delegates to the DataAdviseHolder.
1226  *
1227  * See Windows documentation for more details on IDataObject methods.
1228  */
1229 static HRESULT WINAPI DefaultHandler_EnumDAdvise(
1230             IDataObject*     iface,
1231             IEnumSTATDATA**  ppenumAdvise)
1232 {
1233   DefaultHandler *This = impl_from_IDataObject(iface);
1234
1235   TRACE("(%p, %p)\n", iface, ppenumAdvise);
1236
1237   if (!ppenumAdvise)
1238     return E_POINTER;
1239
1240   *ppenumAdvise = NULL;
1241
1242   /* If we have a data advise holder object, delegate. */
1243   if (This->dataAdviseHolder)
1244     return IDataAdviseHolder_EnumAdvise(This->dataAdviseHolder,
1245                                         ppenumAdvise);
1246
1247   return S_OK;
1248 }
1249
1250 /*********************************************************
1251  * Methods implementation for the IRunnableObject part
1252  * of the DefaultHandler class.
1253  */
1254
1255 /************************************************************************
1256  * DefaultHandler_IRunnableObject_QueryInterface (IUnknown)
1257  *
1258  * See Windows documentation for more details on IUnknown methods.
1259  */
1260 static HRESULT WINAPI DefaultHandler_IRunnableObject_QueryInterface(
1261             IRunnableObject*     iface,
1262             REFIID               riid,
1263             void**               ppvObject)
1264 {
1265   DefaultHandler *This = impl_from_IRunnableObject(iface);
1266
1267   return IUnknown_QueryInterface(This->outerUnknown, riid, ppvObject);
1268 }
1269
1270 /************************************************************************
1271  * DefaultHandler_IRunnableObject_AddRef (IUnknown)
1272  *
1273  * See Windows documentation for more details on IUnknown methods.
1274  */
1275 static ULONG WINAPI DefaultHandler_IRunnableObject_AddRef(
1276             IRunnableObject*     iface)
1277 {
1278   DefaultHandler *This = impl_from_IRunnableObject(iface);
1279
1280   return IUnknown_AddRef(This->outerUnknown);
1281 }
1282
1283 /************************************************************************
1284  * DefaultHandler_IRunnableObject_Release (IUnknown)
1285  *
1286  * See Windows documentation for more details on IUnknown methods.
1287  */
1288 static ULONG WINAPI DefaultHandler_IRunnableObject_Release(
1289             IRunnableObject*     iface)
1290 {
1291   DefaultHandler *This = impl_from_IRunnableObject(iface);
1292
1293   return IUnknown_Release(This->outerUnknown);
1294 }
1295
1296 /************************************************************************
1297  * DefaultHandler_GetRunningClass (IRunnableObject)
1298  *
1299  * See Windows documentation for more details on IRunnableObject methods.
1300  */
1301 static HRESULT WINAPI DefaultHandler_GetRunningClass(
1302             IRunnableObject*     iface,
1303             LPCLSID              lpClsid)
1304 {
1305   FIXME("()\n");
1306   return S_OK;
1307 }
1308
1309 static HRESULT WINAPI DefaultHandler_Run(
1310             IRunnableObject*     iface,
1311             IBindCtx*            pbc)
1312 {
1313   DefaultHandler *This = impl_from_IRunnableObject(iface);
1314   HRESULT hr;
1315
1316   FIXME("(%p): semi-stub\n", pbc);
1317
1318   /* already running? if so nothing to do */
1319   if (object_is_running(This))
1320     return S_OK;
1321
1322   release_delegates(This);
1323
1324   hr = CoCreateInstance(&This->clsid, NULL, CLSCTX_LOCAL_SERVER,
1325                         &IID_IOleObject, (void **)&This->pOleDelegate);
1326   if (FAILED(hr))
1327     return hr;
1328
1329   This->object_state = object_state_running;
1330
1331   hr = IOleObject_Advise(This->pOleDelegate,
1332                          (IAdviseSink *)&This->lpvtblIAdviseSink,
1333                          &This->dwAdvConn);
1334
1335   if (SUCCEEDED(hr) && This->clientSite)
1336     hr = IOleObject_SetClientSite(This->pOleDelegate, This->clientSite);
1337
1338   if (SUCCEEDED(hr))
1339   {
1340     IOleObject_QueryInterface(This->pOleDelegate, &IID_IPersistStorage,
1341                               (void **)&This->pPSDelegate);
1342     if (This->pPSDelegate)
1343     {
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);
1348     }
1349   }
1350
1351   if (SUCCEEDED(hr) && This->containerApp)
1352     hr = IOleObject_SetHostNames(This->pOleDelegate, This->containerApp,
1353                                  This->containerObj);
1354
1355   /* FIXME: do more stuff here:
1356    * - IOleObject_GetMiscStatus
1357    * - IOleObject_GetMoniker
1358    * - IOleCache_OnRun
1359    */
1360
1361   if (SUCCEEDED(hr))
1362     hr = IOleObject_QueryInterface(This->pOleDelegate, &IID_IDataObject,
1363                                    (void **)&This->pDataDelegate);
1364
1365   if (SUCCEEDED(hr) && This->dataAdviseHolder)
1366     hr = DataAdviseHolder_OnConnect(This->dataAdviseHolder, This->pDataDelegate);
1367
1368   if (FAILED(hr))
1369   {
1370     DefaultHandler_Stop(This);
1371     release_delegates(This);
1372   }
1373
1374   return hr;
1375 }
1376
1377 /************************************************************************
1378  * DefaultHandler_IsRunning (IRunnableObject)
1379  *
1380  * See Windows documentation for more details on IRunnableObject methods.
1381  */
1382 static BOOL    WINAPI DefaultHandler_IsRunning(
1383             IRunnableObject*     iface)
1384 {
1385   DefaultHandler *This = impl_from_IRunnableObject(iface);
1386
1387   TRACE("()\n");
1388
1389   if (This->object_state == object_state_running)
1390     return TRUE;
1391   else
1392     return FALSE;
1393 }
1394
1395 /************************************************************************
1396  * DefaultHandler_LockRunning (IRunnableObject)
1397  *
1398  * See Windows documentation for more details on IRunnableObject methods.
1399  */
1400 static HRESULT WINAPI DefaultHandler_LockRunning(
1401             IRunnableObject*     iface,
1402             BOOL                 fLock,
1403             BOOL                 fLastUnlockCloses)
1404 {
1405   FIXME("()\n");
1406   return S_OK;
1407 }
1408
1409 /************************************************************************
1410  * DefaultHandler_SetContainedObject (IRunnableObject)
1411  *
1412  * See Windows documentation for more details on IRunnableObject methods.
1413  */
1414 static HRESULT WINAPI DefaultHandler_SetContainedObject(
1415             IRunnableObject*     iface,
1416             BOOL                 fContained)
1417 {
1418   FIXME("()\n");
1419   return S_OK;
1420 }
1421
1422 static HRESULT WINAPI DefaultHandler_IAdviseSink_QueryInterface(
1423     IAdviseSink *iface,
1424     REFIID riid,
1425     void **ppvObject)
1426 {
1427     if (IsEqualIID(riid, &IID_IUnknown) ||
1428         IsEqualIID(riid, &IID_IAdviseSink))
1429     {
1430         *ppvObject = iface;
1431         IAdviseSink_AddRef(iface);
1432         return S_OK;
1433     }
1434
1435     return E_NOINTERFACE;
1436 }
1437
1438 static ULONG WINAPI DefaultHandler_IAdviseSink_AddRef(
1439     IAdviseSink *iface)
1440 {
1441     DefaultHandler *This = impl_from_IAdviseSink(iface);
1442
1443     return IUnknown_AddRef((IUnknown *)&This->lpvtblIUnknown);
1444 }
1445
1446 static ULONG WINAPI DefaultHandler_IAdviseSink_Release(
1447             IAdviseSink *iface)
1448 {
1449     DefaultHandler *This = impl_from_IAdviseSink(iface);
1450
1451     return IUnknown_Release((IUnknown *)&This->lpvtblIUnknown);
1452 }
1453
1454 static void WINAPI DefaultHandler_IAdviseSink_OnDataChange(
1455     IAdviseSink *iface,
1456     FORMATETC *pFormatetc,
1457     STGMEDIUM *pStgmed)
1458 {
1459     FIXME(": stub\n");
1460 }
1461
1462 static void WINAPI DefaultHandler_IAdviseSink_OnViewChange(
1463     IAdviseSink *iface,
1464     DWORD dwAspect,
1465     LONG lindex)
1466 {
1467     FIXME(": stub\n");
1468 }
1469
1470 static void WINAPI DefaultHandler_IAdviseSink_OnRename(
1471     IAdviseSink *iface,
1472     IMoniker *pmk)
1473 {
1474     DefaultHandler *This = impl_from_IAdviseSink(iface);
1475
1476     TRACE("(%p)\n", pmk);
1477
1478     if (This->oleAdviseHolder)
1479         IOleAdviseHolder_SendOnRename(This->oleAdviseHolder, pmk);
1480 }
1481
1482 static void WINAPI DefaultHandler_IAdviseSink_OnSave(
1483     IAdviseSink *iface)
1484 {
1485     DefaultHandler *This = impl_from_IAdviseSink(iface);
1486
1487     TRACE("()\n");
1488
1489     if (This->oleAdviseHolder)
1490         IOleAdviseHolder_SendOnSave(This->oleAdviseHolder);
1491 }
1492
1493 static void WINAPI DefaultHandler_IAdviseSink_OnClose(
1494     IAdviseSink *iface)
1495 {
1496     DefaultHandler *This = impl_from_IAdviseSink(iface);
1497     
1498     TRACE("()\n");
1499
1500     if (This->oleAdviseHolder)
1501         IOleAdviseHolder_SendOnClose(This->oleAdviseHolder);
1502
1503     DefaultHandler_Stop(This);
1504 }
1505
1506
1507 /************************************************************************
1508  * DefaultHandler_IPersistStorage_QueryInterface
1509  *
1510  */
1511 static HRESULT WINAPI DefaultHandler_IPersistStorage_QueryInterface(
1512             IPersistStorage*     iface,
1513             REFIID               riid,
1514             void**               ppvObject)
1515 {
1516   DefaultHandler *This = impl_from_IPersistStorage(iface);
1517
1518   return IUnknown_QueryInterface(This->outerUnknown, riid, ppvObject);
1519 }
1520
1521 /************************************************************************
1522  * DefaultHandler_IPersistStorage_AddRef
1523  *
1524  */
1525 static ULONG WINAPI DefaultHandler_IPersistStorage_AddRef(
1526             IPersistStorage*     iface)
1527 {
1528   DefaultHandler *This = impl_from_IPersistStorage(iface);
1529
1530   return IUnknown_AddRef(This->outerUnknown);
1531 }
1532
1533 /************************************************************************
1534  * DefaultHandler_IPersistStorage_Release
1535  *
1536  */
1537 static ULONG WINAPI DefaultHandler_IPersistStorage_Release(
1538             IPersistStorage*     iface)
1539 {
1540   DefaultHandler *This = impl_from_IPersistStorage(iface);
1541
1542   return IUnknown_Release(This->outerUnknown);
1543 }
1544
1545 /************************************************************************
1546  * DefaultHandler_IPersistStorage_GetClassID
1547  *
1548  */
1549 static HRESULT WINAPI DefaultHandler_IPersistStorage_GetClassID(
1550             IPersistStorage*     iface,
1551             CLSID*               clsid)
1552 {
1553     DefaultHandler *This = impl_from_IPersistStorage(iface);
1554     HRESULT hr;
1555
1556     TRACE("(%p)->(%p)\n", iface, clsid);
1557
1558     if(object_is_running(This))
1559         hr = IPersistStorage_GetClassID(This->pPSDelegate, clsid);
1560     else
1561         hr = IPersistStorage_GetClassID(This->dataCache_PersistStg, clsid);
1562
1563     return hr;
1564 }
1565
1566 /************************************************************************
1567  * DefaultHandler_IPersistStorage_IsDirty
1568  *
1569  */
1570 static HRESULT WINAPI DefaultHandler_IPersistStorage_IsDirty(
1571             IPersistStorage*     iface)
1572 {
1573     DefaultHandler *This = impl_from_IPersistStorage(iface);
1574     HRESULT hr;
1575
1576     TRACE("(%p)\n", iface);
1577
1578     hr = IPersistStorage_IsDirty(This->dataCache_PersistStg);
1579     if(hr != S_FALSE) return hr;
1580
1581     if(object_is_running(This))
1582         hr = IPersistStorage_IsDirty(This->pPSDelegate);
1583
1584     return hr;
1585 }
1586
1587 /***********************************************************************
1588  *   init_ole_stream
1589  *
1590  * Creates the '\1Ole' stream.
1591  * The format of this stream is as follows:
1592  *
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].
1598  *
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
1603  */
1604
1605 static const WCHAR OleStream[] = {1,'O','l','e',0};
1606 typedef struct
1607 {
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;
1615
1616 static void init_ole_stream(IStorage *storage)
1617 {
1618     HRESULT hr;
1619     IStream *stream;
1620
1621     hr = IStorage_CreateStream(storage, OleStream, STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stream);
1622     if(SUCCEEDED(hr))
1623     {
1624         DWORD written;
1625         ole_stream_header_t header;
1626
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;
1632
1633         IStream_Write(stream, &header, sizeof(header), &written);
1634         IStream_Release(stream);
1635     }
1636     return;
1637 }
1638
1639 static HRESULT load_ole_stream(DefaultHandler *This, IStorage *storage)
1640 {
1641     IStream *stream;
1642     HRESULT hr;
1643
1644     hr = IStorage_OpenStream(storage, OleStream, NULL, STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stream);
1645
1646     if(SUCCEEDED(hr))
1647     {
1648         DWORD read;
1649         ole_stream_header_t header;
1650
1651         hr = IStream_Read(stream, &header, sizeof(header), &read);
1652         if(hr == S_OK && read == sizeof(header) && header.version == ole_stream_version)
1653         {
1654             if(header.flags & 1)
1655             {
1656                 /* FIXME: Read the moniker and deal with the link */
1657                 FIXME("Linked objects are not supported yet\n");
1658             }
1659         }
1660         else
1661         {
1662             WARN("Incorrect OleStream header\n");
1663             hr = DV_E_CLIPFORMAT;
1664         }
1665         IStream_Release(stream);
1666     }
1667     else
1668     {
1669         init_ole_stream(storage);
1670         hr = S_OK;
1671     }
1672     return hr;
1673 }
1674
1675 /************************************************************************
1676  * DefaultHandler_IPersistStorage_InitNew
1677  *
1678  */
1679 static HRESULT WINAPI DefaultHandler_IPersistStorage_InitNew(
1680            IPersistStorage*     iface,
1681            IStorage*            pStg)
1682 {
1683     DefaultHandler *This = impl_from_IPersistStorage(iface);
1684     HRESULT hr;
1685
1686     TRACE("(%p)->(%p)\n", iface, pStg);
1687     init_ole_stream(pStg);
1688
1689     hr = IPersistStorage_InitNew(This->dataCache_PersistStg, pStg);
1690
1691     if(SUCCEEDED(hr) && object_is_running(This))
1692         hr = IPersistStorage_InitNew(This->pPSDelegate, pStg);
1693
1694     if(SUCCEEDED(hr))
1695     {
1696         IStorage_AddRef(pStg);
1697         This->storage = pStg;
1698         This->storage_state = storage_state_initialised;
1699     }
1700
1701     return hr;
1702 }
1703
1704
1705 /************************************************************************
1706  * DefaultHandler_IPersistStorage_Load
1707  *
1708  */
1709 static HRESULT WINAPI DefaultHandler_IPersistStorage_Load(
1710            IPersistStorage*     iface,
1711            IStorage*            pStg)
1712 {
1713     DefaultHandler *This = impl_from_IPersistStorage(iface);
1714     HRESULT hr;
1715
1716     TRACE("(%p)->(%p)\n", iface, pStg);
1717
1718     hr = load_ole_stream(This, pStg);
1719
1720     if(SUCCEEDED(hr))
1721         hr = IPersistStorage_Load(This->dataCache_PersistStg, pStg);
1722
1723     if(SUCCEEDED(hr) && object_is_running(This))
1724         hr = IPersistStorage_Load(This->pPSDelegate, pStg);
1725
1726     if(SUCCEEDED(hr))
1727     {
1728         IStorage_AddRef(pStg);
1729         This->storage = pStg;
1730         This->storage_state = storage_state_loaded;
1731     }
1732     return hr;
1733 }
1734
1735
1736 /************************************************************************
1737  * DefaultHandler_IPersistStorage_Save
1738  *
1739  */
1740 static HRESULT WINAPI DefaultHandler_IPersistStorage_Save(
1741            IPersistStorage*     iface,
1742            IStorage*            pStgSave,
1743            BOOL                 fSameAsLoad)
1744 {
1745     DefaultHandler *This = impl_from_IPersistStorage(iface);
1746     HRESULT hr;
1747
1748     TRACE("(%p)->(%p, %d)\n", iface, pStgSave, fSameAsLoad);
1749
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);
1753
1754     return hr;
1755 }
1756
1757
1758 /************************************************************************
1759  * DefaultHandler_IPersistStorage_SaveCompleted
1760  *
1761  */
1762 static HRESULT WINAPI DefaultHandler_IPersistStorage_SaveCompleted(
1763            IPersistStorage*     iface,
1764            IStorage*            pStgNew)
1765 {
1766     DefaultHandler *This = impl_from_IPersistStorage(iface);
1767     HRESULT hr;
1768
1769     TRACE("(%p)->(%p)\n", iface, pStgNew);
1770
1771     hr = IPersistStorage_SaveCompleted(This->dataCache_PersistStg, pStgNew);
1772
1773     if(SUCCEEDED(hr) && object_is_running(This))
1774         hr = IPersistStorage_SaveCompleted(This->pPSDelegate, pStgNew);
1775
1776     if(pStgNew)
1777     {
1778         IStorage_AddRef(pStgNew);
1779         if(This->storage) IStorage_Release(This->storage);
1780         This->storage = pStgNew;
1781         This->storage_state = storage_state_loaded;
1782     }
1783
1784     return hr;
1785 }
1786
1787
1788 /************************************************************************
1789  * DefaultHandler_IPersistStorage_HandsOffStorage
1790  *
1791  */
1792 static HRESULT WINAPI DefaultHandler_IPersistStorage_HandsOffStorage(
1793             IPersistStorage*     iface)
1794 {
1795     DefaultHandler *This = impl_from_IPersistStorage(iface);
1796     HRESULT hr;
1797
1798     TRACE("(%p)\n", iface);
1799
1800     hr = IPersistStorage_HandsOffStorage(This->dataCache_PersistStg);
1801
1802     if(SUCCEEDED(hr) && object_is_running(This))
1803         hr = IPersistStorage_HandsOffStorage(This->pPSDelegate);
1804
1805     if(This->storage) IStorage_Release(This->storage);
1806     This->storage = NULL;
1807     This->storage_state = storage_state_uninitialised;
1808
1809     return hr;
1810 }
1811
1812
1813 /*
1814  * Virtual function tables for the DefaultHandler class.
1815  */
1816 static const IOleObjectVtbl DefaultHandler_IOleObject_VTable =
1817 {
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
1842 };
1843
1844 static const IUnknownVtbl DefaultHandler_NDIUnknown_VTable =
1845 {
1846   DefaultHandler_NDIUnknown_QueryInterface,
1847   DefaultHandler_NDIUnknown_AddRef,
1848   DefaultHandler_NDIUnknown_Release,
1849 };
1850
1851 static const IDataObjectVtbl DefaultHandler_IDataObject_VTable =
1852 {
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
1865 };
1866
1867 static const IRunnableObjectVtbl DefaultHandler_IRunnableObject_VTable =
1868 {
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
1877 };
1878
1879 static const IAdviseSinkVtbl DefaultHandler_IAdviseSink_VTable =
1880 {
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
1889 };
1890
1891 static const IPersistStorageVtbl DefaultHandler_IPersistStorage_VTable =
1892 {
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
1903 };
1904
1905 /*********************************************************
1906  * Methods implementation for the DefaultHandler class.
1907  */
1908 static DefaultHandler* DefaultHandler_Construct(
1909   REFCLSID  clsid,
1910   LPUNKNOWN pUnkOuter,
1911   DWORD flags,
1912   IClassFactory *pCF)
1913 {
1914   DefaultHandler* This = NULL;
1915   HRESULT hr;
1916
1917   This = HeapAlloc(GetProcessHeap(), 0, sizeof(DefaultHandler));
1918
1919   if (!This)
1920     return This;
1921
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;
1928
1929   This->inproc_server = (flags & EMBDHLP_INPROC_SERVER) ? TRUE : FALSE;
1930
1931   /*
1932    * Start with one reference count. The caller of this function
1933    * must release the interface pointer when it is done.
1934    */
1935   This->ref = 1;
1936
1937   /*
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.
1942    */
1943   if (!pUnkOuter)
1944     pUnkOuter = (IUnknown*)&This->lpvtblIUnknown;
1945
1946   This->outerUnknown = pUnkOuter;
1947
1948   /*
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.
1952    */
1953   hr = CreateDataCache(This->outerUnknown,
1954                        clsid,
1955                        &IID_IUnknown,
1956                        (void**)&This->dataCache);
1957   if(SUCCEEDED(hr))
1958   {
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);
1966   }
1967   if(FAILED(hr))
1968   {
1969     ERR("Unexpected error creating data cache\n");
1970     HeapFree(GetProcessHeap(), 0, This);
1971     return NULL;
1972   }
1973
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;
1984
1985   This->dwAdvConn = 0;
1986   This->storage = NULL;
1987   This->storage_state = storage_state_uninitialised;
1988
1989   if (This->inproc_server && !(flags & EMBDHLP_DELAYCREATE))
1990   {
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);
2006   }
2007   else
2008   {
2009     This->pCFObject = pCF;
2010     if (pCF) IClassFactory_AddRef(pCF);
2011   }
2012
2013   return This;
2014 }
2015
2016 static void DefaultHandler_Destroy(
2017   DefaultHandler* This)
2018 {
2019   TRACE("(%p)\n", This);
2020
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;
2025
2026   /* release delegates */
2027   DefaultHandler_Stop(This);
2028   release_delegates(This);
2029
2030   HeapFree( GetProcessHeap(), 0, This->containerApp );
2031   This->containerApp = NULL;
2032   HeapFree( GetProcessHeap(), 0, This->containerObj );
2033   This->containerObj = NULL;
2034
2035   if (This->dataCache)
2036   {
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;
2044   }
2045
2046   if (This->clientSite)
2047   {
2048     IOleClientSite_Release(This->clientSite);
2049     This->clientSite = NULL;
2050   }
2051
2052   if (This->oleAdviseHolder)
2053   {
2054     IOleAdviseHolder_Release(This->oleAdviseHolder);
2055     This->oleAdviseHolder = NULL;
2056   }
2057
2058   if (This->dataAdviseHolder)
2059   {
2060     IDataAdviseHolder_Release(This->dataAdviseHolder);
2061     This->dataAdviseHolder = NULL;
2062   }
2063
2064   if (This->storage)
2065   {
2066     IStorage_Release(This->storage);
2067     This->storage = NULL;
2068   }
2069
2070   if (This->pCFObject)
2071   {
2072     IClassFactory_Release(This->pCFObject);
2073     This->pCFObject = NULL;
2074   }
2075
2076   HeapFree(GetProcessHeap(), 0, This);
2077 }
2078
2079 /******************************************************************************
2080  * OleCreateEmbeddingHelper [OLE32.@]
2081  */
2082 HRESULT WINAPI OleCreateEmbeddingHelper(
2083   REFCLSID  clsid,
2084   LPUNKNOWN pUnkOuter,
2085   DWORD     flags,
2086   IClassFactory *pCF,
2087   REFIID    riid,
2088   LPVOID*   ppvObj)
2089 {
2090   DefaultHandler* newHandler = NULL;
2091   HRESULT         hr         = S_OK;
2092
2093   TRACE("(%s, %p, %08x, %p, %s, %p)\n", debugstr_guid(clsid), pUnkOuter, flags, pCF, debugstr_guid(riid), ppvObj);
2094
2095   if (!ppvObj)
2096     return E_POINTER;
2097
2098   *ppvObj = NULL;
2099
2100   /*
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.
2105    */
2106   if (pUnkOuter && !IsEqualIID(&IID_IUnknown, riid))
2107     return CLASS_E_NOAGGREGATION;
2108
2109   /*
2110    * Try to construct a new instance of the class.
2111    */
2112   newHandler = DefaultHandler_Construct(clsid, pUnkOuter, flags, pCF);
2113
2114   if (!newHandler)
2115     return E_OUTOFMEMORY;
2116
2117   /*
2118    * Make sure it supports the interface required by the caller.
2119    */
2120   hr = IUnknown_QueryInterface((IUnknown*)&newHandler->lpvtblIUnknown, riid, ppvObj);
2121
2122   /*
2123    * Release the reference obtained in the constructor. If
2124    * the QueryInterface was unsuccessful, it will free the class.
2125    */
2126   IUnknown_Release((IUnknown*)&newHandler->lpvtblIUnknown);
2127
2128   return hr;
2129 }
2130
2131
2132 /******************************************************************************
2133  * OleCreateDefaultHandler [OLE32.@]
2134  */
2135 HRESULT WINAPI OleCreateDefaultHandler(REFCLSID clsid, LPUNKNOWN pUnkOuter,
2136                                        REFIID riid, LPVOID* ppvObj)
2137 {
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);
2141 }
2142
2143 typedef struct HandlerCF
2144 {
2145     const IClassFactoryVtbl *lpVtbl;
2146     LONG refs;
2147     CLSID clsid;
2148 } HandlerCF;
2149
2150 static HRESULT WINAPI
2151 HandlerCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid, LPVOID *ppv)
2152 {
2153     *ppv = NULL;
2154     if (IsEqualIID(riid,&IID_IUnknown) ||
2155         IsEqualIID(riid,&IID_IClassFactory))
2156     {
2157         *ppv = iface;
2158         IClassFactory_AddRef(iface);
2159         return S_OK;
2160     }
2161     return E_NOINTERFACE;
2162 }
2163
2164 static ULONG WINAPI HandlerCF_AddRef(LPCLASSFACTORY iface)
2165 {
2166     HandlerCF *This = (HandlerCF *)iface;
2167     return InterlockedIncrement(&This->refs);
2168 }
2169
2170 static ULONG WINAPI HandlerCF_Release(LPCLASSFACTORY iface)
2171 {
2172     HandlerCF *This = (HandlerCF *)iface;
2173     ULONG refs = InterlockedDecrement(&This->refs);
2174     if (!refs)
2175         HeapFree(GetProcessHeap(), 0, This);
2176     return refs;
2177 }
2178
2179 static HRESULT WINAPI
2180 HandlerCF_CreateInstance(LPCLASSFACTORY iface, LPUNKNOWN pUnk,
2181                          REFIID riid, LPVOID *ppv)
2182 {
2183     HandlerCF *This = (HandlerCF *)iface;
2184     return OleCreateDefaultHandler(&This->clsid, pUnk, riid, ppv);
2185 }
2186
2187 static HRESULT WINAPI HandlerCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
2188 {
2189     FIXME("(%d), stub!\n",fLock);
2190     return S_OK;
2191 }
2192
2193 static const IClassFactoryVtbl HandlerClassFactoryVtbl = {
2194     HandlerCF_QueryInterface,
2195     HandlerCF_AddRef,
2196     HandlerCF_Release,
2197     HandlerCF_CreateInstance,
2198     HandlerCF_LockServer
2199 };
2200
2201 HRESULT HandlerCF_Create(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
2202 {
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;
2209
2210     hr = IUnknown_QueryInterface((IUnknown *)&This->lpVtbl, riid, ppv);
2211     if (FAILED(hr))
2212         HeapFree(GetProcessHeap(), 0, This);
2213
2214     return hr;
2215 }