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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
35 #include "wine/unicode.h"
37 #include "compobj_private.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
43 extern const CLSID CLSID_DfMarshal
;
45 /* number of refs given out for normal marshaling */
46 #define NORMALEXTREFS 5
48 /* private flag indicating that the caller does not want to notify the stub
49 * when the proxy disconnects or is destroyed */
50 #define SORFP_NOLIFETIMEMGMT SORF_OXRES1
52 static HRESULT
unmarshal_object(const STDOBJREF
*stdobjref
, APARTMENT
*apt
,
53 MSHCTX dest_context
, void *dest_context_data
,
54 REFIID riid
, void **object
);
56 /* Marshalling just passes a unique identifier to the remote client,
57 * that makes it possible to find the passed interface again.
59 * So basically we need a set of values that make it unique.
61 * Note that the IUnknown_QI(ob,xiid,&ppv) always returns the SAME ppv value!
63 * A triple is used: OXID (apt id), OID (stub manager id),
64 * IPID (interface ptr/stub id).
66 * OXIDs identify an apartment and are network scoped
67 * OIDs identify a stub manager and are apartment scoped
68 * IPIDs identify an interface stub and are apartment scoped
71 static inline HRESULT
get_facbuf_for_iid(REFIID riid
, IPSFactoryBuffer
**facbuf
)
76 if ((hr
= CoGetPSClsid(riid
, &clsid
)))
78 return CoGetClassObject(&clsid
, CLSCTX_INPROC_SERVER
, NULL
,
79 &IID_IPSFactoryBuffer
, (LPVOID
*)facbuf
);
82 /* marshals an object into a STDOBJREF structure */
83 HRESULT
marshal_object(APARTMENT
*apt
, STDOBJREF
*stdobjref
, REFIID riid
, IUnknown
*object
, MSHLFLAGS mshlflags
)
85 struct stub_manager
*manager
;
86 struct ifstub
*ifstub
;
88 IRpcStubBuffer
*stub
= NULL
;
90 IUnknown
*iobject
= NULL
; /* object of type riid */
92 hr
= apartment_getoxid(apt
, &stdobjref
->oxid
);
96 hr
= apartment_createwindowifneeded(apt
);
100 hr
= IUnknown_QueryInterface(object
, riid
, (void **)&iobject
);
103 ERR("object doesn't expose interface %s, failing with error 0x%08x\n",
104 debugstr_guid(riid
), hr
);
105 return E_NOINTERFACE
;
108 /* IUnknown doesn't require a stub buffer, because it never goes out on
110 if (!IsEqualIID(riid
, &IID_IUnknown
))
112 IPSFactoryBuffer
*psfb
;
114 hr
= get_facbuf_for_iid(riid
, &psfb
);
117 ERR("couldn't get IPSFactory buffer for interface %s\n", debugstr_guid(riid
));
118 IUnknown_Release(iobject
);
122 hr
= IPSFactoryBuffer_CreateStub(psfb
, riid
, iobject
, &stub
);
123 IPSFactoryBuffer_Release(psfb
);
126 ERR("Failed to create an IRpcStubBuffer from IPSFactory for %s with error 0x%08x\n",
127 debugstr_guid(riid
), hr
);
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
);
159 ifstub
= stub_manager_new_ifstub(manager
, stub
, iobject
, riid
, mshlflags
);
161 if (stub
) IRpcStubBuffer_Release(stub
);
162 IUnknown_Release(iobject
);
166 stub_manager_int_release(manager
);
167 /* destroy the stub manager if it has no ifstubs by releasing
168 * zero external references */
169 stub_manager_ext_release(manager
, 0, TRUE
);
170 return E_OUTOFMEMORY
;
175 stdobjref
->cPublicRefs
= NORMALEXTREFS
;
176 stub_manager_ext_addref(manager
, stdobjref
->cPublicRefs
);
180 stdobjref
->cPublicRefs
= 0;
181 if (mshlflags
& MSHLFLAGS_TABLESTRONG
)
182 stub_manager_ext_addref(manager
, 1);
185 /* FIXME: check return value */
186 RPC_RegisterInterface(riid
);
188 stdobjref
->ipid
= ifstub
->ipid
;
190 stub_manager_int_release(manager
);
196 /* Client-side identity of the server object */
198 static HRESULT
proxy_manager_get_remunknown(struct proxy_manager
* This
, IRemUnknown
**remunk
);
199 static void proxy_manager_destroy(struct proxy_manager
* This
);
200 static HRESULT
proxy_manager_find_ifproxy(struct proxy_manager
* This
, REFIID riid
, struct ifproxy
** ifproxy_found
);
201 static HRESULT
proxy_manager_query_local_interface(struct proxy_manager
* This
, REFIID riid
, void ** ppv
);
203 static HRESULT WINAPI
ClientIdentity_QueryInterface(IMultiQI
* iface
, REFIID riid
, void ** ppv
)
208 TRACE("%s\n", debugstr_guid(riid
));
211 hr
= IMultiQI_QueryMultipleInterfaces(iface
, 1, &mqi
);
212 *ppv
= (void *)mqi
.pItf
;
217 static ULONG WINAPI
ClientIdentity_AddRef(IMultiQI
* iface
)
219 struct proxy_manager
* This
= (struct proxy_manager
*)iface
;
220 TRACE("%p - before %d\n", iface
, This
->refs
);
221 return InterlockedIncrement(&This
->refs
);
224 static ULONG WINAPI
ClientIdentity_Release(IMultiQI
* iface
)
226 struct proxy_manager
* This
= (struct proxy_manager
*)iface
;
227 ULONG refs
= InterlockedDecrement(&This
->refs
);
228 TRACE("%p - after %d\n", iface
, refs
);
230 proxy_manager_destroy(This
);
234 static HRESULT WINAPI
ClientIdentity_QueryMultipleInterfaces(IMultiQI
*iface
, ULONG cMQIs
, MULTI_QI
*pMQIs
)
236 struct proxy_manager
* This
= (struct proxy_manager
*)iface
;
237 REMQIRESULT
*qiresults
= NULL
;
238 ULONG nonlocal_mqis
= 0;
240 ULONG successful_mqis
= 0;
241 IID
*iids
= HeapAlloc(GetProcessHeap(), 0, cMQIs
* sizeof(*iids
));
242 /* mapping of RemQueryInterface index to QueryMultipleInterfaces index */
243 ULONG
*mapping
= HeapAlloc(GetProcessHeap(), 0, cMQIs
* sizeof(*mapping
));
245 TRACE("cMQIs: %d\n", cMQIs
);
247 /* try to get a local interface - this includes already active proxy
248 * interfaces and also interfaces exposed by the proxy manager */
249 for (i
= 0; i
< cMQIs
; i
++)
251 TRACE("iid[%d] = %s\n", i
, debugstr_guid(pMQIs
[i
].pIID
));
252 pMQIs
[i
].hr
= proxy_manager_query_local_interface(This
, pMQIs
[i
].pIID
, (void **)&pMQIs
[i
].pItf
);
253 if (pMQIs
[i
].hr
== S_OK
)
257 iids
[nonlocal_mqis
] = *pMQIs
[i
].pIID
;
258 mapping
[nonlocal_mqis
] = i
;
263 TRACE("%d interfaces not found locally\n", nonlocal_mqis
);
265 /* if we have more than one interface not found locally then we must try
266 * to query the remote object for it */
267 if (nonlocal_mqis
!= 0)
273 /* get the ipid of the first entry */
274 /* FIXME: should we implement ClientIdentity on the ifproxies instead
275 * of the proxy_manager so we use the correct ipid here? */
276 ipid
= &LIST_ENTRY(list_head(&This
->interfaces
), struct ifproxy
, entry
)->stdobjref
.ipid
;
278 /* get IRemUnknown proxy so we can communicate with the remote object */
279 hr
= proxy_manager_get_remunknown(This
, &remunk
);
283 hr
= IRemUnknown_RemQueryInterface(remunk
, ipid
, NORMALEXTREFS
,
284 nonlocal_mqis
, iids
, &qiresults
);
286 ERR("IRemUnknown_RemQueryInterface failed with error 0x%08x\n", hr
);
289 /* IRemUnknown_RemQueryInterface can return S_FALSE if only some of
290 * the interfaces were returned */
293 /* try to unmarshal each object returned to us */
294 for (i
= 0; i
< nonlocal_mqis
; i
++)
296 ULONG index
= mapping
[i
];
297 HRESULT hrobj
= qiresults
[i
].hResult
;
299 hrobj
= unmarshal_object(&qiresults
[i
].std
, This
->parent
,
301 This
->dest_context_data
,
303 (void **)&pMQIs
[index
].pItf
);
308 ERR("Failed to get pointer to interface %s\n", debugstr_guid(pMQIs
[index
].pIID
));
309 pMQIs
[index
].hr
= hrobj
;
313 /* free the memory allocated by the proxy */
314 CoTaskMemFree(qiresults
);
317 TRACE("%d/%d successfully queried\n", successful_mqis
, cMQIs
);
319 HeapFree(GetProcessHeap(), 0, iids
);
320 HeapFree(GetProcessHeap(), 0, mapping
);
322 if (successful_mqis
== cMQIs
)
323 return S_OK
; /* we got all requested interfaces */
324 else if (successful_mqis
== 0)
325 return E_NOINTERFACE
; /* we didn't get any interfaces */
327 return S_FALSE
; /* we got some interfaces */
330 static const IMultiQIVtbl ClientIdentity_Vtbl
=
332 ClientIdentity_QueryInterface
,
333 ClientIdentity_AddRef
,
334 ClientIdentity_Release
,
335 ClientIdentity_QueryMultipleInterfaces
338 /* FIXME: remove these */
339 static HRESULT WINAPI
StdMarshalImpl_GetUnmarshalClass(LPMARSHAL iface
, REFIID riid
, void* pv
, DWORD dwDestContext
, void* pvDestContext
, DWORD mshlflags
, CLSID
* pCid
);
340 static HRESULT WINAPI
StdMarshalImpl_GetMarshalSizeMax(LPMARSHAL iface
, REFIID riid
, void* pv
, DWORD dwDestContext
, void* pvDestContext
, DWORD mshlflags
, DWORD
* pSize
);
341 static HRESULT WINAPI
StdMarshalImpl_UnmarshalInterface(LPMARSHAL iface
, IStream
*pStm
, REFIID riid
, void **ppv
);
342 static HRESULT WINAPI
StdMarshalImpl_ReleaseMarshalData(LPMARSHAL iface
, IStream
*pStm
);
343 static HRESULT WINAPI
StdMarshalImpl_DisconnectObject(LPMARSHAL iface
, DWORD dwReserved
);
345 static HRESULT WINAPI
Proxy_QueryInterface(IMarshal
*iface
, REFIID riid
, void **ppvObject
)
347 ICOM_THIS_MULTI(struct proxy_manager
, lpVtblMarshal
, iface
);
348 return IMultiQI_QueryInterface((IMultiQI
*)&This
->lpVtbl
, riid
, ppvObject
);
351 static ULONG WINAPI
Proxy_AddRef(IMarshal
*iface
)
353 ICOM_THIS_MULTI(struct proxy_manager
, lpVtblMarshal
, iface
);
354 return IMultiQI_AddRef((IMultiQI
*)&This
->lpVtbl
);
357 static ULONG WINAPI
Proxy_Release(IMarshal
*iface
)
359 ICOM_THIS_MULTI(struct proxy_manager
, lpVtblMarshal
, iface
);
360 return IMultiQI_Release((IMultiQI
*)&This
->lpVtbl
);
363 static HRESULT WINAPI
Proxy_MarshalInterface(
364 LPMARSHAL iface
, IStream
*pStm
, REFIID riid
, void* pv
, DWORD dwDestContext
,
365 void* pvDestContext
, DWORD mshlflags
)
367 ICOM_THIS_MULTI(struct proxy_manager
, lpVtblMarshal
, iface
);
369 struct ifproxy
*ifproxy
;
371 TRACE("(...,%s,...)\n", debugstr_guid(riid
));
373 hr
= proxy_manager_find_ifproxy(This
, riid
, &ifproxy
);
376 STDOBJREF stdobjref
= ifproxy
->stdobjref
;
377 ULONG cPublicRefs
= ifproxy
->refs
;
378 ULONG cPublicRefsOld
;
380 /* optimization - share out proxy's public references if possible
381 * instead of making new proxy do a roundtrip through the server */
384 ULONG cPublicRefsNew
;
385 cPublicRefsOld
= cPublicRefs
;
386 stdobjref
.cPublicRefs
= cPublicRefs
/ 2;
387 cPublicRefsNew
= cPublicRefs
- stdobjref
.cPublicRefs
;
388 cPublicRefs
= InterlockedCompareExchange(
389 (LONG
*)&ifproxy
->refs
, cPublicRefsNew
, cPublicRefsOld
);
390 } while (cPublicRefs
!= cPublicRefsOld
);
392 if (!stdobjref
.cPublicRefs
)
395 hr
= proxy_manager_get_remunknown(This
, &remunk
);
398 HRESULT hrref
= S_OK
;
400 rif
.ipid
= ifproxy
->stdobjref
.ipid
;
401 rif
.cPublicRefs
= NORMALEXTREFS
;
402 rif
.cPrivateRefs
= 0;
403 hr
= IRemUnknown_RemAddRef(remunk
, 1, &rif
, &hrref
);
404 if (hr
== S_OK
&& hrref
== S_OK
)
405 stdobjref
.cPublicRefs
= rif
.cPublicRefs
;
407 ERR("IRemUnknown_RemAddRef returned with 0x%08x, hrref = 0x%08x\n", hr
, hrref
);
413 TRACE("writing stdobjref:\n\tflags = %04lx\n\tcPublicRefs = %ld\n\toxid = %s\n\toid = %s\n\tipid = %s\n",
414 stdobjref
.flags
, stdobjref
.cPublicRefs
,
415 wine_dbgstr_longlong(stdobjref
.oxid
),
416 wine_dbgstr_longlong(stdobjref
.oid
),
417 debugstr_guid(&stdobjref
.ipid
));
418 hr
= IStream_Write(pStm
, &stdobjref
, sizeof(stdobjref
), NULL
);
423 /* we don't have the interface already unmarshaled so we have to
424 * request the object from the server */
427 REMQIRESULT
*qiresults
= NULL
;
430 /* get the ipid of the first entry */
431 /* FIXME: should we implement ClientIdentity on the ifproxies instead
432 * of the proxy_manager so we use the correct ipid here? */
433 ipid
= &LIST_ENTRY(list_head(&This
->interfaces
), struct ifproxy
, entry
)->stdobjref
.ipid
;
435 /* get IRemUnknown proxy so we can communicate with the remote object */
436 hr
= proxy_manager_get_remunknown(This
, &remunk
);
440 hr
= IRemUnknown_RemQueryInterface(remunk
, ipid
, NORMALEXTREFS
,
441 1, &iid
, &qiresults
);
444 hr
= IStream_Write(pStm
, &qiresults
->std
, sizeof(qiresults
->std
), NULL
);
448 rif
.ipid
= qiresults
->std
.ipid
;
449 rif
.cPublicRefs
= qiresults
->std
.cPublicRefs
;
450 rif
.cPrivateRefs
= 0;
451 IRemUnknown_RemRelease(remunk
, 1, &rif
);
453 CoTaskMemFree(qiresults
);
456 ERR("IRemUnknown_RemQueryInterface failed with error 0x%08x\n", hr
);
463 static const IMarshalVtbl ProxyMarshal_Vtbl
=
465 Proxy_QueryInterface
,
468 StdMarshalImpl_GetUnmarshalClass
,
469 StdMarshalImpl_GetMarshalSizeMax
,
470 Proxy_MarshalInterface
,
471 StdMarshalImpl_UnmarshalInterface
,
472 StdMarshalImpl_ReleaseMarshalData
,
473 StdMarshalImpl_DisconnectObject
476 static HRESULT WINAPI
ProxyCliSec_QueryInterface(IClientSecurity
*iface
, REFIID riid
, void **ppvObject
)
478 ICOM_THIS_MULTI(struct proxy_manager
, lpVtblCliSec
, iface
);
479 return IMultiQI_QueryInterface((IMultiQI
*)&This
->lpVtbl
, riid
, ppvObject
);
482 static ULONG WINAPI
ProxyCliSec_AddRef(IClientSecurity
*iface
)
484 ICOM_THIS_MULTI(struct proxy_manager
, lpVtblCliSec
, iface
);
485 return IMultiQI_AddRef((IMultiQI
*)&This
->lpVtbl
);
488 static ULONG WINAPI
ProxyCliSec_Release(IClientSecurity
*iface
)
490 ICOM_THIS_MULTI(struct proxy_manager
, lpVtblCliSec
, iface
);
491 return IMultiQI_Release((IMultiQI
*)&This
->lpVtbl
);
494 static HRESULT WINAPI
ProxyCliSec_QueryBlanket(IClientSecurity
*iface
,
498 OLECHAR
**pServerPrincName
,
502 DWORD
*pCapabilities
)
504 FIXME("(%p, %p, %p, %p, %p, %p, %p, %p): stub\n", pProxy
, pAuthnSvc
,
505 pAuthzSvc
, pServerPrincName
, pAuthnLevel
, pImpLevel
, pAuthInfo
,
510 static HRESULT WINAPI
ProxyCliSec_SetBlanket(IClientSecurity
*iface
,
511 IUnknown
*pProxy
, DWORD AuthnSvc
,
513 OLECHAR
*pServerPrincName
,
514 DWORD AuthnLevel
, DWORD ImpLevel
,
518 FIXME("(%p, %d, %d, %s, %d, %d, %p, 0x%x): stub\n", pProxy
, AuthnSvc
,
519 AuthzSvc
, debugstr_w(pServerPrincName
), AuthnLevel
, ImpLevel
,
520 pAuthInfo
, Capabilities
);
524 static HRESULT WINAPI
ProxyCliSec_CopyProxy(IClientSecurity
*iface
,
525 IUnknown
*pProxy
, IUnknown
**ppCopy
)
527 FIXME("(%p, %p): stub\n", pProxy
, ppCopy
);
532 static const IClientSecurityVtbl ProxyCliSec_Vtbl
=
534 ProxyCliSec_QueryInterface
,
537 ProxyCliSec_QueryBlanket
,
538 ProxyCliSec_SetBlanket
,
539 ProxyCliSec_CopyProxy
542 static HRESULT
ifproxy_get_public_ref(struct ifproxy
* This
)
546 if (WAIT_OBJECT_0
!= WaitForSingleObject(This
->parent
->remoting_mutex
, INFINITE
))
548 ERR("Wait failed for ifproxy %p\n", This
);
554 IRemUnknown
*remunk
= NULL
;
556 TRACE("getting public ref for ifproxy %p\n", This
);
558 hr
= proxy_manager_get_remunknown(This
->parent
, &remunk
);
561 HRESULT hrref
= S_OK
;
563 rif
.ipid
= This
->stdobjref
.ipid
;
564 rif
.cPublicRefs
= NORMALEXTREFS
;
565 rif
.cPrivateRefs
= 0;
566 hr
= IRemUnknown_RemAddRef(remunk
, 1, &rif
, &hrref
);
567 if (hr
== S_OK
&& hrref
== S_OK
)
568 InterlockedExchangeAdd((LONG
*)&This
->refs
, NORMALEXTREFS
);
570 ERR("IRemUnknown_RemAddRef returned with 0x%08x, hrref = 0x%08x\n", hr
, hrref
);
573 ReleaseMutex(This
->parent
->remoting_mutex
);
578 static HRESULT
ifproxy_release_public_refs(struct ifproxy
* This
)
583 if (WAIT_OBJECT_0
!= WaitForSingleObject(This
->parent
->remoting_mutex
, INFINITE
))
585 ERR("Wait failed for ifproxy %p\n", This
);
589 public_refs
= This
->refs
;
592 IRemUnknown
*remunk
= NULL
;
594 TRACE("releasing %d refs\n", public_refs
);
596 hr
= proxy_manager_get_remunknown(This
->parent
, &remunk
);
600 rif
.ipid
= This
->stdobjref
.ipid
;
601 rif
.cPublicRefs
= public_refs
;
602 rif
.cPrivateRefs
= 0;
603 hr
= IRemUnknown_RemRelease(remunk
, 1, &rif
);
605 InterlockedExchangeAdd((LONG
*)&This
->refs
, -public_refs
);
606 else if (hr
== RPC_E_DISCONNECTED
)
607 WARN("couldn't release references because object was "
608 "disconnected: oxid = %s, oid = %s\n",
609 wine_dbgstr_longlong(This
->parent
->oxid
),
610 wine_dbgstr_longlong(This
->parent
->oid
));
612 ERR("IRemUnknown_RemRelease failed with error 0x%08x\n", hr
);
615 ReleaseMutex(This
->parent
->remoting_mutex
);
620 /* should be called inside This->parent->cs critical section */
621 static void ifproxy_disconnect(struct ifproxy
* This
)
623 ifproxy_release_public_refs(This
);
624 if (This
->proxy
) IRpcProxyBuffer_Disconnect(This
->proxy
);
626 IRpcChannelBuffer_Release(This
->chan
);
630 /* should be called in This->parent->cs critical section if it is an entry in parent's list */
631 static void ifproxy_destroy(struct ifproxy
* This
)
635 /* release public references to this object so that the stub can know
636 * when to destroy itself */
637 ifproxy_release_public_refs(This
);
639 list_remove(&This
->entry
);
643 IRpcChannelBuffer_Release(This
->chan
);
647 if (This
->proxy
) IRpcProxyBuffer_Release(This
->proxy
);
649 HeapFree(GetProcessHeap(), 0, This
);
652 static HRESULT
proxy_manager_construct(
653 APARTMENT
* apt
, ULONG sorflags
, OXID oxid
, OID oid
,
654 struct proxy_manager
** proxy_manager
)
656 struct proxy_manager
* This
= HeapAlloc(GetProcessHeap(), 0, sizeof(*This
));
657 if (!This
) return E_OUTOFMEMORY
;
659 This
->remoting_mutex
= CreateMutexW(NULL
, FALSE
, NULL
);
660 if (!This
->remoting_mutex
)
662 HeapFree(GetProcessHeap(), 0, This
);
663 return HRESULT_FROM_WIN32(GetLastError());
666 This
->lpVtbl
= &ClientIdentity_Vtbl
;
667 This
->lpVtblMarshal
= &ProxyMarshal_Vtbl
;
668 This
->lpVtblCliSec
= &ProxyCliSec_Vtbl
;
670 list_init(&This
->entry
);
671 list_init(&This
->interfaces
);
673 InitializeCriticalSection(&This
->cs
);
674 DEBUG_SET_CRITSEC_NAME(&This
->cs
, "proxy_manager");
676 /* the apartment the object was unmarshaled into */
679 /* the source apartment and id of the object */
685 /* the DCOM draft specification states that the SORF_NOPING flag is
686 * proxy manager specific, not ifproxy specific, so this implies that we
687 * should store the STDOBJREF flags here in the proxy manager. */
688 This
->sorflags
= sorflags
;
690 /* we create the IRemUnknown proxy on demand */
693 /* initialise these values to the weakest values and they will be
694 * overwritten in proxy_manager_set_context */
695 This
->dest_context
= MSHCTX_INPROC
;
696 This
->dest_context_data
= NULL
;
698 EnterCriticalSection(&apt
->cs
);
699 /* FIXME: we are dependent on the ordering in here to make sure a proxy's
700 * IRemUnknown proxy doesn't get destroyed before the regual proxy does
701 * because we need the IRemUnknown proxy during the destruction of the
702 * regular proxy. Ideally, we should maintain a separate list for the
703 * IRemUnknown proxies that need late destruction */
704 list_add_tail(&apt
->proxies
, &This
->entry
);
705 LeaveCriticalSection(&apt
->cs
);
707 TRACE("%p created for OXID %s, OID %s\n", This
,
708 wine_dbgstr_longlong(oxid
), wine_dbgstr_longlong(oid
));
710 *proxy_manager
= This
;
714 static inline void proxy_manager_set_context(struct proxy_manager
*This
, MSHCTX dest_context
, void *dest_context_data
)
716 MSHCTX old_dest_context
= This
->dest_context
;
717 MSHCTX new_dest_context
;
721 new_dest_context
= old_dest_context
;
722 /* "stronger" values overwrite "weaker" values. stronger values are
723 * ones that disable more optimisations */
724 switch (old_dest_context
)
727 new_dest_context
= dest_context
;
729 case MSHCTX_CROSSCTX
:
730 switch (dest_context
)
735 new_dest_context
= dest_context
;
739 switch (dest_context
)
742 case MSHCTX_CROSSCTX
:
745 new_dest_context
= dest_context
;
748 case MSHCTX_NOSHAREDMEM
:
749 switch (dest_context
)
751 case MSHCTX_DIFFERENTMACHINE
:
752 new_dest_context
= dest_context
;
762 if (old_dest_context
== new_dest_context
) break;
764 old_dest_context
= InterlockedCompareExchange((PLONG
)&This
->dest_context
, new_dest_context
, old_dest_context
);
765 } while (new_dest_context
!= old_dest_context
);
767 if (dest_context_data
)
768 InterlockedExchangePointer(&This
->dest_context_data
, dest_context_data
);
771 static HRESULT
proxy_manager_query_local_interface(struct proxy_manager
* This
, REFIID riid
, void ** ppv
)
774 struct ifproxy
* ifproxy
;
776 TRACE("%s\n", debugstr_guid(riid
));
778 if (IsEqualIID(riid
, &IID_IUnknown
) ||
779 IsEqualIID(riid
, &IID_IMultiQI
))
781 *ppv
= (void *)&This
->lpVtbl
;
782 IUnknown_AddRef((IUnknown
*)*ppv
);
785 if (IsEqualIID(riid
, &IID_IMarshal
))
787 *ppv
= (void *)&This
->lpVtblMarshal
;
788 IUnknown_AddRef((IUnknown
*)*ppv
);
791 if (IsEqualIID(riid
, &IID_IClientSecurity
))
793 *ppv
= (void *)&This
->lpVtblCliSec
;
794 IUnknown_AddRef((IUnknown
*)*ppv
);
798 hr
= proxy_manager_find_ifproxy(This
, riid
, &ifproxy
);
801 *ppv
= ifproxy
->iface
;
802 IUnknown_AddRef((IUnknown
*)*ppv
);
807 return E_NOINTERFACE
;
810 static HRESULT
proxy_manager_create_ifproxy(
811 struct proxy_manager
* This
, const STDOBJREF
*stdobjref
, REFIID riid
,
812 IRpcChannelBuffer
* channel
, struct ifproxy
** iif_out
)
815 IPSFactoryBuffer
* psfb
;
816 struct ifproxy
* ifproxy
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ifproxy
));
817 if (!ifproxy
) return E_OUTOFMEMORY
;
819 list_init(&ifproxy
->entry
);
821 ifproxy
->parent
= This
;
822 ifproxy
->stdobjref
= *stdobjref
;
823 ifproxy
->iid
= *riid
;
825 ifproxy
->proxy
= NULL
;
828 ifproxy
->chan
= channel
; /* FIXME: we should take the binding strings and construct the channel in this function */
830 /* the IUnknown interface is special because it does not have a
831 * proxy associated with the ifproxy as we handle IUnknown ourselves */
832 if (IsEqualIID(riid
, &IID_IUnknown
))
834 ifproxy
->iface
= (void *)&This
->lpVtbl
;
835 IMultiQI_AddRef((IMultiQI
*)&This
->lpVtbl
);
840 hr
= get_facbuf_for_iid(riid
, &psfb
);
843 /* important note: the outer unknown is set to the proxy manager.
844 * This ensures the COM identity rules are not violated, by having a
845 * one-to-one mapping of objects on the proxy side to objects on the
846 * stub side, no matter which interface you view the object through */
847 hr
= IPSFactoryBuffer_CreateProxy(psfb
, (IUnknown
*)&This
->lpVtbl
, riid
,
848 &ifproxy
->proxy
, &ifproxy
->iface
);
849 IPSFactoryBuffer_Release(psfb
);
851 ERR("Could not create proxy for interface %s, error 0x%08x\n",
852 debugstr_guid(riid
), hr
);
855 ERR("Could not get IPSFactoryBuffer for interface %s, error 0x%08x\n",
856 debugstr_guid(riid
), hr
);
859 hr
= IRpcProxyBuffer_Connect(ifproxy
->proxy
, ifproxy
->chan
);
864 EnterCriticalSection(&This
->cs
);
865 list_add_tail(&This
->interfaces
, &ifproxy
->entry
);
866 LeaveCriticalSection(&This
->cs
);
869 TRACE("ifproxy %p created for IPID %s, interface %s with %lu public refs\n",
870 ifproxy
, debugstr_guid(&stdobjref
->ipid
), debugstr_guid(riid
), stdobjref
->cPublicRefs
);
873 ifproxy_destroy(ifproxy
);
878 static HRESULT
proxy_manager_find_ifproxy(struct proxy_manager
* This
, REFIID riid
, struct ifproxy
** ifproxy_found
)
880 HRESULT hr
= E_NOINTERFACE
; /* assume not found */
881 struct list
* cursor
;
883 EnterCriticalSection(&This
->cs
);
884 LIST_FOR_EACH(cursor
, &This
->interfaces
)
886 struct ifproxy
* ifproxy
= LIST_ENTRY(cursor
, struct ifproxy
, entry
);
887 if (IsEqualIID(riid
, &ifproxy
->iid
))
889 *ifproxy_found
= ifproxy
;
894 LeaveCriticalSection(&This
->cs
);
899 static void proxy_manager_disconnect(struct proxy_manager
* This
)
901 struct list
* cursor
;
903 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This
->oxid
),
904 wine_dbgstr_longlong(This
->oid
));
906 EnterCriticalSection(&This
->cs
);
908 /* SORFP_NOLIFTIMEMGMT proxies (for IRemUnknown) shouldn't be
909 * disconnected - it won't do anything anyway, except cause
910 * problems for other objects that depend on this proxy always
912 if (!(This
->sorflags
& SORFP_NOLIFETIMEMGMT
))
914 LIST_FOR_EACH(cursor
, &This
->interfaces
)
916 struct ifproxy
* ifproxy
= LIST_ENTRY(cursor
, struct ifproxy
, entry
);
917 ifproxy_disconnect(ifproxy
);
921 /* apartment is being destroyed so don't keep a pointer around to it */
924 LeaveCriticalSection(&This
->cs
);
927 static HRESULT
proxy_manager_get_remunknown(struct proxy_manager
* This
, IRemUnknown
**remunk
)
931 /* we don't want to try and unmarshal or use IRemUnknown if we don't want
932 * lifetime management */
933 if (This
->sorflags
& SORFP_NOLIFETIMEMGMT
)
936 EnterCriticalSection(&This
->cs
);
938 /* already created - return existing object */
939 *remunk
= This
->remunk
;
940 else if (!This
->parent
)
941 /* disconnected - we can't create IRemUnknown */
946 /* Don't want IRemUnknown lifetime management as this is IRemUnknown!
947 * We also don't care about whether or not the stub is still alive */
948 stdobjref
.flags
= SORFP_NOLIFETIMEMGMT
| SORF_NOPING
;
949 stdobjref
.cPublicRefs
= 1;
950 /* oxid of destination object */
951 stdobjref
.oxid
= This
->oxid
;
952 /* FIXME: what should be used for the oid? The DCOM draft doesn't say */
953 stdobjref
.oid
= (OID
)-1;
954 /* FIXME: this is a hack around not having an OXID resolver yet -
955 * the OXID resolver should give us the IPID of the IRemUnknown
957 stdobjref
.ipid
.Data1
= 0xffffffff;
958 stdobjref
.ipid
.Data2
= 0xffff;
959 stdobjref
.ipid
.Data3
= 0xffff;
960 assert(sizeof(stdobjref
.ipid
.Data4
) == sizeof(stdobjref
.oxid
));
961 memcpy(&stdobjref
.ipid
.Data4
, &stdobjref
.oxid
, sizeof(OXID
));
963 /* do the unmarshal */
964 hr
= unmarshal_object(&stdobjref
, This
->parent
, This
->dest_context
,
965 This
->dest_context_data
, &IID_IRemUnknown
,
966 (void**)&This
->remunk
);
968 *remunk
= This
->remunk
;
970 LeaveCriticalSection(&This
->cs
);
972 TRACE("got IRemUnknown* pointer %p, hr = 0x%08x\n", *remunk
, hr
);
977 /* destroys a proxy manager, freeing the memory it used.
978 * Note: this function should not be called from a list iteration in the
979 * apartment, due to the fact that it removes itself from the apartment and
980 * it could add a proxy to IRemUnknown into the apartment. */
981 static void proxy_manager_destroy(struct proxy_manager
* This
)
983 struct list
* cursor
;
985 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This
->oxid
),
986 wine_dbgstr_longlong(This
->oid
));
990 EnterCriticalSection(&This
->parent
->cs
);
992 /* remove ourself from the list of proxy objects in the apartment */
993 LIST_FOR_EACH(cursor
, &This
->parent
->proxies
)
995 if (cursor
== &This
->entry
)
997 list_remove(&This
->entry
);
1002 LeaveCriticalSection(&This
->parent
->cs
);
1005 /* destroy all of the interface proxies */
1006 while ((cursor
= list_head(&This
->interfaces
)))
1008 struct ifproxy
* ifproxy
= LIST_ENTRY(cursor
, struct ifproxy
, entry
);
1009 ifproxy_destroy(ifproxy
);
1012 if (This
->remunk
) IRemUnknown_Release(This
->remunk
);
1014 DEBUG_CLEAR_CRITSEC_NAME(&This
->cs
);
1015 DeleteCriticalSection(&This
->cs
);
1017 CloseHandle(This
->remoting_mutex
);
1019 HeapFree(GetProcessHeap(), 0, This
);
1022 /* finds the proxy manager corresponding to a given OXID and OID that has
1023 * been unmarshaled in the specified apartment. The caller must release the
1024 * reference to the proxy_manager when the object is no longer used. */
1025 static BOOL
find_proxy_manager(APARTMENT
* apt
, OXID oxid
, OID oid
, struct proxy_manager
** proxy_found
)
1028 struct list
* cursor
;
1030 EnterCriticalSection(&apt
->cs
);
1031 LIST_FOR_EACH(cursor
, &apt
->proxies
)
1033 struct proxy_manager
* proxy
= LIST_ENTRY(cursor
, struct proxy_manager
, entry
);
1034 if ((oxid
== proxy
->oxid
) && (oid
== proxy
->oid
))
1036 *proxy_found
= proxy
;
1037 ClientIdentity_AddRef((IMultiQI
*)&proxy
->lpVtbl
);
1042 LeaveCriticalSection(&apt
->cs
);
1046 HRESULT
apartment_disconnectproxies(struct apartment
*apt
)
1048 struct list
* cursor
;
1050 LIST_FOR_EACH(cursor
, &apt
->proxies
)
1052 struct proxy_manager
* proxy
= LIST_ENTRY(cursor
, struct proxy_manager
, entry
);
1053 proxy_manager_disconnect(proxy
);
1059 /********************** StdMarshal implementation ****************************/
1060 typedef struct _StdMarshalImpl
1062 const IMarshalVtbl
*lpvtbl
;
1066 DWORD dwDestContext
;
1067 LPVOID pvDestContext
;
1071 static HRESULT WINAPI
1072 StdMarshalImpl_QueryInterface(LPMARSHAL iface
, REFIID riid
, LPVOID
*ppv
)
1075 if (IsEqualIID(&IID_IUnknown
, riid
) || IsEqualIID(&IID_IMarshal
, riid
))
1078 IUnknown_AddRef(iface
);
1081 FIXME("No interface for %s.\n", debugstr_guid(riid
));
1082 return E_NOINTERFACE
;
1086 StdMarshalImpl_AddRef(LPMARSHAL iface
)
1088 StdMarshalImpl
*This
= (StdMarshalImpl
*)iface
;
1089 return InterlockedIncrement(&This
->ref
);
1093 StdMarshalImpl_Release(LPMARSHAL iface
)
1095 StdMarshalImpl
*This
= (StdMarshalImpl
*)iface
;
1096 ULONG ref
= InterlockedDecrement(&This
->ref
);
1098 if (!ref
) HeapFree(GetProcessHeap(),0,This
);
1102 static HRESULT WINAPI
1103 StdMarshalImpl_GetUnmarshalClass(
1104 LPMARSHAL iface
, REFIID riid
, void* pv
, DWORD dwDestContext
,
1105 void* pvDestContext
, DWORD mshlflags
, CLSID
* pCid
)
1107 *pCid
= CLSID_DfMarshal
;
1111 static HRESULT WINAPI
1112 StdMarshalImpl_GetMarshalSizeMax(
1113 LPMARSHAL iface
, REFIID riid
, void* pv
, DWORD dwDestContext
,
1114 void* pvDestContext
, DWORD mshlflags
, DWORD
* pSize
)
1116 *pSize
= sizeof(STDOBJREF
);
1120 static HRESULT WINAPI
1121 StdMarshalImpl_MarshalInterface(
1122 LPMARSHAL iface
, IStream
*pStm
,REFIID riid
, void* pv
, DWORD dwDestContext
,
1123 void* pvDestContext
, DWORD mshlflags
)
1125 STDOBJREF stdobjref
;
1128 APARTMENT
*apt
= COM_CurrentApt();
1130 TRACE("(...,%s,...)\n", debugstr_guid(riid
));
1134 ERR("Apartment not initialized\n");
1135 return CO_E_NOTINITIALIZED
;
1138 /* make sure this apartment can be reached from other threads / processes */
1139 RPC_StartRemoting(apt
);
1141 hres
= marshal_object(apt
, &stdobjref
, riid
, (IUnknown
*)pv
, mshlflags
);
1144 ERR("Failed to create ifstub, hres=0x%x\n", hres
);
1148 hres
= IStream_Write(pStm
, &stdobjref
, sizeof(stdobjref
), &res
);
1149 if (hres
) return hres
;
1154 /* helper for StdMarshalImpl_UnmarshalInterface - does the unmarshaling with
1155 * no questions asked about the rules surrounding same-apartment unmarshals
1156 * and table marshaling */
1157 static HRESULT
unmarshal_object(const STDOBJREF
*stdobjref
, APARTMENT
*apt
,
1158 MSHCTX dest_context
, void *dest_context_data
,
1159 REFIID riid
, void **object
)
1161 struct proxy_manager
*proxy_manager
= NULL
;
1166 TRACE("stdobjref:\n\tflags = %04lx\n\tcPublicRefs = %ld\n\toxid = %s\n\toid = %s\n\tipid = %s\n",
1167 stdobjref
->flags
, stdobjref
->cPublicRefs
,
1168 wine_dbgstr_longlong(stdobjref
->oxid
),
1169 wine_dbgstr_longlong(stdobjref
->oid
),
1170 debugstr_guid(&stdobjref
->ipid
));
1172 /* create a new proxy manager if one doesn't already exist for the
1174 if (!find_proxy_manager(apt
, stdobjref
->oxid
, stdobjref
->oid
, &proxy_manager
))
1176 hr
= proxy_manager_construct(apt
, stdobjref
->flags
,
1177 stdobjref
->oxid
, stdobjref
->oid
,
1181 TRACE("proxy manager already created, using\n");
1185 struct ifproxy
* ifproxy
;
1187 proxy_manager_set_context(proxy_manager
, dest_context
, dest_context_data
);
1189 hr
= proxy_manager_find_ifproxy(proxy_manager
, riid
, &ifproxy
);
1190 if (hr
== E_NOINTERFACE
)
1192 IRpcChannelBuffer
*chanbuf
;
1193 hr
= RPC_CreateClientChannel(&stdobjref
->oxid
, &stdobjref
->ipid
,
1194 proxy_manager
->dest_context
,
1195 proxy_manager
->dest_context_data
,
1198 hr
= proxy_manager_create_ifproxy(proxy_manager
, stdobjref
,
1199 riid
, chanbuf
, &ifproxy
);
1202 IUnknown_AddRef((IUnknown
*)ifproxy
->iface
);
1206 InterlockedExchangeAdd((LONG
*)&ifproxy
->refs
, stdobjref
->cPublicRefs
);
1207 /* get at least one external reference to the object to keep it alive */
1208 hr
= ifproxy_get_public_ref(ifproxy
);
1210 ifproxy_destroy(ifproxy
);
1214 *object
= ifproxy
->iface
;
1217 /* release our reference to the proxy manager - the client/apartment
1218 * will hold on to the remaining reference for us */
1219 if (proxy_manager
) ClientIdentity_Release((IMultiQI
*)&proxy_manager
->lpVtbl
);
1224 static HRESULT WINAPI
1225 StdMarshalImpl_UnmarshalInterface(LPMARSHAL iface
, IStream
*pStm
, REFIID riid
, void **ppv
)
1227 StdMarshalImpl
*This
= (StdMarshalImpl
*)iface
;
1228 struct stub_manager
*stubmgr
;
1229 STDOBJREF stdobjref
;
1232 APARTMENT
*apt
= COM_CurrentApt();
1233 APARTMENT
*stub_apt
;
1236 TRACE("(...,%s,....)\n", debugstr_guid(riid
));
1238 /* we need an apartment to unmarshal into */
1241 ERR("Apartment not initialized\n");
1242 return CO_E_NOTINITIALIZED
;
1245 /* read STDOBJREF from wire */
1246 hres
= IStream_Read(pStm
, &stdobjref
, sizeof(stdobjref
), &res
);
1247 if (hres
) return STG_E_READFAULT
;
1249 hres
= apartment_getoxid(apt
, &oxid
);
1250 if (hres
) return hres
;
1252 /* check if we're marshalling back to ourselves */
1253 if ((oxid
== stdobjref
.oxid
) && (stubmgr
= get_stub_manager(apt
, stdobjref
.oid
)))
1255 TRACE("Unmarshalling object marshalled in same apartment for iid %s, "
1256 "returning original object %p\n", debugstr_guid(riid
), stubmgr
->object
);
1258 hres
= IUnknown_QueryInterface(stubmgr
->object
, riid
, ppv
);
1260 /* unref the ifstub. FIXME: only do this on success? */
1261 if (!stub_manager_is_table_marshaled(stubmgr
, &stdobjref
.ipid
))
1262 stub_manager_ext_release(stubmgr
, stdobjref
.cPublicRefs
, TRUE
);
1264 stub_manager_int_release(stubmgr
);
1268 /* notify stub manager about unmarshal if process-local object.
1269 * note: if the oxid is not found then we and native will quite happily
1270 * ignore table marshaling and normal marshaling rules regarding number of
1271 * unmarshals, etc, but if you abuse these rules then your proxy could end
1272 * up returning RPC_E_DISCONNECTED. */
1273 if ((stub_apt
= apartment_findfromoxid(stdobjref
.oxid
, TRUE
)))
1275 if ((stubmgr
= get_stub_manager(stub_apt
, stdobjref
.oid
)))
1277 if (!stub_manager_notify_unmarshal(stubmgr
, &stdobjref
.ipid
))
1278 hres
= CO_E_OBJNOTCONNECTED
;
1280 stub_manager_int_release(stubmgr
);
1284 WARN("Couldn't find object for OXID %s, OID %s, assuming disconnected\n",
1285 wine_dbgstr_longlong(stdobjref
.oxid
),
1286 wine_dbgstr_longlong(stdobjref
.oid
));
1287 hres
= CO_E_OBJNOTCONNECTED
;
1290 apartment_release(stub_apt
);
1293 TRACE("Treating unmarshal from OXID %s as inter-process\n",
1294 wine_dbgstr_longlong(stdobjref
.oxid
));
1297 hres
= unmarshal_object(&stdobjref
, apt
, This
->dwDestContext
,
1298 This
->pvDestContext
, riid
, ppv
);
1300 if (hres
) WARN("Failed with error 0x%08x\n", hres
);
1301 else TRACE("Successfully created proxy %p\n", *ppv
);
1306 static HRESULT WINAPI
1307 StdMarshalImpl_ReleaseMarshalData(LPMARSHAL iface
, IStream
*pStm
)
1309 STDOBJREF stdobjref
;
1312 struct stub_manager
*stubmgr
;
1315 TRACE("iface=%p, pStm=%p\n", iface
, pStm
);
1317 hres
= IStream_Read(pStm
, &stdobjref
, sizeof(stdobjref
), &res
);
1318 if (hres
) return STG_E_READFAULT
;
1320 TRACE("oxid = %s, oid = %s, ipid = %s\n",
1321 wine_dbgstr_longlong(stdobjref
.oxid
),
1322 wine_dbgstr_longlong(stdobjref
.oid
),
1323 wine_dbgstr_guid(&stdobjref
.ipid
));
1325 if (!(apt
= apartment_findfromoxid(stdobjref
.oxid
, TRUE
)))
1327 WARN("Could not map OXID %s to apartment object\n",
1328 wine_dbgstr_longlong(stdobjref
.oxid
));
1329 return RPC_E_INVALID_OBJREF
;
1332 if (!(stubmgr
= get_stub_manager(apt
, stdobjref
.oid
)))
1334 ERR("could not map object ID to stub manager, oxid=%s, oid=%s\n",
1335 wine_dbgstr_longlong(stdobjref
.oxid
), wine_dbgstr_longlong(stdobjref
.oid
));
1336 return RPC_E_INVALID_OBJREF
;
1339 stub_manager_release_marshal_data(stubmgr
, stdobjref
.cPublicRefs
, &stdobjref
.ipid
);
1341 stub_manager_int_release(stubmgr
);
1342 apartment_release(apt
);
1347 static HRESULT WINAPI
1348 StdMarshalImpl_DisconnectObject(LPMARSHAL iface
, DWORD dwReserved
)
1350 FIXME("(), stub!\n");
1354 static const IMarshalVtbl VT_StdMarshal
=
1356 StdMarshalImpl_QueryInterface
,
1357 StdMarshalImpl_AddRef
,
1358 StdMarshalImpl_Release
,
1359 StdMarshalImpl_GetUnmarshalClass
,
1360 StdMarshalImpl_GetMarshalSizeMax
,
1361 StdMarshalImpl_MarshalInterface
,
1362 StdMarshalImpl_UnmarshalInterface
,
1363 StdMarshalImpl_ReleaseMarshalData
,
1364 StdMarshalImpl_DisconnectObject
1367 static HRESULT
StdMarshalImpl_Construct(REFIID riid
, void** ppvObject
)
1369 StdMarshalImpl
* pStdMarshal
=
1370 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(StdMarshalImpl
));
1372 return E_OUTOFMEMORY
;
1373 pStdMarshal
->lpvtbl
= &VT_StdMarshal
;
1374 pStdMarshal
->ref
= 0;
1375 return IMarshal_QueryInterface((IMarshal
*)pStdMarshal
, riid
, ppvObject
);
1378 /***********************************************************************
1379 * CoGetStandardMarshal [OLE32.@]
1381 * Gets or creates a standard marshal object.
1384 * riid [I] Interface identifier of the pUnk object.
1385 * pUnk [I] Optional. Object to get the marshal object for.
1386 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1387 * pvDestContext [I] Reserved. Must be NULL.
1388 * mshlflags [I] Flags affecting the marshaling process.
1389 * ppMarshal [O] Address where marshal object will be stored.
1393 * Failure: HRESULT code.
1397 * The function retrieves the IMarshal object associated with an object if
1398 * that object is currently an active stub, otherwise a new marshal object is
1401 HRESULT WINAPI
CoGetStandardMarshal(REFIID riid
, IUnknown
*pUnk
,
1402 DWORD dwDestContext
, LPVOID pvDestContext
,
1403 DWORD mshlflags
, LPMARSHAL
*ppMarshal
)
1409 FIXME("(%s,NULL,%x,%p,%x,%p), unimplemented yet.\n",
1410 debugstr_guid(riid
),dwDestContext
,pvDestContext
,mshlflags
,ppMarshal
);
1413 TRACE("(%s,%p,%x,%p,%x,%p)\n",
1414 debugstr_guid(riid
),pUnk
,dwDestContext
,pvDestContext
,mshlflags
,ppMarshal
);
1415 *ppMarshal
= HeapAlloc(GetProcessHeap(),0,sizeof(StdMarshalImpl
));
1416 dm
= (StdMarshalImpl
*) *ppMarshal
;
1417 if (!dm
) return E_FAIL
;
1418 dm
->lpvtbl
= &VT_StdMarshal
;
1422 dm
->dwDestContext
= dwDestContext
;
1423 dm
->pvDestContext
= pvDestContext
;
1424 dm
->mshlflags
= mshlflags
;
1428 /***********************************************************************
1429 * get_marshaler [internal]
1431 * Retrieves an IMarshal interface for an object.
1433 static HRESULT
get_marshaler(REFIID riid
, IUnknown
*pUnk
, DWORD dwDestContext
,
1434 void *pvDestContext
, DWORD mshlFlags
,
1435 LPMARSHAL
*pMarshal
)
1441 hr
= IUnknown_QueryInterface(pUnk
, &IID_IMarshal
, (LPVOID
*)pMarshal
);
1443 hr
= CoGetStandardMarshal(riid
, pUnk
, dwDestContext
, pvDestContext
,
1444 mshlFlags
, pMarshal
);
1448 /***********************************************************************
1449 * get_unmarshaler_from_stream [internal]
1451 * Creates an IMarshal* object according to the data marshaled to the stream.
1452 * The function leaves the stream pointer at the start of the data written
1453 * to the stream by the IMarshal* object.
1455 static HRESULT
get_unmarshaler_from_stream(IStream
*stream
, IMarshal
**marshal
, IID
*iid
)
1461 /* read common OBJREF header */
1462 hr
= IStream_Read(stream
, &objref
, FIELD_OFFSET(OBJREF
, u_objref
), &res
);
1463 if (hr
|| (res
!= FIELD_OFFSET(OBJREF
, u_objref
)))
1465 ERR("Failed to read common OBJREF header, 0x%08x\n", hr
);
1466 return STG_E_READFAULT
;
1469 /* sanity check on header */
1470 if (objref
.signature
!= OBJREF_SIGNATURE
)
1472 ERR("Bad OBJREF signature 0x%08lx\n", objref
.signature
);
1473 return RPC_E_INVALID_OBJREF
;
1476 if (iid
) *iid
= objref
.iid
;
1478 /* FIXME: handler marshaling */
1479 if (objref
.flags
& OBJREF_STANDARD
)
1481 TRACE("Using standard unmarshaling\n");
1482 hr
= StdMarshalImpl_Construct(&IID_IMarshal
, (LPVOID
*)marshal
);
1484 else if (objref
.flags
& OBJREF_CUSTOM
)
1486 ULONG custom_header_size
= FIELD_OFFSET(OBJREF
, u_objref
.u_custom
.pData
) -
1487 FIELD_OFFSET(OBJREF
, u_objref
.u_custom
);
1488 TRACE("Using custom unmarshaling\n");
1489 /* read constant sized OR_CUSTOM data from stream */
1490 hr
= IStream_Read(stream
, &objref
.u_objref
.u_custom
,
1491 custom_header_size
, &res
);
1492 if (hr
|| (res
!= custom_header_size
))
1494 ERR("Failed to read OR_CUSTOM header, 0x%08x\n", hr
);
1495 return STG_E_READFAULT
;
1497 /* now create the marshaler specified in the stream */
1498 hr
= CoCreateInstance(&objref
.u_objref
.u_custom
.clsid
, NULL
,
1499 CLSCTX_INPROC_SERVER
, &IID_IMarshal
,
1504 FIXME("Invalid or unimplemented marshaling type specified: %lx\n",
1506 return RPC_E_INVALID_OBJREF
;
1510 ERR("Failed to create marshal, 0x%08x\n", hr
);
1515 /***********************************************************************
1516 * CoGetMarshalSizeMax [OLE32.@]
1518 * Gets the maximum amount of data that will be needed by a marshal.
1521 * pulSize [O] Address where maximum marshal size will be stored.
1522 * riid [I] Identifier of the interface to marshal.
1523 * pUnk [I] Pointer to the object to marshal.
1524 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1525 * pvDestContext [I] Reserved. Must be NULL.
1526 * mshlFlags [I] Flags that affect the marshaling. See CoMarshalInterface().
1530 * Failure: HRESULT code.
1533 * CoMarshalInterface().
1535 HRESULT WINAPI
CoGetMarshalSizeMax(ULONG
*pulSize
, REFIID riid
, IUnknown
*pUnk
,
1536 DWORD dwDestContext
, void *pvDestContext
,
1541 CLSID marshaler_clsid
;
1543 hr
= get_marshaler(riid
, pUnk
, dwDestContext
, pvDestContext
, mshlFlags
, &pMarshal
);
1547 hr
= IMarshal_GetUnmarshalClass(pMarshal
, riid
, pUnk
, dwDestContext
,
1548 pvDestContext
, mshlFlags
, &marshaler_clsid
);
1551 ERR("IMarshal::GetUnmarshalClass failed, 0x%08x\n", hr
);
1552 IMarshal_Release(pMarshal
);
1556 hr
= IMarshal_GetMarshalSizeMax(pMarshal
, riid
, pUnk
, dwDestContext
,
1557 pvDestContext
, mshlFlags
, pulSize
);
1558 if (IsEqualCLSID(&marshaler_clsid
, &CLSID_DfMarshal
))
1559 /* add on the size of the common header */
1560 *pulSize
+= FIELD_OFFSET(OBJREF
, u_objref
);
1562 /* custom marshaling: add on the size of the whole OBJREF structure
1563 * like native does */
1564 *pulSize
+= sizeof(OBJREF
);
1566 IMarshal_Release(pMarshal
);
1571 static void dump_MSHLFLAGS(MSHLFLAGS flags
)
1573 if (flags
& MSHLFLAGS_TABLESTRONG
)
1574 TRACE(" MSHLFLAGS_TABLESTRONG");
1575 if (flags
& MSHLFLAGS_TABLEWEAK
)
1576 TRACE(" MSHLFLAGS_TABLEWEAK");
1577 if (!(flags
& (MSHLFLAGS_TABLESTRONG
|MSHLFLAGS_TABLEWEAK
)))
1578 TRACE(" MSHLFLAGS_NORMAL");
1579 if (flags
& MSHLFLAGS_NOPING
)
1580 TRACE(" MSHLFLAGS_NOPING");
1583 /***********************************************************************
1584 * CoMarshalInterface [OLE32.@]
1586 * Marshals an interface into a stream so that the object can then be
1587 * unmarshaled from another COM apartment and used remotely.
1590 * pStream [I] Stream the object will be marshaled into.
1591 * riid [I] Identifier of the interface to marshal.
1592 * pUnk [I] Pointer to the object to marshal.
1593 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1594 * pvDestContext [I] Reserved. Must be NULL.
1595 * mshlFlags [I] Flags that affect the marshaling. See notes.
1599 * Failure: HRESULT code.
1603 * The mshlFlags parameter can take one or more of the following flags:
1604 *| MSHLFLAGS_NORMAL - Unmarshal once, releases stub on last proxy release.
1605 *| MSHLFLAGS_TABLESTRONG - Unmarshal many, release when CoReleaseMarshalData() called.
1606 *| MSHLFLAGS_TABLEWEAK - Unmarshal many, releases stub on last proxy release.
1607 *| MSHLFLAGS_NOPING - No automatic garbage collection (and so reduces network traffic).
1609 * If a marshaled object is not unmarshaled, then CoReleaseMarshalData() must
1610 * be called in order to release the resources used in the marshaling.
1613 * CoUnmarshalInterface(), CoReleaseMarshalData().
1615 HRESULT WINAPI
CoMarshalInterface(IStream
*pStream
, REFIID riid
, IUnknown
*pUnk
,
1616 DWORD dwDestContext
, void *pvDestContext
,
1620 CLSID marshaler_clsid
;
1624 TRACE("(%p, %s, %p, %x, %p,", pStream
, debugstr_guid(riid
), pUnk
,
1625 dwDestContext
, pvDestContext
);
1626 dump_MSHLFLAGS(mshlFlags
);
1629 if (!pUnk
|| !pStream
)
1630 return E_INVALIDARG
;
1632 objref
.signature
= OBJREF_SIGNATURE
;
1635 /* get the marshaler for the specified interface */
1636 hr
= get_marshaler(riid
, pUnk
, dwDestContext
, pvDestContext
, mshlFlags
, &pMarshal
);
1639 ERR("Failed to get marshaller, 0x%08x\n", hr
);
1643 hr
= IMarshal_GetUnmarshalClass(pMarshal
, riid
, pUnk
, dwDestContext
,
1644 pvDestContext
, mshlFlags
, &marshaler_clsid
);
1647 ERR("IMarshal::GetUnmarshalClass failed, 0x%08x\n", hr
);
1651 /* FIXME: implement handler marshaling too */
1652 if (IsEqualCLSID(&marshaler_clsid
, &CLSID_DfMarshal
))
1654 TRACE("Using standard marshaling\n");
1655 objref
.flags
= OBJREF_STANDARD
;
1657 /* write the common OBJREF header to the stream */
1658 hr
= IStream_Write(pStream
, &objref
, FIELD_OFFSET(OBJREF
, u_objref
), NULL
);
1661 ERR("Failed to write OBJREF header to stream, 0x%08x\n", hr
);
1667 TRACE("Using custom marshaling\n");
1668 objref
.flags
= OBJREF_CUSTOM
;
1669 objref
.u_objref
.u_custom
.clsid
= marshaler_clsid
;
1670 objref
.u_objref
.u_custom
.cbExtension
= 0;
1671 objref
.u_objref
.u_custom
.size
= 0;
1672 hr
= IMarshal_GetMarshalSizeMax(pMarshal
, riid
, pUnk
, dwDestContext
,
1673 pvDestContext
, mshlFlags
,
1674 &objref
.u_objref
.u_custom
.size
);
1677 ERR("Failed to get max size of marshal data, error 0x%08x\n", hr
);
1680 /* write constant sized common header and OR_CUSTOM data into stream */
1681 hr
= IStream_Write(pStream
, &objref
,
1682 FIELD_OFFSET(OBJREF
, u_objref
.u_custom
.pData
), NULL
);
1685 ERR("Failed to write OR_CUSTOM header to stream with 0x%08x\n", hr
);
1690 TRACE("Calling IMarshal::MarshalInterace\n");
1691 /* call helper object to do the actual marshaling */
1692 hr
= IMarshal_MarshalInterface(pMarshal
, pStream
, riid
, pUnk
, dwDestContext
,
1693 pvDestContext
, mshlFlags
);
1697 ERR("Failed to marshal the interface %s, %x\n", debugstr_guid(riid
), hr
);
1702 IMarshal_Release(pMarshal
);
1704 TRACE("completed with hr 0x%08x\n", hr
);
1709 /***********************************************************************
1710 * CoUnmarshalInterface [OLE32.@]
1712 * Unmarshals an object from a stream by creating a proxy to the remote
1713 * object, if necessary.
1717 * pStream [I] Stream containing the marshaled object.
1718 * riid [I] Interface identifier of the object to create a proxy to.
1719 * ppv [O] Address where proxy will be stored.
1724 * Failure: HRESULT code.
1727 * CoMarshalInterface().
1729 HRESULT WINAPI
CoUnmarshalInterface(IStream
*pStream
, REFIID riid
, LPVOID
*ppv
)
1736 TRACE("(%p, %s, %p)\n", pStream
, debugstr_guid(riid
), ppv
);
1738 if (!pStream
|| !ppv
)
1739 return E_INVALIDARG
;
1741 hr
= get_unmarshaler_from_stream(pStream
, &pMarshal
, &iid
);
1745 /* call the helper object to do the actual unmarshaling */
1746 hr
= IMarshal_UnmarshalInterface(pMarshal
, pStream
, &iid
, (LPVOID
*)&object
);
1748 ERR("IMarshal::UnmarshalInterface failed, 0x%08x\n", hr
);
1752 /* IID_NULL means use the interface ID of the marshaled object */
1753 if (!IsEqualIID(riid
, &IID_NULL
) && !IsEqualIID(riid
, &iid
))
1755 TRACE("requested interface != marshalled interface, additional QI needed\n");
1756 hr
= IUnknown_QueryInterface(object
, riid
, ppv
);
1758 ERR("Couldn't query for interface %s, hr = 0x%08x\n",
1759 debugstr_guid(riid
), hr
);
1760 IUnknown_Release(object
);
1768 IMarshal_Release(pMarshal
);
1770 TRACE("completed with hr 0x%x\n", hr
);
1775 /***********************************************************************
1776 * CoReleaseMarshalData [OLE32.@]
1778 * Releases resources associated with an object that has been marshaled into
1783 * pStream [I] The stream that the object has been marshaled into.
1787 * Failure: HRESULT error code.
1791 * Call this function to release resources associated with a normal or
1792 * table-weak marshal that will not be unmarshaled, and all table-strong
1793 * marshals when they are no longer needed.
1796 * CoMarshalInterface(), CoUnmarshalInterface().
1798 HRESULT WINAPI
CoReleaseMarshalData(IStream
*pStream
)
1803 TRACE("(%p)\n", pStream
);
1805 hr
= get_unmarshaler_from_stream(pStream
, &pMarshal
, NULL
);
1809 /* call the helper object to do the releasing of marshal data */
1810 hr
= IMarshal_ReleaseMarshalData(pMarshal
, pStream
);
1812 ERR("IMarshal::ReleaseMarshalData failed with error 0x%08x\n", hr
);
1814 IMarshal_Release(pMarshal
);
1819 /***********************************************************************
1820 * CoMarshalInterThreadInterfaceInStream [OLE32.@]
1822 * Marshal an interface across threads in the same process.
1825 * riid [I] Identifier of the interface to be marshalled.
1826 * pUnk [I] Pointer to IUnknown-derived interface that will be marshalled.
1827 * ppStm [O] Pointer to IStream object that is created and then used to store the marshalled interface.
1831 * Failure: E_OUTOFMEMORY and other COM error codes
1834 * CoMarshalInterface(), CoUnmarshalInterface() and CoGetInterfaceAndReleaseStream()
1836 HRESULT WINAPI
CoMarshalInterThreadInterfaceInStream(
1837 REFIID riid
, LPUNKNOWN pUnk
, LPSTREAM
* ppStm
)
1839 ULARGE_INTEGER xpos
;
1840 LARGE_INTEGER seekto
;
1843 TRACE("(%s, %p, %p)\n",debugstr_guid(riid
), pUnk
, ppStm
);
1845 hres
= CreateStreamOnHGlobal(NULL
, TRUE
, ppStm
);
1846 if (FAILED(hres
)) return hres
;
1847 hres
= CoMarshalInterface(*ppStm
, riid
, pUnk
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_NORMAL
);
1849 if (SUCCEEDED(hres
))
1851 memset(&seekto
, 0, sizeof(seekto
));
1852 IStream_Seek(*ppStm
, seekto
, STREAM_SEEK_SET
, &xpos
);
1856 IStream_Release(*ppStm
);
1863 /***********************************************************************
1864 * CoGetInterfaceAndReleaseStream [OLE32.@]
1866 * Unmarshalls an interface from a stream and then releases the stream.
1869 * pStm [I] Stream that contains the marshalled interface.
1870 * riid [I] Interface identifier of the object to unmarshall.
1871 * ppv [O] Address of pointer where the requested interface object will be stored.
1875 * Failure: A COM error code
1878 * CoMarshalInterThreadInterfaceInStream() and CoUnmarshalInterface()
1880 HRESULT WINAPI
CoGetInterfaceAndReleaseStream(LPSTREAM pStm
, REFIID riid
,
1885 TRACE("(%p, %s, %p)\n", pStm
, debugstr_guid(riid
), ppv
);
1887 if(!pStm
) return E_INVALIDARG
;
1888 hres
= CoUnmarshalInterface(pStm
, riid
, ppv
);
1889 IStream_Release(pStm
);
1893 static HRESULT WINAPI
StdMarshalCF_QueryInterface(LPCLASSFACTORY iface
,
1894 REFIID riid
, LPVOID
*ppv
)
1897 if (IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IClassFactory
))
1899 *ppv
= (LPVOID
)iface
;
1902 return E_NOINTERFACE
;
1905 static ULONG WINAPI
StdMarshalCF_AddRef(LPCLASSFACTORY iface
)
1907 return 2; /* non-heap based object */
1910 static ULONG WINAPI
StdMarshalCF_Release(LPCLASSFACTORY iface
)
1912 return 1; /* non-heap based object */
1915 static HRESULT WINAPI
StdMarshalCF_CreateInstance(LPCLASSFACTORY iface
,
1916 LPUNKNOWN pUnk
, REFIID riid
, LPVOID
*ppv
)
1918 if (IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IMarshal
))
1919 return StdMarshalImpl_Construct(riid
, ppv
);
1921 FIXME("(%s), not supported.\n",debugstr_guid(riid
));
1922 return E_NOINTERFACE
;
1925 static HRESULT WINAPI
StdMarshalCF_LockServer(LPCLASSFACTORY iface
, BOOL fLock
)
1927 FIXME("(%d), stub!\n",fLock
);
1931 static const IClassFactoryVtbl StdMarshalCFVtbl
=
1933 StdMarshalCF_QueryInterface
,
1934 StdMarshalCF_AddRef
,
1935 StdMarshalCF_Release
,
1936 StdMarshalCF_CreateInstance
,
1937 StdMarshalCF_LockServer
1939 static const IClassFactoryVtbl
*StdMarshalCF
= &StdMarshalCFVtbl
;
1941 HRESULT
MARSHAL_GetStandardMarshalCF(LPVOID
*ppv
)
1943 *ppv
= &StdMarshalCF
;
1947 /***********************************************************************
1948 * CoMarshalHresult [OLE32.@]
1950 * Marshals an HRESULT value into a stream.
1953 * pStm [I] Stream that hresult will be marshalled into.
1954 * hresult [I] HRESULT to be marshalled.
1958 * Failure: A COM error code
1961 * CoUnmarshalHresult().
1963 HRESULT WINAPI
CoMarshalHresult(LPSTREAM pStm
, HRESULT hresult
)
1965 return IStream_Write(pStm
, &hresult
, sizeof(hresult
), NULL
);
1968 /***********************************************************************
1969 * CoUnmarshalHresult [OLE32.@]
1971 * Unmarshals an HRESULT value from a stream.
1974 * pStm [I] Stream that hresult will be unmarshalled from.
1975 * phresult [I] Pointer to HRESULT where the value will be unmarshalled to.
1979 * Failure: A COM error code
1982 * CoMarshalHresult().
1984 HRESULT WINAPI
CoUnmarshalHresult(LPSTREAM pStm
, HRESULT
* phresult
)
1986 return IStream_Read(pStm
, phresult
, sizeof(*phresult
), NULL
);