ole32: Keep track of the number of weak references to an object.
[wine.git] / dlls / ole32 / marshal.c
blob42ea68d453cf253c92e39b393d36952d0b639b5b
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 static HRESULT unmarshal_object(const STDOBJREF *stdobjref, APARTMENT *apt,
56 MSHCTX dest_context, void *dest_context_data,
57 REFIID riid, const OXID_INFO *oxid_info,
58 void **object);
60 /* Marshalling just passes a unique identifier to the remote client,
61 * that makes it possible to find the passed interface again.
63 * So basically we need a set of values that make it unique.
65 * Note that the IUnknown_QI(ob,xiid,&ppv) always returns the SAME ppv value!
67 * A triple is used: OXID (apt id), OID (stub manager id),
68 * IPID (interface ptr/stub id).
70 * OXIDs identify an apartment and are network scoped
71 * OIDs identify a stub manager and are apartment scoped
72 * IPIDs identify an interface stub and are apartment scoped
75 static inline HRESULT get_facbuf_for_iid(REFIID riid, IPSFactoryBuffer **facbuf)
77 HRESULT hr;
78 CLSID clsid;
80 if ((hr = CoGetPSClsid(riid, &clsid)))
81 return hr;
82 return CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER | WINE_CLSCTX_DONT_HOST,
83 NULL, &IID_IPSFactoryBuffer, (LPVOID*)facbuf);
86 /* marshals an object into a STDOBJREF structure */
87 HRESULT marshal_object(APARTMENT *apt, STDOBJREF *stdobjref, REFIID riid, IUnknown *object, MSHLFLAGS mshlflags)
89 struct stub_manager *manager;
90 struct ifstub *ifstub;
91 BOOL tablemarshal;
92 IRpcStubBuffer *stub = NULL;
93 HRESULT hr;
94 IUnknown *iobject = NULL; /* object of type riid */
96 hr = apartment_getoxid(apt, &stdobjref->oxid);
97 if (hr != S_OK)
98 return hr;
100 hr = apartment_createwindowifneeded(apt);
101 if (hr != S_OK)
102 return hr;
104 hr = IUnknown_QueryInterface(object, riid, (void **)&iobject);
105 if (hr != S_OK)
107 ERR("object doesn't expose interface %s, failing with error 0x%08x\n",
108 debugstr_guid(riid), hr);
109 return E_NOINTERFACE;
112 /* IUnknown doesn't require a stub buffer, because it never goes out on
113 * the wire */
114 if (!IsEqualIID(riid, &IID_IUnknown))
116 IPSFactoryBuffer *psfb;
118 hr = get_facbuf_for_iid(riid, &psfb);
119 if (hr != S_OK)
121 ERR("couldn't get IPSFactory buffer for interface %s\n", debugstr_guid(riid));
122 IUnknown_Release(iobject);
123 return hr;
126 hr = IPSFactoryBuffer_CreateStub(psfb, riid, iobject, &stub);
127 IPSFactoryBuffer_Release(psfb);
128 if (hr != S_OK)
130 ERR("Failed to create an IRpcStubBuffer from IPSFactory for %s with error 0x%08x\n",
131 debugstr_guid(riid), hr);
132 IUnknown_Release(iobject);
133 return hr;
137 stdobjref->flags = SORF_NULL;
138 if (mshlflags & MSHLFLAGS_TABLEWEAK)
139 stdobjref->flags |= SORFP_TABLEWEAK;
140 if (mshlflags & MSHLFLAGS_NOPING)
141 stdobjref->flags |= SORF_NOPING;
143 if ((manager = get_stub_manager_from_object(apt, object)))
144 TRACE("registering new ifstub on pre-existing manager\n");
145 else
147 TRACE("constructing new stub manager\n");
149 manager = new_stub_manager(apt, object);
150 if (!manager)
152 if (stub) IRpcStubBuffer_Release(stub);
153 IUnknown_Release(iobject);
154 return E_OUTOFMEMORY;
157 stdobjref->oid = manager->oid;
159 tablemarshal = ((mshlflags & MSHLFLAGS_TABLESTRONG) || (mshlflags & MSHLFLAGS_TABLEWEAK));
161 /* make sure ifstub that we are creating is unique */
162 ifstub = stub_manager_find_ifstub(manager, riid, mshlflags);
163 if (!ifstub)
164 ifstub = stub_manager_new_ifstub(manager, stub, iobject, riid, mshlflags);
166 if (stub) IRpcStubBuffer_Release(stub);
167 IUnknown_Release(iobject);
169 if (!ifstub)
171 stub_manager_int_release(manager);
172 /* destroy the stub manager if it has no ifstubs by releasing
173 * zero external references */
174 stub_manager_ext_release(manager, 0, FALSE, TRUE);
175 return E_OUTOFMEMORY;
178 if (!tablemarshal)
180 stdobjref->cPublicRefs = NORMALEXTREFS;
181 stub_manager_ext_addref(manager, stdobjref->cPublicRefs, FALSE);
183 else
185 stdobjref->cPublicRefs = 0;
186 if (mshlflags & MSHLFLAGS_TABLESTRONG)
187 stub_manager_ext_addref(manager, 1, FALSE);
188 else
189 stub_manager_ext_addref(manager, 0, TRUE);
192 /* FIXME: check return value */
193 RPC_RegisterInterface(riid);
195 stdobjref->ipid = ifstub->ipid;
197 stub_manager_int_release(manager);
198 return S_OK;
203 /* Client-side identity of the server object */
205 static HRESULT proxy_manager_get_remunknown(struct proxy_manager * This, IRemUnknown **remunk);
206 static void proxy_manager_destroy(struct proxy_manager * This);
207 static HRESULT proxy_manager_find_ifproxy(struct proxy_manager * This, REFIID riid, struct ifproxy ** ifproxy_found);
208 static HRESULT proxy_manager_query_local_interface(struct proxy_manager * This, REFIID riid, void ** ppv);
210 static HRESULT WINAPI ClientIdentity_QueryInterface(IMultiQI * iface, REFIID riid, void ** ppv)
212 HRESULT hr;
213 MULTI_QI mqi;
215 TRACE("%s\n", debugstr_guid(riid));
217 mqi.pIID = riid;
218 hr = IMultiQI_QueryMultipleInterfaces(iface, 1, &mqi);
219 *ppv = (void *)mqi.pItf;
221 return hr;
224 static ULONG WINAPI ClientIdentity_AddRef(IMultiQI * iface)
226 struct proxy_manager * This = (struct proxy_manager *)iface;
227 TRACE("%p - before %d\n", iface, This->refs);
228 return InterlockedIncrement(&This->refs);
231 static ULONG WINAPI ClientIdentity_Release(IMultiQI * iface)
233 struct proxy_manager * This = (struct proxy_manager *)iface;
234 ULONG refs = InterlockedDecrement(&This->refs);
235 TRACE("%p - after %d\n", iface, refs);
236 if (!refs)
237 proxy_manager_destroy(This);
238 return refs;
241 static HRESULT WINAPI ClientIdentity_QueryMultipleInterfaces(IMultiQI *iface, ULONG cMQIs, MULTI_QI *pMQIs)
243 struct proxy_manager * This = (struct proxy_manager *)iface;
244 REMQIRESULT *qiresults = NULL;
245 ULONG nonlocal_mqis = 0;
246 ULONG i;
247 ULONG successful_mqis = 0;
248 IID *iids = HeapAlloc(GetProcessHeap(), 0, cMQIs * sizeof(*iids));
249 /* mapping of RemQueryInterface index to QueryMultipleInterfaces index */
250 ULONG *mapping = HeapAlloc(GetProcessHeap(), 0, cMQIs * sizeof(*mapping));
252 TRACE("cMQIs: %d\n", cMQIs);
254 /* try to get a local interface - this includes already active proxy
255 * interfaces and also interfaces exposed by the proxy manager */
256 for (i = 0; i < cMQIs; i++)
258 TRACE("iid[%d] = %s\n", i, debugstr_guid(pMQIs[i].pIID));
259 pMQIs[i].hr = proxy_manager_query_local_interface(This, pMQIs[i].pIID, (void **)&pMQIs[i].pItf);
260 if (pMQIs[i].hr == S_OK)
261 successful_mqis++;
262 else
264 iids[nonlocal_mqis] = *pMQIs[i].pIID;
265 mapping[nonlocal_mqis] = i;
266 nonlocal_mqis++;
270 TRACE("%d interfaces not found locally\n", nonlocal_mqis);
272 /* if we have more than one interface not found locally then we must try
273 * to query the remote object for it */
274 if (nonlocal_mqis != 0)
276 IRemUnknown *remunk;
277 HRESULT hr;
278 IPID *ipid;
280 /* get the ipid of the first entry */
281 /* FIXME: should we implement ClientIdentity on the ifproxies instead
282 * of the proxy_manager so we use the correct ipid here? */
283 ipid = &LIST_ENTRY(list_head(&This->interfaces), struct ifproxy, entry)->stdobjref.ipid;
285 /* get IRemUnknown proxy so we can communicate with the remote object */
286 hr = proxy_manager_get_remunknown(This, &remunk);
288 if (SUCCEEDED(hr))
290 hr = IRemUnknown_RemQueryInterface(remunk, ipid, NORMALEXTREFS,
291 nonlocal_mqis, iids, &qiresults);
292 IRemUnknown_Release(remunk);
293 if (FAILED(hr))
294 ERR("IRemUnknown_RemQueryInterface failed with error 0x%08x\n", hr);
297 /* IRemUnknown_RemQueryInterface can return S_FALSE if only some of
298 * the interfaces were returned */
299 if (SUCCEEDED(hr))
301 /* try to unmarshal each object returned to us */
302 for (i = 0; i < nonlocal_mqis; i++)
304 ULONG index = mapping[i];
305 HRESULT hrobj = qiresults[i].hResult;
306 if (hrobj == S_OK)
307 hrobj = unmarshal_object(&qiresults[i].std, COM_CurrentApt(),
308 This->dest_context,
309 This->dest_context_data,
310 pMQIs[index].pIID, &This->oxid_info,
311 (void **)&pMQIs[index].pItf);
313 if (hrobj == S_OK)
314 successful_mqis++;
315 else
316 ERR("Failed to get pointer to interface %s\n", debugstr_guid(pMQIs[index].pIID));
317 pMQIs[index].hr = hrobj;
321 /* free the memory allocated by the proxy */
322 CoTaskMemFree(qiresults);
325 TRACE("%d/%d successfully queried\n", successful_mqis, cMQIs);
327 HeapFree(GetProcessHeap(), 0, iids);
328 HeapFree(GetProcessHeap(), 0, mapping);
330 if (successful_mqis == cMQIs)
331 return S_OK; /* we got all requested interfaces */
332 else if (successful_mqis == 0)
333 return E_NOINTERFACE; /* we didn't get any interfaces */
334 else
335 return S_FALSE; /* we got some interfaces */
338 static const IMultiQIVtbl ClientIdentity_Vtbl =
340 ClientIdentity_QueryInterface,
341 ClientIdentity_AddRef,
342 ClientIdentity_Release,
343 ClientIdentity_QueryMultipleInterfaces
346 /* FIXME: remove these */
347 static HRESULT WINAPI StdMarshalImpl_GetUnmarshalClass(LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext, void* pvDestContext, DWORD mshlflags, CLSID* pCid);
348 static HRESULT WINAPI StdMarshalImpl_GetMarshalSizeMax(LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext, void* pvDestContext, DWORD mshlflags, DWORD* pSize);
349 static HRESULT WINAPI StdMarshalImpl_UnmarshalInterface(LPMARSHAL iface, IStream *pStm, REFIID riid, void **ppv);
350 static HRESULT WINAPI StdMarshalImpl_ReleaseMarshalData(LPMARSHAL iface, IStream *pStm);
351 static HRESULT WINAPI StdMarshalImpl_DisconnectObject(LPMARSHAL iface, DWORD dwReserved);
353 static HRESULT WINAPI Proxy_QueryInterface(IMarshal *iface, REFIID riid, void **ppvObject)
355 ICOM_THIS_MULTI(struct proxy_manager, lpVtblMarshal, iface);
356 return IMultiQI_QueryInterface((IMultiQI *)&This->lpVtbl, riid, ppvObject);
359 static ULONG WINAPI Proxy_AddRef(IMarshal *iface)
361 ICOM_THIS_MULTI(struct proxy_manager, lpVtblMarshal, iface);
362 return IMultiQI_AddRef((IMultiQI *)&This->lpVtbl);
365 static ULONG WINAPI Proxy_Release(IMarshal *iface)
367 ICOM_THIS_MULTI(struct proxy_manager, lpVtblMarshal, iface);
368 return IMultiQI_Release((IMultiQI *)&This->lpVtbl);
371 static HRESULT WINAPI Proxy_MarshalInterface(
372 LPMARSHAL iface, IStream *pStm, REFIID riid, void* pv, DWORD dwDestContext,
373 void* pvDestContext, DWORD mshlflags)
375 ICOM_THIS_MULTI(struct proxy_manager, lpVtblMarshal, iface);
376 HRESULT hr;
377 struct ifproxy *ifproxy;
379 TRACE("(...,%s,...)\n", debugstr_guid(riid));
381 hr = proxy_manager_find_ifproxy(This, riid, &ifproxy);
382 if (SUCCEEDED(hr))
384 STDOBJREF stdobjref = ifproxy->stdobjref;
386 stdobjref.cPublicRefs = 0;
388 if ((mshlflags != MSHLFLAGS_TABLEWEAK) &&
389 (mshlflags != MSHLFLAGS_TABLESTRONG))
391 ULONG cPublicRefs = ifproxy->refs;
392 ULONG cPublicRefsOld;
393 /* optimization - share out proxy's public references if possible
394 * instead of making new proxy do a roundtrip through the server */
397 ULONG cPublicRefsNew;
398 cPublicRefsOld = cPublicRefs;
399 stdobjref.cPublicRefs = cPublicRefs / 2;
400 cPublicRefsNew = cPublicRefs - stdobjref.cPublicRefs;
401 cPublicRefs = InterlockedCompareExchange(
402 (LONG *)&ifproxy->refs, cPublicRefsNew, cPublicRefsOld);
403 } while (cPublicRefs != cPublicRefsOld);
406 /* normal and table-strong marshaling need at least one reference */
407 if (!stdobjref.cPublicRefs && (mshlflags != MSHLFLAGS_TABLEWEAK))
409 IRemUnknown *remunk;
410 hr = proxy_manager_get_remunknown(This, &remunk);
411 if (hr == S_OK)
413 HRESULT hrref = S_OK;
414 REMINTERFACEREF rif;
415 rif.ipid = ifproxy->stdobjref.ipid;
416 rif.cPublicRefs = (mshlflags == MSHLFLAGS_TABLESTRONG) ? 1 : NORMALEXTREFS;
417 rif.cPrivateRefs = 0;
418 hr = IRemUnknown_RemAddRef(remunk, 1, &rif, &hrref);
419 IRemUnknown_Release(remunk);
420 if (hr == S_OK && hrref == S_OK)
422 /* table-strong marshaling doesn't give the refs to the
423 * client that unmarshals the STDOBJREF */
424 if (mshlflags != MSHLFLAGS_TABLESTRONG)
425 stdobjref.cPublicRefs = rif.cPublicRefs;
427 else
428 ERR("IRemUnknown_RemAddRef returned with 0x%08x, hrref = 0x%08x\n", hr, hrref);
432 if (SUCCEEDED(hr))
434 TRACE("writing stdobjref:\n\tflags = %04lx\n\tcPublicRefs = %ld\n\toxid = %s\n\toid = %s\n\tipid = %s\n",
435 stdobjref.flags, stdobjref.cPublicRefs,
436 wine_dbgstr_longlong(stdobjref.oxid),
437 wine_dbgstr_longlong(stdobjref.oid),
438 debugstr_guid(&stdobjref.ipid));
439 hr = IStream_Write(pStm, &stdobjref, sizeof(stdobjref), NULL);
442 else
444 /* we don't have the interface already unmarshaled so we have to
445 * request the object from the server */
446 IRemUnknown *remunk;
447 IPID *ipid;
448 REMQIRESULT *qiresults = NULL;
449 IID iid = *riid;
451 /* get the ipid of the first entry */
452 /* FIXME: should we implement ClientIdentity on the ifproxies instead
453 * of the proxy_manager so we use the correct ipid here? */
454 ipid = &LIST_ENTRY(list_head(&This->interfaces), struct ifproxy, entry)->stdobjref.ipid;
456 /* get IRemUnknown proxy so we can communicate with the remote object */
457 hr = proxy_manager_get_remunknown(This, &remunk);
459 if (hr == S_OK)
461 hr = IRemUnknown_RemQueryInterface(remunk, ipid, NORMALEXTREFS,
462 1, &iid, &qiresults);
463 if (SUCCEEDED(hr))
465 hr = IStream_Write(pStm, &qiresults->std, sizeof(qiresults->std), NULL);
466 if (FAILED(hr))
468 REMINTERFACEREF rif;
469 rif.ipid = qiresults->std.ipid;
470 rif.cPublicRefs = qiresults->std.cPublicRefs;
471 rif.cPrivateRefs = 0;
472 IRemUnknown_RemRelease(remunk, 1, &rif);
474 CoTaskMemFree(qiresults);
476 else
477 ERR("IRemUnknown_RemQueryInterface failed with error 0x%08x\n", hr);
478 IRemUnknown_Release(remunk);
482 return hr;
485 static const IMarshalVtbl ProxyMarshal_Vtbl =
487 Proxy_QueryInterface,
488 Proxy_AddRef,
489 Proxy_Release,
490 StdMarshalImpl_GetUnmarshalClass,
491 StdMarshalImpl_GetMarshalSizeMax,
492 Proxy_MarshalInterface,
493 StdMarshalImpl_UnmarshalInterface,
494 StdMarshalImpl_ReleaseMarshalData,
495 StdMarshalImpl_DisconnectObject
498 static HRESULT WINAPI ProxyCliSec_QueryInterface(IClientSecurity *iface, REFIID riid, void **ppvObject)
500 ICOM_THIS_MULTI(struct proxy_manager, lpVtblCliSec, iface);
501 return IMultiQI_QueryInterface((IMultiQI *)&This->lpVtbl, riid, ppvObject);
504 static ULONG WINAPI ProxyCliSec_AddRef(IClientSecurity *iface)
506 ICOM_THIS_MULTI(struct proxy_manager, lpVtblCliSec, iface);
507 return IMultiQI_AddRef((IMultiQI *)&This->lpVtbl);
510 static ULONG WINAPI ProxyCliSec_Release(IClientSecurity *iface)
512 ICOM_THIS_MULTI(struct proxy_manager, lpVtblCliSec, iface);
513 return IMultiQI_Release((IMultiQI *)&This->lpVtbl);
516 static HRESULT WINAPI ProxyCliSec_QueryBlanket(IClientSecurity *iface,
517 IUnknown *pProxy,
518 DWORD *pAuthnSvc,
519 DWORD *pAuthzSvc,
520 OLECHAR **ppServerPrincName,
521 DWORD *pAuthnLevel,
522 DWORD *pImpLevel,
523 void **pAuthInfo,
524 DWORD *pCapabilities)
526 FIXME("(%p, %p, %p, %p, %p, %p, %p, %p): stub\n", pProxy, pAuthnSvc,
527 pAuthzSvc, ppServerPrincName, pAuthnLevel, pImpLevel, pAuthInfo,
528 pCapabilities);
530 if (pAuthnSvc)
531 *pAuthnSvc = 0;
532 if (pAuthzSvc)
533 *pAuthzSvc = 0;
534 if (ppServerPrincName)
535 *ppServerPrincName = NULL;
536 if (pAuthnLevel)
537 *pAuthnLevel = RPC_C_AUTHN_LEVEL_DEFAULT;
538 if (pImpLevel)
539 *pImpLevel = RPC_C_IMP_LEVEL_DEFAULT;
540 if (pAuthInfo)
541 *pAuthInfo = NULL;
542 if (pCapabilities)
543 *pCapabilities = EOAC_NONE;
545 return E_NOTIMPL;
548 static HRESULT WINAPI ProxyCliSec_SetBlanket(IClientSecurity *iface,
549 IUnknown *pProxy, DWORD AuthnSvc,
550 DWORD AuthzSvc,
551 OLECHAR *pServerPrincName,
552 DWORD AuthnLevel, DWORD ImpLevel,
553 void *pAuthInfo,
554 DWORD Capabilities)
556 FIXME("(%p, %d, %d, %s, %d, %d, %p, 0x%x): stub\n", pProxy, AuthnSvc,
557 AuthzSvc, debugstr_w(pServerPrincName), AuthnLevel, ImpLevel,
558 pAuthInfo, Capabilities);
559 return E_NOTIMPL;
562 static HRESULT WINAPI ProxyCliSec_CopyProxy(IClientSecurity *iface,
563 IUnknown *pProxy, IUnknown **ppCopy)
565 FIXME("(%p, %p): stub\n", pProxy, ppCopy);
566 *ppCopy = NULL;
567 return E_NOTIMPL;
570 static const IClientSecurityVtbl ProxyCliSec_Vtbl =
572 ProxyCliSec_QueryInterface,
573 ProxyCliSec_AddRef,
574 ProxyCliSec_Release,
575 ProxyCliSec_QueryBlanket,
576 ProxyCliSec_SetBlanket,
577 ProxyCliSec_CopyProxy
580 static HRESULT ifproxy_get_public_ref(struct ifproxy * This)
582 HRESULT hr = S_OK;
584 if (WAIT_OBJECT_0 != WaitForSingleObject(This->parent->remoting_mutex, INFINITE))
586 ERR("Wait failed for ifproxy %p\n", This);
587 return E_UNEXPECTED;
590 if (This->refs == 0)
592 IRemUnknown *remunk = NULL;
594 TRACE("getting public ref for ifproxy %p\n", This);
596 hr = proxy_manager_get_remunknown(This->parent, &remunk);
597 if (hr == S_OK)
599 HRESULT hrref = S_OK;
600 REMINTERFACEREF rif;
601 rif.ipid = This->stdobjref.ipid;
602 rif.cPublicRefs = NORMALEXTREFS;
603 rif.cPrivateRefs = 0;
604 hr = IRemUnknown_RemAddRef(remunk, 1, &rif, &hrref);
605 IRemUnknown_Release(remunk);
606 if (hr == S_OK && hrref == S_OK)
607 InterlockedExchangeAdd((LONG *)&This->refs, NORMALEXTREFS);
608 else
609 ERR("IRemUnknown_RemAddRef returned with 0x%08x, hrref = 0x%08x\n", hr, hrref);
612 ReleaseMutex(This->parent->remoting_mutex);
614 return hr;
617 static HRESULT ifproxy_release_public_refs(struct ifproxy * This)
619 HRESULT hr = S_OK;
620 LONG public_refs;
622 if (WAIT_OBJECT_0 != WaitForSingleObject(This->parent->remoting_mutex, INFINITE))
624 ERR("Wait failed for ifproxy %p\n", This);
625 return E_UNEXPECTED;
628 public_refs = This->refs;
629 if (public_refs > 0)
631 IRemUnknown *remunk = NULL;
633 TRACE("releasing %d refs\n", public_refs);
635 hr = proxy_manager_get_remunknown(This->parent, &remunk);
636 if (hr == S_OK)
638 REMINTERFACEREF rif;
639 rif.ipid = This->stdobjref.ipid;
640 rif.cPublicRefs = public_refs;
641 rif.cPrivateRefs = 0;
642 hr = IRemUnknown_RemRelease(remunk, 1, &rif);
643 IRemUnknown_Release(remunk);
644 if (hr == S_OK)
645 InterlockedExchangeAdd((LONG *)&This->refs, -public_refs);
646 else if (hr == RPC_E_DISCONNECTED)
647 WARN("couldn't release references because object was "
648 "disconnected: oxid = %s, oid = %s\n",
649 wine_dbgstr_longlong(This->parent->oxid),
650 wine_dbgstr_longlong(This->parent->oid));
651 else
652 ERR("IRemUnknown_RemRelease failed with error 0x%08x\n", hr);
655 ReleaseMutex(This->parent->remoting_mutex);
657 return hr;
660 /* should be called inside This->parent->cs critical section */
661 static void ifproxy_disconnect(struct ifproxy * This)
663 ifproxy_release_public_refs(This);
664 if (This->proxy) IRpcProxyBuffer_Disconnect(This->proxy);
666 IRpcChannelBuffer_Release(This->chan);
667 This->chan = NULL;
670 /* should be called in This->parent->cs critical section if it is an entry in parent's list */
671 static void ifproxy_destroy(struct ifproxy * This)
673 TRACE("%p\n", This);
675 /* release public references to this object so that the stub can know
676 * when to destroy itself */
677 ifproxy_release_public_refs(This);
679 list_remove(&This->entry);
681 if (This->chan)
683 IRpcChannelBuffer_Release(This->chan);
684 This->chan = NULL;
687 if (This->proxy) IRpcProxyBuffer_Release(This->proxy);
689 HeapFree(GetProcessHeap(), 0, This);
692 static HRESULT proxy_manager_construct(
693 APARTMENT * apt, ULONG sorflags, OXID oxid, OID oid,
694 const OXID_INFO *oxid_info, struct proxy_manager ** proxy_manager)
696 struct proxy_manager * This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
697 if (!This) return E_OUTOFMEMORY;
699 This->remoting_mutex = CreateMutexW(NULL, FALSE, NULL);
700 if (!This->remoting_mutex)
702 HeapFree(GetProcessHeap(), 0, This);
703 return HRESULT_FROM_WIN32(GetLastError());
706 if (oxid_info)
708 This->oxid_info.dwPid = oxid_info->dwPid;
709 This->oxid_info.dwTid = oxid_info->dwTid;
710 This->oxid_info.ipidRemUnknown = oxid_info->ipidRemUnknown;
711 This->oxid_info.dwAuthnHint = oxid_info->dwAuthnHint;
712 This->oxid_info.psa = NULL /* FIXME: copy from oxid_info */;
714 else
716 HRESULT hr = RPC_ResolveOxid(oxid, &This->oxid_info);
717 if (FAILED(hr))
719 CloseHandle(This->remoting_mutex);
720 HeapFree(GetProcessHeap(), 0, This);
721 return hr;
725 This->lpVtbl = &ClientIdentity_Vtbl;
726 This->lpVtblMarshal = &ProxyMarshal_Vtbl;
727 This->lpVtblCliSec = &ProxyCliSec_Vtbl;
729 list_init(&This->entry);
730 list_init(&This->interfaces);
732 InitializeCriticalSection(&This->cs);
733 DEBUG_SET_CRITSEC_NAME(&This->cs, "proxy_manager");
735 /* the apartment the object was unmarshaled into */
736 This->parent = apt;
738 /* the source apartment and id of the object */
739 This->oxid = oxid;
740 This->oid = oid;
742 This->refs = 1;
744 /* the DCOM draft specification states that the SORF_NOPING flag is
745 * proxy manager specific, not ifproxy specific, so this implies that we
746 * should store the STDOBJREF flags here in the proxy manager. */
747 This->sorflags = sorflags;
749 /* we create the IRemUnknown proxy on demand */
750 This->remunk = NULL;
752 /* initialise these values to the weakest values and they will be
753 * overwritten in proxy_manager_set_context */
754 This->dest_context = MSHCTX_INPROC;
755 This->dest_context_data = NULL;
757 EnterCriticalSection(&apt->cs);
758 /* FIXME: we are dependent on the ordering in here to make sure a proxy's
759 * IRemUnknown proxy doesn't get destroyed before the regual proxy does
760 * because we need the IRemUnknown proxy during the destruction of the
761 * regular proxy. Ideally, we should maintain a separate list for the
762 * IRemUnknown proxies that need late destruction */
763 list_add_tail(&apt->proxies, &This->entry);
764 LeaveCriticalSection(&apt->cs);
766 TRACE("%p created for OXID %s, OID %s\n", This,
767 wine_dbgstr_longlong(oxid), wine_dbgstr_longlong(oid));
769 *proxy_manager = This;
770 return S_OK;
773 static inline void proxy_manager_set_context(struct proxy_manager *This, MSHCTX dest_context, void *dest_context_data)
775 MSHCTX old_dest_context = This->dest_context;
776 MSHCTX new_dest_context;
780 new_dest_context = old_dest_context;
781 /* "stronger" values overwrite "weaker" values. stronger values are
782 * ones that disable more optimisations */
783 switch (old_dest_context)
785 case MSHCTX_INPROC:
786 new_dest_context = dest_context;
787 break;
788 case MSHCTX_CROSSCTX:
789 switch (dest_context)
791 case MSHCTX_INPROC:
792 break;
793 default:
794 new_dest_context = dest_context;
796 break;
797 case MSHCTX_LOCAL:
798 switch (dest_context)
800 case MSHCTX_INPROC:
801 case MSHCTX_CROSSCTX:
802 break;
803 default:
804 new_dest_context = dest_context;
806 break;
807 case MSHCTX_NOSHAREDMEM:
808 switch (dest_context)
810 case MSHCTX_DIFFERENTMACHINE:
811 new_dest_context = dest_context;
812 break;
813 default:
814 break;
816 break;
817 default:
818 break;
821 if (old_dest_context == new_dest_context) break;
823 old_dest_context = InterlockedCompareExchange((PLONG)&This->dest_context, new_dest_context, old_dest_context);
824 } while (new_dest_context != old_dest_context);
826 if (dest_context_data)
827 InterlockedExchangePointer(&This->dest_context_data, dest_context_data);
830 static HRESULT proxy_manager_query_local_interface(struct proxy_manager * This, REFIID riid, void ** ppv)
832 HRESULT hr;
833 struct ifproxy * ifproxy;
835 TRACE("%s\n", debugstr_guid(riid));
837 if (IsEqualIID(riid, &IID_IUnknown) ||
838 IsEqualIID(riid, &IID_IMultiQI))
840 *ppv = (void *)&This->lpVtbl;
841 IUnknown_AddRef((IUnknown *)*ppv);
842 return S_OK;
844 if (IsEqualIID(riid, &IID_IMarshal))
846 *ppv = (void *)&This->lpVtblMarshal;
847 IUnknown_AddRef((IUnknown *)*ppv);
848 return S_OK;
850 if (IsEqualIID(riid, &IID_IClientSecurity))
852 *ppv = (void *)&This->lpVtblCliSec;
853 IUnknown_AddRef((IUnknown *)*ppv);
854 return S_OK;
857 hr = proxy_manager_find_ifproxy(This, riid, &ifproxy);
858 if (hr == S_OK)
860 *ppv = ifproxy->iface;
861 IUnknown_AddRef((IUnknown *)*ppv);
862 return S_OK;
865 *ppv = NULL;
866 return E_NOINTERFACE;
869 static HRESULT proxy_manager_create_ifproxy(
870 struct proxy_manager * This, const STDOBJREF *stdobjref, REFIID riid,
871 IRpcChannelBuffer * channel, struct ifproxy ** iif_out)
873 HRESULT hr;
874 IPSFactoryBuffer * psfb;
875 struct ifproxy * ifproxy = HeapAlloc(GetProcessHeap(), 0, sizeof(*ifproxy));
876 if (!ifproxy) return E_OUTOFMEMORY;
878 list_init(&ifproxy->entry);
880 ifproxy->parent = This;
881 ifproxy->stdobjref = *stdobjref;
882 ifproxy->iid = *riid;
883 ifproxy->refs = 0;
884 ifproxy->proxy = NULL;
886 assert(channel);
887 ifproxy->chan = channel; /* FIXME: we should take the binding strings and construct the channel in this function */
889 /* the IUnknown interface is special because it does not have a
890 * proxy associated with the ifproxy as we handle IUnknown ourselves */
891 if (IsEqualIID(riid, &IID_IUnknown))
893 ifproxy->iface = (void *)&This->lpVtbl;
894 IMultiQI_AddRef((IMultiQI *)&This->lpVtbl);
895 hr = S_OK;
897 else
899 hr = get_facbuf_for_iid(riid, &psfb);
900 if (hr == S_OK)
902 /* important note: the outer unknown is set to the proxy manager.
903 * This ensures the COM identity rules are not violated, by having a
904 * one-to-one mapping of objects on the proxy side to objects on the
905 * stub side, no matter which interface you view the object through */
906 hr = IPSFactoryBuffer_CreateProxy(psfb, (IUnknown *)&This->lpVtbl, riid,
907 &ifproxy->proxy, &ifproxy->iface);
908 IPSFactoryBuffer_Release(psfb);
909 if (hr != S_OK)
910 ERR("Could not create proxy for interface %s, error 0x%08x\n",
911 debugstr_guid(riid), hr);
913 else
914 ERR("Could not get IPSFactoryBuffer for interface %s, error 0x%08x\n",
915 debugstr_guid(riid), hr);
917 if (hr == S_OK)
918 hr = IRpcProxyBuffer_Connect(ifproxy->proxy, ifproxy->chan);
921 if (hr == S_OK)
923 EnterCriticalSection(&This->cs);
924 list_add_tail(&This->interfaces, &ifproxy->entry);
925 LeaveCriticalSection(&This->cs);
927 *iif_out = ifproxy;
928 TRACE("ifproxy %p created for IPID %s, interface %s with %lu public refs\n",
929 ifproxy, debugstr_guid(&stdobjref->ipid), debugstr_guid(riid), stdobjref->cPublicRefs);
931 else
932 ifproxy_destroy(ifproxy);
934 return hr;
937 static HRESULT proxy_manager_find_ifproxy(struct proxy_manager * This, REFIID riid, struct ifproxy ** ifproxy_found)
939 HRESULT hr = E_NOINTERFACE; /* assume not found */
940 struct list * cursor;
942 EnterCriticalSection(&This->cs);
943 LIST_FOR_EACH(cursor, &This->interfaces)
945 struct ifproxy * ifproxy = LIST_ENTRY(cursor, struct ifproxy, entry);
946 if (IsEqualIID(riid, &ifproxy->iid))
948 *ifproxy_found = ifproxy;
949 hr = S_OK;
950 break;
953 LeaveCriticalSection(&This->cs);
955 return hr;
958 static void proxy_manager_disconnect(struct proxy_manager * This)
960 struct list * cursor;
962 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This->oxid),
963 wine_dbgstr_longlong(This->oid));
965 EnterCriticalSection(&This->cs);
967 /* SORFP_NOLIFTIMEMGMT proxies (for IRemUnknown) shouldn't be
968 * disconnected - it won't do anything anyway, except cause
969 * problems for other objects that depend on this proxy always
970 * working */
971 if (!(This->sorflags & SORFP_NOLIFETIMEMGMT))
973 LIST_FOR_EACH(cursor, &This->interfaces)
975 struct ifproxy * ifproxy = LIST_ENTRY(cursor, struct ifproxy, entry);
976 ifproxy_disconnect(ifproxy);
980 /* apartment is being destroyed so don't keep a pointer around to it */
981 This->parent = NULL;
983 LeaveCriticalSection(&This->cs);
986 static HRESULT proxy_manager_get_remunknown(struct proxy_manager * This, IRemUnknown **remunk)
988 HRESULT hr = S_OK;
989 struct apartment *apt;
990 BOOL called_in_original_apt;
992 /* we don't want to try and unmarshal or use IRemUnknown if we don't want
993 * lifetime management */
994 if (This->sorflags & SORFP_NOLIFETIMEMGMT)
995 return S_FALSE;
997 apt = COM_CurrentApt();
998 if (!apt)
999 return CO_E_NOTINITIALIZED;
1001 called_in_original_apt = This->parent && (This->parent->oxid == apt->oxid);
1003 EnterCriticalSection(&This->cs);
1004 /* only return the cached object if called from the original apartment.
1005 * in future, we might want to make the IRemUnknown proxy callable from any
1006 * apartment to avoid these checks */
1007 if (This->remunk && called_in_original_apt)
1009 /* already created - return existing object */
1010 *remunk = This->remunk;
1011 IRemUnknown_AddRef(*remunk);
1013 else if (!This->parent)
1014 /* disconnected - we can't create IRemUnknown */
1015 hr = S_FALSE;
1016 else
1018 STDOBJREF stdobjref;
1019 /* Don't want IRemUnknown lifetime management as this is IRemUnknown!
1020 * We also don't care about whether or not the stub is still alive */
1021 stdobjref.flags = SORFP_NOLIFETIMEMGMT | SORF_NOPING;
1022 stdobjref.cPublicRefs = 1;
1023 /* oxid of destination object */
1024 stdobjref.oxid = This->oxid;
1025 /* FIXME: what should be used for the oid? The DCOM draft doesn't say */
1026 stdobjref.oid = (OID)-1;
1027 stdobjref.ipid = This->oxid_info.ipidRemUnknown;
1029 /* do the unmarshal */
1030 hr = unmarshal_object(&stdobjref, COM_CurrentApt(), This->dest_context,
1031 This->dest_context_data, &IID_IRemUnknown,
1032 &This->oxid_info, (void**)remunk);
1033 if (hr == S_OK && called_in_original_apt)
1035 This->remunk = *remunk;
1036 IRemUnknown_AddRef(This->remunk);
1039 LeaveCriticalSection(&This->cs);
1041 TRACE("got IRemUnknown* pointer %p, hr = 0x%08x\n", *remunk, hr);
1043 return hr;
1046 /* destroys a proxy manager, freeing the memory it used.
1047 * Note: this function should not be called from a list iteration in the
1048 * apartment, due to the fact that it removes itself from the apartment and
1049 * it could add a proxy to IRemUnknown into the apartment. */
1050 static void proxy_manager_destroy(struct proxy_manager * This)
1052 struct list * cursor;
1054 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This->oxid),
1055 wine_dbgstr_longlong(This->oid));
1057 if (This->parent)
1059 EnterCriticalSection(&This->parent->cs);
1061 /* remove ourself from the list of proxy objects in the apartment */
1062 LIST_FOR_EACH(cursor, &This->parent->proxies)
1064 if (cursor == &This->entry)
1066 list_remove(&This->entry);
1067 break;
1071 LeaveCriticalSection(&This->parent->cs);
1074 /* destroy all of the interface proxies */
1075 while ((cursor = list_head(&This->interfaces)))
1077 struct ifproxy * ifproxy = LIST_ENTRY(cursor, struct ifproxy, entry);
1078 ifproxy_destroy(ifproxy);
1081 if (This->remunk) IRemUnknown_Release(This->remunk);
1082 CoTaskMemFree(This->oxid_info.psa);
1084 DEBUG_CLEAR_CRITSEC_NAME(&This->cs);
1085 DeleteCriticalSection(&This->cs);
1087 CloseHandle(This->remoting_mutex);
1089 HeapFree(GetProcessHeap(), 0, This);
1092 /* finds the proxy manager corresponding to a given OXID and OID that has
1093 * been unmarshaled in the specified apartment. The caller must release the
1094 * reference to the proxy_manager when the object is no longer used. */
1095 static BOOL find_proxy_manager(APARTMENT * apt, OXID oxid, OID oid, struct proxy_manager ** proxy_found)
1097 BOOL found = FALSE;
1098 struct list * cursor;
1100 EnterCriticalSection(&apt->cs);
1101 LIST_FOR_EACH(cursor, &apt->proxies)
1103 struct proxy_manager * proxy = LIST_ENTRY(cursor, struct proxy_manager, entry);
1104 if ((oxid == proxy->oxid) && (oid == proxy->oid))
1106 /* be careful of a race with ClientIdentity_Release, which would
1107 * cause us to return a proxy which is in the process of being
1108 * destroyed */
1109 if (ClientIdentity_AddRef((IMultiQI *)&proxy->lpVtbl) != 0)
1111 *proxy_found = proxy;
1112 found = TRUE;
1113 break;
1117 LeaveCriticalSection(&apt->cs);
1118 return found;
1121 HRESULT apartment_disconnectproxies(struct apartment *apt)
1123 struct list * cursor;
1125 LIST_FOR_EACH(cursor, &apt->proxies)
1127 struct proxy_manager * proxy = LIST_ENTRY(cursor, struct proxy_manager, entry);
1128 proxy_manager_disconnect(proxy);
1131 return S_OK;
1134 /********************** StdMarshal implementation ****************************/
1135 typedef struct _StdMarshalImpl
1137 const IMarshalVtbl *lpvtbl;
1138 LONG ref;
1140 IID iid;
1141 DWORD dwDestContext;
1142 LPVOID pvDestContext;
1143 DWORD mshlflags;
1144 } StdMarshalImpl;
1146 static HRESULT WINAPI
1147 StdMarshalImpl_QueryInterface(LPMARSHAL iface, REFIID riid, LPVOID *ppv)
1149 *ppv = NULL;
1150 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IMarshal, riid))
1152 *ppv = iface;
1153 IUnknown_AddRef(iface);
1154 return S_OK;
1156 FIXME("No interface for %s.\n", debugstr_guid(riid));
1157 return E_NOINTERFACE;
1160 static ULONG WINAPI
1161 StdMarshalImpl_AddRef(LPMARSHAL iface)
1163 StdMarshalImpl *This = (StdMarshalImpl *)iface;
1164 return InterlockedIncrement(&This->ref);
1167 static ULONG WINAPI
1168 StdMarshalImpl_Release(LPMARSHAL iface)
1170 StdMarshalImpl *This = (StdMarshalImpl *)iface;
1171 ULONG ref = InterlockedDecrement(&This->ref);
1173 if (!ref) HeapFree(GetProcessHeap(),0,This);
1174 return ref;
1177 static HRESULT WINAPI
1178 StdMarshalImpl_GetUnmarshalClass(
1179 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
1180 void* pvDestContext, DWORD mshlflags, CLSID* pCid)
1182 *pCid = CLSID_DfMarshal;
1183 return S_OK;
1186 static HRESULT WINAPI
1187 StdMarshalImpl_GetMarshalSizeMax(
1188 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
1189 void* pvDestContext, DWORD mshlflags, DWORD* pSize)
1191 *pSize = sizeof(STDOBJREF);
1192 return S_OK;
1195 static HRESULT WINAPI
1196 StdMarshalImpl_MarshalInterface(
1197 LPMARSHAL iface, IStream *pStm,REFIID riid, void* pv, DWORD dwDestContext,
1198 void* pvDestContext, DWORD mshlflags)
1200 STDOBJREF stdobjref;
1201 ULONG res;
1202 HRESULT hres;
1203 APARTMENT *apt = COM_CurrentApt();
1205 TRACE("(...,%s,...)\n", debugstr_guid(riid));
1207 if (!apt)
1209 ERR("Apartment not initialized\n");
1210 return CO_E_NOTINITIALIZED;
1213 /* make sure this apartment can be reached from other threads / processes */
1214 RPC_StartRemoting(apt);
1216 hres = marshal_object(apt, &stdobjref, riid, (IUnknown *)pv, mshlflags);
1217 if (hres)
1219 ERR("Failed to create ifstub, hres=0x%x\n", hres);
1220 return hres;
1223 hres = IStream_Write(pStm, &stdobjref, sizeof(stdobjref), &res);
1224 if (hres) return hres;
1226 return S_OK;
1229 /* helper for StdMarshalImpl_UnmarshalInterface - does the unmarshaling with
1230 * no questions asked about the rules surrounding same-apartment unmarshals
1231 * and table marshaling */
1232 static HRESULT unmarshal_object(const STDOBJREF *stdobjref, APARTMENT *apt,
1233 MSHCTX dest_context, void *dest_context_data,
1234 REFIID riid, const OXID_INFO *oxid_info,
1235 void **object)
1237 struct proxy_manager *proxy_manager = NULL;
1238 HRESULT hr = S_OK;
1240 assert(apt);
1242 TRACE("stdobjref:\n\tflags = %04lx\n\tcPublicRefs = %ld\n\toxid = %s\n\toid = %s\n\tipid = %s\n",
1243 stdobjref->flags, stdobjref->cPublicRefs,
1244 wine_dbgstr_longlong(stdobjref->oxid),
1245 wine_dbgstr_longlong(stdobjref->oid),
1246 debugstr_guid(&stdobjref->ipid));
1248 /* create a new proxy manager if one doesn't already exist for the
1249 * object */
1250 if (!find_proxy_manager(apt, stdobjref->oxid, stdobjref->oid, &proxy_manager))
1252 hr = proxy_manager_construct(apt, stdobjref->flags,
1253 stdobjref->oxid, stdobjref->oid, oxid_info,
1254 &proxy_manager);
1256 else
1257 TRACE("proxy manager already created, using\n");
1259 if (hr == S_OK)
1261 struct ifproxy * ifproxy;
1263 proxy_manager_set_context(proxy_manager, dest_context, dest_context_data);
1265 hr = proxy_manager_find_ifproxy(proxy_manager, riid, &ifproxy);
1266 if (hr == E_NOINTERFACE)
1268 IRpcChannelBuffer *chanbuf;
1269 hr = RPC_CreateClientChannel(&stdobjref->oxid, &stdobjref->ipid,
1270 &proxy_manager->oxid_info,
1271 proxy_manager->dest_context,
1272 proxy_manager->dest_context_data,
1273 &chanbuf);
1274 if (hr == S_OK)
1275 hr = proxy_manager_create_ifproxy(proxy_manager, stdobjref,
1276 riid, chanbuf, &ifproxy);
1278 else
1279 IUnknown_AddRef((IUnknown *)ifproxy->iface);
1281 if (hr == S_OK)
1283 InterlockedExchangeAdd((LONG *)&ifproxy->refs, stdobjref->cPublicRefs);
1284 /* get at least one external reference to the object to keep it alive */
1285 hr = ifproxy_get_public_ref(ifproxy);
1286 if (FAILED(hr))
1287 ifproxy_destroy(ifproxy);
1290 if (hr == S_OK)
1291 *object = ifproxy->iface;
1294 /* release our reference to the proxy manager - the client/apartment
1295 * will hold on to the remaining reference for us */
1296 if (proxy_manager) ClientIdentity_Release((IMultiQI*)&proxy_manager->lpVtbl);
1298 return hr;
1301 static HRESULT WINAPI
1302 StdMarshalImpl_UnmarshalInterface(LPMARSHAL iface, IStream *pStm, REFIID riid, void **ppv)
1304 StdMarshalImpl *This = (StdMarshalImpl *)iface;
1305 struct stub_manager *stubmgr = NULL;
1306 STDOBJREF stdobjref;
1307 ULONG res;
1308 HRESULT hres;
1309 APARTMENT *apt = COM_CurrentApt();
1310 APARTMENT *stub_apt;
1311 OXID oxid;
1313 TRACE("(...,%s,....)\n", debugstr_guid(riid));
1315 /* we need an apartment to unmarshal into */
1316 if (!apt)
1318 ERR("Apartment not initialized\n");
1319 return CO_E_NOTINITIALIZED;
1322 /* read STDOBJREF from wire */
1323 hres = IStream_Read(pStm, &stdobjref, sizeof(stdobjref), &res);
1324 if (hres) return STG_E_READFAULT;
1326 hres = apartment_getoxid(apt, &oxid);
1327 if (hres) return hres;
1329 /* check if we're marshalling back to ourselves */
1330 if ((oxid == stdobjref.oxid) && (stubmgr = get_stub_manager(apt, stdobjref.oid)))
1332 TRACE("Unmarshalling object marshalled in same apartment for iid %s, "
1333 "returning original object %p\n", debugstr_guid(riid), stubmgr->object);
1335 hres = IUnknown_QueryInterface(stubmgr->object, riid, ppv);
1337 /* unref the ifstub. FIXME: only do this on success? */
1338 if (!stub_manager_is_table_marshaled(stubmgr, &stdobjref.ipid))
1339 stub_manager_ext_release(stubmgr, stdobjref.cPublicRefs, stdobjref.flags & SORFP_TABLEWEAK, TRUE);
1341 stub_manager_int_release(stubmgr);
1342 return hres;
1345 /* notify stub manager about unmarshal if process-local object.
1346 * note: if the oxid is not found then we and native will quite happily
1347 * ignore table marshaling and normal marshaling rules regarding number of
1348 * unmarshals, etc, but if you abuse these rules then your proxy could end
1349 * up returning RPC_E_DISCONNECTED. */
1350 if ((stub_apt = apartment_findfromoxid(stdobjref.oxid, TRUE)))
1352 if ((stubmgr = get_stub_manager(stub_apt, stdobjref.oid)))
1354 if (!stub_manager_notify_unmarshal(stubmgr, &stdobjref.ipid))
1355 hres = CO_E_OBJNOTCONNECTED;
1357 else
1359 WARN("Couldn't find object for OXID %s, OID %s, assuming disconnected\n",
1360 wine_dbgstr_longlong(stdobjref.oxid),
1361 wine_dbgstr_longlong(stdobjref.oid));
1362 hres = CO_E_OBJNOTCONNECTED;
1365 else
1366 TRACE("Treating unmarshal from OXID %s as inter-process\n",
1367 wine_dbgstr_longlong(stdobjref.oxid));
1369 if (hres == S_OK)
1370 hres = unmarshal_object(&stdobjref, apt, This->dwDestContext,
1371 This->pvDestContext, riid,
1372 stubmgr ? &stubmgr->oxid_info : NULL, ppv);
1374 if (stubmgr) stub_manager_int_release(stubmgr);
1375 if (stub_apt) apartment_release(stub_apt);
1377 if (hres) WARN("Failed with error 0x%08x\n", hres);
1378 else TRACE("Successfully created proxy %p\n", *ppv);
1380 return hres;
1383 static HRESULT WINAPI
1384 StdMarshalImpl_ReleaseMarshalData(LPMARSHAL iface, IStream *pStm)
1386 STDOBJREF stdobjref;
1387 ULONG res;
1388 HRESULT hres;
1389 struct stub_manager *stubmgr;
1390 APARTMENT *apt;
1392 TRACE("iface=%p, pStm=%p\n", iface, pStm);
1394 hres = IStream_Read(pStm, &stdobjref, sizeof(stdobjref), &res);
1395 if (hres) return STG_E_READFAULT;
1397 TRACE("oxid = %s, oid = %s, ipid = %s\n",
1398 wine_dbgstr_longlong(stdobjref.oxid),
1399 wine_dbgstr_longlong(stdobjref.oid),
1400 wine_dbgstr_guid(&stdobjref.ipid));
1402 if (!(apt = apartment_findfromoxid(stdobjref.oxid, TRUE)))
1404 WARN("Could not map OXID %s to apartment object\n",
1405 wine_dbgstr_longlong(stdobjref.oxid));
1406 return RPC_E_INVALID_OBJREF;
1409 if (!(stubmgr = get_stub_manager(apt, stdobjref.oid)))
1411 ERR("could not map object ID to stub manager, oxid=%s, oid=%s\n",
1412 wine_dbgstr_longlong(stdobjref.oxid), wine_dbgstr_longlong(stdobjref.oid));
1413 return RPC_E_INVALID_OBJREF;
1416 stub_manager_release_marshal_data(stubmgr, stdobjref.cPublicRefs, &stdobjref.ipid, stdobjref.flags & SORFP_TABLEWEAK);
1418 stub_manager_int_release(stubmgr);
1419 apartment_release(apt);
1421 return S_OK;
1424 static HRESULT WINAPI
1425 StdMarshalImpl_DisconnectObject(LPMARSHAL iface, DWORD dwReserved)
1427 FIXME("(), stub!\n");
1428 return S_OK;
1431 static const IMarshalVtbl VT_StdMarshal =
1433 StdMarshalImpl_QueryInterface,
1434 StdMarshalImpl_AddRef,
1435 StdMarshalImpl_Release,
1436 StdMarshalImpl_GetUnmarshalClass,
1437 StdMarshalImpl_GetMarshalSizeMax,
1438 StdMarshalImpl_MarshalInterface,
1439 StdMarshalImpl_UnmarshalInterface,
1440 StdMarshalImpl_ReleaseMarshalData,
1441 StdMarshalImpl_DisconnectObject
1444 static HRESULT StdMarshalImpl_Construct(REFIID riid, void** ppvObject)
1446 StdMarshalImpl * pStdMarshal =
1447 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(StdMarshalImpl));
1448 if (!pStdMarshal)
1449 return E_OUTOFMEMORY;
1450 pStdMarshal->lpvtbl = &VT_StdMarshal;
1451 pStdMarshal->ref = 0;
1452 return IMarshal_QueryInterface((IMarshal*)pStdMarshal, riid, ppvObject);
1455 /***********************************************************************
1456 * CoGetStandardMarshal [OLE32.@]
1458 * Gets or creates a standard marshal object.
1460 * PARAMS
1461 * riid [I] Interface identifier of the pUnk object.
1462 * pUnk [I] Optional. Object to get the marshal object for.
1463 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1464 * pvDestContext [I] Reserved. Must be NULL.
1465 * mshlflags [I] Flags affecting the marshaling process.
1466 * ppMarshal [O] Address where marshal object will be stored.
1468 * RETURNS
1469 * Success: S_OK.
1470 * Failure: HRESULT code.
1472 * NOTES
1474 * The function retrieves the IMarshal object associated with an object if
1475 * that object is currently an active stub, otherwise a new marshal object is
1476 * created.
1478 HRESULT WINAPI CoGetStandardMarshal(REFIID riid, IUnknown *pUnk,
1479 DWORD dwDestContext, LPVOID pvDestContext,
1480 DWORD mshlflags, LPMARSHAL *ppMarshal)
1482 StdMarshalImpl *dm;
1484 if (pUnk == NULL)
1486 FIXME("(%s,NULL,%x,%p,%x,%p), unimplemented yet.\n",
1487 debugstr_guid(riid),dwDestContext,pvDestContext,mshlflags,ppMarshal);
1488 return E_NOTIMPL;
1490 TRACE("(%s,%p,%x,%p,%x,%p)\n",
1491 debugstr_guid(riid),pUnk,dwDestContext,pvDestContext,mshlflags,ppMarshal);
1492 *ppMarshal = HeapAlloc(GetProcessHeap(),0,sizeof(StdMarshalImpl));
1493 dm = (StdMarshalImpl*) *ppMarshal;
1494 if (!dm) return E_FAIL;
1495 dm->lpvtbl = &VT_StdMarshal;
1496 dm->ref = 1;
1498 dm->iid = *riid;
1499 dm->dwDestContext = dwDestContext;
1500 dm->pvDestContext = pvDestContext;
1501 dm->mshlflags = mshlflags;
1502 return S_OK;
1505 /***********************************************************************
1506 * get_marshaler [internal]
1508 * Retrieves an IMarshal interface for an object.
1510 static HRESULT get_marshaler(REFIID riid, IUnknown *pUnk, DWORD dwDestContext,
1511 void *pvDestContext, DWORD mshlFlags,
1512 LPMARSHAL *pMarshal)
1514 HRESULT hr;
1516 if (!pUnk)
1517 return E_POINTER;
1518 hr = IUnknown_QueryInterface(pUnk, &IID_IMarshal, (LPVOID*)pMarshal);
1519 if (hr)
1520 hr = CoGetStandardMarshal(riid, pUnk, dwDestContext, pvDestContext,
1521 mshlFlags, pMarshal);
1522 return hr;
1525 /***********************************************************************
1526 * get_unmarshaler_from_stream [internal]
1528 * Creates an IMarshal* object according to the data marshaled to the stream.
1529 * The function leaves the stream pointer at the start of the data written
1530 * to the stream by the IMarshal* object.
1532 static HRESULT get_unmarshaler_from_stream(IStream *stream, IMarshal **marshal, IID *iid)
1534 HRESULT hr;
1535 ULONG res;
1536 OBJREF objref;
1538 /* read common OBJREF header */
1539 hr = IStream_Read(stream, &objref, FIELD_OFFSET(OBJREF, u_objref), &res);
1540 if (hr || (res != FIELD_OFFSET(OBJREF, u_objref)))
1542 ERR("Failed to read common OBJREF header, 0x%08x\n", hr);
1543 return STG_E_READFAULT;
1546 /* sanity check on header */
1547 if (objref.signature != OBJREF_SIGNATURE)
1549 ERR("Bad OBJREF signature 0x%08lx\n", objref.signature);
1550 return RPC_E_INVALID_OBJREF;
1553 if (iid) *iid = objref.iid;
1555 /* FIXME: handler marshaling */
1556 if (objref.flags & OBJREF_STANDARD)
1558 TRACE("Using standard unmarshaling\n");
1559 hr = StdMarshalImpl_Construct(&IID_IMarshal, (LPVOID*)marshal);
1561 else if (objref.flags & OBJREF_CUSTOM)
1563 ULONG custom_header_size = FIELD_OFFSET(OBJREF, u_objref.u_custom.pData) -
1564 FIELD_OFFSET(OBJREF, u_objref.u_custom);
1565 TRACE("Using custom unmarshaling\n");
1566 /* read constant sized OR_CUSTOM data from stream */
1567 hr = IStream_Read(stream, &objref.u_objref.u_custom,
1568 custom_header_size, &res);
1569 if (hr || (res != custom_header_size))
1571 ERR("Failed to read OR_CUSTOM header, 0x%08x\n", hr);
1572 return STG_E_READFAULT;
1574 /* now create the marshaler specified in the stream */
1575 hr = CoCreateInstance(&objref.u_objref.u_custom.clsid, NULL,
1576 CLSCTX_INPROC_SERVER, &IID_IMarshal,
1577 (LPVOID*)marshal);
1579 else
1581 FIXME("Invalid or unimplemented marshaling type specified: %lx\n",
1582 objref.flags);
1583 return RPC_E_INVALID_OBJREF;
1586 if (hr)
1587 ERR("Failed to create marshal, 0x%08x\n", hr);
1589 return hr;
1592 /***********************************************************************
1593 * CoGetMarshalSizeMax [OLE32.@]
1595 * Gets the maximum amount of data that will be needed by a marshal.
1597 * PARAMS
1598 * pulSize [O] Address where maximum marshal size will be stored.
1599 * riid [I] Identifier of the interface to marshal.
1600 * pUnk [I] Pointer to the object to marshal.
1601 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1602 * pvDestContext [I] Reserved. Must be NULL.
1603 * mshlFlags [I] Flags that affect the marshaling. See CoMarshalInterface().
1605 * RETURNS
1606 * Success: S_OK.
1607 * Failure: HRESULT code.
1609 * SEE ALSO
1610 * CoMarshalInterface().
1612 HRESULT WINAPI CoGetMarshalSizeMax(ULONG *pulSize, REFIID riid, IUnknown *pUnk,
1613 DWORD dwDestContext, void *pvDestContext,
1614 DWORD mshlFlags)
1616 HRESULT hr;
1617 LPMARSHAL pMarshal;
1618 CLSID marshaler_clsid;
1620 hr = get_marshaler(riid, pUnk, dwDestContext, pvDestContext, mshlFlags, &pMarshal);
1621 if (hr)
1622 return hr;
1624 hr = IMarshal_GetUnmarshalClass(pMarshal, riid, pUnk, dwDestContext,
1625 pvDestContext, mshlFlags, &marshaler_clsid);
1626 if (hr)
1628 ERR("IMarshal::GetUnmarshalClass failed, 0x%08x\n", hr);
1629 IMarshal_Release(pMarshal);
1630 return hr;
1633 hr = IMarshal_GetMarshalSizeMax(pMarshal, riid, pUnk, dwDestContext,
1634 pvDestContext, mshlFlags, pulSize);
1635 if (IsEqualCLSID(&marshaler_clsid, &CLSID_DfMarshal))
1636 /* add on the size of the common header */
1637 *pulSize += FIELD_OFFSET(OBJREF, u_objref);
1638 else
1639 /* custom marshaling: add on the size of the whole OBJREF structure
1640 * like native does */
1641 *pulSize += sizeof(OBJREF);
1643 IMarshal_Release(pMarshal);
1644 return hr;
1648 static void dump_MSHLFLAGS(MSHLFLAGS flags)
1650 if (flags & MSHLFLAGS_TABLESTRONG)
1651 TRACE(" MSHLFLAGS_TABLESTRONG");
1652 if (flags & MSHLFLAGS_TABLEWEAK)
1653 TRACE(" MSHLFLAGS_TABLEWEAK");
1654 if (!(flags & (MSHLFLAGS_TABLESTRONG|MSHLFLAGS_TABLEWEAK)))
1655 TRACE(" MSHLFLAGS_NORMAL");
1656 if (flags & MSHLFLAGS_NOPING)
1657 TRACE(" MSHLFLAGS_NOPING");
1660 /***********************************************************************
1661 * CoMarshalInterface [OLE32.@]
1663 * Marshals an interface into a stream so that the object can then be
1664 * unmarshaled from another COM apartment and used remotely.
1666 * PARAMS
1667 * pStream [I] Stream the object will be marshaled into.
1668 * riid [I] Identifier of the interface to marshal.
1669 * pUnk [I] Pointer to the object to marshal.
1670 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1671 * pvDestContext [I] Reserved. Must be NULL.
1672 * mshlFlags [I] Flags that affect the marshaling. See notes.
1674 * RETURNS
1675 * Success: S_OK.
1676 * Failure: HRESULT code.
1678 * NOTES
1680 * The mshlFlags parameter can take one or more of the following flags:
1681 *| MSHLFLAGS_NORMAL - Unmarshal once, releases stub on last proxy release.
1682 *| MSHLFLAGS_TABLESTRONG - Unmarshal many, release when CoReleaseMarshalData() called.
1683 *| MSHLFLAGS_TABLEWEAK - Unmarshal many, releases stub on last proxy release.
1684 *| MSHLFLAGS_NOPING - No automatic garbage collection (and so reduces network traffic).
1686 * If a marshaled object is not unmarshaled, then CoReleaseMarshalData() must
1687 * be called in order to release the resources used in the marshaling.
1689 * SEE ALSO
1690 * CoUnmarshalInterface(), CoReleaseMarshalData().
1692 HRESULT WINAPI CoMarshalInterface(IStream *pStream, REFIID riid, IUnknown *pUnk,
1693 DWORD dwDestContext, void *pvDestContext,
1694 DWORD mshlFlags)
1696 HRESULT hr;
1697 CLSID marshaler_clsid;
1698 OBJREF objref;
1699 LPMARSHAL pMarshal;
1701 TRACE("(%p, %s, %p, %x, %p,", pStream, debugstr_guid(riid), pUnk,
1702 dwDestContext, pvDestContext);
1703 dump_MSHLFLAGS(mshlFlags);
1704 TRACE(")\n");
1706 if (!pUnk || !pStream)
1707 return E_INVALIDARG;
1709 objref.signature = OBJREF_SIGNATURE;
1710 objref.iid = *riid;
1712 /* get the marshaler for the specified interface */
1713 hr = get_marshaler(riid, pUnk, dwDestContext, pvDestContext, mshlFlags, &pMarshal);
1714 if (hr)
1716 ERR("Failed to get marshaller, 0x%08x\n", hr);
1717 return hr;
1720 hr = IMarshal_GetUnmarshalClass(pMarshal, riid, pUnk, dwDestContext,
1721 pvDestContext, mshlFlags, &marshaler_clsid);
1722 if (hr)
1724 ERR("IMarshal::GetUnmarshalClass failed, 0x%08x\n", hr);
1725 goto cleanup;
1728 /* FIXME: implement handler marshaling too */
1729 if (IsEqualCLSID(&marshaler_clsid, &CLSID_DfMarshal))
1731 TRACE("Using standard marshaling\n");
1732 objref.flags = OBJREF_STANDARD;
1734 /* write the common OBJREF header to the stream */
1735 hr = IStream_Write(pStream, &objref, FIELD_OFFSET(OBJREF, u_objref), NULL);
1736 if (hr)
1738 ERR("Failed to write OBJREF header to stream, 0x%08x\n", hr);
1739 goto cleanup;
1742 else
1744 TRACE("Using custom marshaling\n");
1745 objref.flags = OBJREF_CUSTOM;
1746 objref.u_objref.u_custom.clsid = marshaler_clsid;
1747 objref.u_objref.u_custom.cbExtension = 0;
1748 objref.u_objref.u_custom.size = 0;
1749 hr = IMarshal_GetMarshalSizeMax(pMarshal, riid, pUnk, dwDestContext,
1750 pvDestContext, mshlFlags,
1751 &objref.u_objref.u_custom.size);
1752 if (hr)
1754 ERR("Failed to get max size of marshal data, error 0x%08x\n", hr);
1755 goto cleanup;
1757 /* write constant sized common header and OR_CUSTOM data into stream */
1758 hr = IStream_Write(pStream, &objref,
1759 FIELD_OFFSET(OBJREF, u_objref.u_custom.pData), NULL);
1760 if (hr)
1762 ERR("Failed to write OR_CUSTOM header to stream with 0x%08x\n", hr);
1763 goto cleanup;
1767 TRACE("Calling IMarshal::MarshalInterace\n");
1768 /* call helper object to do the actual marshaling */
1769 hr = IMarshal_MarshalInterface(pMarshal, pStream, riid, pUnk, dwDestContext,
1770 pvDestContext, mshlFlags);
1772 if (hr)
1774 ERR("Failed to marshal the interface %s, %x\n", debugstr_guid(riid), hr);
1775 goto cleanup;
1778 cleanup:
1779 IMarshal_Release(pMarshal);
1781 TRACE("completed with hr 0x%08x\n", hr);
1783 return hr;
1786 /***********************************************************************
1787 * CoUnmarshalInterface [OLE32.@]
1789 * Unmarshals an object from a stream by creating a proxy to the remote
1790 * object, if necessary.
1792 * PARAMS
1794 * pStream [I] Stream containing the marshaled object.
1795 * riid [I] Interface identifier of the object to create a proxy to.
1796 * ppv [O] Address where proxy will be stored.
1798 * RETURNS
1800 * Success: S_OK.
1801 * Failure: HRESULT code.
1803 * SEE ALSO
1804 * CoMarshalInterface().
1806 HRESULT WINAPI CoUnmarshalInterface(IStream *pStream, REFIID riid, LPVOID *ppv)
1808 HRESULT hr;
1809 LPMARSHAL pMarshal;
1810 IID iid;
1811 IUnknown *object;
1813 TRACE("(%p, %s, %p)\n", pStream, debugstr_guid(riid), ppv);
1815 if (!pStream || !ppv)
1816 return E_INVALIDARG;
1818 hr = get_unmarshaler_from_stream(pStream, &pMarshal, &iid);
1819 if (hr != S_OK)
1820 return hr;
1822 /* call the helper object to do the actual unmarshaling */
1823 hr = IMarshal_UnmarshalInterface(pMarshal, pStream, &iid, (LPVOID*)&object);
1824 if (hr)
1825 ERR("IMarshal::UnmarshalInterface failed, 0x%08x\n", hr);
1827 if (hr == S_OK)
1829 /* IID_NULL means use the interface ID of the marshaled object */
1830 if (!IsEqualIID(riid, &IID_NULL) && !IsEqualIID(riid, &iid))
1832 TRACE("requested interface != marshalled interface, additional QI needed\n");
1833 hr = IUnknown_QueryInterface(object, riid, ppv);
1834 if (hr)
1835 ERR("Couldn't query for interface %s, hr = 0x%08x\n",
1836 debugstr_guid(riid), hr);
1837 IUnknown_Release(object);
1839 else
1841 *ppv = object;
1845 IMarshal_Release(pMarshal);
1847 TRACE("completed with hr 0x%x\n", hr);
1849 return hr;
1852 /***********************************************************************
1853 * CoReleaseMarshalData [OLE32.@]
1855 * Releases resources associated with an object that has been marshaled into
1856 * a stream.
1858 * PARAMS
1860 * pStream [I] The stream that the object has been marshaled into.
1862 * RETURNS
1863 * Success: S_OK.
1864 * Failure: HRESULT error code.
1866 * NOTES
1868 * Call this function to release resources associated with a normal or
1869 * table-weak marshal that will not be unmarshaled, and all table-strong
1870 * marshals when they are no longer needed.
1872 * SEE ALSO
1873 * CoMarshalInterface(), CoUnmarshalInterface().
1875 HRESULT WINAPI CoReleaseMarshalData(IStream *pStream)
1877 HRESULT hr;
1878 LPMARSHAL pMarshal;
1880 TRACE("(%p)\n", pStream);
1882 hr = get_unmarshaler_from_stream(pStream, &pMarshal, NULL);
1883 if (hr != S_OK)
1884 return hr;
1886 /* call the helper object to do the releasing of marshal data */
1887 hr = IMarshal_ReleaseMarshalData(pMarshal, pStream);
1888 if (hr)
1889 ERR("IMarshal::ReleaseMarshalData failed with error 0x%08x\n", hr);
1891 IMarshal_Release(pMarshal);
1892 return hr;
1896 /***********************************************************************
1897 * CoMarshalInterThreadInterfaceInStream [OLE32.@]
1899 * Marshal an interface across threads in the same process.
1901 * PARAMS
1902 * riid [I] Identifier of the interface to be marshalled.
1903 * pUnk [I] Pointer to IUnknown-derived interface that will be marshalled.
1904 * ppStm [O] Pointer to IStream object that is created and then used to store the marshalled interface.
1906 * RETURNS
1907 * Success: S_OK
1908 * Failure: E_OUTOFMEMORY and other COM error codes
1910 * SEE ALSO
1911 * CoMarshalInterface(), CoUnmarshalInterface() and CoGetInterfaceAndReleaseStream()
1913 HRESULT WINAPI CoMarshalInterThreadInterfaceInStream(
1914 REFIID riid, LPUNKNOWN pUnk, LPSTREAM * ppStm)
1916 ULARGE_INTEGER xpos;
1917 LARGE_INTEGER seekto;
1918 HRESULT hres;
1920 TRACE("(%s, %p, %p)\n",debugstr_guid(riid), pUnk, ppStm);
1922 hres = CreateStreamOnHGlobal(NULL, TRUE, ppStm);
1923 if (FAILED(hres)) return hres;
1924 hres = CoMarshalInterface(*ppStm, riid, pUnk, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1926 if (SUCCEEDED(hres))
1928 memset(&seekto, 0, sizeof(seekto));
1929 IStream_Seek(*ppStm, seekto, STREAM_SEEK_SET, &xpos);
1931 else
1933 IStream_Release(*ppStm);
1934 *ppStm = NULL;
1937 return hres;
1940 /***********************************************************************
1941 * CoGetInterfaceAndReleaseStream [OLE32.@]
1943 * Unmarshalls an interface from a stream and then releases the stream.
1945 * PARAMS
1946 * pStm [I] Stream that contains the marshalled interface.
1947 * riid [I] Interface identifier of the object to unmarshall.
1948 * ppv [O] Address of pointer where the requested interface object will be stored.
1950 * RETURNS
1951 * Success: S_OK
1952 * Failure: A COM error code
1954 * SEE ALSO
1955 * CoMarshalInterThreadInterfaceInStream() and CoUnmarshalInterface()
1957 HRESULT WINAPI CoGetInterfaceAndReleaseStream(LPSTREAM pStm, REFIID riid,
1958 LPVOID *ppv)
1960 HRESULT hres;
1962 TRACE("(%p, %s, %p)\n", pStm, debugstr_guid(riid), ppv);
1964 if(!pStm) return E_INVALIDARG;
1965 hres = CoUnmarshalInterface(pStm, riid, ppv);
1966 IStream_Release(pStm);
1967 return hres;
1970 static HRESULT WINAPI StdMarshalCF_QueryInterface(LPCLASSFACTORY iface,
1971 REFIID riid, LPVOID *ppv)
1973 *ppv = NULL;
1974 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory))
1976 *ppv = (LPVOID)iface;
1977 return S_OK;
1979 return E_NOINTERFACE;
1982 static ULONG WINAPI StdMarshalCF_AddRef(LPCLASSFACTORY iface)
1984 return 2; /* non-heap based object */
1987 static ULONG WINAPI StdMarshalCF_Release(LPCLASSFACTORY iface)
1989 return 1; /* non-heap based object */
1992 static HRESULT WINAPI StdMarshalCF_CreateInstance(LPCLASSFACTORY iface,
1993 LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv)
1995 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IMarshal))
1996 return StdMarshalImpl_Construct(riid, ppv);
1998 FIXME("(%s), not supported.\n",debugstr_guid(riid));
1999 return E_NOINTERFACE;
2002 static HRESULT WINAPI StdMarshalCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
2004 FIXME("(%d), stub!\n",fLock);
2005 return S_OK;
2008 static const IClassFactoryVtbl StdMarshalCFVtbl =
2010 StdMarshalCF_QueryInterface,
2011 StdMarshalCF_AddRef,
2012 StdMarshalCF_Release,
2013 StdMarshalCF_CreateInstance,
2014 StdMarshalCF_LockServer
2016 static const IClassFactoryVtbl *StdMarshalCF = &StdMarshalCFVtbl;
2018 HRESULT MARSHAL_GetStandardMarshalCF(LPVOID *ppv)
2020 *ppv = &StdMarshalCF;
2021 return S_OK;
2024 /***********************************************************************
2025 * CoMarshalHresult [OLE32.@]
2027 * Marshals an HRESULT value into a stream.
2029 * PARAMS
2030 * pStm [I] Stream that hresult will be marshalled into.
2031 * hresult [I] HRESULT to be marshalled.
2033 * RETURNS
2034 * Success: S_OK
2035 * Failure: A COM error code
2037 * SEE ALSO
2038 * CoUnmarshalHresult().
2040 HRESULT WINAPI CoMarshalHresult(LPSTREAM pStm, HRESULT hresult)
2042 return IStream_Write(pStm, &hresult, sizeof(hresult), NULL);
2045 /***********************************************************************
2046 * CoUnmarshalHresult [OLE32.@]
2048 * Unmarshals an HRESULT value from a stream.
2050 * PARAMS
2051 * pStm [I] Stream that hresult will be unmarshalled from.
2052 * phresult [I] Pointer to HRESULT where the value will be unmarshalled to.
2054 * RETURNS
2055 * Success: S_OK
2056 * Failure: A COM error code
2058 * SEE ALSO
2059 * CoMarshalHresult().
2061 HRESULT WINAPI CoUnmarshalHresult(LPSTREAM pStm, HRESULT * phresult)
2063 return IStream_Read(pStm, phresult, sizeof(*phresult), NULL);