d3d10: Return the read value from read_dword().
[wine.git] / dlls / combase / marshal.c
blobb943c0dd6fa296641fe8aaccb031020be0f59195
1 /*
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
22 #include <assert.h>
24 #define COBJMACROS
25 #include "objbase.h"
27 #include "dcom.h"
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,
43 void **object);
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 */
55 struct ifproxy
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 */
68 struct 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);
103 struct ftmarshaler
105 IUnknown IUnknown_inner;
106 IMarshal IMarshal_iface;
107 IUnknown *outer_unk;
108 LONG refcount;
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)
142 HRESULT hr;
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);
149 return hr;
152 /***********************************************************************
153 * CoMarshalInterThreadInterfaceInStream (combase.@)
155 HRESULT WINAPI CoMarshalInterThreadInterfaceInStream(REFIID riid, IUnknown *unk, IStream **stream)
157 ULARGE_INTEGER xpos;
158 LARGE_INTEGER seekto;
159 HRESULT hr;
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);
167 if (SUCCEEDED(hr))
169 memset(&seekto, 0, sizeof(seekto));
170 IStream_Seek(*stream, seekto, STREAM_SEEK_SET, &xpos);
172 else
174 IStream_Release(*stream);
175 *stream = NULL;
178 return hr;
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);
187 *obj = NULL;
189 if (IsEqualIID(&IID_IUnknown, riid))
190 *obj = &marshaler->IUnknown_inner;
191 else if (IsEqualIID(&IID_IMarshal, riid))
192 *obj = &marshaler->IMarshal_iface;
193 else
195 FIXME("No interface for %s\n", debugstr_guid(riid));
196 return E_NOINTERFACE;
199 IUnknown_AddRef((IUnknown *)*obj);
201 return S_OK;
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);
211 return 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);
221 if (!refcount)
222 heap_free(marshaler);
224 return refcount;
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;
268 else
269 *clsid = CLSID_StdMarshal;
271 return S_OK;
274 union ftmarshaler_data
276 UINT64 value;
277 IUnknown *object;
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;
284 HRESULT hr;
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);
293 return S_OK;
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);
300 return hr;
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;
307 HRESULT hr;
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 };
319 data.value = 0;
320 hr = IUnknown_QueryInterface((IUnknown *)pv, riid, (void **)&data.object);
321 if (FAILED(hr))
322 return hr;
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;
337 return S_OK;
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);
344 return hr;
347 static HRESULT WINAPI ftmarshaler_UnmarshalInterface(IMarshal *iface, IStream *stream, REFIID riid, void **ppv)
349 union ftmarshaler_data data;
350 DWORD mshlflags;
351 GUID unknown_guid;
352 HRESULT hr;
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);
369 return hr;
372 static HRESULT WINAPI ftmarshaler_ReleaseMarshalData(IMarshal *iface, IStream *stream)
374 union ftmarshaler_data data;
375 DWORD mshlflags;
376 GUID unknown_guid;
377 HRESULT hr;
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);
391 return S_OK;
394 static HRESULT WINAPI ftmarshaler_DisconnectObject(IMarshal *iface, DWORD reserved)
396 TRACE("\n");
398 /* nothing to do */
399 return S_OK;
402 static const IMarshalVtbl ftmarshaler_vtbl =
404 ftmarshaler_QueryInterface,
405 ftmarshaler_AddRef,
406 ftmarshaler_Release,
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));
425 if (!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;
435 return S_OK;
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;
445 IMarshal *marshal;
446 HRESULT hr;
448 if (!unk)
449 return E_POINTER;
451 hr = IUnknown_QueryInterface(unk, &IID_IMarshal, (void **)&marshal);
452 if (hr != S_OK)
454 std_marshal = TRUE;
455 hr = CoGetStandardMarshal(riid, unk, dest_context, pvDestContext, mshlFlags, &marshal);
457 if (hr != S_OK)
458 return hr;
460 hr = IMarshal_GetMarshalSizeMax(marshal, riid, unk, dest_context, pvDestContext, mshlFlags, size);
461 if (!std_marshal)
462 /* add on the size of the whole OBJREF structure like native does */
463 *size += sizeof(OBJREF);
465 IMarshal_Release(marshal);
466 return hr;
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;
488 IMarshal *marshal;
489 HRESULT hr;
491 TRACE("%p, %s, %p, %lx, %p, ", stream, debugstr_guid(riid), unk, dest_context, pvDestContext);
492 dump_mshflags(mshlFlags);
493 TRACE("\n");
495 if (!unk || !stream)
496 return E_INVALIDARG;
498 hr = IUnknown_QueryInterface(unk, &IID_IMarshal, (void **)&marshal);
499 if (hr != S_OK)
500 hr = CoGetStandardMarshal(riid, unk, dest_context, pvDestContext, mshlFlags, &marshal);
501 if (hr != S_OK)
503 ERR("Failed to get marshaller, %#lx\n", hr);
504 return hr;
507 hr = IMarshal_GetUnmarshalClass(marshal, riid, unk, dest_context, pvDestContext, mshlFlags,
508 &marshaler_clsid);
509 if (hr != S_OK)
511 ERR("IMarshal::GetUnmarshalClass failed, %#lx\n", hr);
512 goto cleanup;
515 /* FIXME: implement handler marshaling too */
516 if (IsEqualCLSID(&marshaler_clsid, &CLSID_StdMarshal))
518 TRACE("Using standard marshaling\n");
520 else
522 OBJREF objref;
524 TRACE("Using custom marshaling\n");
525 objref.signature = OBJREF_SIGNATURE;
526 objref.iid = *riid;
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);
533 if (hr != S_OK)
535 ERR("Failed to get max size of marshal data, error %#lx\n", hr);
536 goto cleanup;
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);
540 if (hr != S_OK)
542 ERR("Failed to write OR_CUSTOM header to stream with %#lx\n", hr);
543 goto cleanup;
547 TRACE("Calling IMarshal::MarshalInterface\n");
549 hr = IMarshal_MarshalInterface(marshal, stream, riid, unk, dest_context, pvDestContext, mshlFlags);
550 if (hr != S_OK)
552 ERR("Failed to marshal the interface %s, hr %#lx\n", debugstr_guid(riid), hr);
553 goto cleanup;
556 cleanup:
557 IMarshal_Release(marshal);
559 TRACE("completed with hr %#lx\n", hr);
561 return 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)
570 OBJREF objref;
571 HRESULT hr;
572 ULONG res;
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");
595 *marshal = NULL;
596 return S_FALSE;
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,
614 (LPVOID*)marshal);
616 else
618 FIXME("Invalid or unimplemented marshaling type specified: %lx\n", objref.flags);
619 return RPC_E_INVALID_OBJREF;
622 if (hr != S_OK)
623 ERR("Failed to create marshal, %#lx\n", hr);
625 return hr;
628 static HRESULT std_release_marshal_data(IStream *stream)
630 struct stub_manager *stubmgr;
631 struct OR_STANDARD obj;
632 struct apartment *apt;
633 ULONG res;
634 HRESULT hr;
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");
642 return E_NOTIMPL;
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);
668 return S_OK;
671 /***********************************************************************
672 * CoReleaseMarshalData (combase.@)
674 HRESULT WINAPI CoReleaseMarshalData(IStream *stream)
676 IMarshal *marshal;
677 HRESULT hr;
679 TRACE("%p\n", stream);
681 hr = get_unmarshaler_from_stream(stream, &marshal, NULL);
682 if (hr == S_FALSE)
684 hr = std_release_marshal_data(stream);
685 if (hr != S_OK)
686 ERR("StdMarshal ReleaseMarshalData failed with error %#lx\n", hr);
687 return hr;
689 if (hr != S_OK)
690 return hr;
692 /* call the helper object to do the releasing of marshal data */
693 hr = IMarshal_ReleaseMarshalData(marshal, stream);
694 if (hr != S_OK)
695 ERR("IMarshal::ReleaseMarshalData failed with error %#lx\n", hr);
697 IMarshal_Release(marshal);
698 return hr;
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;
706 ULONG res;
707 HRESULT hres;
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);
721 if (hres != S_OK)
723 apartment_release(apt);
724 return STG_E_READFAULT;
727 if (obj.saResAddr.wNumEntries)
729 ERR("unsupported size of DUALSTRINGARRAY\n");
730 return E_NOTIMPL;
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);
747 return hres;
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;
762 else
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;
770 else
771 TRACE("Treating unmarshal from OXID %s as inter-process\n",
772 wine_dbgstr_longlong(obj.std.oxid));
774 if (hres == S_OK)
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);
786 return hres;
789 /***********************************************************************
790 * CoUnmarshalInterface (combase.@)
792 HRESULT WINAPI CoUnmarshalInterface(IStream *stream, REFIID riid, void **ppv)
794 IMarshal *marshal;
795 IUnknown *object;
796 HRESULT hr;
797 IID iid;
799 TRACE("%p, %s, %p\n", stream, debugstr_guid(riid), ppv);
801 if (!stream || !ppv)
802 return E_INVALIDARG;
804 hr = get_unmarshaler_from_stream(stream, &marshal, &iid);
805 if (hr == S_FALSE)
807 hr = std_unmarshal_interface(0, NULL, stream, &iid, (void **)&object);
808 if (hr != S_OK)
809 ERR("StdMarshal UnmarshalInterface failed, hr %#lx\n", hr);
811 else if (hr == S_OK)
813 /* call the helper object to do the actual unmarshaling */
814 hr = IMarshal_UnmarshalInterface(marshal, stream, &iid, (void **)&object);
815 IMarshal_Release(marshal);
816 if (hr != S_OK)
817 ERR("IMarshal::UnmarshalInterface failed, hr %#lx\n", hr);
820 if (hr == S_OK)
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);
827 if (hr != S_OK)
828 ERR("Couldn't query for interface %s, hr %#lx\n", debugstr_guid(riid), hr);
829 IUnknown_Release(object);
831 else
833 *ppv = object;
837 TRACE("completed with hr %#lx\n", hr);
839 return 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)
859 HRESULT hr;
860 CLSID clsid;
862 hr = CoGetPSClsid(riid, &clsid);
863 if (hr != S_OK)
864 return hr;
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;
874 BOOL tablemarshal;
875 HRESULT hr;
877 stdobjref->oxid = apartment_getoxid(apt);
879 hr = apartment_createwindowifneeded(apt);
880 if (hr != S_OK)
881 return hr;
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);
897 if (!ifstub) {
898 IRpcStubBuffer *stub = NULL;
900 /* IUnknown doesn't require a stub buffer, because it never goes out on
901 * the wire */
902 if (!IsEqualIID(riid, &IID_IUnknown))
904 IPSFactoryBuffer *psfb;
906 hr = get_facbuf_for_iid(riid, &psfb);
907 if (hr == S_OK) {
908 hr = IPSFactoryBuffer_CreateStub(psfb, riid, manager->object, &stub);
909 IPSFactoryBuffer_Release(psfb);
910 if (hr != S_OK)
911 ERR("Failed to create an IRpcStubBuffer from IPSFactory for %s with error %#lx\n",
912 debugstr_guid(riid), hr);
913 }else {
914 WARN("couldn't get IPSFactory buffer for interface %s\n", debugstr_guid(riid));
915 hr = E_NOINTERFACE;
920 if (hr == S_OK) {
921 ifstub = stub_manager_new_ifstub(manager, stub, riid, dest_context, dest_context_data, mshlflags);
922 if (!ifstub)
923 hr = E_OUTOFMEMORY;
925 if (stub) IRpcStubBuffer_Release(stub);
927 if (hr != S_OK) {
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);
932 return hr;
936 if (!tablemarshal)
938 stdobjref->cPublicRefs = NORMALEXTREFS;
939 stub_manager_ext_addref(manager, stdobjref->cPublicRefs, FALSE);
941 else
943 stdobjref->cPublicRefs = 0;
944 if (mshlflags & MSHLFLAGS_TABLESTRONG)
945 stub_manager_ext_addref(manager, 1, FALSE);
946 else
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);
956 return S_OK;
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)
968 HRESULT hr;
969 MULTI_QI mqi;
971 TRACE("%s\n", debugstr_guid(riid));
973 mqi.pIID = riid;
974 hr = IMultiQI_QueryMultipleInterfaces(iface, 1, &mqi);
975 *ppv = mqi.pItf;
977 return hr;
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);
992 if (!refs)
993 proxy_manager_destroy(This);
994 return refs;
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;
1002 ULONG i;
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)
1017 successful_mqis++;
1018 else
1020 iids[nonlocal_mqis] = *pMQIs[i].pIID;
1021 mapping[nonlocal_mqis] = i;
1022 nonlocal_mqis++;
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;
1033 HRESULT hr;
1034 IPID *ipid;
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);
1044 if (SUCCEEDED(hr))
1046 hr = IRemUnknown_RemQueryInterface(remunk, ipid, NORMALEXTREFS,
1047 nonlocal_mqis, iids, &qiresults);
1048 IRemUnknown_Release(remunk);
1049 if (FAILED(hr))
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 */
1055 if (SUCCEEDED(hr))
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;
1064 if (hrobj == S_OK)
1065 hrobj = unmarshal_object(&qiresults[i].std, apt,
1066 This->dest_context,
1067 This->dest_context_data,
1068 pMQIs[index].pIID, &This->oxid_info,
1069 (void **)&pMQIs[index].pItf);
1071 if (hrobj == S_OK)
1072 successful_mqis++;
1073 else
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 */
1094 else
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;
1131 return S_OK;
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);
1139 return S_OK;
1142 static void fill_std_objref(OBJREF *objref, const GUID *iid, STDOBJREF *std)
1144 objref->signature = OBJREF_SIGNATURE;
1145 objref->flags = OBJREF_STANDARD;
1146 objref->iid = *iid;
1147 if(std)
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 );
1158 HRESULT hr;
1159 struct ifproxy *ifproxy;
1161 TRACE("(...,%s,...)\n", debugstr_guid(riid));
1163 hr = proxy_manager_find_ifproxy(This, riid, &ifproxy);
1164 if (SUCCEEDED(hr))
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);
1193 if (hr == S_OK)
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;
1209 else
1210 ERR("IRemUnknown_RemAddRef returned with %#lx, hrref = %#lx\n", hr, hrref);
1214 if (SUCCEEDED(hr))
1216 OBJREF objref;
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);
1228 else
1230 /* we don't have the interface already unmarshaled so we have to
1231 * request the object from the server */
1232 IRemUnknown *remunk;
1233 IPID *ipid;
1234 REMQIRESULT *qiresults = NULL;
1235 IID iid = *riid;
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);
1245 if (hr == S_OK)
1247 hr = IRemUnknown_RemQueryInterface(remunk, ipid, NORMALEXTREFS,
1248 1, &iid, &qiresults);
1249 if (SUCCEEDED(hr))
1251 OBJREF objref;
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);
1256 if (FAILED(hr))
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);
1266 else
1267 ERR("IRemUnknown_RemQueryInterface failed with error %#lx\n", hr);
1268 IRemUnknown_Release(remunk);
1272 return hr;
1275 static HRESULT WINAPI Proxy_UnmarshalInterface(
1276 IMarshal *iface, IStream *pStm, REFIID riid, void **ppv)
1278 struct proxy_manager *This = impl_from_IMarshal( iface );
1279 IMarshal *marshal;
1280 HRESULT hr;
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);
1286 if(FAILED(hr))
1287 return hr;
1289 hr = IMarshal_UnmarshalInterface(marshal, pStm, riid, ppv);
1290 IMarshal_Release(marshal);
1291 return hr;
1294 static HRESULT WINAPI Proxy_ReleaseMarshalData(IMarshal *iface, IStream *pStm)
1296 struct proxy_manager *This = impl_from_IMarshal( iface );
1297 IMarshal *marshal;
1298 HRESULT hr;
1300 TRACE("(%p, %p)\n", This, pStm);
1302 hr = StdMarshalImpl_Construct(&IID_IMarshal, This->dest_context,
1303 This->dest_context_data, (void**)&marshal);
1304 if(FAILED(hr))
1305 return hr;
1307 hr = IMarshal_ReleaseMarshalData(marshal, pStm);
1308 IMarshal_Release(marshal);
1309 return hr;
1312 static HRESULT WINAPI Proxy_DisconnectObject(IMarshal *iface, DWORD dwReserved)
1314 struct proxy_manager *This = impl_from_IMarshal( iface );
1315 IMarshal *marshal;
1316 HRESULT hr;
1318 TRACE("%p, %#lx\n", This, dwReserved);
1320 hr = StdMarshalImpl_Construct(&IID_IMarshal, This->dest_context,
1321 This->dest_context_data, (void**)&marshal);
1322 if(FAILED(hr))
1323 return hr;
1325 hr = IMarshal_DisconnectObject(marshal, dwReserved);
1326 IMarshal_Release(marshal);
1327 return hr;
1330 static const IMarshalVtbl ProxyMarshal_Vtbl =
1332 Proxy_QueryInterface,
1333 Proxy_AddRef,
1334 Proxy_Release,
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,
1362 IUnknown *pProxy,
1363 DWORD *pAuthnSvc,
1364 DWORD *pAuthzSvc,
1365 OLECHAR **ppServerPrincName,
1366 DWORD *pAuthnLevel,
1367 DWORD *pImpLevel,
1368 void **pAuthInfo,
1369 DWORD *pCapabilities)
1371 FIXME("(%p, %p, %p, %p, %p, %p, %p, %p): stub\n", pProxy, pAuthnSvc,
1372 pAuthzSvc, ppServerPrincName, pAuthnLevel, pImpLevel, pAuthInfo,
1373 pCapabilities);
1375 if (pAuthnSvc)
1376 *pAuthnSvc = 0;
1377 if (pAuthzSvc)
1378 *pAuthzSvc = 0;
1379 if (ppServerPrincName)
1380 *ppServerPrincName = NULL;
1381 if (pAuthnLevel)
1382 *pAuthnLevel = RPC_C_AUTHN_LEVEL_DEFAULT;
1383 if (pImpLevel)
1384 *pImpLevel = RPC_C_IMP_LEVEL_DEFAULT;
1385 if (pAuthInfo)
1386 *pAuthInfo = NULL;
1387 if (pCapabilities)
1388 *pCapabilities = EOAC_NONE;
1390 return E_NOTIMPL;
1393 static HRESULT WINAPI ProxyCliSec_SetBlanket(IClientSecurity *iface,
1394 IUnknown *pProxy, DWORD AuthnSvc,
1395 DWORD AuthzSvc,
1396 OLECHAR *pServerPrincName,
1397 DWORD AuthnLevel, DWORD ImpLevel,
1398 void *pAuthInfo,
1399 DWORD Capabilities)
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);
1404 return E_NOTIMPL;
1407 static HRESULT WINAPI ProxyCliSec_CopyProxy(IClientSecurity *iface,
1408 IUnknown *pProxy, IUnknown **ppCopy)
1410 FIXME("(%p, %p): stub\n", pProxy, ppCopy);
1411 *ppCopy = NULL;
1412 return E_NOTIMPL;
1415 static const IClientSecurityVtbl ProxyCliSec_Vtbl =
1417 ProxyCliSec_QueryInterface,
1418 ProxyCliSec_AddRef,
1419 ProxyCliSec_Release,
1420 ProxyCliSec_QueryBlanket,
1421 ProxyCliSec_SetBlanket,
1422 ProxyCliSec_CopyProxy
1425 static HRESULT ifproxy_get_public_ref(struct ifproxy * This)
1427 HRESULT hr = S_OK;
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);
1442 if (hr == S_OK)
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);
1453 else
1454 ERR("IRemUnknown_RemAddRef returned with %#lx, hrref = %#lx\n", hr, hrref);
1457 ReleaseMutex(This->parent->remoting_mutex);
1459 return hr;
1462 static HRESULT ifproxy_release_public_refs(struct ifproxy * This)
1464 HRESULT hr = S_OK;
1465 LONG public_refs;
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);
1481 if (hr == S_OK)
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);
1489 if (hr == S_OK)
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));
1496 else
1497 ERR("IRemUnknown_RemRelease failed with error %#lx\n", hr);
1500 ReleaseMutex(This->parent->remoting_mutex);
1502 return hr;
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);
1512 This->chan = NULL;
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);
1526 if (This->chan)
1528 IRpcChannelBuffer_Release(This->chan);
1529 This->chan = NULL;
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());
1551 if (oxid_info)
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 */;
1559 else
1561 HRESULT hr = rpc_resolve_oxid(oxid, &This->oxid_info);
1562 if (FAILED(hr))
1564 CloseHandle(This->remoting_mutex);
1565 HeapFree(GetProcessHeap(), 0, This);
1566 return hr;
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 */
1581 This->parent = apt;
1583 /* the source apartment and id of the object */
1584 This->oxid = oxid;
1585 This->oid = oid;
1587 This->refs = 1;
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;
1615 return S_OK;
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)
1631 case MSHCTX_INPROC:
1632 new_dest_context = dest_context;
1633 break;
1634 case MSHCTX_CROSSCTX:
1635 switch (dest_context)
1637 case MSHCTX_INPROC:
1638 break;
1639 default:
1640 new_dest_context = dest_context;
1642 break;
1643 case MSHCTX_LOCAL:
1644 switch (dest_context)
1646 case MSHCTX_INPROC:
1647 case MSHCTX_CROSSCTX:
1648 break;
1649 default:
1650 new_dest_context = dest_context;
1652 break;
1653 case MSHCTX_NOSHAREDMEM:
1654 switch (dest_context)
1656 case MSHCTX_DIFFERENTMACHINE:
1657 new_dest_context = dest_context;
1658 break;
1659 default:
1660 break;
1662 break;
1663 default:
1664 break;
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)
1678 HRESULT hr;
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);
1688 return S_OK;
1690 if (IsEqualIID(riid, &IID_IMarshal))
1692 *ppv = &This->IMarshal_iface;
1693 IMarshal_AddRef(&This->IMarshal_iface);
1694 return S_OK;
1696 if (IsEqualIID(riid, &IID_IClientSecurity))
1698 *ppv = &This->IClientSecurity_iface;
1699 IClientSecurity_AddRef(&This->IClientSecurity_iface);
1700 return S_OK;
1703 hr = proxy_manager_find_ifproxy(This, riid, &ifproxy);
1704 if (hr == S_OK)
1706 *ppv = ifproxy->iface;
1707 IUnknown_AddRef((IUnknown *)*ppv);
1708 return S_OK;
1711 *ppv = NULL;
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)
1719 HRESULT hr;
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;
1729 ifproxy->refs = 0;
1730 ifproxy->proxy = NULL;
1732 assert(channel);
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);
1741 hr = S_OK;
1743 else
1745 hr = get_facbuf_for_iid(riid, &psfb);
1746 if (hr == S_OK)
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);
1755 if (hr != S_OK)
1756 ERR("Could not create proxy for interface %s, error %#lx\n", debugstr_guid(riid), hr);
1758 else
1759 ERR("Could not get IPSFactoryBuffer for interface %s, error %#lx\n", debugstr_guid(riid), hr);
1761 if (hr == S_OK)
1762 hr = IRpcProxyBuffer_Connect(ifproxy->proxy, ifproxy->chan);
1765 if (hr == S_OK)
1767 EnterCriticalSection(&This->cs);
1768 list_add_tail(&This->interfaces, &ifproxy->entry);
1769 LeaveCriticalSection(&This->cs);
1771 *iif_out = ifproxy;
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);
1775 else
1776 ifproxy_destroy(ifproxy);
1778 return hr;
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;
1792 hr = S_OK;
1793 break;
1796 LeaveCriticalSection(&This->cs);
1798 return hr;
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
1813 * working */
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)
1830 HRESULT hr = S_OK;
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)
1837 return S_FALSE;
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 */
1857 *remunk = NULL;
1858 hr = S_FALSE;
1860 else
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);
1888 return 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));
1902 if (This->parent)
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);
1912 break;
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;
1943 BOOL found = FALSE;
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
1952 * destroyed */
1953 if (IMultiQI_AddRef(&proxy->IMultiQI_iface) != 0)
1955 *proxy_found = proxy;
1956 found = TRUE;
1957 break;
1961 LeaveCriticalSection(&apt->cs);
1962 return found;
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);
1974 return S_OK;
1977 /********************** StdMarshal implementation ****************************/
1979 struct stdmarshal
1981 IMarshal IMarshal_iface;
1982 LONG refcount;
1983 DWORD dest_context;
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)
1994 *ppv = NULL;
1995 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IMarshal, riid))
1997 *ppv = iface;
1998 IMarshal_AddRef(iface);
1999 return S_OK;
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);
2016 if (!refcount)
2017 heap_free(marshal);
2019 return 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;
2026 return S_OK;
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);
2033 return S_OK;
2036 static HRESULT WINAPI StdMarshalImpl_MarshalInterface(IMarshal *iface, IStream *stream, REFIID riid, void *pv,
2037 DWORD dest_context, void *dest_context_data, DWORD mshlflags)
2039 ULONG res;
2040 HRESULT hr;
2041 OBJREF objref;
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);
2059 if (hr != S_OK)
2061 ERR("Failed to create ifstub, hr %#lx\n", hr);
2062 return 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;
2075 HRESULT hr = S_OK;
2077 assert(apt);
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
2086 * object */
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,
2091 &proxy_manager);
2093 else
2094 TRACE("proxy manager already created, using\n");
2096 if (hr == S_OK)
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);
2109 if (hr == S_OK)
2110 hr = proxy_manager_create_ifproxy(proxy_manager, stdobjref, riid, chanbuf, &ifproxy);
2112 else
2113 IUnknown_AddRef((IUnknown *)ifproxy->iface);
2115 if (hr == S_OK)
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);
2120 if (FAILED(hr))
2121 ifproxy_destroy(ifproxy);
2124 if (hr == S_OK)
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);
2132 return hr;
2135 static HRESULT WINAPI StdMarshalImpl_UnmarshalInterface(IMarshal *iface, IStream *stream, REFIID riid, void **ppv)
2137 struct stdmarshal *marshal = impl_from_StdMarshal(iface);
2138 OBJREF objref;
2139 HRESULT hr;
2140 ULONG res;
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);
2158 return E_NOTIMPL;
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)
2166 OBJREF objref;
2167 HRESULT hr;
2168 ULONG res;
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);
2188 return E_NOTIMPL;
2191 return std_release_marshal_data(stream);
2194 static HRESULT WINAPI StdMarshalImpl_DisconnectObject(IMarshal *iface, DWORD reserved)
2196 FIXME("(), stub!\n");
2197 return S_OK;
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;
2216 HRESULT hr;
2218 object = heap_alloc(sizeof(*object));
2219 if (!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);
2230 return hr;
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)
2244 if (pUnk == NULL)
2246 FIXME("%s, NULL, %lx, %p, %lx, %p, unimplemented yet.\n", debugstr_guid(riid), dwDestContext,
2247 dest_context, flags, marshal);
2248 return E_NOTIMPL;
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);