Fix ref counting in StdMarshalImpl_MarshalInterface for case where the
[wine/gsoc_dplay.git] / dlls / ole32 / marshal.c
blobf600603180711b3409b061b07019a7e24700b736
1 /*
2 * Marshalling library
4 * Copyright 2002 Marcus Meissner
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.
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.
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
21 #include "config.h"
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <assert.h>
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"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(ole);
49 extern const CLSID CLSID_DfMarshal;
51 /* Marshalling just passes a unique identifier to the remote client,
52 * that makes it possible to find the passed interface again.
54 * So basically we need a set of values that make it unique.
56 * Process Identifier, Object IUnknown ptr, IID
58 * Note that the IUnknown_QI(ob,xiid,&ppv) always returns the SAME ppv value!
61 inline static HRESULT
62 get_facbuf_for_iid(REFIID riid,IPSFactoryBuffer **facbuf) {
63 HRESULT hres;
64 CLSID pxclsid;
66 if ((hres = CoGetPSClsid(riid,&pxclsid)))
67 return hres;
68 return CoGetClassObject(&pxclsid,CLSCTX_INPROC_SERVER,NULL,&IID_IPSFactoryBuffer,(LPVOID*)facbuf);
71 typedef struct _wine_marshal_data {
72 DWORD dwDestContext;
73 DWORD mshlflags;
74 } wine_marshal_data;
76 typedef struct _mid2unknown {
77 wine_marshal_id mid;
78 LPUNKNOWN pUnk;
79 } mid2unknown;
81 typedef struct _mid2stub {
82 wine_marshal_id mid;
83 IRpcStubBuffer *stub;
84 LPUNKNOWN pUnkServer;
85 BOOL valid;
86 } mid2stub;
88 static mid2stub *stubs = NULL;
89 static int nrofstubs = 0;
91 static mid2unknown *proxies = NULL;
92 static int nrofproxies = 0;
94 HRESULT
95 MARSHAL_Find_Stub_Server(wine_marshal_id *mid,LPUNKNOWN *punk) {
96 int i;
98 for (i=0;i<nrofstubs;i++) {
99 if (!stubs[i].valid) continue;
101 if (MARSHAL_Compare_Mids_NoInterface(mid,&(stubs[i].mid))) {
102 *punk = stubs[i].pUnkServer;
103 IUnknown_AddRef((*punk));
104 return S_OK;
107 return E_FAIL;
110 HRESULT
111 MARSHAL_Find_Stub_Buffer(wine_marshal_id *mid,IRpcStubBuffer **stub) {
112 int i;
114 for (i=0;i<nrofstubs;i++) {
115 if (!stubs[i].valid) continue;
117 if (MARSHAL_Compare_Mids(mid,&(stubs[i].mid))) {
118 *stub = stubs[i].stub;
119 IUnknown_AddRef((*stub));
120 return S_OK;
123 return E_FAIL;
126 HRESULT
127 MARSHAL_Find_Stub(wine_marshal_id *mid,LPUNKNOWN *pUnk) {
128 int i;
130 for (i=0;i<nrofstubs;i++) {
131 if (!stubs[i].valid) continue;
133 if (MARSHAL_Compare_Mids(mid,&(stubs[i].mid))) {
134 *pUnk = stubs[i].pUnkServer;
135 IUnknown_AddRef((*pUnk));
136 return S_OK;
139 return E_FAIL;
142 HRESULT
143 MARSHAL_Register_Stub(wine_marshal_id *mid,LPUNKNOWN pUnk,IRpcStubBuffer *stub) {
144 LPUNKNOWN xPunk;
145 if (!MARSHAL_Find_Stub(mid,&xPunk)) {
146 FIXME("Already have entry for (%lx/%s)!\n",mid->objectid,debugstr_guid(&(mid->iid)));
147 return S_OK;
149 if (nrofstubs)
150 stubs=HeapReAlloc(GetProcessHeap(),0,stubs,sizeof(stubs[0])*(nrofstubs+1));
151 else
152 stubs=HeapAlloc(GetProcessHeap(),0,sizeof(stubs[0]));
153 if (!stubs) return E_OUTOFMEMORY;
154 stubs[nrofstubs].stub = stub;
155 stubs[nrofstubs].pUnkServer = pUnk;
156 memcpy(&(stubs[nrofstubs].mid),mid,sizeof(*mid));
157 stubs[nrofstubs].valid = TRUE; /* set to false when released by ReleaseMarshalData */
158 nrofstubs++;
159 return S_OK;
162 HRESULT
163 MARSHAL_Find_Proxy(wine_marshal_id *mid,LPUNKNOWN *punk) {
164 int i;
166 for (i=0;i<nrofproxies;i++)
167 if (MARSHAL_Compare_Mids(mid,&(proxies[i].mid))) {
168 *punk = proxies[i].pUnk;
169 IUnknown_AddRef((*punk));
170 return S_OK;
172 return E_FAIL;
175 HRESULT
176 MARSHAL_Find_Proxy_Object(wine_marshal_id *mid,LPUNKNOWN *punk) {
177 int i;
179 for (i=0;i<nrofproxies;i++)
180 if (MARSHAL_Compare_Mids_NoInterface(mid,&(proxies[i].mid))) {
181 *punk = proxies[i].pUnk;
182 IUnknown_AddRef((*punk));
183 return S_OK;
185 return E_FAIL;
188 HRESULT
189 MARSHAL_Register_Proxy(wine_marshal_id *mid,LPUNKNOWN punk) {
190 int i;
192 for (i=0;i<nrofproxies;i++) {
193 if (MARSHAL_Compare_Mids(mid,&(proxies[i].mid))) {
194 ERR("Already have mid?\n");
195 return E_FAIL;
198 if (nrofproxies)
199 proxies = HeapReAlloc(GetProcessHeap(),0,proxies,sizeof(proxies[0])*(nrofproxies+1));
200 else
201 proxies = HeapAlloc(GetProcessHeap(),0,sizeof(proxies[0]));
202 memcpy(&(proxies[nrofproxies].mid),mid,sizeof(*mid));
203 proxies[nrofproxies].pUnk = punk;
204 nrofproxies++;
205 IUnknown_AddRef(punk);
206 return S_OK;
209 /********************** StdMarshal implementation ****************************/
210 typedef struct _StdMarshalImpl {
211 ICOM_VTABLE(IMarshal) *lpvtbl;
212 DWORD ref;
214 IID iid;
215 DWORD dwDestContext;
216 LPVOID pvDestContext;
217 DWORD mshlflags;
218 } StdMarshalImpl;
220 HRESULT WINAPI
221 StdMarshalImpl_QueryInterface(LPMARSHAL iface,REFIID riid,LPVOID *ppv) {
222 *ppv = NULL;
223 if (IsEqualIID(&IID_IUnknown,riid) || IsEqualIID(&IID_IMarshal,riid)) {
224 *ppv = iface;
225 IUnknown_AddRef(iface);
226 return S_OK;
228 FIXME("No interface for %s.\n",debugstr_guid(riid));
229 return E_NOINTERFACE;
232 ULONG WINAPI
233 StdMarshalImpl_AddRef(LPMARSHAL iface) {
234 ICOM_THIS(StdMarshalImpl,iface);
235 This->ref++;
236 return This->ref;
239 ULONG WINAPI
240 StdMarshalImpl_Release(LPMARSHAL iface) {
241 ICOM_THIS(StdMarshalImpl,iface);
242 This->ref--;
244 if (This->ref)
245 return This->ref;
246 HeapFree(GetProcessHeap(),0,This);
247 return 0;
250 HRESULT WINAPI
251 StdMarshalImpl_GetUnmarshalClass(
252 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
253 void* pvDestContext, DWORD mshlflags, CLSID* pCid
255 memcpy(pCid,&CLSID_DfMarshal,sizeof(CLSID_DfMarshal));
256 return S_OK;
259 HRESULT WINAPI
260 StdMarshalImpl_GetMarshalSizeMax(
261 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
262 void* pvDestContext, DWORD mshlflags, DWORD* pSize
264 *pSize = sizeof(wine_marshal_id)+sizeof(wine_marshal_data);
265 return S_OK;
268 HRESULT WINAPI
269 StdMarshalImpl_MarshalInterface(
270 LPMARSHAL iface, IStream *pStm,REFIID riid, void* pv, DWORD dwDestContext,
271 void* pvDestContext, DWORD mshlflags
273 wine_marshal_id mid;
274 wine_marshal_data md;
275 IUnknown *pUnk;
276 ULONG res;
277 HRESULT hres;
278 IRpcStubBuffer *stub;
279 IPSFactoryBuffer *psfacbuf;
281 TRACE("(...,%s,...)\n",debugstr_guid(riid));
283 IUnknown_QueryInterface((LPUNKNOWN)pv,&IID_IUnknown,(LPVOID*)&pUnk);
284 mid.processid = GetCurrentProcessId();
285 mid.objectid = (DWORD)pUnk; /* FIXME */
286 IUnknown_Release(pUnk);
287 memcpy(&mid.iid,riid,sizeof(mid.iid));
288 md.dwDestContext = dwDestContext;
289 md.mshlflags = mshlflags;
290 hres = IStream_Write(pStm,&mid,sizeof(mid),&res);
291 if (hres) return hres;
292 hres = IStream_Write(pStm,&md,sizeof(md),&res);
293 if (hres) return hres;
295 if (SUCCEEDED(MARSHAL_Find_Stub_Buffer(&mid,&stub))) {
296 /* Find_Stub_Buffer gives us a ref but we want to keep it, as if we'd created a new one */
297 TRACE("Found RpcStubBuffer %p\n", stub);
298 return S_OK;
300 hres = get_facbuf_for_iid(riid,&psfacbuf);
301 if (hres) return hres;
303 hres = IPSFactoryBuffer_CreateStub(psfacbuf,riid,pv,&stub);
304 IPSFactoryBuffer_Release(psfacbuf);
305 if (hres) {
306 FIXME("Failed to create a stub for %s\n",debugstr_guid(riid));
307 return hres;
309 IUnknown_QueryInterface((LPUNKNOWN)pv,riid,(LPVOID*)&pUnk);
310 MARSHAL_Register_Stub(&mid,pUnk,stub);
311 IUnknown_Release(pUnk);
312 return S_OK;
315 HRESULT WINAPI
316 StdMarshalImpl_UnmarshalInterface(
317 LPMARSHAL iface, IStream *pStm, REFIID riid, void **ppv
319 wine_marshal_id mid;
320 wine_marshal_data md;
321 ULONG res;
322 HRESULT hres;
323 IPSFactoryBuffer *psfacbuf;
324 IRpcProxyBuffer *rpcproxy;
325 IRpcChannelBuffer *chanbuf;
327 TRACE("(...,%s,....)\n",debugstr_guid(riid));
328 hres = IStream_Read(pStm,&mid,sizeof(mid),&res);
329 if (hres) return hres;
330 hres = IStream_Read(pStm,&md,sizeof(md),&res);
331 if (hres) return hres;
332 if (SUCCEEDED(MARSHAL_Find_Stub(&mid,(LPUNKNOWN*)ppv))) {
333 FIXME("Calling back to ourselves for %s!\n",debugstr_guid(riid));
334 return S_OK;
336 hres = get_facbuf_for_iid(riid,&psfacbuf);
337 if (hres) return hres;
338 hres = IPSFactoryBuffer_CreateProxy(psfacbuf,NULL,riid,&rpcproxy,ppv);
339 if (hres) {
340 FIXME("Failed to create a proxy for %s\n",debugstr_guid(riid));
341 return hres;
343 hres = PIPE_GetNewPipeBuf(&mid,&chanbuf);
344 IPSFactoryBuffer_Release(psfacbuf);
345 if (hres) {
346 ERR("Failed to get an rpc channel buffer for %s\n",debugstr_guid(riid));
347 } else {
348 /* Connect the channel buffer to the proxy and release the no longer
349 * needed proxy.
350 * NOTE: The proxy should have taken an extra reference because it also
351 * aggregates the object, so we can safely release our reference to it. */
352 IRpcProxyBuffer_Connect(rpcproxy,chanbuf);
353 IRpcProxyBuffer_Release(rpcproxy);
354 /* IRpcProxyBuffer takes a reference on the channel buffer and
355 * we no longer need it, so release it */
356 IRpcChannelBuffer_Release(chanbuf);
358 return hres;
361 HRESULT WINAPI
362 StdMarshalImpl_ReleaseMarshalData(LPMARSHAL iface, IStream *pStm) {
363 wine_marshal_id mid;
364 ULONG res;
365 HRESULT hres;
366 IRpcStubBuffer *stub = NULL;
367 int i;
369 hres = IStream_Read(pStm,&mid,sizeof(mid),&res);
370 if (hres) return hres;
372 for (i=0; i < nrofstubs; i++)
374 if (!stubs[i].valid) continue;
376 if (MARSHAL_Compare_Mids(&mid, &(stubs[i].mid)))
378 stub = stubs[i].stub;
379 break;
383 if (!stub)
385 FIXME("Could not map MID to stub??\n");
386 return E_FAIL;
389 res = IRpcStubBuffer_Release(stub);
390 stubs[i].valid = FALSE;
391 TRACE("stub refcount of %p is %ld\n", stub, res);
393 return S_OK;
396 HRESULT WINAPI
397 StdMarshalImpl_DisconnectObject(LPMARSHAL iface, DWORD dwReserved) {
398 FIXME("(), stub!\n");
399 return S_OK;
402 ICOM_VTABLE(IMarshal) stdmvtbl = {
403 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
404 StdMarshalImpl_QueryInterface,
405 StdMarshalImpl_AddRef,
406 StdMarshalImpl_Release,
407 StdMarshalImpl_GetUnmarshalClass,
408 StdMarshalImpl_GetMarshalSizeMax,
409 StdMarshalImpl_MarshalInterface,
410 StdMarshalImpl_UnmarshalInterface,
411 StdMarshalImpl_ReleaseMarshalData,
412 StdMarshalImpl_DisconnectObject
415 /***********************************************************************
416 * CoGetStandardMarshal [OLE32.@]
418 * When the COM library in the client process receives a marshalled
419 * interface pointer, it looks for a CLSID to be used in creating a proxy
420 * for the purposes of unmarshalling the packet. If the packet does not
421 * contain a CLSID for the proxy, COM calls CoGetStandardMarshal, passing a
422 * NULL pUnk value.
423 * This function creates a standard proxy in the client process and returns
424 * a pointer to that proxy's implementation of IMarshal.
425 * COM uses this pointer to call CoUnmarshalInterface to retrieve the pointer
426 * to the requested interface.
428 HRESULT WINAPI
429 CoGetStandardMarshal(
430 REFIID riid,IUnknown *pUnk,DWORD dwDestContext,LPVOID pvDestContext,
431 DWORD mshlflags, LPMARSHAL *pMarshal
433 StdMarshalImpl *dm;
435 if (pUnk == NULL) {
436 FIXME("(%s,NULL,%lx,%p,%lx,%p), unimplemented yet.\n",
437 debugstr_guid(riid),dwDestContext,pvDestContext,mshlflags,pMarshal
439 return E_FAIL;
441 TRACE("(%s,%p,%lx,%p,%lx,%p)\n",
442 debugstr_guid(riid),pUnk,dwDestContext,pvDestContext,mshlflags,pMarshal
444 *pMarshal = HeapAlloc(GetProcessHeap(),0,sizeof(StdMarshalImpl));
445 dm = (StdMarshalImpl*) *pMarshal;
446 if (!dm) return E_FAIL;
447 dm->lpvtbl = &stdmvtbl;
448 dm->ref = 1;
450 memcpy(&dm->iid,riid,sizeof(dm->iid));
451 dm->dwDestContext = dwDestContext;
452 dm->pvDestContext = pvDestContext;
453 dm->mshlflags = mshlflags;
454 return S_OK;
457 /* Helper function for getting Marshaler */
458 static HRESULT WINAPI
459 _GetMarshaller(REFIID riid, IUnknown *pUnk,DWORD dwDestContext,
460 void *pvDestContext, DWORD mshlFlags, LPMARSHAL *pMarshal
462 HRESULT hres;
464 if (!pUnk)
465 return E_POINTER;
466 hres = IUnknown_QueryInterface(pUnk,&IID_IMarshal,(LPVOID*)pMarshal);
467 if (hres)
468 hres = CoGetStandardMarshal(riid,pUnk,dwDestContext,pvDestContext,mshlFlags,pMarshal);
469 return hres;
472 /***********************************************************************
473 * CoGetMarshalSizeMax [OLE32.@]
475 HRESULT WINAPI
476 CoGetMarshalSizeMax(ULONG *pulSize, REFIID riid, IUnknown *pUnk,
477 DWORD dwDestContext, void *pvDestContext, DWORD mshlFlags
479 HRESULT hres;
480 LPMARSHAL pMarshal;
482 hres = _GetMarshaller(riid,pUnk,dwDestContext,pvDestContext,mshlFlags,&pMarshal);
483 if (hres)
484 return hres;
485 hres = IMarshal_GetMarshalSizeMax(pMarshal,riid,pUnk,dwDestContext,pvDestContext,mshlFlags,pulSize);
486 *pulSize += sizeof(wine_marshal_id)+sizeof(wine_marshal_data)+sizeof(CLSID);
487 IMarshal_Release(pMarshal);
488 return hres;
492 /***********************************************************************
493 * CoMarshalInterface [OLE32.@]
495 HRESULT WINAPI
496 CoMarshalInterface( IStream *pStm, REFIID riid, IUnknown *pUnk,
497 DWORD dwDestContext, void *pvDestContext, DWORD mshlflags
499 HRESULT hres;
500 LPMARSHAL pMarshal;
501 CLSID xclsid;
502 ULONG writeres;
503 wine_marshal_id mid;
504 wine_marshal_data md;
505 ULONG res;
506 IUnknown *pUnknown;
508 TRACE("(%p, %s, %p, %lx, %p, %lx)\n",
509 pStm,debugstr_guid(riid),pUnk,dwDestContext,pvDestContext,mshlflags
512 if (pUnk == NULL)
513 return E_INVALIDARG;
515 STUBMGR_Start(); /* Just to be sure we have one running. */
516 mid.processid = GetCurrentProcessId();
517 IUnknown_QueryInterface(pUnk,&IID_IUnknown,(LPVOID*)&pUnknown);
518 mid.objectid = (DWORD)pUnknown;
519 IUnknown_Release(pUnknown);
520 memcpy(&mid.iid,riid,sizeof(mid.iid));
521 md.dwDestContext = dwDestContext;
522 md.mshlflags = mshlflags;
523 hres = IStream_Write(pStm,&mid,sizeof(mid),&res);
524 if (hres) return hres;
525 hres = IStream_Write(pStm,&md,sizeof(md),&res);
526 if (hres) return hres;
527 hres = _GetMarshaller(riid,pUnk,dwDestContext,pvDestContext,mshlflags,&pMarshal);
528 if (hres) {
529 FIXME("Failed to get marshaller, %lx?\n",hres);
530 return hres;
532 hres = IMarshal_GetUnmarshalClass(pMarshal,riid,pUnk,dwDestContext,pvDestContext,mshlflags,&xclsid);
533 if (hres) {
534 FIXME("IMarshal:GetUnmarshalClass failed, %lx\n",hres);
535 goto release_marshal;
537 hres = IStream_Write(pStm,&xclsid,sizeof(xclsid),&writeres);
538 if (hres) {
539 FIXME("Stream write failed, %lx\n",hres);
540 goto release_marshal;
543 TRACE("Calling IMarshal::MarshalInterace\n");
544 hres = IMarshal_MarshalInterface(pMarshal,pStm,riid,pUnk,dwDestContext,pvDestContext,mshlflags);
546 if (hres) {
547 if (IsEqualGUID(riid,&IID_IOleObject)) {
548 ERR("WINE currently cannot marshal IOleObject interfaces. This means you cannot embed/link OLE objects between applications.\n");
549 } else {
550 FIXME("Failed to marshal the interface %s, %lx?\n",debugstr_guid(riid),hres);
553 release_marshal:
554 IMarshal_Release(pMarshal);
555 return hres;
559 /***********************************************************************
560 * CoUnmarshalInterface [OLE32.@]
562 HRESULT WINAPI
563 CoUnmarshalInterface(IStream *pStm, REFIID riid, LPVOID *ppv) {
564 HRESULT hres;
565 wine_marshal_id mid;
566 wine_marshal_data md;
567 ULONG res;
568 LPMARSHAL pMarshal;
569 LPUNKNOWN pUnk;
570 CLSID xclsid;
572 TRACE("(%p,%s,%p)\n",pStm,debugstr_guid(riid),ppv);
574 hres = IStream_Read(pStm,&mid,sizeof(mid),&res);
575 if (hres) {
576 FIXME("Stream read 1 failed, %lx, (%ld of %d)\n",hres,res,sizeof(mid));
577 return hres;
579 hres = IStream_Read(pStm,&md,sizeof(md),&res);
580 if (hres) {
581 FIXME("Stream read 2 failed, %lx, (%ld of %d)\n",hres,res,sizeof(md));
582 return hres;
584 hres = IStream_Read(pStm,&xclsid,sizeof(xclsid),&res);
585 if (hres) {
586 FIXME("Stream read 3 failed, %lx, (%ld of %d)\n",hres,res,sizeof(xclsid));
587 return hres;
589 hres=CoCreateInstance(&xclsid,NULL,CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER | CLSCTX_LOCAL_SERVER,&IID_IMarshal,(void**)&pUnk);
590 if (hres) {
591 FIXME("Failed to create instance of unmarshaller %s.\n",debugstr_guid(&xclsid));
592 return hres;
594 hres = _GetMarshaller(riid,pUnk,md.dwDestContext,NULL,md.mshlflags,&pMarshal);
595 if (hres) {
596 FIXME("Failed to get unmarshaller, %lx?\n",hres);
597 return hres;
599 hres = IMarshal_UnmarshalInterface(pMarshal,pStm,riid,ppv);
600 if (hres) {
601 FIXME("Failed to Unmarshal the interface, %lx?\n",hres);
602 goto release_marshal;
604 release_marshal:
605 IMarshal_Release(pMarshal);
606 return hres;
609 /***********************************************************************
610 * CoReleaseMarshalData [OLE32.@]
612 HRESULT WINAPI
613 CoReleaseMarshalData(IStream *pStm) {
614 HRESULT hres;
615 wine_marshal_id mid;
616 wine_marshal_data md;
617 ULONG res;
618 LPMARSHAL pMarshal;
619 LPUNKNOWN pUnk;
620 CLSID xclsid;
622 TRACE("(%p)\n",pStm);
624 hres = IStream_Read(pStm,&mid,sizeof(mid),&res);
625 if (hres) {
626 FIXME("Stream read 1 failed, %lx, (%ld of %d)\n",hres,res,sizeof(mid));
627 return hres;
629 hres = IStream_Read(pStm,&md,sizeof(md),&res);
630 if (hres) {
631 FIXME("Stream read 2 failed, %lx, (%ld of %d)\n",hres,res,sizeof(md));
632 return hres;
634 hres = IStream_Read(pStm,&xclsid,sizeof(xclsid),&res);
635 if (hres) {
636 FIXME("Stream read 3 failed, %lx, (%ld of %d)\n",hres,res,sizeof(xclsid));
637 return hres;
639 hres=CoCreateInstance(&xclsid,NULL,CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER | CLSCTX_LOCAL_SERVER,&IID_IMarshal,(void**)(char*)&pUnk);
640 if (hres) {
641 FIXME("Failed to create instance of unmarshaller %s.\n",debugstr_guid(&xclsid));
642 return hres;
644 hres = IUnknown_QueryInterface(pUnk,&IID_IMarshal,(LPVOID*)(char*)&pMarshal);
645 if (hres) {
646 FIXME("Failed to get IMarshal iface, %lx?\n",hres);
647 return hres;
649 hres = IMarshal_ReleaseMarshalData(pMarshal,pStm);
650 if (hres) {
651 FIXME("Failed to releasemarshaldata the interface, %lx?\n",hres);
653 IMarshal_Release(pMarshal);
654 IUnknown_Release(pUnk);
655 return hres;
659 /***********************************************************************
660 * CoMarshalInterThreadInterfaceInStream [OLE32.@]
662 * Marshal an interface across threads in the same process.
664 * PARAMS
665 * riid [I] Identifier of the interface to be marshalled.
666 * pUnk [I] Pointer to IUnknown-derived interface that will be marshalled.
667 * ppStm [O] Pointer to IStream object that is created and then used to store the marshalled inteface.
669 * RETURNS
670 * Success: S_OK
671 * Failure: E_OUTOFMEMORY and other COM error codes
673 * SEE
674 * CoMarshalInterface(), CoUnmarshalInterface() and CoGetInterfaceAndReleaseStream()
676 HRESULT WINAPI
677 CoMarshalInterThreadInterfaceInStream(
678 REFIID riid, LPUNKNOWN pUnk, LPSTREAM * ppStm)
680 ULARGE_INTEGER xpos;
681 LARGE_INTEGER seekto;
682 HRESULT hres;
684 TRACE("(%s, %p, %p)\n",debugstr_guid(riid), pUnk, ppStm);
686 hres = CreateStreamOnHGlobal(0, TRUE, ppStm);
687 if (FAILED(hres)) return hres;
688 hres = CoMarshalInterface(*ppStm, riid, pUnk, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
690 /* FIXME: is this needed? */
691 memset(&seekto,0,sizeof(seekto));
692 IStream_Seek(*ppStm,seekto,SEEK_SET,&xpos);
694 return hres;
697 /***********************************************************************
698 * CoGetInterfaceAndReleaseStream [OLE32.@]
700 * Unmarshalls an inteface from a stream and then releases the stream.
702 * PARAMS
703 * pStm [I] Stream that contains the marshalled inteface.
704 * riid [I] Interface identifier of the object to unmarshall.
705 * ppv [O] Address of pointer where the requested interface object will be stored.
707 * RETURNS
708 * Success: S_OK
709 * Failure: A COM error code
711 * SEE
712 * CoMarshalInterThreadInterfaceInStream() and CoUnmarshalInteface()
714 HRESULT WINAPI
715 CoGetInterfaceAndReleaseStream(LPSTREAM pStm,REFIID riid, LPVOID *ppv)
717 HRESULT hres;
719 TRACE("(%p, %s, %p)\n", pStm, debugstr_guid(riid), ppv);
721 hres = CoUnmarshalInterface(pStm, riid, ppv);
722 IStream_Release(pStm);
723 return hres;
726 static HRESULT WINAPI
727 SMCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid, LPVOID *ppv) {
728 *ppv = NULL;
729 if (IsEqualIID(riid,&IID_IUnknown) || IsEqualIID(riid,&IID_IClassFactory)) {
730 *ppv = (LPVOID)iface;
731 return S_OK;
733 return E_NOINTERFACE;
735 static ULONG WINAPI SMCF_AddRef(LPCLASSFACTORY iface) { return 2; }
736 static ULONG WINAPI SMCF_Release(LPCLASSFACTORY iface) { return 1; }
738 static HRESULT WINAPI
739 SMCF_CreateInstance(
740 LPCLASSFACTORY iface, LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv
742 if (IsEqualIID(riid,&IID_IMarshal)) {
743 StdMarshalImpl *dm;
744 dm=(StdMarshalImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(StdMarshalImpl));
745 if (!dm)
746 return E_FAIL;
747 dm->lpvtbl = &stdmvtbl;
748 dm->ref = 1;
749 *ppv = (LPVOID)dm;
750 return S_OK;
752 FIXME("(%s), not supported.\n",debugstr_guid(riid));
753 return E_NOINTERFACE;
756 static HRESULT WINAPI
757 SMCF_LockServer(LPCLASSFACTORY iface, BOOL fLock) {
758 FIXME("(%d), stub!\n",fLock);
759 return S_OK;
762 static ICOM_VTABLE(IClassFactory) dfmarshalcfvtbl = {
763 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
764 SMCF_QueryInterface,
765 SMCF_AddRef,
766 SMCF_Release,
767 SMCF_CreateInstance,
768 SMCF_LockServer
770 static ICOM_VTABLE(IClassFactory) *pdfmarshalcfvtbl = &dfmarshalcfvtbl;
772 HRESULT
773 MARSHAL_GetStandardMarshalCF(LPVOID *ppv) {
774 *ppv = &pdfmarshalcfvtbl;
775 return S_OK;