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