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, const OXID_INFO *oxid_info,
57 /* Marshalling just passes a unique identifier to the remote client,
58 * that makes it possible to find the passed interface again.
60 * So basically we need a set of values that make it unique.
62 * Note that the IUnknown_QI(ob,xiid,&ppv) always returns the SAME ppv value!
64 * A triple is used: OXID (apt id), OID (stub manager id),
65 * IPID (interface ptr/stub id).
67 * OXIDs identify an apartment and are network scoped
68 * OIDs identify a stub manager and are apartment scoped
69 * IPIDs identify an interface stub and are apartment scoped
72 static inline HRESULT get_facbuf_for_iid(REFIID riid, IPSFactoryBuffer **facbuf)
77 if ((hr = CoGetPSClsid(riid, &clsid)))
79 return CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL,
80 &IID_IPSFactoryBuffer, (LPVOID*)facbuf);
83 /* marshals an object into a STDOBJREF structure */
84 HRESULT marshal_object(APARTMENT *apt, STDOBJREF *stdobjref, REFIID riid, IUnknown *object, MSHLFLAGS mshlflags)
86 struct stub_manager *manager;
87 struct ifstub *ifstub;
89 IRpcStubBuffer *stub = NULL;
91 IUnknown *iobject = NULL; /* object of type riid */
93 hr = apartment_getoxid(apt, &stdobjref->oxid);
97 hr = apartment_createwindowifneeded(apt);
101 hr = IUnknown_QueryInterface(object, riid, (void **)&iobject);
104 ERR("object doesn't expose interface %s, failing with error 0x%08x\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 with error 0x%08x\n",
128 debugstr_guid(riid), hr);
129 IUnknown_Release(iobject);
134 if (mshlflags & MSHLFLAGS_NOPING)
135 stdobjref->flags = SORF_NOPING;
137 stdobjref->flags = SORF_NULL;
139 if ((manager = get_stub_manager_from_object(apt, object)))
140 TRACE("registering new ifstub on pre-existing manager\n");
143 TRACE("constructing new stub manager\n");
145 manager = new_stub_manager(apt, object);
148 if (stub) IRpcStubBuffer_Release(stub);
149 IUnknown_Release(iobject);
150 return E_OUTOFMEMORY;
153 stdobjref->oid = manager->oid;
155 tablemarshal = ((mshlflags & MSHLFLAGS_TABLESTRONG) || (mshlflags & MSHLFLAGS_TABLEWEAK));
157 /* make sure ifstub that we are creating is unique */
158 ifstub = stub_manager_find_ifstub(manager, riid, mshlflags);
160 ifstub = stub_manager_new_ifstub(manager, stub, iobject, riid, mshlflags);
162 if (stub) IRpcStubBuffer_Release(stub);
163 IUnknown_Release(iobject);
167 stub_manager_int_release(manager);
168 /* destroy the stub manager if it has no ifstubs by releasing
169 * zero external references */
170 stub_manager_ext_release(manager, 0, TRUE);
171 return E_OUTOFMEMORY;
176 stdobjref->cPublicRefs = NORMALEXTREFS;
177 stub_manager_ext_addref(manager, stdobjref->cPublicRefs);
181 stdobjref->cPublicRefs = 0;
182 if (mshlflags & MSHLFLAGS_TABLESTRONG)
183 stub_manager_ext_addref(manager, 1);
186 /* FIXME: check return value */
187 RPC_RegisterInterface(riid);
189 stdobjref->ipid = ifstub->ipid;
191 stub_manager_int_release(manager);
197 /* Client-side identity of the server object */
199 static HRESULT proxy_manager_get_remunknown(struct proxy_manager * This, IRemUnknown **remunk);
200 static void proxy_manager_destroy(struct proxy_manager * This);
201 static HRESULT proxy_manager_find_ifproxy(struct proxy_manager * This, REFIID riid, struct ifproxy ** ifproxy_found);
202 static HRESULT proxy_manager_query_local_interface(struct proxy_manager * This, REFIID riid, void ** ppv);
204 static HRESULT WINAPI ClientIdentity_QueryInterface(IMultiQI * iface, REFIID riid, void ** ppv)
209 TRACE("%s\n", debugstr_guid(riid));
212 hr = IMultiQI_QueryMultipleInterfaces(iface, 1, &mqi);
213 *ppv = (void *)mqi.pItf;
218 static ULONG WINAPI ClientIdentity_AddRef(IMultiQI * iface)
220 struct proxy_manager * This = (struct proxy_manager *)iface;
221 TRACE("%p - before %d\n", iface, This->refs);
222 return InterlockedIncrement(&This->refs);
225 static ULONG WINAPI ClientIdentity_Release(IMultiQI * iface)
227 struct proxy_manager * This = (struct proxy_manager *)iface;
228 ULONG refs = InterlockedDecrement(&This->refs);
229 TRACE("%p - after %d\n", iface, refs);
231 proxy_manager_destroy(This);
235 static HRESULT WINAPI ClientIdentity_QueryMultipleInterfaces(IMultiQI *iface, ULONG cMQIs, MULTI_QI *pMQIs)
237 struct proxy_manager * This = (struct proxy_manager *)iface;
238 REMQIRESULT *qiresults = NULL;
239 ULONG nonlocal_mqis = 0;
241 ULONG successful_mqis = 0;
242 IID *iids = HeapAlloc(GetProcessHeap(), 0, cMQIs * sizeof(*iids));
243 /* mapping of RemQueryInterface index to QueryMultipleInterfaces index */
244 ULONG *mapping = HeapAlloc(GetProcessHeap(), 0, cMQIs * sizeof(*mapping));
246 TRACE("cMQIs: %d\n", cMQIs);
248 /* try to get a local interface - this includes already active proxy
249 * interfaces and also interfaces exposed by the proxy manager */
250 for (i = 0; i < cMQIs; i++)
252 TRACE("iid[%d] = %s\n", i, debugstr_guid(pMQIs[i].pIID));
253 pMQIs[i].hr = proxy_manager_query_local_interface(This, pMQIs[i].pIID, (void **)&pMQIs[i].pItf);
254 if (pMQIs[i].hr == S_OK)
258 iids[nonlocal_mqis] = *pMQIs[i].pIID;
259 mapping[nonlocal_mqis] = i;
264 TRACE("%d interfaces not found locally\n", nonlocal_mqis);
266 /* if we have more than one interface not found locally then we must try
267 * to query the remote object for it */
268 if (nonlocal_mqis != 0)
274 /* get the ipid of the first entry */
275 /* FIXME: should we implement ClientIdentity on the ifproxies instead
276 * of the proxy_manager so we use the correct ipid here? */
277 ipid = &LIST_ENTRY(list_head(&This->interfaces), struct ifproxy, entry)->stdobjref.ipid;
279 /* get IRemUnknown proxy so we can communicate with the remote object */
280 hr = proxy_manager_get_remunknown(This, &remunk);
284 hr = IRemUnknown_RemQueryInterface(remunk, ipid, NORMALEXTREFS,
285 nonlocal_mqis, iids, &qiresults);
286 IRemUnknown_Release(remunk);
288 ERR("IRemUnknown_RemQueryInterface failed with error 0x%08x\n", hr);
291 /* IRemUnknown_RemQueryInterface can return S_FALSE if only some of
292 * the interfaces were returned */
295 /* try to unmarshal each object returned to us */
296 for (i = 0; i < nonlocal_mqis; i++)
298 ULONG index = mapping[i];
299 HRESULT hrobj = qiresults[i].hResult;
301 hrobj = unmarshal_object(&qiresults[i].std, COM_CurrentApt(),
303 This->dest_context_data,
304 pMQIs[index].pIID, &This->oxid_info,
305 (void **)&pMQIs[index].pItf);
310 ERR("Failed to get pointer to interface %s\n", debugstr_guid(pMQIs[index].pIID));
311 pMQIs[index].hr = hrobj;
315 /* free the memory allocated by the proxy */
316 CoTaskMemFree(qiresults);
319 TRACE("%d/%d successfully queried\n", successful_mqis, cMQIs);
321 HeapFree(GetProcessHeap(), 0, iids);
322 HeapFree(GetProcessHeap(), 0, mapping);
324 if (successful_mqis == cMQIs)
325 return S_OK; /* we got all requested interfaces */
326 else if (successful_mqis == 0)
327 return E_NOINTERFACE; /* we didn't get any interfaces */
329 return S_FALSE; /* we got some interfaces */
332 static const IMultiQIVtbl ClientIdentity_Vtbl =
334 ClientIdentity_QueryInterface,
335 ClientIdentity_AddRef,
336 ClientIdentity_Release,
337 ClientIdentity_QueryMultipleInterfaces
340 /* FIXME: remove these */
341 static HRESULT WINAPI StdMarshalImpl_GetUnmarshalClass(LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext, void* pvDestContext, DWORD mshlflags, CLSID* pCid);
342 static HRESULT WINAPI StdMarshalImpl_GetMarshalSizeMax(LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext, void* pvDestContext, DWORD mshlflags, DWORD* pSize);
343 static HRESULT WINAPI StdMarshalImpl_UnmarshalInterface(LPMARSHAL iface, IStream *pStm, REFIID riid, void **ppv);
344 static HRESULT WINAPI StdMarshalImpl_ReleaseMarshalData(LPMARSHAL iface, IStream *pStm);
345 static HRESULT WINAPI StdMarshalImpl_DisconnectObject(LPMARSHAL iface, DWORD dwReserved);
347 static HRESULT WINAPI Proxy_QueryInterface(IMarshal *iface, REFIID riid, void **ppvObject)
349 ICOM_THIS_MULTI(struct proxy_manager, lpVtblMarshal, iface);
350 return IMultiQI_QueryInterface((IMultiQI *)&This->lpVtbl, riid, ppvObject);
353 static ULONG WINAPI Proxy_AddRef(IMarshal *iface)
355 ICOM_THIS_MULTI(struct proxy_manager, lpVtblMarshal, iface);
356 return IMultiQI_AddRef((IMultiQI *)&This->lpVtbl);
359 static ULONG WINAPI Proxy_Release(IMarshal *iface)
361 ICOM_THIS_MULTI(struct proxy_manager, lpVtblMarshal, iface);
362 return IMultiQI_Release((IMultiQI *)&This->lpVtbl);
365 static HRESULT WINAPI Proxy_MarshalInterface(
366 LPMARSHAL iface, IStream *pStm, REFIID riid, void* pv, DWORD dwDestContext,
367 void* pvDestContext, DWORD mshlflags)
369 ICOM_THIS_MULTI(struct proxy_manager, lpVtblMarshal, iface);
371 struct ifproxy *ifproxy;
373 TRACE("(...,%s,...)\n", debugstr_guid(riid));
375 hr = proxy_manager_find_ifproxy(This, riid, &ifproxy);
378 STDOBJREF stdobjref = ifproxy->stdobjref;
380 stdobjref.cPublicRefs = 0;
382 if ((mshlflags != MSHLFLAGS_TABLEWEAK) &&
383 (mshlflags != MSHLFLAGS_TABLESTRONG))
385 ULONG cPublicRefs = ifproxy->refs;
386 ULONG cPublicRefsOld;
387 /* optimization - share out proxy's public references if possible
388 * instead of making new proxy do a roundtrip through the server */
391 ULONG cPublicRefsNew;
392 cPublicRefsOld = cPublicRefs;
393 stdobjref.cPublicRefs = cPublicRefs / 2;
394 cPublicRefsNew = cPublicRefs - stdobjref.cPublicRefs;
395 cPublicRefs = InterlockedCompareExchange(
396 (LONG *)&ifproxy->refs, cPublicRefsNew, cPublicRefsOld);
397 } while (cPublicRefs != cPublicRefsOld);
400 /* normal and table-strong marshaling need at least one reference */
401 if (!stdobjref.cPublicRefs && (mshlflags != MSHLFLAGS_TABLEWEAK))
404 hr = proxy_manager_get_remunknown(This, &remunk);
407 HRESULT hrref = S_OK;
409 rif.ipid = ifproxy->stdobjref.ipid;
410 rif.cPublicRefs = (mshlflags == MSHLFLAGS_TABLESTRONG) ? 1 : NORMALEXTREFS;
411 rif.cPrivateRefs = 0;
412 hr = IRemUnknown_RemAddRef(remunk, 1, &rif, &hrref);
413 IRemUnknown_Release(remunk);
414 if (hr == S_OK && hrref == S_OK)
416 /* table-strong marshaling doesn't give the refs to the
417 * client that unmarshals the STDOBJREF */
418 if (mshlflags != MSHLFLAGS_TABLESTRONG)
419 stdobjref.cPublicRefs = rif.cPublicRefs;
422 ERR("IRemUnknown_RemAddRef returned with 0x%08x, hrref = 0x%08x\n", hr, hrref);
428 TRACE("writing stdobjref:\n\tflags = %04lx\n\tcPublicRefs = %ld\n\toxid = %s\n\toid = %s\n\tipid = %s\n",
429 stdobjref.flags, stdobjref.cPublicRefs,
430 wine_dbgstr_longlong(stdobjref.oxid),
431 wine_dbgstr_longlong(stdobjref.oid),
432 debugstr_guid(&stdobjref.ipid));
433 hr = IStream_Write(pStm, &stdobjref, sizeof(stdobjref), NULL);
438 /* we don't have the interface already unmarshaled so we have to
439 * request the object from the server */
442 REMQIRESULT *qiresults = NULL;
445 /* get the ipid of the first entry */
446 /* FIXME: should we implement ClientIdentity on the ifproxies instead
447 * of the proxy_manager so we use the correct ipid here? */
448 ipid = &LIST_ENTRY(list_head(&This->interfaces), struct ifproxy, entry)->stdobjref.ipid;
450 /* get IRemUnknown proxy so we can communicate with the remote object */
451 hr = proxy_manager_get_remunknown(This, &remunk);
455 hr = IRemUnknown_RemQueryInterface(remunk, ipid, NORMALEXTREFS,
456 1, &iid, &qiresults);
459 hr = IStream_Write(pStm, &qiresults->std, sizeof(qiresults->std), NULL);
463 rif.ipid = qiresults->std.ipid;
464 rif.cPublicRefs = qiresults->std.cPublicRefs;
465 rif.cPrivateRefs = 0;
466 IRemUnknown_RemRelease(remunk, 1, &rif);
468 CoTaskMemFree(qiresults);
471 ERR("IRemUnknown_RemQueryInterface failed with error 0x%08x\n", hr);
472 IRemUnknown_Release(remunk);
479 static const IMarshalVtbl ProxyMarshal_Vtbl =
481 Proxy_QueryInterface,
484 StdMarshalImpl_GetUnmarshalClass,
485 StdMarshalImpl_GetMarshalSizeMax,
486 Proxy_MarshalInterface,
487 StdMarshalImpl_UnmarshalInterface,
488 StdMarshalImpl_ReleaseMarshalData,
489 StdMarshalImpl_DisconnectObject
492 static HRESULT WINAPI ProxyCliSec_QueryInterface(IClientSecurity *iface, REFIID riid, void **ppvObject)
494 ICOM_THIS_MULTI(struct proxy_manager, lpVtblCliSec, iface);
495 return IMultiQI_QueryInterface((IMultiQI *)&This->lpVtbl, riid, ppvObject);
498 static ULONG WINAPI ProxyCliSec_AddRef(IClientSecurity *iface)
500 ICOM_THIS_MULTI(struct proxy_manager, lpVtblCliSec, iface);
501 return IMultiQI_AddRef((IMultiQI *)&This->lpVtbl);
504 static ULONG WINAPI ProxyCliSec_Release(IClientSecurity *iface)
506 ICOM_THIS_MULTI(struct proxy_manager, lpVtblCliSec, iface);
507 return IMultiQI_Release((IMultiQI *)&This->lpVtbl);
510 static HRESULT WINAPI ProxyCliSec_QueryBlanket(IClientSecurity *iface,
514 OLECHAR **pServerPrincName,
518 DWORD *pCapabilities)
520 FIXME("(%p, %p, %p, %p, %p, %p, %p, %p): stub\n", pProxy, pAuthnSvc,
521 pAuthzSvc, pServerPrincName, pAuthnLevel, pImpLevel, pAuthInfo,
526 static HRESULT WINAPI ProxyCliSec_SetBlanket(IClientSecurity *iface,
527 IUnknown *pProxy, DWORD AuthnSvc,
529 OLECHAR *pServerPrincName,
530 DWORD AuthnLevel, DWORD ImpLevel,
534 FIXME("(%p, %d, %d, %s, %d, %d, %p, 0x%x): stub\n", pProxy, AuthnSvc,
535 AuthzSvc, debugstr_w(pServerPrincName), AuthnLevel, ImpLevel,
536 pAuthInfo, Capabilities);
540 static HRESULT WINAPI ProxyCliSec_CopyProxy(IClientSecurity *iface,
541 IUnknown *pProxy, IUnknown **ppCopy)
543 FIXME("(%p, %p): stub\n", pProxy, ppCopy);
548 static const IClientSecurityVtbl ProxyCliSec_Vtbl =
550 ProxyCliSec_QueryInterface,
553 ProxyCliSec_QueryBlanket,
554 ProxyCliSec_SetBlanket,
555 ProxyCliSec_CopyProxy
558 static HRESULT ifproxy_get_public_ref(struct ifproxy * This)
562 if (WAIT_OBJECT_0 != WaitForSingleObject(This->parent->remoting_mutex, INFINITE))
564 ERR("Wait failed for ifproxy %p\n", This);
570 IRemUnknown *remunk = NULL;
572 TRACE("getting public ref for ifproxy %p\n", This);
574 hr = proxy_manager_get_remunknown(This->parent, &remunk);
577 HRESULT hrref = S_OK;
579 rif.ipid = This->stdobjref.ipid;
580 rif.cPublicRefs = NORMALEXTREFS;
581 rif.cPrivateRefs = 0;
582 hr = IRemUnknown_RemAddRef(remunk, 1, &rif, &hrref);
583 IRemUnknown_Release(remunk);
584 if (hr == S_OK && hrref == S_OK)
585 InterlockedExchangeAdd((LONG *)&This->refs, NORMALEXTREFS);
587 ERR("IRemUnknown_RemAddRef returned with 0x%08x, hrref = 0x%08x\n", hr, hrref);
590 ReleaseMutex(This->parent->remoting_mutex);
595 static HRESULT ifproxy_release_public_refs(struct ifproxy * This)
600 if (WAIT_OBJECT_0 != WaitForSingleObject(This->parent->remoting_mutex, INFINITE))
602 ERR("Wait failed for ifproxy %p\n", This);
606 public_refs = This->refs;
609 IRemUnknown *remunk = NULL;
611 TRACE("releasing %d refs\n", public_refs);
613 hr = proxy_manager_get_remunknown(This->parent, &remunk);
617 rif.ipid = This->stdobjref.ipid;
618 rif.cPublicRefs = public_refs;
619 rif.cPrivateRefs = 0;
620 hr = IRemUnknown_RemRelease(remunk, 1, &rif);
621 IRemUnknown_Release(remunk);
623 InterlockedExchangeAdd((LONG *)&This->refs, -public_refs);
624 else if (hr == RPC_E_DISCONNECTED)
625 WARN("couldn't release references because object was "
626 "disconnected: oxid = %s, oid = %s\n",
627 wine_dbgstr_longlong(This->parent->oxid),
628 wine_dbgstr_longlong(This->parent->oid));
630 ERR("IRemUnknown_RemRelease failed with error 0x%08x\n", hr);
633 ReleaseMutex(This->parent->remoting_mutex);
638 /* should be called inside This->parent->cs critical section */
639 static void ifproxy_disconnect(struct ifproxy * This)
641 ifproxy_release_public_refs(This);
642 if (This->proxy) IRpcProxyBuffer_Disconnect(This->proxy);
644 IRpcChannelBuffer_Release(This->chan);
648 /* should be called in This->parent->cs critical section if it is an entry in parent's list */
649 static void ifproxy_destroy(struct ifproxy * This)
653 /* release public references to this object so that the stub can know
654 * when to destroy itself */
655 ifproxy_release_public_refs(This);
657 list_remove(&This->entry);
661 IRpcChannelBuffer_Release(This->chan);
665 if (This->proxy) IRpcProxyBuffer_Release(This->proxy);
667 HeapFree(GetProcessHeap(), 0, This);
670 static HRESULT proxy_manager_construct(
671 APARTMENT * apt, ULONG sorflags, OXID oxid, OID oid,
672 const OXID_INFO *oxid_info, struct proxy_manager ** proxy_manager)
674 struct proxy_manager * This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
675 if (!This) return E_OUTOFMEMORY;
677 This->remoting_mutex = CreateMutexW(NULL, FALSE, NULL);
678 if (!This->remoting_mutex)
680 HeapFree(GetProcessHeap(), 0, This);
681 return HRESULT_FROM_WIN32(GetLastError());
686 This->oxid_info.dwPid = oxid_info->dwPid;
687 This->oxid_info.dwTid = oxid_info->dwTid;
688 This->oxid_info.ipidRemUnknown = oxid_info->ipidRemUnknown;
689 This->oxid_info.dwAuthnHint = oxid_info->dwAuthnHint;
690 This->oxid_info.psa = NULL /* FIXME: copy from oxid_info */;
694 HRESULT hr = RPC_ResolveOxid(oxid, &This->oxid_info);
697 CloseHandle(This->remoting_mutex);
698 HeapFree(GetProcessHeap(), 0, This);
703 This->lpVtbl = &ClientIdentity_Vtbl;
704 This->lpVtblMarshal = &ProxyMarshal_Vtbl;
705 This->lpVtblCliSec = &ProxyCliSec_Vtbl;
707 list_init(&This->entry);
708 list_init(&This->interfaces);
710 InitializeCriticalSection(&This->cs);
711 DEBUG_SET_CRITSEC_NAME(&This->cs, "proxy_manager");
713 /* the apartment the object was unmarshaled into */
716 /* the source apartment and id of the object */
722 /* the DCOM draft specification states that the SORF_NOPING flag is
723 * proxy manager specific, not ifproxy specific, so this implies that we
724 * should store the STDOBJREF flags here in the proxy manager. */
725 This->sorflags = sorflags;
727 /* we create the IRemUnknown proxy on demand */
730 /* initialise these values to the weakest values and they will be
731 * overwritten in proxy_manager_set_context */
732 This->dest_context = MSHCTX_INPROC;
733 This->dest_context_data = NULL;
735 EnterCriticalSection(&apt->cs);
736 /* FIXME: we are dependent on the ordering in here to make sure a proxy's
737 * IRemUnknown proxy doesn't get destroyed before the regual proxy does
738 * because we need the IRemUnknown proxy during the destruction of the
739 * regular proxy. Ideally, we should maintain a separate list for the
740 * IRemUnknown proxies that need late destruction */
741 list_add_tail(&apt->proxies, &This->entry);
742 LeaveCriticalSection(&apt->cs);
744 TRACE("%p created for OXID %s, OID %s\n", This,
745 wine_dbgstr_longlong(oxid), wine_dbgstr_longlong(oid));
747 *proxy_manager = This;
751 static inline void proxy_manager_set_context(struct proxy_manager *This, MSHCTX dest_context, void *dest_context_data)
753 MSHCTX old_dest_context = This->dest_context;
754 MSHCTX new_dest_context;
758 new_dest_context = old_dest_context;
759 /* "stronger" values overwrite "weaker" values. stronger values are
760 * ones that disable more optimisations */
761 switch (old_dest_context)
764 new_dest_context = dest_context;
766 case MSHCTX_CROSSCTX:
767 switch (dest_context)
772 new_dest_context = dest_context;
776 switch (dest_context)
779 case MSHCTX_CROSSCTX:
782 new_dest_context = dest_context;
785 case MSHCTX_NOSHAREDMEM:
786 switch (dest_context)
788 case MSHCTX_DIFFERENTMACHINE:
789 new_dest_context = dest_context;
799 if (old_dest_context == new_dest_context) break;
801 old_dest_context = InterlockedCompareExchange((PLONG)&This->dest_context, new_dest_context, old_dest_context);
802 } while (new_dest_context != old_dest_context);
804 if (dest_context_data)
805 InterlockedExchangePointer(&This->dest_context_data, dest_context_data);
808 static HRESULT proxy_manager_query_local_interface(struct proxy_manager * This, REFIID riid, void ** ppv)
811 struct ifproxy * ifproxy;
813 TRACE("%s\n", debugstr_guid(riid));
815 if (IsEqualIID(riid, &IID_IUnknown) ||
816 IsEqualIID(riid, &IID_IMultiQI))
818 *ppv = (void *)&This->lpVtbl;
819 IUnknown_AddRef((IUnknown *)*ppv);
822 if (IsEqualIID(riid, &IID_IMarshal))
824 *ppv = (void *)&This->lpVtblMarshal;
825 IUnknown_AddRef((IUnknown *)*ppv);
828 if (IsEqualIID(riid, &IID_IClientSecurity))
830 *ppv = (void *)&This->lpVtblCliSec;
831 IUnknown_AddRef((IUnknown *)*ppv);
835 hr = proxy_manager_find_ifproxy(This, riid, &ifproxy);
838 *ppv = ifproxy->iface;
839 IUnknown_AddRef((IUnknown *)*ppv);
844 return E_NOINTERFACE;
847 static HRESULT proxy_manager_create_ifproxy(
848 struct proxy_manager * This, const STDOBJREF *stdobjref, REFIID riid,
849 IRpcChannelBuffer * channel, struct ifproxy ** iif_out)
852 IPSFactoryBuffer * psfb;
853 struct ifproxy * ifproxy = HeapAlloc(GetProcessHeap(), 0, sizeof(*ifproxy));
854 if (!ifproxy) return E_OUTOFMEMORY;
856 list_init(&ifproxy->entry);
858 ifproxy->parent = This;
859 ifproxy->stdobjref = *stdobjref;
860 ifproxy->iid = *riid;
862 ifproxy->proxy = NULL;
865 ifproxy->chan = channel; /* FIXME: we should take the binding strings and construct the channel in this function */
867 /* the IUnknown interface is special because it does not have a
868 * proxy associated with the ifproxy as we handle IUnknown ourselves */
869 if (IsEqualIID(riid, &IID_IUnknown))
871 ifproxy->iface = (void *)&This->lpVtbl;
872 IMultiQI_AddRef((IMultiQI *)&This->lpVtbl);
877 hr = get_facbuf_for_iid(riid, &psfb);
880 /* important note: the outer unknown is set to the proxy manager.
881 * This ensures the COM identity rules are not violated, by having a
882 * one-to-one mapping of objects on the proxy side to objects on the
883 * stub side, no matter which interface you view the object through */
884 hr = IPSFactoryBuffer_CreateProxy(psfb, (IUnknown *)&This->lpVtbl, riid,
885 &ifproxy->proxy, &ifproxy->iface);
886 IPSFactoryBuffer_Release(psfb);
888 ERR("Could not create proxy for interface %s, error 0x%08x\n",
889 debugstr_guid(riid), hr);
892 ERR("Could not get IPSFactoryBuffer for interface %s, error 0x%08x\n",
893 debugstr_guid(riid), hr);
896 hr = IRpcProxyBuffer_Connect(ifproxy->proxy, ifproxy->chan);
901 EnterCriticalSection(&This->cs);
902 list_add_tail(&This->interfaces, &ifproxy->entry);
903 LeaveCriticalSection(&This->cs);
906 TRACE("ifproxy %p created for IPID %s, interface %s with %lu public refs\n",
907 ifproxy, debugstr_guid(&stdobjref->ipid), debugstr_guid(riid), stdobjref->cPublicRefs);
910 ifproxy_destroy(ifproxy);
915 static HRESULT proxy_manager_find_ifproxy(struct proxy_manager * This, REFIID riid, struct ifproxy ** ifproxy_found)
917 HRESULT hr = E_NOINTERFACE; /* assume not found */
918 struct list * cursor;
920 EnterCriticalSection(&This->cs);
921 LIST_FOR_EACH(cursor, &This->interfaces)
923 struct ifproxy * ifproxy = LIST_ENTRY(cursor, struct ifproxy, entry);
924 if (IsEqualIID(riid, &ifproxy->iid))
926 *ifproxy_found = ifproxy;
931 LeaveCriticalSection(&This->cs);
936 static void proxy_manager_disconnect(struct proxy_manager * This)
938 struct list * cursor;
940 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This->oxid),
941 wine_dbgstr_longlong(This->oid));
943 EnterCriticalSection(&This->cs);
945 /* SORFP_NOLIFTIMEMGMT proxies (for IRemUnknown) shouldn't be
946 * disconnected - it won't do anything anyway, except cause
947 * problems for other objects that depend on this proxy always
949 if (!(This->sorflags & SORFP_NOLIFETIMEMGMT))
951 LIST_FOR_EACH(cursor, &This->interfaces)
953 struct ifproxy * ifproxy = LIST_ENTRY(cursor, struct ifproxy, entry);
954 ifproxy_disconnect(ifproxy);
958 /* apartment is being destroyed so don't keep a pointer around to it */
961 LeaveCriticalSection(&This->cs);
964 static HRESULT proxy_manager_get_remunknown(struct proxy_manager * This, IRemUnknown **remunk)
967 struct apartment *apt;
968 BOOL called_in_original_apt;
970 /* we don't want to try and unmarshal or use IRemUnknown if we don't want
971 * lifetime management */
972 if (This->sorflags & SORFP_NOLIFETIMEMGMT)
975 apt = COM_CurrentApt();
977 return CO_E_NOTINITIALIZED;
979 called_in_original_apt = This->parent && (This->parent->oxid == apt->oxid);
981 EnterCriticalSection(&This->cs);
982 /* only return the cached object if called from the original apartment.
983 * in future, we might want to make the IRemUnknown proxy callable from any
984 * apartment to avoid these checks */
985 if (This->remunk && called_in_original_apt)
987 /* already created - return existing object */
988 *remunk = This->remunk;
989 IRemUnknown_AddRef(*remunk);
991 else if (!This->parent)
992 /* disconnected - we can't create IRemUnknown */
997 /* Don't want IRemUnknown lifetime management as this is IRemUnknown!
998 * We also don't care about whether or not the stub is still alive */
999 stdobjref.flags = SORFP_NOLIFETIMEMGMT | SORF_NOPING;
1000 stdobjref.cPublicRefs = 1;
1001 /* oxid of destination object */
1002 stdobjref.oxid = This->oxid;
1003 /* FIXME: what should be used for the oid? The DCOM draft doesn't say */
1004 stdobjref.oid = (OID)-1;
1005 stdobjref.ipid = This->oxid_info.ipidRemUnknown;
1007 /* do the unmarshal */
1008 hr = unmarshal_object(&stdobjref, COM_CurrentApt(), This->dest_context,
1009 This->dest_context_data, &IID_IRemUnknown,
1010 &This->oxid_info, (void**)remunk);
1011 if (hr == S_OK && called_in_original_apt)
1013 This->remunk = *remunk;
1014 IRemUnknown_AddRef(This->remunk);
1017 LeaveCriticalSection(&This->cs);
1019 TRACE("got IRemUnknown* pointer %p, hr = 0x%08x\n", *remunk, hr);
1024 /* destroys a proxy manager, freeing the memory it used.
1025 * Note: this function should not be called from a list iteration in the
1026 * apartment, due to the fact that it removes itself from the apartment and
1027 * it could add a proxy to IRemUnknown into the apartment. */
1028 static void proxy_manager_destroy(struct proxy_manager * This)
1030 struct list * cursor;
1032 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This->oxid),
1033 wine_dbgstr_longlong(This->oid));
1037 EnterCriticalSection(&This->parent->cs);
1039 /* remove ourself from the list of proxy objects in the apartment */
1040 LIST_FOR_EACH(cursor, &This->parent->proxies)
1042 if (cursor == &This->entry)
1044 list_remove(&This->entry);
1049 LeaveCriticalSection(&This->parent->cs);
1052 /* destroy all of the interface proxies */
1053 while ((cursor = list_head(&This->interfaces)))
1055 struct ifproxy * ifproxy = LIST_ENTRY(cursor, struct ifproxy, entry);
1056 ifproxy_destroy(ifproxy);
1059 if (This->remunk) IRemUnknown_Release(This->remunk);
1060 CoTaskMemFree(This->oxid_info.psa);
1062 DEBUG_CLEAR_CRITSEC_NAME(&This->cs);
1063 DeleteCriticalSection(&This->cs);
1065 CloseHandle(This->remoting_mutex);
1067 HeapFree(GetProcessHeap(), 0, This);
1070 /* finds the proxy manager corresponding to a given OXID and OID that has
1071 * been unmarshaled in the specified apartment. The caller must release the
1072 * reference to the proxy_manager when the object is no longer used. */
1073 static BOOL find_proxy_manager(APARTMENT * apt, OXID oxid, OID oid, struct proxy_manager ** proxy_found)
1076 struct list * cursor;
1078 EnterCriticalSection(&apt->cs);
1079 LIST_FOR_EACH(cursor, &apt->proxies)
1081 struct proxy_manager * proxy = LIST_ENTRY(cursor, struct proxy_manager, entry);
1082 if ((oxid == proxy->oxid) && (oid == proxy->oid))
1084 *proxy_found = proxy;
1085 ClientIdentity_AddRef((IMultiQI *)&proxy->lpVtbl);
1090 LeaveCriticalSection(&apt->cs);
1094 HRESULT apartment_disconnectproxies(struct apartment *apt)
1096 struct list * cursor;
1098 LIST_FOR_EACH(cursor, &apt->proxies)
1100 struct proxy_manager * proxy = LIST_ENTRY(cursor, struct proxy_manager, entry);
1101 proxy_manager_disconnect(proxy);
1107 /********************** StdMarshal implementation ****************************/
1108 typedef struct _StdMarshalImpl
1110 const IMarshalVtbl *lpvtbl;
1114 DWORD dwDestContext;
1115 LPVOID pvDestContext;
1119 static HRESULT WINAPI
1120 StdMarshalImpl_QueryInterface(LPMARSHAL iface, REFIID riid, LPVOID *ppv)
1123 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IMarshal, riid))
1126 IUnknown_AddRef(iface);
1129 FIXME("No interface for %s.\n", debugstr_guid(riid));
1130 return E_NOINTERFACE;
1134 StdMarshalImpl_AddRef(LPMARSHAL iface)
1136 StdMarshalImpl *This = (StdMarshalImpl *)iface;
1137 return InterlockedIncrement(&This->ref);
1141 StdMarshalImpl_Release(LPMARSHAL iface)
1143 StdMarshalImpl *This = (StdMarshalImpl *)iface;
1144 ULONG ref = InterlockedDecrement(&This->ref);
1146 if (!ref) HeapFree(GetProcessHeap(),0,This);
1150 static HRESULT WINAPI
1151 StdMarshalImpl_GetUnmarshalClass(
1152 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
1153 void* pvDestContext, DWORD mshlflags, CLSID* pCid)
1155 *pCid = CLSID_DfMarshal;
1159 static HRESULT WINAPI
1160 StdMarshalImpl_GetMarshalSizeMax(
1161 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
1162 void* pvDestContext, DWORD mshlflags, DWORD* pSize)
1164 *pSize = sizeof(STDOBJREF);
1168 static HRESULT WINAPI
1169 StdMarshalImpl_MarshalInterface(
1170 LPMARSHAL iface, IStream *pStm,REFIID riid, void* pv, DWORD dwDestContext,
1171 void* pvDestContext, DWORD mshlflags)
1173 STDOBJREF stdobjref;
1176 APARTMENT *apt = COM_CurrentApt();
1178 TRACE("(...,%s,...)\n", debugstr_guid(riid));
1182 ERR("Apartment not initialized\n");
1183 return CO_E_NOTINITIALIZED;
1186 /* make sure this apartment can be reached from other threads / processes */
1187 RPC_StartRemoting(apt);
1189 hres = marshal_object(apt, &stdobjref, riid, (IUnknown *)pv, mshlflags);
1192 ERR("Failed to create ifstub, hres=0x%x\n", hres);
1196 hres = IStream_Write(pStm, &stdobjref, sizeof(stdobjref), &res);
1197 if (hres) return hres;
1202 /* helper for StdMarshalImpl_UnmarshalInterface - does the unmarshaling with
1203 * no questions asked about the rules surrounding same-apartment unmarshals
1204 * and table marshaling */
1205 static HRESULT unmarshal_object(const STDOBJREF *stdobjref, APARTMENT *apt,
1206 MSHCTX dest_context, void *dest_context_data,
1207 REFIID riid, const OXID_INFO *oxid_info,
1210 struct proxy_manager *proxy_manager = NULL;
1215 TRACE("stdobjref:\n\tflags = %04lx\n\tcPublicRefs = %ld\n\toxid = %s\n\toid = %s\n\tipid = %s\n",
1216 stdobjref->flags, stdobjref->cPublicRefs,
1217 wine_dbgstr_longlong(stdobjref->oxid),
1218 wine_dbgstr_longlong(stdobjref->oid),
1219 debugstr_guid(&stdobjref->ipid));
1221 /* create a new proxy manager if one doesn't already exist for the
1223 if (!find_proxy_manager(apt, stdobjref->oxid, stdobjref->oid, &proxy_manager))
1225 hr = proxy_manager_construct(apt, stdobjref->flags,
1226 stdobjref->oxid, stdobjref->oid, oxid_info,
1230 TRACE("proxy manager already created, using\n");
1234 struct ifproxy * ifproxy;
1236 proxy_manager_set_context(proxy_manager, dest_context, dest_context_data);
1238 hr = proxy_manager_find_ifproxy(proxy_manager, riid, &ifproxy);
1239 if (hr == E_NOINTERFACE)
1241 IRpcChannelBuffer *chanbuf;
1242 hr = RPC_CreateClientChannel(&stdobjref->oxid, &stdobjref->ipid,
1243 &proxy_manager->oxid_info,
1244 proxy_manager->dest_context,
1245 proxy_manager->dest_context_data,
1248 hr = proxy_manager_create_ifproxy(proxy_manager, stdobjref,
1249 riid, chanbuf, &ifproxy);
1252 IUnknown_AddRef((IUnknown *)ifproxy->iface);
1256 InterlockedExchangeAdd((LONG *)&ifproxy->refs, stdobjref->cPublicRefs);
1257 /* get at least one external reference to the object to keep it alive */
1258 hr = ifproxy_get_public_ref(ifproxy);
1260 ifproxy_destroy(ifproxy);
1264 *object = ifproxy->iface;
1267 /* release our reference to the proxy manager - the client/apartment
1268 * will hold on to the remaining reference for us */
1269 if (proxy_manager) ClientIdentity_Release((IMultiQI*)&proxy_manager->lpVtbl);
1274 static HRESULT WINAPI
1275 StdMarshalImpl_UnmarshalInterface(LPMARSHAL iface, IStream *pStm, REFIID riid, void **ppv)
1277 StdMarshalImpl *This = (StdMarshalImpl *)iface;
1278 struct stub_manager *stubmgr = NULL;
1279 STDOBJREF stdobjref;
1282 APARTMENT *apt = COM_CurrentApt();
1283 APARTMENT *stub_apt;
1286 TRACE("(...,%s,....)\n", debugstr_guid(riid));
1288 /* we need an apartment to unmarshal into */
1291 ERR("Apartment not initialized\n");
1292 return CO_E_NOTINITIALIZED;
1295 /* read STDOBJREF from wire */
1296 hres = IStream_Read(pStm, &stdobjref, sizeof(stdobjref), &res);
1297 if (hres) return STG_E_READFAULT;
1299 hres = apartment_getoxid(apt, &oxid);
1300 if (hres) return hres;
1302 /* check if we're marshalling back to ourselves */
1303 if ((oxid == stdobjref.oxid) && (stubmgr = get_stub_manager(apt, stdobjref.oid)))
1305 TRACE("Unmarshalling object marshalled in same apartment for iid %s, "
1306 "returning original object %p\n", debugstr_guid(riid), stubmgr->object);
1308 hres = IUnknown_QueryInterface(stubmgr->object, riid, ppv);
1310 /* unref the ifstub. FIXME: only do this on success? */
1311 if (!stub_manager_is_table_marshaled(stubmgr, &stdobjref.ipid))
1312 stub_manager_ext_release(stubmgr, stdobjref.cPublicRefs, TRUE);
1314 stub_manager_int_release(stubmgr);
1318 /* notify stub manager about unmarshal if process-local object.
1319 * note: if the oxid is not found then we and native will quite happily
1320 * ignore table marshaling and normal marshaling rules regarding number of
1321 * unmarshals, etc, but if you abuse these rules then your proxy could end
1322 * up returning RPC_E_DISCONNECTED. */
1323 if ((stub_apt = apartment_findfromoxid(stdobjref.oxid, TRUE)))
1325 if ((stubmgr = get_stub_manager(stub_apt, stdobjref.oid)))
1327 if (!stub_manager_notify_unmarshal(stubmgr, &stdobjref.ipid))
1328 hres = CO_E_OBJNOTCONNECTED;
1332 WARN("Couldn't find object for OXID %s, OID %s, assuming disconnected\n",
1333 wine_dbgstr_longlong(stdobjref.oxid),
1334 wine_dbgstr_longlong(stdobjref.oid));
1335 hres = CO_E_OBJNOTCONNECTED;
1339 TRACE("Treating unmarshal from OXID %s as inter-process\n",
1340 wine_dbgstr_longlong(stdobjref.oxid));
1343 hres = unmarshal_object(&stdobjref, apt, This->dwDestContext,
1344 This->pvDestContext, riid,
1345 stubmgr ? &stubmgr->oxid_info : NULL, ppv);
1347 if (stubmgr) stub_manager_int_release(stubmgr);
1348 if (stub_apt) apartment_release(stub_apt);
1350 if (hres) WARN("Failed with error 0x%08x\n", hres);
1351 else TRACE("Successfully created proxy %p\n", *ppv);
1356 static HRESULT WINAPI
1357 StdMarshalImpl_ReleaseMarshalData(LPMARSHAL iface, IStream *pStm)
1359 STDOBJREF stdobjref;
1362 struct stub_manager *stubmgr;
1365 TRACE("iface=%p, pStm=%p\n", iface, pStm);
1367 hres = IStream_Read(pStm, &stdobjref, sizeof(stdobjref), &res);
1368 if (hres) return STG_E_READFAULT;
1370 TRACE("oxid = %s, oid = %s, ipid = %s\n",
1371 wine_dbgstr_longlong(stdobjref.oxid),
1372 wine_dbgstr_longlong(stdobjref.oid),
1373 wine_dbgstr_guid(&stdobjref.ipid));
1375 if (!(apt = apartment_findfromoxid(stdobjref.oxid, TRUE)))
1377 WARN("Could not map OXID %s to apartment object\n",
1378 wine_dbgstr_longlong(stdobjref.oxid));
1379 return RPC_E_INVALID_OBJREF;
1382 if (!(stubmgr = get_stub_manager(apt, stdobjref.oid)))
1384 ERR("could not map object ID to stub manager, oxid=%s, oid=%s\n",
1385 wine_dbgstr_longlong(stdobjref.oxid), wine_dbgstr_longlong(stdobjref.oid));
1386 return RPC_E_INVALID_OBJREF;
1389 stub_manager_release_marshal_data(stubmgr, stdobjref.cPublicRefs, &stdobjref.ipid);
1391 stub_manager_int_release(stubmgr);
1392 apartment_release(apt);
1397 static HRESULT WINAPI
1398 StdMarshalImpl_DisconnectObject(LPMARSHAL iface, DWORD dwReserved)
1400 FIXME("(), stub!\n");
1404 static const IMarshalVtbl VT_StdMarshal =
1406 StdMarshalImpl_QueryInterface,
1407 StdMarshalImpl_AddRef,
1408 StdMarshalImpl_Release,
1409 StdMarshalImpl_GetUnmarshalClass,
1410 StdMarshalImpl_GetMarshalSizeMax,
1411 StdMarshalImpl_MarshalInterface,
1412 StdMarshalImpl_UnmarshalInterface,
1413 StdMarshalImpl_ReleaseMarshalData,
1414 StdMarshalImpl_DisconnectObject
1417 static HRESULT StdMarshalImpl_Construct(REFIID riid, void** ppvObject)
1419 StdMarshalImpl * pStdMarshal =
1420 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(StdMarshalImpl));
1422 return E_OUTOFMEMORY;
1423 pStdMarshal->lpvtbl = &VT_StdMarshal;
1424 pStdMarshal->ref = 0;
1425 return IMarshal_QueryInterface((IMarshal*)pStdMarshal, riid, ppvObject);
1428 /***********************************************************************
1429 * CoGetStandardMarshal [OLE32.@]
1431 * Gets or creates a standard marshal object.
1434 * riid [I] Interface identifier of the pUnk object.
1435 * pUnk [I] Optional. Object to get the marshal object for.
1436 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1437 * pvDestContext [I] Reserved. Must be NULL.
1438 * mshlflags [I] Flags affecting the marshaling process.
1439 * ppMarshal [O] Address where marshal object will be stored.
1443 * Failure: HRESULT code.
1447 * The function retrieves the IMarshal object associated with an object if
1448 * that object is currently an active stub, otherwise a new marshal object is
1451 HRESULT WINAPI CoGetStandardMarshal(REFIID riid, IUnknown *pUnk,
1452 DWORD dwDestContext, LPVOID pvDestContext,
1453 DWORD mshlflags, LPMARSHAL *ppMarshal)
1459 FIXME("(%s,NULL,%x,%p,%x,%p), unimplemented yet.\n",
1460 debugstr_guid(riid),dwDestContext,pvDestContext,mshlflags,ppMarshal);
1463 TRACE("(%s,%p,%x,%p,%x,%p)\n",
1464 debugstr_guid(riid),pUnk,dwDestContext,pvDestContext,mshlflags,ppMarshal);
1465 *ppMarshal = HeapAlloc(GetProcessHeap(),0,sizeof(StdMarshalImpl));
1466 dm = (StdMarshalImpl*) *ppMarshal;
1467 if (!dm) return E_FAIL;
1468 dm->lpvtbl = &VT_StdMarshal;
1472 dm->dwDestContext = dwDestContext;
1473 dm->pvDestContext = pvDestContext;
1474 dm->mshlflags = mshlflags;
1478 /***********************************************************************
1479 * get_marshaler [internal]
1481 * Retrieves an IMarshal interface for an object.
1483 static HRESULT get_marshaler(REFIID riid, IUnknown *pUnk, DWORD dwDestContext,
1484 void *pvDestContext, DWORD mshlFlags,
1485 LPMARSHAL *pMarshal)
1491 hr = IUnknown_QueryInterface(pUnk, &IID_IMarshal, (LPVOID*)pMarshal);
1493 hr = CoGetStandardMarshal(riid, pUnk, dwDestContext, pvDestContext,
1494 mshlFlags, pMarshal);
1498 /***********************************************************************
1499 * get_unmarshaler_from_stream [internal]
1501 * Creates an IMarshal* object according to the data marshaled to the stream.
1502 * The function leaves the stream pointer at the start of the data written
1503 * to the stream by the IMarshal* object.
1505 static HRESULT get_unmarshaler_from_stream(IStream *stream, IMarshal **marshal, IID *iid)
1511 /* read common OBJREF header */
1512 hr = IStream_Read(stream, &objref, FIELD_OFFSET(OBJREF, u_objref), &res);
1513 if (hr || (res != FIELD_OFFSET(OBJREF, u_objref)))
1515 ERR("Failed to read common OBJREF header, 0x%08x\n", hr);
1516 return STG_E_READFAULT;
1519 /* sanity check on header */
1520 if (objref.signature != OBJREF_SIGNATURE)
1522 ERR("Bad OBJREF signature 0x%08lx\n", objref.signature);
1523 return RPC_E_INVALID_OBJREF;
1526 if (iid) *iid = objref.iid;
1528 /* FIXME: handler marshaling */
1529 if (objref.flags & OBJREF_STANDARD)
1531 TRACE("Using standard unmarshaling\n");
1532 hr = StdMarshalImpl_Construct(&IID_IMarshal, (LPVOID*)marshal);
1534 else if (objref.flags & OBJREF_CUSTOM)
1536 ULONG custom_header_size = FIELD_OFFSET(OBJREF, u_objref.u_custom.pData) -
1537 FIELD_OFFSET(OBJREF, u_objref.u_custom);
1538 TRACE("Using custom unmarshaling\n");
1539 /* read constant sized OR_CUSTOM data from stream */
1540 hr = IStream_Read(stream, &objref.u_objref.u_custom,
1541 custom_header_size, &res);
1542 if (hr || (res != custom_header_size))
1544 ERR("Failed to read OR_CUSTOM header, 0x%08x\n", hr);
1545 return STG_E_READFAULT;
1547 /* now create the marshaler specified in the stream */
1548 hr = CoCreateInstance(&objref.u_objref.u_custom.clsid, NULL,
1549 CLSCTX_INPROC_SERVER, &IID_IMarshal,
1554 FIXME("Invalid or unimplemented marshaling type specified: %lx\n",
1556 return RPC_E_INVALID_OBJREF;
1560 ERR("Failed to create marshal, 0x%08x\n", hr);
1565 /***********************************************************************
1566 * CoGetMarshalSizeMax [OLE32.@]
1568 * Gets the maximum amount of data that will be needed by a marshal.
1571 * pulSize [O] Address where maximum marshal size will be stored.
1572 * riid [I] Identifier of the interface to marshal.
1573 * pUnk [I] Pointer to the object to marshal.
1574 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1575 * pvDestContext [I] Reserved. Must be NULL.
1576 * mshlFlags [I] Flags that affect the marshaling. See CoMarshalInterface().
1580 * Failure: HRESULT code.
1583 * CoMarshalInterface().
1585 HRESULT WINAPI CoGetMarshalSizeMax(ULONG *pulSize, REFIID riid, IUnknown *pUnk,
1586 DWORD dwDestContext, void *pvDestContext,
1591 CLSID marshaler_clsid;
1593 hr = get_marshaler(riid, pUnk, dwDestContext, pvDestContext, mshlFlags, &pMarshal);
1597 hr = IMarshal_GetUnmarshalClass(pMarshal, riid, pUnk, dwDestContext,
1598 pvDestContext, mshlFlags, &marshaler_clsid);
1601 ERR("IMarshal::GetUnmarshalClass failed, 0x%08x\n", hr);
1602 IMarshal_Release(pMarshal);
1606 hr = IMarshal_GetMarshalSizeMax(pMarshal, riid, pUnk, dwDestContext,
1607 pvDestContext, mshlFlags, pulSize);
1608 if (IsEqualCLSID(&marshaler_clsid, &CLSID_DfMarshal))
1609 /* add on the size of the common header */
1610 *pulSize += FIELD_OFFSET(OBJREF, u_objref);
1612 /* custom marshaling: add on the size of the whole OBJREF structure
1613 * like native does */
1614 *pulSize += sizeof(OBJREF);
1616 IMarshal_Release(pMarshal);
1621 static void dump_MSHLFLAGS(MSHLFLAGS flags)
1623 if (flags & MSHLFLAGS_TABLESTRONG)
1624 TRACE(" MSHLFLAGS_TABLESTRONG");
1625 if (flags & MSHLFLAGS_TABLEWEAK)
1626 TRACE(" MSHLFLAGS_TABLEWEAK");
1627 if (!(flags & (MSHLFLAGS_TABLESTRONG|MSHLFLAGS_TABLEWEAK)))
1628 TRACE(" MSHLFLAGS_NORMAL");
1629 if (flags & MSHLFLAGS_NOPING)
1630 TRACE(" MSHLFLAGS_NOPING");
1633 /***********************************************************************
1634 * CoMarshalInterface [OLE32.@]
1636 * Marshals an interface into a stream so that the object can then be
1637 * unmarshaled from another COM apartment and used remotely.
1640 * pStream [I] Stream the object will be marshaled into.
1641 * riid [I] Identifier of the interface to marshal.
1642 * pUnk [I] Pointer to the object to marshal.
1643 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1644 * pvDestContext [I] Reserved. Must be NULL.
1645 * mshlFlags [I] Flags that affect the marshaling. See notes.
1649 * Failure: HRESULT code.
1653 * The mshlFlags parameter can take one or more of the following flags:
1654 *| MSHLFLAGS_NORMAL - Unmarshal once, releases stub on last proxy release.
1655 *| MSHLFLAGS_TABLESTRONG - Unmarshal many, release when CoReleaseMarshalData() called.
1656 *| MSHLFLAGS_TABLEWEAK - Unmarshal many, releases stub on last proxy release.
1657 *| MSHLFLAGS_NOPING - No automatic garbage collection (and so reduces network traffic).
1659 * If a marshaled object is not unmarshaled, then CoReleaseMarshalData() must
1660 * be called in order to release the resources used in the marshaling.
1663 * CoUnmarshalInterface(), CoReleaseMarshalData().
1665 HRESULT WINAPI CoMarshalInterface(IStream *pStream, REFIID riid, IUnknown *pUnk,
1666 DWORD dwDestContext, void *pvDestContext,
1670 CLSID marshaler_clsid;
1674 TRACE("(%p, %s, %p, %x, %p,", pStream, debugstr_guid(riid), pUnk,
1675 dwDestContext, pvDestContext);
1676 dump_MSHLFLAGS(mshlFlags);
1679 if (!pUnk || !pStream)
1680 return E_INVALIDARG;
1682 objref.signature = OBJREF_SIGNATURE;
1685 /* get the marshaler for the specified interface */
1686 hr = get_marshaler(riid, pUnk, dwDestContext, pvDestContext, mshlFlags, &pMarshal);
1689 ERR("Failed to get marshaller, 0x%08x\n", hr);
1693 hr = IMarshal_GetUnmarshalClass(pMarshal, riid, pUnk, dwDestContext,
1694 pvDestContext, mshlFlags, &marshaler_clsid);
1697 ERR("IMarshal::GetUnmarshalClass failed, 0x%08x\n", hr);
1701 /* FIXME: implement handler marshaling too */
1702 if (IsEqualCLSID(&marshaler_clsid, &CLSID_DfMarshal))
1704 TRACE("Using standard marshaling\n");
1705 objref.flags = OBJREF_STANDARD;
1707 /* write the common OBJREF header to the stream */
1708 hr = IStream_Write(pStream, &objref, FIELD_OFFSET(OBJREF, u_objref), NULL);
1711 ERR("Failed to write OBJREF header to stream, 0x%08x\n", hr);
1717 TRACE("Using custom marshaling\n");
1718 objref.flags = OBJREF_CUSTOM;
1719 objref.u_objref.u_custom.clsid = marshaler_clsid;
1720 objref.u_objref.u_custom.cbExtension = 0;
1721 objref.u_objref.u_custom.size = 0;
1722 hr = IMarshal_GetMarshalSizeMax(pMarshal, riid, pUnk, dwDestContext,
1723 pvDestContext, mshlFlags,
1724 &objref.u_objref.u_custom.size);
1727 ERR("Failed to get max size of marshal data, error 0x%08x\n", hr);
1730 /* write constant sized common header and OR_CUSTOM data into stream */
1731 hr = IStream_Write(pStream, &objref,
1732 FIELD_OFFSET(OBJREF, u_objref.u_custom.pData), NULL);
1735 ERR("Failed to write OR_CUSTOM header to stream with 0x%08x\n", hr);
1740 TRACE("Calling IMarshal::MarshalInterace\n");
1741 /* call helper object to do the actual marshaling */
1742 hr = IMarshal_MarshalInterface(pMarshal, pStream, riid, pUnk, dwDestContext,
1743 pvDestContext, mshlFlags);
1747 ERR("Failed to marshal the interface %s, %x\n", debugstr_guid(riid), hr);
1752 IMarshal_Release(pMarshal);
1754 TRACE("completed with hr 0x%08x\n", hr);
1759 /***********************************************************************
1760 * CoUnmarshalInterface [OLE32.@]
1762 * Unmarshals an object from a stream by creating a proxy to the remote
1763 * object, if necessary.
1767 * pStream [I] Stream containing the marshaled object.
1768 * riid [I] Interface identifier of the object to create a proxy to.
1769 * ppv [O] Address where proxy will be stored.
1774 * Failure: HRESULT code.
1777 * CoMarshalInterface().
1779 HRESULT WINAPI CoUnmarshalInterface(IStream *pStream, REFIID riid, LPVOID *ppv)
1786 TRACE("(%p, %s, %p)\n", pStream, debugstr_guid(riid), ppv);
1788 if (!pStream || !ppv)
1789 return E_INVALIDARG;
1791 hr = get_unmarshaler_from_stream(pStream, &pMarshal, &iid);
1795 /* call the helper object to do the actual unmarshaling */
1796 hr = IMarshal_UnmarshalInterface(pMarshal, pStream, &iid, (LPVOID*)&object);
1798 ERR("IMarshal::UnmarshalInterface failed, 0x%08x\n", hr);
1802 /* IID_NULL means use the interface ID of the marshaled object */
1803 if (!IsEqualIID(riid, &IID_NULL) && !IsEqualIID(riid, &iid))
1805 TRACE("requested interface != marshalled interface, additional QI needed\n");
1806 hr = IUnknown_QueryInterface(object, riid, ppv);
1808 ERR("Couldn't query for interface %s, hr = 0x%08x\n",
1809 debugstr_guid(riid), hr);
1810 IUnknown_Release(object);
1818 IMarshal_Release(pMarshal);
1820 TRACE("completed with hr 0x%x\n", hr);
1825 /***********************************************************************
1826 * CoReleaseMarshalData [OLE32.@]
1828 * Releases resources associated with an object that has been marshaled into
1833 * pStream [I] The stream that the object has been marshaled into.
1837 * Failure: HRESULT error code.
1841 * Call this function to release resources associated with a normal or
1842 * table-weak marshal that will not be unmarshaled, and all table-strong
1843 * marshals when they are no longer needed.
1846 * CoMarshalInterface(), CoUnmarshalInterface().
1848 HRESULT WINAPI CoReleaseMarshalData(IStream *pStream)
1853 TRACE("(%p)\n", pStream);
1855 hr = get_unmarshaler_from_stream(pStream, &pMarshal, NULL);
1859 /* call the helper object to do the releasing of marshal data */
1860 hr = IMarshal_ReleaseMarshalData(pMarshal, pStream);
1862 ERR("IMarshal::ReleaseMarshalData failed with error 0x%08x\n", hr);
1864 IMarshal_Release(pMarshal);
1869 /***********************************************************************
1870 * CoMarshalInterThreadInterfaceInStream [OLE32.@]
1872 * Marshal an interface across threads in the same process.
1875 * riid [I] Identifier of the interface to be marshalled.
1876 * pUnk [I] Pointer to IUnknown-derived interface that will be marshalled.
1877 * ppStm [O] Pointer to IStream object that is created and then used to store the marshalled interface.
1881 * Failure: E_OUTOFMEMORY and other COM error codes
1884 * CoMarshalInterface(), CoUnmarshalInterface() and CoGetInterfaceAndReleaseStream()
1886 HRESULT WINAPI CoMarshalInterThreadInterfaceInStream(
1887 REFIID riid, LPUNKNOWN pUnk, LPSTREAM * ppStm)
1889 ULARGE_INTEGER xpos;
1890 LARGE_INTEGER seekto;
1893 TRACE("(%s, %p, %p)\n",debugstr_guid(riid), pUnk, ppStm);
1895 hres = CreateStreamOnHGlobal(NULL, TRUE, ppStm);
1896 if (FAILED(hres)) return hres;
1897 hres = CoMarshalInterface(*ppStm, riid, pUnk, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1899 if (SUCCEEDED(hres))
1901 memset(&seekto, 0, sizeof(seekto));
1902 IStream_Seek(*ppStm, seekto, STREAM_SEEK_SET, &xpos);
1906 IStream_Release(*ppStm);
1913 /***********************************************************************
1914 * CoGetInterfaceAndReleaseStream [OLE32.@]
1916 * Unmarshalls an interface from a stream and then releases the stream.
1919 * pStm [I] Stream that contains the marshalled interface.
1920 * riid [I] Interface identifier of the object to unmarshall.
1921 * ppv [O] Address of pointer where the requested interface object will be stored.
1925 * Failure: A COM error code
1928 * CoMarshalInterThreadInterfaceInStream() and CoUnmarshalInterface()
1930 HRESULT WINAPI CoGetInterfaceAndReleaseStream(LPSTREAM pStm, REFIID riid,
1935 TRACE("(%p, %s, %p)\n", pStm, debugstr_guid(riid), ppv);
1937 if(!pStm) return E_INVALIDARG;
1938 hres = CoUnmarshalInterface(pStm, riid, ppv);
1939 IStream_Release(pStm);
1943 static HRESULT WINAPI StdMarshalCF_QueryInterface(LPCLASSFACTORY iface,
1944 REFIID riid, LPVOID *ppv)
1947 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory))
1949 *ppv = (LPVOID)iface;
1952 return E_NOINTERFACE;
1955 static ULONG WINAPI StdMarshalCF_AddRef(LPCLASSFACTORY iface)
1957 return 2; /* non-heap based object */
1960 static ULONG WINAPI StdMarshalCF_Release(LPCLASSFACTORY iface)
1962 return 1; /* non-heap based object */
1965 static HRESULT WINAPI StdMarshalCF_CreateInstance(LPCLASSFACTORY iface,
1966 LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv)
1968 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IMarshal))
1969 return StdMarshalImpl_Construct(riid, ppv);
1971 FIXME("(%s), not supported.\n",debugstr_guid(riid));
1972 return E_NOINTERFACE;
1975 static HRESULT WINAPI StdMarshalCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
1977 FIXME("(%d), stub!\n",fLock);
1981 static const IClassFactoryVtbl StdMarshalCFVtbl =
1983 StdMarshalCF_QueryInterface,
1984 StdMarshalCF_AddRef,
1985 StdMarshalCF_Release,
1986 StdMarshalCF_CreateInstance,
1987 StdMarshalCF_LockServer
1989 static const IClassFactoryVtbl *StdMarshalCF = &StdMarshalCFVtbl;
1991 HRESULT MARSHAL_GetStandardMarshalCF(LPVOID *ppv)
1993 *ppv = &StdMarshalCF;
1997 /***********************************************************************
1998 * CoMarshalHresult [OLE32.@]
2000 * Marshals an HRESULT value into a stream.
2003 * pStm [I] Stream that hresult will be marshalled into.
2004 * hresult [I] HRESULT to be marshalled.
2008 * Failure: A COM error code
2011 * CoUnmarshalHresult().
2013 HRESULT WINAPI CoMarshalHresult(LPSTREAM pStm, HRESULT hresult)
2015 return IStream_Write(pStm, &hresult, sizeof(hresult), NULL);
2018 /***********************************************************************
2019 * CoUnmarshalHresult [OLE32.@]
2021 * Unmarshals an HRESULT value from a stream.
2024 * pStm [I] Stream that hresult will be unmarshalled from.
2025 * phresult [I] Pointer to HRESULT where the value will be unmarshalled to.
2029 * Failure: A COM error code
2032 * CoMarshalHresult().
2034 HRESULT WINAPI CoUnmarshalHresult(LPSTREAM pStm, HRESULT * phresult)
2036 return IStream_Read(pStm, phresult, sizeof(*phresult), NULL);