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 static HRESULT
unmarshal_object(const STDOBJREF
*stdobjref
, APARTMENT
*apt
,
56 MSHCTX dest_context
, void *dest_context_data
,
57 REFIID riid
, const OXID_INFO
*oxid_info
,
60 /* Marshalling just passes a unique identifier to the remote client,
61 * that makes it possible to find the passed interface again.
63 * So basically we need a set of values that make it unique.
65 * Note that the IUnknown_QI(ob,xiid,&ppv) always returns the SAME ppv value!
67 * A triple is used: OXID (apt id), OID (stub manager id),
68 * IPID (interface ptr/stub id).
70 * OXIDs identify an apartment and are network scoped
71 * OIDs identify a stub manager and are apartment scoped
72 * IPIDs identify an interface stub and are apartment scoped
75 static inline HRESULT
get_facbuf_for_iid(REFIID riid
, IPSFactoryBuffer
**facbuf
)
80 if ((hr
= CoGetPSClsid(riid
, &clsid
)))
82 return CoGetClassObject(&clsid
, CLSCTX_INPROC_SERVER
| WINE_CLSCTX_DONT_HOST
,
83 NULL
, &IID_IPSFactoryBuffer
, (LPVOID
*)facbuf
);
86 /* marshals an object into a STDOBJREF structure */
87 HRESULT
marshal_object(APARTMENT
*apt
, STDOBJREF
*stdobjref
, REFIID riid
, IUnknown
*object
, MSHLFLAGS mshlflags
)
89 struct stub_manager
*manager
;
90 struct ifstub
*ifstub
;
92 IRpcStubBuffer
*stub
= NULL
;
94 IUnknown
*iobject
= NULL
; /* object of type riid */
96 hr
= apartment_getoxid(apt
, &stdobjref
->oxid
);
100 hr
= apartment_createwindowifneeded(apt
);
104 hr
= IUnknown_QueryInterface(object
, riid
, (void **)&iobject
);
107 ERR("object doesn't expose interface %s, failing with error 0x%08x\n",
108 debugstr_guid(riid
), hr
);
109 return E_NOINTERFACE
;
112 /* IUnknown doesn't require a stub buffer, because it never goes out on
114 if (!IsEqualIID(riid
, &IID_IUnknown
))
116 IPSFactoryBuffer
*psfb
;
118 hr
= get_facbuf_for_iid(riid
, &psfb
);
121 ERR("couldn't get IPSFactory buffer for interface %s\n", debugstr_guid(riid
));
122 IUnknown_Release(iobject
);
126 hr
= IPSFactoryBuffer_CreateStub(psfb
, riid
, iobject
, &stub
);
127 IPSFactoryBuffer_Release(psfb
);
130 ERR("Failed to create an IRpcStubBuffer from IPSFactory for %s with error 0x%08x\n",
131 debugstr_guid(riid
), hr
);
132 IUnknown_Release(iobject
);
137 stdobjref
->flags
= SORF_NULL
;
138 if (mshlflags
& MSHLFLAGS_TABLEWEAK
)
139 stdobjref
->flags
|= SORFP_TABLEWEAK
;
140 if (mshlflags
& MSHLFLAGS_NOPING
)
141 stdobjref
->flags
|= SORF_NOPING
;
143 if ((manager
= get_stub_manager_from_object(apt
, object
)))
144 TRACE("registering new ifstub on pre-existing manager\n");
147 TRACE("constructing new stub manager\n");
149 manager
= new_stub_manager(apt
, object
);
152 if (stub
) IRpcStubBuffer_Release(stub
);
153 IUnknown_Release(iobject
);
154 return E_OUTOFMEMORY
;
157 stdobjref
->oid
= manager
->oid
;
159 tablemarshal
= ((mshlflags
& MSHLFLAGS_TABLESTRONG
) || (mshlflags
& MSHLFLAGS_TABLEWEAK
));
161 /* make sure ifstub that we are creating is unique */
162 ifstub
= stub_manager_find_ifstub(manager
, riid
, mshlflags
);
164 ifstub
= stub_manager_new_ifstub(manager
, stub
, iobject
, riid
, mshlflags
);
166 if (stub
) IRpcStubBuffer_Release(stub
);
167 IUnknown_Release(iobject
);
171 stub_manager_int_release(manager
);
172 /* destroy the stub manager if it has no ifstubs by releasing
173 * zero external references */
174 stub_manager_ext_release(manager
, 0, FALSE
, TRUE
);
175 return E_OUTOFMEMORY
;
180 stdobjref
->cPublicRefs
= NORMALEXTREFS
;
181 stub_manager_ext_addref(manager
, stdobjref
->cPublicRefs
, FALSE
);
185 stdobjref
->cPublicRefs
= 0;
186 if (mshlflags
& MSHLFLAGS_TABLESTRONG
)
187 stub_manager_ext_addref(manager
, 1, FALSE
);
189 stub_manager_ext_addref(manager
, 0, TRUE
);
192 /* FIXME: check return value */
193 RPC_RegisterInterface(riid
);
195 stdobjref
->ipid
= ifstub
->ipid
;
197 stub_manager_int_release(manager
);
203 /* Client-side identity of the server object */
205 static HRESULT
proxy_manager_get_remunknown(struct proxy_manager
* This
, IRemUnknown
**remunk
);
206 static void proxy_manager_destroy(struct proxy_manager
* This
);
207 static HRESULT
proxy_manager_find_ifproxy(struct proxy_manager
* This
, REFIID riid
, struct ifproxy
** ifproxy_found
);
208 static HRESULT
proxy_manager_query_local_interface(struct proxy_manager
* This
, REFIID riid
, void ** ppv
);
210 static HRESULT WINAPI
ClientIdentity_QueryInterface(IMultiQI
* iface
, REFIID riid
, void ** ppv
)
215 TRACE("%s\n", debugstr_guid(riid
));
218 hr
= IMultiQI_QueryMultipleInterfaces(iface
, 1, &mqi
);
224 static ULONG WINAPI
ClientIdentity_AddRef(IMultiQI
* iface
)
226 struct proxy_manager
* This
= (struct proxy_manager
*)iface
;
227 TRACE("%p - before %d\n", iface
, This
->refs
);
228 return InterlockedIncrement(&This
->refs
);
231 static ULONG WINAPI
ClientIdentity_Release(IMultiQI
* iface
)
233 struct proxy_manager
* This
= (struct proxy_manager
*)iface
;
234 ULONG refs
= InterlockedDecrement(&This
->refs
);
235 TRACE("%p - after %d\n", iface
, refs
);
237 proxy_manager_destroy(This
);
241 static HRESULT WINAPI
ClientIdentity_QueryMultipleInterfaces(IMultiQI
*iface
, ULONG cMQIs
, MULTI_QI
*pMQIs
)
243 struct proxy_manager
* This
= (struct proxy_manager
*)iface
;
244 REMQIRESULT
*qiresults
= NULL
;
245 ULONG nonlocal_mqis
= 0;
247 ULONG successful_mqis
= 0;
248 IID
*iids
= HeapAlloc(GetProcessHeap(), 0, cMQIs
* sizeof(*iids
));
249 /* mapping of RemQueryInterface index to QueryMultipleInterfaces index */
250 ULONG
*mapping
= HeapAlloc(GetProcessHeap(), 0, cMQIs
* sizeof(*mapping
));
252 TRACE("cMQIs: %d\n", cMQIs
);
254 /* try to get a local interface - this includes already active proxy
255 * interfaces and also interfaces exposed by the proxy manager */
256 for (i
= 0; i
< cMQIs
; i
++)
258 TRACE("iid[%d] = %s\n", i
, debugstr_guid(pMQIs
[i
].pIID
));
259 pMQIs
[i
].hr
= proxy_manager_query_local_interface(This
, pMQIs
[i
].pIID
, (void **)&pMQIs
[i
].pItf
);
260 if (pMQIs
[i
].hr
== S_OK
)
264 iids
[nonlocal_mqis
] = *pMQIs
[i
].pIID
;
265 mapping
[nonlocal_mqis
] = i
;
270 TRACE("%d interfaces not found locally\n", nonlocal_mqis
);
272 /* if we have more than one interface not found locally then we must try
273 * to query the remote object for it */
274 if (nonlocal_mqis
!= 0)
280 /* get the ipid of the first entry */
281 /* FIXME: should we implement ClientIdentity on the ifproxies instead
282 * of the proxy_manager so we use the correct ipid here? */
283 ipid
= &LIST_ENTRY(list_head(&This
->interfaces
), struct ifproxy
, entry
)->stdobjref
.ipid
;
285 /* get IRemUnknown proxy so we can communicate with the remote object */
286 hr
= proxy_manager_get_remunknown(This
, &remunk
);
290 hr
= IRemUnknown_RemQueryInterface(remunk
, ipid
, NORMALEXTREFS
,
291 nonlocal_mqis
, iids
, &qiresults
);
292 IRemUnknown_Release(remunk
);
294 ERR("IRemUnknown_RemQueryInterface failed with error 0x%08x\n", hr
);
297 /* IRemUnknown_RemQueryInterface can return S_FALSE if only some of
298 * the interfaces were returned */
301 /* try to unmarshal each object returned to us */
302 for (i
= 0; i
< nonlocal_mqis
; i
++)
304 ULONG index
= mapping
[i
];
305 HRESULT hrobj
= qiresults
[i
].hResult
;
307 hrobj
= unmarshal_object(&qiresults
[i
].std
, COM_CurrentApt(),
309 This
->dest_context_data
,
310 pMQIs
[index
].pIID
, &This
->oxid_info
,
311 (void **)&pMQIs
[index
].pItf
);
316 ERR("Failed to get pointer to interface %s\n", debugstr_guid(pMQIs
[index
].pIID
));
317 pMQIs
[index
].hr
= hrobj
;
321 /* free the memory allocated by the proxy */
322 CoTaskMemFree(qiresults
);
325 TRACE("%d/%d successfully queried\n", successful_mqis
, cMQIs
);
327 HeapFree(GetProcessHeap(), 0, iids
);
328 HeapFree(GetProcessHeap(), 0, mapping
);
330 if (successful_mqis
== cMQIs
)
331 return S_OK
; /* we got all requested interfaces */
332 else if (successful_mqis
== 0)
333 return E_NOINTERFACE
; /* we didn't get any interfaces */
335 return S_FALSE
; /* we got some interfaces */
338 static const IMultiQIVtbl ClientIdentity_Vtbl
=
340 ClientIdentity_QueryInterface
,
341 ClientIdentity_AddRef
,
342 ClientIdentity_Release
,
343 ClientIdentity_QueryMultipleInterfaces
346 /* FIXME: remove these */
347 static HRESULT WINAPI
StdMarshalImpl_GetUnmarshalClass(LPMARSHAL iface
, REFIID riid
, void* pv
, DWORD dwDestContext
, void* pvDestContext
, DWORD mshlflags
, CLSID
* pCid
);
348 static HRESULT WINAPI
StdMarshalImpl_GetMarshalSizeMax(LPMARSHAL iface
, REFIID riid
, void* pv
, DWORD dwDestContext
, void* pvDestContext
, DWORD mshlflags
, DWORD
* pSize
);
349 static HRESULT WINAPI
StdMarshalImpl_UnmarshalInterface(LPMARSHAL iface
, IStream
*pStm
, REFIID riid
, void **ppv
);
350 static HRESULT WINAPI
StdMarshalImpl_ReleaseMarshalData(LPMARSHAL iface
, IStream
*pStm
);
351 static HRESULT WINAPI
StdMarshalImpl_DisconnectObject(LPMARSHAL iface
, DWORD dwReserved
);
353 static HRESULT WINAPI
Proxy_QueryInterface(IMarshal
*iface
, REFIID riid
, void **ppvObject
)
355 ICOM_THIS_MULTI(struct proxy_manager
, lpVtblMarshal
, iface
);
356 return IMultiQI_QueryInterface((IMultiQI
*)&This
->lpVtbl
, riid
, ppvObject
);
359 static ULONG WINAPI
Proxy_AddRef(IMarshal
*iface
)
361 ICOM_THIS_MULTI(struct proxy_manager
, lpVtblMarshal
, iface
);
362 return IMultiQI_AddRef((IMultiQI
*)&This
->lpVtbl
);
365 static ULONG WINAPI
Proxy_Release(IMarshal
*iface
)
367 ICOM_THIS_MULTI(struct proxy_manager
, lpVtblMarshal
, iface
);
368 return IMultiQI_Release((IMultiQI
*)&This
->lpVtbl
);
371 static HRESULT WINAPI
Proxy_MarshalInterface(
372 LPMARSHAL iface
, IStream
*pStm
, REFIID riid
, void* pv
, DWORD dwDestContext
,
373 void* pvDestContext
, DWORD mshlflags
)
375 ICOM_THIS_MULTI(struct proxy_manager
, lpVtblMarshal
, iface
);
377 struct ifproxy
*ifproxy
;
379 TRACE("(...,%s,...)\n", debugstr_guid(riid
));
381 hr
= proxy_manager_find_ifproxy(This
, riid
, &ifproxy
);
384 STDOBJREF stdobjref
= ifproxy
->stdobjref
;
386 stdobjref
.cPublicRefs
= 0;
388 if ((mshlflags
!= MSHLFLAGS_TABLEWEAK
) &&
389 (mshlflags
!= MSHLFLAGS_TABLESTRONG
))
391 ULONG cPublicRefs
= ifproxy
->refs
;
392 ULONG cPublicRefsOld
;
393 /* optimization - share out proxy's public references if possible
394 * instead of making new proxy do a roundtrip through the server */
397 ULONG cPublicRefsNew
;
398 cPublicRefsOld
= cPublicRefs
;
399 stdobjref
.cPublicRefs
= cPublicRefs
/ 2;
400 cPublicRefsNew
= cPublicRefs
- stdobjref
.cPublicRefs
;
401 cPublicRefs
= InterlockedCompareExchange(
402 (LONG
*)&ifproxy
->refs
, cPublicRefsNew
, cPublicRefsOld
);
403 } while (cPublicRefs
!= cPublicRefsOld
);
406 /* normal and table-strong marshaling need at least one reference */
407 if (!stdobjref
.cPublicRefs
&& (mshlflags
!= MSHLFLAGS_TABLEWEAK
))
410 hr
= proxy_manager_get_remunknown(This
, &remunk
);
413 HRESULT hrref
= S_OK
;
415 rif
.ipid
= ifproxy
->stdobjref
.ipid
;
416 rif
.cPublicRefs
= (mshlflags
== MSHLFLAGS_TABLESTRONG
) ? 1 : NORMALEXTREFS
;
417 rif
.cPrivateRefs
= 0;
418 hr
= IRemUnknown_RemAddRef(remunk
, 1, &rif
, &hrref
);
419 IRemUnknown_Release(remunk
);
420 if (hr
== S_OK
&& hrref
== S_OK
)
422 /* table-strong marshaling doesn't give the refs to the
423 * client that unmarshals the STDOBJREF */
424 if (mshlflags
!= MSHLFLAGS_TABLESTRONG
)
425 stdobjref
.cPublicRefs
= rif
.cPublicRefs
;
428 ERR("IRemUnknown_RemAddRef returned with 0x%08x, hrref = 0x%08x\n", hr
, hrref
);
434 TRACE("writing stdobjref: flags = %04x cPublicRefs = %d oxid = %s oid = %s ipid = %s\n",
435 stdobjref
.flags
, stdobjref
.cPublicRefs
,
436 wine_dbgstr_longlong(stdobjref
.oxid
),
437 wine_dbgstr_longlong(stdobjref
.oid
),
438 debugstr_guid(&stdobjref
.ipid
));
439 hr
= IStream_Write(pStm
, &stdobjref
, sizeof(stdobjref
), NULL
);
444 /* we don't have the interface already unmarshaled so we have to
445 * request the object from the server */
448 REMQIRESULT
*qiresults
= NULL
;
451 /* get the ipid of the first entry */
452 /* FIXME: should we implement ClientIdentity on the ifproxies instead
453 * of the proxy_manager so we use the correct ipid here? */
454 ipid
= &LIST_ENTRY(list_head(&This
->interfaces
), struct ifproxy
, entry
)->stdobjref
.ipid
;
456 /* get IRemUnknown proxy so we can communicate with the remote object */
457 hr
= proxy_manager_get_remunknown(This
, &remunk
);
461 hr
= IRemUnknown_RemQueryInterface(remunk
, ipid
, NORMALEXTREFS
,
462 1, &iid
, &qiresults
);
465 hr
= IStream_Write(pStm
, &qiresults
->std
, sizeof(qiresults
->std
), NULL
);
469 rif
.ipid
= qiresults
->std
.ipid
;
470 rif
.cPublicRefs
= qiresults
->std
.cPublicRefs
;
471 rif
.cPrivateRefs
= 0;
472 IRemUnknown_RemRelease(remunk
, 1, &rif
);
474 CoTaskMemFree(qiresults
);
477 ERR("IRemUnknown_RemQueryInterface failed with error 0x%08x\n", hr
);
478 IRemUnknown_Release(remunk
);
485 static const IMarshalVtbl ProxyMarshal_Vtbl
=
487 Proxy_QueryInterface
,
490 StdMarshalImpl_GetUnmarshalClass
,
491 StdMarshalImpl_GetMarshalSizeMax
,
492 Proxy_MarshalInterface
,
493 StdMarshalImpl_UnmarshalInterface
,
494 StdMarshalImpl_ReleaseMarshalData
,
495 StdMarshalImpl_DisconnectObject
498 static HRESULT WINAPI
ProxyCliSec_QueryInterface(IClientSecurity
*iface
, REFIID riid
, void **ppvObject
)
500 ICOM_THIS_MULTI(struct proxy_manager
, lpVtblCliSec
, iface
);
501 return IMultiQI_QueryInterface((IMultiQI
*)&This
->lpVtbl
, riid
, ppvObject
);
504 static ULONG WINAPI
ProxyCliSec_AddRef(IClientSecurity
*iface
)
506 ICOM_THIS_MULTI(struct proxy_manager
, lpVtblCliSec
, iface
);
507 return IMultiQI_AddRef((IMultiQI
*)&This
->lpVtbl
);
510 static ULONG WINAPI
ProxyCliSec_Release(IClientSecurity
*iface
)
512 ICOM_THIS_MULTI(struct proxy_manager
, lpVtblCliSec
, iface
);
513 return IMultiQI_Release((IMultiQI
*)&This
->lpVtbl
);
516 static HRESULT WINAPI
ProxyCliSec_QueryBlanket(IClientSecurity
*iface
,
520 OLECHAR
**ppServerPrincName
,
524 DWORD
*pCapabilities
)
526 FIXME("(%p, %p, %p, %p, %p, %p, %p, %p): stub\n", pProxy
, pAuthnSvc
,
527 pAuthzSvc
, ppServerPrincName
, pAuthnLevel
, pImpLevel
, pAuthInfo
,
534 if (ppServerPrincName
)
535 *ppServerPrincName
= NULL
;
537 *pAuthnLevel
= RPC_C_AUTHN_LEVEL_DEFAULT
;
539 *pImpLevel
= RPC_C_IMP_LEVEL_DEFAULT
;
543 *pCapabilities
= EOAC_NONE
;
548 static HRESULT WINAPI
ProxyCliSec_SetBlanket(IClientSecurity
*iface
,
549 IUnknown
*pProxy
, DWORD AuthnSvc
,
551 OLECHAR
*pServerPrincName
,
552 DWORD AuthnLevel
, DWORD ImpLevel
,
556 FIXME("(%p, %d, %d, %s, %d, %d, %p, 0x%x): stub\n", pProxy
, AuthnSvc
, AuthzSvc
,
557 pServerPrincName
== COLE_DEFAULT_PRINCIPAL
? "<default principal>" : debugstr_w(pServerPrincName
),
558 AuthnLevel
, ImpLevel
, pAuthInfo
, Capabilities
);
562 static HRESULT WINAPI
ProxyCliSec_CopyProxy(IClientSecurity
*iface
,
563 IUnknown
*pProxy
, IUnknown
**ppCopy
)
565 FIXME("(%p, %p): stub\n", pProxy
, ppCopy
);
570 static const IClientSecurityVtbl ProxyCliSec_Vtbl
=
572 ProxyCliSec_QueryInterface
,
575 ProxyCliSec_QueryBlanket
,
576 ProxyCliSec_SetBlanket
,
577 ProxyCliSec_CopyProxy
580 static HRESULT
ifproxy_get_public_ref(struct ifproxy
* This
)
584 if (WAIT_OBJECT_0
!= WaitForSingleObject(This
->parent
->remoting_mutex
, INFINITE
))
586 ERR("Wait failed for ifproxy %p\n", This
);
592 IRemUnknown
*remunk
= NULL
;
594 TRACE("getting public ref for ifproxy %p\n", This
);
596 hr
= proxy_manager_get_remunknown(This
->parent
, &remunk
);
599 HRESULT hrref
= S_OK
;
601 rif
.ipid
= This
->stdobjref
.ipid
;
602 rif
.cPublicRefs
= NORMALEXTREFS
;
603 rif
.cPrivateRefs
= 0;
604 hr
= IRemUnknown_RemAddRef(remunk
, 1, &rif
, &hrref
);
605 IRemUnknown_Release(remunk
);
606 if (hr
== S_OK
&& hrref
== S_OK
)
607 InterlockedExchangeAdd((LONG
*)&This
->refs
, NORMALEXTREFS
);
609 ERR("IRemUnknown_RemAddRef returned with 0x%08x, hrref = 0x%08x\n", hr
, hrref
);
612 ReleaseMutex(This
->parent
->remoting_mutex
);
617 static HRESULT
ifproxy_release_public_refs(struct ifproxy
* This
)
622 if (WAIT_OBJECT_0
!= WaitForSingleObject(This
->parent
->remoting_mutex
, INFINITE
))
624 ERR("Wait failed for ifproxy %p\n", This
);
628 public_refs
= This
->refs
;
631 IRemUnknown
*remunk
= NULL
;
633 TRACE("releasing %d refs\n", public_refs
);
635 hr
= proxy_manager_get_remunknown(This
->parent
, &remunk
);
639 rif
.ipid
= This
->stdobjref
.ipid
;
640 rif
.cPublicRefs
= public_refs
;
641 rif
.cPrivateRefs
= 0;
642 hr
= IRemUnknown_RemRelease(remunk
, 1, &rif
);
643 IRemUnknown_Release(remunk
);
645 InterlockedExchangeAdd((LONG
*)&This
->refs
, -public_refs
);
646 else if (hr
== RPC_E_DISCONNECTED
)
647 WARN("couldn't release references because object was "
648 "disconnected: oxid = %s, oid = %s\n",
649 wine_dbgstr_longlong(This
->parent
->oxid
),
650 wine_dbgstr_longlong(This
->parent
->oid
));
652 ERR("IRemUnknown_RemRelease failed with error 0x%08x\n", hr
);
655 ReleaseMutex(This
->parent
->remoting_mutex
);
660 /* should be called inside This->parent->cs critical section */
661 static void ifproxy_disconnect(struct ifproxy
* This
)
663 ifproxy_release_public_refs(This
);
664 if (This
->proxy
) IRpcProxyBuffer_Disconnect(This
->proxy
);
666 IRpcChannelBuffer_Release(This
->chan
);
670 /* should be called in This->parent->cs critical section if it is an entry in parent's list */
671 static void ifproxy_destroy(struct ifproxy
* This
)
675 /* release public references to this object so that the stub can know
676 * when to destroy itself */
677 ifproxy_release_public_refs(This
);
679 list_remove(&This
->entry
);
683 IRpcChannelBuffer_Release(This
->chan
);
687 if (This
->proxy
) IRpcProxyBuffer_Release(This
->proxy
);
689 HeapFree(GetProcessHeap(), 0, This
);
692 static HRESULT
proxy_manager_construct(
693 APARTMENT
* apt
, ULONG sorflags
, OXID oxid
, OID oid
,
694 const OXID_INFO
*oxid_info
, struct proxy_manager
** proxy_manager
)
696 struct proxy_manager
* This
= HeapAlloc(GetProcessHeap(), 0, sizeof(*This
));
697 if (!This
) return E_OUTOFMEMORY
;
699 This
->remoting_mutex
= CreateMutexW(NULL
, FALSE
, NULL
);
700 if (!This
->remoting_mutex
)
702 HeapFree(GetProcessHeap(), 0, This
);
703 return HRESULT_FROM_WIN32(GetLastError());
708 This
->oxid_info
.dwPid
= oxid_info
->dwPid
;
709 This
->oxid_info
.dwTid
= oxid_info
->dwTid
;
710 This
->oxid_info
.ipidRemUnknown
= oxid_info
->ipidRemUnknown
;
711 This
->oxid_info
.dwAuthnHint
= oxid_info
->dwAuthnHint
;
712 This
->oxid_info
.psa
= NULL
/* FIXME: copy from oxid_info */;
716 HRESULT hr
= RPC_ResolveOxid(oxid
, &This
->oxid_info
);
719 CloseHandle(This
->remoting_mutex
);
720 HeapFree(GetProcessHeap(), 0, This
);
725 This
->lpVtbl
= &ClientIdentity_Vtbl
;
726 This
->lpVtblMarshal
= &ProxyMarshal_Vtbl
;
727 This
->lpVtblCliSec
= &ProxyCliSec_Vtbl
;
729 list_init(&This
->entry
);
730 list_init(&This
->interfaces
);
732 InitializeCriticalSection(&This
->cs
);
733 DEBUG_SET_CRITSEC_NAME(&This
->cs
, "proxy_manager");
735 /* the apartment the object was unmarshaled into */
738 /* the source apartment and id of the object */
744 /* the DCOM draft specification states that the SORF_NOPING flag is
745 * proxy manager specific, not ifproxy specific, so this implies that we
746 * should store the STDOBJREF flags here in the proxy manager. */
747 This
->sorflags
= sorflags
;
749 /* we create the IRemUnknown proxy on demand */
752 /* initialise these values to the weakest values and they will be
753 * overwritten in proxy_manager_set_context */
754 This
->dest_context
= MSHCTX_INPROC
;
755 This
->dest_context_data
= NULL
;
757 EnterCriticalSection(&apt
->cs
);
758 /* FIXME: we are dependent on the ordering in here to make sure a proxy's
759 * IRemUnknown proxy doesn't get destroyed before the regual proxy does
760 * because we need the IRemUnknown proxy during the destruction of the
761 * regular proxy. Ideally, we should maintain a separate list for the
762 * IRemUnknown proxies that need late destruction */
763 list_add_tail(&apt
->proxies
, &This
->entry
);
764 LeaveCriticalSection(&apt
->cs
);
766 TRACE("%p created for OXID %s, OID %s\n", This
,
767 wine_dbgstr_longlong(oxid
), wine_dbgstr_longlong(oid
));
769 *proxy_manager
= This
;
773 static inline void proxy_manager_set_context(struct proxy_manager
*This
, MSHCTX dest_context
, void *dest_context_data
)
775 MSHCTX old_dest_context
= This
->dest_context
;
776 MSHCTX new_dest_context
;
780 new_dest_context
= old_dest_context
;
781 /* "stronger" values overwrite "weaker" values. stronger values are
782 * ones that disable more optimisations */
783 switch (old_dest_context
)
786 new_dest_context
= dest_context
;
788 case MSHCTX_CROSSCTX
:
789 switch (dest_context
)
794 new_dest_context
= dest_context
;
798 switch (dest_context
)
801 case MSHCTX_CROSSCTX
:
804 new_dest_context
= dest_context
;
807 case MSHCTX_NOSHAREDMEM
:
808 switch (dest_context
)
810 case MSHCTX_DIFFERENTMACHINE
:
811 new_dest_context
= dest_context
;
821 if (old_dest_context
== new_dest_context
) break;
823 old_dest_context
= InterlockedCompareExchange((PLONG
)&This
->dest_context
, new_dest_context
, old_dest_context
);
824 } while (new_dest_context
!= old_dest_context
);
826 if (dest_context_data
)
827 InterlockedExchangePointer(&This
->dest_context_data
, dest_context_data
);
830 static HRESULT
proxy_manager_query_local_interface(struct proxy_manager
* This
, REFIID riid
, void ** ppv
)
833 struct ifproxy
* ifproxy
;
835 TRACE("%s\n", debugstr_guid(riid
));
837 if (IsEqualIID(riid
, &IID_IUnknown
) ||
838 IsEqualIID(riid
, &IID_IMultiQI
))
840 *ppv
= &This
->lpVtbl
;
841 IUnknown_AddRef((IUnknown
*)*ppv
);
844 if (IsEqualIID(riid
, &IID_IMarshal
))
846 *ppv
= &This
->lpVtblMarshal
;
847 IUnknown_AddRef((IUnknown
*)*ppv
);
850 if (IsEqualIID(riid
, &IID_IClientSecurity
))
852 *ppv
= &This
->lpVtblCliSec
;
853 IUnknown_AddRef((IUnknown
*)*ppv
);
857 hr
= proxy_manager_find_ifproxy(This
, riid
, &ifproxy
);
860 *ppv
= ifproxy
->iface
;
861 IUnknown_AddRef((IUnknown
*)*ppv
);
866 return E_NOINTERFACE
;
869 static HRESULT
proxy_manager_create_ifproxy(
870 struct proxy_manager
* This
, const STDOBJREF
*stdobjref
, REFIID riid
,
871 IRpcChannelBuffer
* channel
, struct ifproxy
** iif_out
)
874 IPSFactoryBuffer
* psfb
;
875 struct ifproxy
* ifproxy
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ifproxy
));
876 if (!ifproxy
) return E_OUTOFMEMORY
;
878 list_init(&ifproxy
->entry
);
880 ifproxy
->parent
= This
;
881 ifproxy
->stdobjref
= *stdobjref
;
882 ifproxy
->iid
= *riid
;
884 ifproxy
->proxy
= NULL
;
887 ifproxy
->chan
= channel
; /* FIXME: we should take the binding strings and construct the channel in this function */
889 /* the IUnknown interface is special because it does not have a
890 * proxy associated with the ifproxy as we handle IUnknown ourselves */
891 if (IsEqualIID(riid
, &IID_IUnknown
))
893 ifproxy
->iface
= &This
->lpVtbl
;
894 IMultiQI_AddRef((IMultiQI
*)&This
->lpVtbl
);
899 hr
= get_facbuf_for_iid(riid
, &psfb
);
902 /* important note: the outer unknown is set to the proxy manager.
903 * This ensures the COM identity rules are not violated, by having a
904 * one-to-one mapping of objects on the proxy side to objects on the
905 * stub side, no matter which interface you view the object through */
906 hr
= IPSFactoryBuffer_CreateProxy(psfb
, (IUnknown
*)&This
->lpVtbl
, riid
,
907 &ifproxy
->proxy
, &ifproxy
->iface
);
908 IPSFactoryBuffer_Release(psfb
);
910 ERR("Could not create proxy for interface %s, error 0x%08x\n",
911 debugstr_guid(riid
), hr
);
914 ERR("Could not get IPSFactoryBuffer for interface %s, error 0x%08x\n",
915 debugstr_guid(riid
), hr
);
918 hr
= IRpcProxyBuffer_Connect(ifproxy
->proxy
, ifproxy
->chan
);
923 EnterCriticalSection(&This
->cs
);
924 list_add_tail(&This
->interfaces
, &ifproxy
->entry
);
925 LeaveCriticalSection(&This
->cs
);
928 TRACE("ifproxy %p created for IPID %s, interface %s with %u public refs\n",
929 ifproxy
, debugstr_guid(&stdobjref
->ipid
), debugstr_guid(riid
), stdobjref
->cPublicRefs
);
932 ifproxy_destroy(ifproxy
);
937 static HRESULT
proxy_manager_find_ifproxy(struct proxy_manager
* This
, REFIID riid
, struct ifproxy
** ifproxy_found
)
939 HRESULT hr
= E_NOINTERFACE
; /* assume not found */
940 struct list
* cursor
;
942 EnterCriticalSection(&This
->cs
);
943 LIST_FOR_EACH(cursor
, &This
->interfaces
)
945 struct ifproxy
* ifproxy
= LIST_ENTRY(cursor
, struct ifproxy
, entry
);
946 if (IsEqualIID(riid
, &ifproxy
->iid
))
948 *ifproxy_found
= ifproxy
;
953 LeaveCriticalSection(&This
->cs
);
958 static void proxy_manager_disconnect(struct proxy_manager
* This
)
960 struct list
* cursor
;
962 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This
->oxid
),
963 wine_dbgstr_longlong(This
->oid
));
965 EnterCriticalSection(&This
->cs
);
967 /* SORFP_NOLIFTIMEMGMT proxies (for IRemUnknown) shouldn't be
968 * disconnected - it won't do anything anyway, except cause
969 * problems for other objects that depend on this proxy always
971 if (!(This
->sorflags
& SORFP_NOLIFETIMEMGMT
))
973 LIST_FOR_EACH(cursor
, &This
->interfaces
)
975 struct ifproxy
* ifproxy
= LIST_ENTRY(cursor
, struct ifproxy
, entry
);
976 ifproxy_disconnect(ifproxy
);
980 /* apartment is being destroyed so don't keep a pointer around to it */
983 LeaveCriticalSection(&This
->cs
);
986 static HRESULT
proxy_manager_get_remunknown(struct proxy_manager
* This
, IRemUnknown
**remunk
)
989 struct apartment
*apt
;
990 BOOL called_in_original_apt
;
992 /* we don't want to try and unmarshal or use IRemUnknown if we don't want
993 * lifetime management */
994 if (This
->sorflags
& SORFP_NOLIFETIMEMGMT
)
997 apt
= COM_CurrentApt();
999 return CO_E_NOTINITIALIZED
;
1001 called_in_original_apt
= This
->parent
&& (This
->parent
->oxid
== apt
->oxid
);
1003 EnterCriticalSection(&This
->cs
);
1004 /* only return the cached object if called from the original apartment.
1005 * in future, we might want to make the IRemUnknown proxy callable from any
1006 * apartment to avoid these checks */
1007 if (This
->remunk
&& called_in_original_apt
)
1009 /* already created - return existing object */
1010 *remunk
= This
->remunk
;
1011 IRemUnknown_AddRef(*remunk
);
1013 else if (!This
->parent
)
1014 /* disconnected - we can't create IRemUnknown */
1018 STDOBJREF stdobjref
;
1019 /* Don't want IRemUnknown lifetime management as this is IRemUnknown!
1020 * We also don't care about whether or not the stub is still alive */
1021 stdobjref
.flags
= SORFP_NOLIFETIMEMGMT
| SORF_NOPING
;
1022 stdobjref
.cPublicRefs
= 1;
1023 /* oxid of destination object */
1024 stdobjref
.oxid
= This
->oxid
;
1025 /* FIXME: what should be used for the oid? The DCOM draft doesn't say */
1026 stdobjref
.oid
= (OID
)-1;
1027 stdobjref
.ipid
= This
->oxid_info
.ipidRemUnknown
;
1029 /* do the unmarshal */
1030 hr
= unmarshal_object(&stdobjref
, COM_CurrentApt(), This
->dest_context
,
1031 This
->dest_context_data
, &IID_IRemUnknown
,
1032 &This
->oxid_info
, (void**)remunk
);
1033 if (hr
== S_OK
&& called_in_original_apt
)
1035 This
->remunk
= *remunk
;
1036 IRemUnknown_AddRef(This
->remunk
);
1039 LeaveCriticalSection(&This
->cs
);
1041 TRACE("got IRemUnknown* pointer %p, hr = 0x%08x\n", *remunk
, hr
);
1046 /* destroys a proxy manager, freeing the memory it used.
1047 * Note: this function should not be called from a list iteration in the
1048 * apartment, due to the fact that it removes itself from the apartment and
1049 * it could add a proxy to IRemUnknown into the apartment. */
1050 static void proxy_manager_destroy(struct proxy_manager
* This
)
1052 struct list
* cursor
;
1054 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This
->oxid
),
1055 wine_dbgstr_longlong(This
->oid
));
1059 EnterCriticalSection(&This
->parent
->cs
);
1061 /* remove ourself from the list of proxy objects in the apartment */
1062 LIST_FOR_EACH(cursor
, &This
->parent
->proxies
)
1064 if (cursor
== &This
->entry
)
1066 list_remove(&This
->entry
);
1071 LeaveCriticalSection(&This
->parent
->cs
);
1074 /* destroy all of the interface proxies */
1075 while ((cursor
= list_head(&This
->interfaces
)))
1077 struct ifproxy
* ifproxy
= LIST_ENTRY(cursor
, struct ifproxy
, entry
);
1078 ifproxy_destroy(ifproxy
);
1081 if (This
->remunk
) IRemUnknown_Release(This
->remunk
);
1082 CoTaskMemFree(This
->oxid_info
.psa
);
1084 DEBUG_CLEAR_CRITSEC_NAME(&This
->cs
);
1085 DeleteCriticalSection(&This
->cs
);
1087 CloseHandle(This
->remoting_mutex
);
1089 HeapFree(GetProcessHeap(), 0, This
);
1092 /* finds the proxy manager corresponding to a given OXID and OID that has
1093 * been unmarshaled in the specified apartment. The caller must release the
1094 * reference to the proxy_manager when the object is no longer used. */
1095 static BOOL
find_proxy_manager(APARTMENT
* apt
, OXID oxid
, OID oid
, struct proxy_manager
** proxy_found
)
1098 struct list
* cursor
;
1100 EnterCriticalSection(&apt
->cs
);
1101 LIST_FOR_EACH(cursor
, &apt
->proxies
)
1103 struct proxy_manager
* proxy
= LIST_ENTRY(cursor
, struct proxy_manager
, entry
);
1104 if ((oxid
== proxy
->oxid
) && (oid
== proxy
->oid
))
1106 /* be careful of a race with ClientIdentity_Release, which would
1107 * cause us to return a proxy which is in the process of being
1109 if (ClientIdentity_AddRef((IMultiQI
*)&proxy
->lpVtbl
) != 0)
1111 *proxy_found
= proxy
;
1117 LeaveCriticalSection(&apt
->cs
);
1121 HRESULT
apartment_disconnectproxies(struct apartment
*apt
)
1123 struct list
* cursor
;
1125 LIST_FOR_EACH(cursor
, &apt
->proxies
)
1127 struct proxy_manager
* proxy
= LIST_ENTRY(cursor
, struct proxy_manager
, entry
);
1128 proxy_manager_disconnect(proxy
);
1134 /********************** StdMarshal implementation ****************************/
1135 typedef struct _StdMarshalImpl
1137 const IMarshalVtbl
*lpvtbl
;
1141 DWORD dwDestContext
;
1142 LPVOID pvDestContext
;
1146 static HRESULT WINAPI
1147 StdMarshalImpl_QueryInterface(LPMARSHAL iface
, REFIID riid
, LPVOID
*ppv
)
1150 if (IsEqualIID(&IID_IUnknown
, riid
) || IsEqualIID(&IID_IMarshal
, riid
))
1153 IUnknown_AddRef(iface
);
1156 FIXME("No interface for %s.\n", debugstr_guid(riid
));
1157 return E_NOINTERFACE
;
1161 StdMarshalImpl_AddRef(LPMARSHAL iface
)
1163 StdMarshalImpl
*This
= (StdMarshalImpl
*)iface
;
1164 return InterlockedIncrement(&This
->ref
);
1168 StdMarshalImpl_Release(LPMARSHAL iface
)
1170 StdMarshalImpl
*This
= (StdMarshalImpl
*)iface
;
1171 ULONG ref
= InterlockedDecrement(&This
->ref
);
1173 if (!ref
) HeapFree(GetProcessHeap(),0,This
);
1177 static HRESULT WINAPI
1178 StdMarshalImpl_GetUnmarshalClass(
1179 LPMARSHAL iface
, REFIID riid
, void* pv
, DWORD dwDestContext
,
1180 void* pvDestContext
, DWORD mshlflags
, CLSID
* pCid
)
1182 *pCid
= CLSID_DfMarshal
;
1186 static HRESULT WINAPI
1187 StdMarshalImpl_GetMarshalSizeMax(
1188 LPMARSHAL iface
, REFIID riid
, void* pv
, DWORD dwDestContext
,
1189 void* pvDestContext
, DWORD mshlflags
, DWORD
* pSize
)
1191 *pSize
= sizeof(STDOBJREF
);
1195 static HRESULT WINAPI
1196 StdMarshalImpl_MarshalInterface(
1197 LPMARSHAL iface
, IStream
*pStm
,REFIID riid
, void* pv
, DWORD dwDestContext
,
1198 void* pvDestContext
, DWORD mshlflags
)
1200 STDOBJREF stdobjref
;
1203 APARTMENT
*apt
= COM_CurrentApt();
1205 TRACE("(...,%s,...)\n", debugstr_guid(riid
));
1209 ERR("Apartment not initialized\n");
1210 return CO_E_NOTINITIALIZED
;
1213 /* make sure this apartment can be reached from other threads / processes */
1214 RPC_StartRemoting(apt
);
1216 hres
= marshal_object(apt
, &stdobjref
, riid
, pv
, mshlflags
);
1219 ERR("Failed to create ifstub, hres=0x%x\n", hres
);
1223 return IStream_Write(pStm
, &stdobjref
, sizeof(stdobjref
), &res
);
1226 /* helper for StdMarshalImpl_UnmarshalInterface - does the unmarshaling with
1227 * no questions asked about the rules surrounding same-apartment unmarshals
1228 * and table marshaling */
1229 static HRESULT
unmarshal_object(const STDOBJREF
*stdobjref
, APARTMENT
*apt
,
1230 MSHCTX dest_context
, void *dest_context_data
,
1231 REFIID riid
, const OXID_INFO
*oxid_info
,
1234 struct proxy_manager
*proxy_manager
= NULL
;
1239 TRACE("stdobjref: flags = %04x cPublicRefs = %d oxid = %s oid = %s ipid = %s\n",
1240 stdobjref
->flags
, stdobjref
->cPublicRefs
,
1241 wine_dbgstr_longlong(stdobjref
->oxid
),
1242 wine_dbgstr_longlong(stdobjref
->oid
),
1243 debugstr_guid(&stdobjref
->ipid
));
1245 /* create a new proxy manager if one doesn't already exist for the
1247 if (!find_proxy_manager(apt
, stdobjref
->oxid
, stdobjref
->oid
, &proxy_manager
))
1249 hr
= proxy_manager_construct(apt
, stdobjref
->flags
,
1250 stdobjref
->oxid
, stdobjref
->oid
, oxid_info
,
1254 TRACE("proxy manager already created, using\n");
1258 struct ifproxy
* ifproxy
;
1260 proxy_manager_set_context(proxy_manager
, dest_context
, dest_context_data
);
1262 hr
= proxy_manager_find_ifproxy(proxy_manager
, riid
, &ifproxy
);
1263 if (hr
== E_NOINTERFACE
)
1265 IRpcChannelBuffer
*chanbuf
;
1266 hr
= RPC_CreateClientChannel(&stdobjref
->oxid
, &stdobjref
->ipid
,
1267 &proxy_manager
->oxid_info
,
1268 proxy_manager
->dest_context
,
1269 proxy_manager
->dest_context_data
,
1272 hr
= proxy_manager_create_ifproxy(proxy_manager
, stdobjref
,
1273 riid
, chanbuf
, &ifproxy
);
1276 IUnknown_AddRef((IUnknown
*)ifproxy
->iface
);
1280 InterlockedExchangeAdd((LONG
*)&ifproxy
->refs
, stdobjref
->cPublicRefs
);
1281 /* get at least one external reference to the object to keep it alive */
1282 hr
= ifproxy_get_public_ref(ifproxy
);
1284 ifproxy_destroy(ifproxy
);
1288 *object
= ifproxy
->iface
;
1291 /* release our reference to the proxy manager - the client/apartment
1292 * will hold on to the remaining reference for us */
1293 if (proxy_manager
) ClientIdentity_Release((IMultiQI
*)&proxy_manager
->lpVtbl
);
1298 static HRESULT WINAPI
1299 StdMarshalImpl_UnmarshalInterface(LPMARSHAL iface
, IStream
*pStm
, REFIID riid
, void **ppv
)
1301 StdMarshalImpl
*This
= (StdMarshalImpl
*)iface
;
1302 struct stub_manager
*stubmgr
= NULL
;
1303 STDOBJREF stdobjref
;
1306 APARTMENT
*apt
= COM_CurrentApt();
1307 APARTMENT
*stub_apt
;
1310 TRACE("(...,%s,....)\n", debugstr_guid(riid
));
1312 /* we need an apartment to unmarshal into */
1315 ERR("Apartment not initialized\n");
1316 return CO_E_NOTINITIALIZED
;
1319 /* read STDOBJREF from wire */
1320 hres
= IStream_Read(pStm
, &stdobjref
, sizeof(stdobjref
), &res
);
1321 if (hres
!= S_OK
) return STG_E_READFAULT
;
1323 hres
= apartment_getoxid(apt
, &oxid
);
1324 if (hres
!= S_OK
) return hres
;
1326 /* check if we're marshalling back to ourselves */
1327 if ((oxid
== stdobjref
.oxid
) && (stubmgr
= get_stub_manager(apt
, stdobjref
.oid
)))
1329 TRACE("Unmarshalling object marshalled in same apartment for iid %s, "
1330 "returning original object %p\n", debugstr_guid(riid
), stubmgr
->object
);
1332 hres
= IUnknown_QueryInterface(stubmgr
->object
, riid
, ppv
);
1334 /* unref the ifstub. FIXME: only do this on success? */
1335 if (!stub_manager_is_table_marshaled(stubmgr
, &stdobjref
.ipid
))
1336 stub_manager_ext_release(stubmgr
, stdobjref
.cPublicRefs
, stdobjref
.flags
& SORFP_TABLEWEAK
, FALSE
);
1338 stub_manager_int_release(stubmgr
);
1342 /* notify stub manager about unmarshal if process-local object.
1343 * note: if the oxid is not found then we and native will quite happily
1344 * ignore table marshaling and normal marshaling rules regarding number of
1345 * unmarshals, etc, but if you abuse these rules then your proxy could end
1346 * up returning RPC_E_DISCONNECTED. */
1347 if ((stub_apt
= apartment_findfromoxid(stdobjref
.oxid
, TRUE
)))
1349 if ((stubmgr
= get_stub_manager(stub_apt
, stdobjref
.oid
)))
1351 if (!stub_manager_notify_unmarshal(stubmgr
, &stdobjref
.ipid
))
1352 hres
= CO_E_OBJNOTCONNECTED
;
1356 WARN("Couldn't find object for OXID %s, OID %s, assuming disconnected\n",
1357 wine_dbgstr_longlong(stdobjref
.oxid
),
1358 wine_dbgstr_longlong(stdobjref
.oid
));
1359 hres
= CO_E_OBJNOTCONNECTED
;
1363 TRACE("Treating unmarshal from OXID %s as inter-process\n",
1364 wine_dbgstr_longlong(stdobjref
.oxid
));
1367 hres
= unmarshal_object(&stdobjref
, apt
, This
->dwDestContext
,
1368 This
->pvDestContext
, riid
,
1369 stubmgr
? &stubmgr
->oxid_info
: NULL
, ppv
);
1371 if (stubmgr
) stub_manager_int_release(stubmgr
);
1372 if (stub_apt
) apartment_release(stub_apt
);
1374 if (hres
!= S_OK
) WARN("Failed with error 0x%08x\n", hres
);
1375 else TRACE("Successfully created proxy %p\n", *ppv
);
1380 static HRESULT WINAPI
1381 StdMarshalImpl_ReleaseMarshalData(LPMARSHAL iface
, IStream
*pStm
)
1383 STDOBJREF stdobjref
;
1386 struct stub_manager
*stubmgr
;
1389 TRACE("iface=%p, pStm=%p\n", iface
, pStm
);
1391 hres
= IStream_Read(pStm
, &stdobjref
, sizeof(stdobjref
), &res
);
1392 if (hres
!= S_OK
) return STG_E_READFAULT
;
1394 TRACE("oxid = %s, oid = %s, ipid = %s\n",
1395 wine_dbgstr_longlong(stdobjref
.oxid
),
1396 wine_dbgstr_longlong(stdobjref
.oid
),
1397 wine_dbgstr_guid(&stdobjref
.ipid
));
1399 if (!(apt
= apartment_findfromoxid(stdobjref
.oxid
, TRUE
)))
1401 WARN("Could not map OXID %s to apartment object\n",
1402 wine_dbgstr_longlong(stdobjref
.oxid
));
1403 return RPC_E_INVALID_OBJREF
;
1406 if (!(stubmgr
= get_stub_manager(apt
, stdobjref
.oid
)))
1408 ERR("could not map object ID to stub manager, oxid=%s, oid=%s\n",
1409 wine_dbgstr_longlong(stdobjref
.oxid
), wine_dbgstr_longlong(stdobjref
.oid
));
1410 return RPC_E_INVALID_OBJREF
;
1413 stub_manager_release_marshal_data(stubmgr
, stdobjref
.cPublicRefs
, &stdobjref
.ipid
, stdobjref
.flags
& SORFP_TABLEWEAK
);
1415 stub_manager_int_release(stubmgr
);
1416 apartment_release(apt
);
1421 static HRESULT WINAPI
1422 StdMarshalImpl_DisconnectObject(LPMARSHAL iface
, DWORD dwReserved
)
1424 FIXME("(), stub!\n");
1428 static const IMarshalVtbl VT_StdMarshal
=
1430 StdMarshalImpl_QueryInterface
,
1431 StdMarshalImpl_AddRef
,
1432 StdMarshalImpl_Release
,
1433 StdMarshalImpl_GetUnmarshalClass
,
1434 StdMarshalImpl_GetMarshalSizeMax
,
1435 StdMarshalImpl_MarshalInterface
,
1436 StdMarshalImpl_UnmarshalInterface
,
1437 StdMarshalImpl_ReleaseMarshalData
,
1438 StdMarshalImpl_DisconnectObject
1441 static HRESULT
StdMarshalImpl_Construct(REFIID riid
, void** ppvObject
)
1443 StdMarshalImpl
* pStdMarshal
=
1444 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(StdMarshalImpl
));
1446 return E_OUTOFMEMORY
;
1447 pStdMarshal
->lpvtbl
= &VT_StdMarshal
;
1448 pStdMarshal
->ref
= 0;
1449 return IMarshal_QueryInterface((IMarshal
*)pStdMarshal
, riid
, ppvObject
);
1452 /***********************************************************************
1453 * CoGetStandardMarshal [OLE32.@]
1455 * Gets or creates a standard marshal object.
1458 * riid [I] Interface identifier of the pUnk object.
1459 * pUnk [I] Optional. Object to get the marshal object for.
1460 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1461 * pvDestContext [I] Reserved. Must be NULL.
1462 * mshlflags [I] Flags affecting the marshaling process.
1463 * ppMarshal [O] Address where marshal object will be stored.
1467 * Failure: HRESULT code.
1471 * The function retrieves the IMarshal object associated with an object if
1472 * that object is currently an active stub, otherwise a new marshal object is
1475 HRESULT WINAPI
CoGetStandardMarshal(REFIID riid
, IUnknown
*pUnk
,
1476 DWORD dwDestContext
, LPVOID pvDestContext
,
1477 DWORD mshlflags
, LPMARSHAL
*ppMarshal
)
1483 FIXME("(%s,NULL,%x,%p,%x,%p), unimplemented yet.\n",
1484 debugstr_guid(riid
),dwDestContext
,pvDestContext
,mshlflags
,ppMarshal
);
1487 TRACE("(%s,%p,%x,%p,%x,%p)\n",
1488 debugstr_guid(riid
),pUnk
,dwDestContext
,pvDestContext
,mshlflags
,ppMarshal
);
1489 *ppMarshal
= HeapAlloc(GetProcessHeap(),0,sizeof(StdMarshalImpl
));
1490 dm
= (StdMarshalImpl
*) *ppMarshal
;
1491 if (!dm
) return E_FAIL
;
1492 dm
->lpvtbl
= &VT_StdMarshal
;
1496 dm
->dwDestContext
= dwDestContext
;
1497 dm
->pvDestContext
= pvDestContext
;
1498 dm
->mshlflags
= mshlflags
;
1502 /***********************************************************************
1503 * get_marshaler [internal]
1505 * Retrieves an IMarshal interface for an object.
1507 static HRESULT
get_marshaler(REFIID riid
, IUnknown
*pUnk
, DWORD dwDestContext
,
1508 void *pvDestContext
, DWORD mshlFlags
,
1509 LPMARSHAL
*pMarshal
)
1515 hr
= IUnknown_QueryInterface(pUnk
, &IID_IMarshal
, (LPVOID
*)pMarshal
);
1517 hr
= CoGetStandardMarshal(riid
, pUnk
, dwDestContext
, pvDestContext
,
1518 mshlFlags
, pMarshal
);
1522 /***********************************************************************
1523 * get_unmarshaler_from_stream [internal]
1525 * Creates an IMarshal* object according to the data marshaled to the stream.
1526 * The function leaves the stream pointer at the start of the data written
1527 * to the stream by the IMarshal* object.
1529 static HRESULT
get_unmarshaler_from_stream(IStream
*stream
, IMarshal
**marshal
, IID
*iid
)
1535 /* read common OBJREF header */
1536 hr
= IStream_Read(stream
, &objref
, FIELD_OFFSET(OBJREF
, u_objref
), &res
);
1537 if (hr
!= S_OK
|| (res
!= FIELD_OFFSET(OBJREF
, u_objref
)))
1539 ERR("Failed to read common OBJREF header, 0x%08x\n", hr
);
1540 return STG_E_READFAULT
;
1543 /* sanity check on header */
1544 if (objref
.signature
!= OBJREF_SIGNATURE
)
1546 ERR("Bad OBJREF signature 0x%08x\n", objref
.signature
);
1547 return RPC_E_INVALID_OBJREF
;
1550 if (iid
) *iid
= objref
.iid
;
1552 /* FIXME: handler marshaling */
1553 if (objref
.flags
& OBJREF_STANDARD
)
1555 TRACE("Using standard unmarshaling\n");
1556 hr
= StdMarshalImpl_Construct(&IID_IMarshal
, (LPVOID
*)marshal
);
1558 else if (objref
.flags
& OBJREF_CUSTOM
)
1560 ULONG custom_header_size
= FIELD_OFFSET(OBJREF
, u_objref
.u_custom
.pData
) -
1561 FIELD_OFFSET(OBJREF
, u_objref
.u_custom
);
1562 TRACE("Using custom unmarshaling\n");
1563 /* read constant sized OR_CUSTOM data from stream */
1564 hr
= IStream_Read(stream
, &objref
.u_objref
.u_custom
,
1565 custom_header_size
, &res
);
1566 if (hr
!= S_OK
|| (res
!= custom_header_size
))
1568 ERR("Failed to read OR_CUSTOM header, 0x%08x\n", hr
);
1569 return STG_E_READFAULT
;
1571 /* now create the marshaler specified in the stream */
1572 hr
= CoCreateInstance(&objref
.u_objref
.u_custom
.clsid
, NULL
,
1573 CLSCTX_INPROC_SERVER
, &IID_IMarshal
,
1578 FIXME("Invalid or unimplemented marshaling type specified: %x\n",
1580 return RPC_E_INVALID_OBJREF
;
1584 ERR("Failed to create marshal, 0x%08x\n", hr
);
1589 /***********************************************************************
1590 * CoGetMarshalSizeMax [OLE32.@]
1592 * Gets the maximum amount of data that will be needed by a marshal.
1595 * pulSize [O] Address where maximum marshal size will be stored.
1596 * riid [I] Identifier of the interface to marshal.
1597 * pUnk [I] Pointer to the object to marshal.
1598 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1599 * pvDestContext [I] Reserved. Must be NULL.
1600 * mshlFlags [I] Flags that affect the marshaling. See CoMarshalInterface().
1604 * Failure: HRESULT code.
1607 * CoMarshalInterface().
1609 HRESULT WINAPI
CoGetMarshalSizeMax(ULONG
*pulSize
, REFIID riid
, IUnknown
*pUnk
,
1610 DWORD dwDestContext
, void *pvDestContext
,
1615 CLSID marshaler_clsid
;
1617 hr
= get_marshaler(riid
, pUnk
, dwDestContext
, pvDestContext
, mshlFlags
, &pMarshal
);
1621 hr
= IMarshal_GetUnmarshalClass(pMarshal
, riid
, pUnk
, dwDestContext
,
1622 pvDestContext
, mshlFlags
, &marshaler_clsid
);
1625 ERR("IMarshal::GetUnmarshalClass failed, 0x%08x\n", hr
);
1626 IMarshal_Release(pMarshal
);
1630 hr
= IMarshal_GetMarshalSizeMax(pMarshal
, riid
, pUnk
, dwDestContext
,
1631 pvDestContext
, mshlFlags
, pulSize
);
1632 if (IsEqualCLSID(&marshaler_clsid
, &CLSID_DfMarshal
))
1633 /* add on the size of the common header */
1634 *pulSize
+= FIELD_OFFSET(OBJREF
, u_objref
);
1636 /* custom marshaling: add on the size of the whole OBJREF structure
1637 * like native does */
1638 *pulSize
+= sizeof(OBJREF
);
1640 IMarshal_Release(pMarshal
);
1645 static void dump_MSHLFLAGS(MSHLFLAGS flags
)
1647 if (flags
& MSHLFLAGS_TABLESTRONG
)
1648 TRACE(" MSHLFLAGS_TABLESTRONG");
1649 if (flags
& MSHLFLAGS_TABLEWEAK
)
1650 TRACE(" MSHLFLAGS_TABLEWEAK");
1651 if (!(flags
& (MSHLFLAGS_TABLESTRONG
|MSHLFLAGS_TABLEWEAK
)))
1652 TRACE(" MSHLFLAGS_NORMAL");
1653 if (flags
& MSHLFLAGS_NOPING
)
1654 TRACE(" MSHLFLAGS_NOPING");
1657 /***********************************************************************
1658 * CoMarshalInterface [OLE32.@]
1660 * Marshals an interface into a stream so that the object can then be
1661 * unmarshaled from another COM apartment and used remotely.
1664 * pStream [I] Stream the object will be marshaled into.
1665 * riid [I] Identifier of the interface to marshal.
1666 * pUnk [I] Pointer to the object to marshal.
1667 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1668 * pvDestContext [I] Reserved. Must be NULL.
1669 * mshlFlags [I] Flags that affect the marshaling. See notes.
1673 * Failure: HRESULT code.
1677 * The mshlFlags parameter can take one or more of the following flags:
1678 *| MSHLFLAGS_NORMAL - Unmarshal once, releases stub on last proxy release.
1679 *| MSHLFLAGS_TABLESTRONG - Unmarshal many, release when CoReleaseMarshalData() called.
1680 *| MSHLFLAGS_TABLEWEAK - Unmarshal many, releases stub on last proxy release.
1681 *| MSHLFLAGS_NOPING - No automatic garbage collection (and so reduces network traffic).
1683 * If a marshaled object is not unmarshaled, then CoReleaseMarshalData() must
1684 * be called in order to release the resources used in the marshaling.
1687 * CoUnmarshalInterface(), CoReleaseMarshalData().
1689 HRESULT WINAPI
CoMarshalInterface(IStream
*pStream
, REFIID riid
, IUnknown
*pUnk
,
1690 DWORD dwDestContext
, void *pvDestContext
,
1694 CLSID marshaler_clsid
;
1698 TRACE("(%p, %s, %p, %x, %p,", pStream
, debugstr_guid(riid
), pUnk
,
1699 dwDestContext
, pvDestContext
);
1700 dump_MSHLFLAGS(mshlFlags
);
1703 if (!pUnk
|| !pStream
)
1704 return E_INVALIDARG
;
1706 objref
.signature
= OBJREF_SIGNATURE
;
1709 /* get the marshaler for the specified interface */
1710 hr
= get_marshaler(riid
, pUnk
, dwDestContext
, pvDestContext
, mshlFlags
, &pMarshal
);
1713 ERR("Failed to get marshaller, 0x%08x\n", hr
);
1717 hr
= IMarshal_GetUnmarshalClass(pMarshal
, riid
, pUnk
, dwDestContext
,
1718 pvDestContext
, mshlFlags
, &marshaler_clsid
);
1721 ERR("IMarshal::GetUnmarshalClass failed, 0x%08x\n", hr
);
1725 /* FIXME: implement handler marshaling too */
1726 if (IsEqualCLSID(&marshaler_clsid
, &CLSID_DfMarshal
))
1728 TRACE("Using standard marshaling\n");
1729 objref
.flags
= OBJREF_STANDARD
;
1731 /* write the common OBJREF header to the stream */
1732 hr
= IStream_Write(pStream
, &objref
, FIELD_OFFSET(OBJREF
, u_objref
), NULL
);
1735 ERR("Failed to write OBJREF header to stream, 0x%08x\n", hr
);
1741 TRACE("Using custom marshaling\n");
1742 objref
.flags
= OBJREF_CUSTOM
;
1743 objref
.u_objref
.u_custom
.clsid
= marshaler_clsid
;
1744 objref
.u_objref
.u_custom
.cbExtension
= 0;
1745 objref
.u_objref
.u_custom
.size
= 0;
1746 hr
= IMarshal_GetMarshalSizeMax(pMarshal
, riid
, pUnk
, dwDestContext
,
1747 pvDestContext
, mshlFlags
,
1748 &objref
.u_objref
.u_custom
.size
);
1751 ERR("Failed to get max size of marshal data, error 0x%08x\n", hr
);
1754 /* write constant sized common header and OR_CUSTOM data into stream */
1755 hr
= IStream_Write(pStream
, &objref
,
1756 FIELD_OFFSET(OBJREF
, u_objref
.u_custom
.pData
), NULL
);
1759 ERR("Failed to write OR_CUSTOM header to stream with 0x%08x\n", hr
);
1764 TRACE("Calling IMarshal::MarshalInterace\n");
1765 /* call helper object to do the actual marshaling */
1766 hr
= IMarshal_MarshalInterface(pMarshal
, pStream
, riid
, pUnk
, dwDestContext
,
1767 pvDestContext
, mshlFlags
);
1771 ERR("Failed to marshal the interface %s, %x\n", debugstr_guid(riid
), hr
);
1776 IMarshal_Release(pMarshal
);
1778 TRACE("completed with hr 0x%08x\n", hr
);
1783 /***********************************************************************
1784 * CoUnmarshalInterface [OLE32.@]
1786 * Unmarshals an object from a stream by creating a proxy to the remote
1787 * object, if necessary.
1791 * pStream [I] Stream containing the marshaled object.
1792 * riid [I] Interface identifier of the object to create a proxy to.
1793 * ppv [O] Address where proxy will be stored.
1798 * Failure: HRESULT code.
1801 * CoMarshalInterface().
1803 HRESULT WINAPI
CoUnmarshalInterface(IStream
*pStream
, REFIID riid
, LPVOID
*ppv
)
1810 TRACE("(%p, %s, %p)\n", pStream
, debugstr_guid(riid
), ppv
);
1812 if (!pStream
|| !ppv
)
1813 return E_INVALIDARG
;
1815 hr
= get_unmarshaler_from_stream(pStream
, &pMarshal
, &iid
);
1819 /* call the helper object to do the actual unmarshaling */
1820 hr
= IMarshal_UnmarshalInterface(pMarshal
, pStream
, &iid
, (LPVOID
*)&object
);
1822 ERR("IMarshal::UnmarshalInterface failed, 0x%08x\n", hr
);
1826 /* IID_NULL means use the interface ID of the marshaled object */
1827 if (!IsEqualIID(riid
, &IID_NULL
) && !IsEqualIID(riid
, &iid
))
1829 TRACE("requested interface != marshalled interface, additional QI needed\n");
1830 hr
= IUnknown_QueryInterface(object
, riid
, ppv
);
1832 ERR("Couldn't query for interface %s, hr = 0x%08x\n",
1833 debugstr_guid(riid
), hr
);
1834 IUnknown_Release(object
);
1842 IMarshal_Release(pMarshal
);
1844 TRACE("completed with hr 0x%x\n", hr
);
1849 /***********************************************************************
1850 * CoReleaseMarshalData [OLE32.@]
1852 * Releases resources associated with an object that has been marshaled into
1857 * pStream [I] The stream that the object has been marshaled into.
1861 * Failure: HRESULT error code.
1865 * Call this function to release resources associated with a normal or
1866 * table-weak marshal that will not be unmarshaled, and all table-strong
1867 * marshals when they are no longer needed.
1870 * CoMarshalInterface(), CoUnmarshalInterface().
1872 HRESULT WINAPI
CoReleaseMarshalData(IStream
*pStream
)
1877 TRACE("(%p)\n", pStream
);
1879 hr
= get_unmarshaler_from_stream(pStream
, &pMarshal
, NULL
);
1883 /* call the helper object to do the releasing of marshal data */
1884 hr
= IMarshal_ReleaseMarshalData(pMarshal
, pStream
);
1886 ERR("IMarshal::ReleaseMarshalData failed with error 0x%08x\n", hr
);
1888 IMarshal_Release(pMarshal
);
1893 /***********************************************************************
1894 * CoMarshalInterThreadInterfaceInStream [OLE32.@]
1896 * Marshal an interface across threads in the same process.
1899 * riid [I] Identifier of the interface to be marshalled.
1900 * pUnk [I] Pointer to IUnknown-derived interface that will be marshalled.
1901 * ppStm [O] Pointer to IStream object that is created and then used to store the marshalled interface.
1905 * Failure: E_OUTOFMEMORY and other COM error codes
1908 * CoMarshalInterface(), CoUnmarshalInterface() and CoGetInterfaceAndReleaseStream()
1910 HRESULT WINAPI
CoMarshalInterThreadInterfaceInStream(
1911 REFIID riid
, LPUNKNOWN pUnk
, LPSTREAM
* ppStm
)
1913 ULARGE_INTEGER xpos
;
1914 LARGE_INTEGER seekto
;
1917 TRACE("(%s, %p, %p)\n",debugstr_guid(riid
), pUnk
, ppStm
);
1919 hres
= CreateStreamOnHGlobal(NULL
, TRUE
, ppStm
);
1920 if (FAILED(hres
)) return hres
;
1921 hres
= CoMarshalInterface(*ppStm
, riid
, pUnk
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_NORMAL
);
1923 if (SUCCEEDED(hres
))
1925 memset(&seekto
, 0, sizeof(seekto
));
1926 IStream_Seek(*ppStm
, seekto
, STREAM_SEEK_SET
, &xpos
);
1930 IStream_Release(*ppStm
);
1937 /***********************************************************************
1938 * CoGetInterfaceAndReleaseStream [OLE32.@]
1940 * Unmarshalls an interface from a stream and then releases the stream.
1943 * pStm [I] Stream that contains the marshalled interface.
1944 * riid [I] Interface identifier of the object to unmarshall.
1945 * ppv [O] Address of pointer where the requested interface object will be stored.
1949 * Failure: A COM error code
1952 * CoMarshalInterThreadInterfaceInStream() and CoUnmarshalInterface()
1954 HRESULT WINAPI
CoGetInterfaceAndReleaseStream(LPSTREAM pStm
, REFIID riid
,
1959 TRACE("(%p, %s, %p)\n", pStm
, debugstr_guid(riid
), ppv
);
1961 if(!pStm
) return E_INVALIDARG
;
1962 hres
= CoUnmarshalInterface(pStm
, riid
, ppv
);
1963 IStream_Release(pStm
);
1967 static HRESULT WINAPI
StdMarshalCF_QueryInterface(LPCLASSFACTORY iface
,
1968 REFIID riid
, LPVOID
*ppv
)
1971 if (IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IClassFactory
))
1976 return E_NOINTERFACE
;
1979 static ULONG WINAPI
StdMarshalCF_AddRef(LPCLASSFACTORY iface
)
1981 return 2; /* non-heap based object */
1984 static ULONG WINAPI
StdMarshalCF_Release(LPCLASSFACTORY iface
)
1986 return 1; /* non-heap based object */
1989 static HRESULT WINAPI
StdMarshalCF_CreateInstance(LPCLASSFACTORY iface
,
1990 LPUNKNOWN pUnk
, REFIID riid
, LPVOID
*ppv
)
1992 if (IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IMarshal
))
1993 return StdMarshalImpl_Construct(riid
, ppv
);
1995 FIXME("(%s), not supported.\n",debugstr_guid(riid
));
1996 return E_NOINTERFACE
;
1999 static HRESULT WINAPI
StdMarshalCF_LockServer(LPCLASSFACTORY iface
, BOOL fLock
)
2001 FIXME("(%d), stub!\n",fLock
);
2005 static const IClassFactoryVtbl StdMarshalCFVtbl
=
2007 StdMarshalCF_QueryInterface
,
2008 StdMarshalCF_AddRef
,
2009 StdMarshalCF_Release
,
2010 StdMarshalCF_CreateInstance
,
2011 StdMarshalCF_LockServer
2013 static const IClassFactoryVtbl
*StdMarshalCF
= &StdMarshalCFVtbl
;
2015 HRESULT
MARSHAL_GetStandardMarshalCF(LPVOID
*ppv
)
2017 *ppv
= &StdMarshalCF
;
2021 /***********************************************************************
2022 * CoMarshalHresult [OLE32.@]
2024 * Marshals an HRESULT value into a stream.
2027 * pStm [I] Stream that hresult will be marshalled into.
2028 * hresult [I] HRESULT to be marshalled.
2032 * Failure: A COM error code
2035 * CoUnmarshalHresult().
2037 HRESULT WINAPI
CoMarshalHresult(LPSTREAM pStm
, HRESULT hresult
)
2039 return IStream_Write(pStm
, &hresult
, sizeof(hresult
), NULL
);
2042 /***********************************************************************
2043 * CoUnmarshalHresult [OLE32.@]
2045 * Unmarshals an HRESULT value from a stream.
2048 * pStm [I] Stream that hresult will be unmarshalled from.
2049 * phresult [I] Pointer to HRESULT where the value will be unmarshalled to.
2053 * Failure: A COM error code
2056 * CoMarshalHresult().
2058 HRESULT WINAPI
CoUnmarshalHresult(LPSTREAM pStm
, HRESULT
* phresult
)
2060 return IStream_Read(pStm
, phresult
, sizeof(*phresult
), NULL
);