dwrite: Implement CreateFontFaceFromHdc().
[wine.git] / dlls / ole32 / marshal.c
blobb308600ef5f53006cad1191bb31b3e779ee53574
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 = This->dest_context;
809 MSHCTX new_dest_context;
813 new_dest_context = old_dest_context;
814 /* "stronger" values overwrite "weaker" values. stronger values are
815 * ones that disable more optimisations */
816 switch (old_dest_context)
818 case MSHCTX_INPROC:
819 new_dest_context = dest_context;
820 break;
821 case MSHCTX_CROSSCTX:
822 switch (dest_context)
824 case MSHCTX_INPROC:
825 break;
826 default:
827 new_dest_context = dest_context;
829 break;
830 case MSHCTX_LOCAL:
831 switch (dest_context)
833 case MSHCTX_INPROC:
834 case MSHCTX_CROSSCTX:
835 break;
836 default:
837 new_dest_context = dest_context;
839 break;
840 case MSHCTX_NOSHAREDMEM:
841 switch (dest_context)
843 case MSHCTX_DIFFERENTMACHINE:
844 new_dest_context = dest_context;
845 break;
846 default:
847 break;
849 break;
850 default:
851 break;
854 if (old_dest_context == new_dest_context) break;
856 old_dest_context = InterlockedCompareExchange((PLONG)&This->dest_context, new_dest_context, old_dest_context);
857 } while (new_dest_context != old_dest_context);
859 if (dest_context_data)
860 InterlockedExchangePointer(&This->dest_context_data, dest_context_data);
863 static HRESULT proxy_manager_query_local_interface(struct proxy_manager * This, REFIID riid, void ** ppv)
865 HRESULT hr;
866 struct ifproxy * ifproxy;
868 TRACE("%s\n", debugstr_guid(riid));
870 if (IsEqualIID(riid, &IID_IUnknown) ||
871 IsEqualIID(riid, &IID_IMultiQI))
873 *ppv = &This->IMultiQI_iface;
874 IMultiQI_AddRef(&This->IMultiQI_iface);
875 return S_OK;
877 if (IsEqualIID(riid, &IID_IMarshal))
879 *ppv = &This->IMarshal_iface;
880 IMarshal_AddRef(&This->IMarshal_iface);
881 return S_OK;
883 if (IsEqualIID(riid, &IID_IClientSecurity))
885 *ppv = &This->IClientSecurity_iface;
886 IClientSecurity_AddRef(&This->IClientSecurity_iface);
887 return S_OK;
890 hr = proxy_manager_find_ifproxy(This, riid, &ifproxy);
891 if (hr == S_OK)
893 *ppv = ifproxy->iface;
894 IUnknown_AddRef((IUnknown *)*ppv);
895 return S_OK;
898 *ppv = NULL;
899 return E_NOINTERFACE;
902 static HRESULT proxy_manager_create_ifproxy(
903 struct proxy_manager * This, const STDOBJREF *stdobjref, REFIID riid,
904 IRpcChannelBuffer * channel, struct ifproxy ** iif_out)
906 HRESULT hr;
907 IPSFactoryBuffer * psfb;
908 struct ifproxy * ifproxy = HeapAlloc(GetProcessHeap(), 0, sizeof(*ifproxy));
909 if (!ifproxy) return E_OUTOFMEMORY;
911 list_init(&ifproxy->entry);
913 ifproxy->parent = This;
914 ifproxy->stdobjref = *stdobjref;
915 ifproxy->iid = *riid;
916 ifproxy->refs = 0;
917 ifproxy->proxy = NULL;
919 assert(channel);
920 ifproxy->chan = channel; /* FIXME: we should take the binding strings and construct the channel in this function */
922 /* the IUnknown interface is special because it does not have a
923 * proxy associated with the ifproxy as we handle IUnknown ourselves */
924 if (IsEqualIID(riid, &IID_IUnknown))
926 ifproxy->iface = &This->IMultiQI_iface;
927 IMultiQI_AddRef(&This->IMultiQI_iface);
928 hr = S_OK;
930 else
932 hr = get_facbuf_for_iid(riid, &psfb);
933 if (hr == S_OK)
935 /* important note: the outer unknown is set to the proxy manager.
936 * This ensures the COM identity rules are not violated, by having a
937 * one-to-one mapping of objects on the proxy side to objects on the
938 * stub side, no matter which interface you view the object through */
939 hr = IPSFactoryBuffer_CreateProxy(psfb, (IUnknown*)&This->IMultiQI_iface, riid,
940 &ifproxy->proxy, &ifproxy->iface);
941 IPSFactoryBuffer_Release(psfb);
942 if (hr != S_OK)
943 ERR("Could not create proxy for interface %s, error 0x%08x\n",
944 debugstr_guid(riid), hr);
946 else
947 ERR("Could not get IPSFactoryBuffer for interface %s, error 0x%08x\n",
948 debugstr_guid(riid), hr);
950 if (hr == S_OK)
951 hr = IRpcProxyBuffer_Connect(ifproxy->proxy, ifproxy->chan);
954 if (hr == S_OK)
956 EnterCriticalSection(&This->cs);
957 list_add_tail(&This->interfaces, &ifproxy->entry);
958 LeaveCriticalSection(&This->cs);
960 *iif_out = ifproxy;
961 TRACE("ifproxy %p created for IPID %s, interface %s with %u public refs\n",
962 ifproxy, debugstr_guid(&stdobjref->ipid), debugstr_guid(riid), stdobjref->cPublicRefs);
964 else
965 ifproxy_destroy(ifproxy);
967 return hr;
970 static HRESULT proxy_manager_find_ifproxy(struct proxy_manager * This, REFIID riid, struct ifproxy ** ifproxy_found)
972 HRESULT hr = E_NOINTERFACE; /* assume not found */
973 struct list * cursor;
975 EnterCriticalSection(&This->cs);
976 LIST_FOR_EACH(cursor, &This->interfaces)
978 struct ifproxy * ifproxy = LIST_ENTRY(cursor, struct ifproxy, entry);
979 if (IsEqualIID(riid, &ifproxy->iid))
981 *ifproxy_found = ifproxy;
982 hr = S_OK;
983 break;
986 LeaveCriticalSection(&This->cs);
988 return hr;
991 static void proxy_manager_disconnect(struct proxy_manager * This)
993 struct list * cursor;
995 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This->oxid),
996 wine_dbgstr_longlong(This->oid));
998 EnterCriticalSection(&This->cs);
1000 /* SORFP_NOLIFTIMEMGMT proxies (for IRemUnknown) shouldn't be
1001 * disconnected - it won't do anything anyway, except cause
1002 * problems for other objects that depend on this proxy always
1003 * working */
1004 if (!(This->sorflags & SORFP_NOLIFETIMEMGMT))
1006 LIST_FOR_EACH(cursor, &This->interfaces)
1008 struct ifproxy * ifproxy = LIST_ENTRY(cursor, struct ifproxy, entry);
1009 ifproxy_disconnect(ifproxy);
1013 /* apartment is being destroyed so don't keep a pointer around to it */
1014 This->parent = NULL;
1016 LeaveCriticalSection(&This->cs);
1019 static HRESULT proxy_manager_get_remunknown(struct proxy_manager * This, IRemUnknown **remunk)
1021 HRESULT hr = S_OK;
1022 struct apartment *apt;
1023 BOOL called_in_original_apt;
1025 /* we don't want to try and unmarshal or use IRemUnknown if we don't want
1026 * lifetime management */
1027 if (This->sorflags & SORFP_NOLIFETIMEMGMT)
1028 return S_FALSE;
1030 apt = COM_CurrentApt();
1031 if (!apt)
1032 return CO_E_NOTINITIALIZED;
1034 called_in_original_apt = This->parent && (This->parent->oxid == apt->oxid);
1036 EnterCriticalSection(&This->cs);
1037 /* only return the cached object if called from the original apartment.
1038 * in future, we might want to make the IRemUnknown proxy callable from any
1039 * apartment to avoid these checks */
1040 if (This->remunk && called_in_original_apt)
1042 /* already created - return existing object */
1043 *remunk = This->remunk;
1044 IRemUnknown_AddRef(*remunk);
1046 else if (!This->parent)
1047 /* disconnected - we can't create IRemUnknown */
1048 hr = S_FALSE;
1049 else
1051 STDOBJREF stdobjref;
1052 /* Don't want IRemUnknown lifetime management as this is IRemUnknown!
1053 * We also don't care about whether or not the stub is still alive */
1054 stdobjref.flags = SORFP_NOLIFETIMEMGMT | SORF_NOPING;
1055 stdobjref.cPublicRefs = 1;
1056 /* oxid of destination object */
1057 stdobjref.oxid = This->oxid;
1058 /* FIXME: what should be used for the oid? The DCOM draft doesn't say */
1059 stdobjref.oid = (OID)-1;
1060 stdobjref.ipid = This->oxid_info.ipidRemUnknown;
1062 /* do the unmarshal */
1063 hr = unmarshal_object(&stdobjref, COM_CurrentApt(), This->dest_context,
1064 This->dest_context_data, &IID_IRemUnknown,
1065 &This->oxid_info, (void**)remunk);
1066 if (hr == S_OK && called_in_original_apt)
1068 This->remunk = *remunk;
1069 IRemUnknown_AddRef(This->remunk);
1072 LeaveCriticalSection(&This->cs);
1074 TRACE("got IRemUnknown* pointer %p, hr = 0x%08x\n", *remunk, hr);
1076 return hr;
1079 /* destroys a proxy manager, freeing the memory it used.
1080 * Note: this function should not be called from a list iteration in the
1081 * apartment, due to the fact that it removes itself from the apartment and
1082 * it could add a proxy to IRemUnknown into the apartment. */
1083 static void proxy_manager_destroy(struct proxy_manager * This)
1085 struct list * cursor;
1087 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This->oxid),
1088 wine_dbgstr_longlong(This->oid));
1090 if (This->parent)
1092 EnterCriticalSection(&This->parent->cs);
1094 /* remove ourself from the list of proxy objects in the apartment */
1095 LIST_FOR_EACH(cursor, &This->parent->proxies)
1097 if (cursor == &This->entry)
1099 list_remove(&This->entry);
1100 break;
1104 LeaveCriticalSection(&This->parent->cs);
1107 /* destroy all of the interface proxies */
1108 while ((cursor = list_head(&This->interfaces)))
1110 struct ifproxy * ifproxy = LIST_ENTRY(cursor, struct ifproxy, entry);
1111 ifproxy_destroy(ifproxy);
1114 if (This->remunk) IRemUnknown_Release(This->remunk);
1115 CoTaskMemFree(This->oxid_info.psa);
1117 DEBUG_CLEAR_CRITSEC_NAME(&This->cs);
1118 DeleteCriticalSection(&This->cs);
1120 CloseHandle(This->remoting_mutex);
1122 HeapFree(GetProcessHeap(), 0, This);
1125 /* finds the proxy manager corresponding to a given OXID and OID that has
1126 * been unmarshaled in the specified apartment. The caller must release the
1127 * reference to the proxy_manager when the object is no longer used. */
1128 static BOOL find_proxy_manager(APARTMENT * apt, OXID oxid, OID oid, struct proxy_manager ** proxy_found)
1130 BOOL found = FALSE;
1131 struct list * cursor;
1133 EnterCriticalSection(&apt->cs);
1134 LIST_FOR_EACH(cursor, &apt->proxies)
1136 struct proxy_manager * proxy = LIST_ENTRY(cursor, struct proxy_manager, entry);
1137 if ((oxid == proxy->oxid) && (oid == proxy->oid))
1139 /* be careful of a race with ClientIdentity_Release, which would
1140 * cause us to return a proxy which is in the process of being
1141 * destroyed */
1142 if (IMultiQI_AddRef(&proxy->IMultiQI_iface) != 0)
1144 *proxy_found = proxy;
1145 found = TRUE;
1146 break;
1150 LeaveCriticalSection(&apt->cs);
1151 return found;
1154 HRESULT apartment_disconnectproxies(struct apartment *apt)
1156 struct list * cursor;
1158 LIST_FOR_EACH(cursor, &apt->proxies)
1160 struct proxy_manager * proxy = LIST_ENTRY(cursor, struct proxy_manager, entry);
1161 proxy_manager_disconnect(proxy);
1164 return S_OK;
1167 /********************** StdMarshal implementation ****************************/
1168 typedef struct _StdMarshalImpl
1170 IMarshal IMarshal_iface;
1171 LONG ref;
1172 DWORD dest_context;
1173 void *dest_context_data;
1174 } StdMarshalImpl;
1176 static inline StdMarshalImpl *impl_from_StdMarshal(IMarshal *iface)
1178 return CONTAINING_RECORD(iface, StdMarshalImpl, IMarshal_iface);
1181 static HRESULT WINAPI
1182 StdMarshalImpl_QueryInterface(IMarshal *iface, REFIID riid, void **ppv)
1184 *ppv = NULL;
1185 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IMarshal, riid))
1187 *ppv = iface;
1188 IMarshal_AddRef(iface);
1189 return S_OK;
1191 FIXME("No interface for %s.\n", debugstr_guid(riid));
1192 return E_NOINTERFACE;
1195 static ULONG WINAPI
1196 StdMarshalImpl_AddRef(IMarshal *iface)
1198 StdMarshalImpl *This = impl_from_StdMarshal(iface);
1199 return InterlockedIncrement(&This->ref);
1202 static ULONG WINAPI
1203 StdMarshalImpl_Release(IMarshal *iface)
1205 StdMarshalImpl *This = impl_from_StdMarshal(iface);
1206 ULONG ref = InterlockedDecrement(&This->ref);
1208 if (!ref) HeapFree(GetProcessHeap(),0,This);
1209 return ref;
1212 static HRESULT WINAPI
1213 StdMarshalImpl_GetUnmarshalClass(
1214 IMarshal *iface, REFIID riid, void* pv, DWORD dwDestContext,
1215 void* pvDestContext, DWORD mshlflags, CLSID* pCid)
1217 *pCid = CLSID_DfMarshal;
1218 return S_OK;
1221 static HRESULT WINAPI
1222 StdMarshalImpl_GetMarshalSizeMax(
1223 IMarshal *iface, REFIID riid, void* pv, DWORD dwDestContext,
1224 void* pvDestContext, DWORD mshlflags, DWORD* pSize)
1226 *pSize = sizeof(STDOBJREF);
1227 return S_OK;
1230 static HRESULT WINAPI
1231 StdMarshalImpl_MarshalInterface(
1232 IMarshal *iface, IStream *pStm,REFIID riid, void* pv, DWORD dest_context,
1233 void* dest_context_data, DWORD mshlflags)
1235 STDOBJREF stdobjref;
1236 ULONG res;
1237 HRESULT hres;
1238 APARTMENT *apt = COM_CurrentApt();
1240 TRACE("(...,%s,...)\n", debugstr_guid(riid));
1242 if (!apt)
1244 ERR("Apartment not initialized\n");
1245 return CO_E_NOTINITIALIZED;
1248 /* make sure this apartment can be reached from other threads / processes */
1249 RPC_StartRemoting(apt);
1251 hres = marshal_object(apt, &stdobjref, riid, pv, dest_context, dest_context_data, mshlflags);
1252 if (hres != S_OK)
1254 ERR("Failed to create ifstub, hres=0x%x\n", hres);
1255 return hres;
1258 return IStream_Write(pStm, &stdobjref, sizeof(stdobjref), &res);
1261 /* helper for StdMarshalImpl_UnmarshalInterface - does the unmarshaling with
1262 * no questions asked about the rules surrounding same-apartment unmarshals
1263 * and table marshaling */
1264 static HRESULT unmarshal_object(const STDOBJREF *stdobjref, APARTMENT *apt,
1265 MSHCTX dest_context, void *dest_context_data,
1266 REFIID riid, const OXID_INFO *oxid_info,
1267 void **object)
1269 struct proxy_manager *proxy_manager = NULL;
1270 HRESULT hr = S_OK;
1272 assert(apt);
1274 TRACE("stdobjref: flags = %04x cPublicRefs = %d oxid = %s oid = %s ipid = %s\n",
1275 stdobjref->flags, stdobjref->cPublicRefs,
1276 wine_dbgstr_longlong(stdobjref->oxid),
1277 wine_dbgstr_longlong(stdobjref->oid),
1278 debugstr_guid(&stdobjref->ipid));
1280 /* create a new proxy manager if one doesn't already exist for the
1281 * object */
1282 if (!find_proxy_manager(apt, stdobjref->oxid, stdobjref->oid, &proxy_manager))
1284 hr = proxy_manager_construct(apt, stdobjref->flags,
1285 stdobjref->oxid, stdobjref->oid, oxid_info,
1286 &proxy_manager);
1288 else
1289 TRACE("proxy manager already created, using\n");
1291 if (hr == S_OK)
1293 struct ifproxy * ifproxy;
1295 proxy_manager_set_context(proxy_manager, dest_context, dest_context_data);
1297 hr = proxy_manager_find_ifproxy(proxy_manager, riid, &ifproxy);
1298 if (hr == E_NOINTERFACE)
1300 IRpcChannelBuffer *chanbuf;
1301 hr = RPC_CreateClientChannel(&stdobjref->oxid, &stdobjref->ipid,
1302 &proxy_manager->oxid_info,
1303 proxy_manager->dest_context,
1304 proxy_manager->dest_context_data,
1305 &chanbuf);
1306 if (hr == S_OK)
1307 hr = proxy_manager_create_ifproxy(proxy_manager, stdobjref,
1308 riid, chanbuf, &ifproxy);
1310 else
1311 IUnknown_AddRef((IUnknown *)ifproxy->iface);
1313 if (hr == S_OK)
1315 InterlockedExchangeAdd((LONG *)&ifproxy->refs, stdobjref->cPublicRefs);
1316 /* get at least one external reference to the object to keep it alive */
1317 hr = ifproxy_get_public_ref(ifproxy);
1318 if (FAILED(hr))
1319 ifproxy_destroy(ifproxy);
1322 if (hr == S_OK)
1323 *object = ifproxy->iface;
1326 /* release our reference to the proxy manager - the client/apartment
1327 * will hold on to the remaining reference for us */
1328 if (proxy_manager) IMultiQI_Release(&proxy_manager->IMultiQI_iface);
1330 return hr;
1333 static HRESULT WINAPI
1334 StdMarshalImpl_UnmarshalInterface(IMarshal *iface, IStream *pStm, REFIID riid, void **ppv)
1336 StdMarshalImpl *This = impl_from_StdMarshal(iface);
1337 struct stub_manager *stubmgr = NULL;
1338 STDOBJREF stdobjref;
1339 ULONG res;
1340 HRESULT hres;
1341 APARTMENT *apt = COM_CurrentApt();
1342 APARTMENT *stub_apt;
1343 OXID oxid;
1345 TRACE("(...,%s,....)\n", debugstr_guid(riid));
1347 /* we need an apartment to unmarshal into */
1348 if (!apt)
1350 ERR("Apartment not initialized\n");
1351 return CO_E_NOTINITIALIZED;
1354 /* read STDOBJREF from wire */
1355 hres = IStream_Read(pStm, &stdobjref, sizeof(stdobjref), &res);
1356 if (hres != S_OK) return STG_E_READFAULT;
1358 hres = apartment_getoxid(apt, &oxid);
1359 if (hres != S_OK) return hres;
1361 /* check if we're marshalling back to ourselves */
1362 if ((oxid == stdobjref.oxid) && (stubmgr = get_stub_manager(apt, stdobjref.oid)))
1364 TRACE("Unmarshalling object marshalled in same apartment for iid %s, "
1365 "returning original object %p\n", debugstr_guid(riid), stubmgr->object);
1367 hres = IUnknown_QueryInterface(stubmgr->object, riid, ppv);
1369 /* unref the ifstub. FIXME: only do this on success? */
1370 if (!stub_manager_is_table_marshaled(stubmgr, &stdobjref.ipid))
1371 stub_manager_ext_release(stubmgr, stdobjref.cPublicRefs, stdobjref.flags & SORFP_TABLEWEAK, FALSE);
1373 stub_manager_int_release(stubmgr);
1374 return hres;
1377 /* notify stub manager about unmarshal if process-local object.
1378 * note: if the oxid is not found then we and native will quite happily
1379 * ignore table marshaling and normal marshaling rules regarding number of
1380 * unmarshals, etc, but if you abuse these rules then your proxy could end
1381 * up returning RPC_E_DISCONNECTED. */
1382 if ((stub_apt = apartment_findfromoxid(stdobjref.oxid, TRUE)))
1384 if ((stubmgr = get_stub_manager(stub_apt, stdobjref.oid)))
1386 if (!stub_manager_notify_unmarshal(stubmgr, &stdobjref.ipid))
1387 hres = CO_E_OBJNOTCONNECTED;
1389 else
1391 WARN("Couldn't find object for OXID %s, OID %s, assuming disconnected\n",
1392 wine_dbgstr_longlong(stdobjref.oxid),
1393 wine_dbgstr_longlong(stdobjref.oid));
1394 hres = CO_E_OBJNOTCONNECTED;
1397 else
1398 TRACE("Treating unmarshal from OXID %s as inter-process\n",
1399 wine_dbgstr_longlong(stdobjref.oxid));
1401 if (hres == S_OK)
1402 hres = unmarshal_object(&stdobjref, apt, This->dest_context,
1403 This->dest_context_data, riid,
1404 stubmgr ? &stubmgr->oxid_info : NULL, ppv);
1406 if (stubmgr) stub_manager_int_release(stubmgr);
1407 if (stub_apt) apartment_release(stub_apt);
1409 if (hres != S_OK) WARN("Failed with error 0x%08x\n", hres);
1410 else TRACE("Successfully created proxy %p\n", *ppv);
1412 return hres;
1415 static HRESULT WINAPI
1416 StdMarshalImpl_ReleaseMarshalData(IMarshal *iface, IStream *pStm)
1418 STDOBJREF stdobjref;
1419 ULONG res;
1420 HRESULT hres;
1421 struct stub_manager *stubmgr;
1422 APARTMENT *apt;
1424 TRACE("iface=%p, pStm=%p\n", iface, pStm);
1426 hres = IStream_Read(pStm, &stdobjref, sizeof(stdobjref), &res);
1427 if (hres != S_OK) return STG_E_READFAULT;
1429 TRACE("oxid = %s, oid = %s, ipid = %s\n",
1430 wine_dbgstr_longlong(stdobjref.oxid),
1431 wine_dbgstr_longlong(stdobjref.oid),
1432 wine_dbgstr_guid(&stdobjref.ipid));
1434 if (!(apt = apartment_findfromoxid(stdobjref.oxid, TRUE)))
1436 WARN("Could not map OXID %s to apartment object\n",
1437 wine_dbgstr_longlong(stdobjref.oxid));
1438 return RPC_E_INVALID_OBJREF;
1441 if (!(stubmgr = get_stub_manager(apt, stdobjref.oid)))
1443 apartment_release(apt);
1444 ERR("could not map object ID to stub manager, oxid=%s, oid=%s\n",
1445 wine_dbgstr_longlong(stdobjref.oxid), wine_dbgstr_longlong(stdobjref.oid));
1446 return RPC_E_INVALID_OBJREF;
1449 stub_manager_release_marshal_data(stubmgr, stdobjref.cPublicRefs, &stdobjref.ipid, stdobjref.flags & SORFP_TABLEWEAK);
1451 stub_manager_int_release(stubmgr);
1452 apartment_release(apt);
1454 return S_OK;
1457 static HRESULT WINAPI
1458 StdMarshalImpl_DisconnectObject(IMarshal *iface, DWORD dwReserved)
1460 FIXME("(), stub!\n");
1461 return S_OK;
1464 static const IMarshalVtbl StdMarshalVtbl =
1466 StdMarshalImpl_QueryInterface,
1467 StdMarshalImpl_AddRef,
1468 StdMarshalImpl_Release,
1469 StdMarshalImpl_GetUnmarshalClass,
1470 StdMarshalImpl_GetMarshalSizeMax,
1471 StdMarshalImpl_MarshalInterface,
1472 StdMarshalImpl_UnmarshalInterface,
1473 StdMarshalImpl_ReleaseMarshalData,
1474 StdMarshalImpl_DisconnectObject
1477 static HRESULT StdMarshalImpl_Construct(REFIID riid, DWORD dest_context, void *dest_context_data, void** ppvObject)
1479 HRESULT hr;
1481 StdMarshalImpl *pStdMarshal = HeapAlloc(GetProcessHeap(), 0, sizeof(StdMarshalImpl));
1482 if (!pStdMarshal)
1483 return E_OUTOFMEMORY;
1485 pStdMarshal->IMarshal_iface.lpVtbl = &StdMarshalVtbl;
1486 pStdMarshal->ref = 0;
1487 pStdMarshal->dest_context = dest_context;
1488 pStdMarshal->dest_context_data = dest_context_data;
1490 hr = IMarshal_QueryInterface(&pStdMarshal->IMarshal_iface, riid, ppvObject);
1491 if (FAILED(hr))
1492 HeapFree(GetProcessHeap(), 0, pStdMarshal);
1494 return hr;
1497 /***********************************************************************
1498 * CoGetStandardMarshal [OLE32.@]
1500 * Gets or creates a standard marshal object.
1502 * PARAMS
1503 * riid [I] Interface identifier of the pUnk object.
1504 * pUnk [I] Optional. Object to get the marshal object for.
1505 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1506 * pvDestContext [I] Reserved. Must be NULL.
1507 * mshlflags [I] Flags affecting the marshaling process.
1508 * ppMarshal [O] Address where marshal object will be stored.
1510 * RETURNS
1511 * Success: S_OK.
1512 * Failure: HRESULT code.
1514 * NOTES
1516 * The function retrieves the IMarshal object associated with an object if
1517 * that object is currently an active stub, otherwise a new marshal object is
1518 * created.
1520 HRESULT WINAPI CoGetStandardMarshal(REFIID riid, IUnknown *pUnk,
1521 DWORD dwDestContext, LPVOID pvDestContext,
1522 DWORD mshlflags, LPMARSHAL *ppMarshal)
1524 if (pUnk == NULL)
1526 FIXME("(%s,NULL,%x,%p,%x,%p), unimplemented yet.\n",
1527 debugstr_guid(riid),dwDestContext,pvDestContext,mshlflags,ppMarshal);
1528 return E_NOTIMPL;
1530 TRACE("(%s,%p,%x,%p,%x,%p)\n",
1531 debugstr_guid(riid),pUnk,dwDestContext,pvDestContext,mshlflags,ppMarshal);
1533 return StdMarshalImpl_Construct(&IID_IMarshal, dwDestContext, pvDestContext, (void**)ppMarshal);
1536 /***********************************************************************
1537 * get_marshaler [internal]
1539 * Retrieves an IMarshal interface for an object.
1541 static HRESULT get_marshaler(REFIID riid, IUnknown *pUnk, DWORD dwDestContext,
1542 void *pvDestContext, DWORD mshlFlags,
1543 LPMARSHAL *pMarshal)
1545 HRESULT hr;
1547 if (!pUnk)
1548 return E_POINTER;
1549 hr = IUnknown_QueryInterface(pUnk, &IID_IMarshal, (LPVOID*)pMarshal);
1550 if (hr != S_OK)
1551 hr = CoGetStandardMarshal(riid, pUnk, dwDestContext, pvDestContext,
1552 mshlFlags, pMarshal);
1553 return hr;
1556 /***********************************************************************
1557 * get_unmarshaler_from_stream [internal]
1559 * Creates an IMarshal* object according to the data marshaled to the stream.
1560 * The function leaves the stream pointer at the start of the data written
1561 * to the stream by the IMarshal* object.
1563 static HRESULT get_unmarshaler_from_stream(IStream *stream, IMarshal **marshal, IID *iid)
1565 HRESULT hr;
1566 ULONG res;
1567 OBJREF objref;
1569 /* read common OBJREF header */
1570 hr = IStream_Read(stream, &objref, FIELD_OFFSET(OBJREF, u_objref), &res);
1571 if (hr != S_OK || (res != FIELD_OFFSET(OBJREF, u_objref)))
1573 ERR("Failed to read common OBJREF header, 0x%08x\n", hr);
1574 return STG_E_READFAULT;
1577 /* sanity check on header */
1578 if (objref.signature != OBJREF_SIGNATURE)
1580 ERR("Bad OBJREF signature 0x%08x\n", objref.signature);
1581 return RPC_E_INVALID_OBJREF;
1584 if (iid) *iid = objref.iid;
1586 /* FIXME: handler marshaling */
1587 if (objref.flags & OBJREF_STANDARD)
1589 TRACE("Using standard unmarshaling\n");
1590 hr = StdMarshalImpl_Construct(&IID_IMarshal, 0, NULL, (LPVOID*)marshal);
1592 else if (objref.flags & OBJREF_CUSTOM)
1594 ULONG custom_header_size = FIELD_OFFSET(OBJREF, u_objref.u_custom.pData) -
1595 FIELD_OFFSET(OBJREF, u_objref.u_custom);
1596 TRACE("Using custom unmarshaling\n");
1597 /* read constant sized OR_CUSTOM data from stream */
1598 hr = IStream_Read(stream, &objref.u_objref.u_custom,
1599 custom_header_size, &res);
1600 if (hr != S_OK || (res != custom_header_size))
1602 ERR("Failed to read OR_CUSTOM header, 0x%08x\n", hr);
1603 return STG_E_READFAULT;
1605 /* now create the marshaler specified in the stream */
1606 hr = CoCreateInstance(&objref.u_objref.u_custom.clsid, NULL,
1607 CLSCTX_INPROC_SERVER, &IID_IMarshal,
1608 (LPVOID*)marshal);
1610 else
1612 FIXME("Invalid or unimplemented marshaling type specified: %x\n",
1613 objref.flags);
1614 return RPC_E_INVALID_OBJREF;
1617 if (hr != S_OK)
1618 ERR("Failed to create marshal, 0x%08x\n", hr);
1620 return hr;
1623 /***********************************************************************
1624 * CoGetMarshalSizeMax [OLE32.@]
1626 * Gets the maximum amount of data that will be needed by a marshal.
1628 * PARAMS
1629 * pulSize [O] Address where maximum marshal size will be stored.
1630 * riid [I] Identifier of the interface to marshal.
1631 * pUnk [I] Pointer to the object to marshal.
1632 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1633 * pvDestContext [I] Reserved. Must be NULL.
1634 * mshlFlags [I] Flags that affect the marshaling. See CoMarshalInterface().
1636 * RETURNS
1637 * Success: S_OK.
1638 * Failure: HRESULT code.
1640 * SEE ALSO
1641 * CoMarshalInterface().
1643 HRESULT WINAPI CoGetMarshalSizeMax(ULONG *pulSize, REFIID riid, IUnknown *pUnk,
1644 DWORD dwDestContext, void *pvDestContext,
1645 DWORD mshlFlags)
1647 HRESULT hr;
1648 LPMARSHAL pMarshal;
1649 CLSID marshaler_clsid;
1651 hr = get_marshaler(riid, pUnk, dwDestContext, pvDestContext, mshlFlags, &pMarshal);
1652 if (hr != S_OK)
1653 return hr;
1655 hr = IMarshal_GetUnmarshalClass(pMarshal, riid, pUnk, dwDestContext,
1656 pvDestContext, mshlFlags, &marshaler_clsid);
1657 if (hr != S_OK)
1659 ERR("IMarshal::GetUnmarshalClass failed, 0x%08x\n", hr);
1660 IMarshal_Release(pMarshal);
1661 return hr;
1664 hr = IMarshal_GetMarshalSizeMax(pMarshal, riid, pUnk, dwDestContext,
1665 pvDestContext, mshlFlags, pulSize);
1666 if (IsEqualCLSID(&marshaler_clsid, &CLSID_DfMarshal))
1667 /* add on the size of the common header */
1668 *pulSize += FIELD_OFFSET(OBJREF, u_objref);
1669 else
1670 /* custom marshaling: add on the size of the whole OBJREF structure
1671 * like native does */
1672 *pulSize += sizeof(OBJREF);
1674 IMarshal_Release(pMarshal);
1675 return hr;
1679 static void dump_MSHLFLAGS(MSHLFLAGS flags)
1681 if (flags & MSHLFLAGS_TABLESTRONG)
1682 TRACE(" MSHLFLAGS_TABLESTRONG");
1683 if (flags & MSHLFLAGS_TABLEWEAK)
1684 TRACE(" MSHLFLAGS_TABLEWEAK");
1685 if (!(flags & (MSHLFLAGS_TABLESTRONG|MSHLFLAGS_TABLEWEAK)))
1686 TRACE(" MSHLFLAGS_NORMAL");
1687 if (flags & MSHLFLAGS_NOPING)
1688 TRACE(" MSHLFLAGS_NOPING");
1691 /***********************************************************************
1692 * CoMarshalInterface [OLE32.@]
1694 * Marshals an interface into a stream so that the object can then be
1695 * unmarshaled from another COM apartment and used remotely.
1697 * PARAMS
1698 * pStream [I] Stream the object will be marshaled into.
1699 * riid [I] Identifier of the interface to marshal.
1700 * pUnk [I] Pointer to the object to marshal.
1701 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1702 * pvDestContext [I] Reserved. Must be NULL.
1703 * mshlFlags [I] Flags that affect the marshaling. See notes.
1705 * RETURNS
1706 * Success: S_OK.
1707 * Failure: HRESULT code.
1709 * NOTES
1711 * The mshlFlags parameter can take one or more of the following flags:
1712 *| MSHLFLAGS_NORMAL - Unmarshal once, releases stub on last proxy release.
1713 *| MSHLFLAGS_TABLESTRONG - Unmarshal many, release when CoReleaseMarshalData() called.
1714 *| MSHLFLAGS_TABLEWEAK - Unmarshal many, releases stub on last proxy release.
1715 *| MSHLFLAGS_NOPING - No automatic garbage collection (and so reduces network traffic).
1717 * If a marshaled object is not unmarshaled, then CoReleaseMarshalData() must
1718 * be called in order to release the resources used in the marshaling.
1720 * SEE ALSO
1721 * CoUnmarshalInterface(), CoReleaseMarshalData().
1723 HRESULT WINAPI CoMarshalInterface(IStream *pStream, REFIID riid, IUnknown *pUnk,
1724 DWORD dwDestContext, void *pvDestContext,
1725 DWORD mshlFlags)
1727 HRESULT hr;
1728 CLSID marshaler_clsid;
1729 OBJREF objref;
1730 LPMARSHAL pMarshal;
1732 TRACE("(%p, %s, %p, %x, %p,", pStream, debugstr_guid(riid), pUnk,
1733 dwDestContext, pvDestContext);
1734 dump_MSHLFLAGS(mshlFlags);
1735 TRACE(")\n");
1737 if (!pUnk || !pStream)
1738 return E_INVALIDARG;
1740 objref.signature = OBJREF_SIGNATURE;
1741 objref.iid = *riid;
1743 /* get the marshaler for the specified interface */
1744 hr = get_marshaler(riid, pUnk, dwDestContext, pvDestContext, mshlFlags, &pMarshal);
1745 if (hr != S_OK)
1747 ERR("Failed to get marshaller, 0x%08x\n", hr);
1748 return hr;
1751 hr = IMarshal_GetUnmarshalClass(pMarshal, riid, pUnk, dwDestContext,
1752 pvDestContext, mshlFlags, &marshaler_clsid);
1753 if (hr != S_OK)
1755 ERR("IMarshal::GetUnmarshalClass failed, 0x%08x\n", hr);
1756 goto cleanup;
1759 /* FIXME: implement handler marshaling too */
1760 if (IsEqualCLSID(&marshaler_clsid, &CLSID_DfMarshal))
1762 TRACE("Using standard marshaling\n");
1763 objref.flags = OBJREF_STANDARD;
1765 /* write the common OBJREF header to the stream */
1766 hr = IStream_Write(pStream, &objref, FIELD_OFFSET(OBJREF, u_objref), NULL);
1767 if (hr != S_OK)
1769 ERR("Failed to write OBJREF header to stream, 0x%08x\n", hr);
1770 goto cleanup;
1773 else
1775 TRACE("Using custom marshaling\n");
1776 objref.flags = OBJREF_CUSTOM;
1777 objref.u_objref.u_custom.clsid = marshaler_clsid;
1778 objref.u_objref.u_custom.cbExtension = 0;
1779 objref.u_objref.u_custom.size = 0;
1780 hr = IMarshal_GetMarshalSizeMax(pMarshal, riid, pUnk, dwDestContext,
1781 pvDestContext, mshlFlags,
1782 &objref.u_objref.u_custom.size);
1783 if (hr != S_OK)
1785 ERR("Failed to get max size of marshal data, error 0x%08x\n", hr);
1786 goto cleanup;
1788 /* write constant sized common header and OR_CUSTOM data into stream */
1789 hr = IStream_Write(pStream, &objref,
1790 FIELD_OFFSET(OBJREF, u_objref.u_custom.pData), NULL);
1791 if (hr != S_OK)
1793 ERR("Failed to write OR_CUSTOM header to stream with 0x%08x\n", hr);
1794 goto cleanup;
1798 TRACE("Calling IMarshal::MarshalInterace\n");
1799 /* call helper object to do the actual marshaling */
1800 hr = IMarshal_MarshalInterface(pMarshal, pStream, riid, pUnk, dwDestContext,
1801 pvDestContext, mshlFlags);
1803 if (hr != S_OK)
1805 ERR("Failed to marshal the interface %s, %x\n", debugstr_guid(riid), hr);
1806 goto cleanup;
1809 cleanup:
1810 IMarshal_Release(pMarshal);
1812 TRACE("completed with hr 0x%08x\n", hr);
1814 return hr;
1817 /***********************************************************************
1818 * CoUnmarshalInterface [OLE32.@]
1820 * Unmarshals an object from a stream by creating a proxy to the remote
1821 * object, if necessary.
1823 * PARAMS
1825 * pStream [I] Stream containing the marshaled object.
1826 * riid [I] Interface identifier of the object to create a proxy to.
1827 * ppv [O] Address where proxy will be stored.
1829 * RETURNS
1831 * Success: S_OK.
1832 * Failure: HRESULT code.
1834 * SEE ALSO
1835 * CoMarshalInterface().
1837 HRESULT WINAPI CoUnmarshalInterface(IStream *pStream, REFIID riid, LPVOID *ppv)
1839 HRESULT hr;
1840 LPMARSHAL pMarshal;
1841 IID iid;
1842 IUnknown *object;
1844 TRACE("(%p, %s, %p)\n", pStream, debugstr_guid(riid), ppv);
1846 if (!pStream || !ppv)
1847 return E_INVALIDARG;
1849 hr = get_unmarshaler_from_stream(pStream, &pMarshal, &iid);
1850 if (hr != S_OK)
1851 return hr;
1853 /* call the helper object to do the actual unmarshaling */
1854 hr = IMarshal_UnmarshalInterface(pMarshal, pStream, &iid, (LPVOID*)&object);
1855 if (hr != S_OK)
1856 ERR("IMarshal::UnmarshalInterface failed, 0x%08x\n", hr);
1858 if (hr == S_OK)
1860 /* IID_NULL means use the interface ID of the marshaled object */
1861 if (!IsEqualIID(riid, &IID_NULL) && !IsEqualIID(riid, &iid))
1863 TRACE("requested interface != marshalled interface, additional QI needed\n");
1864 hr = IUnknown_QueryInterface(object, riid, ppv);
1865 if (hr != S_OK)
1866 ERR("Couldn't query for interface %s, hr = 0x%08x\n",
1867 debugstr_guid(riid), hr);
1868 IUnknown_Release(object);
1870 else
1872 *ppv = object;
1876 IMarshal_Release(pMarshal);
1878 TRACE("completed with hr 0x%x\n", hr);
1880 return hr;
1883 /***********************************************************************
1884 * CoReleaseMarshalData [OLE32.@]
1886 * Releases resources associated with an object that has been marshaled into
1887 * a stream.
1889 * PARAMS
1891 * pStream [I] The stream that the object has been marshaled into.
1893 * RETURNS
1894 * Success: S_OK.
1895 * Failure: HRESULT error code.
1897 * NOTES
1899 * Call this function to release resources associated with a normal or
1900 * table-weak marshal that will not be unmarshaled, and all table-strong
1901 * marshals when they are no longer needed.
1903 * SEE ALSO
1904 * CoMarshalInterface(), CoUnmarshalInterface().
1906 HRESULT WINAPI CoReleaseMarshalData(IStream *pStream)
1908 HRESULT hr;
1909 LPMARSHAL pMarshal;
1911 TRACE("(%p)\n", pStream);
1913 hr = get_unmarshaler_from_stream(pStream, &pMarshal, NULL);
1914 if (hr != S_OK)
1915 return hr;
1917 /* call the helper object to do the releasing of marshal data */
1918 hr = IMarshal_ReleaseMarshalData(pMarshal, pStream);
1919 if (hr != S_OK)
1920 ERR("IMarshal::ReleaseMarshalData failed with error 0x%08x\n", hr);
1922 IMarshal_Release(pMarshal);
1923 return hr;
1927 /***********************************************************************
1928 * CoMarshalInterThreadInterfaceInStream [OLE32.@]
1930 * Marshal an interface across threads in the same process.
1932 * PARAMS
1933 * riid [I] Identifier of the interface to be marshalled.
1934 * pUnk [I] Pointer to IUnknown-derived interface that will be marshalled.
1935 * ppStm [O] Pointer to IStream object that is created and then used to store the marshalled interface.
1937 * RETURNS
1938 * Success: S_OK
1939 * Failure: E_OUTOFMEMORY and other COM error codes
1941 * SEE ALSO
1942 * CoMarshalInterface(), CoUnmarshalInterface() and CoGetInterfaceAndReleaseStream()
1944 HRESULT WINAPI CoMarshalInterThreadInterfaceInStream(
1945 REFIID riid, LPUNKNOWN pUnk, LPSTREAM * ppStm)
1947 ULARGE_INTEGER xpos;
1948 LARGE_INTEGER seekto;
1949 HRESULT hres;
1951 TRACE("(%s, %p, %p)\n",debugstr_guid(riid), pUnk, ppStm);
1953 hres = CreateStreamOnHGlobal(NULL, TRUE, ppStm);
1954 if (FAILED(hres)) return hres;
1955 hres = CoMarshalInterface(*ppStm, riid, pUnk, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1957 if (SUCCEEDED(hres))
1959 memset(&seekto, 0, sizeof(seekto));
1960 IStream_Seek(*ppStm, seekto, STREAM_SEEK_SET, &xpos);
1962 else
1964 IStream_Release(*ppStm);
1965 *ppStm = NULL;
1968 return hres;
1971 /***********************************************************************
1972 * CoGetInterfaceAndReleaseStream [OLE32.@]
1974 * Unmarshalls an interface from a stream and then releases the stream.
1976 * PARAMS
1977 * pStm [I] Stream that contains the marshalled interface.
1978 * riid [I] Interface identifier of the object to unmarshall.
1979 * ppv [O] Address of pointer where the requested interface object will be stored.
1981 * RETURNS
1982 * Success: S_OK
1983 * Failure: A COM error code
1985 * SEE ALSO
1986 * CoMarshalInterThreadInterfaceInStream() and CoUnmarshalInterface()
1988 HRESULT WINAPI CoGetInterfaceAndReleaseStream(LPSTREAM pStm, REFIID riid,
1989 LPVOID *ppv)
1991 HRESULT hres;
1993 TRACE("(%p, %s, %p)\n", pStm, debugstr_guid(riid), ppv);
1995 if(!pStm) return E_INVALIDARG;
1996 hres = CoUnmarshalInterface(pStm, riid, ppv);
1997 IStream_Release(pStm);
1998 return hres;
2001 static HRESULT WINAPI StdMarshalCF_QueryInterface(LPCLASSFACTORY iface,
2002 REFIID riid, LPVOID *ppv)
2004 *ppv = NULL;
2005 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory))
2007 *ppv = iface;
2008 return S_OK;
2010 return E_NOINTERFACE;
2013 static ULONG WINAPI StdMarshalCF_AddRef(LPCLASSFACTORY iface)
2015 return 2; /* non-heap based object */
2018 static ULONG WINAPI StdMarshalCF_Release(LPCLASSFACTORY iface)
2020 return 1; /* non-heap based object */
2023 static HRESULT WINAPI StdMarshalCF_CreateInstance(LPCLASSFACTORY iface,
2024 LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv)
2026 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IMarshal))
2027 return StdMarshalImpl_Construct(riid, 0, NULL, ppv);
2029 FIXME("(%s), not supported.\n",debugstr_guid(riid));
2030 return E_NOINTERFACE;
2033 static HRESULT WINAPI StdMarshalCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
2035 FIXME("(%d), stub!\n",fLock);
2036 return S_OK;
2039 static const IClassFactoryVtbl StdMarshalCFVtbl =
2041 StdMarshalCF_QueryInterface,
2042 StdMarshalCF_AddRef,
2043 StdMarshalCF_Release,
2044 StdMarshalCF_CreateInstance,
2045 StdMarshalCF_LockServer
2047 static const IClassFactoryVtbl *StdMarshalCF = &StdMarshalCFVtbl;
2049 HRESULT MARSHAL_GetStandardMarshalCF(LPVOID *ppv)
2051 *ppv = &StdMarshalCF;
2052 return S_OK;
2055 /***********************************************************************
2056 * CoMarshalHresult [OLE32.@]
2058 * Marshals an HRESULT value into a stream.
2060 * PARAMS
2061 * pStm [I] Stream that hresult will be marshalled into.
2062 * hresult [I] HRESULT to be marshalled.
2064 * RETURNS
2065 * Success: S_OK
2066 * Failure: A COM error code
2068 * SEE ALSO
2069 * CoUnmarshalHresult().
2071 HRESULT WINAPI CoMarshalHresult(LPSTREAM pStm, HRESULT hresult)
2073 return IStream_Write(pStm, &hresult, sizeof(hresult), NULL);
2076 /***********************************************************************
2077 * CoUnmarshalHresult [OLE32.@]
2079 * Unmarshals an HRESULT value from a stream.
2081 * PARAMS
2082 * pStm [I] Stream that hresult will be unmarshalled from.
2083 * phresult [I] Pointer to HRESULT where the value will be unmarshalled to.
2085 * RETURNS
2086 * Success: S_OK
2087 * Failure: A COM error code
2089 * SEE ALSO
2090 * CoMarshalHresult().
2092 HRESULT WINAPI CoUnmarshalHresult(LPSTREAM pStm, HRESULT * phresult)
2094 return IStream_Read(pStm, phresult, sizeof(*phresult), NULL);