2 * Copyright 2002 Juergen Schmied
3 * Copyright 2002 Marcus Meissner
4 * Copyright 2004 Mike Hearn, for CodeWeavers
5 * Copyright 2004 Rob Shearman, for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 #include "combase_private.h"
30 #include "wine/debug.h"
31 #include "wine/heap.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
35 HRESULT WINAPI
RPC_CreateClientChannel(const OXID
*oxid
, const IPID
*ipid
,
36 const OXID_INFO
*oxid_info
, const IID
*iid
,
37 DWORD dest_context
, void *dest_context_data
,
38 IRpcChannelBuffer
**chan
, struct apartment
*apt
);
40 static HRESULT
unmarshal_object(const STDOBJREF
*stdobjref
, struct apartment
*apt
,
41 MSHCTX dest_context
, void *dest_context_data
,
42 REFIID riid
, const OXID_INFO
*oxid_info
,
45 /* number of refs given out for normal marshaling */
46 #define NORMALEXTREFS 5
48 /* private flag indicating that the object was marshaled as table-weak */
49 #define SORFP_TABLEWEAK SORF_OXRES1
50 /* private flag indicating that the caller does not want to notify the stub
51 * when the proxy disconnects or is destroyed */
52 #define SORFP_NOLIFETIMEMGMT SORF_OXRES2
54 /* imported interface proxy */
57 struct list entry
; /* entry in proxy_manager list (CS parent->cs) */
58 struct proxy_manager
*parent
; /* owning proxy_manager (RO) */
59 void *iface
; /* interface pointer (RO) */
60 STDOBJREF stdobjref
; /* marshal data that represents this object (RO) */
61 IID iid
; /* interface ID (RO) */
62 IRpcProxyBuffer
*proxy
; /* interface proxy (RO) */
63 ULONG refs
; /* imported (public) references (LOCK) */
64 IRpcChannelBuffer
*chan
; /* channel to object (CS parent->cs) */
67 /* imported object / proxy manager */
70 IMultiQI IMultiQI_iface
;
71 IMarshal IMarshal_iface
;
72 IClientSecurity IClientSecurity_iface
;
73 struct apartment
*parent
; /* owning apartment (RO) */
74 struct list entry
; /* entry in apartment (CS parent->cs) */
75 OXID oxid
; /* object exported ID (RO) */
76 OXID_INFO oxid_info
; /* string binding, ipid of rem unknown and other information (RO) */
77 OID oid
; /* object ID (RO) */
78 struct list interfaces
; /* imported interfaces (CS cs) */
79 LONG refs
; /* proxy reference count (LOCK) */
80 CRITICAL_SECTION cs
; /* thread safety for this object and children */
81 ULONG sorflags
; /* STDOBJREF flags (RO) */
82 IRemUnknown
*remunk
; /* proxy to IRemUnknown used for lifecycle management (CS cs) */
83 HANDLE remoting_mutex
; /* mutex used for synchronizing access to IRemUnknown */
84 MSHCTX dest_context
; /* context used for activating optimisations (LOCK) */
85 void *dest_context_data
; /* reserved context value (LOCK) */
88 static inline struct proxy_manager
*impl_from_IMultiQI(IMultiQI
*iface
)
90 return CONTAINING_RECORD(iface
, struct proxy_manager
, IMultiQI_iface
);
93 static inline struct proxy_manager
*impl_from_IMarshal(IMarshal
*iface
)
95 return CONTAINING_RECORD(iface
, struct proxy_manager
, IMarshal_iface
);
98 static inline struct proxy_manager
*impl_from_IClientSecurity(IClientSecurity
*iface
)
100 return CONTAINING_RECORD(iface
, struct proxy_manager
, IClientSecurity_iface
);
105 IUnknown IUnknown_inner
;
106 IMarshal IMarshal_iface
;
111 static struct ftmarshaler
*impl_ft_from_IUnknown(IUnknown
*iface
)
113 return CONTAINING_RECORD(iface
, struct ftmarshaler
, IUnknown_inner
);
116 static struct ftmarshaler
*impl_ft_from_IMarshal(IMarshal
*iface
)
118 return CONTAINING_RECORD(iface
, struct ftmarshaler
, IMarshal_iface
);
121 /***********************************************************************
122 * CoMarshalHresult (combase.@)
124 HRESULT WINAPI
CoMarshalHresult(IStream
*stream
, HRESULT hresult
)
126 return IStream_Write(stream
, &hresult
, sizeof(hresult
), NULL
);
129 /***********************************************************************
130 * CoUnmarshalHresult (combase.@)
132 HRESULT WINAPI
CoUnmarshalHresult(IStream
*stream
, HRESULT
*phresult
)
134 return IStream_Read(stream
, phresult
, sizeof(*phresult
), NULL
);
137 /***********************************************************************
138 * CoGetInterfaceAndReleaseStream (combase.@)
140 HRESULT WINAPI
CoGetInterfaceAndReleaseStream(IStream
*stream
, REFIID riid
, void **obj
)
144 TRACE("%p, %s, %p\n", stream
, debugstr_guid(riid
), obj
);
146 if (!stream
) return E_INVALIDARG
;
147 hr
= CoUnmarshalInterface(stream
, riid
, obj
);
148 IStream_Release(stream
);
152 /***********************************************************************
153 * CoMarshalInterThreadInterfaceInStream (combase.@)
155 HRESULT WINAPI
CoMarshalInterThreadInterfaceInStream(REFIID riid
, IUnknown
*unk
, IStream
**stream
)
158 LARGE_INTEGER seekto
;
161 TRACE("%s, %p, %p\n", debugstr_guid(riid
), unk
, stream
);
163 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, stream
);
164 if (FAILED(hr
)) return hr
;
165 hr
= CoMarshalInterface(*stream
, riid
, unk
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_NORMAL
);
169 memset(&seekto
, 0, sizeof(seekto
));
170 IStream_Seek(*stream
, seekto
, STREAM_SEEK_SET
, &xpos
);
174 IStream_Release(*stream
);
181 static HRESULT WINAPI
ftmarshaler_inner_QueryInterface(IUnknown
*iface
, REFIID riid
, void **obj
)
183 struct ftmarshaler
*marshaler
= impl_ft_from_IUnknown(iface
);
185 TRACE("%p, %s, %p\n", iface
, debugstr_guid(riid
), obj
);
189 if (IsEqualIID(&IID_IUnknown
, riid
))
190 *obj
= &marshaler
->IUnknown_inner
;
191 else if (IsEqualIID(&IID_IMarshal
, riid
))
192 *obj
= &marshaler
->IMarshal_iface
;
195 FIXME("No interface for %s\n", debugstr_guid(riid
));
196 return E_NOINTERFACE
;
199 IUnknown_AddRef((IUnknown
*)*obj
);
204 static ULONG WINAPI
ftmarshaler_inner_AddRef(IUnknown
*iface
)
206 struct ftmarshaler
*marshaler
= impl_ft_from_IUnknown(iface
);
207 ULONG refcount
= InterlockedIncrement(&marshaler
->refcount
);
209 TRACE("%p, refcount %lu\n", iface
, refcount
);
214 static ULONG WINAPI
ftmarshaler_inner_Release(IUnknown
*iface
)
216 struct ftmarshaler
*marshaler
= impl_ft_from_IUnknown(iface
);
217 ULONG refcount
= InterlockedDecrement(&marshaler
->refcount
);
219 TRACE("%p, refcount %lu\n", iface
, refcount
);
222 heap_free(marshaler
);
227 static const IUnknownVtbl ftmarshaler_inner_vtbl
=
229 ftmarshaler_inner_QueryInterface
,
230 ftmarshaler_inner_AddRef
,
231 ftmarshaler_inner_Release
234 static HRESULT WINAPI
ftmarshaler_QueryInterface(IMarshal
*iface
, REFIID riid
, void **obj
)
236 struct ftmarshaler
*marshaler
= impl_ft_from_IMarshal(iface
);
238 TRACE("%p, %s, %p\n", iface
, debugstr_guid(riid
), obj
);
240 return IUnknown_QueryInterface(marshaler
->outer_unk
, riid
, obj
);
243 static ULONG WINAPI
ftmarshaler_AddRef(IMarshal
*iface
)
245 struct ftmarshaler
*marshaler
= impl_ft_from_IMarshal(iface
);
247 TRACE("%p\n", iface
);
249 return IUnknown_AddRef(marshaler
->outer_unk
);
252 static ULONG WINAPI
ftmarshaler_Release(IMarshal
*iface
)
254 struct ftmarshaler
*marshaler
= impl_ft_from_IMarshal(iface
);
256 TRACE("%p\n", iface
);
258 return IUnknown_Release(marshaler
->outer_unk
);
261 static HRESULT WINAPI
ftmarshaler_GetUnmarshalClass(IMarshal
*iface
, REFIID riid
, void *pv
,
262 DWORD dest_context
, void *pvDestContext
, DWORD mshlflags
, CLSID
*clsid
)
264 TRACE("%s, %p, %#lx, %p, %#lx, %p\n", debugstr_guid(riid
), pv
, dest_context
, pvDestContext
, mshlflags
, clsid
);
266 if (dest_context
== MSHCTX_INPROC
|| dest_context
== MSHCTX_CROSSCTX
)
267 *clsid
= CLSID_InProcFreeMarshaler
;
269 *clsid
= CLSID_StdMarshal
;
274 union ftmarshaler_data
280 static HRESULT WINAPI
ftmarshaler_GetMarshalSizeMax(IMarshal
*iface
, REFIID riid
, void *pv
,
281 DWORD dest_context
, void *pvDestContext
, DWORD mshlflags
, DWORD
*size
)
283 IMarshal
*marshal
= NULL
;
286 TRACE("%s, %p, %#lx, %p, %#lx, %p\n", debugstr_guid(riid
), pv
, dest_context
, pvDestContext
, mshlflags
, size
);
288 /* If the marshalling happens inside the same process the interface pointer is
289 copied between the apartments */
290 if (dest_context
== MSHCTX_INPROC
|| dest_context
== MSHCTX_CROSSCTX
)
292 *size
= sizeof(mshlflags
) + sizeof(union ftmarshaler_data
) + sizeof(GUID
);
296 /* Use the standard marshaller to handle all other cases */
297 CoGetStandardMarshal(riid
, pv
, dest_context
, pvDestContext
, mshlflags
, &marshal
);
298 hr
= IMarshal_GetMarshalSizeMax(marshal
, riid
, pv
, dest_context
, pvDestContext
, mshlflags
, size
);
299 IMarshal_Release(marshal
);
303 static HRESULT WINAPI
ftmarshaler_MarshalInterface(IMarshal
*iface
, IStream
*stream
, REFIID riid
,
304 void *pv
, DWORD dest_context
, void *pvDestContext
, DWORD mshlflags
)
306 IMarshal
*marshal
= NULL
;
309 TRACE("%p, %s, %p, %#lx, %p, %#lx\n", stream
, debugstr_guid(riid
), pv
,
310 dest_context
, pvDestContext
, mshlflags
);
312 /* If the marshalling happens inside the same process the interface pointer is
313 copied between the apartments */
314 if (dest_context
== MSHCTX_INPROC
|| dest_context
== MSHCTX_CROSSCTX
)
316 union ftmarshaler_data data
;
317 GUID unknown_guid
= { 0 };
320 hr
= IUnknown_QueryInterface((IUnknown
*)pv
, riid
, (void **)&data
.object
);
324 /* don't hold a reference to table-weak marshaled interfaces */
325 if (mshlflags
& MSHLFLAGS_TABLEWEAK
)
326 IUnknown_Release(data
.object
);
328 hr
= IStream_Write(stream
, &mshlflags
, sizeof(mshlflags
), NULL
);
329 if (hr
!= S_OK
) return STG_E_MEDIUMFULL
;
331 hr
= IStream_Write(stream
, &data
, sizeof(data
), NULL
);
332 if (hr
!= S_OK
) return STG_E_MEDIUMFULL
;
334 hr
= IStream_Write(stream
, &unknown_guid
, sizeof(unknown_guid
), NULL
);
335 if (hr
!= S_OK
) return STG_E_MEDIUMFULL
;
340 /* Use the standard marshaler to handle all other cases */
341 CoGetStandardMarshal(riid
, pv
, dest_context
, pvDestContext
, mshlflags
, &marshal
);
342 hr
= IMarshal_MarshalInterface(marshal
, stream
, riid
, pv
, dest_context
, pvDestContext
, mshlflags
);
343 IMarshal_Release(marshal
);
347 static HRESULT WINAPI
ftmarshaler_UnmarshalInterface(IMarshal
*iface
, IStream
*stream
, REFIID riid
, void **ppv
)
349 union ftmarshaler_data data
;
354 TRACE("%p, %s, %p\n", stream
, debugstr_guid(riid
), ppv
);
356 hr
= IStream_Read(stream
, &mshlflags
, sizeof(mshlflags
), NULL
);
357 if (hr
!= S_OK
) return STG_E_READFAULT
;
359 hr
= IStream_Read(stream
, &data
, sizeof(data
), NULL
);
360 if (hr
!= S_OK
) return STG_E_READFAULT
;
362 hr
= IStream_Read(stream
, &unknown_guid
, sizeof(unknown_guid
), NULL
);
363 if (hr
!= S_OK
) return STG_E_READFAULT
;
365 hr
= IUnknown_QueryInterface(data
.object
, riid
, ppv
);
366 if (!(mshlflags
& (MSHLFLAGS_TABLEWEAK
| MSHLFLAGS_TABLESTRONG
)))
367 IUnknown_Release(data
.object
);
372 static HRESULT WINAPI
ftmarshaler_ReleaseMarshalData(IMarshal
*iface
, IStream
*stream
)
374 union ftmarshaler_data data
;
379 TRACE("%p\n", stream
);
381 hr
= IStream_Read(stream
, &mshlflags
, sizeof(mshlflags
), NULL
);
382 if (hr
!= S_OK
) return STG_E_READFAULT
;
384 hr
= IStream_Read(stream
, &data
, sizeof(data
), NULL
);
385 if (hr
!= S_OK
) return STG_E_READFAULT
;
387 hr
= IStream_Read(stream
, &unknown_guid
, sizeof(unknown_guid
), NULL
);
388 if (hr
!= S_OK
) return STG_E_READFAULT
;
390 IUnknown_Release(data
.object
);
394 static HRESULT WINAPI
ftmarshaler_DisconnectObject(IMarshal
*iface
, DWORD reserved
)
402 static const IMarshalVtbl ftmarshaler_vtbl
=
404 ftmarshaler_QueryInterface
,
407 ftmarshaler_GetUnmarshalClass
,
408 ftmarshaler_GetMarshalSizeMax
,
409 ftmarshaler_MarshalInterface
,
410 ftmarshaler_UnmarshalInterface
,
411 ftmarshaler_ReleaseMarshalData
,
412 ftmarshaler_DisconnectObject
415 /***********************************************************************
416 * CoCreateFreeThreadedMarshaler (combase.@)
418 HRESULT WINAPI
CoCreateFreeThreadedMarshaler(IUnknown
*outer
, IUnknown
**marshaler
)
420 struct ftmarshaler
*object
;
422 TRACE("%p, %p\n", outer
, marshaler
);
424 object
= heap_alloc(sizeof(*object
));
426 return E_OUTOFMEMORY
;
428 object
->IUnknown_inner
.lpVtbl
= &ftmarshaler_inner_vtbl
;
429 object
->IMarshal_iface
.lpVtbl
= &ftmarshaler_vtbl
;
430 object
->refcount
= 1;
431 object
->outer_unk
= outer
? outer
: &object
->IUnknown_inner
;
433 *marshaler
= &object
->IUnknown_inner
;
438 /***********************************************************************
439 * CoGetMarshalSizeMax (combase.@)
441 HRESULT WINAPI
CoGetMarshalSizeMax(ULONG
*size
, REFIID riid
, IUnknown
*unk
,
442 DWORD dest_context
, void *pvDestContext
, DWORD mshlFlags
)
444 BOOL std_marshal
= FALSE
;
451 hr
= IUnknown_QueryInterface(unk
, &IID_IMarshal
, (void **)&marshal
);
455 hr
= CoGetStandardMarshal(riid
, unk
, dest_context
, pvDestContext
, mshlFlags
, &marshal
);
460 hr
= IMarshal_GetMarshalSizeMax(marshal
, riid
, unk
, dest_context
, pvDestContext
, mshlFlags
, size
);
462 /* add on the size of the whole OBJREF structure like native does */
463 *size
+= sizeof(OBJREF
);
465 IMarshal_Release(marshal
);
469 static void dump_mshflags(MSHLFLAGS flags
)
471 if (flags
& MSHLFLAGS_TABLESTRONG
)
472 TRACE(" MSHLFLAGS_TABLESTRONG");
473 if (flags
& MSHLFLAGS_TABLEWEAK
)
474 TRACE(" MSHLFLAGS_TABLEWEAK");
475 if (!(flags
& (MSHLFLAGS_TABLESTRONG
|MSHLFLAGS_TABLEWEAK
)))
476 TRACE(" MSHLFLAGS_NORMAL");
477 if (flags
& MSHLFLAGS_NOPING
)
478 TRACE(" MSHLFLAGS_NOPING");
481 /***********************************************************************
482 * CoMarshalInterface (combase.@)
484 HRESULT WINAPI
CoMarshalInterface(IStream
*stream
, REFIID riid
, IUnknown
*unk
,
485 DWORD dest_context
, void *pvDestContext
, DWORD mshlFlags
)
487 CLSID marshaler_clsid
;
491 TRACE("%p, %s, %p, %lx, %p, ", stream
, debugstr_guid(riid
), unk
, dest_context
, pvDestContext
);
492 dump_mshflags(mshlFlags
);
498 hr
= IUnknown_QueryInterface(unk
, &IID_IMarshal
, (void **)&marshal
);
500 hr
= CoGetStandardMarshal(riid
, unk
, dest_context
, pvDestContext
, mshlFlags
, &marshal
);
503 ERR("Failed to get marshaller, %#lx\n", hr
);
507 hr
= IMarshal_GetUnmarshalClass(marshal
, riid
, unk
, dest_context
, pvDestContext
, mshlFlags
,
511 ERR("IMarshal::GetUnmarshalClass failed, %#lx\n", hr
);
515 /* FIXME: implement handler marshaling too */
516 if (IsEqualCLSID(&marshaler_clsid
, &CLSID_StdMarshal
))
518 TRACE("Using standard marshaling\n");
524 TRACE("Using custom marshaling\n");
525 objref
.signature
= OBJREF_SIGNATURE
;
527 objref
.flags
= OBJREF_CUSTOM
;
528 objref
.u_objref
.u_custom
.clsid
= marshaler_clsid
;
529 objref
.u_objref
.u_custom
.cbExtension
= 0;
530 objref
.u_objref
.u_custom
.size
= 0;
531 hr
= IMarshal_GetMarshalSizeMax(marshal
, riid
, unk
, dest_context
, pvDestContext
, mshlFlags
,
532 &objref
.u_objref
.u_custom
.size
);
535 ERR("Failed to get max size of marshal data, error %#lx\n", hr
);
538 /* write constant sized common header and OR_CUSTOM data into stream */
539 hr
= IStream_Write(stream
, &objref
, FIELD_OFFSET(OBJREF
, u_objref
.u_custom
.pData
), NULL
);
542 ERR("Failed to write OR_CUSTOM header to stream with %#lx\n", hr
);
547 TRACE("Calling IMarshal::MarshalInterface\n");
549 hr
= IMarshal_MarshalInterface(marshal
, stream
, riid
, unk
, dest_context
, pvDestContext
, mshlFlags
);
552 ERR("Failed to marshal the interface %s, hr %#lx\n", debugstr_guid(riid
), hr
);
557 IMarshal_Release(marshal
);
559 TRACE("completed with hr %#lx\n", hr
);
564 /* Creates an IMarshal* object according to the data marshaled to the stream.
565 * The function leaves the stream pointer at the start of the data written
566 * to the stream by the IMarshal* object.
568 static HRESULT
get_unmarshaler_from_stream(IStream
*stream
, IMarshal
**marshal
, IID
*iid
)
574 /* read common OBJREF header */
575 hr
= IStream_Read(stream
, &objref
, FIELD_OFFSET(OBJREF
, u_objref
), &res
);
576 if (hr
!= S_OK
|| (res
!= FIELD_OFFSET(OBJREF
, u_objref
)))
578 ERR("Failed to read common OBJREF header, %#lx\n", hr
);
579 return STG_E_READFAULT
;
582 /* sanity check on header */
583 if (objref
.signature
!= OBJREF_SIGNATURE
)
585 ERR("Bad OBJREF signature %#lx\n", objref
.signature
);
586 return RPC_E_INVALID_OBJREF
;
589 if (iid
) *iid
= objref
.iid
;
591 /* FIXME: handler marshaling */
592 if (objref
.flags
& OBJREF_STANDARD
)
594 TRACE("Using standard unmarshaling\n");
598 else if (objref
.flags
& OBJREF_CUSTOM
)
600 ULONG custom_header_size
= FIELD_OFFSET(OBJREF
, u_objref
.u_custom
.pData
) -
601 FIELD_OFFSET(OBJREF
, u_objref
.u_custom
);
602 TRACE("Using custom unmarshaling\n");
603 /* read constant sized OR_CUSTOM data from stream */
604 hr
= IStream_Read(stream
, &objref
.u_objref
.u_custom
,
605 custom_header_size
, &res
);
606 if (hr
!= S_OK
|| (res
!= custom_header_size
))
608 ERR("Failed to read OR_CUSTOM header, %#lx\n", hr
);
609 return STG_E_READFAULT
;
611 /* now create the marshaler specified in the stream */
612 hr
= CoCreateInstance(&objref
.u_objref
.u_custom
.clsid
, NULL
,
613 CLSCTX_INPROC_SERVER
, &IID_IMarshal
,
618 FIXME("Invalid or unimplemented marshaling type specified: %lx\n", objref
.flags
);
619 return RPC_E_INVALID_OBJREF
;
623 ERR("Failed to create marshal, %#lx\n", hr
);
628 static HRESULT
std_release_marshal_data(IStream
*stream
)
630 struct stub_manager
*stubmgr
;
631 struct OR_STANDARD obj
;
632 struct apartment
*apt
;
636 hr
= IStream_Read(stream
, &obj
, FIELD_OFFSET(struct OR_STANDARD
, saResAddr
.aStringArray
), &res
);
637 if (hr
!= S_OK
) return STG_E_READFAULT
;
639 if (obj
.saResAddr
.wNumEntries
)
641 ERR("unsupported size of DUALSTRINGARRAY\n");
645 TRACE("oxid = %s, oid = %s, ipid = %s\n", wine_dbgstr_longlong(obj
.std
.oxid
),
646 wine_dbgstr_longlong(obj
.std
.oid
), wine_dbgstr_guid(&obj
.std
.ipid
));
648 if (!(apt
= apartment_findfromoxid(obj
.std
.oxid
)))
650 WARN("Could not map OXID %s to apartment object\n",
651 wine_dbgstr_longlong(obj
.std
.oxid
));
652 return RPC_E_INVALID_OBJREF
;
655 if (!(stubmgr
= get_stub_manager(apt
, obj
.std
.oid
)))
657 apartment_release(apt
);
658 ERR("could not map object ID to stub manager, oxid=%s, oid=%s\n",
659 wine_dbgstr_longlong(obj
.std
.oxid
), wine_dbgstr_longlong(obj
.std
.oid
));
660 return RPC_E_INVALID_OBJREF
;
663 stub_manager_release_marshal_data(stubmgr
, obj
.std
.cPublicRefs
, &obj
.std
.ipid
, obj
.std
.flags
& SORFP_TABLEWEAK
);
665 stub_manager_int_release(stubmgr
);
666 apartment_release(apt
);
671 /***********************************************************************
672 * CoReleaseMarshalData (combase.@)
674 HRESULT WINAPI
CoReleaseMarshalData(IStream
*stream
)
679 TRACE("%p\n", stream
);
681 hr
= get_unmarshaler_from_stream(stream
, &marshal
, NULL
);
684 hr
= std_release_marshal_data(stream
);
686 ERR("StdMarshal ReleaseMarshalData failed with error %#lx\n", hr
);
692 /* call the helper object to do the releasing of marshal data */
693 hr
= IMarshal_ReleaseMarshalData(marshal
, stream
);
695 ERR("IMarshal::ReleaseMarshalData failed with error %#lx\n", hr
);
697 IMarshal_Release(marshal
);
701 static HRESULT
std_unmarshal_interface(MSHCTX dest_context
, void *dest_context_data
,
702 IStream
*stream
, REFIID riid
, void **ppv
)
704 struct stub_manager
*stubmgr
= NULL
;
705 struct OR_STANDARD obj
;
708 struct apartment
*apt
, *stub_apt
;
710 TRACE("(...,%s,....)\n", debugstr_guid(riid
));
712 /* we need an apartment to unmarshal into */
713 if (!(apt
= apartment_get_current_or_mta()))
715 ERR("Apartment not initialized\n");
716 return CO_E_NOTINITIALIZED
;
719 /* read STDOBJREF from wire */
720 hres
= IStream_Read(stream
, &obj
, FIELD_OFFSET(struct OR_STANDARD
, saResAddr
.aStringArray
), &res
);
723 apartment_release(apt
);
724 return STG_E_READFAULT
;
727 if (obj
.saResAddr
.wNumEntries
)
729 ERR("unsupported size of DUALSTRINGARRAY\n");
733 /* check if we're marshalling back to ourselves */
734 if ((apartment_getoxid(apt
) == obj
.std
.oxid
) && (stubmgr
= get_stub_manager(apt
, obj
.std
.oid
)))
736 TRACE("Unmarshalling object marshalled in same apartment for iid %s, "
737 "returning original object %p\n", debugstr_guid(riid
), stubmgr
->object
);
739 hres
= IUnknown_QueryInterface(stubmgr
->object
, riid
, ppv
);
741 /* unref the ifstub. FIXME: only do this on success? */
742 if (!stub_manager_is_table_marshaled(stubmgr
, &obj
.std
.ipid
))
743 stub_manager_ext_release(stubmgr
, obj
.std
.cPublicRefs
, obj
.std
.flags
& SORFP_TABLEWEAK
, FALSE
);
745 stub_manager_int_release(stubmgr
);
746 apartment_release(apt
);
750 /* notify stub manager about unmarshal if process-local object.
751 * note: if the oxid is not found then we and native will quite happily
752 * ignore table marshaling and normal marshaling rules regarding number of
753 * unmarshals, etc, but if you abuse these rules then your proxy could end
754 * up returning RPC_E_DISCONNECTED. */
755 if ((stub_apt
= apartment_findfromoxid(obj
.std
.oxid
)))
757 if ((stubmgr
= get_stub_manager(stub_apt
, obj
.std
.oid
)))
759 if (!stub_manager_notify_unmarshal(stubmgr
, &obj
.std
.ipid
))
760 hres
= CO_E_OBJNOTCONNECTED
;
764 WARN("Couldn't find object for OXID %s, OID %s, assuming disconnected\n",
765 wine_dbgstr_longlong(obj
.std
.oxid
),
766 wine_dbgstr_longlong(obj
.std
.oid
));
767 hres
= CO_E_OBJNOTCONNECTED
;
771 TRACE("Treating unmarshal from OXID %s as inter-process\n",
772 wine_dbgstr_longlong(obj
.std
.oxid
));
775 hres
= unmarshal_object(&obj
.std
, apt
, dest_context
,
776 dest_context_data
, riid
,
777 stubmgr
? &stubmgr
->oxid_info
: NULL
, ppv
);
779 if (stubmgr
) stub_manager_int_release(stubmgr
);
780 if (stub_apt
) apartment_release(stub_apt
);
782 if (hres
!= S_OK
) WARN("Failed with error %#lx\n", hres
);
783 else TRACE("Successfully created proxy %p\n", *ppv
);
785 apartment_release(apt
);
789 /***********************************************************************
790 * CoUnmarshalInterface (combase.@)
792 HRESULT WINAPI
CoUnmarshalInterface(IStream
*stream
, REFIID riid
, void **ppv
)
799 TRACE("%p, %s, %p\n", stream
, debugstr_guid(riid
), ppv
);
804 hr
= get_unmarshaler_from_stream(stream
, &marshal
, &iid
);
807 hr
= std_unmarshal_interface(0, NULL
, stream
, &iid
, (void **)&object
);
809 ERR("StdMarshal UnmarshalInterface failed, hr %#lx\n", hr
);
813 /* call the helper object to do the actual unmarshaling */
814 hr
= IMarshal_UnmarshalInterface(marshal
, stream
, &iid
, (void **)&object
);
815 IMarshal_Release(marshal
);
817 ERR("IMarshal::UnmarshalInterface failed, hr %#lx\n", hr
);
822 /* IID_NULL means use the interface ID of the marshaled object */
823 if (!IsEqualIID(riid
, &IID_NULL
) && !IsEqualIID(riid
, &iid
))
825 TRACE("requested interface != marshalled interface, additional QI needed\n");
826 hr
= IUnknown_QueryInterface(object
, riid
, ppv
);
828 ERR("Couldn't query for interface %s, hr %#lx\n", debugstr_guid(riid
), hr
);
829 IUnknown_Release(object
);
837 TRACE("completed with hr %#lx\n", hr
);
842 /* Marshalling just passes a unique identifier to the remote client,
843 * that makes it possible to find the passed interface again.
845 * So basically we need a set of values that make it unique.
847 * Note that the IUnknown_QI(ob,xiid,&ppv) always returns the SAME ppv value!
849 * A triple is used: OXID (apt id), OID (stub manager id),
850 * IPID (interface ptr/stub id).
852 * OXIDs identify an apartment and are network scoped
853 * OIDs identify a stub manager and are apartment scoped
854 * IPIDs identify an interface stub and are apartment scoped
857 static inline HRESULT
get_facbuf_for_iid(REFIID riid
, IPSFactoryBuffer
**facbuf
)
862 hr
= CoGetPSClsid(riid
, &clsid
);
865 return CoGetClassObject(&clsid
, CLSCTX_INPROC_SERVER
| CLSCTX_PS_DLL
, NULL
, &IID_IPSFactoryBuffer
, (void **)facbuf
);
868 /* marshals an object into a STDOBJREF structure */
869 HRESULT
marshal_object(struct apartment
*apt
, STDOBJREF
*stdobjref
, REFIID riid
, IUnknown
*object
,
870 DWORD dest_context
, void *dest_context_data
, MSHLFLAGS mshlflags
)
872 struct stub_manager
*manager
;
873 struct ifstub
*ifstub
;
877 stdobjref
->oxid
= apartment_getoxid(apt
);
879 hr
= apartment_createwindowifneeded(apt
);
883 if (!(manager
= get_stub_manager_from_object(apt
, object
, TRUE
)))
884 return E_OUTOFMEMORY
;
886 stdobjref
->flags
= SORF_NULL
;
887 if (mshlflags
& MSHLFLAGS_TABLEWEAK
)
888 stdobjref
->flags
|= SORFP_TABLEWEAK
;
889 if (mshlflags
& MSHLFLAGS_NOPING
)
890 stdobjref
->flags
|= SORF_NOPING
;
891 stdobjref
->oid
= manager
->oid
;
893 tablemarshal
= ((mshlflags
& MSHLFLAGS_TABLESTRONG
) || (mshlflags
& MSHLFLAGS_TABLEWEAK
));
895 /* make sure ifstub that we are creating is unique */
896 ifstub
= stub_manager_find_ifstub(manager
, riid
, mshlflags
);
898 IRpcStubBuffer
*stub
= NULL
;
900 /* IUnknown doesn't require a stub buffer, because it never goes out on
902 if (!IsEqualIID(riid
, &IID_IUnknown
))
904 IPSFactoryBuffer
*psfb
;
906 hr
= get_facbuf_for_iid(riid
, &psfb
);
908 hr
= IPSFactoryBuffer_CreateStub(psfb
, riid
, manager
->object
, &stub
);
909 IPSFactoryBuffer_Release(psfb
);
911 ERR("Failed to create an IRpcStubBuffer from IPSFactory for %s with error %#lx\n",
912 debugstr_guid(riid
), hr
);
914 WARN("couldn't get IPSFactory buffer for interface %s\n", debugstr_guid(riid
));
921 ifstub
= stub_manager_new_ifstub(manager
, stub
, riid
, dest_context
, dest_context_data
, mshlflags
);
925 if (stub
) IRpcStubBuffer_Release(stub
);
928 stub_manager_int_release(manager
);
929 /* destroy the stub manager if it has no ifstubs by releasing
930 * zero external references */
931 stub_manager_ext_release(manager
, 0, FALSE
, TRUE
);
938 stdobjref
->cPublicRefs
= NORMALEXTREFS
;
939 stub_manager_ext_addref(manager
, stdobjref
->cPublicRefs
, FALSE
);
943 stdobjref
->cPublicRefs
= 0;
944 if (mshlflags
& MSHLFLAGS_TABLESTRONG
)
945 stub_manager_ext_addref(manager
, 1, FALSE
);
947 stub_manager_ext_addref(manager
, 0, TRUE
);
950 /* FIXME: check return value */
951 rpc_register_interface(riid
);
953 stdobjref
->ipid
= ifstub
->ipid
;
955 stub_manager_int_release(manager
);
959 /* Client-side identity of the server object */
961 static HRESULT
proxy_manager_get_remunknown(struct proxy_manager
* This
, IRemUnknown
**remunk
);
962 static void proxy_manager_destroy(struct proxy_manager
* This
);
963 static HRESULT
proxy_manager_find_ifproxy(struct proxy_manager
* This
, REFIID riid
, struct ifproxy
** ifproxy_found
);
964 static HRESULT
proxy_manager_query_local_interface(struct proxy_manager
* This
, REFIID riid
, void ** ppv
);
966 static HRESULT WINAPI
ClientIdentity_QueryInterface(IMultiQI
* iface
, REFIID riid
, void ** ppv
)
971 TRACE("%s\n", debugstr_guid(riid
));
974 hr
= IMultiQI_QueryMultipleInterfaces(iface
, 1, &mqi
);
980 static ULONG WINAPI
ClientIdentity_AddRef(IMultiQI
*iface
)
982 struct proxy_manager
*This
= impl_from_IMultiQI(iface
);
983 TRACE("%p - before %ld\n", iface
, This
->refs
);
984 return InterlockedIncrement(&This
->refs
);
987 static ULONG WINAPI
ClientIdentity_Release(IMultiQI
*iface
)
989 struct proxy_manager
*This
= impl_from_IMultiQI(iface
);
990 ULONG refs
= InterlockedDecrement(&This
->refs
);
991 TRACE("%p - after %ld\n", iface
, refs
);
993 proxy_manager_destroy(This
);
997 static HRESULT WINAPI
ClientIdentity_QueryMultipleInterfaces(IMultiQI
*iface
, ULONG cMQIs
, MULTI_QI
*pMQIs
)
999 struct proxy_manager
*This
= impl_from_IMultiQI(iface
);
1000 REMQIRESULT
*qiresults
= NULL
;
1001 ULONG nonlocal_mqis
= 0;
1003 ULONG successful_mqis
= 0;
1004 IID
*iids
= HeapAlloc(GetProcessHeap(), 0, cMQIs
* sizeof(*iids
));
1005 /* mapping of RemQueryInterface index to QueryMultipleInterfaces index */
1006 ULONG
*mapping
= HeapAlloc(GetProcessHeap(), 0, cMQIs
* sizeof(*mapping
));
1008 TRACE("cMQIs: %ld\n", cMQIs
);
1010 /* try to get a local interface - this includes already active proxy
1011 * interfaces and also interfaces exposed by the proxy manager */
1012 for (i
= 0; i
< cMQIs
; i
++)
1014 TRACE("iid[%ld] = %s\n", i
, debugstr_guid(pMQIs
[i
].pIID
));
1015 pMQIs
[i
].hr
= proxy_manager_query_local_interface(This
, pMQIs
[i
].pIID
, (void **)&pMQIs
[i
].pItf
);
1016 if (pMQIs
[i
].hr
== S_OK
)
1020 iids
[nonlocal_mqis
] = *pMQIs
[i
].pIID
;
1021 mapping
[nonlocal_mqis
] = i
;
1026 TRACE("%ld interfaces not found locally\n", nonlocal_mqis
);
1028 /* if we have more than one interface not found locally then we must try
1029 * to query the remote object for it */
1030 if (nonlocal_mqis
!= 0)
1032 IRemUnknown
*remunk
;
1036 /* get the ipid of the first entry */
1037 /* FIXME: should we implement ClientIdentity on the ifproxies instead
1038 * of the proxy_manager so we use the correct ipid here? */
1039 ipid
= &LIST_ENTRY(list_head(&This
->interfaces
), struct ifproxy
, entry
)->stdobjref
.ipid
;
1041 /* get IRemUnknown proxy so we can communicate with the remote object */
1042 hr
= proxy_manager_get_remunknown(This
, &remunk
);
1046 hr
= IRemUnknown_RemQueryInterface(remunk
, ipid
, NORMALEXTREFS
,
1047 nonlocal_mqis
, iids
, &qiresults
);
1048 IRemUnknown_Release(remunk
);
1050 WARN("IRemUnknown_RemQueryInterface failed with error %#lx\n", hr
);
1053 /* IRemUnknown_RemQueryInterface can return S_FALSE if only some of
1054 * the interfaces were returned */
1057 struct apartment
*apt
= apartment_get_current_or_mta();
1059 /* try to unmarshal each object returned to us */
1060 for (i
= 0; i
< nonlocal_mqis
; i
++)
1062 ULONG index
= mapping
[i
];
1063 HRESULT hrobj
= qiresults
[i
].hResult
;
1065 hrobj
= unmarshal_object(&qiresults
[i
].std
, apt
,
1067 This
->dest_context_data
,
1068 pMQIs
[index
].pIID
, &This
->oxid_info
,
1069 (void **)&pMQIs
[index
].pItf
);
1074 ERR("Failed to get pointer to interface %s\n", debugstr_guid(pMQIs
[index
].pIID
));
1075 pMQIs
[index
].hr
= hrobj
;
1078 apartment_release(apt
);
1081 /* free the memory allocated by the proxy */
1082 CoTaskMemFree(qiresults
);
1085 TRACE("%ld/%ld successfully queried\n", successful_mqis
, cMQIs
);
1087 HeapFree(GetProcessHeap(), 0, iids
);
1088 HeapFree(GetProcessHeap(), 0, mapping
);
1090 if (successful_mqis
== cMQIs
)
1091 return S_OK
; /* we got all requested interfaces */
1092 else if (successful_mqis
== 0)
1093 return E_NOINTERFACE
; /* we didn't get any interfaces */
1095 return S_FALSE
; /* we got some interfaces */
1098 static const IMultiQIVtbl ClientIdentity_Vtbl
=
1100 ClientIdentity_QueryInterface
,
1101 ClientIdentity_AddRef
,
1102 ClientIdentity_Release
,
1103 ClientIdentity_QueryMultipleInterfaces
1106 static HRESULT
StdMarshalImpl_Construct(REFIID
, DWORD
, void*, void**);
1108 static HRESULT WINAPI
Proxy_QueryInterface(IMarshal
*iface
, REFIID riid
, void **ppvObject
)
1110 struct proxy_manager
*This
= impl_from_IMarshal( iface
);
1111 return IMultiQI_QueryInterface(&This
->IMultiQI_iface
, riid
, ppvObject
);
1114 static ULONG WINAPI
Proxy_AddRef(IMarshal
*iface
)
1116 struct proxy_manager
*This
= impl_from_IMarshal( iface
);
1117 return IMultiQI_AddRef(&This
->IMultiQI_iface
);
1120 static ULONG WINAPI
Proxy_Release(IMarshal
*iface
)
1122 struct proxy_manager
*This
= impl_from_IMarshal( iface
);
1123 return IMultiQI_Release(&This
->IMultiQI_iface
);
1126 static HRESULT WINAPI
Proxy_GetUnmarshalClass(
1127 IMarshal
*iface
, REFIID riid
, void* pv
, DWORD dwDestContext
,
1128 void* pvDestContext
, DWORD mshlflags
, CLSID
* pCid
)
1130 *pCid
= CLSID_StdMarshal
;
1134 static HRESULT WINAPI
Proxy_GetMarshalSizeMax(
1135 IMarshal
*iface
, REFIID riid
, void* pv
, DWORD dwDestContext
,
1136 void* pvDestContext
, DWORD mshlflags
, DWORD
* pSize
)
1138 *pSize
= FIELD_OFFSET(OBJREF
, u_objref
.u_standard
.saResAddr
.aStringArray
);
1142 static void fill_std_objref(OBJREF
*objref
, const GUID
*iid
, STDOBJREF
*std
)
1144 objref
->signature
= OBJREF_SIGNATURE
;
1145 objref
->flags
= OBJREF_STANDARD
;
1148 objref
->u_objref
.u_standard
.std
= *std
;
1149 memset(&objref
->u_objref
.u_standard
.saResAddr
, 0,
1150 sizeof(objref
->u_objref
.u_standard
.saResAddr
));
1153 static HRESULT WINAPI
Proxy_MarshalInterface(
1154 LPMARSHAL iface
, IStream
*pStm
, REFIID riid
, void* pv
, DWORD dwDestContext
,
1155 void* pvDestContext
, DWORD mshlflags
)
1157 struct proxy_manager
*This
= impl_from_IMarshal( iface
);
1159 struct ifproxy
*ifproxy
;
1161 TRACE("(...,%s,...)\n", debugstr_guid(riid
));
1163 hr
= proxy_manager_find_ifproxy(This
, riid
, &ifproxy
);
1166 STDOBJREF stdobjref
= ifproxy
->stdobjref
;
1168 stdobjref
.cPublicRefs
= 0;
1170 if ((mshlflags
!= MSHLFLAGS_TABLEWEAK
) &&
1171 (mshlflags
!= MSHLFLAGS_TABLESTRONG
))
1173 ULONG cPublicRefs
= ifproxy
->refs
;
1174 ULONG cPublicRefsOld
;
1175 /* optimization - share out proxy's public references if possible
1176 * instead of making new proxy do a roundtrip through the server */
1179 ULONG cPublicRefsNew
;
1180 cPublicRefsOld
= cPublicRefs
;
1181 stdobjref
.cPublicRefs
= cPublicRefs
/ 2;
1182 cPublicRefsNew
= cPublicRefs
- stdobjref
.cPublicRefs
;
1183 cPublicRefs
= InterlockedCompareExchange(
1184 (LONG
*)&ifproxy
->refs
, cPublicRefsNew
, cPublicRefsOld
);
1185 } while (cPublicRefs
!= cPublicRefsOld
);
1188 /* normal and table-strong marshaling need at least one reference */
1189 if (!stdobjref
.cPublicRefs
&& (mshlflags
!= MSHLFLAGS_TABLEWEAK
))
1191 IRemUnknown
*remunk
;
1192 hr
= proxy_manager_get_remunknown(This
, &remunk
);
1195 HRESULT hrref
= S_OK
;
1196 REMINTERFACEREF rif
;
1197 rif
.ipid
= ifproxy
->stdobjref
.ipid
;
1198 rif
.cPublicRefs
= (mshlflags
== MSHLFLAGS_TABLESTRONG
) ? 1 : NORMALEXTREFS
;
1199 rif
.cPrivateRefs
= 0;
1200 hr
= IRemUnknown_RemAddRef(remunk
, 1, &rif
, &hrref
);
1201 IRemUnknown_Release(remunk
);
1202 if (hr
== S_OK
&& hrref
== S_OK
)
1204 /* table-strong marshaling doesn't give the refs to the
1205 * client that unmarshals the STDOBJREF */
1206 if (mshlflags
!= MSHLFLAGS_TABLESTRONG
)
1207 stdobjref
.cPublicRefs
= rif
.cPublicRefs
;
1210 ERR("IRemUnknown_RemAddRef returned with %#lx, hrref = %#lx\n", hr
, hrref
);
1218 TRACE("writing stdobjref: flags = %#lx cPublicRefs = %ld oxid = %s oid = %s ipid = %s\n",
1219 stdobjref
.flags
, stdobjref
.cPublicRefs
,
1220 wine_dbgstr_longlong(stdobjref
.oxid
),
1221 wine_dbgstr_longlong(stdobjref
.oid
),
1222 debugstr_guid(&stdobjref
.ipid
));
1223 fill_std_objref(&objref
, riid
, &stdobjref
);
1224 hr
= IStream_Write(pStm
, &objref
, FIELD_OFFSET(OBJREF
,
1225 u_objref
.u_standard
.saResAddr
.aStringArray
), NULL
);
1230 /* we don't have the interface already unmarshaled so we have to
1231 * request the object from the server */
1232 IRemUnknown
*remunk
;
1234 REMQIRESULT
*qiresults
= NULL
;
1237 /* get the ipid of the first entry */
1238 /* FIXME: should we implement ClientIdentity on the ifproxies instead
1239 * of the proxy_manager so we use the correct ipid here? */
1240 ipid
= &LIST_ENTRY(list_head(&This
->interfaces
), struct ifproxy
, entry
)->stdobjref
.ipid
;
1242 /* get IRemUnknown proxy so we can communicate with the remote object */
1243 hr
= proxy_manager_get_remunknown(This
, &remunk
);
1247 hr
= IRemUnknown_RemQueryInterface(remunk
, ipid
, NORMALEXTREFS
,
1248 1, &iid
, &qiresults
);
1253 fill_std_objref(&objref
, riid
, &qiresults
->std
);
1254 hr
= IStream_Write(pStm
, &objref
, FIELD_OFFSET(OBJREF
,
1255 u_objref
.u_standard
.saResAddr
.aStringArray
), NULL
);
1258 REMINTERFACEREF rif
;
1259 rif
.ipid
= qiresults
->std
.ipid
;
1260 rif
.cPublicRefs
= qiresults
->std
.cPublicRefs
;
1261 rif
.cPrivateRefs
= 0;
1262 IRemUnknown_RemRelease(remunk
, 1, &rif
);
1264 CoTaskMemFree(qiresults
);
1267 ERR("IRemUnknown_RemQueryInterface failed with error %#lx\n", hr
);
1268 IRemUnknown_Release(remunk
);
1275 static HRESULT WINAPI
Proxy_UnmarshalInterface(
1276 IMarshal
*iface
, IStream
*pStm
, REFIID riid
, void **ppv
)
1278 struct proxy_manager
*This
= impl_from_IMarshal( iface
);
1282 TRACE("(%p, %p, %s, %p)\n", This
, pStm
, wine_dbgstr_guid(riid
), ppv
);
1284 hr
= StdMarshalImpl_Construct(&IID_IMarshal
, This
->dest_context
,
1285 This
->dest_context_data
, (void**)&marshal
);
1289 hr
= IMarshal_UnmarshalInterface(marshal
, pStm
, riid
, ppv
);
1290 IMarshal_Release(marshal
);
1294 static HRESULT WINAPI
Proxy_ReleaseMarshalData(IMarshal
*iface
, IStream
*pStm
)
1296 struct proxy_manager
*This
= impl_from_IMarshal( iface
);
1300 TRACE("(%p, %p)\n", This
, pStm
);
1302 hr
= StdMarshalImpl_Construct(&IID_IMarshal
, This
->dest_context
,
1303 This
->dest_context_data
, (void**)&marshal
);
1307 hr
= IMarshal_ReleaseMarshalData(marshal
, pStm
);
1308 IMarshal_Release(marshal
);
1312 static HRESULT WINAPI
Proxy_DisconnectObject(IMarshal
*iface
, DWORD dwReserved
)
1314 struct proxy_manager
*This
= impl_from_IMarshal( iface
);
1318 TRACE("%p, %#lx\n", This
, dwReserved
);
1320 hr
= StdMarshalImpl_Construct(&IID_IMarshal
, This
->dest_context
,
1321 This
->dest_context_data
, (void**)&marshal
);
1325 hr
= IMarshal_DisconnectObject(marshal
, dwReserved
);
1326 IMarshal_Release(marshal
);
1330 static const IMarshalVtbl ProxyMarshal_Vtbl
=
1332 Proxy_QueryInterface
,
1335 Proxy_GetUnmarshalClass
,
1336 Proxy_GetMarshalSizeMax
,
1337 Proxy_MarshalInterface
,
1338 Proxy_UnmarshalInterface
,
1339 Proxy_ReleaseMarshalData
,
1340 Proxy_DisconnectObject
1343 static HRESULT WINAPI
ProxyCliSec_QueryInterface(IClientSecurity
*iface
, REFIID riid
, void **ppvObject
)
1345 struct proxy_manager
*This
= impl_from_IClientSecurity( iface
);
1346 return IMultiQI_QueryInterface(&This
->IMultiQI_iface
, riid
, ppvObject
);
1349 static ULONG WINAPI
ProxyCliSec_AddRef(IClientSecurity
*iface
)
1351 struct proxy_manager
*This
= impl_from_IClientSecurity( iface
);
1352 return IMultiQI_AddRef(&This
->IMultiQI_iface
);
1355 static ULONG WINAPI
ProxyCliSec_Release(IClientSecurity
*iface
)
1357 struct proxy_manager
*This
= impl_from_IClientSecurity( iface
);
1358 return IMultiQI_Release(&This
->IMultiQI_iface
);
1361 static HRESULT WINAPI
ProxyCliSec_QueryBlanket(IClientSecurity
*iface
,
1365 OLECHAR
**ppServerPrincName
,
1369 DWORD
*pCapabilities
)
1371 FIXME("(%p, %p, %p, %p, %p, %p, %p, %p): stub\n", pProxy
, pAuthnSvc
,
1372 pAuthzSvc
, ppServerPrincName
, pAuthnLevel
, pImpLevel
, pAuthInfo
,
1379 if (ppServerPrincName
)
1380 *ppServerPrincName
= NULL
;
1382 *pAuthnLevel
= RPC_C_AUTHN_LEVEL_DEFAULT
;
1384 *pImpLevel
= RPC_C_IMP_LEVEL_DEFAULT
;
1388 *pCapabilities
= EOAC_NONE
;
1393 static HRESULT WINAPI
ProxyCliSec_SetBlanket(IClientSecurity
*iface
,
1394 IUnknown
*pProxy
, DWORD AuthnSvc
,
1396 OLECHAR
*pServerPrincName
,
1397 DWORD AuthnLevel
, DWORD ImpLevel
,
1401 FIXME("%p, %ld, %ld, %s, %ld, %ld, %p, %#lx: stub\n", pProxy
, AuthnSvc
, AuthzSvc
,
1402 pServerPrincName
== COLE_DEFAULT_PRINCIPAL
? "<default principal>" : debugstr_w(pServerPrincName
),
1403 AuthnLevel
, ImpLevel
, pAuthInfo
, Capabilities
);
1407 static HRESULT WINAPI
ProxyCliSec_CopyProxy(IClientSecurity
*iface
,
1408 IUnknown
*pProxy
, IUnknown
**ppCopy
)
1410 FIXME("(%p, %p): stub\n", pProxy
, ppCopy
);
1415 static const IClientSecurityVtbl ProxyCliSec_Vtbl
=
1417 ProxyCliSec_QueryInterface
,
1419 ProxyCliSec_Release
,
1420 ProxyCliSec_QueryBlanket
,
1421 ProxyCliSec_SetBlanket
,
1422 ProxyCliSec_CopyProxy
1425 static HRESULT
ifproxy_get_public_ref(struct ifproxy
* This
)
1429 if (WAIT_OBJECT_0
!= WaitForSingleObject(This
->parent
->remoting_mutex
, INFINITE
))
1431 ERR("Wait failed for ifproxy %p\n", This
);
1432 return E_UNEXPECTED
;
1435 if (This
->refs
== 0)
1437 IRemUnknown
*remunk
= NULL
;
1439 TRACE("getting public ref for ifproxy %p\n", This
);
1441 hr
= proxy_manager_get_remunknown(This
->parent
, &remunk
);
1444 HRESULT hrref
= S_OK
;
1445 REMINTERFACEREF rif
;
1446 rif
.ipid
= This
->stdobjref
.ipid
;
1447 rif
.cPublicRefs
= NORMALEXTREFS
;
1448 rif
.cPrivateRefs
= 0;
1449 hr
= IRemUnknown_RemAddRef(remunk
, 1, &rif
, &hrref
);
1450 IRemUnknown_Release(remunk
);
1451 if (hr
== S_OK
&& hrref
== S_OK
)
1452 InterlockedExchangeAdd((LONG
*)&This
->refs
, NORMALEXTREFS
);
1454 ERR("IRemUnknown_RemAddRef returned with %#lx, hrref = %#lx\n", hr
, hrref
);
1457 ReleaseMutex(This
->parent
->remoting_mutex
);
1462 static HRESULT
ifproxy_release_public_refs(struct ifproxy
* This
)
1467 if (WAIT_OBJECT_0
!= WaitForSingleObject(This
->parent
->remoting_mutex
, INFINITE
))
1469 ERR("Wait failed for ifproxy %p\n", This
);
1470 return E_UNEXPECTED
;
1473 public_refs
= This
->refs
;
1474 if (public_refs
> 0)
1476 IRemUnknown
*remunk
= NULL
;
1478 TRACE("releasing %ld refs\n", public_refs
);
1480 hr
= proxy_manager_get_remunknown(This
->parent
, &remunk
);
1483 REMINTERFACEREF rif
;
1484 rif
.ipid
= This
->stdobjref
.ipid
;
1485 rif
.cPublicRefs
= public_refs
;
1486 rif
.cPrivateRefs
= 0;
1487 hr
= IRemUnknown_RemRelease(remunk
, 1, &rif
);
1488 IRemUnknown_Release(remunk
);
1490 InterlockedExchangeAdd((LONG
*)&This
->refs
, -public_refs
);
1491 else if (hr
== RPC_E_DISCONNECTED
)
1492 WARN("couldn't release references because object was "
1493 "disconnected: oxid = %s, oid = %s\n",
1494 wine_dbgstr_longlong(This
->parent
->oxid
),
1495 wine_dbgstr_longlong(This
->parent
->oid
));
1497 ERR("IRemUnknown_RemRelease failed with error %#lx\n", hr
);
1500 ReleaseMutex(This
->parent
->remoting_mutex
);
1505 /* should be called inside This->parent->cs critical section */
1506 static void ifproxy_disconnect(struct ifproxy
* This
)
1508 ifproxy_release_public_refs(This
);
1509 if (This
->proxy
) IRpcProxyBuffer_Disconnect(This
->proxy
);
1511 IRpcChannelBuffer_Release(This
->chan
);
1515 /* should be called in This->parent->cs critical section if it is an entry in parent's list */
1516 static void ifproxy_destroy(struct ifproxy
* This
)
1518 TRACE("%p\n", This
);
1520 /* release public references to this object so that the stub can know
1521 * when to destroy itself */
1522 ifproxy_release_public_refs(This
);
1524 list_remove(&This
->entry
);
1528 IRpcChannelBuffer_Release(This
->chan
);
1532 if (This
->proxy
) IRpcProxyBuffer_Release(This
->proxy
);
1534 HeapFree(GetProcessHeap(), 0, This
);
1537 static HRESULT
proxy_manager_construct(
1538 struct apartment
* apt
, ULONG sorflags
, OXID oxid
, OID oid
,
1539 const OXID_INFO
*oxid_info
, struct proxy_manager
** proxy_manager
)
1541 struct proxy_manager
* This
= HeapAlloc(GetProcessHeap(), 0, sizeof(*This
));
1542 if (!This
) return E_OUTOFMEMORY
;
1544 This
->remoting_mutex
= CreateMutexW(NULL
, FALSE
, NULL
);
1545 if (!This
->remoting_mutex
)
1547 HeapFree(GetProcessHeap(), 0, This
);
1548 return HRESULT_FROM_WIN32(GetLastError());
1553 This
->oxid_info
.dwPid
= oxid_info
->dwPid
;
1554 This
->oxid_info
.dwTid
= oxid_info
->dwTid
;
1555 This
->oxid_info
.ipidRemUnknown
= oxid_info
->ipidRemUnknown
;
1556 This
->oxid_info
.dwAuthnHint
= oxid_info
->dwAuthnHint
;
1557 This
->oxid_info
.psa
= NULL
/* FIXME: copy from oxid_info */;
1561 HRESULT hr
= rpc_resolve_oxid(oxid
, &This
->oxid_info
);
1564 CloseHandle(This
->remoting_mutex
);
1565 HeapFree(GetProcessHeap(), 0, This
);
1570 This
->IMultiQI_iface
.lpVtbl
= &ClientIdentity_Vtbl
;
1571 This
->IMarshal_iface
.lpVtbl
= &ProxyMarshal_Vtbl
;
1572 This
->IClientSecurity_iface
.lpVtbl
= &ProxyCliSec_Vtbl
;
1574 list_init(&This
->entry
);
1575 list_init(&This
->interfaces
);
1577 InitializeCriticalSection(&This
->cs
);
1578 This
->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": proxy_manager");
1580 /* the apartment the object was unmarshaled into */
1583 /* the source apartment and id of the object */
1589 /* the DCOM draft specification states that the SORF_NOPING flag is
1590 * proxy manager specific, not ifproxy specific, so this implies that we
1591 * should store the STDOBJREF flags here in the proxy manager. */
1592 This
->sorflags
= sorflags
;
1594 /* we create the IRemUnknown proxy on demand */
1595 This
->remunk
= NULL
;
1597 /* initialise these values to the weakest values and they will be
1598 * overwritten in proxy_manager_set_context */
1599 This
->dest_context
= MSHCTX_INPROC
;
1600 This
->dest_context_data
= NULL
;
1602 EnterCriticalSection(&apt
->cs
);
1603 /* FIXME: we are dependent on the ordering in here to make sure a proxy's
1604 * IRemUnknown proxy doesn't get destroyed before the regular proxy does
1605 * because we need the IRemUnknown proxy during the destruction of the
1606 * regular proxy. Ideally, we should maintain a separate list for the
1607 * IRemUnknown proxies that need late destruction */
1608 list_add_tail(&apt
->proxies
, &This
->entry
);
1609 LeaveCriticalSection(&apt
->cs
);
1611 TRACE("%p created for OXID %s, OID %s\n", This
,
1612 wine_dbgstr_longlong(oxid
), wine_dbgstr_longlong(oid
));
1614 *proxy_manager
= This
;
1618 static inline void proxy_manager_set_context(struct proxy_manager
*This
, MSHCTX dest_context
, void *dest_context_data
)
1620 MSHCTX old_dest_context
;
1621 MSHCTX new_dest_context
;
1625 old_dest_context
= This
->dest_context
;
1626 new_dest_context
= old_dest_context
;
1627 /* "stronger" values overwrite "weaker" values. stronger values are
1628 * ones that disable more optimisations */
1629 switch (old_dest_context
)
1632 new_dest_context
= dest_context
;
1634 case MSHCTX_CROSSCTX
:
1635 switch (dest_context
)
1640 new_dest_context
= dest_context
;
1644 switch (dest_context
)
1647 case MSHCTX_CROSSCTX
:
1650 new_dest_context
= dest_context
;
1653 case MSHCTX_NOSHAREDMEM
:
1654 switch (dest_context
)
1656 case MSHCTX_DIFFERENTMACHINE
:
1657 new_dest_context
= dest_context
;
1667 if (old_dest_context
== new_dest_context
) break;
1669 new_dest_context
= InterlockedCompareExchange((PLONG
)&This
->dest_context
, new_dest_context
, old_dest_context
);
1670 } while (new_dest_context
!= old_dest_context
);
1672 if (dest_context_data
)
1673 InterlockedExchangePointer(&This
->dest_context_data
, dest_context_data
);
1676 static HRESULT
proxy_manager_query_local_interface(struct proxy_manager
* This
, REFIID riid
, void ** ppv
)
1679 struct ifproxy
* ifproxy
;
1681 TRACE("%s\n", debugstr_guid(riid
));
1683 if (IsEqualIID(riid
, &IID_IUnknown
) ||
1684 IsEqualIID(riid
, &IID_IMultiQI
))
1686 *ppv
= &This
->IMultiQI_iface
;
1687 IMultiQI_AddRef(&This
->IMultiQI_iface
);
1690 if (IsEqualIID(riid
, &IID_IMarshal
))
1692 *ppv
= &This
->IMarshal_iface
;
1693 IMarshal_AddRef(&This
->IMarshal_iface
);
1696 if (IsEqualIID(riid
, &IID_IClientSecurity
))
1698 *ppv
= &This
->IClientSecurity_iface
;
1699 IClientSecurity_AddRef(&This
->IClientSecurity_iface
);
1703 hr
= proxy_manager_find_ifproxy(This
, riid
, &ifproxy
);
1706 *ppv
= ifproxy
->iface
;
1707 IUnknown_AddRef((IUnknown
*)*ppv
);
1712 return E_NOINTERFACE
;
1715 static HRESULT
proxy_manager_create_ifproxy(
1716 struct proxy_manager
* This
, const STDOBJREF
*stdobjref
, REFIID riid
,
1717 IRpcChannelBuffer
* channel
, struct ifproxy
** iif_out
)
1720 IPSFactoryBuffer
* psfb
;
1721 struct ifproxy
* ifproxy
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ifproxy
));
1722 if (!ifproxy
) return E_OUTOFMEMORY
;
1724 list_init(&ifproxy
->entry
);
1726 ifproxy
->parent
= This
;
1727 ifproxy
->stdobjref
= *stdobjref
;
1728 ifproxy
->iid
= *riid
;
1730 ifproxy
->proxy
= NULL
;
1733 ifproxy
->chan
= channel
; /* FIXME: we should take the binding strings and construct the channel in this function */
1735 /* the IUnknown interface is special because it does not have a
1736 * proxy associated with the ifproxy as we handle IUnknown ourselves */
1737 if (IsEqualIID(riid
, &IID_IUnknown
))
1739 ifproxy
->iface
= &This
->IMultiQI_iface
;
1740 IMultiQI_AddRef(&This
->IMultiQI_iface
);
1745 hr
= get_facbuf_for_iid(riid
, &psfb
);
1748 /* important note: the outer unknown is set to the proxy manager.
1749 * This ensures the COM identity rules are not violated, by having a
1750 * one-to-one mapping of objects on the proxy side to objects on the
1751 * stub side, no matter which interface you view the object through */
1752 hr
= IPSFactoryBuffer_CreateProxy(psfb
, (IUnknown
*)&This
->IMultiQI_iface
, riid
,
1753 &ifproxy
->proxy
, &ifproxy
->iface
);
1754 IPSFactoryBuffer_Release(psfb
);
1756 ERR("Could not create proxy for interface %s, error %#lx\n", debugstr_guid(riid
), hr
);
1759 ERR("Could not get IPSFactoryBuffer for interface %s, error %#lx\n", debugstr_guid(riid
), hr
);
1762 hr
= IRpcProxyBuffer_Connect(ifproxy
->proxy
, ifproxy
->chan
);
1767 EnterCriticalSection(&This
->cs
);
1768 list_add_tail(&This
->interfaces
, &ifproxy
->entry
);
1769 LeaveCriticalSection(&This
->cs
);
1772 TRACE("ifproxy %p created for IPID %s, interface %s with %lu public refs\n",
1773 ifproxy
, debugstr_guid(&stdobjref
->ipid
), debugstr_guid(riid
), stdobjref
->cPublicRefs
);
1776 ifproxy_destroy(ifproxy
);
1781 static HRESULT
proxy_manager_find_ifproxy(struct proxy_manager
* This
, REFIID riid
, struct ifproxy
** ifproxy_found
)
1783 HRESULT hr
= E_NOINTERFACE
; /* assume not found */
1784 struct ifproxy
*ifproxy
;
1786 EnterCriticalSection(&This
->cs
);
1787 LIST_FOR_EACH_ENTRY(ifproxy
, &This
->interfaces
, struct ifproxy
, entry
)
1789 if (IsEqualIID(riid
, &ifproxy
->iid
))
1791 *ifproxy_found
= ifproxy
;
1796 LeaveCriticalSection(&This
->cs
);
1801 static void proxy_manager_disconnect(struct proxy_manager
* This
)
1803 struct ifproxy
*ifproxy
;
1805 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This
->oxid
),
1806 wine_dbgstr_longlong(This
->oid
));
1808 EnterCriticalSection(&This
->cs
);
1810 /* SORFP_NOLIFTIMEMGMT proxies (for IRemUnknown) shouldn't be
1811 * disconnected - it won't do anything anyway, except cause
1812 * problems for other objects that depend on this proxy always
1814 if (!(This
->sorflags
& SORFP_NOLIFETIMEMGMT
))
1816 LIST_FOR_EACH_ENTRY(ifproxy
, &This
->interfaces
, struct ifproxy
, entry
)
1818 ifproxy_disconnect(ifproxy
);
1822 /* apartment is being destroyed so don't keep a pointer around to it */
1823 This
->parent
= NULL
;
1825 LeaveCriticalSection(&This
->cs
);
1828 static HRESULT
proxy_manager_get_remunknown(struct proxy_manager
* This
, IRemUnknown
**remunk
)
1831 struct apartment
*apt
;
1832 BOOL called_in_original_apt
;
1834 /* we don't want to try and unmarshal or use IRemUnknown if we don't want
1835 * lifetime management */
1836 if (This
->sorflags
& SORFP_NOLIFETIMEMGMT
)
1839 if (!(apt
= apartment_get_current_or_mta()))
1840 return CO_E_NOTINITIALIZED
;
1842 called_in_original_apt
= This
->parent
&& (This
->parent
->oxid
== apt
->oxid
);
1844 EnterCriticalSection(&This
->cs
);
1845 /* only return the cached object if called from the original apartment.
1846 * in future, we might want to make the IRemUnknown proxy callable from any
1847 * apartment to avoid these checks */
1848 if (This
->remunk
&& called_in_original_apt
)
1850 /* already created - return existing object */
1851 *remunk
= This
->remunk
;
1852 IRemUnknown_AddRef(*remunk
);
1854 else if (!This
->parent
)
1856 /* disconnected - we can't create IRemUnknown */
1862 STDOBJREF stdobjref
;
1863 /* Don't want IRemUnknown lifetime management as this is IRemUnknown!
1864 * We also don't care about whether or not the stub is still alive */
1865 stdobjref
.flags
= SORFP_NOLIFETIMEMGMT
| SORF_NOPING
;
1866 stdobjref
.cPublicRefs
= 1;
1867 /* oxid of destination object */
1868 stdobjref
.oxid
= This
->oxid
;
1869 /* FIXME: what should be used for the oid? The DCOM draft doesn't say */
1870 stdobjref
.oid
= (OID
)-1;
1871 stdobjref
.ipid
= This
->oxid_info
.ipidRemUnknown
;
1873 /* do the unmarshal */
1874 hr
= unmarshal_object(&stdobjref
, apt
, This
->dest_context
,
1875 This
->dest_context_data
, &IID_IRemUnknown
,
1876 &This
->oxid_info
, (void**)remunk
);
1877 if (hr
== S_OK
&& called_in_original_apt
)
1879 This
->remunk
= *remunk
;
1880 IRemUnknown_AddRef(This
->remunk
);
1883 LeaveCriticalSection(&This
->cs
);
1884 apartment_release(apt
);
1886 TRACE("got IRemUnknown* pointer %p, hr = %#lx\n", *remunk
, hr
);
1891 /* destroys a proxy manager, freeing the memory it used.
1892 * Note: this function should not be called from a list iteration in the
1893 * apartment, due to the fact that it removes itself from the apartment and
1894 * it could add a proxy to IRemUnknown into the apartment. */
1895 static void proxy_manager_destroy(struct proxy_manager
* This
)
1897 struct list
* cursor
;
1899 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This
->oxid
),
1900 wine_dbgstr_longlong(This
->oid
));
1904 EnterCriticalSection(&This
->parent
->cs
);
1906 /* remove ourself from the list of proxy objects in the apartment */
1907 LIST_FOR_EACH(cursor
, &This
->parent
->proxies
)
1909 if (cursor
== &This
->entry
)
1911 list_remove(&This
->entry
);
1916 LeaveCriticalSection(&This
->parent
->cs
);
1919 /* destroy all of the interface proxies */
1920 while ((cursor
= list_head(&This
->interfaces
)))
1922 struct ifproxy
* ifproxy
= LIST_ENTRY(cursor
, struct ifproxy
, entry
);
1923 ifproxy_destroy(ifproxy
);
1926 if (This
->remunk
) IRemUnknown_Release(This
->remunk
);
1927 CoTaskMemFree(This
->oxid_info
.psa
);
1929 This
->cs
.DebugInfo
->Spare
[0] = 0;
1930 DeleteCriticalSection(&This
->cs
);
1932 CloseHandle(This
->remoting_mutex
);
1934 HeapFree(GetProcessHeap(), 0, This
);
1937 /* finds the proxy manager corresponding to a given OXID and OID that has
1938 * been unmarshaled in the specified apartment. The caller must release the
1939 * reference to the proxy_manager when the object is no longer used. */
1940 static BOOL
find_proxy_manager(struct apartment
* apt
, OXID oxid
, OID oid
, struct proxy_manager
** proxy_found
)
1942 struct proxy_manager
*proxy
;
1945 EnterCriticalSection(&apt
->cs
);
1946 LIST_FOR_EACH_ENTRY(proxy
, &apt
->proxies
, struct proxy_manager
, entry
)
1948 if ((oxid
== proxy
->oxid
) && (oid
== proxy
->oid
))
1950 /* be careful of a race with ClientIdentity_Release, which would
1951 * cause us to return a proxy which is in the process of being
1953 if (IMultiQI_AddRef(&proxy
->IMultiQI_iface
) != 0)
1955 *proxy_found
= proxy
;
1961 LeaveCriticalSection(&apt
->cs
);
1965 HRESULT
apartment_disconnectproxies(struct apartment
*apt
)
1967 struct proxy_manager
*proxy
;
1969 LIST_FOR_EACH_ENTRY(proxy
, &apt
->proxies
, struct proxy_manager
, entry
)
1971 proxy_manager_disconnect(proxy
);
1977 /********************** StdMarshal implementation ****************************/
1981 IMarshal IMarshal_iface
;
1984 void *dest_context_data
;
1987 static inline struct stdmarshal
*impl_from_StdMarshal(IMarshal
*iface
)
1989 return CONTAINING_RECORD(iface
, struct stdmarshal
, IMarshal_iface
);
1992 static HRESULT WINAPI
StdMarshalImpl_QueryInterface(IMarshal
*iface
, REFIID riid
, void **ppv
)
1995 if (IsEqualIID(&IID_IUnknown
, riid
) || IsEqualIID(&IID_IMarshal
, riid
))
1998 IMarshal_AddRef(iface
);
2001 FIXME("No interface for %s.\n", debugstr_guid(riid
));
2002 return E_NOINTERFACE
;
2005 static ULONG WINAPI
StdMarshalImpl_AddRef(IMarshal
*iface
)
2007 struct stdmarshal
*marshal
= impl_from_StdMarshal(iface
);
2008 return InterlockedIncrement(&marshal
->refcount
);
2011 static ULONG WINAPI
StdMarshalImpl_Release(IMarshal
*iface
)
2013 struct stdmarshal
*marshal
= impl_from_StdMarshal(iface
);
2014 ULONG refcount
= InterlockedDecrement(&marshal
->refcount
);
2022 static HRESULT WINAPI
StdMarshalImpl_GetUnmarshalClass(IMarshal
*iface
, REFIID riid
, void *pv
,
2023 DWORD dwDestContext
, void *pvDestContext
, DWORD mshlflags
, CLSID
*pCid
)
2025 *pCid
= CLSID_StdMarshal
;
2029 static HRESULT WINAPI
StdMarshalImpl_GetMarshalSizeMax(IMarshal
*iface
, REFIID riid
, void *pv
,
2030 DWORD dwDestContext
, void *pvDestContext
, DWORD mshlflags
, DWORD
*pSize
)
2032 *pSize
= FIELD_OFFSET(OBJREF
, u_objref
.u_standard
.saResAddr
.aStringArray
);
2036 static HRESULT WINAPI
StdMarshalImpl_MarshalInterface(IMarshal
*iface
, IStream
*stream
, REFIID riid
, void *pv
,
2037 DWORD dest_context
, void *dest_context_data
, DWORD mshlflags
)
2042 struct apartment
*apt
;
2044 TRACE("(...,%s,...)\n", debugstr_guid(riid
));
2046 if (!(apt
= apartment_get_current_or_mta()))
2048 ERR("Apartment not initialized\n");
2049 return CO_E_NOTINITIALIZED
;
2052 /* make sure this apartment can be reached from other threads / processes */
2053 rpc_start_remoting(apt
);
2055 fill_std_objref(&objref
, riid
, NULL
);
2056 hr
= marshal_object(apt
, &objref
.u_objref
.u_standard
.std
, riid
, pv
, dest_context
,
2057 dest_context_data
, mshlflags
);
2058 apartment_release(apt
);
2061 ERR("Failed to create ifstub, hr %#lx\n", hr
);
2065 return IStream_Write(stream
, &objref
, FIELD_OFFSET(OBJREF
, u_objref
.u_standard
.saResAddr
.aStringArray
), &res
);
2068 /* helper for StdMarshalImpl_UnmarshalInterface - does the unmarshaling with
2069 * no questions asked about the rules surrounding same-apartment unmarshals
2070 * and table marshaling */
2071 static HRESULT
unmarshal_object(const STDOBJREF
*stdobjref
, struct apartment
*apt
, MSHCTX dest_context
,
2072 void *dest_context_data
, REFIID riid
, const OXID_INFO
*oxid_info
, void **object
)
2074 struct proxy_manager
*proxy_manager
= NULL
;
2079 TRACE("stdobjref: flags = %#lx cPublicRefs = %ld oxid = %s oid = %s ipid = %s\n",
2080 stdobjref
->flags
, stdobjref
->cPublicRefs
,
2081 wine_dbgstr_longlong(stdobjref
->oxid
),
2082 wine_dbgstr_longlong(stdobjref
->oid
),
2083 debugstr_guid(&stdobjref
->ipid
));
2085 /* create a new proxy manager if one doesn't already exist for the
2087 if (!find_proxy_manager(apt
, stdobjref
->oxid
, stdobjref
->oid
, &proxy_manager
))
2089 hr
= proxy_manager_construct(apt
, stdobjref
->flags
,
2090 stdobjref
->oxid
, stdobjref
->oid
, oxid_info
,
2094 TRACE("proxy manager already created, using\n");
2098 struct ifproxy
* ifproxy
;
2100 proxy_manager_set_context(proxy_manager
, dest_context
, dest_context_data
);
2102 hr
= proxy_manager_find_ifproxy(proxy_manager
, riid
, &ifproxy
);
2103 if (hr
== E_NOINTERFACE
)
2105 IRpcChannelBuffer
*chanbuf
;
2106 hr
= rpc_create_clientchannel(&stdobjref
->oxid
, &stdobjref
->ipid
,
2107 &proxy_manager
->oxid_info
, riid
, proxy_manager
->dest_context
,
2108 proxy_manager
->dest_context_data
, &chanbuf
, apt
);
2110 hr
= proxy_manager_create_ifproxy(proxy_manager
, stdobjref
, riid
, chanbuf
, &ifproxy
);
2113 IUnknown_AddRef((IUnknown
*)ifproxy
->iface
);
2117 InterlockedExchangeAdd((LONG
*)&ifproxy
->refs
, stdobjref
->cPublicRefs
);
2118 /* get at least one external reference to the object to keep it alive */
2119 hr
= ifproxy_get_public_ref(ifproxy
);
2121 ifproxy_destroy(ifproxy
);
2125 *object
= ifproxy
->iface
;
2128 /* release our reference to the proxy manager - the client/apartment
2129 * will hold on to the remaining reference for us */
2130 if (proxy_manager
) IMultiQI_Release(&proxy_manager
->IMultiQI_iface
);
2135 static HRESULT WINAPI
StdMarshalImpl_UnmarshalInterface(IMarshal
*iface
, IStream
*stream
, REFIID riid
, void **ppv
)
2137 struct stdmarshal
*marshal
= impl_from_StdMarshal(iface
);
2142 hr
= IStream_Read(stream
, &objref
, FIELD_OFFSET(OBJREF
, u_objref
), &res
);
2143 if (hr
!= S_OK
|| (res
!= FIELD_OFFSET(OBJREF
, u_objref
)))
2145 ERR("Failed to read common OBJREF header, %#lx\n", hr
);
2146 return STG_E_READFAULT
;
2149 if (objref
.signature
!= OBJREF_SIGNATURE
)
2151 ERR("Bad OBJREF signature %#lx\n", objref
.signature
);
2152 return RPC_E_INVALID_OBJREF
;
2155 if (!(objref
.flags
& OBJREF_STANDARD
))
2157 FIXME("unsupported objref.flags = %lx\n", objref
.flags
);
2161 return std_unmarshal_interface(marshal
->dest_context
, marshal
->dest_context_data
, stream
, riid
, ppv
);
2164 static HRESULT WINAPI
StdMarshalImpl_ReleaseMarshalData(IMarshal
*iface
, IStream
*stream
)
2170 TRACE("%p, %p\n", iface
, stream
);
2172 hr
= IStream_Read(stream
, &objref
, FIELD_OFFSET(OBJREF
, u_objref
), &res
);
2173 if (hr
!= S_OK
|| (res
!= FIELD_OFFSET(OBJREF
, u_objref
)))
2175 ERR("Failed to read common OBJREF header, %#lx\n", hr
);
2176 return STG_E_READFAULT
;
2179 if (objref
.signature
!= OBJREF_SIGNATURE
)
2181 ERR("Bad OBJREF signature %#lx\n", objref
.signature
);
2182 return RPC_E_INVALID_OBJREF
;
2185 if (!(objref
.flags
& OBJREF_STANDARD
))
2187 FIXME("unsupported objref.flags = %lx\n", objref
.flags
);
2191 return std_release_marshal_data(stream
);
2194 static HRESULT WINAPI
StdMarshalImpl_DisconnectObject(IMarshal
*iface
, DWORD reserved
)
2196 FIXME("(), stub!\n");
2200 static const IMarshalVtbl StdMarshalVtbl
=
2202 StdMarshalImpl_QueryInterface
,
2203 StdMarshalImpl_AddRef
,
2204 StdMarshalImpl_Release
,
2205 StdMarshalImpl_GetUnmarshalClass
,
2206 StdMarshalImpl_GetMarshalSizeMax
,
2207 StdMarshalImpl_MarshalInterface
,
2208 StdMarshalImpl_UnmarshalInterface
,
2209 StdMarshalImpl_ReleaseMarshalData
,
2210 StdMarshalImpl_DisconnectObject
2213 static HRESULT
StdMarshalImpl_Construct(REFIID riid
, DWORD dest_context
, void *dest_context_data
, void **ppvObject
)
2215 struct stdmarshal
*object
;
2218 object
= heap_alloc(sizeof(*object
));
2220 return E_OUTOFMEMORY
;
2222 object
->IMarshal_iface
.lpVtbl
= &StdMarshalVtbl
;
2223 object
->refcount
= 1;
2224 object
->dest_context
= dest_context
;
2225 object
->dest_context_data
= dest_context_data
;
2227 hr
= IMarshal_QueryInterface(&object
->IMarshal_iface
, riid
, ppvObject
);
2228 IMarshal_Release(&object
->IMarshal_iface
);
2233 HRESULT WINAPI
InternalCoStdMarshalObject(REFIID riid
, DWORD dest_context
, void *dest_context_data
, void **ppvObject
)
2235 return StdMarshalImpl_Construct(riid
, dest_context
, dest_context_data
, ppvObject
);
2238 /***********************************************************************
2239 * CoGetStandardMarshal (combase.@)
2241 HRESULT WINAPI
CoGetStandardMarshal(REFIID riid
, IUnknown
*pUnk
, DWORD dwDestContext
,
2242 void *dest_context
, DWORD flags
, IMarshal
**marshal
)
2246 FIXME("%s, NULL, %lx, %p, %lx, %p, unimplemented yet.\n", debugstr_guid(riid
), dwDestContext
,
2247 dest_context
, flags
, marshal
);
2250 TRACE("%s, %p, %lx, %p, %lx, %p\n", debugstr_guid(riid
), pUnk
, dwDestContext
, dest_context
, flags
, marshal
);
2252 return StdMarshalImpl_Construct(&IID_IMarshal
, dwDestContext
, dest_context
, (void **)marshal
);