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
49 /* private flag indicating that the object was marshaled as table-weak */
50 #define SORFP_TABLEWEAK SORF_OXRES1
51 /* private flag indicating that the caller does not want to notify the stub
52 * when the proxy disconnects or is destroyed */
53 #define SORFP_NOLIFETIMEMGMT SORF_OXRES2
55 /* imported object / proxy manager */
58 IMultiQI IMultiQI_iface
;
59 IMarshal IMarshal_iface
;
60 IClientSecurity IClientSecurity_iface
;
61 struct apartment
*parent
; /* owning apartment (RO) */
62 struct list entry
; /* entry in apartment (CS parent->cs) */
63 OXID oxid
; /* object exported ID (RO) */
64 OXID_INFO oxid_info
; /* string binding, ipid of rem unknown and other information (RO) */
65 OID oid
; /* object ID (RO) */
66 struct list interfaces
; /* imported interfaces (CS cs) */
67 LONG refs
; /* proxy reference count (LOCK) */
68 CRITICAL_SECTION cs
; /* thread safety for this object and children */
69 ULONG sorflags
; /* STDOBJREF flags (RO) */
70 IRemUnknown
*remunk
; /* proxy to IRemUnknown used for lifecycle management (CS cs) */
71 HANDLE remoting_mutex
; /* mutex used for synchronizing access to IRemUnknown */
72 MSHCTX dest_context
; /* context used for activating optimisations (LOCK) */
73 void *dest_context_data
; /* reserved context value (LOCK) */
76 static inline struct proxy_manager
*impl_from_IMarshal( IMarshal
*iface
)
78 return CONTAINING_RECORD(iface
, struct proxy_manager
, IMarshal_iface
);
81 static inline struct proxy_manager
*impl_from_IClientSecurity( IClientSecurity
*iface
)
83 return CONTAINING_RECORD(iface
, struct proxy_manager
, IClientSecurity_iface
);
86 static HRESULT
unmarshal_object(const STDOBJREF
*stdobjref
, APARTMENT
*apt
,
87 MSHCTX dest_context
, void *dest_context_data
,
88 REFIID riid
, const OXID_INFO
*oxid_info
,
91 /* Marshalling just passes a unique identifier to the remote client,
92 * that makes it possible to find the passed interface again.
94 * So basically we need a set of values that make it unique.
96 * Note that the IUnknown_QI(ob,xiid,&ppv) always returns the SAME ppv value!
98 * A triple is used: OXID (apt id), OID (stub manager id),
99 * IPID (interface ptr/stub id).
101 * OXIDs identify an apartment and are network scoped
102 * OIDs identify a stub manager and are apartment scoped
103 * IPIDs identify an interface stub and are apartment scoped
106 static inline HRESULT
get_facbuf_for_iid(REFIID riid
, IPSFactoryBuffer
**facbuf
)
111 hr
= CoGetPSClsid(riid
, &clsid
);
114 return CoGetClassObject(&clsid
, CLSCTX_INPROC_SERVER
| WINE_CLSCTX_DONT_HOST
,
115 NULL
, &IID_IPSFactoryBuffer
, (LPVOID
*)facbuf
);
118 /* marshals an object into a STDOBJREF structure */
119 HRESULT
marshal_object(APARTMENT
*apt
, STDOBJREF
*stdobjref
, REFIID riid
, IUnknown
*object
,
120 DWORD dest_context
, void *dest_context_data
, MSHLFLAGS mshlflags
)
122 struct stub_manager
*manager
;
123 struct ifstub
*ifstub
;
125 IRpcStubBuffer
*stub
= NULL
;
127 IUnknown
*iobject
= NULL
; /* object of type riid */
129 hr
= apartment_getoxid(apt
, &stdobjref
->oxid
);
133 hr
= apartment_createwindowifneeded(apt
);
137 hr
= IUnknown_QueryInterface(object
, riid
, (void **)&iobject
);
140 ERR("object doesn't expose interface %s, failing with error 0x%08x\n",
141 debugstr_guid(riid
), hr
);
142 return E_NOINTERFACE
;
145 /* IUnknown doesn't require a stub buffer, because it never goes out on
147 if (!IsEqualIID(riid
, &IID_IUnknown
))
149 IPSFactoryBuffer
*psfb
;
151 hr
= get_facbuf_for_iid(riid
, &psfb
);
154 ERR("couldn't get IPSFactory buffer for interface %s\n", debugstr_guid(riid
));
155 IUnknown_Release(iobject
);
159 hr
= IPSFactoryBuffer_CreateStub(psfb
, riid
, iobject
, &stub
);
160 IPSFactoryBuffer_Release(psfb
);
163 ERR("Failed to create an IRpcStubBuffer from IPSFactory for %s with error 0x%08x\n",
164 debugstr_guid(riid
), hr
);
165 IUnknown_Release(iobject
);
170 stdobjref
->flags
= SORF_NULL
;
171 if (mshlflags
& MSHLFLAGS_TABLEWEAK
)
172 stdobjref
->flags
|= SORFP_TABLEWEAK
;
173 if (mshlflags
& MSHLFLAGS_NOPING
)
174 stdobjref
->flags
|= SORF_NOPING
;
176 if ((manager
= get_stub_manager_from_object(apt
, object
)))
177 TRACE("registering new ifstub on pre-existing manager\n");
180 TRACE("constructing new stub manager\n");
182 manager
= new_stub_manager(apt
, object
);
185 if (stub
) IRpcStubBuffer_Release(stub
);
186 IUnknown_Release(iobject
);
187 return E_OUTOFMEMORY
;
190 stdobjref
->oid
= manager
->oid
;
192 tablemarshal
= ((mshlflags
& MSHLFLAGS_TABLESTRONG
) || (mshlflags
& MSHLFLAGS_TABLEWEAK
));
194 /* make sure ifstub that we are creating is unique */
195 ifstub
= stub_manager_find_ifstub(manager
, riid
, mshlflags
);
197 ifstub
= stub_manager_new_ifstub(manager
, stub
, iobject
, riid
, dest_context
, dest_context_data
, mshlflags
);
199 if (stub
) IRpcStubBuffer_Release(stub
);
200 IUnknown_Release(iobject
);
204 stub_manager_int_release(manager
);
205 /* destroy the stub manager if it has no ifstubs by releasing
206 * zero external references */
207 stub_manager_ext_release(manager
, 0, FALSE
, TRUE
);
208 return E_OUTOFMEMORY
;
213 stdobjref
->cPublicRefs
= NORMALEXTREFS
;
214 stub_manager_ext_addref(manager
, stdobjref
->cPublicRefs
, FALSE
);
218 stdobjref
->cPublicRefs
= 0;
219 if (mshlflags
& MSHLFLAGS_TABLESTRONG
)
220 stub_manager_ext_addref(manager
, 1, FALSE
);
222 stub_manager_ext_addref(manager
, 0, TRUE
);
225 /* FIXME: check return value */
226 RPC_RegisterInterface(riid
);
228 stdobjref
->ipid
= ifstub
->ipid
;
230 stub_manager_int_release(manager
);
236 /* Client-side identity of the server object */
238 static HRESULT
proxy_manager_get_remunknown(struct proxy_manager
* This
, IRemUnknown
**remunk
);
239 static void proxy_manager_destroy(struct proxy_manager
* This
);
240 static HRESULT
proxy_manager_find_ifproxy(struct proxy_manager
* This
, REFIID riid
, struct ifproxy
** ifproxy_found
);
241 static HRESULT
proxy_manager_query_local_interface(struct proxy_manager
* This
, REFIID riid
, void ** ppv
);
243 static HRESULT WINAPI
ClientIdentity_QueryInterface(IMultiQI
* iface
, REFIID riid
, void ** ppv
)
248 TRACE("%s\n", debugstr_guid(riid
));
251 hr
= IMultiQI_QueryMultipleInterfaces(iface
, 1, &mqi
);
257 static ULONG WINAPI
ClientIdentity_AddRef(IMultiQI
* iface
)
259 struct proxy_manager
* This
= (struct proxy_manager
*)iface
;
260 TRACE("%p - before %d\n", iface
, This
->refs
);
261 return InterlockedIncrement(&This
->refs
);
264 static ULONG WINAPI
ClientIdentity_Release(IMultiQI
* iface
)
266 struct proxy_manager
* This
= (struct proxy_manager
*)iface
;
267 ULONG refs
= InterlockedDecrement(&This
->refs
);
268 TRACE("%p - after %d\n", iface
, refs
);
270 proxy_manager_destroy(This
);
274 static HRESULT WINAPI
ClientIdentity_QueryMultipleInterfaces(IMultiQI
*iface
, ULONG cMQIs
, MULTI_QI
*pMQIs
)
276 struct proxy_manager
* This
= (struct proxy_manager
*)iface
;
277 REMQIRESULT
*qiresults
= NULL
;
278 ULONG nonlocal_mqis
= 0;
280 ULONG successful_mqis
= 0;
281 IID
*iids
= HeapAlloc(GetProcessHeap(), 0, cMQIs
* sizeof(*iids
));
282 /* mapping of RemQueryInterface index to QueryMultipleInterfaces index */
283 ULONG
*mapping
= HeapAlloc(GetProcessHeap(), 0, cMQIs
* sizeof(*mapping
));
285 TRACE("cMQIs: %d\n", cMQIs
);
287 /* try to get a local interface - this includes already active proxy
288 * interfaces and also interfaces exposed by the proxy manager */
289 for (i
= 0; i
< cMQIs
; i
++)
291 TRACE("iid[%d] = %s\n", i
, debugstr_guid(pMQIs
[i
].pIID
));
292 pMQIs
[i
].hr
= proxy_manager_query_local_interface(This
, pMQIs
[i
].pIID
, (void **)&pMQIs
[i
].pItf
);
293 if (pMQIs
[i
].hr
== S_OK
)
297 iids
[nonlocal_mqis
] = *pMQIs
[i
].pIID
;
298 mapping
[nonlocal_mqis
] = i
;
303 TRACE("%d interfaces not found locally\n", nonlocal_mqis
);
305 /* if we have more than one interface not found locally then we must try
306 * to query the remote object for it */
307 if (nonlocal_mqis
!= 0)
313 /* get the ipid of the first entry */
314 /* FIXME: should we implement ClientIdentity on the ifproxies instead
315 * of the proxy_manager so we use the correct ipid here? */
316 ipid
= &LIST_ENTRY(list_head(&This
->interfaces
), struct ifproxy
, entry
)->stdobjref
.ipid
;
318 /* get IRemUnknown proxy so we can communicate with the remote object */
319 hr
= proxy_manager_get_remunknown(This
, &remunk
);
323 hr
= IRemUnknown_RemQueryInterface(remunk
, ipid
, NORMALEXTREFS
,
324 nonlocal_mqis
, iids
, &qiresults
);
325 IRemUnknown_Release(remunk
);
327 ERR("IRemUnknown_RemQueryInterface failed with error 0x%08x\n", hr
);
330 /* IRemUnknown_RemQueryInterface can return S_FALSE if only some of
331 * the interfaces were returned */
334 /* try to unmarshal each object returned to us */
335 for (i
= 0; i
< nonlocal_mqis
; i
++)
337 ULONG index
= mapping
[i
];
338 HRESULT hrobj
= qiresults
[i
].hResult
;
340 hrobj
= unmarshal_object(&qiresults
[i
].std
, COM_CurrentApt(),
342 This
->dest_context_data
,
343 pMQIs
[index
].pIID
, &This
->oxid_info
,
344 (void **)&pMQIs
[index
].pItf
);
349 ERR("Failed to get pointer to interface %s\n", debugstr_guid(pMQIs
[index
].pIID
));
350 pMQIs
[index
].hr
= hrobj
;
354 /* free the memory allocated by the proxy */
355 CoTaskMemFree(qiresults
);
358 TRACE("%d/%d successfully queried\n", successful_mqis
, cMQIs
);
360 HeapFree(GetProcessHeap(), 0, iids
);
361 HeapFree(GetProcessHeap(), 0, mapping
);
363 if (successful_mqis
== cMQIs
)
364 return S_OK
; /* we got all requested interfaces */
365 else if (successful_mqis
== 0)
366 return E_NOINTERFACE
; /* we didn't get any interfaces */
368 return S_FALSE
; /* we got some interfaces */
371 static const IMultiQIVtbl ClientIdentity_Vtbl
=
373 ClientIdentity_QueryInterface
,
374 ClientIdentity_AddRef
,
375 ClientIdentity_Release
,
376 ClientIdentity_QueryMultipleInterfaces
379 /* FIXME: remove these */
380 static HRESULT WINAPI
StdMarshalImpl_GetUnmarshalClass(LPMARSHAL iface
, REFIID riid
, void* pv
, DWORD dwDestContext
, void* pvDestContext
, DWORD mshlflags
, CLSID
* pCid
);
381 static HRESULT WINAPI
StdMarshalImpl_GetMarshalSizeMax(LPMARSHAL iface
, REFIID riid
, void* pv
, DWORD dwDestContext
, void* pvDestContext
, DWORD mshlflags
, DWORD
* pSize
);
382 static HRESULT WINAPI
StdMarshalImpl_UnmarshalInterface(LPMARSHAL iface
, IStream
*pStm
, REFIID riid
, void **ppv
);
383 static HRESULT WINAPI
StdMarshalImpl_ReleaseMarshalData(LPMARSHAL iface
, IStream
*pStm
);
384 static HRESULT WINAPI
StdMarshalImpl_DisconnectObject(LPMARSHAL iface
, DWORD dwReserved
);
386 static HRESULT WINAPI
Proxy_QueryInterface(IMarshal
*iface
, REFIID riid
, void **ppvObject
)
388 struct proxy_manager
*This
= impl_from_IMarshal( iface
);
389 return IMultiQI_QueryInterface(&This
->IMultiQI_iface
, riid
, ppvObject
);
392 static ULONG WINAPI
Proxy_AddRef(IMarshal
*iface
)
394 struct proxy_manager
*This
= impl_from_IMarshal( iface
);
395 return IMultiQI_AddRef(&This
->IMultiQI_iface
);
398 static ULONG WINAPI
Proxy_Release(IMarshal
*iface
)
400 struct proxy_manager
*This
= impl_from_IMarshal( iface
);
401 return IMultiQI_Release(&This
->IMultiQI_iface
);
404 static HRESULT WINAPI
Proxy_MarshalInterface(
405 LPMARSHAL iface
, IStream
*pStm
, REFIID riid
, void* pv
, DWORD dwDestContext
,
406 void* pvDestContext
, DWORD mshlflags
)
408 struct proxy_manager
*This
= impl_from_IMarshal( iface
);
410 struct ifproxy
*ifproxy
;
412 TRACE("(...,%s,...)\n", debugstr_guid(riid
));
414 hr
= proxy_manager_find_ifproxy(This
, riid
, &ifproxy
);
417 STDOBJREF stdobjref
= ifproxy
->stdobjref
;
419 stdobjref
.cPublicRefs
= 0;
421 if ((mshlflags
!= MSHLFLAGS_TABLEWEAK
) &&
422 (mshlflags
!= MSHLFLAGS_TABLESTRONG
))
424 ULONG cPublicRefs
= ifproxy
->refs
;
425 ULONG cPublicRefsOld
;
426 /* optimization - share out proxy's public references if possible
427 * instead of making new proxy do a roundtrip through the server */
430 ULONG cPublicRefsNew
;
431 cPublicRefsOld
= cPublicRefs
;
432 stdobjref
.cPublicRefs
= cPublicRefs
/ 2;
433 cPublicRefsNew
= cPublicRefs
- stdobjref
.cPublicRefs
;
434 cPublicRefs
= InterlockedCompareExchange(
435 (LONG
*)&ifproxy
->refs
, cPublicRefsNew
, cPublicRefsOld
);
436 } while (cPublicRefs
!= cPublicRefsOld
);
439 /* normal and table-strong marshaling need at least one reference */
440 if (!stdobjref
.cPublicRefs
&& (mshlflags
!= MSHLFLAGS_TABLEWEAK
))
443 hr
= proxy_manager_get_remunknown(This
, &remunk
);
446 HRESULT hrref
= S_OK
;
448 rif
.ipid
= ifproxy
->stdobjref
.ipid
;
449 rif
.cPublicRefs
= (mshlflags
== MSHLFLAGS_TABLESTRONG
) ? 1 : NORMALEXTREFS
;
450 rif
.cPrivateRefs
= 0;
451 hr
= IRemUnknown_RemAddRef(remunk
, 1, &rif
, &hrref
);
452 IRemUnknown_Release(remunk
);
453 if (hr
== S_OK
&& hrref
== S_OK
)
455 /* table-strong marshaling doesn't give the refs to the
456 * client that unmarshals the STDOBJREF */
457 if (mshlflags
!= MSHLFLAGS_TABLESTRONG
)
458 stdobjref
.cPublicRefs
= rif
.cPublicRefs
;
461 ERR("IRemUnknown_RemAddRef returned with 0x%08x, hrref = 0x%08x\n", hr
, hrref
);
467 TRACE("writing stdobjref: flags = %04x cPublicRefs = %d oxid = %s oid = %s ipid = %s\n",
468 stdobjref
.flags
, stdobjref
.cPublicRefs
,
469 wine_dbgstr_longlong(stdobjref
.oxid
),
470 wine_dbgstr_longlong(stdobjref
.oid
),
471 debugstr_guid(&stdobjref
.ipid
));
472 hr
= IStream_Write(pStm
, &stdobjref
, sizeof(stdobjref
), NULL
);
477 /* we don't have the interface already unmarshaled so we have to
478 * request the object from the server */
481 REMQIRESULT
*qiresults
= NULL
;
484 /* get the ipid of the first entry */
485 /* FIXME: should we implement ClientIdentity on the ifproxies instead
486 * of the proxy_manager so we use the correct ipid here? */
487 ipid
= &LIST_ENTRY(list_head(&This
->interfaces
), struct ifproxy
, entry
)->stdobjref
.ipid
;
489 /* get IRemUnknown proxy so we can communicate with the remote object */
490 hr
= proxy_manager_get_remunknown(This
, &remunk
);
494 hr
= IRemUnknown_RemQueryInterface(remunk
, ipid
, NORMALEXTREFS
,
495 1, &iid
, &qiresults
);
498 hr
= IStream_Write(pStm
, &qiresults
->std
, sizeof(qiresults
->std
), NULL
);
502 rif
.ipid
= qiresults
->std
.ipid
;
503 rif
.cPublicRefs
= qiresults
->std
.cPublicRefs
;
504 rif
.cPrivateRefs
= 0;
505 IRemUnknown_RemRelease(remunk
, 1, &rif
);
507 CoTaskMemFree(qiresults
);
510 ERR("IRemUnknown_RemQueryInterface failed with error 0x%08x\n", hr
);
511 IRemUnknown_Release(remunk
);
518 static const IMarshalVtbl ProxyMarshal_Vtbl
=
520 Proxy_QueryInterface
,
523 StdMarshalImpl_GetUnmarshalClass
,
524 StdMarshalImpl_GetMarshalSizeMax
,
525 Proxy_MarshalInterface
,
526 StdMarshalImpl_UnmarshalInterface
,
527 StdMarshalImpl_ReleaseMarshalData
,
528 StdMarshalImpl_DisconnectObject
531 static HRESULT WINAPI
ProxyCliSec_QueryInterface(IClientSecurity
*iface
, REFIID riid
, void **ppvObject
)
533 struct proxy_manager
*This
= impl_from_IClientSecurity( iface
);
534 return IMultiQI_QueryInterface(&This
->IMultiQI_iface
, riid
, ppvObject
);
537 static ULONG WINAPI
ProxyCliSec_AddRef(IClientSecurity
*iface
)
539 struct proxy_manager
*This
= impl_from_IClientSecurity( iface
);
540 return IMultiQI_AddRef(&This
->IMultiQI_iface
);
543 static ULONG WINAPI
ProxyCliSec_Release(IClientSecurity
*iface
)
545 struct proxy_manager
*This
= impl_from_IClientSecurity( iface
);
546 return IMultiQI_Release(&This
->IMultiQI_iface
);
549 static HRESULT WINAPI
ProxyCliSec_QueryBlanket(IClientSecurity
*iface
,
553 OLECHAR
**ppServerPrincName
,
557 DWORD
*pCapabilities
)
559 FIXME("(%p, %p, %p, %p, %p, %p, %p, %p): stub\n", pProxy
, pAuthnSvc
,
560 pAuthzSvc
, ppServerPrincName
, pAuthnLevel
, pImpLevel
, pAuthInfo
,
567 if (ppServerPrincName
)
568 *ppServerPrincName
= NULL
;
570 *pAuthnLevel
= RPC_C_AUTHN_LEVEL_DEFAULT
;
572 *pImpLevel
= RPC_C_IMP_LEVEL_DEFAULT
;
576 *pCapabilities
= EOAC_NONE
;
581 static HRESULT WINAPI
ProxyCliSec_SetBlanket(IClientSecurity
*iface
,
582 IUnknown
*pProxy
, DWORD AuthnSvc
,
584 OLECHAR
*pServerPrincName
,
585 DWORD AuthnLevel
, DWORD ImpLevel
,
589 FIXME("(%p, %d, %d, %s, %d, %d, %p, 0x%x): stub\n", pProxy
, AuthnSvc
, AuthzSvc
,
590 pServerPrincName
== COLE_DEFAULT_PRINCIPAL
? "<default principal>" : debugstr_w(pServerPrincName
),
591 AuthnLevel
, ImpLevel
, pAuthInfo
, Capabilities
);
595 static HRESULT WINAPI
ProxyCliSec_CopyProxy(IClientSecurity
*iface
,
596 IUnknown
*pProxy
, IUnknown
**ppCopy
)
598 FIXME("(%p, %p): stub\n", pProxy
, ppCopy
);
603 static const IClientSecurityVtbl ProxyCliSec_Vtbl
=
605 ProxyCliSec_QueryInterface
,
608 ProxyCliSec_QueryBlanket
,
609 ProxyCliSec_SetBlanket
,
610 ProxyCliSec_CopyProxy
613 static HRESULT
ifproxy_get_public_ref(struct ifproxy
* This
)
617 if (WAIT_OBJECT_0
!= WaitForSingleObject(This
->parent
->remoting_mutex
, INFINITE
))
619 ERR("Wait failed for ifproxy %p\n", This
);
625 IRemUnknown
*remunk
= NULL
;
627 TRACE("getting public ref for ifproxy %p\n", This
);
629 hr
= proxy_manager_get_remunknown(This
->parent
, &remunk
);
632 HRESULT hrref
= S_OK
;
634 rif
.ipid
= This
->stdobjref
.ipid
;
635 rif
.cPublicRefs
= NORMALEXTREFS
;
636 rif
.cPrivateRefs
= 0;
637 hr
= IRemUnknown_RemAddRef(remunk
, 1, &rif
, &hrref
);
638 IRemUnknown_Release(remunk
);
639 if (hr
== S_OK
&& hrref
== S_OK
)
640 InterlockedExchangeAdd((LONG
*)&This
->refs
, NORMALEXTREFS
);
642 ERR("IRemUnknown_RemAddRef returned with 0x%08x, hrref = 0x%08x\n", hr
, hrref
);
645 ReleaseMutex(This
->parent
->remoting_mutex
);
650 static HRESULT
ifproxy_release_public_refs(struct ifproxy
* This
)
655 if (WAIT_OBJECT_0
!= WaitForSingleObject(This
->parent
->remoting_mutex
, INFINITE
))
657 ERR("Wait failed for ifproxy %p\n", This
);
661 public_refs
= This
->refs
;
664 IRemUnknown
*remunk
= NULL
;
666 TRACE("releasing %d refs\n", public_refs
);
668 hr
= proxy_manager_get_remunknown(This
->parent
, &remunk
);
672 rif
.ipid
= This
->stdobjref
.ipid
;
673 rif
.cPublicRefs
= public_refs
;
674 rif
.cPrivateRefs
= 0;
675 hr
= IRemUnknown_RemRelease(remunk
, 1, &rif
);
676 IRemUnknown_Release(remunk
);
678 InterlockedExchangeAdd((LONG
*)&This
->refs
, -public_refs
);
679 else if (hr
== RPC_E_DISCONNECTED
)
680 WARN("couldn't release references because object was "
681 "disconnected: oxid = %s, oid = %s\n",
682 wine_dbgstr_longlong(This
->parent
->oxid
),
683 wine_dbgstr_longlong(This
->parent
->oid
));
685 ERR("IRemUnknown_RemRelease failed with error 0x%08x\n", hr
);
688 ReleaseMutex(This
->parent
->remoting_mutex
);
693 /* should be called inside This->parent->cs critical section */
694 static void ifproxy_disconnect(struct ifproxy
* This
)
696 ifproxy_release_public_refs(This
);
697 if (This
->proxy
) IRpcProxyBuffer_Disconnect(This
->proxy
);
699 IRpcChannelBuffer_Release(This
->chan
);
703 /* should be called in This->parent->cs critical section if it is an entry in parent's list */
704 static void ifproxy_destroy(struct ifproxy
* This
)
708 /* release public references to this object so that the stub can know
709 * when to destroy itself */
710 ifproxy_release_public_refs(This
);
712 list_remove(&This
->entry
);
716 IRpcChannelBuffer_Release(This
->chan
);
720 if (This
->proxy
) IRpcProxyBuffer_Release(This
->proxy
);
722 HeapFree(GetProcessHeap(), 0, This
);
725 static HRESULT
proxy_manager_construct(
726 APARTMENT
* apt
, ULONG sorflags
, OXID oxid
, OID oid
,
727 const OXID_INFO
*oxid_info
, struct proxy_manager
** proxy_manager
)
729 struct proxy_manager
* This
= HeapAlloc(GetProcessHeap(), 0, sizeof(*This
));
730 if (!This
) return E_OUTOFMEMORY
;
732 This
->remoting_mutex
= CreateMutexW(NULL
, FALSE
, NULL
);
733 if (!This
->remoting_mutex
)
735 HeapFree(GetProcessHeap(), 0, This
);
736 return HRESULT_FROM_WIN32(GetLastError());
741 This
->oxid_info
.dwPid
= oxid_info
->dwPid
;
742 This
->oxid_info
.dwTid
= oxid_info
->dwTid
;
743 This
->oxid_info
.ipidRemUnknown
= oxid_info
->ipidRemUnknown
;
744 This
->oxid_info
.dwAuthnHint
= oxid_info
->dwAuthnHint
;
745 This
->oxid_info
.psa
= NULL
/* FIXME: copy from oxid_info */;
749 HRESULT hr
= RPC_ResolveOxid(oxid
, &This
->oxid_info
);
752 CloseHandle(This
->remoting_mutex
);
753 HeapFree(GetProcessHeap(), 0, This
);
758 This
->IMultiQI_iface
.lpVtbl
= &ClientIdentity_Vtbl
;
759 This
->IMarshal_iface
.lpVtbl
= &ProxyMarshal_Vtbl
;
760 This
->IClientSecurity_iface
.lpVtbl
= &ProxyCliSec_Vtbl
;
762 list_init(&This
->entry
);
763 list_init(&This
->interfaces
);
765 InitializeCriticalSection(&This
->cs
);
766 DEBUG_SET_CRITSEC_NAME(&This
->cs
, "proxy_manager");
768 /* the apartment the object was unmarshaled into */
771 /* the source apartment and id of the object */
777 /* the DCOM draft specification states that the SORF_NOPING flag is
778 * proxy manager specific, not ifproxy specific, so this implies that we
779 * should store the STDOBJREF flags here in the proxy manager. */
780 This
->sorflags
= sorflags
;
782 /* we create the IRemUnknown proxy on demand */
785 /* initialise these values to the weakest values and they will be
786 * overwritten in proxy_manager_set_context */
787 This
->dest_context
= MSHCTX_INPROC
;
788 This
->dest_context_data
= NULL
;
790 EnterCriticalSection(&apt
->cs
);
791 /* FIXME: we are dependent on the ordering in here to make sure a proxy's
792 * IRemUnknown proxy doesn't get destroyed before the regual proxy does
793 * because we need the IRemUnknown proxy during the destruction of the
794 * regular proxy. Ideally, we should maintain a separate list for the
795 * IRemUnknown proxies that need late destruction */
796 list_add_tail(&apt
->proxies
, &This
->entry
);
797 LeaveCriticalSection(&apt
->cs
);
799 TRACE("%p created for OXID %s, OID %s\n", This
,
800 wine_dbgstr_longlong(oxid
), wine_dbgstr_longlong(oid
));
802 *proxy_manager
= This
;
806 static inline void proxy_manager_set_context(struct proxy_manager
*This
, MSHCTX dest_context
, void *dest_context_data
)
808 MSHCTX old_dest_context
;
809 MSHCTX new_dest_context
;
813 old_dest_context
= This
->dest_context
;
814 new_dest_context
= old_dest_context
;
815 /* "stronger" values overwrite "weaker" values. stronger values are
816 * ones that disable more optimisations */
817 switch (old_dest_context
)
820 new_dest_context
= dest_context
;
822 case MSHCTX_CROSSCTX
:
823 switch (dest_context
)
828 new_dest_context
= dest_context
;
832 switch (dest_context
)
835 case MSHCTX_CROSSCTX
:
838 new_dest_context
= dest_context
;
841 case MSHCTX_NOSHAREDMEM
:
842 switch (dest_context
)
844 case MSHCTX_DIFFERENTMACHINE
:
845 new_dest_context
= dest_context
;
855 if (old_dest_context
== new_dest_context
) break;
857 new_dest_context
= InterlockedCompareExchange((PLONG
)&This
->dest_context
, new_dest_context
, old_dest_context
);
858 } while (new_dest_context
!= old_dest_context
);
860 if (dest_context_data
)
861 InterlockedExchangePointer(&This
->dest_context_data
, dest_context_data
);
864 static HRESULT
proxy_manager_query_local_interface(struct proxy_manager
* This
, REFIID riid
, void ** ppv
)
867 struct ifproxy
* ifproxy
;
869 TRACE("%s\n", debugstr_guid(riid
));
871 if (IsEqualIID(riid
, &IID_IUnknown
) ||
872 IsEqualIID(riid
, &IID_IMultiQI
))
874 *ppv
= &This
->IMultiQI_iface
;
875 IMultiQI_AddRef(&This
->IMultiQI_iface
);
878 if (IsEqualIID(riid
, &IID_IMarshal
))
880 *ppv
= &This
->IMarshal_iface
;
881 IMarshal_AddRef(&This
->IMarshal_iface
);
884 if (IsEqualIID(riid
, &IID_IClientSecurity
))
886 *ppv
= &This
->IClientSecurity_iface
;
887 IClientSecurity_AddRef(&This
->IClientSecurity_iface
);
891 hr
= proxy_manager_find_ifproxy(This
, riid
, &ifproxy
);
894 *ppv
= ifproxy
->iface
;
895 IUnknown_AddRef((IUnknown
*)*ppv
);
900 return E_NOINTERFACE
;
903 static HRESULT
proxy_manager_create_ifproxy(
904 struct proxy_manager
* This
, const STDOBJREF
*stdobjref
, REFIID riid
,
905 IRpcChannelBuffer
* channel
, struct ifproxy
** iif_out
)
908 IPSFactoryBuffer
* psfb
;
909 struct ifproxy
* ifproxy
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ifproxy
));
910 if (!ifproxy
) return E_OUTOFMEMORY
;
912 list_init(&ifproxy
->entry
);
914 ifproxy
->parent
= This
;
915 ifproxy
->stdobjref
= *stdobjref
;
916 ifproxy
->iid
= *riid
;
918 ifproxy
->proxy
= NULL
;
921 ifproxy
->chan
= channel
; /* FIXME: we should take the binding strings and construct the channel in this function */
923 /* the IUnknown interface is special because it does not have a
924 * proxy associated with the ifproxy as we handle IUnknown ourselves */
925 if (IsEqualIID(riid
, &IID_IUnknown
))
927 ifproxy
->iface
= &This
->IMultiQI_iface
;
928 IMultiQI_AddRef(&This
->IMultiQI_iface
);
933 hr
= get_facbuf_for_iid(riid
, &psfb
);
936 /* important note: the outer unknown is set to the proxy manager.
937 * This ensures the COM identity rules are not violated, by having a
938 * one-to-one mapping of objects on the proxy side to objects on the
939 * stub side, no matter which interface you view the object through */
940 hr
= IPSFactoryBuffer_CreateProxy(psfb
, (IUnknown
*)&This
->IMultiQI_iface
, riid
,
941 &ifproxy
->proxy
, &ifproxy
->iface
);
942 IPSFactoryBuffer_Release(psfb
);
944 ERR("Could not create proxy for interface %s, error 0x%08x\n",
945 debugstr_guid(riid
), hr
);
948 ERR("Could not get IPSFactoryBuffer for interface %s, error 0x%08x\n",
949 debugstr_guid(riid
), hr
);
952 hr
= IRpcProxyBuffer_Connect(ifproxy
->proxy
, ifproxy
->chan
);
957 EnterCriticalSection(&This
->cs
);
958 list_add_tail(&This
->interfaces
, &ifproxy
->entry
);
959 LeaveCriticalSection(&This
->cs
);
962 TRACE("ifproxy %p created for IPID %s, interface %s with %u public refs\n",
963 ifproxy
, debugstr_guid(&stdobjref
->ipid
), debugstr_guid(riid
), stdobjref
->cPublicRefs
);
966 ifproxy_destroy(ifproxy
);
971 static HRESULT
proxy_manager_find_ifproxy(struct proxy_manager
* This
, REFIID riid
, struct ifproxy
** ifproxy_found
)
973 HRESULT hr
= E_NOINTERFACE
; /* assume not found */
974 struct list
* cursor
;
976 EnterCriticalSection(&This
->cs
);
977 LIST_FOR_EACH(cursor
, &This
->interfaces
)
979 struct ifproxy
* ifproxy
= LIST_ENTRY(cursor
, struct ifproxy
, entry
);
980 if (IsEqualIID(riid
, &ifproxy
->iid
))
982 *ifproxy_found
= ifproxy
;
987 LeaveCriticalSection(&This
->cs
);
992 static void proxy_manager_disconnect(struct proxy_manager
* This
)
994 struct list
* cursor
;
996 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This
->oxid
),
997 wine_dbgstr_longlong(This
->oid
));
999 EnterCriticalSection(&This
->cs
);
1001 /* SORFP_NOLIFTIMEMGMT proxies (for IRemUnknown) shouldn't be
1002 * disconnected - it won't do anything anyway, except cause
1003 * problems for other objects that depend on this proxy always
1005 if (!(This
->sorflags
& SORFP_NOLIFETIMEMGMT
))
1007 LIST_FOR_EACH(cursor
, &This
->interfaces
)
1009 struct ifproxy
* ifproxy
= LIST_ENTRY(cursor
, struct ifproxy
, entry
);
1010 ifproxy_disconnect(ifproxy
);
1014 /* apartment is being destroyed so don't keep a pointer around to it */
1015 This
->parent
= NULL
;
1017 LeaveCriticalSection(&This
->cs
);
1020 static HRESULT
proxy_manager_get_remunknown(struct proxy_manager
* This
, IRemUnknown
**remunk
)
1023 struct apartment
*apt
;
1024 BOOL called_in_original_apt
;
1026 /* we don't want to try and unmarshal or use IRemUnknown if we don't want
1027 * lifetime management */
1028 if (This
->sorflags
& SORFP_NOLIFETIMEMGMT
)
1031 apt
= COM_CurrentApt();
1033 return CO_E_NOTINITIALIZED
;
1035 called_in_original_apt
= This
->parent
&& (This
->parent
->oxid
== apt
->oxid
);
1037 EnterCriticalSection(&This
->cs
);
1038 /* only return the cached object if called from the original apartment.
1039 * in future, we might want to make the IRemUnknown proxy callable from any
1040 * apartment to avoid these checks */
1041 if (This
->remunk
&& called_in_original_apt
)
1043 /* already created - return existing object */
1044 *remunk
= This
->remunk
;
1045 IRemUnknown_AddRef(*remunk
);
1047 else if (!This
->parent
)
1049 /* disconnected - we can't create IRemUnknown */
1055 STDOBJREF stdobjref
;
1056 /* Don't want IRemUnknown lifetime management as this is IRemUnknown!
1057 * We also don't care about whether or not the stub is still alive */
1058 stdobjref
.flags
= SORFP_NOLIFETIMEMGMT
| SORF_NOPING
;
1059 stdobjref
.cPublicRefs
= 1;
1060 /* oxid of destination object */
1061 stdobjref
.oxid
= This
->oxid
;
1062 /* FIXME: what should be used for the oid? The DCOM draft doesn't say */
1063 stdobjref
.oid
= (OID
)-1;
1064 stdobjref
.ipid
= This
->oxid_info
.ipidRemUnknown
;
1066 /* do the unmarshal */
1067 hr
= unmarshal_object(&stdobjref
, COM_CurrentApt(), This
->dest_context
,
1068 This
->dest_context_data
, &IID_IRemUnknown
,
1069 &This
->oxid_info
, (void**)remunk
);
1070 if (hr
== S_OK
&& called_in_original_apt
)
1072 This
->remunk
= *remunk
;
1073 IRemUnknown_AddRef(This
->remunk
);
1076 LeaveCriticalSection(&This
->cs
);
1078 TRACE("got IRemUnknown* pointer %p, hr = 0x%08x\n", *remunk
, hr
);
1083 /* destroys a proxy manager, freeing the memory it used.
1084 * Note: this function should not be called from a list iteration in the
1085 * apartment, due to the fact that it removes itself from the apartment and
1086 * it could add a proxy to IRemUnknown into the apartment. */
1087 static void proxy_manager_destroy(struct proxy_manager
* This
)
1089 struct list
* cursor
;
1091 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This
->oxid
),
1092 wine_dbgstr_longlong(This
->oid
));
1096 EnterCriticalSection(&This
->parent
->cs
);
1098 /* remove ourself from the list of proxy objects in the apartment */
1099 LIST_FOR_EACH(cursor
, &This
->parent
->proxies
)
1101 if (cursor
== &This
->entry
)
1103 list_remove(&This
->entry
);
1108 LeaveCriticalSection(&This
->parent
->cs
);
1111 /* destroy all of the interface proxies */
1112 while ((cursor
= list_head(&This
->interfaces
)))
1114 struct ifproxy
* ifproxy
= LIST_ENTRY(cursor
, struct ifproxy
, entry
);
1115 ifproxy_destroy(ifproxy
);
1118 if (This
->remunk
) IRemUnknown_Release(This
->remunk
);
1119 CoTaskMemFree(This
->oxid_info
.psa
);
1121 DEBUG_CLEAR_CRITSEC_NAME(&This
->cs
);
1122 DeleteCriticalSection(&This
->cs
);
1124 CloseHandle(This
->remoting_mutex
);
1126 HeapFree(GetProcessHeap(), 0, This
);
1129 /* finds the proxy manager corresponding to a given OXID and OID that has
1130 * been unmarshaled in the specified apartment. The caller must release the
1131 * reference to the proxy_manager when the object is no longer used. */
1132 static BOOL
find_proxy_manager(APARTMENT
* apt
, OXID oxid
, OID oid
, struct proxy_manager
** proxy_found
)
1135 struct list
* cursor
;
1137 EnterCriticalSection(&apt
->cs
);
1138 LIST_FOR_EACH(cursor
, &apt
->proxies
)
1140 struct proxy_manager
* proxy
= LIST_ENTRY(cursor
, struct proxy_manager
, entry
);
1141 if ((oxid
== proxy
->oxid
) && (oid
== proxy
->oid
))
1143 /* be careful of a race with ClientIdentity_Release, which would
1144 * cause us to return a proxy which is in the process of being
1146 if (IMultiQI_AddRef(&proxy
->IMultiQI_iface
) != 0)
1148 *proxy_found
= proxy
;
1154 LeaveCriticalSection(&apt
->cs
);
1158 HRESULT
apartment_disconnectproxies(struct apartment
*apt
)
1160 struct list
* cursor
;
1162 LIST_FOR_EACH(cursor
, &apt
->proxies
)
1164 struct proxy_manager
* proxy
= LIST_ENTRY(cursor
, struct proxy_manager
, entry
);
1165 proxy_manager_disconnect(proxy
);
1171 /********************** StdMarshal implementation ****************************/
1172 typedef struct _StdMarshalImpl
1174 IMarshal IMarshal_iface
;
1177 void *dest_context_data
;
1180 static inline StdMarshalImpl
*impl_from_StdMarshal(IMarshal
*iface
)
1182 return CONTAINING_RECORD(iface
, StdMarshalImpl
, IMarshal_iface
);
1185 static HRESULT WINAPI
1186 StdMarshalImpl_QueryInterface(IMarshal
*iface
, REFIID riid
, void **ppv
)
1189 if (IsEqualIID(&IID_IUnknown
, riid
) || IsEqualIID(&IID_IMarshal
, riid
))
1192 IMarshal_AddRef(iface
);
1195 FIXME("No interface for %s.\n", debugstr_guid(riid
));
1196 return E_NOINTERFACE
;
1200 StdMarshalImpl_AddRef(IMarshal
*iface
)
1202 StdMarshalImpl
*This
= impl_from_StdMarshal(iface
);
1203 return InterlockedIncrement(&This
->ref
);
1207 StdMarshalImpl_Release(IMarshal
*iface
)
1209 StdMarshalImpl
*This
= impl_from_StdMarshal(iface
);
1210 ULONG ref
= InterlockedDecrement(&This
->ref
);
1212 if (!ref
) HeapFree(GetProcessHeap(),0,This
);
1216 static HRESULT WINAPI
1217 StdMarshalImpl_GetUnmarshalClass(
1218 IMarshal
*iface
, REFIID riid
, void* pv
, DWORD dwDestContext
,
1219 void* pvDestContext
, DWORD mshlflags
, CLSID
* pCid
)
1221 *pCid
= CLSID_DfMarshal
;
1225 static HRESULT WINAPI
1226 StdMarshalImpl_GetMarshalSizeMax(
1227 IMarshal
*iface
, REFIID riid
, void* pv
, DWORD dwDestContext
,
1228 void* pvDestContext
, DWORD mshlflags
, DWORD
* pSize
)
1230 *pSize
= sizeof(STDOBJREF
);
1234 static HRESULT WINAPI
1235 StdMarshalImpl_MarshalInterface(
1236 IMarshal
*iface
, IStream
*pStm
,REFIID riid
, void* pv
, DWORD dest_context
,
1237 void* dest_context_data
, DWORD mshlflags
)
1239 STDOBJREF stdobjref
;
1242 APARTMENT
*apt
= COM_CurrentApt();
1244 TRACE("(...,%s,...)\n", debugstr_guid(riid
));
1248 ERR("Apartment not initialized\n");
1249 return CO_E_NOTINITIALIZED
;
1252 /* make sure this apartment can be reached from other threads / processes */
1253 RPC_StartRemoting(apt
);
1255 hres
= marshal_object(apt
, &stdobjref
, riid
, pv
, dest_context
, dest_context_data
, mshlflags
);
1258 ERR("Failed to create ifstub, hres=0x%x\n", hres
);
1262 return IStream_Write(pStm
, &stdobjref
, sizeof(stdobjref
), &res
);
1265 /* helper for StdMarshalImpl_UnmarshalInterface - does the unmarshaling with
1266 * no questions asked about the rules surrounding same-apartment unmarshals
1267 * and table marshaling */
1268 static HRESULT
unmarshal_object(const STDOBJREF
*stdobjref
, APARTMENT
*apt
,
1269 MSHCTX dest_context
, void *dest_context_data
,
1270 REFIID riid
, const OXID_INFO
*oxid_info
,
1273 struct proxy_manager
*proxy_manager
= NULL
;
1278 TRACE("stdobjref: flags = %04x cPublicRefs = %d oxid = %s oid = %s ipid = %s\n",
1279 stdobjref
->flags
, stdobjref
->cPublicRefs
,
1280 wine_dbgstr_longlong(stdobjref
->oxid
),
1281 wine_dbgstr_longlong(stdobjref
->oid
),
1282 debugstr_guid(&stdobjref
->ipid
));
1284 /* create a new proxy manager if one doesn't already exist for the
1286 if (!find_proxy_manager(apt
, stdobjref
->oxid
, stdobjref
->oid
, &proxy_manager
))
1288 hr
= proxy_manager_construct(apt
, stdobjref
->flags
,
1289 stdobjref
->oxid
, stdobjref
->oid
, oxid_info
,
1293 TRACE("proxy manager already created, using\n");
1297 struct ifproxy
* ifproxy
;
1299 proxy_manager_set_context(proxy_manager
, dest_context
, dest_context_data
);
1301 hr
= proxy_manager_find_ifproxy(proxy_manager
, riid
, &ifproxy
);
1302 if (hr
== E_NOINTERFACE
)
1304 IRpcChannelBuffer
*chanbuf
;
1305 hr
= RPC_CreateClientChannel(&stdobjref
->oxid
, &stdobjref
->ipid
,
1306 &proxy_manager
->oxid_info
,
1307 proxy_manager
->dest_context
,
1308 proxy_manager
->dest_context_data
,
1311 hr
= proxy_manager_create_ifproxy(proxy_manager
, stdobjref
,
1312 riid
, chanbuf
, &ifproxy
);
1315 IUnknown_AddRef((IUnknown
*)ifproxy
->iface
);
1319 InterlockedExchangeAdd((LONG
*)&ifproxy
->refs
, stdobjref
->cPublicRefs
);
1320 /* get at least one external reference to the object to keep it alive */
1321 hr
= ifproxy_get_public_ref(ifproxy
);
1323 ifproxy_destroy(ifproxy
);
1327 *object
= ifproxy
->iface
;
1330 /* release our reference to the proxy manager - the client/apartment
1331 * will hold on to the remaining reference for us */
1332 if (proxy_manager
) IMultiQI_Release(&proxy_manager
->IMultiQI_iface
);
1337 static HRESULT WINAPI
1338 StdMarshalImpl_UnmarshalInterface(IMarshal
*iface
, IStream
*pStm
, REFIID riid
, void **ppv
)
1340 StdMarshalImpl
*This
= impl_from_StdMarshal(iface
);
1341 struct stub_manager
*stubmgr
= NULL
;
1342 STDOBJREF stdobjref
;
1345 APARTMENT
*apt
= COM_CurrentApt();
1346 APARTMENT
*stub_apt
;
1349 TRACE("(...,%s,....)\n", debugstr_guid(riid
));
1351 /* we need an apartment to unmarshal into */
1354 ERR("Apartment not initialized\n");
1355 return CO_E_NOTINITIALIZED
;
1358 /* read STDOBJREF from wire */
1359 hres
= IStream_Read(pStm
, &stdobjref
, sizeof(stdobjref
), &res
);
1360 if (hres
!= S_OK
) return STG_E_READFAULT
;
1362 hres
= apartment_getoxid(apt
, &oxid
);
1363 if (hres
!= S_OK
) return hres
;
1365 /* check if we're marshalling back to ourselves */
1366 if ((oxid
== stdobjref
.oxid
) && (stubmgr
= get_stub_manager(apt
, stdobjref
.oid
)))
1368 TRACE("Unmarshalling object marshalled in same apartment for iid %s, "
1369 "returning original object %p\n", debugstr_guid(riid
), stubmgr
->object
);
1371 hres
= IUnknown_QueryInterface(stubmgr
->object
, riid
, ppv
);
1373 /* unref the ifstub. FIXME: only do this on success? */
1374 if (!stub_manager_is_table_marshaled(stubmgr
, &stdobjref
.ipid
))
1375 stub_manager_ext_release(stubmgr
, stdobjref
.cPublicRefs
, stdobjref
.flags
& SORFP_TABLEWEAK
, FALSE
);
1377 stub_manager_int_release(stubmgr
);
1381 /* notify stub manager about unmarshal if process-local object.
1382 * note: if the oxid is not found then we and native will quite happily
1383 * ignore table marshaling and normal marshaling rules regarding number of
1384 * unmarshals, etc, but if you abuse these rules then your proxy could end
1385 * up returning RPC_E_DISCONNECTED. */
1386 if ((stub_apt
= apartment_findfromoxid(stdobjref
.oxid
, TRUE
)))
1388 if ((stubmgr
= get_stub_manager(stub_apt
, stdobjref
.oid
)))
1390 if (!stub_manager_notify_unmarshal(stubmgr
, &stdobjref
.ipid
))
1391 hres
= CO_E_OBJNOTCONNECTED
;
1395 WARN("Couldn't find object for OXID %s, OID %s, assuming disconnected\n",
1396 wine_dbgstr_longlong(stdobjref
.oxid
),
1397 wine_dbgstr_longlong(stdobjref
.oid
));
1398 hres
= CO_E_OBJNOTCONNECTED
;
1402 TRACE("Treating unmarshal from OXID %s as inter-process\n",
1403 wine_dbgstr_longlong(stdobjref
.oxid
));
1406 hres
= unmarshal_object(&stdobjref
, apt
, This
->dest_context
,
1407 This
->dest_context_data
, riid
,
1408 stubmgr
? &stubmgr
->oxid_info
: NULL
, ppv
);
1410 if (stubmgr
) stub_manager_int_release(stubmgr
);
1411 if (stub_apt
) apartment_release(stub_apt
);
1413 if (hres
!= S_OK
) WARN("Failed with error 0x%08x\n", hres
);
1414 else TRACE("Successfully created proxy %p\n", *ppv
);
1419 static HRESULT WINAPI
1420 StdMarshalImpl_ReleaseMarshalData(IMarshal
*iface
, IStream
*pStm
)
1422 STDOBJREF stdobjref
;
1425 struct stub_manager
*stubmgr
;
1428 TRACE("iface=%p, pStm=%p\n", iface
, pStm
);
1430 hres
= IStream_Read(pStm
, &stdobjref
, sizeof(stdobjref
), &res
);
1431 if (hres
!= S_OK
) return STG_E_READFAULT
;
1433 TRACE("oxid = %s, oid = %s, ipid = %s\n",
1434 wine_dbgstr_longlong(stdobjref
.oxid
),
1435 wine_dbgstr_longlong(stdobjref
.oid
),
1436 wine_dbgstr_guid(&stdobjref
.ipid
));
1438 if (!(apt
= apartment_findfromoxid(stdobjref
.oxid
, TRUE
)))
1440 WARN("Could not map OXID %s to apartment object\n",
1441 wine_dbgstr_longlong(stdobjref
.oxid
));
1442 return RPC_E_INVALID_OBJREF
;
1445 if (!(stubmgr
= get_stub_manager(apt
, stdobjref
.oid
)))
1447 apartment_release(apt
);
1448 ERR("could not map object ID to stub manager, oxid=%s, oid=%s\n",
1449 wine_dbgstr_longlong(stdobjref
.oxid
), wine_dbgstr_longlong(stdobjref
.oid
));
1450 return RPC_E_INVALID_OBJREF
;
1453 stub_manager_release_marshal_data(stubmgr
, stdobjref
.cPublicRefs
, &stdobjref
.ipid
, stdobjref
.flags
& SORFP_TABLEWEAK
);
1455 stub_manager_int_release(stubmgr
);
1456 apartment_release(apt
);
1461 static HRESULT WINAPI
1462 StdMarshalImpl_DisconnectObject(IMarshal
*iface
, DWORD dwReserved
)
1464 FIXME("(), stub!\n");
1468 static const IMarshalVtbl StdMarshalVtbl
=
1470 StdMarshalImpl_QueryInterface
,
1471 StdMarshalImpl_AddRef
,
1472 StdMarshalImpl_Release
,
1473 StdMarshalImpl_GetUnmarshalClass
,
1474 StdMarshalImpl_GetMarshalSizeMax
,
1475 StdMarshalImpl_MarshalInterface
,
1476 StdMarshalImpl_UnmarshalInterface
,
1477 StdMarshalImpl_ReleaseMarshalData
,
1478 StdMarshalImpl_DisconnectObject
1481 static HRESULT
StdMarshalImpl_Construct(REFIID riid
, DWORD dest_context
, void *dest_context_data
, void** ppvObject
)
1485 StdMarshalImpl
*pStdMarshal
= HeapAlloc(GetProcessHeap(), 0, sizeof(StdMarshalImpl
));
1487 return E_OUTOFMEMORY
;
1489 pStdMarshal
->IMarshal_iface
.lpVtbl
= &StdMarshalVtbl
;
1490 pStdMarshal
->ref
= 0;
1491 pStdMarshal
->dest_context
= dest_context
;
1492 pStdMarshal
->dest_context_data
= dest_context_data
;
1494 hr
= IMarshal_QueryInterface(&pStdMarshal
->IMarshal_iface
, riid
, ppvObject
);
1496 HeapFree(GetProcessHeap(), 0, pStdMarshal
);
1501 /***********************************************************************
1502 * CoGetStandardMarshal [OLE32.@]
1504 * Gets or creates a standard marshal object.
1507 * riid [I] Interface identifier of the pUnk object.
1508 * pUnk [I] Optional. Object to get the marshal object for.
1509 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1510 * pvDestContext [I] Reserved. Must be NULL.
1511 * mshlflags [I] Flags affecting the marshaling process.
1512 * ppMarshal [O] Address where marshal object will be stored.
1516 * Failure: HRESULT code.
1520 * The function retrieves the IMarshal object associated with an object if
1521 * that object is currently an active stub, otherwise a new marshal object is
1524 HRESULT WINAPI
CoGetStandardMarshal(REFIID riid
, IUnknown
*pUnk
,
1525 DWORD dwDestContext
, LPVOID pvDestContext
,
1526 DWORD mshlflags
, LPMARSHAL
*ppMarshal
)
1530 FIXME("(%s,NULL,%x,%p,%x,%p), unimplemented yet.\n",
1531 debugstr_guid(riid
),dwDestContext
,pvDestContext
,mshlflags
,ppMarshal
);
1534 TRACE("(%s,%p,%x,%p,%x,%p)\n",
1535 debugstr_guid(riid
),pUnk
,dwDestContext
,pvDestContext
,mshlflags
,ppMarshal
);
1537 return StdMarshalImpl_Construct(&IID_IMarshal
, dwDestContext
, pvDestContext
, (void**)ppMarshal
);
1540 /***********************************************************************
1541 * get_marshaler [internal]
1543 * Retrieves an IMarshal interface for an object.
1545 static HRESULT
get_marshaler(REFIID riid
, IUnknown
*pUnk
, DWORD dwDestContext
,
1546 void *pvDestContext
, DWORD mshlFlags
,
1547 LPMARSHAL
*pMarshal
)
1553 hr
= IUnknown_QueryInterface(pUnk
, &IID_IMarshal
, (LPVOID
*)pMarshal
);
1555 hr
= CoGetStandardMarshal(riid
, pUnk
, dwDestContext
, pvDestContext
,
1556 mshlFlags
, pMarshal
);
1560 /***********************************************************************
1561 * get_unmarshaler_from_stream [internal]
1563 * Creates an IMarshal* object according to the data marshaled to the stream.
1564 * The function leaves the stream pointer at the start of the data written
1565 * to the stream by the IMarshal* object.
1567 static HRESULT
get_unmarshaler_from_stream(IStream
*stream
, IMarshal
**marshal
, IID
*iid
)
1573 /* read common OBJREF header */
1574 hr
= IStream_Read(stream
, &objref
, FIELD_OFFSET(OBJREF
, u_objref
), &res
);
1575 if (hr
!= S_OK
|| (res
!= FIELD_OFFSET(OBJREF
, u_objref
)))
1577 ERR("Failed to read common OBJREF header, 0x%08x\n", hr
);
1578 return STG_E_READFAULT
;
1581 /* sanity check on header */
1582 if (objref
.signature
!= OBJREF_SIGNATURE
)
1584 ERR("Bad OBJREF signature 0x%08x\n", objref
.signature
);
1585 return RPC_E_INVALID_OBJREF
;
1588 if (iid
) *iid
= objref
.iid
;
1590 /* FIXME: handler marshaling */
1591 if (objref
.flags
& OBJREF_STANDARD
)
1593 TRACE("Using standard unmarshaling\n");
1594 hr
= StdMarshalImpl_Construct(&IID_IMarshal
, 0, NULL
, (LPVOID
*)marshal
);
1596 else if (objref
.flags
& OBJREF_CUSTOM
)
1598 ULONG custom_header_size
= FIELD_OFFSET(OBJREF
, u_objref
.u_custom
.pData
) -
1599 FIELD_OFFSET(OBJREF
, u_objref
.u_custom
);
1600 TRACE("Using custom unmarshaling\n");
1601 /* read constant sized OR_CUSTOM data from stream */
1602 hr
= IStream_Read(stream
, &objref
.u_objref
.u_custom
,
1603 custom_header_size
, &res
);
1604 if (hr
!= S_OK
|| (res
!= custom_header_size
))
1606 ERR("Failed to read OR_CUSTOM header, 0x%08x\n", hr
);
1607 return STG_E_READFAULT
;
1609 /* now create the marshaler specified in the stream */
1610 hr
= CoCreateInstance(&objref
.u_objref
.u_custom
.clsid
, NULL
,
1611 CLSCTX_INPROC_SERVER
, &IID_IMarshal
,
1616 FIXME("Invalid or unimplemented marshaling type specified: %x\n",
1618 return RPC_E_INVALID_OBJREF
;
1622 ERR("Failed to create marshal, 0x%08x\n", hr
);
1627 /***********************************************************************
1628 * CoGetMarshalSizeMax [OLE32.@]
1630 * Gets the maximum amount of data that will be needed by a marshal.
1633 * pulSize [O] Address where maximum marshal size will be stored.
1634 * riid [I] Identifier of the interface to marshal.
1635 * pUnk [I] Pointer to the object to marshal.
1636 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1637 * pvDestContext [I] Reserved. Must be NULL.
1638 * mshlFlags [I] Flags that affect the marshaling. See CoMarshalInterface().
1642 * Failure: HRESULT code.
1645 * CoMarshalInterface().
1647 HRESULT WINAPI
CoGetMarshalSizeMax(ULONG
*pulSize
, REFIID riid
, IUnknown
*pUnk
,
1648 DWORD dwDestContext
, void *pvDestContext
,
1653 CLSID marshaler_clsid
;
1655 hr
= get_marshaler(riid
, pUnk
, dwDestContext
, pvDestContext
, mshlFlags
, &pMarshal
);
1659 hr
= IMarshal_GetUnmarshalClass(pMarshal
, riid
, pUnk
, dwDestContext
,
1660 pvDestContext
, mshlFlags
, &marshaler_clsid
);
1663 ERR("IMarshal::GetUnmarshalClass failed, 0x%08x\n", hr
);
1664 IMarshal_Release(pMarshal
);
1668 hr
= IMarshal_GetMarshalSizeMax(pMarshal
, riid
, pUnk
, dwDestContext
,
1669 pvDestContext
, mshlFlags
, pulSize
);
1670 if (IsEqualCLSID(&marshaler_clsid
, &CLSID_DfMarshal
))
1671 /* add on the size of the common header */
1672 *pulSize
+= FIELD_OFFSET(OBJREF
, u_objref
);
1674 /* custom marshaling: add on the size of the whole OBJREF structure
1675 * like native does */
1676 *pulSize
+= sizeof(OBJREF
);
1678 IMarshal_Release(pMarshal
);
1683 static void dump_MSHLFLAGS(MSHLFLAGS flags
)
1685 if (flags
& MSHLFLAGS_TABLESTRONG
)
1686 TRACE(" MSHLFLAGS_TABLESTRONG");
1687 if (flags
& MSHLFLAGS_TABLEWEAK
)
1688 TRACE(" MSHLFLAGS_TABLEWEAK");
1689 if (!(flags
& (MSHLFLAGS_TABLESTRONG
|MSHLFLAGS_TABLEWEAK
)))
1690 TRACE(" MSHLFLAGS_NORMAL");
1691 if (flags
& MSHLFLAGS_NOPING
)
1692 TRACE(" MSHLFLAGS_NOPING");
1695 /***********************************************************************
1696 * CoMarshalInterface [OLE32.@]
1698 * Marshals an interface into a stream so that the object can then be
1699 * unmarshaled from another COM apartment and used remotely.
1702 * pStream [I] Stream the object will be marshaled into.
1703 * riid [I] Identifier of the interface to marshal.
1704 * pUnk [I] Pointer to the object to marshal.
1705 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1706 * pvDestContext [I] Reserved. Must be NULL.
1707 * mshlFlags [I] Flags that affect the marshaling. See notes.
1711 * Failure: HRESULT code.
1715 * The mshlFlags parameter can take one or more of the following flags:
1716 *| MSHLFLAGS_NORMAL - Unmarshal once, releases stub on last proxy release.
1717 *| MSHLFLAGS_TABLESTRONG - Unmarshal many, release when CoReleaseMarshalData() called.
1718 *| MSHLFLAGS_TABLEWEAK - Unmarshal many, releases stub on last proxy release.
1719 *| MSHLFLAGS_NOPING - No automatic garbage collection (and so reduces network traffic).
1721 * If a marshaled object is not unmarshaled, then CoReleaseMarshalData() must
1722 * be called in order to release the resources used in the marshaling.
1725 * CoUnmarshalInterface(), CoReleaseMarshalData().
1727 HRESULT WINAPI
CoMarshalInterface(IStream
*pStream
, REFIID riid
, IUnknown
*pUnk
,
1728 DWORD dwDestContext
, void *pvDestContext
,
1732 CLSID marshaler_clsid
;
1736 TRACE("(%p, %s, %p, %x, %p,", pStream
, debugstr_guid(riid
), pUnk
,
1737 dwDestContext
, pvDestContext
);
1738 dump_MSHLFLAGS(mshlFlags
);
1741 if (!pUnk
|| !pStream
)
1742 return E_INVALIDARG
;
1744 objref
.signature
= OBJREF_SIGNATURE
;
1747 /* get the marshaler for the specified interface */
1748 hr
= get_marshaler(riid
, pUnk
, dwDestContext
, pvDestContext
, mshlFlags
, &pMarshal
);
1751 ERR("Failed to get marshaller, 0x%08x\n", hr
);
1755 hr
= IMarshal_GetUnmarshalClass(pMarshal
, riid
, pUnk
, dwDestContext
,
1756 pvDestContext
, mshlFlags
, &marshaler_clsid
);
1759 ERR("IMarshal::GetUnmarshalClass failed, 0x%08x\n", hr
);
1763 /* FIXME: implement handler marshaling too */
1764 if (IsEqualCLSID(&marshaler_clsid
, &CLSID_DfMarshal
))
1766 TRACE("Using standard marshaling\n");
1767 objref
.flags
= OBJREF_STANDARD
;
1769 /* write the common OBJREF header to the stream */
1770 hr
= IStream_Write(pStream
, &objref
, FIELD_OFFSET(OBJREF
, u_objref
), NULL
);
1773 ERR("Failed to write OBJREF header to stream, 0x%08x\n", hr
);
1779 TRACE("Using custom marshaling\n");
1780 objref
.flags
= OBJREF_CUSTOM
;
1781 objref
.u_objref
.u_custom
.clsid
= marshaler_clsid
;
1782 objref
.u_objref
.u_custom
.cbExtension
= 0;
1783 objref
.u_objref
.u_custom
.size
= 0;
1784 hr
= IMarshal_GetMarshalSizeMax(pMarshal
, riid
, pUnk
, dwDestContext
,
1785 pvDestContext
, mshlFlags
,
1786 &objref
.u_objref
.u_custom
.size
);
1789 ERR("Failed to get max size of marshal data, error 0x%08x\n", hr
);
1792 /* write constant sized common header and OR_CUSTOM data into stream */
1793 hr
= IStream_Write(pStream
, &objref
,
1794 FIELD_OFFSET(OBJREF
, u_objref
.u_custom
.pData
), NULL
);
1797 ERR("Failed to write OR_CUSTOM header to stream with 0x%08x\n", hr
);
1802 TRACE("Calling IMarshal::MarshalInterace\n");
1803 /* call helper object to do the actual marshaling */
1804 hr
= IMarshal_MarshalInterface(pMarshal
, pStream
, riid
, pUnk
, dwDestContext
,
1805 pvDestContext
, mshlFlags
);
1809 ERR("Failed to marshal the interface %s, %x\n", debugstr_guid(riid
), hr
);
1814 IMarshal_Release(pMarshal
);
1816 TRACE("completed with hr 0x%08x\n", hr
);
1821 /***********************************************************************
1822 * CoUnmarshalInterface [OLE32.@]
1824 * Unmarshals an object from a stream by creating a proxy to the remote
1825 * object, if necessary.
1829 * pStream [I] Stream containing the marshaled object.
1830 * riid [I] Interface identifier of the object to create a proxy to.
1831 * ppv [O] Address where proxy will be stored.
1836 * Failure: HRESULT code.
1839 * CoMarshalInterface().
1841 HRESULT WINAPI
CoUnmarshalInterface(IStream
*pStream
, REFIID riid
, LPVOID
*ppv
)
1848 TRACE("(%p, %s, %p)\n", pStream
, debugstr_guid(riid
), ppv
);
1850 if (!pStream
|| !ppv
)
1851 return E_INVALIDARG
;
1853 hr
= get_unmarshaler_from_stream(pStream
, &pMarshal
, &iid
);
1857 /* call the helper object to do the actual unmarshaling */
1858 hr
= IMarshal_UnmarshalInterface(pMarshal
, pStream
, &iid
, (LPVOID
*)&object
);
1860 ERR("IMarshal::UnmarshalInterface failed, 0x%08x\n", hr
);
1864 /* IID_NULL means use the interface ID of the marshaled object */
1865 if (!IsEqualIID(riid
, &IID_NULL
) && !IsEqualIID(riid
, &iid
))
1867 TRACE("requested interface != marshalled interface, additional QI needed\n");
1868 hr
= IUnknown_QueryInterface(object
, riid
, ppv
);
1870 ERR("Couldn't query for interface %s, hr = 0x%08x\n",
1871 debugstr_guid(riid
), hr
);
1872 IUnknown_Release(object
);
1880 IMarshal_Release(pMarshal
);
1882 TRACE("completed with hr 0x%x\n", hr
);
1887 /***********************************************************************
1888 * CoReleaseMarshalData [OLE32.@]
1890 * Releases resources associated with an object that has been marshaled into
1895 * pStream [I] The stream that the object has been marshaled into.
1899 * Failure: HRESULT error code.
1903 * Call this function to release resources associated with a normal or
1904 * table-weak marshal that will not be unmarshaled, and all table-strong
1905 * marshals when they are no longer needed.
1908 * CoMarshalInterface(), CoUnmarshalInterface().
1910 HRESULT WINAPI
CoReleaseMarshalData(IStream
*pStream
)
1915 TRACE("(%p)\n", pStream
);
1917 hr
= get_unmarshaler_from_stream(pStream
, &pMarshal
, NULL
);
1921 /* call the helper object to do the releasing of marshal data */
1922 hr
= IMarshal_ReleaseMarshalData(pMarshal
, pStream
);
1924 ERR("IMarshal::ReleaseMarshalData failed with error 0x%08x\n", hr
);
1926 IMarshal_Release(pMarshal
);
1931 /***********************************************************************
1932 * CoMarshalInterThreadInterfaceInStream [OLE32.@]
1934 * Marshal an interface across threads in the same process.
1937 * riid [I] Identifier of the interface to be marshalled.
1938 * pUnk [I] Pointer to IUnknown-derived interface that will be marshalled.
1939 * ppStm [O] Pointer to IStream object that is created and then used to store the marshalled interface.
1943 * Failure: E_OUTOFMEMORY and other COM error codes
1946 * CoMarshalInterface(), CoUnmarshalInterface() and CoGetInterfaceAndReleaseStream()
1948 HRESULT WINAPI
CoMarshalInterThreadInterfaceInStream(
1949 REFIID riid
, LPUNKNOWN pUnk
, LPSTREAM
* ppStm
)
1951 ULARGE_INTEGER xpos
;
1952 LARGE_INTEGER seekto
;
1955 TRACE("(%s, %p, %p)\n",debugstr_guid(riid
), pUnk
, ppStm
);
1957 hres
= CreateStreamOnHGlobal(NULL
, TRUE
, ppStm
);
1958 if (FAILED(hres
)) return hres
;
1959 hres
= CoMarshalInterface(*ppStm
, riid
, pUnk
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_NORMAL
);
1961 if (SUCCEEDED(hres
))
1963 memset(&seekto
, 0, sizeof(seekto
));
1964 IStream_Seek(*ppStm
, seekto
, STREAM_SEEK_SET
, &xpos
);
1968 IStream_Release(*ppStm
);
1975 /***********************************************************************
1976 * CoGetInterfaceAndReleaseStream [OLE32.@]
1978 * Unmarshalls an interface from a stream and then releases the stream.
1981 * pStm [I] Stream that contains the marshalled interface.
1982 * riid [I] Interface identifier of the object to unmarshall.
1983 * ppv [O] Address of pointer where the requested interface object will be stored.
1987 * Failure: A COM error code
1990 * CoMarshalInterThreadInterfaceInStream() and CoUnmarshalInterface()
1992 HRESULT WINAPI
CoGetInterfaceAndReleaseStream(LPSTREAM pStm
, REFIID riid
,
1997 TRACE("(%p, %s, %p)\n", pStm
, debugstr_guid(riid
), ppv
);
1999 if(!pStm
) return E_INVALIDARG
;
2000 hres
= CoUnmarshalInterface(pStm
, riid
, ppv
);
2001 IStream_Release(pStm
);
2005 static HRESULT WINAPI
StdMarshalCF_QueryInterface(LPCLASSFACTORY iface
,
2006 REFIID riid
, LPVOID
*ppv
)
2009 if (IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IClassFactory
))
2014 return E_NOINTERFACE
;
2017 static ULONG WINAPI
StdMarshalCF_AddRef(LPCLASSFACTORY iface
)
2019 return 2; /* non-heap based object */
2022 static ULONG WINAPI
StdMarshalCF_Release(LPCLASSFACTORY iface
)
2024 return 1; /* non-heap based object */
2027 static HRESULT WINAPI
StdMarshalCF_CreateInstance(LPCLASSFACTORY iface
,
2028 LPUNKNOWN pUnk
, REFIID riid
, LPVOID
*ppv
)
2030 if (IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IMarshal
))
2031 return StdMarshalImpl_Construct(riid
, 0, NULL
, ppv
);
2033 FIXME("(%s), not supported.\n",debugstr_guid(riid
));
2034 return E_NOINTERFACE
;
2037 static HRESULT WINAPI
StdMarshalCF_LockServer(LPCLASSFACTORY iface
, BOOL fLock
)
2039 FIXME("(%d), stub!\n",fLock
);
2043 static const IClassFactoryVtbl StdMarshalCFVtbl
=
2045 StdMarshalCF_QueryInterface
,
2046 StdMarshalCF_AddRef
,
2047 StdMarshalCF_Release
,
2048 StdMarshalCF_CreateInstance
,
2049 StdMarshalCF_LockServer
2051 static const IClassFactoryVtbl
*StdMarshalCF
= &StdMarshalCFVtbl
;
2053 HRESULT
MARSHAL_GetStandardMarshalCF(LPVOID
*ppv
)
2055 *ppv
= &StdMarshalCF
;
2059 /***********************************************************************
2060 * CoMarshalHresult [OLE32.@]
2062 * Marshals an HRESULT value into a stream.
2065 * pStm [I] Stream that hresult will be marshalled into.
2066 * hresult [I] HRESULT to be marshalled.
2070 * Failure: A COM error code
2073 * CoUnmarshalHresult().
2075 HRESULT WINAPI
CoMarshalHresult(LPSTREAM pStm
, HRESULT hresult
)
2077 return IStream_Write(pStm
, &hresult
, sizeof(hresult
), NULL
);
2080 /***********************************************************************
2081 * CoUnmarshalHresult [OLE32.@]
2083 * Unmarshals an HRESULT value from a stream.
2086 * pStm [I] Stream that hresult will be unmarshalled from.
2087 * phresult [I] Pointer to HRESULT where the value will be unmarshalled to.
2091 * Failure: A COM error code
2094 * CoMarshalHresult().
2096 HRESULT WINAPI
CoUnmarshalHresult(LPSTREAM pStm
, HRESULT
* phresult
)
2098 return IStream_Read(pStm
, phresult
, sizeof(*phresult
), NULL
);