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
40 #include "wine/unicode.h"
41 #include "wine/winbase16.h"
42 #include "compobj_private.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 typedef struct _mid2unknown
{
66 typedef struct _mid2stub
{
72 static mid2stub
*stubs
= NULL
;
73 static int nrofstubs
= 0;
75 static mid2unknown
*proxies
= NULL
;
76 static int nrofproxies
= 0;
79 MARSHAL_Find_Stub_Server(wine_marshal_id
*mid
,LPUNKNOWN
*punk
) {
82 for (i
=0;i
<nrofstubs
;i
++) {
83 if (MARSHAL_Compare_Mids_NoInterface(mid
,&(stubs
[i
].mid
))) {
84 *punk
= stubs
[i
].pUnkServer
;
85 IUnknown_AddRef((*punk
));
93 MARSHAL_Find_Stub_Buffer(wine_marshal_id
*mid
,IRpcStubBuffer
**stub
) {
96 for (i
=0;i
<nrofstubs
;i
++) {
97 if (MARSHAL_Compare_Mids(mid
,&(stubs
[i
].mid
))) {
98 *stub
= stubs
[i
].stub
;
99 IUnknown_AddRef((*stub
));
107 MARSHAL_Find_Stub(wine_marshal_id
*mid
,LPUNKNOWN
*pUnk
) {
110 for (i
=0;i
<nrofstubs
;i
++) {
111 if (MARSHAL_Compare_Mids(mid
,&(stubs
[i
].mid
))) {
112 *pUnk
= stubs
[i
].pUnkServer
;
113 IUnknown_AddRef((*pUnk
));
121 MARSHAL_Register_Stub(wine_marshal_id
*mid
,LPUNKNOWN pUnk
,IRpcStubBuffer
*stub
) {
123 if (!MARSHAL_Find_Stub(mid
,&xPunk
)) {
124 FIXME("Already have entry for (%lx/%s)!\n",mid
->objectid
,debugstr_guid(&(mid
->iid
)));
128 stubs
=HeapReAlloc(GetProcessHeap(),0,stubs
,sizeof(stubs
[0])*(nrofstubs
+1));
130 stubs
=HeapAlloc(GetProcessHeap(),0,sizeof(stubs
[0]));
131 if (!stubs
) return E_OUTOFMEMORY
;
132 stubs
[nrofstubs
].stub
= stub
;
133 stubs
[nrofstubs
].pUnkServer
= pUnk
;
134 memcpy(&(stubs
[nrofstubs
].mid
),mid
,sizeof(*mid
));
140 MARSHAL_Find_Proxy(wine_marshal_id
*mid
,LPUNKNOWN
*punk
) {
143 for (i
=0;i
<nrofproxies
;i
++)
144 if (MARSHAL_Compare_Mids(mid
,&(proxies
[i
].mid
))) {
145 *punk
= proxies
[i
].pUnk
;
146 IUnknown_AddRef((*punk
));
153 MARSHAL_Find_Proxy_Object(wine_marshal_id
*mid
,LPUNKNOWN
*punk
) {
156 for (i
=0;i
<nrofproxies
;i
++)
157 if (MARSHAL_Compare_Mids_NoInterface(mid
,&(proxies
[i
].mid
))) {
158 *punk
= proxies
[i
].pUnk
;
159 IUnknown_AddRef((*punk
));
166 MARSHAL_Register_Proxy(wine_marshal_id
*mid
,LPUNKNOWN punk
) {
169 for (i
=0;i
<nrofproxies
;i
++) {
170 if (MARSHAL_Compare_Mids(mid
,&(proxies
[i
].mid
))) {
171 ERR("Already have mid?\n");
176 proxies
= HeapReAlloc(GetProcessHeap(),0,proxies
,sizeof(proxies
[0])*(nrofproxies
+1));
178 proxies
= HeapAlloc(GetProcessHeap(),0,sizeof(proxies
[0]));
179 memcpy(&(proxies
[nrofproxies
].mid
),mid
,sizeof(*mid
));
180 proxies
[nrofproxies
].pUnk
= punk
;
182 IUnknown_AddRef(punk
);
186 /********************** StdMarshal implementation ****************************/
187 typedef struct _StdMarshalImpl
{
188 ICOM_VTABLE(IMarshal
) *lpvtbl
;
193 LPVOID pvDestContext
;
198 StdMarshalImpl_QueryInterface(LPMARSHAL iface
,REFIID riid
,LPVOID
*ppv
) {
200 if (IsEqualIID(&IID_IUnknown
,riid
) || IsEqualIID(&IID_IMarshal
,riid
)) {
202 IUnknown_AddRef(iface
);
205 FIXME("No interface for %s.\n",debugstr_guid(riid
));
206 return E_NOINTERFACE
;
210 StdMarshalImpl_AddRef(LPMARSHAL iface
) {
211 ICOM_THIS(StdMarshalImpl
,iface
);
217 StdMarshalImpl_Release(LPMARSHAL iface
) {
218 ICOM_THIS(StdMarshalImpl
,iface
);
223 HeapFree(GetProcessHeap(),0,This
);
228 StdMarshalImpl_GetUnmarshalClass(
229 LPMARSHAL iface
, REFIID riid
, void* pv
, DWORD dwDestContext
,
230 void* pvDestContext
, DWORD mshlflags
, CLSID
* pCid
232 memcpy(pCid
,&CLSID_DfMarshal
,sizeof(CLSID_DfMarshal
));
237 StdMarshalImpl_GetMarshalSizeMax(
238 LPMARSHAL iface
, REFIID riid
, void* pv
, DWORD dwDestContext
,
239 void* pvDestContext
, DWORD mshlflags
, DWORD
* pSize
241 *pSize
= sizeof(wine_marshal_id
)+sizeof(wine_marshal_data
);
246 StdMarshalImpl_MarshalInterface(
247 LPMARSHAL iface
, IStream
*pStm
,REFIID riid
, void* pv
, DWORD dwDestContext
,
248 void* pvDestContext
, DWORD mshlflags
251 wine_marshal_data md
;
255 IRpcStubBuffer
*stub
;
256 IPSFactoryBuffer
*psfacbuf
;
258 TRACE("(...,%s,...)\n",debugstr_guid(riid
));
259 IUnknown_QueryInterface((LPUNKNOWN
)pv
,&IID_IUnknown
,(LPVOID
*)&pUnk
);
260 mid
.processid
= GetCurrentProcessId();
261 mid
.objectid
= (DWORD
)pUnk
; /* FIXME */
262 IUnknown_Release(pUnk
);
263 memcpy(&mid
.iid
,riid
,sizeof(mid
.iid
));
264 md
.dwDestContext
= dwDestContext
;
265 md
.mshlflags
= mshlflags
;
266 hres
= IStream_Write(pStm
,&mid
,sizeof(mid
),&res
);
267 if (hres
) return hres
;
268 hres
= IStream_Write(pStm
,&md
,sizeof(md
),&res
);
269 if (hres
) return hres
;
271 if (SUCCEEDED(MARSHAL_Find_Stub(&mid
,&pUnk
))) {
272 IUnknown_Release(pUnk
);
275 hres
= get_facbuf_for_iid(riid
,&psfacbuf
);
276 if (hres
) return hres
;
277 hres
= IPSFactoryBuffer_CreateStub(psfacbuf
,riid
,pv
,&stub
);
278 IPSFactoryBuffer_Release(psfacbuf
);
280 FIXME("Failed to create a stub for %s\n",debugstr_guid(riid
));
283 IUnknown_QueryInterface((LPUNKNOWN
)pv
,riid
,(LPVOID
*)&pUnk
);
284 MARSHAL_Register_Stub(&mid
,pUnk
,stub
);
285 IUnknown_Release(pUnk
);
290 StdMarshalImpl_UnmarshalInterface(
291 LPMARSHAL iface
, IStream
*pStm
, REFIID riid
, void **ppv
294 wine_marshal_data md
;
297 IPSFactoryBuffer
*psfacbuf
;
298 IRpcProxyBuffer
*rpcproxy
;
299 IRpcChannelBuffer
*chanbuf
;
301 TRACE("(...,%s,....)\n",debugstr_guid(riid
));
302 hres
= IStream_Read(pStm
,&mid
,sizeof(mid
),&res
);
303 if (hres
) return hres
;
304 hres
= IStream_Read(pStm
,&md
,sizeof(md
),&res
);
305 if (hres
) return hres
;
306 if (SUCCEEDED(MARSHAL_Find_Stub(&mid
,(LPUNKNOWN
*)ppv
))) {
307 FIXME("Calling back to ourselves for %s!\n",debugstr_guid(riid
));
310 hres
= get_facbuf_for_iid(riid
,&psfacbuf
);
311 if (hres
) return hres
;
312 hres
= IPSFactoryBuffer_CreateProxy(psfacbuf
,NULL
,riid
,&rpcproxy
,ppv
);
314 FIXME("Failed to create a proxy for %s\n",debugstr_guid(riid
));
317 hres
= PIPE_GetNewPipeBuf(&mid
,&chanbuf
);
319 ERR("Failed to get an rpc channel buffer for %s\n",debugstr_guid(riid
));
321 IRpcProxyBuffer_Connect(rpcproxy
,chanbuf
);
322 IRpcProxyBuffer_Release(rpcproxy
); /* no need */
324 IPSFactoryBuffer_Release(psfacbuf
);
329 StdMarshalImpl_ReleaseMarshalData(LPMARSHAL iface
, IStream
*pStm
) {
330 FIXME("(), stub!\n");
335 StdMarshalImpl_DisconnectObject(LPMARSHAL iface
, DWORD dwReserved
) {
336 FIXME("(), stub!\n");
340 ICOM_VTABLE(IMarshal
) stdmvtbl
= {
341 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
342 StdMarshalImpl_QueryInterface
,
343 StdMarshalImpl_AddRef
,
344 StdMarshalImpl_Release
,
345 StdMarshalImpl_GetUnmarshalClass
,
346 StdMarshalImpl_GetMarshalSizeMax
,
347 StdMarshalImpl_MarshalInterface
,
348 StdMarshalImpl_UnmarshalInterface
,
349 StdMarshalImpl_ReleaseMarshalData
,
350 StdMarshalImpl_DisconnectObject
353 /***********************************************************************
354 * CoGetStandardMarshal [OLE32.@]
356 * When the COM library in the client process receives a marshalled
357 * interface pointer, it looks for a CLSID to be used in creating a proxy
358 * for the purposes of unmarshalling the packet. If the packet does not
359 * contain a CLSID for the proxy, COM calls CoGetStandardMarshal, passing a
361 * This function creates a standard proxy in the client process and returns
362 * a pointer to that proxy's implementation of IMarshal.
363 * COM uses this pointer to call CoUnmarshalInterface to retrieve the pointer
364 * to the requested interface.
367 CoGetStandardMarshal(
368 REFIID riid
,IUnknown
*pUnk
,DWORD dwDestContext
,LPVOID pvDestContext
,
369 DWORD mshlflags
, LPMARSHAL
*pMarshal
374 FIXME("(%s,NULL,%lx,%p,%lx,%p), unimplemented yet.\n",
375 debugstr_guid(riid
),dwDestContext
,pvDestContext
,mshlflags
,pMarshal
379 TRACE("(%s,%p,%lx,%p,%lx,%p)\n",
380 debugstr_guid(riid
),pUnk
,dwDestContext
,pvDestContext
,mshlflags
,pMarshal
382 *pMarshal
= HeapAlloc(GetProcessHeap(),0,sizeof(StdMarshalImpl
));
383 dm
= (StdMarshalImpl
*) *pMarshal
;
384 if (!dm
) return E_FAIL
;
385 dm
->lpvtbl
= &stdmvtbl
;
388 memcpy(&dm
->iid
,riid
,sizeof(dm
->iid
));
389 dm
->dwDestContext
= dwDestContext
;
390 dm
->pvDestContext
= pvDestContext
;
391 dm
->mshlflags
= mshlflags
;
395 /* Helper function for getting Marshaler */
396 static HRESULT WINAPI
397 _GetMarshaller(REFIID riid
, IUnknown
*pUnk
,DWORD dwDestContext
,
398 void *pvDestContext
, DWORD mshlFlags
, LPMARSHAL
*pMarshal
404 hres
= IUnknown_QueryInterface(pUnk
,&IID_IMarshal
,(LPVOID
*)pMarshal
);
406 hres
= CoGetStandardMarshal(riid
,pUnk
,dwDestContext
,pvDestContext
,mshlFlags
,pMarshal
);
410 /***********************************************************************
411 * CoGetMarshalSizeMax [OLE32.@]
414 CoGetMarshalSizeMax(ULONG
*pulSize
, REFIID riid
, IUnknown
*pUnk
,
415 DWORD dwDestContext
, void *pvDestContext
, DWORD mshlFlags
420 hres
= _GetMarshaller(riid
,pUnk
,dwDestContext
,pvDestContext
,mshlFlags
,&pMarshal
);
423 hres
= IMarshal_GetMarshalSizeMax(pMarshal
,riid
,pUnk
,dwDestContext
,pvDestContext
,mshlFlags
,pulSize
);
424 *pulSize
+= sizeof(wine_marshal_id
)+sizeof(wine_marshal_data
)+sizeof(CLSID
);
425 IMarshal_Release(pMarshal
);
430 /***********************************************************************
431 * CoMarshalInterface [OLE32.@]
434 CoMarshalInterface( IStream
*pStm
, REFIID riid
, IUnknown
*pUnk
,
435 DWORD dwDestContext
, void *pvDestContext
, DWORD mshlflags
442 wine_marshal_data md
;
446 TRACE("(%p, %s, %p, %lx, %p, %lx)\n",
447 pStm
,debugstr_guid(riid
),pUnk
,dwDestContext
,pvDestContext
,mshlflags
449 STUBMGR_Start(); /* Just to be sure we have one running. */
450 mid
.processid
= GetCurrentProcessId();
451 IUnknown_QueryInterface(pUnk
,&IID_IUnknown
,(LPVOID
*)&pUnknown
);
452 mid
.objectid
= (DWORD
)pUnknown
;
453 IUnknown_Release(pUnknown
);
454 memcpy(&mid
.iid
,riid
,sizeof(mid
.iid
));
455 md
.dwDestContext
= dwDestContext
;
456 md
.mshlflags
= mshlflags
;
457 hres
= IStream_Write(pStm
,&mid
,sizeof(mid
),&res
);
458 if (hres
) return hres
;
459 hres
= IStream_Write(pStm
,&md
,sizeof(md
),&res
);
460 if (hres
) return hres
;
461 hres
= _GetMarshaller(riid
,pUnk
,dwDestContext
,pvDestContext
,mshlflags
,&pMarshal
);
463 FIXME("Failed to get marshaller, %lx?\n",hres
);
466 hres
= IMarshal_GetUnmarshalClass(pMarshal
,riid
,pUnk
,dwDestContext
,pvDestContext
,mshlflags
,&xclsid
);
468 FIXME("IMarshal:GetUnmarshalClass failed, %lx\n",hres
);
469 goto release_marshal
;
471 hres
= IStream_Write(pStm
,&xclsid
,sizeof(xclsid
),&writeres
);
473 FIXME("Stream write failed, %lx\n",hres
);
474 goto release_marshal
;
476 hres
= IMarshal_MarshalInterface(pMarshal
,pStm
,riid
,pUnk
,dwDestContext
,pvDestContext
,mshlflags
);
478 if (IsEqualGUID(riid
,&IID_IClassFactory
)) {
479 MESSAGE("\nERROR: You need to merge the 'winedefault.reg' file into your\n");
480 MESSAGE(" Wine registry by running: `regedit winedefault.reg'\n\n");
482 if (IsEqualGUID(riid
,&IID_IOleObject
)) {
483 ERR("WINE currently cannot marshal IOleObject interfaces. This means you cannot embed/link OLE objects between applications.\n");
485 FIXME("Failed to marshal the interface %s, %lx?\n",debugstr_guid(riid
),hres
);
488 goto release_marshal
;
491 IMarshal_Release(pMarshal
);
496 /***********************************************************************
497 * CoUnmarshalInterface [OLE32.@]
500 CoUnmarshalInterface(IStream
*pStm
, REFIID riid
, LPVOID
*ppv
) {
503 wine_marshal_data md
;
509 TRACE("(%p,%s,%p)\n",pStm
,debugstr_guid(riid
),ppv
);
511 hres
= IStream_Read(pStm
,&mid
,sizeof(mid
),&res
);
513 FIXME("Stream read 1 failed, %lx, (%ld of %d)\n",hres
,res
,sizeof(mid
));
516 hres
= IStream_Read(pStm
,&md
,sizeof(md
),&res
);
518 FIXME("Stream read 2 failed, %lx, (%ld of %d)\n",hres
,res
,sizeof(md
));
521 hres
= IStream_Read(pStm
,&xclsid
,sizeof(xclsid
),&res
);
523 FIXME("Stream read 3 failed, %lx, (%ld of %d)\n",hres
,res
,sizeof(xclsid
));
526 hres
=CoCreateInstance(&xclsid
,NULL
,CLSCTX_INPROC_SERVER
| CLSCTX_INPROC_HANDLER
| CLSCTX_LOCAL_SERVER
,&IID_IMarshal
,(void**)&pUnk
);
528 FIXME("Failed to create instance of unmarshaller %s.\n",debugstr_guid(&xclsid
));
531 hres
= _GetMarshaller(riid
,pUnk
,md
.dwDestContext
,NULL
,md
.mshlflags
,&pMarshal
);
533 FIXME("Failed to get unmarshaller, %lx?\n",hres
);
536 hres
= IMarshal_UnmarshalInterface(pMarshal
,pStm
,riid
,ppv
);
538 FIXME("Failed to Unmarshal the interface, %lx?\n",hres
);
539 goto release_marshal
;
542 IMarshal_Release(pMarshal
);
546 /***********************************************************************
547 * CoReleaseMarshalData [OLE32.@]
550 CoReleaseMarshalData(IStream
*pStm
) {
553 wine_marshal_data md
;
559 TRACE("(%p)\n",pStm
);
561 hres
= IStream_Read(pStm
,&mid
,sizeof(mid
),&res
);
563 FIXME("Stream read 1 failed, %lx, (%ld of %d)\n",hres
,res
,sizeof(mid
));
566 hres
= IStream_Read(pStm
,&md
,sizeof(md
),&res
);
568 FIXME("Stream read 2 failed, %lx, (%ld of %d)\n",hres
,res
,sizeof(md
));
571 hres
= IStream_Read(pStm
,&xclsid
,sizeof(xclsid
),&res
);
573 FIXME("Stream read 3 failed, %lx, (%ld of %d)\n",hres
,res
,sizeof(xclsid
));
576 hres
=CoCreateInstance(&xclsid
,NULL
,CLSCTX_INPROC_SERVER
| CLSCTX_INPROC_HANDLER
| CLSCTX_LOCAL_SERVER
,&IID_IMarshal
,(void**)(char*)&pUnk
);
578 FIXME("Failed to create instance of unmarshaller %s.\n",debugstr_guid(&xclsid
));
581 hres
= IUnknown_QueryInterface(pUnk
,&IID_IMarshal
,(LPVOID
*)(char*)&pMarshal
);
583 FIXME("Failed to get IMarshal iface, %lx?\n",hres
);
586 hres
= IMarshal_ReleaseMarshalData(pMarshal
,pStm
);
588 FIXME("Failed to releasemarshaldata the interface, %lx?\n",hres
);
590 IMarshal_Release(pMarshal
);
591 IUnknown_Release(pUnk
);
596 /***********************************************************************
597 * CoMarshalInterThreadInterfaceInStream [OLE32.@]
599 * Marshal an interface across threads in the same process.
602 * riid [I] Identifier of the interface to be marshalled.
603 * pUnk [I] Pointer to IUnknown-derived interface that will be marshalled.
604 * ppStm [O] Pointer to IStream object that is created and then used to store the marshalled inteface.
608 * Failure: E_OUTOFMEMORY and other COM error codes
611 * CoMarshalInterface(), CoUnmarshalInterface() and CoGetInterfaceAndReleaseStream()
614 CoMarshalInterThreadInterfaceInStream(
615 REFIID riid
, LPUNKNOWN pUnk
, LPSTREAM
* ppStm
)
618 LARGE_INTEGER seekto
;
621 TRACE("(%s, %p, %p)\n",debugstr_guid(riid
), pUnk
, ppStm
);
623 hres
= CreateStreamOnHGlobal(0, TRUE
, ppStm
);
624 if (FAILED(hres
)) return hres
;
625 hres
= CoMarshalInterface(*ppStm
, riid
, pUnk
, MSHCTX_INPROC
, NULL
, MSHLFLAGS_NORMAL
);
627 /* FIXME: is this needed? */
628 memset(&seekto
,0,sizeof(seekto
));
629 IStream_Seek(*ppStm
,seekto
,SEEK_SET
,&xpos
);
634 /***********************************************************************
635 * CoGetInterfaceAndReleaseStream [OLE32.@]
637 * Unmarshalls an inteface from a stream and then releases the stream.
640 * pStm [I] Stream that contains the marshalled inteface.
641 * riid [I] Interface identifier of the object to unmarshall.
642 * ppv [O] Address of pointer where the requested interface object will be stored.
646 * Failure: A COM error code
649 * CoMarshalInterThreadInterfaceInStream() and CoUnmarshalInteface()
652 CoGetInterfaceAndReleaseStream(LPSTREAM pStm
,REFIID riid
, LPVOID
*ppv
)
656 TRACE("(%p, %s, %p)\n", pStm
, debugstr_guid(riid
), ppv
);
658 hres
= CoUnmarshalInterface(pStm
, riid
, ppv
);
659 IStream_Release(pStm
);
663 static HRESULT WINAPI
664 SMCF_QueryInterface(LPCLASSFACTORY iface
,REFIID riid
, LPVOID
*ppv
) {
666 if (IsEqualIID(riid
,&IID_IUnknown
) || IsEqualIID(riid
,&IID_IClassFactory
)) {
667 *ppv
= (LPVOID
)iface
;
670 return E_NOINTERFACE
;
672 static ULONG WINAPI
SMCF_AddRef(LPCLASSFACTORY iface
) { return 2; }
673 static ULONG WINAPI
SMCF_Release(LPCLASSFACTORY iface
) { return 1; }
675 static HRESULT WINAPI
677 LPCLASSFACTORY iface
, LPUNKNOWN pUnk
, REFIID riid
, LPVOID
*ppv
679 if (IsEqualIID(riid
,&IID_IMarshal
)) {
681 dm
=(StdMarshalImpl
*)HeapAlloc(GetProcessHeap(),0,sizeof(StdMarshalImpl
));
684 dm
->lpvtbl
= &stdmvtbl
;
689 FIXME("(%s), not supported.\n",debugstr_guid(riid
));
690 return E_NOINTERFACE
;
693 static HRESULT WINAPI
694 SMCF_LockServer(LPCLASSFACTORY iface
, BOOL fLock
) {
695 FIXME("(%d), stub!\n",fLock
);
699 static ICOM_VTABLE(IClassFactory
) dfmarshalcfvtbl
= {
700 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
707 static ICOM_VTABLE(IClassFactory
) *pdfmarshalcfvtbl
= &dfmarshalcfvtbl
;
710 MARSHAL_GetStandardMarshalCF(LPVOID
*ppv
) {
711 *ppv
= &pdfmarshalcfvtbl
;