Initialize clsid member to the marshaler clsid to fix custom
[wine/multimedia.git] / dlls / ole32 / marshal.c
blob8c4264ffda07ef33af76364374c4d807873ad2fe
1 /*
2 * Marshalling library
4 * Copyright 2002 Marcus Meissner
5 * Copyright 2004 Mike Hearn, for CodeWeavers
6 * Copyright 2004 Rob Shearman, for CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "config.h"
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <assert.h>
31 #define COBJMACROS
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winuser.h"
36 #include "objbase.h"
37 #include "ole2.h"
38 #include "rpc.h"
39 #include "winerror.h"
40 #include "winreg.h"
41 #include "wtypes.h"
42 #include "wine/unicode.h"
44 #include "compobj_private.h"
46 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(ole);
50 extern const CLSID CLSID_DfMarshal;
52 /* Marshalling just passes a unique identifier to the remote client,
53 * that makes it possible to find the passed interface again.
55 * So basically we need a set of values that make it unique.
57 * Process Identifier, Object IUnknown ptr, IID
59 * Note that the IUnknown_QI(ob,xiid,&ppv) always returns the SAME ppv value!
61 * In Windows, a different triple is used: OXID (apt id), OID (stub
62 * manager id), IPID (interface ptr/stub id).
64 * OXIDs identify an apartment and are network scoped
65 * OIDs identify a stub manager and are apartment scoped
66 * IPIDs identify an interface stub and are apartment scoped
69 inline static HRESULT
70 get_facbuf_for_iid(REFIID riid,IPSFactoryBuffer **facbuf) {
71 HRESULT hres;
72 CLSID pxclsid;
74 if ((hres = CoGetPSClsid(riid,&pxclsid)))
75 return hres;
76 return CoGetClassObject(&pxclsid,CLSCTX_INPROC_SERVER,NULL,&IID_IPSFactoryBuffer,(LPVOID*)facbuf);
79 IRpcStubBuffer *mid_to_stubbuffer(wine_marshal_id *mid)
81 IRpcStubBuffer *ret;
82 APARTMENT *apt;
83 struct stub_manager *m;
85 if (!(apt = COM_ApartmentFromOXID(mid->oxid, TRUE)))
87 WARN("Could not map OXID %s to apartment object\n", wine_dbgstr_longlong(mid->oxid));
88 return NULL;
91 if (!(m = get_stub_manager(apt, mid->oid)))
93 WARN("unknown OID %s\n", wine_dbgstr_longlong(mid->oid));
94 return NULL;
97 ret = stub_manager_ipid_to_stubbuffer(m, &mid->ipid);
99 stub_manager_int_release(m);
101 COM_ApartmentRelease(apt);
103 return ret;
106 /* creates a new stub manager and sets mid->oid when mid->oid == 0 */
107 static HRESULT register_ifstub(wine_marshal_id *mid, REFIID riid, IUnknown *obj, IRpcStubBuffer *stub, MSHLFLAGS mshlflags)
109 struct stub_manager *manager = NULL;
110 struct ifstub *ifstub;
111 APARTMENT *apt;
112 BOOL tablemarshal;
114 if (!(apt = COM_ApartmentFromOXID(mid->oxid, TRUE)))
116 ERR("Could not map OXID %s to apartment object\n", wine_dbgstr_longlong(mid->oxid));
117 return E_UNEXPECTED;
120 /* mid->oid of zero means create a new stub manager */
122 if (mid->oid && (manager = get_stub_manager(apt, mid->oid)))
124 TRACE("registering new ifstub on pre-existing manager\n");
126 else
128 TRACE("constructing new stub manager\n");
130 manager = new_stub_manager(apt, obj);
131 if (!manager)
133 COM_ApartmentRelease(apt);
134 return E_OUTOFMEMORY;
137 mid->oid = manager->oid;
140 tablemarshal = ((mshlflags & MSHLFLAGS_TABLESTRONG) || (mshlflags & MSHLFLAGS_TABLEWEAK));
142 ifstub = stub_manager_new_ifstub(manager, stub, obj, riid, tablemarshal);
143 if (!ifstub)
145 stub_manager_int_release(manager);
146 /* FIXME: should we do another release to completely destroy the
147 * stub manager? */
148 COM_ApartmentRelease(apt);
149 return E_OUTOFMEMORY;
152 if (!tablemarshal)
153 stub_manager_ext_addref(manager, 1);
154 else if (mshlflags & MSHLFLAGS_TABLESTRONG)
155 stub_manager_ext_addref(manager, 1);
157 mid->ipid = ifstub->ipid;
159 stub_manager_int_release(manager);
160 COM_ApartmentRelease(apt);
161 return S_OK;
166 /* Client-side identity of the server object */
168 static void proxy_manager_destroy(struct proxy_manager * This);
169 static HRESULT proxy_manager_find_ifproxy(struct proxy_manager * This, REFIID riid, struct ifproxy ** ifproxy_found);
171 static HRESULT WINAPI ClientIdentity_QueryInterface(IInternalUnknown * iface, REFIID riid, void ** ppv)
173 struct proxy_manager * This = (struct proxy_manager *)iface;
174 HRESULT hr;
175 struct ifproxy * ifproxy;
177 TRACE("%s\n", debugstr_guid(riid));
179 if (IsEqualIID(riid, &IID_IUnknown) ||
180 IsEqualIID(riid, &IID_IInternalUnknown))
182 *ppv = (void *)iface;
183 IInternalUnknown_AddRef(iface);
184 return S_OK;
187 hr = proxy_manager_find_ifproxy(This, riid, &ifproxy);
188 if (hr == S_OK)
190 *ppv = ifproxy->iface;
191 IUnknown_AddRef((IUnknown *)*ppv);
192 return S_OK;
195 FIXME("interface not found %s\n", debugstr_guid(riid));
197 /* FIXME: call IRemUnknown::RemQueryInterface */
198 return E_NOINTERFACE;
201 static ULONG WINAPI ClientIdentity_AddRef(IInternalUnknown * iface)
203 struct proxy_manager * This = (struct proxy_manager *)iface;
204 TRACE("%p - before %ld\n", iface, This->refs);
205 return InterlockedIncrement(&This->refs);
208 static ULONG WINAPI ClientIdentity_Release(IInternalUnknown * iface)
210 struct proxy_manager * This = (struct proxy_manager *)iface;
211 ULONG refs = InterlockedDecrement(&This->refs);
212 TRACE("%p - after %ld\n", iface, refs);
213 if (!refs)
214 proxy_manager_destroy(This);
215 return refs;
218 static HRESULT WINAPI ClientIdentity_QueryInternalInterface(IInternalUnknown * iface, REFIID riid, void ** ppv)
220 FIXME("(%s, %p): stub!\n", debugstr_guid(riid), ppv);
221 return E_NOINTERFACE;
224 static const IInternalUnknownVtbl ClientIdentity_Vtbl =
226 ClientIdentity_QueryInterface,
227 ClientIdentity_AddRef,
228 ClientIdentity_Release,
229 ClientIdentity_QueryInternalInterface
232 static HRESULT ifproxy_get_public_ref(struct ifproxy * This)
234 EnterCriticalSection(&This->parent->cs);
235 if (This->refs == 0)
237 APARTMENT * apt;
239 TRACE("getting public ref for ifproxy %p\n", This);
241 /* FIXME: call IRemUnknown::RemAddRef if necessary */
243 /* FIXME: this is a hack around not yet implemented IRemUnknown */
244 if ((apt = COM_ApartmentFromOXID(This->parent->oxid, TRUE)))
246 struct stub_manager * stubmgr;
247 if ((stubmgr = get_stub_manager(apt, This->parent->oid)))
249 stub_manager_ext_addref(stubmgr, 1);
250 This->refs += 1;
252 stub_manager_int_release(stubmgr);
255 COM_ApartmentRelease(apt);
257 else
258 FIXME("Need to implement IRemUnknown for inter-process table marshaling\n");
260 LeaveCriticalSection(&This->parent->cs);
262 return S_OK;
265 static HRESULT ifproxy_release_public_refs(struct ifproxy * This)
267 EnterCriticalSection(&This->parent->cs);
269 if (This->refs > 0)
271 // FIXME: call IRemUnknown::RemRelease
272 This->refs = 0;
275 LeaveCriticalSection(&This->parent->cs);
276 return S_OK;
279 static void ifproxy_disconnect(struct ifproxy * This)
281 IRpcProxyBuffer_Disconnect(This->proxy);
284 static void ifproxy_destroy(struct ifproxy * This)
286 /* release public references to this object so that the stub can know
287 * when to destroy itself */
288 ifproxy_release_public_refs(This);
290 list_remove(&This->entry);
292 if (This->proxy) IRpcProxyBuffer_Release(This->proxy);
293 HeapFree(GetProcessHeap(), 0, This);
296 static HRESULT proxy_manager_construct(APARTMENT * apt, OXID oxid, OID oid, IRpcChannelBuffer * channel, struct proxy_manager ** proxy_manager)
298 struct proxy_manager * This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
299 if (!This) return E_OUTOFMEMORY;
301 This->lpVtbl = &ClientIdentity_Vtbl;
303 list_init(&This->entry);
304 list_init(&This->interfaces);
306 InitializeCriticalSection(&This->cs);
308 /* the apartment the object was unmarshaled into */
309 This->parent = apt;
311 /* the source apartment and id of the object */
312 This->oxid = oxid;
313 This->oid = oid;
315 This->refs = 0; /* will be incremented on creation of first proxy */
317 This->chan = channel; /* FIXME: we should take the binding strings and construct the channel in this function */
319 EnterCriticalSection(&apt->cs);
320 list_add_head(&apt->proxies, &This->entry);
321 LeaveCriticalSection(&apt->cs);
323 *proxy_manager = This;
324 return S_OK;
327 static HRESULT proxy_manager_create_ifproxy(struct proxy_manager * This, IPID ipid, REFIID riid, ULONG cPublicRefs, struct ifproxy ** iif_out)
329 HRESULT hr;
330 IPSFactoryBuffer * psfb;
331 struct ifproxy * ifproxy = HeapAlloc(GetProcessHeap(), 0, sizeof(*ifproxy));
332 if (!ifproxy) return E_OUTOFMEMORY;
334 list_init(&ifproxy->entry);
336 ifproxy->parent = This;
337 ifproxy->ipid = ipid;
338 ifproxy->iid = *riid;
339 ifproxy->refs = cPublicRefs;
340 ifproxy->proxy = NULL;
342 hr = get_facbuf_for_iid(riid, &psfb);
343 if (hr == S_OK)
345 /* important note: the outer unknown is set to the proxy manager.
346 * This ensures the COM identity rules are not violated, by having a
347 * one-to-one mapping of objects on the proxy side to objects on the
348 * stub side, no matter which interface you view the object through */
349 hr = IPSFactoryBuffer_CreateProxy(psfb, (IUnknown *)&This->lpVtbl, riid,
350 &ifproxy->proxy, &ifproxy->iface);
351 IPSFactoryBuffer_Release(psfb);
354 if (hr == S_OK)
355 hr = IRpcProxyBuffer_Connect(ifproxy->proxy, This->chan);
357 /* get at least one external reference to the object to keep it alive */
358 if (hr == S_OK)
359 hr = ifproxy_get_public_ref(ifproxy);
361 if (hr == S_OK)
363 EnterCriticalSection(&This->cs);
364 list_add_tail(&This->interfaces, &ifproxy->entry);
365 LeaveCriticalSection(&This->cs);
367 *iif_out = ifproxy;
369 else
370 ifproxy_destroy(ifproxy);
372 return hr;
375 static HRESULT proxy_manager_find_ifproxy(struct proxy_manager * This, REFIID riid, struct ifproxy ** ifproxy_found)
377 HRESULT hr = E_NOINTERFACE; /* assume not found */
378 struct list * cursor;
380 EnterCriticalSection(&This->cs);
381 LIST_FOR_EACH(cursor, &This->interfaces)
383 struct ifproxy * ifproxy = LIST_ENTRY(cursor, struct ifproxy, entry);
384 if (IsEqualIID(riid, &ifproxy->iid))
386 *ifproxy_found = ifproxy;
387 hr = S_OK;
388 break;
391 LeaveCriticalSection(&This->cs);
393 return hr;
396 static void proxy_manager_disconnect(struct proxy_manager * This)
398 struct list * cursor;
400 TRACE("oid = %s\n", wine_dbgstr_longlong(This->oid));
402 EnterCriticalSection(&This->cs);
404 LIST_FOR_EACH(cursor, &This->interfaces)
406 struct ifproxy * ifproxy = LIST_ENTRY(cursor, struct ifproxy, entry);
407 ifproxy_disconnect(ifproxy);
410 /* apartment is being destroyed so don't keep a pointer around to it */
411 This->parent = NULL;
413 /* FIXME: will this still be necessary if/when we use a real RPC
414 * channel? */
415 IRpcChannelBuffer_Release(This->chan);
416 This->chan = NULL;
418 LeaveCriticalSection(&This->cs);
421 static void proxy_manager_destroy(struct proxy_manager * This)
423 struct list * cursor;
425 if (This->parent)
427 EnterCriticalSection(&This->parent->cs);
429 /* remove ourself from the list of proxy objects in the apartment */
430 LIST_FOR_EACH(cursor, &This->parent->proxies)
432 if (cursor == &This->entry)
434 list_remove(&This->entry);
435 break;
439 LeaveCriticalSection(&This->parent->cs);
442 /* destroy all of the interface proxies */
443 while (!(cursor = list_head(&This->interfaces)))
445 struct ifproxy * ifproxy = LIST_ENTRY(cursor, struct ifproxy, entry);
446 ifproxy_destroy(ifproxy);
449 if (This->chan) IRpcChannelBuffer_Release(This->chan);
451 DeleteCriticalSection(&This->cs);
453 HeapFree(GetProcessHeap(), 0, This);
456 static BOOL find_proxy_manager(APARTMENT * apt, OXID oxid, OID oid, struct proxy_manager ** proxy_found)
458 BOOL found = FALSE;
459 struct list * cursor;
461 EnterCriticalSection(&apt->cs);
462 LIST_FOR_EACH(cursor, &apt->proxies)
464 struct proxy_manager * proxy = LIST_ENTRY(cursor, struct proxy_manager, entry);
465 if ((oxid == proxy->oxid) && (oid == proxy->oid))
467 *proxy_found = proxy;
468 found = TRUE;
469 break;
472 LeaveCriticalSection(&apt->cs);
473 return found;
476 HRESULT MARSHAL_Disconnect_Proxies(APARTMENT *apt)
478 struct list * cursor;
480 EnterCriticalSection(&apt->cs);
481 LIST_FOR_EACH(cursor, &apt->proxies)
483 struct proxy_manager * proxy = LIST_ENTRY(cursor, struct proxy_manager, entry);
484 proxy_manager_disconnect(proxy);
486 LeaveCriticalSection(&apt->cs);
488 return S_OK;
491 /********************** StdMarshal implementation ****************************/
492 typedef struct _StdMarshalImpl {
493 IMarshalVtbl *lpvtbl;
494 DWORD ref;
496 IID iid;
497 DWORD dwDestContext;
498 LPVOID pvDestContext;
499 DWORD mshlflags;
500 } StdMarshalImpl;
502 static HRESULT WINAPI
503 StdMarshalImpl_QueryInterface(LPMARSHAL iface,REFIID riid,LPVOID *ppv) {
504 *ppv = NULL;
505 if (IsEqualIID(&IID_IUnknown,riid) || IsEqualIID(&IID_IMarshal,riid)) {
506 *ppv = iface;
507 IUnknown_AddRef(iface);
508 return S_OK;
510 FIXME("No interface for %s.\n",debugstr_guid(riid));
511 return E_NOINTERFACE;
514 static ULONG WINAPI
515 StdMarshalImpl_AddRef(LPMARSHAL iface) {
516 StdMarshalImpl *This = (StdMarshalImpl *)iface;
517 return InterlockedIncrement(&This->ref);
520 static ULONG WINAPI
521 StdMarshalImpl_Release(LPMARSHAL iface) {
522 StdMarshalImpl *This = (StdMarshalImpl *)iface;
523 ULONG ref = InterlockedDecrement(&This->ref);
525 if (!ref) HeapFree(GetProcessHeap(),0,This);
526 return ref;
529 static HRESULT WINAPI
530 StdMarshalImpl_GetUnmarshalClass(
531 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
532 void* pvDestContext, DWORD mshlflags, CLSID* pCid
534 memcpy(pCid,&CLSID_DfMarshal,sizeof(CLSID_DfMarshal));
535 return S_OK;
538 static HRESULT WINAPI
539 StdMarshalImpl_GetMarshalSizeMax(
540 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
541 void* pvDestContext, DWORD mshlflags, DWORD* pSize
543 *pSize = sizeof(wine_marshal_id);
544 return S_OK;
547 static HRESULT WINAPI
548 StdMarshalImpl_MarshalInterface(
549 LPMARSHAL iface, IStream *pStm,REFIID riid, void* pv, DWORD dwDestContext,
550 void* pvDestContext, DWORD mshlflags
552 wine_marshal_id mid;
553 IUnknown *pUnk;
554 ULONG res;
555 HRESULT hres;
556 IRpcStubBuffer *stubbuffer;
557 IPSFactoryBuffer *psfacbuf;
558 struct stub_manager *manager;
559 APARTMENT *apt = COM_CurrentApt();
561 TRACE("(...,%s,...)\n",debugstr_guid(riid));
563 if (!apt)
565 ERR("Apartment not initialized\n");
566 return CO_E_NOTINITIALIZED;
569 start_apartment_listener_thread(); /* just to be sure we have one running. */
571 hres = get_facbuf_for_iid(riid,&psfacbuf);
572 if (hres) return hres;
574 hres = IPSFactoryBuffer_CreateStub(psfacbuf,riid,pv,&stubbuffer);
575 IPSFactoryBuffer_Release(psfacbuf);
576 if (hres) {
577 FIXME("Failed to create an RpcStubBuffer from PSFactory for %s\n",debugstr_guid(riid));
578 return hres;
581 /* now fill out the MID */
582 mid.oxid = apt->oxid;
584 IUnknown_QueryInterface((LPUNKNOWN)pv, riid, (LPVOID*)&pUnk);
586 if ((manager = get_stub_manager_from_object(apt, pUnk)))
588 mid.oid = manager->oid;
589 stub_manager_int_release(manager);
591 else
593 mid.oid = 0; /* will be set by register_ifstub */
596 hres = register_ifstub(&mid, riid, pUnk, stubbuffer, mshlflags);
598 IUnknown_Release(pUnk);
600 if (hres)
602 FIXME("Failed to create ifstub, hres=0x%lx\n", hres);
603 return hres;
606 hres = IStream_Write(pStm,&mid,sizeof(mid),&res);
607 if (hres) return hres;
609 return S_OK;
612 static HRESULT WINAPI
613 StdMarshalImpl_UnmarshalInterface(LPMARSHAL iface, IStream *pStm, REFIID riid, void **ppv)
615 struct stub_manager *stubmgr;
616 wine_marshal_id mid;
617 ULONG res;
618 HRESULT hres;
619 IRpcChannelBuffer *chanbuf;
620 struct proxy_manager *proxy_manager;
621 APARTMENT *apt = COM_CurrentApt();
622 APARTMENT *stub_apt;
623 ULONG cPublicRefs = 1;
625 TRACE("(...,%s,....)\n",debugstr_guid(riid));
627 if (!apt)
629 ERR("Apartment not initialized\n");
630 return CO_E_NOTINITIALIZED;
633 hres = IStream_Read(pStm,&mid,sizeof(mid),&res);
634 if (hres) return hres;
636 /* check if we're marshalling back to ourselves */
637 if ((apt->oxid == mid.oxid) && (stubmgr = get_stub_manager(apt, mid.oid)))
639 TRACE("Unmarshalling object marshalled in same apartment for iid %s, returning original object %p\n", debugstr_guid(riid), stubmgr->object);
641 hres = IUnknown_QueryInterface(stubmgr->object, riid, ppv);
643 /* unref the ifstub. FIXME: only do this on success? */
644 if (!stub_manager_is_table_marshaled(stubmgr, &mid.ipid))
645 stub_manager_ext_release(stubmgr, 1);
647 stub_manager_int_release(stubmgr);
648 return hres;
651 /* notify stub manager about unmarshal if process-local object.
652 * note: if the oxid is not found then we and native will quite happily
653 * ignore table marshaling and normal marshaling rules regarding number of
654 * unmarshals, etc, but if you abuse these rules then your proxy could end
655 * up returning RPC_E_DISCONNECTED. */
656 if ((stub_apt = COM_ApartmentFromOXID(mid.oxid, TRUE)))
658 if ((stubmgr = get_stub_manager(stub_apt, mid.oid)))
660 if (!stub_manager_notify_unmarshal(stubmgr, &mid.ipid))
661 hres = CO_E_OBJNOTCONNECTED;
663 /* FIXME: hack around not using STDOBJREF yet */
664 if (stub_manager_is_table_marshaled(stubmgr, &mid.ipid))
665 cPublicRefs = 0;
667 stub_manager_int_release(stubmgr);
669 else
671 WARN("Couldn't find object for OXID %s, OID %s, assuming disconnected\n",
672 wine_dbgstr_longlong(mid.oxid),
673 wine_dbgstr_longlong(mid.oid));
674 hres = CO_E_OBJNOTCONNECTED;
677 COM_ApartmentRelease(stub_apt);
679 else
680 TRACE("Treating unmarshal from OXID %s as inter-process\n", wine_dbgstr_longlong(mid.oxid));
682 if (hres) return hres;
684 if (!find_proxy_manager(apt, mid.oxid, mid.oid, &proxy_manager))
686 hres = PIPE_GetNewPipeBuf(&mid,&chanbuf);
687 if (hres == S_OK)
688 hres = proxy_manager_construct(apt, mid.oxid, mid.oid, chanbuf, &proxy_manager);
691 if (hres == S_OK)
693 struct ifproxy * ifproxy;
694 hres = proxy_manager_find_ifproxy(proxy_manager, riid, &ifproxy);
695 if (hres == S_OK)
696 IUnknown_AddRef((IUnknown *)ifproxy->iface);
697 else if (hres == E_NOINTERFACE)
698 hres = proxy_manager_create_ifproxy(proxy_manager, mid.ipid, riid, cPublicRefs, &ifproxy);
700 if (hres == S_OK)
701 *ppv = ifproxy->iface; /* AddRef'd above */
704 return hres;
707 static HRESULT WINAPI
708 StdMarshalImpl_ReleaseMarshalData(LPMARSHAL iface, IStream *pStm) {
709 wine_marshal_id mid;
710 ULONG res;
711 HRESULT hres;
712 struct stub_manager *stubmgr;
713 APARTMENT *apt;
715 TRACE("iface=%p, pStm=%p\n", iface, pStm);
717 hres = IStream_Read(pStm,&mid,sizeof(mid),&res);
718 if (hres) return hres;
720 if (!(apt = COM_ApartmentFromOXID(mid.oxid, TRUE)))
722 WARN("Could not map OXID %s to apartment object\n", wine_dbgstr_longlong(mid.oxid));
723 return RPC_E_INVALID_OBJREF;
726 if (!(stubmgr = get_stub_manager(apt, mid.oid)))
728 ERR("could not map MID to stub manager, oxid=%s, oid=%s\n",
729 wine_dbgstr_longlong(mid.oxid), wine_dbgstr_longlong(mid.oid));
730 return RPC_E_INVALID_OBJREF;
733 stub_manager_ext_release(stubmgr, 1);
735 stub_manager_int_release(stubmgr);
736 COM_ApartmentRelease(apt);
738 return S_OK;
741 static HRESULT WINAPI
742 StdMarshalImpl_DisconnectObject(LPMARSHAL iface, DWORD dwReserved) {
743 FIXME("(), stub!\n");
744 return S_OK;
747 IMarshalVtbl stdmvtbl = {
748 StdMarshalImpl_QueryInterface,
749 StdMarshalImpl_AddRef,
750 StdMarshalImpl_Release,
751 StdMarshalImpl_GetUnmarshalClass,
752 StdMarshalImpl_GetMarshalSizeMax,
753 StdMarshalImpl_MarshalInterface,
754 StdMarshalImpl_UnmarshalInterface,
755 StdMarshalImpl_ReleaseMarshalData,
756 StdMarshalImpl_DisconnectObject
759 static HRESULT StdMarshalImpl_Construct(REFIID riid, void** ppvObject)
761 StdMarshalImpl * pStdMarshal =
762 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(StdMarshalImpl));
763 if (!pStdMarshal)
764 return E_OUTOFMEMORY;
765 pStdMarshal->lpvtbl = &stdmvtbl;
766 pStdMarshal->ref = 0;
767 return IMarshal_QueryInterface((IMarshal*)pStdMarshal, riid, ppvObject);
770 /***********************************************************************
771 * CoGetStandardMarshal [OLE32.@]
773 * Gets or creates a standard marshal object.
775 * PARAMS
776 * riid [I] Interface identifier of the pUnk object.
777 * pUnk [I] Optional. Object to get the marshal object for.
778 * dwDestContext [I] Destination. Used to enable or disable optimizations.
779 * pvDestContext [I] Reserved. Must be NULL.
780 * mshlflags [I] Flags affecting the marshaling process.
781 * ppMarshal [O] Address where marshal object will be stored.
783 * RETURNS
784 * Success: S_OK.
785 * Failure: HRESULT code.
787 * NOTES
789 * The function retrieves the IMarshal object associated with an object if
790 * that object is currently an active stub, otherwise a new marshal object is
791 * created.
793 HRESULT WINAPI CoGetStandardMarshal(REFIID riid, IUnknown *pUnk,
794 DWORD dwDestContext, LPVOID pvDestContext,
795 DWORD mshlflags, LPMARSHAL *ppMarshal)
797 StdMarshalImpl *dm;
799 if (pUnk == NULL) {
800 FIXME("(%s,NULL,%lx,%p,%lx,%p), unimplemented yet.\n",
801 debugstr_guid(riid),dwDestContext,pvDestContext,mshlflags,ppMarshal
803 return E_FAIL;
805 TRACE("(%s,%p,%lx,%p,%lx,%p)\n",
806 debugstr_guid(riid),pUnk,dwDestContext,pvDestContext,mshlflags,ppMarshal
808 *ppMarshal = HeapAlloc(GetProcessHeap(),0,sizeof(StdMarshalImpl));
809 dm = (StdMarshalImpl*) *ppMarshal;
810 if (!dm) return E_FAIL;
811 dm->lpvtbl = &stdmvtbl;
812 dm->ref = 1;
814 memcpy(&dm->iid,riid,sizeof(dm->iid));
815 dm->dwDestContext = dwDestContext;
816 dm->pvDestContext = pvDestContext;
817 dm->mshlflags = mshlflags;
818 return S_OK;
821 /***********************************************************************
822 * get_marshaler [internal]
824 * Retrieves an IMarshal interface for an object.
826 static HRESULT get_marshaler(REFIID riid, IUnknown *pUnk, DWORD dwDestContext,
827 void *pvDestContext, DWORD mshlFlags,
828 LPMARSHAL *pMarshal)
830 HRESULT hr;
832 if (!pUnk)
833 return E_POINTER;
834 hr = IUnknown_QueryInterface(pUnk, &IID_IMarshal, (LPVOID*)pMarshal);
835 if (hr)
836 hr = CoGetStandardMarshal(riid, pUnk, dwDestContext, pvDestContext,
837 mshlFlags, pMarshal);
838 return hr;
841 /***********************************************************************
842 * get_unmarshaler_from_stream [internal]
844 * Creates an IMarshal* object according to the data marshaled to the stream.
845 * The function leaves the stream pointer at the start of the data written
846 * to the stream by the IMarshal* object.
848 static HRESULT get_unmarshaler_from_stream(IStream *stream, IMarshal **marshal)
850 HRESULT hr;
851 ULONG res;
852 OBJREF objref;
854 /* read common OBJREF header */
855 hr = IStream_Read(stream, &objref, FIELD_OFFSET(OBJREF, u_objref), &res);
856 if (hr || (res != FIELD_OFFSET(OBJREF, u_objref)))
858 ERR("Failed to read common OBJREF header, 0x%08lx\n", hr);
859 return STG_E_READFAULT;
862 /* sanity check on header */
863 if (objref.signature != OBJREF_SIGNATURE)
865 ERR("Bad OBJREF signature 0x%08lx\n", objref.signature);
866 return RPC_E_INVALID_OBJREF;
869 /* FIXME: handler marshaling */
870 if (objref.flags & OBJREF_STANDARD)
872 TRACE("Using standard unmarshaling\n");
873 hr = StdMarshalImpl_Construct(&IID_IMarshal, (LPVOID*)marshal);
875 else if (objref.flags & OBJREF_CUSTOM)
877 ULONG custom_header_size = FIELD_OFFSET(OBJREF, u_objref.u_custom.size) -
878 FIELD_OFFSET(OBJREF, u_objref.u_custom);
879 TRACE("Using custom unmarshaling\n");
880 /* read constant sized OR_CUSTOM data from stream */
881 hr = IStream_Read(stream, &objref.u_objref.u_custom,
882 custom_header_size, &res);
883 if (hr || (res != custom_header_size))
885 ERR("Failed to read OR_CUSTOM header, 0x%08lx\n", hr);
886 return STG_E_READFAULT;
888 /* now create the marshaler specified in the stream */
889 hr = CoCreateInstance(&objref.u_objref.u_custom.clsid, NULL,
890 CLSCTX_INPROC_SERVER, &IID_IMarshal,
891 (LPVOID*)marshal);
893 else
895 FIXME("Invalid or unimplemented marshaling type specified: %lx\n",
896 objref.flags);
897 return RPC_E_INVALID_OBJREF;
900 if (hr)
901 ERR("Failed to create marshal, 0x%08lx\n", hr);
903 return hr;
906 /***********************************************************************
907 * CoGetMarshalSizeMax [OLE32.@]
909 * Gets the maximum amount of data that will be needed by a marshal.
911 * PARAMS
912 * pulSize [O] Address where maximum marshal size will be stored.
913 * riid [I] Identifier of the interface to marshal.
914 * pUnk [I] Pointer to the object to marshal.
915 * dwDestContext [I] Destination. Used to enable or disable optimizations.
916 * pvDestContext [I] Reserved. Must be NULL.
917 * mshlFlags [I] Flags that affect the marshaling. See CoMarshalInterface().
919 * RETURNS
920 * Success: S_OK.
921 * Failure: HRESULT code.
923 * SEE ALSO
924 * CoMarshalInterface().
926 HRESULT WINAPI CoGetMarshalSizeMax(ULONG *pulSize, REFIID riid, IUnknown *pUnk,
927 DWORD dwDestContext, void *pvDestContext,
928 DWORD mshlFlags)
930 HRESULT hr;
931 LPMARSHAL pMarshal;
932 CLSID marshaler_clsid;
934 hr = get_marshaler(riid, pUnk, dwDestContext, pvDestContext, mshlFlags, &pMarshal);
935 if (hr)
936 return hr;
938 hr = IMarshal_GetUnmarshalClass(pMarshal, riid, pUnk, dwDestContext,
939 pvDestContext, mshlFlags, &marshaler_clsid);
940 if (hr)
942 ERR("IMarshal::GetUnmarshalClass failed, 0x%08lx\n", hr);
943 IMarshal_Release(pMarshal);
944 return hr;
947 hr = IMarshal_GetMarshalSizeMax(pMarshal, riid, pUnk, dwDestContext,
948 pvDestContext, mshlFlags, pulSize);
949 /* add on the size of the common header */
950 *pulSize += FIELD_OFFSET(OBJREF, u_objref);
952 /* if custom marshaling, add on size of custom header */
953 if (!IsEqualCLSID(&marshaler_clsid, &CLSID_DfMarshal))
954 *pulSize += FIELD_OFFSET(OBJREF, u_objref.u_custom.size) -
955 FIELD_OFFSET(OBJREF, u_objref.u_custom);
957 IMarshal_Release(pMarshal);
958 return hr;
962 /***********************************************************************
963 * CoMarshalInterface [OLE32.@]
965 * Marshals an interface into a stream so that the object can then be
966 * unmarshaled from another COM apartment and used remotely.
968 * PARAMS
969 * pStream [I] Stream the object will be marshaled into.
970 * riid [I] Identifier of the interface to marshal.
971 * pUnk [I] Pointer to the object to marshal.
972 * dwDestContext [I] Destination. Used to enable or disable optimizations.
973 * pvDestContext [I] Reserved. Must be NULL.
974 * mshlFlags [I] Flags that affect the marshaling. See notes.
976 * RETURNS
977 * Success: S_OK.
978 * Failure: HRESULT code.
980 * NOTES
982 * The mshlFlags parameter can take one or more of the following flags:
983 *| MSHLFLAGS_NORMAL - Unmarshal once, releases stub on last proxy release.
984 *| MSHLFLAGS_TABLESTRONG - Unmarshal many, release when CoReleaseMarshalData() called.
985 *| MSHLFLAGS_TABLEWEAK - Unmarshal many, releases stub on last proxy release.
986 *| MSHLFLAGS_NOPING - No automatic garbage collection (and so reduces network traffic).
988 * If a marshaled object is not unmarshaled, then CoReleaseMarshalData() must
989 * be called in order to release the resources used in the marshaling.
991 * SEE ALSO
992 * CoUnmarshalInterface(), CoReleaseMarshalData().
994 HRESULT WINAPI CoMarshalInterface(IStream *pStream, REFIID riid, IUnknown *pUnk,
995 DWORD dwDestContext, void *pvDestContext,
996 DWORD mshlFlags)
998 HRESULT hr;
999 CLSID marshaler_clsid;
1000 OBJREF objref;
1001 IStream * pMarshalStream = NULL;
1002 LPMARSHAL pMarshal;
1004 TRACE("(%p, %s, %p, %lx, %p, %lx)\n", pStream, debugstr_guid(riid), pUnk,
1005 dwDestContext, pvDestContext, mshlFlags);
1007 if (pUnk == NULL)
1008 return E_INVALIDARG;
1010 objref.signature = OBJREF_SIGNATURE;
1011 objref.iid = *riid;
1013 /* get the marshaler for the specified interface */
1014 hr = get_marshaler(riid, pUnk, dwDestContext, pvDestContext, mshlFlags, &pMarshal);
1015 if (hr)
1017 ERR("Failed to get marshaller, 0x%08lx\n", hr);
1018 return hr;
1021 hr = IMarshal_GetUnmarshalClass(pMarshal, riid, pUnk, dwDestContext,
1022 pvDestContext, mshlFlags, &marshaler_clsid);
1023 if (hr)
1025 ERR("IMarshal::GetUnmarshalClass failed, 0x%08lx\n", hr);
1026 goto cleanup;
1029 /* FIXME: implement handler marshaling too */
1030 if (IsEqualCLSID(&marshaler_clsid, &CLSID_DfMarshal))
1032 TRACE("Using standard marshaling\n");
1033 objref.flags = OBJREF_STANDARD;
1034 pMarshalStream = pStream;
1036 else
1038 TRACE("Using custom marshaling\n");
1039 objref.flags = OBJREF_CUSTOM;
1040 /* we do custom marshaling into a memory stream so that we know what
1041 * size to write into the OR_CUSTOM header */
1042 hr = CreateStreamOnHGlobal(NULL, TRUE, &pMarshalStream);
1043 if (hr)
1045 ERR("CreateStreamOnHGLOBAL failed with 0x%08lx\n", hr);
1046 goto cleanup;
1050 /* write the common OBJREF header to the stream */
1051 hr = IStream_Write(pStream, &objref, FIELD_OFFSET(OBJREF, u_objref), NULL);
1052 if (hr)
1054 ERR("Failed to write OBJREF header to stream, 0x%08lx\n", hr);
1055 goto cleanup;
1058 TRACE("Calling IMarshal::MarshalInterace\n");
1059 /* call helper object to do the actual marshaling */
1060 hr = IMarshal_MarshalInterface(pMarshal, pMarshalStream, riid, pUnk, dwDestContext,
1061 pvDestContext, mshlFlags);
1063 if (hr)
1065 ERR("Failed to marshal the interface %s, %lx\n", debugstr_guid(riid), hr);
1066 goto cleanup;
1069 if (objref.flags & OBJREF_CUSTOM)
1071 ULONG custom_header_size = FIELD_OFFSET(OBJREF, u_objref.u_custom.size) -
1072 FIELD_OFFSET(OBJREF, u_objref.u_custom);
1073 HGLOBAL hGlobal;
1074 LPVOID data;
1075 hr = GetHGlobalFromStream(pMarshalStream, &hGlobal);
1076 if (hr)
1078 ERR("Couldn't get HGLOBAL from stream\n");
1079 hr = E_UNEXPECTED;
1080 goto cleanup;
1082 objref.u_objref.u_custom.clsid = marshaler_clsid;
1083 objref.u_objref.u_custom.cbExtension = 0;
1084 objref.u_objref.u_custom.size = GlobalSize(hGlobal);
1085 /* write constant sized OR_CUSTOM data into stream */
1086 hr = IStream_Write(pStream, &objref.u_objref.u_custom,
1087 custom_header_size, NULL);
1088 if (hr)
1090 ERR("Failed to write OR_CUSTOM header to stream with 0x%08lx\n", hr);
1091 goto cleanup;
1094 data = GlobalLock(hGlobal);
1095 if (!data)
1097 ERR("GlobalLock failed\n");
1098 hr = E_UNEXPECTED;
1099 goto cleanup;
1101 /* write custom marshal data */
1102 hr = IStream_Write(pStream, data, objref.u_objref.u_custom.size, NULL);
1103 if (hr)
1105 ERR("Failed to write custom marshal data with 0x%08lx\n", hr);
1106 goto cleanup;
1108 GlobalUnlock(hGlobal);
1111 cleanup:
1112 if (pMarshalStream && (objref.flags & OBJREF_CUSTOM))
1113 IStream_Release(pMarshalStream);
1114 IMarshal_Release(pMarshal);
1115 return hr;
1118 /***********************************************************************
1119 * CoUnmarshalInterface [OLE32.@]
1121 * Unmarshals an object from a stream by creating a proxy to the remote
1122 * object, if necessary.
1124 * PARAMS
1126 * pStream [I] Stream containing the marshaled object.
1127 * riid [I] Interface identifier of the object to create a proxy to.
1128 * ppv [O] Address where proxy will be stored.
1130 * RETURNS
1132 * Success: S_OK.
1133 * Failure: HRESULT code.
1135 * SEE ALSO
1136 * CoMarshalInterface().
1138 HRESULT WINAPI CoUnmarshalInterface(IStream *pStream, REFIID riid, LPVOID *ppv)
1140 HRESULT hr;
1141 LPMARSHAL pMarshal;
1143 TRACE("(%p, %s, %p)\n", pStream, debugstr_guid(riid), ppv);
1145 hr = get_unmarshaler_from_stream(pStream, &pMarshal);
1146 if (hr != S_OK)
1147 return hr;
1149 /* call the helper object to do the actual unmarshaling */
1150 hr = IMarshal_UnmarshalInterface(pMarshal, pStream, riid, ppv);
1151 if (hr)
1152 ERR("IMarshal::UnmarshalInterface failed, 0x%08lx\n", hr);
1154 IMarshal_Release(pMarshal);
1155 return hr;
1158 /***********************************************************************
1159 * CoReleaseMarshalData [OLE32.@]
1161 * Releases resources associated with an object that has been marshaled into
1162 * a stream.
1164 * PARAMS
1166 * pStream [I] The stream that the object has been marshaled into.
1168 * RETURNS
1169 * Success: S_OK.
1170 * Failure: HRESULT error code.
1172 * NOTES
1174 * Call this function to release resources associated with a normal or
1175 * table-weak marshal that will not be unmarshaled, and all table-strong
1176 * marshals when they are no longer needed.
1178 * SEE ALSO
1179 * CoMarshalInterface(), CoUnmarshalInterface().
1181 HRESULT WINAPI CoReleaseMarshalData(IStream *pStream)
1183 HRESULT hr;
1184 LPMARSHAL pMarshal;
1186 TRACE("(%p)\n", pStream);
1188 hr = get_unmarshaler_from_stream(pStream, &pMarshal);
1189 if (hr != S_OK)
1190 return hr;
1192 /* call the helper object to do the releasing of marshal data */
1193 hr = IMarshal_ReleaseMarshalData(pMarshal, pStream);
1194 if (hr)
1195 ERR("IMarshal::ReleaseMarshalData failed with error 0x%08lx\n", hr);
1197 IMarshal_Release(pMarshal);
1198 return hr;
1202 /***********************************************************************
1203 * CoMarshalInterThreadInterfaceInStream [OLE32.@]
1205 * Marshal an interface across threads in the same process.
1207 * PARAMS
1208 * riid [I] Identifier of the interface to be marshalled.
1209 * pUnk [I] Pointer to IUnknown-derived interface that will be marshalled.
1210 * ppStm [O] Pointer to IStream object that is created and then used to store the marshalled inteface.
1212 * RETURNS
1213 * Success: S_OK
1214 * Failure: E_OUTOFMEMORY and other COM error codes
1216 * SEE ALSO
1217 * CoMarshalInterface(), CoUnmarshalInterface() and CoGetInterfaceAndReleaseStream()
1219 HRESULT WINAPI CoMarshalInterThreadInterfaceInStream(
1220 REFIID riid, LPUNKNOWN pUnk, LPSTREAM * ppStm)
1222 ULARGE_INTEGER xpos;
1223 LARGE_INTEGER seekto;
1224 HRESULT hres;
1226 TRACE("(%s, %p, %p)\n",debugstr_guid(riid), pUnk, ppStm);
1228 hres = CreateStreamOnHGlobal(0, TRUE, ppStm);
1229 if (FAILED(hres)) return hres;
1230 hres = CoMarshalInterface(*ppStm, riid, pUnk, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1232 /* FIXME: is this needed? */
1233 memset(&seekto,0,sizeof(seekto));
1234 IStream_Seek(*ppStm,seekto,SEEK_SET,&xpos);
1236 return hres;
1239 /***********************************************************************
1240 * CoGetInterfaceAndReleaseStream [OLE32.@]
1242 * Unmarshalls an inteface from a stream and then releases the stream.
1244 * PARAMS
1245 * pStm [I] Stream that contains the marshalled inteface.
1246 * riid [I] Interface identifier of the object to unmarshall.
1247 * ppv [O] Address of pointer where the requested interface object will be stored.
1249 * RETURNS
1250 * Success: S_OK
1251 * Failure: A COM error code
1253 * SEE ALSO
1254 * CoMarshalInterThreadInterfaceInStream() and CoUnmarshalInteface()
1256 HRESULT WINAPI CoGetInterfaceAndReleaseStream(LPSTREAM pStm, REFIID riid,
1257 LPVOID *ppv)
1259 HRESULT hres;
1261 TRACE("(%p, %s, %p)\n", pStm, debugstr_guid(riid), ppv);
1263 hres = CoUnmarshalInterface(pStm, riid, ppv);
1264 IStream_Release(pStm);
1265 return hres;
1268 static HRESULT WINAPI StdMarshalCF_QueryInterface(LPCLASSFACTORY iface,
1269 REFIID riid, LPVOID *ppv)
1271 *ppv = NULL;
1272 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory))
1274 *ppv = (LPVOID)iface;
1275 return S_OK;
1277 return E_NOINTERFACE;
1280 static ULONG WINAPI StdMarshalCF_AddRef(LPCLASSFACTORY iface)
1282 return 2; /* non-heap based object */
1285 static ULONG WINAPI StdMarshalCF_Release(LPCLASSFACTORY iface)
1287 return 1; /* non-heap based object */
1290 static HRESULT WINAPI StdMarshalCF_CreateInstance(LPCLASSFACTORY iface,
1291 LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv)
1293 if (IsEqualIID(riid,&IID_IMarshal))
1294 return StdMarshalImpl_Construct(riid, ppv);
1296 FIXME("(%s), not supported.\n",debugstr_guid(riid));
1297 return E_NOINTERFACE;
1300 static HRESULT WINAPI StdMarshalCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
1302 FIXME("(%d), stub!\n",fLock);
1303 return S_OK;
1306 static IClassFactoryVtbl StdMarshalCFVtbl =
1308 StdMarshalCF_QueryInterface,
1309 StdMarshalCF_AddRef,
1310 StdMarshalCF_Release,
1311 StdMarshalCF_CreateInstance,
1312 StdMarshalCF_LockServer
1314 static IClassFactoryVtbl *StdMarshalCF = &StdMarshalCFVtbl;
1316 HRESULT MARSHAL_GetStandardMarshalCF(LPVOID *ppv)
1318 *ppv = &StdMarshalCF;
1319 return S_OK;
1322 /***********************************************************************
1323 * CoMarshalHresult [OLE32.@]
1325 * Marshals an HRESULT value into a stream.
1327 * PARAMS
1328 * pStm [I] Stream that hresult will be marshalled into.
1329 * hresult [I] HRESULT to be marshalled.
1331 * RETURNS
1332 * Success: S_OK
1333 * Failure: A COM error code
1335 * SEE ALSO
1336 * CoUnmarshalHresult().
1338 HRESULT WINAPI CoMarshalHresult(LPSTREAM pStm, HRESULT hresult)
1340 return IStream_Write(pStm, &hresult, sizeof(hresult), NULL);
1343 /***********************************************************************
1344 * CoUnmarshalHresult [OLE32.@]
1346 * Unmarshals an HRESULT value from a stream.
1348 * PARAMS
1349 * pStm [I] Stream that hresult will be unmarshalled from.
1350 * phresult [I] Pointer to HRESULT where the value will be unmarshalled to.
1352 * RETURNS
1353 * Success: S_OK
1354 * Failure: A COM error code
1356 * SEE ALSO
1357 * CoMarshalHresult().
1359 HRESULT WINAPI CoUnmarshalHresult(LPSTREAM pStm, HRESULT * phresult)
1361 return IStream_Read(pStm, phresult, sizeof(*phresult), NULL);