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
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 /* number of refs given out for normal marshaling */
53 #define NORMALEXTREFS 1 /* FIXME: this should be 5, but we have to wait for IRemUnknown support first */
55 /* private flag indicating that the caller does not want to notify the stub
56 * when the proxy disconnects or is destroyed */
57 #define SORFP_NOLIFETIMEMGMT SORF_OXRES1
59 static HRESULT unmarshal_object(const STDOBJREF *stdobjref, APARTMENT *apt, REFIID riid, void **object);
61 /* Marshalling just passes a unique identifier to the remote client,
62 * that makes it possible to find the passed interface again.
64 * So basically we need a set of values that make it unique.
66 * Note that the IUnknown_QI(ob,xiid,&ppv) always returns the SAME ppv value!
68 * A triple is used: OXID (apt id), OID (stub manager id),
69 * IPID (interface ptr/stub id).
71 * OXIDs identify an apartment and are network scoped
72 * OIDs identify a stub manager and are apartment scoped
73 * IPIDs identify an interface stub and are apartment scoped
76 inline static HRESULT get_facbuf_for_iid(REFIID riid, IPSFactoryBuffer **facbuf)
81 if ((hr = CoGetPSClsid(riid, &clsid)))
83 return CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL,
84 &IID_IPSFactoryBuffer, (LPVOID*)facbuf);
87 /* creates a new stub manager */
88 HRESULT marshal_object(APARTMENT *apt, STDOBJREF *stdobjref, REFIID riid, IUnknown *object, MSHLFLAGS mshlflags)
90 struct stub_manager *manager;
91 struct ifstub *ifstub;
93 IRpcStubBuffer *stub = NULL;
95 IUnknown *iobject = NULL; /* object of type riid */
97 hr = apartment_getoxid(apt, &stdobjref->oxid);
101 hr = IUnknown_QueryInterface(object, riid, (void **)&iobject);
104 ERR("object doesn't expose interface %s, failing with error 0x%08lx\n",
105 debugstr_guid(riid), hr);
106 return E_NOINTERFACE;
109 /* IUnknown doesn't require a stub buffer, because it never goes out on
111 if (!IsEqualIID(riid, &IID_IUnknown))
113 IPSFactoryBuffer *psfb;
115 hr = get_facbuf_for_iid(riid, &psfb);
118 ERR("couldn't get IPSFactory buffer for interface %s\n", debugstr_guid(riid));
119 IUnknown_Release(iobject);
123 hr = IPSFactoryBuffer_CreateStub(psfb, riid, iobject, &stub);
124 IPSFactoryBuffer_Release(psfb);
127 ERR("Failed to create an IRpcStubBuffer from IPSFactory for %s\n", debugstr_guid(riid));
128 IUnknown_Release(iobject);
133 if (mshlflags & MSHLFLAGS_NOPING)
134 stdobjref->flags = SORF_NOPING;
136 stdobjref->flags = SORF_NULL;
138 /* FIXME: what happens if we register an interface twice with different
139 * marshaling flags? */
140 if ((manager = get_stub_manager_from_object(apt, object)))
141 TRACE("registering new ifstub on pre-existing manager\n");
144 TRACE("constructing new stub manager\n");
146 manager = new_stub_manager(apt, object, mshlflags);
149 if (stub) IRpcStubBuffer_Release(stub);
150 IUnknown_Release(iobject);
151 return E_OUTOFMEMORY;
154 stdobjref->oid = manager->oid;
156 tablemarshal = ((mshlflags & MSHLFLAGS_TABLESTRONG) || (mshlflags & MSHLFLAGS_TABLEWEAK));
158 ifstub = stub_manager_new_ifstub(manager, stub, iobject, riid);
159 IUnknown_Release(iobject);
160 if (stub) IRpcStubBuffer_Release(stub);
163 stub_manager_int_release(manager);
164 /* FIXME: should we do another release to completely destroy the
166 return E_OUTOFMEMORY;
171 stdobjref->cPublicRefs = NORMALEXTREFS;
172 stub_manager_ext_addref(manager, stdobjref->cPublicRefs);
176 stdobjref->cPublicRefs = 0;
177 if (mshlflags & MSHLFLAGS_TABLESTRONG)
178 stub_manager_ext_addref(manager, 1);
181 /* FIXME: check return value */
182 RPC_RegisterInterface(riid);
184 stdobjref->ipid = ifstub->ipid;
186 stub_manager_int_release(manager);
192 /* Client-side identity of the server object */
194 static HRESULT proxy_manager_get_remunknown(struct proxy_manager * This, IRemUnknown **remunk);
195 static void proxy_manager_destroy(struct proxy_manager * This);
196 static HRESULT proxy_manager_find_ifproxy(struct proxy_manager * This, REFIID riid, struct ifproxy ** ifproxy_found);
197 static HRESULT proxy_manager_query_local_interface(struct proxy_manager * This, REFIID riid, void ** ppv);
199 static HRESULT WINAPI ClientIdentity_QueryInterface(IMultiQI * iface, REFIID riid, void ** ppv)
204 TRACE("%s\n", debugstr_guid(riid));
207 hr = IMultiQI_QueryMultipleInterfaces(iface, 1, &mqi);
208 *ppv = (void *)mqi.pItf;
213 static ULONG WINAPI ClientIdentity_AddRef(IMultiQI * iface)
215 struct proxy_manager * This = (struct proxy_manager *)iface;
216 TRACE("%p - before %ld\n", iface, This->refs);
217 return InterlockedIncrement(&This->refs);
220 static ULONG WINAPI ClientIdentity_Release(IMultiQI * iface)
222 struct proxy_manager * This = (struct proxy_manager *)iface;
223 ULONG refs = InterlockedDecrement(&This->refs);
224 TRACE("%p - after %ld\n", iface, refs);
226 proxy_manager_destroy(This);
230 static HRESULT WINAPI ClientIdentity_QueryMultipleInterfaces(IMultiQI *iface, ULONG cMQIs, MULTI_QI *pMQIs)
232 struct proxy_manager * This = (struct proxy_manager *)iface;
233 REMQIRESULT *qiresults = NULL;
234 ULONG nonlocal_mqis = 0;
236 ULONG successful_mqis = 0;
237 IID *iids = HeapAlloc(GetProcessHeap(), 0, cMQIs * sizeof(*iids));
238 /* mapping of RemQueryInterface index to QueryMultipleInterfaces index */
239 ULONG *mapping = HeapAlloc(GetProcessHeap(), 0, cMQIs * sizeof(*mapping));
241 TRACE("cMQIs: %ld\n", cMQIs);
243 /* try to get a local interface - this includes already active proxy
244 * interfaces and also interfaces exposed by the proxy manager */
245 for (i = 0; i < cMQIs; i++)
247 TRACE("iid[%ld] = %s\n", i, debugstr_guid(pMQIs[i].pIID));
248 pMQIs[i].hr = proxy_manager_query_local_interface(This, pMQIs[i].pIID, (void **)&pMQIs[i].pItf);
249 if (pMQIs[i].hr == S_OK)
253 iids[nonlocal_mqis] = *pMQIs[i].pIID;
254 mapping[nonlocal_mqis] = i;
259 TRACE("%ld interfaces not found locally\n", nonlocal_mqis);
261 /* if we have more than one interface not found locally then we must try
262 * to query the remote object for it */
263 if (nonlocal_mqis != 0)
269 /* get the ipid of the first entry */
270 /* FIXME: should we implement ClientIdentity on the ifproxies instead
271 * of the proxy_manager so we use the correct ipid here? */
272 ipid = &LIST_ENTRY(list_head(&This->interfaces), struct ifproxy, entry)->stdobjref.ipid;
274 /* get IRemUnknown proxy so we can communicate with the remote object */
275 hr = proxy_manager_get_remunknown(This, &remunk);
279 hr = IRemUnknown_RemQueryInterface(remunk, ipid, NORMALEXTREFS,
280 nonlocal_mqis, iids, &qiresults);
282 ERR("IRemUnknown_RemQueryInterface failed with error 0x%08lx\n", hr);
285 /* IRemUnknown_RemQueryInterface can return S_FALSE if only some of
286 * the interfaces were returned */
289 /* try to unmarshal each object returned to us */
290 for (i = 0; i < nonlocal_mqis; i++)
292 ULONG index = mapping[i];
293 HRESULT hrobj = qiresults[i].hResult;
295 hrobj = unmarshal_object(&qiresults[i].std, This->parent,
297 (void **)&pMQIs[index].pItf);
302 ERR("Failed to get pointer to interface %s\n", debugstr_guid(pMQIs[index].pIID));
303 pMQIs[index].hr = hrobj;
307 /* free the memory allocated by the proxy */
308 CoTaskMemFree(qiresults);
311 TRACE("%ld/%ld successfully queried\n", successful_mqis, cMQIs);
313 HeapFree(GetProcessHeap(), 0, iids);
314 HeapFree(GetProcessHeap(), 0, mapping);
316 if (successful_mqis == cMQIs)
317 return S_OK; /* we got all requested interfaces */
318 else if (successful_mqis == 0)
319 return E_NOINTERFACE; /* we didn't get any interfaces */
321 return S_FALSE; /* we got some interfaces */
324 static const IMultiQIVtbl ClientIdentity_Vtbl =
326 ClientIdentity_QueryInterface,
327 ClientIdentity_AddRef,
328 ClientIdentity_Release,
329 ClientIdentity_QueryMultipleInterfaces
332 static HRESULT WINAPI Proxy_QueryInterface(IMarshal *iface, REFIID riid, void **ppvObject)
334 ICOM_THIS_MULTI(struct proxy_manager, lpVtblMarshal, iface);
335 return IMultiQI_QueryInterface((IMultiQI *)&This->lpVtbl, riid, ppvObject);
338 static ULONG WINAPI Proxy_AddRef(IMarshal *iface)
340 ICOM_THIS_MULTI(struct proxy_manager, lpVtblMarshal, iface);
341 return IMultiQI_AddRef((IMultiQI *)&This->lpVtbl);
344 /* FIXME: remove these */
345 static HRESULT WINAPI StdMarshalImpl_GetUnmarshalClass(LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext, void* pvDestContext, DWORD mshlflags, CLSID* pCid);
346 static HRESULT WINAPI StdMarshalImpl_GetMarshalSizeMax(LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext, void* pvDestContext, DWORD mshlflags, DWORD* pSize);
347 static HRESULT WINAPI StdMarshalImpl_UnmarshalInterface(LPMARSHAL iface, IStream *pStm, REFIID riid, void **ppv);
348 static HRESULT WINAPI StdMarshalImpl_ReleaseMarshalData(LPMARSHAL iface, IStream *pStm);
349 static HRESULT WINAPI StdMarshalImpl_DisconnectObject(LPMARSHAL iface, DWORD dwReserved);
351 static ULONG WINAPI Proxy_Release(IMarshal *iface)
353 ICOM_THIS_MULTI(struct proxy_manager, lpVtblMarshal, iface);
354 return IMultiQI_Release((IMultiQI *)&This->lpVtbl);
357 static HRESULT WINAPI Proxy_MarshalInterface(
358 LPMARSHAL iface, IStream *pStm, REFIID riid, void* pv, DWORD dwDestContext,
359 void* pvDestContext, DWORD mshlflags)
361 ICOM_THIS_MULTI(struct proxy_manager, lpVtblMarshal, iface);
365 struct ifproxy *ifproxy;
367 TRACE("(...,%s,...)\n", debugstr_guid(riid));
369 hr = proxy_manager_find_ifproxy(This, riid, &ifproxy);
372 ERR("couldn't find proxy for interface %s, error 0x%08lx\n", debugstr_guid(riid), hr);
376 stdobjref = ifproxy->stdobjref;
377 /* FIXME: optimization - share out proxy's public references if possible
378 * instead of making new proxy do a roundtrip through the server */
379 stdobjref.cPublicRefs = 0; /* InterlockedDecrement(&This->stdobjref.cPublicRefs) >= 0 ? 1 : 0 */
380 hr = IStream_Write(pStm, &stdobjref, sizeof(stdobjref), &res);
385 static const IMarshalVtbl ProxyMarshal_Vtbl =
387 Proxy_QueryInterface,
390 StdMarshalImpl_GetUnmarshalClass,
391 StdMarshalImpl_GetMarshalSizeMax,
392 Proxy_MarshalInterface,
393 StdMarshalImpl_UnmarshalInterface,
394 StdMarshalImpl_ReleaseMarshalData,
395 StdMarshalImpl_DisconnectObject
398 static HRESULT ifproxy_get_public_ref(struct ifproxy * This)
402 if (WAIT_OBJECT_0 != WaitForSingleObject(This->parent->remoting_mutex, INFINITE))
404 ERR("Wait failed for ifproxy %p\n", This);
410 IRemUnknown *remunk = NULL;
412 TRACE("getting public ref for ifproxy %p\n", This);
414 hr = proxy_manager_get_remunknown(This->parent, &remunk);
419 rif.ipid = This->stdobjref.ipid;
420 rif.cPublicRefs = NORMALEXTREFS;
421 rif.cPrivateRefs = 0;
422 hr = IRemUnknown_RemAddRef(remunk, 1, &rif, &hrref);
423 if (hr == S_OK && hrref == S_OK)
424 This->refs += NORMALEXTREFS;
426 ERR("IRemUnknown_RemAddRef returned with 0x%08lx, hrref = 0x%08lx\n", hr, hrref);
429 ReleaseMutex(This->parent->remoting_mutex);
434 static HRESULT ifproxy_release_public_refs(struct ifproxy * This)
438 if (WAIT_OBJECT_0 != WaitForSingleObject(This->parent->remoting_mutex, INFINITE))
440 ERR("Wait failed for ifproxy %p\n", This);
446 IRemUnknown *remunk = NULL;
448 TRACE("releasing %ld refs\n", This->refs);
450 hr = proxy_manager_get_remunknown(This->parent, &remunk);
454 rif.ipid = This->stdobjref.ipid;
455 rif.cPublicRefs = This->refs;
456 rif.cPrivateRefs = 0;
457 hr = IRemUnknown_RemRelease(remunk, 1, &rif);
460 else if (hr == RPC_E_DISCONNECTED)
461 WARN("couldn't release references because object was "
462 "disconnected: oxid = %s, oid = %s\n",
463 wine_dbgstr_longlong(This->parent->oxid),
464 wine_dbgstr_longlong(This->parent->oid));
466 ERR("IRemUnknown_RemRelease failed with error 0x%08lx\n", hr);
469 ReleaseMutex(This->parent->remoting_mutex);
474 /* should be called inside This->parent->cs critical section */
475 static void ifproxy_disconnect(struct ifproxy * This)
477 ifproxy_release_public_refs(This);
478 if (This->proxy) IRpcProxyBuffer_Disconnect(This->proxy);
480 IRpcChannelBuffer_Release(This->chan);
484 /* should be called in This->parent->cs critical section if it is an entry in parent's list */
485 static void ifproxy_destroy(struct ifproxy * This)
489 /* release public references to this object so that the stub can know
490 * when to destroy itself */
491 ifproxy_release_public_refs(This);
493 list_remove(&This->entry);
497 IRpcChannelBuffer_Release(This->chan);
501 /* note: we don't call Release for This->proxy because its lifetime is
502 * controlled by the return value from ClientIdentity_Release, which this
503 * function is always called from */
505 HeapFree(GetProcessHeap(), 0, This);
508 static HRESULT proxy_manager_construct(
509 APARTMENT * apt, ULONG sorflags, OXID oxid, OID oid,
510 struct proxy_manager ** proxy_manager)
512 struct proxy_manager * This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
513 if (!This) return E_OUTOFMEMORY;
515 This->remoting_mutex = CreateMutexW(NULL, FALSE, NULL);
516 if (!This->remoting_mutex)
518 HeapFree(GetProcessHeap(), 0, This);
519 return HRESULT_FROM_WIN32(GetLastError());
522 This->lpVtbl = &ClientIdentity_Vtbl;
523 This->lpVtblMarshal = &ProxyMarshal_Vtbl;
525 list_init(&This->entry);
526 list_init(&This->interfaces);
528 InitializeCriticalSection(&This->cs);
529 DEBUG_SET_CRITSEC_NAME(&This->cs, "proxy_manager");
531 /* the apartment the object was unmarshaled into */
534 /* the source apartment and id of the object */
540 /* the DCOM draft specification states that the SORF_NOPING flag is
541 * proxy manager specific, not ifproxy specific, so this implies that we
542 * should store the STDOBJREF flags here in the proxy manager. */
543 This->sorflags = sorflags;
545 /* we create the IRemUnknown proxy on demand */
548 EnterCriticalSection(&apt->cs);
549 /* FIXME: we are dependent on the ordering in here to make sure a proxy's
550 * IRemUnknown proxy doesn't get destroyed before the regual proxy does
551 * because we need the IRemUnknown proxy during the destruction of the
552 * regular proxy. Ideally, we should maintain a separate list for the
553 * IRemUnknown proxies that need late destruction */
554 list_add_tail(&apt->proxies, &This->entry);
555 LeaveCriticalSection(&apt->cs);
557 TRACE("%p created for OXID %s, OID %s\n", This,
558 wine_dbgstr_longlong(oxid), wine_dbgstr_longlong(oid));
560 *proxy_manager = This;
564 static HRESULT proxy_manager_query_local_interface(struct proxy_manager * This, REFIID riid, void ** ppv)
567 struct ifproxy * ifproxy;
569 TRACE("%s\n", debugstr_guid(riid));
571 if (IsEqualIID(riid, &IID_IUnknown) ||
572 IsEqualIID(riid, &IID_IMultiQI))
574 *ppv = (void *)&This->lpVtbl;
575 IUnknown_AddRef((IUnknown *)*ppv);
578 if (IsEqualIID(riid, &IID_IMarshal))
580 *ppv = (void *)&This->lpVtblMarshal;
581 IUnknown_AddRef((IUnknown *)*ppv);
584 if (IsEqualIID(riid, &IID_IClientSecurity))
586 FIXME("requesting IClientSecurity, but it is unimplemented\n");
588 return E_NOINTERFACE;
591 hr = proxy_manager_find_ifproxy(This, riid, &ifproxy);
594 *ppv = ifproxy->iface;
595 IUnknown_AddRef((IUnknown *)*ppv);
600 return E_NOINTERFACE;
603 static HRESULT proxy_manager_create_ifproxy(
604 struct proxy_manager * This, const STDOBJREF *stdobjref, REFIID riid,
605 IRpcChannelBuffer * channel, struct ifproxy ** iif_out)
608 IPSFactoryBuffer * psfb;
609 struct ifproxy * ifproxy = HeapAlloc(GetProcessHeap(), 0, sizeof(*ifproxy));
610 if (!ifproxy) return E_OUTOFMEMORY;
612 list_init(&ifproxy->entry);
614 ifproxy->parent = This;
615 ifproxy->stdobjref = *stdobjref;
616 ifproxy->iid = *riid;
617 ifproxy->refs = stdobjref->cPublicRefs;
618 ifproxy->proxy = NULL;
621 ifproxy->chan = channel; /* FIXME: we should take the binding strings and construct the channel in this function */
623 /* the IUnknown interface is special because it does not have a
624 * proxy associated with the ifproxy as we handle IUnknown ourselves */
625 if (IsEqualIID(riid, &IID_IUnknown))
627 ifproxy->iface = (void *)&This->lpVtbl;
632 hr = get_facbuf_for_iid(riid, &psfb);
635 /* important note: the outer unknown is set to the proxy manager.
636 * This ensures the COM identity rules are not violated, by having a
637 * one-to-one mapping of objects on the proxy side to objects on the
638 * stub side, no matter which interface you view the object through */
639 hr = IPSFactoryBuffer_CreateProxy(psfb, (IUnknown *)&This->lpVtbl, riid,
640 &ifproxy->proxy, &ifproxy->iface);
641 IPSFactoryBuffer_Release(psfb);
643 ERR("Could not create proxy for interface %s, error 0x%08lx\n",
644 debugstr_guid(riid), hr);
647 ERR("Could not get IPSFactoryBuffer for interface %s, error 0x%08lx\n",
648 debugstr_guid(riid), hr);
651 hr = IRpcProxyBuffer_Connect(ifproxy->proxy, ifproxy->chan);
654 /* get at least one external reference to the object to keep it alive */
656 hr = ifproxy_get_public_ref(ifproxy);
660 EnterCriticalSection(&This->cs);
661 list_add_tail(&This->interfaces, &ifproxy->entry);
662 LeaveCriticalSection(&This->cs);
665 TRACE("ifproxy %p created for IPID %s, interface %s with %lu public refs\n",
666 ifproxy, debugstr_guid(&stdobjref->ipid), debugstr_guid(riid), stdobjref->cPublicRefs);
669 ifproxy_destroy(ifproxy);
674 static HRESULT proxy_manager_find_ifproxy(struct proxy_manager * This, REFIID riid, struct ifproxy ** ifproxy_found)
676 HRESULT hr = E_NOINTERFACE; /* assume not found */
677 struct list * cursor;
679 EnterCriticalSection(&This->cs);
680 LIST_FOR_EACH(cursor, &This->interfaces)
682 struct ifproxy * ifproxy = LIST_ENTRY(cursor, struct ifproxy, entry);
683 if (IsEqualIID(riid, &ifproxy->iid))
685 *ifproxy_found = ifproxy;
690 LeaveCriticalSection(&This->cs);
695 static void proxy_manager_disconnect(struct proxy_manager * This)
697 struct list * cursor;
699 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This->oxid),
700 wine_dbgstr_longlong(This->oid));
702 /* SORFP_NOLIFTIMEMGMT proxies (for IRemUnknown) shouldn't be
703 * disconnected - it won't do anything anyway, except cause
704 * problems for other objects that depend on this proxy always
706 if (This->sorflags & SORFP_NOLIFETIMEMGMT) return;
708 EnterCriticalSection(&This->cs);
710 LIST_FOR_EACH(cursor, &This->interfaces)
712 struct ifproxy * ifproxy = LIST_ENTRY(cursor, struct ifproxy, entry);
713 ifproxy_disconnect(ifproxy);
716 /* apartment is being destroyed so don't keep a pointer around to it */
719 LeaveCriticalSection(&This->cs);
722 static HRESULT proxy_manager_get_remunknown(struct proxy_manager * This, IRemUnknown **remunk)
726 /* we don't want to try and unmarshal or use IRemUnknown if we don't want
727 * lifetime management */
728 if (This->sorflags & SORFP_NOLIFETIMEMGMT)
731 EnterCriticalSection(&This->cs);
733 /* already created - return existing object */
734 *remunk = This->remunk;
735 else if (!This->parent)
736 /* disconnected - we can't create IRemUnknown */
741 /* Don't want IRemUnknown lifetime management as this is IRemUnknown!
742 * We also don't care about whether or not the stub is still alive */
743 stdobjref.flags = SORFP_NOLIFETIMEMGMT | SORF_NOPING;
744 stdobjref.cPublicRefs = 1;
745 /* oxid of destination object */
746 stdobjref.oxid = This->oxid;
747 /* FIXME: what should be used for the oid? The DCOM draft doesn't say */
748 stdobjref.oid = (OID)-1;
749 /* FIXME: this is a hack around not having an OXID resolver yet -
750 * the OXID resolver should give us the IPID of the IRemUnknown
752 stdobjref.ipid.Data1 = 0xffffffff;
753 stdobjref.ipid.Data2 = 0xffff;
754 stdobjref.ipid.Data3 = 0xffff;
755 assert(sizeof(stdobjref.ipid.Data4) == sizeof(stdobjref.oxid));
756 memcpy(&stdobjref.ipid.Data4, &stdobjref.oxid, sizeof(OXID));
758 /* do the unmarshal */
759 hr = unmarshal_object(&stdobjref, This->parent, &IID_IRemUnknown, (void**)&This->remunk);
761 *remunk = This->remunk;
763 LeaveCriticalSection(&This->cs);
765 TRACE("got IRemUnknown* pointer %p, hr = 0x%08lx\n", *remunk, hr);
770 /* destroys a proxy manager, freeing the memory it used.
771 * Note: this function should not be called from a list iteration in the
772 * apartment, due to the fact that it removes itself from the apartment and
773 * it could add a proxy to IRemUnknown into the apartment. */
774 static void proxy_manager_destroy(struct proxy_manager * This)
776 struct list * cursor;
778 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This->oxid),
779 wine_dbgstr_longlong(This->oid));
783 EnterCriticalSection(&This->parent->cs);
785 /* remove ourself from the list of proxy objects in the apartment */
786 LIST_FOR_EACH(cursor, &This->parent->proxies)
788 if (cursor == &This->entry)
790 list_remove(&This->entry);
795 LeaveCriticalSection(&This->parent->cs);
798 /* destroy all of the interface proxies */
799 while ((cursor = list_head(&This->interfaces)))
801 struct ifproxy * ifproxy = LIST_ENTRY(cursor, struct ifproxy, entry);
802 ifproxy_destroy(ifproxy);
805 if (This->remunk) IRemUnknown_Release(This->remunk);
807 DEBUG_CLEAR_CRITSEC_NAME(&This->cs);
808 DeleteCriticalSection(&This->cs);
810 CloseHandle(This->remoting_mutex);
812 HeapFree(GetProcessHeap(), 0, This);
815 /* finds the proxy manager corresponding to a given OXID and OID that has
816 * been unmarshaled in the specified apartment. The caller must release the
817 * reference to the proxy_manager when the object is no longer used. */
818 static BOOL find_proxy_manager(APARTMENT * apt, OXID oxid, OID oid, struct proxy_manager ** proxy_found)
821 struct list * cursor;
823 EnterCriticalSection(&apt->cs);
824 LIST_FOR_EACH(cursor, &apt->proxies)
826 struct proxy_manager * proxy = LIST_ENTRY(cursor, struct proxy_manager, entry);
827 if ((oxid == proxy->oxid) && (oid == proxy->oid))
829 *proxy_found = proxy;
830 ClientIdentity_AddRef((IMultiQI *)&proxy->lpVtbl);
835 LeaveCriticalSection(&apt->cs);
839 HRESULT apartment_disconnectproxies(struct apartment *apt)
841 struct list * cursor;
843 LIST_FOR_EACH(cursor, &apt->proxies)
845 struct proxy_manager * proxy = LIST_ENTRY(cursor, struct proxy_manager, entry);
846 proxy_manager_disconnect(proxy);
852 /********************** StdMarshal implementation ****************************/
853 typedef struct _StdMarshalImpl
855 const IMarshalVtbl *lpvtbl;
860 LPVOID pvDestContext;
864 static HRESULT WINAPI
865 StdMarshalImpl_QueryInterface(LPMARSHAL iface, REFIID riid, LPVOID *ppv)
868 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IMarshal, riid))
871 IUnknown_AddRef(iface);
874 FIXME("No interface for %s.\n", debugstr_guid(riid));
875 return E_NOINTERFACE;
879 StdMarshalImpl_AddRef(LPMARSHAL iface)
881 StdMarshalImpl *This = (StdMarshalImpl *)iface;
882 return InterlockedIncrement(&This->ref);
886 StdMarshalImpl_Release(LPMARSHAL iface)
888 StdMarshalImpl *This = (StdMarshalImpl *)iface;
889 ULONG ref = InterlockedDecrement(&This->ref);
891 if (!ref) HeapFree(GetProcessHeap(),0,This);
895 static HRESULT WINAPI
896 StdMarshalImpl_GetUnmarshalClass(
897 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
898 void* pvDestContext, DWORD mshlflags, CLSID* pCid)
900 *pCid = CLSID_DfMarshal;
904 static HRESULT WINAPI
905 StdMarshalImpl_GetMarshalSizeMax(
906 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
907 void* pvDestContext, DWORD mshlflags, DWORD* pSize)
909 *pSize = sizeof(STDOBJREF);
913 static HRESULT WINAPI
914 StdMarshalImpl_MarshalInterface(
915 LPMARSHAL iface, IStream *pStm,REFIID riid, void* pv, DWORD dwDestContext,
916 void* pvDestContext, DWORD mshlflags)
921 APARTMENT *apt = COM_CurrentApt();
923 TRACE("(...,%s,...)\n", debugstr_guid(riid));
927 ERR("Apartment not initialized\n");
928 return CO_E_NOTINITIALIZED;
931 /* make sure this apartment can be reached from other threads / processes */
932 RPC_StartRemoting(apt);
934 hres = marshal_object(apt, &stdobjref, riid, (IUnknown *)pv, mshlflags);
937 ERR("Failed to create ifstub, hres=0x%lx\n", hres);
941 hres = IStream_Write(pStm, &stdobjref, sizeof(stdobjref), &res);
942 if (hres) return hres;
947 /* helper for StdMarshalImpl_UnmarshalInterface - does the unmarshaling with
948 * no questions asked about the rules surrounding same-apartment unmarshals
949 * and table marshaling */
950 static HRESULT unmarshal_object(const STDOBJREF *stdobjref, APARTMENT *apt, REFIID riid, void **object)
952 struct proxy_manager *proxy_manager = NULL;
957 TRACE("stdobjref:\n\tflags = %04lx\n\tcPublicRefs = %ld\n\toxid = %s\n\toid = %s\n\tipid = %s\n",
958 stdobjref->flags, stdobjref->cPublicRefs,
959 wine_dbgstr_longlong(stdobjref->oxid),
960 wine_dbgstr_longlong(stdobjref->oid),
961 debugstr_guid(&stdobjref->ipid));
963 /* create an a new proxy manager if one doesn't already exist for the
965 if (!find_proxy_manager(apt, stdobjref->oxid, stdobjref->oid, &proxy_manager))
967 hr = proxy_manager_construct(apt, stdobjref->flags,
968 stdobjref->oxid, stdobjref->oid,
972 TRACE("proxy manager already created, using\n");
976 struct ifproxy * ifproxy;
977 hr = proxy_manager_find_ifproxy(proxy_manager, riid, &ifproxy);
978 if (hr == E_NOINTERFACE)
980 IRpcChannelBuffer *chanbuf;
981 hr = RPC_CreateClientChannel(&stdobjref->oxid, &stdobjref->ipid, &chanbuf);
983 hr = proxy_manager_create_ifproxy(proxy_manager, stdobjref,
984 riid, chanbuf, &ifproxy);
989 ClientIdentity_AddRef((IMultiQI*)&proxy_manager->lpVtbl);
990 *object = ifproxy->iface;
994 /* release our reference to the proxy manager - the client/apartment
995 * will hold on to the remaining reference for us */
996 if (proxy_manager) ClientIdentity_Release((IMultiQI*)&proxy_manager->lpVtbl);
1001 static HRESULT WINAPI
1002 StdMarshalImpl_UnmarshalInterface(LPMARSHAL iface, IStream *pStm, REFIID riid, void **ppv)
1004 struct stub_manager *stubmgr;
1005 STDOBJREF stdobjref;
1008 APARTMENT *apt = COM_CurrentApt();
1009 APARTMENT *stub_apt;
1012 TRACE("(...,%s,....)\n", debugstr_guid(riid));
1014 /* we need an apartment to unmarshal into */
1017 ERR("Apartment not initialized\n");
1018 return CO_E_NOTINITIALIZED;
1021 /* read STDOBJREF from wire */
1022 hres = IStream_Read(pStm, &stdobjref, sizeof(stdobjref), &res);
1023 if (hres) return hres;
1025 hres = apartment_getoxid(apt, &oxid);
1026 if (hres) return hres;
1028 /* check if we're marshalling back to ourselves */
1029 if ((oxid == stdobjref.oxid) && (stubmgr = get_stub_manager(apt, stdobjref.oid)))
1031 TRACE("Unmarshalling object marshalled in same apartment for iid %s, "
1032 "returning original object %p\n", debugstr_guid(riid), stubmgr->object);
1034 hres = IUnknown_QueryInterface(stubmgr->object, riid, ppv);
1036 /* unref the ifstub. FIXME: only do this on success? */
1037 if (!stub_manager_is_table_marshaled(stubmgr))
1038 stub_manager_ext_release(stubmgr, stdobjref.cPublicRefs);
1040 stub_manager_int_release(stubmgr);
1044 /* notify stub manager about unmarshal if process-local object.
1045 * note: if the oxid is not found then we and native will quite happily
1046 * ignore table marshaling and normal marshaling rules regarding number of
1047 * unmarshals, etc, but if you abuse these rules then your proxy could end
1048 * up returning RPC_E_DISCONNECTED. */
1049 if ((stub_apt = apartment_findfromoxid(stdobjref.oxid, TRUE)))
1051 if ((stubmgr = get_stub_manager(stub_apt, stdobjref.oid)))
1053 if (!stub_manager_notify_unmarshal(stubmgr))
1054 hres = CO_E_OBJNOTCONNECTED;
1056 stub_manager_int_release(stubmgr);
1060 WARN("Couldn't find object for OXID %s, OID %s, assuming disconnected\n",
1061 wine_dbgstr_longlong(stdobjref.oxid),
1062 wine_dbgstr_longlong(stdobjref.oid));
1063 hres = CO_E_OBJNOTCONNECTED;
1066 apartment_release(stub_apt);
1069 TRACE("Treating unmarshal from OXID %s as inter-process\n",
1070 wine_dbgstr_longlong(stdobjref.oxid));
1073 hres = unmarshal_object(&stdobjref, apt, riid, ppv);
1075 if (hres) WARN("Failed with error 0x%08lx\n", hres);
1076 else TRACE("Successfully created proxy %p\n", *ppv);
1081 static HRESULT WINAPI
1082 StdMarshalImpl_ReleaseMarshalData(LPMARSHAL iface, IStream *pStm)
1084 STDOBJREF stdobjref;
1087 struct stub_manager *stubmgr;
1090 TRACE("iface=%p, pStm=%p\n", iface, pStm);
1092 hres = IStream_Read(pStm, &stdobjref, sizeof(stdobjref), &res);
1093 if (hres) return hres;
1095 TRACE("oxid = %s, oid = %s, ipid = %s\n",
1096 wine_dbgstr_longlong(stdobjref.oxid),
1097 wine_dbgstr_longlong(stdobjref.oid),
1098 wine_dbgstr_guid(&stdobjref.ipid));
1100 if (!(apt = apartment_findfromoxid(stdobjref.oxid, TRUE)))
1102 WARN("Could not map OXID %s to apartment object\n",
1103 wine_dbgstr_longlong(stdobjref.oxid));
1104 return RPC_E_INVALID_OBJREF;
1107 if (!(stubmgr = get_stub_manager(apt, stdobjref.oid)))
1109 ERR("could not map MID to stub manager, oxid=%s, oid=%s\n",
1110 wine_dbgstr_longlong(stdobjref.oxid), wine_dbgstr_longlong(stdobjref.oid));
1111 return RPC_E_INVALID_OBJREF;
1114 stub_manager_release_marshal_data(stubmgr, stdobjref.cPublicRefs);
1116 stub_manager_int_release(stubmgr);
1117 apartment_release(apt);
1122 static HRESULT WINAPI
1123 StdMarshalImpl_DisconnectObject(LPMARSHAL iface, DWORD dwReserved)
1125 FIXME("(), stub!\n");
1129 static const IMarshalVtbl VT_StdMarshal =
1131 StdMarshalImpl_QueryInterface,
1132 StdMarshalImpl_AddRef,
1133 StdMarshalImpl_Release,
1134 StdMarshalImpl_GetUnmarshalClass,
1135 StdMarshalImpl_GetMarshalSizeMax,
1136 StdMarshalImpl_MarshalInterface,
1137 StdMarshalImpl_UnmarshalInterface,
1138 StdMarshalImpl_ReleaseMarshalData,
1139 StdMarshalImpl_DisconnectObject
1142 static HRESULT StdMarshalImpl_Construct(REFIID riid, void** ppvObject)
1144 StdMarshalImpl * pStdMarshal =
1145 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(StdMarshalImpl));
1147 return E_OUTOFMEMORY;
1148 pStdMarshal->lpvtbl = &VT_StdMarshal;
1149 pStdMarshal->ref = 0;
1150 return IMarshal_QueryInterface((IMarshal*)pStdMarshal, riid, ppvObject);
1153 /***********************************************************************
1154 * CoGetStandardMarshal [OLE32.@]
1156 * Gets or creates a standard marshal object.
1159 * riid [I] Interface identifier of the pUnk object.
1160 * pUnk [I] Optional. Object to get the marshal object for.
1161 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1162 * pvDestContext [I] Reserved. Must be NULL.
1163 * mshlflags [I] Flags affecting the marshaling process.
1164 * ppMarshal [O] Address where marshal object will be stored.
1168 * Failure: HRESULT code.
1172 * The function retrieves the IMarshal object associated with an object if
1173 * that object is currently an active stub, otherwise a new marshal object is
1176 HRESULT WINAPI CoGetStandardMarshal(REFIID riid, IUnknown *pUnk,
1177 DWORD dwDestContext, LPVOID pvDestContext,
1178 DWORD mshlflags, LPMARSHAL *ppMarshal)
1184 FIXME("(%s,NULL,%lx,%p,%lx,%p), unimplemented yet.\n",
1185 debugstr_guid(riid),dwDestContext,pvDestContext,mshlflags,ppMarshal);
1188 TRACE("(%s,%p,%lx,%p,%lx,%p)\n",
1189 debugstr_guid(riid),pUnk,dwDestContext,pvDestContext,mshlflags,ppMarshal);
1190 *ppMarshal = HeapAlloc(GetProcessHeap(),0,sizeof(StdMarshalImpl));
1191 dm = (StdMarshalImpl*) *ppMarshal;
1192 if (!dm) return E_FAIL;
1193 dm->lpvtbl = &VT_StdMarshal;
1197 dm->dwDestContext = dwDestContext;
1198 dm->pvDestContext = pvDestContext;
1199 dm->mshlflags = mshlflags;
1203 /***********************************************************************
1204 * get_marshaler [internal]
1206 * Retrieves an IMarshal interface for an object.
1208 static HRESULT get_marshaler(REFIID riid, IUnknown *pUnk, DWORD dwDestContext,
1209 void *pvDestContext, DWORD mshlFlags,
1210 LPMARSHAL *pMarshal)
1216 hr = IUnknown_QueryInterface(pUnk, &IID_IMarshal, (LPVOID*)pMarshal);
1218 hr = CoGetStandardMarshal(riid, pUnk, dwDestContext, pvDestContext,
1219 mshlFlags, pMarshal);
1223 /***********************************************************************
1224 * get_unmarshaler_from_stream [internal]
1226 * Creates an IMarshal* object according to the data marshaled to the stream.
1227 * The function leaves the stream pointer at the start of the data written
1228 * to the stream by the IMarshal* object.
1230 static HRESULT get_unmarshaler_from_stream(IStream *stream, IMarshal **marshal, IID *iid)
1236 /* read common OBJREF header */
1237 hr = IStream_Read(stream, &objref, FIELD_OFFSET(OBJREF, u_objref), &res);
1238 if (hr || (res != FIELD_OFFSET(OBJREF, u_objref)))
1240 ERR("Failed to read common OBJREF header, 0x%08lx\n", hr);
1241 return STG_E_READFAULT;
1244 /* sanity check on header */
1245 if (objref.signature != OBJREF_SIGNATURE)
1247 ERR("Bad OBJREF signature 0x%08lx\n", objref.signature);
1248 return RPC_E_INVALID_OBJREF;
1251 if (iid) *iid = objref.iid;
1253 /* FIXME: handler marshaling */
1254 if (objref.flags & OBJREF_STANDARD)
1256 TRACE("Using standard unmarshaling\n");
1257 hr = StdMarshalImpl_Construct(&IID_IMarshal, (LPVOID*)marshal);
1259 else if (objref.flags & OBJREF_CUSTOM)
1261 ULONG custom_header_size = FIELD_OFFSET(OBJREF, u_objref.u_custom.pData) -
1262 FIELD_OFFSET(OBJREF, u_objref.u_custom);
1263 TRACE("Using custom unmarshaling\n");
1264 /* read constant sized OR_CUSTOM data from stream */
1265 hr = IStream_Read(stream, &objref.u_objref.u_custom,
1266 custom_header_size, &res);
1267 if (hr || (res != custom_header_size))
1269 ERR("Failed to read OR_CUSTOM header, 0x%08lx\n", hr);
1270 return STG_E_READFAULT;
1272 /* now create the marshaler specified in the stream */
1273 hr = CoCreateInstance(&objref.u_objref.u_custom.clsid, NULL,
1274 CLSCTX_INPROC_SERVER, &IID_IMarshal,
1279 FIXME("Invalid or unimplemented marshaling type specified: %lx\n",
1281 return RPC_E_INVALID_OBJREF;
1285 ERR("Failed to create marshal, 0x%08lx\n", hr);
1290 /***********************************************************************
1291 * CoGetMarshalSizeMax [OLE32.@]
1293 * Gets the maximum amount of data that will be needed by a marshal.
1296 * pulSize [O] Address where maximum marshal size will be stored.
1297 * riid [I] Identifier of the interface to marshal.
1298 * pUnk [I] Pointer to the object to marshal.
1299 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1300 * pvDestContext [I] Reserved. Must be NULL.
1301 * mshlFlags [I] Flags that affect the marshaling. See CoMarshalInterface().
1305 * Failure: HRESULT code.
1308 * CoMarshalInterface().
1310 HRESULT WINAPI CoGetMarshalSizeMax(ULONG *pulSize, REFIID riid, IUnknown *pUnk,
1311 DWORD dwDestContext, void *pvDestContext,
1316 CLSID marshaler_clsid;
1318 hr = get_marshaler(riid, pUnk, dwDestContext, pvDestContext, mshlFlags, &pMarshal);
1322 hr = IMarshal_GetUnmarshalClass(pMarshal, riid, pUnk, dwDestContext,
1323 pvDestContext, mshlFlags, &marshaler_clsid);
1326 ERR("IMarshal::GetUnmarshalClass failed, 0x%08lx\n", hr);
1327 IMarshal_Release(pMarshal);
1331 hr = IMarshal_GetMarshalSizeMax(pMarshal, riid, pUnk, dwDestContext,
1332 pvDestContext, mshlFlags, pulSize);
1333 /* add on the size of the common header */
1334 *pulSize += FIELD_OFFSET(OBJREF, u_objref);
1336 /* if custom marshaling, add on size of custom header */
1337 if (!IsEqualCLSID(&marshaler_clsid, &CLSID_DfMarshal))
1338 *pulSize += FIELD_OFFSET(OBJREF, u_objref.u_custom.pData) -
1339 FIELD_OFFSET(OBJREF, u_objref.u_custom);
1341 IMarshal_Release(pMarshal);
1346 /***********************************************************************
1347 * CoMarshalInterface [OLE32.@]
1349 * Marshals an interface into a stream so that the object can then be
1350 * unmarshaled from another COM apartment and used remotely.
1353 * pStream [I] Stream the object will be marshaled into.
1354 * riid [I] Identifier of the interface to marshal.
1355 * pUnk [I] Pointer to the object to marshal.
1356 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1357 * pvDestContext [I] Reserved. Must be NULL.
1358 * mshlFlags [I] Flags that affect the marshaling. See notes.
1362 * Failure: HRESULT code.
1366 * The mshlFlags parameter can take one or more of the following flags:
1367 *| MSHLFLAGS_NORMAL - Unmarshal once, releases stub on last proxy release.
1368 *| MSHLFLAGS_TABLESTRONG - Unmarshal many, release when CoReleaseMarshalData() called.
1369 *| MSHLFLAGS_TABLEWEAK - Unmarshal many, releases stub on last proxy release.
1370 *| MSHLFLAGS_NOPING - No automatic garbage collection (and so reduces network traffic).
1372 * If a marshaled object is not unmarshaled, then CoReleaseMarshalData() must
1373 * be called in order to release the resources used in the marshaling.
1376 * CoUnmarshalInterface(), CoReleaseMarshalData().
1378 HRESULT WINAPI CoMarshalInterface(IStream *pStream, REFIID riid, IUnknown *pUnk,
1379 DWORD dwDestContext, void *pvDestContext,
1383 CLSID marshaler_clsid;
1387 TRACE("(%p, %s, %p, %lx, %p, %lx)\n", pStream, debugstr_guid(riid), pUnk,
1388 dwDestContext, pvDestContext, mshlFlags);
1391 return E_INVALIDARG;
1393 objref.signature = OBJREF_SIGNATURE;
1396 /* get the marshaler for the specified interface */
1397 hr = get_marshaler(riid, pUnk, dwDestContext, pvDestContext, mshlFlags, &pMarshal);
1400 ERR("Failed to get marshaller, 0x%08lx\n", hr);
1404 hr = IMarshal_GetUnmarshalClass(pMarshal, riid, pUnk, dwDestContext,
1405 pvDestContext, mshlFlags, &marshaler_clsid);
1408 ERR("IMarshal::GetUnmarshalClass failed, 0x%08lx\n", hr);
1412 /* FIXME: implement handler marshaling too */
1413 if (IsEqualCLSID(&marshaler_clsid, &CLSID_DfMarshal))
1415 TRACE("Using standard marshaling\n");
1416 objref.flags = OBJREF_STANDARD;
1418 /* write the common OBJREF header to the stream */
1419 hr = IStream_Write(pStream, &objref, FIELD_OFFSET(OBJREF, u_objref), NULL);
1422 ERR("Failed to write OBJREF header to stream, 0x%08lx\n", hr);
1428 TRACE("Using custom marshaling\n");
1429 objref.flags = OBJREF_CUSTOM;
1430 objref.u_objref.u_custom.clsid = marshaler_clsid;
1431 objref.u_objref.u_custom.cbExtension = 0;
1432 objref.u_objref.u_custom.size = 0;
1433 hr = IMarshal_GetMarshalSizeMax(pMarshal, riid, pUnk, dwDestContext,
1434 pvDestContext, mshlFlags,
1435 &objref.u_objref.u_custom.size);
1438 ERR("Failed to get max size of marshal data, error 0x%08lx\n", hr);
1441 /* write constant sized common header and OR_CUSTOM data into stream */
1442 hr = IStream_Write(pStream, &objref,
1443 FIELD_OFFSET(OBJREF, u_objref.u_custom.pData), NULL);
1446 ERR("Failed to write OR_CUSTOM header to stream with 0x%08lx\n", hr);
1451 TRACE("Calling IMarshal::MarshalInterace\n");
1452 /* call helper object to do the actual marshaling */
1453 hr = IMarshal_MarshalInterface(pMarshal, pStream, riid, pUnk, dwDestContext,
1454 pvDestContext, mshlFlags);
1458 ERR("Failed to marshal the interface %s, %lx\n", debugstr_guid(riid), hr);
1463 IMarshal_Release(pMarshal);
1465 TRACE("completed with hr 0x%08lx\n", hr);
1470 /***********************************************************************
1471 * CoUnmarshalInterface [OLE32.@]
1473 * Unmarshals an object from a stream by creating a proxy to the remote
1474 * object, if necessary.
1478 * pStream [I] Stream containing the marshaled object.
1479 * riid [I] Interface identifier of the object to create a proxy to.
1480 * ppv [O] Address where proxy will be stored.
1485 * Failure: HRESULT code.
1488 * CoMarshalInterface().
1490 HRESULT WINAPI CoUnmarshalInterface(IStream *pStream, REFIID riid, LPVOID *ppv)
1497 TRACE("(%p, %s, %p)\n", pStream, debugstr_guid(riid), ppv);
1499 hr = get_unmarshaler_from_stream(pStream, &pMarshal, &iid);
1503 /* call the helper object to do the actual unmarshaling */
1504 hr = IMarshal_UnmarshalInterface(pMarshal, pStream, &iid, (LPVOID*)&object);
1506 ERR("IMarshal::UnmarshalInterface failed, 0x%08lx\n", hr);
1508 /* IID_NULL means use the interface ID of the marshaled object */
1509 if (!IsEqualIID(riid, &IID_NULL))
1514 if (!IsEqualIID(riid, &iid))
1516 TRACE("requested interface != marshalled interface, additional QI needed\n");
1517 hr = IUnknown_QueryInterface(object, &iid, ppv);
1519 ERR("Couldn't query for interface %s, hr = 0x%08lx\n",
1520 debugstr_guid(riid), hr);
1521 IUnknown_Release(object);
1529 IMarshal_Release(pMarshal);
1531 TRACE("completed with hr 0x%lx\n", hr);
1536 /***********************************************************************
1537 * CoReleaseMarshalData [OLE32.@]
1539 * Releases resources associated with an object that has been marshaled into
1544 * pStream [I] The stream that the object has been marshaled into.
1548 * Failure: HRESULT error code.
1552 * Call this function to release resources associated with a normal or
1553 * table-weak marshal that will not be unmarshaled, and all table-strong
1554 * marshals when they are no longer needed.
1557 * CoMarshalInterface(), CoUnmarshalInterface().
1559 HRESULT WINAPI CoReleaseMarshalData(IStream *pStream)
1564 TRACE("(%p)\n", pStream);
1566 hr = get_unmarshaler_from_stream(pStream, &pMarshal, NULL);
1570 /* call the helper object to do the releasing of marshal data */
1571 hr = IMarshal_ReleaseMarshalData(pMarshal, pStream);
1573 ERR("IMarshal::ReleaseMarshalData failed with error 0x%08lx\n", hr);
1575 IMarshal_Release(pMarshal);
1580 /***********************************************************************
1581 * CoMarshalInterThreadInterfaceInStream [OLE32.@]
1583 * Marshal an interface across threads in the same process.
1586 * riid [I] Identifier of the interface to be marshalled.
1587 * pUnk [I] Pointer to IUnknown-derived interface that will be marshalled.
1588 * ppStm [O] Pointer to IStream object that is created and then used to store the marshalled inteface.
1592 * Failure: E_OUTOFMEMORY and other COM error codes
1595 * CoMarshalInterface(), CoUnmarshalInterface() and CoGetInterfaceAndReleaseStream()
1597 HRESULT WINAPI CoMarshalInterThreadInterfaceInStream(
1598 REFIID riid, LPUNKNOWN pUnk, LPSTREAM * ppStm)
1600 ULARGE_INTEGER xpos;
1601 LARGE_INTEGER seekto;
1604 TRACE("(%s, %p, %p)\n",debugstr_guid(riid), pUnk, ppStm);
1606 hres = CreateStreamOnHGlobal(0, TRUE, ppStm);
1607 if (FAILED(hres)) return hres;
1608 hres = CoMarshalInterface(*ppStm, riid, pUnk, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1610 /* FIXME: is this needed? */
1611 memset(&seekto,0,sizeof(seekto));
1612 IStream_Seek(*ppStm,seekto,SEEK_SET,&xpos);
1617 /***********************************************************************
1618 * CoGetInterfaceAndReleaseStream [OLE32.@]
1620 * Unmarshalls an inteface from a stream and then releases the stream.
1623 * pStm [I] Stream that contains the marshalled inteface.
1624 * riid [I] Interface identifier of the object to unmarshall.
1625 * ppv [O] Address of pointer where the requested interface object will be stored.
1629 * Failure: A COM error code
1632 * CoMarshalInterThreadInterfaceInStream() and CoUnmarshalInteface()
1634 HRESULT WINAPI CoGetInterfaceAndReleaseStream(LPSTREAM pStm, REFIID riid,
1639 TRACE("(%p, %s, %p)\n", pStm, debugstr_guid(riid), ppv);
1641 hres = CoUnmarshalInterface(pStm, riid, ppv);
1642 IStream_Release(pStm);
1646 static HRESULT WINAPI StdMarshalCF_QueryInterface(LPCLASSFACTORY iface,
1647 REFIID riid, LPVOID *ppv)
1650 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory))
1652 *ppv = (LPVOID)iface;
1655 return E_NOINTERFACE;
1658 static ULONG WINAPI StdMarshalCF_AddRef(LPCLASSFACTORY iface)
1660 return 2; /* non-heap based object */
1663 static ULONG WINAPI StdMarshalCF_Release(LPCLASSFACTORY iface)
1665 return 1; /* non-heap based object */
1668 static HRESULT WINAPI StdMarshalCF_CreateInstance(LPCLASSFACTORY iface,
1669 LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv)
1671 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IMarshal))
1672 return StdMarshalImpl_Construct(riid, ppv);
1674 FIXME("(%s), not supported.\n",debugstr_guid(riid));
1675 return E_NOINTERFACE;
1678 static HRESULT WINAPI StdMarshalCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
1680 FIXME("(%d), stub!\n",fLock);
1684 static const IClassFactoryVtbl StdMarshalCFVtbl =
1686 StdMarshalCF_QueryInterface,
1687 StdMarshalCF_AddRef,
1688 StdMarshalCF_Release,
1689 StdMarshalCF_CreateInstance,
1690 StdMarshalCF_LockServer
1692 static const IClassFactoryVtbl *StdMarshalCF = &StdMarshalCFVtbl;
1694 HRESULT MARSHAL_GetStandardMarshalCF(LPVOID *ppv)
1696 *ppv = &StdMarshalCF;
1700 /***********************************************************************
1701 * CoMarshalHresult [OLE32.@]
1703 * Marshals an HRESULT value into a stream.
1706 * pStm [I] Stream that hresult will be marshalled into.
1707 * hresult [I] HRESULT to be marshalled.
1711 * Failure: A COM error code
1714 * CoUnmarshalHresult().
1716 HRESULT WINAPI CoMarshalHresult(LPSTREAM pStm, HRESULT hresult)
1718 return IStream_Write(pStm, &hresult, sizeof(hresult), NULL);
1721 /***********************************************************************
1722 * CoUnmarshalHresult [OLE32.@]
1724 * Unmarshals an HRESULT value from a stream.
1727 * pStm [I] Stream that hresult will be unmarshalled from.
1728 * phresult [I] Pointer to HRESULT where the value will be unmarshalled to.
1732 * Failure: A COM error code
1735 * CoMarshalHresult().
1737 HRESULT WINAPI CoUnmarshalHresult(LPSTREAM pStm, HRESULT * phresult)
1739 return IStream_Read(pStm, phresult, sizeof(*phresult), NULL);