d3dcompiler: Allow D3DCompile2() to succeed with null output shader blob pointer.
[wine.git] / dlls / combase / marshal.c
bloba3da851b13977899840400d28b9ef6392697fea4
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"
32 WINE_DEFAULT_DEBUG_CHANNEL(ole);
34 HRESULT WINAPI RPC_CreateClientChannel(const OXID *oxid, const IPID *ipid,
35 const OXID_INFO *oxid_info, const IID *iid,
36 DWORD dest_context, void *dest_context_data,
37 IRpcChannelBuffer **chan, struct apartment *apt);
39 static HRESULT unmarshal_object(const STDOBJREF *stdobjref, struct apartment *apt,
40 MSHCTX dest_context, void *dest_context_data,
41 REFIID riid, const OXID_INFO *oxid_info,
42 void **object);
44 /* number of refs given out for normal marshaling */
45 #define NORMALEXTREFS 5
47 /* private flag indicating that the object was marshaled as table-weak */
48 #define SORFP_TABLEWEAK SORF_OXRES1
49 /* private flag indicating that the caller does not want to notify the stub
50 * when the proxy disconnects or is destroyed */
51 #define SORFP_NOLIFETIMEMGMT SORF_OXRES2
53 /* imported interface proxy */
54 struct ifproxy
56 struct list entry; /* entry in proxy_manager list (CS parent->cs) */
57 struct proxy_manager *parent; /* owning proxy_manager (RO) */
58 void *iface; /* interface pointer (RO) */
59 STDOBJREF stdobjref; /* marshal data that represents this object (RO) */
60 IID iid; /* interface ID (RO) */
61 IRpcProxyBuffer *proxy; /* interface proxy (RO) */
62 ULONG refs; /* imported (public) references (LOCK) */
63 IRpcChannelBuffer *chan; /* channel to object (CS parent->cs) */
66 /* imported object / proxy manager */
67 struct proxy_manager
69 IMultiQI IMultiQI_iface;
70 IMarshal IMarshal_iface;
71 IClientSecurity IClientSecurity_iface;
72 struct apartment *parent; /* owning apartment (RO) */
73 struct list entry; /* entry in apartment (CS parent->cs) */
74 OXID oxid; /* object exported ID (RO) */
75 OXID_INFO oxid_info; /* string binding, ipid of rem unknown and other information (RO) */
76 OID oid; /* object ID (RO) */
77 struct list interfaces; /* imported interfaces (CS cs) */
78 LONG refs; /* proxy reference count (LOCK) */
79 CRITICAL_SECTION cs; /* thread safety for this object and children */
80 ULONG sorflags; /* STDOBJREF flags (RO) */
81 IRemUnknown *remunk; /* proxy to IRemUnknown used for lifecycle management (CS cs) */
82 HANDLE remoting_mutex; /* mutex used for synchronizing access to IRemUnknown */
83 MSHCTX dest_context; /* context used for activating optimisations (LOCK) */
84 void *dest_context_data; /* reserved context value (LOCK) */
87 static inline struct proxy_manager *impl_from_IMultiQI(IMultiQI *iface)
89 return CONTAINING_RECORD(iface, struct proxy_manager, IMultiQI_iface);
92 static inline struct proxy_manager *impl_from_IMarshal(IMarshal *iface)
94 return CONTAINING_RECORD(iface, struct proxy_manager, IMarshal_iface);
97 static inline struct proxy_manager *impl_from_IClientSecurity(IClientSecurity *iface)
99 return CONTAINING_RECORD(iface, struct proxy_manager, IClientSecurity_iface);
102 struct ftmarshaler
104 IUnknown IUnknown_inner;
105 IMarshal IMarshal_iface;
106 IUnknown *outer_unk;
107 LONG refcount;
110 static struct ftmarshaler *impl_ft_from_IUnknown(IUnknown *iface)
112 return CONTAINING_RECORD(iface, struct ftmarshaler, IUnknown_inner);
115 static struct ftmarshaler *impl_ft_from_IMarshal(IMarshal *iface)
117 return CONTAINING_RECORD(iface, struct ftmarshaler, IMarshal_iface);
120 /***********************************************************************
121 * CoMarshalHresult (combase.@)
123 HRESULT WINAPI CoMarshalHresult(IStream *stream, HRESULT hresult)
125 return IStream_Write(stream, &hresult, sizeof(hresult), NULL);
128 /***********************************************************************
129 * CoUnmarshalHresult (combase.@)
131 HRESULT WINAPI CoUnmarshalHresult(IStream *stream, HRESULT *phresult)
133 return IStream_Read(stream, phresult, sizeof(*phresult), NULL);
136 /***********************************************************************
137 * CoGetInterfaceAndReleaseStream (combase.@)
139 HRESULT WINAPI CoGetInterfaceAndReleaseStream(IStream *stream, REFIID riid, void **obj)
141 HRESULT hr;
143 TRACE("%p, %s, %p\n", stream, debugstr_guid(riid), obj);
145 if (!stream) return E_INVALIDARG;
146 hr = CoUnmarshalInterface(stream, riid, obj);
147 IStream_Release(stream);
148 return hr;
151 /***********************************************************************
152 * CoMarshalInterThreadInterfaceInStream (combase.@)
154 HRESULT WINAPI CoMarshalInterThreadInterfaceInStream(REFIID riid, IUnknown *unk, IStream **stream)
156 ULARGE_INTEGER xpos;
157 LARGE_INTEGER seekto;
158 HRESULT hr;
160 TRACE("%s, %p, %p\n", debugstr_guid(riid), unk, stream);
162 hr = CreateStreamOnHGlobal(NULL, TRUE, stream);
163 if (FAILED(hr)) return hr;
164 hr = CoMarshalInterface(*stream, riid, unk, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
166 if (SUCCEEDED(hr))
168 memset(&seekto, 0, sizeof(seekto));
169 IStream_Seek(*stream, seekto, STREAM_SEEK_SET, &xpos);
171 else
173 IStream_Release(*stream);
174 *stream = NULL;
177 return hr;
180 static HRESULT WINAPI ftmarshaler_inner_QueryInterface(IUnknown *iface, REFIID riid, void **obj)
182 struct ftmarshaler *marshaler = impl_ft_from_IUnknown(iface);
184 TRACE("%p, %s, %p\n", iface, debugstr_guid(riid), obj);
186 *obj = NULL;
188 if (IsEqualIID(&IID_IUnknown, riid))
189 *obj = &marshaler->IUnknown_inner;
190 else if (IsEqualIID(&IID_IMarshal, riid))
191 *obj = &marshaler->IMarshal_iface;
192 else
194 FIXME("No interface for %s\n", debugstr_guid(riid));
195 return E_NOINTERFACE;
198 IUnknown_AddRef((IUnknown *)*obj);
200 return S_OK;
203 static ULONG WINAPI ftmarshaler_inner_AddRef(IUnknown *iface)
205 struct ftmarshaler *marshaler = impl_ft_from_IUnknown(iface);
206 ULONG refcount = InterlockedIncrement(&marshaler->refcount);
208 TRACE("%p, refcount %lu\n", iface, refcount);
210 return refcount;
213 static ULONG WINAPI ftmarshaler_inner_Release(IUnknown *iface)
215 struct ftmarshaler *marshaler = impl_ft_from_IUnknown(iface);
216 ULONG refcount = InterlockedDecrement(&marshaler->refcount);
218 TRACE("%p, refcount %lu\n", iface, refcount);
220 if (!refcount)
221 free(marshaler);
223 return refcount;
226 static const IUnknownVtbl ftmarshaler_inner_vtbl =
228 ftmarshaler_inner_QueryInterface,
229 ftmarshaler_inner_AddRef,
230 ftmarshaler_inner_Release
233 static HRESULT WINAPI ftmarshaler_QueryInterface(IMarshal *iface, REFIID riid, void **obj)
235 struct ftmarshaler *marshaler = impl_ft_from_IMarshal(iface);
237 TRACE("%p, %s, %p\n", iface, debugstr_guid(riid), obj);
239 return IUnknown_QueryInterface(marshaler->outer_unk, riid, obj);
242 static ULONG WINAPI ftmarshaler_AddRef(IMarshal *iface)
244 struct ftmarshaler *marshaler = impl_ft_from_IMarshal(iface);
246 TRACE("%p\n", iface);
248 return IUnknown_AddRef(marshaler->outer_unk);
251 static ULONG WINAPI ftmarshaler_Release(IMarshal *iface)
253 struct ftmarshaler *marshaler = impl_ft_from_IMarshal(iface);
255 TRACE("%p\n", iface);
257 return IUnknown_Release(marshaler->outer_unk);
260 static HRESULT WINAPI ftmarshaler_GetUnmarshalClass(IMarshal *iface, REFIID riid, void *pv,
261 DWORD dest_context, void *pvDestContext, DWORD mshlflags, CLSID *clsid)
263 TRACE("%s, %p, %#lx, %p, %#lx, %p\n", debugstr_guid(riid), pv, dest_context, pvDestContext, mshlflags, clsid);
265 if (dest_context == MSHCTX_INPROC || dest_context == MSHCTX_CROSSCTX)
266 *clsid = CLSID_InProcFreeMarshaler;
267 else
268 *clsid = CLSID_StdMarshal;
270 return S_OK;
273 union ftmarshaler_data
275 UINT64 value;
276 IUnknown *object;
279 static HRESULT WINAPI ftmarshaler_GetMarshalSizeMax(IMarshal *iface, REFIID riid, void *pv,
280 DWORD dest_context, void *pvDestContext, DWORD mshlflags, DWORD *size)
282 IMarshal *marshal = NULL;
283 HRESULT hr;
285 TRACE("%s, %p, %#lx, %p, %#lx, %p\n", debugstr_guid(riid), pv, dest_context, pvDestContext, mshlflags, size);
287 /* If the marshalling happens inside the same process the interface pointer is
288 copied between the apartments */
289 if (dest_context == MSHCTX_INPROC || dest_context == MSHCTX_CROSSCTX)
291 *size = sizeof(mshlflags) + sizeof(union ftmarshaler_data) + sizeof(GUID);
292 return S_OK;
295 /* Use the standard marshaller to handle all other cases */
296 CoGetStandardMarshal(riid, pv, dest_context, pvDestContext, mshlflags, &marshal);
297 hr = IMarshal_GetMarshalSizeMax(marshal, riid, pv, dest_context, pvDestContext, mshlflags, size);
298 IMarshal_Release(marshal);
299 return hr;
302 static HRESULT WINAPI ftmarshaler_MarshalInterface(IMarshal *iface, IStream *stream, REFIID riid,
303 void *pv, DWORD dest_context, void *pvDestContext, DWORD mshlflags)
305 IMarshal *marshal = NULL;
306 HRESULT hr;
308 TRACE("%p, %s, %p, %#lx, %p, %#lx\n", stream, debugstr_guid(riid), pv,
309 dest_context, pvDestContext, mshlflags);
311 /* If the marshalling happens inside the same process the interface pointer is
312 copied between the apartments */
313 if (dest_context == MSHCTX_INPROC || dest_context == MSHCTX_CROSSCTX)
315 union ftmarshaler_data data;
316 GUID unknown_guid = { 0 };
318 data.value = 0;
319 hr = IUnknown_QueryInterface((IUnknown *)pv, riid, (void **)&data.object);
320 if (FAILED(hr))
321 return hr;
323 /* don't hold a reference to table-weak marshaled interfaces */
324 if (mshlflags & MSHLFLAGS_TABLEWEAK)
325 IUnknown_Release(data.object);
327 hr = IStream_Write(stream, &mshlflags, sizeof(mshlflags), NULL);
328 if (hr != S_OK) return STG_E_MEDIUMFULL;
330 hr = IStream_Write(stream, &data, sizeof(data), NULL);
331 if (hr != S_OK) return STG_E_MEDIUMFULL;
333 hr = IStream_Write(stream, &unknown_guid, sizeof(unknown_guid), NULL);
334 if (hr != S_OK) return STG_E_MEDIUMFULL;
336 return S_OK;
339 /* Use the standard marshaler to handle all other cases */
340 CoGetStandardMarshal(riid, pv, dest_context, pvDestContext, mshlflags, &marshal);
341 hr = IMarshal_MarshalInterface(marshal, stream, riid, pv, dest_context, pvDestContext, mshlflags);
342 IMarshal_Release(marshal);
343 return hr;
346 static HRESULT WINAPI ftmarshaler_UnmarshalInterface(IMarshal *iface, IStream *stream, REFIID riid, void **ppv)
348 union ftmarshaler_data data;
349 DWORD mshlflags;
350 GUID unknown_guid;
351 HRESULT hr;
353 TRACE("%p, %s, %p\n", stream, debugstr_guid(riid), ppv);
355 hr = IStream_Read(stream, &mshlflags, sizeof(mshlflags), NULL);
356 if (hr != S_OK) return STG_E_READFAULT;
358 hr = IStream_Read(stream, &data, sizeof(data), NULL);
359 if (hr != S_OK) return STG_E_READFAULT;
361 hr = IStream_Read(stream, &unknown_guid, sizeof(unknown_guid), NULL);
362 if (hr != S_OK) return STG_E_READFAULT;
364 hr = IUnknown_QueryInterface(data.object, riid, ppv);
365 if (!(mshlflags & (MSHLFLAGS_TABLEWEAK | MSHLFLAGS_TABLESTRONG)))
366 IUnknown_Release(data.object);
368 return hr;
371 static HRESULT WINAPI ftmarshaler_ReleaseMarshalData(IMarshal *iface, IStream *stream)
373 union ftmarshaler_data data;
374 DWORD mshlflags;
375 GUID unknown_guid;
376 HRESULT hr;
378 TRACE("%p\n", stream);
380 hr = IStream_Read(stream, &mshlflags, sizeof(mshlflags), NULL);
381 if (hr != S_OK) return STG_E_READFAULT;
383 hr = IStream_Read(stream, &data, sizeof(data), NULL);
384 if (hr != S_OK) return STG_E_READFAULT;
386 hr = IStream_Read(stream, &unknown_guid, sizeof(unknown_guid), NULL);
387 if (hr != S_OK) return STG_E_READFAULT;
389 IUnknown_Release(data.object);
390 return S_OK;
393 static HRESULT WINAPI ftmarshaler_DisconnectObject(IMarshal *iface, DWORD reserved)
395 TRACE("\n");
397 /* nothing to do */
398 return S_OK;
401 static const IMarshalVtbl ftmarshaler_vtbl =
403 ftmarshaler_QueryInterface,
404 ftmarshaler_AddRef,
405 ftmarshaler_Release,
406 ftmarshaler_GetUnmarshalClass,
407 ftmarshaler_GetMarshalSizeMax,
408 ftmarshaler_MarshalInterface,
409 ftmarshaler_UnmarshalInterface,
410 ftmarshaler_ReleaseMarshalData,
411 ftmarshaler_DisconnectObject
414 /***********************************************************************
415 * CoCreateFreeThreadedMarshaler (combase.@)
417 HRESULT WINAPI CoCreateFreeThreadedMarshaler(IUnknown *outer, IUnknown **marshaler)
419 struct ftmarshaler *object;
421 TRACE("%p, %p\n", outer, marshaler);
423 object = malloc(sizeof(*object));
424 if (!object)
425 return E_OUTOFMEMORY;
427 object->IUnknown_inner.lpVtbl = &ftmarshaler_inner_vtbl;
428 object->IMarshal_iface.lpVtbl = &ftmarshaler_vtbl;
429 object->refcount = 1;
430 object->outer_unk = outer ? outer : &object->IUnknown_inner;
432 *marshaler = &object->IUnknown_inner;
434 return S_OK;
437 /***********************************************************************
438 * CoGetMarshalSizeMax (combase.@)
440 HRESULT WINAPI CoGetMarshalSizeMax(ULONG *size, REFIID riid, IUnknown *unk,
441 DWORD dest_context, void *pvDestContext, DWORD mshlFlags)
443 BOOL std_marshal = FALSE;
444 IMarshal *marshal;
445 HRESULT hr;
447 if (!unk)
448 return E_POINTER;
450 hr = IUnknown_QueryInterface(unk, &IID_IMarshal, (void **)&marshal);
451 if (hr != S_OK)
453 std_marshal = TRUE;
454 hr = CoGetStandardMarshal(riid, unk, dest_context, pvDestContext, mshlFlags, &marshal);
456 if (hr != S_OK)
457 return hr;
459 hr = IMarshal_GetMarshalSizeMax(marshal, riid, unk, dest_context, pvDestContext, mshlFlags, size);
460 if (!std_marshal)
461 /* add on the size of the whole OBJREF structure like native does */
462 *size += sizeof(OBJREF);
464 IMarshal_Release(marshal);
465 return hr;
468 static void dump_mshflags(MSHLFLAGS flags)
470 if (flags & MSHLFLAGS_TABLESTRONG)
471 TRACE(" MSHLFLAGS_TABLESTRONG");
472 if (flags & MSHLFLAGS_TABLEWEAK)
473 TRACE(" MSHLFLAGS_TABLEWEAK");
474 if (!(flags & (MSHLFLAGS_TABLESTRONG|MSHLFLAGS_TABLEWEAK)))
475 TRACE(" MSHLFLAGS_NORMAL");
476 if (flags & MSHLFLAGS_NOPING)
477 TRACE(" MSHLFLAGS_NOPING");
480 /***********************************************************************
481 * CoMarshalInterface (combase.@)
483 HRESULT WINAPI CoMarshalInterface(IStream *stream, REFIID riid, IUnknown *unk,
484 DWORD dest_context, void *pvDestContext, DWORD mshlFlags)
486 CLSID marshaler_clsid;
487 IMarshal *marshal;
488 HRESULT hr;
490 TRACE("%p, %s, %p, %lx, %p, ", stream, debugstr_guid(riid), unk, dest_context, pvDestContext);
491 dump_mshflags(mshlFlags);
492 TRACE("\n");
494 if (!unk || !stream)
495 return E_INVALIDARG;
497 hr = IUnknown_QueryInterface(unk, &IID_IMarshal, (void **)&marshal);
498 if (hr != S_OK)
499 hr = CoGetStandardMarshal(riid, unk, dest_context, pvDestContext, mshlFlags, &marshal);
500 if (hr != S_OK)
502 ERR("Failed to get marshaller, %#lx\n", hr);
503 return hr;
506 hr = IMarshal_GetUnmarshalClass(marshal, riid, unk, dest_context, pvDestContext, mshlFlags,
507 &marshaler_clsid);
508 if (hr != S_OK)
510 ERR("IMarshal::GetUnmarshalClass failed, %#lx\n", hr);
511 goto cleanup;
514 /* FIXME: implement handler marshaling too */
515 if (IsEqualCLSID(&marshaler_clsid, &CLSID_StdMarshal))
517 TRACE("Using standard marshaling\n");
519 else
521 OBJREF objref;
523 TRACE("Using custom marshaling\n");
524 objref.signature = OBJREF_SIGNATURE;
525 objref.iid = *riid;
526 objref.flags = OBJREF_CUSTOM;
527 objref.u_objref.u_custom.clsid = marshaler_clsid;
528 objref.u_objref.u_custom.cbExtension = 0;
529 objref.u_objref.u_custom.size = 0;
530 hr = IMarshal_GetMarshalSizeMax(marshal, riid, unk, dest_context, pvDestContext, mshlFlags,
531 &objref.u_objref.u_custom.size);
532 if (hr != S_OK)
534 ERR("Failed to get max size of marshal data, error %#lx\n", hr);
535 goto cleanup;
537 /* write constant sized common header and OR_CUSTOM data into stream */
538 hr = IStream_Write(stream, &objref, FIELD_OFFSET(OBJREF, u_objref.u_custom.pData), NULL);
539 if (hr != S_OK)
541 ERR("Failed to write OR_CUSTOM header to stream with %#lx\n", hr);
542 goto cleanup;
546 TRACE("Calling IMarshal::MarshalInterface\n");
548 hr = IMarshal_MarshalInterface(marshal, stream, riid, unk, dest_context, pvDestContext, mshlFlags);
549 if (hr != S_OK)
551 ERR("Failed to marshal the interface %s, hr %#lx\n", debugstr_guid(riid), hr);
552 goto cleanup;
555 cleanup:
556 IMarshal_Release(marshal);
558 TRACE("completed with hr %#lx\n", hr);
560 return hr;
563 /* Creates an IMarshal* object according to the data marshaled to the stream.
564 * The function leaves the stream pointer at the start of the data written
565 * to the stream by the IMarshal* object.
567 static HRESULT get_unmarshaler_from_stream(IStream *stream, IMarshal **marshal, IID *iid)
569 OBJREF objref;
570 HRESULT hr;
571 ULONG res;
573 /* read common OBJREF header */
574 hr = IStream_Read(stream, &objref, FIELD_OFFSET(OBJREF, u_objref), &res);
575 if (hr != S_OK || (res != FIELD_OFFSET(OBJREF, u_objref)))
577 ERR("Failed to read common OBJREF header, %#lx\n", hr);
578 return STG_E_READFAULT;
581 /* sanity check on header */
582 if (objref.signature != OBJREF_SIGNATURE)
584 ERR("Bad OBJREF signature %#lx\n", objref.signature);
585 return RPC_E_INVALID_OBJREF;
588 if (iid) *iid = objref.iid;
590 /* FIXME: handler marshaling */
591 if (objref.flags & OBJREF_STANDARD)
593 TRACE("Using standard unmarshaling\n");
594 *marshal = NULL;
595 return S_FALSE;
597 else if (objref.flags & OBJREF_CUSTOM)
599 ULONG custom_header_size = FIELD_OFFSET(OBJREF, u_objref.u_custom.pData) -
600 FIELD_OFFSET(OBJREF, u_objref.u_custom);
601 TRACE("Using custom unmarshaling\n");
602 /* read constant sized OR_CUSTOM data from stream */
603 hr = IStream_Read(stream, &objref.u_objref.u_custom,
604 custom_header_size, &res);
605 if (hr != S_OK || (res != custom_header_size))
607 ERR("Failed to read OR_CUSTOM header, %#lx\n", hr);
608 return STG_E_READFAULT;
610 /* now create the marshaler specified in the stream */
611 hr = CoCreateInstance(&objref.u_objref.u_custom.clsid, NULL,
612 CLSCTX_INPROC_SERVER, &IID_IMarshal,
613 (LPVOID*)marshal);
615 else
617 FIXME("Invalid or unimplemented marshaling type specified: %lx\n", objref.flags);
618 return RPC_E_INVALID_OBJREF;
621 if (hr != S_OK)
622 ERR("Failed to create marshal, %#lx\n", hr);
624 return hr;
627 static HRESULT std_release_marshal_data(IStream *stream)
629 struct stub_manager *stubmgr;
630 struct OR_STANDARD obj;
631 struct apartment *apt;
632 ULONG res;
633 HRESULT hr;
635 hr = IStream_Read(stream, &obj, FIELD_OFFSET(struct OR_STANDARD, saResAddr.aStringArray), &res);
636 if (hr != S_OK) return STG_E_READFAULT;
638 if (obj.saResAddr.wNumEntries)
640 ERR("unsupported size of DUALSTRINGARRAY\n");
641 return E_NOTIMPL;
644 TRACE("oxid = %s, oid = %s, ipid = %s\n", wine_dbgstr_longlong(obj.std.oxid),
645 wine_dbgstr_longlong(obj.std.oid), wine_dbgstr_guid(&obj.std.ipid));
647 if (!(apt = apartment_findfromoxid(obj.std.oxid)))
649 WARN("Could not map OXID %s to apartment object\n",
650 wine_dbgstr_longlong(obj.std.oxid));
651 return RPC_E_INVALID_OBJREF;
654 if (!(stubmgr = get_stub_manager(apt, obj.std.oid)))
656 apartment_release(apt);
657 ERR("could not map object ID to stub manager, oxid=%s, oid=%s\n",
658 wine_dbgstr_longlong(obj.std.oxid), wine_dbgstr_longlong(obj.std.oid));
659 return RPC_E_INVALID_OBJREF;
662 stub_manager_release_marshal_data(stubmgr, obj.std.cPublicRefs, &obj.std.ipid, obj.std.flags & SORFP_TABLEWEAK);
664 stub_manager_int_release(stubmgr);
665 apartment_release(apt);
667 return S_OK;
670 /***********************************************************************
671 * CoReleaseMarshalData (combase.@)
673 HRESULT WINAPI CoReleaseMarshalData(IStream *stream)
675 IMarshal *marshal;
676 HRESULT hr;
678 TRACE("%p\n", stream);
680 hr = get_unmarshaler_from_stream(stream, &marshal, NULL);
681 if (hr == S_FALSE)
683 hr = std_release_marshal_data(stream);
684 if (hr != S_OK)
685 ERR("StdMarshal ReleaseMarshalData failed with error %#lx\n", hr);
686 return hr;
688 if (hr != S_OK)
689 return hr;
691 /* call the helper object to do the releasing of marshal data */
692 hr = IMarshal_ReleaseMarshalData(marshal, stream);
693 if (hr != S_OK)
694 ERR("IMarshal::ReleaseMarshalData failed with error %#lx\n", hr);
696 IMarshal_Release(marshal);
697 return hr;
700 static HRESULT std_unmarshal_interface(MSHCTX dest_context, void *dest_context_data,
701 IStream *stream, REFIID riid, void **ppv)
703 struct stub_manager *stubmgr = NULL;
704 struct OR_STANDARD obj;
705 ULONG res;
706 HRESULT hres;
707 struct apartment *apt, *stub_apt;
709 TRACE("(...,%s,....)\n", debugstr_guid(riid));
711 /* we need an apartment to unmarshal into */
712 if (!(apt = apartment_get_current_or_mta()))
714 ERR("Apartment not initialized\n");
715 return CO_E_NOTINITIALIZED;
718 /* read STDOBJREF from wire */
719 hres = IStream_Read(stream, &obj, FIELD_OFFSET(struct OR_STANDARD, saResAddr.aStringArray), &res);
720 if (hres != S_OK)
722 apartment_release(apt);
723 return STG_E_READFAULT;
726 if (obj.saResAddr.wNumEntries)
728 ERR("unsupported size of DUALSTRINGARRAY\n");
729 return E_NOTIMPL;
732 /* check if we're marshalling back to ourselves */
733 if ((apartment_getoxid(apt) == obj.std.oxid) && (stubmgr = get_stub_manager(apt, obj.std.oid)))
735 TRACE("Unmarshalling object marshalled in same apartment for iid %s, "
736 "returning original object %p\n", debugstr_guid(riid), stubmgr->object);
738 hres = IUnknown_QueryInterface(stubmgr->object, riid, ppv);
740 /* unref the ifstub. FIXME: only do this on success? */
741 if (!stub_manager_is_table_marshaled(stubmgr, &obj.std.ipid))
742 stub_manager_ext_release(stubmgr, obj.std.cPublicRefs, obj.std.flags & SORFP_TABLEWEAK, FALSE);
744 stub_manager_int_release(stubmgr);
745 apartment_release(apt);
746 return hres;
749 /* notify stub manager about unmarshal if process-local object.
750 * note: if the oxid is not found then we and native will quite happily
751 * ignore table marshaling and normal marshaling rules regarding number of
752 * unmarshals, etc, but if you abuse these rules then your proxy could end
753 * up returning RPC_E_DISCONNECTED. */
754 if ((stub_apt = apartment_findfromoxid(obj.std.oxid)))
756 if ((stubmgr = get_stub_manager(stub_apt, obj.std.oid)))
758 if (!stub_manager_notify_unmarshal(stubmgr, &obj.std.ipid))
759 hres = CO_E_OBJNOTCONNECTED;
761 else
763 WARN("Couldn't find object for OXID %s, OID %s, assuming disconnected\n",
764 wine_dbgstr_longlong(obj.std.oxid),
765 wine_dbgstr_longlong(obj.std.oid));
766 hres = CO_E_OBJNOTCONNECTED;
769 else
770 TRACE("Treating unmarshal from OXID %s as inter-process\n",
771 wine_dbgstr_longlong(obj.std.oxid));
773 if (hres == S_OK)
774 hres = unmarshal_object(&obj.std, apt, dest_context,
775 dest_context_data, riid,
776 stubmgr ? &stubmgr->oxid_info : NULL, ppv);
778 if (stubmgr) stub_manager_int_release(stubmgr);
779 if (stub_apt) apartment_release(stub_apt);
781 if (hres != S_OK) WARN("Failed with error %#lx\n", hres);
782 else TRACE("Successfully created proxy %p\n", *ppv);
784 apartment_release(apt);
785 return hres;
788 /***********************************************************************
789 * CoUnmarshalInterface (combase.@)
791 HRESULT WINAPI CoUnmarshalInterface(IStream *stream, REFIID riid, void **ppv)
793 IMarshal *marshal;
794 IUnknown *object;
795 HRESULT hr;
796 IID iid;
798 TRACE("%p, %s, %p\n", stream, debugstr_guid(riid), ppv);
800 if (!stream || !ppv)
801 return E_INVALIDARG;
803 hr = get_unmarshaler_from_stream(stream, &marshal, &iid);
804 if (hr == S_FALSE)
806 hr = std_unmarshal_interface(0, NULL, stream, &iid, (void **)&object);
807 if (hr != S_OK)
808 ERR("StdMarshal UnmarshalInterface failed, hr %#lx\n", hr);
810 else if (hr == S_OK)
812 /* call the helper object to do the actual unmarshaling */
813 hr = IMarshal_UnmarshalInterface(marshal, stream, &iid, (void **)&object);
814 IMarshal_Release(marshal);
815 if (hr != S_OK)
816 ERR("IMarshal::UnmarshalInterface failed, hr %#lx\n", hr);
819 if (hr == S_OK)
821 /* IID_NULL means use the interface ID of the marshaled object */
822 if (!IsEqualIID(riid, &IID_NULL) && !IsEqualIID(riid, &iid))
824 TRACE("requested interface != marshalled interface, additional QI needed\n");
825 hr = IUnknown_QueryInterface(object, riid, ppv);
826 if (hr != S_OK)
827 ERR("Couldn't query for interface %s, hr %#lx\n", debugstr_guid(riid), hr);
828 IUnknown_Release(object);
830 else
832 *ppv = object;
836 TRACE("completed with hr %#lx\n", hr);
838 return hr;
841 /* Marshalling just passes a unique identifier to the remote client,
842 * that makes it possible to find the passed interface again.
844 * So basically we need a set of values that make it unique.
846 * Note that the IUnknown_QI(ob,xiid,&ppv) always returns the SAME ppv value!
848 * A triple is used: OXID (apt id), OID (stub manager id),
849 * IPID (interface ptr/stub id).
851 * OXIDs identify an apartment and are network scoped
852 * OIDs identify a stub manager and are apartment scoped
853 * IPIDs identify an interface stub and are apartment scoped
856 static inline HRESULT get_facbuf_for_iid(REFIID riid, IPSFactoryBuffer **facbuf)
858 HRESULT hr;
859 CLSID clsid;
861 hr = CoGetPSClsid(riid, &clsid);
862 if (hr != S_OK)
863 return hr;
864 return CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER | CLSCTX_PS_DLL, NULL, &IID_IPSFactoryBuffer, (void **)facbuf);
867 /* marshals an object into a STDOBJREF structure */
868 HRESULT marshal_object(struct apartment *apt, STDOBJREF *stdobjref, REFIID riid, IUnknown *object,
869 DWORD dest_context, void *dest_context_data, MSHLFLAGS mshlflags)
871 struct stub_manager *manager;
872 struct ifstub *ifstub;
873 BOOL tablemarshal;
874 HRESULT hr;
876 stdobjref->oxid = apartment_getoxid(apt);
878 hr = apartment_createwindowifneeded(apt);
879 if (hr != S_OK)
880 return hr;
882 if (!(manager = get_stub_manager_from_object(apt, object, TRUE)))
883 return E_OUTOFMEMORY;
885 stdobjref->flags = SORF_NULL;
886 if (mshlflags & MSHLFLAGS_TABLEWEAK)
887 stdobjref->flags |= SORFP_TABLEWEAK;
888 if (mshlflags & MSHLFLAGS_NOPING)
889 stdobjref->flags |= SORF_NOPING;
890 stdobjref->oid = manager->oid;
892 tablemarshal = ((mshlflags & MSHLFLAGS_TABLESTRONG) || (mshlflags & MSHLFLAGS_TABLEWEAK));
894 /* make sure ifstub that we are creating is unique */
895 ifstub = stub_manager_find_ifstub(manager, riid, mshlflags);
896 if (!ifstub) {
897 IRpcStubBuffer *stub = NULL;
899 /* IUnknown doesn't require a stub buffer, because it never goes out on
900 * the wire */
901 if (!IsEqualIID(riid, &IID_IUnknown))
903 IPSFactoryBuffer *psfb;
905 hr = get_facbuf_for_iid(riid, &psfb);
906 if (hr == S_OK) {
907 hr = IPSFactoryBuffer_CreateStub(psfb, riid, manager->object, &stub);
908 IPSFactoryBuffer_Release(psfb);
909 if (hr != S_OK)
910 ERR("Failed to create an IRpcStubBuffer from IPSFactory for %s with error %#lx\n",
911 debugstr_guid(riid), hr);
912 }else {
913 WARN("couldn't get IPSFactory buffer for interface %s\n", debugstr_guid(riid));
914 hr = E_NOINTERFACE;
919 if (hr == S_OK) {
920 ifstub = stub_manager_new_ifstub(manager, stub, riid, dest_context, dest_context_data, mshlflags);
921 if (!ifstub)
922 hr = E_OUTOFMEMORY;
924 if (stub) IRpcStubBuffer_Release(stub);
926 if (hr != S_OK) {
927 stub_manager_int_release(manager);
928 /* destroy the stub manager if it has no ifstubs by releasing
929 * zero external references */
930 stub_manager_ext_release(manager, 0, FALSE, TRUE);
931 return hr;
935 if (!tablemarshal)
937 stdobjref->cPublicRefs = NORMALEXTREFS;
938 stub_manager_ext_addref(manager, stdobjref->cPublicRefs, FALSE);
940 else
942 stdobjref->cPublicRefs = 0;
943 if (mshlflags & MSHLFLAGS_TABLESTRONG)
944 stub_manager_ext_addref(manager, 1, FALSE);
945 else
946 stub_manager_ext_addref(manager, 0, TRUE);
949 /* FIXME: check return value */
950 rpc_register_interface(riid);
952 stdobjref->ipid = ifstub->ipid;
954 stub_manager_int_release(manager);
955 return S_OK;
958 /* Client-side identity of the server object */
960 static HRESULT proxy_manager_get_remunknown(struct proxy_manager * This, IRemUnknown **remunk);
961 static void proxy_manager_destroy(struct proxy_manager * This);
962 static HRESULT proxy_manager_find_ifproxy(struct proxy_manager * This, REFIID riid, struct ifproxy ** ifproxy_found);
963 static HRESULT proxy_manager_query_local_interface(struct proxy_manager * This, REFIID riid, void ** ppv);
965 static HRESULT WINAPI ClientIdentity_QueryInterface(IMultiQI * iface, REFIID riid, void ** ppv)
967 HRESULT hr;
968 MULTI_QI mqi;
970 TRACE("%s\n", debugstr_guid(riid));
972 mqi.pIID = riid;
973 hr = IMultiQI_QueryMultipleInterfaces(iface, 1, &mqi);
974 *ppv = mqi.pItf;
976 return hr;
979 static ULONG WINAPI ClientIdentity_AddRef(IMultiQI *iface)
981 struct proxy_manager *This = impl_from_IMultiQI(iface);
982 TRACE("%p - before %ld\n", iface, This->refs);
983 return InterlockedIncrement(&This->refs);
986 static ULONG WINAPI ClientIdentity_Release(IMultiQI *iface)
988 struct proxy_manager *This = impl_from_IMultiQI(iface);
989 ULONG refs = InterlockedDecrement(&This->refs);
990 TRACE("%p - after %ld\n", iface, refs);
991 if (!refs)
992 proxy_manager_destroy(This);
993 return refs;
996 static HRESULT WINAPI ClientIdentity_QueryMultipleInterfaces(IMultiQI *iface, ULONG cMQIs, MULTI_QI *pMQIs)
998 struct proxy_manager *This = impl_from_IMultiQI(iface);
999 REMQIRESULT *qiresults = NULL;
1000 ULONG nonlocal_mqis = 0;
1001 ULONG i;
1002 ULONG successful_mqis = 0;
1003 IID *iids = malloc(cMQIs * sizeof(*iids));
1004 /* mapping of RemQueryInterface index to QueryMultipleInterfaces index */
1005 ULONG *mapping = malloc(cMQIs * sizeof(*mapping));
1007 TRACE("cMQIs: %ld\n", cMQIs);
1009 /* try to get a local interface - this includes already active proxy
1010 * interfaces and also interfaces exposed by the proxy manager */
1011 for (i = 0; i < cMQIs; i++)
1013 TRACE("iid[%ld] = %s\n", i, debugstr_guid(pMQIs[i].pIID));
1014 pMQIs[i].hr = proxy_manager_query_local_interface(This, pMQIs[i].pIID, (void **)&pMQIs[i].pItf);
1015 if (pMQIs[i].hr == S_OK)
1016 successful_mqis++;
1017 else
1019 iids[nonlocal_mqis] = *pMQIs[i].pIID;
1020 mapping[nonlocal_mqis] = i;
1021 nonlocal_mqis++;
1025 TRACE("%ld interfaces not found locally\n", nonlocal_mqis);
1027 /* if we have more than one interface not found locally then we must try
1028 * to query the remote object for it */
1029 if (nonlocal_mqis != 0)
1031 IRemUnknown *remunk;
1032 HRESULT hr;
1033 IPID *ipid;
1035 /* get the ipid of the first entry */
1036 /* FIXME: should we implement ClientIdentity on the ifproxies instead
1037 * of the proxy_manager so we use the correct ipid here? */
1038 ipid = &LIST_ENTRY(list_head(&This->interfaces), struct ifproxy, entry)->stdobjref.ipid;
1040 /* get IRemUnknown proxy so we can communicate with the remote object */
1041 hr = proxy_manager_get_remunknown(This, &remunk);
1043 if (SUCCEEDED(hr))
1045 hr = IRemUnknown_RemQueryInterface(remunk, ipid, NORMALEXTREFS,
1046 nonlocal_mqis, iids, &qiresults);
1047 IRemUnknown_Release(remunk);
1048 if (FAILED(hr))
1049 WARN("IRemUnknown_RemQueryInterface failed with error %#lx\n", hr);
1052 /* IRemUnknown_RemQueryInterface can return S_FALSE if only some of
1053 * the interfaces were returned */
1054 if (SUCCEEDED(hr))
1056 struct apartment *apt = apartment_get_current_or_mta();
1058 /* try to unmarshal each object returned to us */
1059 for (i = 0; i < nonlocal_mqis; i++)
1061 ULONG index = mapping[i];
1062 HRESULT hrobj = qiresults[i].hResult;
1063 if (hrobj == S_OK)
1064 hrobj = unmarshal_object(&qiresults[i].std, apt,
1065 This->dest_context,
1066 This->dest_context_data,
1067 pMQIs[index].pIID, &This->oxid_info,
1068 (void **)&pMQIs[index].pItf);
1070 if (hrobj == S_OK)
1071 successful_mqis++;
1072 else
1073 ERR("Failed to get pointer to interface %s\n", debugstr_guid(pMQIs[index].pIID));
1074 pMQIs[index].hr = hrobj;
1077 apartment_release(apt);
1080 /* free the memory allocated by the proxy */
1081 CoTaskMemFree(qiresults);
1084 TRACE("%ld/%ld successfully queried\n", successful_mqis, cMQIs);
1086 free(iids);
1087 free(mapping);
1089 if (successful_mqis == cMQIs)
1090 return S_OK; /* we got all requested interfaces */
1091 else if (successful_mqis == 0)
1092 return E_NOINTERFACE; /* we didn't get any interfaces */
1093 else
1094 return S_FALSE; /* we got some interfaces */
1097 static const IMultiQIVtbl ClientIdentity_Vtbl =
1099 ClientIdentity_QueryInterface,
1100 ClientIdentity_AddRef,
1101 ClientIdentity_Release,
1102 ClientIdentity_QueryMultipleInterfaces
1105 static HRESULT StdMarshalImpl_Construct(REFIID, DWORD, void*, void**);
1107 static HRESULT WINAPI Proxy_QueryInterface(IMarshal *iface, REFIID riid, void **ppvObject)
1109 struct proxy_manager *This = impl_from_IMarshal( iface );
1110 return IMultiQI_QueryInterface(&This->IMultiQI_iface, riid, ppvObject);
1113 static ULONG WINAPI Proxy_AddRef(IMarshal *iface)
1115 struct proxy_manager *This = impl_from_IMarshal( iface );
1116 return IMultiQI_AddRef(&This->IMultiQI_iface);
1119 static ULONG WINAPI Proxy_Release(IMarshal *iface)
1121 struct proxy_manager *This = impl_from_IMarshal( iface );
1122 return IMultiQI_Release(&This->IMultiQI_iface);
1125 static HRESULT WINAPI Proxy_GetUnmarshalClass(
1126 IMarshal *iface, REFIID riid, void* pv, DWORD dwDestContext,
1127 void* pvDestContext, DWORD mshlflags, CLSID* pCid)
1129 *pCid = CLSID_StdMarshal;
1130 return S_OK;
1133 static HRESULT WINAPI Proxy_GetMarshalSizeMax(
1134 IMarshal *iface, REFIID riid, void* pv, DWORD dwDestContext,
1135 void* pvDestContext, DWORD mshlflags, DWORD* pSize)
1137 *pSize = FIELD_OFFSET(OBJREF, u_objref.u_standard.saResAddr.aStringArray);
1138 return S_OK;
1141 static void fill_std_objref(OBJREF *objref, const GUID *iid, STDOBJREF *std)
1143 objref->signature = OBJREF_SIGNATURE;
1144 objref->flags = OBJREF_STANDARD;
1145 objref->iid = *iid;
1146 if(std)
1147 objref->u_objref.u_standard.std = *std;
1148 memset(&objref->u_objref.u_standard.saResAddr, 0,
1149 sizeof(objref->u_objref.u_standard.saResAddr));
1152 static HRESULT WINAPI Proxy_MarshalInterface(
1153 LPMARSHAL iface, IStream *pStm, REFIID riid, void* pv, DWORD dwDestContext,
1154 void* pvDestContext, DWORD mshlflags)
1156 struct proxy_manager *This = impl_from_IMarshal( iface );
1157 HRESULT hr;
1158 struct ifproxy *ifproxy;
1160 TRACE("(...,%s,...)\n", debugstr_guid(riid));
1162 hr = proxy_manager_find_ifproxy(This, riid, &ifproxy);
1163 if (SUCCEEDED(hr))
1165 STDOBJREF stdobjref = ifproxy->stdobjref;
1167 stdobjref.cPublicRefs = 0;
1169 if ((mshlflags != MSHLFLAGS_TABLEWEAK) &&
1170 (mshlflags != MSHLFLAGS_TABLESTRONG))
1172 ULONG cPublicRefs = ifproxy->refs;
1173 ULONG cPublicRefsOld;
1174 /* optimization - share out proxy's public references if possible
1175 * instead of making new proxy do a roundtrip through the server */
1178 ULONG cPublicRefsNew;
1179 cPublicRefsOld = cPublicRefs;
1180 stdobjref.cPublicRefs = cPublicRefs / 2;
1181 cPublicRefsNew = cPublicRefs - stdobjref.cPublicRefs;
1182 cPublicRefs = InterlockedCompareExchange(
1183 (LONG *)&ifproxy->refs, cPublicRefsNew, cPublicRefsOld);
1184 } while (cPublicRefs != cPublicRefsOld);
1187 /* normal and table-strong marshaling need at least one reference */
1188 if (!stdobjref.cPublicRefs && (mshlflags != MSHLFLAGS_TABLEWEAK))
1190 IRemUnknown *remunk;
1191 hr = proxy_manager_get_remunknown(This, &remunk);
1192 if (hr == S_OK)
1194 HRESULT hrref = S_OK;
1195 REMINTERFACEREF rif;
1196 rif.ipid = ifproxy->stdobjref.ipid;
1197 rif.cPublicRefs = (mshlflags == MSHLFLAGS_TABLESTRONG) ? 1 : NORMALEXTREFS;
1198 rif.cPrivateRefs = 0;
1199 hr = IRemUnknown_RemAddRef(remunk, 1, &rif, &hrref);
1200 IRemUnknown_Release(remunk);
1201 if (hr == S_OK && hrref == S_OK)
1203 /* table-strong marshaling doesn't give the refs to the
1204 * client that unmarshals the STDOBJREF */
1205 if (mshlflags != MSHLFLAGS_TABLESTRONG)
1206 stdobjref.cPublicRefs = rif.cPublicRefs;
1208 else
1209 ERR("IRemUnknown_RemAddRef returned with %#lx, hrref = %#lx\n", hr, hrref);
1213 if (SUCCEEDED(hr))
1215 OBJREF objref;
1217 TRACE("writing stdobjref: flags = %#lx cPublicRefs = %ld oxid = %s oid = %s ipid = %s\n",
1218 stdobjref.flags, stdobjref.cPublicRefs,
1219 wine_dbgstr_longlong(stdobjref.oxid),
1220 wine_dbgstr_longlong(stdobjref.oid),
1221 debugstr_guid(&stdobjref.ipid));
1222 fill_std_objref(&objref, riid, &stdobjref);
1223 hr = IStream_Write(pStm, &objref, FIELD_OFFSET(OBJREF,
1224 u_objref.u_standard.saResAddr.aStringArray), NULL);
1227 else
1229 /* we don't have the interface already unmarshaled so we have to
1230 * request the object from the server */
1231 IRemUnknown *remunk;
1232 IPID *ipid;
1233 REMQIRESULT *qiresults = NULL;
1234 IID iid = *riid;
1236 /* get the ipid of the first entry */
1237 /* FIXME: should we implement ClientIdentity on the ifproxies instead
1238 * of the proxy_manager so we use the correct ipid here? */
1239 ipid = &LIST_ENTRY(list_head(&This->interfaces), struct ifproxy, entry)->stdobjref.ipid;
1241 /* get IRemUnknown proxy so we can communicate with the remote object */
1242 hr = proxy_manager_get_remunknown(This, &remunk);
1244 if (hr == S_OK)
1246 hr = IRemUnknown_RemQueryInterface(remunk, ipid, NORMALEXTREFS,
1247 1, &iid, &qiresults);
1248 if (SUCCEEDED(hr))
1250 OBJREF objref;
1252 fill_std_objref(&objref, riid, &qiresults->std);
1253 hr = IStream_Write(pStm, &objref, FIELD_OFFSET(OBJREF,
1254 u_objref.u_standard.saResAddr.aStringArray), NULL);
1255 if (FAILED(hr))
1257 REMINTERFACEREF rif;
1258 rif.ipid = qiresults->std.ipid;
1259 rif.cPublicRefs = qiresults->std.cPublicRefs;
1260 rif.cPrivateRefs = 0;
1261 IRemUnknown_RemRelease(remunk, 1, &rif);
1263 CoTaskMemFree(qiresults);
1265 else
1266 ERR("IRemUnknown_RemQueryInterface failed with error %#lx\n", hr);
1267 IRemUnknown_Release(remunk);
1271 return hr;
1274 static HRESULT WINAPI Proxy_UnmarshalInterface(
1275 IMarshal *iface, IStream *pStm, REFIID riid, void **ppv)
1277 struct proxy_manager *This = impl_from_IMarshal( iface );
1278 IMarshal *marshal;
1279 HRESULT hr;
1281 TRACE("(%p, %p, %s, %p)\n", This, pStm, wine_dbgstr_guid(riid), ppv);
1283 hr = StdMarshalImpl_Construct(&IID_IMarshal, This->dest_context,
1284 This->dest_context_data, (void**)&marshal);
1285 if(FAILED(hr))
1286 return hr;
1288 hr = IMarshal_UnmarshalInterface(marshal, pStm, riid, ppv);
1289 IMarshal_Release(marshal);
1290 return hr;
1293 static HRESULT WINAPI Proxy_ReleaseMarshalData(IMarshal *iface, IStream *pStm)
1295 struct proxy_manager *This = impl_from_IMarshal( iface );
1296 IMarshal *marshal;
1297 HRESULT hr;
1299 TRACE("(%p, %p)\n", This, pStm);
1301 hr = StdMarshalImpl_Construct(&IID_IMarshal, This->dest_context,
1302 This->dest_context_data, (void**)&marshal);
1303 if(FAILED(hr))
1304 return hr;
1306 hr = IMarshal_ReleaseMarshalData(marshal, pStm);
1307 IMarshal_Release(marshal);
1308 return hr;
1311 static HRESULT WINAPI Proxy_DisconnectObject(IMarshal *iface, DWORD dwReserved)
1313 struct proxy_manager *This = impl_from_IMarshal( iface );
1314 IMarshal *marshal;
1315 HRESULT hr;
1317 TRACE("%p, %#lx\n", This, dwReserved);
1319 hr = StdMarshalImpl_Construct(&IID_IMarshal, This->dest_context,
1320 This->dest_context_data, (void**)&marshal);
1321 if(FAILED(hr))
1322 return hr;
1324 hr = IMarshal_DisconnectObject(marshal, dwReserved);
1325 IMarshal_Release(marshal);
1326 return hr;
1329 static const IMarshalVtbl ProxyMarshal_Vtbl =
1331 Proxy_QueryInterface,
1332 Proxy_AddRef,
1333 Proxy_Release,
1334 Proxy_GetUnmarshalClass,
1335 Proxy_GetMarshalSizeMax,
1336 Proxy_MarshalInterface,
1337 Proxy_UnmarshalInterface,
1338 Proxy_ReleaseMarshalData,
1339 Proxy_DisconnectObject
1342 static HRESULT WINAPI ProxyCliSec_QueryInterface(IClientSecurity *iface, REFIID riid, void **ppvObject)
1344 struct proxy_manager *This = impl_from_IClientSecurity( iface );
1345 return IMultiQI_QueryInterface(&This->IMultiQI_iface, riid, ppvObject);
1348 static ULONG WINAPI ProxyCliSec_AddRef(IClientSecurity *iface)
1350 struct proxy_manager *This = impl_from_IClientSecurity( iface );
1351 return IMultiQI_AddRef(&This->IMultiQI_iface);
1354 static ULONG WINAPI ProxyCliSec_Release(IClientSecurity *iface)
1356 struct proxy_manager *This = impl_from_IClientSecurity( iface );
1357 return IMultiQI_Release(&This->IMultiQI_iface);
1360 static HRESULT WINAPI ProxyCliSec_QueryBlanket(IClientSecurity *iface,
1361 IUnknown *pProxy,
1362 DWORD *pAuthnSvc,
1363 DWORD *pAuthzSvc,
1364 OLECHAR **ppServerPrincName,
1365 DWORD *pAuthnLevel,
1366 DWORD *pImpLevel,
1367 void **pAuthInfo,
1368 DWORD *pCapabilities)
1370 FIXME("(%p, %p, %p, %p, %p, %p, %p, %p): stub\n", pProxy, pAuthnSvc,
1371 pAuthzSvc, ppServerPrincName, pAuthnLevel, pImpLevel, pAuthInfo,
1372 pCapabilities);
1374 if (pAuthnSvc)
1375 *pAuthnSvc = 0;
1376 if (pAuthzSvc)
1377 *pAuthzSvc = 0;
1378 if (ppServerPrincName)
1379 *ppServerPrincName = NULL;
1380 if (pAuthnLevel)
1381 *pAuthnLevel = RPC_C_AUTHN_LEVEL_DEFAULT;
1382 if (pImpLevel)
1383 *pImpLevel = RPC_C_IMP_LEVEL_DEFAULT;
1384 if (pAuthInfo)
1385 *pAuthInfo = NULL;
1386 if (pCapabilities)
1387 *pCapabilities = EOAC_NONE;
1389 return E_NOTIMPL;
1392 static HRESULT WINAPI ProxyCliSec_SetBlanket(IClientSecurity *iface,
1393 IUnknown *pProxy, DWORD AuthnSvc,
1394 DWORD AuthzSvc,
1395 OLECHAR *pServerPrincName,
1396 DWORD AuthnLevel, DWORD ImpLevel,
1397 void *pAuthInfo,
1398 DWORD Capabilities)
1400 FIXME("%p, %ld, %ld, %s, %ld, %ld, %p, %#lx: stub\n", pProxy, AuthnSvc, AuthzSvc,
1401 pServerPrincName == COLE_DEFAULT_PRINCIPAL ? "<default principal>" : debugstr_w(pServerPrincName),
1402 AuthnLevel, ImpLevel, pAuthInfo, Capabilities);
1403 return E_NOTIMPL;
1406 static HRESULT WINAPI ProxyCliSec_CopyProxy(IClientSecurity *iface,
1407 IUnknown *pProxy, IUnknown **ppCopy)
1409 FIXME("(%p, %p): stub\n", pProxy, ppCopy);
1410 *ppCopy = NULL;
1411 return E_NOTIMPL;
1414 static const IClientSecurityVtbl ProxyCliSec_Vtbl =
1416 ProxyCliSec_QueryInterface,
1417 ProxyCliSec_AddRef,
1418 ProxyCliSec_Release,
1419 ProxyCliSec_QueryBlanket,
1420 ProxyCliSec_SetBlanket,
1421 ProxyCliSec_CopyProxy
1424 static HRESULT ifproxy_get_public_ref(struct ifproxy * This)
1426 HRESULT hr = S_OK;
1428 if (WAIT_OBJECT_0 != WaitForSingleObject(This->parent->remoting_mutex, INFINITE))
1430 ERR("Wait failed for ifproxy %p\n", This);
1431 return E_UNEXPECTED;
1434 if (This->refs == 0)
1436 IRemUnknown *remunk = NULL;
1438 TRACE("getting public ref for ifproxy %p\n", This);
1440 hr = proxy_manager_get_remunknown(This->parent, &remunk);
1441 if (hr == S_OK)
1443 HRESULT hrref = S_OK;
1444 REMINTERFACEREF rif;
1445 rif.ipid = This->stdobjref.ipid;
1446 rif.cPublicRefs = NORMALEXTREFS;
1447 rif.cPrivateRefs = 0;
1448 hr = IRemUnknown_RemAddRef(remunk, 1, &rif, &hrref);
1449 IRemUnknown_Release(remunk);
1450 if (hr == S_OK && hrref == S_OK)
1451 InterlockedExchangeAdd((LONG *)&This->refs, NORMALEXTREFS);
1452 else
1453 ERR("IRemUnknown_RemAddRef returned with %#lx, hrref = %#lx\n", hr, hrref);
1456 ReleaseMutex(This->parent->remoting_mutex);
1458 return hr;
1461 static HRESULT ifproxy_release_public_refs(struct ifproxy * This)
1463 HRESULT hr = S_OK;
1464 LONG public_refs;
1466 if (WAIT_OBJECT_0 != WaitForSingleObject(This->parent->remoting_mutex, INFINITE))
1468 ERR("Wait failed for ifproxy %p\n", This);
1469 return E_UNEXPECTED;
1472 public_refs = This->refs;
1473 if (public_refs > 0)
1475 IRemUnknown *remunk = NULL;
1477 TRACE("releasing %ld refs\n", public_refs);
1479 hr = proxy_manager_get_remunknown(This->parent, &remunk);
1480 if (hr == S_OK)
1482 REMINTERFACEREF rif;
1483 rif.ipid = This->stdobjref.ipid;
1484 rif.cPublicRefs = public_refs;
1485 rif.cPrivateRefs = 0;
1486 hr = IRemUnknown_RemRelease(remunk, 1, &rif);
1487 IRemUnknown_Release(remunk);
1488 if (hr == S_OK)
1489 InterlockedExchangeAdd((LONG *)&This->refs, -public_refs);
1490 else if (hr == RPC_E_DISCONNECTED)
1491 WARN("couldn't release references because object was "
1492 "disconnected: oxid = %s, oid = %s\n",
1493 wine_dbgstr_longlong(This->parent->oxid),
1494 wine_dbgstr_longlong(This->parent->oid));
1495 else
1496 ERR("IRemUnknown_RemRelease failed with error %#lx\n", hr);
1499 ReleaseMutex(This->parent->remoting_mutex);
1501 return hr;
1504 /* should be called inside This->parent->cs critical section */
1505 static void ifproxy_disconnect(struct ifproxy * This)
1507 ifproxy_release_public_refs(This);
1508 if (This->proxy) IRpcProxyBuffer_Disconnect(This->proxy);
1510 IRpcChannelBuffer_Release(This->chan);
1511 This->chan = NULL;
1514 /* should be called in This->parent->cs critical section if it is an entry in parent's list */
1515 static void ifproxy_destroy(struct ifproxy * This)
1517 TRACE("%p\n", This);
1519 /* release public references to this object so that the stub can know
1520 * when to destroy itself */
1521 ifproxy_release_public_refs(This);
1523 list_remove(&This->entry);
1525 if (This->chan)
1527 IRpcChannelBuffer_Release(This->chan);
1528 This->chan = NULL;
1531 if (This->proxy) IRpcProxyBuffer_Release(This->proxy);
1533 free(This);
1536 static HRESULT proxy_manager_construct(
1537 struct apartment * apt, ULONG sorflags, OXID oxid, OID oid,
1538 const OXID_INFO *oxid_info, struct proxy_manager ** proxy_manager)
1540 struct proxy_manager * This = malloc(sizeof(*This));
1541 if (!This) return E_OUTOFMEMORY;
1543 This->remoting_mutex = CreateMutexW(NULL, FALSE, NULL);
1544 if (!This->remoting_mutex)
1546 free(This);
1547 return HRESULT_FROM_WIN32(GetLastError());
1550 if (oxid_info)
1552 This->oxid_info.dwPid = oxid_info->dwPid;
1553 This->oxid_info.dwTid = oxid_info->dwTid;
1554 This->oxid_info.ipidRemUnknown = oxid_info->ipidRemUnknown;
1555 This->oxid_info.dwAuthnHint = oxid_info->dwAuthnHint;
1556 This->oxid_info.psa = NULL /* FIXME: copy from oxid_info */;
1558 else
1560 HRESULT hr = rpc_resolve_oxid(oxid, &This->oxid_info);
1561 if (FAILED(hr))
1563 CloseHandle(This->remoting_mutex);
1564 free(This);
1565 return hr;
1569 This->IMultiQI_iface.lpVtbl = &ClientIdentity_Vtbl;
1570 This->IMarshal_iface.lpVtbl = &ProxyMarshal_Vtbl;
1571 This->IClientSecurity_iface.lpVtbl = &ProxyCliSec_Vtbl;
1573 list_init(&This->entry);
1574 list_init(&This->interfaces);
1576 InitializeCriticalSection(&This->cs);
1577 This->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": proxy_manager");
1579 /* the apartment the object was unmarshaled into */
1580 This->parent = apt;
1582 /* the source apartment and id of the object */
1583 This->oxid = oxid;
1584 This->oid = oid;
1586 This->refs = 1;
1588 /* the DCOM draft specification states that the SORF_NOPING flag is
1589 * proxy manager specific, not ifproxy specific, so this implies that we
1590 * should store the STDOBJREF flags here in the proxy manager. */
1591 This->sorflags = sorflags;
1593 /* we create the IRemUnknown proxy on demand */
1594 This->remunk = NULL;
1596 /* initialise these values to the weakest values and they will be
1597 * overwritten in proxy_manager_set_context */
1598 This->dest_context = MSHCTX_INPROC;
1599 This->dest_context_data = NULL;
1601 EnterCriticalSection(&apt->cs);
1602 /* FIXME: we are dependent on the ordering in here to make sure a proxy's
1603 * IRemUnknown proxy doesn't get destroyed before the regular proxy does
1604 * because we need the IRemUnknown proxy during the destruction of the
1605 * regular proxy. Ideally, we should maintain a separate list for the
1606 * IRemUnknown proxies that need late destruction */
1607 list_add_tail(&apt->proxies, &This->entry);
1608 LeaveCriticalSection(&apt->cs);
1610 TRACE("%p created for OXID %s, OID %s\n", This,
1611 wine_dbgstr_longlong(oxid), wine_dbgstr_longlong(oid));
1613 *proxy_manager = This;
1614 return S_OK;
1617 static inline void proxy_manager_set_context(struct proxy_manager *This, MSHCTX dest_context, void *dest_context_data)
1619 MSHCTX old_dest_context;
1620 MSHCTX new_dest_context;
1624 old_dest_context = This->dest_context;
1625 new_dest_context = old_dest_context;
1626 /* "stronger" values overwrite "weaker" values. stronger values are
1627 * ones that disable more optimisations */
1628 switch (old_dest_context)
1630 case MSHCTX_INPROC:
1631 new_dest_context = dest_context;
1632 break;
1633 case MSHCTX_CROSSCTX:
1634 switch (dest_context)
1636 case MSHCTX_INPROC:
1637 break;
1638 default:
1639 new_dest_context = dest_context;
1641 break;
1642 case MSHCTX_LOCAL:
1643 switch (dest_context)
1645 case MSHCTX_INPROC:
1646 case MSHCTX_CROSSCTX:
1647 break;
1648 default:
1649 new_dest_context = dest_context;
1651 break;
1652 case MSHCTX_NOSHAREDMEM:
1653 switch (dest_context)
1655 case MSHCTX_DIFFERENTMACHINE:
1656 new_dest_context = dest_context;
1657 break;
1658 default:
1659 break;
1661 break;
1662 default:
1663 break;
1666 if (old_dest_context == new_dest_context) break;
1668 new_dest_context = InterlockedCompareExchange((PLONG)&This->dest_context, new_dest_context, old_dest_context);
1669 } while (new_dest_context != old_dest_context);
1671 if (dest_context_data)
1672 InterlockedExchangePointer(&This->dest_context_data, dest_context_data);
1675 static HRESULT proxy_manager_query_local_interface(struct proxy_manager * This, REFIID riid, void ** ppv)
1677 HRESULT hr;
1678 struct ifproxy * ifproxy;
1680 TRACE("%s\n", debugstr_guid(riid));
1682 if (IsEqualIID(riid, &IID_IUnknown) ||
1683 IsEqualIID(riid, &IID_IMultiQI))
1685 *ppv = &This->IMultiQI_iface;
1686 IMultiQI_AddRef(&This->IMultiQI_iface);
1687 return S_OK;
1689 if (IsEqualIID(riid, &IID_IMarshal))
1691 *ppv = &This->IMarshal_iface;
1692 IMarshal_AddRef(&This->IMarshal_iface);
1693 return S_OK;
1695 if (IsEqualIID(riid, &IID_IClientSecurity))
1697 *ppv = &This->IClientSecurity_iface;
1698 IClientSecurity_AddRef(&This->IClientSecurity_iface);
1699 return S_OK;
1702 hr = proxy_manager_find_ifproxy(This, riid, &ifproxy);
1703 if (hr == S_OK)
1705 *ppv = ifproxy->iface;
1706 IUnknown_AddRef((IUnknown *)*ppv);
1707 return S_OK;
1710 *ppv = NULL;
1711 return E_NOINTERFACE;
1714 static HRESULT proxy_manager_create_ifproxy(
1715 struct proxy_manager * This, const STDOBJREF *stdobjref, REFIID riid,
1716 IRpcChannelBuffer * channel, struct ifproxy ** iif_out)
1718 HRESULT hr;
1719 IPSFactoryBuffer * psfb;
1720 struct ifproxy * ifproxy = malloc(sizeof(*ifproxy));
1721 if (!ifproxy) return E_OUTOFMEMORY;
1723 list_init(&ifproxy->entry);
1725 ifproxy->parent = This;
1726 ifproxy->stdobjref = *stdobjref;
1727 ifproxy->iid = *riid;
1728 ifproxy->refs = 0;
1729 ifproxy->proxy = NULL;
1731 assert(channel);
1732 ifproxy->chan = channel; /* FIXME: we should take the binding strings and construct the channel in this function */
1734 /* the IUnknown interface is special because it does not have a
1735 * proxy associated with the ifproxy as we handle IUnknown ourselves */
1736 if (IsEqualIID(riid, &IID_IUnknown))
1738 ifproxy->iface = &This->IMultiQI_iface;
1739 IMultiQI_AddRef(&This->IMultiQI_iface);
1740 hr = S_OK;
1742 else
1744 hr = get_facbuf_for_iid(riid, &psfb);
1745 if (hr == S_OK)
1747 /* important note: the outer unknown is set to the proxy manager.
1748 * This ensures the COM identity rules are not violated, by having a
1749 * one-to-one mapping of objects on the proxy side to objects on the
1750 * stub side, no matter which interface you view the object through */
1751 hr = IPSFactoryBuffer_CreateProxy(psfb, (IUnknown*)&This->IMultiQI_iface, riid,
1752 &ifproxy->proxy, &ifproxy->iface);
1753 IPSFactoryBuffer_Release(psfb);
1754 if (hr != S_OK)
1755 ERR("Could not create proxy for interface %s, error %#lx\n", debugstr_guid(riid), hr);
1757 else
1758 ERR("Could not get IPSFactoryBuffer for interface %s, error %#lx\n", debugstr_guid(riid), hr);
1760 if (hr == S_OK)
1761 hr = IRpcProxyBuffer_Connect(ifproxy->proxy, ifproxy->chan);
1764 if (hr == S_OK)
1766 EnterCriticalSection(&This->cs);
1767 list_add_tail(&This->interfaces, &ifproxy->entry);
1768 LeaveCriticalSection(&This->cs);
1770 *iif_out = ifproxy;
1771 TRACE("ifproxy %p created for IPID %s, interface %s with %lu public refs\n",
1772 ifproxy, debugstr_guid(&stdobjref->ipid), debugstr_guid(riid), stdobjref->cPublicRefs);
1774 else
1775 ifproxy_destroy(ifproxy);
1777 return hr;
1780 static HRESULT proxy_manager_find_ifproxy(struct proxy_manager * This, REFIID riid, struct ifproxy ** ifproxy_found)
1782 HRESULT hr = E_NOINTERFACE; /* assume not found */
1783 struct ifproxy *ifproxy;
1785 EnterCriticalSection(&This->cs);
1786 LIST_FOR_EACH_ENTRY(ifproxy, &This->interfaces, struct ifproxy, entry)
1788 if (IsEqualIID(riid, &ifproxy->iid))
1790 *ifproxy_found = ifproxy;
1791 hr = S_OK;
1792 break;
1795 LeaveCriticalSection(&This->cs);
1797 return hr;
1800 static void proxy_manager_disconnect(struct proxy_manager * This)
1802 struct ifproxy *ifproxy;
1804 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This->oxid),
1805 wine_dbgstr_longlong(This->oid));
1807 EnterCriticalSection(&This->cs);
1809 /* SORFP_NOLIFTIMEMGMT proxies (for IRemUnknown) shouldn't be
1810 * disconnected - it won't do anything anyway, except cause
1811 * problems for other objects that depend on this proxy always
1812 * working */
1813 if (!(This->sorflags & SORFP_NOLIFETIMEMGMT))
1815 LIST_FOR_EACH_ENTRY(ifproxy, &This->interfaces, struct ifproxy, entry)
1817 ifproxy_disconnect(ifproxy);
1821 /* apartment is being destroyed so don't keep a pointer around to it */
1822 This->parent = NULL;
1824 LeaveCriticalSection(&This->cs);
1827 static HRESULT proxy_manager_get_remunknown(struct proxy_manager * This, IRemUnknown **remunk)
1829 HRESULT hr = S_OK;
1830 struct apartment *apt;
1831 BOOL called_in_original_apt;
1833 /* we don't want to try and unmarshal or use IRemUnknown if we don't want
1834 * lifetime management */
1835 if (This->sorflags & SORFP_NOLIFETIMEMGMT)
1836 return S_FALSE;
1838 if (!(apt = apartment_get_current_or_mta()))
1839 return CO_E_NOTINITIALIZED;
1841 called_in_original_apt = This->parent && (This->parent->oxid == apt->oxid);
1843 EnterCriticalSection(&This->cs);
1844 /* only return the cached object if called from the original apartment.
1845 * in future, we might want to make the IRemUnknown proxy callable from any
1846 * apartment to avoid these checks */
1847 if (This->remunk && called_in_original_apt)
1849 /* already created - return existing object */
1850 *remunk = This->remunk;
1851 IRemUnknown_AddRef(*remunk);
1853 else if (!This->parent)
1855 /* disconnected - we can't create IRemUnknown */
1856 *remunk = NULL;
1857 hr = S_FALSE;
1859 else
1861 STDOBJREF stdobjref;
1862 /* Don't want IRemUnknown lifetime management as this is IRemUnknown!
1863 * We also don't care about whether or not the stub is still alive */
1864 stdobjref.flags = SORFP_NOLIFETIMEMGMT | SORF_NOPING;
1865 stdobjref.cPublicRefs = 1;
1866 /* oxid of destination object */
1867 stdobjref.oxid = This->oxid;
1868 /* FIXME: what should be used for the oid? The DCOM draft doesn't say */
1869 stdobjref.oid = (OID)-1;
1870 stdobjref.ipid = This->oxid_info.ipidRemUnknown;
1872 /* do the unmarshal */
1873 hr = unmarshal_object(&stdobjref, apt, This->dest_context,
1874 This->dest_context_data, &IID_IRemUnknown,
1875 &This->oxid_info, (void**)remunk);
1876 if (hr == S_OK && called_in_original_apt)
1878 This->remunk = *remunk;
1879 IRemUnknown_AddRef(This->remunk);
1882 LeaveCriticalSection(&This->cs);
1883 apartment_release(apt);
1885 TRACE("got IRemUnknown* pointer %p, hr = %#lx\n", *remunk, hr);
1887 return hr;
1890 /* destroys a proxy manager, freeing the memory it used.
1891 * Note: this function should not be called from a list iteration in the
1892 * apartment, due to the fact that it removes itself from the apartment and
1893 * it could add a proxy to IRemUnknown into the apartment. */
1894 static void proxy_manager_destroy(struct proxy_manager * This)
1896 struct list * cursor;
1898 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This->oxid),
1899 wine_dbgstr_longlong(This->oid));
1901 if (This->parent)
1903 EnterCriticalSection(&This->parent->cs);
1905 /* remove ourself from the list of proxy objects in the apartment */
1906 LIST_FOR_EACH(cursor, &This->parent->proxies)
1908 if (cursor == &This->entry)
1910 list_remove(&This->entry);
1911 break;
1915 LeaveCriticalSection(&This->parent->cs);
1918 /* destroy all of the interface proxies */
1919 while ((cursor = list_head(&This->interfaces)))
1921 struct ifproxy * ifproxy = LIST_ENTRY(cursor, struct ifproxy, entry);
1922 ifproxy_destroy(ifproxy);
1925 if (This->remunk) IRemUnknown_Release(This->remunk);
1926 CoTaskMemFree(This->oxid_info.psa);
1928 This->cs.DebugInfo->Spare[0] = 0;
1929 DeleteCriticalSection(&This->cs);
1931 CloseHandle(This->remoting_mutex);
1933 free(This);
1936 /* finds the proxy manager corresponding to a given OXID and OID that has
1937 * been unmarshaled in the specified apartment. The caller must release the
1938 * reference to the proxy_manager when the object is no longer used. */
1939 static BOOL find_proxy_manager(struct apartment * apt, OXID oxid, OID oid, struct proxy_manager ** proxy_found)
1941 struct proxy_manager *proxy;
1942 BOOL found = FALSE;
1944 EnterCriticalSection(&apt->cs);
1945 LIST_FOR_EACH_ENTRY(proxy, &apt->proxies, struct proxy_manager, entry)
1947 if ((oxid == proxy->oxid) && (oid == proxy->oid))
1949 /* be careful of a race with ClientIdentity_Release, which would
1950 * cause us to return a proxy which is in the process of being
1951 * destroyed */
1952 if (IMultiQI_AddRef(&proxy->IMultiQI_iface) != 0)
1954 *proxy_found = proxy;
1955 found = TRUE;
1956 break;
1960 LeaveCriticalSection(&apt->cs);
1961 return found;
1964 HRESULT apartment_disconnectproxies(struct apartment *apt)
1966 struct proxy_manager *proxy;
1968 LIST_FOR_EACH_ENTRY(proxy, &apt->proxies, struct proxy_manager, entry)
1970 proxy_manager_disconnect(proxy);
1973 return S_OK;
1976 /********************** StdMarshal implementation ****************************/
1978 struct stdmarshal
1980 IMarshal IMarshal_iface;
1981 LONG refcount;
1982 DWORD dest_context;
1983 void *dest_context_data;
1986 static inline struct stdmarshal *impl_from_StdMarshal(IMarshal *iface)
1988 return CONTAINING_RECORD(iface, struct stdmarshal, IMarshal_iface);
1991 static HRESULT WINAPI StdMarshalImpl_QueryInterface(IMarshal *iface, REFIID riid, void **ppv)
1993 *ppv = NULL;
1994 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IMarshal, riid))
1996 *ppv = iface;
1997 IMarshal_AddRef(iface);
1998 return S_OK;
2000 FIXME("No interface for %s.\n", debugstr_guid(riid));
2001 return E_NOINTERFACE;
2004 static ULONG WINAPI StdMarshalImpl_AddRef(IMarshal *iface)
2006 struct stdmarshal *marshal = impl_from_StdMarshal(iface);
2007 return InterlockedIncrement(&marshal->refcount);
2010 static ULONG WINAPI StdMarshalImpl_Release(IMarshal *iface)
2012 struct stdmarshal *marshal = impl_from_StdMarshal(iface);
2013 ULONG refcount = InterlockedDecrement(&marshal->refcount);
2015 if (!refcount)
2016 free(marshal);
2018 return refcount;
2021 static HRESULT WINAPI StdMarshalImpl_GetUnmarshalClass(IMarshal *iface, REFIID riid, void *pv,
2022 DWORD dwDestContext, void *pvDestContext, DWORD mshlflags, CLSID *pCid)
2024 *pCid = CLSID_StdMarshal;
2025 return S_OK;
2028 static HRESULT WINAPI StdMarshalImpl_GetMarshalSizeMax(IMarshal *iface, REFIID riid, void *pv,
2029 DWORD dwDestContext, void *pvDestContext, DWORD mshlflags, DWORD *pSize)
2031 *pSize = FIELD_OFFSET(OBJREF, u_objref.u_standard.saResAddr.aStringArray);
2032 return S_OK;
2035 static HRESULT WINAPI StdMarshalImpl_MarshalInterface(IMarshal *iface, IStream *stream, REFIID riid, void *pv,
2036 DWORD dest_context, void *dest_context_data, DWORD mshlflags)
2038 ULONG res;
2039 HRESULT hr;
2040 OBJREF objref;
2041 struct apartment *apt;
2043 TRACE("(...,%s,...)\n", debugstr_guid(riid));
2045 if (!(apt = apartment_get_current_or_mta()))
2047 ERR("Apartment not initialized\n");
2048 return CO_E_NOTINITIALIZED;
2051 /* make sure this apartment can be reached from other threads / processes */
2052 rpc_start_remoting(apt);
2054 fill_std_objref(&objref, riid, NULL);
2055 hr = marshal_object(apt, &objref.u_objref.u_standard.std, riid, pv, dest_context,
2056 dest_context_data, mshlflags);
2057 apartment_release(apt);
2058 if (hr != S_OK)
2060 ERR("Failed to create ifstub, hr %#lx\n", hr);
2061 return hr;
2064 return IStream_Write(stream, &objref, FIELD_OFFSET(OBJREF, u_objref.u_standard.saResAddr.aStringArray), &res);
2067 /* helper for StdMarshalImpl_UnmarshalInterface - does the unmarshaling with
2068 * no questions asked about the rules surrounding same-apartment unmarshals
2069 * and table marshaling */
2070 static HRESULT unmarshal_object(const STDOBJREF *stdobjref, struct apartment *apt, MSHCTX dest_context,
2071 void *dest_context_data, REFIID riid, const OXID_INFO *oxid_info, void **object)
2073 struct proxy_manager *proxy_manager = NULL;
2074 HRESULT hr = S_OK;
2076 assert(apt);
2078 TRACE("stdobjref: flags = %#lx cPublicRefs = %ld oxid = %s oid = %s ipid = %s\n",
2079 stdobjref->flags, stdobjref->cPublicRefs,
2080 wine_dbgstr_longlong(stdobjref->oxid),
2081 wine_dbgstr_longlong(stdobjref->oid),
2082 debugstr_guid(&stdobjref->ipid));
2084 /* create a new proxy manager if one doesn't already exist for the
2085 * object */
2086 if (!find_proxy_manager(apt, stdobjref->oxid, stdobjref->oid, &proxy_manager))
2088 hr = proxy_manager_construct(apt, stdobjref->flags,
2089 stdobjref->oxid, stdobjref->oid, oxid_info,
2090 &proxy_manager);
2092 else
2093 TRACE("proxy manager already created, using\n");
2095 if (hr == S_OK)
2097 struct ifproxy * ifproxy;
2099 proxy_manager_set_context(proxy_manager, dest_context, dest_context_data);
2101 hr = proxy_manager_find_ifproxy(proxy_manager, riid, &ifproxy);
2102 if (hr == E_NOINTERFACE)
2104 IRpcChannelBuffer *chanbuf;
2105 hr = rpc_create_clientchannel(&stdobjref->oxid, &stdobjref->ipid,
2106 &proxy_manager->oxid_info, riid, proxy_manager->dest_context,
2107 proxy_manager->dest_context_data, &chanbuf, apt);
2108 if (hr == S_OK)
2109 hr = proxy_manager_create_ifproxy(proxy_manager, stdobjref, riid, chanbuf, &ifproxy);
2111 else
2112 IUnknown_AddRef((IUnknown *)ifproxy->iface);
2114 if (hr == S_OK)
2116 InterlockedExchangeAdd((LONG *)&ifproxy->refs, stdobjref->cPublicRefs);
2117 /* get at least one external reference to the object to keep it alive */
2118 hr = ifproxy_get_public_ref(ifproxy);
2119 if (FAILED(hr))
2120 ifproxy_destroy(ifproxy);
2123 if (hr == S_OK)
2124 *object = ifproxy->iface;
2127 /* release our reference to the proxy manager - the client/apartment
2128 * will hold on to the remaining reference for us */
2129 if (proxy_manager) IMultiQI_Release(&proxy_manager->IMultiQI_iface);
2131 return hr;
2134 static HRESULT WINAPI StdMarshalImpl_UnmarshalInterface(IMarshal *iface, IStream *stream, REFIID riid, void **ppv)
2136 struct stdmarshal *marshal = impl_from_StdMarshal(iface);
2137 OBJREF objref;
2138 HRESULT hr;
2139 ULONG res;
2141 hr = IStream_Read(stream, &objref, FIELD_OFFSET(OBJREF, u_objref), &res);
2142 if (hr != S_OK || (res != FIELD_OFFSET(OBJREF, u_objref)))
2144 ERR("Failed to read common OBJREF header, %#lx\n", hr);
2145 return STG_E_READFAULT;
2148 if (objref.signature != OBJREF_SIGNATURE)
2150 ERR("Bad OBJREF signature %#lx\n", objref.signature);
2151 return RPC_E_INVALID_OBJREF;
2154 if (!(objref.flags & OBJREF_STANDARD))
2156 FIXME("unsupported objref.flags = %lx\n", objref.flags);
2157 return E_NOTIMPL;
2160 return std_unmarshal_interface(marshal->dest_context, marshal->dest_context_data, stream, riid, ppv);
2163 static HRESULT WINAPI StdMarshalImpl_ReleaseMarshalData(IMarshal *iface, IStream *stream)
2165 OBJREF objref;
2166 HRESULT hr;
2167 ULONG res;
2169 TRACE("%p, %p\n", iface, stream);
2171 hr = IStream_Read(stream, &objref, FIELD_OFFSET(OBJREF, u_objref), &res);
2172 if (hr != S_OK || (res != FIELD_OFFSET(OBJREF, u_objref)))
2174 ERR("Failed to read common OBJREF header, %#lx\n", hr);
2175 return STG_E_READFAULT;
2178 if (objref.signature != OBJREF_SIGNATURE)
2180 ERR("Bad OBJREF signature %#lx\n", objref.signature);
2181 return RPC_E_INVALID_OBJREF;
2184 if (!(objref.flags & OBJREF_STANDARD))
2186 FIXME("unsupported objref.flags = %lx\n", objref.flags);
2187 return E_NOTIMPL;
2190 return std_release_marshal_data(stream);
2193 static HRESULT WINAPI StdMarshalImpl_DisconnectObject(IMarshal *iface, DWORD reserved)
2195 FIXME("(), stub!\n");
2196 return S_OK;
2199 static const IMarshalVtbl StdMarshalVtbl =
2201 StdMarshalImpl_QueryInterface,
2202 StdMarshalImpl_AddRef,
2203 StdMarshalImpl_Release,
2204 StdMarshalImpl_GetUnmarshalClass,
2205 StdMarshalImpl_GetMarshalSizeMax,
2206 StdMarshalImpl_MarshalInterface,
2207 StdMarshalImpl_UnmarshalInterface,
2208 StdMarshalImpl_ReleaseMarshalData,
2209 StdMarshalImpl_DisconnectObject
2212 static HRESULT StdMarshalImpl_Construct(REFIID riid, DWORD dest_context, void *dest_context_data, void **ppvObject)
2214 struct stdmarshal *object;
2215 HRESULT hr;
2217 object = malloc(sizeof(*object));
2218 if (!object)
2219 return E_OUTOFMEMORY;
2221 object->IMarshal_iface.lpVtbl = &StdMarshalVtbl;
2222 object->refcount = 1;
2223 object->dest_context = dest_context;
2224 object->dest_context_data = dest_context_data;
2226 hr = IMarshal_QueryInterface(&object->IMarshal_iface, riid, ppvObject);
2227 IMarshal_Release(&object->IMarshal_iface);
2229 return hr;
2232 HRESULT WINAPI InternalCoStdMarshalObject(REFIID riid, DWORD dest_context, void *dest_context_data, void **ppvObject)
2234 return StdMarshalImpl_Construct(riid, dest_context, dest_context_data, ppvObject);
2237 /***********************************************************************
2238 * CoGetStandardMarshal (combase.@)
2240 HRESULT WINAPI CoGetStandardMarshal(REFIID riid, IUnknown *pUnk, DWORD dwDestContext,
2241 void *dest_context, DWORD flags, IMarshal **marshal)
2243 if (pUnk == NULL)
2245 FIXME("%s, NULL, %lx, %p, %lx, %p, unimplemented yet.\n", debugstr_guid(riid), dwDestContext,
2246 dest_context, flags, marshal);
2247 return E_NOTIMPL;
2249 TRACE("%s, %p, %lx, %p, %lx, %p\n", debugstr_guid(riid), pUnk, dwDestContext, dest_context, flags, marshal);
2251 return StdMarshalImpl_Construct(&IID_IMarshal, dwDestContext, dest_context, (void **)marshal);