4 * Copyright 2002 Marcus Meissner
5 * Copyright 2004 Mike Hearn, for CodeWeavers
6 * Copyright 2004 Rob Shearman, for CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
35 #include "wine/unicode.h"
37 #include "compobj_private.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
43 extern const CLSID CLSID_DfMarshal
;
45 /* number of refs given out for normal marshaling */
46 #define NORMALEXTREFS 5
48 /* private flag indicating that the caller does not want to notify the stub
49 * when the proxy disconnects or is destroyed */
50 #define SORFP_NOLIFETIMEMGMT SORF_OXRES1
52 static HRESULT
unmarshal_object(const STDOBJREF
*stdobjref
, APARTMENT
*apt
,
53 MSHCTX dest_context
, void *dest_context_data
,
54 REFIID riid
, const OXID_INFO
*oxid_info
,
57 /* Marshalling just passes a unique identifier to the remote client,
58 * that makes it possible to find the passed interface again.
60 * So basically we need a set of values that make it unique.
62 * Note that the IUnknown_QI(ob,xiid,&ppv) always returns the SAME ppv value!
64 * A triple is used: OXID (apt id), OID (stub manager id),
65 * IPID (interface ptr/stub id).
67 * OXIDs identify an apartment and are network scoped
68 * OIDs identify a stub manager and are apartment scoped
69 * IPIDs identify an interface stub and are apartment scoped
72 static inline HRESULT
get_facbuf_for_iid(REFIID riid
, IPSFactoryBuffer
**facbuf
)
77 if ((hr
= CoGetPSClsid(riid
, &clsid
)))
79 return CoGetClassObject(&clsid
, CLSCTX_INPROC_SERVER
| WINE_CLSCTX_DONT_HOST
,
80 NULL
, &IID_IPSFactoryBuffer
, (LPVOID
*)facbuf
);
83 /* marshals an object into a STDOBJREF structure */
84 HRESULT
marshal_object(APARTMENT
*apt
, STDOBJREF
*stdobjref
, REFIID riid
, IUnknown
*object
, MSHLFLAGS mshlflags
)
86 struct stub_manager
*manager
;
87 struct ifstub
*ifstub
;
89 IRpcStubBuffer
*stub
= NULL
;
91 IUnknown
*iobject
= NULL
; /* object of type riid */
93 hr
= apartment_getoxid(apt
, &stdobjref
->oxid
);
97 hr
= apartment_createwindowifneeded(apt
);
101 hr
= IUnknown_QueryInterface(object
, riid
, (void **)&iobject
);
104 ERR("object doesn't expose interface %s, failing with error 0x%08x\n",
105 debugstr_guid(riid
), hr
);
106 return E_NOINTERFACE
;
109 /* IUnknown doesn't require a stub buffer, because it never goes out on
111 if (!IsEqualIID(riid
, &IID_IUnknown
))
113 IPSFactoryBuffer
*psfb
;
115 hr
= get_facbuf_for_iid(riid
, &psfb
);
118 ERR("couldn't get IPSFactory buffer for interface %s\n", debugstr_guid(riid
));
119 IUnknown_Release(iobject
);
123 hr
= IPSFactoryBuffer_CreateStub(psfb
, riid
, iobject
, &stub
);
124 IPSFactoryBuffer_Release(psfb
);
127 ERR("Failed to create an IRpcStubBuffer from IPSFactory for %s with error 0x%08x\n",
128 debugstr_guid(riid
), hr
);
129 IUnknown_Release(iobject
);
134 if (mshlflags
& MSHLFLAGS_NOPING
)
135 stdobjref
->flags
= SORF_NOPING
;
137 stdobjref
->flags
= SORF_NULL
;
139 if ((manager
= get_stub_manager_from_object(apt
, object
)))
140 TRACE("registering new ifstub on pre-existing manager\n");
143 TRACE("constructing new stub manager\n");
145 manager
= new_stub_manager(apt
, object
);
148 if (stub
) IRpcStubBuffer_Release(stub
);
149 IUnknown_Release(iobject
);
150 return E_OUTOFMEMORY
;
153 stdobjref
->oid
= manager
->oid
;
155 tablemarshal
= ((mshlflags
& MSHLFLAGS_TABLESTRONG
) || (mshlflags
& MSHLFLAGS_TABLEWEAK
));
157 /* make sure ifstub that we are creating is unique */
158 ifstub
= stub_manager_find_ifstub(manager
, riid
, mshlflags
);
160 ifstub
= stub_manager_new_ifstub(manager
, stub
, iobject
, riid
, mshlflags
);
162 if (stub
) IRpcStubBuffer_Release(stub
);
163 IUnknown_Release(iobject
);
167 stub_manager_int_release(manager
);
168 /* destroy the stub manager if it has no ifstubs by releasing
169 * zero external references */
170 stub_manager_ext_release(manager
, 0, TRUE
);
171 return E_OUTOFMEMORY
;
176 stdobjref
->cPublicRefs
= NORMALEXTREFS
;
177 stub_manager_ext_addref(manager
, stdobjref
->cPublicRefs
);
181 stdobjref
->cPublicRefs
= 0;
182 if (mshlflags
& MSHLFLAGS_TABLESTRONG
)
183 stub_manager_ext_addref(manager
, 1);
186 /* FIXME: check return value */
187 RPC_RegisterInterface(riid
);
189 stdobjref
->ipid
= ifstub
->ipid
;
191 stub_manager_int_release(manager
);
197 /* Client-side identity of the server object */
199 static HRESULT
proxy_manager_get_remunknown(struct proxy_manager
* This
, IRemUnknown
**remunk
);
200 static void proxy_manager_destroy(struct proxy_manager
* This
);
201 static HRESULT
proxy_manager_find_ifproxy(struct proxy_manager
* This
, REFIID riid
, struct ifproxy
** ifproxy_found
);
202 static HRESULT
proxy_manager_query_local_interface(struct proxy_manager
* This
, REFIID riid
, void ** ppv
);
204 static HRESULT WINAPI
ClientIdentity_QueryInterface(IMultiQI
* iface
, REFIID riid
, void ** ppv
)
209 TRACE("%s\n", debugstr_guid(riid
));
212 hr
= IMultiQI_QueryMultipleInterfaces(iface
, 1, &mqi
);
213 *ppv
= (void *)mqi
.pItf
;
218 static ULONG WINAPI
ClientIdentity_AddRef(IMultiQI
* iface
)
220 struct proxy_manager
* This
= (struct proxy_manager
*)iface
;
221 TRACE("%p - before %d\n", iface
, This
->refs
);
222 return InterlockedIncrement(&This
->refs
);
225 static ULONG WINAPI
ClientIdentity_Release(IMultiQI
* iface
)
227 struct proxy_manager
* This
= (struct proxy_manager
*)iface
;
228 ULONG refs
= InterlockedDecrement(&This
->refs
);
229 TRACE("%p - after %d\n", iface
, refs
);
231 proxy_manager_destroy(This
);
235 static HRESULT WINAPI
ClientIdentity_QueryMultipleInterfaces(IMultiQI
*iface
, ULONG cMQIs
, MULTI_QI
*pMQIs
)
237 struct proxy_manager
* This
= (struct proxy_manager
*)iface
;
238 REMQIRESULT
*qiresults
= NULL
;
239 ULONG nonlocal_mqis
= 0;
241 ULONG successful_mqis
= 0;
242 IID
*iids
= HeapAlloc(GetProcessHeap(), 0, cMQIs
* sizeof(*iids
));
243 /* mapping of RemQueryInterface index to QueryMultipleInterfaces index */
244 ULONG
*mapping
= HeapAlloc(GetProcessHeap(), 0, cMQIs
* sizeof(*mapping
));
246 TRACE("cMQIs: %d\n", cMQIs
);
248 /* try to get a local interface - this includes already active proxy
249 * interfaces and also interfaces exposed by the proxy manager */
250 for (i
= 0; i
< cMQIs
; i
++)
252 TRACE("iid[%d] = %s\n", i
, debugstr_guid(pMQIs
[i
].pIID
));
253 pMQIs
[i
].hr
= proxy_manager_query_local_interface(This
, pMQIs
[i
].pIID
, (void **)&pMQIs
[i
].pItf
);
254 if (pMQIs
[i
].hr
== S_OK
)
258 iids
[nonlocal_mqis
] = *pMQIs
[i
].pIID
;
259 mapping
[nonlocal_mqis
] = i
;
264 TRACE("%d interfaces not found locally\n", nonlocal_mqis
);
266 /* if we have more than one interface not found locally then we must try
267 * to query the remote object for it */
268 if (nonlocal_mqis
!= 0)
274 /* get the ipid of the first entry */
275 /* FIXME: should we implement ClientIdentity on the ifproxies instead
276 * of the proxy_manager so we use the correct ipid here? */
277 ipid
= &LIST_ENTRY(list_head(&This
->interfaces
), struct ifproxy
, entry
)->stdobjref
.ipid
;
279 /* get IRemUnknown proxy so we can communicate with the remote object */
280 hr
= proxy_manager_get_remunknown(This
, &remunk
);
284 hr
= IRemUnknown_RemQueryInterface(remunk
, ipid
, NORMALEXTREFS
,
285 nonlocal_mqis
, iids
, &qiresults
);
286 IRemUnknown_Release(remunk
);
288 ERR("IRemUnknown_RemQueryInterface failed with error 0x%08x\n", hr
);
291 /* IRemUnknown_RemQueryInterface can return S_FALSE if only some of
292 * the interfaces were returned */
295 /* try to unmarshal each object returned to us */
296 for (i
= 0; i
< nonlocal_mqis
; i
++)
298 ULONG index
= mapping
[i
];
299 HRESULT hrobj
= qiresults
[i
].hResult
;
301 hrobj
= unmarshal_object(&qiresults
[i
].std
, COM_CurrentApt(),
303 This
->dest_context_data
,
304 pMQIs
[index
].pIID
, &This
->oxid_info
,
305 (void **)&pMQIs
[index
].pItf
);
310 ERR("Failed to get pointer to interface %s\n", debugstr_guid(pMQIs
[index
].pIID
));
311 pMQIs
[index
].hr
= hrobj
;
315 /* free the memory allocated by the proxy */
316 CoTaskMemFree(qiresults
);
319 TRACE("%d/%d successfully queried\n", successful_mqis
, cMQIs
);
321 HeapFree(GetProcessHeap(), 0, iids
);
322 HeapFree(GetProcessHeap(), 0, mapping
);
324 if (successful_mqis
== cMQIs
)
325 return S_OK
; /* we got all requested interfaces */
326 else if (successful_mqis
== 0)
327 return E_NOINTERFACE
; /* we didn't get any interfaces */
329 return S_FALSE
; /* we got some interfaces */
332 static const IMultiQIVtbl ClientIdentity_Vtbl
=
334 ClientIdentity_QueryInterface
,
335 ClientIdentity_AddRef
,
336 ClientIdentity_Release
,
337 ClientIdentity_QueryMultipleInterfaces
340 /* FIXME: remove these */
341 static HRESULT WINAPI
StdMarshalImpl_GetUnmarshalClass(LPMARSHAL iface
, REFIID riid
, void* pv
, DWORD dwDestContext
, void* pvDestContext
, DWORD mshlflags
, CLSID
* pCid
);
342 static HRESULT WINAPI
StdMarshalImpl_GetMarshalSizeMax(LPMARSHAL iface
, REFIID riid
, void* pv
, DWORD dwDestContext
, void* pvDestContext
, DWORD mshlflags
, DWORD
* pSize
);
343 static HRESULT WINAPI
StdMarshalImpl_UnmarshalInterface(LPMARSHAL iface
, IStream
*pStm
, REFIID riid
, void **ppv
);
344 static HRESULT WINAPI
StdMarshalImpl_ReleaseMarshalData(LPMARSHAL iface
, IStream
*pStm
);
345 static HRESULT WINAPI
StdMarshalImpl_DisconnectObject(LPMARSHAL iface
, DWORD dwReserved
);
347 static HRESULT WINAPI
Proxy_QueryInterface(IMarshal
*iface
, REFIID riid
, void **ppvObject
)
349 ICOM_THIS_MULTI(struct proxy_manager
, lpVtblMarshal
, iface
);
350 return IMultiQI_QueryInterface((IMultiQI
*)&This
->lpVtbl
, riid
, ppvObject
);
353 static ULONG WINAPI
Proxy_AddRef(IMarshal
*iface
)
355 ICOM_THIS_MULTI(struct proxy_manager
, lpVtblMarshal
, iface
);
356 return IMultiQI_AddRef((IMultiQI
*)&This
->lpVtbl
);
359 static ULONG WINAPI
Proxy_Release(IMarshal
*iface
)
361 ICOM_THIS_MULTI(struct proxy_manager
, lpVtblMarshal
, iface
);
362 return IMultiQI_Release((IMultiQI
*)&This
->lpVtbl
);
365 static HRESULT WINAPI
Proxy_MarshalInterface(
366 LPMARSHAL iface
, IStream
*pStm
, REFIID riid
, void* pv
, DWORD dwDestContext
,
367 void* pvDestContext
, DWORD mshlflags
)
369 ICOM_THIS_MULTI(struct proxy_manager
, lpVtblMarshal
, iface
);
371 struct ifproxy
*ifproxy
;
373 TRACE("(...,%s,...)\n", debugstr_guid(riid
));
375 hr
= proxy_manager_find_ifproxy(This
, riid
, &ifproxy
);
378 STDOBJREF stdobjref
= ifproxy
->stdobjref
;
380 stdobjref
.cPublicRefs
= 0;
382 if ((mshlflags
!= MSHLFLAGS_TABLEWEAK
) &&
383 (mshlflags
!= MSHLFLAGS_TABLESTRONG
))
385 ULONG cPublicRefs
= ifproxy
->refs
;
386 ULONG cPublicRefsOld
;
387 /* optimization - share out proxy's public references if possible
388 * instead of making new proxy do a roundtrip through the server */
391 ULONG cPublicRefsNew
;
392 cPublicRefsOld
= cPublicRefs
;
393 stdobjref
.cPublicRefs
= cPublicRefs
/ 2;
394 cPublicRefsNew
= cPublicRefs
- stdobjref
.cPublicRefs
;
395 cPublicRefs
= InterlockedCompareExchange(
396 (LONG
*)&ifproxy
->refs
, cPublicRefsNew
, cPublicRefsOld
);
397 } while (cPublicRefs
!= cPublicRefsOld
);
400 /* normal and table-strong marshaling need at least one reference */
401 if (!stdobjref
.cPublicRefs
&& (mshlflags
!= MSHLFLAGS_TABLEWEAK
))
404 hr
= proxy_manager_get_remunknown(This
, &remunk
);
407 HRESULT hrref
= S_OK
;
409 rif
.ipid
= ifproxy
->stdobjref
.ipid
;
410 rif
.cPublicRefs
= (mshlflags
== MSHLFLAGS_TABLESTRONG
) ? 1 : NORMALEXTREFS
;
411 rif
.cPrivateRefs
= 0;
412 hr
= IRemUnknown_RemAddRef(remunk
, 1, &rif
, &hrref
);
413 IRemUnknown_Release(remunk
);
414 if (hr
== S_OK
&& hrref
== S_OK
)
416 /* table-strong marshaling doesn't give the refs to the
417 * client that unmarshals the STDOBJREF */
418 if (mshlflags
!= MSHLFLAGS_TABLESTRONG
)
419 stdobjref
.cPublicRefs
= rif
.cPublicRefs
;
422 ERR("IRemUnknown_RemAddRef returned with 0x%08x, hrref = 0x%08x\n", hr
, hrref
);
428 TRACE("writing stdobjref:\n\tflags = %04lx\n\tcPublicRefs = %ld\n\toxid = %s\n\toid = %s\n\tipid = %s\n",
429 stdobjref
.flags
, stdobjref
.cPublicRefs
,
430 wine_dbgstr_longlong(stdobjref
.oxid
),
431 wine_dbgstr_longlong(stdobjref
.oid
),
432 debugstr_guid(&stdobjref
.ipid
));
433 hr
= IStream_Write(pStm
, &stdobjref
, sizeof(stdobjref
), NULL
);
438 /* we don't have the interface already unmarshaled so we have to
439 * request the object from the server */
442 REMQIRESULT
*qiresults
= NULL
;
445 /* get the ipid of the first entry */
446 /* FIXME: should we implement ClientIdentity on the ifproxies instead
447 * of the proxy_manager so we use the correct ipid here? */
448 ipid
= &LIST_ENTRY(list_head(&This
->interfaces
), struct ifproxy
, entry
)->stdobjref
.ipid
;
450 /* get IRemUnknown proxy so we can communicate with the remote object */
451 hr
= proxy_manager_get_remunknown(This
, &remunk
);
455 hr
= IRemUnknown_RemQueryInterface(remunk
, ipid
, NORMALEXTREFS
,
456 1, &iid
, &qiresults
);
459 hr
= IStream_Write(pStm
, &qiresults
->std
, sizeof(qiresults
->std
), NULL
);
463 rif
.ipid
= qiresults
->std
.ipid
;
464 rif
.cPublicRefs
= qiresults
->std
.cPublicRefs
;
465 rif
.cPrivateRefs
= 0;
466 IRemUnknown_RemRelease(remunk
, 1, &rif
);
468 CoTaskMemFree(qiresults
);
471 ERR("IRemUnknown_RemQueryInterface failed with error 0x%08x\n", hr
);
472 IRemUnknown_Release(remunk
);
479 static const IMarshalVtbl ProxyMarshal_Vtbl
=
481 Proxy_QueryInterface
,
484 StdMarshalImpl_GetUnmarshalClass
,
485 StdMarshalImpl_GetMarshalSizeMax
,
486 Proxy_MarshalInterface
,
487 StdMarshalImpl_UnmarshalInterface
,
488 StdMarshalImpl_ReleaseMarshalData
,
489 StdMarshalImpl_DisconnectObject
492 static HRESULT WINAPI
ProxyCliSec_QueryInterface(IClientSecurity
*iface
, REFIID riid
, void **ppvObject
)
494 ICOM_THIS_MULTI(struct proxy_manager
, lpVtblCliSec
, iface
);
495 return IMultiQI_QueryInterface((IMultiQI
*)&This
->lpVtbl
, riid
, ppvObject
);
498 static ULONG WINAPI
ProxyCliSec_AddRef(IClientSecurity
*iface
)
500 ICOM_THIS_MULTI(struct proxy_manager
, lpVtblCliSec
, iface
);
501 return IMultiQI_AddRef((IMultiQI
*)&This
->lpVtbl
);
504 static ULONG WINAPI
ProxyCliSec_Release(IClientSecurity
*iface
)
506 ICOM_THIS_MULTI(struct proxy_manager
, lpVtblCliSec
, iface
);
507 return IMultiQI_Release((IMultiQI
*)&This
->lpVtbl
);
510 static HRESULT WINAPI
ProxyCliSec_QueryBlanket(IClientSecurity
*iface
,
514 OLECHAR
**ppServerPrincName
,
518 DWORD
*pCapabilities
)
520 FIXME("(%p, %p, %p, %p, %p, %p, %p, %p): stub\n", pProxy
, pAuthnSvc
,
521 pAuthzSvc
, ppServerPrincName
, pAuthnLevel
, pImpLevel
, pAuthInfo
,
528 if (ppServerPrincName
)
529 *ppServerPrincName
= NULL
;
531 *pAuthnLevel
= RPC_C_AUTHN_LEVEL_DEFAULT
;
533 *pImpLevel
= RPC_C_IMP_LEVEL_DEFAULT
;
537 *pCapabilities
= EOAC_NONE
;
542 static HRESULT WINAPI
ProxyCliSec_SetBlanket(IClientSecurity
*iface
,
543 IUnknown
*pProxy
, DWORD AuthnSvc
,
545 OLECHAR
*pServerPrincName
,
546 DWORD AuthnLevel
, DWORD ImpLevel
,
550 FIXME("(%p, %d, %d, %s, %d, %d, %p, 0x%x): stub\n", pProxy
, AuthnSvc
,
551 AuthzSvc
, debugstr_w(pServerPrincName
), AuthnLevel
, ImpLevel
,
552 pAuthInfo
, Capabilities
);
556 static HRESULT WINAPI
ProxyCliSec_CopyProxy(IClientSecurity
*iface
,
557 IUnknown
*pProxy
, IUnknown
**ppCopy
)
559 FIXME("(%p, %p): stub\n", pProxy
, ppCopy
);
564 static const IClientSecurityVtbl ProxyCliSec_Vtbl
=
566 ProxyCliSec_QueryInterface
,
569 ProxyCliSec_QueryBlanket
,
570 ProxyCliSec_SetBlanket
,
571 ProxyCliSec_CopyProxy
574 static HRESULT
ifproxy_get_public_ref(struct ifproxy
* This
)
578 if (WAIT_OBJECT_0
!= WaitForSingleObject(This
->parent
->remoting_mutex
, INFINITE
))
580 ERR("Wait failed for ifproxy %p\n", This
);
586 IRemUnknown
*remunk
= NULL
;
588 TRACE("getting public ref for ifproxy %p\n", This
);
590 hr
= proxy_manager_get_remunknown(This
->parent
, &remunk
);
593 HRESULT hrref
= S_OK
;
595 rif
.ipid
= This
->stdobjref
.ipid
;
596 rif
.cPublicRefs
= NORMALEXTREFS
;
597 rif
.cPrivateRefs
= 0;
598 hr
= IRemUnknown_RemAddRef(remunk
, 1, &rif
, &hrref
);
599 IRemUnknown_Release(remunk
);
600 if (hr
== S_OK
&& hrref
== S_OK
)
601 InterlockedExchangeAdd((LONG
*)&This
->refs
, NORMALEXTREFS
);
603 ERR("IRemUnknown_RemAddRef returned with 0x%08x, hrref = 0x%08x\n", hr
, hrref
);
606 ReleaseMutex(This
->parent
->remoting_mutex
);
611 static HRESULT
ifproxy_release_public_refs(struct ifproxy
* This
)
616 if (WAIT_OBJECT_0
!= WaitForSingleObject(This
->parent
->remoting_mutex
, INFINITE
))
618 ERR("Wait failed for ifproxy %p\n", This
);
622 public_refs
= This
->refs
;
625 IRemUnknown
*remunk
= NULL
;
627 TRACE("releasing %d refs\n", public_refs
);
629 hr
= proxy_manager_get_remunknown(This
->parent
, &remunk
);
633 rif
.ipid
= This
->stdobjref
.ipid
;
634 rif
.cPublicRefs
= public_refs
;
635 rif
.cPrivateRefs
= 0;
636 hr
= IRemUnknown_RemRelease(remunk
, 1, &rif
);
637 IRemUnknown_Release(remunk
);
639 InterlockedExchangeAdd((LONG
*)&This
->refs
, -public_refs
);
640 else if (hr
== RPC_E_DISCONNECTED
)
641 WARN("couldn't release references because object was "
642 "disconnected: oxid = %s, oid = %s\n",
643 wine_dbgstr_longlong(This
->parent
->oxid
),
644 wine_dbgstr_longlong(This
->parent
->oid
));
646 ERR("IRemUnknown_RemRelease failed with error 0x%08x\n", hr
);
649 ReleaseMutex(This
->parent
->remoting_mutex
);
654 /* should be called inside This->parent->cs critical section */
655 static void ifproxy_disconnect(struct ifproxy
* This
)
657 ifproxy_release_public_refs(This
);
658 if (This
->proxy
) IRpcProxyBuffer_Disconnect(This
->proxy
);
660 IRpcChannelBuffer_Release(This
->chan
);
664 /* should be called in This->parent->cs critical section if it is an entry in parent's list */
665 static void ifproxy_destroy(struct ifproxy
* This
)
669 /* release public references to this object so that the stub can know
670 * when to destroy itself */
671 ifproxy_release_public_refs(This
);
673 list_remove(&This
->entry
);
677 IRpcChannelBuffer_Release(This
->chan
);
681 if (This
->proxy
) IRpcProxyBuffer_Release(This
->proxy
);
683 HeapFree(GetProcessHeap(), 0, This
);
686 static HRESULT
proxy_manager_construct(
687 APARTMENT
* apt
, ULONG sorflags
, OXID oxid
, OID oid
,
688 const OXID_INFO
*oxid_info
, struct proxy_manager
** proxy_manager
)
690 struct proxy_manager
* This
= HeapAlloc(GetProcessHeap(), 0, sizeof(*This
));
691 if (!This
) return E_OUTOFMEMORY
;
693 This
->remoting_mutex
= CreateMutexW(NULL
, FALSE
, NULL
);
694 if (!This
->remoting_mutex
)
696 HeapFree(GetProcessHeap(), 0, This
);
697 return HRESULT_FROM_WIN32(GetLastError());
702 This
->oxid_info
.dwPid
= oxid_info
->dwPid
;
703 This
->oxid_info
.dwTid
= oxid_info
->dwTid
;
704 This
->oxid_info
.ipidRemUnknown
= oxid_info
->ipidRemUnknown
;
705 This
->oxid_info
.dwAuthnHint
= oxid_info
->dwAuthnHint
;
706 This
->oxid_info
.psa
= NULL
/* FIXME: copy from oxid_info */;
710 HRESULT hr
= RPC_ResolveOxid(oxid
, &This
->oxid_info
);
713 CloseHandle(This
->remoting_mutex
);
714 HeapFree(GetProcessHeap(), 0, This
);
719 This
->lpVtbl
= &ClientIdentity_Vtbl
;
720 This
->lpVtblMarshal
= &ProxyMarshal_Vtbl
;
721 This
->lpVtblCliSec
= &ProxyCliSec_Vtbl
;
723 list_init(&This
->entry
);
724 list_init(&This
->interfaces
);
726 InitializeCriticalSection(&This
->cs
);
727 DEBUG_SET_CRITSEC_NAME(&This
->cs
, "proxy_manager");
729 /* the apartment the object was unmarshaled into */
732 /* the source apartment and id of the object */
738 /* the DCOM draft specification states that the SORF_NOPING flag is
739 * proxy manager specific, not ifproxy specific, so this implies that we
740 * should store the STDOBJREF flags here in the proxy manager. */
741 This
->sorflags
= sorflags
;
743 /* we create the IRemUnknown proxy on demand */
746 /* initialise these values to the weakest values and they will be
747 * overwritten in proxy_manager_set_context */
748 This
->dest_context
= MSHCTX_INPROC
;
749 This
->dest_context_data
= NULL
;
751 EnterCriticalSection(&apt
->cs
);
752 /* FIXME: we are dependent on the ordering in here to make sure a proxy's
753 * IRemUnknown proxy doesn't get destroyed before the regual proxy does
754 * because we need the IRemUnknown proxy during the destruction of the
755 * regular proxy. Ideally, we should maintain a separate list for the
756 * IRemUnknown proxies that need late destruction */
757 list_add_tail(&apt
->proxies
, &This
->entry
);
758 LeaveCriticalSection(&apt
->cs
);
760 TRACE("%p created for OXID %s, OID %s\n", This
,
761 wine_dbgstr_longlong(oxid
), wine_dbgstr_longlong(oid
));
763 *proxy_manager
= This
;
767 static inline void proxy_manager_set_context(struct proxy_manager
*This
, MSHCTX dest_context
, void *dest_context_data
)
769 MSHCTX old_dest_context
= This
->dest_context
;
770 MSHCTX new_dest_context
;
774 new_dest_context
= old_dest_context
;
775 /* "stronger" values overwrite "weaker" values. stronger values are
776 * ones that disable more optimisations */
777 switch (old_dest_context
)
780 new_dest_context
= dest_context
;
782 case MSHCTX_CROSSCTX
:
783 switch (dest_context
)
788 new_dest_context
= dest_context
;
792 switch (dest_context
)
795 case MSHCTX_CROSSCTX
:
798 new_dest_context
= dest_context
;
801 case MSHCTX_NOSHAREDMEM
:
802 switch (dest_context
)
804 case MSHCTX_DIFFERENTMACHINE
:
805 new_dest_context
= dest_context
;
815 if (old_dest_context
== new_dest_context
) break;
817 old_dest_context
= InterlockedCompareExchange((PLONG
)&This
->dest_context
, new_dest_context
, old_dest_context
);
818 } while (new_dest_context
!= old_dest_context
);
820 if (dest_context_data
)
821 InterlockedExchangePointer(&This
->dest_context_data
, dest_context_data
);
824 static HRESULT
proxy_manager_query_local_interface(struct proxy_manager
* This
, REFIID riid
, void ** ppv
)
827 struct ifproxy
* ifproxy
;
829 TRACE("%s\n", debugstr_guid(riid
));
831 if (IsEqualIID(riid
, &IID_IUnknown
) ||
832 IsEqualIID(riid
, &IID_IMultiQI
))
834 *ppv
= (void *)&This
->lpVtbl
;
835 IUnknown_AddRef((IUnknown
*)*ppv
);
838 if (IsEqualIID(riid
, &IID_IMarshal
))
840 *ppv
= (void *)&This
->lpVtblMarshal
;
841 IUnknown_AddRef((IUnknown
*)*ppv
);
844 if (IsEqualIID(riid
, &IID_IClientSecurity
))
846 *ppv
= (void *)&This
->lpVtblCliSec
;
847 IUnknown_AddRef((IUnknown
*)*ppv
);
851 hr
= proxy_manager_find_ifproxy(This
, riid
, &ifproxy
);
854 *ppv
= ifproxy
->iface
;
855 IUnknown_AddRef((IUnknown
*)*ppv
);
860 return E_NOINTERFACE
;
863 static HRESULT
proxy_manager_create_ifproxy(
864 struct proxy_manager
* This
, const STDOBJREF
*stdobjref
, REFIID riid
,
865 IRpcChannelBuffer
* channel
, struct ifproxy
** iif_out
)
868 IPSFactoryBuffer
* psfb
;
869 struct ifproxy
* ifproxy
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ifproxy
));
870 if (!ifproxy
) return E_OUTOFMEMORY
;
872 list_init(&ifproxy
->entry
);
874 ifproxy
->parent
= This
;
875 ifproxy
->stdobjref
= *stdobjref
;
876 ifproxy
->iid
= *riid
;
878 ifproxy
->proxy
= NULL
;
881 ifproxy
->chan
= channel
; /* FIXME: we should take the binding strings and construct the channel in this function */
883 /* the IUnknown interface is special because it does not have a
884 * proxy associated with the ifproxy as we handle IUnknown ourselves */
885 if (IsEqualIID(riid
, &IID_IUnknown
))
887 ifproxy
->iface
= (void *)&This
->lpVtbl
;
888 IMultiQI_AddRef((IMultiQI
*)&This
->lpVtbl
);
893 hr
= get_facbuf_for_iid(riid
, &psfb
);
896 /* important note: the outer unknown is set to the proxy manager.
897 * This ensures the COM identity rules are not violated, by having a
898 * one-to-one mapping of objects on the proxy side to objects on the
899 * stub side, no matter which interface you view the object through */
900 hr
= IPSFactoryBuffer_CreateProxy(psfb
, (IUnknown
*)&This
->lpVtbl
, riid
,
901 &ifproxy
->proxy
, &ifproxy
->iface
);
902 IPSFactoryBuffer_Release(psfb
);
904 ERR("Could not create proxy for interface %s, error 0x%08x\n",
905 debugstr_guid(riid
), hr
);
908 ERR("Could not get IPSFactoryBuffer for interface %s, error 0x%08x\n",
909 debugstr_guid(riid
), hr
);
912 hr
= IRpcProxyBuffer_Connect(ifproxy
->proxy
, ifproxy
->chan
);
917 EnterCriticalSection(&This
->cs
);
918 list_add_tail(&This
->interfaces
, &ifproxy
->entry
);
919 LeaveCriticalSection(&This
->cs
);
922 TRACE("ifproxy %p created for IPID %s, interface %s with %lu public refs\n",
923 ifproxy
, debugstr_guid(&stdobjref
->ipid
), debugstr_guid(riid
), stdobjref
->cPublicRefs
);
926 ifproxy_destroy(ifproxy
);
931 static HRESULT
proxy_manager_find_ifproxy(struct proxy_manager
* This
, REFIID riid
, struct ifproxy
** ifproxy_found
)
933 HRESULT hr
= E_NOINTERFACE
; /* assume not found */
934 struct list
* cursor
;
936 EnterCriticalSection(&This
->cs
);
937 LIST_FOR_EACH(cursor
, &This
->interfaces
)
939 struct ifproxy
* ifproxy
= LIST_ENTRY(cursor
, struct ifproxy
, entry
);
940 if (IsEqualIID(riid
, &ifproxy
->iid
))
942 *ifproxy_found
= ifproxy
;
947 LeaveCriticalSection(&This
->cs
);
952 static void proxy_manager_disconnect(struct proxy_manager
* This
)
954 struct list
* cursor
;
956 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This
->oxid
),
957 wine_dbgstr_longlong(This
->oid
));
959 EnterCriticalSection(&This
->cs
);
961 /* SORFP_NOLIFTIMEMGMT proxies (for IRemUnknown) shouldn't be
962 * disconnected - it won't do anything anyway, except cause
963 * problems for other objects that depend on this proxy always
965 if (!(This
->sorflags
& SORFP_NOLIFETIMEMGMT
))
967 LIST_FOR_EACH(cursor
, &This
->interfaces
)
969 struct ifproxy
* ifproxy
= LIST_ENTRY(cursor
, struct ifproxy
, entry
);
970 ifproxy_disconnect(ifproxy
);
974 /* apartment is being destroyed so don't keep a pointer around to it */
977 LeaveCriticalSection(&This
->cs
);
980 static HRESULT
proxy_manager_get_remunknown(struct proxy_manager
* This
, IRemUnknown
**remunk
)
983 struct apartment
*apt
;
984 BOOL called_in_original_apt
;
986 /* we don't want to try and unmarshal or use IRemUnknown if we don't want
987 * lifetime management */
988 if (This
->sorflags
& SORFP_NOLIFETIMEMGMT
)
991 apt
= COM_CurrentApt();
993 return CO_E_NOTINITIALIZED
;
995 called_in_original_apt
= This
->parent
&& (This
->parent
->oxid
== apt
->oxid
);
997 EnterCriticalSection(&This
->cs
);
998 /* only return the cached object if called from the original apartment.
999 * in future, we might want to make the IRemUnknown proxy callable from any
1000 * apartment to avoid these checks */
1001 if (This
->remunk
&& called_in_original_apt
)
1003 /* already created - return existing object */
1004 *remunk
= This
->remunk
;
1005 IRemUnknown_AddRef(*remunk
);
1007 else if (!This
->parent
)
1008 /* disconnected - we can't create IRemUnknown */
1012 STDOBJREF stdobjref
;
1013 /* Don't want IRemUnknown lifetime management as this is IRemUnknown!
1014 * We also don't care about whether or not the stub is still alive */
1015 stdobjref
.flags
= SORFP_NOLIFETIMEMGMT
| SORF_NOPING
;
1016 stdobjref
.cPublicRefs
= 1;
1017 /* oxid of destination object */
1018 stdobjref
.oxid
= This
->oxid
;
1019 /* FIXME: what should be used for the oid? The DCOM draft doesn't say */
1020 stdobjref
.oid
= (OID
)-1;
1021 stdobjref
.ipid
= This
->oxid_info
.ipidRemUnknown
;
1023 /* do the unmarshal */
1024 hr
= unmarshal_object(&stdobjref
, COM_CurrentApt(), This
->dest_context
,
1025 This
->dest_context_data
, &IID_IRemUnknown
,
1026 &This
->oxid_info
, (void**)remunk
);
1027 if (hr
== S_OK
&& called_in_original_apt
)
1029 This
->remunk
= *remunk
;
1030 IRemUnknown_AddRef(This
->remunk
);
1033 LeaveCriticalSection(&This
->cs
);
1035 TRACE("got IRemUnknown* pointer %p, hr = 0x%08x\n", *remunk
, hr
);
1040 /* destroys a proxy manager, freeing the memory it used.
1041 * Note: this function should not be called from a list iteration in the
1042 * apartment, due to the fact that it removes itself from the apartment and
1043 * it could add a proxy to IRemUnknown into the apartment. */
1044 static void proxy_manager_destroy(struct proxy_manager
* This
)
1046 struct list
* cursor
;
1048 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This
->oxid
),
1049 wine_dbgstr_longlong(This
->oid
));
1053 EnterCriticalSection(&This
->parent
->cs
);
1055 /* remove ourself from the list of proxy objects in the apartment */
1056 LIST_FOR_EACH(cursor
, &This
->parent
->proxies
)
1058 if (cursor
== &This
->entry
)
1060 list_remove(&This
->entry
);
1065 LeaveCriticalSection(&This
->parent
->cs
);
1068 /* destroy all of the interface proxies */
1069 while ((cursor
= list_head(&This
->interfaces
)))
1071 struct ifproxy
* ifproxy
= LIST_ENTRY(cursor
, struct ifproxy
, entry
);
1072 ifproxy_destroy(ifproxy
);
1075 if (This
->remunk
) IRemUnknown_Release(This
->remunk
);
1076 CoTaskMemFree(This
->oxid_info
.psa
);
1078 DEBUG_CLEAR_CRITSEC_NAME(&This
->cs
);
1079 DeleteCriticalSection(&This
->cs
);
1081 CloseHandle(This
->remoting_mutex
);
1083 HeapFree(GetProcessHeap(), 0, This
);
1086 /* finds the proxy manager corresponding to a given OXID and OID that has
1087 * been unmarshaled in the specified apartment. The caller must release the
1088 * reference to the proxy_manager when the object is no longer used. */
1089 static BOOL
find_proxy_manager(APARTMENT
* apt
, OXID oxid
, OID oid
, struct proxy_manager
** proxy_found
)
1092 struct list
* cursor
;
1094 EnterCriticalSection(&apt
->cs
);
1095 LIST_FOR_EACH(cursor
, &apt
->proxies
)
1097 struct proxy_manager
* proxy
= LIST_ENTRY(cursor
, struct proxy_manager
, entry
);
1098 if ((oxid
== proxy
->oxid
) && (oid
== proxy
->oid
))
1100 /* be careful of a race with ClientIdentity_Release, which would
1101 * cause us to return a proxy which is in the process of being
1103 if (ClientIdentity_AddRef((IMultiQI
*)&proxy
->lpVtbl
) != 0)
1105 *proxy_found
= proxy
;
1111 LeaveCriticalSection(&apt
->cs
);
1115 HRESULT
apartment_disconnectproxies(struct apartment
*apt
)
1117 struct list
* cursor
;
1119 LIST_FOR_EACH(cursor
, &apt
->proxies
)
1121 struct proxy_manager
* proxy
= LIST_ENTRY(cursor
, struct proxy_manager
, entry
);
1122 proxy_manager_disconnect(proxy
);
1128 /********************** StdMarshal implementation ****************************/
1129 typedef struct _StdMarshalImpl
1131 const IMarshalVtbl
*lpvtbl
;
1135 DWORD dwDestContext
;
1136 LPVOID pvDestContext
;
1140 static HRESULT WINAPI
1141 StdMarshalImpl_QueryInterface(LPMARSHAL iface
, REFIID riid
, LPVOID
*ppv
)
1144 if (IsEqualIID(&IID_IUnknown
, riid
) || IsEqualIID(&IID_IMarshal
, riid
))
1147 IUnknown_AddRef(iface
);
1150 FIXME("No interface for %s.\n", debugstr_guid(riid
));
1151 return E_NOINTERFACE
;
1155 StdMarshalImpl_AddRef(LPMARSHAL iface
)
1157 StdMarshalImpl
*This
= (StdMarshalImpl
*)iface
;
1158 return InterlockedIncrement(&This
->ref
);
1162 StdMarshalImpl_Release(LPMARSHAL iface
)
1164 StdMarshalImpl
*This
= (StdMarshalImpl
*)iface
;
1165 ULONG ref
= InterlockedDecrement(&This
->ref
);
1167 if (!ref
) HeapFree(GetProcessHeap(),0,This
);
1171 static HRESULT WINAPI
1172 StdMarshalImpl_GetUnmarshalClass(
1173 LPMARSHAL iface
, REFIID riid
, void* pv
, DWORD dwDestContext
,
1174 void* pvDestContext
, DWORD mshlflags
, CLSID
* pCid
)
1176 *pCid
= CLSID_DfMarshal
;
1180 static HRESULT WINAPI
1181 StdMarshalImpl_GetMarshalSizeMax(
1182 LPMARSHAL iface
, REFIID riid
, void* pv
, DWORD dwDestContext
,
1183 void* pvDestContext
, DWORD mshlflags
, DWORD
* pSize
)
1185 *pSize
= sizeof(STDOBJREF
);
1189 static HRESULT WINAPI
1190 StdMarshalImpl_MarshalInterface(
1191 LPMARSHAL iface
, IStream
*pStm
,REFIID riid
, void* pv
, DWORD dwDestContext
,
1192 void* pvDestContext
, DWORD mshlflags
)
1194 STDOBJREF stdobjref
;
1197 APARTMENT
*apt
= COM_CurrentApt();
1199 TRACE("(...,%s,...)\n", debugstr_guid(riid
));
1203 ERR("Apartment not initialized\n");
1204 return CO_E_NOTINITIALIZED
;
1207 /* make sure this apartment can be reached from other threads / processes */
1208 RPC_StartRemoting(apt
);
1210 hres
= marshal_object(apt
, &stdobjref
, riid
, (IUnknown
*)pv
, mshlflags
);
1213 ERR("Failed to create ifstub, hres=0x%x\n", hres
);
1217 hres
= IStream_Write(pStm
, &stdobjref
, sizeof(stdobjref
), &res
);
1218 if (hres
) return hres
;
1223 /* helper for StdMarshalImpl_UnmarshalInterface - does the unmarshaling with
1224 * no questions asked about the rules surrounding same-apartment unmarshals
1225 * and table marshaling */
1226 static HRESULT
unmarshal_object(const STDOBJREF
*stdobjref
, APARTMENT
*apt
,
1227 MSHCTX dest_context
, void *dest_context_data
,
1228 REFIID riid
, const OXID_INFO
*oxid_info
,
1231 struct proxy_manager
*proxy_manager
= NULL
;
1236 TRACE("stdobjref:\n\tflags = %04lx\n\tcPublicRefs = %ld\n\toxid = %s\n\toid = %s\n\tipid = %s\n",
1237 stdobjref
->flags
, stdobjref
->cPublicRefs
,
1238 wine_dbgstr_longlong(stdobjref
->oxid
),
1239 wine_dbgstr_longlong(stdobjref
->oid
),
1240 debugstr_guid(&stdobjref
->ipid
));
1242 /* create a new proxy manager if one doesn't already exist for the
1244 if (!find_proxy_manager(apt
, stdobjref
->oxid
, stdobjref
->oid
, &proxy_manager
))
1246 hr
= proxy_manager_construct(apt
, stdobjref
->flags
,
1247 stdobjref
->oxid
, stdobjref
->oid
, oxid_info
,
1251 TRACE("proxy manager already created, using\n");
1255 struct ifproxy
* ifproxy
;
1257 proxy_manager_set_context(proxy_manager
, dest_context
, dest_context_data
);
1259 hr
= proxy_manager_find_ifproxy(proxy_manager
, riid
, &ifproxy
);
1260 if (hr
== E_NOINTERFACE
)
1262 IRpcChannelBuffer
*chanbuf
;
1263 hr
= RPC_CreateClientChannel(&stdobjref
->oxid
, &stdobjref
->ipid
,
1264 &proxy_manager
->oxid_info
,
1265 proxy_manager
->dest_context
,
1266 proxy_manager
->dest_context_data
,
1269 hr
= proxy_manager_create_ifproxy(proxy_manager
, stdobjref
,
1270 riid
, chanbuf
, &ifproxy
);
1273 IUnknown_AddRef((IUnknown
*)ifproxy
->iface
);
1277 InterlockedExchangeAdd((LONG
*)&ifproxy
->refs
, stdobjref
->cPublicRefs
);
1278 /* get at least one external reference to the object to keep it alive */
1279 hr
= ifproxy_get_public_ref(ifproxy
);
1281 ifproxy_destroy(ifproxy
);
1285 *object
= ifproxy
->iface
;
1288 /* release our reference to the proxy manager - the client/apartment
1289 * will hold on to the remaining reference for us */
1290 if (proxy_manager
) ClientIdentity_Release((IMultiQI
*)&proxy_manager
->lpVtbl
);
1295 static HRESULT WINAPI
1296 StdMarshalImpl_UnmarshalInterface(LPMARSHAL iface
, IStream
*pStm
, REFIID riid
, void **ppv
)
1298 StdMarshalImpl
*This
= (StdMarshalImpl
*)iface
;
1299 struct stub_manager
*stubmgr
= NULL
;
1300 STDOBJREF stdobjref
;
1303 APARTMENT
*apt
= COM_CurrentApt();
1304 APARTMENT
*stub_apt
;
1307 TRACE("(...,%s,....)\n", debugstr_guid(riid
));
1309 /* we need an apartment to unmarshal into */
1312 ERR("Apartment not initialized\n");
1313 return CO_E_NOTINITIALIZED
;
1316 /* read STDOBJREF from wire */
1317 hres
= IStream_Read(pStm
, &stdobjref
, sizeof(stdobjref
), &res
);
1318 if (hres
) return STG_E_READFAULT
;
1320 hres
= apartment_getoxid(apt
, &oxid
);
1321 if (hres
) return hres
;
1323 /* check if we're marshalling back to ourselves */
1324 if ((oxid
== stdobjref
.oxid
) && (stubmgr
= get_stub_manager(apt
, stdobjref
.oid
)))
1326 TRACE("Unmarshalling object marshalled in same apartment for iid %s, "
1327 "returning original object %p\n", debugstr_guid(riid
), stubmgr
->object
);
1329 hres
= IUnknown_QueryInterface(stubmgr
->object
, riid
, ppv
);
1331 /* unref the ifstub. FIXME: only do this on success? */
1332 if (!stub_manager_is_table_marshaled(stubmgr
, &stdobjref
.ipid
))
1333 stub_manager_ext_release(stubmgr
, stdobjref
.cPublicRefs
, TRUE
);
1335 stub_manager_int_release(stubmgr
);
1339 /* notify stub manager about unmarshal if process-local object.
1340 * note: if the oxid is not found then we and native will quite happily
1341 * ignore table marshaling and normal marshaling rules regarding number of
1342 * unmarshals, etc, but if you abuse these rules then your proxy could end
1343 * up returning RPC_E_DISCONNECTED. */
1344 if ((stub_apt
= apartment_findfromoxid(stdobjref
.oxid
, TRUE
)))
1346 if ((stubmgr
= get_stub_manager(stub_apt
, stdobjref
.oid
)))
1348 if (!stub_manager_notify_unmarshal(stubmgr
, &stdobjref
.ipid
))
1349 hres
= CO_E_OBJNOTCONNECTED
;
1353 WARN("Couldn't find object for OXID %s, OID %s, assuming disconnected\n",
1354 wine_dbgstr_longlong(stdobjref
.oxid
),
1355 wine_dbgstr_longlong(stdobjref
.oid
));
1356 hres
= CO_E_OBJNOTCONNECTED
;
1360 TRACE("Treating unmarshal from OXID %s as inter-process\n",
1361 wine_dbgstr_longlong(stdobjref
.oxid
));
1364 hres
= unmarshal_object(&stdobjref
, apt
, This
->dwDestContext
,
1365 This
->pvDestContext
, riid
,
1366 stubmgr
? &stubmgr
->oxid_info
: NULL
, ppv
);
1368 if (stubmgr
) stub_manager_int_release(stubmgr
);
1369 if (stub_apt
) apartment_release(stub_apt
);
1371 if (hres
) WARN("Failed with error 0x%08x\n", hres
);
1372 else TRACE("Successfully created proxy %p\n", *ppv
);
1377 static HRESULT WINAPI
1378 StdMarshalImpl_ReleaseMarshalData(LPMARSHAL iface
, IStream
*pStm
)
1380 STDOBJREF stdobjref
;
1383 struct stub_manager
*stubmgr
;
1386 TRACE("iface=%p, pStm=%p\n", iface
, pStm
);
1388 hres
= IStream_Read(pStm
, &stdobjref
, sizeof(stdobjref
), &res
);
1389 if (hres
) return STG_E_READFAULT
;
1391 TRACE("oxid = %s, oid = %s, ipid = %s\n",
1392 wine_dbgstr_longlong(stdobjref
.oxid
),
1393 wine_dbgstr_longlong(stdobjref
.oid
),
1394 wine_dbgstr_guid(&stdobjref
.ipid
));
1396 if (!(apt
= apartment_findfromoxid(stdobjref
.oxid
, TRUE
)))
1398 WARN("Could not map OXID %s to apartment object\n",
1399 wine_dbgstr_longlong(stdobjref
.oxid
));
1400 return RPC_E_INVALID_OBJREF
;
1403 if (!(stubmgr
= get_stub_manager(apt
, stdobjref
.oid
)))
1405 ERR("could not map object ID to stub manager, oxid=%s, oid=%s\n",
1406 wine_dbgstr_longlong(stdobjref
.oxid
), wine_dbgstr_longlong(stdobjref
.oid
));
1407 return RPC_E_INVALID_OBJREF
;
1410 stub_manager_release_marshal_data(stubmgr
, stdobjref
.cPublicRefs
, &stdobjref
.ipid
);
1412 stub_manager_int_release(stubmgr
);
1413 apartment_release(apt
);
1418 static HRESULT WINAPI
1419 StdMarshalImpl_DisconnectObject(LPMARSHAL iface
, DWORD dwReserved
)
1421 FIXME("(), stub!\n");
1425 static const IMarshalVtbl VT_StdMarshal
=
1427 StdMarshalImpl_QueryInterface
,
1428 StdMarshalImpl_AddRef
,
1429 StdMarshalImpl_Release
,
1430 StdMarshalImpl_GetUnmarshalClass
,
1431 StdMarshalImpl_GetMarshalSizeMax
,
1432 StdMarshalImpl_MarshalInterface
,
1433 StdMarshalImpl_UnmarshalInterface
,
1434 StdMarshalImpl_ReleaseMarshalData
,
1435 StdMarshalImpl_DisconnectObject
1438 static HRESULT
StdMarshalImpl_Construct(REFIID riid
, void** ppvObject
)
1440 StdMarshalImpl
* pStdMarshal
=
1441 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(StdMarshalImpl
));
1443 return E_OUTOFMEMORY
;
1444 pStdMarshal
->lpvtbl
= &VT_StdMarshal
;
1445 pStdMarshal
->ref
= 0;
1446 return IMarshal_QueryInterface((IMarshal
*)pStdMarshal
, riid
, ppvObject
);
1449 /***********************************************************************
1450 * CoGetStandardMarshal [OLE32.@]
1452 * Gets or creates a standard marshal object.
1455 * riid [I] Interface identifier of the pUnk object.
1456 * pUnk [I] Optional. Object to get the marshal object for.
1457 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1458 * pvDestContext [I] Reserved. Must be NULL.
1459 * mshlflags [I] Flags affecting the marshaling process.
1460 * ppMarshal [O] Address where marshal object will be stored.
1464 * Failure: HRESULT code.
1468 * The function retrieves the IMarshal object associated with an object if
1469 * that object is currently an active stub, otherwise a new marshal object is
1472 HRESULT WINAPI
CoGetStandardMarshal(REFIID riid
, IUnknown
*pUnk
,
1473 DWORD dwDestContext
, LPVOID pvDestContext
,
1474 DWORD mshlflags
, LPMARSHAL
*ppMarshal
)
1480 FIXME("(%s,NULL,%x,%p,%x,%p), unimplemented yet.\n",
1481 debugstr_guid(riid
),dwDestContext
,pvDestContext
,mshlflags
,ppMarshal
);
1484 TRACE("(%s,%p,%x,%p,%x,%p)\n",
1485 debugstr_guid(riid
),pUnk
,dwDestContext
,pvDestContext
,mshlflags
,ppMarshal
);
1486 *ppMarshal
= HeapAlloc(GetProcessHeap(),0,sizeof(StdMarshalImpl
));
1487 dm
= (StdMarshalImpl
*) *ppMarshal
;
1488 if (!dm
) return E_FAIL
;
1489 dm
->lpvtbl
= &VT_StdMarshal
;
1493 dm
->dwDestContext
= dwDestContext
;
1494 dm
->pvDestContext
= pvDestContext
;
1495 dm
->mshlflags
= mshlflags
;
1499 /***********************************************************************
1500 * get_marshaler [internal]
1502 * Retrieves an IMarshal interface for an object.
1504 static HRESULT
get_marshaler(REFIID riid
, IUnknown
*pUnk
, DWORD dwDestContext
,
1505 void *pvDestContext
, DWORD mshlFlags
,
1506 LPMARSHAL
*pMarshal
)
1512 hr
= IUnknown_QueryInterface(pUnk
, &IID_IMarshal
, (LPVOID
*)pMarshal
);
1514 hr
= CoGetStandardMarshal(riid
, pUnk
, dwDestContext
, pvDestContext
,
1515 mshlFlags
, pMarshal
);
1519 /***********************************************************************
1520 * get_unmarshaler_from_stream [internal]
1522 * Creates an IMarshal* object according to the data marshaled to the stream.
1523 * The function leaves the stream pointer at the start of the data written
1524 * to the stream by the IMarshal* object.
1526 static HRESULT
get_unmarshaler_from_stream(IStream
*stream
, IMarshal
**marshal
, IID
*iid
)
1532 /* read common OBJREF header */
1533 hr
= IStream_Read(stream
, &objref
, FIELD_OFFSET(OBJREF
, u_objref
), &res
);
1534 if (hr
|| (res
!= FIELD_OFFSET(OBJREF
, u_objref
)))
1536 ERR("Failed to read common OBJREF header, 0x%08x\n", hr
);
1537 return STG_E_READFAULT
;
1540 /* sanity check on header */
1541 if (objref
.signature
!= OBJREF_SIGNATURE
)
1543 ERR("Bad OBJREF signature 0x%08lx\n", objref
.signature
);
1544 return RPC_E_INVALID_OBJREF
;
1547 if (iid
) *iid
= objref
.iid
;
1549 /* FIXME: handler marshaling */
1550 if (objref
.flags
& OBJREF_STANDARD
)
1552 TRACE("Using standard unmarshaling\n");
1553 hr
= StdMarshalImpl_Construct(&IID_IMarshal
, (LPVOID
*)marshal
);
1555 else if (objref
.flags
& OBJREF_CUSTOM
)
1557 ULONG custom_header_size
= FIELD_OFFSET(OBJREF
, u_objref
.u_custom
.pData
) -
1558 FIELD_OFFSET(OBJREF
, u_objref
.u_custom
);
1559 TRACE("Using custom unmarshaling\n");
1560 /* read constant sized OR_CUSTOM data from stream */
1561 hr
= IStream_Read(stream
, &objref
.u_objref
.u_custom
,
1562 custom_header_size
, &res
);
1563 if (hr
|| (res
!= custom_header_size
))
1565 ERR("Failed to read OR_CUSTOM header, 0x%08x\n", hr
);
1566 return STG_E_READFAULT
;
1568 /* now create the marshaler specified in the stream */
1569 hr
= CoCreateInstance(&objref
.u_objref
.u_custom
.clsid
, NULL
,
1570 CLSCTX_INPROC_SERVER
, &IID_IMarshal
,
1575 FIXME("Invalid or unimplemented marshaling type specified: %lx\n",
1577 return RPC_E_INVALID_OBJREF
;
1581 ERR("Failed to create marshal, 0x%08x\n", hr
);
1586 /***********************************************************************
1587 * CoGetMarshalSizeMax [OLE32.@]
1589 * Gets the maximum amount of data that will be needed by a marshal.
1592 * pulSize [O] Address where maximum marshal size will be stored.
1593 * riid [I] Identifier of the interface to marshal.
1594 * pUnk [I] Pointer to the object to marshal.
1595 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1596 * pvDestContext [I] Reserved. Must be NULL.
1597 * mshlFlags [I] Flags that affect the marshaling. See CoMarshalInterface().
1601 * Failure: HRESULT code.
1604 * CoMarshalInterface().
1606 HRESULT WINAPI
CoGetMarshalSizeMax(ULONG
*pulSize
, REFIID riid
, IUnknown
*pUnk
,
1607 DWORD dwDestContext
, void *pvDestContext
,
1612 CLSID marshaler_clsid
;
1614 hr
= get_marshaler(riid
, pUnk
, dwDestContext
, pvDestContext
, mshlFlags
, &pMarshal
);
1618 hr
= IMarshal_GetUnmarshalClass(pMarshal
, riid
, pUnk
, dwDestContext
,
1619 pvDestContext
, mshlFlags
, &marshaler_clsid
);
1622 ERR("IMarshal::GetUnmarshalClass failed, 0x%08x\n", hr
);
1623 IMarshal_Release(pMarshal
);
1627 hr
= IMarshal_GetMarshalSizeMax(pMarshal
, riid
, pUnk
, dwDestContext
,
1628 pvDestContext
, mshlFlags
, pulSize
);
1629 if (IsEqualCLSID(&marshaler_clsid
, &CLSID_DfMarshal
))
1630 /* add on the size of the common header */
1631 *pulSize
+= FIELD_OFFSET(OBJREF
, u_objref
);
1633 /* custom marshaling: add on the size of the whole OBJREF structure
1634 * like native does */
1635 *pulSize
+= sizeof(OBJREF
);
1637 IMarshal_Release(pMarshal
);
1642 static void dump_MSHLFLAGS(MSHLFLAGS flags
)
1644 if (flags
& MSHLFLAGS_TABLESTRONG
)
1645 TRACE(" MSHLFLAGS_TABLESTRONG");
1646 if (flags
& MSHLFLAGS_TABLEWEAK
)
1647 TRACE(" MSHLFLAGS_TABLEWEAK");
1648 if (!(flags
& (MSHLFLAGS_TABLESTRONG
|MSHLFLAGS_TABLEWEAK
)))
1649 TRACE(" MSHLFLAGS_NORMAL");
1650 if (flags
& MSHLFLAGS_NOPING
)
1651 TRACE(" MSHLFLAGS_NOPING");
1654 /***********************************************************************
1655 * CoMarshalInterface [OLE32.@]
1657 * Marshals an interface into a stream so that the object can then be
1658 * unmarshaled from another COM apartment and used remotely.
1661 * pStream [I] Stream the object will be marshaled into.
1662 * riid [I] Identifier of the interface to marshal.
1663 * pUnk [I] Pointer to the object to marshal.
1664 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1665 * pvDestContext [I] Reserved. Must be NULL.
1666 * mshlFlags [I] Flags that affect the marshaling. See notes.
1670 * Failure: HRESULT code.
1674 * The mshlFlags parameter can take one or more of the following flags:
1675 *| MSHLFLAGS_NORMAL - Unmarshal once, releases stub on last proxy release.
1676 *| MSHLFLAGS_TABLESTRONG - Unmarshal many, release when CoReleaseMarshalData() called.
1677 *| MSHLFLAGS_TABLEWEAK - Unmarshal many, releases stub on last proxy release.
1678 *| MSHLFLAGS_NOPING - No automatic garbage collection (and so reduces network traffic).
1680 * If a marshaled object is not unmarshaled, then CoReleaseMarshalData() must
1681 * be called in order to release the resources used in the marshaling.
1684 * CoUnmarshalInterface(), CoReleaseMarshalData().
1686 HRESULT WINAPI
CoMarshalInterface(IStream
*pStream
, REFIID riid
, IUnknown
*pUnk
,
1687 DWORD dwDestContext
, void *pvDestContext
,
1691 CLSID marshaler_clsid
;
1695 TRACE("(%p, %s, %p, %x, %p,", pStream
, debugstr_guid(riid
), pUnk
,
1696 dwDestContext
, pvDestContext
);
1697 dump_MSHLFLAGS(mshlFlags
);
1700 if (!pUnk
|| !pStream
)
1701 return E_INVALIDARG
;
1703 objref
.signature
= OBJREF_SIGNATURE
;
1706 /* get the marshaler for the specified interface */
1707 hr
= get_marshaler(riid
, pUnk
, dwDestContext
, pvDestContext
, mshlFlags
, &pMarshal
);
1710 ERR("Failed to get marshaller, 0x%08x\n", hr
);
1714 hr
= IMarshal_GetUnmarshalClass(pMarshal
, riid
, pUnk
, dwDestContext
,
1715 pvDestContext
, mshlFlags
, &marshaler_clsid
);
1718 ERR("IMarshal::GetUnmarshalClass failed, 0x%08x\n", hr
);
1722 /* FIXME: implement handler marshaling too */
1723 if (IsEqualCLSID(&marshaler_clsid
, &CLSID_DfMarshal
))
1725 TRACE("Using standard marshaling\n");
1726 objref
.flags
= OBJREF_STANDARD
;
1728 /* write the common OBJREF header to the stream */
1729 hr
= IStream_Write(pStream
, &objref
, FIELD_OFFSET(OBJREF
, u_objref
), NULL
);
1732 ERR("Failed to write OBJREF header to stream, 0x%08x\n", hr
);
1738 TRACE("Using custom marshaling\n");
1739 objref
.flags
= OBJREF_CUSTOM
;
1740 objref
.u_objref
.u_custom
.clsid
= marshaler_clsid
;
1741 objref
.u_objref
.u_custom
.cbExtension
= 0;
1742 objref
.u_objref
.u_custom
.size
= 0;
1743 hr
= IMarshal_GetMarshalSizeMax(pMarshal
, riid
, pUnk
, dwDestContext
,
1744 pvDestContext
, mshlFlags
,
1745 &objref
.u_objref
.u_custom
.size
);
1748 ERR("Failed to get max size of marshal data, error 0x%08x\n", hr
);
1751 /* write constant sized common header and OR_CUSTOM data into stream */
1752 hr
= IStream_Write(pStream
, &objref
,
1753 FIELD_OFFSET(OBJREF
, u_objref
.u_custom
.pData
), NULL
);
1756 ERR("Failed to write OR_CUSTOM header to stream with 0x%08x\n", hr
);
1761 TRACE("Calling IMarshal::MarshalInterace\n");
1762 /* call helper object to do the actual marshaling */
1763 hr
= IMarshal_MarshalInterface(pMarshal
, pStream
, riid
, pUnk
, dwDestContext
,
1764 pvDestContext
, mshlFlags
);
1768 ERR("Failed to marshal the interface %s, %x\n", debugstr_guid(riid
), hr
);
1773 IMarshal_Release(pMarshal
);
1775 TRACE("completed with hr 0x%08x\n", hr
);
1780 /***********************************************************************
1781 * CoUnmarshalInterface [OLE32.@]
1783 * Unmarshals an object from a stream by creating a proxy to the remote
1784 * object, if necessary.
1788 * pStream [I] Stream containing the marshaled object.
1789 * riid [I] Interface identifier of the object to create a proxy to.
1790 * ppv [O] Address where proxy will be stored.
1795 * Failure: HRESULT code.
1798 * CoMarshalInterface().
1800 HRESULT WINAPI
CoUnmarshalInterface(IStream
*pStream
, REFIID riid
, LPVOID
*ppv
)
1807 TRACE("(%p, %s, %p)\n", pStream
, debugstr_guid(riid
), ppv
);
1809 if (!pStream
|| !ppv
)
1810 return E_INVALIDARG
;
1812 hr
= get_unmarshaler_from_stream(pStream
, &pMarshal
, &iid
);
1816 /* call the helper object to do the actual unmarshaling */
1817 hr
= IMarshal_UnmarshalInterface(pMarshal
, pStream
, &iid
, (LPVOID
*)&object
);
1819 ERR("IMarshal::UnmarshalInterface failed, 0x%08x\n", hr
);
1823 /* IID_NULL means use the interface ID of the marshaled object */
1824 if (!IsEqualIID(riid
, &IID_NULL
) && !IsEqualIID(riid
, &iid
))
1826 TRACE("requested interface != marshalled interface, additional QI needed\n");
1827 hr
= IUnknown_QueryInterface(object
, riid
, ppv
);
1829 ERR("Couldn't query for interface %s, hr = 0x%08x\n",
1830 debugstr_guid(riid
), hr
);
1831 IUnknown_Release(object
);
1839 IMarshal_Release(pMarshal
);
1841 TRACE("completed with hr 0x%x\n", hr
);
1846 /***********************************************************************
1847 * CoReleaseMarshalData [OLE32.@]
1849 * Releases resources associated with an object that has been marshaled into
1854 * pStream [I] The stream that the object has been marshaled into.
1858 * Failure: HRESULT error code.
1862 * Call this function to release resources associated with a normal or
1863 * table-weak marshal that will not be unmarshaled, and all table-strong
1864 * marshals when they are no longer needed.
1867 * CoMarshalInterface(), CoUnmarshalInterface().
1869 HRESULT WINAPI
CoReleaseMarshalData(IStream
*pStream
)
1874 TRACE("(%p)\n", pStream
);
1876 hr
= get_unmarshaler_from_stream(pStream
, &pMarshal
, NULL
);
1880 /* call the helper object to do the releasing of marshal data */
1881 hr
= IMarshal_ReleaseMarshalData(pMarshal
, pStream
);
1883 ERR("IMarshal::ReleaseMarshalData failed with error 0x%08x\n", hr
);
1885 IMarshal_Release(pMarshal
);
1890 /***********************************************************************
1891 * CoMarshalInterThreadInterfaceInStream [OLE32.@]
1893 * Marshal an interface across threads in the same process.
1896 * riid [I] Identifier of the interface to be marshalled.
1897 * pUnk [I] Pointer to IUnknown-derived interface that will be marshalled.
1898 * ppStm [O] Pointer to IStream object that is created and then used to store the marshalled interface.
1902 * Failure: E_OUTOFMEMORY and other COM error codes
1905 * CoMarshalInterface(), CoUnmarshalInterface() and CoGetInterfaceAndReleaseStream()
1907 HRESULT WINAPI
CoMarshalInterThreadInterfaceInStream(
1908 REFIID riid
, LPUNKNOWN pUnk
, LPSTREAM
* ppStm
)
1910 ULARGE_INTEGER xpos
;
1911 LARGE_INTEGER seekto
;
1914 TRACE("(%s, %p, %p)\n",debugstr_guid(riid
), pUnk
, ppStm
);
1916 hres
= CreateStreamOnHGlobal(NULL
, TRUE
, ppStm
);
1917 if (FAILED(hres
)) return hres
;
1918 hres
= CoMarshalInterface(*ppStm
, riid
, pUnk
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_NORMAL
);
1920 if (SUCCEEDED(hres
))
1922 memset(&seekto
, 0, sizeof(seekto
));
1923 IStream_Seek(*ppStm
, seekto
, STREAM_SEEK_SET
, &xpos
);
1927 IStream_Release(*ppStm
);
1934 /***********************************************************************
1935 * CoGetInterfaceAndReleaseStream [OLE32.@]
1937 * Unmarshalls an interface from a stream and then releases the stream.
1940 * pStm [I] Stream that contains the marshalled interface.
1941 * riid [I] Interface identifier of the object to unmarshall.
1942 * ppv [O] Address of pointer where the requested interface object will be stored.
1946 * Failure: A COM error code
1949 * CoMarshalInterThreadInterfaceInStream() and CoUnmarshalInterface()
1951 HRESULT WINAPI
CoGetInterfaceAndReleaseStream(LPSTREAM pStm
, REFIID riid
,
1956 TRACE("(%p, %s, %p)\n", pStm
, debugstr_guid(riid
), ppv
);
1958 if(!pStm
) return E_INVALIDARG
;
1959 hres
= CoUnmarshalInterface(pStm
, riid
, ppv
);
1960 IStream_Release(pStm
);
1964 static HRESULT WINAPI
StdMarshalCF_QueryInterface(LPCLASSFACTORY iface
,
1965 REFIID riid
, LPVOID
*ppv
)
1968 if (IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IClassFactory
))
1970 *ppv
= (LPVOID
)iface
;
1973 return E_NOINTERFACE
;
1976 static ULONG WINAPI
StdMarshalCF_AddRef(LPCLASSFACTORY iface
)
1978 return 2; /* non-heap based object */
1981 static ULONG WINAPI
StdMarshalCF_Release(LPCLASSFACTORY iface
)
1983 return 1; /* non-heap based object */
1986 static HRESULT WINAPI
StdMarshalCF_CreateInstance(LPCLASSFACTORY iface
,
1987 LPUNKNOWN pUnk
, REFIID riid
, LPVOID
*ppv
)
1989 if (IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IMarshal
))
1990 return StdMarshalImpl_Construct(riid
, ppv
);
1992 FIXME("(%s), not supported.\n",debugstr_guid(riid
));
1993 return E_NOINTERFACE
;
1996 static HRESULT WINAPI
StdMarshalCF_LockServer(LPCLASSFACTORY iface
, BOOL fLock
)
1998 FIXME("(%d), stub!\n",fLock
);
2002 static const IClassFactoryVtbl StdMarshalCFVtbl
=
2004 StdMarshalCF_QueryInterface
,
2005 StdMarshalCF_AddRef
,
2006 StdMarshalCF_Release
,
2007 StdMarshalCF_CreateInstance
,
2008 StdMarshalCF_LockServer
2010 static const IClassFactoryVtbl
*StdMarshalCF
= &StdMarshalCFVtbl
;
2012 HRESULT
MARSHAL_GetStandardMarshalCF(LPVOID
*ppv
)
2014 *ppv
= &StdMarshalCF
;
2018 /***********************************************************************
2019 * CoMarshalHresult [OLE32.@]
2021 * Marshals an HRESULT value into a stream.
2024 * pStm [I] Stream that hresult will be marshalled into.
2025 * hresult [I] HRESULT to be marshalled.
2029 * Failure: A COM error code
2032 * CoUnmarshalHresult().
2034 HRESULT WINAPI
CoMarshalHresult(LPSTREAM pStm
, HRESULT hresult
)
2036 return IStream_Write(pStm
, &hresult
, sizeof(hresult
), NULL
);
2039 /***********************************************************************
2040 * CoUnmarshalHresult [OLE32.@]
2042 * Unmarshals an HRESULT value from a stream.
2045 * pStm [I] Stream that hresult will be unmarshalled from.
2046 * phresult [I] Pointer to HRESULT where the value will be unmarshalled to.
2050 * Failure: A COM error code
2053 * CoMarshalHresult().
2055 HRESULT WINAPI
CoUnmarshalHresult(LPSTREAM pStm
, HRESULT
* phresult
)
2057 return IStream_Read(pStm
, phresult
, sizeof(*phresult
), NULL
);