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 /* marshals an object into a STDOBJREF structure */
88 HRESULT
marshal_object(APARTMENT
*apt
, STDOBJREF
*stdobjref
, REFIID riid
, IUnknown
*object
, MSHLFLAGS mshlflags
)
90 struct stub_manager
*manager
;
91 struct ifstub
*ifstub
;
93 IRpcStubBuffer
*stub
= NULL
;
95 IUnknown
*iobject
= NULL
; /* object of type riid */
97 hr
= apartment_getoxid(apt
, &stdobjref
->oxid
);
101 hr
= IUnknown_QueryInterface(object
, riid
, (void **)&iobject
);
104 ERR("object doesn't expose interface %s, failing with error 0x%08lx\n",
105 debugstr_guid(riid
), hr
);
106 return E_NOINTERFACE
;
109 /* IUnknown doesn't require a stub buffer, because it never goes out on
111 if (!IsEqualIID(riid
, &IID_IUnknown
))
113 IPSFactoryBuffer
*psfb
;
115 hr
= get_facbuf_for_iid(riid
, &psfb
);
118 ERR("couldn't get IPSFactory buffer for interface %s\n", debugstr_guid(riid
));
119 IUnknown_Release(iobject
);
123 hr
= IPSFactoryBuffer_CreateStub(psfb
, riid
, iobject
, &stub
);
124 IPSFactoryBuffer_Release(psfb
);
127 ERR("Failed to create an IRpcStubBuffer from IPSFactory for %s\n", debugstr_guid(riid
));
128 IUnknown_Release(iobject
);
133 if (mshlflags
& MSHLFLAGS_NOPING
)
134 stdobjref
->flags
= SORF_NOPING
;
136 stdobjref
->flags
= SORF_NULL
;
138 if ((manager
= get_stub_manager_from_object(apt
, object
)))
139 TRACE("registering new ifstub on pre-existing manager\n");
142 TRACE("constructing new stub manager\n");
144 manager
= new_stub_manager(apt
, object
);
147 if (stub
) IRpcStubBuffer_Release(stub
);
148 IUnknown_Release(iobject
);
149 return E_OUTOFMEMORY
;
152 stdobjref
->oid
= manager
->oid
;
154 tablemarshal
= ((mshlflags
& MSHLFLAGS_TABLESTRONG
) || (mshlflags
& MSHLFLAGS_TABLEWEAK
));
156 /* make sure ifstub that we are creating is unique */
157 ifstub
= stub_manager_find_ifstub(manager
, riid
, mshlflags
);
160 ifstub
= stub_manager_new_ifstub(manager
, stub
, iobject
, riid
, mshlflags
);
161 IUnknown_Release(iobject
);
162 if (stub
) IRpcStubBuffer_Release(stub
);
165 stub_manager_int_release(manager
);
166 /* FIXME: should we do another release to completely destroy the
168 return E_OUTOFMEMORY
;
174 stdobjref
->cPublicRefs
= NORMALEXTREFS
;
175 stub_manager_ext_addref(manager
, stdobjref
->cPublicRefs
);
179 stdobjref
->cPublicRefs
= 0;
180 if (mshlflags
& MSHLFLAGS_TABLESTRONG
)
181 stub_manager_ext_addref(manager
, 1);
184 /* FIXME: check return value */
185 RPC_RegisterInterface(riid
);
187 stdobjref
->ipid
= ifstub
->ipid
;
189 stub_manager_int_release(manager
);
195 /* Client-side identity of the server object */
197 static HRESULT
proxy_manager_get_remunknown(struct proxy_manager
* This
, IRemUnknown
**remunk
);
198 static void proxy_manager_destroy(struct proxy_manager
* This
);
199 static HRESULT
proxy_manager_find_ifproxy(struct proxy_manager
* This
, REFIID riid
, struct ifproxy
** ifproxy_found
);
200 static HRESULT
proxy_manager_query_local_interface(struct proxy_manager
* This
, REFIID riid
, void ** ppv
);
202 static HRESULT WINAPI
ClientIdentity_QueryInterface(IMultiQI
* iface
, REFIID riid
, void ** ppv
)
207 TRACE("%s\n", debugstr_guid(riid
));
210 hr
= IMultiQI_QueryMultipleInterfaces(iface
, 1, &mqi
);
211 *ppv
= (void *)mqi
.pItf
;
216 static ULONG WINAPI
ClientIdentity_AddRef(IMultiQI
* iface
)
218 struct proxy_manager
* This
= (struct proxy_manager
*)iface
;
219 TRACE("%p - before %ld\n", iface
, This
->refs
);
220 return InterlockedIncrement(&This
->refs
);
223 static ULONG WINAPI
ClientIdentity_Release(IMultiQI
* iface
)
225 struct proxy_manager
* This
= (struct proxy_manager
*)iface
;
226 ULONG refs
= InterlockedDecrement(&This
->refs
);
227 TRACE("%p - after %ld\n", iface
, refs
);
229 proxy_manager_destroy(This
);
233 static HRESULT WINAPI
ClientIdentity_QueryMultipleInterfaces(IMultiQI
*iface
, ULONG cMQIs
, MULTI_QI
*pMQIs
)
235 struct proxy_manager
* This
= (struct proxy_manager
*)iface
;
236 REMQIRESULT
*qiresults
= NULL
;
237 ULONG nonlocal_mqis
= 0;
239 ULONG successful_mqis
= 0;
240 IID
*iids
= HeapAlloc(GetProcessHeap(), 0, cMQIs
* sizeof(*iids
));
241 /* mapping of RemQueryInterface index to QueryMultipleInterfaces index */
242 ULONG
*mapping
= HeapAlloc(GetProcessHeap(), 0, cMQIs
* sizeof(*mapping
));
244 TRACE("cMQIs: %ld\n", cMQIs
);
246 /* try to get a local interface - this includes already active proxy
247 * interfaces and also interfaces exposed by the proxy manager */
248 for (i
= 0; i
< cMQIs
; i
++)
250 TRACE("iid[%ld] = %s\n", i
, debugstr_guid(pMQIs
[i
].pIID
));
251 pMQIs
[i
].hr
= proxy_manager_query_local_interface(This
, pMQIs
[i
].pIID
, (void **)&pMQIs
[i
].pItf
);
252 if (pMQIs
[i
].hr
== S_OK
)
256 iids
[nonlocal_mqis
] = *pMQIs
[i
].pIID
;
257 mapping
[nonlocal_mqis
] = i
;
262 TRACE("%ld interfaces not found locally\n", nonlocal_mqis
);
264 /* if we have more than one interface not found locally then we must try
265 * to query the remote object for it */
266 if (nonlocal_mqis
!= 0)
272 /* get the ipid of the first entry */
273 /* FIXME: should we implement ClientIdentity on the ifproxies instead
274 * of the proxy_manager so we use the correct ipid here? */
275 ipid
= &LIST_ENTRY(list_head(&This
->interfaces
), struct ifproxy
, entry
)->stdobjref
.ipid
;
277 /* get IRemUnknown proxy so we can communicate with the remote object */
278 hr
= proxy_manager_get_remunknown(This
, &remunk
);
282 hr
= IRemUnknown_RemQueryInterface(remunk
, ipid
, NORMALEXTREFS
,
283 nonlocal_mqis
, iids
, &qiresults
);
285 ERR("IRemUnknown_RemQueryInterface failed with error 0x%08lx\n", hr
);
288 /* IRemUnknown_RemQueryInterface can return S_FALSE if only some of
289 * the interfaces were returned */
292 /* try to unmarshal each object returned to us */
293 for (i
= 0; i
< nonlocal_mqis
; i
++)
295 ULONG index
= mapping
[i
];
296 HRESULT hrobj
= qiresults
[i
].hResult
;
298 hrobj
= unmarshal_object(&qiresults
[i
].std
, This
->parent
,
300 (void **)&pMQIs
[index
].pItf
);
305 ERR("Failed to get pointer to interface %s\n", debugstr_guid(pMQIs
[index
].pIID
));
306 pMQIs
[index
].hr
= hrobj
;
310 /* free the memory allocated by the proxy */
311 CoTaskMemFree(qiresults
);
314 TRACE("%ld/%ld successfully queried\n", successful_mqis
, cMQIs
);
316 HeapFree(GetProcessHeap(), 0, iids
);
317 HeapFree(GetProcessHeap(), 0, mapping
);
319 if (successful_mqis
== cMQIs
)
320 return S_OK
; /* we got all requested interfaces */
321 else if (successful_mqis
== 0)
322 return E_NOINTERFACE
; /* we didn't get any interfaces */
324 return S_FALSE
; /* we got some interfaces */
327 static const IMultiQIVtbl ClientIdentity_Vtbl
=
329 ClientIdentity_QueryInterface
,
330 ClientIdentity_AddRef
,
331 ClientIdentity_Release
,
332 ClientIdentity_QueryMultipleInterfaces
335 static HRESULT WINAPI
Proxy_QueryInterface(IMarshal
*iface
, REFIID riid
, void **ppvObject
)
337 ICOM_THIS_MULTI(struct proxy_manager
, lpVtblMarshal
, iface
);
338 return IMultiQI_QueryInterface((IMultiQI
*)&This
->lpVtbl
, riid
, ppvObject
);
341 static ULONG WINAPI
Proxy_AddRef(IMarshal
*iface
)
343 ICOM_THIS_MULTI(struct proxy_manager
, lpVtblMarshal
, iface
);
344 return IMultiQI_AddRef((IMultiQI
*)&This
->lpVtbl
);
347 /* FIXME: remove these */
348 static HRESULT WINAPI
StdMarshalImpl_GetUnmarshalClass(LPMARSHAL iface
, REFIID riid
, void* pv
, DWORD dwDestContext
, void* pvDestContext
, DWORD mshlflags
, CLSID
* pCid
);
349 static HRESULT WINAPI
StdMarshalImpl_GetMarshalSizeMax(LPMARSHAL iface
, REFIID riid
, void* pv
, DWORD dwDestContext
, void* pvDestContext
, DWORD mshlflags
, DWORD
* pSize
);
350 static HRESULT WINAPI
StdMarshalImpl_UnmarshalInterface(LPMARSHAL iface
, IStream
*pStm
, REFIID riid
, void **ppv
);
351 static HRESULT WINAPI
StdMarshalImpl_ReleaseMarshalData(LPMARSHAL iface
, IStream
*pStm
);
352 static HRESULT WINAPI
StdMarshalImpl_DisconnectObject(LPMARSHAL iface
, DWORD dwReserved
);
354 static ULONG WINAPI
Proxy_Release(IMarshal
*iface
)
356 ICOM_THIS_MULTI(struct proxy_manager
, lpVtblMarshal
, iface
);
357 return IMultiQI_Release((IMultiQI
*)&This
->lpVtbl
);
360 static HRESULT WINAPI
Proxy_MarshalInterface(
361 LPMARSHAL iface
, IStream
*pStm
, REFIID riid
, void* pv
, DWORD dwDestContext
,
362 void* pvDestContext
, DWORD mshlflags
)
364 ICOM_THIS_MULTI(struct proxy_manager
, lpVtblMarshal
, iface
);
368 struct ifproxy
*ifproxy
;
370 TRACE("(...,%s,...)\n", debugstr_guid(riid
));
372 hr
= proxy_manager_find_ifproxy(This
, riid
, &ifproxy
);
375 ERR("couldn't find proxy for interface %s, error 0x%08lx\n", debugstr_guid(riid
), hr
);
379 stdobjref
= ifproxy
->stdobjref
;
380 /* FIXME: optimization - share out proxy's public references if possible
381 * instead of making new proxy do a roundtrip through the server */
382 stdobjref
.cPublicRefs
= 0; /* InterlockedDecrement(&This->stdobjref.cPublicRefs) >= 0 ? 1 : 0 */
383 hr
= IStream_Write(pStm
, &stdobjref
, sizeof(stdobjref
), &res
);
388 static const IMarshalVtbl ProxyMarshal_Vtbl
=
390 Proxy_QueryInterface
,
393 StdMarshalImpl_GetUnmarshalClass
,
394 StdMarshalImpl_GetMarshalSizeMax
,
395 Proxy_MarshalInterface
,
396 StdMarshalImpl_UnmarshalInterface
,
397 StdMarshalImpl_ReleaseMarshalData
,
398 StdMarshalImpl_DisconnectObject
401 static HRESULT
ifproxy_get_public_ref(struct ifproxy
* This
)
405 if (WAIT_OBJECT_0
!= WaitForSingleObject(This
->parent
->remoting_mutex
, INFINITE
))
407 ERR("Wait failed for ifproxy %p\n", This
);
413 IRemUnknown
*remunk
= NULL
;
415 TRACE("getting public ref for ifproxy %p\n", This
);
417 hr
= proxy_manager_get_remunknown(This
->parent
, &remunk
);
422 rif
.ipid
= This
->stdobjref
.ipid
;
423 rif
.cPublicRefs
= NORMALEXTREFS
;
424 rif
.cPrivateRefs
= 0;
425 hr
= IRemUnknown_RemAddRef(remunk
, 1, &rif
, &hrref
);
426 if (hr
== S_OK
&& hrref
== S_OK
)
427 This
->refs
+= NORMALEXTREFS
;
429 ERR("IRemUnknown_RemAddRef returned with 0x%08lx, hrref = 0x%08lx\n", hr
, hrref
);
432 ReleaseMutex(This
->parent
->remoting_mutex
);
437 static HRESULT
ifproxy_release_public_refs(struct ifproxy
* This
)
441 if (WAIT_OBJECT_0
!= WaitForSingleObject(This
->parent
->remoting_mutex
, INFINITE
))
443 ERR("Wait failed for ifproxy %p\n", This
);
449 IRemUnknown
*remunk
= NULL
;
451 TRACE("releasing %ld refs\n", This
->refs
);
453 hr
= proxy_manager_get_remunknown(This
->parent
, &remunk
);
457 rif
.ipid
= This
->stdobjref
.ipid
;
458 rif
.cPublicRefs
= This
->refs
;
459 rif
.cPrivateRefs
= 0;
460 hr
= IRemUnknown_RemRelease(remunk
, 1, &rif
);
463 else if (hr
== RPC_E_DISCONNECTED
)
464 WARN("couldn't release references because object was "
465 "disconnected: oxid = %s, oid = %s\n",
466 wine_dbgstr_longlong(This
->parent
->oxid
),
467 wine_dbgstr_longlong(This
->parent
->oid
));
469 ERR("IRemUnknown_RemRelease failed with error 0x%08lx\n", hr
);
472 ReleaseMutex(This
->parent
->remoting_mutex
);
477 /* should be called inside This->parent->cs critical section */
478 static void ifproxy_disconnect(struct ifproxy
* This
)
480 ifproxy_release_public_refs(This
);
481 if (This
->proxy
) IRpcProxyBuffer_Disconnect(This
->proxy
);
483 IRpcChannelBuffer_Release(This
->chan
);
487 /* should be called in This->parent->cs critical section if it is an entry in parent's list */
488 static void ifproxy_destroy(struct ifproxy
* This
)
492 /* release public references to this object so that the stub can know
493 * when to destroy itself */
494 ifproxy_release_public_refs(This
);
496 list_remove(&This
->entry
);
500 IRpcChannelBuffer_Release(This
->chan
);
504 /* note: we don't call Release for This->proxy because its lifetime is
505 * controlled by the return value from ClientIdentity_Release, which this
506 * function is always called from */
508 HeapFree(GetProcessHeap(), 0, This
);
511 static HRESULT
proxy_manager_construct(
512 APARTMENT
* apt
, ULONG sorflags
, OXID oxid
, OID oid
,
513 struct proxy_manager
** proxy_manager
)
515 struct proxy_manager
* This
= HeapAlloc(GetProcessHeap(), 0, sizeof(*This
));
516 if (!This
) return E_OUTOFMEMORY
;
518 This
->remoting_mutex
= CreateMutexW(NULL
, FALSE
, NULL
);
519 if (!This
->remoting_mutex
)
521 HeapFree(GetProcessHeap(), 0, This
);
522 return HRESULT_FROM_WIN32(GetLastError());
525 This
->lpVtbl
= &ClientIdentity_Vtbl
;
526 This
->lpVtblMarshal
= &ProxyMarshal_Vtbl
;
528 list_init(&This
->entry
);
529 list_init(&This
->interfaces
);
531 InitializeCriticalSection(&This
->cs
);
532 DEBUG_SET_CRITSEC_NAME(&This
->cs
, "proxy_manager");
534 /* the apartment the object was unmarshaled into */
537 /* the source apartment and id of the object */
543 /* the DCOM draft specification states that the SORF_NOPING flag is
544 * proxy manager specific, not ifproxy specific, so this implies that we
545 * should store the STDOBJREF flags here in the proxy manager. */
546 This
->sorflags
= sorflags
;
548 /* we create the IRemUnknown proxy on demand */
551 EnterCriticalSection(&apt
->cs
);
552 /* FIXME: we are dependent on the ordering in here to make sure a proxy's
553 * IRemUnknown proxy doesn't get destroyed before the regual proxy does
554 * because we need the IRemUnknown proxy during the destruction of the
555 * regular proxy. Ideally, we should maintain a separate list for the
556 * IRemUnknown proxies that need late destruction */
557 list_add_tail(&apt
->proxies
, &This
->entry
);
558 LeaveCriticalSection(&apt
->cs
);
560 TRACE("%p created for OXID %s, OID %s\n", This
,
561 wine_dbgstr_longlong(oxid
), wine_dbgstr_longlong(oid
));
563 *proxy_manager
= This
;
567 static HRESULT
proxy_manager_query_local_interface(struct proxy_manager
* This
, REFIID riid
, void ** ppv
)
570 struct ifproxy
* ifproxy
;
572 TRACE("%s\n", debugstr_guid(riid
));
574 if (IsEqualIID(riid
, &IID_IUnknown
) ||
575 IsEqualIID(riid
, &IID_IMultiQI
))
577 *ppv
= (void *)&This
->lpVtbl
;
578 IUnknown_AddRef((IUnknown
*)*ppv
);
581 if (IsEqualIID(riid
, &IID_IMarshal
))
583 *ppv
= (void *)&This
->lpVtblMarshal
;
584 IUnknown_AddRef((IUnknown
*)*ppv
);
587 if (IsEqualIID(riid
, &IID_IClientSecurity
))
589 FIXME("requesting IClientSecurity, but it is unimplemented\n");
591 return E_NOINTERFACE
;
594 hr
= proxy_manager_find_ifproxy(This
, riid
, &ifproxy
);
597 *ppv
= ifproxy
->iface
;
598 IUnknown_AddRef((IUnknown
*)*ppv
);
603 return E_NOINTERFACE
;
606 static HRESULT
proxy_manager_create_ifproxy(
607 struct proxy_manager
* This
, const STDOBJREF
*stdobjref
, REFIID riid
,
608 IRpcChannelBuffer
* channel
, struct ifproxy
** iif_out
)
611 IPSFactoryBuffer
* psfb
;
612 struct ifproxy
* ifproxy
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ifproxy
));
613 if (!ifproxy
) return E_OUTOFMEMORY
;
615 list_init(&ifproxy
->entry
);
617 ifproxy
->parent
= This
;
618 ifproxy
->stdobjref
= *stdobjref
;
619 ifproxy
->iid
= *riid
;
620 ifproxy
->refs
= stdobjref
->cPublicRefs
;
621 ifproxy
->proxy
= NULL
;
624 ifproxy
->chan
= channel
; /* FIXME: we should take the binding strings and construct the channel in this function */
626 /* the IUnknown interface is special because it does not have a
627 * proxy associated with the ifproxy as we handle IUnknown ourselves */
628 if (IsEqualIID(riid
, &IID_IUnknown
))
630 ifproxy
->iface
= (void *)&This
->lpVtbl
;
631 IMultiQI_AddRef((IMultiQI
*)&This
->lpVtbl
);
636 hr
= get_facbuf_for_iid(riid
, &psfb
);
639 /* important note: the outer unknown is set to the proxy manager.
640 * This ensures the COM identity rules are not violated, by having a
641 * one-to-one mapping of objects on the proxy side to objects on the
642 * stub side, no matter which interface you view the object through */
643 hr
= IPSFactoryBuffer_CreateProxy(psfb
, (IUnknown
*)&This
->lpVtbl
, riid
,
644 &ifproxy
->proxy
, &ifproxy
->iface
);
645 IPSFactoryBuffer_Release(psfb
);
647 ERR("Could not create proxy for interface %s, error 0x%08lx\n",
648 debugstr_guid(riid
), hr
);
651 ERR("Could not get IPSFactoryBuffer for interface %s, error 0x%08lx\n",
652 debugstr_guid(riid
), hr
);
655 hr
= IRpcProxyBuffer_Connect(ifproxy
->proxy
, ifproxy
->chan
);
658 /* get at least one external reference to the object to keep it alive */
660 hr
= ifproxy_get_public_ref(ifproxy
);
664 EnterCriticalSection(&This
->cs
);
665 list_add_tail(&This
->interfaces
, &ifproxy
->entry
);
666 LeaveCriticalSection(&This
->cs
);
669 TRACE("ifproxy %p created for IPID %s, interface %s with %lu public refs\n",
670 ifproxy
, debugstr_guid(&stdobjref
->ipid
), debugstr_guid(riid
), stdobjref
->cPublicRefs
);
673 ifproxy_destroy(ifproxy
);
678 static HRESULT
proxy_manager_find_ifproxy(struct proxy_manager
* This
, REFIID riid
, struct ifproxy
** ifproxy_found
)
680 HRESULT hr
= E_NOINTERFACE
; /* assume not found */
681 struct list
* cursor
;
683 EnterCriticalSection(&This
->cs
);
684 LIST_FOR_EACH(cursor
, &This
->interfaces
)
686 struct ifproxy
* ifproxy
= LIST_ENTRY(cursor
, struct ifproxy
, entry
);
687 if (IsEqualIID(riid
, &ifproxy
->iid
))
689 *ifproxy_found
= ifproxy
;
694 LeaveCriticalSection(&This
->cs
);
699 static void proxy_manager_disconnect(struct proxy_manager
* This
)
701 struct list
* cursor
;
703 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This
->oxid
),
704 wine_dbgstr_longlong(This
->oid
));
706 EnterCriticalSection(&This
->cs
);
708 /* SORFP_NOLIFTIMEMGMT proxies (for IRemUnknown) shouldn't be
709 * disconnected - it won't do anything anyway, except cause
710 * problems for other objects that depend on this proxy always
712 if (!(This
->sorflags
& SORFP_NOLIFETIMEMGMT
))
714 LIST_FOR_EACH(cursor
, &This
->interfaces
)
716 struct ifproxy
* ifproxy
= LIST_ENTRY(cursor
, struct ifproxy
, entry
);
717 ifproxy_disconnect(ifproxy
);
721 /* apartment is being destroyed so don't keep a pointer around to it */
724 LeaveCriticalSection(&This
->cs
);
727 static HRESULT
proxy_manager_get_remunknown(struct proxy_manager
* This
, IRemUnknown
**remunk
)
731 /* we don't want to try and unmarshal or use IRemUnknown if we don't want
732 * lifetime management */
733 if (This
->sorflags
& SORFP_NOLIFETIMEMGMT
)
736 EnterCriticalSection(&This
->cs
);
738 /* already created - return existing object */
739 *remunk
= This
->remunk
;
740 else if (!This
->parent
)
741 /* disconnected - we can't create IRemUnknown */
746 /* Don't want IRemUnknown lifetime management as this is IRemUnknown!
747 * We also don't care about whether or not the stub is still alive */
748 stdobjref
.flags
= SORFP_NOLIFETIMEMGMT
| SORF_NOPING
;
749 stdobjref
.cPublicRefs
= 1;
750 /* oxid of destination object */
751 stdobjref
.oxid
= This
->oxid
;
752 /* FIXME: what should be used for the oid? The DCOM draft doesn't say */
753 stdobjref
.oid
= (OID
)-1;
754 /* FIXME: this is a hack around not having an OXID resolver yet -
755 * the OXID resolver should give us the IPID of the IRemUnknown
757 stdobjref
.ipid
.Data1
= 0xffffffff;
758 stdobjref
.ipid
.Data2
= 0xffff;
759 stdobjref
.ipid
.Data3
= 0xffff;
760 assert(sizeof(stdobjref
.ipid
.Data4
) == sizeof(stdobjref
.oxid
));
761 memcpy(&stdobjref
.ipid
.Data4
, &stdobjref
.oxid
, sizeof(OXID
));
763 /* do the unmarshal */
764 hr
= unmarshal_object(&stdobjref
, This
->parent
, &IID_IRemUnknown
, (void**)&This
->remunk
);
766 *remunk
= This
->remunk
;
768 LeaveCriticalSection(&This
->cs
);
770 TRACE("got IRemUnknown* pointer %p, hr = 0x%08lx\n", *remunk
, hr
);
775 /* destroys a proxy manager, freeing the memory it used.
776 * Note: this function should not be called from a list iteration in the
777 * apartment, due to the fact that it removes itself from the apartment and
778 * it could add a proxy to IRemUnknown into the apartment. */
779 static void proxy_manager_destroy(struct proxy_manager
* This
)
781 struct list
* cursor
;
783 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This
->oxid
),
784 wine_dbgstr_longlong(This
->oid
));
788 EnterCriticalSection(&This
->parent
->cs
);
790 /* remove ourself from the list of proxy objects in the apartment */
791 LIST_FOR_EACH(cursor
, &This
->parent
->proxies
)
793 if (cursor
== &This
->entry
)
795 list_remove(&This
->entry
);
800 LeaveCriticalSection(&This
->parent
->cs
);
803 /* destroy all of the interface proxies */
804 while ((cursor
= list_head(&This
->interfaces
)))
806 struct ifproxy
* ifproxy
= LIST_ENTRY(cursor
, struct ifproxy
, entry
);
807 ifproxy_destroy(ifproxy
);
810 if (This
->remunk
) IRemUnknown_Release(This
->remunk
);
812 DEBUG_CLEAR_CRITSEC_NAME(&This
->cs
);
813 DeleteCriticalSection(&This
->cs
);
815 CloseHandle(This
->remoting_mutex
);
817 HeapFree(GetProcessHeap(), 0, This
);
820 /* finds the proxy manager corresponding to a given OXID and OID that has
821 * been unmarshaled in the specified apartment. The caller must release the
822 * reference to the proxy_manager when the object is no longer used. */
823 static BOOL
find_proxy_manager(APARTMENT
* apt
, OXID oxid
, OID oid
, struct proxy_manager
** proxy_found
)
826 struct list
* cursor
;
828 EnterCriticalSection(&apt
->cs
);
829 LIST_FOR_EACH(cursor
, &apt
->proxies
)
831 struct proxy_manager
* proxy
= LIST_ENTRY(cursor
, struct proxy_manager
, entry
);
832 if ((oxid
== proxy
->oxid
) && (oid
== proxy
->oid
))
834 *proxy_found
= proxy
;
835 ClientIdentity_AddRef((IMultiQI
*)&proxy
->lpVtbl
);
840 LeaveCriticalSection(&apt
->cs
);
844 HRESULT
apartment_disconnectproxies(struct apartment
*apt
)
846 struct list
* cursor
;
848 LIST_FOR_EACH(cursor
, &apt
->proxies
)
850 struct proxy_manager
* proxy
= LIST_ENTRY(cursor
, struct proxy_manager
, entry
);
851 proxy_manager_disconnect(proxy
);
857 /********************** StdMarshal implementation ****************************/
858 typedef struct _StdMarshalImpl
860 const IMarshalVtbl
*lpvtbl
;
865 LPVOID pvDestContext
;
869 static HRESULT WINAPI
870 StdMarshalImpl_QueryInterface(LPMARSHAL iface
, REFIID riid
, LPVOID
*ppv
)
873 if (IsEqualIID(&IID_IUnknown
, riid
) || IsEqualIID(&IID_IMarshal
, riid
))
876 IUnknown_AddRef(iface
);
879 FIXME("No interface for %s.\n", debugstr_guid(riid
));
880 return E_NOINTERFACE
;
884 StdMarshalImpl_AddRef(LPMARSHAL iface
)
886 StdMarshalImpl
*This
= (StdMarshalImpl
*)iface
;
887 return InterlockedIncrement(&This
->ref
);
891 StdMarshalImpl_Release(LPMARSHAL iface
)
893 StdMarshalImpl
*This
= (StdMarshalImpl
*)iface
;
894 ULONG ref
= InterlockedDecrement(&This
->ref
);
896 if (!ref
) HeapFree(GetProcessHeap(),0,This
);
900 static HRESULT WINAPI
901 StdMarshalImpl_GetUnmarshalClass(
902 LPMARSHAL iface
, REFIID riid
, void* pv
, DWORD dwDestContext
,
903 void* pvDestContext
, DWORD mshlflags
, CLSID
* pCid
)
905 *pCid
= CLSID_DfMarshal
;
909 static HRESULT WINAPI
910 StdMarshalImpl_GetMarshalSizeMax(
911 LPMARSHAL iface
, REFIID riid
, void* pv
, DWORD dwDestContext
,
912 void* pvDestContext
, DWORD mshlflags
, DWORD
* pSize
)
914 *pSize
= sizeof(STDOBJREF
);
918 static HRESULT WINAPI
919 StdMarshalImpl_MarshalInterface(
920 LPMARSHAL iface
, IStream
*pStm
,REFIID riid
, void* pv
, DWORD dwDestContext
,
921 void* pvDestContext
, DWORD mshlflags
)
926 APARTMENT
*apt
= COM_CurrentApt();
928 TRACE("(...,%s,...)\n", debugstr_guid(riid
));
932 ERR("Apartment not initialized\n");
933 return CO_E_NOTINITIALIZED
;
936 /* make sure this apartment can be reached from other threads / processes */
937 RPC_StartRemoting(apt
);
939 hres
= marshal_object(apt
, &stdobjref
, riid
, (IUnknown
*)pv
, mshlflags
);
942 ERR("Failed to create ifstub, hres=0x%lx\n", hres
);
946 hres
= IStream_Write(pStm
, &stdobjref
, sizeof(stdobjref
), &res
);
947 if (hres
) return hres
;
952 /* helper for StdMarshalImpl_UnmarshalInterface - does the unmarshaling with
953 * no questions asked about the rules surrounding same-apartment unmarshals
954 * and table marshaling */
955 static HRESULT
unmarshal_object(const STDOBJREF
*stdobjref
, APARTMENT
*apt
, REFIID riid
, void **object
)
957 struct proxy_manager
*proxy_manager
= NULL
;
962 TRACE("stdobjref:\n\tflags = %04lx\n\tcPublicRefs = %ld\n\toxid = %s\n\toid = %s\n\tipid = %s\n",
963 stdobjref
->flags
, stdobjref
->cPublicRefs
,
964 wine_dbgstr_longlong(stdobjref
->oxid
),
965 wine_dbgstr_longlong(stdobjref
->oid
),
966 debugstr_guid(&stdobjref
->ipid
));
968 /* create an a new proxy manager if one doesn't already exist for the
970 if (!find_proxy_manager(apt
, stdobjref
->oxid
, stdobjref
->oid
, &proxy_manager
))
972 hr
= proxy_manager_construct(apt
, stdobjref
->flags
,
973 stdobjref
->oxid
, stdobjref
->oid
,
977 TRACE("proxy manager already created, using\n");
981 struct ifproxy
* ifproxy
;
982 hr
= proxy_manager_find_ifproxy(proxy_manager
, riid
, &ifproxy
);
983 if (hr
== E_NOINTERFACE
)
985 IRpcChannelBuffer
*chanbuf
;
986 hr
= RPC_CreateClientChannel(&stdobjref
->oxid
, &stdobjref
->ipid
, &chanbuf
);
988 hr
= proxy_manager_create_ifproxy(proxy_manager
, stdobjref
,
989 riid
, chanbuf
, &ifproxy
);
992 IUnknown_AddRef((IUnknown
*)ifproxy
->iface
);
995 *object
= ifproxy
->iface
;
998 /* release our reference to the proxy manager - the client/apartment
999 * will hold on to the remaining reference for us */
1000 if (proxy_manager
) ClientIdentity_Release((IMultiQI
*)&proxy_manager
->lpVtbl
);
1005 static HRESULT WINAPI
1006 StdMarshalImpl_UnmarshalInterface(LPMARSHAL iface
, IStream
*pStm
, REFIID riid
, void **ppv
)
1008 struct stub_manager
*stubmgr
;
1009 STDOBJREF stdobjref
;
1012 APARTMENT
*apt
= COM_CurrentApt();
1013 APARTMENT
*stub_apt
;
1016 TRACE("(...,%s,....)\n", debugstr_guid(riid
));
1018 /* we need an apartment to unmarshal into */
1021 ERR("Apartment not initialized\n");
1022 return CO_E_NOTINITIALIZED
;
1025 /* read STDOBJREF from wire */
1026 hres
= IStream_Read(pStm
, &stdobjref
, sizeof(stdobjref
), &res
);
1027 if (hres
) return STG_E_READFAULT
;
1029 hres
= apartment_getoxid(apt
, &oxid
);
1030 if (hres
) return hres
;
1032 /* check if we're marshalling back to ourselves */
1033 if ((oxid
== stdobjref
.oxid
) && (stubmgr
= get_stub_manager(apt
, stdobjref
.oid
)))
1035 TRACE("Unmarshalling object marshalled in same apartment for iid %s, "
1036 "returning original object %p\n", debugstr_guid(riid
), stubmgr
->object
);
1038 hres
= IUnknown_QueryInterface(stubmgr
->object
, riid
, ppv
);
1040 /* unref the ifstub. FIXME: only do this on success? */
1041 if (!stub_manager_is_table_marshaled(stubmgr
, &stdobjref
.ipid
))
1042 stub_manager_ext_release(stubmgr
, stdobjref
.cPublicRefs
);
1044 stub_manager_int_release(stubmgr
);
1048 /* notify stub manager about unmarshal if process-local object.
1049 * note: if the oxid is not found then we and native will quite happily
1050 * ignore table marshaling and normal marshaling rules regarding number of
1051 * unmarshals, etc, but if you abuse these rules then your proxy could end
1052 * up returning RPC_E_DISCONNECTED. */
1053 if ((stub_apt
= apartment_findfromoxid(stdobjref
.oxid
, TRUE
)))
1055 if ((stubmgr
= get_stub_manager(stub_apt
, stdobjref
.oid
)))
1057 if (!stub_manager_notify_unmarshal(stubmgr
, &stdobjref
.ipid
))
1058 hres
= CO_E_OBJNOTCONNECTED
;
1060 stub_manager_int_release(stubmgr
);
1064 WARN("Couldn't find object for OXID %s, OID %s, assuming disconnected\n",
1065 wine_dbgstr_longlong(stdobjref
.oxid
),
1066 wine_dbgstr_longlong(stdobjref
.oid
));
1067 hres
= CO_E_OBJNOTCONNECTED
;
1070 apartment_release(stub_apt
);
1073 TRACE("Treating unmarshal from OXID %s as inter-process\n",
1074 wine_dbgstr_longlong(stdobjref
.oxid
));
1077 hres
= unmarshal_object(&stdobjref
, apt
, riid
, ppv
);
1079 if (hres
) WARN("Failed with error 0x%08lx\n", hres
);
1080 else TRACE("Successfully created proxy %p\n", *ppv
);
1085 static HRESULT WINAPI
1086 StdMarshalImpl_ReleaseMarshalData(LPMARSHAL iface
, IStream
*pStm
)
1088 STDOBJREF stdobjref
;
1091 struct stub_manager
*stubmgr
;
1094 TRACE("iface=%p, pStm=%p\n", iface
, pStm
);
1096 hres
= IStream_Read(pStm
, &stdobjref
, sizeof(stdobjref
), &res
);
1097 if (hres
) return STG_E_READFAULT
;
1099 TRACE("oxid = %s, oid = %s, ipid = %s\n",
1100 wine_dbgstr_longlong(stdobjref
.oxid
),
1101 wine_dbgstr_longlong(stdobjref
.oid
),
1102 wine_dbgstr_guid(&stdobjref
.ipid
));
1104 if (!(apt
= apartment_findfromoxid(stdobjref
.oxid
, TRUE
)))
1106 WARN("Could not map OXID %s to apartment object\n",
1107 wine_dbgstr_longlong(stdobjref
.oxid
));
1108 return RPC_E_INVALID_OBJREF
;
1111 if (!(stubmgr
= get_stub_manager(apt
, stdobjref
.oid
)))
1113 ERR("could not map object ID to stub manager, oxid=%s, oid=%s\n",
1114 wine_dbgstr_longlong(stdobjref
.oxid
), wine_dbgstr_longlong(stdobjref
.oid
));
1115 return RPC_E_INVALID_OBJREF
;
1118 stub_manager_release_marshal_data(stubmgr
, stdobjref
.cPublicRefs
, &stdobjref
.ipid
);
1120 stub_manager_int_release(stubmgr
);
1121 apartment_release(apt
);
1126 static HRESULT WINAPI
1127 StdMarshalImpl_DisconnectObject(LPMARSHAL iface
, DWORD dwReserved
)
1129 FIXME("(), stub!\n");
1133 static const IMarshalVtbl VT_StdMarshal
=
1135 StdMarshalImpl_QueryInterface
,
1136 StdMarshalImpl_AddRef
,
1137 StdMarshalImpl_Release
,
1138 StdMarshalImpl_GetUnmarshalClass
,
1139 StdMarshalImpl_GetMarshalSizeMax
,
1140 StdMarshalImpl_MarshalInterface
,
1141 StdMarshalImpl_UnmarshalInterface
,
1142 StdMarshalImpl_ReleaseMarshalData
,
1143 StdMarshalImpl_DisconnectObject
1146 static HRESULT
StdMarshalImpl_Construct(REFIID riid
, void** ppvObject
)
1148 StdMarshalImpl
* pStdMarshal
=
1149 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(StdMarshalImpl
));
1151 return E_OUTOFMEMORY
;
1152 pStdMarshal
->lpvtbl
= &VT_StdMarshal
;
1153 pStdMarshal
->ref
= 0;
1154 return IMarshal_QueryInterface((IMarshal
*)pStdMarshal
, riid
, ppvObject
);
1157 /***********************************************************************
1158 * CoGetStandardMarshal [OLE32.@]
1160 * Gets or creates a standard marshal object.
1163 * riid [I] Interface identifier of the pUnk object.
1164 * pUnk [I] Optional. Object to get the marshal object for.
1165 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1166 * pvDestContext [I] Reserved. Must be NULL.
1167 * mshlflags [I] Flags affecting the marshaling process.
1168 * ppMarshal [O] Address where marshal object will be stored.
1172 * Failure: HRESULT code.
1176 * The function retrieves the IMarshal object associated with an object if
1177 * that object is currently an active stub, otherwise a new marshal object is
1180 HRESULT WINAPI
CoGetStandardMarshal(REFIID riid
, IUnknown
*pUnk
,
1181 DWORD dwDestContext
, LPVOID pvDestContext
,
1182 DWORD mshlflags
, LPMARSHAL
*ppMarshal
)
1188 FIXME("(%s,NULL,%lx,%p,%lx,%p), unimplemented yet.\n",
1189 debugstr_guid(riid
),dwDestContext
,pvDestContext
,mshlflags
,ppMarshal
);
1192 TRACE("(%s,%p,%lx,%p,%lx,%p)\n",
1193 debugstr_guid(riid
),pUnk
,dwDestContext
,pvDestContext
,mshlflags
,ppMarshal
);
1194 *ppMarshal
= HeapAlloc(GetProcessHeap(),0,sizeof(StdMarshalImpl
));
1195 dm
= (StdMarshalImpl
*) *ppMarshal
;
1196 if (!dm
) return E_FAIL
;
1197 dm
->lpvtbl
= &VT_StdMarshal
;
1201 dm
->dwDestContext
= dwDestContext
;
1202 dm
->pvDestContext
= pvDestContext
;
1203 dm
->mshlflags
= mshlflags
;
1207 /***********************************************************************
1208 * get_marshaler [internal]
1210 * Retrieves an IMarshal interface for an object.
1212 static HRESULT
get_marshaler(REFIID riid
, IUnknown
*pUnk
, DWORD dwDestContext
,
1213 void *pvDestContext
, DWORD mshlFlags
,
1214 LPMARSHAL
*pMarshal
)
1220 hr
= IUnknown_QueryInterface(pUnk
, &IID_IMarshal
, (LPVOID
*)pMarshal
);
1222 hr
= CoGetStandardMarshal(riid
, pUnk
, dwDestContext
, pvDestContext
,
1223 mshlFlags
, pMarshal
);
1227 /***********************************************************************
1228 * get_unmarshaler_from_stream [internal]
1230 * Creates an IMarshal* object according to the data marshaled to the stream.
1231 * The function leaves the stream pointer at the start of the data written
1232 * to the stream by the IMarshal* object.
1234 static HRESULT
get_unmarshaler_from_stream(IStream
*stream
, IMarshal
**marshal
, IID
*iid
)
1240 /* read common OBJREF header */
1241 hr
= IStream_Read(stream
, &objref
, FIELD_OFFSET(OBJREF
, u_objref
), &res
);
1242 if (hr
|| (res
!= FIELD_OFFSET(OBJREF
, u_objref
)))
1244 ERR("Failed to read common OBJREF header, 0x%08lx\n", hr
);
1245 return STG_E_READFAULT
;
1248 /* sanity check on header */
1249 if (objref
.signature
!= OBJREF_SIGNATURE
)
1251 ERR("Bad OBJREF signature 0x%08lx\n", objref
.signature
);
1252 return RPC_E_INVALID_OBJREF
;
1255 if (iid
) *iid
= objref
.iid
;
1257 /* FIXME: handler marshaling */
1258 if (objref
.flags
& OBJREF_STANDARD
)
1260 TRACE("Using standard unmarshaling\n");
1261 hr
= StdMarshalImpl_Construct(&IID_IMarshal
, (LPVOID
*)marshal
);
1263 else if (objref
.flags
& OBJREF_CUSTOM
)
1265 ULONG custom_header_size
= FIELD_OFFSET(OBJREF
, u_objref
.u_custom
.pData
) -
1266 FIELD_OFFSET(OBJREF
, u_objref
.u_custom
);
1267 TRACE("Using custom unmarshaling\n");
1268 /* read constant sized OR_CUSTOM data from stream */
1269 hr
= IStream_Read(stream
, &objref
.u_objref
.u_custom
,
1270 custom_header_size
, &res
);
1271 if (hr
|| (res
!= custom_header_size
))
1273 ERR("Failed to read OR_CUSTOM header, 0x%08lx\n", hr
);
1274 return STG_E_READFAULT
;
1276 /* now create the marshaler specified in the stream */
1277 hr
= CoCreateInstance(&objref
.u_objref
.u_custom
.clsid
, NULL
,
1278 CLSCTX_INPROC_SERVER
, &IID_IMarshal
,
1283 FIXME("Invalid or unimplemented marshaling type specified: %lx\n",
1285 return RPC_E_INVALID_OBJREF
;
1289 ERR("Failed to create marshal, 0x%08lx\n", hr
);
1294 /***********************************************************************
1295 * CoGetMarshalSizeMax [OLE32.@]
1297 * Gets the maximum amount of data that will be needed by a marshal.
1300 * pulSize [O] Address where maximum marshal size will be stored.
1301 * riid [I] Identifier of the interface to marshal.
1302 * pUnk [I] Pointer to the object to marshal.
1303 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1304 * pvDestContext [I] Reserved. Must be NULL.
1305 * mshlFlags [I] Flags that affect the marshaling. See CoMarshalInterface().
1309 * Failure: HRESULT code.
1312 * CoMarshalInterface().
1314 HRESULT WINAPI
CoGetMarshalSizeMax(ULONG
*pulSize
, REFIID riid
, IUnknown
*pUnk
,
1315 DWORD dwDestContext
, void *pvDestContext
,
1320 CLSID marshaler_clsid
;
1322 hr
= get_marshaler(riid
, pUnk
, dwDestContext
, pvDestContext
, mshlFlags
, &pMarshal
);
1326 hr
= IMarshal_GetUnmarshalClass(pMarshal
, riid
, pUnk
, dwDestContext
,
1327 pvDestContext
, mshlFlags
, &marshaler_clsid
);
1330 ERR("IMarshal::GetUnmarshalClass failed, 0x%08lx\n", hr
);
1331 IMarshal_Release(pMarshal
);
1335 hr
= IMarshal_GetMarshalSizeMax(pMarshal
, riid
, pUnk
, dwDestContext
,
1336 pvDestContext
, mshlFlags
, pulSize
);
1337 /* add on the size of the common header */
1338 *pulSize
+= FIELD_OFFSET(OBJREF
, u_objref
);
1340 /* if custom marshaling, add on size of custom header */
1341 if (!IsEqualCLSID(&marshaler_clsid
, &CLSID_DfMarshal
))
1342 *pulSize
+= FIELD_OFFSET(OBJREF
, u_objref
.u_custom
.pData
) -
1343 FIELD_OFFSET(OBJREF
, u_objref
.u_custom
);
1345 IMarshal_Release(pMarshal
);
1350 static void dump_MSHLFLAGS(MSHLFLAGS flags
)
1352 if (flags
& MSHLFLAGS_TABLESTRONG
)
1353 TRACE(" MSHLFLAGS_TABLESTRONG");
1354 if (flags
& MSHLFLAGS_TABLEWEAK
)
1355 TRACE(" MSHLFLAGS_TABLEWEAK");
1356 if (!(flags
& (MSHLFLAGS_TABLESTRONG
|MSHLFLAGS_TABLEWEAK
)))
1357 TRACE(" MSHLFLAGS_NORMAL");
1358 if (flags
& MSHLFLAGS_NOPING
)
1359 TRACE(" MSHLFLAGS_NOPING");
1362 /***********************************************************************
1363 * CoMarshalInterface [OLE32.@]
1365 * Marshals an interface into a stream so that the object can then be
1366 * unmarshaled from another COM apartment and used remotely.
1369 * pStream [I] Stream the object will be marshaled into.
1370 * riid [I] Identifier of the interface to marshal.
1371 * pUnk [I] Pointer to the object to marshal.
1372 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1373 * pvDestContext [I] Reserved. Must be NULL.
1374 * mshlFlags [I] Flags that affect the marshaling. See notes.
1378 * Failure: HRESULT code.
1382 * The mshlFlags parameter can take one or more of the following flags:
1383 *| MSHLFLAGS_NORMAL - Unmarshal once, releases stub on last proxy release.
1384 *| MSHLFLAGS_TABLESTRONG - Unmarshal many, release when CoReleaseMarshalData() called.
1385 *| MSHLFLAGS_TABLEWEAK - Unmarshal many, releases stub on last proxy release.
1386 *| MSHLFLAGS_NOPING - No automatic garbage collection (and so reduces network traffic).
1388 * If a marshaled object is not unmarshaled, then CoReleaseMarshalData() must
1389 * be called in order to release the resources used in the marshaling.
1392 * CoUnmarshalInterface(), CoReleaseMarshalData().
1394 HRESULT WINAPI
CoMarshalInterface(IStream
*pStream
, REFIID riid
, IUnknown
*pUnk
,
1395 DWORD dwDestContext
, void *pvDestContext
,
1399 CLSID marshaler_clsid
;
1403 TRACE("(%p, %s, %p, %lx, %p,", pStream
, debugstr_guid(riid
), pUnk
,
1404 dwDestContext
, pvDestContext
);
1405 dump_MSHLFLAGS(mshlFlags
);
1409 return E_INVALIDARG
;
1411 objref
.signature
= OBJREF_SIGNATURE
;
1414 /* get the marshaler for the specified interface */
1415 hr
= get_marshaler(riid
, pUnk
, dwDestContext
, pvDestContext
, mshlFlags
, &pMarshal
);
1418 ERR("Failed to get marshaller, 0x%08lx\n", hr
);
1422 hr
= IMarshal_GetUnmarshalClass(pMarshal
, riid
, pUnk
, dwDestContext
,
1423 pvDestContext
, mshlFlags
, &marshaler_clsid
);
1426 ERR("IMarshal::GetUnmarshalClass failed, 0x%08lx\n", hr
);
1430 /* FIXME: implement handler marshaling too */
1431 if (IsEqualCLSID(&marshaler_clsid
, &CLSID_DfMarshal
))
1433 TRACE("Using standard marshaling\n");
1434 objref
.flags
= OBJREF_STANDARD
;
1436 /* write the common OBJREF header to the stream */
1437 hr
= IStream_Write(pStream
, &objref
, FIELD_OFFSET(OBJREF
, u_objref
), NULL
);
1440 ERR("Failed to write OBJREF header to stream, 0x%08lx\n", hr
);
1446 TRACE("Using custom marshaling\n");
1447 objref
.flags
= OBJREF_CUSTOM
;
1448 objref
.u_objref
.u_custom
.clsid
= marshaler_clsid
;
1449 objref
.u_objref
.u_custom
.cbExtension
= 0;
1450 objref
.u_objref
.u_custom
.size
= 0;
1451 hr
= IMarshal_GetMarshalSizeMax(pMarshal
, riid
, pUnk
, dwDestContext
,
1452 pvDestContext
, mshlFlags
,
1453 &objref
.u_objref
.u_custom
.size
);
1456 ERR("Failed to get max size of marshal data, error 0x%08lx\n", hr
);
1459 /* write constant sized common header and OR_CUSTOM data into stream */
1460 hr
= IStream_Write(pStream
, &objref
,
1461 FIELD_OFFSET(OBJREF
, u_objref
.u_custom
.pData
), NULL
);
1464 ERR("Failed to write OR_CUSTOM header to stream with 0x%08lx\n", hr
);
1469 TRACE("Calling IMarshal::MarshalInterace\n");
1470 /* call helper object to do the actual marshaling */
1471 hr
= IMarshal_MarshalInterface(pMarshal
, pStream
, riid
, pUnk
, dwDestContext
,
1472 pvDestContext
, mshlFlags
);
1476 ERR("Failed to marshal the interface %s, %lx\n", debugstr_guid(riid
), hr
);
1481 IMarshal_Release(pMarshal
);
1483 TRACE("completed with hr 0x%08lx\n", hr
);
1488 /***********************************************************************
1489 * CoUnmarshalInterface [OLE32.@]
1491 * Unmarshals an object from a stream by creating a proxy to the remote
1492 * object, if necessary.
1496 * pStream [I] Stream containing the marshaled object.
1497 * riid [I] Interface identifier of the object to create a proxy to.
1498 * ppv [O] Address where proxy will be stored.
1503 * Failure: HRESULT code.
1506 * CoMarshalInterface().
1508 HRESULT WINAPI
CoUnmarshalInterface(IStream
*pStream
, REFIID riid
, LPVOID
*ppv
)
1515 TRACE("(%p, %s, %p)\n", pStream
, debugstr_guid(riid
), ppv
);
1517 hr
= get_unmarshaler_from_stream(pStream
, &pMarshal
, &iid
);
1521 /* call the helper object to do the actual unmarshaling */
1522 hr
= IMarshal_UnmarshalInterface(pMarshal
, pStream
, &iid
, (LPVOID
*)&object
);
1524 ERR("IMarshal::UnmarshalInterface failed, 0x%08lx\n", hr
);
1526 /* IID_NULL means use the interface ID of the marshaled object */
1527 if (!IsEqualIID(riid
, &IID_NULL
))
1532 if (!IsEqualIID(riid
, &iid
))
1534 TRACE("requested interface != marshalled interface, additional QI needed\n");
1535 hr
= IUnknown_QueryInterface(object
, &iid
, ppv
);
1537 ERR("Couldn't query for interface %s, hr = 0x%08lx\n",
1538 debugstr_guid(riid
), hr
);
1539 IUnknown_Release(object
);
1547 IMarshal_Release(pMarshal
);
1549 TRACE("completed with hr 0x%lx\n", hr
);
1554 /***********************************************************************
1555 * CoReleaseMarshalData [OLE32.@]
1557 * Releases resources associated with an object that has been marshaled into
1562 * pStream [I] The stream that the object has been marshaled into.
1566 * Failure: HRESULT error code.
1570 * Call this function to release resources associated with a normal or
1571 * table-weak marshal that will not be unmarshaled, and all table-strong
1572 * marshals when they are no longer needed.
1575 * CoMarshalInterface(), CoUnmarshalInterface().
1577 HRESULT WINAPI
CoReleaseMarshalData(IStream
*pStream
)
1582 TRACE("(%p)\n", pStream
);
1584 hr
= get_unmarshaler_from_stream(pStream
, &pMarshal
, NULL
);
1588 /* call the helper object to do the releasing of marshal data */
1589 hr
= IMarshal_ReleaseMarshalData(pMarshal
, pStream
);
1591 ERR("IMarshal::ReleaseMarshalData failed with error 0x%08lx\n", hr
);
1593 IMarshal_Release(pMarshal
);
1598 /***********************************************************************
1599 * CoMarshalInterThreadInterfaceInStream [OLE32.@]
1601 * Marshal an interface across threads in the same process.
1604 * riid [I] Identifier of the interface to be marshalled.
1605 * pUnk [I] Pointer to IUnknown-derived interface that will be marshalled.
1606 * ppStm [O] Pointer to IStream object that is created and then used to store the marshalled inteface.
1610 * Failure: E_OUTOFMEMORY and other COM error codes
1613 * CoMarshalInterface(), CoUnmarshalInterface() and CoGetInterfaceAndReleaseStream()
1615 HRESULT WINAPI
CoMarshalInterThreadInterfaceInStream(
1616 REFIID riid
, LPUNKNOWN pUnk
, LPSTREAM
* ppStm
)
1618 ULARGE_INTEGER xpos
;
1619 LARGE_INTEGER seekto
;
1622 TRACE("(%s, %p, %p)\n",debugstr_guid(riid
), pUnk
, ppStm
);
1624 hres
= CreateStreamOnHGlobal(NULL
, TRUE
, ppStm
);
1625 if (FAILED(hres
)) return hres
;
1626 hres
= CoMarshalInterface(*ppStm
, riid
, pUnk
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_NORMAL
);
1628 if (SUCCEEDED(hres
))
1630 memset(&seekto
, 0, sizeof(seekto
));
1631 IStream_Seek(*ppStm
, seekto
, SEEK_SET
, &xpos
);
1635 IStream_Release(*ppStm
);
1642 /***********************************************************************
1643 * CoGetInterfaceAndReleaseStream [OLE32.@]
1645 * Unmarshalls an inteface from a stream and then releases the stream.
1648 * pStm [I] Stream that contains the marshalled inteface.
1649 * riid [I] Interface identifier of the object to unmarshall.
1650 * ppv [O] Address of pointer where the requested interface object will be stored.
1654 * Failure: A COM error code
1657 * CoMarshalInterThreadInterfaceInStream() and CoUnmarshalInteface()
1659 HRESULT WINAPI
CoGetInterfaceAndReleaseStream(LPSTREAM pStm
, REFIID riid
,
1664 TRACE("(%p, %s, %p)\n", pStm
, debugstr_guid(riid
), ppv
);
1666 hres
= CoUnmarshalInterface(pStm
, riid
, ppv
);
1667 IStream_Release(pStm
);
1671 static HRESULT WINAPI
StdMarshalCF_QueryInterface(LPCLASSFACTORY iface
,
1672 REFIID riid
, LPVOID
*ppv
)
1675 if (IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IClassFactory
))
1677 *ppv
= (LPVOID
)iface
;
1680 return E_NOINTERFACE
;
1683 static ULONG WINAPI
StdMarshalCF_AddRef(LPCLASSFACTORY iface
)
1685 return 2; /* non-heap based object */
1688 static ULONG WINAPI
StdMarshalCF_Release(LPCLASSFACTORY iface
)
1690 return 1; /* non-heap based object */
1693 static HRESULT WINAPI
StdMarshalCF_CreateInstance(LPCLASSFACTORY iface
,
1694 LPUNKNOWN pUnk
, REFIID riid
, LPVOID
*ppv
)
1696 if (IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IMarshal
))
1697 return StdMarshalImpl_Construct(riid
, ppv
);
1699 FIXME("(%s), not supported.\n",debugstr_guid(riid
));
1700 return E_NOINTERFACE
;
1703 static HRESULT WINAPI
StdMarshalCF_LockServer(LPCLASSFACTORY iface
, BOOL fLock
)
1705 FIXME("(%d), stub!\n",fLock
);
1709 static const IClassFactoryVtbl StdMarshalCFVtbl
=
1711 StdMarshalCF_QueryInterface
,
1712 StdMarshalCF_AddRef
,
1713 StdMarshalCF_Release
,
1714 StdMarshalCF_CreateInstance
,
1715 StdMarshalCF_LockServer
1717 static const IClassFactoryVtbl
*StdMarshalCF
= &StdMarshalCFVtbl
;
1719 HRESULT
MARSHAL_GetStandardMarshalCF(LPVOID
*ppv
)
1721 *ppv
= &StdMarshalCF
;
1725 /***********************************************************************
1726 * CoMarshalHresult [OLE32.@]
1728 * Marshals an HRESULT value into a stream.
1731 * pStm [I] Stream that hresult will be marshalled into.
1732 * hresult [I] HRESULT to be marshalled.
1736 * Failure: A COM error code
1739 * CoUnmarshalHresult().
1741 HRESULT WINAPI
CoMarshalHresult(LPSTREAM pStm
, HRESULT hresult
)
1743 return IStream_Write(pStm
, &hresult
, sizeof(hresult
), NULL
);
1746 /***********************************************************************
1747 * CoUnmarshalHresult [OLE32.@]
1749 * Unmarshals an HRESULT value from a stream.
1752 * pStm [I] Stream that hresult will be unmarshalled from.
1753 * phresult [I] Pointer to HRESULT where the value will be unmarshalled to.
1757 * Failure: A COM error code
1760 * CoMarshalHresult().
1762 HRESULT WINAPI
CoUnmarshalHresult(LPSTREAM pStm
, HRESULT
* phresult
)
1764 return IStream_Read(pStm
, phresult
, sizeof(*phresult
), NULL
);