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
42 #include "wine/unicode.h"
44 #include "compobj_private.h"
46 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
50 extern const CLSID CLSID_DfMarshal
;
52 /* number of refs given out for normal marshaling */
53 #define NORMALEXTREFS 5
55 /* private flag indicating that the caller does not want to notify the stub
56 * when the proxy disconnects or is destroyed */
57 #define SORFP_NOLIFETIMEMGMT SORF_OXRES1
59 static HRESULT
unmarshal_object(const STDOBJREF
*stdobjref
, APARTMENT
*apt
,
60 MSHCTX dest_context
, void *dest_context_data
,
61 REFIID riid
, void **object
);
63 /* Marshalling just passes a unique identifier to the remote client,
64 * that makes it possible to find the passed interface again.
66 * So basically we need a set of values that make it unique.
68 * Note that the IUnknown_QI(ob,xiid,&ppv) always returns the SAME ppv value!
70 * A triple is used: OXID (apt id), OID (stub manager id),
71 * IPID (interface ptr/stub id).
73 * OXIDs identify an apartment and are network scoped
74 * OIDs identify a stub manager and are apartment scoped
75 * IPIDs identify an interface stub and are apartment scoped
78 inline static HRESULT
get_facbuf_for_iid(REFIID riid
, IPSFactoryBuffer
**facbuf
)
83 if ((hr
= CoGetPSClsid(riid
, &clsid
)))
85 return CoGetClassObject(&clsid
, CLSCTX_INPROC_SERVER
, NULL
,
86 &IID_IPSFactoryBuffer
, (LPVOID
*)facbuf
);
89 /* marshals an object into a STDOBJREF structure */
90 HRESULT
marshal_object(APARTMENT
*apt
, STDOBJREF
*stdobjref
, REFIID riid
, IUnknown
*object
, MSHLFLAGS mshlflags
)
92 struct stub_manager
*manager
;
93 struct ifstub
*ifstub
;
95 IRpcStubBuffer
*stub
= NULL
;
97 IUnknown
*iobject
= NULL
; /* object of type riid */
99 hr
= apartment_getoxid(apt
, &stdobjref
->oxid
);
103 hr
= apartment_createwindowifneeded(apt
);
107 hr
= IUnknown_QueryInterface(object
, riid
, (void **)&iobject
);
110 ERR("object doesn't expose interface %s, failing with error 0x%08lx\n",
111 debugstr_guid(riid
), hr
);
112 return E_NOINTERFACE
;
115 /* IUnknown doesn't require a stub buffer, because it never goes out on
117 if (!IsEqualIID(riid
, &IID_IUnknown
))
119 IPSFactoryBuffer
*psfb
;
121 hr
= get_facbuf_for_iid(riid
, &psfb
);
124 ERR("couldn't get IPSFactory buffer for interface %s\n", debugstr_guid(riid
));
125 IUnknown_Release(iobject
);
129 hr
= IPSFactoryBuffer_CreateStub(psfb
, riid
, iobject
, &stub
);
130 IPSFactoryBuffer_Release(psfb
);
133 ERR("Failed to create an IRpcStubBuffer from IPSFactory for %s\n", debugstr_guid(riid
));
134 IUnknown_Release(iobject
);
139 if (mshlflags
& MSHLFLAGS_NOPING
)
140 stdobjref
->flags
= SORF_NOPING
;
142 stdobjref
->flags
= SORF_NULL
;
144 if ((manager
= get_stub_manager_from_object(apt
, object
)))
145 TRACE("registering new ifstub on pre-existing manager\n");
148 TRACE("constructing new stub manager\n");
150 manager
= new_stub_manager(apt
, object
);
153 if (stub
) IRpcStubBuffer_Release(stub
);
154 IUnknown_Release(iobject
);
155 return E_OUTOFMEMORY
;
158 stdobjref
->oid
= manager
->oid
;
160 tablemarshal
= ((mshlflags
& MSHLFLAGS_TABLESTRONG
) || (mshlflags
& MSHLFLAGS_TABLEWEAK
));
162 /* make sure ifstub that we are creating is unique */
163 ifstub
= stub_manager_find_ifstub(manager
, riid
, mshlflags
);
166 ifstub
= stub_manager_new_ifstub(manager
, stub
, iobject
, riid
, mshlflags
);
167 IUnknown_Release(iobject
);
168 if (stub
) IRpcStubBuffer_Release(stub
);
171 stub_manager_int_release(manager
);
172 /* FIXME: should we do another release to completely destroy the
174 return E_OUTOFMEMORY
;
180 stdobjref
->cPublicRefs
= NORMALEXTREFS
;
181 stub_manager_ext_addref(manager
, stdobjref
->cPublicRefs
);
185 stdobjref
->cPublicRefs
= 0;
186 if (mshlflags
& MSHLFLAGS_TABLESTRONG
)
187 stub_manager_ext_addref(manager
, 1);
190 /* FIXME: check return value */
191 RPC_RegisterInterface(riid
);
193 stdobjref
->ipid
= ifstub
->ipid
;
195 stub_manager_int_release(manager
);
201 /* Client-side identity of the server object */
203 static HRESULT
proxy_manager_get_remunknown(struct proxy_manager
* This
, IRemUnknown
**remunk
);
204 static void proxy_manager_destroy(struct proxy_manager
* This
);
205 static HRESULT
proxy_manager_find_ifproxy(struct proxy_manager
* This
, REFIID riid
, struct ifproxy
** ifproxy_found
);
206 static HRESULT
proxy_manager_query_local_interface(struct proxy_manager
* This
, REFIID riid
, void ** ppv
);
208 static HRESULT WINAPI
ClientIdentity_QueryInterface(IMultiQI
* iface
, REFIID riid
, void ** ppv
)
213 TRACE("%s\n", debugstr_guid(riid
));
216 hr
= IMultiQI_QueryMultipleInterfaces(iface
, 1, &mqi
);
217 *ppv
= (void *)mqi
.pItf
;
222 static ULONG WINAPI
ClientIdentity_AddRef(IMultiQI
* iface
)
224 struct proxy_manager
* This
= (struct proxy_manager
*)iface
;
225 TRACE("%p - before %ld\n", iface
, This
->refs
);
226 return InterlockedIncrement(&This
->refs
);
229 static ULONG WINAPI
ClientIdentity_Release(IMultiQI
* iface
)
231 struct proxy_manager
* This
= (struct proxy_manager
*)iface
;
232 ULONG refs
= InterlockedDecrement(&This
->refs
);
233 TRACE("%p - after %ld\n", iface
, refs
);
235 proxy_manager_destroy(This
);
239 static HRESULT WINAPI
ClientIdentity_QueryMultipleInterfaces(IMultiQI
*iface
, ULONG cMQIs
, MULTI_QI
*pMQIs
)
241 struct proxy_manager
* This
= (struct proxy_manager
*)iface
;
242 REMQIRESULT
*qiresults
= NULL
;
243 ULONG nonlocal_mqis
= 0;
245 ULONG successful_mqis
= 0;
246 IID
*iids
= HeapAlloc(GetProcessHeap(), 0, cMQIs
* sizeof(*iids
));
247 /* mapping of RemQueryInterface index to QueryMultipleInterfaces index */
248 ULONG
*mapping
= HeapAlloc(GetProcessHeap(), 0, cMQIs
* sizeof(*mapping
));
250 TRACE("cMQIs: %ld\n", cMQIs
);
252 /* try to get a local interface - this includes already active proxy
253 * interfaces and also interfaces exposed by the proxy manager */
254 for (i
= 0; i
< cMQIs
; i
++)
256 TRACE("iid[%ld] = %s\n", i
, debugstr_guid(pMQIs
[i
].pIID
));
257 pMQIs
[i
].hr
= proxy_manager_query_local_interface(This
, pMQIs
[i
].pIID
, (void **)&pMQIs
[i
].pItf
);
258 if (pMQIs
[i
].hr
== S_OK
)
262 iids
[nonlocal_mqis
] = *pMQIs
[i
].pIID
;
263 mapping
[nonlocal_mqis
] = i
;
268 TRACE("%ld interfaces not found locally\n", nonlocal_mqis
);
270 /* if we have more than one interface not found locally then we must try
271 * to query the remote object for it */
272 if (nonlocal_mqis
!= 0)
278 /* get the ipid of the first entry */
279 /* FIXME: should we implement ClientIdentity on the ifproxies instead
280 * of the proxy_manager so we use the correct ipid here? */
281 ipid
= &LIST_ENTRY(list_head(&This
->interfaces
), struct ifproxy
, entry
)->stdobjref
.ipid
;
283 /* get IRemUnknown proxy so we can communicate with the remote object */
284 hr
= proxy_manager_get_remunknown(This
, &remunk
);
288 hr
= IRemUnknown_RemQueryInterface(remunk
, ipid
, NORMALEXTREFS
,
289 nonlocal_mqis
, iids
, &qiresults
);
291 ERR("IRemUnknown_RemQueryInterface failed with error 0x%08lx\n", hr
);
294 /* IRemUnknown_RemQueryInterface can return S_FALSE if only some of
295 * the interfaces were returned */
298 /* try to unmarshal each object returned to us */
299 for (i
= 0; i
< nonlocal_mqis
; i
++)
301 ULONG index
= mapping
[i
];
302 HRESULT hrobj
= qiresults
[i
].hResult
;
304 hrobj
= unmarshal_object(&qiresults
[i
].std
, This
->parent
,
306 This
->dest_context_data
,
308 (void **)&pMQIs
[index
].pItf
);
313 ERR("Failed to get pointer to interface %s\n", debugstr_guid(pMQIs
[index
].pIID
));
314 pMQIs
[index
].hr
= hrobj
;
318 /* free the memory allocated by the proxy */
319 CoTaskMemFree(qiresults
);
322 TRACE("%ld/%ld successfully queried\n", successful_mqis
, cMQIs
);
324 HeapFree(GetProcessHeap(), 0, iids
);
325 HeapFree(GetProcessHeap(), 0, mapping
);
327 if (successful_mqis
== cMQIs
)
328 return S_OK
; /* we got all requested interfaces */
329 else if (successful_mqis
== 0)
330 return E_NOINTERFACE
; /* we didn't get any interfaces */
332 return S_FALSE
; /* we got some interfaces */
335 static const IMultiQIVtbl ClientIdentity_Vtbl
=
337 ClientIdentity_QueryInterface
,
338 ClientIdentity_AddRef
,
339 ClientIdentity_Release
,
340 ClientIdentity_QueryMultipleInterfaces
343 static HRESULT WINAPI
Proxy_QueryInterface(IMarshal
*iface
, REFIID riid
, void **ppvObject
)
345 ICOM_THIS_MULTI(struct proxy_manager
, lpVtblMarshal
, iface
);
346 return IMultiQI_QueryInterface((IMultiQI
*)&This
->lpVtbl
, riid
, ppvObject
);
349 static ULONG WINAPI
Proxy_AddRef(IMarshal
*iface
)
351 ICOM_THIS_MULTI(struct proxy_manager
, lpVtblMarshal
, iface
);
352 return IMultiQI_AddRef((IMultiQI
*)&This
->lpVtbl
);
355 /* FIXME: remove these */
356 static HRESULT WINAPI
StdMarshalImpl_GetUnmarshalClass(LPMARSHAL iface
, REFIID riid
, void* pv
, DWORD dwDestContext
, void* pvDestContext
, DWORD mshlflags
, CLSID
* pCid
);
357 static HRESULT WINAPI
StdMarshalImpl_GetMarshalSizeMax(LPMARSHAL iface
, REFIID riid
, void* pv
, DWORD dwDestContext
, void* pvDestContext
, DWORD mshlflags
, DWORD
* pSize
);
358 static HRESULT WINAPI
StdMarshalImpl_UnmarshalInterface(LPMARSHAL iface
, IStream
*pStm
, REFIID riid
, void **ppv
);
359 static HRESULT WINAPI
StdMarshalImpl_ReleaseMarshalData(LPMARSHAL iface
, IStream
*pStm
);
360 static HRESULT WINAPI
StdMarshalImpl_DisconnectObject(LPMARSHAL iface
, DWORD dwReserved
);
362 static ULONG WINAPI
Proxy_Release(IMarshal
*iface
)
364 ICOM_THIS_MULTI(struct proxy_manager
, lpVtblMarshal
, iface
);
365 return IMultiQI_Release((IMultiQI
*)&This
->lpVtbl
);
368 static HRESULT WINAPI
Proxy_MarshalInterface(
369 LPMARSHAL iface
, IStream
*pStm
, REFIID riid
, void* pv
, DWORD dwDestContext
,
370 void* pvDestContext
, DWORD mshlflags
)
372 ICOM_THIS_MULTI(struct proxy_manager
, lpVtblMarshal
, iface
);
374 struct ifproxy
*ifproxy
;
376 TRACE("(...,%s,...)\n", debugstr_guid(riid
));
378 hr
= proxy_manager_find_ifproxy(This
, riid
, &ifproxy
);
381 STDOBJREF stdobjref
= ifproxy
->stdobjref
;
382 /* FIXME: optimization - share out proxy's public references if possible
383 * instead of making new proxy do a roundtrip through the server */
384 stdobjref
.cPublicRefs
= 0; /* InterlockedDecrement(&This->stdobjref.cPublicRefs) >= 0 ? 1 : 0 */
386 hr
= IStream_Write(pStm
, &stdobjref
, sizeof(stdobjref
), NULL
);
390 /* we don't have the interface already unmarshaled so we have to
391 * request the object from the server */
394 REMQIRESULT
*qiresults
= NULL
;
397 /* get the ipid of the first entry */
398 /* FIXME: should we implement ClientIdentity on the ifproxies instead
399 * of the proxy_manager so we use the correct ipid here? */
400 ipid
= &LIST_ENTRY(list_head(&This
->interfaces
), struct ifproxy
, entry
)->stdobjref
.ipid
;
402 /* get IRemUnknown proxy so we can communicate with the remote object */
403 hr
= proxy_manager_get_remunknown(This
, &remunk
);
407 hr
= IRemUnknown_RemQueryInterface(remunk
, ipid
, NORMALEXTREFS
,
408 1, &iid
, &qiresults
);
411 hr
= IStream_Write(pStm
, &qiresults
->std
, sizeof(qiresults
->std
), NULL
);
415 rif
.ipid
= qiresults
->std
.ipid
;
416 rif
.cPublicRefs
= qiresults
->std
.cPublicRefs
;
417 rif
.cPrivateRefs
= 0;
418 IRemUnknown_RemRelease(remunk
, 1, &rif
);
420 CoTaskMemFree(qiresults
);
423 ERR("IRemUnknown_RemQueryInterface failed with error 0x%08lx\n", hr
);
430 static const IMarshalVtbl ProxyMarshal_Vtbl
=
432 Proxy_QueryInterface
,
435 StdMarshalImpl_GetUnmarshalClass
,
436 StdMarshalImpl_GetMarshalSizeMax
,
437 Proxy_MarshalInterface
,
438 StdMarshalImpl_UnmarshalInterface
,
439 StdMarshalImpl_ReleaseMarshalData
,
440 StdMarshalImpl_DisconnectObject
443 static HRESULT
ifproxy_get_public_ref(struct ifproxy
* This
)
447 if (WAIT_OBJECT_0
!= WaitForSingleObject(This
->parent
->remoting_mutex
, INFINITE
))
449 ERR("Wait failed for ifproxy %p\n", This
);
455 IRemUnknown
*remunk
= NULL
;
457 TRACE("getting public ref for ifproxy %p\n", This
);
459 hr
= proxy_manager_get_remunknown(This
->parent
, &remunk
);
462 HRESULT hrref
= S_OK
;
464 rif
.ipid
= This
->stdobjref
.ipid
;
465 rif
.cPublicRefs
= NORMALEXTREFS
;
466 rif
.cPrivateRefs
= 0;
467 hr
= IRemUnknown_RemAddRef(remunk
, 1, &rif
, &hrref
);
468 if (hr
== S_OK
&& hrref
== S_OK
)
469 This
->refs
+= NORMALEXTREFS
;
471 ERR("IRemUnknown_RemAddRef returned with 0x%08lx, hrref = 0x%08lx\n", hr
, hrref
);
474 ReleaseMutex(This
->parent
->remoting_mutex
);
479 static HRESULT
ifproxy_release_public_refs(struct ifproxy
* This
)
483 if (WAIT_OBJECT_0
!= WaitForSingleObject(This
->parent
->remoting_mutex
, INFINITE
))
485 ERR("Wait failed for ifproxy %p\n", This
);
491 IRemUnknown
*remunk
= NULL
;
493 TRACE("releasing %ld refs\n", This
->refs
);
495 hr
= proxy_manager_get_remunknown(This
->parent
, &remunk
);
499 rif
.ipid
= This
->stdobjref
.ipid
;
500 rif
.cPublicRefs
= This
->refs
;
501 rif
.cPrivateRefs
= 0;
502 hr
= IRemUnknown_RemRelease(remunk
, 1, &rif
);
505 else if (hr
== RPC_E_DISCONNECTED
)
506 WARN("couldn't release references because object was "
507 "disconnected: oxid = %s, oid = %s\n",
508 wine_dbgstr_longlong(This
->parent
->oxid
),
509 wine_dbgstr_longlong(This
->parent
->oid
));
511 ERR("IRemUnknown_RemRelease failed with error 0x%08lx\n", hr
);
514 ReleaseMutex(This
->parent
->remoting_mutex
);
519 /* should be called inside This->parent->cs critical section */
520 static void ifproxy_disconnect(struct ifproxy
* This
)
522 ifproxy_release_public_refs(This
);
523 if (This
->proxy
) IRpcProxyBuffer_Disconnect(This
->proxy
);
525 IRpcChannelBuffer_Release(This
->chan
);
529 /* should be called in This->parent->cs critical section if it is an entry in parent's list */
530 static void ifproxy_destroy(struct ifproxy
* This
)
534 /* release public references to this object so that the stub can know
535 * when to destroy itself */
536 ifproxy_release_public_refs(This
);
538 list_remove(&This
->entry
);
542 IRpcChannelBuffer_Release(This
->chan
);
546 if (This
->proxy
) IRpcProxyBuffer_Release(This
->proxy
);
548 HeapFree(GetProcessHeap(), 0, This
);
551 static HRESULT
proxy_manager_construct(
552 APARTMENT
* apt
, ULONG sorflags
, OXID oxid
, OID oid
,
553 struct proxy_manager
** proxy_manager
)
555 struct proxy_manager
* This
= HeapAlloc(GetProcessHeap(), 0, sizeof(*This
));
556 if (!This
) return E_OUTOFMEMORY
;
558 This
->remoting_mutex
= CreateMutexW(NULL
, FALSE
, NULL
);
559 if (!This
->remoting_mutex
)
561 HeapFree(GetProcessHeap(), 0, This
);
562 return HRESULT_FROM_WIN32(GetLastError());
565 This
->lpVtbl
= &ClientIdentity_Vtbl
;
566 This
->lpVtblMarshal
= &ProxyMarshal_Vtbl
;
568 list_init(&This
->entry
);
569 list_init(&This
->interfaces
);
571 InitializeCriticalSection(&This
->cs
);
572 DEBUG_SET_CRITSEC_NAME(&This
->cs
, "proxy_manager");
574 /* the apartment the object was unmarshaled into */
577 /* the source apartment and id of the object */
583 /* the DCOM draft specification states that the SORF_NOPING flag is
584 * proxy manager specific, not ifproxy specific, so this implies that we
585 * should store the STDOBJREF flags here in the proxy manager. */
586 This
->sorflags
= sorflags
;
588 /* we create the IRemUnknown proxy on demand */
591 /* initialise these values to the weakest values and they will be
592 * overwritten in proxy_manager_set_context */
593 This
->dest_context
= MSHCTX_INPROC
;
594 This
->dest_context_data
= NULL
;
596 EnterCriticalSection(&apt
->cs
);
597 /* FIXME: we are dependent on the ordering in here to make sure a proxy's
598 * IRemUnknown proxy doesn't get destroyed before the regual proxy does
599 * because we need the IRemUnknown proxy during the destruction of the
600 * regular proxy. Ideally, we should maintain a separate list for the
601 * IRemUnknown proxies that need late destruction */
602 list_add_tail(&apt
->proxies
, &This
->entry
);
603 LeaveCriticalSection(&apt
->cs
);
605 TRACE("%p created for OXID %s, OID %s\n", This
,
606 wine_dbgstr_longlong(oxid
), wine_dbgstr_longlong(oid
));
608 *proxy_manager
= This
;
612 static inline void proxy_manager_set_context(struct proxy_manager
*This
, MSHCTX dest_context
, void *dest_context_data
)
614 MSHCTX old_dest_context
= This
->dest_context
;
615 MSHCTX new_dest_context
;
619 new_dest_context
= old_dest_context
;
620 /* "stronger" values overwrite "weaker" values. stronger values are
621 * ones that disable more optimisations */
622 switch (old_dest_context
)
625 new_dest_context
= dest_context
;
627 case MSHCTX_CROSSCTX
:
628 switch (dest_context
)
633 new_dest_context
= dest_context
;
637 switch (dest_context
)
640 case MSHCTX_CROSSCTX
:
643 new_dest_context
= dest_context
;
646 case MSHCTX_NOSHAREDMEM
:
647 switch (dest_context
)
649 case MSHCTX_DIFFERENTMACHINE
:
650 new_dest_context
= dest_context
;
660 if (old_dest_context
== new_dest_context
) break;
662 old_dest_context
= InterlockedCompareExchange((PLONG
)&This
->dest_context
, new_dest_context
, old_dest_context
);
663 } while (new_dest_context
!= old_dest_context
);
665 if (dest_context_data
)
666 InterlockedExchangePointer(&This
->dest_context_data
, dest_context_data
);
669 static HRESULT
proxy_manager_query_local_interface(struct proxy_manager
* This
, REFIID riid
, void ** ppv
)
672 struct ifproxy
* ifproxy
;
674 TRACE("%s\n", debugstr_guid(riid
));
676 if (IsEqualIID(riid
, &IID_IUnknown
) ||
677 IsEqualIID(riid
, &IID_IMultiQI
))
679 *ppv
= (void *)&This
->lpVtbl
;
680 IUnknown_AddRef((IUnknown
*)*ppv
);
683 if (IsEqualIID(riid
, &IID_IMarshal
))
685 *ppv
= (void *)&This
->lpVtblMarshal
;
686 IUnknown_AddRef((IUnknown
*)*ppv
);
689 if (IsEqualIID(riid
, &IID_IClientSecurity
))
691 FIXME("requesting IClientSecurity, but it is unimplemented\n");
693 return E_NOINTERFACE
;
696 hr
= proxy_manager_find_ifproxy(This
, riid
, &ifproxy
);
699 *ppv
= ifproxy
->iface
;
700 IUnknown_AddRef((IUnknown
*)*ppv
);
705 return E_NOINTERFACE
;
708 static HRESULT
proxy_manager_create_ifproxy(
709 struct proxy_manager
* This
, const STDOBJREF
*stdobjref
, REFIID riid
,
710 IRpcChannelBuffer
* channel
, struct ifproxy
** iif_out
)
713 IPSFactoryBuffer
* psfb
;
714 struct ifproxy
* ifproxy
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ifproxy
));
715 if (!ifproxy
) return E_OUTOFMEMORY
;
717 list_init(&ifproxy
->entry
);
719 ifproxy
->parent
= This
;
720 ifproxy
->stdobjref
= *stdobjref
;
721 ifproxy
->iid
= *riid
;
722 ifproxy
->refs
= stdobjref
->cPublicRefs
;
723 ifproxy
->proxy
= NULL
;
726 ifproxy
->chan
= channel
; /* FIXME: we should take the binding strings and construct the channel in this function */
728 /* the IUnknown interface is special because it does not have a
729 * proxy associated with the ifproxy as we handle IUnknown ourselves */
730 if (IsEqualIID(riid
, &IID_IUnknown
))
732 ifproxy
->iface
= (void *)&This
->lpVtbl
;
733 IMultiQI_AddRef((IMultiQI
*)&This
->lpVtbl
);
738 hr
= get_facbuf_for_iid(riid
, &psfb
);
741 /* important note: the outer unknown is set to the proxy manager.
742 * This ensures the COM identity rules are not violated, by having a
743 * one-to-one mapping of objects on the proxy side to objects on the
744 * stub side, no matter which interface you view the object through */
745 hr
= IPSFactoryBuffer_CreateProxy(psfb
, (IUnknown
*)&This
->lpVtbl
, riid
,
746 &ifproxy
->proxy
, &ifproxy
->iface
);
747 IPSFactoryBuffer_Release(psfb
);
749 ERR("Could not create proxy for interface %s, error 0x%08lx\n",
750 debugstr_guid(riid
), hr
);
753 ERR("Could not get IPSFactoryBuffer for interface %s, error 0x%08lx\n",
754 debugstr_guid(riid
), hr
);
757 hr
= IRpcProxyBuffer_Connect(ifproxy
->proxy
, ifproxy
->chan
);
760 /* get at least one external reference to the object to keep it alive */
762 hr
= ifproxy_get_public_ref(ifproxy
);
766 EnterCriticalSection(&This
->cs
);
767 list_add_tail(&This
->interfaces
, &ifproxy
->entry
);
768 LeaveCriticalSection(&This
->cs
);
771 TRACE("ifproxy %p created for IPID %s, interface %s with %lu public refs\n",
772 ifproxy
, debugstr_guid(&stdobjref
->ipid
), debugstr_guid(riid
), stdobjref
->cPublicRefs
);
775 ifproxy_destroy(ifproxy
);
780 static HRESULT
proxy_manager_find_ifproxy(struct proxy_manager
* This
, REFIID riid
, struct ifproxy
** ifproxy_found
)
782 HRESULT hr
= E_NOINTERFACE
; /* assume not found */
783 struct list
* cursor
;
785 EnterCriticalSection(&This
->cs
);
786 LIST_FOR_EACH(cursor
, &This
->interfaces
)
788 struct ifproxy
* ifproxy
= LIST_ENTRY(cursor
, struct ifproxy
, entry
);
789 if (IsEqualIID(riid
, &ifproxy
->iid
))
791 *ifproxy_found
= ifproxy
;
796 LeaveCriticalSection(&This
->cs
);
801 static void proxy_manager_disconnect(struct proxy_manager
* This
)
803 struct list
* cursor
;
805 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This
->oxid
),
806 wine_dbgstr_longlong(This
->oid
));
808 EnterCriticalSection(&This
->cs
);
810 /* SORFP_NOLIFTIMEMGMT proxies (for IRemUnknown) shouldn't be
811 * disconnected - it won't do anything anyway, except cause
812 * problems for other objects that depend on this proxy always
814 if (!(This
->sorflags
& SORFP_NOLIFETIMEMGMT
))
816 LIST_FOR_EACH(cursor
, &This
->interfaces
)
818 struct ifproxy
* ifproxy
= LIST_ENTRY(cursor
, struct ifproxy
, entry
);
819 ifproxy_disconnect(ifproxy
);
823 /* apartment is being destroyed so don't keep a pointer around to it */
826 LeaveCriticalSection(&This
->cs
);
829 static HRESULT
proxy_manager_get_remunknown(struct proxy_manager
* This
, IRemUnknown
**remunk
)
833 /* we don't want to try and unmarshal or use IRemUnknown if we don't want
834 * lifetime management */
835 if (This
->sorflags
& SORFP_NOLIFETIMEMGMT
)
838 EnterCriticalSection(&This
->cs
);
840 /* already created - return existing object */
841 *remunk
= This
->remunk
;
842 else if (!This
->parent
)
843 /* disconnected - we can't create IRemUnknown */
848 /* Don't want IRemUnknown lifetime management as this is IRemUnknown!
849 * We also don't care about whether or not the stub is still alive */
850 stdobjref
.flags
= SORFP_NOLIFETIMEMGMT
| SORF_NOPING
;
851 stdobjref
.cPublicRefs
= 1;
852 /* oxid of destination object */
853 stdobjref
.oxid
= This
->oxid
;
854 /* FIXME: what should be used for the oid? The DCOM draft doesn't say */
855 stdobjref
.oid
= (OID
)-1;
856 /* FIXME: this is a hack around not having an OXID resolver yet -
857 * the OXID resolver should give us the IPID of the IRemUnknown
859 stdobjref
.ipid
.Data1
= 0xffffffff;
860 stdobjref
.ipid
.Data2
= 0xffff;
861 stdobjref
.ipid
.Data3
= 0xffff;
862 assert(sizeof(stdobjref
.ipid
.Data4
) == sizeof(stdobjref
.oxid
));
863 memcpy(&stdobjref
.ipid
.Data4
, &stdobjref
.oxid
, sizeof(OXID
));
865 /* do the unmarshal */
866 hr
= unmarshal_object(&stdobjref
, This
->parent
, This
->dest_context
,
867 This
->dest_context_data
, &IID_IRemUnknown
,
868 (void**)&This
->remunk
);
870 *remunk
= This
->remunk
;
872 LeaveCriticalSection(&This
->cs
);
874 TRACE("got IRemUnknown* pointer %p, hr = 0x%08lx\n", *remunk
, hr
);
879 /* destroys a proxy manager, freeing the memory it used.
880 * Note: this function should not be called from a list iteration in the
881 * apartment, due to the fact that it removes itself from the apartment and
882 * it could add a proxy to IRemUnknown into the apartment. */
883 static void proxy_manager_destroy(struct proxy_manager
* This
)
885 struct list
* cursor
;
887 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This
->oxid
),
888 wine_dbgstr_longlong(This
->oid
));
892 EnterCriticalSection(&This
->parent
->cs
);
894 /* remove ourself from the list of proxy objects in the apartment */
895 LIST_FOR_EACH(cursor
, &This
->parent
->proxies
)
897 if (cursor
== &This
->entry
)
899 list_remove(&This
->entry
);
904 LeaveCriticalSection(&This
->parent
->cs
);
907 /* destroy all of the interface proxies */
908 while ((cursor
= list_head(&This
->interfaces
)))
910 struct ifproxy
* ifproxy
= LIST_ENTRY(cursor
, struct ifproxy
, entry
);
911 ifproxy_destroy(ifproxy
);
914 if (This
->remunk
) IRemUnknown_Release(This
->remunk
);
916 DEBUG_CLEAR_CRITSEC_NAME(&This
->cs
);
917 DeleteCriticalSection(&This
->cs
);
919 CloseHandle(This
->remoting_mutex
);
921 HeapFree(GetProcessHeap(), 0, This
);
924 /* finds the proxy manager corresponding to a given OXID and OID that has
925 * been unmarshaled in the specified apartment. The caller must release the
926 * reference to the proxy_manager when the object is no longer used. */
927 static BOOL
find_proxy_manager(APARTMENT
* apt
, OXID oxid
, OID oid
, struct proxy_manager
** proxy_found
)
930 struct list
* cursor
;
932 EnterCriticalSection(&apt
->cs
);
933 LIST_FOR_EACH(cursor
, &apt
->proxies
)
935 struct proxy_manager
* proxy
= LIST_ENTRY(cursor
, struct proxy_manager
, entry
);
936 if ((oxid
== proxy
->oxid
) && (oid
== proxy
->oid
))
938 *proxy_found
= proxy
;
939 ClientIdentity_AddRef((IMultiQI
*)&proxy
->lpVtbl
);
944 LeaveCriticalSection(&apt
->cs
);
948 HRESULT
apartment_disconnectproxies(struct apartment
*apt
)
950 struct list
* cursor
;
952 LIST_FOR_EACH(cursor
, &apt
->proxies
)
954 struct proxy_manager
* proxy
= LIST_ENTRY(cursor
, struct proxy_manager
, entry
);
955 proxy_manager_disconnect(proxy
);
961 /********************** StdMarshal implementation ****************************/
962 typedef struct _StdMarshalImpl
964 const IMarshalVtbl
*lpvtbl
;
969 LPVOID pvDestContext
;
973 static HRESULT WINAPI
974 StdMarshalImpl_QueryInterface(LPMARSHAL iface
, REFIID riid
, LPVOID
*ppv
)
977 if (IsEqualIID(&IID_IUnknown
, riid
) || IsEqualIID(&IID_IMarshal
, riid
))
980 IUnknown_AddRef(iface
);
983 FIXME("No interface for %s.\n", debugstr_guid(riid
));
984 return E_NOINTERFACE
;
988 StdMarshalImpl_AddRef(LPMARSHAL iface
)
990 StdMarshalImpl
*This
= (StdMarshalImpl
*)iface
;
991 return InterlockedIncrement(&This
->ref
);
995 StdMarshalImpl_Release(LPMARSHAL iface
)
997 StdMarshalImpl
*This
= (StdMarshalImpl
*)iface
;
998 ULONG ref
= InterlockedDecrement(&This
->ref
);
1000 if (!ref
) HeapFree(GetProcessHeap(),0,This
);
1004 static HRESULT WINAPI
1005 StdMarshalImpl_GetUnmarshalClass(
1006 LPMARSHAL iface
, REFIID riid
, void* pv
, DWORD dwDestContext
,
1007 void* pvDestContext
, DWORD mshlflags
, CLSID
* pCid
)
1009 *pCid
= CLSID_DfMarshal
;
1013 static HRESULT WINAPI
1014 StdMarshalImpl_GetMarshalSizeMax(
1015 LPMARSHAL iface
, REFIID riid
, void* pv
, DWORD dwDestContext
,
1016 void* pvDestContext
, DWORD mshlflags
, DWORD
* pSize
)
1018 *pSize
= sizeof(STDOBJREF
);
1022 static HRESULT WINAPI
1023 StdMarshalImpl_MarshalInterface(
1024 LPMARSHAL iface
, IStream
*pStm
,REFIID riid
, void* pv
, DWORD dwDestContext
,
1025 void* pvDestContext
, DWORD mshlflags
)
1027 STDOBJREF stdobjref
;
1030 APARTMENT
*apt
= COM_CurrentApt();
1032 TRACE("(...,%s,...)\n", debugstr_guid(riid
));
1036 ERR("Apartment not initialized\n");
1037 return CO_E_NOTINITIALIZED
;
1040 /* make sure this apartment can be reached from other threads / processes */
1041 RPC_StartRemoting(apt
);
1043 hres
= marshal_object(apt
, &stdobjref
, riid
, (IUnknown
*)pv
, mshlflags
);
1046 ERR("Failed to create ifstub, hres=0x%lx\n", hres
);
1050 hres
= IStream_Write(pStm
, &stdobjref
, sizeof(stdobjref
), &res
);
1051 if (hres
) return hres
;
1056 /* helper for StdMarshalImpl_UnmarshalInterface - does the unmarshaling with
1057 * no questions asked about the rules surrounding same-apartment unmarshals
1058 * and table marshaling */
1059 static HRESULT
unmarshal_object(const STDOBJREF
*stdobjref
, APARTMENT
*apt
,
1060 MSHCTX dest_context
, void *dest_context_data
,
1061 REFIID riid
, void **object
)
1063 struct proxy_manager
*proxy_manager
= NULL
;
1068 TRACE("stdobjref:\n\tflags = %04lx\n\tcPublicRefs = %ld\n\toxid = %s\n\toid = %s\n\tipid = %s\n",
1069 stdobjref
->flags
, stdobjref
->cPublicRefs
,
1070 wine_dbgstr_longlong(stdobjref
->oxid
),
1071 wine_dbgstr_longlong(stdobjref
->oid
),
1072 debugstr_guid(&stdobjref
->ipid
));
1074 /* create an a new proxy manager if one doesn't already exist for the
1076 if (!find_proxy_manager(apt
, stdobjref
->oxid
, stdobjref
->oid
, &proxy_manager
))
1078 hr
= proxy_manager_construct(apt
, stdobjref
->flags
,
1079 stdobjref
->oxid
, stdobjref
->oid
,
1083 TRACE("proxy manager already created, using\n");
1087 struct ifproxy
* ifproxy
;
1089 proxy_manager_set_context(proxy_manager
, dest_context
, dest_context_data
);
1091 hr
= proxy_manager_find_ifproxy(proxy_manager
, riid
, &ifproxy
);
1092 if (hr
== E_NOINTERFACE
)
1094 IRpcChannelBuffer
*chanbuf
;
1095 hr
= RPC_CreateClientChannel(&stdobjref
->oxid
, &stdobjref
->ipid
,
1096 proxy_manager
->dest_context
,
1097 proxy_manager
->dest_context_data
,
1100 hr
= proxy_manager_create_ifproxy(proxy_manager
, stdobjref
,
1101 riid
, chanbuf
, &ifproxy
);
1104 IUnknown_AddRef((IUnknown
*)ifproxy
->iface
);
1107 *object
= ifproxy
->iface
;
1110 /* release our reference to the proxy manager - the client/apartment
1111 * will hold on to the remaining reference for us */
1112 if (proxy_manager
) ClientIdentity_Release((IMultiQI
*)&proxy_manager
->lpVtbl
);
1117 static HRESULT WINAPI
1118 StdMarshalImpl_UnmarshalInterface(LPMARSHAL iface
, IStream
*pStm
, REFIID riid
, void **ppv
)
1120 StdMarshalImpl
*This
= (StdMarshalImpl
*)iface
;
1121 struct stub_manager
*stubmgr
;
1122 STDOBJREF stdobjref
;
1125 APARTMENT
*apt
= COM_CurrentApt();
1126 APARTMENT
*stub_apt
;
1129 TRACE("(...,%s,....)\n", debugstr_guid(riid
));
1131 /* we need an apartment to unmarshal into */
1134 ERR("Apartment not initialized\n");
1135 return CO_E_NOTINITIALIZED
;
1138 /* read STDOBJREF from wire */
1139 hres
= IStream_Read(pStm
, &stdobjref
, sizeof(stdobjref
), &res
);
1140 if (hres
) return STG_E_READFAULT
;
1142 hres
= apartment_getoxid(apt
, &oxid
);
1143 if (hres
) return hres
;
1145 /* check if we're marshalling back to ourselves */
1146 if ((oxid
== stdobjref
.oxid
) && (stubmgr
= get_stub_manager(apt
, stdobjref
.oid
)))
1148 TRACE("Unmarshalling object marshalled in same apartment for iid %s, "
1149 "returning original object %p\n", debugstr_guid(riid
), stubmgr
->object
);
1151 hres
= IUnknown_QueryInterface(stubmgr
->object
, riid
, ppv
);
1153 /* unref the ifstub. FIXME: only do this on success? */
1154 if (!stub_manager_is_table_marshaled(stubmgr
, &stdobjref
.ipid
))
1155 stub_manager_ext_release(stubmgr
, stdobjref
.cPublicRefs
);
1157 stub_manager_int_release(stubmgr
);
1161 /* notify stub manager about unmarshal if process-local object.
1162 * note: if the oxid is not found then we and native will quite happily
1163 * ignore table marshaling and normal marshaling rules regarding number of
1164 * unmarshals, etc, but if you abuse these rules then your proxy could end
1165 * up returning RPC_E_DISCONNECTED. */
1166 if ((stub_apt
= apartment_findfromoxid(stdobjref
.oxid
, TRUE
)))
1168 if ((stubmgr
= get_stub_manager(stub_apt
, stdobjref
.oid
)))
1170 if (!stub_manager_notify_unmarshal(stubmgr
, &stdobjref
.ipid
))
1171 hres
= CO_E_OBJNOTCONNECTED
;
1173 stub_manager_int_release(stubmgr
);
1177 WARN("Couldn't find object for OXID %s, OID %s, assuming disconnected\n",
1178 wine_dbgstr_longlong(stdobjref
.oxid
),
1179 wine_dbgstr_longlong(stdobjref
.oid
));
1180 hres
= CO_E_OBJNOTCONNECTED
;
1183 apartment_release(stub_apt
);
1186 TRACE("Treating unmarshal from OXID %s as inter-process\n",
1187 wine_dbgstr_longlong(stdobjref
.oxid
));
1190 hres
= unmarshal_object(&stdobjref
, apt
, This
->dwDestContext
,
1191 This
->pvDestContext
, riid
, ppv
);
1193 if (hres
) WARN("Failed with error 0x%08lx\n", hres
);
1194 else TRACE("Successfully created proxy %p\n", *ppv
);
1199 static HRESULT WINAPI
1200 StdMarshalImpl_ReleaseMarshalData(LPMARSHAL iface
, IStream
*pStm
)
1202 STDOBJREF stdobjref
;
1205 struct stub_manager
*stubmgr
;
1208 TRACE("iface=%p, pStm=%p\n", iface
, pStm
);
1210 hres
= IStream_Read(pStm
, &stdobjref
, sizeof(stdobjref
), &res
);
1211 if (hres
) return STG_E_READFAULT
;
1213 TRACE("oxid = %s, oid = %s, ipid = %s\n",
1214 wine_dbgstr_longlong(stdobjref
.oxid
),
1215 wine_dbgstr_longlong(stdobjref
.oid
),
1216 wine_dbgstr_guid(&stdobjref
.ipid
));
1218 if (!(apt
= apartment_findfromoxid(stdobjref
.oxid
, TRUE
)))
1220 WARN("Could not map OXID %s to apartment object\n",
1221 wine_dbgstr_longlong(stdobjref
.oxid
));
1222 return RPC_E_INVALID_OBJREF
;
1225 if (!(stubmgr
= get_stub_manager(apt
, stdobjref
.oid
)))
1227 ERR("could not map object ID to stub manager, oxid=%s, oid=%s\n",
1228 wine_dbgstr_longlong(stdobjref
.oxid
), wine_dbgstr_longlong(stdobjref
.oid
));
1229 return RPC_E_INVALID_OBJREF
;
1232 stub_manager_release_marshal_data(stubmgr
, stdobjref
.cPublicRefs
, &stdobjref
.ipid
);
1234 stub_manager_int_release(stubmgr
);
1235 apartment_release(apt
);
1240 static HRESULT WINAPI
1241 StdMarshalImpl_DisconnectObject(LPMARSHAL iface
, DWORD dwReserved
)
1243 FIXME("(), stub!\n");
1247 static const IMarshalVtbl VT_StdMarshal
=
1249 StdMarshalImpl_QueryInterface
,
1250 StdMarshalImpl_AddRef
,
1251 StdMarshalImpl_Release
,
1252 StdMarshalImpl_GetUnmarshalClass
,
1253 StdMarshalImpl_GetMarshalSizeMax
,
1254 StdMarshalImpl_MarshalInterface
,
1255 StdMarshalImpl_UnmarshalInterface
,
1256 StdMarshalImpl_ReleaseMarshalData
,
1257 StdMarshalImpl_DisconnectObject
1260 static HRESULT
StdMarshalImpl_Construct(REFIID riid
, void** ppvObject
)
1262 StdMarshalImpl
* pStdMarshal
=
1263 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(StdMarshalImpl
));
1265 return E_OUTOFMEMORY
;
1266 pStdMarshal
->lpvtbl
= &VT_StdMarshal
;
1267 pStdMarshal
->ref
= 0;
1268 return IMarshal_QueryInterface((IMarshal
*)pStdMarshal
, riid
, ppvObject
);
1271 /***********************************************************************
1272 * CoGetStandardMarshal [OLE32.@]
1274 * Gets or creates a standard marshal object.
1277 * riid [I] Interface identifier of the pUnk object.
1278 * pUnk [I] Optional. Object to get the marshal object for.
1279 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1280 * pvDestContext [I] Reserved. Must be NULL.
1281 * mshlflags [I] Flags affecting the marshaling process.
1282 * ppMarshal [O] Address where marshal object will be stored.
1286 * Failure: HRESULT code.
1290 * The function retrieves the IMarshal object associated with an object if
1291 * that object is currently an active stub, otherwise a new marshal object is
1294 HRESULT WINAPI
CoGetStandardMarshal(REFIID riid
, IUnknown
*pUnk
,
1295 DWORD dwDestContext
, LPVOID pvDestContext
,
1296 DWORD mshlflags
, LPMARSHAL
*ppMarshal
)
1302 FIXME("(%s,NULL,%lx,%p,%lx,%p), unimplemented yet.\n",
1303 debugstr_guid(riid
),dwDestContext
,pvDestContext
,mshlflags
,ppMarshal
);
1306 TRACE("(%s,%p,%lx,%p,%lx,%p)\n",
1307 debugstr_guid(riid
),pUnk
,dwDestContext
,pvDestContext
,mshlflags
,ppMarshal
);
1308 *ppMarshal
= HeapAlloc(GetProcessHeap(),0,sizeof(StdMarshalImpl
));
1309 dm
= (StdMarshalImpl
*) *ppMarshal
;
1310 if (!dm
) return E_FAIL
;
1311 dm
->lpvtbl
= &VT_StdMarshal
;
1315 dm
->dwDestContext
= dwDestContext
;
1316 dm
->pvDestContext
= pvDestContext
;
1317 dm
->mshlflags
= mshlflags
;
1321 /***********************************************************************
1322 * get_marshaler [internal]
1324 * Retrieves an IMarshal interface for an object.
1326 static HRESULT
get_marshaler(REFIID riid
, IUnknown
*pUnk
, DWORD dwDestContext
,
1327 void *pvDestContext
, DWORD mshlFlags
,
1328 LPMARSHAL
*pMarshal
)
1334 hr
= IUnknown_QueryInterface(pUnk
, &IID_IMarshal
, (LPVOID
*)pMarshal
);
1336 hr
= CoGetStandardMarshal(riid
, pUnk
, dwDestContext
, pvDestContext
,
1337 mshlFlags
, pMarshal
);
1341 /***********************************************************************
1342 * get_unmarshaler_from_stream [internal]
1344 * Creates an IMarshal* object according to the data marshaled to the stream.
1345 * The function leaves the stream pointer at the start of the data written
1346 * to the stream by the IMarshal* object.
1348 static HRESULT
get_unmarshaler_from_stream(IStream
*stream
, IMarshal
**marshal
, IID
*iid
)
1354 /* read common OBJREF header */
1355 hr
= IStream_Read(stream
, &objref
, FIELD_OFFSET(OBJREF
, u_objref
), &res
);
1356 if (hr
|| (res
!= FIELD_OFFSET(OBJREF
, u_objref
)))
1358 ERR("Failed to read common OBJREF header, 0x%08lx\n", hr
);
1359 return STG_E_READFAULT
;
1362 /* sanity check on header */
1363 if (objref
.signature
!= OBJREF_SIGNATURE
)
1365 ERR("Bad OBJREF signature 0x%08lx\n", objref
.signature
);
1366 return RPC_E_INVALID_OBJREF
;
1369 if (iid
) *iid
= objref
.iid
;
1371 /* FIXME: handler marshaling */
1372 if (objref
.flags
& OBJREF_STANDARD
)
1374 TRACE("Using standard unmarshaling\n");
1375 hr
= StdMarshalImpl_Construct(&IID_IMarshal
, (LPVOID
*)marshal
);
1377 else if (objref
.flags
& OBJREF_CUSTOM
)
1379 ULONG custom_header_size
= FIELD_OFFSET(OBJREF
, u_objref
.u_custom
.pData
) -
1380 FIELD_OFFSET(OBJREF
, u_objref
.u_custom
);
1381 TRACE("Using custom unmarshaling\n");
1382 /* read constant sized OR_CUSTOM data from stream */
1383 hr
= IStream_Read(stream
, &objref
.u_objref
.u_custom
,
1384 custom_header_size
, &res
);
1385 if (hr
|| (res
!= custom_header_size
))
1387 ERR("Failed to read OR_CUSTOM header, 0x%08lx\n", hr
);
1388 return STG_E_READFAULT
;
1390 /* now create the marshaler specified in the stream */
1391 hr
= CoCreateInstance(&objref
.u_objref
.u_custom
.clsid
, NULL
,
1392 CLSCTX_INPROC_SERVER
, &IID_IMarshal
,
1397 FIXME("Invalid or unimplemented marshaling type specified: %lx\n",
1399 return RPC_E_INVALID_OBJREF
;
1403 ERR("Failed to create marshal, 0x%08lx\n", hr
);
1408 /***********************************************************************
1409 * CoGetMarshalSizeMax [OLE32.@]
1411 * Gets the maximum amount of data that will be needed by a marshal.
1414 * pulSize [O] Address where maximum marshal size will be stored.
1415 * riid [I] Identifier of the interface to marshal.
1416 * pUnk [I] Pointer to the object to marshal.
1417 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1418 * pvDestContext [I] Reserved. Must be NULL.
1419 * mshlFlags [I] Flags that affect the marshaling. See CoMarshalInterface().
1423 * Failure: HRESULT code.
1426 * CoMarshalInterface().
1428 HRESULT WINAPI
CoGetMarshalSizeMax(ULONG
*pulSize
, REFIID riid
, IUnknown
*pUnk
,
1429 DWORD dwDestContext
, void *pvDestContext
,
1434 CLSID marshaler_clsid
;
1436 hr
= get_marshaler(riid
, pUnk
, dwDestContext
, pvDestContext
, mshlFlags
, &pMarshal
);
1440 hr
= IMarshal_GetUnmarshalClass(pMarshal
, riid
, pUnk
, dwDestContext
,
1441 pvDestContext
, mshlFlags
, &marshaler_clsid
);
1444 ERR("IMarshal::GetUnmarshalClass failed, 0x%08lx\n", hr
);
1445 IMarshal_Release(pMarshal
);
1449 hr
= IMarshal_GetMarshalSizeMax(pMarshal
, riid
, pUnk
, dwDestContext
,
1450 pvDestContext
, mshlFlags
, pulSize
);
1451 if (IsEqualCLSID(&marshaler_clsid
, &CLSID_DfMarshal
))
1452 /* add on the size of the common header */
1453 *pulSize
+= FIELD_OFFSET(OBJREF
, u_objref
);
1455 /* custom marshaling: add on the size of the whole OBJREF structure
1456 * like native does */
1457 *pulSize
+= sizeof(OBJREF
);
1459 IMarshal_Release(pMarshal
);
1464 static void dump_MSHLFLAGS(MSHLFLAGS flags
)
1466 if (flags
& MSHLFLAGS_TABLESTRONG
)
1467 TRACE(" MSHLFLAGS_TABLESTRONG");
1468 if (flags
& MSHLFLAGS_TABLEWEAK
)
1469 TRACE(" MSHLFLAGS_TABLEWEAK");
1470 if (!(flags
& (MSHLFLAGS_TABLESTRONG
|MSHLFLAGS_TABLEWEAK
)))
1471 TRACE(" MSHLFLAGS_NORMAL");
1472 if (flags
& MSHLFLAGS_NOPING
)
1473 TRACE(" MSHLFLAGS_NOPING");
1476 /***********************************************************************
1477 * CoMarshalInterface [OLE32.@]
1479 * Marshals an interface into a stream so that the object can then be
1480 * unmarshaled from another COM apartment and used remotely.
1483 * pStream [I] Stream the object will be marshaled into.
1484 * riid [I] Identifier of the interface to marshal.
1485 * pUnk [I] Pointer to the object to marshal.
1486 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1487 * pvDestContext [I] Reserved. Must be NULL.
1488 * mshlFlags [I] Flags that affect the marshaling. See notes.
1492 * Failure: HRESULT code.
1496 * The mshlFlags parameter can take one or more of the following flags:
1497 *| MSHLFLAGS_NORMAL - Unmarshal once, releases stub on last proxy release.
1498 *| MSHLFLAGS_TABLESTRONG - Unmarshal many, release when CoReleaseMarshalData() called.
1499 *| MSHLFLAGS_TABLEWEAK - Unmarshal many, releases stub on last proxy release.
1500 *| MSHLFLAGS_NOPING - No automatic garbage collection (and so reduces network traffic).
1502 * If a marshaled object is not unmarshaled, then CoReleaseMarshalData() must
1503 * be called in order to release the resources used in the marshaling.
1506 * CoUnmarshalInterface(), CoReleaseMarshalData().
1508 HRESULT WINAPI
CoMarshalInterface(IStream
*pStream
, REFIID riid
, IUnknown
*pUnk
,
1509 DWORD dwDestContext
, void *pvDestContext
,
1513 CLSID marshaler_clsid
;
1517 TRACE("(%p, %s, %p, %lx, %p,", pStream
, debugstr_guid(riid
), pUnk
,
1518 dwDestContext
, pvDestContext
);
1519 dump_MSHLFLAGS(mshlFlags
);
1523 return E_INVALIDARG
;
1525 objref
.signature
= OBJREF_SIGNATURE
;
1528 /* get the marshaler for the specified interface */
1529 hr
= get_marshaler(riid
, pUnk
, dwDestContext
, pvDestContext
, mshlFlags
, &pMarshal
);
1532 ERR("Failed to get marshaller, 0x%08lx\n", hr
);
1536 hr
= IMarshal_GetUnmarshalClass(pMarshal
, riid
, pUnk
, dwDestContext
,
1537 pvDestContext
, mshlFlags
, &marshaler_clsid
);
1540 ERR("IMarshal::GetUnmarshalClass failed, 0x%08lx\n", hr
);
1544 /* FIXME: implement handler marshaling too */
1545 if (IsEqualCLSID(&marshaler_clsid
, &CLSID_DfMarshal
))
1547 TRACE("Using standard marshaling\n");
1548 objref
.flags
= OBJREF_STANDARD
;
1550 /* write the common OBJREF header to the stream */
1551 hr
= IStream_Write(pStream
, &objref
, FIELD_OFFSET(OBJREF
, u_objref
), NULL
);
1554 ERR("Failed to write OBJREF header to stream, 0x%08lx\n", hr
);
1560 TRACE("Using custom marshaling\n");
1561 objref
.flags
= OBJREF_CUSTOM
;
1562 objref
.u_objref
.u_custom
.clsid
= marshaler_clsid
;
1563 objref
.u_objref
.u_custom
.cbExtension
= 0;
1564 objref
.u_objref
.u_custom
.size
= 0;
1565 hr
= IMarshal_GetMarshalSizeMax(pMarshal
, riid
, pUnk
, dwDestContext
,
1566 pvDestContext
, mshlFlags
,
1567 &objref
.u_objref
.u_custom
.size
);
1570 ERR("Failed to get max size of marshal data, error 0x%08lx\n", hr
);
1573 /* write constant sized common header and OR_CUSTOM data into stream */
1574 hr
= IStream_Write(pStream
, &objref
,
1575 FIELD_OFFSET(OBJREF
, u_objref
.u_custom
.pData
), NULL
);
1578 ERR("Failed to write OR_CUSTOM header to stream with 0x%08lx\n", hr
);
1583 TRACE("Calling IMarshal::MarshalInterace\n");
1584 /* call helper object to do the actual marshaling */
1585 hr
= IMarshal_MarshalInterface(pMarshal
, pStream
, riid
, pUnk
, dwDestContext
,
1586 pvDestContext
, mshlFlags
);
1590 ERR("Failed to marshal the interface %s, %lx\n", debugstr_guid(riid
), hr
);
1595 IMarshal_Release(pMarshal
);
1597 TRACE("completed with hr 0x%08lx\n", hr
);
1602 /***********************************************************************
1603 * CoUnmarshalInterface [OLE32.@]
1605 * Unmarshals an object from a stream by creating a proxy to the remote
1606 * object, if necessary.
1610 * pStream [I] Stream containing the marshaled object.
1611 * riid [I] Interface identifier of the object to create a proxy to.
1612 * ppv [O] Address where proxy will be stored.
1617 * Failure: HRESULT code.
1620 * CoMarshalInterface().
1622 HRESULT WINAPI
CoUnmarshalInterface(IStream
*pStream
, REFIID riid
, LPVOID
*ppv
)
1629 TRACE("(%p, %s, %p)\n", pStream
, debugstr_guid(riid
), ppv
);
1631 hr
= get_unmarshaler_from_stream(pStream
, &pMarshal
, &iid
);
1635 /* call the helper object to do the actual unmarshaling */
1636 hr
= IMarshal_UnmarshalInterface(pMarshal
, pStream
, &iid
, (LPVOID
*)&object
);
1638 ERR("IMarshal::UnmarshalInterface failed, 0x%08lx\n", hr
);
1640 /* IID_NULL means use the interface ID of the marshaled object */
1641 if (!IsEqualIID(riid
, &IID_NULL
))
1646 if (!IsEqualIID(riid
, &iid
))
1648 TRACE("requested interface != marshalled interface, additional QI needed\n");
1649 hr
= IUnknown_QueryInterface(object
, &iid
, ppv
);
1651 ERR("Couldn't query for interface %s, hr = 0x%08lx\n",
1652 debugstr_guid(riid
), hr
);
1653 IUnknown_Release(object
);
1661 IMarshal_Release(pMarshal
);
1663 TRACE("completed with hr 0x%lx\n", hr
);
1668 /***********************************************************************
1669 * CoReleaseMarshalData [OLE32.@]
1671 * Releases resources associated with an object that has been marshaled into
1676 * pStream [I] The stream that the object has been marshaled into.
1680 * Failure: HRESULT error code.
1684 * Call this function to release resources associated with a normal or
1685 * table-weak marshal that will not be unmarshaled, and all table-strong
1686 * marshals when they are no longer needed.
1689 * CoMarshalInterface(), CoUnmarshalInterface().
1691 HRESULT WINAPI
CoReleaseMarshalData(IStream
*pStream
)
1696 TRACE("(%p)\n", pStream
);
1698 hr
= get_unmarshaler_from_stream(pStream
, &pMarshal
, NULL
);
1702 /* call the helper object to do the releasing of marshal data */
1703 hr
= IMarshal_ReleaseMarshalData(pMarshal
, pStream
);
1705 ERR("IMarshal::ReleaseMarshalData failed with error 0x%08lx\n", hr
);
1707 IMarshal_Release(pMarshal
);
1712 /***********************************************************************
1713 * CoMarshalInterThreadInterfaceInStream [OLE32.@]
1715 * Marshal an interface across threads in the same process.
1718 * riid [I] Identifier of the interface to be marshalled.
1719 * pUnk [I] Pointer to IUnknown-derived interface that will be marshalled.
1720 * ppStm [O] Pointer to IStream object that is created and then used to store the marshalled inteface.
1724 * Failure: E_OUTOFMEMORY and other COM error codes
1727 * CoMarshalInterface(), CoUnmarshalInterface() and CoGetInterfaceAndReleaseStream()
1729 HRESULT WINAPI
CoMarshalInterThreadInterfaceInStream(
1730 REFIID riid
, LPUNKNOWN pUnk
, LPSTREAM
* ppStm
)
1732 ULARGE_INTEGER xpos
;
1733 LARGE_INTEGER seekto
;
1736 TRACE("(%s, %p, %p)\n",debugstr_guid(riid
), pUnk
, ppStm
);
1738 hres
= CreateStreamOnHGlobal(NULL
, TRUE
, ppStm
);
1739 if (FAILED(hres
)) return hres
;
1740 hres
= CoMarshalInterface(*ppStm
, riid
, pUnk
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_NORMAL
);
1742 if (SUCCEEDED(hres
))
1744 memset(&seekto
, 0, sizeof(seekto
));
1745 IStream_Seek(*ppStm
, seekto
, SEEK_SET
, &xpos
);
1749 IStream_Release(*ppStm
);
1756 /***********************************************************************
1757 * CoGetInterfaceAndReleaseStream [OLE32.@]
1759 * Unmarshalls an inteface from a stream and then releases the stream.
1762 * pStm [I] Stream that contains the marshalled inteface.
1763 * riid [I] Interface identifier of the object to unmarshall.
1764 * ppv [O] Address of pointer where the requested interface object will be stored.
1768 * Failure: A COM error code
1771 * CoMarshalInterThreadInterfaceInStream() and CoUnmarshalInteface()
1773 HRESULT WINAPI
CoGetInterfaceAndReleaseStream(LPSTREAM pStm
, REFIID riid
,
1778 TRACE("(%p, %s, %p)\n", pStm
, debugstr_guid(riid
), ppv
);
1780 hres
= CoUnmarshalInterface(pStm
, riid
, ppv
);
1781 IStream_Release(pStm
);
1785 static HRESULT WINAPI
StdMarshalCF_QueryInterface(LPCLASSFACTORY iface
,
1786 REFIID riid
, LPVOID
*ppv
)
1789 if (IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IClassFactory
))
1791 *ppv
= (LPVOID
)iface
;
1794 return E_NOINTERFACE
;
1797 static ULONG WINAPI
StdMarshalCF_AddRef(LPCLASSFACTORY iface
)
1799 return 2; /* non-heap based object */
1802 static ULONG WINAPI
StdMarshalCF_Release(LPCLASSFACTORY iface
)
1804 return 1; /* non-heap based object */
1807 static HRESULT WINAPI
StdMarshalCF_CreateInstance(LPCLASSFACTORY iface
,
1808 LPUNKNOWN pUnk
, REFIID riid
, LPVOID
*ppv
)
1810 if (IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IMarshal
))
1811 return StdMarshalImpl_Construct(riid
, ppv
);
1813 FIXME("(%s), not supported.\n",debugstr_guid(riid
));
1814 return E_NOINTERFACE
;
1817 static HRESULT WINAPI
StdMarshalCF_LockServer(LPCLASSFACTORY iface
, BOOL fLock
)
1819 FIXME("(%d), stub!\n",fLock
);
1823 static const IClassFactoryVtbl StdMarshalCFVtbl
=
1825 StdMarshalCF_QueryInterface
,
1826 StdMarshalCF_AddRef
,
1827 StdMarshalCF_Release
,
1828 StdMarshalCF_CreateInstance
,
1829 StdMarshalCF_LockServer
1831 static const IClassFactoryVtbl
*StdMarshalCF
= &StdMarshalCFVtbl
;
1833 HRESULT
MARSHAL_GetStandardMarshalCF(LPVOID
*ppv
)
1835 *ppv
= &StdMarshalCF
;
1839 /***********************************************************************
1840 * CoMarshalHresult [OLE32.@]
1842 * Marshals an HRESULT value into a stream.
1845 * pStm [I] Stream that hresult will be marshalled into.
1846 * hresult [I] HRESULT to be marshalled.
1850 * Failure: A COM error code
1853 * CoUnmarshalHresult().
1855 HRESULT WINAPI
CoMarshalHresult(LPSTREAM pStm
, HRESULT hresult
)
1857 return IStream_Write(pStm
, &hresult
, sizeof(hresult
), NULL
);
1860 /***********************************************************************
1861 * CoUnmarshalHresult [OLE32.@]
1863 * Unmarshals an HRESULT value from a stream.
1866 * pStm [I] Stream that hresult will be unmarshalled from.
1867 * phresult [I] Pointer to HRESULT where the value will be unmarshalled to.
1871 * Failure: A COM error code
1874 * CoMarshalHresult().
1876 HRESULT WINAPI
CoUnmarshalHresult(LPSTREAM pStm
, HRESULT
* phresult
)
1878 return IStream_Read(pStm
, phresult
, sizeof(*phresult
), NULL
);