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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
42 #include "wine/unicode.h"
44 #include "compobj_private.h"
46 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(ole);
50 extern const CLSID CLSID_DfMarshal;
52 /* number of refs given out for normal marshaling */
53 #define NORMALEXTREFS 1 /* FIXME: this should be 5, but we have to wait for IRemUnknown support first */
55 /* private flag indicating that the caller does not want to notify the stub
56 * when the proxy disconnects or is destroyed */
57 #define SORFP_NOLIFETIMEMGMT SORF_OXRES1
59 static HRESULT unmarshal_object(const STDOBJREF *stdobjref, APARTMENT *apt, REFIID riid, void **object);
61 /* Marshalling just passes a unique identifier to the remote client,
62 * that makes it possible to find the passed interface again.
64 * So basically we need a set of values that make it unique.
66 * Note that the IUnknown_QI(ob,xiid,&ppv) always returns the SAME ppv value!
68 * A triple is used: OXID (apt id), OID (stub manager id),
69 * IPID (interface ptr/stub id).
71 * OXIDs identify an apartment and are network scoped
72 * OIDs identify a stub manager and are apartment scoped
73 * IPIDs identify an interface stub and are apartment scoped
76 inline static HRESULT get_facbuf_for_iid(REFIID riid, IPSFactoryBuffer **facbuf)
81 if ((hr = CoGetPSClsid(riid, &clsid)))
83 return CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL,
84 &IID_IPSFactoryBuffer, (LPVOID*)facbuf);
87 /* creates a new stub manager */
88 HRESULT marshal_object(APARTMENT *apt, STDOBJREF *stdobjref, REFIID riid, IUnknown *obj, MSHLFLAGS mshlflags)
90 struct stub_manager *manager;
91 struct ifstub *ifstub;
93 IRpcStubBuffer *stub = NULL;
96 hr = apartment_getoxid(apt, &stdobjref->oxid);
100 /* IUnknown doesn't require a stub buffer, because it never goes out on
102 if (!IsEqualIID(riid, &IID_IUnknown))
104 IPSFactoryBuffer *psfb;
106 hr = get_facbuf_for_iid(riid, &psfb);
109 ERR("couldn't get IPSFactory buffer for interface %s\n", debugstr_guid(riid));
113 hr = IPSFactoryBuffer_CreateStub(psfb, riid, obj, &stub);
114 IPSFactoryBuffer_Release(psfb);
117 ERR("Failed to create an IRpcStubBuffer from IPSFactory for %s\n", debugstr_guid(riid));
121 else /* need to addref object anyway */
122 IUnknown_AddRef(obj);
124 if (mshlflags & MSHLFLAGS_NOPING)
125 stdobjref->flags = SORF_NOPING;
127 stdobjref->flags = SORF_NULL;
129 /* FIXME: what happens if we register an interface twice with different
130 * marshaling flags? */
131 if ((manager = get_stub_manager_from_object(apt, obj)))
132 TRACE("registering new ifstub on pre-existing manager\n");
135 TRACE("constructing new stub manager\n");
137 manager = new_stub_manager(apt, obj, mshlflags);
140 if (stub) IRpcStubBuffer_Release(stub);
141 return E_OUTOFMEMORY;
144 stdobjref->oid = manager->oid;
146 tablemarshal = ((mshlflags & MSHLFLAGS_TABLESTRONG) || (mshlflags & MSHLFLAGS_TABLEWEAK));
148 ifstub = stub_manager_new_ifstub(manager, stub, obj, riid);
151 IRpcStubBuffer_Release(stub);
152 stub_manager_int_release(manager);
153 /* FIXME: should we do another release to completely destroy the
155 return E_OUTOFMEMORY;
160 stdobjref->cPublicRefs = NORMALEXTREFS;
161 stub_manager_ext_addref(manager, stdobjref->cPublicRefs);
165 stdobjref->cPublicRefs = 0;
166 if (mshlflags & MSHLFLAGS_TABLESTRONG)
167 stub_manager_ext_addref(manager, 1);
170 /* FIXME: check return value */
171 RPC_RegisterInterface(riid);
173 stdobjref->ipid = ifstub->ipid;
175 stub_manager_int_release(manager);
181 /* Client-side identity of the server object */
183 static HRESULT proxy_manager_get_remunknown(struct proxy_manager * This, IRemUnknown **remunk);
184 static void proxy_manager_destroy(struct proxy_manager * This);
185 static HRESULT proxy_manager_find_ifproxy(struct proxy_manager * This, REFIID riid, struct ifproxy ** ifproxy_found);
186 static HRESULT proxy_manager_query_local_interface(struct proxy_manager * This, REFIID riid, void ** ppv);
188 static HRESULT WINAPI ClientIdentity_QueryInterface(IMultiQI * iface, REFIID riid, void ** ppv)
193 TRACE("%s\n", debugstr_guid(riid));
196 hr = IMultiQI_QueryMultipleInterfaces(iface, 1, &mqi);
197 *ppv = (void *)mqi.pItf;
202 static ULONG WINAPI ClientIdentity_AddRef(IMultiQI * iface)
204 struct proxy_manager * This = (struct proxy_manager *)iface;
205 TRACE("%p - before %ld\n", iface, This->refs);
206 return InterlockedIncrement(&This->refs);
209 static ULONG WINAPI ClientIdentity_Release(IMultiQI * iface)
211 struct proxy_manager * This = (struct proxy_manager *)iface;
212 ULONG refs = InterlockedDecrement(&This->refs);
213 TRACE("%p - after %ld\n", iface, refs);
215 proxy_manager_destroy(This);
219 static HRESULT WINAPI ClientIdentity_QueryMultipleInterfaces(IMultiQI *iface, ULONG cMQIs, MULTI_QI *pMQIs)
221 struct proxy_manager * This = (struct proxy_manager *)iface;
222 REMQIRESULT *qiresults = NULL;
223 ULONG nonlocal_mqis = 0;
225 ULONG successful_mqis = 0;
226 IID *iids = HeapAlloc(GetProcessHeap(), 0, cMQIs * sizeof(*iids));
227 /* mapping of RemQueryInterface index to QueryMultipleInterfaces index */
228 ULONG *mapping = HeapAlloc(GetProcessHeap(), 0, cMQIs * sizeof(*mapping));
230 TRACE("cMQIs: %ld\n", cMQIs);
232 /* try to get a local interface - this includes already active proxy
233 * interfaces and also interfaces exposed by the proxy manager */
234 for (i = 0; i < cMQIs; i++)
236 TRACE("iid[%ld] = %s\n", i, debugstr_guid(pMQIs[i].pIID));
237 pMQIs[i].hr = proxy_manager_query_local_interface(This, pMQIs[i].pIID, (void **)&pMQIs[i].pItf);
238 if (pMQIs[i].hr == S_OK)
242 iids[nonlocal_mqis] = *pMQIs[i].pIID;
243 mapping[nonlocal_mqis] = i;
248 TRACE("%ld interfaces not found locally\n", nonlocal_mqis);
250 /* if we have more than one interface not found locally then we must try
251 * to query the remote object for it */
252 if (nonlocal_mqis != 0)
258 /* get the ipid of the first entry */
259 /* FIXME: should we implement ClientIdentity on the ifproxies instead
260 * of the proxy_manager so we use the correct ipid here? */
261 ipid = &LIST_ENTRY(list_head(&This->interfaces), struct ifproxy, entry)->ipid;
263 /* get IRemUnknown proxy so we can communicate with the remote object */
264 hr = proxy_manager_get_remunknown(This, &remunk);
268 hr = IRemUnknown_RemQueryInterface(remunk, ipid, NORMALEXTREFS,
269 nonlocal_mqis, iids, &qiresults);
271 ERR("IRemUnknown_RemQueryInterface failed with error 0x%08lx\n", hr);
274 /* IRemUnknown_RemQueryInterface can return S_FALSE if only some of
275 * the interfaces were returned */
278 /* try to unmarshal each object returned to us */
279 for (i = 0; i < nonlocal_mqis; i++)
281 ULONG index = mapping[i];
282 HRESULT hrobj = qiresults[i].hResult;
284 hrobj = unmarshal_object(&qiresults[i].std, This->parent,
286 (void **)&pMQIs[index].pItf);
291 ERR("Failed to get pointer to interface %s\n", debugstr_guid(pMQIs[index].pIID));
292 pMQIs[index].hr = hrobj;
296 /* free the memory allocated by the proxy */
297 CoTaskMemFree(qiresults);
300 TRACE("%ld/%ld successfully queried\n", successful_mqis, cMQIs);
302 HeapFree(GetProcessHeap(), 0, iids);
303 HeapFree(GetProcessHeap(), 0, mapping);
305 if (successful_mqis == cMQIs)
306 return S_OK; /* we got all requested interfaces */
307 else if (successful_mqis == 0)
308 return E_NOINTERFACE; /* we didn't get any interfaces */
310 return S_FALSE; /* we got some interfaces */
313 static const IMultiQIVtbl ClientIdentity_Vtbl =
315 ClientIdentity_QueryInterface,
316 ClientIdentity_AddRef,
317 ClientIdentity_Release,
318 ClientIdentity_QueryMultipleInterfaces
321 static HRESULT ifproxy_get_public_ref(struct ifproxy * This)
325 if (WAIT_OBJECT_0 != WaitForSingleObject(This->parent->remoting_mutex, INFINITE))
327 ERR("Wait failed for ifproxy %p\n", This);
333 IRemUnknown *remunk = NULL;
335 TRACE("getting public ref for ifproxy %p\n", This);
337 hr = proxy_manager_get_remunknown(This->parent, &remunk);
342 rif.ipid = This->ipid;
343 rif.cPublicRefs = NORMALEXTREFS;
344 rif.cPrivateRefs = 0;
345 hr = IRemUnknown_RemAddRef(remunk, 1, &rif, &hrref);
346 if (hr == S_OK && hrref == S_OK)
347 This->refs += NORMALEXTREFS;
349 ERR("IRemUnknown_RemAddRef returned with 0x%08lx, hrref = 0x%08lx\n", hr, hrref);
352 ReleaseMutex(This->parent->remoting_mutex);
357 static HRESULT ifproxy_release_public_refs(struct ifproxy * This)
361 if (WAIT_OBJECT_0 != WaitForSingleObject(This->parent->remoting_mutex, INFINITE))
363 ERR("Wait failed for ifproxy %p\n", This);
369 IRemUnknown *remunk = NULL;
371 TRACE("releasing %ld refs\n", This->refs);
373 hr = proxy_manager_get_remunknown(This->parent, &remunk);
377 rif.ipid = This->ipid;
378 rif.cPublicRefs = This->refs;
379 rif.cPrivateRefs = 0;
380 hr = IRemUnknown_RemRelease(remunk, 1, &rif);
383 else if (hr == RPC_E_DISCONNECTED)
384 WARN("couldn't release references because object was "
385 "disconnected: oxid = %s, oid = %s\n",
386 wine_dbgstr_longlong(This->parent->oxid),
387 wine_dbgstr_longlong(This->parent->oid));
389 ERR("IRemUnknown_RemRelease failed with error 0x%08lx\n", hr);
392 ReleaseMutex(This->parent->remoting_mutex);
397 /* should be called inside This->parent->cs critical section */
398 static void ifproxy_disconnect(struct ifproxy * This)
400 ifproxy_release_public_refs(This);
401 if (This->proxy) IRpcProxyBuffer_Disconnect(This->proxy);
403 IRpcChannelBuffer_Release(This->chan);
407 /* should be called in This->parent->cs critical section if it is an entry in parent's list */
408 static void ifproxy_destroy(struct ifproxy * This)
412 /* release public references to this object so that the stub can know
413 * when to destroy itself */
414 ifproxy_release_public_refs(This);
416 list_remove(&This->entry);
420 IRpcChannelBuffer_Release(This->chan);
424 /* note: we don't call Release for This->proxy because its lifetime is
425 * controlled by the return value from ClientIdentity_Release, which this
426 * function is always called from */
428 HeapFree(GetProcessHeap(), 0, This);
431 static HRESULT proxy_manager_construct(
432 APARTMENT * apt, ULONG sorflags, OXID oxid, OID oid,
433 struct proxy_manager ** proxy_manager)
435 struct proxy_manager * This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
436 if (!This) return E_OUTOFMEMORY;
438 This->remoting_mutex = CreateMutexW(NULL, FALSE, NULL);
439 if (!This->remoting_mutex)
441 HeapFree(GetProcessHeap(), 0, This);
442 return HRESULT_FROM_WIN32(GetLastError());
445 This->lpVtbl = &ClientIdentity_Vtbl;
447 list_init(&This->entry);
448 list_init(&This->interfaces);
450 InitializeCriticalSection(&This->cs);
451 DEBUG_SET_CRITSEC_NAME(&This->cs, "proxy_manager");
453 /* the apartment the object was unmarshaled into */
456 /* the source apartment and id of the object */
462 /* the DCOM draft specification states that the SORF_NOPING flag is
463 * proxy manager specific, not ifproxy specific, so this implies that we
464 * should store the STDOBJREF flags here in the proxy manager. */
465 This->sorflags = sorflags;
467 /* we create the IRemUnknown proxy on demand */
470 EnterCriticalSection(&apt->cs);
471 /* FIXME: we are dependent on the ordering in here to make sure a proxy's
472 * IRemUnknown proxy doesn't get destroyed before the regual proxy does
473 * because we need the IRemUnknown proxy during the destruction of the
474 * regular proxy. Ideally, we should maintain a separate list for the
475 * IRemUnknown proxies that need late destruction */
476 list_add_tail(&apt->proxies, &This->entry);
477 LeaveCriticalSection(&apt->cs);
479 TRACE("%p created for OXID %s, OID %s\n", This,
480 wine_dbgstr_longlong(oxid), wine_dbgstr_longlong(oid));
482 *proxy_manager = This;
486 static HRESULT proxy_manager_query_local_interface(struct proxy_manager * This, REFIID riid, void ** ppv)
489 struct ifproxy * ifproxy;
491 TRACE("%s\n", debugstr_guid(riid));
493 if (IsEqualIID(riid, &IID_IUnknown) ||
494 IsEqualIID(riid, &IID_IMultiQI))
496 *ppv = (void *)&This->lpVtbl;
497 IMultiQI_AddRef((IMultiQI *)&This->lpVtbl);
501 hr = proxy_manager_find_ifproxy(This, riid, &ifproxy);
504 *ppv = ifproxy->iface;
505 IUnknown_AddRef((IUnknown *)*ppv);
510 return E_NOINTERFACE;
513 static HRESULT proxy_manager_create_ifproxy(
514 struct proxy_manager * This, const IPID *ipid, REFIID riid, ULONG cPublicRefs,
515 IRpcChannelBuffer * channel, struct ifproxy ** iif_out)
518 IPSFactoryBuffer * psfb;
519 struct ifproxy * ifproxy = HeapAlloc(GetProcessHeap(), 0, sizeof(*ifproxy));
520 if (!ifproxy) return E_OUTOFMEMORY;
522 list_init(&ifproxy->entry);
524 ifproxy->parent = This;
525 ifproxy->ipid = *ipid;
526 ifproxy->iid = *riid;
527 ifproxy->refs = cPublicRefs;
528 ifproxy->proxy = NULL;
531 ifproxy->chan = channel; /* FIXME: we should take the binding strings and construct the channel in this function */
533 /* the IUnknown interface is special because it does not have a
534 * proxy associated with the ifproxy as we handle IUnknown ourselves */
535 if (IsEqualIID(riid, &IID_IUnknown))
537 ifproxy->iface = (void *)&This->lpVtbl;
542 hr = get_facbuf_for_iid(riid, &psfb);
545 /* important note: the outer unknown is set to the proxy manager.
546 * This ensures the COM identity rules are not violated, by having a
547 * one-to-one mapping of objects on the proxy side to objects on the
548 * stub side, no matter which interface you view the object through */
549 hr = IPSFactoryBuffer_CreateProxy(psfb, (IUnknown *)&This->lpVtbl, riid,
550 &ifproxy->proxy, &ifproxy->iface);
551 IPSFactoryBuffer_Release(psfb);
553 ERR("Could not create proxy for interface %s, error 0x%08lx\n",
554 debugstr_guid(riid), hr);
557 ERR("Could not get IPSFactoryBuffer for interface %s, error 0x%08lx\n",
558 debugstr_guid(riid), hr);
561 hr = IRpcProxyBuffer_Connect(ifproxy->proxy, ifproxy->chan);
564 /* get at least one external reference to the object to keep it alive */
566 hr = ifproxy_get_public_ref(ifproxy);
570 EnterCriticalSection(&This->cs);
571 list_add_tail(&This->interfaces, &ifproxy->entry);
572 LeaveCriticalSection(&This->cs);
575 TRACE("ifproxy %p created for IPID %s, interface %s with %lu public refs\n",
576 ifproxy, debugstr_guid(ipid), debugstr_guid(riid), cPublicRefs);
579 ifproxy_destroy(ifproxy);
584 static HRESULT proxy_manager_find_ifproxy(struct proxy_manager * This, REFIID riid, struct ifproxy ** ifproxy_found)
586 HRESULT hr = E_NOINTERFACE; /* assume not found */
587 struct list * cursor;
589 EnterCriticalSection(&This->cs);
590 LIST_FOR_EACH(cursor, &This->interfaces)
592 struct ifproxy * ifproxy = LIST_ENTRY(cursor, struct ifproxy, entry);
593 if (IsEqualIID(riid, &ifproxy->iid))
595 *ifproxy_found = ifproxy;
600 LeaveCriticalSection(&This->cs);
605 static void proxy_manager_disconnect(struct proxy_manager * This)
607 struct list * cursor;
609 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This->oxid),
610 wine_dbgstr_longlong(This->oid));
612 /* SORFP_NOLIFTIMEMGMT proxies (for IRemUnknown) shouldn't be
613 * disconnected - it won't do anything anyway, except cause
614 * problems for other objects that depend on this proxy always
616 if (This->sorflags & SORFP_NOLIFETIMEMGMT) return;
618 EnterCriticalSection(&This->cs);
620 LIST_FOR_EACH(cursor, &This->interfaces)
622 struct ifproxy * ifproxy = LIST_ENTRY(cursor, struct ifproxy, entry);
623 ifproxy_disconnect(ifproxy);
626 /* apartment is being destroyed so don't keep a pointer around to it */
629 LeaveCriticalSection(&This->cs);
632 static HRESULT proxy_manager_get_remunknown(struct proxy_manager * This, IRemUnknown **remunk)
636 /* we don't want to try and unmarshal or use IRemUnknown if we don't want
637 * lifetime management */
638 if (This->sorflags & SORFP_NOLIFETIMEMGMT)
641 EnterCriticalSection(&This->cs);
643 /* already created - return existing object */
644 *remunk = This->remunk;
645 else if (!This->parent)
646 /* disconnected - we can't create IRemUnknown */
651 /* Don't want IRemUnknown lifetime management as this is IRemUnknown!
652 * We also don't care about whether or not the stub is still alive */
653 stdobjref.flags = SORFP_NOLIFETIMEMGMT | SORF_NOPING;
654 stdobjref.cPublicRefs = 1;
655 /* oxid of destination object */
656 stdobjref.oxid = This->oxid;
657 /* FIXME: what should be used for the oid? The DCOM draft doesn't say */
658 stdobjref.oid = (OID)-1;
659 /* FIXME: this is a hack around not having an OXID resolver yet -
660 * the OXID resolver should give us the IPID of the IRemUnknown
662 stdobjref.ipid.Data1 = 0xffffffff;
663 stdobjref.ipid.Data2 = 0xffff;
664 stdobjref.ipid.Data3 = 0xffff;
665 assert(sizeof(stdobjref.ipid.Data4) == sizeof(stdobjref.oxid));
666 memcpy(&stdobjref.ipid.Data4, &stdobjref.oxid, sizeof(OXID));
668 /* do the unmarshal */
669 hr = unmarshal_object(&stdobjref, This->parent, &IID_IRemUnknown, (void**)&This->remunk);
671 *remunk = This->remunk;
673 LeaveCriticalSection(&This->cs);
675 TRACE("got IRemUnknown* pointer %p, hr = 0x%08lx\n", *remunk, hr);
680 /* destroys a proxy manager, freeing the memory it used.
681 * Note: this function should not be called from a list iteration in the
682 * apartment, due to the fact that it removes itself from the apartment and
683 * it could add a proxy to IRemUnknown into the apartment. */
684 static void proxy_manager_destroy(struct proxy_manager * This)
686 struct list * cursor;
688 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This->oxid),
689 wine_dbgstr_longlong(This->oid));
693 EnterCriticalSection(&This->parent->cs);
695 /* remove ourself from the list of proxy objects in the apartment */
696 LIST_FOR_EACH(cursor, &This->parent->proxies)
698 if (cursor == &This->entry)
700 list_remove(&This->entry);
705 LeaveCriticalSection(&This->parent->cs);
708 /* destroy all of the interface proxies */
709 while ((cursor = list_head(&This->interfaces)))
711 struct ifproxy * ifproxy = LIST_ENTRY(cursor, struct ifproxy, entry);
712 ifproxy_destroy(ifproxy);
715 if (This->remunk) IRemUnknown_Release(This->remunk);
717 DEBUG_CLEAR_CRITSEC_NAME(&This->cs);
718 DeleteCriticalSection(&This->cs);
720 CloseHandle(This->remoting_mutex);
722 HeapFree(GetProcessHeap(), 0, This);
725 /* finds the proxy manager corresponding to a given OXID and OID that has
726 * been unmarshaled in the specified apartment. The caller must release the
727 * reference to the proxy_manager when the object is no longer used. */
728 static BOOL find_proxy_manager(APARTMENT * apt, OXID oxid, OID oid, struct proxy_manager ** proxy_found)
731 struct list * cursor;
733 EnterCriticalSection(&apt->cs);
734 LIST_FOR_EACH(cursor, &apt->proxies)
736 struct proxy_manager * proxy = LIST_ENTRY(cursor, struct proxy_manager, entry);
737 if ((oxid == proxy->oxid) && (oid == proxy->oid))
739 *proxy_found = proxy;
740 ClientIdentity_AddRef((IMultiQI *)&proxy->lpVtbl);
745 LeaveCriticalSection(&apt->cs);
749 HRESULT apartment_disconnectproxies(struct apartment *apt)
751 struct list * cursor;
753 LIST_FOR_EACH(cursor, &apt->proxies)
755 struct proxy_manager * proxy = LIST_ENTRY(cursor, struct proxy_manager, entry);
756 proxy_manager_disconnect(proxy);
762 /********************** StdMarshal implementation ****************************/
763 typedef struct _StdMarshalImpl
765 const IMarshalVtbl *lpvtbl;
770 LPVOID pvDestContext;
774 static HRESULT WINAPI
775 StdMarshalImpl_QueryInterface(LPMARSHAL iface, REFIID riid, LPVOID *ppv)
778 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IMarshal, riid))
781 IUnknown_AddRef(iface);
784 FIXME("No interface for %s.\n", debugstr_guid(riid));
785 return E_NOINTERFACE;
789 StdMarshalImpl_AddRef(LPMARSHAL iface)
791 StdMarshalImpl *This = (StdMarshalImpl *)iface;
792 return InterlockedIncrement(&This->ref);
796 StdMarshalImpl_Release(LPMARSHAL iface)
798 StdMarshalImpl *This = (StdMarshalImpl *)iface;
799 ULONG ref = InterlockedDecrement(&This->ref);
801 if (!ref) HeapFree(GetProcessHeap(),0,This);
805 static HRESULT WINAPI
806 StdMarshalImpl_GetUnmarshalClass(
807 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
808 void* pvDestContext, DWORD mshlflags, CLSID* pCid)
810 *pCid = CLSID_DfMarshal;
814 static HRESULT WINAPI
815 StdMarshalImpl_GetMarshalSizeMax(
816 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
817 void* pvDestContext, DWORD mshlflags, DWORD* pSize)
819 *pSize = sizeof(STDOBJREF);
823 static HRESULT WINAPI
824 StdMarshalImpl_MarshalInterface(
825 LPMARSHAL iface, IStream *pStm,REFIID riid, void* pv, DWORD dwDestContext,
826 void* pvDestContext, DWORD mshlflags)
832 APARTMENT *apt = COM_CurrentApt();
834 TRACE("(...,%s,...)\n", debugstr_guid(riid));
838 ERR("Apartment not initialized\n");
839 return CO_E_NOTINITIALIZED;
842 /* make sure this apartment can be reached from other threads / processes */
843 RPC_StartRemoting(apt);
845 hres = IUnknown_QueryInterface((LPUNKNOWN)pv, riid, (LPVOID*)&pUnk);
848 ERR("object doesn't expose interface %s, failing with error 0x%08lx\n",
849 debugstr_guid(riid), hres);
850 return E_NOINTERFACE;
853 hres = marshal_object(apt, &stdobjref, riid, pUnk, mshlflags);
855 IUnknown_Release(pUnk);
859 ERR("Failed to create ifstub, hres=0x%lx\n", hres);
863 hres = IStream_Write(pStm, &stdobjref, sizeof(stdobjref), &res);
864 if (hres) return hres;
869 /* helper for StdMarshalImpl_UnmarshalInterface - does the unmarshaling with
870 * no questions asked about the rules surrounding same-apartment unmarshals
871 * and table marshaling */
872 static HRESULT unmarshal_object(const STDOBJREF *stdobjref, APARTMENT *apt, REFIID riid, void **object)
874 struct proxy_manager *proxy_manager = NULL;
879 TRACE("stdobjref:\n\tflags = %04lx\n\tcPublicRefs = %ld\n\toxid = %s\n\toid = %s\n\tipid = %s\n",
880 stdobjref->flags, stdobjref->cPublicRefs,
881 wine_dbgstr_longlong(stdobjref->oxid),
882 wine_dbgstr_longlong(stdobjref->oid),
883 debugstr_guid(&stdobjref->ipid));
885 /* create an a new proxy manager if one doesn't already exist for the
887 if (!find_proxy_manager(apt, stdobjref->oxid, stdobjref->oid, &proxy_manager))
889 hr = proxy_manager_construct(apt, stdobjref->flags,
890 stdobjref->oxid, stdobjref->oid,
894 TRACE("proxy manager already created, using\n");
898 struct ifproxy * ifproxy;
899 hr = proxy_manager_find_ifproxy(proxy_manager, riid, &ifproxy);
900 if (hr == E_NOINTERFACE)
902 IRpcChannelBuffer *chanbuf;
903 hr = RPC_CreateClientChannel(&stdobjref->oxid, &stdobjref->ipid, &chanbuf);
905 hr = proxy_manager_create_ifproxy(proxy_manager, &stdobjref->ipid,
906 riid, stdobjref->cPublicRefs,
912 /* FIXME: push this AddRef inside proxy_manager_find_ifproxy/create_ifproxy? */
913 ClientIdentity_AddRef((IMultiQI*)&proxy_manager->lpVtbl);
914 *object = ifproxy->iface;
918 /* release our reference to the proxy manager - the client/apartment
919 * will hold on to the remaining reference for us */
920 if (proxy_manager) ClientIdentity_Release((IMultiQI*)&proxy_manager->lpVtbl);
925 static HRESULT WINAPI
926 StdMarshalImpl_UnmarshalInterface(LPMARSHAL iface, IStream *pStm, REFIID riid, void **ppv)
928 struct stub_manager *stubmgr;
932 APARTMENT *apt = COM_CurrentApt();
936 TRACE("(...,%s,....)\n", debugstr_guid(riid));
938 /* we need an apartment to unmarshal into */
941 ERR("Apartment not initialized\n");
942 return CO_E_NOTINITIALIZED;
945 /* read STDOBJREF from wire */
946 hres = IStream_Read(pStm, &stdobjref, sizeof(stdobjref), &res);
947 if (hres) return hres;
949 hres = apartment_getoxid(apt, &oxid);
950 if (hres) return hres;
952 /* check if we're marshalling back to ourselves */
953 if ((oxid == stdobjref.oxid) && (stubmgr = get_stub_manager(apt, stdobjref.oid)))
955 TRACE("Unmarshalling object marshalled in same apartment for iid %s, "
956 "returning original object %p\n", debugstr_guid(riid), stubmgr->object);
958 hres = IUnknown_QueryInterface(stubmgr->object, riid, ppv);
960 /* unref the ifstub. FIXME: only do this on success? */
961 if (!stub_manager_is_table_marshaled(stubmgr))
962 stub_manager_ext_release(stubmgr, 1);
964 stub_manager_int_release(stubmgr);
968 /* notify stub manager about unmarshal if process-local object.
969 * note: if the oxid is not found then we and native will quite happily
970 * ignore table marshaling and normal marshaling rules regarding number of
971 * unmarshals, etc, but if you abuse these rules then your proxy could end
972 * up returning RPC_E_DISCONNECTED. */
973 if ((stub_apt = apartment_findfromoxid(stdobjref.oxid, TRUE)))
975 if ((stubmgr = get_stub_manager(stub_apt, stdobjref.oid)))
977 if (!stub_manager_notify_unmarshal(stubmgr))
978 hres = CO_E_OBJNOTCONNECTED;
980 stub_manager_int_release(stubmgr);
984 WARN("Couldn't find object for OXID %s, OID %s, assuming disconnected\n",
985 wine_dbgstr_longlong(stdobjref.oxid),
986 wine_dbgstr_longlong(stdobjref.oid));
987 hres = CO_E_OBJNOTCONNECTED;
990 apartment_release(stub_apt);
993 TRACE("Treating unmarshal from OXID %s as inter-process\n",
994 wine_dbgstr_longlong(stdobjref.oxid));
997 hres = unmarshal_object(&stdobjref, apt, riid, ppv);
999 if (hres) WARN("Failed with error 0x%08lx\n", hres);
1000 else TRACE("Successfully created proxy %p\n", *ppv);
1005 static HRESULT WINAPI
1006 StdMarshalImpl_ReleaseMarshalData(LPMARSHAL iface, IStream *pStm)
1008 STDOBJREF stdobjref;
1011 struct stub_manager *stubmgr;
1014 TRACE("iface=%p, pStm=%p\n", iface, pStm);
1016 hres = IStream_Read(pStm, &stdobjref, sizeof(stdobjref), &res);
1017 if (hres) return hres;
1019 TRACE("oxid = %s, oid = %s, ipid = %s\n",
1020 wine_dbgstr_longlong(stdobjref.oxid),
1021 wine_dbgstr_longlong(stdobjref.oid),
1022 wine_dbgstr_guid(&stdobjref.ipid));
1024 if (!(apt = apartment_findfromoxid(stdobjref.oxid, TRUE)))
1026 WARN("Could not map OXID %s to apartment object\n",
1027 wine_dbgstr_longlong(stdobjref.oxid));
1028 return RPC_E_INVALID_OBJREF;
1031 if (!(stubmgr = get_stub_manager(apt, stdobjref.oid)))
1033 ERR("could not map MID to stub manager, oxid=%s, oid=%s\n",
1034 wine_dbgstr_longlong(stdobjref.oxid), wine_dbgstr_longlong(stdobjref.oid));
1035 return RPC_E_INVALID_OBJREF;
1038 stub_manager_release_marshal_data(stubmgr, stdobjref.cPublicRefs);
1040 stub_manager_int_release(stubmgr);
1041 apartment_release(apt);
1046 static HRESULT WINAPI
1047 StdMarshalImpl_DisconnectObject(LPMARSHAL iface, DWORD dwReserved)
1049 FIXME("(), stub!\n");
1053 static const IMarshalVtbl VT_StdMarshal =
1055 StdMarshalImpl_QueryInterface,
1056 StdMarshalImpl_AddRef,
1057 StdMarshalImpl_Release,
1058 StdMarshalImpl_GetUnmarshalClass,
1059 StdMarshalImpl_GetMarshalSizeMax,
1060 StdMarshalImpl_MarshalInterface,
1061 StdMarshalImpl_UnmarshalInterface,
1062 StdMarshalImpl_ReleaseMarshalData,
1063 StdMarshalImpl_DisconnectObject
1066 static HRESULT StdMarshalImpl_Construct(REFIID riid, void** ppvObject)
1068 StdMarshalImpl * pStdMarshal =
1069 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(StdMarshalImpl));
1071 return E_OUTOFMEMORY;
1072 pStdMarshal->lpvtbl = &VT_StdMarshal;
1073 pStdMarshal->ref = 0;
1074 return IMarshal_QueryInterface((IMarshal*)pStdMarshal, riid, ppvObject);
1077 /***********************************************************************
1078 * CoGetStandardMarshal [OLE32.@]
1080 * Gets or creates a standard marshal object.
1083 * riid [I] Interface identifier of the pUnk object.
1084 * pUnk [I] Optional. Object to get the marshal object for.
1085 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1086 * pvDestContext [I] Reserved. Must be NULL.
1087 * mshlflags [I] Flags affecting the marshaling process.
1088 * ppMarshal [O] Address where marshal object will be stored.
1092 * Failure: HRESULT code.
1096 * The function retrieves the IMarshal object associated with an object if
1097 * that object is currently an active stub, otherwise a new marshal object is
1100 HRESULT WINAPI CoGetStandardMarshal(REFIID riid, IUnknown *pUnk,
1101 DWORD dwDestContext, LPVOID pvDestContext,
1102 DWORD mshlflags, LPMARSHAL *ppMarshal)
1108 FIXME("(%s,NULL,%lx,%p,%lx,%p), unimplemented yet.\n",
1109 debugstr_guid(riid),dwDestContext,pvDestContext,mshlflags,ppMarshal);
1112 TRACE("(%s,%p,%lx,%p,%lx,%p)\n",
1113 debugstr_guid(riid),pUnk,dwDestContext,pvDestContext,mshlflags,ppMarshal);
1114 *ppMarshal = HeapAlloc(GetProcessHeap(),0,sizeof(StdMarshalImpl));
1115 dm = (StdMarshalImpl*) *ppMarshal;
1116 if (!dm) return E_FAIL;
1117 dm->lpvtbl = &VT_StdMarshal;
1121 dm->dwDestContext = dwDestContext;
1122 dm->pvDestContext = pvDestContext;
1123 dm->mshlflags = mshlflags;
1127 /***********************************************************************
1128 * get_marshaler [internal]
1130 * Retrieves an IMarshal interface for an object.
1132 static HRESULT get_marshaler(REFIID riid, IUnknown *pUnk, DWORD dwDestContext,
1133 void *pvDestContext, DWORD mshlFlags,
1134 LPMARSHAL *pMarshal)
1140 hr = IUnknown_QueryInterface(pUnk, &IID_IMarshal, (LPVOID*)pMarshal);
1142 hr = CoGetStandardMarshal(riid, pUnk, dwDestContext, pvDestContext,
1143 mshlFlags, pMarshal);
1147 /***********************************************************************
1148 * get_unmarshaler_from_stream [internal]
1150 * Creates an IMarshal* object according to the data marshaled to the stream.
1151 * The function leaves the stream pointer at the start of the data written
1152 * to the stream by the IMarshal* object.
1154 static HRESULT get_unmarshaler_from_stream(IStream *stream, IMarshal **marshal, IID *iid)
1160 /* read common OBJREF header */
1161 hr = IStream_Read(stream, &objref, FIELD_OFFSET(OBJREF, u_objref), &res);
1162 if (hr || (res != FIELD_OFFSET(OBJREF, u_objref)))
1164 ERR("Failed to read common OBJREF header, 0x%08lx\n", hr);
1165 return STG_E_READFAULT;
1168 /* sanity check on header */
1169 if (objref.signature != OBJREF_SIGNATURE)
1171 ERR("Bad OBJREF signature 0x%08lx\n", objref.signature);
1172 return RPC_E_INVALID_OBJREF;
1175 if (iid) *iid = objref.iid;
1177 /* FIXME: handler marshaling */
1178 if (objref.flags & OBJREF_STANDARD)
1180 TRACE("Using standard unmarshaling\n");
1181 hr = StdMarshalImpl_Construct(&IID_IMarshal, (LPVOID*)marshal);
1183 else if (objref.flags & OBJREF_CUSTOM)
1185 ULONG custom_header_size = FIELD_OFFSET(OBJREF, u_objref.u_custom.pData) -
1186 FIELD_OFFSET(OBJREF, u_objref.u_custom);
1187 TRACE("Using custom unmarshaling\n");
1188 /* read constant sized OR_CUSTOM data from stream */
1189 hr = IStream_Read(stream, &objref.u_objref.u_custom,
1190 custom_header_size, &res);
1191 if (hr || (res != custom_header_size))
1193 ERR("Failed to read OR_CUSTOM header, 0x%08lx\n", hr);
1194 return STG_E_READFAULT;
1196 /* now create the marshaler specified in the stream */
1197 hr = CoCreateInstance(&objref.u_objref.u_custom.clsid, NULL,
1198 CLSCTX_INPROC_SERVER, &IID_IMarshal,
1203 FIXME("Invalid or unimplemented marshaling type specified: %lx\n",
1205 return RPC_E_INVALID_OBJREF;
1209 ERR("Failed to create marshal, 0x%08lx\n", hr);
1214 /***********************************************************************
1215 * CoGetMarshalSizeMax [OLE32.@]
1217 * Gets the maximum amount of data that will be needed by a marshal.
1220 * pulSize [O] Address where maximum marshal size will be stored.
1221 * riid [I] Identifier of the interface to marshal.
1222 * pUnk [I] Pointer to the object to marshal.
1223 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1224 * pvDestContext [I] Reserved. Must be NULL.
1225 * mshlFlags [I] Flags that affect the marshaling. See CoMarshalInterface().
1229 * Failure: HRESULT code.
1232 * CoMarshalInterface().
1234 HRESULT WINAPI CoGetMarshalSizeMax(ULONG *pulSize, REFIID riid, IUnknown *pUnk,
1235 DWORD dwDestContext, void *pvDestContext,
1240 CLSID marshaler_clsid;
1242 hr = get_marshaler(riid, pUnk, dwDestContext, pvDestContext, mshlFlags, &pMarshal);
1246 hr = IMarshal_GetUnmarshalClass(pMarshal, riid, pUnk, dwDestContext,
1247 pvDestContext, mshlFlags, &marshaler_clsid);
1250 ERR("IMarshal::GetUnmarshalClass failed, 0x%08lx\n", hr);
1251 IMarshal_Release(pMarshal);
1255 hr = IMarshal_GetMarshalSizeMax(pMarshal, riid, pUnk, dwDestContext,
1256 pvDestContext, mshlFlags, pulSize);
1257 /* add on the size of the common header */
1258 *pulSize += FIELD_OFFSET(OBJREF, u_objref);
1260 /* if custom marshaling, add on size of custom header */
1261 if (!IsEqualCLSID(&marshaler_clsid, &CLSID_DfMarshal))
1262 *pulSize += FIELD_OFFSET(OBJREF, u_objref.u_custom.pData) -
1263 FIELD_OFFSET(OBJREF, u_objref.u_custom);
1265 IMarshal_Release(pMarshal);
1270 /***********************************************************************
1271 * CoMarshalInterface [OLE32.@]
1273 * Marshals an interface into a stream so that the object can then be
1274 * unmarshaled from another COM apartment and used remotely.
1277 * pStream [I] Stream the object will be marshaled into.
1278 * riid [I] Identifier of the interface to marshal.
1279 * pUnk [I] Pointer to the object to marshal.
1280 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1281 * pvDestContext [I] Reserved. Must be NULL.
1282 * mshlFlags [I] Flags that affect the marshaling. See notes.
1286 * Failure: HRESULT code.
1290 * The mshlFlags parameter can take one or more of the following flags:
1291 *| MSHLFLAGS_NORMAL - Unmarshal once, releases stub on last proxy release.
1292 *| MSHLFLAGS_TABLESTRONG - Unmarshal many, release when CoReleaseMarshalData() called.
1293 *| MSHLFLAGS_TABLEWEAK - Unmarshal many, releases stub on last proxy release.
1294 *| MSHLFLAGS_NOPING - No automatic garbage collection (and so reduces network traffic).
1296 * If a marshaled object is not unmarshaled, then CoReleaseMarshalData() must
1297 * be called in order to release the resources used in the marshaling.
1300 * CoUnmarshalInterface(), CoReleaseMarshalData().
1302 HRESULT WINAPI CoMarshalInterface(IStream *pStream, REFIID riid, IUnknown *pUnk,
1303 DWORD dwDestContext, void *pvDestContext,
1307 CLSID marshaler_clsid;
1311 TRACE("(%p, %s, %p, %lx, %p, %lx)\n", pStream, debugstr_guid(riid), pUnk,
1312 dwDestContext, pvDestContext, mshlFlags);
1315 return E_INVALIDARG;
1317 objref.signature = OBJREF_SIGNATURE;
1320 /* get the marshaler for the specified interface */
1321 hr = get_marshaler(riid, pUnk, dwDestContext, pvDestContext, mshlFlags, &pMarshal);
1324 ERR("Failed to get marshaller, 0x%08lx\n", hr);
1328 hr = IMarshal_GetUnmarshalClass(pMarshal, riid, pUnk, dwDestContext,
1329 pvDestContext, mshlFlags, &marshaler_clsid);
1332 ERR("IMarshal::GetUnmarshalClass failed, 0x%08lx\n", hr);
1336 /* FIXME: implement handler marshaling too */
1337 if (IsEqualCLSID(&marshaler_clsid, &CLSID_DfMarshal))
1339 TRACE("Using standard marshaling\n");
1340 objref.flags = OBJREF_STANDARD;
1342 /* write the common OBJREF header to the stream */
1343 hr = IStream_Write(pStream, &objref, FIELD_OFFSET(OBJREF, u_objref), NULL);
1346 ERR("Failed to write OBJREF header to stream, 0x%08lx\n", hr);
1352 TRACE("Using custom marshaling\n");
1353 objref.flags = OBJREF_CUSTOM;
1354 objref.u_objref.u_custom.clsid = marshaler_clsid;
1355 objref.u_objref.u_custom.cbExtension = 0;
1356 objref.u_objref.u_custom.size = 0;
1357 hr = IMarshal_GetMarshalSizeMax(pMarshal, riid, pUnk, dwDestContext,
1358 pvDestContext, mshlFlags,
1359 &objref.u_objref.u_custom.size);
1362 ERR("Failed to get max size of marshal data, error 0x%08lx\n", hr);
1365 /* write constant sized common header and OR_CUSTOM data into stream */
1366 hr = IStream_Write(pStream, &objref,
1367 FIELD_OFFSET(OBJREF, u_objref.u_custom.pData), NULL);
1370 ERR("Failed to write OR_CUSTOM header to stream with 0x%08lx\n", hr);
1375 TRACE("Calling IMarshal::MarshalInterace\n");
1376 /* call helper object to do the actual marshaling */
1377 hr = IMarshal_MarshalInterface(pMarshal, pStream, riid, pUnk, dwDestContext,
1378 pvDestContext, mshlFlags);
1382 ERR("Failed to marshal the interface %s, %lx\n", debugstr_guid(riid), hr);
1387 IMarshal_Release(pMarshal);
1389 TRACE("completed with hr 0x%08lx\n", hr);
1394 /***********************************************************************
1395 * CoUnmarshalInterface [OLE32.@]
1397 * Unmarshals an object from a stream by creating a proxy to the remote
1398 * object, if necessary.
1402 * pStream [I] Stream containing the marshaled object.
1403 * riid [I] Interface identifier of the object to create a proxy to.
1404 * ppv [O] Address where proxy will be stored.
1409 * Failure: HRESULT code.
1412 * CoMarshalInterface().
1414 HRESULT WINAPI CoUnmarshalInterface(IStream *pStream, REFIID riid, LPVOID *ppv)
1421 TRACE("(%p, %s, %p)\n", pStream, debugstr_guid(riid), ppv);
1423 hr = get_unmarshaler_from_stream(pStream, &pMarshal, &iid);
1427 /* call the helper object to do the actual unmarshaling */
1428 hr = IMarshal_UnmarshalInterface(pMarshal, pStream, &iid, (LPVOID*)&object);
1430 ERR("IMarshal::UnmarshalInterface failed, 0x%08lx\n", hr);
1432 /* IID_NULL means use the interface ID of the marshaled object */
1433 if (!IsEqualIID(riid, &IID_NULL))
1438 if (!IsEqualIID(riid, &iid))
1440 TRACE("requested interface != marshalled interface, additional QI needed\n");
1441 hr = IUnknown_QueryInterface(object, &iid, ppv);
1443 ERR("Couldn't query for interface %s, hr = 0x%08lx\n",
1444 debugstr_guid(riid), hr);
1445 IUnknown_Release(object);
1453 IMarshal_Release(pMarshal);
1455 TRACE("completed with hr 0x%lx\n", hr);
1460 /***********************************************************************
1461 * CoReleaseMarshalData [OLE32.@]
1463 * Releases resources associated with an object that has been marshaled into
1468 * pStream [I] The stream that the object has been marshaled into.
1472 * Failure: HRESULT error code.
1476 * Call this function to release resources associated with a normal or
1477 * table-weak marshal that will not be unmarshaled, and all table-strong
1478 * marshals when they are no longer needed.
1481 * CoMarshalInterface(), CoUnmarshalInterface().
1483 HRESULT WINAPI CoReleaseMarshalData(IStream *pStream)
1488 TRACE("(%p)\n", pStream);
1490 hr = get_unmarshaler_from_stream(pStream, &pMarshal, NULL);
1494 /* call the helper object to do the releasing of marshal data */
1495 hr = IMarshal_ReleaseMarshalData(pMarshal, pStream);
1497 ERR("IMarshal::ReleaseMarshalData failed with error 0x%08lx\n", hr);
1499 IMarshal_Release(pMarshal);
1504 /***********************************************************************
1505 * CoMarshalInterThreadInterfaceInStream [OLE32.@]
1507 * Marshal an interface across threads in the same process.
1510 * riid [I] Identifier of the interface to be marshalled.
1511 * pUnk [I] Pointer to IUnknown-derived interface that will be marshalled.
1512 * ppStm [O] Pointer to IStream object that is created and then used to store the marshalled inteface.
1516 * Failure: E_OUTOFMEMORY and other COM error codes
1519 * CoMarshalInterface(), CoUnmarshalInterface() and CoGetInterfaceAndReleaseStream()
1521 HRESULT WINAPI CoMarshalInterThreadInterfaceInStream(
1522 REFIID riid, LPUNKNOWN pUnk, LPSTREAM * ppStm)
1524 ULARGE_INTEGER xpos;
1525 LARGE_INTEGER seekto;
1528 TRACE("(%s, %p, %p)\n",debugstr_guid(riid), pUnk, ppStm);
1530 hres = CreateStreamOnHGlobal(0, TRUE, ppStm);
1531 if (FAILED(hres)) return hres;
1532 hres = CoMarshalInterface(*ppStm, riid, pUnk, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1534 /* FIXME: is this needed? */
1535 memset(&seekto,0,sizeof(seekto));
1536 IStream_Seek(*ppStm,seekto,SEEK_SET,&xpos);
1541 /***********************************************************************
1542 * CoGetInterfaceAndReleaseStream [OLE32.@]
1544 * Unmarshalls an inteface from a stream and then releases the stream.
1547 * pStm [I] Stream that contains the marshalled inteface.
1548 * riid [I] Interface identifier of the object to unmarshall.
1549 * ppv [O] Address of pointer where the requested interface object will be stored.
1553 * Failure: A COM error code
1556 * CoMarshalInterThreadInterfaceInStream() and CoUnmarshalInteface()
1558 HRESULT WINAPI CoGetInterfaceAndReleaseStream(LPSTREAM pStm, REFIID riid,
1563 TRACE("(%p, %s, %p)\n", pStm, debugstr_guid(riid), ppv);
1565 hres = CoUnmarshalInterface(pStm, riid, ppv);
1566 IStream_Release(pStm);
1570 static HRESULT WINAPI StdMarshalCF_QueryInterface(LPCLASSFACTORY iface,
1571 REFIID riid, LPVOID *ppv)
1574 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory))
1576 *ppv = (LPVOID)iface;
1579 return E_NOINTERFACE;
1582 static ULONG WINAPI StdMarshalCF_AddRef(LPCLASSFACTORY iface)
1584 return 2; /* non-heap based object */
1587 static ULONG WINAPI StdMarshalCF_Release(LPCLASSFACTORY iface)
1589 return 1; /* non-heap based object */
1592 static HRESULT WINAPI StdMarshalCF_CreateInstance(LPCLASSFACTORY iface,
1593 LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv)
1595 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IMarshal))
1596 return StdMarshalImpl_Construct(riid, ppv);
1598 FIXME("(%s), not supported.\n",debugstr_guid(riid));
1599 return E_NOINTERFACE;
1602 static HRESULT WINAPI StdMarshalCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
1604 FIXME("(%d), stub!\n",fLock);
1608 static const IClassFactoryVtbl StdMarshalCFVtbl =
1610 StdMarshalCF_QueryInterface,
1611 StdMarshalCF_AddRef,
1612 StdMarshalCF_Release,
1613 StdMarshalCF_CreateInstance,
1614 StdMarshalCF_LockServer
1616 static const IClassFactoryVtbl *StdMarshalCF = &StdMarshalCFVtbl;
1618 HRESULT MARSHAL_GetStandardMarshalCF(LPVOID *ppv)
1620 *ppv = &StdMarshalCF;
1624 /***********************************************************************
1625 * CoMarshalHresult [OLE32.@]
1627 * Marshals an HRESULT value into a stream.
1630 * pStm [I] Stream that hresult will be marshalled into.
1631 * hresult [I] HRESULT to be marshalled.
1635 * Failure: A COM error code
1638 * CoUnmarshalHresult().
1640 HRESULT WINAPI CoMarshalHresult(LPSTREAM pStm, HRESULT hresult)
1642 return IStream_Write(pStm, &hresult, sizeof(hresult), NULL);
1645 /***********************************************************************
1646 * CoUnmarshalHresult [OLE32.@]
1648 * Unmarshals an HRESULT value from a stream.
1651 * pStm [I] Stream that hresult will be unmarshalled from.
1652 * phresult [I] Pointer to HRESULT where the value will be unmarshalled to.
1656 * Failure: A COM error code
1659 * CoMarshalHresult().
1661 HRESULT WINAPI CoUnmarshalHresult(LPSTREAM pStm, HRESULT * phresult)
1663 return IStream_Read(pStm, phresult, sizeof(*phresult), NULL);