4 * Copyright 2002 Marcus Meissner
23 #include "wine/unicode.h"
24 #include "wine/obj_base.h"
25 #include "wine/obj_clientserver.h"
26 #include "wine/obj_misc.h"
27 #include "wine/obj_marshal.h"
28 #include "wine/obj_storage.h"
29 #include "wine/obj_channel.h"
30 #include "wine/winbase16.h"
31 #include "compobj_private.h"
34 #include "debugtools.h"
36 DEFAULT_DEBUG_CHANNEL(ole);
38 /* Marshaling just passes a unique identifier to the remote client,
39 * that makes it possible to find the passed interface again.
41 * So basically we need a set of values that make it unique.
43 * Process Identifier, Object IUnknown ptr, IID
45 * Note that the IUnknown_QI(ob,xiid,&ppv) always returns the SAME ppv value!
48 typedef struct _mid2unknown {
53 typedef struct _mid2stub {
59 static mid2stub *stubs = NULL;
60 static int nrofstubs = 0;
62 static mid2unknown *proxies = NULL;
63 static int nrofproxies = 0;
66 MARSHAL_Find_Stub_Server(wine_marshal_id *mid,LPUNKNOWN *punk) {
69 for (i=0;i<nrofstubs;i++) {
70 if (MARSHAL_Compare_Mids_NoInterface(mid,&(stubs[i].mid))) {
71 *punk = stubs[i].pUnkServer;
72 IUnknown_AddRef((*punk));
80 MARSHAL_Find_Stub_Buffer(wine_marshal_id *mid,IRpcStubBuffer **stub) {
83 for (i=0;i<nrofstubs;i++) {
84 if (MARSHAL_Compare_Mids(mid,&(stubs[i].mid))) {
85 *stub = stubs[i].stub;
86 IUnknown_AddRef((*stub));
94 MARSHAL_Find_Stub(wine_marshal_id *mid,LPUNKNOWN *pUnk) {
97 for (i=0;i<nrofstubs;i++) {
98 if (MARSHAL_Compare_Mids(mid,&(stubs[i].mid))) {
99 *pUnk = stubs[i].pUnkServer;
100 IUnknown_AddRef((*pUnk));
108 MARSHAL_Register_Stub(wine_marshal_id *mid,LPUNKNOWN pUnk,IRpcStubBuffer *stub) {
110 if (!MARSHAL_Find_Stub(mid,&xPunk)) {
111 FIXME("Already have entry for (%lx/%s)!\n",mid->objectid,debugstr_guid(&(mid->iid)));
115 stubs=HeapReAlloc(GetProcessHeap(),0,stubs,sizeof(stubs[0])*(nrofstubs+1));
117 stubs=HeapAlloc(GetProcessHeap(),0,sizeof(stubs[0]));
118 if (!stubs) return E_OUTOFMEMORY;
119 stubs[nrofstubs].stub = stub;
120 stubs[nrofstubs].pUnkServer = pUnk;
121 memcpy(&(stubs[nrofstubs].mid),mid,sizeof(*mid));
127 MARSHAL_Find_Proxy(wine_marshal_id *mid,LPUNKNOWN *punk) {
130 for (i=0;i<nrofproxies;i++)
131 if (MARSHAL_Compare_Mids(mid,&(proxies[i].mid))) {
132 *punk = proxies[i].pUnk;
133 IUnknown_AddRef((*punk));
140 MARSHAL_Find_Proxy_Object(wine_marshal_id *mid,LPUNKNOWN *punk) {
143 for (i=0;i<nrofproxies;i++)
144 if (MARSHAL_Compare_Mids_NoInterface(mid,&(proxies[i].mid))) {
145 *punk = proxies[i].pUnk;
146 IUnknown_AddRef((*punk));
153 MARSHAL_Register_Proxy(wine_marshal_id *mid,LPUNKNOWN punk) {
156 for (i=0;i<nrofproxies;i++) {
157 if (MARSHAL_Compare_Mids(mid,&(proxies[i].mid))) {
158 ERR("Already have mid?\n");
163 proxies = HeapReAlloc(GetProcessHeap(),0,proxies,sizeof(proxies[0])*(nrofproxies+1));
165 proxies = HeapAlloc(GetProcessHeap(),0,sizeof(proxies[0]));
166 memcpy(&(proxies[nrofproxies].mid),mid,sizeof(*mid));
167 proxies[nrofproxies].pUnk = punk;
169 IUnknown_AddRef(punk);
173 /********************** StdMarshal implementation ****************************/
174 typedef struct _StdMarshalImpl {
175 ICOM_VTABLE(IMarshal) *lpvtbl;
180 LPVOID pvDestContext;
185 StdMarshalImpl_QueryInterface(LPMARSHAL iface,REFIID riid,LPVOID *ppv) {
187 if (IsEqualIID(&IID_IUnknown,riid) || IsEqualIID(&IID_IMarshal,riid)) {
189 IUnknown_AddRef(iface);
192 FIXME("No interface for %s.\n",debugstr_guid(riid));
193 return E_NOINTERFACE;
197 StdMarshalImpl_AddRef(LPMARSHAL iface) {
198 ICOM_THIS(StdMarshalImpl,iface);
204 StdMarshalImpl_Release(LPMARSHAL iface) {
205 ICOM_THIS(StdMarshalImpl,iface);
210 HeapFree(GetProcessHeap(),0,This);
215 StdMarshalImpl_GetUnmarshalClass(
216 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
217 void* pvDestContext, DWORD mshlflags, CLSID* pCid
219 memcpy(pCid,&CLSID_DfMarshal,sizeof(CLSID_DfMarshal));
224 StdMarshalImpl_GetMarshalSizeMax(
225 LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
226 void* pvDestContext, DWORD mshlflags, DWORD* pSize
228 *pSize = sizeof(wine_marshal_id)+sizeof(wine_marshal_data);
233 StdMarshalImpl_MarshalInterface(
234 LPMARSHAL iface, IStream *pStm,REFIID riid, void* pv, DWORD dwDestContext,
235 void* pvDestContext, DWORD mshlflags
238 wine_marshal_data md;
242 IRpcStubBuffer *stub;
243 IPSFactoryBuffer *psfacbuf;
245 TRACE("(...,%s,...)\n",debugstr_guid(riid));
246 IUnknown_QueryInterface((LPUNKNOWN)pv,&IID_IUnknown,(LPVOID*)&pUnk);
247 mid.processid = GetCurrentProcessId();
248 mid.objectid = (DWORD)pUnk; /* FIXME */
249 IUnknown_Release(pUnk);
250 memcpy(&mid.iid,riid,sizeof(mid.iid));
251 md.dwDestContext = dwDestContext;
252 md.mshlflags = mshlflags;
253 hres = IStream_Write(pStm,&mid,sizeof(mid),&res);
254 if (hres) return hres;
255 hres = IStream_Write(pStm,&md,sizeof(md),&res);
256 if (hres) return hres;
258 if (SUCCEEDED(MARSHAL_Find_Stub(&mid,&pUnk))) {
259 IUnknown_Release(pUnk);
262 hres = get_facbuf_for_iid(riid,&psfacbuf);
263 if (hres) return hres;
264 hres = IPSFactoryBuffer_CreateStub(psfacbuf,riid,pv,&stub);
265 IPSFactoryBuffer_Release(psfacbuf);
267 FIXME("Failed to create a stub for %s\n",debugstr_guid(riid));
270 IUnknown_QueryInterface((LPUNKNOWN)pv,riid,(LPVOID*)&pUnk);
271 MARSHAL_Register_Stub(&mid,pUnk,stub);
272 IUnknown_Release(pUnk);
277 StdMarshalImpl_UnmarshalInterface(
278 LPMARSHAL iface, IStream *pStm, REFIID riid, void **ppv
281 wine_marshal_data md;
284 IPSFactoryBuffer *psfacbuf;
285 IRpcProxyBuffer *rpcproxy;
286 IRpcChannelBuffer *chanbuf;
288 TRACE("(...,%s,....)\n",debugstr_guid(riid));
289 hres = IStream_Read(pStm,&mid,sizeof(mid),&res);
290 if (hres) return hres;
291 hres = IStream_Read(pStm,&md,sizeof(md),&res);
292 if (hres) return hres;
293 if (SUCCEEDED(MARSHAL_Find_Stub(&mid,(LPUNKNOWN*)ppv))) {
294 FIXME("Calling back to ourselves for %s!\n",debugstr_guid(riid));
297 hres = get_facbuf_for_iid(riid,&psfacbuf);
298 if (hres) return hres;
299 hres = IPSFactoryBuffer_CreateProxy(psfacbuf,NULL,riid,&rpcproxy,ppv);
301 FIXME("Failed to create a proxy for %s\n",debugstr_guid(riid));
304 hres = PIPE_GetNewPipeBuf(&mid,&chanbuf);
306 FIXME("Failed to get an rpc channel buffer for %s\n",debugstr_guid(riid));
307 IRpcProxyBuffer_Connect(rpcproxy,chanbuf);
308 IRpcProxyBuffer_Release(rpcproxy); /* no need */
309 IPSFactoryBuffer_Release(psfacbuf);
314 StdMarshalImpl_ReleaseMarshalData(LPMARSHAL iface, IStream *pStm) {
315 FIXME("(), stub!\n");
320 StdMarshalImpl_DisconnectObject(LPMARSHAL iface, DWORD dwReserved) {
321 FIXME("(), stub!\n");
325 ICOM_VTABLE(IMarshal) stdmvtbl = {
326 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
327 StdMarshalImpl_QueryInterface,
328 StdMarshalImpl_AddRef,
329 StdMarshalImpl_Release,
330 StdMarshalImpl_GetUnmarshalClass,
331 StdMarshalImpl_GetMarshalSizeMax,
332 StdMarshalImpl_MarshalInterface,
333 StdMarshalImpl_UnmarshalInterface,
334 StdMarshalImpl_ReleaseMarshalData,
335 StdMarshalImpl_DisconnectObject
338 /***********************************************************************
339 * CoGetStandardMarshal [OLE32.23]
341 * When the COM library in the client process receives a marshaled
342 * interface pointer, it looks for a CLSID to be used in creating a proxy
343 * for the purposes of unmarshaling the packet. If the packet does not
344 * contain a CLSID for the proxy, COM calls CoGetStandardMarshal, passing a
346 * This function creates a standard proxy in the client process and returns
347 * a pointer to that proxy's implementation of IMarshal.
348 * COM uses this pointer to call CoUnmarshalInterface to retrieve the pointer
349 * to the requested interface.
352 CoGetStandardMarshal(
353 REFIID riid,IUnknown *pUnk,DWORD dwDestContext,LPVOID pvDestContext,
354 DWORD mshlflags, LPMARSHAL *pMarshal
359 FIXME("(%s,NULL,%lx,%p,%lx,%p), unimplemented yet.\n",
360 debugstr_guid(riid),dwDestContext,pvDestContext,mshlflags,pMarshal
364 TRACE("(%s,%p,%lx,%p,%lx,%p)\n",
365 debugstr_guid(riid),pUnk,dwDestContext,pvDestContext,mshlflags,pMarshal
367 dm = (StdMarshalImpl*) *pMarshal = HeapAlloc(GetProcessHeap(),0,sizeof(StdMarshalImpl));
368 if (!dm) return E_FAIL;
369 dm->lpvtbl = &stdmvtbl;
372 memcpy(&dm->iid,riid,sizeof(dm->iid));
373 dm->dwDestContext = dwDestContext;
374 dm->pvDestContext = pvDestContext;
375 dm->mshlflags = mshlflags;
379 /* Helper function for getting Marshaler */
380 static HRESULT WINAPI
381 _GetMarshaller(REFIID riid, IUnknown *pUnk,DWORD dwDestContext,
382 void *pvDestContext, DWORD mshlFlags, LPMARSHAL *pMarshal
388 hres = IUnknown_QueryInterface(pUnk,&IID_IMarshal,(LPVOID*)pMarshal);
390 hres = CoGetStandardMarshal(riid,pUnk,dwDestContext,pvDestContext,mshlFlags,pMarshal);
394 /***********************************************************************
395 * CoGetMarshalSizeMax [OLE32.21]
398 CoGetMarshalSizeMax(ULONG *pulSize, REFIID riid, IUnknown *pUnk,
399 DWORD dwDestContext, void *pvDestContext, DWORD mshlFlags
404 hres = _GetMarshaller(riid,pUnk,dwDestContext,pvDestContext,mshlFlags,&pMarshal);
407 hres = IMarshal_GetMarshalSizeMax(pMarshal,riid,pUnk,dwDestContext,pvDestContext,mshlFlags,pulSize);
408 *pulSize += sizeof(wine_marshal_id)+sizeof(wine_marshal_data)+sizeof(CLSID);
409 IMarshal_Release(pMarshal);
414 /***********************************************************************
415 * CoMarshalInterface [OLE32.34]
418 CoMarshalInterface( IStream *pStm, REFIID riid, IUnknown *pUnk,
419 DWORD dwDestContext, void *pvDestContext, DWORD mshlflags
426 wine_marshal_data md;
430 TRACE("(%p, %s, %p, %lx, %p, %lx)\n",
431 pStm,debugstr_guid(riid),pUnk,dwDestContext,pvDestContext,mshlflags
433 STUBMGR_Start(); /* Just to be sure we have one running. */
434 mid.processid = GetCurrentProcessId();
435 IUnknown_QueryInterface(pUnk,&IID_IUnknown,(LPVOID*)&pUnknown);
436 mid.objectid = (DWORD)pUnknown;
437 IUnknown_Release(pUnknown);
438 memcpy(&mid.iid,riid,sizeof(mid.iid));
439 md.dwDestContext = dwDestContext;
440 md.mshlflags = mshlflags;
441 hres = IStream_Write(pStm,&mid,sizeof(mid),&res);
442 if (hres) return hres;
443 hres = IStream_Write(pStm,&md,sizeof(md),&res);
444 if (hres) return hres;
445 hres = _GetMarshaller(riid,pUnk,dwDestContext,pvDestContext,mshlflags,&pMarshal);
447 FIXME("Failed to get marshaller, %lx?\n",hres);
450 hres = IMarshal_GetUnmarshalClass(pMarshal,riid,pUnk,dwDestContext,pvDestContext,mshlflags,&xclsid);
452 FIXME("IMarshal:GetUnmarshalClass failed, %lx\n",hres);
453 goto release_marshal;
455 hres = IStream_Write(pStm,&xclsid,sizeof(xclsid),&writeres);
457 FIXME("Stream write failed, %lx\n",hres);
458 goto release_marshal;
460 hres = IMarshal_MarshalInterface(pMarshal,pStm,riid,pUnk,dwDestContext,pvDestContext,mshlflags);
462 FIXME("Failed to Marshal the interface, %lx?\n",hres);
463 goto release_marshal;
466 IMarshal_Release(pMarshal);
471 /***********************************************************************
472 * CoUnmarshalInterface [OLE32.50]
475 CoUnmarshalInterface(IStream *pStm, REFIID riid, LPVOID *ppv) {
478 wine_marshal_data md;
484 TRACE("(%p,%s,%p)\n",pStm,debugstr_guid(riid),ppv);
486 hres = IStream_Read(pStm,&mid,sizeof(mid),&res);
488 FIXME("Stream read 1 failed, %lx, (%ld of %d)\n",hres,res,sizeof(mid));
491 hres = IStream_Read(pStm,&md,sizeof(md),&res);
493 FIXME("Stream read 2 failed, %lx, (%ld of %d)\n",hres,res,sizeof(md));
496 hres = IStream_Read(pStm,&xclsid,sizeof(xclsid),&res);
498 FIXME("Stream read 3 failed, %lx, (%ld of %d)\n",hres,res,sizeof(xclsid));
501 hres=CoCreateInstance(&xclsid,NULL,CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER | CLSCTX_LOCAL_SERVER,&IID_IMarshal,(void**)&pUnk);
503 FIXME("Failed to create instance of unmarshaller %s.\n",debugstr_guid(&xclsid));
506 hres = _GetMarshaller(riid,pUnk,md.dwDestContext,NULL,md.mshlflags,&pMarshal);
508 FIXME("Failed to get unmarshaller, %lx?\n",hres);
511 hres = IMarshal_UnmarshalInterface(pMarshal,pStm,riid,ppv);
513 FIXME("Failed to Unmarshal the interface, %lx?\n",hres);
514 goto release_marshal;
517 IMarshal_Release(pMarshal);
521 /***********************************************************************
522 * CoMarshalInterThreadInterfaceInStream [OLE32.33]
524 * Marshal interfaces across threads. We don't have a thread distinction,
525 * meaning most interfaces just work across different threads, the RPC
529 CoMarshalInterThreadInterfaceInStream(
530 REFIID riid, LPUNKNOWN pUnk, LPSTREAM * ppStm
534 LARGE_INTEGER seekto;
537 TRACE("(,%s,)\n",debugstr_guid(riid));
538 hres = CreateStreamOnHGlobal(0, TRUE, ppStm);
539 if (hres) return hres;
540 /* CoMarshalInterface(...); */
541 hres = IStream_Write(*ppStm,&pUnk,sizeof(LPUNKNOWN),&res);
542 if (hres) return hres;
543 memset(&seekto,0,sizeof(seekto));
544 IStream_Seek(*ppStm,seekto,SEEK_SET,&xpos);
548 /***********************************************************************
549 * CoGetInterfaceAndReleaseStream [OLE32.19]
552 CoGetInterfaceAndReleaseStream(LPSTREAM pStm,REFIID riid, LPVOID *ppv) {
557 TRACE("(,%s,)\n",debugstr_guid(riid));
558 /* CoUnmarshalInterface(...); */
559 hres = IStream_Read(pStm,&pUnk,sizeof(LPUNKNOWN),&res);
560 if (hres) return hres;
561 IStream_Release(pStm);
562 return IUnknown_QueryInterface(pUnk,riid,ppv);
565 static HRESULT WINAPI
566 SMCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid, LPVOID *ppv) {
568 if (IsEqualIID(riid,&IID_IUnknown) || IsEqualIID(riid,&IID_IClassFactory)) {
569 *ppv = (LPVOID)iface;
572 return E_NOINTERFACE;
574 static ULONG WINAPI SMCF_AddRef(LPCLASSFACTORY iface) { return 2; }
575 static ULONG WINAPI SMCF_Release(LPCLASSFACTORY iface) { return 1; }
577 static HRESULT WINAPI
579 LPCLASSFACTORY iface, LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv
581 if (IsEqualIID(riid,&IID_IMarshal)) {
583 dm=(StdMarshalImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(StdMarshalImpl));
586 dm->lpvtbl = &stdmvtbl;
591 FIXME("(%s), not supported.\n",debugstr_guid(riid));
592 return E_NOINTERFACE;
595 static HRESULT WINAPI
596 SMCF_LockServer(LPCLASSFACTORY iface, BOOL fLock) {
597 FIXME("(%d), stub!\n",fLock);
601 static ICOM_VTABLE(IClassFactory) dfmarshalcfvtbl = {
602 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
609 static ICOM_VTABLE(IClassFactory) *pdfmarshalcfvtbl = &dfmarshalcfvtbl;
612 MARSHAL_GetStandardMarshalCF(LPVOID *ppv) {
613 *ppv = &pdfmarshalcfvtbl;