- Use InterlockedIncrement for the ipid counter instead of a critical
[wine/multimedia.git] / dlls / ole32 / marshal.c
blobca050700479b55d8bc40027408cb8f2b001316b3
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 and sets stdobjref->oid when stdobjref->oid == 0 */
89 HRESULT register_ifstub(APARTMENT *apt, STDOBJREF *stdobjref, REFIID riid, IUnknown *obj, MSHLFLAGS mshlflags)
91 struct stub_manager *manager = NULL;
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 /* mid->oid of zero means create a new stub manager */
122 if (stdobjref->oid && (manager = get_stub_manager(apt, stdobjref->oid)))
124 TRACE("registering new ifstub on pre-existing manager\n");
126 else
128 TRACE("constructing new stub manager\n");
130 manager = new_stub_manager(apt, obj);
131 if (!manager)
132 return E_OUTOFMEMORY;
134 stdobjref->oid = manager->oid;
137 tablemarshal = ((mshlflags & MSHLFLAGS_TABLESTRONG) || (mshlflags & MSHLFLAGS_TABLEWEAK));
139 ifstub = stub_manager_new_ifstub(manager, stub, obj, riid, tablemarshal);
140 if (!ifstub)
142 IRpcStubBuffer_Release(stub);
143 stub_manager_int_release(manager);
144 /* FIXME: should we do another release to completely destroy the
145 * stub manager? */
146 return E_OUTOFMEMORY;
149 if (!tablemarshal)
151 stdobjref->cPublicRefs = NORMALEXTREFS;
152 stub_manager_ext_addref(manager, stdobjref->cPublicRefs);
154 else
156 stdobjref->cPublicRefs = 0;
157 if (mshlflags & MSHLFLAGS_TABLESTRONG)
158 stub_manager_ext_addref(manager, 1);
161 stdobjref->ipid = ifstub->ipid;
163 stub_manager_int_release(manager);
164 return S_OK;
169 /* Client-side identity of the server object */
171 static HRESULT proxy_manager_get_remunknown(struct proxy_manager * This, IRemUnknown **remunk);
172 static void proxy_manager_destroy(struct proxy_manager * This);
173 static HRESULT proxy_manager_find_ifproxy(struct proxy_manager * This, REFIID riid, struct ifproxy ** ifproxy_found);
175 static HRESULT WINAPI ClientIdentity_QueryInterface(IInternalUnknown * iface, REFIID riid, void ** ppv)
177 struct proxy_manager * This = (struct proxy_manager *)iface;
178 HRESULT hr;
179 struct ifproxy * ifproxy;
181 TRACE("%s\n", debugstr_guid(riid));
183 if (IsEqualIID(riid, &IID_IUnknown) ||
184 IsEqualIID(riid, &IID_IInternalUnknown))
186 *ppv = (void *)iface;
187 IInternalUnknown_AddRef(iface);
188 return S_OK;
191 hr = proxy_manager_find_ifproxy(This, riid, &ifproxy);
192 if (hr == S_OK)
194 *ppv = ifproxy->iface;
195 IUnknown_AddRef((IUnknown *)*ppv);
196 return S_OK;
199 FIXME("interface not found %s\n", debugstr_guid(riid));
201 /* FIXME: call IRemUnknown::RemQueryInterface */
203 *ppv = NULL;
204 return E_NOINTERFACE;
207 static ULONG WINAPI ClientIdentity_AddRef(IInternalUnknown * iface)
209 struct proxy_manager * This = (struct proxy_manager *)iface;
210 TRACE("%p - before %ld\n", iface, This->refs);
211 return InterlockedIncrement(&This->refs);
214 static ULONG WINAPI ClientIdentity_Release(IInternalUnknown * iface)
216 struct proxy_manager * This = (struct proxy_manager *)iface;
217 ULONG refs = InterlockedDecrement(&This->refs);
218 TRACE("%p - after %ld\n", iface, refs);
219 if (!refs)
220 proxy_manager_destroy(This);
221 return refs;
224 static HRESULT WINAPI ClientIdentity_QueryInternalInterface(IInternalUnknown * iface, REFIID riid, void ** ppv)
226 FIXME("(%s, %p): stub!\n", debugstr_guid(riid), ppv);
227 return E_NOINTERFACE;
230 static const IInternalUnknownVtbl ClientIdentity_Vtbl =
232 ClientIdentity_QueryInterface,
233 ClientIdentity_AddRef,
234 ClientIdentity_Release,
235 ClientIdentity_QueryInternalInterface
238 static HRESULT ifproxy_get_public_ref(struct ifproxy * This)
240 /* FIXME: as this call could possibly be going over the network, we
241 * are going to spend a long time in this CS. We might want to replace
242 * this with a mutex */
243 EnterCriticalSection(&This->parent->cs);
244 if (This->refs == 0)
246 APARTMENT * apt;
248 TRACE("getting public ref for ifproxy %p\n", This);
250 /* FIXME: call IRemUnknown::RemAddRef if necessary */
252 /* FIXME: this is a hack around not yet implemented IRemUnknown */
253 if ((apt = COM_ApartmentFromOXID(This->parent->oxid, TRUE)))
255 struct stub_manager * stubmgr;
256 if ((stubmgr = get_stub_manager(apt, This->parent->oid)))
258 stub_manager_ext_addref(stubmgr, 1);
259 This->refs += 1;
261 stub_manager_int_release(stubmgr);
264 COM_ApartmentRelease(apt);
266 else
267 FIXME("Need to implement IRemUnknown for inter-process table marshaling\n");
269 LeaveCriticalSection(&This->parent->cs);
271 return S_OK;
274 static HRESULT ifproxy_release_public_refs(struct ifproxy * This)
276 HRESULT hr = S_OK;
278 /* FIXME: as this call could possibly be going over the network, we
279 * are going to spend a long time in this CS. We might want to replace
280 * this with a mutex */
281 EnterCriticalSection(&This->parent->cs);
282 if (This->refs > 0)
284 IRemUnknown *remunk = NULL;
286 TRACE("releasing %ld refs\n", This->refs);
288 hr = proxy_manager_get_remunknown(This->parent, &remunk);
289 if (hr == S_OK)
291 REMINTERFACEREF rif;
292 rif.ipid = This->ipid;
293 rif.cPublicRefs = This->refs;
294 rif.cPrivateRefs = 0;
295 hr = IRemUnknown_RemRelease(remunk, 1, &rif);
296 if (hr == S_OK)
297 This->refs = 0;
298 else
299 ERR("IRemUnknown_RemRelease failed with error 0x%08lx\n", hr);
302 LeaveCriticalSection(&This->parent->cs);
304 return hr;
307 static void ifproxy_disconnect(struct ifproxy * This)
309 ifproxy_release_public_refs(This);
310 IRpcProxyBuffer_Disconnect(This->proxy);
313 static void ifproxy_destroy(struct ifproxy * This)
315 TRACE("%p\n", This);
317 /* release public references to this object so that the stub can know
318 * when to destroy itself */
319 ifproxy_release_public_refs(This);
321 list_remove(&This->entry);
323 /* note: we don't call Release for This->proxy because its lifetime is
324 * controlled by the return value from ClientIdentity_Release, which this
325 * function is always called from */
327 HeapFree(GetProcessHeap(), 0, This);
330 static HRESULT proxy_manager_construct(
331 APARTMENT * apt, ULONG sorflags, OXID oxid, OID oid,
332 IRpcChannelBuffer * channel, struct proxy_manager ** proxy_manager)
334 struct proxy_manager * This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
335 if (!This) return E_OUTOFMEMORY;
337 This->lpVtbl = &ClientIdentity_Vtbl;
339 list_init(&This->entry);
340 list_init(&This->interfaces);
342 InitializeCriticalSection(&This->cs);
344 /* the apartment the object was unmarshaled into */
345 This->parent = apt;
347 /* the source apartment and id of the object */
348 This->oxid = oxid;
349 This->oid = oid;
351 This->refs = 1;
353 /* the DCOM draft specification states that the SORF_NOPING flag is
354 * proxy manager specific, not ifproxy specific, so this implies that we
355 * should store the STDOBJREF flags in the proxy manager. */
356 This->sorflags = sorflags;
358 This->chan = channel; /* FIXME: we should take the binding strings and construct the channel in this function */
360 /* we create the IRemUnknown proxy on demand */
361 This->remunk = NULL;
363 EnterCriticalSection(&apt->cs);
364 list_add_head(&apt->proxies, &This->entry);
365 LeaveCriticalSection(&apt->cs);
367 TRACE("%p created for OXID %s, OID %s\n", This,
368 wine_dbgstr_longlong(oxid), wine_dbgstr_longlong(oid));
370 *proxy_manager = This;
371 return S_OK;
374 static HRESULT proxy_manager_create_ifproxy(
375 struct proxy_manager * This, IPID ipid, REFIID riid, ULONG cPublicRefs,
376 struct ifproxy ** iif_out)
378 HRESULT hr;
379 IPSFactoryBuffer * psfb;
380 struct ifproxy * ifproxy = HeapAlloc(GetProcessHeap(), 0, sizeof(*ifproxy));
381 if (!ifproxy) return E_OUTOFMEMORY;
383 list_init(&ifproxy->entry);
385 ifproxy->parent = This;
386 ifproxy->ipid = ipid;
387 ifproxy->iid = *riid;
388 ifproxy->refs = cPublicRefs;
389 ifproxy->proxy = NULL;
391 hr = get_facbuf_for_iid(riid, &psfb);
392 if (hr == S_OK)
394 /* important note: the outer unknown is set to the proxy manager.
395 * This ensures the COM identity rules are not violated, by having a
396 * one-to-one mapping of objects on the proxy side to objects on the
397 * stub side, no matter which interface you view the object through */
398 hr = IPSFactoryBuffer_CreateProxy(psfb, (IUnknown *)&This->lpVtbl, riid,
399 &ifproxy->proxy, &ifproxy->iface);
400 IPSFactoryBuffer_Release(psfb);
401 if (hr != S_OK)
402 ERR("Could not create proxy for interface %s, error 0x%08lx\n",
403 debugstr_guid(riid), hr);
405 else
406 ERR("Could not get IPSFactoryBuffer for interface %s, error 0x%08lx\n",
407 debugstr_guid(riid), hr);
409 if (hr == S_OK)
410 hr = IRpcProxyBuffer_Connect(ifproxy->proxy, This->chan);
412 /* get at least one external reference to the object to keep it alive */
413 if (hr == S_OK)
414 hr = ifproxy_get_public_ref(ifproxy);
416 if (hr == S_OK)
418 EnterCriticalSection(&This->cs);
419 list_add_tail(&This->interfaces, &ifproxy->entry);
420 LeaveCriticalSection(&This->cs);
422 *iif_out = ifproxy;
423 TRACE("ifproxy %p created for IPID %s, interface %s with %lu public refs\n",
424 ifproxy, debugstr_guid(&ipid), debugstr_guid(riid), cPublicRefs);
426 else
427 ifproxy_destroy(ifproxy);
429 return hr;
432 static HRESULT proxy_manager_find_ifproxy(struct proxy_manager * This, REFIID riid, struct ifproxy ** ifproxy_found)
434 HRESULT hr = E_NOINTERFACE; /* assume not found */
435 struct list * cursor;
437 EnterCriticalSection(&This->cs);
438 LIST_FOR_EACH(cursor, &This->interfaces)
440 struct ifproxy * ifproxy = LIST_ENTRY(cursor, struct ifproxy, entry);
441 if (IsEqualIID(riid, &ifproxy->iid))
443 *ifproxy_found = ifproxy;
444 hr = S_OK;
445 break;
448 LeaveCriticalSection(&This->cs);
450 return hr;
453 static void proxy_manager_disconnect(struct proxy_manager * This)
455 struct list * cursor;
457 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This->oxid),
458 wine_dbgstr_longlong(This->oid));
460 EnterCriticalSection(&This->cs);
462 LIST_FOR_EACH(cursor, &This->interfaces)
464 struct ifproxy * ifproxy = LIST_ENTRY(cursor, struct ifproxy, entry);
465 ifproxy_disconnect(ifproxy);
468 /* apartment is being destroyed so don't keep a pointer around to it */
469 This->parent = NULL;
471 /* FIXME: will this still be necessary if/when we use a real RPC
472 * channel? */
473 IRpcChannelBuffer_Release(This->chan);
474 This->chan = NULL;
476 LeaveCriticalSection(&This->cs);
479 static HRESULT proxy_manager_get_remunknown(struct proxy_manager * This, IRemUnknown **remunk)
481 HRESULT hr = S_OK;
483 /* we don't want to try and unmarshal or use IRemUnknown if we don't want
484 * lifetime management */
485 if (This->sorflags & SORFP_NOLIFETIMEMGMT)
486 return S_FALSE;
488 EnterCriticalSection(&This->cs);
489 if (This->remunk)
490 /* already created - return existing object */
491 *remunk = This->remunk;
492 else if (!This->parent)
493 /* disconnected - we can't create IRemUnknown */
494 hr = S_FALSE;
495 else
497 STDOBJREF stdobjref;
498 /* Don't want IRemUnknown lifetime management as this is IRemUnknown!
499 * We also don't care about whether or not the stub is still alive */
500 stdobjref.flags = SORFP_NOLIFETIMEMGMT | SORF_NOPING;
501 stdobjref.cPublicRefs = 1;
502 /* oxid of destination object */
503 stdobjref.oxid = This->oxid;
504 /* FIXME: what should be used for the oid? The DCOM draft doesn't say */
505 stdobjref.oid = (OID)-1;
506 /* FIXME: this is a hack around not having an OXID resolver yet -
507 * the OXID resolver should give us the IPID of the IRemUnknown
508 * interface */
509 stdobjref.ipid.Data1 = 0xffffffff;
510 stdobjref.ipid.Data2 = 0xffff;
511 stdobjref.ipid.Data3 = 0xffff;
512 assert(sizeof(stdobjref.ipid.Data4) == sizeof(stdobjref.oxid));
513 memcpy(&stdobjref.ipid.Data4, &stdobjref.oxid, sizeof(OXID));
515 /* do the unmarshal */
516 hr = unmarshal_object(&stdobjref, This->parent, &IID_IRemUnknown, (void**)&This->remunk);
517 if (hr == S_OK)
518 *remunk = This->remunk;
520 LeaveCriticalSection(&This->cs);
522 TRACE("got IRemUnknown* pointer %p, hr = 0x%08lx\n", *remunk, hr);
524 return hr;
527 /* destroys a proxy manager, freeing the memory it used.
528 * Note: this function should not be called from a list iteration in the
529 * apartment, due to the fact that it removes itself from the apartment and
530 * it could add a proxy to IRemUnknown into the apartment. */
531 static void proxy_manager_destroy(struct proxy_manager * This)
533 struct list * cursor;
535 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This->oxid),
536 wine_dbgstr_longlong(This->oid));
538 if (This->parent)
540 EnterCriticalSection(&This->parent->cs);
542 /* remove ourself from the list of proxy objects in the apartment */
543 LIST_FOR_EACH(cursor, &This->parent->proxies)
545 if (cursor == &This->entry)
547 list_remove(&This->entry);
548 break;
552 LeaveCriticalSection(&This->parent->cs);
555 /* destroy all of the interface proxies */
556 while ((cursor = list_head(&This->interfaces)))
558 struct ifproxy * ifproxy = LIST_ENTRY(cursor, struct ifproxy, entry);
559 ifproxy_destroy(ifproxy);
562 if (This->remunk) IRemUnknown_Release(This->remunk);
563 if (This->chan) IRpcChannelBuffer_Release(This->chan);
565 DeleteCriticalSection(&This->cs);
567 HeapFree(GetProcessHeap(), 0, This);
570 /* finds the proxy manager corresponding to a given OXID and OID that has
571 * been unmarshaled in the specified apartment. The caller must release the
572 * reference to the proxy_manager when the object is no longer used. */
573 static BOOL find_proxy_manager(APARTMENT * apt, OXID oxid, OID oid, struct proxy_manager ** proxy_found)
575 BOOL found = FALSE;
576 struct list * cursor;
578 EnterCriticalSection(&apt->cs);
579 LIST_FOR_EACH(cursor, &apt->proxies)
581 struct proxy_manager * proxy = LIST_ENTRY(cursor, struct proxy_manager, entry);
582 if ((oxid == proxy->oxid) && (oid == proxy->oid))
584 *proxy_found = proxy;
585 ClientIdentity_AddRef((IInternalUnknown *)&proxy->lpVtbl);
586 found = TRUE;
587 break;
590 LeaveCriticalSection(&apt->cs);
591 return found;
594 HRESULT MARSHAL_Disconnect_Proxies(APARTMENT *apt)
596 struct list * cursor;
598 EnterCriticalSection(&apt->cs);
599 LIST_FOR_EACH(cursor, &apt->proxies)
601 struct proxy_manager * proxy = LIST_ENTRY(cursor, struct proxy_manager, entry);
602 proxy_manager_disconnect(proxy);
604 LeaveCriticalSection(&apt->cs);
606 return S_OK;
609 /********************** StdMarshal implementation ****************************/
610 typedef struct _StdMarshalImpl {
611 IMarshalVtbl *lpvtbl;
612 DWORD ref;
614 IID iid;
615 DWORD dwDestContext;
616 LPVOID pvDestContext;
617 DWORD mshlflags;
618 } StdMarshalImpl;
620 static HRESULT WINAPI
621 StdMarshalImpl_QueryInterface(LPMARSHAL iface,REFIID riid,LPVOID *ppv) {
622 *ppv = NULL;
623 if (IsEqualIID(&IID_IUnknown,riid) || IsEqualIID(&IID_IMarshal,riid)) {
624 *ppv = iface;
625 IUnknown_AddRef(iface);
626 return S_OK;
628 FIXME("No interface for %s.\n",debugstr_guid(riid));
629 return E_NOINTERFACE;
632 static ULONG WINAPI
633 StdMarshalImpl_AddRef(LPMARSHAL iface) {
634 StdMarshalImpl *This = (StdMarshalImpl *)iface;
635 return InterlockedIncrement(&This->ref);
638 static ULONG WINAPI
639 StdMarshalImpl_Release(LPMARSHAL iface) {
640 StdMarshalImpl *This = (StdMarshalImpl *)iface;
641 ULONG ref = InterlockedDecrement(&This->ref);
643 if (!ref) HeapFree(GetProcessHeap(),0,This);
644 return ref;
647 static HRESULT WINAPI
648 StdMarshalImpl_GetUnmarshalClass(
649 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
650 void* pvDestContext, DWORD mshlflags, CLSID* pCid
652 memcpy(pCid,&CLSID_DfMarshal,sizeof(CLSID_DfMarshal));
653 return S_OK;
656 static HRESULT WINAPI
657 StdMarshalImpl_GetMarshalSizeMax(
658 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
659 void* pvDestContext, DWORD mshlflags, DWORD* pSize)
661 *pSize = sizeof(STDOBJREF);
662 return S_OK;
665 static HRESULT WINAPI
666 StdMarshalImpl_MarshalInterface(
667 LPMARSHAL iface, IStream *pStm,REFIID riid, void* pv, DWORD dwDestContext,
668 void* pvDestContext, DWORD mshlflags
670 STDOBJREF stdobjref;
671 IUnknown *pUnk;
672 ULONG res;
673 HRESULT hres;
674 struct stub_manager *manager;
675 APARTMENT *apt = COM_CurrentApt();
677 TRACE("(...,%s,...)\n",debugstr_guid(riid));
679 if (!apt)
681 ERR("Apartment not initialized\n");
682 return CO_E_NOTINITIALIZED;
685 start_apartment_listener_thread(); /* just to be sure we have one running. */
686 start_apartment_remote_unknown();
688 IUnknown_QueryInterface((LPUNKNOWN)pv, riid, (LPVOID*)&pUnk);
690 if ((manager = get_stub_manager_from_object(apt, pUnk)))
692 stdobjref.oid = manager->oid;
693 stub_manager_int_release(manager);
695 else
697 stdobjref.oid = 0; /* will be set by register_ifstub */
700 hres = register_ifstub(apt, &stdobjref, riid, pUnk, mshlflags);
702 IUnknown_Release(pUnk);
704 if (hres)
706 FIXME("Failed to create ifstub, hres=0x%lx\n", hres);
707 return hres;
710 hres = IStream_Write(pStm, &stdobjref, sizeof(stdobjref), &res);
711 if (hres) return hres;
713 return S_OK;
716 /* helper for StdMarshalImpl_UnmarshalInterface - does the unmarshaling with
717 * no questions asked about the rules surrounding same-apartment unmarshals
718 * and table marshaling */
719 static HRESULT unmarshal_object(const STDOBJREF *stdobjref, APARTMENT *apt, REFIID riid, void **object)
721 struct proxy_manager *proxy_manager = NULL;
722 HRESULT hr = S_OK;
724 assert(apt);
726 TRACE("stdobjref:\n\tflags = %04lx\n\tcPublicRefs = %ld\n\toxid = %s\n\toid = %s\n\tipid = %s\n",
727 stdobjref->flags, stdobjref->cPublicRefs,
728 wine_dbgstr_longlong(stdobjref->oxid),
729 wine_dbgstr_longlong(stdobjref->oid),
730 debugstr_guid(&stdobjref->ipid));
732 /* create an a new proxy manager if one doesn't already exist for the
733 * object */
734 if (!find_proxy_manager(apt, stdobjref->oxid, stdobjref->oid, &proxy_manager))
736 IRpcChannelBuffer *chanbuf;
737 wine_marshal_id mid;
739 mid.oxid = stdobjref->oxid;
740 mid.oid = stdobjref->oid;
741 mid.ipid = stdobjref->ipid;
743 hr = PIPE_GetNewPipeBuf(&mid,&chanbuf);
744 if (hr == S_OK)
745 hr = proxy_manager_construct(apt, stdobjref->flags,
746 stdobjref->oxid, stdobjref->oid,
747 chanbuf, &proxy_manager);
750 if (hr == S_OK)
752 /* the IUnknown interface is special because it does not have an
753 * ifproxy associated with it. we simply return the controlling
754 * IUnknown of the proxy manager. */
755 if (IsEqualIID(riid, &IID_IUnknown))
757 ClientIdentity_AddRef((IInternalUnknown*)&proxy_manager->lpVtbl);
758 *object = (LPVOID)(&proxy_manager->lpVtbl);
760 else
762 struct ifproxy * ifproxy;
763 hr = proxy_manager_find_ifproxy(proxy_manager, riid, &ifproxy);
764 if (hr == E_NOINTERFACE)
765 hr = proxy_manager_create_ifproxy(proxy_manager, stdobjref->ipid,
766 riid, stdobjref->cPublicRefs,
767 &ifproxy);
769 if (hr == S_OK)
771 /* FIXME: push this AddRef inside proxy_manager_find_ifproxy/create_ifproxy? */
772 ClientIdentity_AddRef((IInternalUnknown*)&proxy_manager->lpVtbl);
773 *object = ifproxy->iface;
778 /* release our reference to the proxy manager - the client/apartment
779 * will hold on to the remaining reference for us */
780 if (proxy_manager) ClientIdentity_Release((IInternalUnknown*)&proxy_manager->lpVtbl);
782 return hr;
785 static HRESULT WINAPI
786 StdMarshalImpl_UnmarshalInterface(LPMARSHAL iface, IStream *pStm, REFIID riid, void **ppv)
788 struct stub_manager *stubmgr;
789 STDOBJREF stdobjref;
790 ULONG res;
791 HRESULT hres;
792 APARTMENT *apt = COM_CurrentApt();
793 APARTMENT *stub_apt;
795 TRACE("(...,%s,....)\n",debugstr_guid(riid));
797 /* we need an apartment to unmarshal into */
798 if (!apt)
800 ERR("Apartment not initialized\n");
801 return CO_E_NOTINITIALIZED;
804 /* read STDOBJREF from wire */
805 hres = IStream_Read(pStm, &stdobjref, sizeof(stdobjref), &res);
806 if (hres) return hres;
808 /* check if we're marshalling back to ourselves */
809 if ((apt->oxid == stdobjref.oxid) && (stubmgr = get_stub_manager(apt, stdobjref.oid)))
811 TRACE("Unmarshalling object marshalled in same apartment for iid %s, "
812 "returning original object %p\n", debugstr_guid(riid), stubmgr->object);
814 hres = IUnknown_QueryInterface(stubmgr->object, riid, ppv);
816 /* unref the ifstub. FIXME: only do this on success? */
817 if (!stub_manager_is_table_marshaled(stubmgr, &stdobjref.ipid))
818 stub_manager_ext_release(stubmgr, 1);
820 stub_manager_int_release(stubmgr);
821 return hres;
824 /* notify stub manager about unmarshal if process-local object.
825 * note: if the oxid is not found then we and native will quite happily
826 * ignore table marshaling and normal marshaling rules regarding number of
827 * unmarshals, etc, but if you abuse these rules then your proxy could end
828 * up returning RPC_E_DISCONNECTED. */
829 if ((stub_apt = COM_ApartmentFromOXID(stdobjref.oxid, TRUE)))
831 if ((stubmgr = get_stub_manager(stub_apt, stdobjref.oid)))
833 if (!stub_manager_notify_unmarshal(stubmgr, &stdobjref.ipid))
834 hres = CO_E_OBJNOTCONNECTED;
836 stub_manager_int_release(stubmgr);
838 else
840 WARN("Couldn't find object for OXID %s, OID %s, assuming disconnected\n",
841 wine_dbgstr_longlong(stdobjref.oxid),
842 wine_dbgstr_longlong(stdobjref.oid));
843 hres = CO_E_OBJNOTCONNECTED;
846 COM_ApartmentRelease(stub_apt);
848 else
849 TRACE("Treating unmarshal from OXID %s as inter-process\n",
850 wine_dbgstr_longlong(stdobjref.oxid));
852 if (hres == S_OK)
853 hres = unmarshal_object(&stdobjref, apt, riid, ppv);
855 if (hres) WARN("Failed with error 0x%08lx\n", hres);
856 else TRACE("Successfully created proxy %p\n", *ppv);
858 return hres;
861 static HRESULT WINAPI
862 StdMarshalImpl_ReleaseMarshalData(LPMARSHAL iface, IStream *pStm) {
863 STDOBJREF stdobjref;
864 ULONG res;
865 HRESULT hres;
866 struct stub_manager *stubmgr;
867 APARTMENT *apt;
869 TRACE("iface=%p, pStm=%p\n", iface, pStm);
871 hres = IStream_Read(pStm, &stdobjref, sizeof(stdobjref), &res);
872 if (hres) return hres;
874 if (!(apt = COM_ApartmentFromOXID(stdobjref.oxid, TRUE)))
876 WARN("Could not map OXID %s to apartment object\n",
877 wine_dbgstr_longlong(stdobjref.oxid));
878 return RPC_E_INVALID_OBJREF;
881 if (!(stubmgr = get_stub_manager(apt, stdobjref.oid)))
883 ERR("could not map MID to stub manager, oxid=%s, oid=%s\n",
884 wine_dbgstr_longlong(stdobjref.oxid), wine_dbgstr_longlong(stdobjref.oid));
885 return RPC_E_INVALID_OBJREF;
888 /* FIXME: don't release if table-weak and already unmarshaled an object */
889 /* FIXME: this should also depend on stdobjref.cPublicRefs */
890 stub_manager_ext_release(stubmgr, 1);
892 stub_manager_int_release(stubmgr);
893 COM_ApartmentRelease(apt);
895 return S_OK;
898 static HRESULT WINAPI
899 StdMarshalImpl_DisconnectObject(LPMARSHAL iface, DWORD dwReserved) {
900 FIXME("(), stub!\n");
901 return S_OK;
904 IMarshalVtbl stdmvtbl = {
905 StdMarshalImpl_QueryInterface,
906 StdMarshalImpl_AddRef,
907 StdMarshalImpl_Release,
908 StdMarshalImpl_GetUnmarshalClass,
909 StdMarshalImpl_GetMarshalSizeMax,
910 StdMarshalImpl_MarshalInterface,
911 StdMarshalImpl_UnmarshalInterface,
912 StdMarshalImpl_ReleaseMarshalData,
913 StdMarshalImpl_DisconnectObject
916 static HRESULT StdMarshalImpl_Construct(REFIID riid, void** ppvObject)
918 StdMarshalImpl * pStdMarshal =
919 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(StdMarshalImpl));
920 if (!pStdMarshal)
921 return E_OUTOFMEMORY;
922 pStdMarshal->lpvtbl = &stdmvtbl;
923 pStdMarshal->ref = 0;
924 return IMarshal_QueryInterface((IMarshal*)pStdMarshal, riid, ppvObject);
927 /***********************************************************************
928 * CoGetStandardMarshal [OLE32.@]
930 * Gets or creates a standard marshal object.
932 * PARAMS
933 * riid [I] Interface identifier of the pUnk object.
934 * pUnk [I] Optional. Object to get the marshal object for.
935 * dwDestContext [I] Destination. Used to enable or disable optimizations.
936 * pvDestContext [I] Reserved. Must be NULL.
937 * mshlflags [I] Flags affecting the marshaling process.
938 * ppMarshal [O] Address where marshal object will be stored.
940 * RETURNS
941 * Success: S_OK.
942 * Failure: HRESULT code.
944 * NOTES
946 * The function retrieves the IMarshal object associated with an object if
947 * that object is currently an active stub, otherwise a new marshal object is
948 * created.
950 HRESULT WINAPI CoGetStandardMarshal(REFIID riid, IUnknown *pUnk,
951 DWORD dwDestContext, LPVOID pvDestContext,
952 DWORD mshlflags, LPMARSHAL *ppMarshal)
954 StdMarshalImpl *dm;
956 if (pUnk == NULL) {
957 FIXME("(%s,NULL,%lx,%p,%lx,%p), unimplemented yet.\n",
958 debugstr_guid(riid),dwDestContext,pvDestContext,mshlflags,ppMarshal
960 return E_FAIL;
962 TRACE("(%s,%p,%lx,%p,%lx,%p)\n",
963 debugstr_guid(riid),pUnk,dwDestContext,pvDestContext,mshlflags,ppMarshal
965 *ppMarshal = HeapAlloc(GetProcessHeap(),0,sizeof(StdMarshalImpl));
966 dm = (StdMarshalImpl*) *ppMarshal;
967 if (!dm) return E_FAIL;
968 dm->lpvtbl = &stdmvtbl;
969 dm->ref = 1;
971 memcpy(&dm->iid,riid,sizeof(dm->iid));
972 dm->dwDestContext = dwDestContext;
973 dm->pvDestContext = pvDestContext;
974 dm->mshlflags = mshlflags;
975 return S_OK;
978 /***********************************************************************
979 * get_marshaler [internal]
981 * Retrieves an IMarshal interface for an object.
983 static HRESULT get_marshaler(REFIID riid, IUnknown *pUnk, DWORD dwDestContext,
984 void *pvDestContext, DWORD mshlFlags,
985 LPMARSHAL *pMarshal)
987 HRESULT hr;
989 if (!pUnk)
990 return E_POINTER;
991 hr = IUnknown_QueryInterface(pUnk, &IID_IMarshal, (LPVOID*)pMarshal);
992 if (hr)
993 hr = CoGetStandardMarshal(riid, pUnk, dwDestContext, pvDestContext,
994 mshlFlags, pMarshal);
995 return hr;
998 /***********************************************************************
999 * get_unmarshaler_from_stream [internal]
1001 * Creates an IMarshal* object according to the data marshaled to the stream.
1002 * The function leaves the stream pointer at the start of the data written
1003 * to the stream by the IMarshal* object.
1005 static HRESULT get_unmarshaler_from_stream(IStream *stream, IMarshal **marshal)
1007 HRESULT hr;
1008 ULONG res;
1009 OBJREF objref;
1011 /* read common OBJREF header */
1012 hr = IStream_Read(stream, &objref, FIELD_OFFSET(OBJREF, u_objref), &res);
1013 if (hr || (res != FIELD_OFFSET(OBJREF, u_objref)))
1015 ERR("Failed to read common OBJREF header, 0x%08lx\n", hr);
1016 return STG_E_READFAULT;
1019 /* sanity check on header */
1020 if (objref.signature != OBJREF_SIGNATURE)
1022 ERR("Bad OBJREF signature 0x%08lx\n", objref.signature);
1023 return RPC_E_INVALID_OBJREF;
1026 /* FIXME: handler marshaling */
1027 if (objref.flags & OBJREF_STANDARD)
1029 TRACE("Using standard unmarshaling\n");
1030 hr = StdMarshalImpl_Construct(&IID_IMarshal, (LPVOID*)marshal);
1032 else if (objref.flags & OBJREF_CUSTOM)
1034 ULONG custom_header_size = FIELD_OFFSET(OBJREF, u_objref.u_custom.size) -
1035 FIELD_OFFSET(OBJREF, u_objref.u_custom);
1036 TRACE("Using custom unmarshaling\n");
1037 /* read constant sized OR_CUSTOM data from stream */
1038 hr = IStream_Read(stream, &objref.u_objref.u_custom,
1039 custom_header_size, &res);
1040 if (hr || (res != custom_header_size))
1042 ERR("Failed to read OR_CUSTOM header, 0x%08lx\n", hr);
1043 return STG_E_READFAULT;
1045 /* now create the marshaler specified in the stream */
1046 hr = CoCreateInstance(&objref.u_objref.u_custom.clsid, NULL,
1047 CLSCTX_INPROC_SERVER, &IID_IMarshal,
1048 (LPVOID*)marshal);
1050 else
1052 FIXME("Invalid or unimplemented marshaling type specified: %lx\n",
1053 objref.flags);
1054 return RPC_E_INVALID_OBJREF;
1057 if (hr)
1058 ERR("Failed to create marshal, 0x%08lx\n", hr);
1060 return hr;
1063 /***********************************************************************
1064 * CoGetMarshalSizeMax [OLE32.@]
1066 * Gets the maximum amount of data that will be needed by a marshal.
1068 * PARAMS
1069 * pulSize [O] Address where maximum marshal size will be stored.
1070 * riid [I] Identifier of the interface to marshal.
1071 * pUnk [I] Pointer to the object to marshal.
1072 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1073 * pvDestContext [I] Reserved. Must be NULL.
1074 * mshlFlags [I] Flags that affect the marshaling. See CoMarshalInterface().
1076 * RETURNS
1077 * Success: S_OK.
1078 * Failure: HRESULT code.
1080 * SEE ALSO
1081 * CoMarshalInterface().
1083 HRESULT WINAPI CoGetMarshalSizeMax(ULONG *pulSize, REFIID riid, IUnknown *pUnk,
1084 DWORD dwDestContext, void *pvDestContext,
1085 DWORD mshlFlags)
1087 HRESULT hr;
1088 LPMARSHAL pMarshal;
1089 CLSID marshaler_clsid;
1091 hr = get_marshaler(riid, pUnk, dwDestContext, pvDestContext, mshlFlags, &pMarshal);
1092 if (hr)
1093 return hr;
1095 hr = IMarshal_GetUnmarshalClass(pMarshal, riid, pUnk, dwDestContext,
1096 pvDestContext, mshlFlags, &marshaler_clsid);
1097 if (hr)
1099 ERR("IMarshal::GetUnmarshalClass failed, 0x%08lx\n", hr);
1100 IMarshal_Release(pMarshal);
1101 return hr;
1104 hr = IMarshal_GetMarshalSizeMax(pMarshal, riid, pUnk, dwDestContext,
1105 pvDestContext, mshlFlags, pulSize);
1106 /* add on the size of the common header */
1107 *pulSize += FIELD_OFFSET(OBJREF, u_objref);
1109 /* if custom marshaling, add on size of custom header */
1110 if (!IsEqualCLSID(&marshaler_clsid, &CLSID_DfMarshal))
1111 *pulSize += FIELD_OFFSET(OBJREF, u_objref.u_custom.size) -
1112 FIELD_OFFSET(OBJREF, u_objref.u_custom);
1114 IMarshal_Release(pMarshal);
1115 return hr;
1119 /***********************************************************************
1120 * CoMarshalInterface [OLE32.@]
1122 * Marshals an interface into a stream so that the object can then be
1123 * unmarshaled from another COM apartment and used remotely.
1125 * PARAMS
1126 * pStream [I] Stream the object will be marshaled into.
1127 * riid [I] Identifier of the interface to marshal.
1128 * pUnk [I] Pointer to the object to marshal.
1129 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1130 * pvDestContext [I] Reserved. Must be NULL.
1131 * mshlFlags [I] Flags that affect the marshaling. See notes.
1133 * RETURNS
1134 * Success: S_OK.
1135 * Failure: HRESULT code.
1137 * NOTES
1139 * The mshlFlags parameter can take one or more of the following flags:
1140 *| MSHLFLAGS_NORMAL - Unmarshal once, releases stub on last proxy release.
1141 *| MSHLFLAGS_TABLESTRONG - Unmarshal many, release when CoReleaseMarshalData() called.
1142 *| MSHLFLAGS_TABLEWEAK - Unmarshal many, releases stub on last proxy release.
1143 *| MSHLFLAGS_NOPING - No automatic garbage collection (and so reduces network traffic).
1145 * If a marshaled object is not unmarshaled, then CoReleaseMarshalData() must
1146 * be called in order to release the resources used in the marshaling.
1148 * SEE ALSO
1149 * CoUnmarshalInterface(), CoReleaseMarshalData().
1151 HRESULT WINAPI CoMarshalInterface(IStream *pStream, REFIID riid, IUnknown *pUnk,
1152 DWORD dwDestContext, void *pvDestContext,
1153 DWORD mshlFlags)
1155 HRESULT hr;
1156 CLSID marshaler_clsid;
1157 OBJREF objref;
1158 IStream * pMarshalStream = NULL;
1159 LPMARSHAL pMarshal;
1161 TRACE("(%p, %s, %p, %lx, %p, %lx)\n", pStream, debugstr_guid(riid), pUnk,
1162 dwDestContext, pvDestContext, mshlFlags);
1164 if (pUnk == NULL)
1165 return E_INVALIDARG;
1167 objref.signature = OBJREF_SIGNATURE;
1168 objref.iid = *riid;
1170 /* get the marshaler for the specified interface */
1171 hr = get_marshaler(riid, pUnk, dwDestContext, pvDestContext, mshlFlags, &pMarshal);
1172 if (hr)
1174 ERR("Failed to get marshaller, 0x%08lx\n", hr);
1175 return hr;
1178 hr = IMarshal_GetUnmarshalClass(pMarshal, riid, pUnk, dwDestContext,
1179 pvDestContext, mshlFlags, &marshaler_clsid);
1180 if (hr)
1182 ERR("IMarshal::GetUnmarshalClass failed, 0x%08lx\n", hr);
1183 goto cleanup;
1186 /* FIXME: implement handler marshaling too */
1187 if (IsEqualCLSID(&marshaler_clsid, &CLSID_DfMarshal))
1189 TRACE("Using standard marshaling\n");
1190 objref.flags = OBJREF_STANDARD;
1191 pMarshalStream = pStream;
1193 else
1195 TRACE("Using custom marshaling\n");
1196 objref.flags = OBJREF_CUSTOM;
1197 /* we do custom marshaling into a memory stream so that we know what
1198 * size to write into the OR_CUSTOM header */
1199 hr = CreateStreamOnHGlobal(NULL, TRUE, &pMarshalStream);
1200 if (hr)
1202 ERR("CreateStreamOnHGLOBAL failed with 0x%08lx\n", hr);
1203 goto cleanup;
1207 /* write the common OBJREF header to the stream */
1208 hr = IStream_Write(pStream, &objref, FIELD_OFFSET(OBJREF, u_objref), NULL);
1209 if (hr)
1211 ERR("Failed to write OBJREF header to stream, 0x%08lx\n", hr);
1212 goto cleanup;
1215 TRACE("Calling IMarshal::MarshalInterace\n");
1216 /* call helper object to do the actual marshaling */
1217 hr = IMarshal_MarshalInterface(pMarshal, pMarshalStream, riid, pUnk, dwDestContext,
1218 pvDestContext, mshlFlags);
1220 if (hr)
1222 ERR("Failed to marshal the interface %s, %lx\n", debugstr_guid(riid), hr);
1223 goto cleanup;
1226 if (objref.flags & OBJREF_CUSTOM)
1228 ULONG custom_header_size = FIELD_OFFSET(OBJREF, u_objref.u_custom.size) -
1229 FIELD_OFFSET(OBJREF, u_objref.u_custom);
1230 HGLOBAL hGlobal;
1231 LPVOID data;
1232 hr = GetHGlobalFromStream(pMarshalStream, &hGlobal);
1233 if (hr)
1235 ERR("Couldn't get HGLOBAL from stream\n");
1236 hr = E_UNEXPECTED;
1237 goto cleanup;
1239 objref.u_objref.u_custom.clsid = marshaler_clsid;
1240 objref.u_objref.u_custom.cbExtension = 0;
1241 objref.u_objref.u_custom.size = GlobalSize(hGlobal);
1242 /* write constant sized OR_CUSTOM data into stream */
1243 hr = IStream_Write(pStream, &objref.u_objref.u_custom,
1244 custom_header_size, NULL);
1245 if (hr)
1247 ERR("Failed to write OR_CUSTOM header to stream with 0x%08lx\n", hr);
1248 goto cleanup;
1251 data = GlobalLock(hGlobal);
1252 if (!data)
1254 ERR("GlobalLock failed\n");
1255 hr = E_UNEXPECTED;
1256 goto cleanup;
1258 /* write custom marshal data */
1259 hr = IStream_Write(pStream, data, objref.u_objref.u_custom.size, NULL);
1260 if (hr)
1262 ERR("Failed to write custom marshal data with 0x%08lx\n", hr);
1263 goto cleanup;
1265 GlobalUnlock(hGlobal);
1268 cleanup:
1269 if (pMarshalStream && (objref.flags & OBJREF_CUSTOM))
1270 IStream_Release(pMarshalStream);
1271 IMarshal_Release(pMarshal);
1272 return hr;
1275 /***********************************************************************
1276 * CoUnmarshalInterface [OLE32.@]
1278 * Unmarshals an object from a stream by creating a proxy to the remote
1279 * object, if necessary.
1281 * PARAMS
1283 * pStream [I] Stream containing the marshaled object.
1284 * riid [I] Interface identifier of the object to create a proxy to.
1285 * ppv [O] Address where proxy will be stored.
1287 * RETURNS
1289 * Success: S_OK.
1290 * Failure: HRESULT code.
1292 * SEE ALSO
1293 * CoMarshalInterface().
1295 HRESULT WINAPI CoUnmarshalInterface(IStream *pStream, REFIID riid, LPVOID *ppv)
1297 HRESULT hr;
1298 LPMARSHAL pMarshal;
1300 TRACE("(%p, %s, %p)\n", pStream, debugstr_guid(riid), ppv);
1302 hr = get_unmarshaler_from_stream(pStream, &pMarshal);
1303 if (hr != S_OK)
1304 return hr;
1306 /* call the helper object to do the actual unmarshaling */
1307 hr = IMarshal_UnmarshalInterface(pMarshal, pStream, riid, ppv);
1308 if (hr)
1309 ERR("IMarshal::UnmarshalInterface failed, 0x%08lx\n", hr);
1311 IMarshal_Release(pMarshal);
1312 return hr;
1315 /***********************************************************************
1316 * CoReleaseMarshalData [OLE32.@]
1318 * Releases resources associated with an object that has been marshaled into
1319 * a stream.
1321 * PARAMS
1323 * pStream [I] The stream that the object has been marshaled into.
1325 * RETURNS
1326 * Success: S_OK.
1327 * Failure: HRESULT error code.
1329 * NOTES
1331 * Call this function to release resources associated with a normal or
1332 * table-weak marshal that will not be unmarshaled, and all table-strong
1333 * marshals when they are no longer needed.
1335 * SEE ALSO
1336 * CoMarshalInterface(), CoUnmarshalInterface().
1338 HRESULT WINAPI CoReleaseMarshalData(IStream *pStream)
1340 HRESULT hr;
1341 LPMARSHAL pMarshal;
1343 TRACE("(%p)\n", pStream);
1345 hr = get_unmarshaler_from_stream(pStream, &pMarshal);
1346 if (hr != S_OK)
1347 return hr;
1349 /* call the helper object to do the releasing of marshal data */
1350 hr = IMarshal_ReleaseMarshalData(pMarshal, pStream);
1351 if (hr)
1352 ERR("IMarshal::ReleaseMarshalData failed with error 0x%08lx\n", hr);
1354 IMarshal_Release(pMarshal);
1355 return hr;
1359 /***********************************************************************
1360 * CoMarshalInterThreadInterfaceInStream [OLE32.@]
1362 * Marshal an interface across threads in the same process.
1364 * PARAMS
1365 * riid [I] Identifier of the interface to be marshalled.
1366 * pUnk [I] Pointer to IUnknown-derived interface that will be marshalled.
1367 * ppStm [O] Pointer to IStream object that is created and then used to store the marshalled inteface.
1369 * RETURNS
1370 * Success: S_OK
1371 * Failure: E_OUTOFMEMORY and other COM error codes
1373 * SEE ALSO
1374 * CoMarshalInterface(), CoUnmarshalInterface() and CoGetInterfaceAndReleaseStream()
1376 HRESULT WINAPI CoMarshalInterThreadInterfaceInStream(
1377 REFIID riid, LPUNKNOWN pUnk, LPSTREAM * ppStm)
1379 ULARGE_INTEGER xpos;
1380 LARGE_INTEGER seekto;
1381 HRESULT hres;
1383 TRACE("(%s, %p, %p)\n",debugstr_guid(riid), pUnk, ppStm);
1385 hres = CreateStreamOnHGlobal(0, TRUE, ppStm);
1386 if (FAILED(hres)) return hres;
1387 hres = CoMarshalInterface(*ppStm, riid, pUnk, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1389 /* FIXME: is this needed? */
1390 memset(&seekto,0,sizeof(seekto));
1391 IStream_Seek(*ppStm,seekto,SEEK_SET,&xpos);
1393 return hres;
1396 /***********************************************************************
1397 * CoGetInterfaceAndReleaseStream [OLE32.@]
1399 * Unmarshalls an inteface from a stream and then releases the stream.
1401 * PARAMS
1402 * pStm [I] Stream that contains the marshalled inteface.
1403 * riid [I] Interface identifier of the object to unmarshall.
1404 * ppv [O] Address of pointer where the requested interface object will be stored.
1406 * RETURNS
1407 * Success: S_OK
1408 * Failure: A COM error code
1410 * SEE ALSO
1411 * CoMarshalInterThreadInterfaceInStream() and CoUnmarshalInteface()
1413 HRESULT WINAPI CoGetInterfaceAndReleaseStream(LPSTREAM pStm, REFIID riid,
1414 LPVOID *ppv)
1416 HRESULT hres;
1418 TRACE("(%p, %s, %p)\n", pStm, debugstr_guid(riid), ppv);
1420 hres = CoUnmarshalInterface(pStm, riid, ppv);
1421 IStream_Release(pStm);
1422 return hres;
1425 static HRESULT WINAPI StdMarshalCF_QueryInterface(LPCLASSFACTORY iface,
1426 REFIID riid, LPVOID *ppv)
1428 *ppv = NULL;
1429 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory))
1431 *ppv = (LPVOID)iface;
1432 return S_OK;
1434 return E_NOINTERFACE;
1437 static ULONG WINAPI StdMarshalCF_AddRef(LPCLASSFACTORY iface)
1439 return 2; /* non-heap based object */
1442 static ULONG WINAPI StdMarshalCF_Release(LPCLASSFACTORY iface)
1444 return 1; /* non-heap based object */
1447 static HRESULT WINAPI StdMarshalCF_CreateInstance(LPCLASSFACTORY iface,
1448 LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv)
1450 if (IsEqualIID(riid,&IID_IMarshal))
1451 return StdMarshalImpl_Construct(riid, ppv);
1453 FIXME("(%s), not supported.\n",debugstr_guid(riid));
1454 return E_NOINTERFACE;
1457 static HRESULT WINAPI StdMarshalCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
1459 FIXME("(%d), stub!\n",fLock);
1460 return S_OK;
1463 static IClassFactoryVtbl StdMarshalCFVtbl =
1465 StdMarshalCF_QueryInterface,
1466 StdMarshalCF_AddRef,
1467 StdMarshalCF_Release,
1468 StdMarshalCF_CreateInstance,
1469 StdMarshalCF_LockServer
1471 static IClassFactoryVtbl *StdMarshalCF = &StdMarshalCFVtbl;
1473 HRESULT MARSHAL_GetStandardMarshalCF(LPVOID *ppv)
1475 *ppv = &StdMarshalCF;
1476 return S_OK;
1479 /***********************************************************************
1480 * CoMarshalHresult [OLE32.@]
1482 * Marshals an HRESULT value into a stream.
1484 * PARAMS
1485 * pStm [I] Stream that hresult will be marshalled into.
1486 * hresult [I] HRESULT to be marshalled.
1488 * RETURNS
1489 * Success: S_OK
1490 * Failure: A COM error code
1492 * SEE ALSO
1493 * CoUnmarshalHresult().
1495 HRESULT WINAPI CoMarshalHresult(LPSTREAM pStm, HRESULT hresult)
1497 return IStream_Write(pStm, &hresult, sizeof(hresult), NULL);
1500 /***********************************************************************
1501 * CoUnmarshalHresult [OLE32.@]
1503 * Unmarshals an HRESULT value from a stream.
1505 * PARAMS
1506 * pStm [I] Stream that hresult will be unmarshalled from.
1507 * phresult [I] Pointer to HRESULT where the value will be unmarshalled to.
1509 * RETURNS
1510 * Success: S_OK
1511 * Failure: A COM error code
1513 * SEE ALSO
1514 * CoMarshalHresult().
1516 HRESULT WINAPI CoUnmarshalHresult(LPSTREAM pStm, HRESULT * phresult)
1518 return IStream_Read(pStm, phresult, sizeof(*phresult), NULL);