ole32: Fix infinite recursion in CompositeMonikerImpl_BindToStorage.
[wine/wine64.git] / dlls / ole32 / marshal.c
blob976d9e70b3f9c7ae86c78447c56c029c800a9f63
1 /*
2 * Marshalling library
4 * Copyright 2002 Marcus Meissner
5 * Copyright 2004 Mike Hearn, for CodeWeavers
6 * Copyright 2004 Rob Shearman, for CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "config.h"
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <assert.h>
31 #define COBJMACROS
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winuser.h"
36 #include "objbase.h"
37 #include "ole2.h"
38 #include "rpc.h"
39 #include "winerror.h"
40 #include "winreg.h"
41 #include "wtypes.h"
42 #include "wine/unicode.h"
44 #include "compobj_private.h"
46 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(ole);
50 extern const CLSID CLSID_DfMarshal;
52 /* number of refs given out for normal marshaling */
53 #define NORMALEXTREFS 5
55 /* private flag indicating that the caller does not want to notify the stub
56 * when the proxy disconnects or is destroyed */
57 #define SORFP_NOLIFETIMEMGMT SORF_OXRES1
59 static HRESULT unmarshal_object(const STDOBJREF *stdobjref, APARTMENT *apt,
60 MSHCTX dest_context, void *dest_context_data,
61 REFIID riid, void **object);
63 /* Marshalling just passes a unique identifier to the remote client,
64 * that makes it possible to find the passed interface again.
66 * So basically we need a set of values that make it unique.
68 * Note that the IUnknown_QI(ob,xiid,&ppv) always returns the SAME ppv value!
70 * A triple is used: OXID (apt id), OID (stub manager id),
71 * IPID (interface ptr/stub id).
73 * OXIDs identify an apartment and are network scoped
74 * OIDs identify a stub manager and are apartment scoped
75 * IPIDs identify an interface stub and are apartment scoped
78 inline static HRESULT get_facbuf_for_iid(REFIID riid, IPSFactoryBuffer **facbuf)
80 HRESULT hr;
81 CLSID clsid;
83 if ((hr = CoGetPSClsid(riid, &clsid)))
84 return hr;
85 return CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL,
86 &IID_IPSFactoryBuffer, (LPVOID*)facbuf);
89 /* marshals an object into a STDOBJREF structure */
90 HRESULT marshal_object(APARTMENT *apt, STDOBJREF *stdobjref, REFIID riid, IUnknown *object, MSHLFLAGS mshlflags)
92 struct stub_manager *manager;
93 struct ifstub *ifstub;
94 BOOL tablemarshal;
95 IRpcStubBuffer *stub = NULL;
96 HRESULT hr;
97 IUnknown *iobject = NULL; /* object of type riid */
99 hr = apartment_getoxid(apt, &stdobjref->oxid);
100 if (hr != S_OK)
101 return hr;
103 hr = apartment_createwindowifneeded(apt);
104 if (hr != S_OK)
105 return hr;
107 hr = IUnknown_QueryInterface(object, riid, (void **)&iobject);
108 if (hr != S_OK)
110 ERR("object doesn't expose interface %s, failing with error 0x%08lx\n",
111 debugstr_guid(riid), hr);
112 return E_NOINTERFACE;
115 /* IUnknown doesn't require a stub buffer, because it never goes out on
116 * the wire */
117 if (!IsEqualIID(riid, &IID_IUnknown))
119 IPSFactoryBuffer *psfb;
121 hr = get_facbuf_for_iid(riid, &psfb);
122 if (hr != S_OK)
124 ERR("couldn't get IPSFactory buffer for interface %s\n", debugstr_guid(riid));
125 IUnknown_Release(iobject);
126 return hr;
129 hr = IPSFactoryBuffer_CreateStub(psfb, riid, iobject, &stub);
130 IPSFactoryBuffer_Release(psfb);
131 if (hr != S_OK)
133 ERR("Failed to create an IRpcStubBuffer from IPSFactory for %s\n", debugstr_guid(riid));
134 IUnknown_Release(iobject);
135 return hr;
139 if (mshlflags & MSHLFLAGS_NOPING)
140 stdobjref->flags = SORF_NOPING;
141 else
142 stdobjref->flags = SORF_NULL;
144 if ((manager = get_stub_manager_from_object(apt, object)))
145 TRACE("registering new ifstub on pre-existing manager\n");
146 else
148 TRACE("constructing new stub manager\n");
150 manager = new_stub_manager(apt, object);
151 if (!manager)
153 if (stub) IRpcStubBuffer_Release(stub);
154 IUnknown_Release(iobject);
155 return E_OUTOFMEMORY;
158 stdobjref->oid = manager->oid;
160 tablemarshal = ((mshlflags & MSHLFLAGS_TABLESTRONG) || (mshlflags & MSHLFLAGS_TABLEWEAK));
162 /* make sure ifstub that we are creating is unique */
163 ifstub = stub_manager_find_ifstub(manager, riid, mshlflags);
164 if (!ifstub)
166 ifstub = stub_manager_new_ifstub(manager, stub, iobject, riid, mshlflags);
167 IUnknown_Release(iobject);
168 if (stub) IRpcStubBuffer_Release(stub);
169 if (!ifstub)
171 stub_manager_int_release(manager);
172 /* FIXME: should we do another release to completely destroy the
173 * stub manager? */
174 return E_OUTOFMEMORY;
178 if (!tablemarshal)
180 stdobjref->cPublicRefs = NORMALEXTREFS;
181 stub_manager_ext_addref(manager, stdobjref->cPublicRefs);
183 else
185 stdobjref->cPublicRefs = 0;
186 if (mshlflags & MSHLFLAGS_TABLESTRONG)
187 stub_manager_ext_addref(manager, 1);
190 /* FIXME: check return value */
191 RPC_RegisterInterface(riid);
193 stdobjref->ipid = ifstub->ipid;
195 stub_manager_int_release(manager);
196 return S_OK;
201 /* Client-side identity of the server object */
203 static HRESULT proxy_manager_get_remunknown(struct proxy_manager * This, IRemUnknown **remunk);
204 static void proxy_manager_destroy(struct proxy_manager * This);
205 static HRESULT proxy_manager_find_ifproxy(struct proxy_manager * This, REFIID riid, struct ifproxy ** ifproxy_found);
206 static HRESULT proxy_manager_query_local_interface(struct proxy_manager * This, REFIID riid, void ** ppv);
208 static HRESULT WINAPI ClientIdentity_QueryInterface(IMultiQI * iface, REFIID riid, void ** ppv)
210 HRESULT hr;
211 MULTI_QI mqi;
213 TRACE("%s\n", debugstr_guid(riid));
215 mqi.pIID = riid;
216 hr = IMultiQI_QueryMultipleInterfaces(iface, 1, &mqi);
217 *ppv = (void *)mqi.pItf;
219 return hr;
222 static ULONG WINAPI ClientIdentity_AddRef(IMultiQI * iface)
224 struct proxy_manager * This = (struct proxy_manager *)iface;
225 TRACE("%p - before %ld\n", iface, This->refs);
226 return InterlockedIncrement(&This->refs);
229 static ULONG WINAPI ClientIdentity_Release(IMultiQI * iface)
231 struct proxy_manager * This = (struct proxy_manager *)iface;
232 ULONG refs = InterlockedDecrement(&This->refs);
233 TRACE("%p - after %ld\n", iface, refs);
234 if (!refs)
235 proxy_manager_destroy(This);
236 return refs;
239 static HRESULT WINAPI ClientIdentity_QueryMultipleInterfaces(IMultiQI *iface, ULONG cMQIs, MULTI_QI *pMQIs)
241 struct proxy_manager * This = (struct proxy_manager *)iface;
242 REMQIRESULT *qiresults = NULL;
243 ULONG nonlocal_mqis = 0;
244 ULONG i;
245 ULONG successful_mqis = 0;
246 IID *iids = HeapAlloc(GetProcessHeap(), 0, cMQIs * sizeof(*iids));
247 /* mapping of RemQueryInterface index to QueryMultipleInterfaces index */
248 ULONG *mapping = HeapAlloc(GetProcessHeap(), 0, cMQIs * sizeof(*mapping));
250 TRACE("cMQIs: %ld\n", cMQIs);
252 /* try to get a local interface - this includes already active proxy
253 * interfaces and also interfaces exposed by the proxy manager */
254 for (i = 0; i < cMQIs; i++)
256 TRACE("iid[%ld] = %s\n", i, debugstr_guid(pMQIs[i].pIID));
257 pMQIs[i].hr = proxy_manager_query_local_interface(This, pMQIs[i].pIID, (void **)&pMQIs[i].pItf);
258 if (pMQIs[i].hr == S_OK)
259 successful_mqis++;
260 else
262 iids[nonlocal_mqis] = *pMQIs[i].pIID;
263 mapping[nonlocal_mqis] = i;
264 nonlocal_mqis++;
268 TRACE("%ld interfaces not found locally\n", nonlocal_mqis);
270 /* if we have more than one interface not found locally then we must try
271 * to query the remote object for it */
272 if (nonlocal_mqis != 0)
274 IRemUnknown *remunk;
275 HRESULT hr;
276 IPID *ipid;
278 /* get the ipid of the first entry */
279 /* FIXME: should we implement ClientIdentity on the ifproxies instead
280 * of the proxy_manager so we use the correct ipid here? */
281 ipid = &LIST_ENTRY(list_head(&This->interfaces), struct ifproxy, entry)->stdobjref.ipid;
283 /* get IRemUnknown proxy so we can communicate with the remote object */
284 hr = proxy_manager_get_remunknown(This, &remunk);
286 if (hr == S_OK)
288 hr = IRemUnknown_RemQueryInterface(remunk, ipid, NORMALEXTREFS,
289 nonlocal_mqis, iids, &qiresults);
290 if (FAILED(hr))
291 ERR("IRemUnknown_RemQueryInterface failed with error 0x%08lx\n", hr);
294 /* IRemUnknown_RemQueryInterface can return S_FALSE if only some of
295 * the interfaces were returned */
296 if (SUCCEEDED(hr))
298 /* try to unmarshal each object returned to us */
299 for (i = 0; i < nonlocal_mqis; i++)
301 ULONG index = mapping[i];
302 HRESULT hrobj = qiresults[i].hResult;
303 if (hrobj == S_OK)
304 hrobj = unmarshal_object(&qiresults[i].std, This->parent,
305 This->dest_context,
306 This->dest_context_data,
307 pMQIs[index].pIID,
308 (void **)&pMQIs[index].pItf);
310 if (hrobj == S_OK)
311 successful_mqis++;
312 else
313 ERR("Failed to get pointer to interface %s\n", debugstr_guid(pMQIs[index].pIID));
314 pMQIs[index].hr = hrobj;
318 /* free the memory allocated by the proxy */
319 CoTaskMemFree(qiresults);
322 TRACE("%ld/%ld successfully queried\n", successful_mqis, cMQIs);
324 HeapFree(GetProcessHeap(), 0, iids);
325 HeapFree(GetProcessHeap(), 0, mapping);
327 if (successful_mqis == cMQIs)
328 return S_OK; /* we got all requested interfaces */
329 else if (successful_mqis == 0)
330 return E_NOINTERFACE; /* we didn't get any interfaces */
331 else
332 return S_FALSE; /* we got some interfaces */
335 static const IMultiQIVtbl ClientIdentity_Vtbl =
337 ClientIdentity_QueryInterface,
338 ClientIdentity_AddRef,
339 ClientIdentity_Release,
340 ClientIdentity_QueryMultipleInterfaces
343 static HRESULT WINAPI Proxy_QueryInterface(IMarshal *iface, REFIID riid, void **ppvObject)
345 ICOM_THIS_MULTI(struct proxy_manager, lpVtblMarshal, iface);
346 return IMultiQI_QueryInterface((IMultiQI *)&This->lpVtbl, riid, ppvObject);
349 static ULONG WINAPI Proxy_AddRef(IMarshal *iface)
351 ICOM_THIS_MULTI(struct proxy_manager, lpVtblMarshal, iface);
352 return IMultiQI_AddRef((IMultiQI *)&This->lpVtbl);
355 /* FIXME: remove these */
356 static HRESULT WINAPI StdMarshalImpl_GetUnmarshalClass(LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext, void* pvDestContext, DWORD mshlflags, CLSID* pCid);
357 static HRESULT WINAPI StdMarshalImpl_GetMarshalSizeMax(LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext, void* pvDestContext, DWORD mshlflags, DWORD* pSize);
358 static HRESULT WINAPI StdMarshalImpl_UnmarshalInterface(LPMARSHAL iface, IStream *pStm, REFIID riid, void **ppv);
359 static HRESULT WINAPI StdMarshalImpl_ReleaseMarshalData(LPMARSHAL iface, IStream *pStm);
360 static HRESULT WINAPI StdMarshalImpl_DisconnectObject(LPMARSHAL iface, DWORD dwReserved);
362 static ULONG WINAPI Proxy_Release(IMarshal *iface)
364 ICOM_THIS_MULTI(struct proxy_manager, lpVtblMarshal, iface);
365 return IMultiQI_Release((IMultiQI *)&This->lpVtbl);
368 static HRESULT WINAPI Proxy_MarshalInterface(
369 LPMARSHAL iface, IStream *pStm, REFIID riid, void* pv, DWORD dwDestContext,
370 void* pvDestContext, DWORD mshlflags)
372 ICOM_THIS_MULTI(struct proxy_manager, lpVtblMarshal, iface);
373 HRESULT hr;
374 struct ifproxy *ifproxy;
376 TRACE("(...,%s,...)\n", debugstr_guid(riid));
378 hr = proxy_manager_find_ifproxy(This, riid, &ifproxy);
379 if (SUCCEEDED(hr))
381 STDOBJREF stdobjref = ifproxy->stdobjref;
382 /* FIXME: optimization - share out proxy's public references if possible
383 * instead of making new proxy do a roundtrip through the server */
384 stdobjref.cPublicRefs = 0; /* InterlockedDecrement(&This->stdobjref.cPublicRefs) >= 0 ? 1 : 0 */
386 hr = IStream_Write(pStm, &stdobjref, sizeof(stdobjref), NULL);
388 else
390 /* we don't have the interface already unmarshaled so we have to
391 * request the object from the server */
392 IRemUnknown *remunk;
393 IPID *ipid;
394 REMQIRESULT *qiresults = NULL;
395 IID iid = *riid;
397 /* get the ipid of the first entry */
398 /* FIXME: should we implement ClientIdentity on the ifproxies instead
399 * of the proxy_manager so we use the correct ipid here? */
400 ipid = &LIST_ENTRY(list_head(&This->interfaces), struct ifproxy, entry)->stdobjref.ipid;
402 /* get IRemUnknown proxy so we can communicate with the remote object */
403 hr = proxy_manager_get_remunknown(This, &remunk);
405 if (hr == S_OK)
407 hr = IRemUnknown_RemQueryInterface(remunk, ipid, NORMALEXTREFS,
408 1, &iid, &qiresults);
409 if (SUCCEEDED(hr))
411 hr = IStream_Write(pStm, &qiresults->std, sizeof(qiresults->std), NULL);
412 if (FAILED(hr))
414 REMINTERFACEREF rif;
415 rif.ipid = qiresults->std.ipid;
416 rif.cPublicRefs = qiresults->std.cPublicRefs;
417 rif.cPrivateRefs = 0;
418 IRemUnknown_RemRelease(remunk, 1, &rif);
420 CoTaskMemFree(qiresults);
422 else
423 ERR("IRemUnknown_RemQueryInterface failed with error 0x%08lx\n", hr);
427 return hr;
430 static const IMarshalVtbl ProxyMarshal_Vtbl =
432 Proxy_QueryInterface,
433 Proxy_AddRef,
434 Proxy_Release,
435 StdMarshalImpl_GetUnmarshalClass,
436 StdMarshalImpl_GetMarshalSizeMax,
437 Proxy_MarshalInterface,
438 StdMarshalImpl_UnmarshalInterface,
439 StdMarshalImpl_ReleaseMarshalData,
440 StdMarshalImpl_DisconnectObject
443 static HRESULT ifproxy_get_public_ref(struct ifproxy * This)
445 HRESULT hr = S_OK;
447 if (WAIT_OBJECT_0 != WaitForSingleObject(This->parent->remoting_mutex, INFINITE))
449 ERR("Wait failed for ifproxy %p\n", This);
450 return E_UNEXPECTED;
453 if (This->refs == 0)
455 IRemUnknown *remunk = NULL;
457 TRACE("getting public ref for ifproxy %p\n", This);
459 hr = proxy_manager_get_remunknown(This->parent, &remunk);
460 if (hr == S_OK)
462 HRESULT hrref = S_OK;
463 REMINTERFACEREF rif;
464 rif.ipid = This->stdobjref.ipid;
465 rif.cPublicRefs = NORMALEXTREFS;
466 rif.cPrivateRefs = 0;
467 hr = IRemUnknown_RemAddRef(remunk, 1, &rif, &hrref);
468 if (hr == S_OK && hrref == S_OK)
469 This->refs += NORMALEXTREFS;
470 else
471 ERR("IRemUnknown_RemAddRef returned with 0x%08lx, hrref = 0x%08lx\n", hr, hrref);
474 ReleaseMutex(This->parent->remoting_mutex);
476 return hr;
479 static HRESULT ifproxy_release_public_refs(struct ifproxy * This)
481 HRESULT hr = S_OK;
483 if (WAIT_OBJECT_0 != WaitForSingleObject(This->parent->remoting_mutex, INFINITE))
485 ERR("Wait failed for ifproxy %p\n", This);
486 return E_UNEXPECTED;
489 if (This->refs > 0)
491 IRemUnknown *remunk = NULL;
493 TRACE("releasing %ld refs\n", This->refs);
495 hr = proxy_manager_get_remunknown(This->parent, &remunk);
496 if (hr == S_OK)
498 REMINTERFACEREF rif;
499 rif.ipid = This->stdobjref.ipid;
500 rif.cPublicRefs = This->refs;
501 rif.cPrivateRefs = 0;
502 hr = IRemUnknown_RemRelease(remunk, 1, &rif);
503 if (hr == S_OK)
504 This->refs = 0;
505 else if (hr == RPC_E_DISCONNECTED)
506 WARN("couldn't release references because object was "
507 "disconnected: oxid = %s, oid = %s\n",
508 wine_dbgstr_longlong(This->parent->oxid),
509 wine_dbgstr_longlong(This->parent->oid));
510 else
511 ERR("IRemUnknown_RemRelease failed with error 0x%08lx\n", hr);
514 ReleaseMutex(This->parent->remoting_mutex);
516 return hr;
519 /* should be called inside This->parent->cs critical section */
520 static void ifproxy_disconnect(struct ifproxy * This)
522 ifproxy_release_public_refs(This);
523 if (This->proxy) IRpcProxyBuffer_Disconnect(This->proxy);
525 IRpcChannelBuffer_Release(This->chan);
526 This->chan = NULL;
529 /* should be called in This->parent->cs critical section if it is an entry in parent's list */
530 static void ifproxy_destroy(struct ifproxy * This)
532 TRACE("%p\n", This);
534 /* release public references to this object so that the stub can know
535 * when to destroy itself */
536 ifproxy_release_public_refs(This);
538 list_remove(&This->entry);
540 if (This->chan)
542 IRpcChannelBuffer_Release(This->chan);
543 This->chan = NULL;
546 /* note: we don't call Release for This->proxy because its lifetime is
547 * controlled by the return value from ClientIdentity_Release, which this
548 * function is always called from */
550 HeapFree(GetProcessHeap(), 0, This);
553 static HRESULT proxy_manager_construct(
554 APARTMENT * apt, ULONG sorflags, OXID oxid, OID oid,
555 struct proxy_manager ** proxy_manager)
557 struct proxy_manager * This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
558 if (!This) return E_OUTOFMEMORY;
560 This->remoting_mutex = CreateMutexW(NULL, FALSE, NULL);
561 if (!This->remoting_mutex)
563 HeapFree(GetProcessHeap(), 0, This);
564 return HRESULT_FROM_WIN32(GetLastError());
567 This->lpVtbl = &ClientIdentity_Vtbl;
568 This->lpVtblMarshal = &ProxyMarshal_Vtbl;
570 list_init(&This->entry);
571 list_init(&This->interfaces);
573 InitializeCriticalSection(&This->cs);
574 DEBUG_SET_CRITSEC_NAME(&This->cs, "proxy_manager");
576 /* the apartment the object was unmarshaled into */
577 This->parent = apt;
579 /* the source apartment and id of the object */
580 This->oxid = oxid;
581 This->oid = oid;
583 This->refs = 1;
585 /* the DCOM draft specification states that the SORF_NOPING flag is
586 * proxy manager specific, not ifproxy specific, so this implies that we
587 * should store the STDOBJREF flags here in the proxy manager. */
588 This->sorflags = sorflags;
590 /* we create the IRemUnknown proxy on demand */
591 This->remunk = NULL;
593 /* initialise these values to the weakest values and they will be
594 * overwritten in proxy_manager_set_context */
595 This->dest_context = MSHCTX_INPROC;
596 This->dest_context_data = NULL;
598 EnterCriticalSection(&apt->cs);
599 /* FIXME: we are dependent on the ordering in here to make sure a proxy's
600 * IRemUnknown proxy doesn't get destroyed before the regual proxy does
601 * because we need the IRemUnknown proxy during the destruction of the
602 * regular proxy. Ideally, we should maintain a separate list for the
603 * IRemUnknown proxies that need late destruction */
604 list_add_tail(&apt->proxies, &This->entry);
605 LeaveCriticalSection(&apt->cs);
607 TRACE("%p created for OXID %s, OID %s\n", This,
608 wine_dbgstr_longlong(oxid), wine_dbgstr_longlong(oid));
610 *proxy_manager = This;
611 return S_OK;
614 static inline void proxy_manager_set_context(struct proxy_manager *This, MSHCTX dest_context, void *dest_context_data)
616 MSHCTX old_dest_context = This->dest_context;
617 MSHCTX new_dest_context;
621 new_dest_context = old_dest_context;
622 /* "stronger" values overwrite "weaker" values. stronger values are
623 * ones that disable more optimisations */
624 switch (old_dest_context)
626 case MSHCTX_INPROC:
627 new_dest_context = dest_context;
628 break;
629 case MSHCTX_CROSSCTX:
630 switch (dest_context)
632 case MSHCTX_INPROC:
633 break;
634 default:
635 new_dest_context = dest_context;
637 break;
638 case MSHCTX_LOCAL:
639 switch (dest_context)
641 case MSHCTX_INPROC:
642 case MSHCTX_CROSSCTX:
643 break;
644 default:
645 new_dest_context = dest_context;
647 break;
648 case MSHCTX_NOSHAREDMEM:
649 switch (dest_context)
651 case MSHCTX_DIFFERENTMACHINE:
652 new_dest_context = dest_context;
653 break;
654 default:
655 break;
657 break;
658 default:
659 break;
662 if (old_dest_context == new_dest_context) break;
664 old_dest_context = InterlockedCompareExchange((PLONG)&This->dest_context, new_dest_context, old_dest_context);
665 } while (new_dest_context != old_dest_context);
667 if (dest_context_data)
668 InterlockedExchangePointer(&This->dest_context_data, dest_context_data);
671 static HRESULT proxy_manager_query_local_interface(struct proxy_manager * This, REFIID riid, void ** ppv)
673 HRESULT hr;
674 struct ifproxy * ifproxy;
676 TRACE("%s\n", debugstr_guid(riid));
678 if (IsEqualIID(riid, &IID_IUnknown) ||
679 IsEqualIID(riid, &IID_IMultiQI))
681 *ppv = (void *)&This->lpVtbl;
682 IUnknown_AddRef((IUnknown *)*ppv);
683 return S_OK;
685 if (IsEqualIID(riid, &IID_IMarshal))
687 *ppv = (void *)&This->lpVtblMarshal;
688 IUnknown_AddRef((IUnknown *)*ppv);
689 return S_OK;
691 if (IsEqualIID(riid, &IID_IClientSecurity))
693 FIXME("requesting IClientSecurity, but it is unimplemented\n");
694 *ppv = NULL;
695 return E_NOINTERFACE;
698 hr = proxy_manager_find_ifproxy(This, riid, &ifproxy);
699 if (hr == S_OK)
701 *ppv = ifproxy->iface;
702 IUnknown_AddRef((IUnknown *)*ppv);
703 return S_OK;
706 *ppv = NULL;
707 return E_NOINTERFACE;
710 static HRESULT proxy_manager_create_ifproxy(
711 struct proxy_manager * This, const STDOBJREF *stdobjref, REFIID riid,
712 IRpcChannelBuffer * channel, struct ifproxy ** iif_out)
714 HRESULT hr;
715 IPSFactoryBuffer * psfb;
716 struct ifproxy * ifproxy = HeapAlloc(GetProcessHeap(), 0, sizeof(*ifproxy));
717 if (!ifproxy) return E_OUTOFMEMORY;
719 list_init(&ifproxy->entry);
721 ifproxy->parent = This;
722 ifproxy->stdobjref = *stdobjref;
723 ifproxy->iid = *riid;
724 ifproxy->refs = stdobjref->cPublicRefs;
725 ifproxy->proxy = NULL;
727 assert(channel);
728 ifproxy->chan = channel; /* FIXME: we should take the binding strings and construct the channel in this function */
730 /* the IUnknown interface is special because it does not have a
731 * proxy associated with the ifproxy as we handle IUnknown ourselves */
732 if (IsEqualIID(riid, &IID_IUnknown))
734 ifproxy->iface = (void *)&This->lpVtbl;
735 IMultiQI_AddRef((IMultiQI *)&This->lpVtbl);
736 hr = S_OK;
738 else
740 hr = get_facbuf_for_iid(riid, &psfb);
741 if (hr == S_OK)
743 /* important note: the outer unknown is set to the proxy manager.
744 * This ensures the COM identity rules are not violated, by having a
745 * one-to-one mapping of objects on the proxy side to objects on the
746 * stub side, no matter which interface you view the object through */
747 hr = IPSFactoryBuffer_CreateProxy(psfb, (IUnknown *)&This->lpVtbl, riid,
748 &ifproxy->proxy, &ifproxy->iface);
749 IPSFactoryBuffer_Release(psfb);
750 if (hr != S_OK)
751 ERR("Could not create proxy for interface %s, error 0x%08lx\n",
752 debugstr_guid(riid), hr);
754 else
755 ERR("Could not get IPSFactoryBuffer for interface %s, error 0x%08lx\n",
756 debugstr_guid(riid), hr);
758 if (hr == S_OK)
759 hr = IRpcProxyBuffer_Connect(ifproxy->proxy, ifproxy->chan);
762 /* get at least one external reference to the object to keep it alive */
763 if (hr == S_OK)
764 hr = ifproxy_get_public_ref(ifproxy);
766 if (hr == S_OK)
768 EnterCriticalSection(&This->cs);
769 list_add_tail(&This->interfaces, &ifproxy->entry);
770 LeaveCriticalSection(&This->cs);
772 *iif_out = ifproxy;
773 TRACE("ifproxy %p created for IPID %s, interface %s with %lu public refs\n",
774 ifproxy, debugstr_guid(&stdobjref->ipid), debugstr_guid(riid), stdobjref->cPublicRefs);
776 else
777 ifproxy_destroy(ifproxy);
779 return hr;
782 static HRESULT proxy_manager_find_ifproxy(struct proxy_manager * This, REFIID riid, struct ifproxy ** ifproxy_found)
784 HRESULT hr = E_NOINTERFACE; /* assume not found */
785 struct list * cursor;
787 EnterCriticalSection(&This->cs);
788 LIST_FOR_EACH(cursor, &This->interfaces)
790 struct ifproxy * ifproxy = LIST_ENTRY(cursor, struct ifproxy, entry);
791 if (IsEqualIID(riid, &ifproxy->iid))
793 *ifproxy_found = ifproxy;
794 hr = S_OK;
795 break;
798 LeaveCriticalSection(&This->cs);
800 return hr;
803 static void proxy_manager_disconnect(struct proxy_manager * This)
805 struct list * cursor;
807 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This->oxid),
808 wine_dbgstr_longlong(This->oid));
810 EnterCriticalSection(&This->cs);
812 /* SORFP_NOLIFTIMEMGMT proxies (for IRemUnknown) shouldn't be
813 * disconnected - it won't do anything anyway, except cause
814 * problems for other objects that depend on this proxy always
815 * working */
816 if (!(This->sorflags & SORFP_NOLIFETIMEMGMT))
818 LIST_FOR_EACH(cursor, &This->interfaces)
820 struct ifproxy * ifproxy = LIST_ENTRY(cursor, struct ifproxy, entry);
821 ifproxy_disconnect(ifproxy);
825 /* apartment is being destroyed so don't keep a pointer around to it */
826 This->parent = NULL;
828 LeaveCriticalSection(&This->cs);
831 static HRESULT proxy_manager_get_remunknown(struct proxy_manager * This, IRemUnknown **remunk)
833 HRESULT hr = S_OK;
835 /* we don't want to try and unmarshal or use IRemUnknown if we don't want
836 * lifetime management */
837 if (This->sorflags & SORFP_NOLIFETIMEMGMT)
838 return S_FALSE;
840 EnterCriticalSection(&This->cs);
841 if (This->remunk)
842 /* already created - return existing object */
843 *remunk = This->remunk;
844 else if (!This->parent)
845 /* disconnected - we can't create IRemUnknown */
846 hr = S_FALSE;
847 else
849 STDOBJREF stdobjref;
850 /* Don't want IRemUnknown lifetime management as this is IRemUnknown!
851 * We also don't care about whether or not the stub is still alive */
852 stdobjref.flags = SORFP_NOLIFETIMEMGMT | SORF_NOPING;
853 stdobjref.cPublicRefs = 1;
854 /* oxid of destination object */
855 stdobjref.oxid = This->oxid;
856 /* FIXME: what should be used for the oid? The DCOM draft doesn't say */
857 stdobjref.oid = (OID)-1;
858 /* FIXME: this is a hack around not having an OXID resolver yet -
859 * the OXID resolver should give us the IPID of the IRemUnknown
860 * interface */
861 stdobjref.ipid.Data1 = 0xffffffff;
862 stdobjref.ipid.Data2 = 0xffff;
863 stdobjref.ipid.Data3 = 0xffff;
864 assert(sizeof(stdobjref.ipid.Data4) == sizeof(stdobjref.oxid));
865 memcpy(&stdobjref.ipid.Data4, &stdobjref.oxid, sizeof(OXID));
867 /* do the unmarshal */
868 hr = unmarshal_object(&stdobjref, This->parent, This->dest_context,
869 This->dest_context_data, &IID_IRemUnknown,
870 (void**)&This->remunk);
871 if (hr == S_OK)
872 *remunk = This->remunk;
874 LeaveCriticalSection(&This->cs);
876 TRACE("got IRemUnknown* pointer %p, hr = 0x%08lx\n", *remunk, hr);
878 return hr;
881 /* destroys a proxy manager, freeing the memory it used.
882 * Note: this function should not be called from a list iteration in the
883 * apartment, due to the fact that it removes itself from the apartment and
884 * it could add a proxy to IRemUnknown into the apartment. */
885 static void proxy_manager_destroy(struct proxy_manager * This)
887 struct list * cursor;
889 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This->oxid),
890 wine_dbgstr_longlong(This->oid));
892 if (This->parent)
894 EnterCriticalSection(&This->parent->cs);
896 /* remove ourself from the list of proxy objects in the apartment */
897 LIST_FOR_EACH(cursor, &This->parent->proxies)
899 if (cursor == &This->entry)
901 list_remove(&This->entry);
902 break;
906 LeaveCriticalSection(&This->parent->cs);
909 /* destroy all of the interface proxies */
910 while ((cursor = list_head(&This->interfaces)))
912 struct ifproxy * ifproxy = LIST_ENTRY(cursor, struct ifproxy, entry);
913 ifproxy_destroy(ifproxy);
916 if (This->remunk) IRemUnknown_Release(This->remunk);
918 DEBUG_CLEAR_CRITSEC_NAME(&This->cs);
919 DeleteCriticalSection(&This->cs);
921 CloseHandle(This->remoting_mutex);
923 HeapFree(GetProcessHeap(), 0, This);
926 /* finds the proxy manager corresponding to a given OXID and OID that has
927 * been unmarshaled in the specified apartment. The caller must release the
928 * reference to the proxy_manager when the object is no longer used. */
929 static BOOL find_proxy_manager(APARTMENT * apt, OXID oxid, OID oid, struct proxy_manager ** proxy_found)
931 BOOL found = FALSE;
932 struct list * cursor;
934 EnterCriticalSection(&apt->cs);
935 LIST_FOR_EACH(cursor, &apt->proxies)
937 struct proxy_manager * proxy = LIST_ENTRY(cursor, struct proxy_manager, entry);
938 if ((oxid == proxy->oxid) && (oid == proxy->oid))
940 *proxy_found = proxy;
941 ClientIdentity_AddRef((IMultiQI *)&proxy->lpVtbl);
942 found = TRUE;
943 break;
946 LeaveCriticalSection(&apt->cs);
947 return found;
950 HRESULT apartment_disconnectproxies(struct apartment *apt)
952 struct list * cursor;
954 LIST_FOR_EACH(cursor, &apt->proxies)
956 struct proxy_manager * proxy = LIST_ENTRY(cursor, struct proxy_manager, entry);
957 proxy_manager_disconnect(proxy);
960 return S_OK;
963 /********************** StdMarshal implementation ****************************/
964 typedef struct _StdMarshalImpl
966 const IMarshalVtbl *lpvtbl;
967 LONG ref;
969 IID iid;
970 DWORD dwDestContext;
971 LPVOID pvDestContext;
972 DWORD mshlflags;
973 } StdMarshalImpl;
975 static HRESULT WINAPI
976 StdMarshalImpl_QueryInterface(LPMARSHAL iface, REFIID riid, LPVOID *ppv)
978 *ppv = NULL;
979 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IMarshal, riid))
981 *ppv = iface;
982 IUnknown_AddRef(iface);
983 return S_OK;
985 FIXME("No interface for %s.\n", debugstr_guid(riid));
986 return E_NOINTERFACE;
989 static ULONG WINAPI
990 StdMarshalImpl_AddRef(LPMARSHAL iface)
992 StdMarshalImpl *This = (StdMarshalImpl *)iface;
993 return InterlockedIncrement(&This->ref);
996 static ULONG WINAPI
997 StdMarshalImpl_Release(LPMARSHAL iface)
999 StdMarshalImpl *This = (StdMarshalImpl *)iface;
1000 ULONG ref = InterlockedDecrement(&This->ref);
1002 if (!ref) HeapFree(GetProcessHeap(),0,This);
1003 return ref;
1006 static HRESULT WINAPI
1007 StdMarshalImpl_GetUnmarshalClass(
1008 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
1009 void* pvDestContext, DWORD mshlflags, CLSID* pCid)
1011 *pCid = CLSID_DfMarshal;
1012 return S_OK;
1015 static HRESULT WINAPI
1016 StdMarshalImpl_GetMarshalSizeMax(
1017 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
1018 void* pvDestContext, DWORD mshlflags, DWORD* pSize)
1020 *pSize = sizeof(STDOBJREF);
1021 return S_OK;
1024 static HRESULT WINAPI
1025 StdMarshalImpl_MarshalInterface(
1026 LPMARSHAL iface, IStream *pStm,REFIID riid, void* pv, DWORD dwDestContext,
1027 void* pvDestContext, DWORD mshlflags)
1029 STDOBJREF stdobjref;
1030 ULONG res;
1031 HRESULT hres;
1032 APARTMENT *apt = COM_CurrentApt();
1034 TRACE("(...,%s,...)\n", debugstr_guid(riid));
1036 if (!apt)
1038 ERR("Apartment not initialized\n");
1039 return CO_E_NOTINITIALIZED;
1042 /* make sure this apartment can be reached from other threads / processes */
1043 RPC_StartRemoting(apt);
1045 hres = marshal_object(apt, &stdobjref, riid, (IUnknown *)pv, mshlflags);
1046 if (hres)
1048 ERR("Failed to create ifstub, hres=0x%lx\n", hres);
1049 return hres;
1052 hres = IStream_Write(pStm, &stdobjref, sizeof(stdobjref), &res);
1053 if (hres) return hres;
1055 return S_OK;
1058 /* helper for StdMarshalImpl_UnmarshalInterface - does the unmarshaling with
1059 * no questions asked about the rules surrounding same-apartment unmarshals
1060 * and table marshaling */
1061 static HRESULT unmarshal_object(const STDOBJREF *stdobjref, APARTMENT *apt,
1062 MSHCTX dest_context, void *dest_context_data,
1063 REFIID riid, void **object)
1065 struct proxy_manager *proxy_manager = NULL;
1066 HRESULT hr = S_OK;
1068 assert(apt);
1070 TRACE("stdobjref:\n\tflags = %04lx\n\tcPublicRefs = %ld\n\toxid = %s\n\toid = %s\n\tipid = %s\n",
1071 stdobjref->flags, stdobjref->cPublicRefs,
1072 wine_dbgstr_longlong(stdobjref->oxid),
1073 wine_dbgstr_longlong(stdobjref->oid),
1074 debugstr_guid(&stdobjref->ipid));
1076 /* create an a new proxy manager if one doesn't already exist for the
1077 * object */
1078 if (!find_proxy_manager(apt, stdobjref->oxid, stdobjref->oid, &proxy_manager))
1080 hr = proxy_manager_construct(apt, stdobjref->flags,
1081 stdobjref->oxid, stdobjref->oid,
1082 &proxy_manager);
1084 else
1085 TRACE("proxy manager already created, using\n");
1087 if (hr == S_OK)
1089 struct ifproxy * ifproxy;
1091 proxy_manager_set_context(proxy_manager, dest_context, dest_context_data);
1093 hr = proxy_manager_find_ifproxy(proxy_manager, riid, &ifproxy);
1094 if (hr == E_NOINTERFACE)
1096 IRpcChannelBuffer *chanbuf;
1097 hr = RPC_CreateClientChannel(&stdobjref->oxid, &stdobjref->ipid,
1098 proxy_manager->dest_context,
1099 proxy_manager->dest_context_data,
1100 &chanbuf);
1101 if (hr == S_OK)
1102 hr = proxy_manager_create_ifproxy(proxy_manager, stdobjref,
1103 riid, chanbuf, &ifproxy);
1105 else
1106 IUnknown_AddRef((IUnknown *)ifproxy->iface);
1108 if (hr == S_OK)
1109 *object = ifproxy->iface;
1112 /* release our reference to the proxy manager - the client/apartment
1113 * will hold on to the remaining reference for us */
1114 if (proxy_manager) ClientIdentity_Release((IMultiQI*)&proxy_manager->lpVtbl);
1116 return hr;
1119 static HRESULT WINAPI
1120 StdMarshalImpl_UnmarshalInterface(LPMARSHAL iface, IStream *pStm, REFIID riid, void **ppv)
1122 StdMarshalImpl *This = (StdMarshalImpl *)iface;
1123 struct stub_manager *stubmgr;
1124 STDOBJREF stdobjref;
1125 ULONG res;
1126 HRESULT hres;
1127 APARTMENT *apt = COM_CurrentApt();
1128 APARTMENT *stub_apt;
1129 OXID oxid;
1131 TRACE("(...,%s,....)\n", debugstr_guid(riid));
1133 /* we need an apartment to unmarshal into */
1134 if (!apt)
1136 ERR("Apartment not initialized\n");
1137 return CO_E_NOTINITIALIZED;
1140 /* read STDOBJREF from wire */
1141 hres = IStream_Read(pStm, &stdobjref, sizeof(stdobjref), &res);
1142 if (hres) return STG_E_READFAULT;
1144 hres = apartment_getoxid(apt, &oxid);
1145 if (hres) return hres;
1147 /* check if we're marshalling back to ourselves */
1148 if ((oxid == stdobjref.oxid) && (stubmgr = get_stub_manager(apt, stdobjref.oid)))
1150 TRACE("Unmarshalling object marshalled in same apartment for iid %s, "
1151 "returning original object %p\n", debugstr_guid(riid), stubmgr->object);
1153 hres = IUnknown_QueryInterface(stubmgr->object, riid, ppv);
1155 /* unref the ifstub. FIXME: only do this on success? */
1156 if (!stub_manager_is_table_marshaled(stubmgr, &stdobjref.ipid))
1157 stub_manager_ext_release(stubmgr, stdobjref.cPublicRefs);
1159 stub_manager_int_release(stubmgr);
1160 return hres;
1163 /* notify stub manager about unmarshal if process-local object.
1164 * note: if the oxid is not found then we and native will quite happily
1165 * ignore table marshaling and normal marshaling rules regarding number of
1166 * unmarshals, etc, but if you abuse these rules then your proxy could end
1167 * up returning RPC_E_DISCONNECTED. */
1168 if ((stub_apt = apartment_findfromoxid(stdobjref.oxid, TRUE)))
1170 if ((stubmgr = get_stub_manager(stub_apt, stdobjref.oid)))
1172 if (!stub_manager_notify_unmarshal(stubmgr, &stdobjref.ipid))
1173 hres = CO_E_OBJNOTCONNECTED;
1175 stub_manager_int_release(stubmgr);
1177 else
1179 WARN("Couldn't find object for OXID %s, OID %s, assuming disconnected\n",
1180 wine_dbgstr_longlong(stdobjref.oxid),
1181 wine_dbgstr_longlong(stdobjref.oid));
1182 hres = CO_E_OBJNOTCONNECTED;
1185 apartment_release(stub_apt);
1187 else
1188 TRACE("Treating unmarshal from OXID %s as inter-process\n",
1189 wine_dbgstr_longlong(stdobjref.oxid));
1191 if (hres == S_OK)
1192 hres = unmarshal_object(&stdobjref, apt, This->dwDestContext,
1193 This->pvDestContext, riid, ppv);
1195 if (hres) WARN("Failed with error 0x%08lx\n", hres);
1196 else TRACE("Successfully created proxy %p\n", *ppv);
1198 return hres;
1201 static HRESULT WINAPI
1202 StdMarshalImpl_ReleaseMarshalData(LPMARSHAL iface, IStream *pStm)
1204 STDOBJREF stdobjref;
1205 ULONG res;
1206 HRESULT hres;
1207 struct stub_manager *stubmgr;
1208 APARTMENT *apt;
1210 TRACE("iface=%p, pStm=%p\n", iface, pStm);
1212 hres = IStream_Read(pStm, &stdobjref, sizeof(stdobjref), &res);
1213 if (hres) return STG_E_READFAULT;
1215 TRACE("oxid = %s, oid = %s, ipid = %s\n",
1216 wine_dbgstr_longlong(stdobjref.oxid),
1217 wine_dbgstr_longlong(stdobjref.oid),
1218 wine_dbgstr_guid(&stdobjref.ipid));
1220 if (!(apt = apartment_findfromoxid(stdobjref.oxid, TRUE)))
1222 WARN("Could not map OXID %s to apartment object\n",
1223 wine_dbgstr_longlong(stdobjref.oxid));
1224 return RPC_E_INVALID_OBJREF;
1227 if (!(stubmgr = get_stub_manager(apt, stdobjref.oid)))
1229 ERR("could not map object ID to stub manager, oxid=%s, oid=%s\n",
1230 wine_dbgstr_longlong(stdobjref.oxid), wine_dbgstr_longlong(stdobjref.oid));
1231 return RPC_E_INVALID_OBJREF;
1234 stub_manager_release_marshal_data(stubmgr, stdobjref.cPublicRefs, &stdobjref.ipid);
1236 stub_manager_int_release(stubmgr);
1237 apartment_release(apt);
1239 return S_OK;
1242 static HRESULT WINAPI
1243 StdMarshalImpl_DisconnectObject(LPMARSHAL iface, DWORD dwReserved)
1245 FIXME("(), stub!\n");
1246 return S_OK;
1249 static const IMarshalVtbl VT_StdMarshal =
1251 StdMarshalImpl_QueryInterface,
1252 StdMarshalImpl_AddRef,
1253 StdMarshalImpl_Release,
1254 StdMarshalImpl_GetUnmarshalClass,
1255 StdMarshalImpl_GetMarshalSizeMax,
1256 StdMarshalImpl_MarshalInterface,
1257 StdMarshalImpl_UnmarshalInterface,
1258 StdMarshalImpl_ReleaseMarshalData,
1259 StdMarshalImpl_DisconnectObject
1262 static HRESULT StdMarshalImpl_Construct(REFIID riid, void** ppvObject)
1264 StdMarshalImpl * pStdMarshal =
1265 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(StdMarshalImpl));
1266 if (!pStdMarshal)
1267 return E_OUTOFMEMORY;
1268 pStdMarshal->lpvtbl = &VT_StdMarshal;
1269 pStdMarshal->ref = 0;
1270 return IMarshal_QueryInterface((IMarshal*)pStdMarshal, riid, ppvObject);
1273 /***********************************************************************
1274 * CoGetStandardMarshal [OLE32.@]
1276 * Gets or creates a standard marshal object.
1278 * PARAMS
1279 * riid [I] Interface identifier of the pUnk object.
1280 * pUnk [I] Optional. Object to get the marshal object for.
1281 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1282 * pvDestContext [I] Reserved. Must be NULL.
1283 * mshlflags [I] Flags affecting the marshaling process.
1284 * ppMarshal [O] Address where marshal object will be stored.
1286 * RETURNS
1287 * Success: S_OK.
1288 * Failure: HRESULT code.
1290 * NOTES
1292 * The function retrieves the IMarshal object associated with an object if
1293 * that object is currently an active stub, otherwise a new marshal object is
1294 * created.
1296 HRESULT WINAPI CoGetStandardMarshal(REFIID riid, IUnknown *pUnk,
1297 DWORD dwDestContext, LPVOID pvDestContext,
1298 DWORD mshlflags, LPMARSHAL *ppMarshal)
1300 StdMarshalImpl *dm;
1302 if (pUnk == NULL)
1304 FIXME("(%s,NULL,%lx,%p,%lx,%p), unimplemented yet.\n",
1305 debugstr_guid(riid),dwDestContext,pvDestContext,mshlflags,ppMarshal);
1306 return E_NOTIMPL;
1308 TRACE("(%s,%p,%lx,%p,%lx,%p)\n",
1309 debugstr_guid(riid),pUnk,dwDestContext,pvDestContext,mshlflags,ppMarshal);
1310 *ppMarshal = HeapAlloc(GetProcessHeap(),0,sizeof(StdMarshalImpl));
1311 dm = (StdMarshalImpl*) *ppMarshal;
1312 if (!dm) return E_FAIL;
1313 dm->lpvtbl = &VT_StdMarshal;
1314 dm->ref = 1;
1316 dm->iid = *riid;
1317 dm->dwDestContext = dwDestContext;
1318 dm->pvDestContext = pvDestContext;
1319 dm->mshlflags = mshlflags;
1320 return S_OK;
1323 /***********************************************************************
1324 * get_marshaler [internal]
1326 * Retrieves an IMarshal interface for an object.
1328 static HRESULT get_marshaler(REFIID riid, IUnknown *pUnk, DWORD dwDestContext,
1329 void *pvDestContext, DWORD mshlFlags,
1330 LPMARSHAL *pMarshal)
1332 HRESULT hr;
1334 if (!pUnk)
1335 return E_POINTER;
1336 hr = IUnknown_QueryInterface(pUnk, &IID_IMarshal, (LPVOID*)pMarshal);
1337 if (hr)
1338 hr = CoGetStandardMarshal(riid, pUnk, dwDestContext, pvDestContext,
1339 mshlFlags, pMarshal);
1340 return hr;
1343 /***********************************************************************
1344 * get_unmarshaler_from_stream [internal]
1346 * Creates an IMarshal* object according to the data marshaled to the stream.
1347 * The function leaves the stream pointer at the start of the data written
1348 * to the stream by the IMarshal* object.
1350 static HRESULT get_unmarshaler_from_stream(IStream *stream, IMarshal **marshal, IID *iid)
1352 HRESULT hr;
1353 ULONG res;
1354 OBJREF objref;
1356 /* read common OBJREF header */
1357 hr = IStream_Read(stream, &objref, FIELD_OFFSET(OBJREF, u_objref), &res);
1358 if (hr || (res != FIELD_OFFSET(OBJREF, u_objref)))
1360 ERR("Failed to read common OBJREF header, 0x%08lx\n", hr);
1361 return STG_E_READFAULT;
1364 /* sanity check on header */
1365 if (objref.signature != OBJREF_SIGNATURE)
1367 ERR("Bad OBJREF signature 0x%08lx\n", objref.signature);
1368 return RPC_E_INVALID_OBJREF;
1371 if (iid) *iid = objref.iid;
1373 /* FIXME: handler marshaling */
1374 if (objref.flags & OBJREF_STANDARD)
1376 TRACE("Using standard unmarshaling\n");
1377 hr = StdMarshalImpl_Construct(&IID_IMarshal, (LPVOID*)marshal);
1379 else if (objref.flags & OBJREF_CUSTOM)
1381 ULONG custom_header_size = FIELD_OFFSET(OBJREF, u_objref.u_custom.pData) -
1382 FIELD_OFFSET(OBJREF, u_objref.u_custom);
1383 TRACE("Using custom unmarshaling\n");
1384 /* read constant sized OR_CUSTOM data from stream */
1385 hr = IStream_Read(stream, &objref.u_objref.u_custom,
1386 custom_header_size, &res);
1387 if (hr || (res != custom_header_size))
1389 ERR("Failed to read OR_CUSTOM header, 0x%08lx\n", hr);
1390 return STG_E_READFAULT;
1392 /* now create the marshaler specified in the stream */
1393 hr = CoCreateInstance(&objref.u_objref.u_custom.clsid, NULL,
1394 CLSCTX_INPROC_SERVER, &IID_IMarshal,
1395 (LPVOID*)marshal);
1397 else
1399 FIXME("Invalid or unimplemented marshaling type specified: %lx\n",
1400 objref.flags);
1401 return RPC_E_INVALID_OBJREF;
1404 if (hr)
1405 ERR("Failed to create marshal, 0x%08lx\n", hr);
1407 return hr;
1410 /***********************************************************************
1411 * CoGetMarshalSizeMax [OLE32.@]
1413 * Gets the maximum amount of data that will be needed by a marshal.
1415 * PARAMS
1416 * pulSize [O] Address where maximum marshal size will be stored.
1417 * riid [I] Identifier of the interface to marshal.
1418 * pUnk [I] Pointer to the object to marshal.
1419 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1420 * pvDestContext [I] Reserved. Must be NULL.
1421 * mshlFlags [I] Flags that affect the marshaling. See CoMarshalInterface().
1423 * RETURNS
1424 * Success: S_OK.
1425 * Failure: HRESULT code.
1427 * SEE ALSO
1428 * CoMarshalInterface().
1430 HRESULT WINAPI CoGetMarshalSizeMax(ULONG *pulSize, REFIID riid, IUnknown *pUnk,
1431 DWORD dwDestContext, void *pvDestContext,
1432 DWORD mshlFlags)
1434 HRESULT hr;
1435 LPMARSHAL pMarshal;
1436 CLSID marshaler_clsid;
1438 hr = get_marshaler(riid, pUnk, dwDestContext, pvDestContext, mshlFlags, &pMarshal);
1439 if (hr)
1440 return hr;
1442 hr = IMarshal_GetUnmarshalClass(pMarshal, riid, pUnk, dwDestContext,
1443 pvDestContext, mshlFlags, &marshaler_clsid);
1444 if (hr)
1446 ERR("IMarshal::GetUnmarshalClass failed, 0x%08lx\n", hr);
1447 IMarshal_Release(pMarshal);
1448 return hr;
1451 hr = IMarshal_GetMarshalSizeMax(pMarshal, riid, pUnk, dwDestContext,
1452 pvDestContext, mshlFlags, pulSize);
1453 if (IsEqualCLSID(&marshaler_clsid, &CLSID_DfMarshal))
1454 /* add on the size of the common header */
1455 *pulSize += FIELD_OFFSET(OBJREF, u_objref);
1456 else
1457 /* custom marshaling: add on the size of the whole OBJREF structure
1458 * like native does */
1459 *pulSize += sizeof(OBJREF);
1461 IMarshal_Release(pMarshal);
1462 return hr;
1466 static void dump_MSHLFLAGS(MSHLFLAGS flags)
1468 if (flags & MSHLFLAGS_TABLESTRONG)
1469 TRACE(" MSHLFLAGS_TABLESTRONG");
1470 if (flags & MSHLFLAGS_TABLEWEAK)
1471 TRACE(" MSHLFLAGS_TABLEWEAK");
1472 if (!(flags & (MSHLFLAGS_TABLESTRONG|MSHLFLAGS_TABLEWEAK)))
1473 TRACE(" MSHLFLAGS_NORMAL");
1474 if (flags & MSHLFLAGS_NOPING)
1475 TRACE(" MSHLFLAGS_NOPING");
1478 /***********************************************************************
1479 * CoMarshalInterface [OLE32.@]
1481 * Marshals an interface into a stream so that the object can then be
1482 * unmarshaled from another COM apartment and used remotely.
1484 * PARAMS
1485 * pStream [I] Stream the object will be marshaled into.
1486 * riid [I] Identifier of the interface to marshal.
1487 * pUnk [I] Pointer to the object to marshal.
1488 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1489 * pvDestContext [I] Reserved. Must be NULL.
1490 * mshlFlags [I] Flags that affect the marshaling. See notes.
1492 * RETURNS
1493 * Success: S_OK.
1494 * Failure: HRESULT code.
1496 * NOTES
1498 * The mshlFlags parameter can take one or more of the following flags:
1499 *| MSHLFLAGS_NORMAL - Unmarshal once, releases stub on last proxy release.
1500 *| MSHLFLAGS_TABLESTRONG - Unmarshal many, release when CoReleaseMarshalData() called.
1501 *| MSHLFLAGS_TABLEWEAK - Unmarshal many, releases stub on last proxy release.
1502 *| MSHLFLAGS_NOPING - No automatic garbage collection (and so reduces network traffic).
1504 * If a marshaled object is not unmarshaled, then CoReleaseMarshalData() must
1505 * be called in order to release the resources used in the marshaling.
1507 * SEE ALSO
1508 * CoUnmarshalInterface(), CoReleaseMarshalData().
1510 HRESULT WINAPI CoMarshalInterface(IStream *pStream, REFIID riid, IUnknown *pUnk,
1511 DWORD dwDestContext, void *pvDestContext,
1512 DWORD mshlFlags)
1514 HRESULT hr;
1515 CLSID marshaler_clsid;
1516 OBJREF objref;
1517 LPMARSHAL pMarshal;
1519 TRACE("(%p, %s, %p, %lx, %p,", pStream, debugstr_guid(riid), pUnk,
1520 dwDestContext, pvDestContext);
1521 dump_MSHLFLAGS(mshlFlags);
1522 TRACE(")\n");
1524 if (pUnk == NULL)
1525 return E_INVALIDARG;
1527 objref.signature = OBJREF_SIGNATURE;
1528 objref.iid = *riid;
1530 /* get the marshaler for the specified interface */
1531 hr = get_marshaler(riid, pUnk, dwDestContext, pvDestContext, mshlFlags, &pMarshal);
1532 if (hr)
1534 ERR("Failed to get marshaller, 0x%08lx\n", hr);
1535 return hr;
1538 hr = IMarshal_GetUnmarshalClass(pMarshal, riid, pUnk, dwDestContext,
1539 pvDestContext, mshlFlags, &marshaler_clsid);
1540 if (hr)
1542 ERR("IMarshal::GetUnmarshalClass failed, 0x%08lx\n", hr);
1543 goto cleanup;
1546 /* FIXME: implement handler marshaling too */
1547 if (IsEqualCLSID(&marshaler_clsid, &CLSID_DfMarshal))
1549 TRACE("Using standard marshaling\n");
1550 objref.flags = OBJREF_STANDARD;
1552 /* write the common OBJREF header to the stream */
1553 hr = IStream_Write(pStream, &objref, FIELD_OFFSET(OBJREF, u_objref), NULL);
1554 if (hr)
1556 ERR("Failed to write OBJREF header to stream, 0x%08lx\n", hr);
1557 goto cleanup;
1560 else
1562 TRACE("Using custom marshaling\n");
1563 objref.flags = OBJREF_CUSTOM;
1564 objref.u_objref.u_custom.clsid = marshaler_clsid;
1565 objref.u_objref.u_custom.cbExtension = 0;
1566 objref.u_objref.u_custom.size = 0;
1567 hr = IMarshal_GetMarshalSizeMax(pMarshal, riid, pUnk, dwDestContext,
1568 pvDestContext, mshlFlags,
1569 &objref.u_objref.u_custom.size);
1570 if (hr)
1572 ERR("Failed to get max size of marshal data, error 0x%08lx\n", hr);
1573 goto cleanup;
1575 /* write constant sized common header and OR_CUSTOM data into stream */
1576 hr = IStream_Write(pStream, &objref,
1577 FIELD_OFFSET(OBJREF, u_objref.u_custom.pData), NULL);
1578 if (hr)
1580 ERR("Failed to write OR_CUSTOM header to stream with 0x%08lx\n", hr);
1581 goto cleanup;
1585 TRACE("Calling IMarshal::MarshalInterace\n");
1586 /* call helper object to do the actual marshaling */
1587 hr = IMarshal_MarshalInterface(pMarshal, pStream, riid, pUnk, dwDestContext,
1588 pvDestContext, mshlFlags);
1590 if (hr)
1592 ERR("Failed to marshal the interface %s, %lx\n", debugstr_guid(riid), hr);
1593 goto cleanup;
1596 cleanup:
1597 IMarshal_Release(pMarshal);
1599 TRACE("completed with hr 0x%08lx\n", hr);
1601 return hr;
1604 /***********************************************************************
1605 * CoUnmarshalInterface [OLE32.@]
1607 * Unmarshals an object from a stream by creating a proxy to the remote
1608 * object, if necessary.
1610 * PARAMS
1612 * pStream [I] Stream containing the marshaled object.
1613 * riid [I] Interface identifier of the object to create a proxy to.
1614 * ppv [O] Address where proxy will be stored.
1616 * RETURNS
1618 * Success: S_OK.
1619 * Failure: HRESULT code.
1621 * SEE ALSO
1622 * CoMarshalInterface().
1624 HRESULT WINAPI CoUnmarshalInterface(IStream *pStream, REFIID riid, LPVOID *ppv)
1626 HRESULT hr;
1627 LPMARSHAL pMarshal;
1628 IID iid;
1629 IUnknown *object;
1631 TRACE("(%p, %s, %p)\n", pStream, debugstr_guid(riid), ppv);
1633 hr = get_unmarshaler_from_stream(pStream, &pMarshal, &iid);
1634 if (hr != S_OK)
1635 return hr;
1637 /* call the helper object to do the actual unmarshaling */
1638 hr = IMarshal_UnmarshalInterface(pMarshal, pStream, &iid, (LPVOID*)&object);
1639 if (hr)
1640 ERR("IMarshal::UnmarshalInterface failed, 0x%08lx\n", hr);
1642 /* IID_NULL means use the interface ID of the marshaled object */
1643 if (!IsEqualIID(riid, &IID_NULL))
1644 iid = *riid;
1646 if (hr == S_OK)
1648 if (!IsEqualIID(riid, &iid))
1650 TRACE("requested interface != marshalled interface, additional QI needed\n");
1651 hr = IUnknown_QueryInterface(object, &iid, ppv);
1652 if (hr)
1653 ERR("Couldn't query for interface %s, hr = 0x%08lx\n",
1654 debugstr_guid(riid), hr);
1655 IUnknown_Release(object);
1657 else
1659 *ppv = object;
1663 IMarshal_Release(pMarshal);
1665 TRACE("completed with hr 0x%lx\n", hr);
1667 return hr;
1670 /***********************************************************************
1671 * CoReleaseMarshalData [OLE32.@]
1673 * Releases resources associated with an object that has been marshaled into
1674 * a stream.
1676 * PARAMS
1678 * pStream [I] The stream that the object has been marshaled into.
1680 * RETURNS
1681 * Success: S_OK.
1682 * Failure: HRESULT error code.
1684 * NOTES
1686 * Call this function to release resources associated with a normal or
1687 * table-weak marshal that will not be unmarshaled, and all table-strong
1688 * marshals when they are no longer needed.
1690 * SEE ALSO
1691 * CoMarshalInterface(), CoUnmarshalInterface().
1693 HRESULT WINAPI CoReleaseMarshalData(IStream *pStream)
1695 HRESULT hr;
1696 LPMARSHAL pMarshal;
1698 TRACE("(%p)\n", pStream);
1700 hr = get_unmarshaler_from_stream(pStream, &pMarshal, NULL);
1701 if (hr != S_OK)
1702 return hr;
1704 /* call the helper object to do the releasing of marshal data */
1705 hr = IMarshal_ReleaseMarshalData(pMarshal, pStream);
1706 if (hr)
1707 ERR("IMarshal::ReleaseMarshalData failed with error 0x%08lx\n", hr);
1709 IMarshal_Release(pMarshal);
1710 return hr;
1714 /***********************************************************************
1715 * CoMarshalInterThreadInterfaceInStream [OLE32.@]
1717 * Marshal an interface across threads in the same process.
1719 * PARAMS
1720 * riid [I] Identifier of the interface to be marshalled.
1721 * pUnk [I] Pointer to IUnknown-derived interface that will be marshalled.
1722 * ppStm [O] Pointer to IStream object that is created and then used to store the marshalled inteface.
1724 * RETURNS
1725 * Success: S_OK
1726 * Failure: E_OUTOFMEMORY and other COM error codes
1728 * SEE ALSO
1729 * CoMarshalInterface(), CoUnmarshalInterface() and CoGetInterfaceAndReleaseStream()
1731 HRESULT WINAPI CoMarshalInterThreadInterfaceInStream(
1732 REFIID riid, LPUNKNOWN pUnk, LPSTREAM * ppStm)
1734 ULARGE_INTEGER xpos;
1735 LARGE_INTEGER seekto;
1736 HRESULT hres;
1738 TRACE("(%s, %p, %p)\n",debugstr_guid(riid), pUnk, ppStm);
1740 hres = CreateStreamOnHGlobal(NULL, TRUE, ppStm);
1741 if (FAILED(hres)) return hres;
1742 hres = CoMarshalInterface(*ppStm, riid, pUnk, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1744 if (SUCCEEDED(hres))
1746 memset(&seekto, 0, sizeof(seekto));
1747 IStream_Seek(*ppStm, seekto, SEEK_SET, &xpos);
1749 else
1751 IStream_Release(*ppStm);
1752 *ppStm = NULL;
1755 return hres;
1758 /***********************************************************************
1759 * CoGetInterfaceAndReleaseStream [OLE32.@]
1761 * Unmarshalls an inteface from a stream and then releases the stream.
1763 * PARAMS
1764 * pStm [I] Stream that contains the marshalled inteface.
1765 * riid [I] Interface identifier of the object to unmarshall.
1766 * ppv [O] Address of pointer where the requested interface object will be stored.
1768 * RETURNS
1769 * Success: S_OK
1770 * Failure: A COM error code
1772 * SEE ALSO
1773 * CoMarshalInterThreadInterfaceInStream() and CoUnmarshalInteface()
1775 HRESULT WINAPI CoGetInterfaceAndReleaseStream(LPSTREAM pStm, REFIID riid,
1776 LPVOID *ppv)
1778 HRESULT hres;
1780 TRACE("(%p, %s, %p)\n", pStm, debugstr_guid(riid), ppv);
1782 hres = CoUnmarshalInterface(pStm, riid, ppv);
1783 IStream_Release(pStm);
1784 return hres;
1787 static HRESULT WINAPI StdMarshalCF_QueryInterface(LPCLASSFACTORY iface,
1788 REFIID riid, LPVOID *ppv)
1790 *ppv = NULL;
1791 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory))
1793 *ppv = (LPVOID)iface;
1794 return S_OK;
1796 return E_NOINTERFACE;
1799 static ULONG WINAPI StdMarshalCF_AddRef(LPCLASSFACTORY iface)
1801 return 2; /* non-heap based object */
1804 static ULONG WINAPI StdMarshalCF_Release(LPCLASSFACTORY iface)
1806 return 1; /* non-heap based object */
1809 static HRESULT WINAPI StdMarshalCF_CreateInstance(LPCLASSFACTORY iface,
1810 LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv)
1812 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IMarshal))
1813 return StdMarshalImpl_Construct(riid, ppv);
1815 FIXME("(%s), not supported.\n",debugstr_guid(riid));
1816 return E_NOINTERFACE;
1819 static HRESULT WINAPI StdMarshalCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
1821 FIXME("(%d), stub!\n",fLock);
1822 return S_OK;
1825 static const IClassFactoryVtbl StdMarshalCFVtbl =
1827 StdMarshalCF_QueryInterface,
1828 StdMarshalCF_AddRef,
1829 StdMarshalCF_Release,
1830 StdMarshalCF_CreateInstance,
1831 StdMarshalCF_LockServer
1833 static const IClassFactoryVtbl *StdMarshalCF = &StdMarshalCFVtbl;
1835 HRESULT MARSHAL_GetStandardMarshalCF(LPVOID *ppv)
1837 *ppv = &StdMarshalCF;
1838 return S_OK;
1841 /***********************************************************************
1842 * CoMarshalHresult [OLE32.@]
1844 * Marshals an HRESULT value into a stream.
1846 * PARAMS
1847 * pStm [I] Stream that hresult will be marshalled into.
1848 * hresult [I] HRESULT to be marshalled.
1850 * RETURNS
1851 * Success: S_OK
1852 * Failure: A COM error code
1854 * SEE ALSO
1855 * CoUnmarshalHresult().
1857 HRESULT WINAPI CoMarshalHresult(LPSTREAM pStm, HRESULT hresult)
1859 return IStream_Write(pStm, &hresult, sizeof(hresult), NULL);
1862 /***********************************************************************
1863 * CoUnmarshalHresult [OLE32.@]
1865 * Unmarshals an HRESULT value from a stream.
1867 * PARAMS
1868 * pStm [I] Stream that hresult will be unmarshalled from.
1869 * phresult [I] Pointer to HRESULT where the value will be unmarshalled to.
1871 * RETURNS
1872 * Success: S_OK
1873 * Failure: A COM error code
1875 * SEE ALSO
1876 * CoMarshalHresult().
1878 HRESULT WINAPI CoUnmarshalHresult(LPSTREAM pStm, HRESULT * phresult)
1880 return IStream_Read(pStm, phresult, sizeof(*phresult), NULL);