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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 /* note: we don't call Release for This->proxy because its lifetime is
547 * controlled by the return value from ClientIdentity_Release, which this
548 * function is always called from */
550 HeapFree(GetProcessHeap(), 0, This
);
553 static HRESULT
proxy_manager_construct(
554 APARTMENT
* apt
, ULONG sorflags
, OXID oxid
, OID oid
,
555 struct proxy_manager
** proxy_manager
)
557 struct proxy_manager
* This
= HeapAlloc(GetProcessHeap(), 0, sizeof(*This
));
558 if (!This
) return E_OUTOFMEMORY
;
560 This
->remoting_mutex
= CreateMutexW(NULL
, FALSE
, NULL
);
561 if (!This
->remoting_mutex
)
563 HeapFree(GetProcessHeap(), 0, This
);
564 return HRESULT_FROM_WIN32(GetLastError());
567 This
->lpVtbl
= &ClientIdentity_Vtbl
;
568 This
->lpVtblMarshal
= &ProxyMarshal_Vtbl
;
570 list_init(&This
->entry
);
571 list_init(&This
->interfaces
);
573 InitializeCriticalSection(&This
->cs
);
574 DEBUG_SET_CRITSEC_NAME(&This
->cs
, "proxy_manager");
576 /* the apartment the object was unmarshaled into */
579 /* the source apartment and id of the object */
585 /* the DCOM draft specification states that the SORF_NOPING flag is
586 * proxy manager specific, not ifproxy specific, so this implies that we
587 * should store the STDOBJREF flags here in the proxy manager. */
588 This
->sorflags
= sorflags
;
590 /* we create the IRemUnknown proxy on demand */
593 /* initialise these values to the weakest values and they will be
594 * overwritten in proxy_manager_set_context */
595 This
->dest_context
= MSHCTX_INPROC
;
596 This
->dest_context_data
= NULL
;
598 EnterCriticalSection(&apt
->cs
);
599 /* FIXME: we are dependent on the ordering in here to make sure a proxy's
600 * IRemUnknown proxy doesn't get destroyed before the regual proxy does
601 * because we need the IRemUnknown proxy during the destruction of the
602 * regular proxy. Ideally, we should maintain a separate list for the
603 * IRemUnknown proxies that need late destruction */
604 list_add_tail(&apt
->proxies
, &This
->entry
);
605 LeaveCriticalSection(&apt
->cs
);
607 TRACE("%p created for OXID %s, OID %s\n", This
,
608 wine_dbgstr_longlong(oxid
), wine_dbgstr_longlong(oid
));
610 *proxy_manager
= This
;
614 static inline void proxy_manager_set_context(struct proxy_manager
*This
, MSHCTX dest_context
, void *dest_context_data
)
616 MSHCTX old_dest_context
= This
->dest_context
;
617 MSHCTX new_dest_context
;
621 new_dest_context
= old_dest_context
;
622 /* "stronger" values overwrite "weaker" values. stronger values are
623 * ones that disable more optimisations */
624 switch (old_dest_context
)
627 new_dest_context
= dest_context
;
629 case MSHCTX_CROSSCTX
:
630 switch (dest_context
)
635 new_dest_context
= dest_context
;
639 switch (dest_context
)
642 case MSHCTX_CROSSCTX
:
645 new_dest_context
= dest_context
;
648 case MSHCTX_NOSHAREDMEM
:
649 switch (dest_context
)
651 case MSHCTX_DIFFERENTMACHINE
:
652 new_dest_context
= dest_context
;
662 if (old_dest_context
== new_dest_context
) break;
664 old_dest_context
= InterlockedCompareExchange((PLONG
)&This
->dest_context
, new_dest_context
, old_dest_context
);
665 } while (new_dest_context
!= old_dest_context
);
667 if (dest_context_data
)
668 InterlockedExchangePointer(&This
->dest_context_data
, dest_context_data
);
671 static HRESULT
proxy_manager_query_local_interface(struct proxy_manager
* This
, REFIID riid
, void ** ppv
)
674 struct ifproxy
* ifproxy
;
676 TRACE("%s\n", debugstr_guid(riid
));
678 if (IsEqualIID(riid
, &IID_IUnknown
) ||
679 IsEqualIID(riid
, &IID_IMultiQI
))
681 *ppv
= (void *)&This
->lpVtbl
;
682 IUnknown_AddRef((IUnknown
*)*ppv
);
685 if (IsEqualIID(riid
, &IID_IMarshal
))
687 *ppv
= (void *)&This
->lpVtblMarshal
;
688 IUnknown_AddRef((IUnknown
*)*ppv
);
691 if (IsEqualIID(riid
, &IID_IClientSecurity
))
693 FIXME("requesting IClientSecurity, but it is unimplemented\n");
695 return E_NOINTERFACE
;
698 hr
= proxy_manager_find_ifproxy(This
, riid
, &ifproxy
);
701 *ppv
= ifproxy
->iface
;
702 IUnknown_AddRef((IUnknown
*)*ppv
);
707 return E_NOINTERFACE
;
710 static HRESULT
proxy_manager_create_ifproxy(
711 struct proxy_manager
* This
, const STDOBJREF
*stdobjref
, REFIID riid
,
712 IRpcChannelBuffer
* channel
, struct ifproxy
** iif_out
)
715 IPSFactoryBuffer
* psfb
;
716 struct ifproxy
* ifproxy
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ifproxy
));
717 if (!ifproxy
) return E_OUTOFMEMORY
;
719 list_init(&ifproxy
->entry
);
721 ifproxy
->parent
= This
;
722 ifproxy
->stdobjref
= *stdobjref
;
723 ifproxy
->iid
= *riid
;
724 ifproxy
->refs
= stdobjref
->cPublicRefs
;
725 ifproxy
->proxy
= NULL
;
728 ifproxy
->chan
= channel
; /* FIXME: we should take the binding strings and construct the channel in this function */
730 /* the IUnknown interface is special because it does not have a
731 * proxy associated with the ifproxy as we handle IUnknown ourselves */
732 if (IsEqualIID(riid
, &IID_IUnknown
))
734 ifproxy
->iface
= (void *)&This
->lpVtbl
;
735 IMultiQI_AddRef((IMultiQI
*)&This
->lpVtbl
);
740 hr
= get_facbuf_for_iid(riid
, &psfb
);
743 /* important note: the outer unknown is set to the proxy manager.
744 * This ensures the COM identity rules are not violated, by having a
745 * one-to-one mapping of objects on the proxy side to objects on the
746 * stub side, no matter which interface you view the object through */
747 hr
= IPSFactoryBuffer_CreateProxy(psfb
, (IUnknown
*)&This
->lpVtbl
, riid
,
748 &ifproxy
->proxy
, &ifproxy
->iface
);
749 IPSFactoryBuffer_Release(psfb
);
751 ERR("Could not create proxy for interface %s, error 0x%08lx\n",
752 debugstr_guid(riid
), hr
);
755 ERR("Could not get IPSFactoryBuffer for interface %s, error 0x%08lx\n",
756 debugstr_guid(riid
), hr
);
759 hr
= IRpcProxyBuffer_Connect(ifproxy
->proxy
, ifproxy
->chan
);
762 /* get at least one external reference to the object to keep it alive */
764 hr
= ifproxy_get_public_ref(ifproxy
);
768 EnterCriticalSection(&This
->cs
);
769 list_add_tail(&This
->interfaces
, &ifproxy
->entry
);
770 LeaveCriticalSection(&This
->cs
);
773 TRACE("ifproxy %p created for IPID %s, interface %s with %lu public refs\n",
774 ifproxy
, debugstr_guid(&stdobjref
->ipid
), debugstr_guid(riid
), stdobjref
->cPublicRefs
);
777 ifproxy_destroy(ifproxy
);
782 static HRESULT
proxy_manager_find_ifproxy(struct proxy_manager
* This
, REFIID riid
, struct ifproxy
** ifproxy_found
)
784 HRESULT hr
= E_NOINTERFACE
; /* assume not found */
785 struct list
* cursor
;
787 EnterCriticalSection(&This
->cs
);
788 LIST_FOR_EACH(cursor
, &This
->interfaces
)
790 struct ifproxy
* ifproxy
= LIST_ENTRY(cursor
, struct ifproxy
, entry
);
791 if (IsEqualIID(riid
, &ifproxy
->iid
))
793 *ifproxy_found
= ifproxy
;
798 LeaveCriticalSection(&This
->cs
);
803 static void proxy_manager_disconnect(struct proxy_manager
* This
)
805 struct list
* cursor
;
807 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This
->oxid
),
808 wine_dbgstr_longlong(This
->oid
));
810 EnterCriticalSection(&This
->cs
);
812 /* SORFP_NOLIFTIMEMGMT proxies (for IRemUnknown) shouldn't be
813 * disconnected - it won't do anything anyway, except cause
814 * problems for other objects that depend on this proxy always
816 if (!(This
->sorflags
& SORFP_NOLIFETIMEMGMT
))
818 LIST_FOR_EACH(cursor
, &This
->interfaces
)
820 struct ifproxy
* ifproxy
= LIST_ENTRY(cursor
, struct ifproxy
, entry
);
821 ifproxy_disconnect(ifproxy
);
825 /* apartment is being destroyed so don't keep a pointer around to it */
828 LeaveCriticalSection(&This
->cs
);
831 static HRESULT
proxy_manager_get_remunknown(struct proxy_manager
* This
, IRemUnknown
**remunk
)
835 /* we don't want to try and unmarshal or use IRemUnknown if we don't want
836 * lifetime management */
837 if (This
->sorflags
& SORFP_NOLIFETIMEMGMT
)
840 EnterCriticalSection(&This
->cs
);
842 /* already created - return existing object */
843 *remunk
= This
->remunk
;
844 else if (!This
->parent
)
845 /* disconnected - we can't create IRemUnknown */
850 /* Don't want IRemUnknown lifetime management as this is IRemUnknown!
851 * We also don't care about whether or not the stub is still alive */
852 stdobjref
.flags
= SORFP_NOLIFETIMEMGMT
| SORF_NOPING
;
853 stdobjref
.cPublicRefs
= 1;
854 /* oxid of destination object */
855 stdobjref
.oxid
= This
->oxid
;
856 /* FIXME: what should be used for the oid? The DCOM draft doesn't say */
857 stdobjref
.oid
= (OID
)-1;
858 /* FIXME: this is a hack around not having an OXID resolver yet -
859 * the OXID resolver should give us the IPID of the IRemUnknown
861 stdobjref
.ipid
.Data1
= 0xffffffff;
862 stdobjref
.ipid
.Data2
= 0xffff;
863 stdobjref
.ipid
.Data3
= 0xffff;
864 assert(sizeof(stdobjref
.ipid
.Data4
) == sizeof(stdobjref
.oxid
));
865 memcpy(&stdobjref
.ipid
.Data4
, &stdobjref
.oxid
, sizeof(OXID
));
867 /* do the unmarshal */
868 hr
= unmarshal_object(&stdobjref
, This
->parent
, This
->dest_context
,
869 This
->dest_context_data
, &IID_IRemUnknown
,
870 (void**)&This
->remunk
);
872 *remunk
= This
->remunk
;
874 LeaveCriticalSection(&This
->cs
);
876 TRACE("got IRemUnknown* pointer %p, hr = 0x%08lx\n", *remunk
, hr
);
881 /* destroys a proxy manager, freeing the memory it used.
882 * Note: this function should not be called from a list iteration in the
883 * apartment, due to the fact that it removes itself from the apartment and
884 * it could add a proxy to IRemUnknown into the apartment. */
885 static void proxy_manager_destroy(struct proxy_manager
* This
)
887 struct list
* cursor
;
889 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This
->oxid
),
890 wine_dbgstr_longlong(This
->oid
));
894 EnterCriticalSection(&This
->parent
->cs
);
896 /* remove ourself from the list of proxy objects in the apartment */
897 LIST_FOR_EACH(cursor
, &This
->parent
->proxies
)
899 if (cursor
== &This
->entry
)
901 list_remove(&This
->entry
);
906 LeaveCriticalSection(&This
->parent
->cs
);
909 /* destroy all of the interface proxies */
910 while ((cursor
= list_head(&This
->interfaces
)))
912 struct ifproxy
* ifproxy
= LIST_ENTRY(cursor
, struct ifproxy
, entry
);
913 ifproxy_destroy(ifproxy
);
916 if (This
->remunk
) IRemUnknown_Release(This
->remunk
);
918 DEBUG_CLEAR_CRITSEC_NAME(&This
->cs
);
919 DeleteCriticalSection(&This
->cs
);
921 CloseHandle(This
->remoting_mutex
);
923 HeapFree(GetProcessHeap(), 0, This
);
926 /* finds the proxy manager corresponding to a given OXID and OID that has
927 * been unmarshaled in the specified apartment. The caller must release the
928 * reference to the proxy_manager when the object is no longer used. */
929 static BOOL
find_proxy_manager(APARTMENT
* apt
, OXID oxid
, OID oid
, struct proxy_manager
** proxy_found
)
932 struct list
* cursor
;
934 EnterCriticalSection(&apt
->cs
);
935 LIST_FOR_EACH(cursor
, &apt
->proxies
)
937 struct proxy_manager
* proxy
= LIST_ENTRY(cursor
, struct proxy_manager
, entry
);
938 if ((oxid
== proxy
->oxid
) && (oid
== proxy
->oid
))
940 *proxy_found
= proxy
;
941 ClientIdentity_AddRef((IMultiQI
*)&proxy
->lpVtbl
);
946 LeaveCriticalSection(&apt
->cs
);
950 HRESULT
apartment_disconnectproxies(struct apartment
*apt
)
952 struct list
* cursor
;
954 LIST_FOR_EACH(cursor
, &apt
->proxies
)
956 struct proxy_manager
* proxy
= LIST_ENTRY(cursor
, struct proxy_manager
, entry
);
957 proxy_manager_disconnect(proxy
);
963 /********************** StdMarshal implementation ****************************/
964 typedef struct _StdMarshalImpl
966 const IMarshalVtbl
*lpvtbl
;
971 LPVOID pvDestContext
;
975 static HRESULT WINAPI
976 StdMarshalImpl_QueryInterface(LPMARSHAL iface
, REFIID riid
, LPVOID
*ppv
)
979 if (IsEqualIID(&IID_IUnknown
, riid
) || IsEqualIID(&IID_IMarshal
, riid
))
982 IUnknown_AddRef(iface
);
985 FIXME("No interface for %s.\n", debugstr_guid(riid
));
986 return E_NOINTERFACE
;
990 StdMarshalImpl_AddRef(LPMARSHAL iface
)
992 StdMarshalImpl
*This
= (StdMarshalImpl
*)iface
;
993 return InterlockedIncrement(&This
->ref
);
997 StdMarshalImpl_Release(LPMARSHAL iface
)
999 StdMarshalImpl
*This
= (StdMarshalImpl
*)iface
;
1000 ULONG ref
= InterlockedDecrement(&This
->ref
);
1002 if (!ref
) HeapFree(GetProcessHeap(),0,This
);
1006 static HRESULT WINAPI
1007 StdMarshalImpl_GetUnmarshalClass(
1008 LPMARSHAL iface
, REFIID riid
, void* pv
, DWORD dwDestContext
,
1009 void* pvDestContext
, DWORD mshlflags
, CLSID
* pCid
)
1011 *pCid
= CLSID_DfMarshal
;
1015 static HRESULT WINAPI
1016 StdMarshalImpl_GetMarshalSizeMax(
1017 LPMARSHAL iface
, REFIID riid
, void* pv
, DWORD dwDestContext
,
1018 void* pvDestContext
, DWORD mshlflags
, DWORD
* pSize
)
1020 *pSize
= sizeof(STDOBJREF
);
1024 static HRESULT WINAPI
1025 StdMarshalImpl_MarshalInterface(
1026 LPMARSHAL iface
, IStream
*pStm
,REFIID riid
, void* pv
, DWORD dwDestContext
,
1027 void* pvDestContext
, DWORD mshlflags
)
1029 STDOBJREF stdobjref
;
1032 APARTMENT
*apt
= COM_CurrentApt();
1034 TRACE("(...,%s,...)\n", debugstr_guid(riid
));
1038 ERR("Apartment not initialized\n");
1039 return CO_E_NOTINITIALIZED
;
1042 /* make sure this apartment can be reached from other threads / processes */
1043 RPC_StartRemoting(apt
);
1045 hres
= marshal_object(apt
, &stdobjref
, riid
, (IUnknown
*)pv
, mshlflags
);
1048 ERR("Failed to create ifstub, hres=0x%lx\n", hres
);
1052 hres
= IStream_Write(pStm
, &stdobjref
, sizeof(stdobjref
), &res
);
1053 if (hres
) return hres
;
1058 /* helper for StdMarshalImpl_UnmarshalInterface - does the unmarshaling with
1059 * no questions asked about the rules surrounding same-apartment unmarshals
1060 * and table marshaling */
1061 static HRESULT
unmarshal_object(const STDOBJREF
*stdobjref
, APARTMENT
*apt
,
1062 MSHCTX dest_context
, void *dest_context_data
,
1063 REFIID riid
, void **object
)
1065 struct proxy_manager
*proxy_manager
= NULL
;
1070 TRACE("stdobjref:\n\tflags = %04lx\n\tcPublicRefs = %ld\n\toxid = %s\n\toid = %s\n\tipid = %s\n",
1071 stdobjref
->flags
, stdobjref
->cPublicRefs
,
1072 wine_dbgstr_longlong(stdobjref
->oxid
),
1073 wine_dbgstr_longlong(stdobjref
->oid
),
1074 debugstr_guid(&stdobjref
->ipid
));
1076 /* create an a new proxy manager if one doesn't already exist for the
1078 if (!find_proxy_manager(apt
, stdobjref
->oxid
, stdobjref
->oid
, &proxy_manager
))
1080 hr
= proxy_manager_construct(apt
, stdobjref
->flags
,
1081 stdobjref
->oxid
, stdobjref
->oid
,
1085 TRACE("proxy manager already created, using\n");
1089 struct ifproxy
* ifproxy
;
1091 proxy_manager_set_context(proxy_manager
, dest_context
, dest_context_data
);
1093 hr
= proxy_manager_find_ifproxy(proxy_manager
, riid
, &ifproxy
);
1094 if (hr
== E_NOINTERFACE
)
1096 IRpcChannelBuffer
*chanbuf
;
1097 hr
= RPC_CreateClientChannel(&stdobjref
->oxid
, &stdobjref
->ipid
,
1098 proxy_manager
->dest_context
,
1099 proxy_manager
->dest_context_data
,
1102 hr
= proxy_manager_create_ifproxy(proxy_manager
, stdobjref
,
1103 riid
, chanbuf
, &ifproxy
);
1106 IUnknown_AddRef((IUnknown
*)ifproxy
->iface
);
1109 *object
= ifproxy
->iface
;
1112 /* release our reference to the proxy manager - the client/apartment
1113 * will hold on to the remaining reference for us */
1114 if (proxy_manager
) ClientIdentity_Release((IMultiQI
*)&proxy_manager
->lpVtbl
);
1119 static HRESULT WINAPI
1120 StdMarshalImpl_UnmarshalInterface(LPMARSHAL iface
, IStream
*pStm
, REFIID riid
, void **ppv
)
1122 StdMarshalImpl
*This
= (StdMarshalImpl
*)iface
;
1123 struct stub_manager
*stubmgr
;
1124 STDOBJREF stdobjref
;
1127 APARTMENT
*apt
= COM_CurrentApt();
1128 APARTMENT
*stub_apt
;
1131 TRACE("(...,%s,....)\n", debugstr_guid(riid
));
1133 /* we need an apartment to unmarshal into */
1136 ERR("Apartment not initialized\n");
1137 return CO_E_NOTINITIALIZED
;
1140 /* read STDOBJREF from wire */
1141 hres
= IStream_Read(pStm
, &stdobjref
, sizeof(stdobjref
), &res
);
1142 if (hres
) return STG_E_READFAULT
;
1144 hres
= apartment_getoxid(apt
, &oxid
);
1145 if (hres
) return hres
;
1147 /* check if we're marshalling back to ourselves */
1148 if ((oxid
== stdobjref
.oxid
) && (stubmgr
= get_stub_manager(apt
, stdobjref
.oid
)))
1150 TRACE("Unmarshalling object marshalled in same apartment for iid %s, "
1151 "returning original object %p\n", debugstr_guid(riid
), stubmgr
->object
);
1153 hres
= IUnknown_QueryInterface(stubmgr
->object
, riid
, ppv
);
1155 /* unref the ifstub. FIXME: only do this on success? */
1156 if (!stub_manager_is_table_marshaled(stubmgr
, &stdobjref
.ipid
))
1157 stub_manager_ext_release(stubmgr
, stdobjref
.cPublicRefs
);
1159 stub_manager_int_release(stubmgr
);
1163 /* notify stub manager about unmarshal if process-local object.
1164 * note: if the oxid is not found then we and native will quite happily
1165 * ignore table marshaling and normal marshaling rules regarding number of
1166 * unmarshals, etc, but if you abuse these rules then your proxy could end
1167 * up returning RPC_E_DISCONNECTED. */
1168 if ((stub_apt
= apartment_findfromoxid(stdobjref
.oxid
, TRUE
)))
1170 if ((stubmgr
= get_stub_manager(stub_apt
, stdobjref
.oid
)))
1172 if (!stub_manager_notify_unmarshal(stubmgr
, &stdobjref
.ipid
))
1173 hres
= CO_E_OBJNOTCONNECTED
;
1175 stub_manager_int_release(stubmgr
);
1179 WARN("Couldn't find object for OXID %s, OID %s, assuming disconnected\n",
1180 wine_dbgstr_longlong(stdobjref
.oxid
),
1181 wine_dbgstr_longlong(stdobjref
.oid
));
1182 hres
= CO_E_OBJNOTCONNECTED
;
1185 apartment_release(stub_apt
);
1188 TRACE("Treating unmarshal from OXID %s as inter-process\n",
1189 wine_dbgstr_longlong(stdobjref
.oxid
));
1192 hres
= unmarshal_object(&stdobjref
, apt
, This
->dwDestContext
,
1193 This
->pvDestContext
, riid
, ppv
);
1195 if (hres
) WARN("Failed with error 0x%08lx\n", hres
);
1196 else TRACE("Successfully created proxy %p\n", *ppv
);
1201 static HRESULT WINAPI
1202 StdMarshalImpl_ReleaseMarshalData(LPMARSHAL iface
, IStream
*pStm
)
1204 STDOBJREF stdobjref
;
1207 struct stub_manager
*stubmgr
;
1210 TRACE("iface=%p, pStm=%p\n", iface
, pStm
);
1212 hres
= IStream_Read(pStm
, &stdobjref
, sizeof(stdobjref
), &res
);
1213 if (hres
) return STG_E_READFAULT
;
1215 TRACE("oxid = %s, oid = %s, ipid = %s\n",
1216 wine_dbgstr_longlong(stdobjref
.oxid
),
1217 wine_dbgstr_longlong(stdobjref
.oid
),
1218 wine_dbgstr_guid(&stdobjref
.ipid
));
1220 if (!(apt
= apartment_findfromoxid(stdobjref
.oxid
, TRUE
)))
1222 WARN("Could not map OXID %s to apartment object\n",
1223 wine_dbgstr_longlong(stdobjref
.oxid
));
1224 return RPC_E_INVALID_OBJREF
;
1227 if (!(stubmgr
= get_stub_manager(apt
, stdobjref
.oid
)))
1229 ERR("could not map object ID to stub manager, oxid=%s, oid=%s\n",
1230 wine_dbgstr_longlong(stdobjref
.oxid
), wine_dbgstr_longlong(stdobjref
.oid
));
1231 return RPC_E_INVALID_OBJREF
;
1234 stub_manager_release_marshal_data(stubmgr
, stdobjref
.cPublicRefs
, &stdobjref
.ipid
);
1236 stub_manager_int_release(stubmgr
);
1237 apartment_release(apt
);
1242 static HRESULT WINAPI
1243 StdMarshalImpl_DisconnectObject(LPMARSHAL iface
, DWORD dwReserved
)
1245 FIXME("(), stub!\n");
1249 static const IMarshalVtbl VT_StdMarshal
=
1251 StdMarshalImpl_QueryInterface
,
1252 StdMarshalImpl_AddRef
,
1253 StdMarshalImpl_Release
,
1254 StdMarshalImpl_GetUnmarshalClass
,
1255 StdMarshalImpl_GetMarshalSizeMax
,
1256 StdMarshalImpl_MarshalInterface
,
1257 StdMarshalImpl_UnmarshalInterface
,
1258 StdMarshalImpl_ReleaseMarshalData
,
1259 StdMarshalImpl_DisconnectObject
1262 static HRESULT
StdMarshalImpl_Construct(REFIID riid
, void** ppvObject
)
1264 StdMarshalImpl
* pStdMarshal
=
1265 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(StdMarshalImpl
));
1267 return E_OUTOFMEMORY
;
1268 pStdMarshal
->lpvtbl
= &VT_StdMarshal
;
1269 pStdMarshal
->ref
= 0;
1270 return IMarshal_QueryInterface((IMarshal
*)pStdMarshal
, riid
, ppvObject
);
1273 /***********************************************************************
1274 * CoGetStandardMarshal [OLE32.@]
1276 * Gets or creates a standard marshal object.
1279 * riid [I] Interface identifier of the pUnk object.
1280 * pUnk [I] Optional. Object to get the marshal object for.
1281 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1282 * pvDestContext [I] Reserved. Must be NULL.
1283 * mshlflags [I] Flags affecting the marshaling process.
1284 * ppMarshal [O] Address where marshal object will be stored.
1288 * Failure: HRESULT code.
1292 * The function retrieves the IMarshal object associated with an object if
1293 * that object is currently an active stub, otherwise a new marshal object is
1296 HRESULT WINAPI
CoGetStandardMarshal(REFIID riid
, IUnknown
*pUnk
,
1297 DWORD dwDestContext
, LPVOID pvDestContext
,
1298 DWORD mshlflags
, LPMARSHAL
*ppMarshal
)
1304 FIXME("(%s,NULL,%lx,%p,%lx,%p), unimplemented yet.\n",
1305 debugstr_guid(riid
),dwDestContext
,pvDestContext
,mshlflags
,ppMarshal
);
1308 TRACE("(%s,%p,%lx,%p,%lx,%p)\n",
1309 debugstr_guid(riid
),pUnk
,dwDestContext
,pvDestContext
,mshlflags
,ppMarshal
);
1310 *ppMarshal
= HeapAlloc(GetProcessHeap(),0,sizeof(StdMarshalImpl
));
1311 dm
= (StdMarshalImpl
*) *ppMarshal
;
1312 if (!dm
) return E_FAIL
;
1313 dm
->lpvtbl
= &VT_StdMarshal
;
1317 dm
->dwDestContext
= dwDestContext
;
1318 dm
->pvDestContext
= pvDestContext
;
1319 dm
->mshlflags
= mshlflags
;
1323 /***********************************************************************
1324 * get_marshaler [internal]
1326 * Retrieves an IMarshal interface for an object.
1328 static HRESULT
get_marshaler(REFIID riid
, IUnknown
*pUnk
, DWORD dwDestContext
,
1329 void *pvDestContext
, DWORD mshlFlags
,
1330 LPMARSHAL
*pMarshal
)
1336 hr
= IUnknown_QueryInterface(pUnk
, &IID_IMarshal
, (LPVOID
*)pMarshal
);
1338 hr
= CoGetStandardMarshal(riid
, pUnk
, dwDestContext
, pvDestContext
,
1339 mshlFlags
, pMarshal
);
1343 /***********************************************************************
1344 * get_unmarshaler_from_stream [internal]
1346 * Creates an IMarshal* object according to the data marshaled to the stream.
1347 * The function leaves the stream pointer at the start of the data written
1348 * to the stream by the IMarshal* object.
1350 static HRESULT
get_unmarshaler_from_stream(IStream
*stream
, IMarshal
**marshal
, IID
*iid
)
1356 /* read common OBJREF header */
1357 hr
= IStream_Read(stream
, &objref
, FIELD_OFFSET(OBJREF
, u_objref
), &res
);
1358 if (hr
|| (res
!= FIELD_OFFSET(OBJREF
, u_objref
)))
1360 ERR("Failed to read common OBJREF header, 0x%08lx\n", hr
);
1361 return STG_E_READFAULT
;
1364 /* sanity check on header */
1365 if (objref
.signature
!= OBJREF_SIGNATURE
)
1367 ERR("Bad OBJREF signature 0x%08lx\n", objref
.signature
);
1368 return RPC_E_INVALID_OBJREF
;
1371 if (iid
) *iid
= objref
.iid
;
1373 /* FIXME: handler marshaling */
1374 if (objref
.flags
& OBJREF_STANDARD
)
1376 TRACE("Using standard unmarshaling\n");
1377 hr
= StdMarshalImpl_Construct(&IID_IMarshal
, (LPVOID
*)marshal
);
1379 else if (objref
.flags
& OBJREF_CUSTOM
)
1381 ULONG custom_header_size
= FIELD_OFFSET(OBJREF
, u_objref
.u_custom
.pData
) -
1382 FIELD_OFFSET(OBJREF
, u_objref
.u_custom
);
1383 TRACE("Using custom unmarshaling\n");
1384 /* read constant sized OR_CUSTOM data from stream */
1385 hr
= IStream_Read(stream
, &objref
.u_objref
.u_custom
,
1386 custom_header_size
, &res
);
1387 if (hr
|| (res
!= custom_header_size
))
1389 ERR("Failed to read OR_CUSTOM header, 0x%08lx\n", hr
);
1390 return STG_E_READFAULT
;
1392 /* now create the marshaler specified in the stream */
1393 hr
= CoCreateInstance(&objref
.u_objref
.u_custom
.clsid
, NULL
,
1394 CLSCTX_INPROC_SERVER
, &IID_IMarshal
,
1399 FIXME("Invalid or unimplemented marshaling type specified: %lx\n",
1401 return RPC_E_INVALID_OBJREF
;
1405 ERR("Failed to create marshal, 0x%08lx\n", hr
);
1410 /***********************************************************************
1411 * CoGetMarshalSizeMax [OLE32.@]
1413 * Gets the maximum amount of data that will be needed by a marshal.
1416 * pulSize [O] Address where maximum marshal size will be stored.
1417 * riid [I] Identifier of the interface to marshal.
1418 * pUnk [I] Pointer to the object to marshal.
1419 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1420 * pvDestContext [I] Reserved. Must be NULL.
1421 * mshlFlags [I] Flags that affect the marshaling. See CoMarshalInterface().
1425 * Failure: HRESULT code.
1428 * CoMarshalInterface().
1430 HRESULT WINAPI
CoGetMarshalSizeMax(ULONG
*pulSize
, REFIID riid
, IUnknown
*pUnk
,
1431 DWORD dwDestContext
, void *pvDestContext
,
1436 CLSID marshaler_clsid
;
1438 hr
= get_marshaler(riid
, pUnk
, dwDestContext
, pvDestContext
, mshlFlags
, &pMarshal
);
1442 hr
= IMarshal_GetUnmarshalClass(pMarshal
, riid
, pUnk
, dwDestContext
,
1443 pvDestContext
, mshlFlags
, &marshaler_clsid
);
1446 ERR("IMarshal::GetUnmarshalClass failed, 0x%08lx\n", hr
);
1447 IMarshal_Release(pMarshal
);
1451 hr
= IMarshal_GetMarshalSizeMax(pMarshal
, riid
, pUnk
, dwDestContext
,
1452 pvDestContext
, mshlFlags
, pulSize
);
1453 /* add on the size of the common header */
1454 *pulSize
+= FIELD_OFFSET(OBJREF
, u_objref
);
1456 /* if custom marshaling, add on size of custom header */
1457 if (!IsEqualCLSID(&marshaler_clsid
, &CLSID_DfMarshal
))
1458 *pulSize
+= FIELD_OFFSET(OBJREF
, u_objref
.u_custom
.pData
) -
1459 FIELD_OFFSET(OBJREF
, u_objref
.u_custom
);
1461 IMarshal_Release(pMarshal
);
1466 static void dump_MSHLFLAGS(MSHLFLAGS flags
)
1468 if (flags
& MSHLFLAGS_TABLESTRONG
)
1469 TRACE(" MSHLFLAGS_TABLESTRONG");
1470 if (flags
& MSHLFLAGS_TABLEWEAK
)
1471 TRACE(" MSHLFLAGS_TABLEWEAK");
1472 if (!(flags
& (MSHLFLAGS_TABLESTRONG
|MSHLFLAGS_TABLEWEAK
)))
1473 TRACE(" MSHLFLAGS_NORMAL");
1474 if (flags
& MSHLFLAGS_NOPING
)
1475 TRACE(" MSHLFLAGS_NOPING");
1478 /***********************************************************************
1479 * CoMarshalInterface [OLE32.@]
1481 * Marshals an interface into a stream so that the object can then be
1482 * unmarshaled from another COM apartment and used remotely.
1485 * pStream [I] Stream the object will be marshaled into.
1486 * riid [I] Identifier of the interface to marshal.
1487 * pUnk [I] Pointer to the object to marshal.
1488 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1489 * pvDestContext [I] Reserved. Must be NULL.
1490 * mshlFlags [I] Flags that affect the marshaling. See notes.
1494 * Failure: HRESULT code.
1498 * The mshlFlags parameter can take one or more of the following flags:
1499 *| MSHLFLAGS_NORMAL - Unmarshal once, releases stub on last proxy release.
1500 *| MSHLFLAGS_TABLESTRONG - Unmarshal many, release when CoReleaseMarshalData() called.
1501 *| MSHLFLAGS_TABLEWEAK - Unmarshal many, releases stub on last proxy release.
1502 *| MSHLFLAGS_NOPING - No automatic garbage collection (and so reduces network traffic).
1504 * If a marshaled object is not unmarshaled, then CoReleaseMarshalData() must
1505 * be called in order to release the resources used in the marshaling.
1508 * CoUnmarshalInterface(), CoReleaseMarshalData().
1510 HRESULT WINAPI
CoMarshalInterface(IStream
*pStream
, REFIID riid
, IUnknown
*pUnk
,
1511 DWORD dwDestContext
, void *pvDestContext
,
1515 CLSID marshaler_clsid
;
1519 TRACE("(%p, %s, %p, %lx, %p,", pStream
, debugstr_guid(riid
), pUnk
,
1520 dwDestContext
, pvDestContext
);
1521 dump_MSHLFLAGS(mshlFlags
);
1525 return E_INVALIDARG
;
1527 objref
.signature
= OBJREF_SIGNATURE
;
1530 /* get the marshaler for the specified interface */
1531 hr
= get_marshaler(riid
, pUnk
, dwDestContext
, pvDestContext
, mshlFlags
, &pMarshal
);
1534 ERR("Failed to get marshaller, 0x%08lx\n", hr
);
1538 hr
= IMarshal_GetUnmarshalClass(pMarshal
, riid
, pUnk
, dwDestContext
,
1539 pvDestContext
, mshlFlags
, &marshaler_clsid
);
1542 ERR("IMarshal::GetUnmarshalClass failed, 0x%08lx\n", hr
);
1546 /* FIXME: implement handler marshaling too */
1547 if (IsEqualCLSID(&marshaler_clsid
, &CLSID_DfMarshal
))
1549 TRACE("Using standard marshaling\n");
1550 objref
.flags
= OBJREF_STANDARD
;
1552 /* write the common OBJREF header to the stream */
1553 hr
= IStream_Write(pStream
, &objref
, FIELD_OFFSET(OBJREF
, u_objref
), NULL
);
1556 ERR("Failed to write OBJREF header to stream, 0x%08lx\n", hr
);
1562 TRACE("Using custom marshaling\n");
1563 objref
.flags
= OBJREF_CUSTOM
;
1564 objref
.u_objref
.u_custom
.clsid
= marshaler_clsid
;
1565 objref
.u_objref
.u_custom
.cbExtension
= 0;
1566 objref
.u_objref
.u_custom
.size
= 0;
1567 hr
= IMarshal_GetMarshalSizeMax(pMarshal
, riid
, pUnk
, dwDestContext
,
1568 pvDestContext
, mshlFlags
,
1569 &objref
.u_objref
.u_custom
.size
);
1572 ERR("Failed to get max size of marshal data, error 0x%08lx\n", hr
);
1575 /* write constant sized common header and OR_CUSTOM data into stream */
1576 hr
= IStream_Write(pStream
, &objref
,
1577 FIELD_OFFSET(OBJREF
, u_objref
.u_custom
.pData
), NULL
);
1580 ERR("Failed to write OR_CUSTOM header to stream with 0x%08lx\n", hr
);
1585 TRACE("Calling IMarshal::MarshalInterace\n");
1586 /* call helper object to do the actual marshaling */
1587 hr
= IMarshal_MarshalInterface(pMarshal
, pStream
, riid
, pUnk
, dwDestContext
,
1588 pvDestContext
, mshlFlags
);
1592 ERR("Failed to marshal the interface %s, %lx\n", debugstr_guid(riid
), hr
);
1597 IMarshal_Release(pMarshal
);
1599 TRACE("completed with hr 0x%08lx\n", hr
);
1604 /***********************************************************************
1605 * CoUnmarshalInterface [OLE32.@]
1607 * Unmarshals an object from a stream by creating a proxy to the remote
1608 * object, if necessary.
1612 * pStream [I] Stream containing the marshaled object.
1613 * riid [I] Interface identifier of the object to create a proxy to.
1614 * ppv [O] Address where proxy will be stored.
1619 * Failure: HRESULT code.
1622 * CoMarshalInterface().
1624 HRESULT WINAPI
CoUnmarshalInterface(IStream
*pStream
, REFIID riid
, LPVOID
*ppv
)
1631 TRACE("(%p, %s, %p)\n", pStream
, debugstr_guid(riid
), ppv
);
1633 hr
= get_unmarshaler_from_stream(pStream
, &pMarshal
, &iid
);
1637 /* call the helper object to do the actual unmarshaling */
1638 hr
= IMarshal_UnmarshalInterface(pMarshal
, pStream
, &iid
, (LPVOID
*)&object
);
1640 ERR("IMarshal::UnmarshalInterface failed, 0x%08lx\n", hr
);
1642 /* IID_NULL means use the interface ID of the marshaled object */
1643 if (!IsEqualIID(riid
, &IID_NULL
))
1648 if (!IsEqualIID(riid
, &iid
))
1650 TRACE("requested interface != marshalled interface, additional QI needed\n");
1651 hr
= IUnknown_QueryInterface(object
, &iid
, ppv
);
1653 ERR("Couldn't query for interface %s, hr = 0x%08lx\n",
1654 debugstr_guid(riid
), hr
);
1655 IUnknown_Release(object
);
1663 IMarshal_Release(pMarshal
);
1665 TRACE("completed with hr 0x%lx\n", hr
);
1670 /***********************************************************************
1671 * CoReleaseMarshalData [OLE32.@]
1673 * Releases resources associated with an object that has been marshaled into
1678 * pStream [I] The stream that the object has been marshaled into.
1682 * Failure: HRESULT error code.
1686 * Call this function to release resources associated with a normal or
1687 * table-weak marshal that will not be unmarshaled, and all table-strong
1688 * marshals when they are no longer needed.
1691 * CoMarshalInterface(), CoUnmarshalInterface().
1693 HRESULT WINAPI
CoReleaseMarshalData(IStream
*pStream
)
1698 TRACE("(%p)\n", pStream
);
1700 hr
= get_unmarshaler_from_stream(pStream
, &pMarshal
, NULL
);
1704 /* call the helper object to do the releasing of marshal data */
1705 hr
= IMarshal_ReleaseMarshalData(pMarshal
, pStream
);
1707 ERR("IMarshal::ReleaseMarshalData failed with error 0x%08lx\n", hr
);
1709 IMarshal_Release(pMarshal
);
1714 /***********************************************************************
1715 * CoMarshalInterThreadInterfaceInStream [OLE32.@]
1717 * Marshal an interface across threads in the same process.
1720 * riid [I] Identifier of the interface to be marshalled.
1721 * pUnk [I] Pointer to IUnknown-derived interface that will be marshalled.
1722 * ppStm [O] Pointer to IStream object that is created and then used to store the marshalled inteface.
1726 * Failure: E_OUTOFMEMORY and other COM error codes
1729 * CoMarshalInterface(), CoUnmarshalInterface() and CoGetInterfaceAndReleaseStream()
1731 HRESULT WINAPI
CoMarshalInterThreadInterfaceInStream(
1732 REFIID riid
, LPUNKNOWN pUnk
, LPSTREAM
* ppStm
)
1734 ULARGE_INTEGER xpos
;
1735 LARGE_INTEGER seekto
;
1738 TRACE("(%s, %p, %p)\n",debugstr_guid(riid
), pUnk
, ppStm
);
1740 hres
= CreateStreamOnHGlobal(NULL
, TRUE
, ppStm
);
1741 if (FAILED(hres
)) return hres
;
1742 hres
= CoMarshalInterface(*ppStm
, riid
, pUnk
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_NORMAL
);
1744 if (SUCCEEDED(hres
))
1746 memset(&seekto
, 0, sizeof(seekto
));
1747 IStream_Seek(*ppStm
, seekto
, SEEK_SET
, &xpos
);
1751 IStream_Release(*ppStm
);
1758 /***********************************************************************
1759 * CoGetInterfaceAndReleaseStream [OLE32.@]
1761 * Unmarshalls an inteface from a stream and then releases the stream.
1764 * pStm [I] Stream that contains the marshalled inteface.
1765 * riid [I] Interface identifier of the object to unmarshall.
1766 * ppv [O] Address of pointer where the requested interface object will be stored.
1770 * Failure: A COM error code
1773 * CoMarshalInterThreadInterfaceInStream() and CoUnmarshalInteface()
1775 HRESULT WINAPI
CoGetInterfaceAndReleaseStream(LPSTREAM pStm
, REFIID riid
,
1780 TRACE("(%p, %s, %p)\n", pStm
, debugstr_guid(riid
), ppv
);
1782 hres
= CoUnmarshalInterface(pStm
, riid
, ppv
);
1783 IStream_Release(pStm
);
1787 static HRESULT WINAPI
StdMarshalCF_QueryInterface(LPCLASSFACTORY iface
,
1788 REFIID riid
, LPVOID
*ppv
)
1791 if (IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IClassFactory
))
1793 *ppv
= (LPVOID
)iface
;
1796 return E_NOINTERFACE
;
1799 static ULONG WINAPI
StdMarshalCF_AddRef(LPCLASSFACTORY iface
)
1801 return 2; /* non-heap based object */
1804 static ULONG WINAPI
StdMarshalCF_Release(LPCLASSFACTORY iface
)
1806 return 1; /* non-heap based object */
1809 static HRESULT WINAPI
StdMarshalCF_CreateInstance(LPCLASSFACTORY iface
,
1810 LPUNKNOWN pUnk
, REFIID riid
, LPVOID
*ppv
)
1812 if (IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IMarshal
))
1813 return StdMarshalImpl_Construct(riid
, ppv
);
1815 FIXME("(%s), not supported.\n",debugstr_guid(riid
));
1816 return E_NOINTERFACE
;
1819 static HRESULT WINAPI
StdMarshalCF_LockServer(LPCLASSFACTORY iface
, BOOL fLock
)
1821 FIXME("(%d), stub!\n",fLock
);
1825 static const IClassFactoryVtbl StdMarshalCFVtbl
=
1827 StdMarshalCF_QueryInterface
,
1828 StdMarshalCF_AddRef
,
1829 StdMarshalCF_Release
,
1830 StdMarshalCF_CreateInstance
,
1831 StdMarshalCF_LockServer
1833 static const IClassFactoryVtbl
*StdMarshalCF
= &StdMarshalCFVtbl
;
1835 HRESULT
MARSHAL_GetStandardMarshalCF(LPVOID
*ppv
)
1837 *ppv
= &StdMarshalCF
;
1841 /***********************************************************************
1842 * CoMarshalHresult [OLE32.@]
1844 * Marshals an HRESULT value into a stream.
1847 * pStm [I] Stream that hresult will be marshalled into.
1848 * hresult [I] HRESULT to be marshalled.
1852 * Failure: A COM error code
1855 * CoUnmarshalHresult().
1857 HRESULT WINAPI
CoMarshalHresult(LPSTREAM pStm
, HRESULT hresult
)
1859 return IStream_Write(pStm
, &hresult
, sizeof(hresult
), NULL
);
1862 /***********************************************************************
1863 * CoUnmarshalHresult [OLE32.@]
1865 * Unmarshals an HRESULT value from a stream.
1868 * pStm [I] Stream that hresult will be unmarshalled from.
1869 * phresult [I] Pointer to HRESULT where the value will be unmarshalled to.
1873 * Failure: A COM error code
1876 * CoMarshalHresult().
1878 HRESULT WINAPI
CoUnmarshalHresult(LPSTREAM pStm
, HRESULT
* phresult
)
1880 return IStream_Read(pStm
, phresult
, sizeof(*phresult
), NULL
);