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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
35 #include "wine/unicode.h"
37 #include "compobj_private.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(ole);
43 extern const CLSID CLSID_DfMarshal;
45 /* number of refs given out for normal marshaling */
46 #define NORMALEXTREFS 5
48 /* private flag indicating that the caller does not want to notify the stub
49 * when the proxy disconnects or is destroyed */
50 #define SORFP_NOLIFETIMEMGMT SORF_OXRES1
52 static HRESULT unmarshal_object(const STDOBJREF *stdobjref, APARTMENT *apt,
53 MSHCTX dest_context, void *dest_context_data,
54 REFIID riid, void **object);
56 /* Marshalling just passes a unique identifier to the remote client,
57 * that makes it possible to find the passed interface again.
59 * So basically we need a set of values that make it unique.
61 * Note that the IUnknown_QI(ob,xiid,&ppv) always returns the SAME ppv value!
63 * A triple is used: OXID (apt id), OID (stub manager id),
64 * IPID (interface ptr/stub id).
66 * OXIDs identify an apartment and are network scoped
67 * OIDs identify a stub manager and are apartment scoped
68 * IPIDs identify an interface stub and are apartment scoped
71 inline static HRESULT get_facbuf_for_iid(REFIID riid, IPSFactoryBuffer **facbuf)
76 if ((hr = CoGetPSClsid(riid, &clsid)))
78 return CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL,
79 &IID_IPSFactoryBuffer, (LPVOID*)facbuf);
82 /* marshals an object into a STDOBJREF structure */
83 HRESULT marshal_object(APARTMENT *apt, STDOBJREF *stdobjref, REFIID riid, IUnknown *object, MSHLFLAGS mshlflags)
85 struct stub_manager *manager;
86 struct ifstub *ifstub;
88 IRpcStubBuffer *stub = NULL;
90 IUnknown *iobject = NULL; /* object of type riid */
92 hr = apartment_getoxid(apt, &stdobjref->oxid);
96 hr = apartment_createwindowifneeded(apt);
100 hr = IUnknown_QueryInterface(object, riid, (void **)&iobject);
103 ERR("object doesn't expose interface %s, failing with error 0x%08x\n",
104 debugstr_guid(riid), hr);
105 return E_NOINTERFACE;
108 /* IUnknown doesn't require a stub buffer, because it never goes out on
110 if (!IsEqualIID(riid, &IID_IUnknown))
112 IPSFactoryBuffer *psfb;
114 hr = get_facbuf_for_iid(riid, &psfb);
117 ERR("couldn't get IPSFactory buffer for interface %s\n", debugstr_guid(riid));
118 IUnknown_Release(iobject);
122 hr = IPSFactoryBuffer_CreateStub(psfb, riid, iobject, &stub);
123 IPSFactoryBuffer_Release(psfb);
126 ERR("Failed to create an IRpcStubBuffer from IPSFactory for %s\n", debugstr_guid(riid));
127 IUnknown_Release(iobject);
132 if (mshlflags & MSHLFLAGS_NOPING)
133 stdobjref->flags = SORF_NOPING;
135 stdobjref->flags = SORF_NULL;
137 if ((manager = get_stub_manager_from_object(apt, object)))
138 TRACE("registering new ifstub on pre-existing manager\n");
141 TRACE("constructing new stub manager\n");
143 manager = new_stub_manager(apt, object);
146 if (stub) IRpcStubBuffer_Release(stub);
147 IUnknown_Release(iobject);
148 return E_OUTOFMEMORY;
151 stdobjref->oid = manager->oid;
153 tablemarshal = ((mshlflags & MSHLFLAGS_TABLESTRONG) || (mshlflags & MSHLFLAGS_TABLEWEAK));
155 /* make sure ifstub that we are creating is unique */
156 ifstub = stub_manager_find_ifstub(manager, riid, mshlflags);
158 ifstub = stub_manager_new_ifstub(manager, stub, iobject, riid, mshlflags);
160 if (stub) IRpcStubBuffer_Release(stub);
161 IUnknown_Release(iobject);
165 stub_manager_int_release(manager);
166 /* destroy the stub manager if it has no ifstubs by releasing
167 * zero external references */
168 stub_manager_ext_release(manager, 0, TRUE);
169 return E_OUTOFMEMORY;
174 stdobjref->cPublicRefs = NORMALEXTREFS;
175 stub_manager_ext_addref(manager, stdobjref->cPublicRefs);
179 stdobjref->cPublicRefs = 0;
180 if (mshlflags & MSHLFLAGS_TABLESTRONG)
181 stub_manager_ext_addref(manager, 1);
184 /* FIXME: check return value */
185 RPC_RegisterInterface(riid);
187 stdobjref->ipid = ifstub->ipid;
189 stub_manager_int_release(manager);
195 /* Client-side identity of the server object */
197 static HRESULT proxy_manager_get_remunknown(struct proxy_manager * This, IRemUnknown **remunk);
198 static void proxy_manager_destroy(struct proxy_manager * This);
199 static HRESULT proxy_manager_find_ifproxy(struct proxy_manager * This, REFIID riid, struct ifproxy ** ifproxy_found);
200 static HRESULT proxy_manager_query_local_interface(struct proxy_manager * This, REFIID riid, void ** ppv);
202 static HRESULT WINAPI ClientIdentity_QueryInterface(IMultiQI * iface, REFIID riid, void ** ppv)
207 TRACE("%s\n", debugstr_guid(riid));
210 hr = IMultiQI_QueryMultipleInterfaces(iface, 1, &mqi);
211 *ppv = (void *)mqi.pItf;
216 static ULONG WINAPI ClientIdentity_AddRef(IMultiQI * iface)
218 struct proxy_manager * This = (struct proxy_manager *)iface;
219 TRACE("%p - before %d\n", iface, This->refs);
220 return InterlockedIncrement(&This->refs);
223 static ULONG WINAPI ClientIdentity_Release(IMultiQI * iface)
225 struct proxy_manager * This = (struct proxy_manager *)iface;
226 ULONG refs = InterlockedDecrement(&This->refs);
227 TRACE("%p - after %d\n", iface, refs);
229 proxy_manager_destroy(This);
233 static HRESULT WINAPI ClientIdentity_QueryMultipleInterfaces(IMultiQI *iface, ULONG cMQIs, MULTI_QI *pMQIs)
235 struct proxy_manager * This = (struct proxy_manager *)iface;
236 REMQIRESULT *qiresults = NULL;
237 ULONG nonlocal_mqis = 0;
239 ULONG successful_mqis = 0;
240 IID *iids = HeapAlloc(GetProcessHeap(), 0, cMQIs * sizeof(*iids));
241 /* mapping of RemQueryInterface index to QueryMultipleInterfaces index */
242 ULONG *mapping = HeapAlloc(GetProcessHeap(), 0, cMQIs * sizeof(*mapping));
244 TRACE("cMQIs: %d\n", cMQIs);
246 /* try to get a local interface - this includes already active proxy
247 * interfaces and also interfaces exposed by the proxy manager */
248 for (i = 0; i < cMQIs; i++)
250 TRACE("iid[%d] = %s\n", i, debugstr_guid(pMQIs[i].pIID));
251 pMQIs[i].hr = proxy_manager_query_local_interface(This, pMQIs[i].pIID, (void **)&pMQIs[i].pItf);
252 if (pMQIs[i].hr == S_OK)
256 iids[nonlocal_mqis] = *pMQIs[i].pIID;
257 mapping[nonlocal_mqis] = i;
262 TRACE("%d interfaces not found locally\n", nonlocal_mqis);
264 /* if we have more than one interface not found locally then we must try
265 * to query the remote object for it */
266 if (nonlocal_mqis != 0)
272 /* get the ipid of the first entry */
273 /* FIXME: should we implement ClientIdentity on the ifproxies instead
274 * of the proxy_manager so we use the correct ipid here? */
275 ipid = &LIST_ENTRY(list_head(&This->interfaces), struct ifproxy, entry)->stdobjref.ipid;
277 /* get IRemUnknown proxy so we can communicate with the remote object */
278 hr = proxy_manager_get_remunknown(This, &remunk);
282 hr = IRemUnknown_RemQueryInterface(remunk, ipid, NORMALEXTREFS,
283 nonlocal_mqis, iids, &qiresults);
285 ERR("IRemUnknown_RemQueryInterface failed with error 0x%08x\n", hr);
288 /* IRemUnknown_RemQueryInterface can return S_FALSE if only some of
289 * the interfaces were returned */
292 /* try to unmarshal each object returned to us */
293 for (i = 0; i < nonlocal_mqis; i++)
295 ULONG index = mapping[i];
296 HRESULT hrobj = qiresults[i].hResult;
298 hrobj = unmarshal_object(&qiresults[i].std, This->parent,
300 This->dest_context_data,
302 (void **)&pMQIs[index].pItf);
307 ERR("Failed to get pointer to interface %s\n", debugstr_guid(pMQIs[index].pIID));
308 pMQIs[index].hr = hrobj;
312 /* free the memory allocated by the proxy */
313 CoTaskMemFree(qiresults);
316 TRACE("%d/%d successfully queried\n", successful_mqis, cMQIs);
318 HeapFree(GetProcessHeap(), 0, iids);
319 HeapFree(GetProcessHeap(), 0, mapping);
321 if (successful_mqis == cMQIs)
322 return S_OK; /* we got all requested interfaces */
323 else if (successful_mqis == 0)
324 return E_NOINTERFACE; /* we didn't get any interfaces */
326 return S_FALSE; /* we got some interfaces */
329 static const IMultiQIVtbl ClientIdentity_Vtbl =
331 ClientIdentity_QueryInterface,
332 ClientIdentity_AddRef,
333 ClientIdentity_Release,
334 ClientIdentity_QueryMultipleInterfaces
337 static HRESULT WINAPI Proxy_QueryInterface(IMarshal *iface, REFIID riid, void **ppvObject)
339 ICOM_THIS_MULTI(struct proxy_manager, lpVtblMarshal, iface);
340 return IMultiQI_QueryInterface((IMultiQI *)&This->lpVtbl, riid, ppvObject);
343 static ULONG WINAPI Proxy_AddRef(IMarshal *iface)
345 ICOM_THIS_MULTI(struct proxy_manager, lpVtblMarshal, iface);
346 return IMultiQI_AddRef((IMultiQI *)&This->lpVtbl);
349 /* FIXME: remove these */
350 static HRESULT WINAPI StdMarshalImpl_GetUnmarshalClass(LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext, void* pvDestContext, DWORD mshlflags, CLSID* pCid);
351 static HRESULT WINAPI StdMarshalImpl_GetMarshalSizeMax(LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext, void* pvDestContext, DWORD mshlflags, DWORD* pSize);
352 static HRESULT WINAPI StdMarshalImpl_UnmarshalInterface(LPMARSHAL iface, IStream *pStm, REFIID riid, void **ppv);
353 static HRESULT WINAPI StdMarshalImpl_ReleaseMarshalData(LPMARSHAL iface, IStream *pStm);
354 static HRESULT WINAPI StdMarshalImpl_DisconnectObject(LPMARSHAL iface, DWORD dwReserved);
356 static ULONG WINAPI Proxy_Release(IMarshal *iface)
358 ICOM_THIS_MULTI(struct proxy_manager, lpVtblMarshal, iface);
359 return IMultiQI_Release((IMultiQI *)&This->lpVtbl);
362 static HRESULT WINAPI Proxy_MarshalInterface(
363 LPMARSHAL iface, IStream *pStm, REFIID riid, void* pv, DWORD dwDestContext,
364 void* pvDestContext, DWORD mshlflags)
366 ICOM_THIS_MULTI(struct proxy_manager, lpVtblMarshal, iface);
368 struct ifproxy *ifproxy;
370 TRACE("(...,%s,...)\n", debugstr_guid(riid));
372 hr = proxy_manager_find_ifproxy(This, riid, &ifproxy);
375 STDOBJREF stdobjref = ifproxy->stdobjref;
376 ULONG cPublicRefs = ifproxy->refs;
377 ULONG cPublicRefsOld;
379 /* optimization - share out proxy's public references if possible
380 * instead of making new proxy do a roundtrip through the server */
383 ULONG cPublicRefsNew;
384 cPublicRefsOld = cPublicRefs;
385 stdobjref.cPublicRefs = cPublicRefs / 2;
386 cPublicRefsNew = cPublicRefs - stdobjref.cPublicRefs;
387 cPublicRefs = InterlockedCompareExchange(
388 (LONG *)&ifproxy->refs, cPublicRefsNew, cPublicRefsOld);
389 } while (cPublicRefs != cPublicRefsOld);
391 if (!stdobjref.cPublicRefs)
394 hr = proxy_manager_get_remunknown(This, &remunk);
397 HRESULT hrref = S_OK;
399 rif.ipid = ifproxy->stdobjref.ipid;
400 rif.cPublicRefs = NORMALEXTREFS;
401 rif.cPrivateRefs = 0;
402 hr = IRemUnknown_RemAddRef(remunk, 1, &rif, &hrref);
403 if (hr == S_OK && hrref == S_OK)
404 stdobjref.cPublicRefs = rif.cPublicRefs;
406 ERR("IRemUnknown_RemAddRef returned with 0x%08x, hrref = 0x%08x\n", hr, hrref);
412 TRACE("writing stdobjref:\n\tflags = %04lx\n\tcPublicRefs = %ld\n\toxid = %s\n\toid = %s\n\tipid = %s\n",
413 stdobjref.flags, stdobjref.cPublicRefs,
414 wine_dbgstr_longlong(stdobjref.oxid),
415 wine_dbgstr_longlong(stdobjref.oid),
416 debugstr_guid(&stdobjref.ipid));
417 hr = IStream_Write(pStm, &stdobjref, sizeof(stdobjref), NULL);
422 /* we don't have the interface already unmarshaled so we have to
423 * request the object from the server */
426 REMQIRESULT *qiresults = NULL;
429 /* get the ipid of the first entry */
430 /* FIXME: should we implement ClientIdentity on the ifproxies instead
431 * of the proxy_manager so we use the correct ipid here? */
432 ipid = &LIST_ENTRY(list_head(&This->interfaces), struct ifproxy, entry)->stdobjref.ipid;
434 /* get IRemUnknown proxy so we can communicate with the remote object */
435 hr = proxy_manager_get_remunknown(This, &remunk);
439 hr = IRemUnknown_RemQueryInterface(remunk, ipid, NORMALEXTREFS,
440 1, &iid, &qiresults);
443 hr = IStream_Write(pStm, &qiresults->std, sizeof(qiresults->std), NULL);
447 rif.ipid = qiresults->std.ipid;
448 rif.cPublicRefs = qiresults->std.cPublicRefs;
449 rif.cPrivateRefs = 0;
450 IRemUnknown_RemRelease(remunk, 1, &rif);
452 CoTaskMemFree(qiresults);
455 ERR("IRemUnknown_RemQueryInterface failed with error 0x%08x\n", hr);
462 static const IMarshalVtbl ProxyMarshal_Vtbl =
464 Proxy_QueryInterface,
467 StdMarshalImpl_GetUnmarshalClass,
468 StdMarshalImpl_GetMarshalSizeMax,
469 Proxy_MarshalInterface,
470 StdMarshalImpl_UnmarshalInterface,
471 StdMarshalImpl_ReleaseMarshalData,
472 StdMarshalImpl_DisconnectObject
475 static HRESULT ifproxy_get_public_ref(struct ifproxy * This)
479 if (WAIT_OBJECT_0 != WaitForSingleObject(This->parent->remoting_mutex, INFINITE))
481 ERR("Wait failed for ifproxy %p\n", This);
487 IRemUnknown *remunk = NULL;
489 TRACE("getting public ref for ifproxy %p\n", This);
491 hr = proxy_manager_get_remunknown(This->parent, &remunk);
494 HRESULT hrref = S_OK;
496 rif.ipid = This->stdobjref.ipid;
497 rif.cPublicRefs = NORMALEXTREFS;
498 rif.cPrivateRefs = 0;
499 hr = IRemUnknown_RemAddRef(remunk, 1, &rif, &hrref);
500 if (hr == S_OK && hrref == S_OK)
501 InterlockedExchangeAdd((LONG *)&This->refs, NORMALEXTREFS);
503 ERR("IRemUnknown_RemAddRef returned with 0x%08x, hrref = 0x%08x\n", hr, hrref);
506 ReleaseMutex(This->parent->remoting_mutex);
511 static HRESULT ifproxy_release_public_refs(struct ifproxy * This)
516 if (WAIT_OBJECT_0 != WaitForSingleObject(This->parent->remoting_mutex, INFINITE))
518 ERR("Wait failed for ifproxy %p\n", This);
522 public_refs = This->refs;
525 IRemUnknown *remunk = NULL;
527 TRACE("releasing %d refs\n", public_refs);
529 hr = proxy_manager_get_remunknown(This->parent, &remunk);
533 rif.ipid = This->stdobjref.ipid;
534 rif.cPublicRefs = public_refs;
535 rif.cPrivateRefs = 0;
536 hr = IRemUnknown_RemRelease(remunk, 1, &rif);
538 InterlockedExchangeAdd((LONG *)&This->refs, -public_refs);
539 else if (hr == RPC_E_DISCONNECTED)
540 WARN("couldn't release references because object was "
541 "disconnected: oxid = %s, oid = %s\n",
542 wine_dbgstr_longlong(This->parent->oxid),
543 wine_dbgstr_longlong(This->parent->oid));
545 ERR("IRemUnknown_RemRelease failed with error 0x%08x\n", hr);
548 ReleaseMutex(This->parent->remoting_mutex);
553 /* should be called inside This->parent->cs critical section */
554 static void ifproxy_disconnect(struct ifproxy * This)
556 ifproxy_release_public_refs(This);
557 if (This->proxy) IRpcProxyBuffer_Disconnect(This->proxy);
559 IRpcChannelBuffer_Release(This->chan);
563 /* should be called in This->parent->cs critical section if it is an entry in parent's list */
564 static void ifproxy_destroy(struct ifproxy * This)
568 /* release public references to this object so that the stub can know
569 * when to destroy itself */
570 ifproxy_release_public_refs(This);
572 list_remove(&This->entry);
576 IRpcChannelBuffer_Release(This->chan);
580 if (This->proxy) IRpcProxyBuffer_Release(This->proxy);
582 HeapFree(GetProcessHeap(), 0, This);
585 static HRESULT proxy_manager_construct(
586 APARTMENT * apt, ULONG sorflags, OXID oxid, OID oid,
587 struct proxy_manager ** proxy_manager)
589 struct proxy_manager * This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
590 if (!This) return E_OUTOFMEMORY;
592 This->remoting_mutex = CreateMutexW(NULL, FALSE, NULL);
593 if (!This->remoting_mutex)
595 HeapFree(GetProcessHeap(), 0, This);
596 return HRESULT_FROM_WIN32(GetLastError());
599 This->lpVtbl = &ClientIdentity_Vtbl;
600 This->lpVtblMarshal = &ProxyMarshal_Vtbl;
602 list_init(&This->entry);
603 list_init(&This->interfaces);
605 InitializeCriticalSection(&This->cs);
606 DEBUG_SET_CRITSEC_NAME(&This->cs, "proxy_manager");
608 /* the apartment the object was unmarshaled into */
611 /* the source apartment and id of the object */
617 /* the DCOM draft specification states that the SORF_NOPING flag is
618 * proxy manager specific, not ifproxy specific, so this implies that we
619 * should store the STDOBJREF flags here in the proxy manager. */
620 This->sorflags = sorflags;
622 /* we create the IRemUnknown proxy on demand */
625 /* initialise these values to the weakest values and they will be
626 * overwritten in proxy_manager_set_context */
627 This->dest_context = MSHCTX_INPROC;
628 This->dest_context_data = NULL;
630 EnterCriticalSection(&apt->cs);
631 /* FIXME: we are dependent on the ordering in here to make sure a proxy's
632 * IRemUnknown proxy doesn't get destroyed before the regual proxy does
633 * because we need the IRemUnknown proxy during the destruction of the
634 * regular proxy. Ideally, we should maintain a separate list for the
635 * IRemUnknown proxies that need late destruction */
636 list_add_tail(&apt->proxies, &This->entry);
637 LeaveCriticalSection(&apt->cs);
639 TRACE("%p created for OXID %s, OID %s\n", This,
640 wine_dbgstr_longlong(oxid), wine_dbgstr_longlong(oid));
642 *proxy_manager = This;
646 static inline void proxy_manager_set_context(struct proxy_manager *This, MSHCTX dest_context, void *dest_context_data)
648 MSHCTX old_dest_context = This->dest_context;
649 MSHCTX new_dest_context;
653 new_dest_context = old_dest_context;
654 /* "stronger" values overwrite "weaker" values. stronger values are
655 * ones that disable more optimisations */
656 switch (old_dest_context)
659 new_dest_context = dest_context;
661 case MSHCTX_CROSSCTX:
662 switch (dest_context)
667 new_dest_context = dest_context;
671 switch (dest_context)
674 case MSHCTX_CROSSCTX:
677 new_dest_context = dest_context;
680 case MSHCTX_NOSHAREDMEM:
681 switch (dest_context)
683 case MSHCTX_DIFFERENTMACHINE:
684 new_dest_context = dest_context;
694 if (old_dest_context == new_dest_context) break;
696 old_dest_context = InterlockedCompareExchange((PLONG)&This->dest_context, new_dest_context, old_dest_context);
697 } while (new_dest_context != old_dest_context);
699 if (dest_context_data)
700 InterlockedExchangePointer(&This->dest_context_data, dest_context_data);
703 static HRESULT proxy_manager_query_local_interface(struct proxy_manager * This, REFIID riid, void ** ppv)
706 struct ifproxy * ifproxy;
708 TRACE("%s\n", debugstr_guid(riid));
710 if (IsEqualIID(riid, &IID_IUnknown) ||
711 IsEqualIID(riid, &IID_IMultiQI))
713 *ppv = (void *)&This->lpVtbl;
714 IUnknown_AddRef((IUnknown *)*ppv);
717 if (IsEqualIID(riid, &IID_IMarshal))
719 *ppv = (void *)&This->lpVtblMarshal;
720 IUnknown_AddRef((IUnknown *)*ppv);
723 if (IsEqualIID(riid, &IID_IClientSecurity))
725 FIXME("requesting IClientSecurity, but it is unimplemented\n");
727 return E_NOINTERFACE;
730 hr = proxy_manager_find_ifproxy(This, riid, &ifproxy);
733 *ppv = ifproxy->iface;
734 IUnknown_AddRef((IUnknown *)*ppv);
739 return E_NOINTERFACE;
742 static HRESULT proxy_manager_create_ifproxy(
743 struct proxy_manager * This, const STDOBJREF *stdobjref, REFIID riid,
744 IRpcChannelBuffer * channel, struct ifproxy ** iif_out)
747 IPSFactoryBuffer * psfb;
748 struct ifproxy * ifproxy = HeapAlloc(GetProcessHeap(), 0, sizeof(*ifproxy));
749 if (!ifproxy) return E_OUTOFMEMORY;
751 list_init(&ifproxy->entry);
753 ifproxy->parent = This;
754 ifproxy->stdobjref = *stdobjref;
755 ifproxy->iid = *riid;
757 ifproxy->proxy = NULL;
760 ifproxy->chan = channel; /* FIXME: we should take the binding strings and construct the channel in this function */
762 /* the IUnknown interface is special because it does not have a
763 * proxy associated with the ifproxy as we handle IUnknown ourselves */
764 if (IsEqualIID(riid, &IID_IUnknown))
766 ifproxy->iface = (void *)&This->lpVtbl;
767 IMultiQI_AddRef((IMultiQI *)&This->lpVtbl);
772 hr = get_facbuf_for_iid(riid, &psfb);
775 /* important note: the outer unknown is set to the proxy manager.
776 * This ensures the COM identity rules are not violated, by having a
777 * one-to-one mapping of objects on the proxy side to objects on the
778 * stub side, no matter which interface you view the object through */
779 hr = IPSFactoryBuffer_CreateProxy(psfb, (IUnknown *)&This->lpVtbl, riid,
780 &ifproxy->proxy, &ifproxy->iface);
781 IPSFactoryBuffer_Release(psfb);
783 ERR("Could not create proxy for interface %s, error 0x%08x\n",
784 debugstr_guid(riid), hr);
787 ERR("Could not get IPSFactoryBuffer for interface %s, error 0x%08x\n",
788 debugstr_guid(riid), hr);
791 hr = IRpcProxyBuffer_Connect(ifproxy->proxy, ifproxy->chan);
796 EnterCriticalSection(&This->cs);
797 list_add_tail(&This->interfaces, &ifproxy->entry);
798 LeaveCriticalSection(&This->cs);
801 TRACE("ifproxy %p created for IPID %s, interface %s with %lu public refs\n",
802 ifproxy, debugstr_guid(&stdobjref->ipid), debugstr_guid(riid), stdobjref->cPublicRefs);
805 ifproxy_destroy(ifproxy);
810 static HRESULT proxy_manager_find_ifproxy(struct proxy_manager * This, REFIID riid, struct ifproxy ** ifproxy_found)
812 HRESULT hr = E_NOINTERFACE; /* assume not found */
813 struct list * cursor;
815 EnterCriticalSection(&This->cs);
816 LIST_FOR_EACH(cursor, &This->interfaces)
818 struct ifproxy * ifproxy = LIST_ENTRY(cursor, struct ifproxy, entry);
819 if (IsEqualIID(riid, &ifproxy->iid))
821 *ifproxy_found = ifproxy;
826 LeaveCriticalSection(&This->cs);
831 static void proxy_manager_disconnect(struct proxy_manager * This)
833 struct list * cursor;
835 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This->oxid),
836 wine_dbgstr_longlong(This->oid));
838 EnterCriticalSection(&This->cs);
840 /* SORFP_NOLIFTIMEMGMT proxies (for IRemUnknown) shouldn't be
841 * disconnected - it won't do anything anyway, except cause
842 * problems for other objects that depend on this proxy always
844 if (!(This->sorflags & SORFP_NOLIFETIMEMGMT))
846 LIST_FOR_EACH(cursor, &This->interfaces)
848 struct ifproxy * ifproxy = LIST_ENTRY(cursor, struct ifproxy, entry);
849 ifproxy_disconnect(ifproxy);
853 /* apartment is being destroyed so don't keep a pointer around to it */
856 LeaveCriticalSection(&This->cs);
859 static HRESULT proxy_manager_get_remunknown(struct proxy_manager * This, IRemUnknown **remunk)
863 /* we don't want to try and unmarshal or use IRemUnknown if we don't want
864 * lifetime management */
865 if (This->sorflags & SORFP_NOLIFETIMEMGMT)
868 EnterCriticalSection(&This->cs);
870 /* already created - return existing object */
871 *remunk = This->remunk;
872 else if (!This->parent)
873 /* disconnected - we can't create IRemUnknown */
878 /* Don't want IRemUnknown lifetime management as this is IRemUnknown!
879 * We also don't care about whether or not the stub is still alive */
880 stdobjref.flags = SORFP_NOLIFETIMEMGMT | SORF_NOPING;
881 stdobjref.cPublicRefs = 1;
882 /* oxid of destination object */
883 stdobjref.oxid = This->oxid;
884 /* FIXME: what should be used for the oid? The DCOM draft doesn't say */
885 stdobjref.oid = (OID)-1;
886 /* FIXME: this is a hack around not having an OXID resolver yet -
887 * the OXID resolver should give us the IPID of the IRemUnknown
889 stdobjref.ipid.Data1 = 0xffffffff;
890 stdobjref.ipid.Data2 = 0xffff;
891 stdobjref.ipid.Data3 = 0xffff;
892 assert(sizeof(stdobjref.ipid.Data4) == sizeof(stdobjref.oxid));
893 memcpy(&stdobjref.ipid.Data4, &stdobjref.oxid, sizeof(OXID));
895 /* do the unmarshal */
896 hr = unmarshal_object(&stdobjref, This->parent, This->dest_context,
897 This->dest_context_data, &IID_IRemUnknown,
898 (void**)&This->remunk);
900 *remunk = This->remunk;
902 LeaveCriticalSection(&This->cs);
904 TRACE("got IRemUnknown* pointer %p, hr = 0x%08x\n", *remunk, hr);
909 /* destroys a proxy manager, freeing the memory it used.
910 * Note: this function should not be called from a list iteration in the
911 * apartment, due to the fact that it removes itself from the apartment and
912 * it could add a proxy to IRemUnknown into the apartment. */
913 static void proxy_manager_destroy(struct proxy_manager * This)
915 struct list * cursor;
917 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This->oxid),
918 wine_dbgstr_longlong(This->oid));
922 EnterCriticalSection(&This->parent->cs);
924 /* remove ourself from the list of proxy objects in the apartment */
925 LIST_FOR_EACH(cursor, &This->parent->proxies)
927 if (cursor == &This->entry)
929 list_remove(&This->entry);
934 LeaveCriticalSection(&This->parent->cs);
937 /* destroy all of the interface proxies */
938 while ((cursor = list_head(&This->interfaces)))
940 struct ifproxy * ifproxy = LIST_ENTRY(cursor, struct ifproxy, entry);
941 ifproxy_destroy(ifproxy);
944 if (This->remunk) IRemUnknown_Release(This->remunk);
946 DEBUG_CLEAR_CRITSEC_NAME(&This->cs);
947 DeleteCriticalSection(&This->cs);
949 CloseHandle(This->remoting_mutex);
951 HeapFree(GetProcessHeap(), 0, This);
954 /* finds the proxy manager corresponding to a given OXID and OID that has
955 * been unmarshaled in the specified apartment. The caller must release the
956 * reference to the proxy_manager when the object is no longer used. */
957 static BOOL find_proxy_manager(APARTMENT * apt, OXID oxid, OID oid, struct proxy_manager ** proxy_found)
960 struct list * cursor;
962 EnterCriticalSection(&apt->cs);
963 LIST_FOR_EACH(cursor, &apt->proxies)
965 struct proxy_manager * proxy = LIST_ENTRY(cursor, struct proxy_manager, entry);
966 if ((oxid == proxy->oxid) && (oid == proxy->oid))
968 *proxy_found = proxy;
969 ClientIdentity_AddRef((IMultiQI *)&proxy->lpVtbl);
974 LeaveCriticalSection(&apt->cs);
978 HRESULT apartment_disconnectproxies(struct apartment *apt)
980 struct list * cursor;
982 LIST_FOR_EACH(cursor, &apt->proxies)
984 struct proxy_manager * proxy = LIST_ENTRY(cursor, struct proxy_manager, entry);
985 proxy_manager_disconnect(proxy);
991 /********************** StdMarshal implementation ****************************/
992 typedef struct _StdMarshalImpl
994 const IMarshalVtbl *lpvtbl;
999 LPVOID pvDestContext;
1003 static HRESULT WINAPI
1004 StdMarshalImpl_QueryInterface(LPMARSHAL iface, REFIID riid, LPVOID *ppv)
1007 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IMarshal, riid))
1010 IUnknown_AddRef(iface);
1013 FIXME("No interface for %s.\n", debugstr_guid(riid));
1014 return E_NOINTERFACE;
1018 StdMarshalImpl_AddRef(LPMARSHAL iface)
1020 StdMarshalImpl *This = (StdMarshalImpl *)iface;
1021 return InterlockedIncrement(&This->ref);
1025 StdMarshalImpl_Release(LPMARSHAL iface)
1027 StdMarshalImpl *This = (StdMarshalImpl *)iface;
1028 ULONG ref = InterlockedDecrement(&This->ref);
1030 if (!ref) HeapFree(GetProcessHeap(),0,This);
1034 static HRESULT WINAPI
1035 StdMarshalImpl_GetUnmarshalClass(
1036 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
1037 void* pvDestContext, DWORD mshlflags, CLSID* pCid)
1039 *pCid = CLSID_DfMarshal;
1043 static HRESULT WINAPI
1044 StdMarshalImpl_GetMarshalSizeMax(
1045 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
1046 void* pvDestContext, DWORD mshlflags, DWORD* pSize)
1048 *pSize = sizeof(STDOBJREF);
1052 static HRESULT WINAPI
1053 StdMarshalImpl_MarshalInterface(
1054 LPMARSHAL iface, IStream *pStm,REFIID riid, void* pv, DWORD dwDestContext,
1055 void* pvDestContext, DWORD mshlflags)
1057 STDOBJREF stdobjref;
1060 APARTMENT *apt = COM_CurrentApt();
1062 TRACE("(...,%s,...)\n", debugstr_guid(riid));
1066 ERR("Apartment not initialized\n");
1067 return CO_E_NOTINITIALIZED;
1070 /* make sure this apartment can be reached from other threads / processes */
1071 RPC_StartRemoting(apt);
1073 hres = marshal_object(apt, &stdobjref, riid, (IUnknown *)pv, mshlflags);
1076 ERR("Failed to create ifstub, hres=0x%x\n", hres);
1080 hres = IStream_Write(pStm, &stdobjref, sizeof(stdobjref), &res);
1081 if (hres) return hres;
1086 /* helper for StdMarshalImpl_UnmarshalInterface - does the unmarshaling with
1087 * no questions asked about the rules surrounding same-apartment unmarshals
1088 * and table marshaling */
1089 static HRESULT unmarshal_object(const STDOBJREF *stdobjref, APARTMENT *apt,
1090 MSHCTX dest_context, void *dest_context_data,
1091 REFIID riid, void **object)
1093 struct proxy_manager *proxy_manager = NULL;
1098 TRACE("stdobjref:\n\tflags = %04lx\n\tcPublicRefs = %ld\n\toxid = %s\n\toid = %s\n\tipid = %s\n",
1099 stdobjref->flags, stdobjref->cPublicRefs,
1100 wine_dbgstr_longlong(stdobjref->oxid),
1101 wine_dbgstr_longlong(stdobjref->oid),
1102 debugstr_guid(&stdobjref->ipid));
1104 /* create a new proxy manager if one doesn't already exist for the
1106 if (!find_proxy_manager(apt, stdobjref->oxid, stdobjref->oid, &proxy_manager))
1108 hr = proxy_manager_construct(apt, stdobjref->flags,
1109 stdobjref->oxid, stdobjref->oid,
1113 TRACE("proxy manager already created, using\n");
1117 struct ifproxy * ifproxy;
1119 proxy_manager_set_context(proxy_manager, dest_context, dest_context_data);
1121 hr = proxy_manager_find_ifproxy(proxy_manager, riid, &ifproxy);
1122 if (hr == E_NOINTERFACE)
1124 IRpcChannelBuffer *chanbuf;
1125 hr = RPC_CreateClientChannel(&stdobjref->oxid, &stdobjref->ipid,
1126 proxy_manager->dest_context,
1127 proxy_manager->dest_context_data,
1130 hr = proxy_manager_create_ifproxy(proxy_manager, stdobjref,
1131 riid, chanbuf, &ifproxy);
1134 IUnknown_AddRef((IUnknown *)ifproxy->iface);
1138 InterlockedExchangeAdd((LONG *)&ifproxy->refs, stdobjref->cPublicRefs);
1139 /* get at least one external reference to the object to keep it alive */
1140 hr = ifproxy_get_public_ref(ifproxy);
1142 ifproxy_destroy(ifproxy);
1146 *object = ifproxy->iface;
1149 /* release our reference to the proxy manager - the client/apartment
1150 * will hold on to the remaining reference for us */
1151 if (proxy_manager) ClientIdentity_Release((IMultiQI*)&proxy_manager->lpVtbl);
1156 static HRESULT WINAPI
1157 StdMarshalImpl_UnmarshalInterface(LPMARSHAL iface, IStream *pStm, REFIID riid, void **ppv)
1159 StdMarshalImpl *This = (StdMarshalImpl *)iface;
1160 struct stub_manager *stubmgr;
1161 STDOBJREF stdobjref;
1164 APARTMENT *apt = COM_CurrentApt();
1165 APARTMENT *stub_apt;
1168 TRACE("(...,%s,....)\n", debugstr_guid(riid));
1170 /* we need an apartment to unmarshal into */
1173 ERR("Apartment not initialized\n");
1174 return CO_E_NOTINITIALIZED;
1177 /* read STDOBJREF from wire */
1178 hres = IStream_Read(pStm, &stdobjref, sizeof(stdobjref), &res);
1179 if (hres) return STG_E_READFAULT;
1181 hres = apartment_getoxid(apt, &oxid);
1182 if (hres) return hres;
1184 /* check if we're marshalling back to ourselves */
1185 if ((oxid == stdobjref.oxid) && (stubmgr = get_stub_manager(apt, stdobjref.oid)))
1187 TRACE("Unmarshalling object marshalled in same apartment for iid %s, "
1188 "returning original object %p\n", debugstr_guid(riid), stubmgr->object);
1190 hres = IUnknown_QueryInterface(stubmgr->object, riid, ppv);
1192 /* unref the ifstub. FIXME: only do this on success? */
1193 if (!stub_manager_is_table_marshaled(stubmgr, &stdobjref.ipid))
1194 stub_manager_ext_release(stubmgr, stdobjref.cPublicRefs, TRUE);
1196 stub_manager_int_release(stubmgr);
1200 /* notify stub manager about unmarshal if process-local object.
1201 * note: if the oxid is not found then we and native will quite happily
1202 * ignore table marshaling and normal marshaling rules regarding number of
1203 * unmarshals, etc, but if you abuse these rules then your proxy could end
1204 * up returning RPC_E_DISCONNECTED. */
1205 if ((stub_apt = apartment_findfromoxid(stdobjref.oxid, TRUE)))
1207 if ((stubmgr = get_stub_manager(stub_apt, stdobjref.oid)))
1209 if (!stub_manager_notify_unmarshal(stubmgr, &stdobjref.ipid))
1210 hres = CO_E_OBJNOTCONNECTED;
1212 stub_manager_int_release(stubmgr);
1216 WARN("Couldn't find object for OXID %s, OID %s, assuming disconnected\n",
1217 wine_dbgstr_longlong(stdobjref.oxid),
1218 wine_dbgstr_longlong(stdobjref.oid));
1219 hres = CO_E_OBJNOTCONNECTED;
1222 apartment_release(stub_apt);
1225 TRACE("Treating unmarshal from OXID %s as inter-process\n",
1226 wine_dbgstr_longlong(stdobjref.oxid));
1229 hres = unmarshal_object(&stdobjref, apt, This->dwDestContext,
1230 This->pvDestContext, riid, ppv);
1232 if (hres) WARN("Failed with error 0x%08x\n", hres);
1233 else TRACE("Successfully created proxy %p\n", *ppv);
1238 static HRESULT WINAPI
1239 StdMarshalImpl_ReleaseMarshalData(LPMARSHAL iface, IStream *pStm)
1241 STDOBJREF stdobjref;
1244 struct stub_manager *stubmgr;
1247 TRACE("iface=%p, pStm=%p\n", iface, pStm);
1249 hres = IStream_Read(pStm, &stdobjref, sizeof(stdobjref), &res);
1250 if (hres) return STG_E_READFAULT;
1252 TRACE("oxid = %s, oid = %s, ipid = %s\n",
1253 wine_dbgstr_longlong(stdobjref.oxid),
1254 wine_dbgstr_longlong(stdobjref.oid),
1255 wine_dbgstr_guid(&stdobjref.ipid));
1257 if (!(apt = apartment_findfromoxid(stdobjref.oxid, TRUE)))
1259 WARN("Could not map OXID %s to apartment object\n",
1260 wine_dbgstr_longlong(stdobjref.oxid));
1261 return RPC_E_INVALID_OBJREF;
1264 if (!(stubmgr = get_stub_manager(apt, stdobjref.oid)))
1266 ERR("could not map object ID to stub manager, oxid=%s, oid=%s\n",
1267 wine_dbgstr_longlong(stdobjref.oxid), wine_dbgstr_longlong(stdobjref.oid));
1268 return RPC_E_INVALID_OBJREF;
1271 stub_manager_release_marshal_data(stubmgr, stdobjref.cPublicRefs, &stdobjref.ipid);
1273 stub_manager_int_release(stubmgr);
1274 apartment_release(apt);
1279 static HRESULT WINAPI
1280 StdMarshalImpl_DisconnectObject(LPMARSHAL iface, DWORD dwReserved)
1282 FIXME("(), stub!\n");
1286 static const IMarshalVtbl VT_StdMarshal =
1288 StdMarshalImpl_QueryInterface,
1289 StdMarshalImpl_AddRef,
1290 StdMarshalImpl_Release,
1291 StdMarshalImpl_GetUnmarshalClass,
1292 StdMarshalImpl_GetMarshalSizeMax,
1293 StdMarshalImpl_MarshalInterface,
1294 StdMarshalImpl_UnmarshalInterface,
1295 StdMarshalImpl_ReleaseMarshalData,
1296 StdMarshalImpl_DisconnectObject
1299 static HRESULT StdMarshalImpl_Construct(REFIID riid, void** ppvObject)
1301 StdMarshalImpl * pStdMarshal =
1302 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(StdMarshalImpl));
1304 return E_OUTOFMEMORY;
1305 pStdMarshal->lpvtbl = &VT_StdMarshal;
1306 pStdMarshal->ref = 0;
1307 return IMarshal_QueryInterface((IMarshal*)pStdMarshal, riid, ppvObject);
1310 /***********************************************************************
1311 * CoGetStandardMarshal [OLE32.@]
1313 * Gets or creates a standard marshal object.
1316 * riid [I] Interface identifier of the pUnk object.
1317 * pUnk [I] Optional. Object to get the marshal object for.
1318 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1319 * pvDestContext [I] Reserved. Must be NULL.
1320 * mshlflags [I] Flags affecting the marshaling process.
1321 * ppMarshal [O] Address where marshal object will be stored.
1325 * Failure: HRESULT code.
1329 * The function retrieves the IMarshal object associated with an object if
1330 * that object is currently an active stub, otherwise a new marshal object is
1333 HRESULT WINAPI CoGetStandardMarshal(REFIID riid, IUnknown *pUnk,
1334 DWORD dwDestContext, LPVOID pvDestContext,
1335 DWORD mshlflags, LPMARSHAL *ppMarshal)
1341 FIXME("(%s,NULL,%x,%p,%x,%p), unimplemented yet.\n",
1342 debugstr_guid(riid),dwDestContext,pvDestContext,mshlflags,ppMarshal);
1345 TRACE("(%s,%p,%x,%p,%x,%p)\n",
1346 debugstr_guid(riid),pUnk,dwDestContext,pvDestContext,mshlflags,ppMarshal);
1347 *ppMarshal = HeapAlloc(GetProcessHeap(),0,sizeof(StdMarshalImpl));
1348 dm = (StdMarshalImpl*) *ppMarshal;
1349 if (!dm) return E_FAIL;
1350 dm->lpvtbl = &VT_StdMarshal;
1354 dm->dwDestContext = dwDestContext;
1355 dm->pvDestContext = pvDestContext;
1356 dm->mshlflags = mshlflags;
1360 /***********************************************************************
1361 * get_marshaler [internal]
1363 * Retrieves an IMarshal interface for an object.
1365 static HRESULT get_marshaler(REFIID riid, IUnknown *pUnk, DWORD dwDestContext,
1366 void *pvDestContext, DWORD mshlFlags,
1367 LPMARSHAL *pMarshal)
1373 hr = IUnknown_QueryInterface(pUnk, &IID_IMarshal, (LPVOID*)pMarshal);
1375 hr = CoGetStandardMarshal(riid, pUnk, dwDestContext, pvDestContext,
1376 mshlFlags, pMarshal);
1380 /***********************************************************************
1381 * get_unmarshaler_from_stream [internal]
1383 * Creates an IMarshal* object according to the data marshaled to the stream.
1384 * The function leaves the stream pointer at the start of the data written
1385 * to the stream by the IMarshal* object.
1387 static HRESULT get_unmarshaler_from_stream(IStream *stream, IMarshal **marshal, IID *iid)
1393 /* read common OBJREF header */
1394 hr = IStream_Read(stream, &objref, FIELD_OFFSET(OBJREF, u_objref), &res);
1395 if (hr || (res != FIELD_OFFSET(OBJREF, u_objref)))
1397 ERR("Failed to read common OBJREF header, 0x%08x\n", hr);
1398 return STG_E_READFAULT;
1401 /* sanity check on header */
1402 if (objref.signature != OBJREF_SIGNATURE)
1404 ERR("Bad OBJREF signature 0x%08lx\n", objref.signature);
1405 return RPC_E_INVALID_OBJREF;
1408 if (iid) *iid = objref.iid;
1410 /* FIXME: handler marshaling */
1411 if (objref.flags & OBJREF_STANDARD)
1413 TRACE("Using standard unmarshaling\n");
1414 hr = StdMarshalImpl_Construct(&IID_IMarshal, (LPVOID*)marshal);
1416 else if (objref.flags & OBJREF_CUSTOM)
1418 ULONG custom_header_size = FIELD_OFFSET(OBJREF, u_objref.u_custom.pData) -
1419 FIELD_OFFSET(OBJREF, u_objref.u_custom);
1420 TRACE("Using custom unmarshaling\n");
1421 /* read constant sized OR_CUSTOM data from stream */
1422 hr = IStream_Read(stream, &objref.u_objref.u_custom,
1423 custom_header_size, &res);
1424 if (hr || (res != custom_header_size))
1426 ERR("Failed to read OR_CUSTOM header, 0x%08x\n", hr);
1427 return STG_E_READFAULT;
1429 /* now create the marshaler specified in the stream */
1430 hr = CoCreateInstance(&objref.u_objref.u_custom.clsid, NULL,
1431 CLSCTX_INPROC_SERVER, &IID_IMarshal,
1436 FIXME("Invalid or unimplemented marshaling type specified: %lx\n",
1438 return RPC_E_INVALID_OBJREF;
1442 ERR("Failed to create marshal, 0x%08x\n", hr);
1447 /***********************************************************************
1448 * CoGetMarshalSizeMax [OLE32.@]
1450 * Gets the maximum amount of data that will be needed by a marshal.
1453 * pulSize [O] Address where maximum marshal size will be stored.
1454 * riid [I] Identifier of the interface to marshal.
1455 * pUnk [I] Pointer to the object to marshal.
1456 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1457 * pvDestContext [I] Reserved. Must be NULL.
1458 * mshlFlags [I] Flags that affect the marshaling. See CoMarshalInterface().
1462 * Failure: HRESULT code.
1465 * CoMarshalInterface().
1467 HRESULT WINAPI CoGetMarshalSizeMax(ULONG *pulSize, REFIID riid, IUnknown *pUnk,
1468 DWORD dwDestContext, void *pvDestContext,
1473 CLSID marshaler_clsid;
1475 hr = get_marshaler(riid, pUnk, dwDestContext, pvDestContext, mshlFlags, &pMarshal);
1479 hr = IMarshal_GetUnmarshalClass(pMarshal, riid, pUnk, dwDestContext,
1480 pvDestContext, mshlFlags, &marshaler_clsid);
1483 ERR("IMarshal::GetUnmarshalClass failed, 0x%08x\n", hr);
1484 IMarshal_Release(pMarshal);
1488 hr = IMarshal_GetMarshalSizeMax(pMarshal, riid, pUnk, dwDestContext,
1489 pvDestContext, mshlFlags, pulSize);
1490 if (IsEqualCLSID(&marshaler_clsid, &CLSID_DfMarshal))
1491 /* add on the size of the common header */
1492 *pulSize += FIELD_OFFSET(OBJREF, u_objref);
1494 /* custom marshaling: add on the size of the whole OBJREF structure
1495 * like native does */
1496 *pulSize += sizeof(OBJREF);
1498 IMarshal_Release(pMarshal);
1503 static void dump_MSHLFLAGS(MSHLFLAGS flags)
1505 if (flags & MSHLFLAGS_TABLESTRONG)
1506 TRACE(" MSHLFLAGS_TABLESTRONG");
1507 if (flags & MSHLFLAGS_TABLEWEAK)
1508 TRACE(" MSHLFLAGS_TABLEWEAK");
1509 if (!(flags & (MSHLFLAGS_TABLESTRONG|MSHLFLAGS_TABLEWEAK)))
1510 TRACE(" MSHLFLAGS_NORMAL");
1511 if (flags & MSHLFLAGS_NOPING)
1512 TRACE(" MSHLFLAGS_NOPING");
1515 /***********************************************************************
1516 * CoMarshalInterface [OLE32.@]
1518 * Marshals an interface into a stream so that the object can then be
1519 * unmarshaled from another COM apartment and used remotely.
1522 * pStream [I] Stream the object will be marshaled into.
1523 * riid [I] Identifier of the interface to marshal.
1524 * pUnk [I] Pointer to the object to marshal.
1525 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1526 * pvDestContext [I] Reserved. Must be NULL.
1527 * mshlFlags [I] Flags that affect the marshaling. See notes.
1531 * Failure: HRESULT code.
1535 * The mshlFlags parameter can take one or more of the following flags:
1536 *| MSHLFLAGS_NORMAL - Unmarshal once, releases stub on last proxy release.
1537 *| MSHLFLAGS_TABLESTRONG - Unmarshal many, release when CoReleaseMarshalData() called.
1538 *| MSHLFLAGS_TABLEWEAK - Unmarshal many, releases stub on last proxy release.
1539 *| MSHLFLAGS_NOPING - No automatic garbage collection (and so reduces network traffic).
1541 * If a marshaled object is not unmarshaled, then CoReleaseMarshalData() must
1542 * be called in order to release the resources used in the marshaling.
1545 * CoUnmarshalInterface(), CoReleaseMarshalData().
1547 HRESULT WINAPI CoMarshalInterface(IStream *pStream, REFIID riid, IUnknown *pUnk,
1548 DWORD dwDestContext, void *pvDestContext,
1552 CLSID marshaler_clsid;
1556 TRACE("(%p, %s, %p, %x, %p,", pStream, debugstr_guid(riid), pUnk,
1557 dwDestContext, pvDestContext);
1558 dump_MSHLFLAGS(mshlFlags);
1561 if (!pUnk || !pStream)
1562 return E_INVALIDARG;
1564 objref.signature = OBJREF_SIGNATURE;
1567 /* get the marshaler for the specified interface */
1568 hr = get_marshaler(riid, pUnk, dwDestContext, pvDestContext, mshlFlags, &pMarshal);
1571 ERR("Failed to get marshaller, 0x%08x\n", hr);
1575 hr = IMarshal_GetUnmarshalClass(pMarshal, riid, pUnk, dwDestContext,
1576 pvDestContext, mshlFlags, &marshaler_clsid);
1579 ERR("IMarshal::GetUnmarshalClass failed, 0x%08x\n", hr);
1583 /* FIXME: implement handler marshaling too */
1584 if (IsEqualCLSID(&marshaler_clsid, &CLSID_DfMarshal))
1586 TRACE("Using standard marshaling\n");
1587 objref.flags = OBJREF_STANDARD;
1589 /* write the common OBJREF header to the stream */
1590 hr = IStream_Write(pStream, &objref, FIELD_OFFSET(OBJREF, u_objref), NULL);
1593 ERR("Failed to write OBJREF header to stream, 0x%08x\n", hr);
1599 TRACE("Using custom marshaling\n");
1600 objref.flags = OBJREF_CUSTOM;
1601 objref.u_objref.u_custom.clsid = marshaler_clsid;
1602 objref.u_objref.u_custom.cbExtension = 0;
1603 objref.u_objref.u_custom.size = 0;
1604 hr = IMarshal_GetMarshalSizeMax(pMarshal, riid, pUnk, dwDestContext,
1605 pvDestContext, mshlFlags,
1606 &objref.u_objref.u_custom.size);
1609 ERR("Failed to get max size of marshal data, error 0x%08x\n", hr);
1612 /* write constant sized common header and OR_CUSTOM data into stream */
1613 hr = IStream_Write(pStream, &objref,
1614 FIELD_OFFSET(OBJREF, u_objref.u_custom.pData), NULL);
1617 ERR("Failed to write OR_CUSTOM header to stream with 0x%08x\n", hr);
1622 TRACE("Calling IMarshal::MarshalInterace\n");
1623 /* call helper object to do the actual marshaling */
1624 hr = IMarshal_MarshalInterface(pMarshal, pStream, riid, pUnk, dwDestContext,
1625 pvDestContext, mshlFlags);
1629 ERR("Failed to marshal the interface %s, %x\n", debugstr_guid(riid), hr);
1634 IMarshal_Release(pMarshal);
1636 TRACE("completed with hr 0x%08x\n", hr);
1641 /***********************************************************************
1642 * CoUnmarshalInterface [OLE32.@]
1644 * Unmarshals an object from a stream by creating a proxy to the remote
1645 * object, if necessary.
1649 * pStream [I] Stream containing the marshaled object.
1650 * riid [I] Interface identifier of the object to create a proxy to.
1651 * ppv [O] Address where proxy will be stored.
1656 * Failure: HRESULT code.
1659 * CoMarshalInterface().
1661 HRESULT WINAPI CoUnmarshalInterface(IStream *pStream, REFIID riid, LPVOID *ppv)
1668 TRACE("(%p, %s, %p)\n", pStream, debugstr_guid(riid), ppv);
1670 if (!pStream || !ppv)
1671 return E_INVALIDARG;
1673 hr = get_unmarshaler_from_stream(pStream, &pMarshal, &iid);
1677 /* call the helper object to do the actual unmarshaling */
1678 hr = IMarshal_UnmarshalInterface(pMarshal, pStream, &iid, (LPVOID*)&object);
1680 ERR("IMarshal::UnmarshalInterface failed, 0x%08x\n", hr);
1684 /* IID_NULL means use the interface ID of the marshaled object */
1685 if (!IsEqualIID(riid, &IID_NULL) && !IsEqualIID(riid, &iid))
1687 TRACE("requested interface != marshalled interface, additional QI needed\n");
1688 hr = IUnknown_QueryInterface(object, riid, ppv);
1690 ERR("Couldn't query for interface %s, hr = 0x%08x\n",
1691 debugstr_guid(riid), hr);
1692 IUnknown_Release(object);
1700 IMarshal_Release(pMarshal);
1702 TRACE("completed with hr 0x%x\n", hr);
1707 /***********************************************************************
1708 * CoReleaseMarshalData [OLE32.@]
1710 * Releases resources associated with an object that has been marshaled into
1715 * pStream [I] The stream that the object has been marshaled into.
1719 * Failure: HRESULT error code.
1723 * Call this function to release resources associated with a normal or
1724 * table-weak marshal that will not be unmarshaled, and all table-strong
1725 * marshals when they are no longer needed.
1728 * CoMarshalInterface(), CoUnmarshalInterface().
1730 HRESULT WINAPI CoReleaseMarshalData(IStream *pStream)
1735 TRACE("(%p)\n", pStream);
1737 hr = get_unmarshaler_from_stream(pStream, &pMarshal, NULL);
1741 /* call the helper object to do the releasing of marshal data */
1742 hr = IMarshal_ReleaseMarshalData(pMarshal, pStream);
1744 ERR("IMarshal::ReleaseMarshalData failed with error 0x%08x\n", hr);
1746 IMarshal_Release(pMarshal);
1751 /***********************************************************************
1752 * CoMarshalInterThreadInterfaceInStream [OLE32.@]
1754 * Marshal an interface across threads in the same process.
1757 * riid [I] Identifier of the interface to be marshalled.
1758 * pUnk [I] Pointer to IUnknown-derived interface that will be marshalled.
1759 * ppStm [O] Pointer to IStream object that is created and then used to store the marshalled interface.
1763 * Failure: E_OUTOFMEMORY and other COM error codes
1766 * CoMarshalInterface(), CoUnmarshalInterface() and CoGetInterfaceAndReleaseStream()
1768 HRESULT WINAPI CoMarshalInterThreadInterfaceInStream(
1769 REFIID riid, LPUNKNOWN pUnk, LPSTREAM * ppStm)
1771 ULARGE_INTEGER xpos;
1772 LARGE_INTEGER seekto;
1775 TRACE("(%s, %p, %p)\n",debugstr_guid(riid), pUnk, ppStm);
1777 hres = CreateStreamOnHGlobal(NULL, TRUE, ppStm);
1778 if (FAILED(hres)) return hres;
1779 hres = CoMarshalInterface(*ppStm, riid, pUnk, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1781 if (SUCCEEDED(hres))
1783 memset(&seekto, 0, sizeof(seekto));
1784 IStream_Seek(*ppStm, seekto, STREAM_SEEK_SET, &xpos);
1788 IStream_Release(*ppStm);
1795 /***********************************************************************
1796 * CoGetInterfaceAndReleaseStream [OLE32.@]
1798 * Unmarshalls an interface from a stream and then releases the stream.
1801 * pStm [I] Stream that contains the marshalled interface.
1802 * riid [I] Interface identifier of the object to unmarshall.
1803 * ppv [O] Address of pointer where the requested interface object will be stored.
1807 * Failure: A COM error code
1810 * CoMarshalInterThreadInterfaceInStream() and CoUnmarshalInterface()
1812 HRESULT WINAPI CoGetInterfaceAndReleaseStream(LPSTREAM pStm, REFIID riid,
1817 TRACE("(%p, %s, %p)\n", pStm, debugstr_guid(riid), ppv);
1819 if(!pStm) return E_INVALIDARG;
1820 hres = CoUnmarshalInterface(pStm, riid, ppv);
1821 IStream_Release(pStm);
1825 static HRESULT WINAPI StdMarshalCF_QueryInterface(LPCLASSFACTORY iface,
1826 REFIID riid, LPVOID *ppv)
1829 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory))
1831 *ppv = (LPVOID)iface;
1834 return E_NOINTERFACE;
1837 static ULONG WINAPI StdMarshalCF_AddRef(LPCLASSFACTORY iface)
1839 return 2; /* non-heap based object */
1842 static ULONG WINAPI StdMarshalCF_Release(LPCLASSFACTORY iface)
1844 return 1; /* non-heap based object */
1847 static HRESULT WINAPI StdMarshalCF_CreateInstance(LPCLASSFACTORY iface,
1848 LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv)
1850 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IMarshal))
1851 return StdMarshalImpl_Construct(riid, ppv);
1853 FIXME("(%s), not supported.\n",debugstr_guid(riid));
1854 return E_NOINTERFACE;
1857 static HRESULT WINAPI StdMarshalCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
1859 FIXME("(%d), stub!\n",fLock);
1863 static const IClassFactoryVtbl StdMarshalCFVtbl =
1865 StdMarshalCF_QueryInterface,
1866 StdMarshalCF_AddRef,
1867 StdMarshalCF_Release,
1868 StdMarshalCF_CreateInstance,
1869 StdMarshalCF_LockServer
1871 static const IClassFactoryVtbl *StdMarshalCF = &StdMarshalCFVtbl;
1873 HRESULT MARSHAL_GetStandardMarshalCF(LPVOID *ppv)
1875 *ppv = &StdMarshalCF;
1879 /***********************************************************************
1880 * CoMarshalHresult [OLE32.@]
1882 * Marshals an HRESULT value into a stream.
1885 * pStm [I] Stream that hresult will be marshalled into.
1886 * hresult [I] HRESULT to be marshalled.
1890 * Failure: A COM error code
1893 * CoUnmarshalHresult().
1895 HRESULT WINAPI CoMarshalHresult(LPSTREAM pStm, HRESULT hresult)
1897 return IStream_Write(pStm, &hresult, sizeof(hresult), NULL);
1900 /***********************************************************************
1901 * CoUnmarshalHresult [OLE32.@]
1903 * Unmarshals an HRESULT value from a stream.
1906 * pStm [I] Stream that hresult will be unmarshalled from.
1907 * phresult [I] Pointer to HRESULT where the value will be unmarshalled to.
1911 * Failure: A COM error code
1914 * CoMarshalHresult().
1916 HRESULT WINAPI CoUnmarshalHresult(LPSTREAM pStm, HRESULT * phresult)
1918 return IStream_Read(pStm, phresult, sizeof(*phresult), NULL);