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
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 * Note that the IUnknown_QI(ob,xiid,&ppv) always returns the SAME ppv value!
68 * A triple is used: OXID (apt id), OID (stub manager id),
69 * IPID (interface ptr/stub id).
71 * OXIDs identify an apartment and are network scoped
72 * OIDs identify a stub manager and are apartment scoped
73 * IPIDs identify an interface stub and are apartment scoped
76 inline static HRESULT
get_facbuf_for_iid(REFIID riid
, IPSFactoryBuffer
**facbuf
)
81 if ((hr
= CoGetPSClsid(riid
, &clsid
)))
83 return CoGetClassObject(&clsid
, CLSCTX_INPROC_SERVER
, NULL
,
84 &IID_IPSFactoryBuffer
, (LPVOID
*)facbuf
);
87 /* creates a new stub manager */
88 HRESULT
marshal_object(APARTMENT
*apt
, STDOBJREF
*stdobjref
, REFIID riid
, IUnknown
*obj
, MSHLFLAGS mshlflags
)
90 struct stub_manager
*manager
;
91 struct ifstub
*ifstub
;
94 IPSFactoryBuffer
*psfb
;
97 hr
= apartment_getoxid(apt
, &stdobjref
->oxid
);
101 hr
= get_facbuf_for_iid(riid
, &psfb
);
104 ERR("couldn't get IPSFactory buffer for interface %s\n", debugstr_guid(riid
));
108 hr
= IPSFactoryBuffer_CreateStub(psfb
, riid
, obj
, &stub
);
109 IPSFactoryBuffer_Release(psfb
);
112 ERR("Failed to create an IRpcStubBuffer from IPSFactory for %s\n", debugstr_guid(riid
));
116 if (mshlflags
& MSHLFLAGS_NOPING
)
117 stdobjref
->flags
= SORF_NOPING
;
119 stdobjref
->flags
= SORF_NULL
;
121 /* FIXME: what happens if we register an interface twice with different
122 * marshaling flags? */
123 if ((manager
= get_stub_manager_from_object(apt
, obj
)))
124 TRACE("registering new ifstub on pre-existing manager\n");
127 TRACE("constructing new stub manager\n");
129 manager
= new_stub_manager(apt
, obj
, mshlflags
);
132 IRpcStubBuffer_Release(stub
);
133 return E_OUTOFMEMORY
;
136 stdobjref
->oid
= manager
->oid
;
138 tablemarshal
= ((mshlflags
& MSHLFLAGS_TABLESTRONG
) || (mshlflags
& MSHLFLAGS_TABLEWEAK
));
140 ifstub
= stub_manager_new_ifstub(manager
, stub
, obj
, riid
);
143 IRpcStubBuffer_Release(stub
);
144 stub_manager_int_release(manager
);
145 /* FIXME: should we do another release to completely destroy the
147 return E_OUTOFMEMORY
;
152 stdobjref
->cPublicRefs
= NORMALEXTREFS
;
153 stub_manager_ext_addref(manager
, stdobjref
->cPublicRefs
);
157 stdobjref
->cPublicRefs
= 0;
158 if (mshlflags
& MSHLFLAGS_TABLESTRONG
)
159 stub_manager_ext_addref(manager
, 1);
162 /* FIXME: check return value */
163 RPC_RegisterInterface(riid
);
165 stdobjref
->ipid
= ifstub
->ipid
;
167 stub_manager_int_release(manager
);
173 /* Client-side identity of the server object */
175 static HRESULT
proxy_manager_get_remunknown(struct proxy_manager
* This
, IRemUnknown
**remunk
);
176 static void proxy_manager_destroy(struct proxy_manager
* This
);
177 static HRESULT
proxy_manager_find_ifproxy(struct proxy_manager
* This
, REFIID riid
, struct ifproxy
** ifproxy_found
);
178 static HRESULT
proxy_manager_query_local_interface(struct proxy_manager
* This
, REFIID riid
, void ** ppv
);
180 static HRESULT WINAPI
ClientIdentity_QueryInterface(IMultiQI
* iface
, REFIID riid
, void ** ppv
)
185 TRACE("%s\n", debugstr_guid(riid
));
188 hr
= IMultiQI_QueryMultipleInterfaces(iface
, 1, &mqi
);
189 *ppv
= (void *)mqi
.pItf
;
194 static ULONG WINAPI
ClientIdentity_AddRef(IMultiQI
* iface
)
196 struct proxy_manager
* This
= (struct proxy_manager
*)iface
;
197 TRACE("%p - before %ld\n", iface
, This
->refs
);
198 return InterlockedIncrement(&This
->refs
);
201 static ULONG WINAPI
ClientIdentity_Release(IMultiQI
* iface
)
203 struct proxy_manager
* This
= (struct proxy_manager
*)iface
;
204 ULONG refs
= InterlockedDecrement(&This
->refs
);
205 TRACE("%p - after %ld\n", iface
, refs
);
207 proxy_manager_destroy(This
);
211 static HRESULT WINAPI
ClientIdentity_QueryMultipleInterfaces(IMultiQI
*iface
, ULONG cMQIs
, MULTI_QI
*pMQIs
)
213 struct proxy_manager
* This
= (struct proxy_manager
*)iface
;
214 REMQIRESULT
*qiresults
= NULL
;
215 ULONG nonlocal_mqis
= 0;
217 ULONG successful_mqis
= 0;
218 IID
*iids
= HeapAlloc(GetProcessHeap(), 0, cMQIs
* sizeof(*iids
));
219 /* mapping of RemQueryInterface index to QueryMultipleInterfaces index */
220 ULONG
*mapping
= HeapAlloc(GetProcessHeap(), 0, cMQIs
* sizeof(*mapping
));
222 TRACE("cMQIs: %ld\n", cMQIs
);
224 /* try to get a local interface - this includes already active proxy
225 * interfaces and also interfaces exposed by the proxy manager */
226 for (i
= 0; i
< cMQIs
; i
++)
228 TRACE("iid[%ld] = %s\n", i
, debugstr_guid(pMQIs
[i
].pIID
));
229 pMQIs
[i
].hr
= proxy_manager_query_local_interface(This
, pMQIs
[i
].pIID
, (void **)&pMQIs
[i
].pItf
);
230 if (pMQIs
[i
].hr
== S_OK
)
234 iids
[nonlocal_mqis
] = *pMQIs
[i
].pIID
;
235 mapping
[nonlocal_mqis
] = i
;
240 TRACE("%ld interfaces not found locally\n", nonlocal_mqis
);
242 /* if we have more than one interface not found locally then we must try
243 * to query the remote object for it */
244 if (nonlocal_mqis
!= 0)
250 /* get the ipid of the first entry */
251 /* FIXME: should we implement ClientIdentity on the ifproxies instead
252 * of the proxy_manager so we use the correct ipid here? */
253 ipid
= &LIST_ENTRY(list_head(&This
->interfaces
), struct ifproxy
, entry
)->ipid
;
255 /* get IRemUnknown proxy so we can communicate with the remote object */
256 hr
= proxy_manager_get_remunknown(This
, &remunk
);
260 hr
= IRemUnknown_RemQueryInterface(remunk
, ipid
, NORMALEXTREFS
,
261 nonlocal_mqis
, iids
, &qiresults
);
263 ERR("IRemUnknown_RemQueryInterface failed with error 0x%08lx\n", hr
);
266 /* IRemUnknown_RemQueryInterface can return S_FALSE if only some of
267 * the interfaces were returned */
270 /* try to unmarshal each object returned to us */
271 for (i
= 0; i
< nonlocal_mqis
; i
++)
273 ULONG index
= mapping
[i
];
274 HRESULT hrobj
= qiresults
[i
].hResult
;
276 hrobj
= unmarshal_object(&qiresults
[i
].std
, This
->parent
,
278 (void **)&pMQIs
[index
].pItf
);
283 ERR("Failed to get pointer to interface %s\n", debugstr_guid(pMQIs
[index
].pIID
));
284 pMQIs
[index
].hr
= hrobj
;
288 /* free the memory allocated by the proxy */
289 CoTaskMemFree(qiresults
);
292 TRACE("%ld/%ld successfully queried\n", successful_mqis
, cMQIs
);
294 HeapFree(GetProcessHeap(), 0, iids
);
295 HeapFree(GetProcessHeap(), 0, mapping
);
297 if (successful_mqis
== cMQIs
)
298 return S_OK
; /* we got all requested interfaces */
299 else if (successful_mqis
== 0)
300 return E_NOINTERFACE
; /* we didn't get any interfaces */
302 return S_FALSE
; /* we got some interfaces */
305 static const IMultiQIVtbl ClientIdentity_Vtbl
=
307 ClientIdentity_QueryInterface
,
308 ClientIdentity_AddRef
,
309 ClientIdentity_Release
,
310 ClientIdentity_QueryMultipleInterfaces
313 static HRESULT
ifproxy_get_public_ref(struct ifproxy
* This
)
317 if (WAIT_OBJECT_0
!= WaitForSingleObject(This
->parent
->remoting_mutex
, INFINITE
))
319 ERR("Wait failed for ifproxy %p\n", This
);
325 IRemUnknown
*remunk
= NULL
;
327 TRACE("getting public ref for ifproxy %p\n", This
);
329 hr
= proxy_manager_get_remunknown(This
->parent
, &remunk
);
334 rif
.ipid
= This
->ipid
;
335 rif
.cPublicRefs
= NORMALEXTREFS
;
336 rif
.cPrivateRefs
= 0;
337 hr
= IRemUnknown_RemAddRef(remunk
, 1, &rif
, &hrref
);
338 if (hr
== S_OK
&& hrref
== S_OK
)
339 This
->refs
+= NORMALEXTREFS
;
341 ERR("IRemUnknown_RemAddRef returned with 0x%08lx, hrref = 0x%08lx\n", hr
, hrref
);
344 ReleaseMutex(This
->parent
->remoting_mutex
);
349 static HRESULT
ifproxy_release_public_refs(struct ifproxy
* This
)
353 if (WAIT_OBJECT_0
!= WaitForSingleObject(This
->parent
->remoting_mutex
, INFINITE
))
355 ERR("Wait failed for ifproxy %p\n", This
);
361 IRemUnknown
*remunk
= NULL
;
363 TRACE("releasing %ld refs\n", This
->refs
);
365 hr
= proxy_manager_get_remunknown(This
->parent
, &remunk
);
369 rif
.ipid
= This
->ipid
;
370 rif
.cPublicRefs
= This
->refs
;
371 rif
.cPrivateRefs
= 0;
372 hr
= IRemUnknown_RemRelease(remunk
, 1, &rif
);
375 else if (hr
== RPC_E_DISCONNECTED
)
376 WARN("couldn't release references because object was "
377 "disconnected: oxid = %s, oid = %s\n",
378 wine_dbgstr_longlong(This
->parent
->oxid
),
379 wine_dbgstr_longlong(This
->parent
->oid
));
381 ERR("IRemUnknown_RemRelease failed with error 0x%08lx\n", hr
);
384 ReleaseMutex(This
->parent
->remoting_mutex
);
389 /* should be called inside This->parent->cs critical section */
390 static void ifproxy_disconnect(struct ifproxy
* This
)
392 ifproxy_release_public_refs(This
);
393 if (This
->proxy
) IRpcProxyBuffer_Disconnect(This
->proxy
);
395 IRpcChannelBuffer_Release(This
->chan
);
399 /* should be called in This->parent->cs critical section if it is an entry in parent's list */
400 static void ifproxy_destroy(struct ifproxy
* This
)
404 /* release public references to this object so that the stub can know
405 * when to destroy itself */
406 ifproxy_release_public_refs(This
);
408 list_remove(&This
->entry
);
412 IRpcChannelBuffer_Release(This
->chan
);
416 /* note: we don't call Release for This->proxy because its lifetime is
417 * controlled by the return value from ClientIdentity_Release, which this
418 * function is always called from */
420 HeapFree(GetProcessHeap(), 0, This
);
423 static HRESULT
proxy_manager_construct(
424 APARTMENT
* apt
, ULONG sorflags
, OXID oxid
, OID oid
,
425 struct proxy_manager
** proxy_manager
)
427 struct proxy_manager
* This
= HeapAlloc(GetProcessHeap(), 0, sizeof(*This
));
428 if (!This
) return E_OUTOFMEMORY
;
430 This
->remoting_mutex
= CreateMutexW(NULL
, FALSE
, NULL
);
431 if (!This
->remoting_mutex
)
433 HeapFree(GetProcessHeap(), 0, This
);
434 return HRESULT_FROM_WIN32(GetLastError());
437 This
->lpVtbl
= &ClientIdentity_Vtbl
;
439 list_init(&This
->entry
);
440 list_init(&This
->interfaces
);
442 InitializeCriticalSection(&This
->cs
);
443 DEBUG_SET_CRITSEC_NAME(&This
->cs
, "proxy_manager");
445 /* the apartment the object was unmarshaled into */
448 /* the source apartment and id of the object */
454 /* the DCOM draft specification states that the SORF_NOPING flag is
455 * proxy manager specific, not ifproxy specific, so this implies that we
456 * should store the STDOBJREF flags here in the proxy manager. */
457 This
->sorflags
= sorflags
;
459 /* we create the IRemUnknown proxy on demand */
462 EnterCriticalSection(&apt
->cs
);
463 /* FIXME: we are dependent on the ordering in here to make sure a proxy's
464 * IRemUnknown proxy doesn't get destroyed before the regual proxy does
465 * because we need the IRemUnknown proxy during the destruction of the
466 * regular proxy. Ideally, we should maintain a separate list for the
467 * IRemUnknown proxies that need late destruction */
468 list_add_tail(&apt
->proxies
, &This
->entry
);
469 LeaveCriticalSection(&apt
->cs
);
471 TRACE("%p created for OXID %s, OID %s\n", This
,
472 wine_dbgstr_longlong(oxid
), wine_dbgstr_longlong(oid
));
474 *proxy_manager
= This
;
478 static HRESULT
proxy_manager_query_local_interface(struct proxy_manager
* This
, REFIID riid
, void ** ppv
)
481 struct ifproxy
* ifproxy
;
483 TRACE("%s\n", debugstr_guid(riid
));
485 if (IsEqualIID(riid
, &IID_IUnknown
) ||
486 IsEqualIID(riid
, &IID_IMultiQI
))
488 *ppv
= (void *)&This
->lpVtbl
;
489 IMultiQI_AddRef((IMultiQI
*)&This
->lpVtbl
);
493 hr
= proxy_manager_find_ifproxy(This
, riid
, &ifproxy
);
496 *ppv
= ifproxy
->iface
;
497 IUnknown_AddRef((IUnknown
*)*ppv
);
502 return E_NOINTERFACE
;
505 static HRESULT
proxy_manager_create_ifproxy(
506 struct proxy_manager
* This
, const IPID
*ipid
, REFIID riid
, ULONG cPublicRefs
,
507 IRpcChannelBuffer
* channel
, struct ifproxy
** iif_out
)
510 IPSFactoryBuffer
* psfb
;
511 struct ifproxy
* ifproxy
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ifproxy
));
512 if (!ifproxy
) return E_OUTOFMEMORY
;
514 list_init(&ifproxy
->entry
);
516 ifproxy
->parent
= This
;
517 ifproxy
->ipid
= *ipid
;
518 ifproxy
->iid
= *riid
;
519 ifproxy
->refs
= cPublicRefs
;
520 ifproxy
->proxy
= NULL
;
523 ifproxy
->chan
= channel
; /* FIXME: we should take the binding strings and construct the channel in this function */
525 /* the IUnknown interface is special because it does not have a
526 * proxy associated with the ifproxy as we handle IUnknown ourselves */
527 if (IsEqualIID(riid
, &IID_IUnknown
))
529 ifproxy
->iface
= (void *)&This
->lpVtbl
;
534 hr
= get_facbuf_for_iid(riid
, &psfb
);
537 /* important note: the outer unknown is set to the proxy manager.
538 * This ensures the COM identity rules are not violated, by having a
539 * one-to-one mapping of objects on the proxy side to objects on the
540 * stub side, no matter which interface you view the object through */
541 hr
= IPSFactoryBuffer_CreateProxy(psfb
, (IUnknown
*)&This
->lpVtbl
, riid
,
542 &ifproxy
->proxy
, &ifproxy
->iface
);
543 IPSFactoryBuffer_Release(psfb
);
545 ERR("Could not create proxy for interface %s, error 0x%08lx\n",
546 debugstr_guid(riid
), hr
);
549 ERR("Could not get IPSFactoryBuffer for interface %s, error 0x%08lx\n",
550 debugstr_guid(riid
), hr
);
553 hr
= IRpcProxyBuffer_Connect(ifproxy
->proxy
, ifproxy
->chan
);
556 /* get at least one external reference to the object to keep it alive */
558 hr
= ifproxy_get_public_ref(ifproxy
);
562 EnterCriticalSection(&This
->cs
);
563 list_add_tail(&This
->interfaces
, &ifproxy
->entry
);
564 LeaveCriticalSection(&This
->cs
);
567 TRACE("ifproxy %p created for IPID %s, interface %s with %lu public refs\n",
568 ifproxy
, debugstr_guid(ipid
), debugstr_guid(riid
), cPublicRefs
);
571 ifproxy_destroy(ifproxy
);
576 static HRESULT
proxy_manager_find_ifproxy(struct proxy_manager
* This
, REFIID riid
, struct ifproxy
** ifproxy_found
)
578 HRESULT hr
= E_NOINTERFACE
; /* assume not found */
579 struct list
* cursor
;
581 EnterCriticalSection(&This
->cs
);
582 LIST_FOR_EACH(cursor
, &This
->interfaces
)
584 struct ifproxy
* ifproxy
= LIST_ENTRY(cursor
, struct ifproxy
, entry
);
585 if (IsEqualIID(riid
, &ifproxy
->iid
))
587 *ifproxy_found
= ifproxy
;
592 LeaveCriticalSection(&This
->cs
);
597 static void proxy_manager_disconnect(struct proxy_manager
* This
)
599 struct list
* cursor
;
601 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This
->oxid
),
602 wine_dbgstr_longlong(This
->oid
));
604 EnterCriticalSection(&This
->cs
);
606 LIST_FOR_EACH(cursor
, &This
->interfaces
)
608 struct ifproxy
* ifproxy
= LIST_ENTRY(cursor
, struct ifproxy
, entry
);
609 ifproxy_disconnect(ifproxy
);
612 /* apartment is being destroyed so don't keep a pointer around to it */
615 LeaveCriticalSection(&This
->cs
);
618 static HRESULT
proxy_manager_get_remunknown(struct proxy_manager
* This
, IRemUnknown
**remunk
)
622 /* we don't want to try and unmarshal or use IRemUnknown if we don't want
623 * lifetime management */
624 if (This
->sorflags
& SORFP_NOLIFETIMEMGMT
)
627 EnterCriticalSection(&This
->cs
);
629 /* already created - return existing object */
630 *remunk
= This
->remunk
;
631 else if (!This
->parent
)
632 /* disconnected - we can't create IRemUnknown */
637 /* Don't want IRemUnknown lifetime management as this is IRemUnknown!
638 * We also don't care about whether or not the stub is still alive */
639 stdobjref
.flags
= SORFP_NOLIFETIMEMGMT
| SORF_NOPING
;
640 stdobjref
.cPublicRefs
= 1;
641 /* oxid of destination object */
642 stdobjref
.oxid
= This
->oxid
;
643 /* FIXME: what should be used for the oid? The DCOM draft doesn't say */
644 stdobjref
.oid
= (OID
)-1;
645 /* FIXME: this is a hack around not having an OXID resolver yet -
646 * the OXID resolver should give us the IPID of the IRemUnknown
648 stdobjref
.ipid
.Data1
= 0xffffffff;
649 stdobjref
.ipid
.Data2
= 0xffff;
650 stdobjref
.ipid
.Data3
= 0xffff;
651 assert(sizeof(stdobjref
.ipid
.Data4
) == sizeof(stdobjref
.oxid
));
652 memcpy(&stdobjref
.ipid
.Data4
, &stdobjref
.oxid
, sizeof(OXID
));
654 /* do the unmarshal */
655 hr
= unmarshal_object(&stdobjref
, This
->parent
, &IID_IRemUnknown
, (void**)&This
->remunk
);
657 *remunk
= This
->remunk
;
659 LeaveCriticalSection(&This
->cs
);
661 TRACE("got IRemUnknown* pointer %p, hr = 0x%08lx\n", *remunk
, hr
);
666 /* destroys a proxy manager, freeing the memory it used.
667 * Note: this function should not be called from a list iteration in the
668 * apartment, due to the fact that it removes itself from the apartment and
669 * it could add a proxy to IRemUnknown into the apartment. */
670 static void proxy_manager_destroy(struct proxy_manager
* This
)
672 struct list
* cursor
;
674 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This
->oxid
),
675 wine_dbgstr_longlong(This
->oid
));
679 EnterCriticalSection(&This
->parent
->cs
);
681 /* remove ourself from the list of proxy objects in the apartment */
682 LIST_FOR_EACH(cursor
, &This
->parent
->proxies
)
684 if (cursor
== &This
->entry
)
686 list_remove(&This
->entry
);
691 LeaveCriticalSection(&This
->parent
->cs
);
694 /* destroy all of the interface proxies */
695 while ((cursor
= list_head(&This
->interfaces
)))
697 struct ifproxy
* ifproxy
= LIST_ENTRY(cursor
, struct ifproxy
, entry
);
698 ifproxy_destroy(ifproxy
);
701 if (This
->remunk
) IRemUnknown_Release(This
->remunk
);
703 DEBUG_CLEAR_CRITSEC_NAME(&This
->cs
);
704 DeleteCriticalSection(&This
->cs
);
706 CloseHandle(This
->remoting_mutex
);
708 HeapFree(GetProcessHeap(), 0, This
);
711 /* finds the proxy manager corresponding to a given OXID and OID that has
712 * been unmarshaled in the specified apartment. The caller must release the
713 * reference to the proxy_manager when the object is no longer used. */
714 static BOOL
find_proxy_manager(APARTMENT
* apt
, OXID oxid
, OID oid
, struct proxy_manager
** proxy_found
)
717 struct list
* cursor
;
719 EnterCriticalSection(&apt
->cs
);
720 LIST_FOR_EACH(cursor
, &apt
->proxies
)
722 struct proxy_manager
* proxy
= LIST_ENTRY(cursor
, struct proxy_manager
, entry
);
723 if ((oxid
== proxy
->oxid
) && (oid
== proxy
->oid
))
725 *proxy_found
= proxy
;
726 ClientIdentity_AddRef((IMultiQI
*)&proxy
->lpVtbl
);
731 LeaveCriticalSection(&apt
->cs
);
735 HRESULT
apartment_disconnectproxies(struct apartment
*apt
)
737 struct list
* cursor
;
739 LIST_FOR_EACH(cursor
, &apt
->proxies
)
741 struct proxy_manager
* proxy
= LIST_ENTRY(cursor
, struct proxy_manager
, entry
);
742 proxy_manager_disconnect(proxy
);
748 /********************** StdMarshal implementation ****************************/
749 typedef struct _StdMarshalImpl
751 const IMarshalVtbl
*lpvtbl
;
756 LPVOID pvDestContext
;
760 static HRESULT WINAPI
761 StdMarshalImpl_QueryInterface(LPMARSHAL iface
, REFIID riid
, LPVOID
*ppv
)
764 if (IsEqualIID(&IID_IUnknown
, riid
) || IsEqualIID(&IID_IMarshal
, riid
))
767 IUnknown_AddRef(iface
);
770 FIXME("No interface for %s.\n", debugstr_guid(riid
));
771 return E_NOINTERFACE
;
775 StdMarshalImpl_AddRef(LPMARSHAL iface
)
777 StdMarshalImpl
*This
= (StdMarshalImpl
*)iface
;
778 return InterlockedIncrement(&This
->ref
);
782 StdMarshalImpl_Release(LPMARSHAL iface
)
784 StdMarshalImpl
*This
= (StdMarshalImpl
*)iface
;
785 ULONG ref
= InterlockedDecrement(&This
->ref
);
787 if (!ref
) HeapFree(GetProcessHeap(),0,This
);
791 static HRESULT WINAPI
792 StdMarshalImpl_GetUnmarshalClass(
793 LPMARSHAL iface
, REFIID riid
, void* pv
, DWORD dwDestContext
,
794 void* pvDestContext
, DWORD mshlflags
, CLSID
* pCid
)
796 *pCid
= CLSID_DfMarshal
;
800 static HRESULT WINAPI
801 StdMarshalImpl_GetMarshalSizeMax(
802 LPMARSHAL iface
, REFIID riid
, void* pv
, DWORD dwDestContext
,
803 void* pvDestContext
, DWORD mshlflags
, DWORD
* pSize
)
805 *pSize
= sizeof(STDOBJREF
);
809 static HRESULT WINAPI
810 StdMarshalImpl_MarshalInterface(
811 LPMARSHAL iface
, IStream
*pStm
,REFIID riid
, void* pv
, DWORD dwDestContext
,
812 void* pvDestContext
, DWORD mshlflags
)
818 APARTMENT
*apt
= COM_CurrentApt();
820 TRACE("(...,%s,...)\n", debugstr_guid(riid
));
824 ERR("Apartment not initialized\n");
825 return CO_E_NOTINITIALIZED
;
828 /* make sure this apartment can be reached from other threads / processes */
829 RPC_StartRemoting(apt
);
831 hres
= IUnknown_QueryInterface((LPUNKNOWN
)pv
, riid
, (LPVOID
*)&pUnk
);
834 ERR("object doesn't expose interface %s, failing with error 0x%08lx\n",
835 debugstr_guid(riid
), hres
);
836 return E_NOINTERFACE
;
839 hres
= marshal_object(apt
, &stdobjref
, riid
, pUnk
, mshlflags
);
841 IUnknown_Release(pUnk
);
845 ERR("Failed to create ifstub, hres=0x%lx\n", hres
);
849 hres
= IStream_Write(pStm
, &stdobjref
, sizeof(stdobjref
), &res
);
850 if (hres
) return hres
;
855 /* helper for StdMarshalImpl_UnmarshalInterface - does the unmarshaling with
856 * no questions asked about the rules surrounding same-apartment unmarshals
857 * and table marshaling */
858 static HRESULT
unmarshal_object(const STDOBJREF
*stdobjref
, APARTMENT
*apt
, REFIID riid
, void **object
)
860 struct proxy_manager
*proxy_manager
= NULL
;
865 TRACE("stdobjref:\n\tflags = %04lx\n\tcPublicRefs = %ld\n\toxid = %s\n\toid = %s\n\tipid = %s\n",
866 stdobjref
->flags
, stdobjref
->cPublicRefs
,
867 wine_dbgstr_longlong(stdobjref
->oxid
),
868 wine_dbgstr_longlong(stdobjref
->oid
),
869 debugstr_guid(&stdobjref
->ipid
));
871 /* create an a new proxy manager if one doesn't already exist for the
873 if (!find_proxy_manager(apt
, stdobjref
->oxid
, stdobjref
->oid
, &proxy_manager
))
875 hr
= proxy_manager_construct(apt
, stdobjref
->flags
,
876 stdobjref
->oxid
, stdobjref
->oid
,
880 TRACE("proxy manager already created, using\n");
884 struct ifproxy
* ifproxy
;
885 hr
= proxy_manager_find_ifproxy(proxy_manager
, riid
, &ifproxy
);
886 if (hr
== E_NOINTERFACE
)
888 IRpcChannelBuffer
*chanbuf
;
889 hr
= RPC_CreateClientChannel(&stdobjref
->oxid
, &stdobjref
->ipid
, &chanbuf
);
891 hr
= proxy_manager_create_ifproxy(proxy_manager
, &stdobjref
->ipid
,
892 riid
, stdobjref
->cPublicRefs
,
898 /* FIXME: push this AddRef inside proxy_manager_find_ifproxy/create_ifproxy? */
899 ClientIdentity_AddRef((IMultiQI
*)&proxy_manager
->lpVtbl
);
900 *object
= ifproxy
->iface
;
904 /* release our reference to the proxy manager - the client/apartment
905 * will hold on to the remaining reference for us */
906 if (proxy_manager
) ClientIdentity_Release((IMultiQI
*)&proxy_manager
->lpVtbl
);
911 static HRESULT WINAPI
912 StdMarshalImpl_UnmarshalInterface(LPMARSHAL iface
, IStream
*pStm
, REFIID riid
, void **ppv
)
914 struct stub_manager
*stubmgr
;
918 APARTMENT
*apt
= COM_CurrentApt();
922 TRACE("(...,%s,....)\n", debugstr_guid(riid
));
924 /* we need an apartment to unmarshal into */
927 ERR("Apartment not initialized\n");
928 return CO_E_NOTINITIALIZED
;
931 /* read STDOBJREF from wire */
932 hres
= IStream_Read(pStm
, &stdobjref
, sizeof(stdobjref
), &res
);
933 if (hres
) return hres
;
935 hres
= apartment_getoxid(apt
, &oxid
);
936 if (hres
) return hres
;
938 /* check if we're marshalling back to ourselves */
939 if ((oxid
== stdobjref
.oxid
) && (stubmgr
= get_stub_manager(apt
, stdobjref
.oid
)))
941 TRACE("Unmarshalling object marshalled in same apartment for iid %s, "
942 "returning original object %p\n", debugstr_guid(riid
), stubmgr
->object
);
944 hres
= IUnknown_QueryInterface(stubmgr
->object
, riid
, ppv
);
946 /* unref the ifstub. FIXME: only do this on success? */
947 if (!stub_manager_is_table_marshaled(stubmgr
))
948 stub_manager_ext_release(stubmgr
, 1);
950 stub_manager_int_release(stubmgr
);
954 /* notify stub manager about unmarshal if process-local object.
955 * note: if the oxid is not found then we and native will quite happily
956 * ignore table marshaling and normal marshaling rules regarding number of
957 * unmarshals, etc, but if you abuse these rules then your proxy could end
958 * up returning RPC_E_DISCONNECTED. */
959 if ((stub_apt
= apartment_findfromoxid(stdobjref
.oxid
, TRUE
)))
961 if ((stubmgr
= get_stub_manager(stub_apt
, stdobjref
.oid
)))
963 if (!stub_manager_notify_unmarshal(stubmgr
))
964 hres
= CO_E_OBJNOTCONNECTED
;
966 stub_manager_int_release(stubmgr
);
970 WARN("Couldn't find object for OXID %s, OID %s, assuming disconnected\n",
971 wine_dbgstr_longlong(stdobjref
.oxid
),
972 wine_dbgstr_longlong(stdobjref
.oid
));
973 hres
= CO_E_OBJNOTCONNECTED
;
976 apartment_release(stub_apt
);
979 TRACE("Treating unmarshal from OXID %s as inter-process\n",
980 wine_dbgstr_longlong(stdobjref
.oxid
));
983 hres
= unmarshal_object(&stdobjref
, apt
, riid
, ppv
);
985 if (hres
) WARN("Failed with error 0x%08lx\n", hres
);
986 else TRACE("Successfully created proxy %p\n", *ppv
);
991 static HRESULT WINAPI
992 StdMarshalImpl_ReleaseMarshalData(LPMARSHAL iface
, IStream
*pStm
)
997 struct stub_manager
*stubmgr
;
1000 TRACE("iface=%p, pStm=%p\n", iface
, pStm
);
1002 hres
= IStream_Read(pStm
, &stdobjref
, sizeof(stdobjref
), &res
);
1003 if (hres
) return hres
;
1005 TRACE("oxid = %s, oid = %s, ipid = %s\n",
1006 wine_dbgstr_longlong(stdobjref
.oxid
),
1007 wine_dbgstr_longlong(stdobjref
.oid
),
1008 wine_dbgstr_guid(&stdobjref
.ipid
));
1010 if (!(apt
= apartment_findfromoxid(stdobjref
.oxid
, TRUE
)))
1012 WARN("Could not map OXID %s to apartment object\n",
1013 wine_dbgstr_longlong(stdobjref
.oxid
));
1014 return RPC_E_INVALID_OBJREF
;
1017 if (!(stubmgr
= get_stub_manager(apt
, stdobjref
.oid
)))
1019 ERR("could not map MID to stub manager, oxid=%s, oid=%s\n",
1020 wine_dbgstr_longlong(stdobjref
.oxid
), wine_dbgstr_longlong(stdobjref
.oid
));
1021 return RPC_E_INVALID_OBJREF
;
1024 stub_manager_release_marshal_data(stubmgr
, stdobjref
.cPublicRefs
);
1026 stub_manager_int_release(stubmgr
);
1027 apartment_release(apt
);
1032 static HRESULT WINAPI
1033 StdMarshalImpl_DisconnectObject(LPMARSHAL iface
, DWORD dwReserved
)
1035 FIXME("(), stub!\n");
1039 static const IMarshalVtbl VT_StdMarshal
=
1041 StdMarshalImpl_QueryInterface
,
1042 StdMarshalImpl_AddRef
,
1043 StdMarshalImpl_Release
,
1044 StdMarshalImpl_GetUnmarshalClass
,
1045 StdMarshalImpl_GetMarshalSizeMax
,
1046 StdMarshalImpl_MarshalInterface
,
1047 StdMarshalImpl_UnmarshalInterface
,
1048 StdMarshalImpl_ReleaseMarshalData
,
1049 StdMarshalImpl_DisconnectObject
1052 static HRESULT
StdMarshalImpl_Construct(REFIID riid
, void** ppvObject
)
1054 StdMarshalImpl
* pStdMarshal
=
1055 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(StdMarshalImpl
));
1057 return E_OUTOFMEMORY
;
1058 pStdMarshal
->lpvtbl
= &VT_StdMarshal
;
1059 pStdMarshal
->ref
= 0;
1060 return IMarshal_QueryInterface((IMarshal
*)pStdMarshal
, riid
, ppvObject
);
1063 /***********************************************************************
1064 * CoGetStandardMarshal [OLE32.@]
1066 * Gets or creates a standard marshal object.
1069 * riid [I] Interface identifier of the pUnk object.
1070 * pUnk [I] Optional. Object to get the marshal object for.
1071 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1072 * pvDestContext [I] Reserved. Must be NULL.
1073 * mshlflags [I] Flags affecting the marshaling process.
1074 * ppMarshal [O] Address where marshal object will be stored.
1078 * Failure: HRESULT code.
1082 * The function retrieves the IMarshal object associated with an object if
1083 * that object is currently an active stub, otherwise a new marshal object is
1086 HRESULT WINAPI
CoGetStandardMarshal(REFIID riid
, IUnknown
*pUnk
,
1087 DWORD dwDestContext
, LPVOID pvDestContext
,
1088 DWORD mshlflags
, LPMARSHAL
*ppMarshal
)
1094 FIXME("(%s,NULL,%lx,%p,%lx,%p), unimplemented yet.\n",
1095 debugstr_guid(riid
),dwDestContext
,pvDestContext
,mshlflags
,ppMarshal
);
1098 TRACE("(%s,%p,%lx,%p,%lx,%p)\n",
1099 debugstr_guid(riid
),pUnk
,dwDestContext
,pvDestContext
,mshlflags
,ppMarshal
);
1100 *ppMarshal
= HeapAlloc(GetProcessHeap(),0,sizeof(StdMarshalImpl
));
1101 dm
= (StdMarshalImpl
*) *ppMarshal
;
1102 if (!dm
) return E_FAIL
;
1103 dm
->lpvtbl
= &VT_StdMarshal
;
1107 dm
->dwDestContext
= dwDestContext
;
1108 dm
->pvDestContext
= pvDestContext
;
1109 dm
->mshlflags
= mshlflags
;
1113 /***********************************************************************
1114 * get_marshaler [internal]
1116 * Retrieves an IMarshal interface for an object.
1118 static HRESULT
get_marshaler(REFIID riid
, IUnknown
*pUnk
, DWORD dwDestContext
,
1119 void *pvDestContext
, DWORD mshlFlags
,
1120 LPMARSHAL
*pMarshal
)
1126 hr
= IUnknown_QueryInterface(pUnk
, &IID_IMarshal
, (LPVOID
*)pMarshal
);
1128 hr
= CoGetStandardMarshal(riid
, pUnk
, dwDestContext
, pvDestContext
,
1129 mshlFlags
, pMarshal
);
1133 /***********************************************************************
1134 * get_unmarshaler_from_stream [internal]
1136 * Creates an IMarshal* object according to the data marshaled to the stream.
1137 * The function leaves the stream pointer at the start of the data written
1138 * to the stream by the IMarshal* object.
1140 static HRESULT
get_unmarshaler_from_stream(IStream
*stream
, IMarshal
**marshal
, IID
*iid
)
1146 /* read common OBJREF header */
1147 hr
= IStream_Read(stream
, &objref
, FIELD_OFFSET(OBJREF
, u_objref
), &res
);
1148 if (hr
|| (res
!= FIELD_OFFSET(OBJREF
, u_objref
)))
1150 ERR("Failed to read common OBJREF header, 0x%08lx\n", hr
);
1151 return STG_E_READFAULT
;
1154 /* sanity check on header */
1155 if (objref
.signature
!= OBJREF_SIGNATURE
)
1157 ERR("Bad OBJREF signature 0x%08lx\n", objref
.signature
);
1158 return RPC_E_INVALID_OBJREF
;
1161 if (iid
) *iid
= objref
.iid
;
1163 /* FIXME: handler marshaling */
1164 if (objref
.flags
& OBJREF_STANDARD
)
1166 TRACE("Using standard unmarshaling\n");
1167 hr
= StdMarshalImpl_Construct(&IID_IMarshal
, (LPVOID
*)marshal
);
1169 else if (objref
.flags
& OBJREF_CUSTOM
)
1171 ULONG custom_header_size
= FIELD_OFFSET(OBJREF
, u_objref
.u_custom
.pData
) -
1172 FIELD_OFFSET(OBJREF
, u_objref
.u_custom
);
1173 TRACE("Using custom unmarshaling\n");
1174 /* read constant sized OR_CUSTOM data from stream */
1175 hr
= IStream_Read(stream
, &objref
.u_objref
.u_custom
,
1176 custom_header_size
, &res
);
1177 if (hr
|| (res
!= custom_header_size
))
1179 ERR("Failed to read OR_CUSTOM header, 0x%08lx\n", hr
);
1180 return STG_E_READFAULT
;
1182 /* now create the marshaler specified in the stream */
1183 hr
= CoCreateInstance(&objref
.u_objref
.u_custom
.clsid
, NULL
,
1184 CLSCTX_INPROC_SERVER
, &IID_IMarshal
,
1189 FIXME("Invalid or unimplemented marshaling type specified: %lx\n",
1191 return RPC_E_INVALID_OBJREF
;
1195 ERR("Failed to create marshal, 0x%08lx\n", hr
);
1200 /***********************************************************************
1201 * CoGetMarshalSizeMax [OLE32.@]
1203 * Gets the maximum amount of data that will be needed by a marshal.
1206 * pulSize [O] Address where maximum marshal size will be stored.
1207 * riid [I] Identifier of the interface to marshal.
1208 * pUnk [I] Pointer to the object to marshal.
1209 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1210 * pvDestContext [I] Reserved. Must be NULL.
1211 * mshlFlags [I] Flags that affect the marshaling. See CoMarshalInterface().
1215 * Failure: HRESULT code.
1218 * CoMarshalInterface().
1220 HRESULT WINAPI
CoGetMarshalSizeMax(ULONG
*pulSize
, REFIID riid
, IUnknown
*pUnk
,
1221 DWORD dwDestContext
, void *pvDestContext
,
1226 CLSID marshaler_clsid
;
1228 hr
= get_marshaler(riid
, pUnk
, dwDestContext
, pvDestContext
, mshlFlags
, &pMarshal
);
1232 hr
= IMarshal_GetUnmarshalClass(pMarshal
, riid
, pUnk
, dwDestContext
,
1233 pvDestContext
, mshlFlags
, &marshaler_clsid
);
1236 ERR("IMarshal::GetUnmarshalClass failed, 0x%08lx\n", hr
);
1237 IMarshal_Release(pMarshal
);
1241 hr
= IMarshal_GetMarshalSizeMax(pMarshal
, riid
, pUnk
, dwDestContext
,
1242 pvDestContext
, mshlFlags
, pulSize
);
1243 /* add on the size of the common header */
1244 *pulSize
+= FIELD_OFFSET(OBJREF
, u_objref
);
1246 /* if custom marshaling, add on size of custom header */
1247 if (!IsEqualCLSID(&marshaler_clsid
, &CLSID_DfMarshal
))
1248 *pulSize
+= FIELD_OFFSET(OBJREF
, u_objref
.u_custom
.pData
) -
1249 FIELD_OFFSET(OBJREF
, u_objref
.u_custom
);
1251 IMarshal_Release(pMarshal
);
1256 /***********************************************************************
1257 * CoMarshalInterface [OLE32.@]
1259 * Marshals an interface into a stream so that the object can then be
1260 * unmarshaled from another COM apartment and used remotely.
1263 * pStream [I] Stream the object will be marshaled into.
1264 * riid [I] Identifier of the interface to marshal.
1265 * pUnk [I] Pointer to the object to marshal.
1266 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1267 * pvDestContext [I] Reserved. Must be NULL.
1268 * mshlFlags [I] Flags that affect the marshaling. See notes.
1272 * Failure: HRESULT code.
1276 * The mshlFlags parameter can take one or more of the following flags:
1277 *| MSHLFLAGS_NORMAL - Unmarshal once, releases stub on last proxy release.
1278 *| MSHLFLAGS_TABLESTRONG - Unmarshal many, release when CoReleaseMarshalData() called.
1279 *| MSHLFLAGS_TABLEWEAK - Unmarshal many, releases stub on last proxy release.
1280 *| MSHLFLAGS_NOPING - No automatic garbage collection (and so reduces network traffic).
1282 * If a marshaled object is not unmarshaled, then CoReleaseMarshalData() must
1283 * be called in order to release the resources used in the marshaling.
1286 * CoUnmarshalInterface(), CoReleaseMarshalData().
1288 HRESULT WINAPI
CoMarshalInterface(IStream
*pStream
, REFIID riid
, IUnknown
*pUnk
,
1289 DWORD dwDestContext
, void *pvDestContext
,
1293 CLSID marshaler_clsid
;
1297 TRACE("(%p, %s, %p, %lx, %p, %lx)\n", pStream
, debugstr_guid(riid
), pUnk
,
1298 dwDestContext
, pvDestContext
, mshlFlags
);
1301 return E_INVALIDARG
;
1303 objref
.signature
= OBJREF_SIGNATURE
;
1306 /* get the marshaler for the specified interface */
1307 hr
= get_marshaler(riid
, pUnk
, dwDestContext
, pvDestContext
, mshlFlags
, &pMarshal
);
1310 ERR("Failed to get marshaller, 0x%08lx\n", hr
);
1314 hr
= IMarshal_GetUnmarshalClass(pMarshal
, riid
, pUnk
, dwDestContext
,
1315 pvDestContext
, mshlFlags
, &marshaler_clsid
);
1318 ERR("IMarshal::GetUnmarshalClass failed, 0x%08lx\n", hr
);
1322 /* FIXME: implement handler marshaling too */
1323 if (IsEqualCLSID(&marshaler_clsid
, &CLSID_DfMarshal
))
1325 TRACE("Using standard marshaling\n");
1326 objref
.flags
= OBJREF_STANDARD
;
1328 /* write the common OBJREF header to the stream */
1329 hr
= IStream_Write(pStream
, &objref
, FIELD_OFFSET(OBJREF
, u_objref
), NULL
);
1332 ERR("Failed to write OBJREF header to stream, 0x%08lx\n", hr
);
1338 TRACE("Using custom marshaling\n");
1339 objref
.flags
= OBJREF_CUSTOM
;
1340 objref
.u_objref
.u_custom
.clsid
= marshaler_clsid
;
1341 objref
.u_objref
.u_custom
.cbExtension
= 0;
1342 objref
.u_objref
.u_custom
.size
= 0;
1343 hr
= IMarshal_GetMarshalSizeMax(pMarshal
, riid
, pUnk
, dwDestContext
,
1344 pvDestContext
, mshlFlags
,
1345 &objref
.u_objref
.u_custom
.size
);
1348 ERR("Failed to get max size of marshal data, error 0x%08lx\n", hr
);
1351 /* write constant sized common header and OR_CUSTOM data into stream */
1352 hr
= IStream_Write(pStream
, &objref
,
1353 FIELD_OFFSET(OBJREF
, u_objref
.u_custom
.pData
), NULL
);
1356 ERR("Failed to write OR_CUSTOM header to stream with 0x%08lx\n", hr
);
1361 TRACE("Calling IMarshal::MarshalInterace\n");
1362 /* call helper object to do the actual marshaling */
1363 hr
= IMarshal_MarshalInterface(pMarshal
, pStream
, riid
, pUnk
, dwDestContext
,
1364 pvDestContext
, mshlFlags
);
1368 ERR("Failed to marshal the interface %s, %lx\n", debugstr_guid(riid
), hr
);
1373 IMarshal_Release(pMarshal
);
1375 TRACE("completed with hr 0x%08lx\n", hr
);
1380 /***********************************************************************
1381 * CoUnmarshalInterface [OLE32.@]
1383 * Unmarshals an object from a stream by creating a proxy to the remote
1384 * object, if necessary.
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.
1395 * Failure: HRESULT code.
1398 * CoMarshalInterface().
1400 HRESULT WINAPI
CoUnmarshalInterface(IStream
*pStream
, REFIID riid
, LPVOID
*ppv
)
1407 TRACE("(%p, %s, %p)\n", pStream
, debugstr_guid(riid
), ppv
);
1409 hr
= get_unmarshaler_from_stream(pStream
, &pMarshal
, &iid
);
1413 /* call the helper object to do the actual unmarshaling */
1414 hr
= IMarshal_UnmarshalInterface(pMarshal
, pStream
, &iid
, (LPVOID
*)&object
);
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
))
1424 if (!IsEqualIID(riid
, &iid
))
1426 TRACE("requested interface != marshalled interface, additional QI needed\n");
1427 hr
= IUnknown_QueryInterface(object
, &iid
, ppv
);
1429 ERR("Couldn't query for interface %s, hr = 0x%08lx\n",
1430 debugstr_guid(riid
), hr
);
1431 IUnknown_Release(object
);
1439 IMarshal_Release(pMarshal
);
1441 TRACE("completed with hr 0x%lx\n", hr
);
1446 /***********************************************************************
1447 * CoReleaseMarshalData [OLE32.@]
1449 * Releases resources associated with an object that has been marshaled into
1454 * pStream [I] The stream that the object has been marshaled into.
1458 * Failure: HRESULT error code.
1462 * Call this function to release resources associated with a normal or
1463 * table-weak marshal that will not be unmarshaled, and all table-strong
1464 * marshals when they are no longer needed.
1467 * CoMarshalInterface(), CoUnmarshalInterface().
1469 HRESULT WINAPI
CoReleaseMarshalData(IStream
*pStream
)
1474 TRACE("(%p)\n", pStream
);
1476 hr
= get_unmarshaler_from_stream(pStream
, &pMarshal
, NULL
);
1480 /* call the helper object to do the releasing of marshal data */
1481 hr
= IMarshal_ReleaseMarshalData(pMarshal
, pStream
);
1483 ERR("IMarshal::ReleaseMarshalData failed with error 0x%08lx\n", hr
);
1485 IMarshal_Release(pMarshal
);
1490 /***********************************************************************
1491 * CoMarshalInterThreadInterfaceInStream [OLE32.@]
1493 * Marshal an interface across threads in the same process.
1496 * riid [I] Identifier of the interface to be marshalled.
1497 * pUnk [I] Pointer to IUnknown-derived interface that will be marshalled.
1498 * ppStm [O] Pointer to IStream object that is created and then used to store the marshalled inteface.
1502 * Failure: E_OUTOFMEMORY and other COM error codes
1505 * CoMarshalInterface(), CoUnmarshalInterface() and CoGetInterfaceAndReleaseStream()
1507 HRESULT WINAPI
CoMarshalInterThreadInterfaceInStream(
1508 REFIID riid
, LPUNKNOWN pUnk
, LPSTREAM
* ppStm
)
1510 ULARGE_INTEGER xpos
;
1511 LARGE_INTEGER seekto
;
1514 TRACE("(%s, %p, %p)\n",debugstr_guid(riid
), pUnk
, ppStm
);
1516 hres
= CreateStreamOnHGlobal(0, TRUE
, ppStm
);
1517 if (FAILED(hres
)) return hres
;
1518 hres
= CoMarshalInterface(*ppStm
, riid
, pUnk
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_NORMAL
);
1520 /* FIXME: is this needed? */
1521 memset(&seekto
,0,sizeof(seekto
));
1522 IStream_Seek(*ppStm
,seekto
,SEEK_SET
,&xpos
);
1527 /***********************************************************************
1528 * CoGetInterfaceAndReleaseStream [OLE32.@]
1530 * Unmarshalls an inteface from a stream and then releases the stream.
1533 * pStm [I] Stream that contains the marshalled inteface.
1534 * riid [I] Interface identifier of the object to unmarshall.
1535 * ppv [O] Address of pointer where the requested interface object will be stored.
1539 * Failure: A COM error code
1542 * CoMarshalInterThreadInterfaceInStream() and CoUnmarshalInteface()
1544 HRESULT WINAPI
CoGetInterfaceAndReleaseStream(LPSTREAM pStm
, REFIID riid
,
1549 TRACE("(%p, %s, %p)\n", pStm
, debugstr_guid(riid
), ppv
);
1551 hres
= CoUnmarshalInterface(pStm
, riid
, ppv
);
1552 IStream_Release(pStm
);
1556 static HRESULT WINAPI
StdMarshalCF_QueryInterface(LPCLASSFACTORY iface
,
1557 REFIID riid
, LPVOID
*ppv
)
1560 if (IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IClassFactory
))
1562 *ppv
= (LPVOID
)iface
;
1565 return E_NOINTERFACE
;
1568 static ULONG WINAPI
StdMarshalCF_AddRef(LPCLASSFACTORY iface
)
1570 return 2; /* non-heap based object */
1573 static ULONG WINAPI
StdMarshalCF_Release(LPCLASSFACTORY iface
)
1575 return 1; /* non-heap based object */
1578 static HRESULT WINAPI
StdMarshalCF_CreateInstance(LPCLASSFACTORY iface
,
1579 LPUNKNOWN pUnk
, REFIID riid
, LPVOID
*ppv
)
1581 if (IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IMarshal
))
1582 return StdMarshalImpl_Construct(riid
, ppv
);
1584 FIXME("(%s), not supported.\n",debugstr_guid(riid
));
1585 return E_NOINTERFACE
;
1588 static HRESULT WINAPI
StdMarshalCF_LockServer(LPCLASSFACTORY iface
, BOOL fLock
)
1590 FIXME("(%d), stub!\n",fLock
);
1594 static const IClassFactoryVtbl StdMarshalCFVtbl
=
1596 StdMarshalCF_QueryInterface
,
1597 StdMarshalCF_AddRef
,
1598 StdMarshalCF_Release
,
1599 StdMarshalCF_CreateInstance
,
1600 StdMarshalCF_LockServer
1602 static const IClassFactoryVtbl
*StdMarshalCF
= &StdMarshalCFVtbl
;
1604 HRESULT
MARSHAL_GetStandardMarshalCF(LPVOID
*ppv
)
1606 *ppv
= &StdMarshalCF
;
1610 /***********************************************************************
1611 * CoMarshalHresult [OLE32.@]
1613 * Marshals an HRESULT value into a stream.
1616 * pStm [I] Stream that hresult will be marshalled into.
1617 * hresult [I] HRESULT to be marshalled.
1621 * Failure: A COM error code
1624 * CoUnmarshalHresult().
1626 HRESULT WINAPI
CoMarshalHresult(LPSTREAM pStm
, HRESULT hresult
)
1628 return IStream_Write(pStm
, &hresult
, sizeof(hresult
), NULL
);
1631 /***********************************************************************
1632 * CoUnmarshalHresult [OLE32.@]
1634 * Unmarshals an HRESULT value from a stream.
1637 * pStm [I] Stream that hresult will be unmarshalled from.
1638 * phresult [I] Pointer to HRESULT where the value will be unmarshalled to.
1642 * Failure: A COM error code
1645 * CoMarshalHresult().
1647 HRESULT WINAPI
CoUnmarshalHresult(LPSTREAM pStm
, HRESULT
* phresult
)
1649 return IStream_Read(pStm
, phresult
, sizeof(*phresult
), NULL
);