wined3d: Use a separate STATE_TRANSFORM(WINED3D_TS_VIEW) state handler in the GLSL...
[wine/multimedia.git] / dlls / ole32 / marshal.c
blobc5c2023088c7ce1388cbd31a0ffe4767ebf22f60
1 /*
2 * Marshalling library
4 * Copyright 2002 Marcus Meissner
5 * Copyright 2004 Mike Hearn, for CodeWeavers
6 * Copyright 2004 Rob Shearman, for CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include <stdarg.h>
24 #include <string.h>
25 #include <assert.h>
27 #define COBJMACROS
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winuser.h"
32 #include "objbase.h"
33 #include "ole2.h"
34 #include "winerror.h"
35 #include "wine/unicode.h"
37 #include "compobj_private.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(ole);
43 extern const CLSID CLSID_DfMarshal;
45 /* number of refs given out for normal marshaling */
46 #define NORMALEXTREFS 5
49 /* private flag indicating that the object was marshaled as table-weak */
50 #define SORFP_TABLEWEAK SORF_OXRES1
51 /* private flag indicating that the caller does not want to notify the stub
52 * when the proxy disconnects or is destroyed */
53 #define SORFP_NOLIFETIMEMGMT SORF_OXRES2
55 /* imported object / proxy manager */
56 struct proxy_manager
58 IMultiQI IMultiQI_iface;
59 IMarshal IMarshal_iface;
60 IClientSecurity IClientSecurity_iface;
61 struct apartment *parent; /* owning apartment (RO) */
62 struct list entry; /* entry in apartment (CS parent->cs) */
63 OXID oxid; /* object exported ID (RO) */
64 OXID_INFO oxid_info; /* string binding, ipid of rem unknown and other information (RO) */
65 OID oid; /* object ID (RO) */
66 struct list interfaces; /* imported interfaces (CS cs) */
67 LONG refs; /* proxy reference count (LOCK) */
68 CRITICAL_SECTION cs; /* thread safety for this object and children */
69 ULONG sorflags; /* STDOBJREF flags (RO) */
70 IRemUnknown *remunk; /* proxy to IRemUnknown used for lifecycle management (CS cs) */
71 HANDLE remoting_mutex; /* mutex used for synchronizing access to IRemUnknown */
72 MSHCTX dest_context; /* context used for activating optimisations (LOCK) */
73 void *dest_context_data; /* reserved context value (LOCK) */
76 static inline struct proxy_manager *impl_from_IMarshal( IMarshal *iface )
78 return CONTAINING_RECORD(iface, struct proxy_manager, IMarshal_iface);
81 static inline struct proxy_manager *impl_from_IClientSecurity( IClientSecurity *iface )
83 return CONTAINING_RECORD(iface, struct proxy_manager, IClientSecurity_iface);
86 static HRESULT unmarshal_object(const STDOBJREF *stdobjref, APARTMENT *apt,
87 MSHCTX dest_context, void *dest_context_data,
88 REFIID riid, const OXID_INFO *oxid_info,
89 void **object);
91 /* Marshalling just passes a unique identifier to the remote client,
92 * that makes it possible to find the passed interface again.
94 * So basically we need a set of values that make it unique.
96 * Note that the IUnknown_QI(ob,xiid,&ppv) always returns the SAME ppv value!
98 * A triple is used: OXID (apt id), OID (stub manager id),
99 * IPID (interface ptr/stub id).
101 * OXIDs identify an apartment and are network scoped
102 * OIDs identify a stub manager and are apartment scoped
103 * IPIDs identify an interface stub and are apartment scoped
106 static inline HRESULT get_facbuf_for_iid(REFIID riid, IPSFactoryBuffer **facbuf)
108 HRESULT hr;
109 CLSID clsid;
111 hr = CoGetPSClsid(riid, &clsid);
112 if (hr != S_OK)
113 return hr;
114 return CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER | WINE_CLSCTX_DONT_HOST,
115 NULL, &IID_IPSFactoryBuffer, (LPVOID*)facbuf);
118 /* marshals an object into a STDOBJREF structure */
119 HRESULT marshal_object(APARTMENT *apt, STDOBJREF *stdobjref, REFIID riid, IUnknown *object,
120 DWORD dest_context, void *dest_context_data, MSHLFLAGS mshlflags)
122 struct stub_manager *manager;
123 struct ifstub *ifstub;
124 BOOL tablemarshal;
125 IRpcStubBuffer *stub = NULL;
126 HRESULT hr;
127 IUnknown *iobject = NULL; /* object of type riid */
129 hr = apartment_getoxid(apt, &stdobjref->oxid);
130 if (hr != S_OK)
131 return hr;
133 hr = apartment_createwindowifneeded(apt);
134 if (hr != S_OK)
135 return hr;
137 hr = IUnknown_QueryInterface(object, riid, (void **)&iobject);
138 if (hr != S_OK)
140 ERR("object doesn't expose interface %s, failing with error 0x%08x\n",
141 debugstr_guid(riid), hr);
142 return E_NOINTERFACE;
145 /* IUnknown doesn't require a stub buffer, because it never goes out on
146 * the wire */
147 if (!IsEqualIID(riid, &IID_IUnknown))
149 IPSFactoryBuffer *psfb;
151 hr = get_facbuf_for_iid(riid, &psfb);
152 if (hr != S_OK)
154 ERR("couldn't get IPSFactory buffer for interface %s\n", debugstr_guid(riid));
155 IUnknown_Release(iobject);
156 return hr;
159 hr = IPSFactoryBuffer_CreateStub(psfb, riid, iobject, &stub);
160 IPSFactoryBuffer_Release(psfb);
161 if (hr != S_OK)
163 ERR("Failed to create an IRpcStubBuffer from IPSFactory for %s with error 0x%08x\n",
164 debugstr_guid(riid), hr);
165 IUnknown_Release(iobject);
166 return hr;
170 stdobjref->flags = SORF_NULL;
171 if (mshlflags & MSHLFLAGS_TABLEWEAK)
172 stdobjref->flags |= SORFP_TABLEWEAK;
173 if (mshlflags & MSHLFLAGS_NOPING)
174 stdobjref->flags |= SORF_NOPING;
176 if ((manager = get_stub_manager_from_object(apt, object)))
177 TRACE("registering new ifstub on pre-existing manager\n");
178 else
180 TRACE("constructing new stub manager\n");
182 manager = new_stub_manager(apt, object);
183 if (!manager)
185 if (stub) IRpcStubBuffer_Release(stub);
186 IUnknown_Release(iobject);
187 return E_OUTOFMEMORY;
190 stdobjref->oid = manager->oid;
192 tablemarshal = ((mshlflags & MSHLFLAGS_TABLESTRONG) || (mshlflags & MSHLFLAGS_TABLEWEAK));
194 /* make sure ifstub that we are creating is unique */
195 ifstub = stub_manager_find_ifstub(manager, riid, mshlflags);
196 if (!ifstub)
197 ifstub = stub_manager_new_ifstub(manager, stub, iobject, riid, dest_context, dest_context_data, mshlflags);
199 if (stub) IRpcStubBuffer_Release(stub);
200 IUnknown_Release(iobject);
202 if (!ifstub)
204 stub_manager_int_release(manager);
205 /* destroy the stub manager if it has no ifstubs by releasing
206 * zero external references */
207 stub_manager_ext_release(manager, 0, FALSE, TRUE);
208 return E_OUTOFMEMORY;
211 if (!tablemarshal)
213 stdobjref->cPublicRefs = NORMALEXTREFS;
214 stub_manager_ext_addref(manager, stdobjref->cPublicRefs, FALSE);
216 else
218 stdobjref->cPublicRefs = 0;
219 if (mshlflags & MSHLFLAGS_TABLESTRONG)
220 stub_manager_ext_addref(manager, 1, FALSE);
221 else
222 stub_manager_ext_addref(manager, 0, TRUE);
225 /* FIXME: check return value */
226 RPC_RegisterInterface(riid);
228 stdobjref->ipid = ifstub->ipid;
230 stub_manager_int_release(manager);
231 return S_OK;
236 /* Client-side identity of the server object */
238 static HRESULT proxy_manager_get_remunknown(struct proxy_manager * This, IRemUnknown **remunk);
239 static void proxy_manager_destroy(struct proxy_manager * This);
240 static HRESULT proxy_manager_find_ifproxy(struct proxy_manager * This, REFIID riid, struct ifproxy ** ifproxy_found);
241 static HRESULT proxy_manager_query_local_interface(struct proxy_manager * This, REFIID riid, void ** ppv);
243 static HRESULT WINAPI ClientIdentity_QueryInterface(IMultiQI * iface, REFIID riid, void ** ppv)
245 HRESULT hr;
246 MULTI_QI mqi;
248 TRACE("%s\n", debugstr_guid(riid));
250 mqi.pIID = riid;
251 hr = IMultiQI_QueryMultipleInterfaces(iface, 1, &mqi);
252 *ppv = mqi.pItf;
254 return hr;
257 static ULONG WINAPI ClientIdentity_AddRef(IMultiQI * iface)
259 struct proxy_manager * This = (struct proxy_manager *)iface;
260 TRACE("%p - before %d\n", iface, This->refs);
261 return InterlockedIncrement(&This->refs);
264 static ULONG WINAPI ClientIdentity_Release(IMultiQI * iface)
266 struct proxy_manager * This = (struct proxy_manager *)iface;
267 ULONG refs = InterlockedDecrement(&This->refs);
268 TRACE("%p - after %d\n", iface, refs);
269 if (!refs)
270 proxy_manager_destroy(This);
271 return refs;
274 static HRESULT WINAPI ClientIdentity_QueryMultipleInterfaces(IMultiQI *iface, ULONG cMQIs, MULTI_QI *pMQIs)
276 struct proxy_manager * This = (struct proxy_manager *)iface;
277 REMQIRESULT *qiresults = NULL;
278 ULONG nonlocal_mqis = 0;
279 ULONG i;
280 ULONG successful_mqis = 0;
281 IID *iids = HeapAlloc(GetProcessHeap(), 0, cMQIs * sizeof(*iids));
282 /* mapping of RemQueryInterface index to QueryMultipleInterfaces index */
283 ULONG *mapping = HeapAlloc(GetProcessHeap(), 0, cMQIs * sizeof(*mapping));
285 TRACE("cMQIs: %d\n", cMQIs);
287 /* try to get a local interface - this includes already active proxy
288 * interfaces and also interfaces exposed by the proxy manager */
289 for (i = 0; i < cMQIs; i++)
291 TRACE("iid[%d] = %s\n", i, debugstr_guid(pMQIs[i].pIID));
292 pMQIs[i].hr = proxy_manager_query_local_interface(This, pMQIs[i].pIID, (void **)&pMQIs[i].pItf);
293 if (pMQIs[i].hr == S_OK)
294 successful_mqis++;
295 else
297 iids[nonlocal_mqis] = *pMQIs[i].pIID;
298 mapping[nonlocal_mqis] = i;
299 nonlocal_mqis++;
303 TRACE("%d interfaces not found locally\n", nonlocal_mqis);
305 /* if we have more than one interface not found locally then we must try
306 * to query the remote object for it */
307 if (nonlocal_mqis != 0)
309 IRemUnknown *remunk;
310 HRESULT hr;
311 IPID *ipid;
313 /* get the ipid of the first entry */
314 /* FIXME: should we implement ClientIdentity on the ifproxies instead
315 * of the proxy_manager so we use the correct ipid here? */
316 ipid = &LIST_ENTRY(list_head(&This->interfaces), struct ifproxy, entry)->stdobjref.ipid;
318 /* get IRemUnknown proxy so we can communicate with the remote object */
319 hr = proxy_manager_get_remunknown(This, &remunk);
321 if (SUCCEEDED(hr))
323 hr = IRemUnknown_RemQueryInterface(remunk, ipid, NORMALEXTREFS,
324 nonlocal_mqis, iids, &qiresults);
325 IRemUnknown_Release(remunk);
326 if (FAILED(hr))
327 ERR("IRemUnknown_RemQueryInterface failed with error 0x%08x\n", hr);
330 /* IRemUnknown_RemQueryInterface can return S_FALSE if only some of
331 * the interfaces were returned */
332 if (SUCCEEDED(hr))
334 /* try to unmarshal each object returned to us */
335 for (i = 0; i < nonlocal_mqis; i++)
337 ULONG index = mapping[i];
338 HRESULT hrobj = qiresults[i].hResult;
339 if (hrobj == S_OK)
340 hrobj = unmarshal_object(&qiresults[i].std, COM_CurrentApt(),
341 This->dest_context,
342 This->dest_context_data,
343 pMQIs[index].pIID, &This->oxid_info,
344 (void **)&pMQIs[index].pItf);
346 if (hrobj == S_OK)
347 successful_mqis++;
348 else
349 ERR("Failed to get pointer to interface %s\n", debugstr_guid(pMQIs[index].pIID));
350 pMQIs[index].hr = hrobj;
354 /* free the memory allocated by the proxy */
355 CoTaskMemFree(qiresults);
358 TRACE("%d/%d successfully queried\n", successful_mqis, cMQIs);
360 HeapFree(GetProcessHeap(), 0, iids);
361 HeapFree(GetProcessHeap(), 0, mapping);
363 if (successful_mqis == cMQIs)
364 return S_OK; /* we got all requested interfaces */
365 else if (successful_mqis == 0)
366 return E_NOINTERFACE; /* we didn't get any interfaces */
367 else
368 return S_FALSE; /* we got some interfaces */
371 static const IMultiQIVtbl ClientIdentity_Vtbl =
373 ClientIdentity_QueryInterface,
374 ClientIdentity_AddRef,
375 ClientIdentity_Release,
376 ClientIdentity_QueryMultipleInterfaces
379 /* FIXME: remove these */
380 static HRESULT WINAPI StdMarshalImpl_GetUnmarshalClass(LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext, void* pvDestContext, DWORD mshlflags, CLSID* pCid);
381 static HRESULT WINAPI StdMarshalImpl_GetMarshalSizeMax(LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext, void* pvDestContext, DWORD mshlflags, DWORD* pSize);
382 static HRESULT WINAPI StdMarshalImpl_UnmarshalInterface(LPMARSHAL iface, IStream *pStm, REFIID riid, void **ppv);
383 static HRESULT WINAPI StdMarshalImpl_ReleaseMarshalData(LPMARSHAL iface, IStream *pStm);
384 static HRESULT WINAPI StdMarshalImpl_DisconnectObject(LPMARSHAL iface, DWORD dwReserved);
386 static HRESULT WINAPI Proxy_QueryInterface(IMarshal *iface, REFIID riid, void **ppvObject)
388 struct proxy_manager *This = impl_from_IMarshal( iface );
389 return IMultiQI_QueryInterface(&This->IMultiQI_iface, riid, ppvObject);
392 static ULONG WINAPI Proxy_AddRef(IMarshal *iface)
394 struct proxy_manager *This = impl_from_IMarshal( iface );
395 return IMultiQI_AddRef(&This->IMultiQI_iface);
398 static ULONG WINAPI Proxy_Release(IMarshal *iface)
400 struct proxy_manager *This = impl_from_IMarshal( iface );
401 return IMultiQI_Release(&This->IMultiQI_iface);
404 static HRESULT WINAPI Proxy_MarshalInterface(
405 LPMARSHAL iface, IStream *pStm, REFIID riid, void* pv, DWORD dwDestContext,
406 void* pvDestContext, DWORD mshlflags)
408 struct proxy_manager *This = impl_from_IMarshal( iface );
409 HRESULT hr;
410 struct ifproxy *ifproxy;
412 TRACE("(...,%s,...)\n", debugstr_guid(riid));
414 hr = proxy_manager_find_ifproxy(This, riid, &ifproxy);
415 if (SUCCEEDED(hr))
417 STDOBJREF stdobjref = ifproxy->stdobjref;
419 stdobjref.cPublicRefs = 0;
421 if ((mshlflags != MSHLFLAGS_TABLEWEAK) &&
422 (mshlflags != MSHLFLAGS_TABLESTRONG))
424 ULONG cPublicRefs = ifproxy->refs;
425 ULONG cPublicRefsOld;
426 /* optimization - share out proxy's public references if possible
427 * instead of making new proxy do a roundtrip through the server */
430 ULONG cPublicRefsNew;
431 cPublicRefsOld = cPublicRefs;
432 stdobjref.cPublicRefs = cPublicRefs / 2;
433 cPublicRefsNew = cPublicRefs - stdobjref.cPublicRefs;
434 cPublicRefs = InterlockedCompareExchange(
435 (LONG *)&ifproxy->refs, cPublicRefsNew, cPublicRefsOld);
436 } while (cPublicRefs != cPublicRefsOld);
439 /* normal and table-strong marshaling need at least one reference */
440 if (!stdobjref.cPublicRefs && (mshlflags != MSHLFLAGS_TABLEWEAK))
442 IRemUnknown *remunk;
443 hr = proxy_manager_get_remunknown(This, &remunk);
444 if (hr == S_OK)
446 HRESULT hrref = S_OK;
447 REMINTERFACEREF rif;
448 rif.ipid = ifproxy->stdobjref.ipid;
449 rif.cPublicRefs = (mshlflags == MSHLFLAGS_TABLESTRONG) ? 1 : NORMALEXTREFS;
450 rif.cPrivateRefs = 0;
451 hr = IRemUnknown_RemAddRef(remunk, 1, &rif, &hrref);
452 IRemUnknown_Release(remunk);
453 if (hr == S_OK && hrref == S_OK)
455 /* table-strong marshaling doesn't give the refs to the
456 * client that unmarshals the STDOBJREF */
457 if (mshlflags != MSHLFLAGS_TABLESTRONG)
458 stdobjref.cPublicRefs = rif.cPublicRefs;
460 else
461 ERR("IRemUnknown_RemAddRef returned with 0x%08x, hrref = 0x%08x\n", hr, hrref);
465 if (SUCCEEDED(hr))
467 TRACE("writing stdobjref: flags = %04x cPublicRefs = %d oxid = %s oid = %s ipid = %s\n",
468 stdobjref.flags, stdobjref.cPublicRefs,
469 wine_dbgstr_longlong(stdobjref.oxid),
470 wine_dbgstr_longlong(stdobjref.oid),
471 debugstr_guid(&stdobjref.ipid));
472 hr = IStream_Write(pStm, &stdobjref, sizeof(stdobjref), NULL);
475 else
477 /* we don't have the interface already unmarshaled so we have to
478 * request the object from the server */
479 IRemUnknown *remunk;
480 IPID *ipid;
481 REMQIRESULT *qiresults = NULL;
482 IID iid = *riid;
484 /* get the ipid of the first entry */
485 /* FIXME: should we implement ClientIdentity on the ifproxies instead
486 * of the proxy_manager so we use the correct ipid here? */
487 ipid = &LIST_ENTRY(list_head(&This->interfaces), struct ifproxy, entry)->stdobjref.ipid;
489 /* get IRemUnknown proxy so we can communicate with the remote object */
490 hr = proxy_manager_get_remunknown(This, &remunk);
492 if (hr == S_OK)
494 hr = IRemUnknown_RemQueryInterface(remunk, ipid, NORMALEXTREFS,
495 1, &iid, &qiresults);
496 if (SUCCEEDED(hr))
498 hr = IStream_Write(pStm, &qiresults->std, sizeof(qiresults->std), NULL);
499 if (FAILED(hr))
501 REMINTERFACEREF rif;
502 rif.ipid = qiresults->std.ipid;
503 rif.cPublicRefs = qiresults->std.cPublicRefs;
504 rif.cPrivateRefs = 0;
505 IRemUnknown_RemRelease(remunk, 1, &rif);
507 CoTaskMemFree(qiresults);
509 else
510 ERR("IRemUnknown_RemQueryInterface failed with error 0x%08x\n", hr);
511 IRemUnknown_Release(remunk);
515 return hr;
518 static const IMarshalVtbl ProxyMarshal_Vtbl =
520 Proxy_QueryInterface,
521 Proxy_AddRef,
522 Proxy_Release,
523 StdMarshalImpl_GetUnmarshalClass,
524 StdMarshalImpl_GetMarshalSizeMax,
525 Proxy_MarshalInterface,
526 StdMarshalImpl_UnmarshalInterface,
527 StdMarshalImpl_ReleaseMarshalData,
528 StdMarshalImpl_DisconnectObject
531 static HRESULT WINAPI ProxyCliSec_QueryInterface(IClientSecurity *iface, REFIID riid, void **ppvObject)
533 struct proxy_manager *This = impl_from_IClientSecurity( iface );
534 return IMultiQI_QueryInterface(&This->IMultiQI_iface, riid, ppvObject);
537 static ULONG WINAPI ProxyCliSec_AddRef(IClientSecurity *iface)
539 struct proxy_manager *This = impl_from_IClientSecurity( iface );
540 return IMultiQI_AddRef(&This->IMultiQI_iface);
543 static ULONG WINAPI ProxyCliSec_Release(IClientSecurity *iface)
545 struct proxy_manager *This = impl_from_IClientSecurity( iface );
546 return IMultiQI_Release(&This->IMultiQI_iface);
549 static HRESULT WINAPI ProxyCliSec_QueryBlanket(IClientSecurity *iface,
550 IUnknown *pProxy,
551 DWORD *pAuthnSvc,
552 DWORD *pAuthzSvc,
553 OLECHAR **ppServerPrincName,
554 DWORD *pAuthnLevel,
555 DWORD *pImpLevel,
556 void **pAuthInfo,
557 DWORD *pCapabilities)
559 FIXME("(%p, %p, %p, %p, %p, %p, %p, %p): stub\n", pProxy, pAuthnSvc,
560 pAuthzSvc, ppServerPrincName, pAuthnLevel, pImpLevel, pAuthInfo,
561 pCapabilities);
563 if (pAuthnSvc)
564 *pAuthnSvc = 0;
565 if (pAuthzSvc)
566 *pAuthzSvc = 0;
567 if (ppServerPrincName)
568 *ppServerPrincName = NULL;
569 if (pAuthnLevel)
570 *pAuthnLevel = RPC_C_AUTHN_LEVEL_DEFAULT;
571 if (pImpLevel)
572 *pImpLevel = RPC_C_IMP_LEVEL_DEFAULT;
573 if (pAuthInfo)
574 *pAuthInfo = NULL;
575 if (pCapabilities)
576 *pCapabilities = EOAC_NONE;
578 return E_NOTIMPL;
581 static HRESULT WINAPI ProxyCliSec_SetBlanket(IClientSecurity *iface,
582 IUnknown *pProxy, DWORD AuthnSvc,
583 DWORD AuthzSvc,
584 OLECHAR *pServerPrincName,
585 DWORD AuthnLevel, DWORD ImpLevel,
586 void *pAuthInfo,
587 DWORD Capabilities)
589 FIXME("(%p, %d, %d, %s, %d, %d, %p, 0x%x): stub\n", pProxy, AuthnSvc, AuthzSvc,
590 pServerPrincName == COLE_DEFAULT_PRINCIPAL ? "<default principal>" : debugstr_w(pServerPrincName),
591 AuthnLevel, ImpLevel, pAuthInfo, Capabilities);
592 return E_NOTIMPL;
595 static HRESULT WINAPI ProxyCliSec_CopyProxy(IClientSecurity *iface,
596 IUnknown *pProxy, IUnknown **ppCopy)
598 FIXME("(%p, %p): stub\n", pProxy, ppCopy);
599 *ppCopy = NULL;
600 return E_NOTIMPL;
603 static const IClientSecurityVtbl ProxyCliSec_Vtbl =
605 ProxyCliSec_QueryInterface,
606 ProxyCliSec_AddRef,
607 ProxyCliSec_Release,
608 ProxyCliSec_QueryBlanket,
609 ProxyCliSec_SetBlanket,
610 ProxyCliSec_CopyProxy
613 static HRESULT ifproxy_get_public_ref(struct ifproxy * This)
615 HRESULT hr = S_OK;
617 if (WAIT_OBJECT_0 != WaitForSingleObject(This->parent->remoting_mutex, INFINITE))
619 ERR("Wait failed for ifproxy %p\n", This);
620 return E_UNEXPECTED;
623 if (This->refs == 0)
625 IRemUnknown *remunk = NULL;
627 TRACE("getting public ref for ifproxy %p\n", This);
629 hr = proxy_manager_get_remunknown(This->parent, &remunk);
630 if (hr == S_OK)
632 HRESULT hrref = S_OK;
633 REMINTERFACEREF rif;
634 rif.ipid = This->stdobjref.ipid;
635 rif.cPublicRefs = NORMALEXTREFS;
636 rif.cPrivateRefs = 0;
637 hr = IRemUnknown_RemAddRef(remunk, 1, &rif, &hrref);
638 IRemUnknown_Release(remunk);
639 if (hr == S_OK && hrref == S_OK)
640 InterlockedExchangeAdd((LONG *)&This->refs, NORMALEXTREFS);
641 else
642 ERR("IRemUnknown_RemAddRef returned with 0x%08x, hrref = 0x%08x\n", hr, hrref);
645 ReleaseMutex(This->parent->remoting_mutex);
647 return hr;
650 static HRESULT ifproxy_release_public_refs(struct ifproxy * This)
652 HRESULT hr = S_OK;
653 LONG public_refs;
655 if (WAIT_OBJECT_0 != WaitForSingleObject(This->parent->remoting_mutex, INFINITE))
657 ERR("Wait failed for ifproxy %p\n", This);
658 return E_UNEXPECTED;
661 public_refs = This->refs;
662 if (public_refs > 0)
664 IRemUnknown *remunk = NULL;
666 TRACE("releasing %d refs\n", public_refs);
668 hr = proxy_manager_get_remunknown(This->parent, &remunk);
669 if (hr == S_OK)
671 REMINTERFACEREF rif;
672 rif.ipid = This->stdobjref.ipid;
673 rif.cPublicRefs = public_refs;
674 rif.cPrivateRefs = 0;
675 hr = IRemUnknown_RemRelease(remunk, 1, &rif);
676 IRemUnknown_Release(remunk);
677 if (hr == S_OK)
678 InterlockedExchangeAdd((LONG *)&This->refs, -public_refs);
679 else if (hr == RPC_E_DISCONNECTED)
680 WARN("couldn't release references because object was "
681 "disconnected: oxid = %s, oid = %s\n",
682 wine_dbgstr_longlong(This->parent->oxid),
683 wine_dbgstr_longlong(This->parent->oid));
684 else
685 ERR("IRemUnknown_RemRelease failed with error 0x%08x\n", hr);
688 ReleaseMutex(This->parent->remoting_mutex);
690 return hr;
693 /* should be called inside This->parent->cs critical section */
694 static void ifproxy_disconnect(struct ifproxy * This)
696 ifproxy_release_public_refs(This);
697 if (This->proxy) IRpcProxyBuffer_Disconnect(This->proxy);
699 IRpcChannelBuffer_Release(This->chan);
700 This->chan = NULL;
703 /* should be called in This->parent->cs critical section if it is an entry in parent's list */
704 static void ifproxy_destroy(struct ifproxy * This)
706 TRACE("%p\n", This);
708 /* release public references to this object so that the stub can know
709 * when to destroy itself */
710 ifproxy_release_public_refs(This);
712 list_remove(&This->entry);
714 if (This->chan)
716 IRpcChannelBuffer_Release(This->chan);
717 This->chan = NULL;
720 if (This->proxy) IRpcProxyBuffer_Release(This->proxy);
722 HeapFree(GetProcessHeap(), 0, This);
725 static HRESULT proxy_manager_construct(
726 APARTMENT * apt, ULONG sorflags, OXID oxid, OID oid,
727 const OXID_INFO *oxid_info, struct proxy_manager ** proxy_manager)
729 struct proxy_manager * This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
730 if (!This) return E_OUTOFMEMORY;
732 This->remoting_mutex = CreateMutexW(NULL, FALSE, NULL);
733 if (!This->remoting_mutex)
735 HeapFree(GetProcessHeap(), 0, This);
736 return HRESULT_FROM_WIN32(GetLastError());
739 if (oxid_info)
741 This->oxid_info.dwPid = oxid_info->dwPid;
742 This->oxid_info.dwTid = oxid_info->dwTid;
743 This->oxid_info.ipidRemUnknown = oxid_info->ipidRemUnknown;
744 This->oxid_info.dwAuthnHint = oxid_info->dwAuthnHint;
745 This->oxid_info.psa = NULL /* FIXME: copy from oxid_info */;
747 else
749 HRESULT hr = RPC_ResolveOxid(oxid, &This->oxid_info);
750 if (FAILED(hr))
752 CloseHandle(This->remoting_mutex);
753 HeapFree(GetProcessHeap(), 0, This);
754 return hr;
758 This->IMultiQI_iface.lpVtbl = &ClientIdentity_Vtbl;
759 This->IMarshal_iface.lpVtbl = &ProxyMarshal_Vtbl;
760 This->IClientSecurity_iface.lpVtbl = &ProxyCliSec_Vtbl;
762 list_init(&This->entry);
763 list_init(&This->interfaces);
765 InitializeCriticalSection(&This->cs);
766 DEBUG_SET_CRITSEC_NAME(&This->cs, "proxy_manager");
768 /* the apartment the object was unmarshaled into */
769 This->parent = apt;
771 /* the source apartment and id of the object */
772 This->oxid = oxid;
773 This->oid = oid;
775 This->refs = 1;
777 /* the DCOM draft specification states that the SORF_NOPING flag is
778 * proxy manager specific, not ifproxy specific, so this implies that we
779 * should store the STDOBJREF flags here in the proxy manager. */
780 This->sorflags = sorflags;
782 /* we create the IRemUnknown proxy on demand */
783 This->remunk = NULL;
785 /* initialise these values to the weakest values and they will be
786 * overwritten in proxy_manager_set_context */
787 This->dest_context = MSHCTX_INPROC;
788 This->dest_context_data = NULL;
790 EnterCriticalSection(&apt->cs);
791 /* FIXME: we are dependent on the ordering in here to make sure a proxy's
792 * IRemUnknown proxy doesn't get destroyed before the regual proxy does
793 * because we need the IRemUnknown proxy during the destruction of the
794 * regular proxy. Ideally, we should maintain a separate list for the
795 * IRemUnknown proxies that need late destruction */
796 list_add_tail(&apt->proxies, &This->entry);
797 LeaveCriticalSection(&apt->cs);
799 TRACE("%p created for OXID %s, OID %s\n", This,
800 wine_dbgstr_longlong(oxid), wine_dbgstr_longlong(oid));
802 *proxy_manager = This;
803 return S_OK;
806 static inline void proxy_manager_set_context(struct proxy_manager *This, MSHCTX dest_context, void *dest_context_data)
808 MSHCTX old_dest_context;
809 MSHCTX new_dest_context;
813 old_dest_context = This->dest_context;
814 new_dest_context = old_dest_context;
815 /* "stronger" values overwrite "weaker" values. stronger values are
816 * ones that disable more optimisations */
817 switch (old_dest_context)
819 case MSHCTX_INPROC:
820 new_dest_context = dest_context;
821 break;
822 case MSHCTX_CROSSCTX:
823 switch (dest_context)
825 case MSHCTX_INPROC:
826 break;
827 default:
828 new_dest_context = dest_context;
830 break;
831 case MSHCTX_LOCAL:
832 switch (dest_context)
834 case MSHCTX_INPROC:
835 case MSHCTX_CROSSCTX:
836 break;
837 default:
838 new_dest_context = dest_context;
840 break;
841 case MSHCTX_NOSHAREDMEM:
842 switch (dest_context)
844 case MSHCTX_DIFFERENTMACHINE:
845 new_dest_context = dest_context;
846 break;
847 default:
848 break;
850 break;
851 default:
852 break;
855 if (old_dest_context == new_dest_context) break;
857 new_dest_context = InterlockedCompareExchange((PLONG)&This->dest_context, new_dest_context, old_dest_context);
858 } while (new_dest_context != old_dest_context);
860 if (dest_context_data)
861 InterlockedExchangePointer(&This->dest_context_data, dest_context_data);
864 static HRESULT proxy_manager_query_local_interface(struct proxy_manager * This, REFIID riid, void ** ppv)
866 HRESULT hr;
867 struct ifproxy * ifproxy;
869 TRACE("%s\n", debugstr_guid(riid));
871 if (IsEqualIID(riid, &IID_IUnknown) ||
872 IsEqualIID(riid, &IID_IMultiQI))
874 *ppv = &This->IMultiQI_iface;
875 IMultiQI_AddRef(&This->IMultiQI_iface);
876 return S_OK;
878 if (IsEqualIID(riid, &IID_IMarshal))
880 *ppv = &This->IMarshal_iface;
881 IMarshal_AddRef(&This->IMarshal_iface);
882 return S_OK;
884 if (IsEqualIID(riid, &IID_IClientSecurity))
886 *ppv = &This->IClientSecurity_iface;
887 IClientSecurity_AddRef(&This->IClientSecurity_iface);
888 return S_OK;
891 hr = proxy_manager_find_ifproxy(This, riid, &ifproxy);
892 if (hr == S_OK)
894 *ppv = ifproxy->iface;
895 IUnknown_AddRef((IUnknown *)*ppv);
896 return S_OK;
899 *ppv = NULL;
900 return E_NOINTERFACE;
903 static HRESULT proxy_manager_create_ifproxy(
904 struct proxy_manager * This, const STDOBJREF *stdobjref, REFIID riid,
905 IRpcChannelBuffer * channel, struct ifproxy ** iif_out)
907 HRESULT hr;
908 IPSFactoryBuffer * psfb;
909 struct ifproxy * ifproxy = HeapAlloc(GetProcessHeap(), 0, sizeof(*ifproxy));
910 if (!ifproxy) return E_OUTOFMEMORY;
912 list_init(&ifproxy->entry);
914 ifproxy->parent = This;
915 ifproxy->stdobjref = *stdobjref;
916 ifproxy->iid = *riid;
917 ifproxy->refs = 0;
918 ifproxy->proxy = NULL;
920 assert(channel);
921 ifproxy->chan = channel; /* FIXME: we should take the binding strings and construct the channel in this function */
923 /* the IUnknown interface is special because it does not have a
924 * proxy associated with the ifproxy as we handle IUnknown ourselves */
925 if (IsEqualIID(riid, &IID_IUnknown))
927 ifproxy->iface = &This->IMultiQI_iface;
928 IMultiQI_AddRef(&This->IMultiQI_iface);
929 hr = S_OK;
931 else
933 hr = get_facbuf_for_iid(riid, &psfb);
934 if (hr == S_OK)
936 /* important note: the outer unknown is set to the proxy manager.
937 * This ensures the COM identity rules are not violated, by having a
938 * one-to-one mapping of objects on the proxy side to objects on the
939 * stub side, no matter which interface you view the object through */
940 hr = IPSFactoryBuffer_CreateProxy(psfb, (IUnknown*)&This->IMultiQI_iface, riid,
941 &ifproxy->proxy, &ifproxy->iface);
942 IPSFactoryBuffer_Release(psfb);
943 if (hr != S_OK)
944 ERR("Could not create proxy for interface %s, error 0x%08x\n",
945 debugstr_guid(riid), hr);
947 else
948 ERR("Could not get IPSFactoryBuffer for interface %s, error 0x%08x\n",
949 debugstr_guid(riid), hr);
951 if (hr == S_OK)
952 hr = IRpcProxyBuffer_Connect(ifproxy->proxy, ifproxy->chan);
955 if (hr == S_OK)
957 EnterCriticalSection(&This->cs);
958 list_add_tail(&This->interfaces, &ifproxy->entry);
959 LeaveCriticalSection(&This->cs);
961 *iif_out = ifproxy;
962 TRACE("ifproxy %p created for IPID %s, interface %s with %u public refs\n",
963 ifproxy, debugstr_guid(&stdobjref->ipid), debugstr_guid(riid), stdobjref->cPublicRefs);
965 else
966 ifproxy_destroy(ifproxy);
968 return hr;
971 static HRESULT proxy_manager_find_ifproxy(struct proxy_manager * This, REFIID riid, struct ifproxy ** ifproxy_found)
973 HRESULT hr = E_NOINTERFACE; /* assume not found */
974 struct list * cursor;
976 EnterCriticalSection(&This->cs);
977 LIST_FOR_EACH(cursor, &This->interfaces)
979 struct ifproxy * ifproxy = LIST_ENTRY(cursor, struct ifproxy, entry);
980 if (IsEqualIID(riid, &ifproxy->iid))
982 *ifproxy_found = ifproxy;
983 hr = S_OK;
984 break;
987 LeaveCriticalSection(&This->cs);
989 return hr;
992 static void proxy_manager_disconnect(struct proxy_manager * This)
994 struct list * cursor;
996 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This->oxid),
997 wine_dbgstr_longlong(This->oid));
999 EnterCriticalSection(&This->cs);
1001 /* SORFP_NOLIFTIMEMGMT proxies (for IRemUnknown) shouldn't be
1002 * disconnected - it won't do anything anyway, except cause
1003 * problems for other objects that depend on this proxy always
1004 * working */
1005 if (!(This->sorflags & SORFP_NOLIFETIMEMGMT))
1007 LIST_FOR_EACH(cursor, &This->interfaces)
1009 struct ifproxy * ifproxy = LIST_ENTRY(cursor, struct ifproxy, entry);
1010 ifproxy_disconnect(ifproxy);
1014 /* apartment is being destroyed so don't keep a pointer around to it */
1015 This->parent = NULL;
1017 LeaveCriticalSection(&This->cs);
1020 static HRESULT proxy_manager_get_remunknown(struct proxy_manager * This, IRemUnknown **remunk)
1022 HRESULT hr = S_OK;
1023 struct apartment *apt;
1024 BOOL called_in_original_apt;
1026 /* we don't want to try and unmarshal or use IRemUnknown if we don't want
1027 * lifetime management */
1028 if (This->sorflags & SORFP_NOLIFETIMEMGMT)
1029 return S_FALSE;
1031 apt = COM_CurrentApt();
1032 if (!apt)
1033 return CO_E_NOTINITIALIZED;
1035 called_in_original_apt = This->parent && (This->parent->oxid == apt->oxid);
1037 EnterCriticalSection(&This->cs);
1038 /* only return the cached object if called from the original apartment.
1039 * in future, we might want to make the IRemUnknown proxy callable from any
1040 * apartment to avoid these checks */
1041 if (This->remunk && called_in_original_apt)
1043 /* already created - return existing object */
1044 *remunk = This->remunk;
1045 IRemUnknown_AddRef(*remunk);
1047 else if (!This->parent)
1048 /* disconnected - we can't create IRemUnknown */
1049 hr = S_FALSE;
1050 else
1052 STDOBJREF stdobjref;
1053 /* Don't want IRemUnknown lifetime management as this is IRemUnknown!
1054 * We also don't care about whether or not the stub is still alive */
1055 stdobjref.flags = SORFP_NOLIFETIMEMGMT | SORF_NOPING;
1056 stdobjref.cPublicRefs = 1;
1057 /* oxid of destination object */
1058 stdobjref.oxid = This->oxid;
1059 /* FIXME: what should be used for the oid? The DCOM draft doesn't say */
1060 stdobjref.oid = (OID)-1;
1061 stdobjref.ipid = This->oxid_info.ipidRemUnknown;
1063 /* do the unmarshal */
1064 hr = unmarshal_object(&stdobjref, COM_CurrentApt(), This->dest_context,
1065 This->dest_context_data, &IID_IRemUnknown,
1066 &This->oxid_info, (void**)remunk);
1067 if (hr == S_OK && called_in_original_apt)
1069 This->remunk = *remunk;
1070 IRemUnknown_AddRef(This->remunk);
1073 LeaveCriticalSection(&This->cs);
1075 TRACE("got IRemUnknown* pointer %p, hr = 0x%08x\n", *remunk, hr);
1077 return hr;
1080 /* destroys a proxy manager, freeing the memory it used.
1081 * Note: this function should not be called from a list iteration in the
1082 * apartment, due to the fact that it removes itself from the apartment and
1083 * it could add a proxy to IRemUnknown into the apartment. */
1084 static void proxy_manager_destroy(struct proxy_manager * This)
1086 struct list * cursor;
1088 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This->oxid),
1089 wine_dbgstr_longlong(This->oid));
1091 if (This->parent)
1093 EnterCriticalSection(&This->parent->cs);
1095 /* remove ourself from the list of proxy objects in the apartment */
1096 LIST_FOR_EACH(cursor, &This->parent->proxies)
1098 if (cursor == &This->entry)
1100 list_remove(&This->entry);
1101 break;
1105 LeaveCriticalSection(&This->parent->cs);
1108 /* destroy all of the interface proxies */
1109 while ((cursor = list_head(&This->interfaces)))
1111 struct ifproxy * ifproxy = LIST_ENTRY(cursor, struct ifproxy, entry);
1112 ifproxy_destroy(ifproxy);
1115 if (This->remunk) IRemUnknown_Release(This->remunk);
1116 CoTaskMemFree(This->oxid_info.psa);
1118 DEBUG_CLEAR_CRITSEC_NAME(&This->cs);
1119 DeleteCriticalSection(&This->cs);
1121 CloseHandle(This->remoting_mutex);
1123 HeapFree(GetProcessHeap(), 0, This);
1126 /* finds the proxy manager corresponding to a given OXID and OID that has
1127 * been unmarshaled in the specified apartment. The caller must release the
1128 * reference to the proxy_manager when the object is no longer used. */
1129 static BOOL find_proxy_manager(APARTMENT * apt, OXID oxid, OID oid, struct proxy_manager ** proxy_found)
1131 BOOL found = FALSE;
1132 struct list * cursor;
1134 EnterCriticalSection(&apt->cs);
1135 LIST_FOR_EACH(cursor, &apt->proxies)
1137 struct proxy_manager * proxy = LIST_ENTRY(cursor, struct proxy_manager, entry);
1138 if ((oxid == proxy->oxid) && (oid == proxy->oid))
1140 /* be careful of a race with ClientIdentity_Release, which would
1141 * cause us to return a proxy which is in the process of being
1142 * destroyed */
1143 if (IMultiQI_AddRef(&proxy->IMultiQI_iface) != 0)
1145 *proxy_found = proxy;
1146 found = TRUE;
1147 break;
1151 LeaveCriticalSection(&apt->cs);
1152 return found;
1155 HRESULT apartment_disconnectproxies(struct apartment *apt)
1157 struct list * cursor;
1159 LIST_FOR_EACH(cursor, &apt->proxies)
1161 struct proxy_manager * proxy = LIST_ENTRY(cursor, struct proxy_manager, entry);
1162 proxy_manager_disconnect(proxy);
1165 return S_OK;
1168 /********************** StdMarshal implementation ****************************/
1169 typedef struct _StdMarshalImpl
1171 IMarshal IMarshal_iface;
1172 LONG ref;
1173 DWORD dest_context;
1174 void *dest_context_data;
1175 } StdMarshalImpl;
1177 static inline StdMarshalImpl *impl_from_StdMarshal(IMarshal *iface)
1179 return CONTAINING_RECORD(iface, StdMarshalImpl, IMarshal_iface);
1182 static HRESULT WINAPI
1183 StdMarshalImpl_QueryInterface(IMarshal *iface, REFIID riid, void **ppv)
1185 *ppv = NULL;
1186 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IMarshal, riid))
1188 *ppv = iface;
1189 IMarshal_AddRef(iface);
1190 return S_OK;
1192 FIXME("No interface for %s.\n", debugstr_guid(riid));
1193 return E_NOINTERFACE;
1196 static ULONG WINAPI
1197 StdMarshalImpl_AddRef(IMarshal *iface)
1199 StdMarshalImpl *This = impl_from_StdMarshal(iface);
1200 return InterlockedIncrement(&This->ref);
1203 static ULONG WINAPI
1204 StdMarshalImpl_Release(IMarshal *iface)
1206 StdMarshalImpl *This = impl_from_StdMarshal(iface);
1207 ULONG ref = InterlockedDecrement(&This->ref);
1209 if (!ref) HeapFree(GetProcessHeap(),0,This);
1210 return ref;
1213 static HRESULT WINAPI
1214 StdMarshalImpl_GetUnmarshalClass(
1215 IMarshal *iface, REFIID riid, void* pv, DWORD dwDestContext,
1216 void* pvDestContext, DWORD mshlflags, CLSID* pCid)
1218 *pCid = CLSID_DfMarshal;
1219 return S_OK;
1222 static HRESULT WINAPI
1223 StdMarshalImpl_GetMarshalSizeMax(
1224 IMarshal *iface, REFIID riid, void* pv, DWORD dwDestContext,
1225 void* pvDestContext, DWORD mshlflags, DWORD* pSize)
1227 *pSize = sizeof(STDOBJREF);
1228 return S_OK;
1231 static HRESULT WINAPI
1232 StdMarshalImpl_MarshalInterface(
1233 IMarshal *iface, IStream *pStm,REFIID riid, void* pv, DWORD dest_context,
1234 void* dest_context_data, DWORD mshlflags)
1236 STDOBJREF stdobjref;
1237 ULONG res;
1238 HRESULT hres;
1239 APARTMENT *apt = COM_CurrentApt();
1241 TRACE("(...,%s,...)\n", debugstr_guid(riid));
1243 if (!apt)
1245 ERR("Apartment not initialized\n");
1246 return CO_E_NOTINITIALIZED;
1249 /* make sure this apartment can be reached from other threads / processes */
1250 RPC_StartRemoting(apt);
1252 hres = marshal_object(apt, &stdobjref, riid, pv, dest_context, dest_context_data, mshlflags);
1253 if (hres != S_OK)
1255 ERR("Failed to create ifstub, hres=0x%x\n", hres);
1256 return hres;
1259 return IStream_Write(pStm, &stdobjref, sizeof(stdobjref), &res);
1262 /* helper for StdMarshalImpl_UnmarshalInterface - does the unmarshaling with
1263 * no questions asked about the rules surrounding same-apartment unmarshals
1264 * and table marshaling */
1265 static HRESULT unmarshal_object(const STDOBJREF *stdobjref, APARTMENT *apt,
1266 MSHCTX dest_context, void *dest_context_data,
1267 REFIID riid, const OXID_INFO *oxid_info,
1268 void **object)
1270 struct proxy_manager *proxy_manager = NULL;
1271 HRESULT hr = S_OK;
1273 assert(apt);
1275 TRACE("stdobjref: flags = %04x cPublicRefs = %d oxid = %s oid = %s ipid = %s\n",
1276 stdobjref->flags, stdobjref->cPublicRefs,
1277 wine_dbgstr_longlong(stdobjref->oxid),
1278 wine_dbgstr_longlong(stdobjref->oid),
1279 debugstr_guid(&stdobjref->ipid));
1281 /* create a new proxy manager if one doesn't already exist for the
1282 * object */
1283 if (!find_proxy_manager(apt, stdobjref->oxid, stdobjref->oid, &proxy_manager))
1285 hr = proxy_manager_construct(apt, stdobjref->flags,
1286 stdobjref->oxid, stdobjref->oid, oxid_info,
1287 &proxy_manager);
1289 else
1290 TRACE("proxy manager already created, using\n");
1292 if (hr == S_OK)
1294 struct ifproxy * ifproxy;
1296 proxy_manager_set_context(proxy_manager, dest_context, dest_context_data);
1298 hr = proxy_manager_find_ifproxy(proxy_manager, riid, &ifproxy);
1299 if (hr == E_NOINTERFACE)
1301 IRpcChannelBuffer *chanbuf;
1302 hr = RPC_CreateClientChannel(&stdobjref->oxid, &stdobjref->ipid,
1303 &proxy_manager->oxid_info,
1304 proxy_manager->dest_context,
1305 proxy_manager->dest_context_data,
1306 &chanbuf);
1307 if (hr == S_OK)
1308 hr = proxy_manager_create_ifproxy(proxy_manager, stdobjref,
1309 riid, chanbuf, &ifproxy);
1311 else
1312 IUnknown_AddRef((IUnknown *)ifproxy->iface);
1314 if (hr == S_OK)
1316 InterlockedExchangeAdd((LONG *)&ifproxy->refs, stdobjref->cPublicRefs);
1317 /* get at least one external reference to the object to keep it alive */
1318 hr = ifproxy_get_public_ref(ifproxy);
1319 if (FAILED(hr))
1320 ifproxy_destroy(ifproxy);
1323 if (hr == S_OK)
1324 *object = ifproxy->iface;
1327 /* release our reference to the proxy manager - the client/apartment
1328 * will hold on to the remaining reference for us */
1329 if (proxy_manager) IMultiQI_Release(&proxy_manager->IMultiQI_iface);
1331 return hr;
1334 static HRESULT WINAPI
1335 StdMarshalImpl_UnmarshalInterface(IMarshal *iface, IStream *pStm, REFIID riid, void **ppv)
1337 StdMarshalImpl *This = impl_from_StdMarshal(iface);
1338 struct stub_manager *stubmgr = NULL;
1339 STDOBJREF stdobjref;
1340 ULONG res;
1341 HRESULT hres;
1342 APARTMENT *apt = COM_CurrentApt();
1343 APARTMENT *stub_apt;
1344 OXID oxid;
1346 TRACE("(...,%s,....)\n", debugstr_guid(riid));
1348 /* we need an apartment to unmarshal into */
1349 if (!apt)
1351 ERR("Apartment not initialized\n");
1352 return CO_E_NOTINITIALIZED;
1355 /* read STDOBJREF from wire */
1356 hres = IStream_Read(pStm, &stdobjref, sizeof(stdobjref), &res);
1357 if (hres != S_OK) return STG_E_READFAULT;
1359 hres = apartment_getoxid(apt, &oxid);
1360 if (hres != S_OK) return hres;
1362 /* check if we're marshalling back to ourselves */
1363 if ((oxid == stdobjref.oxid) && (stubmgr = get_stub_manager(apt, stdobjref.oid)))
1365 TRACE("Unmarshalling object marshalled in same apartment for iid %s, "
1366 "returning original object %p\n", debugstr_guid(riid), stubmgr->object);
1368 hres = IUnknown_QueryInterface(stubmgr->object, riid, ppv);
1370 /* unref the ifstub. FIXME: only do this on success? */
1371 if (!stub_manager_is_table_marshaled(stubmgr, &stdobjref.ipid))
1372 stub_manager_ext_release(stubmgr, stdobjref.cPublicRefs, stdobjref.flags & SORFP_TABLEWEAK, FALSE);
1374 stub_manager_int_release(stubmgr);
1375 return hres;
1378 /* notify stub manager about unmarshal if process-local object.
1379 * note: if the oxid is not found then we and native will quite happily
1380 * ignore table marshaling and normal marshaling rules regarding number of
1381 * unmarshals, etc, but if you abuse these rules then your proxy could end
1382 * up returning RPC_E_DISCONNECTED. */
1383 if ((stub_apt = apartment_findfromoxid(stdobjref.oxid, TRUE)))
1385 if ((stubmgr = get_stub_manager(stub_apt, stdobjref.oid)))
1387 if (!stub_manager_notify_unmarshal(stubmgr, &stdobjref.ipid))
1388 hres = CO_E_OBJNOTCONNECTED;
1390 else
1392 WARN("Couldn't find object for OXID %s, OID %s, assuming disconnected\n",
1393 wine_dbgstr_longlong(stdobjref.oxid),
1394 wine_dbgstr_longlong(stdobjref.oid));
1395 hres = CO_E_OBJNOTCONNECTED;
1398 else
1399 TRACE("Treating unmarshal from OXID %s as inter-process\n",
1400 wine_dbgstr_longlong(stdobjref.oxid));
1402 if (hres == S_OK)
1403 hres = unmarshal_object(&stdobjref, apt, This->dest_context,
1404 This->dest_context_data, riid,
1405 stubmgr ? &stubmgr->oxid_info : NULL, ppv);
1407 if (stubmgr) stub_manager_int_release(stubmgr);
1408 if (stub_apt) apartment_release(stub_apt);
1410 if (hres != S_OK) WARN("Failed with error 0x%08x\n", hres);
1411 else TRACE("Successfully created proxy %p\n", *ppv);
1413 return hres;
1416 static HRESULT WINAPI
1417 StdMarshalImpl_ReleaseMarshalData(IMarshal *iface, IStream *pStm)
1419 STDOBJREF stdobjref;
1420 ULONG res;
1421 HRESULT hres;
1422 struct stub_manager *stubmgr;
1423 APARTMENT *apt;
1425 TRACE("iface=%p, pStm=%p\n", iface, pStm);
1427 hres = IStream_Read(pStm, &stdobjref, sizeof(stdobjref), &res);
1428 if (hres != S_OK) return STG_E_READFAULT;
1430 TRACE("oxid = %s, oid = %s, ipid = %s\n",
1431 wine_dbgstr_longlong(stdobjref.oxid),
1432 wine_dbgstr_longlong(stdobjref.oid),
1433 wine_dbgstr_guid(&stdobjref.ipid));
1435 if (!(apt = apartment_findfromoxid(stdobjref.oxid, TRUE)))
1437 WARN("Could not map OXID %s to apartment object\n",
1438 wine_dbgstr_longlong(stdobjref.oxid));
1439 return RPC_E_INVALID_OBJREF;
1442 if (!(stubmgr = get_stub_manager(apt, stdobjref.oid)))
1444 apartment_release(apt);
1445 ERR("could not map object ID to stub manager, oxid=%s, oid=%s\n",
1446 wine_dbgstr_longlong(stdobjref.oxid), wine_dbgstr_longlong(stdobjref.oid));
1447 return RPC_E_INVALID_OBJREF;
1450 stub_manager_release_marshal_data(stubmgr, stdobjref.cPublicRefs, &stdobjref.ipid, stdobjref.flags & SORFP_TABLEWEAK);
1452 stub_manager_int_release(stubmgr);
1453 apartment_release(apt);
1455 return S_OK;
1458 static HRESULT WINAPI
1459 StdMarshalImpl_DisconnectObject(IMarshal *iface, DWORD dwReserved)
1461 FIXME("(), stub!\n");
1462 return S_OK;
1465 static const IMarshalVtbl StdMarshalVtbl =
1467 StdMarshalImpl_QueryInterface,
1468 StdMarshalImpl_AddRef,
1469 StdMarshalImpl_Release,
1470 StdMarshalImpl_GetUnmarshalClass,
1471 StdMarshalImpl_GetMarshalSizeMax,
1472 StdMarshalImpl_MarshalInterface,
1473 StdMarshalImpl_UnmarshalInterface,
1474 StdMarshalImpl_ReleaseMarshalData,
1475 StdMarshalImpl_DisconnectObject
1478 static HRESULT StdMarshalImpl_Construct(REFIID riid, DWORD dest_context, void *dest_context_data, void** ppvObject)
1480 HRESULT hr;
1482 StdMarshalImpl *pStdMarshal = HeapAlloc(GetProcessHeap(), 0, sizeof(StdMarshalImpl));
1483 if (!pStdMarshal)
1484 return E_OUTOFMEMORY;
1486 pStdMarshal->IMarshal_iface.lpVtbl = &StdMarshalVtbl;
1487 pStdMarshal->ref = 0;
1488 pStdMarshal->dest_context = dest_context;
1489 pStdMarshal->dest_context_data = dest_context_data;
1491 hr = IMarshal_QueryInterface(&pStdMarshal->IMarshal_iface, riid, ppvObject);
1492 if (FAILED(hr))
1493 HeapFree(GetProcessHeap(), 0, pStdMarshal);
1495 return hr;
1498 /***********************************************************************
1499 * CoGetStandardMarshal [OLE32.@]
1501 * Gets or creates a standard marshal object.
1503 * PARAMS
1504 * riid [I] Interface identifier of the pUnk object.
1505 * pUnk [I] Optional. Object to get the marshal object for.
1506 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1507 * pvDestContext [I] Reserved. Must be NULL.
1508 * mshlflags [I] Flags affecting the marshaling process.
1509 * ppMarshal [O] Address where marshal object will be stored.
1511 * RETURNS
1512 * Success: S_OK.
1513 * Failure: HRESULT code.
1515 * NOTES
1517 * The function retrieves the IMarshal object associated with an object if
1518 * that object is currently an active stub, otherwise a new marshal object is
1519 * created.
1521 HRESULT WINAPI CoGetStandardMarshal(REFIID riid, IUnknown *pUnk,
1522 DWORD dwDestContext, LPVOID pvDestContext,
1523 DWORD mshlflags, LPMARSHAL *ppMarshal)
1525 if (pUnk == NULL)
1527 FIXME("(%s,NULL,%x,%p,%x,%p), unimplemented yet.\n",
1528 debugstr_guid(riid),dwDestContext,pvDestContext,mshlflags,ppMarshal);
1529 return E_NOTIMPL;
1531 TRACE("(%s,%p,%x,%p,%x,%p)\n",
1532 debugstr_guid(riid),pUnk,dwDestContext,pvDestContext,mshlflags,ppMarshal);
1534 return StdMarshalImpl_Construct(&IID_IMarshal, dwDestContext, pvDestContext, (void**)ppMarshal);
1537 /***********************************************************************
1538 * get_marshaler [internal]
1540 * Retrieves an IMarshal interface for an object.
1542 static HRESULT get_marshaler(REFIID riid, IUnknown *pUnk, DWORD dwDestContext,
1543 void *pvDestContext, DWORD mshlFlags,
1544 LPMARSHAL *pMarshal)
1546 HRESULT hr;
1548 if (!pUnk)
1549 return E_POINTER;
1550 hr = IUnknown_QueryInterface(pUnk, &IID_IMarshal, (LPVOID*)pMarshal);
1551 if (hr != S_OK)
1552 hr = CoGetStandardMarshal(riid, pUnk, dwDestContext, pvDestContext,
1553 mshlFlags, pMarshal);
1554 return hr;
1557 /***********************************************************************
1558 * get_unmarshaler_from_stream [internal]
1560 * Creates an IMarshal* object according to the data marshaled to the stream.
1561 * The function leaves the stream pointer at the start of the data written
1562 * to the stream by the IMarshal* object.
1564 static HRESULT get_unmarshaler_from_stream(IStream *stream, IMarshal **marshal, IID *iid)
1566 HRESULT hr;
1567 ULONG res;
1568 OBJREF objref;
1570 /* read common OBJREF header */
1571 hr = IStream_Read(stream, &objref, FIELD_OFFSET(OBJREF, u_objref), &res);
1572 if (hr != S_OK || (res != FIELD_OFFSET(OBJREF, u_objref)))
1574 ERR("Failed to read common OBJREF header, 0x%08x\n", hr);
1575 return STG_E_READFAULT;
1578 /* sanity check on header */
1579 if (objref.signature != OBJREF_SIGNATURE)
1581 ERR("Bad OBJREF signature 0x%08x\n", objref.signature);
1582 return RPC_E_INVALID_OBJREF;
1585 if (iid) *iid = objref.iid;
1587 /* FIXME: handler marshaling */
1588 if (objref.flags & OBJREF_STANDARD)
1590 TRACE("Using standard unmarshaling\n");
1591 hr = StdMarshalImpl_Construct(&IID_IMarshal, 0, NULL, (LPVOID*)marshal);
1593 else if (objref.flags & OBJREF_CUSTOM)
1595 ULONG custom_header_size = FIELD_OFFSET(OBJREF, u_objref.u_custom.pData) -
1596 FIELD_OFFSET(OBJREF, u_objref.u_custom);
1597 TRACE("Using custom unmarshaling\n");
1598 /* read constant sized OR_CUSTOM data from stream */
1599 hr = IStream_Read(stream, &objref.u_objref.u_custom,
1600 custom_header_size, &res);
1601 if (hr != S_OK || (res != custom_header_size))
1603 ERR("Failed to read OR_CUSTOM header, 0x%08x\n", hr);
1604 return STG_E_READFAULT;
1606 /* now create the marshaler specified in the stream */
1607 hr = CoCreateInstance(&objref.u_objref.u_custom.clsid, NULL,
1608 CLSCTX_INPROC_SERVER, &IID_IMarshal,
1609 (LPVOID*)marshal);
1611 else
1613 FIXME("Invalid or unimplemented marshaling type specified: %x\n",
1614 objref.flags);
1615 return RPC_E_INVALID_OBJREF;
1618 if (hr != S_OK)
1619 ERR("Failed to create marshal, 0x%08x\n", hr);
1621 return hr;
1624 /***********************************************************************
1625 * CoGetMarshalSizeMax [OLE32.@]
1627 * Gets the maximum amount of data that will be needed by a marshal.
1629 * PARAMS
1630 * pulSize [O] Address where maximum marshal size will be stored.
1631 * riid [I] Identifier of the interface to marshal.
1632 * pUnk [I] Pointer to the object to marshal.
1633 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1634 * pvDestContext [I] Reserved. Must be NULL.
1635 * mshlFlags [I] Flags that affect the marshaling. See CoMarshalInterface().
1637 * RETURNS
1638 * Success: S_OK.
1639 * Failure: HRESULT code.
1641 * SEE ALSO
1642 * CoMarshalInterface().
1644 HRESULT WINAPI CoGetMarshalSizeMax(ULONG *pulSize, REFIID riid, IUnknown *pUnk,
1645 DWORD dwDestContext, void *pvDestContext,
1646 DWORD mshlFlags)
1648 HRESULT hr;
1649 LPMARSHAL pMarshal;
1650 CLSID marshaler_clsid;
1652 hr = get_marshaler(riid, pUnk, dwDestContext, pvDestContext, mshlFlags, &pMarshal);
1653 if (hr != S_OK)
1654 return hr;
1656 hr = IMarshal_GetUnmarshalClass(pMarshal, riid, pUnk, dwDestContext,
1657 pvDestContext, mshlFlags, &marshaler_clsid);
1658 if (hr != S_OK)
1660 ERR("IMarshal::GetUnmarshalClass failed, 0x%08x\n", hr);
1661 IMarshal_Release(pMarshal);
1662 return hr;
1665 hr = IMarshal_GetMarshalSizeMax(pMarshal, riid, pUnk, dwDestContext,
1666 pvDestContext, mshlFlags, pulSize);
1667 if (IsEqualCLSID(&marshaler_clsid, &CLSID_DfMarshal))
1668 /* add on the size of the common header */
1669 *pulSize += FIELD_OFFSET(OBJREF, u_objref);
1670 else
1671 /* custom marshaling: add on the size of the whole OBJREF structure
1672 * like native does */
1673 *pulSize += sizeof(OBJREF);
1675 IMarshal_Release(pMarshal);
1676 return hr;
1680 static void dump_MSHLFLAGS(MSHLFLAGS flags)
1682 if (flags & MSHLFLAGS_TABLESTRONG)
1683 TRACE(" MSHLFLAGS_TABLESTRONG");
1684 if (flags & MSHLFLAGS_TABLEWEAK)
1685 TRACE(" MSHLFLAGS_TABLEWEAK");
1686 if (!(flags & (MSHLFLAGS_TABLESTRONG|MSHLFLAGS_TABLEWEAK)))
1687 TRACE(" MSHLFLAGS_NORMAL");
1688 if (flags & MSHLFLAGS_NOPING)
1689 TRACE(" MSHLFLAGS_NOPING");
1692 /***********************************************************************
1693 * CoMarshalInterface [OLE32.@]
1695 * Marshals an interface into a stream so that the object can then be
1696 * unmarshaled from another COM apartment and used remotely.
1698 * PARAMS
1699 * pStream [I] Stream the object will be marshaled into.
1700 * riid [I] Identifier of the interface to marshal.
1701 * pUnk [I] Pointer to the object to marshal.
1702 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1703 * pvDestContext [I] Reserved. Must be NULL.
1704 * mshlFlags [I] Flags that affect the marshaling. See notes.
1706 * RETURNS
1707 * Success: S_OK.
1708 * Failure: HRESULT code.
1710 * NOTES
1712 * The mshlFlags parameter can take one or more of the following flags:
1713 *| MSHLFLAGS_NORMAL - Unmarshal once, releases stub on last proxy release.
1714 *| MSHLFLAGS_TABLESTRONG - Unmarshal many, release when CoReleaseMarshalData() called.
1715 *| MSHLFLAGS_TABLEWEAK - Unmarshal many, releases stub on last proxy release.
1716 *| MSHLFLAGS_NOPING - No automatic garbage collection (and so reduces network traffic).
1718 * If a marshaled object is not unmarshaled, then CoReleaseMarshalData() must
1719 * be called in order to release the resources used in the marshaling.
1721 * SEE ALSO
1722 * CoUnmarshalInterface(), CoReleaseMarshalData().
1724 HRESULT WINAPI CoMarshalInterface(IStream *pStream, REFIID riid, IUnknown *pUnk,
1725 DWORD dwDestContext, void *pvDestContext,
1726 DWORD mshlFlags)
1728 HRESULT hr;
1729 CLSID marshaler_clsid;
1730 OBJREF objref;
1731 LPMARSHAL pMarshal;
1733 TRACE("(%p, %s, %p, %x, %p,", pStream, debugstr_guid(riid), pUnk,
1734 dwDestContext, pvDestContext);
1735 dump_MSHLFLAGS(mshlFlags);
1736 TRACE(")\n");
1738 if (!pUnk || !pStream)
1739 return E_INVALIDARG;
1741 objref.signature = OBJREF_SIGNATURE;
1742 objref.iid = *riid;
1744 /* get the marshaler for the specified interface */
1745 hr = get_marshaler(riid, pUnk, dwDestContext, pvDestContext, mshlFlags, &pMarshal);
1746 if (hr != S_OK)
1748 ERR("Failed to get marshaller, 0x%08x\n", hr);
1749 return hr;
1752 hr = IMarshal_GetUnmarshalClass(pMarshal, riid, pUnk, dwDestContext,
1753 pvDestContext, mshlFlags, &marshaler_clsid);
1754 if (hr != S_OK)
1756 ERR("IMarshal::GetUnmarshalClass failed, 0x%08x\n", hr);
1757 goto cleanup;
1760 /* FIXME: implement handler marshaling too */
1761 if (IsEqualCLSID(&marshaler_clsid, &CLSID_DfMarshal))
1763 TRACE("Using standard marshaling\n");
1764 objref.flags = OBJREF_STANDARD;
1766 /* write the common OBJREF header to the stream */
1767 hr = IStream_Write(pStream, &objref, FIELD_OFFSET(OBJREF, u_objref), NULL);
1768 if (hr != S_OK)
1770 ERR("Failed to write OBJREF header to stream, 0x%08x\n", hr);
1771 goto cleanup;
1774 else
1776 TRACE("Using custom marshaling\n");
1777 objref.flags = OBJREF_CUSTOM;
1778 objref.u_objref.u_custom.clsid = marshaler_clsid;
1779 objref.u_objref.u_custom.cbExtension = 0;
1780 objref.u_objref.u_custom.size = 0;
1781 hr = IMarshal_GetMarshalSizeMax(pMarshal, riid, pUnk, dwDestContext,
1782 pvDestContext, mshlFlags,
1783 &objref.u_objref.u_custom.size);
1784 if (hr != S_OK)
1786 ERR("Failed to get max size of marshal data, error 0x%08x\n", hr);
1787 goto cleanup;
1789 /* write constant sized common header and OR_CUSTOM data into stream */
1790 hr = IStream_Write(pStream, &objref,
1791 FIELD_OFFSET(OBJREF, u_objref.u_custom.pData), NULL);
1792 if (hr != S_OK)
1794 ERR("Failed to write OR_CUSTOM header to stream with 0x%08x\n", hr);
1795 goto cleanup;
1799 TRACE("Calling IMarshal::MarshalInterace\n");
1800 /* call helper object to do the actual marshaling */
1801 hr = IMarshal_MarshalInterface(pMarshal, pStream, riid, pUnk, dwDestContext,
1802 pvDestContext, mshlFlags);
1804 if (hr != S_OK)
1806 ERR("Failed to marshal the interface %s, %x\n", debugstr_guid(riid), hr);
1807 goto cleanup;
1810 cleanup:
1811 IMarshal_Release(pMarshal);
1813 TRACE("completed with hr 0x%08x\n", hr);
1815 return hr;
1818 /***********************************************************************
1819 * CoUnmarshalInterface [OLE32.@]
1821 * Unmarshals an object from a stream by creating a proxy to the remote
1822 * object, if necessary.
1824 * PARAMS
1826 * pStream [I] Stream containing the marshaled object.
1827 * riid [I] Interface identifier of the object to create a proxy to.
1828 * ppv [O] Address where proxy will be stored.
1830 * RETURNS
1832 * Success: S_OK.
1833 * Failure: HRESULT code.
1835 * SEE ALSO
1836 * CoMarshalInterface().
1838 HRESULT WINAPI CoUnmarshalInterface(IStream *pStream, REFIID riid, LPVOID *ppv)
1840 HRESULT hr;
1841 LPMARSHAL pMarshal;
1842 IID iid;
1843 IUnknown *object;
1845 TRACE("(%p, %s, %p)\n", pStream, debugstr_guid(riid), ppv);
1847 if (!pStream || !ppv)
1848 return E_INVALIDARG;
1850 hr = get_unmarshaler_from_stream(pStream, &pMarshal, &iid);
1851 if (hr != S_OK)
1852 return hr;
1854 /* call the helper object to do the actual unmarshaling */
1855 hr = IMarshal_UnmarshalInterface(pMarshal, pStream, &iid, (LPVOID*)&object);
1856 if (hr != S_OK)
1857 ERR("IMarshal::UnmarshalInterface failed, 0x%08x\n", hr);
1859 if (hr == S_OK)
1861 /* IID_NULL means use the interface ID of the marshaled object */
1862 if (!IsEqualIID(riid, &IID_NULL) && !IsEqualIID(riid, &iid))
1864 TRACE("requested interface != marshalled interface, additional QI needed\n");
1865 hr = IUnknown_QueryInterface(object, riid, ppv);
1866 if (hr != S_OK)
1867 ERR("Couldn't query for interface %s, hr = 0x%08x\n",
1868 debugstr_guid(riid), hr);
1869 IUnknown_Release(object);
1871 else
1873 *ppv = object;
1877 IMarshal_Release(pMarshal);
1879 TRACE("completed with hr 0x%x\n", hr);
1881 return hr;
1884 /***********************************************************************
1885 * CoReleaseMarshalData [OLE32.@]
1887 * Releases resources associated with an object that has been marshaled into
1888 * a stream.
1890 * PARAMS
1892 * pStream [I] The stream that the object has been marshaled into.
1894 * RETURNS
1895 * Success: S_OK.
1896 * Failure: HRESULT error code.
1898 * NOTES
1900 * Call this function to release resources associated with a normal or
1901 * table-weak marshal that will not be unmarshaled, and all table-strong
1902 * marshals when they are no longer needed.
1904 * SEE ALSO
1905 * CoMarshalInterface(), CoUnmarshalInterface().
1907 HRESULT WINAPI CoReleaseMarshalData(IStream *pStream)
1909 HRESULT hr;
1910 LPMARSHAL pMarshal;
1912 TRACE("(%p)\n", pStream);
1914 hr = get_unmarshaler_from_stream(pStream, &pMarshal, NULL);
1915 if (hr != S_OK)
1916 return hr;
1918 /* call the helper object to do the releasing of marshal data */
1919 hr = IMarshal_ReleaseMarshalData(pMarshal, pStream);
1920 if (hr != S_OK)
1921 ERR("IMarshal::ReleaseMarshalData failed with error 0x%08x\n", hr);
1923 IMarshal_Release(pMarshal);
1924 return hr;
1928 /***********************************************************************
1929 * CoMarshalInterThreadInterfaceInStream [OLE32.@]
1931 * Marshal an interface across threads in the same process.
1933 * PARAMS
1934 * riid [I] Identifier of the interface to be marshalled.
1935 * pUnk [I] Pointer to IUnknown-derived interface that will be marshalled.
1936 * ppStm [O] Pointer to IStream object that is created and then used to store the marshalled interface.
1938 * RETURNS
1939 * Success: S_OK
1940 * Failure: E_OUTOFMEMORY and other COM error codes
1942 * SEE ALSO
1943 * CoMarshalInterface(), CoUnmarshalInterface() and CoGetInterfaceAndReleaseStream()
1945 HRESULT WINAPI CoMarshalInterThreadInterfaceInStream(
1946 REFIID riid, LPUNKNOWN pUnk, LPSTREAM * ppStm)
1948 ULARGE_INTEGER xpos;
1949 LARGE_INTEGER seekto;
1950 HRESULT hres;
1952 TRACE("(%s, %p, %p)\n",debugstr_guid(riid), pUnk, ppStm);
1954 hres = CreateStreamOnHGlobal(NULL, TRUE, ppStm);
1955 if (FAILED(hres)) return hres;
1956 hres = CoMarshalInterface(*ppStm, riid, pUnk, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1958 if (SUCCEEDED(hres))
1960 memset(&seekto, 0, sizeof(seekto));
1961 IStream_Seek(*ppStm, seekto, STREAM_SEEK_SET, &xpos);
1963 else
1965 IStream_Release(*ppStm);
1966 *ppStm = NULL;
1969 return hres;
1972 /***********************************************************************
1973 * CoGetInterfaceAndReleaseStream [OLE32.@]
1975 * Unmarshalls an interface from a stream and then releases the stream.
1977 * PARAMS
1978 * pStm [I] Stream that contains the marshalled interface.
1979 * riid [I] Interface identifier of the object to unmarshall.
1980 * ppv [O] Address of pointer where the requested interface object will be stored.
1982 * RETURNS
1983 * Success: S_OK
1984 * Failure: A COM error code
1986 * SEE ALSO
1987 * CoMarshalInterThreadInterfaceInStream() and CoUnmarshalInterface()
1989 HRESULT WINAPI CoGetInterfaceAndReleaseStream(LPSTREAM pStm, REFIID riid,
1990 LPVOID *ppv)
1992 HRESULT hres;
1994 TRACE("(%p, %s, %p)\n", pStm, debugstr_guid(riid), ppv);
1996 if(!pStm) return E_INVALIDARG;
1997 hres = CoUnmarshalInterface(pStm, riid, ppv);
1998 IStream_Release(pStm);
1999 return hres;
2002 static HRESULT WINAPI StdMarshalCF_QueryInterface(LPCLASSFACTORY iface,
2003 REFIID riid, LPVOID *ppv)
2005 *ppv = NULL;
2006 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory))
2008 *ppv = iface;
2009 return S_OK;
2011 return E_NOINTERFACE;
2014 static ULONG WINAPI StdMarshalCF_AddRef(LPCLASSFACTORY iface)
2016 return 2; /* non-heap based object */
2019 static ULONG WINAPI StdMarshalCF_Release(LPCLASSFACTORY iface)
2021 return 1; /* non-heap based object */
2024 static HRESULT WINAPI StdMarshalCF_CreateInstance(LPCLASSFACTORY iface,
2025 LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv)
2027 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IMarshal))
2028 return StdMarshalImpl_Construct(riid, 0, NULL, ppv);
2030 FIXME("(%s), not supported.\n",debugstr_guid(riid));
2031 return E_NOINTERFACE;
2034 static HRESULT WINAPI StdMarshalCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
2036 FIXME("(%d), stub!\n",fLock);
2037 return S_OK;
2040 static const IClassFactoryVtbl StdMarshalCFVtbl =
2042 StdMarshalCF_QueryInterface,
2043 StdMarshalCF_AddRef,
2044 StdMarshalCF_Release,
2045 StdMarshalCF_CreateInstance,
2046 StdMarshalCF_LockServer
2048 static const IClassFactoryVtbl *StdMarshalCF = &StdMarshalCFVtbl;
2050 HRESULT MARSHAL_GetStandardMarshalCF(LPVOID *ppv)
2052 *ppv = &StdMarshalCF;
2053 return S_OK;
2056 /***********************************************************************
2057 * CoMarshalHresult [OLE32.@]
2059 * Marshals an HRESULT value into a stream.
2061 * PARAMS
2062 * pStm [I] Stream that hresult will be marshalled into.
2063 * hresult [I] HRESULT to be marshalled.
2065 * RETURNS
2066 * Success: S_OK
2067 * Failure: A COM error code
2069 * SEE ALSO
2070 * CoUnmarshalHresult().
2072 HRESULT WINAPI CoMarshalHresult(LPSTREAM pStm, HRESULT hresult)
2074 return IStream_Write(pStm, &hresult, sizeof(hresult), NULL);
2077 /***********************************************************************
2078 * CoUnmarshalHresult [OLE32.@]
2080 * Unmarshals an HRESULT value from a stream.
2082 * PARAMS
2083 * pStm [I] Stream that hresult will be unmarshalled from.
2084 * phresult [I] Pointer to HRESULT where the value will be unmarshalled to.
2086 * RETURNS
2087 * Success: S_OK
2088 * Failure: A COM error code
2090 * SEE ALSO
2091 * CoMarshalHresult().
2093 HRESULT WINAPI CoUnmarshalHresult(LPSTREAM pStm, HRESULT * phresult)
2095 return IStream_Read(pStm, phresult, sizeof(*phresult), NULL);