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;
94 IPSFactoryBuffer *psfb;
97 hr = apartment_getoxid(apt, &stdobjref->oxid);
101 hr = get_facbuf_for_iid(riid, &psfb);
104 ERR("couldn't get IPSFactory buffer for interface %s\n", debugstr_guid(riid));
108 hr = IPSFactoryBuffer_CreateStub(psfb, riid, obj, &stub);
109 IPSFactoryBuffer_Release(psfb);
112 ERR("Failed to create an IRpcStubBuffer from IPSFactory for %s\n", debugstr_guid(riid));
116 if (mshlflags & MSHLFLAGS_NOPING)
117 stdobjref->flags = SORF_NOPING;
119 stdobjref->flags = SORF_NULL;
121 /* FIXME: what happens if we register an interface twice with different
122 * marshaling flags? */
123 if ((manager = get_stub_manager_from_object(apt, obj)))
124 TRACE("registering new ifstub on pre-existing manager\n");
127 TRACE("constructing new stub manager\n");
129 manager = new_stub_manager(apt, obj, mshlflags);
132 IRpcStubBuffer_Release(stub);
133 return E_OUTOFMEMORY;
136 stdobjref->oid = manager->oid;
138 tablemarshal = ((mshlflags & MSHLFLAGS_TABLESTRONG) || (mshlflags & MSHLFLAGS_TABLEWEAK));
140 ifstub = stub_manager_new_ifstub(manager, stub, obj, riid);
143 IRpcStubBuffer_Release(stub);
144 stub_manager_int_release(manager);
145 /* FIXME: should we do another release to completely destroy the
147 return E_OUTOFMEMORY;
152 stdobjref->cPublicRefs = NORMALEXTREFS;
153 stub_manager_ext_addref(manager, stdobjref->cPublicRefs);
157 stdobjref->cPublicRefs = 0;
158 if (mshlflags & MSHLFLAGS_TABLESTRONG)
159 stub_manager_ext_addref(manager, 1);
162 /* FIXME: check return value */
163 RPC_RegisterInterface(riid);
165 stdobjref->ipid = ifstub->ipid;
167 stub_manager_int_release(manager);
173 /* Client-side identity of the server object */
175 static HRESULT proxy_manager_get_remunknown(struct proxy_manager * This, IRemUnknown **remunk);
176 static void proxy_manager_destroy(struct proxy_manager * This);
177 static HRESULT proxy_manager_find_ifproxy(struct proxy_manager * This, REFIID riid, struct ifproxy ** ifproxy_found);
178 static HRESULT proxy_manager_query_local_interface(struct proxy_manager * This, REFIID riid, void ** ppv);
180 static HRESULT WINAPI ClientIdentity_QueryInterface(IMultiQI * iface, REFIID riid, void ** ppv)
185 TRACE("%s\n", debugstr_guid(riid));
188 hr = IMultiQI_QueryMultipleInterfaces(iface, 1, &mqi);
189 *ppv = (void *)mqi.pItf;
194 static ULONG WINAPI ClientIdentity_AddRef(IMultiQI * iface)
196 struct proxy_manager * This = (struct proxy_manager *)iface;
197 TRACE("%p - before %ld\n", iface, This->refs);
198 return InterlockedIncrement(&This->refs);
201 static ULONG WINAPI ClientIdentity_Release(IMultiQI * iface)
203 struct proxy_manager * This = (struct proxy_manager *)iface;
204 ULONG refs = InterlockedDecrement(&This->refs);
205 TRACE("%p - after %ld\n", iface, refs);
207 proxy_manager_destroy(This);
211 static HRESULT WINAPI ClientIdentity_QueryMultipleInterfaces(IMultiQI *iface, ULONG cMQIs, MULTI_QI *pMQIs)
213 struct proxy_manager * This = (struct proxy_manager *)iface;
214 REMQIRESULT *qiresults = NULL;
215 ULONG nonlocal_mqis = 0;
217 ULONG successful_mqis = 0;
218 IID *iids = HeapAlloc(GetProcessHeap(), 0, cMQIs * sizeof(*iids));
219 /* mapping of RemQueryInterface index to QueryMultipleInterfaces index */
220 ULONG *mapping = HeapAlloc(GetProcessHeap(), 0, cMQIs * sizeof(*mapping));
222 TRACE("cMQIs: %ld\n", cMQIs);
224 /* try to get a local interface - this includes already active proxy
225 * interfaces and also interfaces exposed by the proxy manager */
226 for (i = 0; i < cMQIs; i++)
228 TRACE("iid[%ld] = %s\n", i, debugstr_guid(pMQIs[i].pIID));
229 pMQIs[i].hr = proxy_manager_query_local_interface(This, pMQIs[i].pIID, (void **)&pMQIs[i].pItf);
230 if (pMQIs[i].hr == S_OK)
234 iids[nonlocal_mqis] = *pMQIs[i].pIID;
235 mapping[nonlocal_mqis] = i;
240 TRACE("%ld interfaces not found locally\n", nonlocal_mqis);
242 /* if we have more than one interface not found locally then we must try
243 * to query the remote object for it */
244 if (nonlocal_mqis != 0)
250 /* get the ipid of the first entry */
251 /* FIXME: should we implement ClientIdentity on the ifproxies instead
252 * of the proxy_manager so we use the correct ipid here? */
253 ipid = &LIST_ENTRY(list_head(&This->interfaces), struct ifproxy, entry)->ipid;
255 /* get IRemUnknown proxy so we can communicate with the remote object */
256 hr = proxy_manager_get_remunknown(This, &remunk);
260 hr = IRemUnknown_RemQueryInterface(remunk, ipid, NORMALEXTREFS,
261 nonlocal_mqis, iids, &qiresults);
263 ERR("IRemUnknown_RemQueryInterface failed with error 0x%08lx\n", hr);
266 /* IRemUnknown_RemQueryInterface can return S_FALSE if only some of
267 * the interfaces were returned */
270 /* try to unmarshal each object returned to us */
271 for (i = 0; i < nonlocal_mqis; i++)
273 ULONG index = mapping[i];
274 HRESULT hrobj = qiresults[i].hResult;
276 hrobj = unmarshal_object(&qiresults[i].std, This->parent,
278 (void **)&pMQIs[index].pItf);
283 ERR("Failed to get pointer to interface %s\n", debugstr_guid(pMQIs[index].pIID));
284 pMQIs[index].hr = hrobj;
288 /* free the memory allocated by the proxy */
289 CoTaskMemFree(qiresults);
292 TRACE("%ld/%ld successfully queried\n", successful_mqis, cMQIs);
294 HeapFree(GetProcessHeap(), 0, iids);
295 HeapFree(GetProcessHeap(), 0, mapping);
297 if (successful_mqis == cMQIs)
298 return S_OK; /* we got all requested interfaces */
299 else if (successful_mqis == 0)
300 return E_NOINTERFACE; /* we didn't get any interfaces */
302 return S_FALSE; /* we got some interfaces */
305 static const IMultiQIVtbl ClientIdentity_Vtbl =
307 ClientIdentity_QueryInterface,
308 ClientIdentity_AddRef,
309 ClientIdentity_Release,
310 ClientIdentity_QueryMultipleInterfaces
313 static HRESULT ifproxy_get_public_ref(struct ifproxy * This)
317 if (WAIT_OBJECT_0 != WaitForSingleObject(This->parent->remoting_mutex, INFINITE))
319 ERR("Wait failed for ifproxy %p\n", This);
325 IRemUnknown *remunk = NULL;
327 TRACE("getting public ref for ifproxy %p\n", This);
329 hr = proxy_manager_get_remunknown(This->parent, &remunk);
334 rif.ipid = This->ipid;
335 rif.cPublicRefs = NORMALEXTREFS;
336 rif.cPrivateRefs = 0;
337 hr = IRemUnknown_RemAddRef(remunk, 1, &rif, &hrref);
338 if (hr == S_OK && hrref == S_OK)
339 This->refs += NORMALEXTREFS;
341 ERR("IRemUnknown_RemAddRef returned with 0x%08lx, hrref = 0x%08lx\n", hr, hrref);
344 ReleaseMutex(This->parent->remoting_mutex);
349 static HRESULT ifproxy_release_public_refs(struct ifproxy * This)
353 if (WAIT_OBJECT_0 != WaitForSingleObject(This->parent->remoting_mutex, INFINITE))
355 ERR("Wait failed for ifproxy %p\n", This);
361 IRemUnknown *remunk = NULL;
363 TRACE("releasing %ld refs\n", This->refs);
365 hr = proxy_manager_get_remunknown(This->parent, &remunk);
369 rif.ipid = This->ipid;
370 rif.cPublicRefs = This->refs;
371 rif.cPrivateRefs = 0;
372 hr = IRemUnknown_RemRelease(remunk, 1, &rif);
375 else if (hr == RPC_E_DISCONNECTED)
376 WARN("couldn't release references because object was "
377 "disconnected: oxid = %s, oid = %s\n",
378 wine_dbgstr_longlong(This->parent->oxid),
379 wine_dbgstr_longlong(This->parent->oid));
381 ERR("IRemUnknown_RemRelease failed with error 0x%08lx\n", hr);
384 ReleaseMutex(This->parent->remoting_mutex);
389 /* should be called inside This->parent->cs critical section */
390 static void ifproxy_disconnect(struct ifproxy * This)
392 ifproxy_release_public_refs(This);
393 if (This->proxy) IRpcProxyBuffer_Disconnect(This->proxy);
395 IRpcChannelBuffer_Release(This->chan);
399 /* should be called in This->parent->cs critical section if it is an entry in parent's list */
400 static void ifproxy_destroy(struct ifproxy * This)
404 /* release public references to this object so that the stub can know
405 * when to destroy itself */
406 ifproxy_release_public_refs(This);
408 list_remove(&This->entry);
412 IRpcChannelBuffer_Release(This->chan);
416 /* note: we don't call Release for This->proxy because its lifetime is
417 * controlled by the return value from ClientIdentity_Release, which this
418 * function is always called from */
420 HeapFree(GetProcessHeap(), 0, This);
423 static HRESULT proxy_manager_construct(
424 APARTMENT * apt, ULONG sorflags, OXID oxid, OID oid,
425 struct proxy_manager ** proxy_manager)
427 struct proxy_manager * This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
428 if (!This) return E_OUTOFMEMORY;
430 This->remoting_mutex = CreateMutexW(NULL, FALSE, NULL);
431 if (!This->remoting_mutex)
433 HeapFree(GetProcessHeap(), 0, This);
434 return HRESULT_FROM_WIN32(GetLastError());
437 This->lpVtbl = &ClientIdentity_Vtbl;
439 list_init(&This->entry);
440 list_init(&This->interfaces);
442 InitializeCriticalSection(&This->cs);
443 DEBUG_SET_CRITSEC_NAME(&This->cs, "proxy_manager");
445 /* the apartment the object was unmarshaled into */
448 /* the source apartment and id of the object */
454 /* the DCOM draft specification states that the SORF_NOPING flag is
455 * proxy manager specific, not ifproxy specific, so this implies that we
456 * should store the STDOBJREF flags here in the proxy manager. */
457 This->sorflags = sorflags;
459 /* we create the IRemUnknown proxy on demand */
462 EnterCriticalSection(&apt->cs);
463 /* FIXME: we are dependent on the ordering in here to make sure a proxy's
464 * IRemUnknown proxy doesn't get destroyed before the regual proxy does
465 * because we need the IRemUnknown proxy during the destruction of the
466 * regular proxy. Ideally, we should maintain a separate list for the
467 * IRemUnknown proxies that need late destruction */
468 list_add_tail(&apt->proxies, &This->entry);
469 LeaveCriticalSection(&apt->cs);
471 TRACE("%p created for OXID %s, OID %s\n", This,
472 wine_dbgstr_longlong(oxid), wine_dbgstr_longlong(oid));
474 *proxy_manager = This;
478 static HRESULT proxy_manager_query_local_interface(struct proxy_manager * This, REFIID riid, void ** ppv)
481 struct ifproxy * ifproxy;
483 TRACE("%s\n", debugstr_guid(riid));
485 if (IsEqualIID(riid, &IID_IUnknown) ||
486 IsEqualIID(riid, &IID_IMultiQI))
488 *ppv = (void *)&This->lpVtbl;
489 IMultiQI_AddRef((IMultiQI *)&This->lpVtbl);
493 hr = proxy_manager_find_ifproxy(This, riid, &ifproxy);
496 *ppv = ifproxy->iface;
497 IUnknown_AddRef((IUnknown *)*ppv);
502 return E_NOINTERFACE;
505 static HRESULT proxy_manager_create_ifproxy(
506 struct proxy_manager * This, const IPID *ipid, REFIID riid, ULONG cPublicRefs,
507 IRpcChannelBuffer * channel, struct ifproxy ** iif_out)
510 IPSFactoryBuffer * psfb;
511 struct ifproxy * ifproxy = HeapAlloc(GetProcessHeap(), 0, sizeof(*ifproxy));
512 if (!ifproxy) return E_OUTOFMEMORY;
514 list_init(&ifproxy->entry);
516 ifproxy->parent = This;
517 ifproxy->ipid = *ipid;
518 ifproxy->iid = *riid;
519 ifproxy->refs = cPublicRefs;
520 ifproxy->proxy = NULL;
523 ifproxy->chan = channel; /* FIXME: we should take the binding strings and construct the channel in this function */
525 /* the IUnknown interface is special because it does not have a
526 * proxy associated with the ifproxy as we handle IUnknown ourselves */
527 if (IsEqualIID(riid, &IID_IUnknown))
529 ifproxy->iface = (void *)&This->lpVtbl;
534 hr = get_facbuf_for_iid(riid, &psfb);
537 /* important note: the outer unknown is set to the proxy manager.
538 * This ensures the COM identity rules are not violated, by having a
539 * one-to-one mapping of objects on the proxy side to objects on the
540 * stub side, no matter which interface you view the object through */
541 hr = IPSFactoryBuffer_CreateProxy(psfb, (IUnknown *)&This->lpVtbl, riid,
542 &ifproxy->proxy, &ifproxy->iface);
543 IPSFactoryBuffer_Release(psfb);
545 ERR("Could not create proxy for interface %s, error 0x%08lx\n",
546 debugstr_guid(riid), hr);
549 ERR("Could not get IPSFactoryBuffer for interface %s, error 0x%08lx\n",
550 debugstr_guid(riid), hr);
553 hr = IRpcProxyBuffer_Connect(ifproxy->proxy, ifproxy->chan);
556 /* get at least one external reference to the object to keep it alive */
558 hr = ifproxy_get_public_ref(ifproxy);
562 EnterCriticalSection(&This->cs);
563 list_add_tail(&This->interfaces, &ifproxy->entry);
564 LeaveCriticalSection(&This->cs);
567 TRACE("ifproxy %p created for IPID %s, interface %s with %lu public refs\n",
568 ifproxy, debugstr_guid(ipid), debugstr_guid(riid), cPublicRefs);
571 ifproxy_destroy(ifproxy);
576 static HRESULT proxy_manager_find_ifproxy(struct proxy_manager * This, REFIID riid, struct ifproxy ** ifproxy_found)
578 HRESULT hr = E_NOINTERFACE; /* assume not found */
579 struct list * cursor;
581 EnterCriticalSection(&This->cs);
582 LIST_FOR_EACH(cursor, &This->interfaces)
584 struct ifproxy * ifproxy = LIST_ENTRY(cursor, struct ifproxy, entry);
585 if (IsEqualIID(riid, &ifproxy->iid))
587 *ifproxy_found = ifproxy;
592 LeaveCriticalSection(&This->cs);
597 static void proxy_manager_disconnect(struct proxy_manager * This)
599 struct list * cursor;
601 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This->oxid),
602 wine_dbgstr_longlong(This->oid));
604 /* SORFP_NOLIFTIMEMGMT proxies (for IRemUnknown) shouldn't be
605 * disconnected - it won't do anything anyway, except cause
606 * problems for other objects that depend on this proxy always
608 if (This->sorflags & SORFP_NOLIFETIMEMGMT) return;
610 EnterCriticalSection(&This->cs);
612 LIST_FOR_EACH(cursor, &This->interfaces)
614 struct ifproxy * ifproxy = LIST_ENTRY(cursor, struct ifproxy, entry);
615 ifproxy_disconnect(ifproxy);
618 /* apartment is being destroyed so don't keep a pointer around to it */
621 LeaveCriticalSection(&This->cs);
624 static HRESULT proxy_manager_get_remunknown(struct proxy_manager * This, IRemUnknown **remunk)
628 /* we don't want to try and unmarshal or use IRemUnknown if we don't want
629 * lifetime management */
630 if (This->sorflags & SORFP_NOLIFETIMEMGMT)
633 EnterCriticalSection(&This->cs);
635 /* already created - return existing object */
636 *remunk = This->remunk;
637 else if (!This->parent)
638 /* disconnected - we can't create IRemUnknown */
643 /* Don't want IRemUnknown lifetime management as this is IRemUnknown!
644 * We also don't care about whether or not the stub is still alive */
645 stdobjref.flags = SORFP_NOLIFETIMEMGMT | SORF_NOPING;
646 stdobjref.cPublicRefs = 1;
647 /* oxid of destination object */
648 stdobjref.oxid = This->oxid;
649 /* FIXME: what should be used for the oid? The DCOM draft doesn't say */
650 stdobjref.oid = (OID)-1;
651 /* FIXME: this is a hack around not having an OXID resolver yet -
652 * the OXID resolver should give us the IPID of the IRemUnknown
654 stdobjref.ipid.Data1 = 0xffffffff;
655 stdobjref.ipid.Data2 = 0xffff;
656 stdobjref.ipid.Data3 = 0xffff;
657 assert(sizeof(stdobjref.ipid.Data4) == sizeof(stdobjref.oxid));
658 memcpy(&stdobjref.ipid.Data4, &stdobjref.oxid, sizeof(OXID));
660 /* do the unmarshal */
661 hr = unmarshal_object(&stdobjref, This->parent, &IID_IRemUnknown, (void**)&This->remunk);
663 *remunk = This->remunk;
665 LeaveCriticalSection(&This->cs);
667 TRACE("got IRemUnknown* pointer %p, hr = 0x%08lx\n", *remunk, hr);
672 /* destroys a proxy manager, freeing the memory it used.
673 * Note: this function should not be called from a list iteration in the
674 * apartment, due to the fact that it removes itself from the apartment and
675 * it could add a proxy to IRemUnknown into the apartment. */
676 static void proxy_manager_destroy(struct proxy_manager * This)
678 struct list * cursor;
680 TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This->oxid),
681 wine_dbgstr_longlong(This->oid));
685 EnterCriticalSection(&This->parent->cs);
687 /* remove ourself from the list of proxy objects in the apartment */
688 LIST_FOR_EACH(cursor, &This->parent->proxies)
690 if (cursor == &This->entry)
692 list_remove(&This->entry);
697 LeaveCriticalSection(&This->parent->cs);
700 /* destroy all of the interface proxies */
701 while ((cursor = list_head(&This->interfaces)))
703 struct ifproxy * ifproxy = LIST_ENTRY(cursor, struct ifproxy, entry);
704 ifproxy_destroy(ifproxy);
707 if (This->remunk) IRemUnknown_Release(This->remunk);
709 DEBUG_CLEAR_CRITSEC_NAME(&This->cs);
710 DeleteCriticalSection(&This->cs);
712 CloseHandle(This->remoting_mutex);
714 HeapFree(GetProcessHeap(), 0, This);
717 /* finds the proxy manager corresponding to a given OXID and OID that has
718 * been unmarshaled in the specified apartment. The caller must release the
719 * reference to the proxy_manager when the object is no longer used. */
720 static BOOL find_proxy_manager(APARTMENT * apt, OXID oxid, OID oid, struct proxy_manager ** proxy_found)
723 struct list * cursor;
725 EnterCriticalSection(&apt->cs);
726 LIST_FOR_EACH(cursor, &apt->proxies)
728 struct proxy_manager * proxy = LIST_ENTRY(cursor, struct proxy_manager, entry);
729 if ((oxid == proxy->oxid) && (oid == proxy->oid))
731 *proxy_found = proxy;
732 ClientIdentity_AddRef((IMultiQI *)&proxy->lpVtbl);
737 LeaveCriticalSection(&apt->cs);
741 HRESULT apartment_disconnectproxies(struct apartment *apt)
743 struct list * cursor;
745 LIST_FOR_EACH(cursor, &apt->proxies)
747 struct proxy_manager * proxy = LIST_ENTRY(cursor, struct proxy_manager, entry);
748 proxy_manager_disconnect(proxy);
754 /********************** StdMarshal implementation ****************************/
755 typedef struct _StdMarshalImpl
757 const IMarshalVtbl *lpvtbl;
762 LPVOID pvDestContext;
766 static HRESULT WINAPI
767 StdMarshalImpl_QueryInterface(LPMARSHAL iface, REFIID riid, LPVOID *ppv)
770 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IMarshal, riid))
773 IUnknown_AddRef(iface);
776 FIXME("No interface for %s.\n", debugstr_guid(riid));
777 return E_NOINTERFACE;
781 StdMarshalImpl_AddRef(LPMARSHAL iface)
783 StdMarshalImpl *This = (StdMarshalImpl *)iface;
784 return InterlockedIncrement(&This->ref);
788 StdMarshalImpl_Release(LPMARSHAL iface)
790 StdMarshalImpl *This = (StdMarshalImpl *)iface;
791 ULONG ref = InterlockedDecrement(&This->ref);
793 if (!ref) HeapFree(GetProcessHeap(),0,This);
797 static HRESULT WINAPI
798 StdMarshalImpl_GetUnmarshalClass(
799 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
800 void* pvDestContext, DWORD mshlflags, CLSID* pCid)
802 *pCid = CLSID_DfMarshal;
806 static HRESULT WINAPI
807 StdMarshalImpl_GetMarshalSizeMax(
808 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
809 void* pvDestContext, DWORD mshlflags, DWORD* pSize)
811 *pSize = sizeof(STDOBJREF);
815 static HRESULT WINAPI
816 StdMarshalImpl_MarshalInterface(
817 LPMARSHAL iface, IStream *pStm,REFIID riid, void* pv, DWORD dwDestContext,
818 void* pvDestContext, DWORD mshlflags)
824 APARTMENT *apt = COM_CurrentApt();
826 TRACE("(...,%s,...)\n", debugstr_guid(riid));
830 ERR("Apartment not initialized\n");
831 return CO_E_NOTINITIALIZED;
834 /* make sure this apartment can be reached from other threads / processes */
835 RPC_StartRemoting(apt);
837 hres = IUnknown_QueryInterface((LPUNKNOWN)pv, riid, (LPVOID*)&pUnk);
840 ERR("object doesn't expose interface %s, failing with error 0x%08lx\n",
841 debugstr_guid(riid), hres);
842 return E_NOINTERFACE;
845 hres = marshal_object(apt, &stdobjref, riid, pUnk, mshlflags);
847 IUnknown_Release(pUnk);
851 ERR("Failed to create ifstub, hres=0x%lx\n", hres);
855 hres = IStream_Write(pStm, &stdobjref, sizeof(stdobjref), &res);
856 if (hres) return hres;
861 /* helper for StdMarshalImpl_UnmarshalInterface - does the unmarshaling with
862 * no questions asked about the rules surrounding same-apartment unmarshals
863 * and table marshaling */
864 static HRESULT unmarshal_object(const STDOBJREF *stdobjref, APARTMENT *apt, REFIID riid, void **object)
866 struct proxy_manager *proxy_manager = NULL;
871 TRACE("stdobjref:\n\tflags = %04lx\n\tcPublicRefs = %ld\n\toxid = %s\n\toid = %s\n\tipid = %s\n",
872 stdobjref->flags, stdobjref->cPublicRefs,
873 wine_dbgstr_longlong(stdobjref->oxid),
874 wine_dbgstr_longlong(stdobjref->oid),
875 debugstr_guid(&stdobjref->ipid));
877 /* create an a new proxy manager if one doesn't already exist for the
879 if (!find_proxy_manager(apt, stdobjref->oxid, stdobjref->oid, &proxy_manager))
881 hr = proxy_manager_construct(apt, stdobjref->flags,
882 stdobjref->oxid, stdobjref->oid,
886 TRACE("proxy manager already created, using\n");
890 struct ifproxy * ifproxy;
891 hr = proxy_manager_find_ifproxy(proxy_manager, riid, &ifproxy);
892 if (hr == E_NOINTERFACE)
894 IRpcChannelBuffer *chanbuf;
895 hr = RPC_CreateClientChannel(&stdobjref->oxid, &stdobjref->ipid, &chanbuf);
897 hr = proxy_manager_create_ifproxy(proxy_manager, &stdobjref->ipid,
898 riid, stdobjref->cPublicRefs,
904 /* FIXME: push this AddRef inside proxy_manager_find_ifproxy/create_ifproxy? */
905 ClientIdentity_AddRef((IMultiQI*)&proxy_manager->lpVtbl);
906 *object = ifproxy->iface;
910 /* release our reference to the proxy manager - the client/apartment
911 * will hold on to the remaining reference for us */
912 if (proxy_manager) ClientIdentity_Release((IMultiQI*)&proxy_manager->lpVtbl);
917 static HRESULT WINAPI
918 StdMarshalImpl_UnmarshalInterface(LPMARSHAL iface, IStream *pStm, REFIID riid, void **ppv)
920 struct stub_manager *stubmgr;
924 APARTMENT *apt = COM_CurrentApt();
928 TRACE("(...,%s,....)\n", debugstr_guid(riid));
930 /* we need an apartment to unmarshal into */
933 ERR("Apartment not initialized\n");
934 return CO_E_NOTINITIALIZED;
937 /* read STDOBJREF from wire */
938 hres = IStream_Read(pStm, &stdobjref, sizeof(stdobjref), &res);
939 if (hres) return hres;
941 hres = apartment_getoxid(apt, &oxid);
942 if (hres) return hres;
944 /* check if we're marshalling back to ourselves */
945 if ((oxid == stdobjref.oxid) && (stubmgr = get_stub_manager(apt, stdobjref.oid)))
947 TRACE("Unmarshalling object marshalled in same apartment for iid %s, "
948 "returning original object %p\n", debugstr_guid(riid), stubmgr->object);
950 hres = IUnknown_QueryInterface(stubmgr->object, riid, ppv);
952 /* unref the ifstub. FIXME: only do this on success? */
953 if (!stub_manager_is_table_marshaled(stubmgr))
954 stub_manager_ext_release(stubmgr, 1);
956 stub_manager_int_release(stubmgr);
960 /* notify stub manager about unmarshal if process-local object.
961 * note: if the oxid is not found then we and native will quite happily
962 * ignore table marshaling and normal marshaling rules regarding number of
963 * unmarshals, etc, but if you abuse these rules then your proxy could end
964 * up returning RPC_E_DISCONNECTED. */
965 if ((stub_apt = apartment_findfromoxid(stdobjref.oxid, TRUE)))
967 if ((stubmgr = get_stub_manager(stub_apt, stdobjref.oid)))
969 if (!stub_manager_notify_unmarshal(stubmgr))
970 hres = CO_E_OBJNOTCONNECTED;
972 stub_manager_int_release(stubmgr);
976 WARN("Couldn't find object for OXID %s, OID %s, assuming disconnected\n",
977 wine_dbgstr_longlong(stdobjref.oxid),
978 wine_dbgstr_longlong(stdobjref.oid));
979 hres = CO_E_OBJNOTCONNECTED;
982 apartment_release(stub_apt);
985 TRACE("Treating unmarshal from OXID %s as inter-process\n",
986 wine_dbgstr_longlong(stdobjref.oxid));
989 hres = unmarshal_object(&stdobjref, apt, riid, ppv);
991 if (hres) WARN("Failed with error 0x%08lx\n", hres);
992 else TRACE("Successfully created proxy %p\n", *ppv);
997 static HRESULT WINAPI
998 StdMarshalImpl_ReleaseMarshalData(LPMARSHAL iface, IStream *pStm)
1000 STDOBJREF stdobjref;
1003 struct stub_manager *stubmgr;
1006 TRACE("iface=%p, pStm=%p\n", iface, pStm);
1008 hres = IStream_Read(pStm, &stdobjref, sizeof(stdobjref), &res);
1009 if (hres) return hres;
1011 TRACE("oxid = %s, oid = %s, ipid = %s\n",
1012 wine_dbgstr_longlong(stdobjref.oxid),
1013 wine_dbgstr_longlong(stdobjref.oid),
1014 wine_dbgstr_guid(&stdobjref.ipid));
1016 if (!(apt = apartment_findfromoxid(stdobjref.oxid, TRUE)))
1018 WARN("Could not map OXID %s to apartment object\n",
1019 wine_dbgstr_longlong(stdobjref.oxid));
1020 return RPC_E_INVALID_OBJREF;
1023 if (!(stubmgr = get_stub_manager(apt, stdobjref.oid)))
1025 ERR("could not map MID to stub manager, oxid=%s, oid=%s\n",
1026 wine_dbgstr_longlong(stdobjref.oxid), wine_dbgstr_longlong(stdobjref.oid));
1027 return RPC_E_INVALID_OBJREF;
1030 stub_manager_release_marshal_data(stubmgr, stdobjref.cPublicRefs);
1032 stub_manager_int_release(stubmgr);
1033 apartment_release(apt);
1038 static HRESULT WINAPI
1039 StdMarshalImpl_DisconnectObject(LPMARSHAL iface, DWORD dwReserved)
1041 FIXME("(), stub!\n");
1045 static const IMarshalVtbl VT_StdMarshal =
1047 StdMarshalImpl_QueryInterface,
1048 StdMarshalImpl_AddRef,
1049 StdMarshalImpl_Release,
1050 StdMarshalImpl_GetUnmarshalClass,
1051 StdMarshalImpl_GetMarshalSizeMax,
1052 StdMarshalImpl_MarshalInterface,
1053 StdMarshalImpl_UnmarshalInterface,
1054 StdMarshalImpl_ReleaseMarshalData,
1055 StdMarshalImpl_DisconnectObject
1058 static HRESULT StdMarshalImpl_Construct(REFIID riid, void** ppvObject)
1060 StdMarshalImpl * pStdMarshal =
1061 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(StdMarshalImpl));
1063 return E_OUTOFMEMORY;
1064 pStdMarshal->lpvtbl = &VT_StdMarshal;
1065 pStdMarshal->ref = 0;
1066 return IMarshal_QueryInterface((IMarshal*)pStdMarshal, riid, ppvObject);
1069 /***********************************************************************
1070 * CoGetStandardMarshal [OLE32.@]
1072 * Gets or creates a standard marshal object.
1075 * riid [I] Interface identifier of the pUnk object.
1076 * pUnk [I] Optional. Object to get the marshal object for.
1077 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1078 * pvDestContext [I] Reserved. Must be NULL.
1079 * mshlflags [I] Flags affecting the marshaling process.
1080 * ppMarshal [O] Address where marshal object will be stored.
1084 * Failure: HRESULT code.
1088 * The function retrieves the IMarshal object associated with an object if
1089 * that object is currently an active stub, otherwise a new marshal object is
1092 HRESULT WINAPI CoGetStandardMarshal(REFIID riid, IUnknown *pUnk,
1093 DWORD dwDestContext, LPVOID pvDestContext,
1094 DWORD mshlflags, LPMARSHAL *ppMarshal)
1100 FIXME("(%s,NULL,%lx,%p,%lx,%p), unimplemented yet.\n",
1101 debugstr_guid(riid),dwDestContext,pvDestContext,mshlflags,ppMarshal);
1104 TRACE("(%s,%p,%lx,%p,%lx,%p)\n",
1105 debugstr_guid(riid),pUnk,dwDestContext,pvDestContext,mshlflags,ppMarshal);
1106 *ppMarshal = HeapAlloc(GetProcessHeap(),0,sizeof(StdMarshalImpl));
1107 dm = (StdMarshalImpl*) *ppMarshal;
1108 if (!dm) return E_FAIL;
1109 dm->lpvtbl = &VT_StdMarshal;
1113 dm->dwDestContext = dwDestContext;
1114 dm->pvDestContext = pvDestContext;
1115 dm->mshlflags = mshlflags;
1119 /***********************************************************************
1120 * get_marshaler [internal]
1122 * Retrieves an IMarshal interface for an object.
1124 static HRESULT get_marshaler(REFIID riid, IUnknown *pUnk, DWORD dwDestContext,
1125 void *pvDestContext, DWORD mshlFlags,
1126 LPMARSHAL *pMarshal)
1132 hr = IUnknown_QueryInterface(pUnk, &IID_IMarshal, (LPVOID*)pMarshal);
1134 hr = CoGetStandardMarshal(riid, pUnk, dwDestContext, pvDestContext,
1135 mshlFlags, pMarshal);
1139 /***********************************************************************
1140 * get_unmarshaler_from_stream [internal]
1142 * Creates an IMarshal* object according to the data marshaled to the stream.
1143 * The function leaves the stream pointer at the start of the data written
1144 * to the stream by the IMarshal* object.
1146 static HRESULT get_unmarshaler_from_stream(IStream *stream, IMarshal **marshal, IID *iid)
1152 /* read common OBJREF header */
1153 hr = IStream_Read(stream, &objref, FIELD_OFFSET(OBJREF, u_objref), &res);
1154 if (hr || (res != FIELD_OFFSET(OBJREF, u_objref)))
1156 ERR("Failed to read common OBJREF header, 0x%08lx\n", hr);
1157 return STG_E_READFAULT;
1160 /* sanity check on header */
1161 if (objref.signature != OBJREF_SIGNATURE)
1163 ERR("Bad OBJREF signature 0x%08lx\n", objref.signature);
1164 return RPC_E_INVALID_OBJREF;
1167 if (iid) *iid = objref.iid;
1169 /* FIXME: handler marshaling */
1170 if (objref.flags & OBJREF_STANDARD)
1172 TRACE("Using standard unmarshaling\n");
1173 hr = StdMarshalImpl_Construct(&IID_IMarshal, (LPVOID*)marshal);
1175 else if (objref.flags & OBJREF_CUSTOM)
1177 ULONG custom_header_size = FIELD_OFFSET(OBJREF, u_objref.u_custom.pData) -
1178 FIELD_OFFSET(OBJREF, u_objref.u_custom);
1179 TRACE("Using custom unmarshaling\n");
1180 /* read constant sized OR_CUSTOM data from stream */
1181 hr = IStream_Read(stream, &objref.u_objref.u_custom,
1182 custom_header_size, &res);
1183 if (hr || (res != custom_header_size))
1185 ERR("Failed to read OR_CUSTOM header, 0x%08lx\n", hr);
1186 return STG_E_READFAULT;
1188 /* now create the marshaler specified in the stream */
1189 hr = CoCreateInstance(&objref.u_objref.u_custom.clsid, NULL,
1190 CLSCTX_INPROC_SERVER, &IID_IMarshal,
1195 FIXME("Invalid or unimplemented marshaling type specified: %lx\n",
1197 return RPC_E_INVALID_OBJREF;
1201 ERR("Failed to create marshal, 0x%08lx\n", hr);
1206 /***********************************************************************
1207 * CoGetMarshalSizeMax [OLE32.@]
1209 * Gets the maximum amount of data that will be needed by a marshal.
1212 * pulSize [O] Address where maximum marshal size will be stored.
1213 * riid [I] Identifier of the interface to marshal.
1214 * pUnk [I] Pointer to the object to marshal.
1215 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1216 * pvDestContext [I] Reserved. Must be NULL.
1217 * mshlFlags [I] Flags that affect the marshaling. See CoMarshalInterface().
1221 * Failure: HRESULT code.
1224 * CoMarshalInterface().
1226 HRESULT WINAPI CoGetMarshalSizeMax(ULONG *pulSize, REFIID riid, IUnknown *pUnk,
1227 DWORD dwDestContext, void *pvDestContext,
1232 CLSID marshaler_clsid;
1234 hr = get_marshaler(riid, pUnk, dwDestContext, pvDestContext, mshlFlags, &pMarshal);
1238 hr = IMarshal_GetUnmarshalClass(pMarshal, riid, pUnk, dwDestContext,
1239 pvDestContext, mshlFlags, &marshaler_clsid);
1242 ERR("IMarshal::GetUnmarshalClass failed, 0x%08lx\n", hr);
1243 IMarshal_Release(pMarshal);
1247 hr = IMarshal_GetMarshalSizeMax(pMarshal, riid, pUnk, dwDestContext,
1248 pvDestContext, mshlFlags, pulSize);
1249 /* add on the size of the common header */
1250 *pulSize += FIELD_OFFSET(OBJREF, u_objref);
1252 /* if custom marshaling, add on size of custom header */
1253 if (!IsEqualCLSID(&marshaler_clsid, &CLSID_DfMarshal))
1254 *pulSize += FIELD_OFFSET(OBJREF, u_objref.u_custom.pData) -
1255 FIELD_OFFSET(OBJREF, u_objref.u_custom);
1257 IMarshal_Release(pMarshal);
1262 /***********************************************************************
1263 * CoMarshalInterface [OLE32.@]
1265 * Marshals an interface into a stream so that the object can then be
1266 * unmarshaled from another COM apartment and used remotely.
1269 * pStream [I] Stream the object will be marshaled into.
1270 * riid [I] Identifier of the interface to marshal.
1271 * pUnk [I] Pointer to the object to marshal.
1272 * dwDestContext [I] Destination. Used to enable or disable optimizations.
1273 * pvDestContext [I] Reserved. Must be NULL.
1274 * mshlFlags [I] Flags that affect the marshaling. See notes.
1278 * Failure: HRESULT code.
1282 * The mshlFlags parameter can take one or more of the following flags:
1283 *| MSHLFLAGS_NORMAL - Unmarshal once, releases stub on last proxy release.
1284 *| MSHLFLAGS_TABLESTRONG - Unmarshal many, release when CoReleaseMarshalData() called.
1285 *| MSHLFLAGS_TABLEWEAK - Unmarshal many, releases stub on last proxy release.
1286 *| MSHLFLAGS_NOPING - No automatic garbage collection (and so reduces network traffic).
1288 * If a marshaled object is not unmarshaled, then CoReleaseMarshalData() must
1289 * be called in order to release the resources used in the marshaling.
1292 * CoUnmarshalInterface(), CoReleaseMarshalData().
1294 HRESULT WINAPI CoMarshalInterface(IStream *pStream, REFIID riid, IUnknown *pUnk,
1295 DWORD dwDestContext, void *pvDestContext,
1299 CLSID marshaler_clsid;
1303 TRACE("(%p, %s, %p, %lx, %p, %lx)\n", pStream, debugstr_guid(riid), pUnk,
1304 dwDestContext, pvDestContext, mshlFlags);
1307 return E_INVALIDARG;
1309 objref.signature = OBJREF_SIGNATURE;
1312 /* get the marshaler for the specified interface */
1313 hr = get_marshaler(riid, pUnk, dwDestContext, pvDestContext, mshlFlags, &pMarshal);
1316 ERR("Failed to get marshaller, 0x%08lx\n", hr);
1320 hr = IMarshal_GetUnmarshalClass(pMarshal, riid, pUnk, dwDestContext,
1321 pvDestContext, mshlFlags, &marshaler_clsid);
1324 ERR("IMarshal::GetUnmarshalClass failed, 0x%08lx\n", hr);
1328 /* FIXME: implement handler marshaling too */
1329 if (IsEqualCLSID(&marshaler_clsid, &CLSID_DfMarshal))
1331 TRACE("Using standard marshaling\n");
1332 objref.flags = OBJREF_STANDARD;
1334 /* write the common OBJREF header to the stream */
1335 hr = IStream_Write(pStream, &objref, FIELD_OFFSET(OBJREF, u_objref), NULL);
1338 ERR("Failed to write OBJREF header to stream, 0x%08lx\n", hr);
1344 TRACE("Using custom marshaling\n");
1345 objref.flags = OBJREF_CUSTOM;
1346 objref.u_objref.u_custom.clsid = marshaler_clsid;
1347 objref.u_objref.u_custom.cbExtension = 0;
1348 objref.u_objref.u_custom.size = 0;
1349 hr = IMarshal_GetMarshalSizeMax(pMarshal, riid, pUnk, dwDestContext,
1350 pvDestContext, mshlFlags,
1351 &objref.u_objref.u_custom.size);
1354 ERR("Failed to get max size of marshal data, error 0x%08lx\n", hr);
1357 /* write constant sized common header and OR_CUSTOM data into stream */
1358 hr = IStream_Write(pStream, &objref,
1359 FIELD_OFFSET(OBJREF, u_objref.u_custom.pData), NULL);
1362 ERR("Failed to write OR_CUSTOM header to stream with 0x%08lx\n", hr);
1367 TRACE("Calling IMarshal::MarshalInterace\n");
1368 /* call helper object to do the actual marshaling */
1369 hr = IMarshal_MarshalInterface(pMarshal, pStream, riid, pUnk, dwDestContext,
1370 pvDestContext, mshlFlags);
1374 ERR("Failed to marshal the interface %s, %lx\n", debugstr_guid(riid), hr);
1379 IMarshal_Release(pMarshal);
1381 TRACE("completed with hr 0x%08lx\n", hr);
1386 /***********************************************************************
1387 * CoUnmarshalInterface [OLE32.@]
1389 * Unmarshals an object from a stream by creating a proxy to the remote
1390 * object, if necessary.
1394 * pStream [I] Stream containing the marshaled object.
1395 * riid [I] Interface identifier of the object to create a proxy to.
1396 * ppv [O] Address where proxy will be stored.
1401 * Failure: HRESULT code.
1404 * CoMarshalInterface().
1406 HRESULT WINAPI CoUnmarshalInterface(IStream *pStream, REFIID riid, LPVOID *ppv)
1413 TRACE("(%p, %s, %p)\n", pStream, debugstr_guid(riid), ppv);
1415 hr = get_unmarshaler_from_stream(pStream, &pMarshal, &iid);
1419 /* call the helper object to do the actual unmarshaling */
1420 hr = IMarshal_UnmarshalInterface(pMarshal, pStream, &iid, (LPVOID*)&object);
1422 ERR("IMarshal::UnmarshalInterface failed, 0x%08lx\n", hr);
1424 /* IID_NULL means use the interface ID of the marshaled object */
1425 if (!IsEqualIID(riid, &IID_NULL))
1430 if (!IsEqualIID(riid, &iid))
1432 TRACE("requested interface != marshalled interface, additional QI needed\n");
1433 hr = IUnknown_QueryInterface(object, &iid, ppv);
1435 ERR("Couldn't query for interface %s, hr = 0x%08lx\n",
1436 debugstr_guid(riid), hr);
1437 IUnknown_Release(object);
1445 IMarshal_Release(pMarshal);
1447 TRACE("completed with hr 0x%lx\n", hr);
1452 /***********************************************************************
1453 * CoReleaseMarshalData [OLE32.@]
1455 * Releases resources associated with an object that has been marshaled into
1460 * pStream [I] The stream that the object has been marshaled into.
1464 * Failure: HRESULT error code.
1468 * Call this function to release resources associated with a normal or
1469 * table-weak marshal that will not be unmarshaled, and all table-strong
1470 * marshals when they are no longer needed.
1473 * CoMarshalInterface(), CoUnmarshalInterface().
1475 HRESULT WINAPI CoReleaseMarshalData(IStream *pStream)
1480 TRACE("(%p)\n", pStream);
1482 hr = get_unmarshaler_from_stream(pStream, &pMarshal, NULL);
1486 /* call the helper object to do the releasing of marshal data */
1487 hr = IMarshal_ReleaseMarshalData(pMarshal, pStream);
1489 ERR("IMarshal::ReleaseMarshalData failed with error 0x%08lx\n", hr);
1491 IMarshal_Release(pMarshal);
1496 /***********************************************************************
1497 * CoMarshalInterThreadInterfaceInStream [OLE32.@]
1499 * Marshal an interface across threads in the same process.
1502 * riid [I] Identifier of the interface to be marshalled.
1503 * pUnk [I] Pointer to IUnknown-derived interface that will be marshalled.
1504 * ppStm [O] Pointer to IStream object that is created and then used to store the marshalled inteface.
1508 * Failure: E_OUTOFMEMORY and other COM error codes
1511 * CoMarshalInterface(), CoUnmarshalInterface() and CoGetInterfaceAndReleaseStream()
1513 HRESULT WINAPI CoMarshalInterThreadInterfaceInStream(
1514 REFIID riid, LPUNKNOWN pUnk, LPSTREAM * ppStm)
1516 ULARGE_INTEGER xpos;
1517 LARGE_INTEGER seekto;
1520 TRACE("(%s, %p, %p)\n",debugstr_guid(riid), pUnk, ppStm);
1522 hres = CreateStreamOnHGlobal(0, TRUE, ppStm);
1523 if (FAILED(hres)) return hres;
1524 hres = CoMarshalInterface(*ppStm, riid, pUnk, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1526 /* FIXME: is this needed? */
1527 memset(&seekto,0,sizeof(seekto));
1528 IStream_Seek(*ppStm,seekto,SEEK_SET,&xpos);
1533 /***********************************************************************
1534 * CoGetInterfaceAndReleaseStream [OLE32.@]
1536 * Unmarshalls an inteface from a stream and then releases the stream.
1539 * pStm [I] Stream that contains the marshalled inteface.
1540 * riid [I] Interface identifier of the object to unmarshall.
1541 * ppv [O] Address of pointer where the requested interface object will be stored.
1545 * Failure: A COM error code
1548 * CoMarshalInterThreadInterfaceInStream() and CoUnmarshalInteface()
1550 HRESULT WINAPI CoGetInterfaceAndReleaseStream(LPSTREAM pStm, REFIID riid,
1555 TRACE("(%p, %s, %p)\n", pStm, debugstr_guid(riid), ppv);
1557 hres = CoUnmarshalInterface(pStm, riid, ppv);
1558 IStream_Release(pStm);
1562 static HRESULT WINAPI StdMarshalCF_QueryInterface(LPCLASSFACTORY iface,
1563 REFIID riid, LPVOID *ppv)
1566 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory))
1568 *ppv = (LPVOID)iface;
1571 return E_NOINTERFACE;
1574 static ULONG WINAPI StdMarshalCF_AddRef(LPCLASSFACTORY iface)
1576 return 2; /* non-heap based object */
1579 static ULONG WINAPI StdMarshalCF_Release(LPCLASSFACTORY iface)
1581 return 1; /* non-heap based object */
1584 static HRESULT WINAPI StdMarshalCF_CreateInstance(LPCLASSFACTORY iface,
1585 LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv)
1587 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IMarshal))
1588 return StdMarshalImpl_Construct(riid, ppv);
1590 FIXME("(%s), not supported.\n",debugstr_guid(riid));
1591 return E_NOINTERFACE;
1594 static HRESULT WINAPI StdMarshalCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
1596 FIXME("(%d), stub!\n",fLock);
1600 static const IClassFactoryVtbl StdMarshalCFVtbl =
1602 StdMarshalCF_QueryInterface,
1603 StdMarshalCF_AddRef,
1604 StdMarshalCF_Release,
1605 StdMarshalCF_CreateInstance,
1606 StdMarshalCF_LockServer
1608 static const IClassFactoryVtbl *StdMarshalCF = &StdMarshalCFVtbl;
1610 HRESULT MARSHAL_GetStandardMarshalCF(LPVOID *ppv)
1612 *ppv = &StdMarshalCF;
1616 /***********************************************************************
1617 * CoMarshalHresult [OLE32.@]
1619 * Marshals an HRESULT value into a stream.
1622 * pStm [I] Stream that hresult will be marshalled into.
1623 * hresult [I] HRESULT to be marshalled.
1627 * Failure: A COM error code
1630 * CoUnmarshalHresult().
1632 HRESULT WINAPI CoMarshalHresult(LPSTREAM pStm, HRESULT hresult)
1634 return IStream_Write(pStm, &hresult, sizeof(hresult), NULL);
1637 /***********************************************************************
1638 * CoUnmarshalHresult [OLE32.@]
1640 * Unmarshals an HRESULT value from a stream.
1643 * pStm [I] Stream that hresult will be unmarshalled from.
1644 * phresult [I] Pointer to HRESULT where the value will be unmarshalled to.
1648 * Failure: A COM error code
1651 * CoMarshalHresult().
1653 HRESULT WINAPI CoUnmarshalHresult(LPSTREAM pStm, HRESULT * phresult)
1655 return IStream_Read(pStm, phresult, sizeof(*phresult), NULL);