Implement A->W call for GetNamedSecurityInfo.
[wine] / dlls / ole32 / marshal.c
1 /*
2  *      Marshalling library
3  *
4  *  Copyright 2002  Marcus Meissner
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <assert.h>
28
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winuser.h"
32 #include "objbase.h"
33 #include "ole2.h"
34 #include "ole2ver.h"
35 #include "rpc.h"
36 #include "winerror.h"
37 #include "winreg.h"
38 #include "wownt32.h"
39 #include "wtypes.h"
40 #include "wine/unicode.h"
41 #include "wine/winbase16.h"
42 #include "compobj_private.h"
43 #include "ifs.h"
44
45 #include "wine/debug.h"
46
47 WINE_DEFAULT_DEBUG_CHANNEL(ole);
48
49 extern const CLSID CLSID_DfMarshal;
50
51 /* Marshalling just passes a unique identifier to the remote client,
52  * that makes it possible to find the passed interface again.
53  *
54  * So basically we need a set of values that make it unique.
55  *
56  *      Process Identifier, Object IUnknown ptr, IID
57  *
58  * Note that the IUnknown_QI(ob,xiid,&ppv) always returns the SAME ppv value!
59  */
60
61 inline static HRESULT
62 get_facbuf_for_iid(REFIID riid,IPSFactoryBuffer **facbuf) {
63     HRESULT       hres;
64     CLSID         pxclsid;
65
66     if ((hres = CoGetPSClsid(riid,&pxclsid)))
67         return hres;
68     return CoGetClassObject(&pxclsid,CLSCTX_INPROC_SERVER,NULL,&IID_IPSFactoryBuffer,(LPVOID*)facbuf);
69 }
70
71 typedef struct _wine_marshal_data {
72     DWORD       dwDestContext;
73     DWORD       mshlflags;
74 } wine_marshal_data;
75
76 typedef struct _mid2unknown {
77     wine_marshal_id     mid;
78     LPUNKNOWN           pUnk;
79 } mid2unknown;
80
81 typedef struct _mid2stub {
82     wine_marshal_id     mid;
83     IRpcStubBuffer      *stub;
84     LPUNKNOWN           pUnkServer;
85     BOOL               valid;
86 } mid2stub;
87
88 static mid2stub *stubs = NULL;
89 static int nrofstubs = 0;
90
91 static mid2unknown *proxies = NULL;
92 static int nrofproxies = 0;
93
94 void MARSHAL_Invalidate_Stub_From_MID(wine_marshal_id *mid) {
95     int i;
96
97     for (i=0;i<nrofstubs;i++) {
98         if (!stubs[i].valid) continue;
99         
100         if (MARSHAL_Compare_Mids(mid,&(stubs[i].mid))) {
101             stubs[i].valid = FALSE;
102             return;
103         }
104     }
105     
106     return;
107 }
108
109 HRESULT
110 MARSHAL_Find_Stub_Buffer(wine_marshal_id *mid,IRpcStubBuffer **stub) {
111     int i;
112
113     for (i=0;i<nrofstubs;i++) {
114        if (!stubs[i].valid) continue;
115
116         if (MARSHAL_Compare_Mids(mid,&(stubs[i].mid))) {
117             *stub = stubs[i].stub;
118             IUnknown_AddRef((*stub));
119             return S_OK;
120         }
121     }
122     return E_FAIL;
123 }
124
125 static HRESULT
126 MARSHAL_Find_Stub(wine_marshal_id *mid,LPUNKNOWN *pUnk) {
127     int i;
128
129     for (i=0;i<nrofstubs;i++) {
130        if (!stubs[i].valid) continue;
131
132         if (MARSHAL_Compare_Mids(mid,&(stubs[i].mid))) {
133             *pUnk = stubs[i].pUnkServer;
134             IUnknown_AddRef((*pUnk));
135             return S_OK;
136         }
137     }
138     return E_FAIL;
139 }
140
141 static HRESULT
142 MARSHAL_Register_Stub(wine_marshal_id *mid,LPUNKNOWN pUnk,IRpcStubBuffer *stub) {
143     LPUNKNOWN   xPunk;
144     if (!MARSHAL_Find_Stub(mid,&xPunk)) {
145         FIXME("Already have entry for (%lx/%s)!\n",mid->objectid,debugstr_guid(&(mid->iid)));
146         return S_OK;
147     }
148     if (nrofstubs)
149         stubs=HeapReAlloc(GetProcessHeap(),0,stubs,sizeof(stubs[0])*(nrofstubs+1));
150     else
151         stubs=HeapAlloc(GetProcessHeap(),0,sizeof(stubs[0]));
152     if (!stubs) return E_OUTOFMEMORY;
153     stubs[nrofstubs].stub = stub;
154     stubs[nrofstubs].pUnkServer = pUnk;
155     memcpy(&(stubs[nrofstubs].mid),mid,sizeof(*mid));
156     stubs[nrofstubs].valid = TRUE; /* set to false when released by ReleaseMarshalData */
157     nrofstubs++;
158     return S_OK;
159 }
160
161 HRESULT
162 MARSHAL_Disconnect_Proxies() {
163     int i;
164
165     TRACE("Disconnecting %d proxies\n", nrofproxies);
166
167     for (i = 0; i < nrofproxies; i++)
168         IRpcProxyBuffer_Disconnect((IRpcProxyBuffer*)proxies[i].pUnk);
169     
170     return S_OK;
171 }
172
173 static HRESULT
174 MARSHAL_Register_Proxy(wine_marshal_id *mid,LPUNKNOWN punk) {
175     int i;
176
177     for (i=0;i<nrofproxies;i++) {
178         if (MARSHAL_Compare_Mids(mid,&(proxies[i].mid))) {
179             ERR("Already have mid?\n");
180             return E_FAIL;
181         }
182     }
183     if (nrofproxies)
184         proxies = HeapReAlloc(GetProcessHeap(),0,proxies,sizeof(proxies[0])*(nrofproxies+1));
185     else
186         proxies = HeapAlloc(GetProcessHeap(),0,sizeof(proxies[0]));
187     memcpy(&(proxies[nrofproxies].mid),mid,sizeof(*mid));
188     proxies[nrofproxies].pUnk = punk;
189     nrofproxies++;
190     IUnknown_AddRef(punk);
191     return S_OK;
192 }
193
194 /********************** StdMarshal implementation ****************************/
195 typedef struct _StdMarshalImpl {
196   ICOM_VTABLE(IMarshal) *lpvtbl;
197   DWORD                 ref;
198
199   IID                   iid;
200   DWORD                 dwDestContext;
201   LPVOID                pvDestContext;
202   DWORD                 mshlflags;
203 } StdMarshalImpl;
204
205 static HRESULT WINAPI
206 StdMarshalImpl_QueryInterface(LPMARSHAL iface,REFIID riid,LPVOID *ppv) {
207   *ppv = NULL;
208   if (IsEqualIID(&IID_IUnknown,riid) || IsEqualIID(&IID_IMarshal,riid)) {
209     *ppv = iface;
210     IUnknown_AddRef(iface);
211     return S_OK;
212   }
213   FIXME("No interface for %s.\n",debugstr_guid(riid));
214   return E_NOINTERFACE;
215 }
216
217 static ULONG WINAPI
218 StdMarshalImpl_AddRef(LPMARSHAL iface) {
219   ICOM_THIS(StdMarshalImpl,iface);
220   This->ref++;
221   return This->ref;
222 }
223
224 static ULONG WINAPI
225 StdMarshalImpl_Release(LPMARSHAL iface) {
226   ICOM_THIS(StdMarshalImpl,iface);
227   This->ref--;
228
229   if (This->ref)
230     return This->ref;
231   HeapFree(GetProcessHeap(),0,This);
232   return 0;
233 }
234
235 static HRESULT WINAPI
236 StdMarshalImpl_GetUnmarshalClass(
237   LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
238   void* pvDestContext, DWORD mshlflags, CLSID* pCid
239 ) {
240   memcpy(pCid,&CLSID_DfMarshal,sizeof(CLSID_DfMarshal));
241   return S_OK;
242 }
243
244 static HRESULT WINAPI
245 StdMarshalImpl_GetMarshalSizeMax(
246   LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
247   void* pvDestContext, DWORD mshlflags, DWORD* pSize
248 ) {
249   *pSize = sizeof(wine_marshal_id)+sizeof(wine_marshal_data);
250   return S_OK;
251 }
252
253 static HRESULT WINAPI
254 StdMarshalImpl_MarshalInterface(
255   LPMARSHAL iface, IStream *pStm,REFIID riid, void* pv, DWORD dwDestContext,
256   void* pvDestContext, DWORD mshlflags
257 ) {
258   wine_marshal_id       mid;
259   wine_marshal_data     md;
260   IUnknown              *pUnk;
261   ULONG                 res;
262   HRESULT               hres;
263   IRpcStubBuffer        *stub;
264   IPSFactoryBuffer      *psfacbuf;
265
266   TRACE("(...,%s,...)\n",debugstr_guid(riid));
267
268   IUnknown_QueryInterface((LPUNKNOWN)pv,&IID_IUnknown,(LPVOID*)&pUnk);
269   mid.processid = GetCurrentProcessId();
270   mid.objectid = (DWORD)pUnk; /* FIXME */
271   IUnknown_Release(pUnk);
272   memcpy(&mid.iid,riid,sizeof(mid.iid));
273   md.dwDestContext      = dwDestContext;
274   md.mshlflags          = mshlflags;
275   hres = IStream_Write(pStm,&mid,sizeof(mid),&res);
276   if (hres) return hres;
277   hres = IStream_Write(pStm,&md,sizeof(md),&res);
278   if (hres) return hres;
279
280   if (SUCCEEDED(MARSHAL_Find_Stub_Buffer(&mid,&stub))) {
281       /* Find_Stub_Buffer gives us a ref but we want to keep it, as if we'd created a new one */
282       TRACE("Found RpcStubBuffer %p\n", stub);
283       return S_OK;
284   }
285   hres = get_facbuf_for_iid(riid,&psfacbuf);
286   if (hres) return hres;
287
288   hres = IPSFactoryBuffer_CreateStub(psfacbuf,riid,pv,&stub);
289   IPSFactoryBuffer_Release(psfacbuf);
290   if (hres) {
291     FIXME("Failed to create a stub for %s\n",debugstr_guid(riid));
292     return hres;
293   }
294   IUnknown_QueryInterface((LPUNKNOWN)pv,riid,(LPVOID*)&pUnk);
295   MARSHAL_Register_Stub(&mid,pUnk,stub);
296   IUnknown_Release(pUnk);
297   return S_OK;
298 }
299
300 static HRESULT WINAPI
301 StdMarshalImpl_UnmarshalInterface(
302   LPMARSHAL iface, IStream *pStm, REFIID riid, void **ppv
303 ) {
304   wine_marshal_id       mid;
305   wine_marshal_data     md;
306   ULONG                 res;
307   HRESULT               hres;
308   IPSFactoryBuffer      *psfacbuf;
309   IRpcProxyBuffer       *rpcproxy;
310   IRpcChannelBuffer     *chanbuf;
311
312   TRACE("(...,%s,....)\n",debugstr_guid(riid));
313   hres = IStream_Read(pStm,&mid,sizeof(mid),&res);
314   if (hres) return hres;
315   hres = IStream_Read(pStm,&md,sizeof(md),&res);
316   if (hres) return hres;
317   if (SUCCEEDED(MARSHAL_Find_Stub(&mid,(LPUNKNOWN*)ppv))) {
318       FIXME("Calling back to ourselves for %s!\n",debugstr_guid(riid));
319       return S_OK;
320   }
321   if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_NULL)) {
322     /* should return proxy manager IUnknown object */
323     FIXME("Special treatment required for IID of %s\n", debugstr_guid(riid));
324   }
325   hres = get_facbuf_for_iid(riid,&psfacbuf);
326   if (hres) return hres;
327   hres = IPSFactoryBuffer_CreateProxy(psfacbuf,NULL,riid,&rpcproxy,ppv);
328   if (hres) {
329     FIXME("Failed to create a proxy for %s\n",debugstr_guid(riid));
330     return hres;
331   }
332
333   MARSHAL_Register_Proxy(&mid, (LPUNKNOWN) rpcproxy);
334
335   hres = PIPE_GetNewPipeBuf(&mid,&chanbuf);
336   IPSFactoryBuffer_Release(psfacbuf);
337   if (hres) {
338     ERR("Failed to get an rpc channel buffer for %s\n",debugstr_guid(riid));
339   } else {
340     /* Connect the channel buffer to the proxy and release the no longer
341      * needed proxy.
342      * NOTE: The proxy should have taken an extra reference because it also
343      * aggregates the object, so we can safely release our reference to it. */
344     IRpcProxyBuffer_Connect(rpcproxy,chanbuf);
345     IRpcProxyBuffer_Release(rpcproxy);
346     /* IRpcProxyBuffer takes a reference on the channel buffer and
347      * we no longer need it, so release it */
348     IRpcChannelBuffer_Release(chanbuf);
349   }
350   return hres;
351 }
352
353 static HRESULT WINAPI
354 StdMarshalImpl_ReleaseMarshalData(LPMARSHAL iface, IStream *pStm) {
355   wine_marshal_id       mid;
356   ULONG                 res;
357   HRESULT               hres;
358   IRpcStubBuffer       *stub = NULL;
359   int                   i;
360
361   hres = IStream_Read(pStm,&mid,sizeof(mid),&res);
362   if (hres) return hres;
363
364   for (i=0; i < nrofstubs; i++)
365   {
366        if (!stubs[i].valid) continue;
367
368        if (MARSHAL_Compare_Mids(&mid, &(stubs[i].mid)))
369        {
370            stub = stubs[i].stub;
371            break;
372        }
373   }
374
375   if (!stub)
376   {
377       FIXME("Could not map MID to stub??\n");
378       return E_FAIL;
379   }
380
381   res = IRpcStubBuffer_Release(stub);
382   stubs[i].valid = FALSE;
383   TRACE("stub refcount of %p is %ld\n", stub, res);
384
385   return S_OK;
386 }
387
388 static HRESULT WINAPI
389 StdMarshalImpl_DisconnectObject(LPMARSHAL iface, DWORD dwReserved) {
390   FIXME("(), stub!\n");
391   return S_OK;
392 }
393
394 ICOM_VTABLE(IMarshal) stdmvtbl = {
395     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
396     StdMarshalImpl_QueryInterface,
397     StdMarshalImpl_AddRef,
398     StdMarshalImpl_Release,
399     StdMarshalImpl_GetUnmarshalClass,
400     StdMarshalImpl_GetMarshalSizeMax,
401     StdMarshalImpl_MarshalInterface,
402     StdMarshalImpl_UnmarshalInterface,
403     StdMarshalImpl_ReleaseMarshalData,
404     StdMarshalImpl_DisconnectObject
405 };
406
407 /***********************************************************************
408  *              CoGetStandardMarshal    [OLE32.@]
409  *
410  * When the COM library in the client process receives a marshalled
411  * interface pointer, it looks for a CLSID to be used in creating a proxy
412  * for the purposes of unmarshalling the packet. If the packet does not
413  * contain a CLSID for the proxy, COM calls CoGetStandardMarshal, passing a
414  * NULL pUnk value.
415  * This function creates a standard proxy in the client process and returns
416  * a pointer to that proxy's implementation of IMarshal.
417  * COM uses this pointer to call CoUnmarshalInterface to retrieve the pointer
418  * to the requested interface.
419  */
420 HRESULT WINAPI
421 CoGetStandardMarshal(
422   REFIID riid,IUnknown *pUnk,DWORD dwDestContext,LPVOID pvDestContext,
423   DWORD mshlflags, LPMARSHAL *pMarshal
424 ) {
425   StdMarshalImpl *dm;
426
427   if (pUnk == NULL) {
428     FIXME("(%s,NULL,%lx,%p,%lx,%p), unimplemented yet.\n",
429       debugstr_guid(riid),dwDestContext,pvDestContext,mshlflags,pMarshal
430     );
431     return E_FAIL;
432   }
433   TRACE("(%s,%p,%lx,%p,%lx,%p)\n",
434     debugstr_guid(riid),pUnk,dwDestContext,pvDestContext,mshlflags,pMarshal
435   );
436   *pMarshal = HeapAlloc(GetProcessHeap(),0,sizeof(StdMarshalImpl));
437   dm = (StdMarshalImpl*) *pMarshal;
438   if (!dm) return E_FAIL;
439   dm->lpvtbl            = &stdmvtbl;
440   dm->ref               = 1;
441
442   memcpy(&dm->iid,riid,sizeof(dm->iid));
443   dm->dwDestContext     = dwDestContext;
444   dm->pvDestContext     = pvDestContext;
445   dm->mshlflags         = mshlflags;
446   return S_OK;
447 }
448
449 /* Helper function for getting Marshaler */
450 static HRESULT WINAPI
451 _GetMarshaller(REFIID riid, IUnknown *pUnk,DWORD dwDestContext,
452   void *pvDestContext, DWORD mshlFlags, LPMARSHAL *pMarshal
453 ) {
454   HRESULT hres;
455
456   if (!pUnk)
457       return E_POINTER;
458   hres = IUnknown_QueryInterface(pUnk,&IID_IMarshal,(LPVOID*)pMarshal);
459   if (hres)
460     hres = CoGetStandardMarshal(riid,pUnk,dwDestContext,pvDestContext,mshlFlags,pMarshal);
461   return hres;
462 }
463
464 /***********************************************************************
465  *              CoGetMarshalSizeMax     [OLE32.@]
466  */
467 HRESULT WINAPI
468 CoGetMarshalSizeMax(ULONG *pulSize, REFIID riid, IUnknown *pUnk,
469   DWORD dwDestContext, void *pvDestContext, DWORD mshlFlags
470 ) {
471   HRESULT       hres;
472   LPMARSHAL     pMarshal;
473
474   hres = _GetMarshaller(riid,pUnk,dwDestContext,pvDestContext,mshlFlags,&pMarshal);
475   if (hres)
476     return hres;
477   hres = IMarshal_GetMarshalSizeMax(pMarshal,riid,pUnk,dwDestContext,pvDestContext,mshlFlags,pulSize);
478   *pulSize += sizeof(wine_marshal_id)+sizeof(wine_marshal_data)+sizeof(CLSID);
479   IMarshal_Release(pMarshal);
480   return hres;
481 }
482
483
484 /***********************************************************************
485  *              CoMarshalInterface      [OLE32.@]
486  */
487 HRESULT WINAPI
488 CoMarshalInterface( IStream *pStm, REFIID riid, IUnknown *pUnk,
489   DWORD dwDestContext, void *pvDestContext, DWORD mshlflags
490 ) {
491   HRESULT               hres;
492   LPMARSHAL             pMarshal;
493   CLSID                 xclsid;
494   ULONG                 writeres;
495   wine_marshal_id       mid;
496   wine_marshal_data     md;
497   ULONG                 res;
498   IUnknown              *pUnknown;
499
500   TRACE("(%p, %s, %p, %lx, %p, %lx)\n",
501     pStm,debugstr_guid(riid),pUnk,dwDestContext,pvDestContext,mshlflags
502   );
503
504   if (pUnk == NULL)
505     return E_INVALIDARG;
506
507   STUBMGR_Start(); /* Just to be sure we have one running. */
508   mid.processid = GetCurrentProcessId();
509   IUnknown_QueryInterface(pUnk,&IID_IUnknown,(LPVOID*)&pUnknown);
510   mid.objectid = (DWORD)pUnknown;
511   IUnknown_Release(pUnknown);
512   memcpy(&mid.iid,riid,sizeof(mid.iid));
513   md.dwDestContext      = dwDestContext;
514   md.mshlflags          = mshlflags;
515   hres = IStream_Write(pStm,&mid,sizeof(mid),&res);
516   if (hres) return hres;
517   hres = IStream_Write(pStm,&md,sizeof(md),&res);
518   if (hres) return hres;
519   hres = _GetMarshaller(riid,pUnk,dwDestContext,pvDestContext,mshlflags,&pMarshal);
520   if (hres) {
521     FIXME("Failed to get marshaller, %lx?\n",hres);
522     return hres;
523   }
524   hres = IMarshal_GetUnmarshalClass(pMarshal,riid,pUnk,dwDestContext,pvDestContext,mshlflags,&xclsid);
525   if (hres) {
526     FIXME("IMarshal:GetUnmarshalClass failed, %lx\n",hres);
527     goto release_marshal;
528   }
529   hres = IStream_Write(pStm,&xclsid,sizeof(xclsid),&writeres);
530   if (hres) {
531     FIXME("Stream write failed, %lx\n",hres);
532     goto release_marshal;
533   }
534
535   TRACE("Calling IMarshal::MarshalInterace\n");
536   hres = IMarshal_MarshalInterface(pMarshal,pStm,riid,pUnk,dwDestContext,pvDestContext,mshlflags);
537
538   if (hres) {
539     if (IsEqualGUID(riid,&IID_IOleObject)) {
540       ERR("WINE currently cannot marshal IOleObject interfaces. This means you cannot embed/link OLE objects between applications.\n");
541     } else {
542       FIXME("Failed to marshal the interface %s, %lx?\n",debugstr_guid(riid),hres);
543     }
544   }
545 release_marshal:
546   IMarshal_Release(pMarshal);
547   return hres;
548 }
549
550
551 /***********************************************************************
552  *              CoUnmarshalInterface    [OLE32.@]
553  */
554 HRESULT WINAPI
555 CoUnmarshalInterface(IStream *pStm, REFIID riid, LPVOID *ppv) {
556   HRESULT               hres;
557   wine_marshal_id       mid;
558   wine_marshal_data     md;
559   ULONG                 res;
560   LPMARSHAL             pMarshal;
561   LPUNKNOWN             pUnk;
562   CLSID                 xclsid;
563
564   TRACE("(%p,%s,%p)\n",pStm,debugstr_guid(riid),ppv);
565
566   hres = IStream_Read(pStm,&mid,sizeof(mid),&res);
567   if (hres) {
568       FIXME("Stream read 1 failed, %lx, (%ld of %d)\n",hres,res,sizeof(mid));
569       return hres;
570   }
571   hres = IStream_Read(pStm,&md,sizeof(md),&res);
572   if (hres) {
573       FIXME("Stream read 2 failed, %lx, (%ld of %d)\n",hres,res,sizeof(md));
574       return hres;
575   }
576   hres = IStream_Read(pStm,&xclsid,sizeof(xclsid),&res);
577   if (hres) {
578       FIXME("Stream read 3 failed, %lx, (%ld of %d)\n",hres,res,sizeof(xclsid));
579       return hres;
580   }
581   hres=CoCreateInstance(&xclsid,NULL,CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER | CLSCTX_LOCAL_SERVER,&IID_IMarshal,(void**)&pUnk);
582   if (hres) {
583       FIXME("Failed to create instance of unmarshaller %s.\n",debugstr_guid(&xclsid));
584       return hres;
585   }
586   hres = _GetMarshaller(riid,pUnk,md.dwDestContext,NULL,md.mshlflags,&pMarshal);
587   if (hres) {
588       FIXME("Failed to get unmarshaller, %lx?\n",hres);
589       return hres;
590   }
591   hres = IMarshal_UnmarshalInterface(pMarshal,pStm,riid,ppv);
592   if (hres) {
593     FIXME("Failed to Unmarshal the interface, %lx?\n",hres);
594     goto release_marshal;
595   }
596 release_marshal:
597   IMarshal_Release(pMarshal);
598   return hres;
599 }
600
601 /***********************************************************************
602  *              CoReleaseMarshalData    [OLE32.@]
603  */
604 HRESULT WINAPI
605 CoReleaseMarshalData(IStream *pStm) {
606   HRESULT               hres;
607   wine_marshal_id       mid;
608   wine_marshal_data     md;
609   ULONG                 res;
610   LPMARSHAL             pMarshal;
611   LPUNKNOWN             pUnk;
612   CLSID                 xclsid;
613
614   TRACE("(%p)\n",pStm);
615
616   hres = IStream_Read(pStm,&mid,sizeof(mid),&res);
617   if (hres) {
618       FIXME("Stream read 1 failed, %lx, (%ld of %d)\n",hres,res,sizeof(mid));
619       return hres;
620   }
621   hres = IStream_Read(pStm,&md,sizeof(md),&res);
622   if (hres) {
623       FIXME("Stream read 2 failed, %lx, (%ld of %d)\n",hres,res,sizeof(md));
624       return hres;
625   }
626   hres = IStream_Read(pStm,&xclsid,sizeof(xclsid),&res);
627   if (hres) {
628       FIXME("Stream read 3 failed, %lx, (%ld of %d)\n",hres,res,sizeof(xclsid));
629       return hres;
630   }
631   hres=CoCreateInstance(&xclsid,NULL,CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER | CLSCTX_LOCAL_SERVER,&IID_IMarshal,(void**)(char*)&pUnk);
632   if (hres) {
633       FIXME("Failed to create instance of unmarshaller %s.\n",debugstr_guid(&xclsid));
634       return hres;
635   }
636   hres = IUnknown_QueryInterface(pUnk,&IID_IMarshal,(LPVOID*)(char*)&pMarshal);
637   if (hres) {
638       FIXME("Failed to get IMarshal iface, %lx?\n",hres);
639       return hres;
640   }
641   hres = IMarshal_ReleaseMarshalData(pMarshal,pStm);
642   if (hres) {
643     FIXME("Failed to releasemarshaldata the interface, %lx?\n",hres);
644   }
645   IMarshal_Release(pMarshal);
646   IUnknown_Release(pUnk);
647   return hres;
648 }
649
650
651 /***********************************************************************
652  *              CoMarshalInterThreadInterfaceInStream   [OLE32.@]
653  *
654  * Marshal an interface across threads in the same process.
655  *
656  * PARAMS
657  *  riid  [I] Identifier of the interface to be marshalled.
658  *  pUnk  [I] Pointer to IUnknown-derived interface that will be marshalled.
659  *  ppStm [O] Pointer to IStream object that is created and then used to store the marshalled inteface.
660  *
661  * RETURNS
662  *  Success: S_OK
663  *  Failure: E_OUTOFMEMORY and other COM error codes
664  *
665  * SEE
666  *   CoMarshalInterface(), CoUnmarshalInterface() and CoGetInterfaceAndReleaseStream()
667  */
668 HRESULT WINAPI
669 CoMarshalInterThreadInterfaceInStream(
670   REFIID riid, LPUNKNOWN pUnk, LPSTREAM * ppStm)
671 {
672     ULARGE_INTEGER      xpos;
673     LARGE_INTEGER               seekto;
674     HRESULT             hres;
675
676     TRACE("(%s, %p, %p)\n",debugstr_guid(riid), pUnk, ppStm);
677
678     hres = CreateStreamOnHGlobal(0, TRUE, ppStm);
679     if (FAILED(hres)) return hres;
680     hres = CoMarshalInterface(*ppStm, riid, pUnk, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
681
682     /* FIXME: is this needed? */
683     memset(&seekto,0,sizeof(seekto));
684     IStream_Seek(*ppStm,seekto,SEEK_SET,&xpos);
685
686     return hres;
687 }
688
689 /***********************************************************************
690  *              CoGetInterfaceAndReleaseStream  [OLE32.@]
691  *
692  * Unmarshalls an inteface from a stream and then releases the stream.
693  *
694  * PARAMS
695  *  pStm [I] Stream that contains the marshalled inteface.
696  *  riid [I] Interface identifier of the object to unmarshall.
697  *  ppv  [O] Address of pointer where the requested interface object will be stored.
698  *
699  * RETURNS
700  *  Success: S_OK
701  *  Failure: A COM error code
702  *
703  * SEE
704  *  CoMarshalInterThreadInterfaceInStream() and CoUnmarshalInteface()
705  */
706 HRESULT WINAPI
707 CoGetInterfaceAndReleaseStream(LPSTREAM pStm,REFIID riid, LPVOID *ppv)
708 {
709     HRESULT hres;
710
711     TRACE("(%p, %s, %p)\n", pStm, debugstr_guid(riid), ppv);
712
713     hres = CoUnmarshalInterface(pStm, riid, ppv);
714     IStream_Release(pStm);
715     return hres;
716 }
717
718 static HRESULT WINAPI
719 SMCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid, LPVOID *ppv) {
720   *ppv = NULL;
721   if (IsEqualIID(riid,&IID_IUnknown) || IsEqualIID(riid,&IID_IClassFactory)) {
722     *ppv = (LPVOID)iface;
723     return S_OK;
724   }
725   return E_NOINTERFACE;
726 }
727 static ULONG WINAPI SMCF_AddRef(LPCLASSFACTORY iface) { return 2; }
728 static ULONG WINAPI SMCF_Release(LPCLASSFACTORY iface) { return 1; }
729
730 static HRESULT WINAPI
731 SMCF_CreateInstance(
732   LPCLASSFACTORY iface, LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv
733 ) {
734   if (IsEqualIID(riid,&IID_IMarshal)) {
735       StdMarshalImpl    *dm;
736       dm=(StdMarshalImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(StdMarshalImpl));
737       if (!dm)
738           return E_FAIL;
739       dm->lpvtbl        = &stdmvtbl;
740       dm->ref           = 1;
741       *ppv = (LPVOID)dm;
742       return S_OK;
743   }
744   FIXME("(%s), not supported.\n",debugstr_guid(riid));
745   return E_NOINTERFACE;
746 }
747
748 static HRESULT WINAPI
749 SMCF_LockServer(LPCLASSFACTORY iface, BOOL fLock) {
750     FIXME("(%d), stub!\n",fLock);
751     return S_OK;
752 }
753
754 static ICOM_VTABLE(IClassFactory) dfmarshalcfvtbl = {
755     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
756     SMCF_QueryInterface,
757     SMCF_AddRef,
758     SMCF_Release,
759     SMCF_CreateInstance,
760     SMCF_LockServer
761 };
762 static ICOM_VTABLE(IClassFactory) *pdfmarshalcfvtbl = &dfmarshalcfvtbl;
763
764 HRESULT
765 MARSHAL_GetStandardMarshalCF(LPVOID *ppv) {
766   *ppv = &pdfmarshalcfvtbl;
767   return S_OK;
768 }