Make COM use the RPC runtime as the backend for RPC calls. Based on a
[wine/wine64.git] / dlls / ole32 / marshal.c
blobbcc54d861de268d8c02842de5914af19ed88eced
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 1 /* FIXME: this should be 5, but we have to wait for IRemUnknown support first */
55 /* private flag indicating that the caller does not want to notify the stub
56 * when the proxy disconnects or is destroyed */
57 #define SORFP_NOLIFETIMEMGMT SORF_OXRES1
59 static HRESULT unmarshal_object(const STDOBJREF *stdobjref, APARTMENT *apt, REFIID riid, void **object);
61 /* Marshalling just passes a unique identifier to the remote client,
62 * that makes it possible to find the passed interface again.
64 * So basically we need a set of values that make it unique.
66 * Process Identifier, Object IUnknown ptr, IID
68 * Note that the IUnknown_QI(ob,xiid,&ppv) always returns the SAME ppv value!
70 * In Windows, a different triple is used: OXID (apt id), OID (stub
71 * manager id), 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
79 get_facbuf_for_iid(REFIID riid,IPSFactoryBuffer **facbuf) {
80 HRESULT hres;
81 CLSID pxclsid;
83 if ((hres = CoGetPSClsid(riid,&pxclsid)))
84 return hres;
85 return CoGetClassObject(&pxclsid,CLSCTX_INPROC_SERVER,NULL,&IID_IPSFactoryBuffer,(LPVOID*)facbuf);
88 /* creates a new stub manager */
89 HRESULT register_ifstub(APARTMENT *apt, STDOBJREF *stdobjref, REFIID riid, IUnknown *obj, MSHLFLAGS mshlflags)
91 struct stub_manager *manager;
92 struct ifstub *ifstub;
93 BOOL tablemarshal;
94 IRpcStubBuffer *stub;
95 IPSFactoryBuffer *psfb;
96 HRESULT hr;
98 hr = get_facbuf_for_iid(riid, &psfb);
99 if (hr != S_OK)
101 ERR("couldn't get IPSFactory buffer for interface %s\n", debugstr_guid(riid));
102 return hr;
105 hr = IPSFactoryBuffer_CreateStub(psfb, riid, obj, &stub);
106 IPSFactoryBuffer_Release(psfb);
107 if (hr != S_OK)
109 ERR("Failed to create an IRpcStubBuffer from IPSFactory for %s\n", debugstr_guid(riid));
110 return hr;
113 if (mshlflags & MSHLFLAGS_NOPING)
114 stdobjref->flags = SORF_NOPING;
115 else
116 stdobjref->flags = SORF_NULL;
118 stdobjref->oxid = apt->oxid;
120 if ((manager = get_stub_manager_from_object(apt, obj)))
121 TRACE("registering new ifstub on pre-existing manager\n");
122 else
124 TRACE("constructing new stub manager\n");
126 manager = new_stub_manager(apt, obj);
127 if (!manager)
128 return E_OUTOFMEMORY;
130 stdobjref->oid = manager->oid;
132 tablemarshal = ((mshlflags & MSHLFLAGS_TABLESTRONG) || (mshlflags & MSHLFLAGS_TABLEWEAK));
134 ifstub = stub_manager_new_ifstub(manager, stub, obj, riid, tablemarshal);
135 if (!ifstub)
137 IRpcStubBuffer_Release(stub);
138 stub_manager_int_release(manager);
139 /* FIXME: should we do another release to completely destroy the
140 * stub manager? */
141 return E_OUTOFMEMORY;
144 if (!tablemarshal)
146 stdobjref->cPublicRefs = NORMALEXTREFS;
147 stub_manager_ext_addref(manager, stdobjref->cPublicRefs);
149 else
151 stdobjref->cPublicRefs = 0;
152 if (mshlflags & MSHLFLAGS_TABLESTRONG)
153 stub_manager_ext_addref(manager, 1);
156 /* FIXME: check return value */
157 RPC_RegisterInterface(riid);
159 stdobjref->ipid = ifstub->ipid;
161 stub_manager_int_release(manager);
162 return S_OK;
167 /* Client-side identity of the server object */
169 static HRESULT proxy_manager_get_remunknown(struct proxy_manager * This, IRemUnknown **remunk);
170 static void proxy_manager_destroy(struct proxy_manager * This);
171 static HRESULT proxy_manager_find_ifproxy(struct proxy_manager * This, REFIID riid, struct ifproxy ** ifproxy_found);
172 static HRESULT proxy_manager_query_local_interface(struct proxy_manager * This, REFIID riid, void ** ppv);
174 static HRESULT WINAPI ClientIdentity_QueryInterface(IMultiQI * iface, REFIID riid, void ** ppv)
176 HRESULT hr;
177 MULTI_QI mqi;
179 TRACE("%s\n", debugstr_guid(riid));
181 mqi.pIID = riid;
182 hr = IMultiQI_QueryMultipleInterfaces(iface, 1, &mqi);
183 *ppv = (void *)mqi.pItf;
185 return hr;
188 static ULONG WINAPI ClientIdentity_AddRef(IMultiQI * iface)
190 struct proxy_manager * This = (struct proxy_manager *)iface;
191 TRACE("%p - before %ld\n", iface, This->refs);
192 return InterlockedIncrement(&This->refs);
195 static ULONG WINAPI ClientIdentity_Release(IMultiQI * iface)
197 struct proxy_manager * This = (struct proxy_manager *)iface;
198 ULONG refs = InterlockedDecrement(&This->refs);
199 TRACE("%p - after %ld\n", iface, refs);
200 if (!refs)
201 proxy_manager_destroy(This);
202 return refs;
205 static HRESULT WINAPI ClientIdentity_QueryMultipleInterfaces(IMultiQI *iface, ULONG cMQIs, MULTI_QI *pMQIs)
207 struct proxy_manager * This = (struct proxy_manager *)iface;
208 REMQIRESULT *qiresults = NULL;
209 ULONG nonlocal_mqis = 0;
210 ULONG i;
211 ULONG successful_mqis = 0;
212 IID *iids = HeapAlloc(GetProcessHeap(), 0, cMQIs * sizeof(*iids));
213 /* mapping of RemQueryInterface index to QueryMultipleInterfaces index */
214 ULONG *mapping = HeapAlloc(GetProcessHeap(), 0, cMQIs * sizeof(*mapping));
216 TRACE("cMQIs: %ld\n", cMQIs);
218 /* try to get a local interface - this includes already active proxy
219 * interfaces and also interfaces exposed by the proxy manager */
220 for (i = 0; i < cMQIs; i++)
222 TRACE("iid[%ld] = %s\n", i, debugstr_guid(pMQIs[i].pIID));
223 pMQIs[i].hr = proxy_manager_query_local_interface(This, pMQIs[i].pIID, (void **)&pMQIs[i].pItf);
224 if (pMQIs[i].hr == S_OK)
225 successful_mqis++;
226 else
228 iids[nonlocal_mqis] = *pMQIs[i].pIID;
229 mapping[nonlocal_mqis] = i;
230 nonlocal_mqis++;
234 TRACE("%ld interfaces not found locally\n", nonlocal_mqis);
236 /* if we have more than one interface not found locally then we must try
237 * to query the remote object for it */
238 if (nonlocal_mqis != 0)
240 IRemUnknown *remunk;
241 HRESULT hr;
242 IPID *ipid;
244 /* get the ipid of the first entry */
245 /* FIXME: should we implement ClientIdentity on the ifproxies instead
246 * of the proxy_manager so we use the correct ipid here? */
247 ipid = &LIST_ENTRY(list_head(&This->interfaces), struct ifproxy, entry)->ipid;
249 /* get IRemUnknown proxy so we can communicate with the remote object */
250 hr = proxy_manager_get_remunknown(This, &remunk);
252 if (hr == S_OK)
254 hr = IRemUnknown_RemQueryInterface(remunk, ipid, NORMALEXTREFS,
255 nonlocal_mqis, iids, &qiresults);
256 if (FAILED(hr))
257 ERR("IRemUnknown_RemQueryInterface failed with error 0x%08lx\n", hr);
260 /* IRemUnknown_RemQueryInterface can return S_FALSE if only some of
261 * the interfaces were returned */
262 if (SUCCEEDED(hr))
264 /* try to unmarshal each object returned to us */
265 for (i = 0; i < nonlocal_mqis; i++)
267 ULONG index = mapping[i];
268 HRESULT hrobj = qiresults[i].hResult;
269 if (hrobj == S_OK)
270 hrobj = unmarshal_object(&qiresults[i].std, This->parent,
271 pMQIs[index].pIID,
272 (void **)&pMQIs[index].pItf);
274 if (hrobj == S_OK)
275 successful_mqis++;
276 else
277 ERR("Failed to get pointer to interface %s\n", debugstr_guid(pMQIs[index].pIID));
278 pMQIs[index].hr = hrobj;
282 /* free the memory allocated by the proxy */
283 CoTaskMemFree(qiresults);
286 TRACE("%ld/%ld successfully queried\n", successful_mqis, cMQIs);
288 HeapFree(GetProcessHeap(), 0, iids);
289 HeapFree(GetProcessHeap(), 0, mapping);
291 if (successful_mqis == cMQIs)
292 return S_OK; /* we got all requested interfaces */
293 else if (successful_mqis == 0)
294 return E_NOINTERFACE; /* we didn't get any interfaces */
295 else
296 return S_FALSE; /* we got some interfaces */
299 static const IMultiQIVtbl ClientIdentity_Vtbl =
301 ClientIdentity_QueryInterface,
302 ClientIdentity_AddRef,
303 ClientIdentity_Release,
304 ClientIdentity_QueryMultipleInterfaces
307 static HRESULT ifproxy_get_public_ref(struct ifproxy * This)
309 HRESULT hr = S_OK;
310 /* FIXME: as this call could possibly be going over the network, we
311 * are going to spend a long time in this CS. We might want to replace
312 * this with a mutex */
313 EnterCriticalSection(&This->parent->cs);
314 if (This->refs == 0)
316 IRemUnknown *remunk = NULL;
318 TRACE("getting public ref for ifproxy %p\n", This);
320 hr = proxy_manager_get_remunknown(This->parent, &remunk);
321 if (hr == S_OK)
323 HRESULT hrref;
324 REMINTERFACEREF rif;
325 rif.ipid = This->ipid;
326 rif.cPublicRefs = NORMALEXTREFS;
327 rif.cPrivateRefs = 0;
328 hr = IRemUnknown_RemAddRef(remunk, 1, &rif, &hrref);
329 if (hr == S_OK && hrref == S_OK)
330 This->refs += NORMALEXTREFS;
331 else
332 ERR("IRemUnknown_RemAddRef returned with 0x%08lx, hrref = 0x%08lx\n", hr, hrref);
335 LeaveCriticalSection(&This->parent->cs);
337 return hr;
340 static HRESULT ifproxy_release_public_refs(struct ifproxy * This)
342 HRESULT hr = S_OK;
344 /* FIXME: as this call could possibly be going over the network, we
345 * are going to spend a long time in this CS. We might want to replace
346 * this with a mutex */
347 EnterCriticalSection(&This->parent->cs);
348 if (This->refs > 0)
350 IRemUnknown *remunk = NULL;
352 TRACE("releasing %ld refs\n", This->refs);
354 hr = proxy_manager_get_remunknown(This->parent, &remunk);
355 if (hr == S_OK)
357 REMINTERFACEREF rif;
358 rif.ipid = This->ipid;
359 rif.cPublicRefs = This->refs;
360 rif.cPrivateRefs = 0;
361 hr = IRemUnknown_RemRelease(remunk, 1, &rif);
362 if (hr == S_OK)
363 This->refs = 0;
364 else if (hr == RPC_E_DISCONNECTED)
365 WARN("couldn't release references because object was "
366 "disconnected: oxid = %s, oid = %s\n",
367 wine_dbgstr_longlong(This->parent->oxid),
368 wine_dbgstr_longlong(This->parent->oid));
369 else
370 ERR("IRemUnknown_RemRelease failed with error 0x%08lx\n", hr);
373 LeaveCriticalSection(&This->parent->cs);
375 return hr;
378 static void ifproxy_disconnect(struct ifproxy * This)
380 ifproxy_release_public_refs(This);
381 if (This->proxy) IRpcProxyBuffer_Disconnect(This->proxy);
384 static void ifproxy_destroy(struct ifproxy * This)
386 TRACE("%p\n", This);
388 /* release public references to this object so that the stub can know
389 * when to destroy itself */
390 ifproxy_release_public_refs(This);
392 list_remove(&This->entry);
394 /* note: we don't call Release for This->proxy because its lifetime is
395 * controlled by the return value from ClientIdentity_Release, which this
396 * function is always called from */
398 HeapFree(GetProcessHeap(), 0, This);
401 static HRESULT proxy_manager_construct(
402 APARTMENT * apt, ULONG sorflags, OXID oxid, OID oid,
403 IRpcChannelBuffer * channel, struct proxy_manager ** proxy_manager)
405 struct proxy_manager * This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
406 if (!This) return E_OUTOFMEMORY;
408 This->lpVtbl = &ClientIdentity_Vtbl;
410 list_init(&This->entry);
411 list_init(&This->interfaces);
413 InitializeCriticalSection(&This->cs);
415 /* the apartment the object was unmarshaled into */
416 This->parent = apt;
418 /* the source apartment and id of the object */
419 This->oxid = oxid;
420 This->oid = oid;
422 This->refs = 1;
424 /* the DCOM draft specification states that the SORF_NOPING flag is
425 * proxy manager specific, not ifproxy specific, so this implies that we
426 * should store the STDOBJREF flags in the proxy manager. */
427 This->sorflags = sorflags;
429 assert(channel);
430 This->chan = channel; /* FIXME: we should take the binding strings and construct the channel in this function */
432 /* we create the IRemUnknown proxy on demand */
433 This->remunk = NULL;
435 EnterCriticalSection(&apt->cs);
436 /* FIXME: we are dependent on the ordering in here to make sure a proxy's
437 * IRemUnknown proxy doesn't get destroyed before the regual proxy does
438 * because we need the IRemUnknown proxy during the destruction of the
439 * regular proxy. Ideally, we should maintain a separate list for the
440 * IRemUnknown proxies that need late destruction */
441 list_add_tail(&apt->proxies, &This->entry);
442 LeaveCriticalSection(&apt->cs);
444 TRACE("%p created for OXID %s, OID %s\n", This,
445 wine_dbgstr_longlong(oxid), wine_dbgstr_longlong(oid));
447 *proxy_manager = This;
448 return S_OK;
451 static HRESULT proxy_manager_query_local_interface(struct proxy_manager * This, REFIID riid, void ** ppv)
453 HRESULT hr;
454 struct ifproxy * ifproxy;
456 TRACE("%s\n", debugstr_guid(riid));
458 if (IsEqualIID(riid, &IID_IUnknown) ||
459 IsEqualIID(riid, &IID_IMultiQI))
461 *ppv = (void *)&This->lpVtbl;
462 IMultiQI_AddRef((IMultiQI *)&This->lpVtbl);
463 return S_OK;
466 hr = proxy_manager_find_ifproxy(This, riid, &ifproxy);
467 if (hr == S_OK)
469 *ppv = ifproxy->iface;
470 IUnknown_AddRef((IUnknown *)*ppv);
471 return S_OK;
474 *ppv = NULL;
475 return E_NOINTERFACE;
478 static HRESULT proxy_manager_create_ifproxy(
479 struct proxy_manager * This, IPID ipid, REFIID riid, ULONG cPublicRefs,
480 struct ifproxy ** iif_out)
482 HRESULT hr;
483 IPSFactoryBuffer * psfb;
484 struct ifproxy * ifproxy = HeapAlloc(GetProcessHeap(), 0, sizeof(*ifproxy));
485 if (!ifproxy) return E_OUTOFMEMORY;
487 list_init(&ifproxy->entry);
489 ifproxy->parent = This;
490 ifproxy->ipid = ipid;
491 ifproxy->iid = *riid;
492 ifproxy->refs = cPublicRefs;
493 ifproxy->proxy = NULL;
495 /* the IUnknown interface is special because it does not have a
496 * proxy associated with the ifproxy as we handle IUnknown ourselves */
497 if (IsEqualIID(riid, &IID_IUnknown))
499 ifproxy->iface = (void *)&This->lpVtbl;
500 hr = S_OK;
502 else
504 hr = get_facbuf_for_iid(riid, &psfb);
505 if (hr == S_OK)
507 /* important note: the outer unknown is set to the proxy manager.
508 * This ensures the COM identity rules are not violated, by having a
509 * one-to-one mapping of objects on the proxy side to objects on the
510 * stub side, no matter which interface you view the object through */
511 hr = IPSFactoryBuffer_CreateProxy(psfb, (IUnknown *)&This->lpVtbl, riid,
512 &ifproxy->proxy, &ifproxy->iface);
513 IPSFactoryBuffer_Release(psfb);
514 if (hr != S_OK)
515 ERR("Could not create proxy for interface %s, error 0x%08lx\n",
516 debugstr_guid(riid), hr);
518 else
519 ERR("Could not get IPSFactoryBuffer for interface %s, error 0x%08lx\n",
520 debugstr_guid(riid), hr);
522 if (hr == S_OK)
523 hr = IRpcProxyBuffer_Connect(ifproxy->proxy, This->chan);
526 /* get at least one external reference to the object to keep it alive */
527 if (hr == S_OK)
528 hr = ifproxy_get_public_ref(ifproxy);
530 if (hr == S_OK)
532 EnterCriticalSection(&This->cs);
533 list_add_tail(&This->interfaces, &ifproxy->entry);
534 LeaveCriticalSection(&This->cs);
536 *iif_out = ifproxy;
537 TRACE("ifproxy %p created for IPID %s, interface %s with %lu public refs\n",
538 ifproxy, debugstr_guid(&ipid), debugstr_guid(riid), cPublicRefs);
540 else
541 ifproxy_destroy(ifproxy);
543 return hr;
546 static HRESULT proxy_manager_find_ifproxy(struct proxy_manager * This, REFIID riid, struct ifproxy ** ifproxy_found)
548 HRESULT hr = E_NOINTERFACE; /* assume not found */
549 struct list * cursor;
551 EnterCriticalSection(&This->cs);
552 LIST_FOR_EACH(cursor, &This->interfaces)
554 struct ifproxy * ifproxy = LIST_ENTRY(cursor, struct ifproxy, entry);
555 if (IsEqualIID(riid, &ifproxy->iid))
557 *ifproxy_found = ifproxy;
558 hr = S_OK;
559 break;
562 LeaveCriticalSection(&This->cs);
564 return hr;
567 static void proxy_manager_disconnect(struct proxy_manager * This)
569 struct list * cursor;
571 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This->oxid),
572 wine_dbgstr_longlong(This->oid));
574 EnterCriticalSection(&This->cs);
576 LIST_FOR_EACH(cursor, &This->interfaces)
578 struct ifproxy * ifproxy = LIST_ENTRY(cursor, struct ifproxy, entry);
579 ifproxy_disconnect(ifproxy);
582 /* apartment is being destroyed so don't keep a pointer around to it */
583 This->parent = NULL;
585 /* FIXME: will this still be necessary if/when we use a real RPC
586 * channel? */
587 IRpcChannelBuffer_Release(This->chan);
588 This->chan = NULL;
590 LeaveCriticalSection(&This->cs);
593 static HRESULT proxy_manager_get_remunknown(struct proxy_manager * This, IRemUnknown **remunk)
595 HRESULT hr = S_OK;
597 /* we don't want to try and unmarshal or use IRemUnknown if we don't want
598 * lifetime management */
599 if (This->sorflags & SORFP_NOLIFETIMEMGMT)
600 return S_FALSE;
602 EnterCriticalSection(&This->cs);
603 if (This->remunk)
604 /* already created - return existing object */
605 *remunk = This->remunk;
606 else if (!This->parent)
607 /* disconnected - we can't create IRemUnknown */
608 hr = S_FALSE;
609 else
611 STDOBJREF stdobjref;
612 /* Don't want IRemUnknown lifetime management as this is IRemUnknown!
613 * We also don't care about whether or not the stub is still alive */
614 stdobjref.flags = SORFP_NOLIFETIMEMGMT | SORF_NOPING;
615 stdobjref.cPublicRefs = 1;
616 /* oxid of destination object */
617 stdobjref.oxid = This->oxid;
618 /* FIXME: what should be used for the oid? The DCOM draft doesn't say */
619 stdobjref.oid = (OID)-1;
620 /* FIXME: this is a hack around not having an OXID resolver yet -
621 * the OXID resolver should give us the IPID of the IRemUnknown
622 * interface */
623 stdobjref.ipid.Data1 = 0xffffffff;
624 stdobjref.ipid.Data2 = 0xffff;
625 stdobjref.ipid.Data3 = 0xffff;
626 assert(sizeof(stdobjref.ipid.Data4) == sizeof(stdobjref.oxid));
627 memcpy(&stdobjref.ipid.Data4, &stdobjref.oxid, sizeof(OXID));
629 /* do the unmarshal */
630 hr = unmarshal_object(&stdobjref, This->parent, &IID_IRemUnknown, (void**)&This->remunk);
631 if (hr == S_OK)
632 *remunk = This->remunk;
634 LeaveCriticalSection(&This->cs);
636 TRACE("got IRemUnknown* pointer %p, hr = 0x%08lx\n", *remunk, hr);
638 return hr;
641 /* destroys a proxy manager, freeing the memory it used.
642 * Note: this function should not be called from a list iteration in the
643 * apartment, due to the fact that it removes itself from the apartment and
644 * it could add a proxy to IRemUnknown into the apartment. */
645 static void proxy_manager_destroy(struct proxy_manager * This)
647 struct list * cursor;
649 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This->oxid),
650 wine_dbgstr_longlong(This->oid));
652 if (This->parent)
654 EnterCriticalSection(&This->parent->cs);
656 /* remove ourself from the list of proxy objects in the apartment */
657 LIST_FOR_EACH(cursor, &This->parent->proxies)
659 if (cursor == &This->entry)
661 list_remove(&This->entry);
662 break;
666 LeaveCriticalSection(&This->parent->cs);
669 /* destroy all of the interface proxies */
670 while ((cursor = list_head(&This->interfaces)))
672 struct ifproxy * ifproxy = LIST_ENTRY(cursor, struct ifproxy, entry);
673 ifproxy_destroy(ifproxy);
676 if (This->remunk) IRemUnknown_Release(This->remunk);
677 if (This->chan) IRpcChannelBuffer_Release(This->chan);
679 DeleteCriticalSection(&This->cs);
681 HeapFree(GetProcessHeap(), 0, This);
684 /* finds the proxy manager corresponding to a given OXID and OID that has
685 * been unmarshaled in the specified apartment. The caller must release the
686 * reference to the proxy_manager when the object is no longer used. */
687 static BOOL find_proxy_manager(APARTMENT * apt, OXID oxid, OID oid, struct proxy_manager ** proxy_found)
689 BOOL found = FALSE;
690 struct list * cursor;
692 EnterCriticalSection(&apt->cs);
693 LIST_FOR_EACH(cursor, &apt->proxies)
695 struct proxy_manager * proxy = LIST_ENTRY(cursor, struct proxy_manager, entry);
696 if ((oxid == proxy->oxid) && (oid == proxy->oid))
698 *proxy_found = proxy;
699 ClientIdentity_AddRef((IMultiQI *)&proxy->lpVtbl);
700 found = TRUE;
701 break;
704 LeaveCriticalSection(&apt->cs);
705 return found;
708 HRESULT MARSHAL_Disconnect_Proxies(APARTMENT *apt)
710 struct list * cursor;
712 EnterCriticalSection(&apt->cs);
713 LIST_FOR_EACH(cursor, &apt->proxies)
715 struct proxy_manager * proxy = LIST_ENTRY(cursor, struct proxy_manager, entry);
716 proxy_manager_disconnect(proxy);
718 LeaveCriticalSection(&apt->cs);
720 return S_OK;
723 /********************** StdMarshal implementation ****************************/
724 typedef struct _StdMarshalImpl {
725 IMarshalVtbl *lpvtbl;
726 DWORD ref;
728 IID iid;
729 DWORD dwDestContext;
730 LPVOID pvDestContext;
731 DWORD mshlflags;
732 } StdMarshalImpl;
734 static HRESULT WINAPI
735 StdMarshalImpl_QueryInterface(LPMARSHAL iface,REFIID riid,LPVOID *ppv) {
736 *ppv = NULL;
737 if (IsEqualIID(&IID_IUnknown,riid) || IsEqualIID(&IID_IMarshal,riid)) {
738 *ppv = iface;
739 IUnknown_AddRef(iface);
740 return S_OK;
742 FIXME("No interface for %s.\n",debugstr_guid(riid));
743 return E_NOINTERFACE;
746 static ULONG WINAPI
747 StdMarshalImpl_AddRef(LPMARSHAL iface) {
748 StdMarshalImpl *This = (StdMarshalImpl *)iface;
749 return InterlockedIncrement(&This->ref);
752 static ULONG WINAPI
753 StdMarshalImpl_Release(LPMARSHAL iface) {
754 StdMarshalImpl *This = (StdMarshalImpl *)iface;
755 ULONG ref = InterlockedDecrement(&This->ref);
757 if (!ref) HeapFree(GetProcessHeap(),0,This);
758 return ref;
761 static HRESULT WINAPI
762 StdMarshalImpl_GetUnmarshalClass(
763 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
764 void* pvDestContext, DWORD mshlflags, CLSID* pCid
766 memcpy(pCid,&CLSID_DfMarshal,sizeof(CLSID_DfMarshal));
767 return S_OK;
770 static HRESULT WINAPI
771 StdMarshalImpl_GetMarshalSizeMax(
772 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
773 void* pvDestContext, DWORD mshlflags, DWORD* pSize)
775 *pSize = sizeof(STDOBJREF);
776 return S_OK;
779 static HRESULT WINAPI
780 StdMarshalImpl_MarshalInterface(
781 LPMARSHAL iface, IStream *pStm,REFIID riid, void* pv, DWORD dwDestContext,
782 void* pvDestContext, DWORD mshlflags
784 STDOBJREF stdobjref;
785 IUnknown *pUnk;
786 ULONG res;
787 HRESULT hres;
788 APARTMENT *apt = COM_CurrentApt();
790 TRACE("(...,%s,...)\n",debugstr_guid(riid));
792 if (!apt)
794 ERR("Apartment not initialized\n");
795 return CO_E_NOTINITIALIZED;
798 start_apartment_listener_thread(); /* just to be sure we have one running. */
799 start_apartment_remote_unknown();
801 hres = IUnknown_QueryInterface((LPUNKNOWN)pv, riid, (LPVOID*)&pUnk);
802 if (hres != S_OK)
804 ERR("object doesn't expose interface %s, failing with error 0x%08lx\n",
805 debugstr_guid(riid), hres);
806 return E_NOINTERFACE;
809 hres = register_ifstub(apt, &stdobjref, riid, pUnk, mshlflags);
811 IUnknown_Release(pUnk);
813 if (hres)
815 FIXME("Failed to create ifstub, hres=0x%lx\n", hres);
816 return hres;
819 hres = IStream_Write(pStm, &stdobjref, sizeof(stdobjref), &res);
820 if (hres) return hres;
822 return S_OK;
825 /* helper for StdMarshalImpl_UnmarshalInterface - does the unmarshaling with
826 * no questions asked about the rules surrounding same-apartment unmarshals
827 * and table marshaling */
828 static HRESULT unmarshal_object(const STDOBJREF *stdobjref, APARTMENT *apt, REFIID riid, void **object)
830 struct proxy_manager *proxy_manager = NULL;
831 HRESULT hr = S_OK;
833 assert(apt);
835 TRACE("stdobjref:\n\tflags = %04lx\n\tcPublicRefs = %ld\n\toxid = %s\n\toid = %s\n\tipid = %s\n",
836 stdobjref->flags, stdobjref->cPublicRefs,
837 wine_dbgstr_longlong(stdobjref->oxid),
838 wine_dbgstr_longlong(stdobjref->oid),
839 debugstr_guid(&stdobjref->ipid));
841 /* create an a new proxy manager if one doesn't already exist for the
842 * object */
843 if (!find_proxy_manager(apt, stdobjref->oxid, stdobjref->oid, &proxy_manager))
845 IRpcChannelBuffer *chanbuf;
846 wine_marshal_id mid;
848 mid.oxid = stdobjref->oxid;
849 mid.oid = stdobjref->oid;
850 mid.ipid = stdobjref->ipid;
852 hr = PIPE_GetNewPipeBuf(&mid,&chanbuf);
853 if (hr == S_OK)
854 hr = proxy_manager_construct(apt, stdobjref->flags,
855 stdobjref->oxid, stdobjref->oid,
856 chanbuf, &proxy_manager);
858 else
859 TRACE("proxy manager already created, using\n");
861 if (hr == S_OK)
863 struct ifproxy * ifproxy;
864 hr = proxy_manager_find_ifproxy(proxy_manager, riid, &ifproxy);
865 if (hr == E_NOINTERFACE)
866 hr = proxy_manager_create_ifproxy(proxy_manager, stdobjref->ipid,
867 riid, stdobjref->cPublicRefs,
868 &ifproxy);
870 if (hr == S_OK)
872 /* FIXME: push this AddRef inside proxy_manager_find_ifproxy/create_ifproxy? */
873 ClientIdentity_AddRef((IMultiQI*)&proxy_manager->lpVtbl);
874 *object = ifproxy->iface;
878 /* release our reference to the proxy manager - the client/apartment
879 * will hold on to the remaining reference for us */
880 if (proxy_manager) ClientIdentity_Release((IMultiQI*)&proxy_manager->lpVtbl);
882 return hr;
885 static HRESULT WINAPI
886 StdMarshalImpl_UnmarshalInterface(LPMARSHAL iface, IStream *pStm, REFIID riid, void **ppv)
888 struct stub_manager *stubmgr;
889 STDOBJREF stdobjref;
890 ULONG res;
891 HRESULT hres;
892 APARTMENT *apt = COM_CurrentApt();
893 APARTMENT *stub_apt;
895 TRACE("(...,%s,....)\n",debugstr_guid(riid));
897 /* we need an apartment to unmarshal into */
898 if (!apt)
900 ERR("Apartment not initialized\n");
901 return CO_E_NOTINITIALIZED;
904 /* read STDOBJREF from wire */
905 hres = IStream_Read(pStm, &stdobjref, sizeof(stdobjref), &res);
906 if (hres) return hres;
908 /* check if we're marshalling back to ourselves */
909 if ((apt->oxid == stdobjref.oxid) && (stubmgr = get_stub_manager(apt, stdobjref.oid)))
911 TRACE("Unmarshalling object marshalled in same apartment for iid %s, "
912 "returning original object %p\n", debugstr_guid(riid), stubmgr->object);
914 hres = IUnknown_QueryInterface(stubmgr->object, riid, ppv);
916 /* unref the ifstub. FIXME: only do this on success? */
917 if (!stub_manager_is_table_marshaled(stubmgr, &stdobjref.ipid))
918 stub_manager_ext_release(stubmgr, 1);
920 stub_manager_int_release(stubmgr);
921 return hres;
924 /* notify stub manager about unmarshal if process-local object.
925 * note: if the oxid is not found then we and native will quite happily
926 * ignore table marshaling and normal marshaling rules regarding number of
927 * unmarshals, etc, but if you abuse these rules then your proxy could end
928 * up returning RPC_E_DISCONNECTED. */
929 if ((stub_apt = COM_ApartmentFromOXID(stdobjref.oxid, TRUE)))
931 if ((stubmgr = get_stub_manager(stub_apt, stdobjref.oid)))
933 if (!stub_manager_notify_unmarshal(stubmgr, &stdobjref.ipid))
934 hres = CO_E_OBJNOTCONNECTED;
936 stub_manager_int_release(stubmgr);
938 else
940 WARN("Couldn't find object for OXID %s, OID %s, assuming disconnected\n",
941 wine_dbgstr_longlong(stdobjref.oxid),
942 wine_dbgstr_longlong(stdobjref.oid));
943 hres = CO_E_OBJNOTCONNECTED;
946 COM_ApartmentRelease(stub_apt);
948 else
949 TRACE("Treating unmarshal from OXID %s as inter-process\n",
950 wine_dbgstr_longlong(stdobjref.oxid));
952 if (hres == S_OK)
953 hres = unmarshal_object(&stdobjref, apt, riid, ppv);
955 if (hres) WARN("Failed with error 0x%08lx\n", hres);
956 else TRACE("Successfully created proxy %p\n", *ppv);
958 return hres;
961 static HRESULT WINAPI
962 StdMarshalImpl_ReleaseMarshalData(LPMARSHAL iface, IStream *pStm) {
963 STDOBJREF stdobjref;
964 ULONG res;
965 HRESULT hres;
966 struct stub_manager *stubmgr;
967 APARTMENT *apt;
969 TRACE("iface=%p, pStm=%p\n", iface, pStm);
971 hres = IStream_Read(pStm, &stdobjref, sizeof(stdobjref), &res);
972 if (hres) return hres;
974 if (!(apt = COM_ApartmentFromOXID(stdobjref.oxid, TRUE)))
976 WARN("Could not map OXID %s to apartment object\n",
977 wine_dbgstr_longlong(stdobjref.oxid));
978 return RPC_E_INVALID_OBJREF;
981 if (!(stubmgr = get_stub_manager(apt, stdobjref.oid)))
983 ERR("could not map MID to stub manager, oxid=%s, oid=%s\n",
984 wine_dbgstr_longlong(stdobjref.oxid), wine_dbgstr_longlong(stdobjref.oid));
985 return RPC_E_INVALID_OBJREF;
988 /* FIXME: don't release if table-weak and already unmarshaled an object */
989 /* FIXME: this should also depend on stdobjref.cPublicRefs */
990 stub_manager_ext_release(stubmgr, 1);
992 stub_manager_int_release(stubmgr);
993 COM_ApartmentRelease(apt);
995 return S_OK;
998 static HRESULT WINAPI
999 StdMarshalImpl_DisconnectObject(LPMARSHAL iface, DWORD dwReserved) {
1000 FIXME("(), stub!\n");
1001 return S_OK;
1004 IMarshalVtbl stdmvtbl = {
1005 StdMarshalImpl_QueryInterface,
1006 StdMarshalImpl_AddRef,
1007 StdMarshalImpl_Release,
1008 StdMarshalImpl_GetUnmarshalClass,
1009 StdMarshalImpl_GetMarshalSizeMax,
1010 StdMarshalImpl_MarshalInterface,
1011 StdMarshalImpl_UnmarshalInterface,
1012 StdMarshalImpl_ReleaseMarshalData,
1013 StdMarshalImpl_DisconnectObject
1016 static HRESULT StdMarshalImpl_Construct(REFIID riid, void** ppvObject)
1018 StdMarshalImpl * pStdMarshal =
1019 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(StdMarshalImpl));
1020 if (!pStdMarshal)
1021 return E_OUTOFMEMORY;
1022 pStdMarshal->lpvtbl = &stdmvtbl;
1023 pStdMarshal->ref = 0;
1024 return IMarshal_QueryInterface((IMarshal*)pStdMarshal, riid, ppvObject);
1027 /***********************************************************************
1028 * CoGetStandardMarshal [OLE32.@]
1030 * Gets or creates a standard marshal object.
1032 * PARAMS
1033 * riid [I] Interface identifier of the pUnk object.
1034 * pUnk [I] Optional. Object to get the marshal object for.
1035 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1036 * pvDestContext [I] Reserved. Must be NULL.
1037 * mshlflags [I] Flags affecting the marshaling process.
1038 * ppMarshal [O] Address where marshal object will be stored.
1040 * RETURNS
1041 * Success: S_OK.
1042 * Failure: HRESULT code.
1044 * NOTES
1046 * The function retrieves the IMarshal object associated with an object if
1047 * that object is currently an active stub, otherwise a new marshal object is
1048 * created.
1050 HRESULT WINAPI CoGetStandardMarshal(REFIID riid, IUnknown *pUnk,
1051 DWORD dwDestContext, LPVOID pvDestContext,
1052 DWORD mshlflags, LPMARSHAL *ppMarshal)
1054 StdMarshalImpl *dm;
1056 if (pUnk == NULL) {
1057 FIXME("(%s,NULL,%lx,%p,%lx,%p), unimplemented yet.\n",
1058 debugstr_guid(riid),dwDestContext,pvDestContext,mshlflags,ppMarshal
1060 return E_FAIL;
1062 TRACE("(%s,%p,%lx,%p,%lx,%p)\n",
1063 debugstr_guid(riid),pUnk,dwDestContext,pvDestContext,mshlflags,ppMarshal
1065 *ppMarshal = HeapAlloc(GetProcessHeap(),0,sizeof(StdMarshalImpl));
1066 dm = (StdMarshalImpl*) *ppMarshal;
1067 if (!dm) return E_FAIL;
1068 dm->lpvtbl = &stdmvtbl;
1069 dm->ref = 1;
1071 memcpy(&dm->iid,riid,sizeof(dm->iid));
1072 dm->dwDestContext = dwDestContext;
1073 dm->pvDestContext = pvDestContext;
1074 dm->mshlflags = mshlflags;
1075 return S_OK;
1078 /***********************************************************************
1079 * get_marshaler [internal]
1081 * Retrieves an IMarshal interface for an object.
1083 static HRESULT get_marshaler(REFIID riid, IUnknown *pUnk, DWORD dwDestContext,
1084 void *pvDestContext, DWORD mshlFlags,
1085 LPMARSHAL *pMarshal)
1087 HRESULT hr;
1089 if (!pUnk)
1090 return E_POINTER;
1091 hr = IUnknown_QueryInterface(pUnk, &IID_IMarshal, (LPVOID*)pMarshal);
1092 if (hr)
1093 hr = CoGetStandardMarshal(riid, pUnk, dwDestContext, pvDestContext,
1094 mshlFlags, pMarshal);
1095 return hr;
1098 /***********************************************************************
1099 * get_unmarshaler_from_stream [internal]
1101 * Creates an IMarshal* object according to the data marshaled to the stream.
1102 * The function leaves the stream pointer at the start of the data written
1103 * to the stream by the IMarshal* object.
1105 static HRESULT get_unmarshaler_from_stream(IStream *stream, IMarshal **marshal, IID *iid)
1107 HRESULT hr;
1108 ULONG res;
1109 OBJREF objref;
1111 /* read common OBJREF header */
1112 hr = IStream_Read(stream, &objref, FIELD_OFFSET(OBJREF, u_objref), &res);
1113 if (hr || (res != FIELD_OFFSET(OBJREF, u_objref)))
1115 ERR("Failed to read common OBJREF header, 0x%08lx\n", hr);
1116 return STG_E_READFAULT;
1119 /* sanity check on header */
1120 if (objref.signature != OBJREF_SIGNATURE)
1122 ERR("Bad OBJREF signature 0x%08lx\n", objref.signature);
1123 return RPC_E_INVALID_OBJREF;
1126 if (iid) *iid = objref.iid;
1128 /* FIXME: handler marshaling */
1129 if (objref.flags & OBJREF_STANDARD)
1131 TRACE("Using standard unmarshaling\n");
1132 hr = StdMarshalImpl_Construct(&IID_IMarshal, (LPVOID*)marshal);
1134 else if (objref.flags & OBJREF_CUSTOM)
1136 ULONG custom_header_size = FIELD_OFFSET(OBJREF, u_objref.u_custom.size) -
1137 FIELD_OFFSET(OBJREF, u_objref.u_custom);
1138 TRACE("Using custom unmarshaling\n");
1139 /* read constant sized OR_CUSTOM data from stream */
1140 hr = IStream_Read(stream, &objref.u_objref.u_custom,
1141 custom_header_size, &res);
1142 if (hr || (res != custom_header_size))
1144 ERR("Failed to read OR_CUSTOM header, 0x%08lx\n", hr);
1145 return STG_E_READFAULT;
1147 /* now create the marshaler specified in the stream */
1148 hr = CoCreateInstance(&objref.u_objref.u_custom.clsid, NULL,
1149 CLSCTX_INPROC_SERVER, &IID_IMarshal,
1150 (LPVOID*)marshal);
1152 else
1154 FIXME("Invalid or unimplemented marshaling type specified: %lx\n",
1155 objref.flags);
1156 return RPC_E_INVALID_OBJREF;
1159 if (hr)
1160 ERR("Failed to create marshal, 0x%08lx\n", hr);
1162 return hr;
1165 /***********************************************************************
1166 * CoGetMarshalSizeMax [OLE32.@]
1168 * Gets the maximum amount of data that will be needed by a marshal.
1170 * PARAMS
1171 * pulSize [O] Address where maximum marshal size will be stored.
1172 * riid [I] Identifier of the interface to marshal.
1173 * pUnk [I] Pointer to the object to marshal.
1174 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1175 * pvDestContext [I] Reserved. Must be NULL.
1176 * mshlFlags [I] Flags that affect the marshaling. See CoMarshalInterface().
1178 * RETURNS
1179 * Success: S_OK.
1180 * Failure: HRESULT code.
1182 * SEE ALSO
1183 * CoMarshalInterface().
1185 HRESULT WINAPI CoGetMarshalSizeMax(ULONG *pulSize, REFIID riid, IUnknown *pUnk,
1186 DWORD dwDestContext, void *pvDestContext,
1187 DWORD mshlFlags)
1189 HRESULT hr;
1190 LPMARSHAL pMarshal;
1191 CLSID marshaler_clsid;
1193 hr = get_marshaler(riid, pUnk, dwDestContext, pvDestContext, mshlFlags, &pMarshal);
1194 if (hr)
1195 return hr;
1197 hr = IMarshal_GetUnmarshalClass(pMarshal, riid, pUnk, dwDestContext,
1198 pvDestContext, mshlFlags, &marshaler_clsid);
1199 if (hr)
1201 ERR("IMarshal::GetUnmarshalClass failed, 0x%08lx\n", hr);
1202 IMarshal_Release(pMarshal);
1203 return hr;
1206 hr = IMarshal_GetMarshalSizeMax(pMarshal, riid, pUnk, dwDestContext,
1207 pvDestContext, mshlFlags, pulSize);
1208 /* add on the size of the common header */
1209 *pulSize += FIELD_OFFSET(OBJREF, u_objref);
1211 /* if custom marshaling, add on size of custom header */
1212 if (!IsEqualCLSID(&marshaler_clsid, &CLSID_DfMarshal))
1213 *pulSize += FIELD_OFFSET(OBJREF, u_objref.u_custom.size) -
1214 FIELD_OFFSET(OBJREF, u_objref.u_custom);
1216 IMarshal_Release(pMarshal);
1217 return hr;
1221 /***********************************************************************
1222 * CoMarshalInterface [OLE32.@]
1224 * Marshals an interface into a stream so that the object can then be
1225 * unmarshaled from another COM apartment and used remotely.
1227 * PARAMS
1228 * pStream [I] Stream the object will be marshaled into.
1229 * riid [I] Identifier of the interface to marshal.
1230 * pUnk [I] Pointer to the object to marshal.
1231 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1232 * pvDestContext [I] Reserved. Must be NULL.
1233 * mshlFlags [I] Flags that affect the marshaling. See notes.
1235 * RETURNS
1236 * Success: S_OK.
1237 * Failure: HRESULT code.
1239 * NOTES
1241 * The mshlFlags parameter can take one or more of the following flags:
1242 *| MSHLFLAGS_NORMAL - Unmarshal once, releases stub on last proxy release.
1243 *| MSHLFLAGS_TABLESTRONG - Unmarshal many, release when CoReleaseMarshalData() called.
1244 *| MSHLFLAGS_TABLEWEAK - Unmarshal many, releases stub on last proxy release.
1245 *| MSHLFLAGS_NOPING - No automatic garbage collection (and so reduces network traffic).
1247 * If a marshaled object is not unmarshaled, then CoReleaseMarshalData() must
1248 * be called in order to release the resources used in the marshaling.
1250 * SEE ALSO
1251 * CoUnmarshalInterface(), CoReleaseMarshalData().
1253 HRESULT WINAPI CoMarshalInterface(IStream *pStream, REFIID riid, IUnknown *pUnk,
1254 DWORD dwDestContext, void *pvDestContext,
1255 DWORD mshlFlags)
1257 HRESULT hr;
1258 CLSID marshaler_clsid;
1259 OBJREF objref;
1260 IStream * pMarshalStream = NULL;
1261 LPMARSHAL pMarshal;
1263 TRACE("(%p, %s, %p, %lx, %p, %lx)\n", pStream, debugstr_guid(riid), pUnk,
1264 dwDestContext, pvDestContext, mshlFlags);
1266 if (pUnk == NULL)
1267 return E_INVALIDARG;
1269 objref.signature = OBJREF_SIGNATURE;
1270 objref.iid = *riid;
1272 /* get the marshaler for the specified interface */
1273 hr = get_marshaler(riid, pUnk, dwDestContext, pvDestContext, mshlFlags, &pMarshal);
1274 if (hr)
1276 ERR("Failed to get marshaller, 0x%08lx\n", hr);
1277 return hr;
1280 hr = IMarshal_GetUnmarshalClass(pMarshal, riid, pUnk, dwDestContext,
1281 pvDestContext, mshlFlags, &marshaler_clsid);
1282 if (hr)
1284 ERR("IMarshal::GetUnmarshalClass failed, 0x%08lx\n", hr);
1285 goto cleanup;
1288 /* FIXME: implement handler marshaling too */
1289 if (IsEqualCLSID(&marshaler_clsid, &CLSID_DfMarshal))
1291 TRACE("Using standard marshaling\n");
1292 objref.flags = OBJREF_STANDARD;
1293 pMarshalStream = pStream;
1295 else
1297 TRACE("Using custom marshaling\n");
1298 objref.flags = OBJREF_CUSTOM;
1299 /* we do custom marshaling into a memory stream so that we know what
1300 * size to write into the OR_CUSTOM header */
1301 hr = CreateStreamOnHGlobal(NULL, TRUE, &pMarshalStream);
1302 if (hr)
1304 ERR("CreateStreamOnHGLOBAL failed with 0x%08lx\n", hr);
1305 goto cleanup;
1309 /* write the common OBJREF header to the stream */
1310 hr = IStream_Write(pStream, &objref, FIELD_OFFSET(OBJREF, u_objref), NULL);
1311 if (hr)
1313 ERR("Failed to write OBJREF header to stream, 0x%08lx\n", hr);
1314 goto cleanup;
1317 TRACE("Calling IMarshal::MarshalInterace\n");
1318 /* call helper object to do the actual marshaling */
1319 hr = IMarshal_MarshalInterface(pMarshal, pMarshalStream, riid, pUnk, dwDestContext,
1320 pvDestContext, mshlFlags);
1322 if (hr)
1324 ERR("Failed to marshal the interface %s, %lx\n", debugstr_guid(riid), hr);
1325 goto cleanup;
1328 if (objref.flags & OBJREF_CUSTOM)
1330 ULONG custom_header_size = FIELD_OFFSET(OBJREF, u_objref.u_custom.size) -
1331 FIELD_OFFSET(OBJREF, u_objref.u_custom);
1332 HGLOBAL hGlobal;
1333 LPVOID data;
1334 hr = GetHGlobalFromStream(pMarshalStream, &hGlobal);
1335 if (hr)
1337 ERR("Couldn't get HGLOBAL from stream\n");
1338 hr = E_UNEXPECTED;
1339 goto cleanup;
1341 objref.u_objref.u_custom.clsid = marshaler_clsid;
1342 objref.u_objref.u_custom.cbExtension = 0;
1343 objref.u_objref.u_custom.size = GlobalSize(hGlobal);
1344 /* write constant sized OR_CUSTOM data into stream */
1345 hr = IStream_Write(pStream, &objref.u_objref.u_custom,
1346 custom_header_size, NULL);
1347 if (hr)
1349 ERR("Failed to write OR_CUSTOM header to stream with 0x%08lx\n", hr);
1350 goto cleanup;
1353 data = GlobalLock(hGlobal);
1354 if (!data)
1356 ERR("GlobalLock failed\n");
1357 hr = E_UNEXPECTED;
1358 goto cleanup;
1360 /* write custom marshal data */
1361 hr = IStream_Write(pStream, data, objref.u_objref.u_custom.size, NULL);
1362 if (hr)
1364 ERR("Failed to write custom marshal data with 0x%08lx\n", hr);
1365 goto cleanup;
1367 GlobalUnlock(hGlobal);
1370 cleanup:
1371 if (pMarshalStream && (objref.flags & OBJREF_CUSTOM))
1372 IStream_Release(pMarshalStream);
1373 IMarshal_Release(pMarshal);
1375 TRACE("completed with hr 0x%08lx\n", hr);
1377 return hr;
1380 /***********************************************************************
1381 * CoUnmarshalInterface [OLE32.@]
1383 * Unmarshals an object from a stream by creating a proxy to the remote
1384 * object, if necessary.
1386 * PARAMS
1388 * pStream [I] Stream containing the marshaled object.
1389 * riid [I] Interface identifier of the object to create a proxy to.
1390 * ppv [O] Address where proxy will be stored.
1392 * RETURNS
1394 * Success: S_OK.
1395 * Failure: HRESULT code.
1397 * SEE ALSO
1398 * CoMarshalInterface().
1400 HRESULT WINAPI CoUnmarshalInterface(IStream *pStream, REFIID riid, LPVOID *ppv)
1402 HRESULT hr;
1403 LPMARSHAL pMarshal;
1404 IID iid;
1405 IUnknown *object;
1407 TRACE("(%p, %s, %p)\n", pStream, debugstr_guid(riid), ppv);
1409 hr = get_unmarshaler_from_stream(pStream, &pMarshal, &iid);
1410 if (hr != S_OK)
1411 return hr;
1413 /* call the helper object to do the actual unmarshaling */
1414 hr = IMarshal_UnmarshalInterface(pMarshal, pStream, &iid, (LPVOID*)&object);
1415 if (hr)
1416 ERR("IMarshal::UnmarshalInterface failed, 0x%08lx\n", hr);
1418 /* IID_NULL means use the interface ID of the marshaled object */
1419 if (!IsEqualIID(riid, &IID_NULL))
1420 iid = *riid;
1422 if (hr == S_OK)
1424 hr = IUnknown_QueryInterface(object, &iid, ppv);
1425 if (hr)
1426 ERR("Couldn't query for interface %s, hr = 0x%08lx\n",
1427 debugstr_guid(riid), hr);
1428 IUnknown_Release(object);
1431 IMarshal_Release(pMarshal);
1433 TRACE("completed with hr 0x%lx\n", hr);
1435 return hr;
1438 /***********************************************************************
1439 * CoReleaseMarshalData [OLE32.@]
1441 * Releases resources associated with an object that has been marshaled into
1442 * a stream.
1444 * PARAMS
1446 * pStream [I] The stream that the object has been marshaled into.
1448 * RETURNS
1449 * Success: S_OK.
1450 * Failure: HRESULT error code.
1452 * NOTES
1454 * Call this function to release resources associated with a normal or
1455 * table-weak marshal that will not be unmarshaled, and all table-strong
1456 * marshals when they are no longer needed.
1458 * SEE ALSO
1459 * CoMarshalInterface(), CoUnmarshalInterface().
1461 HRESULT WINAPI CoReleaseMarshalData(IStream *pStream)
1463 HRESULT hr;
1464 LPMARSHAL pMarshal;
1466 TRACE("(%p)\n", pStream);
1468 hr = get_unmarshaler_from_stream(pStream, &pMarshal, NULL);
1469 if (hr != S_OK)
1470 return hr;
1472 /* call the helper object to do the releasing of marshal data */
1473 hr = IMarshal_ReleaseMarshalData(pMarshal, pStream);
1474 if (hr)
1475 ERR("IMarshal::ReleaseMarshalData failed with error 0x%08lx\n", hr);
1477 IMarshal_Release(pMarshal);
1478 return hr;
1482 /***********************************************************************
1483 * CoMarshalInterThreadInterfaceInStream [OLE32.@]
1485 * Marshal an interface across threads in the same process.
1487 * PARAMS
1488 * riid [I] Identifier of the interface to be marshalled.
1489 * pUnk [I] Pointer to IUnknown-derived interface that will be marshalled.
1490 * ppStm [O] Pointer to IStream object that is created and then used to store the marshalled inteface.
1492 * RETURNS
1493 * Success: S_OK
1494 * Failure: E_OUTOFMEMORY and other COM error codes
1496 * SEE ALSO
1497 * CoMarshalInterface(), CoUnmarshalInterface() and CoGetInterfaceAndReleaseStream()
1499 HRESULT WINAPI CoMarshalInterThreadInterfaceInStream(
1500 REFIID riid, LPUNKNOWN pUnk, LPSTREAM * ppStm)
1502 ULARGE_INTEGER xpos;
1503 LARGE_INTEGER seekto;
1504 HRESULT hres;
1506 TRACE("(%s, %p, %p)\n",debugstr_guid(riid), pUnk, ppStm);
1508 hres = CreateStreamOnHGlobal(0, TRUE, ppStm);
1509 if (FAILED(hres)) return hres;
1510 hres = CoMarshalInterface(*ppStm, riid, pUnk, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1512 /* FIXME: is this needed? */
1513 memset(&seekto,0,sizeof(seekto));
1514 IStream_Seek(*ppStm,seekto,SEEK_SET,&xpos);
1516 return hres;
1519 /***********************************************************************
1520 * CoGetInterfaceAndReleaseStream [OLE32.@]
1522 * Unmarshalls an inteface from a stream and then releases the stream.
1524 * PARAMS
1525 * pStm [I] Stream that contains the marshalled inteface.
1526 * riid [I] Interface identifier of the object to unmarshall.
1527 * ppv [O] Address of pointer where the requested interface object will be stored.
1529 * RETURNS
1530 * Success: S_OK
1531 * Failure: A COM error code
1533 * SEE ALSO
1534 * CoMarshalInterThreadInterfaceInStream() and CoUnmarshalInteface()
1536 HRESULT WINAPI CoGetInterfaceAndReleaseStream(LPSTREAM pStm, REFIID riid,
1537 LPVOID *ppv)
1539 HRESULT hres;
1541 TRACE("(%p, %s, %p)\n", pStm, debugstr_guid(riid), ppv);
1543 hres = CoUnmarshalInterface(pStm, riid, ppv);
1544 IStream_Release(pStm);
1545 return hres;
1548 static HRESULT WINAPI StdMarshalCF_QueryInterface(LPCLASSFACTORY iface,
1549 REFIID riid, LPVOID *ppv)
1551 *ppv = NULL;
1552 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory))
1554 *ppv = (LPVOID)iface;
1555 return S_OK;
1557 return E_NOINTERFACE;
1560 static ULONG WINAPI StdMarshalCF_AddRef(LPCLASSFACTORY iface)
1562 return 2; /* non-heap based object */
1565 static ULONG WINAPI StdMarshalCF_Release(LPCLASSFACTORY iface)
1567 return 1; /* non-heap based object */
1570 static HRESULT WINAPI StdMarshalCF_CreateInstance(LPCLASSFACTORY iface,
1571 LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv)
1573 if (IsEqualIID(riid,&IID_IMarshal))
1574 return StdMarshalImpl_Construct(riid, ppv);
1576 FIXME("(%s), not supported.\n",debugstr_guid(riid));
1577 return E_NOINTERFACE;
1580 static HRESULT WINAPI StdMarshalCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
1582 FIXME("(%d), stub!\n",fLock);
1583 return S_OK;
1586 static IClassFactoryVtbl StdMarshalCFVtbl =
1588 StdMarshalCF_QueryInterface,
1589 StdMarshalCF_AddRef,
1590 StdMarshalCF_Release,
1591 StdMarshalCF_CreateInstance,
1592 StdMarshalCF_LockServer
1594 static IClassFactoryVtbl *StdMarshalCF = &StdMarshalCFVtbl;
1596 HRESULT MARSHAL_GetStandardMarshalCF(LPVOID *ppv)
1598 *ppv = &StdMarshalCF;
1599 return S_OK;
1602 /***********************************************************************
1603 * CoMarshalHresult [OLE32.@]
1605 * Marshals an HRESULT value into a stream.
1607 * PARAMS
1608 * pStm [I] Stream that hresult will be marshalled into.
1609 * hresult [I] HRESULT to be marshalled.
1611 * RETURNS
1612 * Success: S_OK
1613 * Failure: A COM error code
1615 * SEE ALSO
1616 * CoUnmarshalHresult().
1618 HRESULT WINAPI CoMarshalHresult(LPSTREAM pStm, HRESULT hresult)
1620 return IStream_Write(pStm, &hresult, sizeof(hresult), NULL);
1623 /***********************************************************************
1624 * CoUnmarshalHresult [OLE32.@]
1626 * Unmarshals an HRESULT value from a stream.
1628 * PARAMS
1629 * pStm [I] Stream that hresult will be unmarshalled from.
1630 * phresult [I] Pointer to HRESULT where the value will be unmarshalled to.
1632 * RETURNS
1633 * Success: S_OK
1634 * Failure: A COM error code
1636 * SEE ALSO
1637 * CoMarshalHresult().
1639 HRESULT WINAPI CoUnmarshalHresult(LPSTREAM pStm, HRESULT * phresult)
1641 return IStream_Read(pStm, phresult, sizeof(*phresult), NULL);