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);
318 IPSFactoryBuffer_Release(psfacbuf);
320 ERR("Failed to get an rpc channel buffer for %s\n",debugstr_guid(riid));
322 /* Connect the channel buffer to the proxy and release the no longer
324 * NOTE: The proxy should have taken an extra reference because it also
325 * aggregates the object, so we can safely release our reference to it. */
326 IRpcProxyBuffer_Connect(rpcproxy,chanbuf);
327 IRpcProxyBuffer_Release(rpcproxy);
328 /* IRpcProxyBuffer takes a reference on the channel buffer and
329 * we no longer need it, so release it */
330 IRpcChannelBuffer_Release(chanbuf);
336 StdMarshalImpl_ReleaseMarshalData(LPMARSHAL iface, IStream *pStm) {
337 FIXME("(), stub!\n");
342 StdMarshalImpl_DisconnectObject(LPMARSHAL iface, DWORD dwReserved) {
343 FIXME("(), stub!\n");
347 ICOM_VTABLE(IMarshal) stdmvtbl = {
348 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
349 StdMarshalImpl_QueryInterface,
350 StdMarshalImpl_AddRef,
351 StdMarshalImpl_Release,
352 StdMarshalImpl_GetUnmarshalClass,
353 StdMarshalImpl_GetMarshalSizeMax,
354 StdMarshalImpl_MarshalInterface,
355 StdMarshalImpl_UnmarshalInterface,
356 StdMarshalImpl_ReleaseMarshalData,
357 StdMarshalImpl_DisconnectObject
360 /***********************************************************************
361 * CoGetStandardMarshal [OLE32.@]
363 * When the COM library in the client process receives a marshalled
364 * interface pointer, it looks for a CLSID to be used in creating a proxy
365 * for the purposes of unmarshalling the packet. If the packet does not
366 * contain a CLSID for the proxy, COM calls CoGetStandardMarshal, passing a
368 * This function creates a standard proxy in the client process and returns
369 * a pointer to that proxy's implementation of IMarshal.
370 * COM uses this pointer to call CoUnmarshalInterface to retrieve the pointer
371 * to the requested interface.
374 CoGetStandardMarshal(
375 REFIID riid,IUnknown *pUnk,DWORD dwDestContext,LPVOID pvDestContext,
376 DWORD mshlflags, LPMARSHAL *pMarshal
381 FIXME("(%s,NULL,%lx,%p,%lx,%p), unimplemented yet.\n",
382 debugstr_guid(riid),dwDestContext,pvDestContext,mshlflags,pMarshal
386 TRACE("(%s,%p,%lx,%p,%lx,%p)\n",
387 debugstr_guid(riid),pUnk,dwDestContext,pvDestContext,mshlflags,pMarshal
389 *pMarshal = HeapAlloc(GetProcessHeap(),0,sizeof(StdMarshalImpl));
390 dm = (StdMarshalImpl*) *pMarshal;
391 if (!dm) return E_FAIL;
392 dm->lpvtbl = &stdmvtbl;
395 memcpy(&dm->iid,riid,sizeof(dm->iid));
396 dm->dwDestContext = dwDestContext;
397 dm->pvDestContext = pvDestContext;
398 dm->mshlflags = mshlflags;
402 /* Helper function for getting Marshaler */
403 static HRESULT WINAPI
404 _GetMarshaller(REFIID riid, IUnknown *pUnk,DWORD dwDestContext,
405 void *pvDestContext, DWORD mshlFlags, LPMARSHAL *pMarshal
411 hres = IUnknown_QueryInterface(pUnk,&IID_IMarshal,(LPVOID*)pMarshal);
413 hres = CoGetStandardMarshal(riid,pUnk,dwDestContext,pvDestContext,mshlFlags,pMarshal);
417 /***********************************************************************
418 * CoGetMarshalSizeMax [OLE32.@]
421 CoGetMarshalSizeMax(ULONG *pulSize, REFIID riid, IUnknown *pUnk,
422 DWORD dwDestContext, void *pvDestContext, DWORD mshlFlags
427 hres = _GetMarshaller(riid,pUnk,dwDestContext,pvDestContext,mshlFlags,&pMarshal);
430 hres = IMarshal_GetMarshalSizeMax(pMarshal,riid,pUnk,dwDestContext,pvDestContext,mshlFlags,pulSize);
431 *pulSize += sizeof(wine_marshal_id)+sizeof(wine_marshal_data)+sizeof(CLSID);
432 IMarshal_Release(pMarshal);
437 /***********************************************************************
438 * CoMarshalInterface [OLE32.@]
441 CoMarshalInterface( IStream *pStm, REFIID riid, IUnknown *pUnk,
442 DWORD dwDestContext, void *pvDestContext, DWORD mshlflags
449 wine_marshal_data md;
453 TRACE("(%p, %s, %p, %lx, %p, %lx)\n",
454 pStm,debugstr_guid(riid),pUnk,dwDestContext,pvDestContext,mshlflags
456 STUBMGR_Start(); /* Just to be sure we have one running. */
457 mid.processid = GetCurrentProcessId();
458 IUnknown_QueryInterface(pUnk,&IID_IUnknown,(LPVOID*)&pUnknown);
459 mid.objectid = (DWORD)pUnknown;
460 IUnknown_Release(pUnknown);
461 memcpy(&mid.iid,riid,sizeof(mid.iid));
462 md.dwDestContext = dwDestContext;
463 md.mshlflags = mshlflags;
464 hres = IStream_Write(pStm,&mid,sizeof(mid),&res);
465 if (hres) return hres;
466 hres = IStream_Write(pStm,&md,sizeof(md),&res);
467 if (hres) return hres;
468 hres = _GetMarshaller(riid,pUnk,dwDestContext,pvDestContext,mshlflags,&pMarshal);
470 FIXME("Failed to get marshaller, %lx?\n",hres);
473 hres = IMarshal_GetUnmarshalClass(pMarshal,riid,pUnk,dwDestContext,pvDestContext,mshlflags,&xclsid);
475 FIXME("IMarshal:GetUnmarshalClass failed, %lx\n",hres);
476 goto release_marshal;
478 hres = IStream_Write(pStm,&xclsid,sizeof(xclsid),&writeres);
480 FIXME("Stream write failed, %lx\n",hres);
481 goto release_marshal;
483 hres = IMarshal_MarshalInterface(pMarshal,pStm,riid,pUnk,dwDestContext,pvDestContext,mshlflags);
485 if (IsEqualGUID(riid,&IID_IOleObject)) {
486 ERR("WINE currently cannot marshal IOleObject interfaces. This means you cannot embed/link OLE objects between applications.\n");
488 FIXME("Failed to marshal the interface %s, %lx?\n",debugstr_guid(riid),hres);
492 IMarshal_Release(pMarshal);
497 /***********************************************************************
498 * CoUnmarshalInterface [OLE32.@]
501 CoUnmarshalInterface(IStream *pStm, REFIID riid, LPVOID *ppv) {
504 wine_marshal_data md;
510 TRACE("(%p,%s,%p)\n",pStm,debugstr_guid(riid),ppv);
512 hres = IStream_Read(pStm,&mid,sizeof(mid),&res);
514 FIXME("Stream read 1 failed, %lx, (%ld of %d)\n",hres,res,sizeof(mid));
517 hres = IStream_Read(pStm,&md,sizeof(md),&res);
519 FIXME("Stream read 2 failed, %lx, (%ld of %d)\n",hres,res,sizeof(md));
522 hres = IStream_Read(pStm,&xclsid,sizeof(xclsid),&res);
524 FIXME("Stream read 3 failed, %lx, (%ld of %d)\n",hres,res,sizeof(xclsid));
527 hres=CoCreateInstance(&xclsid,NULL,CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER | CLSCTX_LOCAL_SERVER,&IID_IMarshal,(void**)&pUnk);
529 FIXME("Failed to create instance of unmarshaller %s.\n",debugstr_guid(&xclsid));
532 hres = _GetMarshaller(riid,pUnk,md.dwDestContext,NULL,md.mshlflags,&pMarshal);
534 FIXME("Failed to get unmarshaller, %lx?\n",hres);
537 hres = IMarshal_UnmarshalInterface(pMarshal,pStm,riid,ppv);
539 FIXME("Failed to Unmarshal the interface, %lx?\n",hres);
540 goto release_marshal;
543 IMarshal_Release(pMarshal);
547 /***********************************************************************
548 * CoReleaseMarshalData [OLE32.@]
551 CoReleaseMarshalData(IStream *pStm) {
554 wine_marshal_data md;
560 TRACE("(%p)\n",pStm);
562 hres = IStream_Read(pStm,&mid,sizeof(mid),&res);
564 FIXME("Stream read 1 failed, %lx, (%ld of %d)\n",hres,res,sizeof(mid));
567 hres = IStream_Read(pStm,&md,sizeof(md),&res);
569 FIXME("Stream read 2 failed, %lx, (%ld of %d)\n",hres,res,sizeof(md));
572 hres = IStream_Read(pStm,&xclsid,sizeof(xclsid),&res);
574 FIXME("Stream read 3 failed, %lx, (%ld of %d)\n",hres,res,sizeof(xclsid));
577 hres=CoCreateInstance(&xclsid,NULL,CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER | CLSCTX_LOCAL_SERVER,&IID_IMarshal,(void**)(char*)&pUnk);
579 FIXME("Failed to create instance of unmarshaller %s.\n",debugstr_guid(&xclsid));
582 hres = IUnknown_QueryInterface(pUnk,&IID_IMarshal,(LPVOID*)(char*)&pMarshal);
584 FIXME("Failed to get IMarshal iface, %lx?\n",hres);
587 hres = IMarshal_ReleaseMarshalData(pMarshal,pStm);
589 FIXME("Failed to releasemarshaldata the interface, %lx?\n",hres);
591 IMarshal_Release(pMarshal);
592 IUnknown_Release(pUnk);
597 /***********************************************************************
598 * CoMarshalInterThreadInterfaceInStream [OLE32.@]
600 * Marshal an interface across threads in the same process.
603 * riid [I] Identifier of the interface to be marshalled.
604 * pUnk [I] Pointer to IUnknown-derived interface that will be marshalled.
605 * ppStm [O] Pointer to IStream object that is created and then used to store the marshalled inteface.
609 * Failure: E_OUTOFMEMORY and other COM error codes
612 * CoMarshalInterface(), CoUnmarshalInterface() and CoGetInterfaceAndReleaseStream()
615 CoMarshalInterThreadInterfaceInStream(
616 REFIID riid, LPUNKNOWN pUnk, LPSTREAM * ppStm)
619 LARGE_INTEGER seekto;
622 TRACE("(%s, %p, %p)\n",debugstr_guid(riid), pUnk, ppStm);
624 hres = CreateStreamOnHGlobal(0, TRUE, ppStm);
625 if (FAILED(hres)) return hres;
626 hres = CoMarshalInterface(*ppStm, riid, pUnk, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
628 /* FIXME: is this needed? */
629 memset(&seekto,0,sizeof(seekto));
630 IStream_Seek(*ppStm,seekto,SEEK_SET,&xpos);
635 /***********************************************************************
636 * CoGetInterfaceAndReleaseStream [OLE32.@]
638 * Unmarshalls an inteface from a stream and then releases the stream.
641 * pStm [I] Stream that contains the marshalled inteface.
642 * riid [I] Interface identifier of the object to unmarshall.
643 * ppv [O] Address of pointer where the requested interface object will be stored.
647 * Failure: A COM error code
650 * CoMarshalInterThreadInterfaceInStream() and CoUnmarshalInteface()
653 CoGetInterfaceAndReleaseStream(LPSTREAM pStm,REFIID riid, LPVOID *ppv)
657 TRACE("(%p, %s, %p)\n", pStm, debugstr_guid(riid), ppv);
659 hres = CoUnmarshalInterface(pStm, riid, ppv);
660 IStream_Release(pStm);
664 static HRESULT WINAPI
665 SMCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid, LPVOID *ppv) {
667 if (IsEqualIID(riid,&IID_IUnknown) || IsEqualIID(riid,&IID_IClassFactory)) {
668 *ppv = (LPVOID)iface;
671 return E_NOINTERFACE;
673 static ULONG WINAPI SMCF_AddRef(LPCLASSFACTORY iface) { return 2; }
674 static ULONG WINAPI SMCF_Release(LPCLASSFACTORY iface) { return 1; }
676 static HRESULT WINAPI
678 LPCLASSFACTORY iface, LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv
680 if (IsEqualIID(riid,&IID_IMarshal)) {
682 dm=(StdMarshalImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(StdMarshalImpl));
685 dm->lpvtbl = &stdmvtbl;
690 FIXME("(%s), not supported.\n",debugstr_guid(riid));
691 return E_NOINTERFACE;
694 static HRESULT WINAPI
695 SMCF_LockServer(LPCLASSFACTORY iface, BOOL fLock) {
696 FIXME("(%d), stub!\n",fLock);
700 static ICOM_VTABLE(IClassFactory) dfmarshalcfvtbl = {
701 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
708 static ICOM_VTABLE(IClassFactory) *pdfmarshalcfvtbl = &dfmarshalcfvtbl;
711 MARSHAL_GetStandardMarshalCF(LPVOID *ppv) {
712 *ppv = &pdfmarshalcfvtbl;