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
37 #include "wine/unicode.h"
38 #include "wine/obj_base.h"
39 #include "wine/obj_clientserver.h"
40 #include "wine/obj_misc.h"
41 #include "wine/obj_marshal.h"
42 #include "wine/obj_storage.h"
43 #include "wine/obj_channel.h"
44 #include "wine/winbase16.h"
45 #include "compobj_private.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(ole);
52 /* Marshaling just passes a unique identifier to the remote client,
53 * that makes it possible to find the passed interface again.
55 * So basically we need a set of values that make it unique.
57 * Process Identifier, Object IUnknown ptr, IID
59 * Note that the IUnknown_QI(ob,xiid,&ppv) always returns the SAME ppv value!
62 typedef struct _mid2unknown {
67 typedef struct _mid2stub {
73 static mid2stub *stubs = NULL;
74 static int nrofstubs = 0;
76 static mid2unknown *proxies = NULL;
77 static int nrofproxies = 0;
80 MARSHAL_Find_Stub_Server(wine_marshal_id *mid,LPUNKNOWN *punk) {
83 for (i=0;i<nrofstubs;i++) {
84 if (MARSHAL_Compare_Mids_NoInterface(mid,&(stubs[i].mid))) {
85 *punk = stubs[i].pUnkServer;
86 IUnknown_AddRef((*punk));
94 MARSHAL_Find_Stub_Buffer(wine_marshal_id *mid,IRpcStubBuffer **stub) {
97 for (i=0;i<nrofstubs;i++) {
98 if (MARSHAL_Compare_Mids(mid,&(stubs[i].mid))) {
99 *stub = stubs[i].stub;
100 IUnknown_AddRef((*stub));
108 MARSHAL_Find_Stub(wine_marshal_id *mid,LPUNKNOWN *pUnk) {
111 for (i=0;i<nrofstubs;i++) {
112 if (MARSHAL_Compare_Mids(mid,&(stubs[i].mid))) {
113 *pUnk = stubs[i].pUnkServer;
114 IUnknown_AddRef((*pUnk));
122 MARSHAL_Register_Stub(wine_marshal_id *mid,LPUNKNOWN pUnk,IRpcStubBuffer *stub) {
124 if (!MARSHAL_Find_Stub(mid,&xPunk)) {
125 FIXME("Already have entry for (%lx/%s)!\n",mid->objectid,debugstr_guid(&(mid->iid)));
129 stubs=HeapReAlloc(GetProcessHeap(),0,stubs,sizeof(stubs[0])*(nrofstubs+1));
131 stubs=HeapAlloc(GetProcessHeap(),0,sizeof(stubs[0]));
132 if (!stubs) return E_OUTOFMEMORY;
133 stubs[nrofstubs].stub = stub;
134 stubs[nrofstubs].pUnkServer = pUnk;
135 memcpy(&(stubs[nrofstubs].mid),mid,sizeof(*mid));
141 MARSHAL_Find_Proxy(wine_marshal_id *mid,LPUNKNOWN *punk) {
144 for (i=0;i<nrofproxies;i++)
145 if (MARSHAL_Compare_Mids(mid,&(proxies[i].mid))) {
146 *punk = proxies[i].pUnk;
147 IUnknown_AddRef((*punk));
154 MARSHAL_Find_Proxy_Object(wine_marshal_id *mid,LPUNKNOWN *punk) {
157 for (i=0;i<nrofproxies;i++)
158 if (MARSHAL_Compare_Mids_NoInterface(mid,&(proxies[i].mid))) {
159 *punk = proxies[i].pUnk;
160 IUnknown_AddRef((*punk));
167 MARSHAL_Register_Proxy(wine_marshal_id *mid,LPUNKNOWN punk) {
170 for (i=0;i<nrofproxies;i++) {
171 if (MARSHAL_Compare_Mids(mid,&(proxies[i].mid))) {
172 ERR("Already have mid?\n");
177 proxies = HeapReAlloc(GetProcessHeap(),0,proxies,sizeof(proxies[0])*(nrofproxies+1));
179 proxies = HeapAlloc(GetProcessHeap(),0,sizeof(proxies[0]));
180 memcpy(&(proxies[nrofproxies].mid),mid,sizeof(*mid));
181 proxies[nrofproxies].pUnk = punk;
183 IUnknown_AddRef(punk);
187 /********************** StdMarshal implementation ****************************/
188 typedef struct _StdMarshalImpl {
189 ICOM_VTABLE(IMarshal) *lpvtbl;
194 LPVOID pvDestContext;
199 StdMarshalImpl_QueryInterface(LPMARSHAL iface,REFIID riid,LPVOID *ppv) {
201 if (IsEqualIID(&IID_IUnknown,riid) || IsEqualIID(&IID_IMarshal,riid)) {
203 IUnknown_AddRef(iface);
206 FIXME("No interface for %s.\n",debugstr_guid(riid));
207 return E_NOINTERFACE;
211 StdMarshalImpl_AddRef(LPMARSHAL iface) {
212 ICOM_THIS(StdMarshalImpl,iface);
218 StdMarshalImpl_Release(LPMARSHAL iface) {
219 ICOM_THIS(StdMarshalImpl,iface);
224 HeapFree(GetProcessHeap(),0,This);
229 StdMarshalImpl_GetUnmarshalClass(
230 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
231 void* pvDestContext, DWORD mshlflags, CLSID* pCid
233 memcpy(pCid,&CLSID_DfMarshal,sizeof(CLSID_DfMarshal));
238 StdMarshalImpl_GetMarshalSizeMax(
239 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
240 void* pvDestContext, DWORD mshlflags, DWORD* pSize
242 *pSize = sizeof(wine_marshal_id)+sizeof(wine_marshal_data);
247 StdMarshalImpl_MarshalInterface(
248 LPMARSHAL iface, IStream *pStm,REFIID riid, void* pv, DWORD dwDestContext,
249 void* pvDestContext, DWORD mshlflags
252 wine_marshal_data md;
256 IRpcStubBuffer *stub;
257 IPSFactoryBuffer *psfacbuf;
259 TRACE("(...,%s,...)\n",debugstr_guid(riid));
260 IUnknown_QueryInterface((LPUNKNOWN)pv,&IID_IUnknown,(LPVOID*)&pUnk);
261 mid.processid = GetCurrentProcessId();
262 mid.objectid = (DWORD)pUnk; /* FIXME */
263 IUnknown_Release(pUnk);
264 memcpy(&mid.iid,riid,sizeof(mid.iid));
265 md.dwDestContext = dwDestContext;
266 md.mshlflags = mshlflags;
267 hres = IStream_Write(pStm,&mid,sizeof(mid),&res);
268 if (hres) return hres;
269 hres = IStream_Write(pStm,&md,sizeof(md),&res);
270 if (hres) return hres;
272 if (SUCCEEDED(MARSHAL_Find_Stub(&mid,&pUnk))) {
273 IUnknown_Release(pUnk);
276 hres = get_facbuf_for_iid(riid,&psfacbuf);
277 if (hres) return hres;
278 hres = IPSFactoryBuffer_CreateStub(psfacbuf,riid,pv,&stub);
279 IPSFactoryBuffer_Release(psfacbuf);
281 FIXME("Failed to create a stub for %s\n",debugstr_guid(riid));
284 IUnknown_QueryInterface((LPUNKNOWN)pv,riid,(LPVOID*)&pUnk);
285 MARSHAL_Register_Stub(&mid,pUnk,stub);
286 IUnknown_Release(pUnk);
291 StdMarshalImpl_UnmarshalInterface(
292 LPMARSHAL iface, IStream *pStm, REFIID riid, void **ppv
295 wine_marshal_data md;
298 IPSFactoryBuffer *psfacbuf;
299 IRpcProxyBuffer *rpcproxy;
300 IRpcChannelBuffer *chanbuf;
302 TRACE("(...,%s,....)\n",debugstr_guid(riid));
303 hres = IStream_Read(pStm,&mid,sizeof(mid),&res);
304 if (hres) return hres;
305 hres = IStream_Read(pStm,&md,sizeof(md),&res);
306 if (hres) return hres;
307 if (SUCCEEDED(MARSHAL_Find_Stub(&mid,(LPUNKNOWN*)ppv))) {
308 FIXME("Calling back to ourselves for %s!\n",debugstr_guid(riid));
311 hres = get_facbuf_for_iid(riid,&psfacbuf);
312 if (hres) return hres;
313 hres = IPSFactoryBuffer_CreateProxy(psfacbuf,NULL,riid,&rpcproxy,ppv);
315 FIXME("Failed to create a proxy for %s\n",debugstr_guid(riid));
318 hres = PIPE_GetNewPipeBuf(&mid,&chanbuf);
320 ERR("Failed to get an rpc channel buffer for %s\n",debugstr_guid(riid));
322 IRpcProxyBuffer_Connect(rpcproxy,chanbuf);
323 IRpcProxyBuffer_Release(rpcproxy); /* no need */
325 IPSFactoryBuffer_Release(psfacbuf);
330 StdMarshalImpl_ReleaseMarshalData(LPMARSHAL iface, IStream *pStm) {
331 FIXME("(), stub!\n");
336 StdMarshalImpl_DisconnectObject(LPMARSHAL iface, DWORD dwReserved) {
337 FIXME("(), stub!\n");
341 ICOM_VTABLE(IMarshal) stdmvtbl = {
342 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
343 StdMarshalImpl_QueryInterface,
344 StdMarshalImpl_AddRef,
345 StdMarshalImpl_Release,
346 StdMarshalImpl_GetUnmarshalClass,
347 StdMarshalImpl_GetMarshalSizeMax,
348 StdMarshalImpl_MarshalInterface,
349 StdMarshalImpl_UnmarshalInterface,
350 StdMarshalImpl_ReleaseMarshalData,
351 StdMarshalImpl_DisconnectObject
354 /***********************************************************************
355 * CoGetStandardMarshal [OLE32.23]
357 * When the COM library in the client process receives a marshaled
358 * interface pointer, it looks for a CLSID to be used in creating a proxy
359 * for the purposes of unmarshaling the packet. If the packet does not
360 * contain a CLSID for the proxy, COM calls CoGetStandardMarshal, passing a
362 * This function creates a standard proxy in the client process and returns
363 * a pointer to that proxy's implementation of IMarshal.
364 * COM uses this pointer to call CoUnmarshalInterface to retrieve the pointer
365 * to the requested interface.
368 CoGetStandardMarshal(
369 REFIID riid,IUnknown *pUnk,DWORD dwDestContext,LPVOID pvDestContext,
370 DWORD mshlflags, LPMARSHAL *pMarshal
375 FIXME("(%s,NULL,%lx,%p,%lx,%p), unimplemented yet.\n",
376 debugstr_guid(riid),dwDestContext,pvDestContext,mshlflags,pMarshal
380 TRACE("(%s,%p,%lx,%p,%lx,%p)\n",
381 debugstr_guid(riid),pUnk,dwDestContext,pvDestContext,mshlflags,pMarshal
383 dm = (StdMarshalImpl*) *pMarshal = HeapAlloc(GetProcessHeap(),0,sizeof(StdMarshalImpl));
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.21]
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.34]
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.50]
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 * CoMarshalInterThreadInterfaceInStream [OLE32.33]
549 * Marshal interfaces across threads. We don't have a thread distinction,
550 * meaning most interfaces just work across different threads, the RPC
554 CoMarshalInterThreadInterfaceInStream(
555 REFIID riid, LPUNKNOWN pUnk, LPSTREAM * ppStm
559 LARGE_INTEGER seekto;
562 TRACE("(,%s,)\n",debugstr_guid(riid));
563 hres = CreateStreamOnHGlobal(0, TRUE, ppStm);
564 if (hres) return hres;
565 /* CoMarshalInterface(...); */
566 hres = IStream_Write(*ppStm,&pUnk,sizeof(LPUNKNOWN),&res);
567 if (hres) return hres;
568 memset(&seekto,0,sizeof(seekto));
569 IStream_Seek(*ppStm,seekto,SEEK_SET,&xpos);
573 /***********************************************************************
574 * CoGetInterfaceAndReleaseStream [OLE32.19]
577 CoGetInterfaceAndReleaseStream(LPSTREAM pStm,REFIID riid, LPVOID *ppv) {
582 TRACE("(,%s,)\n",debugstr_guid(riid));
583 /* CoUnmarshalInterface(...); */
584 hres = IStream_Read(pStm,&pUnk,sizeof(LPUNKNOWN),&res);
585 if (hres) return hres;
586 IStream_Release(pStm);
587 return IUnknown_QueryInterface(pUnk,riid,ppv);
590 static HRESULT WINAPI
591 SMCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid, LPVOID *ppv) {
593 if (IsEqualIID(riid,&IID_IUnknown) || IsEqualIID(riid,&IID_IClassFactory)) {
594 *ppv = (LPVOID)iface;
597 return E_NOINTERFACE;
599 static ULONG WINAPI SMCF_AddRef(LPCLASSFACTORY iface) { return 2; }
600 static ULONG WINAPI SMCF_Release(LPCLASSFACTORY iface) { return 1; }
602 static HRESULT WINAPI
604 LPCLASSFACTORY iface, LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv
606 if (IsEqualIID(riid,&IID_IMarshal)) {
608 dm=(StdMarshalImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(StdMarshalImpl));
611 dm->lpvtbl = &stdmvtbl;
616 FIXME("(%s), not supported.\n",debugstr_guid(riid));
617 return E_NOINTERFACE;
620 static HRESULT WINAPI
621 SMCF_LockServer(LPCLASSFACTORY iface, BOOL fLock) {
622 FIXME("(%d), stub!\n",fLock);
626 static ICOM_VTABLE(IClassFactory) dfmarshalcfvtbl = {
627 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
634 static ICOM_VTABLE(IClassFactory) *pdfmarshalcfvtbl = &dfmarshalcfvtbl;
637 MARSHAL_GetStandardMarshalCF(LPVOID *ppv) {
638 *ppv = &pdfmarshalcfvtbl;