comctl32: A couple fixes for tab icon offsets.
[wine] / dlls / ole32 / marshal.c
1 /*
2  *      Marshalling library
3  *
4  * Copyright 2002 Marcus Meissner
5  * Copyright 2004 Mike Hearn, for CodeWeavers
6  * Copyright 2004 Rob Shearman, for CodeWeavers
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include "config.h"
24
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <assert.h>
30
31 #define COBJMACROS
32
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winuser.h"
36 #include "objbase.h"
37 #include "ole2.h"
38 #include "rpc.h"
39 #include "winerror.h"
40 #include "winreg.h"
41 #include "wtypes.h"
42 #include "wine/unicode.h"
43
44 #include "compobj_private.h"
45
46 #include "wine/debug.h"
47
48 WINE_DEFAULT_DEBUG_CHANNEL(ole);
49
50 extern const CLSID CLSID_DfMarshal;
51
52 /* number of refs given out for normal marshaling */
53 #define NORMALEXTREFS 5
54
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
58
59 static HRESULT unmarshal_object(const STDOBJREF *stdobjref, APARTMENT *apt,
60                                 MSHCTX dest_context, void *dest_context_data,
61                                 REFIID riid, void **object);
62
63 /* Marshalling just passes a unique identifier to the remote client,
64  * that makes it possible to find the passed interface again.
65  *
66  * So basically we need a set of values that make it unique.
67  *
68  * Note that the IUnknown_QI(ob,xiid,&ppv) always returns the SAME ppv value!
69  *
70  * A triple is used: OXID (apt id), OID (stub manager id),
71  * IPID (interface ptr/stub id).
72  *
73  * OXIDs identify an apartment and are network scoped
74  * OIDs identify a stub manager and are apartment scoped
75  * IPIDs identify an interface stub and are apartment scoped
76  */
77
78 inline static HRESULT get_facbuf_for_iid(REFIID riid, IPSFactoryBuffer **facbuf)
79 {
80     HRESULT       hr;
81     CLSID         clsid;
82
83     if ((hr = CoGetPSClsid(riid, &clsid)))
84         return hr;
85     return CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL,
86         &IID_IPSFactoryBuffer, (LPVOID*)facbuf);
87 }
88
89 /* marshals an object into a STDOBJREF structure */
90 HRESULT marshal_object(APARTMENT *apt, STDOBJREF *stdobjref, REFIID riid, IUnknown *object, MSHLFLAGS mshlflags)
91 {
92     struct stub_manager *manager;
93     struct ifstub       *ifstub;
94     BOOL                 tablemarshal;
95     IRpcStubBuffer      *stub = NULL;
96     HRESULT              hr;
97     IUnknown            *iobject = NULL; /* object of type riid */
98
99     hr = apartment_getoxid(apt, &stdobjref->oxid);
100     if (hr != S_OK)
101         return hr;
102
103     hr = apartment_createwindowifneeded(apt);
104     if (hr != S_OK)
105         return hr;
106
107     hr = IUnknown_QueryInterface(object, riid, (void **)&iobject);
108     if (hr != S_OK)
109     {
110         ERR("object doesn't expose interface %s, failing with error 0x%08lx\n",
111             debugstr_guid(riid), hr);
112         return E_NOINTERFACE;
113     }
114   
115     /* IUnknown doesn't require a stub buffer, because it never goes out on
116      * the wire */
117     if (!IsEqualIID(riid, &IID_IUnknown))
118     {
119         IPSFactoryBuffer *psfb;
120
121         hr = get_facbuf_for_iid(riid, &psfb);
122         if (hr != S_OK)
123         {
124             ERR("couldn't get IPSFactory buffer for interface %s\n", debugstr_guid(riid));
125             IUnknown_Release(iobject);
126             return hr;
127         }
128     
129         hr = IPSFactoryBuffer_CreateStub(psfb, riid, iobject, &stub);
130         IPSFactoryBuffer_Release(psfb);
131         if (hr != S_OK)
132         {
133             ERR("Failed to create an IRpcStubBuffer from IPSFactory for %s\n", debugstr_guid(riid));
134             IUnknown_Release(iobject);
135             return hr;
136         }
137     }
138
139     if (mshlflags & MSHLFLAGS_NOPING)
140         stdobjref->flags = SORF_NOPING;
141     else
142         stdobjref->flags = SORF_NULL;
143
144     if ((manager = get_stub_manager_from_object(apt, object)))
145         TRACE("registering new ifstub on pre-existing manager\n");
146     else
147     {
148         TRACE("constructing new stub manager\n");
149
150         manager = new_stub_manager(apt, object);
151         if (!manager)
152         {
153             if (stub) IRpcStubBuffer_Release(stub);
154             IUnknown_Release(iobject);
155             return E_OUTOFMEMORY;
156         }
157     }
158     stdobjref->oid = manager->oid;
159
160     tablemarshal = ((mshlflags & MSHLFLAGS_TABLESTRONG) || (mshlflags & MSHLFLAGS_TABLEWEAK));
161
162     /* make sure ifstub that we are creating is unique */
163     ifstub = stub_manager_find_ifstub(manager, riid, mshlflags);
164     if (!ifstub)
165     {
166         ifstub = stub_manager_new_ifstub(manager, stub, iobject, riid, mshlflags);
167         IUnknown_Release(iobject);
168         if (stub) IRpcStubBuffer_Release(stub);
169         if (!ifstub)
170         {
171             stub_manager_int_release(manager);
172             /* FIXME: should we do another release to completely destroy the
173              * stub manager? */
174             return E_OUTOFMEMORY;
175         }
176     }
177
178     if (!tablemarshal)
179     {
180         stdobjref->cPublicRefs = NORMALEXTREFS;
181         stub_manager_ext_addref(manager, stdobjref->cPublicRefs);
182     }
183     else
184     {
185         stdobjref->cPublicRefs = 0;
186         if (mshlflags & MSHLFLAGS_TABLESTRONG)
187             stub_manager_ext_addref(manager, 1);
188     }
189
190     /* FIXME: check return value */
191     RPC_RegisterInterface(riid);
192
193     stdobjref->ipid = ifstub->ipid;
194
195     stub_manager_int_release(manager);
196     return S_OK;
197 }
198
199
200
201 /* Client-side identity of the server object */
202
203 static HRESULT proxy_manager_get_remunknown(struct proxy_manager * This, IRemUnknown **remunk);
204 static void proxy_manager_destroy(struct proxy_manager * This);
205 static HRESULT proxy_manager_find_ifproxy(struct proxy_manager * This, REFIID riid, struct ifproxy ** ifproxy_found);
206 static HRESULT proxy_manager_query_local_interface(struct proxy_manager * This, REFIID riid, void ** ppv);
207
208 static HRESULT WINAPI ClientIdentity_QueryInterface(IMultiQI * iface, REFIID riid, void ** ppv)
209 {
210     HRESULT hr;
211     MULTI_QI mqi;
212
213     TRACE("%s\n", debugstr_guid(riid));
214
215     mqi.pIID = riid;
216     hr = IMultiQI_QueryMultipleInterfaces(iface, 1, &mqi);
217     *ppv = (void *)mqi.pItf;
218
219     return hr;
220 }
221
222 static ULONG WINAPI ClientIdentity_AddRef(IMultiQI * iface)
223 {
224     struct proxy_manager * This = (struct proxy_manager *)iface;
225     TRACE("%p - before %ld\n", iface, This->refs);
226     return InterlockedIncrement(&This->refs);
227 }
228
229 static ULONG WINAPI ClientIdentity_Release(IMultiQI * iface)
230 {
231     struct proxy_manager * This = (struct proxy_manager *)iface;
232     ULONG refs = InterlockedDecrement(&This->refs);
233     TRACE("%p - after %ld\n", iface, refs);
234     if (!refs)
235         proxy_manager_destroy(This);
236     return refs;
237 }
238
239 static HRESULT WINAPI ClientIdentity_QueryMultipleInterfaces(IMultiQI *iface, ULONG cMQIs, MULTI_QI *pMQIs)
240 {
241     struct proxy_manager * This = (struct proxy_manager *)iface;
242     REMQIRESULT *qiresults = NULL;
243     ULONG nonlocal_mqis = 0;
244     ULONG i;
245     ULONG successful_mqis = 0;
246     IID *iids = HeapAlloc(GetProcessHeap(), 0, cMQIs * sizeof(*iids));
247     /* mapping of RemQueryInterface index to QueryMultipleInterfaces index */
248     ULONG *mapping = HeapAlloc(GetProcessHeap(), 0, cMQIs * sizeof(*mapping));
249
250     TRACE("cMQIs: %ld\n", cMQIs);
251
252     /* try to get a local interface - this includes already active proxy
253      * interfaces and also interfaces exposed by the proxy manager */
254     for (i = 0; i < cMQIs; i++)
255     {
256         TRACE("iid[%ld] = %s\n", i, debugstr_guid(pMQIs[i].pIID));
257         pMQIs[i].hr = proxy_manager_query_local_interface(This, pMQIs[i].pIID, (void **)&pMQIs[i].pItf);
258         if (pMQIs[i].hr == S_OK)
259             successful_mqis++;
260         else
261         {
262             iids[nonlocal_mqis] = *pMQIs[i].pIID;
263             mapping[nonlocal_mqis] = i;
264             nonlocal_mqis++;
265         }
266     }
267
268     TRACE("%ld interfaces not found locally\n", nonlocal_mqis);
269
270     /* if we have more than one interface not found locally then we must try
271      * to query the remote object for it */
272     if (nonlocal_mqis != 0)
273     {
274         IRemUnknown *remunk;
275         HRESULT hr;
276         IPID *ipid;
277
278         /* get the ipid of the first entry */
279         /* FIXME: should we implement ClientIdentity on the ifproxies instead
280          * of the proxy_manager so we use the correct ipid here? */
281         ipid = &LIST_ENTRY(list_head(&This->interfaces), struct ifproxy, entry)->stdobjref.ipid;
282
283         /* get IRemUnknown proxy so we can communicate with the remote object */
284         hr = proxy_manager_get_remunknown(This, &remunk);
285
286         if (hr == S_OK)
287         {
288             hr = IRemUnknown_RemQueryInterface(remunk, ipid, NORMALEXTREFS,
289                                                nonlocal_mqis, iids, &qiresults);
290             if (FAILED(hr))
291                 ERR("IRemUnknown_RemQueryInterface failed with error 0x%08lx\n", hr);
292         }
293
294         /* IRemUnknown_RemQueryInterface can return S_FALSE if only some of
295          * the interfaces were returned */
296         if (SUCCEEDED(hr))
297         {
298             /* try to unmarshal each object returned to us */
299             for (i = 0; i < nonlocal_mqis; i++)
300             {
301                 ULONG index = mapping[i];
302                 HRESULT hrobj = qiresults[i].hResult;
303                 if (hrobj == S_OK)
304                     hrobj = unmarshal_object(&qiresults[i].std, This->parent,
305                                              This->dest_context,
306                                              This->dest_context_data,
307                                              pMQIs[index].pIID,
308                                              (void **)&pMQIs[index].pItf);
309
310                 if (hrobj == S_OK)
311                     successful_mqis++;
312                 else
313                     ERR("Failed to get pointer to interface %s\n", debugstr_guid(pMQIs[index].pIID));
314                 pMQIs[index].hr = hrobj;
315             }
316         }
317
318         /* free the memory allocated by the proxy */
319         CoTaskMemFree(qiresults);
320     }
321
322     TRACE("%ld/%ld successfully queried\n", successful_mqis, cMQIs);
323
324     HeapFree(GetProcessHeap(), 0, iids);
325     HeapFree(GetProcessHeap(), 0, mapping);
326
327     if (successful_mqis == cMQIs)
328         return S_OK; /* we got all requested interfaces */
329     else if (successful_mqis == 0)
330         return E_NOINTERFACE; /* we didn't get any interfaces */
331     else
332         return S_FALSE; /* we got some interfaces */
333 }
334
335 static const IMultiQIVtbl ClientIdentity_Vtbl =
336 {
337     ClientIdentity_QueryInterface,
338     ClientIdentity_AddRef,
339     ClientIdentity_Release,
340     ClientIdentity_QueryMultipleInterfaces
341 };
342
343 static HRESULT WINAPI Proxy_QueryInterface(IMarshal *iface, REFIID riid, void **ppvObject)
344 {
345     ICOM_THIS_MULTI(struct proxy_manager, lpVtblMarshal, iface);
346     return IMultiQI_QueryInterface((IMultiQI *)&This->lpVtbl, riid, ppvObject);
347 }
348
349 static ULONG WINAPI Proxy_AddRef(IMarshal *iface)
350 {
351     ICOM_THIS_MULTI(struct proxy_manager, lpVtblMarshal, iface);
352     return IMultiQI_AddRef((IMultiQI *)&This->lpVtbl);
353 }
354
355 /* FIXME: remove these */
356 static HRESULT WINAPI StdMarshalImpl_GetUnmarshalClass(LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext, void* pvDestContext, DWORD mshlflags, CLSID* pCid);
357 static HRESULT WINAPI StdMarshalImpl_GetMarshalSizeMax(LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext, void* pvDestContext, DWORD mshlflags, DWORD* pSize);
358 static HRESULT WINAPI StdMarshalImpl_UnmarshalInterface(LPMARSHAL iface, IStream *pStm, REFIID riid, void **ppv);
359 static HRESULT WINAPI StdMarshalImpl_ReleaseMarshalData(LPMARSHAL iface, IStream *pStm);
360 static HRESULT WINAPI StdMarshalImpl_DisconnectObject(LPMARSHAL iface, DWORD dwReserved);
361
362 static ULONG WINAPI Proxy_Release(IMarshal *iface)
363 {
364     ICOM_THIS_MULTI(struct proxy_manager, lpVtblMarshal, iface);
365     return IMultiQI_Release((IMultiQI *)&This->lpVtbl);
366 }
367
368 static HRESULT WINAPI Proxy_MarshalInterface(
369     LPMARSHAL iface, IStream *pStm, REFIID riid, void* pv, DWORD dwDestContext,
370     void* pvDestContext, DWORD mshlflags)
371 {
372     ICOM_THIS_MULTI(struct proxy_manager, lpVtblMarshal, iface);
373     HRESULT hr;
374     struct ifproxy *ifproxy;
375
376     TRACE("(...,%s,...)\n", debugstr_guid(riid));
377
378     hr = proxy_manager_find_ifproxy(This, riid, &ifproxy);
379     if (SUCCEEDED(hr))
380     {
381         STDOBJREF stdobjref = ifproxy->stdobjref;
382         /* FIXME: optimization - share out proxy's public references if possible
383          * instead of making new proxy do a roundtrip through the server */
384         stdobjref.cPublicRefs = 0; /* InterlockedDecrement(&This->stdobjref.cPublicRefs) >= 0 ? 1 : 0 */
385
386         hr = IStream_Write(pStm, &stdobjref, sizeof(stdobjref), NULL);
387     }
388     else
389     {
390         /* we don't have the interface already unmarshaled so we have to
391          * request the object from the server */
392         IRemUnknown *remunk;
393         IPID *ipid;
394         REMQIRESULT *qiresults = NULL;
395         IID iid = *riid;
396
397         /* get the ipid of the first entry */
398         /* FIXME: should we implement ClientIdentity on the ifproxies instead
399          * of the proxy_manager so we use the correct ipid here? */
400         ipid = &LIST_ENTRY(list_head(&This->interfaces), struct ifproxy, entry)->stdobjref.ipid;
401
402         /* get IRemUnknown proxy so we can communicate with the remote object */
403         hr = proxy_manager_get_remunknown(This, &remunk);
404
405         if (hr == S_OK)
406         {
407             hr = IRemUnknown_RemQueryInterface(remunk, ipid, NORMALEXTREFS,
408                                                1, &iid, &qiresults);
409             if (SUCCEEDED(hr))
410             {
411                 hr = IStream_Write(pStm, &qiresults->std, sizeof(qiresults->std), NULL);
412                 if (FAILED(hr))
413                 {
414                     REMINTERFACEREF rif;
415                     rif.ipid = qiresults->std.ipid;
416                     rif.cPublicRefs = qiresults->std.cPublicRefs;
417                     rif.cPrivateRefs = 0;
418                     IRemUnknown_RemRelease(remunk, 1, &rif);
419                 }
420                 CoTaskMemFree(qiresults);
421             }
422             else
423                 ERR("IRemUnknown_RemQueryInterface failed with error 0x%08lx\n", hr);
424         }
425     }
426
427     return hr;
428 }
429
430 static const IMarshalVtbl ProxyMarshal_Vtbl =
431 {
432     Proxy_QueryInterface,
433     Proxy_AddRef,
434     Proxy_Release,
435     StdMarshalImpl_GetUnmarshalClass,
436     StdMarshalImpl_GetMarshalSizeMax,
437     Proxy_MarshalInterface,
438     StdMarshalImpl_UnmarshalInterface,
439     StdMarshalImpl_ReleaseMarshalData,
440     StdMarshalImpl_DisconnectObject
441 };
442
443 static HRESULT ifproxy_get_public_ref(struct ifproxy * This)
444 {
445     HRESULT hr = S_OK;
446
447     if (WAIT_OBJECT_0 != WaitForSingleObject(This->parent->remoting_mutex, INFINITE))
448     {
449         ERR("Wait failed for ifproxy %p\n", This);
450         return E_UNEXPECTED;
451     }
452
453     if (This->refs == 0)
454     {
455         IRemUnknown *remunk = NULL;
456
457         TRACE("getting public ref for ifproxy %p\n", This);
458
459         hr = proxy_manager_get_remunknown(This->parent, &remunk);
460         if (hr == S_OK)
461         {
462             HRESULT hrref = S_OK;
463             REMINTERFACEREF rif;
464             rif.ipid = This->stdobjref.ipid;
465             rif.cPublicRefs = NORMALEXTREFS;
466             rif.cPrivateRefs = 0;
467             hr = IRemUnknown_RemAddRef(remunk, 1, &rif, &hrref);
468             if (hr == S_OK && hrref == S_OK)
469                 This->refs += NORMALEXTREFS;
470             else
471                 ERR("IRemUnknown_RemAddRef returned with 0x%08lx, hrref = 0x%08lx\n", hr, hrref);
472         }
473     }
474     ReleaseMutex(This->parent->remoting_mutex);
475
476     return hr;
477 }
478
479 static HRESULT ifproxy_release_public_refs(struct ifproxy * This)
480 {
481     HRESULT hr = S_OK;
482
483     if (WAIT_OBJECT_0 != WaitForSingleObject(This->parent->remoting_mutex, INFINITE))
484     {
485         ERR("Wait failed for ifproxy %p\n", This);
486         return E_UNEXPECTED;
487     }
488
489     if (This->refs > 0)
490     {
491         IRemUnknown *remunk = NULL;
492
493         TRACE("releasing %ld refs\n", This->refs);
494
495         hr = proxy_manager_get_remunknown(This->parent, &remunk);
496         if (hr == S_OK)
497         {
498             REMINTERFACEREF rif;
499             rif.ipid = This->stdobjref.ipid;
500             rif.cPublicRefs = This->refs;
501             rif.cPrivateRefs = 0;
502             hr = IRemUnknown_RemRelease(remunk, 1, &rif);
503             if (hr == S_OK)
504                 This->refs = 0;
505             else if (hr == RPC_E_DISCONNECTED)
506                 WARN("couldn't release references because object was "
507                      "disconnected: oxid = %s, oid = %s\n",
508                      wine_dbgstr_longlong(This->parent->oxid),
509                      wine_dbgstr_longlong(This->parent->oid));
510             else
511                 ERR("IRemUnknown_RemRelease failed with error 0x%08lx\n", hr);
512         }
513     }
514     ReleaseMutex(This->parent->remoting_mutex);
515
516     return hr;
517 }
518
519 /* should be called inside This->parent->cs critical section */
520 static void ifproxy_disconnect(struct ifproxy * This)
521 {
522     ifproxy_release_public_refs(This);
523     if (This->proxy) IRpcProxyBuffer_Disconnect(This->proxy);
524
525     IRpcChannelBuffer_Release(This->chan);
526     This->chan = NULL;
527 }
528
529 /* should be called in This->parent->cs critical section if it is an entry in parent's list */
530 static void ifproxy_destroy(struct ifproxy * This)
531 {
532     TRACE("%p\n", This);
533
534     /* release public references to this object so that the stub can know
535      * when to destroy itself */
536     ifproxy_release_public_refs(This);
537
538     list_remove(&This->entry);
539
540     if (This->chan)
541     {
542         IRpcChannelBuffer_Release(This->chan);
543         This->chan = NULL;
544     }
545
546     /* note: we don't call Release for This->proxy because its lifetime is
547      * controlled by the return value from ClientIdentity_Release, which this
548      * function is always called from */
549
550     HeapFree(GetProcessHeap(), 0, This);
551 }
552
553 static HRESULT proxy_manager_construct(
554     APARTMENT * apt, ULONG sorflags, OXID oxid, OID oid,
555     struct proxy_manager ** proxy_manager)
556 {
557     struct proxy_manager * This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
558     if (!This) return E_OUTOFMEMORY;
559
560     This->remoting_mutex = CreateMutexW(NULL, FALSE, NULL);
561     if (!This->remoting_mutex)
562     {
563         HeapFree(GetProcessHeap(), 0, This);
564         return HRESULT_FROM_WIN32(GetLastError());
565     }
566
567     This->lpVtbl = &ClientIdentity_Vtbl;
568     This->lpVtblMarshal = &ProxyMarshal_Vtbl;
569
570     list_init(&This->entry);
571     list_init(&This->interfaces);
572
573     InitializeCriticalSection(&This->cs);
574     DEBUG_SET_CRITSEC_NAME(&This->cs, "proxy_manager");
575
576     /* the apartment the object was unmarshaled into */
577     This->parent = apt;
578
579     /* the source apartment and id of the object */
580     This->oxid = oxid;
581     This->oid = oid;
582
583     This->refs = 1;
584
585     /* the DCOM draft specification states that the SORF_NOPING flag is
586      * proxy manager specific, not ifproxy specific, so this implies that we
587      * should store the STDOBJREF flags here in the proxy manager. */
588     This->sorflags = sorflags;
589
590     /* we create the IRemUnknown proxy on demand */
591     This->remunk = NULL;
592
593     /* initialise these values to the weakest values and they will be
594      * overwritten in proxy_manager_set_context */
595     This->dest_context = MSHCTX_INPROC;
596     This->dest_context_data = NULL;
597
598     EnterCriticalSection(&apt->cs);
599     /* FIXME: we are dependent on the ordering in here to make sure a proxy's
600      * IRemUnknown proxy doesn't get destroyed before the regual proxy does
601      * because we need the IRemUnknown proxy during the destruction of the
602      * regular proxy. Ideally, we should maintain a separate list for the
603      * IRemUnknown proxies that need late destruction */
604     list_add_tail(&apt->proxies, &This->entry);
605     LeaveCriticalSection(&apt->cs);
606
607     TRACE("%p created for OXID %s, OID %s\n", This,
608         wine_dbgstr_longlong(oxid), wine_dbgstr_longlong(oid));
609
610     *proxy_manager = This;
611     return S_OK;
612 }
613
614 static inline void proxy_manager_set_context(struct proxy_manager *This, MSHCTX dest_context, void *dest_context_data)
615 {
616     MSHCTX old_dest_context = This->dest_context;
617     MSHCTX new_dest_context;
618
619     do
620     {
621         new_dest_context = old_dest_context;
622         /* "stronger" values overwrite "weaker" values. stronger values are
623          * ones that disable more optimisations */
624         switch (old_dest_context)
625         {
626         case MSHCTX_INPROC:
627             new_dest_context = dest_context;
628             break;
629         case MSHCTX_CROSSCTX:
630             switch (dest_context)
631             {
632             case MSHCTX_INPROC:
633                 break;
634             default:
635                 new_dest_context = dest_context;
636             }
637             break;
638         case MSHCTX_LOCAL:
639             switch (dest_context)
640             {
641             case MSHCTX_INPROC:
642             case MSHCTX_CROSSCTX:
643                 break;
644             default:
645                 new_dest_context = dest_context;
646             }
647             break;
648         case MSHCTX_NOSHAREDMEM:
649             switch (dest_context)
650             {
651             case MSHCTX_DIFFERENTMACHINE:
652                 new_dest_context = dest_context;
653                 break;
654             default:
655                 break;
656             }
657             break;
658         default:
659             break;
660         }
661
662         if (old_dest_context == new_dest_context) break;
663
664         old_dest_context = InterlockedCompareExchange((PLONG)&This->dest_context, new_dest_context, old_dest_context);
665     } while (new_dest_context != old_dest_context);
666
667     if (dest_context_data)
668         InterlockedExchangePointer(&This->dest_context_data, dest_context_data);
669 }
670
671 static HRESULT proxy_manager_query_local_interface(struct proxy_manager * This, REFIID riid, void ** ppv)
672 {
673     HRESULT hr;
674     struct ifproxy * ifproxy;
675
676     TRACE("%s\n", debugstr_guid(riid));
677
678     if (IsEqualIID(riid, &IID_IUnknown) ||
679         IsEqualIID(riid, &IID_IMultiQI))
680     {
681         *ppv = (void *)&This->lpVtbl;
682         IUnknown_AddRef((IUnknown *)*ppv);
683         return S_OK;
684     }
685     if (IsEqualIID(riid, &IID_IMarshal))
686     {
687         *ppv = (void *)&This->lpVtblMarshal;
688         IUnknown_AddRef((IUnknown *)*ppv);
689         return S_OK;
690     }
691     if (IsEqualIID(riid, &IID_IClientSecurity))
692     {
693         FIXME("requesting IClientSecurity, but it is unimplemented\n");
694         *ppv = NULL;
695         return E_NOINTERFACE;
696     }
697
698     hr = proxy_manager_find_ifproxy(This, riid, &ifproxy);
699     if (hr == S_OK)
700     {
701         *ppv = ifproxy->iface;
702         IUnknown_AddRef((IUnknown *)*ppv);
703         return S_OK;
704     }
705
706     *ppv = NULL;
707     return E_NOINTERFACE;
708 }
709
710 static HRESULT proxy_manager_create_ifproxy(
711     struct proxy_manager * This, const STDOBJREF *stdobjref, REFIID riid,
712     IRpcChannelBuffer * channel, struct ifproxy ** iif_out)
713 {
714     HRESULT hr;
715     IPSFactoryBuffer * psfb;
716     struct ifproxy * ifproxy = HeapAlloc(GetProcessHeap(), 0, sizeof(*ifproxy));
717     if (!ifproxy) return E_OUTOFMEMORY;
718
719     list_init(&ifproxy->entry);
720
721     ifproxy->parent = This;
722     ifproxy->stdobjref = *stdobjref;
723     ifproxy->iid = *riid;
724     ifproxy->refs = stdobjref->cPublicRefs;
725     ifproxy->proxy = NULL;
726
727     assert(channel);
728     ifproxy->chan = channel; /* FIXME: we should take the binding strings and construct the channel in this function */
729
730     /* the IUnknown interface is special because it does not have a
731      * proxy associated with the ifproxy as we handle IUnknown ourselves */
732     if (IsEqualIID(riid, &IID_IUnknown))
733     {
734         ifproxy->iface = (void *)&This->lpVtbl;
735         IMultiQI_AddRef((IMultiQI *)&This->lpVtbl);
736         hr = S_OK;
737     }
738     else
739     {
740         hr = get_facbuf_for_iid(riid, &psfb);
741         if (hr == S_OK)
742         {
743             /* important note: the outer unknown is set to the proxy manager.
744              * This ensures the COM identity rules are not violated, by having a
745              * one-to-one mapping of objects on the proxy side to objects on the
746              * stub side, no matter which interface you view the object through */
747             hr = IPSFactoryBuffer_CreateProxy(psfb, (IUnknown *)&This->lpVtbl, riid,
748                                               &ifproxy->proxy, &ifproxy->iface);
749             IPSFactoryBuffer_Release(psfb);
750             if (hr != S_OK)
751                 ERR("Could not create proxy for interface %s, error 0x%08lx\n",
752                     debugstr_guid(riid), hr);
753         }
754         else
755             ERR("Could not get IPSFactoryBuffer for interface %s, error 0x%08lx\n",
756                 debugstr_guid(riid), hr);
757
758         if (hr == S_OK)
759             hr = IRpcProxyBuffer_Connect(ifproxy->proxy, ifproxy->chan);
760     }
761
762     /* get at least one external reference to the object to keep it alive */
763     if (hr == S_OK)
764         hr = ifproxy_get_public_ref(ifproxy);
765
766     if (hr == S_OK)
767     {
768         EnterCriticalSection(&This->cs);
769         list_add_tail(&This->interfaces, &ifproxy->entry);
770         LeaveCriticalSection(&This->cs);
771
772         *iif_out = ifproxy;
773         TRACE("ifproxy %p created for IPID %s, interface %s with %lu public refs\n",
774               ifproxy, debugstr_guid(&stdobjref->ipid), debugstr_guid(riid), stdobjref->cPublicRefs);
775     }
776     else
777         ifproxy_destroy(ifproxy);
778
779     return hr;
780 }
781
782 static HRESULT proxy_manager_find_ifproxy(struct proxy_manager * This, REFIID riid, struct ifproxy ** ifproxy_found)
783 {
784     HRESULT hr = E_NOINTERFACE; /* assume not found */
785     struct list * cursor;
786
787     EnterCriticalSection(&This->cs);
788     LIST_FOR_EACH(cursor, &This->interfaces)
789     {
790         struct ifproxy * ifproxy = LIST_ENTRY(cursor, struct ifproxy, entry);
791         if (IsEqualIID(riid, &ifproxy->iid))
792         {
793             *ifproxy_found = ifproxy;
794             hr = S_OK;
795             break;
796         }
797     }
798     LeaveCriticalSection(&This->cs);
799
800     return hr;
801 }
802
803 static void proxy_manager_disconnect(struct proxy_manager * This)
804 {
805     struct list * cursor;
806
807     TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This->oxid),
808         wine_dbgstr_longlong(This->oid));
809
810     EnterCriticalSection(&This->cs);
811
812     /* SORFP_NOLIFTIMEMGMT proxies (for IRemUnknown) shouldn't be
813      * disconnected - it won't do anything anyway, except cause
814      * problems for other objects that depend on this proxy always
815      * working */
816     if (!(This->sorflags & SORFP_NOLIFETIMEMGMT))
817     {
818         LIST_FOR_EACH(cursor, &This->interfaces)
819         {
820             struct ifproxy * ifproxy = LIST_ENTRY(cursor, struct ifproxy, entry);
821             ifproxy_disconnect(ifproxy);
822         }
823     }
824
825     /* apartment is being destroyed so don't keep a pointer around to it */
826     This->parent = NULL;
827
828     LeaveCriticalSection(&This->cs);
829 }
830
831 static HRESULT proxy_manager_get_remunknown(struct proxy_manager * This, IRemUnknown **remunk)
832 {
833     HRESULT hr = S_OK;
834
835     /* we don't want to try and unmarshal or use IRemUnknown if we don't want
836      * lifetime management */
837     if (This->sorflags & SORFP_NOLIFETIMEMGMT)
838         return S_FALSE;
839
840     EnterCriticalSection(&This->cs);
841     if (This->remunk)
842         /* already created - return existing object */
843         *remunk = This->remunk;
844     else if (!This->parent)
845         /* disconnected - we can't create IRemUnknown */
846         hr = S_FALSE;
847     else
848     {
849         STDOBJREF stdobjref;
850         /* Don't want IRemUnknown lifetime management as this is IRemUnknown!
851          * We also don't care about whether or not the stub is still alive */
852         stdobjref.flags = SORFP_NOLIFETIMEMGMT | SORF_NOPING;
853         stdobjref.cPublicRefs = 1;
854         /* oxid of destination object */
855         stdobjref.oxid = This->oxid;
856         /* FIXME: what should be used for the oid? The DCOM draft doesn't say */
857         stdobjref.oid = (OID)-1;
858         /* FIXME: this is a hack around not having an OXID resolver yet -
859          * the OXID resolver should give us the IPID of the IRemUnknown
860          * interface */
861         stdobjref.ipid.Data1 = 0xffffffff;
862         stdobjref.ipid.Data2 = 0xffff;
863         stdobjref.ipid.Data3 = 0xffff;
864         assert(sizeof(stdobjref.ipid.Data4) == sizeof(stdobjref.oxid));
865         memcpy(&stdobjref.ipid.Data4, &stdobjref.oxid, sizeof(OXID));
866         
867         /* do the unmarshal */
868         hr = unmarshal_object(&stdobjref, This->parent, This->dest_context,
869                               This->dest_context_data, &IID_IRemUnknown,
870                               (void**)&This->remunk);
871         if (hr == S_OK)
872             *remunk = This->remunk;
873     }
874     LeaveCriticalSection(&This->cs);
875
876     TRACE("got IRemUnknown* pointer %p, hr = 0x%08lx\n", *remunk, hr);
877
878     return hr;
879 }
880
881 /* destroys a proxy manager, freeing the memory it used.
882  * Note: this function should not be called from a list iteration in the
883  * apartment, due to the fact that it removes itself from the apartment and
884  * it could add a proxy to IRemUnknown into the apartment. */
885 static void proxy_manager_destroy(struct proxy_manager * This)
886 {
887     struct list * cursor;
888
889     TRACE("oxid = %s, oid = %s\n", wine_dbgstr_longlong(This->oxid),
890         wine_dbgstr_longlong(This->oid));
891
892     if (This->parent)
893     {
894         EnterCriticalSection(&This->parent->cs);
895
896         /* remove ourself from the list of proxy objects in the apartment */
897         LIST_FOR_EACH(cursor, &This->parent->proxies)
898         {
899             if (cursor == &This->entry)
900             {
901                 list_remove(&This->entry);
902                 break;
903             }
904         }
905
906         LeaveCriticalSection(&This->parent->cs);
907     }
908
909     /* destroy all of the interface proxies */
910     while ((cursor = list_head(&This->interfaces)))
911     {
912         struct ifproxy * ifproxy = LIST_ENTRY(cursor, struct ifproxy, entry);
913         ifproxy_destroy(ifproxy);
914     }
915
916     if (This->remunk) IRemUnknown_Release(This->remunk);
917
918     DEBUG_CLEAR_CRITSEC_NAME(&This->cs);
919     DeleteCriticalSection(&This->cs);
920
921     CloseHandle(This->remoting_mutex);
922
923     HeapFree(GetProcessHeap(), 0, This);
924 }
925
926 /* finds the proxy manager corresponding to a given OXID and OID that has
927  * been unmarshaled in the specified apartment. The caller must release the
928  * reference to the proxy_manager when the object is no longer used. */
929 static BOOL find_proxy_manager(APARTMENT * apt, OXID oxid, OID oid, struct proxy_manager ** proxy_found)
930 {
931     BOOL found = FALSE;
932     struct list * cursor;
933
934     EnterCriticalSection(&apt->cs);
935     LIST_FOR_EACH(cursor, &apt->proxies)
936     {
937         struct proxy_manager * proxy = LIST_ENTRY(cursor, struct proxy_manager, entry);
938         if ((oxid == proxy->oxid) && (oid == proxy->oid))
939         {
940             *proxy_found = proxy;
941             ClientIdentity_AddRef((IMultiQI *)&proxy->lpVtbl);
942             found = TRUE;
943             break;
944         }
945     }
946     LeaveCriticalSection(&apt->cs);
947     return found;
948 }
949
950 HRESULT apartment_disconnectproxies(struct apartment *apt)
951 {
952     struct list * cursor;
953
954     LIST_FOR_EACH(cursor, &apt->proxies)
955     {
956         struct proxy_manager * proxy = LIST_ENTRY(cursor, struct proxy_manager, entry);
957         proxy_manager_disconnect(proxy);
958     }
959
960     return S_OK;
961 }
962
963 /********************** StdMarshal implementation ****************************/
964 typedef struct _StdMarshalImpl
965 {
966     const IMarshalVtbl  *lpvtbl;
967     LONG                ref;
968
969     IID                 iid;
970     DWORD               dwDestContext;
971     LPVOID              pvDestContext;
972     DWORD               mshlflags;
973 } StdMarshalImpl;
974
975 static HRESULT WINAPI 
976 StdMarshalImpl_QueryInterface(LPMARSHAL iface, REFIID riid, LPVOID *ppv)
977 {
978     *ppv = NULL;
979     if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IMarshal, riid))
980     {
981         *ppv = iface;
982         IUnknown_AddRef(iface);
983         return S_OK;
984     }
985     FIXME("No interface for %s.\n", debugstr_guid(riid));
986     return E_NOINTERFACE;
987 }
988
989 static ULONG WINAPI
990 StdMarshalImpl_AddRef(LPMARSHAL iface)
991 {
992     StdMarshalImpl *This = (StdMarshalImpl *)iface;
993     return InterlockedIncrement(&This->ref);
994 }
995
996 static ULONG WINAPI
997 StdMarshalImpl_Release(LPMARSHAL iface)
998 {
999     StdMarshalImpl *This = (StdMarshalImpl *)iface;
1000     ULONG ref = InterlockedDecrement(&This->ref);
1001
1002     if (!ref) HeapFree(GetProcessHeap(),0,This);
1003     return ref;
1004 }
1005
1006 static HRESULT WINAPI
1007 StdMarshalImpl_GetUnmarshalClass(
1008     LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
1009     void* pvDestContext, DWORD mshlflags, CLSID* pCid)
1010 {
1011     *pCid = CLSID_DfMarshal;
1012     return S_OK;
1013 }
1014
1015 static HRESULT WINAPI
1016 StdMarshalImpl_GetMarshalSizeMax(
1017     LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
1018     void* pvDestContext, DWORD mshlflags, DWORD* pSize)
1019 {
1020     *pSize = sizeof(STDOBJREF);
1021     return S_OK;
1022 }
1023
1024 static HRESULT WINAPI
1025 StdMarshalImpl_MarshalInterface(
1026     LPMARSHAL iface, IStream *pStm,REFIID riid, void* pv, DWORD dwDestContext,
1027     void* pvDestContext, DWORD mshlflags)
1028 {
1029     STDOBJREF             stdobjref;
1030     ULONG                 res;
1031     HRESULT               hres;
1032     APARTMENT            *apt = COM_CurrentApt();
1033
1034     TRACE("(...,%s,...)\n", debugstr_guid(riid));
1035
1036     if (!apt)
1037     {
1038         ERR("Apartment not initialized\n");
1039         return CO_E_NOTINITIALIZED;
1040     }
1041
1042     /* make sure this apartment can be reached from other threads / processes */
1043     RPC_StartRemoting(apt);
1044
1045     hres = marshal_object(apt, &stdobjref, riid, (IUnknown *)pv, mshlflags);
1046     if (hres)
1047     {
1048         ERR("Failed to create ifstub, hres=0x%lx\n", hres);
1049         return hres;
1050     }
1051
1052     hres = IStream_Write(pStm, &stdobjref, sizeof(stdobjref), &res);
1053     if (hres) return hres;
1054
1055     return S_OK;
1056 }
1057
1058 /* helper for StdMarshalImpl_UnmarshalInterface - does the unmarshaling with
1059  * no questions asked about the rules surrounding same-apartment unmarshals
1060  * and table marshaling */
1061 static HRESULT unmarshal_object(const STDOBJREF *stdobjref, APARTMENT *apt,
1062                                 MSHCTX dest_context, void *dest_context_data,
1063                                 REFIID riid, void **object)
1064 {
1065     struct proxy_manager *proxy_manager = NULL;
1066     HRESULT hr = S_OK;
1067
1068     assert(apt);
1069
1070     TRACE("stdobjref:\n\tflags = %04lx\n\tcPublicRefs = %ld\n\toxid = %s\n\toid = %s\n\tipid = %s\n",
1071         stdobjref->flags, stdobjref->cPublicRefs,
1072         wine_dbgstr_longlong(stdobjref->oxid),
1073         wine_dbgstr_longlong(stdobjref->oid),
1074         debugstr_guid(&stdobjref->ipid));
1075
1076     /* create an a new proxy manager if one doesn't already exist for the
1077      * object */
1078     if (!find_proxy_manager(apt, stdobjref->oxid, stdobjref->oid, &proxy_manager))
1079     {
1080         hr = proxy_manager_construct(apt, stdobjref->flags,
1081                                      stdobjref->oxid, stdobjref->oid,
1082                                      &proxy_manager);
1083     }
1084     else
1085         TRACE("proxy manager already created, using\n");
1086
1087     if (hr == S_OK)
1088     {
1089         struct ifproxy * ifproxy;
1090
1091         proxy_manager_set_context(proxy_manager, dest_context, dest_context_data);
1092
1093         hr = proxy_manager_find_ifproxy(proxy_manager, riid, &ifproxy);
1094         if (hr == E_NOINTERFACE)
1095         {
1096             IRpcChannelBuffer *chanbuf;
1097             hr = RPC_CreateClientChannel(&stdobjref->oxid, &stdobjref->ipid,
1098                                          proxy_manager->dest_context,
1099                                          proxy_manager->dest_context_data,
1100                                          &chanbuf);
1101             if (hr == S_OK)
1102                 hr = proxy_manager_create_ifproxy(proxy_manager, stdobjref,
1103                                                   riid, chanbuf, &ifproxy);
1104         }
1105         else
1106             IUnknown_AddRef((IUnknown *)ifproxy->iface);
1107
1108         if (hr == S_OK)
1109             *object = ifproxy->iface;
1110     }
1111
1112     /* release our reference to the proxy manager - the client/apartment
1113      * will hold on to the remaining reference for us */
1114     if (proxy_manager) ClientIdentity_Release((IMultiQI*)&proxy_manager->lpVtbl);
1115
1116     return hr;
1117 }
1118
1119 static HRESULT WINAPI
1120 StdMarshalImpl_UnmarshalInterface(LPMARSHAL iface, IStream *pStm, REFIID riid, void **ppv)
1121 {
1122     StdMarshalImpl *This = (StdMarshalImpl *)iface;
1123     struct stub_manager  *stubmgr;
1124     STDOBJREF stdobjref;
1125     ULONG res;
1126     HRESULT hres;
1127     APARTMENT *apt = COM_CurrentApt();
1128     APARTMENT *stub_apt;
1129     OXID oxid;
1130
1131     TRACE("(...,%s,....)\n", debugstr_guid(riid));
1132
1133     /* we need an apartment to unmarshal into */
1134     if (!apt)
1135     {
1136         ERR("Apartment not initialized\n");
1137         return CO_E_NOTINITIALIZED;
1138     }
1139
1140     /* read STDOBJREF from wire */
1141     hres = IStream_Read(pStm, &stdobjref, sizeof(stdobjref), &res);
1142     if (hres) return STG_E_READFAULT;
1143
1144     hres = apartment_getoxid(apt, &oxid);
1145     if (hres) return hres;
1146
1147     /* check if we're marshalling back to ourselves */
1148     if ((oxid == stdobjref.oxid) && (stubmgr = get_stub_manager(apt, stdobjref.oid)))
1149     {
1150         TRACE("Unmarshalling object marshalled in same apartment for iid %s, "
1151               "returning original object %p\n", debugstr_guid(riid), stubmgr->object);
1152     
1153         hres = IUnknown_QueryInterface(stubmgr->object, riid, ppv);
1154       
1155         /* unref the ifstub. FIXME: only do this on success? */
1156         if (!stub_manager_is_table_marshaled(stubmgr, &stdobjref.ipid))
1157             stub_manager_ext_release(stubmgr, stdobjref.cPublicRefs);
1158
1159         stub_manager_int_release(stubmgr);
1160         return hres;
1161     }
1162
1163     /* notify stub manager about unmarshal if process-local object.
1164      * note: if the oxid is not found then we and native will quite happily
1165      * ignore table marshaling and normal marshaling rules regarding number of
1166      * unmarshals, etc, but if you abuse these rules then your proxy could end
1167      * up returning RPC_E_DISCONNECTED. */
1168     if ((stub_apt = apartment_findfromoxid(stdobjref.oxid, TRUE)))
1169     {
1170         if ((stubmgr = get_stub_manager(stub_apt, stdobjref.oid)))
1171         {
1172             if (!stub_manager_notify_unmarshal(stubmgr, &stdobjref.ipid))
1173                 hres = CO_E_OBJNOTCONNECTED;
1174
1175             stub_manager_int_release(stubmgr);
1176         }
1177         else
1178         {
1179             WARN("Couldn't find object for OXID %s, OID %s, assuming disconnected\n",
1180                 wine_dbgstr_longlong(stdobjref.oxid),
1181                 wine_dbgstr_longlong(stdobjref.oid));
1182             hres = CO_E_OBJNOTCONNECTED;
1183         }
1184
1185         apartment_release(stub_apt);
1186     }
1187     else
1188         TRACE("Treating unmarshal from OXID %s as inter-process\n",
1189             wine_dbgstr_longlong(stdobjref.oxid));
1190
1191     if (hres == S_OK)
1192         hres = unmarshal_object(&stdobjref, apt, This->dwDestContext,
1193                                 This->pvDestContext, riid, ppv);
1194
1195     if (hres) WARN("Failed with error 0x%08lx\n", hres);
1196     else TRACE("Successfully created proxy %p\n", *ppv);
1197
1198     return hres;
1199 }
1200
1201 static HRESULT WINAPI
1202 StdMarshalImpl_ReleaseMarshalData(LPMARSHAL iface, IStream *pStm)
1203 {
1204     STDOBJREF            stdobjref;
1205     ULONG                res;
1206     HRESULT              hres;
1207     struct stub_manager *stubmgr;
1208     APARTMENT           *apt;
1209
1210     TRACE("iface=%p, pStm=%p\n", iface, pStm);
1211     
1212     hres = IStream_Read(pStm, &stdobjref, sizeof(stdobjref), &res);
1213     if (hres) return STG_E_READFAULT;
1214
1215     TRACE("oxid = %s, oid = %s, ipid = %s\n",
1216         wine_dbgstr_longlong(stdobjref.oxid),
1217         wine_dbgstr_longlong(stdobjref.oid),
1218         wine_dbgstr_guid(&stdobjref.ipid));
1219
1220     if (!(apt = apartment_findfromoxid(stdobjref.oxid, TRUE)))
1221     {
1222         WARN("Could not map OXID %s to apartment object\n",
1223             wine_dbgstr_longlong(stdobjref.oxid));
1224         return RPC_E_INVALID_OBJREF;
1225     }
1226
1227     if (!(stubmgr = get_stub_manager(apt, stdobjref.oid)))
1228     {
1229         ERR("could not map object ID to stub manager, oxid=%s, oid=%s\n",
1230             wine_dbgstr_longlong(stdobjref.oxid), wine_dbgstr_longlong(stdobjref.oid));
1231         return RPC_E_INVALID_OBJREF;
1232     }
1233
1234     stub_manager_release_marshal_data(stubmgr, stdobjref.cPublicRefs, &stdobjref.ipid);
1235
1236     stub_manager_int_release(stubmgr);
1237     apartment_release(apt);
1238
1239     return S_OK;
1240 }
1241
1242 static HRESULT WINAPI
1243 StdMarshalImpl_DisconnectObject(LPMARSHAL iface, DWORD dwReserved)
1244 {
1245     FIXME("(), stub!\n");
1246     return S_OK;
1247 }
1248
1249 static const IMarshalVtbl VT_StdMarshal =
1250 {
1251     StdMarshalImpl_QueryInterface,
1252     StdMarshalImpl_AddRef,
1253     StdMarshalImpl_Release,
1254     StdMarshalImpl_GetUnmarshalClass,
1255     StdMarshalImpl_GetMarshalSizeMax,
1256     StdMarshalImpl_MarshalInterface,
1257     StdMarshalImpl_UnmarshalInterface,
1258     StdMarshalImpl_ReleaseMarshalData,
1259     StdMarshalImpl_DisconnectObject
1260 };
1261
1262 static HRESULT StdMarshalImpl_Construct(REFIID riid, void** ppvObject)
1263 {
1264     StdMarshalImpl * pStdMarshal = 
1265         HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(StdMarshalImpl));
1266     if (!pStdMarshal)
1267         return E_OUTOFMEMORY;
1268     pStdMarshal->lpvtbl = &VT_StdMarshal;
1269     pStdMarshal->ref = 0;
1270     return IMarshal_QueryInterface((IMarshal*)pStdMarshal, riid, ppvObject);
1271 }
1272
1273 /***********************************************************************
1274  *              CoGetStandardMarshal    [OLE32.@]
1275  *
1276  * Gets or creates a standard marshal object.
1277  *
1278  * PARAMS
1279  *  riid          [I] Interface identifier of the pUnk object.
1280  *  pUnk          [I] Optional. Object to get the marshal object for.
1281  *  dwDestContext [I] Destination. Used to enable or disable optimizations.
1282  *  pvDestContext [I] Reserved. Must be NULL.
1283  *  mshlflags     [I] Flags affecting the marshaling process.
1284  *  ppMarshal     [O] Address where marshal object will be stored.
1285  *
1286  * RETURNS
1287  *  Success: S_OK.
1288  *  Failure: HRESULT code.
1289  *
1290  * NOTES
1291  *
1292  * The function retrieves the IMarshal object associated with an object if
1293  * that object is currently an active stub, otherwise a new marshal object is
1294  * created.
1295  */
1296 HRESULT WINAPI CoGetStandardMarshal(REFIID riid, IUnknown *pUnk,
1297                                     DWORD dwDestContext, LPVOID pvDestContext,
1298                                     DWORD mshlflags, LPMARSHAL *ppMarshal)
1299 {
1300     StdMarshalImpl *dm;
1301
1302     if (pUnk == NULL)
1303     {
1304         FIXME("(%s,NULL,%lx,%p,%lx,%p), unimplemented yet.\n",
1305             debugstr_guid(riid),dwDestContext,pvDestContext,mshlflags,ppMarshal);
1306         return E_NOTIMPL;
1307     }
1308     TRACE("(%s,%p,%lx,%p,%lx,%p)\n",
1309         debugstr_guid(riid),pUnk,dwDestContext,pvDestContext,mshlflags,ppMarshal);
1310     *ppMarshal = HeapAlloc(GetProcessHeap(),0,sizeof(StdMarshalImpl));
1311     dm = (StdMarshalImpl*) *ppMarshal;
1312     if (!dm) return E_FAIL;
1313     dm->lpvtbl          = &VT_StdMarshal;
1314     dm->ref             = 1;
1315
1316     dm->iid             = *riid;
1317     dm->dwDestContext   = dwDestContext;
1318     dm->pvDestContext   = pvDestContext;
1319     dm->mshlflags       = mshlflags;
1320     return S_OK;
1321 }
1322
1323 /***********************************************************************
1324  *              get_marshaler   [internal]
1325  *
1326  * Retrieves an IMarshal interface for an object.
1327  */
1328 static HRESULT get_marshaler(REFIID riid, IUnknown *pUnk, DWORD dwDestContext,
1329                              void *pvDestContext, DWORD mshlFlags,
1330                              LPMARSHAL *pMarshal)
1331 {
1332     HRESULT hr;
1333
1334     if (!pUnk)
1335         return E_POINTER;
1336     hr = IUnknown_QueryInterface(pUnk, &IID_IMarshal, (LPVOID*)pMarshal);
1337     if (hr)
1338         hr = CoGetStandardMarshal(riid, pUnk, dwDestContext, pvDestContext,
1339                                   mshlFlags, pMarshal);
1340     return hr;
1341 }
1342
1343 /***********************************************************************
1344  *              get_unmarshaler_from_stream     [internal]
1345  *
1346  * Creates an IMarshal* object according to the data marshaled to the stream.
1347  * The function leaves the stream pointer at the start of the data written
1348  * to the stream by the IMarshal* object.
1349  */
1350 static HRESULT get_unmarshaler_from_stream(IStream *stream, IMarshal **marshal, IID *iid)
1351 {
1352     HRESULT hr;
1353     ULONG res;
1354     OBJREF objref;
1355
1356     /* read common OBJREF header */
1357     hr = IStream_Read(stream, &objref, FIELD_OFFSET(OBJREF, u_objref), &res);
1358     if (hr || (res != FIELD_OFFSET(OBJREF, u_objref)))
1359     {
1360         ERR("Failed to read common OBJREF header, 0x%08lx\n", hr);
1361         return STG_E_READFAULT;
1362     }
1363
1364     /* sanity check on header */
1365     if (objref.signature != OBJREF_SIGNATURE)
1366     {
1367         ERR("Bad OBJREF signature 0x%08lx\n", objref.signature);
1368         return RPC_E_INVALID_OBJREF;
1369     }
1370
1371     if (iid) *iid = objref.iid;
1372
1373     /* FIXME: handler marshaling */
1374     if (objref.flags & OBJREF_STANDARD)
1375     {
1376         TRACE("Using standard unmarshaling\n");
1377         hr = StdMarshalImpl_Construct(&IID_IMarshal, (LPVOID*)marshal);
1378     }
1379     else if (objref.flags & OBJREF_CUSTOM)
1380     {
1381         ULONG custom_header_size = FIELD_OFFSET(OBJREF, u_objref.u_custom.pData) - 
1382                                    FIELD_OFFSET(OBJREF, u_objref.u_custom);
1383         TRACE("Using custom unmarshaling\n");
1384         /* read constant sized OR_CUSTOM data from stream */
1385         hr = IStream_Read(stream, &objref.u_objref.u_custom,
1386                           custom_header_size, &res);
1387         if (hr || (res != custom_header_size))
1388         {
1389             ERR("Failed to read OR_CUSTOM header, 0x%08lx\n", hr);
1390             return STG_E_READFAULT;
1391         }
1392         /* now create the marshaler specified in the stream */
1393         hr = CoCreateInstance(&objref.u_objref.u_custom.clsid, NULL,
1394                               CLSCTX_INPROC_SERVER, &IID_IMarshal,
1395                               (LPVOID*)marshal);
1396     }
1397     else
1398     {
1399         FIXME("Invalid or unimplemented marshaling type specified: %lx\n",
1400             objref.flags);
1401         return RPC_E_INVALID_OBJREF;
1402     }
1403
1404     if (hr)
1405         ERR("Failed to create marshal, 0x%08lx\n", hr);
1406
1407     return hr;
1408 }
1409
1410 /***********************************************************************
1411  *              CoGetMarshalSizeMax     [OLE32.@]
1412  *
1413  * Gets the maximum amount of data that will be needed by a marshal.
1414  *
1415  * PARAMS
1416  *  pulSize       [O] Address where maximum marshal size will be stored.
1417  *  riid          [I] Identifier of the interface to marshal.
1418  *  pUnk          [I] Pointer to the object to marshal.
1419  *  dwDestContext [I] Destination. Used to enable or disable optimizations.
1420  *  pvDestContext [I] Reserved. Must be NULL.
1421  *  mshlFlags     [I] Flags that affect the marshaling. See CoMarshalInterface().
1422  *
1423  * RETURNS
1424  *  Success: S_OK.
1425  *  Failure: HRESULT code.
1426  *
1427  * SEE ALSO
1428  *  CoMarshalInterface().
1429  */
1430 HRESULT WINAPI CoGetMarshalSizeMax(ULONG *pulSize, REFIID riid, IUnknown *pUnk,
1431                                    DWORD dwDestContext, void *pvDestContext,
1432                                    DWORD mshlFlags)
1433 {
1434     HRESULT hr;
1435     LPMARSHAL pMarshal;
1436     CLSID marshaler_clsid;
1437
1438     hr = get_marshaler(riid, pUnk, dwDestContext, pvDestContext, mshlFlags, &pMarshal);
1439     if (hr)
1440         return hr;
1441
1442     hr = IMarshal_GetUnmarshalClass(pMarshal, riid, pUnk, dwDestContext,
1443                                     pvDestContext, mshlFlags, &marshaler_clsid);
1444     if (hr)
1445     {
1446         ERR("IMarshal::GetUnmarshalClass failed, 0x%08lx\n", hr);
1447         IMarshal_Release(pMarshal);
1448         return hr;
1449     }
1450
1451     hr = IMarshal_GetMarshalSizeMax(pMarshal, riid, pUnk, dwDestContext,
1452                                     pvDestContext, mshlFlags, pulSize);
1453     /* add on the size of the common header */
1454     *pulSize += FIELD_OFFSET(OBJREF, u_objref);
1455
1456     /* if custom marshaling, add on size of custom header */
1457     if (!IsEqualCLSID(&marshaler_clsid, &CLSID_DfMarshal))
1458         *pulSize += FIELD_OFFSET(OBJREF, u_objref.u_custom.pData) - 
1459                     FIELD_OFFSET(OBJREF, u_objref.u_custom);
1460
1461     IMarshal_Release(pMarshal);
1462     return hr;
1463 }
1464
1465
1466 static void dump_MSHLFLAGS(MSHLFLAGS flags)
1467 {
1468     if (flags & MSHLFLAGS_TABLESTRONG)
1469         TRACE(" MSHLFLAGS_TABLESTRONG");
1470     if (flags & MSHLFLAGS_TABLEWEAK)
1471         TRACE(" MSHLFLAGS_TABLEWEAK");
1472     if (!(flags & (MSHLFLAGS_TABLESTRONG|MSHLFLAGS_TABLEWEAK)))
1473         TRACE(" MSHLFLAGS_NORMAL");
1474     if (flags & MSHLFLAGS_NOPING)
1475         TRACE(" MSHLFLAGS_NOPING");
1476 }
1477
1478 /***********************************************************************
1479  *              CoMarshalInterface      [OLE32.@]
1480  *
1481  * Marshals an interface into a stream so that the object can then be
1482  * unmarshaled from another COM apartment and used remotely.
1483  *
1484  * PARAMS
1485  *  pStream       [I] Stream the object will be marshaled into.
1486  *  riid          [I] Identifier of the interface to marshal.
1487  *  pUnk          [I] Pointer to the object to marshal.
1488  *  dwDestContext [I] Destination. Used to enable or disable optimizations.
1489  *  pvDestContext [I] Reserved. Must be NULL.
1490  *  mshlFlags     [I] Flags that affect the marshaling. See notes.
1491  *
1492  * RETURNS
1493  *  Success: S_OK.
1494  *  Failure: HRESULT code.
1495  *
1496  * NOTES
1497  *
1498  * The mshlFlags parameter can take one or more of the following flags:
1499  *| MSHLFLAGS_NORMAL - Unmarshal once, releases stub on last proxy release.
1500  *| MSHLFLAGS_TABLESTRONG - Unmarshal many, release when CoReleaseMarshalData() called.
1501  *| MSHLFLAGS_TABLEWEAK - Unmarshal many, releases stub on last proxy release.
1502  *| MSHLFLAGS_NOPING - No automatic garbage collection (and so reduces network traffic).
1503  *
1504  * If a marshaled object is not unmarshaled, then CoReleaseMarshalData() must
1505  * be called in order to release the resources used in the marshaling.
1506  *
1507  * SEE ALSO
1508  *  CoUnmarshalInterface(), CoReleaseMarshalData().
1509  */
1510 HRESULT WINAPI CoMarshalInterface(IStream *pStream, REFIID riid, IUnknown *pUnk,
1511                                   DWORD dwDestContext, void *pvDestContext,
1512                                   DWORD mshlFlags)
1513 {
1514     HRESULT     hr;
1515     CLSID marshaler_clsid;
1516     OBJREF objref;
1517     LPMARSHAL pMarshal;
1518
1519     TRACE("(%p, %s, %p, %lx, %p,", pStream, debugstr_guid(riid), pUnk,
1520         dwDestContext, pvDestContext);
1521     dump_MSHLFLAGS(mshlFlags);
1522     TRACE(")\n");
1523
1524     if (pUnk == NULL)
1525         return E_INVALIDARG;
1526
1527     objref.signature = OBJREF_SIGNATURE;
1528     objref.iid = *riid;
1529
1530     /* get the marshaler for the specified interface */
1531     hr = get_marshaler(riid, pUnk, dwDestContext, pvDestContext, mshlFlags, &pMarshal);
1532     if (hr)
1533     {
1534         ERR("Failed to get marshaller, 0x%08lx\n", hr);
1535         return hr;
1536     }
1537
1538     hr = IMarshal_GetUnmarshalClass(pMarshal, riid, pUnk, dwDestContext,
1539                                     pvDestContext, mshlFlags, &marshaler_clsid);
1540     if (hr)
1541     {
1542         ERR("IMarshal::GetUnmarshalClass failed, 0x%08lx\n", hr);
1543         goto cleanup;
1544     }
1545
1546     /* FIXME: implement handler marshaling too */
1547     if (IsEqualCLSID(&marshaler_clsid, &CLSID_DfMarshal))
1548     {
1549         TRACE("Using standard marshaling\n");
1550         objref.flags = OBJREF_STANDARD;
1551
1552         /* write the common OBJREF header to the stream */
1553         hr = IStream_Write(pStream, &objref, FIELD_OFFSET(OBJREF, u_objref), NULL);
1554         if (hr)
1555         {
1556             ERR("Failed to write OBJREF header to stream, 0x%08lx\n", hr);
1557             goto cleanup;
1558         }
1559     }
1560     else
1561     {
1562         TRACE("Using custom marshaling\n");
1563         objref.flags = OBJREF_CUSTOM;
1564         objref.u_objref.u_custom.clsid = marshaler_clsid;
1565         objref.u_objref.u_custom.cbExtension = 0;
1566         objref.u_objref.u_custom.size = 0;
1567         hr = IMarshal_GetMarshalSizeMax(pMarshal, riid, pUnk, dwDestContext,
1568                                         pvDestContext, mshlFlags,
1569                                         &objref.u_objref.u_custom.size);
1570         if (hr)
1571         {
1572             ERR("Failed to get max size of marshal data, error 0x%08lx\n", hr);
1573             goto cleanup;
1574         }
1575         /* write constant sized common header and OR_CUSTOM data into stream */
1576         hr = IStream_Write(pStream, &objref,
1577                           FIELD_OFFSET(OBJREF, u_objref.u_custom.pData), NULL);
1578         if (hr)
1579         {
1580             ERR("Failed to write OR_CUSTOM header to stream with 0x%08lx\n", hr);
1581             goto cleanup;
1582         }
1583     }
1584
1585     TRACE("Calling IMarshal::MarshalInterace\n");
1586     /* call helper object to do the actual marshaling */
1587     hr = IMarshal_MarshalInterface(pMarshal, pStream, riid, pUnk, dwDestContext,
1588                                    pvDestContext, mshlFlags);
1589
1590     if (hr)
1591     {
1592         ERR("Failed to marshal the interface %s, %lx\n", debugstr_guid(riid), hr);
1593         goto cleanup;
1594     }
1595
1596 cleanup:
1597     IMarshal_Release(pMarshal);
1598
1599     TRACE("completed with hr 0x%08lx\n", hr);
1600     
1601     return hr;
1602 }
1603
1604 /***********************************************************************
1605  *              CoUnmarshalInterface    [OLE32.@]
1606  *
1607  * Unmarshals an object from a stream by creating a proxy to the remote
1608  * object, if necessary.
1609  *
1610  * PARAMS
1611  *
1612  *  pStream [I] Stream containing the marshaled object.
1613  *  riid    [I] Interface identifier of the object to create a proxy to.
1614  *  ppv     [O] Address where proxy will be stored.
1615  *
1616  * RETURNS
1617  *
1618  *  Success: S_OK.
1619  *  Failure: HRESULT code.
1620  *
1621  * SEE ALSO
1622  *  CoMarshalInterface().
1623  */
1624 HRESULT WINAPI CoUnmarshalInterface(IStream *pStream, REFIID riid, LPVOID *ppv)
1625 {
1626     HRESULT hr;
1627     LPMARSHAL pMarshal;
1628     IID iid;
1629     IUnknown *object;
1630
1631     TRACE("(%p, %s, %p)\n", pStream, debugstr_guid(riid), ppv);
1632
1633     hr = get_unmarshaler_from_stream(pStream, &pMarshal, &iid);
1634     if (hr != S_OK)
1635         return hr;
1636
1637     /* call the helper object to do the actual unmarshaling */
1638     hr = IMarshal_UnmarshalInterface(pMarshal, pStream, &iid, (LPVOID*)&object);
1639     if (hr)
1640         ERR("IMarshal::UnmarshalInterface failed, 0x%08lx\n", hr);
1641
1642     /* IID_NULL means use the interface ID of the marshaled object */
1643     if (!IsEqualIID(riid, &IID_NULL))
1644         iid = *riid;
1645
1646     if (hr == S_OK)
1647     {
1648         if (!IsEqualIID(riid, &iid))
1649         {
1650             TRACE("requested interface != marshalled interface, additional QI needed\n");
1651             hr = IUnknown_QueryInterface(object, &iid, ppv);
1652             if (hr)
1653                 ERR("Couldn't query for interface %s, hr = 0x%08lx\n",
1654                     debugstr_guid(riid), hr);
1655             IUnknown_Release(object);
1656         }
1657         else
1658         {
1659             *ppv = object;
1660         }
1661     }
1662
1663     IMarshal_Release(pMarshal);
1664
1665     TRACE("completed with hr 0x%lx\n", hr);
1666     
1667     return hr;
1668 }
1669
1670 /***********************************************************************
1671  *              CoReleaseMarshalData    [OLE32.@]
1672  *
1673  * Releases resources associated with an object that has been marshaled into
1674  * a stream.
1675  *
1676  * PARAMS
1677  *
1678  *  pStream [I] The stream that the object has been marshaled into.
1679  *
1680  * RETURNS
1681  *  Success: S_OK.
1682  *  Failure: HRESULT error code.
1683  *
1684  * NOTES
1685  * 
1686  * Call this function to release resources associated with a normal or
1687  * table-weak marshal that will not be unmarshaled, and all table-strong
1688  * marshals when they are no longer needed.
1689  *
1690  * SEE ALSO
1691  *  CoMarshalInterface(), CoUnmarshalInterface().
1692  */
1693 HRESULT WINAPI CoReleaseMarshalData(IStream *pStream)
1694 {
1695     HRESULT     hr;
1696     LPMARSHAL pMarshal;
1697
1698     TRACE("(%p)\n", pStream);
1699
1700     hr = get_unmarshaler_from_stream(pStream, &pMarshal, NULL);
1701     if (hr != S_OK)
1702         return hr;
1703
1704     /* call the helper object to do the releasing of marshal data */
1705     hr = IMarshal_ReleaseMarshalData(pMarshal, pStream);
1706     if (hr)
1707         ERR("IMarshal::ReleaseMarshalData failed with error 0x%08lx\n", hr);
1708
1709     IMarshal_Release(pMarshal);
1710     return hr;
1711 }
1712
1713
1714 /***********************************************************************
1715  *              CoMarshalInterThreadInterfaceInStream   [OLE32.@]
1716  *
1717  * Marshal an interface across threads in the same process.
1718  *
1719  * PARAMS
1720  *  riid  [I] Identifier of the interface to be marshalled.
1721  *  pUnk  [I] Pointer to IUnknown-derived interface that will be marshalled.
1722  *  ppStm [O] Pointer to IStream object that is created and then used to store the marshalled inteface.
1723  *
1724  * RETURNS
1725  *  Success: S_OK
1726  *  Failure: E_OUTOFMEMORY and other COM error codes
1727  *
1728  * SEE ALSO
1729  *   CoMarshalInterface(), CoUnmarshalInterface() and CoGetInterfaceAndReleaseStream()
1730  */
1731 HRESULT WINAPI CoMarshalInterThreadInterfaceInStream(
1732     REFIID riid, LPUNKNOWN pUnk, LPSTREAM * ppStm)
1733 {
1734     ULARGE_INTEGER      xpos;
1735     LARGE_INTEGER               seekto;
1736     HRESULT             hres;
1737
1738     TRACE("(%s, %p, %p)\n",debugstr_guid(riid), pUnk, ppStm);
1739
1740     hres = CreateStreamOnHGlobal(NULL, TRUE, ppStm);
1741     if (FAILED(hres)) return hres;
1742     hres = CoMarshalInterface(*ppStm, riid, pUnk, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
1743
1744     if (SUCCEEDED(hres))
1745     {
1746         memset(&seekto, 0, sizeof(seekto));
1747         IStream_Seek(*ppStm, seekto, SEEK_SET, &xpos);
1748     }
1749     else
1750     {
1751         IStream_Release(*ppStm);
1752         *ppStm = NULL;
1753     }
1754
1755     return hres;
1756 }
1757
1758 /***********************************************************************
1759  *              CoGetInterfaceAndReleaseStream  [OLE32.@]
1760  *
1761  * Unmarshalls an inteface from a stream and then releases the stream.
1762  *
1763  * PARAMS
1764  *  pStm [I] Stream that contains the marshalled inteface.
1765  *  riid [I] Interface identifier of the object to unmarshall.
1766  *  ppv  [O] Address of pointer where the requested interface object will be stored.
1767  *
1768  * RETURNS
1769  *  Success: S_OK
1770  *  Failure: A COM error code
1771  *
1772  * SEE ALSO
1773  *  CoMarshalInterThreadInterfaceInStream() and CoUnmarshalInteface()
1774  */
1775 HRESULT WINAPI CoGetInterfaceAndReleaseStream(LPSTREAM pStm, REFIID riid,
1776                                               LPVOID *ppv)
1777 {
1778     HRESULT hres;
1779
1780     TRACE("(%p, %s, %p)\n", pStm, debugstr_guid(riid), ppv);
1781
1782     hres = CoUnmarshalInterface(pStm, riid, ppv);
1783     IStream_Release(pStm);
1784     return hres;
1785 }
1786
1787 static HRESULT WINAPI StdMarshalCF_QueryInterface(LPCLASSFACTORY iface,
1788                                                   REFIID riid, LPVOID *ppv)
1789 {
1790     *ppv = NULL;
1791     if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory))
1792     {
1793         *ppv = (LPVOID)iface;
1794         return S_OK;
1795     }
1796     return E_NOINTERFACE;
1797 }
1798
1799 static ULONG WINAPI StdMarshalCF_AddRef(LPCLASSFACTORY iface)
1800 {
1801     return 2; /* non-heap based object */
1802 }
1803
1804 static ULONG WINAPI StdMarshalCF_Release(LPCLASSFACTORY iface)
1805 {
1806     return 1; /* non-heap based object */
1807 }
1808
1809 static HRESULT WINAPI StdMarshalCF_CreateInstance(LPCLASSFACTORY iface,
1810     LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv)
1811 {
1812     if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IMarshal))
1813         return StdMarshalImpl_Construct(riid, ppv);
1814
1815     FIXME("(%s), not supported.\n",debugstr_guid(riid));
1816     return E_NOINTERFACE;
1817 }
1818
1819 static HRESULT WINAPI StdMarshalCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
1820 {
1821     FIXME("(%d), stub!\n",fLock);
1822     return S_OK;
1823 }
1824
1825 static const IClassFactoryVtbl StdMarshalCFVtbl =
1826 {
1827     StdMarshalCF_QueryInterface,
1828     StdMarshalCF_AddRef,
1829     StdMarshalCF_Release,
1830     StdMarshalCF_CreateInstance,
1831     StdMarshalCF_LockServer
1832 };
1833 static const IClassFactoryVtbl *StdMarshalCF = &StdMarshalCFVtbl;
1834
1835 HRESULT MARSHAL_GetStandardMarshalCF(LPVOID *ppv)
1836 {
1837     *ppv = &StdMarshalCF;
1838     return S_OK;
1839 }
1840
1841 /***********************************************************************
1842  *              CoMarshalHresult        [OLE32.@]
1843  *
1844  * Marshals an HRESULT value into a stream.
1845  *
1846  * PARAMS
1847  *  pStm    [I] Stream that hresult will be marshalled into.
1848  *  hresult [I] HRESULT to be marshalled.
1849  *
1850  * RETURNS
1851  *  Success: S_OK
1852  *  Failure: A COM error code
1853  *
1854  * SEE ALSO
1855  *  CoUnmarshalHresult().
1856  */
1857 HRESULT WINAPI CoMarshalHresult(LPSTREAM pStm, HRESULT hresult)
1858 {
1859     return IStream_Write(pStm, &hresult, sizeof(hresult), NULL);
1860 }
1861
1862 /***********************************************************************
1863  *              CoUnmarshalHresult      [OLE32.@]
1864  *
1865  * Unmarshals an HRESULT value from a stream.
1866  *
1867  * PARAMS
1868  *  pStm     [I] Stream that hresult will be unmarshalled from.
1869  *  phresult [I] Pointer to HRESULT where the value will be unmarshalled to.
1870  *
1871  * RETURNS
1872  *  Success: S_OK
1873  *  Failure: A COM error code
1874  *
1875  * SEE ALSO
1876  *  CoMarshalHresult().
1877  */
1878 HRESULT WINAPI CoUnmarshalHresult(LPSTREAM pStm, HRESULT * phresult)
1879 {
1880     return IStream_Read(pStm, phresult, sizeof(*phresult), NULL);
1881 }