4 * Copyright 2002,2005 Marcus Meissner
6 * The olerelay debug channel allows you to see calls marshalled by
7 * the typelib marshaller. It is not a generic COM relaying system.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "wine/port.h"
35 #define NONAMELESSUNION
36 #define NONAMELESSSTRUCT
46 #include "propidl.h" /* for LPSAFEARRAY_User* functions */
49 #include "wine/debug.h"
50 #include "wine/exception.h"
52 static const WCHAR IDispatchW[] = { 'I','D','i','s','p','a','t','c','h',0};
54 WINE_DEFAULT_DEBUG_CHANNEL(ole);
55 WINE_DECLARE_DEBUG_CHANNEL(olerelay);
57 static HRESULT TMarshalDispatchChannel_Create(
58 IRpcChannelBuffer *pDelegateChannel, REFIID tmarshal_riid,
59 IRpcChannelBuffer **ppChannel);
61 typedef struct _marshal_state {
67 /* used in the olerelay code to avoid having the L"" stuff added by debugstr_w */
68 static char *relaystr(WCHAR *in) {
69 char *tmp = (char *)debugstr_w(in);
71 tmp[strlen(tmp)-1] = '\0';
76 xbuf_resize(marshal_state *buf, DWORD newsize)
78 if(buf->size >= newsize)
83 buf->base = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buf->base, newsize);
89 buf->base = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, newsize);
98 xbuf_add(marshal_state *buf, const BYTE *stuff, DWORD size)
102 if(buf->size - buf->curoff < size)
104 hr = xbuf_resize(buf, buf->size + size + 100);
105 if(FAILED(hr)) return hr;
107 memcpy(buf->base+buf->curoff,stuff,size);
113 xbuf_get(marshal_state *buf, LPBYTE stuff, DWORD size) {
114 if (buf->size < buf->curoff+size) return E_FAIL;
115 memcpy(stuff,buf->base+buf->curoff,size);
121 xbuf_skip(marshal_state *buf, DWORD size) {
122 if (buf->size < buf->curoff+size) return E_FAIL;
128 _unmarshal_interface(marshal_state *buf, REFIID riid, LPUNKNOWN *pUnk) {
130 ULARGE_INTEGER newpos;
131 LARGE_INTEGER seekto;
136 TRACE("...%s...\n",debugstr_guid(riid));
139 hres = xbuf_get(buf,(LPBYTE)&xsize,sizeof(xsize));
141 ERR("xbuf_get failed\n");
145 if (xsize == 0) return S_OK;
147 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
149 ERR("Stream create failed %x\n",hres);
153 hres = IStream_Write(pStm,buf->base+buf->curoff,xsize,&res);
155 ERR("stream write %x\n",hres);
159 memset(&seekto,0,sizeof(seekto));
160 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
162 ERR("Failed Seek %x\n",hres);
166 hres = CoUnmarshalInterface(pStm,riid,(LPVOID*)pUnk);
168 ERR("Unmarshalling interface %s failed with %x\n",debugstr_guid(riid),hres);
172 IStream_Release(pStm);
173 return xbuf_skip(buf,xsize);
177 _marshal_interface(marshal_state *buf, REFIID riid, LPUNKNOWN pUnk) {
178 LPBYTE tempbuf = NULL;
179 IStream *pStm = NULL;
181 ULARGE_INTEGER newpos;
182 LARGE_INTEGER seekto;
188 /* this is valid, if for instance we serialize
189 * a VT_DISPATCH with NULL ptr which apparently
190 * can happen. S_OK to make sure we continue
193 WARN("pUnk is NULL\n");
195 return xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
200 TRACE("...%s...\n",debugstr_guid(riid));
202 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
204 ERR("Stream create failed %x\n",hres);
208 hres = CoMarshalInterface(pStm,riid,pUnk,0,NULL,0);
210 ERR("Marshalling interface %s failed with %x\n", debugstr_guid(riid), hres);
214 hres = IStream_Stat(pStm,&ststg,STATFLAG_NONAME);
216 ERR("Stream stat failed\n");
220 tempbuf = HeapAlloc(GetProcessHeap(), 0, ststg.cbSize.u.LowPart);
221 memset(&seekto,0,sizeof(seekto));
222 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
224 ERR("Failed Seek %x\n",hres);
228 hres = IStream_Read(pStm,tempbuf,ststg.cbSize.u.LowPart,&res);
230 ERR("Failed Read %x\n",hres);
234 xsize = ststg.cbSize.u.LowPart;
235 xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
236 hres = xbuf_add(buf,tempbuf,ststg.cbSize.u.LowPart);
238 HeapFree(GetProcessHeap(),0,tempbuf);
239 IStream_Release(pStm);
245 xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
246 if (pStm) IUnknown_Release(pStm);
247 HeapFree(GetProcessHeap(), 0, tempbuf);
251 /********************* OLE Proxy/Stub Factory ********************************/
252 static HRESULT WINAPI
253 PSFacBuf_QueryInterface(LPPSFACTORYBUFFER iface, REFIID iid, LPVOID *ppv) {
254 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)||IsEqualIID(iid,&IID_IUnknown)) {
256 /* No ref counting, static class */
259 FIXME("(%s) unknown IID?\n",debugstr_guid(iid));
260 return E_NOINTERFACE;
263 static ULONG WINAPI PSFacBuf_AddRef(LPPSFACTORYBUFFER iface) { return 2; }
264 static ULONG WINAPI PSFacBuf_Release(LPPSFACTORYBUFFER iface) { return 1; }
267 _get_typeinfo_for_iid(REFIID riid, ITypeInfo**ti) {
270 char tlguid[200],typelibkey[300],interfacekey[300],ver[100];
273 DWORD tlguidlen, verlen, type;
277 sprintf( interfacekey, "Interface\\{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\Typelib",
278 riid->Data1, riid->Data2, riid->Data3,
279 riid->Data4[0], riid->Data4[1], riid->Data4[2], riid->Data4[3],
280 riid->Data4[4], riid->Data4[5], riid->Data4[6], riid->Data4[7]
283 if (RegOpenKeyA(HKEY_CLASSES_ROOT,interfacekey,&ikey)) {
284 ERR("No %s key found.\n",interfacekey);
287 tlguidlen = sizeof(tlguid);
288 if (RegQueryValueExA(ikey,NULL,NULL,&type,(LPBYTE)tlguid,&tlguidlen)) {
289 ERR("Getting typelib guid failed.\n");
293 verlen = sizeof(ver);
294 if (RegQueryValueExA(ikey,"Version",NULL,&type,(LPBYTE)ver,&verlen)) {
295 ERR("Could not get version value?\n");
300 sprintf(typelibkey,"Typelib\\%s\\%s\\0\\win%u",tlguid,ver,(sizeof(void*) == 8) ? 64 : 32);
301 tlfnlen = sizeof(tlfn);
302 if (RegQueryValueA(HKEY_CLASSES_ROOT,typelibkey,tlfn,&tlfnlen)) {
303 ERR("Could not get typelib fn?\n");
306 MultiByteToWideChar(CP_ACP, 0, tlfn, -1, tlfnW, sizeof(tlfnW) / sizeof(tlfnW[0]));
307 hres = LoadTypeLib(tlfnW,&tl);
309 ERR("Failed to load typelib for %s, but it should be there.\n",debugstr_guid(riid));
312 hres = ITypeLib_GetTypeInfoOfGuid(tl,riid,ti);
314 ERR("typelib does not contain info for %s?\n",debugstr_guid(riid));
315 ITypeLib_Release(tl);
318 ITypeLib_Release(tl);
323 * Determine the number of functions including all inherited functions.
324 * Note for non-dual dispinterfaces we simply return the size of IDispatch.
326 static HRESULT num_of_funcs(ITypeInfo *tinfo, unsigned int *num)
333 hres = ITypeInfo_GetTypeAttr(tinfo, &attr);
335 ERR("GetTypeAttr failed with %x\n",hres);
339 if(attr->typekind == TKIND_DISPATCH && (attr->wTypeFlags & TYPEFLAG_FDUAL))
342 hres = ITypeInfo_GetRefTypeOfImplType(tinfo, -1, &href);
345 ERR("Unable to get interface href from dual dispinterface\n");
348 hres = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
351 ERR("Unable to get interface from dual dispinterface\n");
354 hres = num_of_funcs(tinfo2, num);
355 ITypeInfo_Release(tinfo2);
359 *num = attr->cbSizeVft / 4;
363 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
369 #include "pshpack1.h"
371 typedef struct _TMAsmProxy {
386 # warning You need to implement stubless proxies for your architecture
387 typedef struct _TMAsmProxy {
391 typedef struct _TMProxyImpl {
393 IRpcProxyBuffer IRpcProxyBuffer_iface;
396 TMAsmProxy *asmstubs;
398 IRpcChannelBuffer* chanbuf;
400 CRITICAL_SECTION crit;
401 IUnknown *outerunknown;
403 IRpcProxyBuffer *dispatch_proxy;
406 static inline TMProxyImpl *impl_from_IRpcProxyBuffer( IRpcProxyBuffer *iface )
408 return CONTAINING_RECORD(iface, TMProxyImpl, IRpcProxyBuffer_iface);
411 static HRESULT WINAPI
412 TMProxyImpl_QueryInterface(LPRPCPROXYBUFFER iface, REFIID riid, LPVOID *ppv)
415 if (IsEqualIID(riid,&IID_IUnknown)||IsEqualIID(riid,&IID_IRpcProxyBuffer)) {
417 IRpcProxyBuffer_AddRef(iface);
420 FIXME("no interface for %s\n",debugstr_guid(riid));
421 return E_NOINTERFACE;
425 TMProxyImpl_AddRef(LPRPCPROXYBUFFER iface)
427 TMProxyImpl *This = impl_from_IRpcProxyBuffer( iface );
428 ULONG refCount = InterlockedIncrement(&This->ref);
430 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
436 TMProxyImpl_Release(LPRPCPROXYBUFFER iface)
438 TMProxyImpl *This = impl_from_IRpcProxyBuffer( iface );
439 ULONG refCount = InterlockedDecrement(&This->ref);
441 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
445 if (This->dispatch_proxy) IRpcProxyBuffer_Release(This->dispatch_proxy);
446 This->crit.DebugInfo->Spare[0] = 0;
447 DeleteCriticalSection(&This->crit);
448 if (This->chanbuf) IRpcChannelBuffer_Release(This->chanbuf);
449 VirtualFree(This->asmstubs, 0, MEM_RELEASE);
450 HeapFree(GetProcessHeap(), 0, This->lpvtbl);
451 ITypeInfo_Release(This->tinfo);
457 static HRESULT WINAPI
459 LPRPCPROXYBUFFER iface,IRpcChannelBuffer* pRpcChannelBuffer)
461 TMProxyImpl *This = impl_from_IRpcProxyBuffer( iface );
463 TRACE("(%p)\n", pRpcChannelBuffer);
465 EnterCriticalSection(&This->crit);
467 IRpcChannelBuffer_AddRef(pRpcChannelBuffer);
468 This->chanbuf = pRpcChannelBuffer;
470 LeaveCriticalSection(&This->crit);
472 if (This->dispatch_proxy)
474 IRpcChannelBuffer *pDelegateChannel;
475 HRESULT hr = TMarshalDispatchChannel_Create(pRpcChannelBuffer, &This->iid, &pDelegateChannel);
478 hr = IRpcProxyBuffer_Connect(This->dispatch_proxy, pDelegateChannel);
479 IRpcChannelBuffer_Release(pDelegateChannel);
487 TMProxyImpl_Disconnect(LPRPCPROXYBUFFER iface)
489 TMProxyImpl *This = impl_from_IRpcProxyBuffer( iface );
493 EnterCriticalSection(&This->crit);
495 IRpcChannelBuffer_Release(This->chanbuf);
496 This->chanbuf = NULL;
498 LeaveCriticalSection(&This->crit);
500 if (This->dispatch_proxy)
501 IRpcProxyBuffer_Disconnect(This->dispatch_proxy);
505 static const IRpcProxyBufferVtbl tmproxyvtable = {
506 TMProxyImpl_QueryInterface,
510 TMProxyImpl_Disconnect
513 /* how much space do we use on stack in DWORD steps. */
515 _argsize(TYPEDESC *tdesc, ITypeInfo *tinfo) {
519 return 8/sizeof(DWORD);
521 return sizeof(double)/sizeof(DWORD);
523 return sizeof(CY)/sizeof(DWORD);
525 return sizeof(DATE)/sizeof(DWORD);
527 return (sizeof(DECIMAL)+3)/sizeof(DWORD);
529 return (sizeof(VARIANT)+3)/sizeof(DWORD);
537 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
539 return 0; /* should fail critically in serialize_param */
540 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
541 ret = (tattr->cbSizeInstance+3)/sizeof(DWORD);
542 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
543 ITypeInfo_Release(tinfo2);
551 /* how much space do we use on the heap (in bytes) */
553 _xsize(const TYPEDESC *td, ITypeInfo *tinfo) {
559 /* FIXME: VT_BOOL should return 2? */
561 return sizeof(VARIANT)+3; /* FIXME: why the +3? */
564 const ARRAYDESC *adesc = td->u.lpadesc;
566 for (i=0;i<adesc->cDims;i++)
567 arrsize *= adesc->rgbounds[i].cElements;
568 return arrsize*_xsize(&adesc->tdescElem, tinfo);
587 hres = ITypeInfo_GetRefTypeInfo(tinfo,td->u.hreftype,&tinfo2);
590 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
591 ret = tattr->cbSizeInstance;
592 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
593 ITypeInfo_Release(tinfo2);
614 TRACE("(tdesc.vt %s)\n",debugstr_vt(tdesc->vt));
617 if ((vartype & 0xf000) == VT_ARRAY)
618 vartype = VT_SAFEARRAY;
627 if (debugout) TRACE_(olerelay)("%x%x\n",arg[0],arg[1]);
629 hres = xbuf_add(buf,(LPBYTE)arg,8);
639 if (debugout) TRACE_(olerelay)("%x\n",*arg);
641 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
646 if (debugout) TRACE_(olerelay)("%04x\n",*arg & 0xffff);
648 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
653 if (debugout) TRACE_(olerelay)("%02x\n",*arg & 0xff);
655 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
658 if (debugout) TRACE_(olerelay)("Vt(%s%s)(",debugstr_vt(V_VT((VARIANT *)arg)),debugstr_vf(V_VT((VARIANT *)arg)));
661 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
662 ULONG size = VARIANT_UserSize(&flags, buf->curoff, (VARIANT *)arg);
663 xbuf_resize(buf, size);
664 VARIANT_UserMarshal(&flags, buf->base + buf->curoff, (VARIANT *)arg);
669 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
670 VARIANT_UserFree(&flags, (VARIANT *)arg);
677 TRACE_(olerelay)("%s",relaystr((WCHAR*)*arg));
679 TRACE_(olerelay)("<bstr NULL>");
683 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
684 ULONG size = BSTR_UserSize(&flags, buf->curoff, (BSTR *)arg);
685 xbuf_resize(buf, size);
686 BSTR_UserMarshal(&flags, buf->base + buf->curoff, (BSTR *)arg);
691 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
692 BSTR_UserFree(&flags, (BSTR *)arg);
698 BOOL derefhere = TRUE;
700 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
704 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
706 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
709 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
710 switch (tattr->typekind) {
712 if (tattr->tdescAlias.vt == VT_USERDEFINED)
714 DWORD href = tattr->tdescAlias.u.hreftype;
715 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
716 ITypeInfo_Release(tinfo2);
717 hres = ITypeInfo_GetRefTypeInfo(tinfo,href,&tinfo2);
719 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
722 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
723 derefhere = (tattr->typekind != TKIND_DISPATCH && tattr->typekind != TKIND_INTERFACE);
726 case TKIND_ENUM: /* confirmed */
727 case TKIND_RECORD: /* FIXME: mostly untested */
729 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
730 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
734 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
738 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
739 ITypeInfo_Release(tinfo2);
742 if (debugout) TRACE_(olerelay)("*");
743 /* Write always, so the other side knows when it gets a NULL pointer.
745 cookie = *arg ? 0x42424242 : 0;
746 hres = xbuf_add(buf,(LPBYTE)&cookie,sizeof(cookie));
750 if (debugout) TRACE_(olerelay)("NULL");
753 hres = serialize_param(tinfo,writeit,debugout,dealloc,tdesc->u.lptdesc,(DWORD*)*arg,buf);
754 if (derefhere && dealloc) HeapFree(GetProcessHeap(),0,(LPVOID)*arg);
758 if (debugout) TRACE_(olerelay)("unk(0x%x)",*arg);
760 hres = _marshal_interface(buf,&IID_IUnknown,(LPUNKNOWN)*arg);
761 if (dealloc && *(IUnknown **)arg)
762 IUnknown_Release((LPUNKNOWN)*arg);
765 if (debugout) TRACE_(olerelay)("idisp(0x%x)",*arg);
767 hres = _marshal_interface(buf,&IID_IDispatch,(LPUNKNOWN)*arg);
768 if (dealloc && *(IUnknown **)arg)
769 IUnknown_Release((LPUNKNOWN)*arg);
772 if (debugout) TRACE_(olerelay)("<void>");
774 case VT_USERDEFINED: {
778 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
780 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
783 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
784 switch (tattr->typekind) {
786 case TKIND_INTERFACE:
788 hres=_marshal_interface(buf,&(tattr->guid),(LPUNKNOWN)arg);
790 IUnknown_Release((LPUNKNOWN)arg);
794 if (debugout) TRACE_(olerelay)("{");
795 for (i=0;i<tattr->cVars;i++) {
800 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
802 ERR("Could not get vardesc of %d\n",i);
805 elem2 = &vdesc->elemdescVar;
806 tdesc2 = &elem2->tdesc;
807 hres = serialize_param(
813 (DWORD*)(((LPBYTE)arg)+vdesc->u.oInst),
816 ITypeInfo_ReleaseVarDesc(tinfo2, vdesc);
819 if (debugout && (i<(tattr->cVars-1)))
820 TRACE_(olerelay)(",");
822 if (debugout) TRACE_(olerelay)("}");
826 hres = serialize_param(tinfo2,writeit,debugout,dealloc,&tattr->tdescAlias,arg,buf);
830 if (debugout) TRACE_(olerelay)("%x",*arg);
832 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
835 FIXME("Unhandled typekind %d\n",tattr->typekind);
839 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
840 ITypeInfo_Release(tinfo2);
844 ARRAYDESC *adesc = tdesc->u.lpadesc;
847 if (debugout) TRACE_(olerelay)("carr");
848 for (i=0;i<adesc->cDims;i++) {
849 if (debugout) TRACE_(olerelay)("[%d]",adesc->rgbounds[i].cElements);
850 arrsize *= adesc->rgbounds[i].cElements;
852 if (debugout) TRACE_(olerelay)("(vt %s)",debugstr_vt(adesc->tdescElem.vt));
853 if (debugout) TRACE_(olerelay)("[");
854 for (i=0;i<arrsize;i++) {
855 hres = serialize_param(tinfo, writeit, debugout, dealloc, &adesc->tdescElem, (DWORD*)((LPBYTE)(*arg)+i*_xsize(&adesc->tdescElem, tinfo)), buf);
858 if (debugout && (i<arrsize-1)) TRACE_(olerelay)(",");
860 if (debugout) TRACE_(olerelay)("]");
862 HeapFree(GetProcessHeap(), 0, *(void **)arg);
868 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
869 ULONG size = LPSAFEARRAY_UserSize(&flags, buf->curoff, (LPSAFEARRAY *)arg);
870 xbuf_resize(buf, size);
871 LPSAFEARRAY_UserMarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
876 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
877 LPSAFEARRAY_UserFree(&flags, (LPSAFEARRAY *)arg);
882 ERR("Unhandled marshal type %d.\n",tdesc->vt);
900 TRACE("vt %s at %p\n",debugstr_vt(tdesc->vt),arg);
903 if ((vartype & 0xf000) == VT_ARRAY)
904 vartype = VT_SAFEARRAY;
911 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
912 unsigned char *buffer;
913 buffer = VARIANT_UserUnmarshal(&flags, buf->base + buf->curoff, (VARIANT *)arg);
914 buf->curoff = buffer - buf->base;
924 hres = xbuf_get(buf,(LPBYTE)arg,8);
925 if (hres) ERR("Failed to read integer 8 byte\n");
927 if (debugout) TRACE_(olerelay)("%x%x",arg[0],arg[1]);
937 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
938 if (hres) ERR("Failed to read integer 4 byte\n");
940 if (debugout) TRACE_(olerelay)("%x",*arg);
946 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
947 if (hres) ERR("Failed to read integer 4 byte\n");
950 if (debugout) TRACE_(olerelay)("%04x",*arg & 0xffff);
956 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
957 if (hres) ERR("Failed to read integer 4 byte\n");
960 if (debugout) TRACE_(olerelay)("%02x",*arg & 0xff);
965 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
966 unsigned char *buffer;
967 buffer = BSTR_UserUnmarshal(&flags, buf->base + buf->curoff, (BSTR *)arg);
968 buf->curoff = buffer - buf->base;
969 if (debugout) TRACE_(olerelay)("%s",debugstr_w(*(BSTR *)arg));
975 BOOL derefhere = TRUE;
977 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
981 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
983 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
986 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
987 switch (tattr->typekind) {
989 if (tattr->tdescAlias.vt == VT_USERDEFINED)
991 DWORD href = tattr->tdescAlias.u.hreftype;
992 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
993 ITypeInfo_Release(tinfo2);
994 hres = ITypeInfo_GetRefTypeInfo(tinfo,href,&tinfo2);
996 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
999 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1000 derefhere = (tattr->typekind != TKIND_DISPATCH && tattr->typekind != TKIND_INTERFACE);
1003 case TKIND_ENUM: /* confirmed */
1004 case TKIND_RECORD: /* FIXME: mostly untested */
1006 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
1007 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
1011 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
1015 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1016 ITypeInfo_Release(tinfo2);
1018 /* read it in all cases, we need to know if we have
1019 * NULL pointer or not.
1021 hres = xbuf_get(buf,(LPBYTE)&cookie,sizeof(cookie));
1023 ERR("Failed to load pointer cookie.\n");
1026 if (cookie != 0x42424242) {
1027 /* we read a NULL ptr from the remote side */
1028 if (debugout) TRACE_(olerelay)("NULL");
1032 if (debugout) TRACE_(olerelay)("*");
1034 /* Allocate space for the referenced struct */
1036 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,_xsize(tdesc->u.lptdesc, tinfo));
1039 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, (LPDWORD)*arg, buf);
1041 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, arg, buf);
1044 /* FIXME: UNKNOWN is unknown ..., but allocate 4 byte for it */
1046 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
1049 hres = _unmarshal_interface(buf,&IID_IUnknown,(LPUNKNOWN*)arg);
1051 TRACE_(olerelay)("unk(%p)",arg);
1056 hres = _unmarshal_interface(buf,&IID_IDispatch,(LPUNKNOWN*)arg);
1058 TRACE_(olerelay)("idisp(%p)",arg);
1061 if (debugout) TRACE_(olerelay)("<void>");
1063 case VT_USERDEFINED: {
1067 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
1069 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
1072 hres = ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1074 ERR("Could not get typeattr in VT_USERDEFINED.\n");
1076 switch (tattr->typekind) {
1077 case TKIND_DISPATCH:
1078 case TKIND_INTERFACE:
1080 hres = _unmarshal_interface(buf,&(tattr->guid),(LPUNKNOWN*)arg);
1082 case TKIND_RECORD: {
1085 if (debugout) TRACE_(olerelay)("{");
1086 for (i=0;i<tattr->cVars;i++) {
1089 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
1091 ERR("Could not get vardesc of %d\n",i);
1092 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1093 ITypeInfo_Release(tinfo2);
1096 hres = deserialize_param(
1101 &vdesc->elemdescVar.tdesc,
1102 (DWORD*)(((LPBYTE)arg)+vdesc->u.oInst),
1105 ITypeInfo2_ReleaseVarDesc(tinfo2, vdesc);
1106 if (debugout && (i<tattr->cVars-1)) TRACE_(olerelay)(",");
1108 if (debugout) TRACE_(olerelay)("}");
1112 hres = deserialize_param(tinfo2,readit,debugout,alloc,&tattr->tdescAlias,arg,buf);
1116 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
1117 if (hres) ERR("Failed to read enum (4 byte)\n");
1119 if (debugout) TRACE_(olerelay)("%x",*arg);
1122 ERR("Unhandled typekind %d\n",tattr->typekind);
1126 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1129 ERR("failed to stuballoc in TKIND_RECORD.\n");
1130 ITypeInfo_Release(tinfo2);
1134 /* arg is pointing to the start of the array. */
1135 ARRAYDESC *adesc = tdesc->u.lpadesc;
1138 if (adesc->cDims > 1) FIXME("cDims > 1 in VT_CARRAY. Does it work?\n");
1139 for (i=0;i<adesc->cDims;i++)
1140 arrsize *= adesc->rgbounds[i].cElements;
1141 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,_xsize(tdesc->u.lptdesc, tinfo) * arrsize);
1142 for (i=0;i<arrsize;i++)
1149 (DWORD*)((LPBYTE)(*arg)+i*_xsize(&adesc->tdescElem, tinfo)),
1154 case VT_SAFEARRAY: {
1157 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
1158 unsigned char *buffer;
1159 buffer = LPSAFEARRAY_UserUnmarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
1160 buf->curoff = buffer - buf->base;
1165 ERR("No handler for VT type %d!\n",tdesc->vt);
1171 /* Retrieves a function's funcdesc, searching back into inherited interfaces. */
1172 static HRESULT get_funcdesc(ITypeInfo *tinfo, int iMethod, ITypeInfo **tactual, const FUNCDESC **fdesc,
1173 BSTR *iname, BSTR *fname, UINT *num)
1177 UINT inherited_funcs = 0;
1180 if (fname) *fname = NULL;
1181 if (iname) *iname = NULL;
1185 hr = ITypeInfo_GetTypeAttr(tinfo, &attr);
1188 ERR("GetTypeAttr failed with %x\n",hr);
1192 if(attr->typekind == TKIND_DISPATCH)
1194 if(attr->wTypeFlags & TYPEFLAG_FDUAL)
1199 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, -1, &href);
1202 ERR("Cannot get interface href from dual dispinterface\n");
1203 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1206 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
1209 ERR("Cannot get interface from dual dispinterface\n");
1210 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1213 hr = get_funcdesc(tinfo2, iMethod, tactual, fdesc, iname, fname, num);
1214 ITypeInfo_Release(tinfo2);
1215 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1218 ERR("Shouldn't be called with a non-dual dispinterface\n");
1222 impl_types = attr->cImplTypes;
1223 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1225 for (i = 0; i < impl_types; i++)
1228 ITypeInfo *pSubTypeInfo;
1231 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, i, &href);
1232 if (FAILED(hr)) return hr;
1233 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &pSubTypeInfo);
1234 if (FAILED(hr)) return hr;
1236 hr = get_funcdesc(pSubTypeInfo, iMethod, tactual, fdesc, iname, fname, &sub_funcs);
1237 inherited_funcs += sub_funcs;
1238 ITypeInfo_Release(pSubTypeInfo);
1239 if(SUCCEEDED(hr)) return hr;
1241 if(iMethod < inherited_funcs)
1243 ERR("shouldn't be here\n");
1244 return E_INVALIDARG;
1247 for(i = inherited_funcs; i <= iMethod; i++)
1249 hr = ITypeInfoImpl_GetInternalFuncDesc(tinfo, i - inherited_funcs, fdesc);
1257 /* found it. We don't care about num so zero it */
1260 ITypeInfo_AddRef(*tactual);
1261 if (fname) ITypeInfo_GetDocumentation(tinfo,(*fdesc)->memid,fname,NULL,NULL,NULL);
1262 if (iname) ITypeInfo_GetDocumentation(tinfo,-1,iname,NULL,NULL,NULL);
1266 static inline BOOL is_in_elem(const ELEMDESC *elem)
1268 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN || !elem->u.paramdesc.wParamFlags);
1271 static inline BOOL is_out_elem(const ELEMDESC *elem)
1273 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT || !elem->u.paramdesc.wParamFlags);
1277 xCall(LPVOID retptr, int method, TMProxyImpl *tpinfo /*, args */)
1279 DWORD *args = ((DWORD*)&tpinfo)+1, *xargs;
1280 const FUNCDESC *fdesc;
1282 int i, relaydeb = TRACE_ON(olerelay);
1289 DWORD remoteresult = 0;
1291 IRpcChannelBuffer *chanbuf;
1293 EnterCriticalSection(&tpinfo->crit);
1295 hres = get_funcdesc(tpinfo->tinfo,method,&tinfo,&fdesc,&iname,&fname,NULL);
1297 ERR("Did not find typeinfo/funcdesc entry for method %d!\n",method);
1298 LeaveCriticalSection(&tpinfo->crit);
1302 if (!tpinfo->chanbuf)
1304 WARN("Tried to use disconnected proxy\n");
1305 ITypeInfo_Release(tinfo);
1306 LeaveCriticalSection(&tpinfo->crit);
1307 return RPC_E_DISCONNECTED;
1309 chanbuf = tpinfo->chanbuf;
1310 IRpcChannelBuffer_AddRef(chanbuf);
1312 LeaveCriticalSection(&tpinfo->crit);
1315 TRACE_(olerelay)("->");
1317 TRACE_(olerelay)("%s:",relaystr(iname));
1319 TRACE_(olerelay)("%s(%d)",relaystr(fname),method);
1321 TRACE_(olerelay)("%d",method);
1322 TRACE_(olerelay)("(");
1325 SysFreeString(iname);
1326 SysFreeString(fname);
1328 memset(&buf,0,sizeof(buf));
1330 /* normal typelib driven serializing */
1332 /* Need them for hack below */
1333 memset(names,0,sizeof(names));
1334 if (ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames))
1336 if (nrofnames > sizeof(names)/sizeof(names[0]))
1337 ERR("Need more names!\n");
1340 for (i=0;i<fdesc->cParams;i++) {
1341 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1343 if (i) TRACE_(olerelay)(",");
1344 if (i+1<nrofnames && names[i+1])
1345 TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1347 /* No need to marshal other data than FIN and any VT_PTR. */
1348 if (!is_in_elem(elem) && (elem->tdesc.vt != VT_PTR)) {
1349 xargs+=_argsize(&elem->tdesc, tinfo);
1350 if (relaydeb) TRACE_(olerelay)("[out]");
1353 hres = serialize_param(
1364 ERR("Failed to serialize param, hres %x\n",hres);
1367 xargs+=_argsize(&elem->tdesc, tinfo);
1369 if (relaydeb) TRACE_(olerelay)(")");
1371 memset(&msg,0,sizeof(msg));
1372 msg.cbBuffer = buf.curoff;
1373 msg.iMethod = method;
1374 hres = IRpcChannelBuffer_GetBuffer(chanbuf,&msg,&(tpinfo->iid));
1376 ERR("RpcChannelBuffer GetBuffer failed, %x\n",hres);
1379 memcpy(msg.Buffer,buf.base,buf.curoff);
1380 if (relaydeb) TRACE_(olerelay)("\n");
1381 hres = IRpcChannelBuffer_SendReceive(chanbuf,&msg,&status);
1383 ERR("RpcChannelBuffer SendReceive failed, %x\n",hres);
1387 if (relaydeb) TRACE_(olerelay)(" status = %08x (",status);
1389 buf.base = HeapReAlloc(GetProcessHeap(),0,buf.base,msg.cbBuffer);
1391 buf.base = HeapAlloc(GetProcessHeap(),0,msg.cbBuffer);
1392 buf.size = msg.cbBuffer;
1393 memcpy(buf.base,msg.Buffer,buf.size);
1396 /* generic deserializer using typelib description */
1399 for (i=0;i<fdesc->cParams;i++) {
1400 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1403 if (i) TRACE_(olerelay)(",");
1404 if (i+1<nrofnames && names[i+1]) TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1406 /* No need to marshal other data than FOUT and any VT_PTR */
1407 if (!is_out_elem(elem) && (elem->tdesc.vt != VT_PTR)) {
1408 xargs += _argsize(&elem->tdesc, tinfo);
1409 if (relaydeb) TRACE_(olerelay)("[in]");
1412 hres = deserialize_param(
1422 ERR("Failed to unmarshall param, hres %x\n",hres);
1426 xargs += _argsize(&elem->tdesc, tinfo);
1429 hres = xbuf_get(&buf, (LPBYTE)&remoteresult, sizeof(DWORD));
1432 if (relaydeb) TRACE_(olerelay)(") = %08x\n", remoteresult);
1434 hres = remoteresult;
1437 IRpcChannelBuffer_FreeBuffer(chanbuf,&msg);
1438 for (i = 0; i < nrofnames; i++)
1439 SysFreeString(names[i]);
1440 HeapFree(GetProcessHeap(),0,buf.base);
1441 IRpcChannelBuffer_Release(chanbuf);
1442 ITypeInfo_Release(tinfo);
1443 TRACE("-- 0x%08x\n", hres);
1447 static HRESULT WINAPI ProxyIUnknown_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
1449 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1451 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1453 if (proxy->outerunknown)
1454 return IUnknown_QueryInterface(proxy->outerunknown, riid, ppv);
1456 FIXME("No interface\n");
1457 return E_NOINTERFACE;
1460 static ULONG WINAPI ProxyIUnknown_AddRef(IUnknown *iface)
1462 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1466 if (proxy->outerunknown)
1467 return IUnknown_AddRef(proxy->outerunknown);
1469 return 2; /* FIXME */
1472 static ULONG WINAPI ProxyIUnknown_Release(IUnknown *iface)
1474 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1478 if (proxy->outerunknown)
1479 return IUnknown_Release(proxy->outerunknown);
1481 return 1; /* FIXME */
1484 static HRESULT WINAPI ProxyIDispatch_GetTypeInfoCount(LPDISPATCH iface, UINT * pctinfo)
1486 TMProxyImpl *This = (TMProxyImpl *)iface;
1488 TRACE("(%p)\n", pctinfo);
1490 return IDispatch_GetTypeInfoCount(This->dispatch, pctinfo);
1493 static HRESULT WINAPI ProxyIDispatch_GetTypeInfo(LPDISPATCH iface, UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo)
1495 TMProxyImpl *This = (TMProxyImpl *)iface;
1497 TRACE("(%d, %x, %p)\n", iTInfo, lcid, ppTInfo);
1499 return IDispatch_GetTypeInfo(This->dispatch, iTInfo, lcid, ppTInfo);
1502 static HRESULT WINAPI ProxyIDispatch_GetIDsOfNames(LPDISPATCH iface, REFIID riid, LPOLESTR * rgszNames, UINT cNames, LCID lcid, DISPID * rgDispId)
1504 TMProxyImpl *This = (TMProxyImpl *)iface;
1506 TRACE("(%s, %p, %d, 0x%x, %p)\n", debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);
1508 return IDispatch_GetIDsOfNames(This->dispatch, riid, rgszNames,
1509 cNames, lcid, rgDispId);
1512 static HRESULT WINAPI ProxyIDispatch_Invoke(LPDISPATCH iface, DISPID dispIdMember, REFIID riid, LCID lcid,
1513 WORD wFlags, DISPPARAMS * pDispParams, VARIANT * pVarResult,
1514 EXCEPINFO * pExcepInfo, UINT * puArgErr)
1516 TMProxyImpl *This = (TMProxyImpl *)iface;
1518 TRACE("(%d, %s, 0x%x, 0x%x, %p, %p, %p, %p)\n", dispIdMember,
1519 debugstr_guid(riid), lcid, wFlags, pDispParams, pVarResult,
1520 pExcepInfo, puArgErr);
1522 return IDispatch_Invoke(This->dispatch, dispIdMember, riid, lcid,
1523 wFlags, pDispParams, pVarResult, pExcepInfo,
1529 IRpcChannelBuffer IRpcChannelBuffer_iface;
1531 /* the IDispatch-derived interface we are handling */
1533 IRpcChannelBuffer *pDelegateChannel;
1534 } TMarshalDispatchChannel;
1536 static inline TMarshalDispatchChannel *impl_from_IRpcChannelBuffer(IRpcChannelBuffer *iface)
1538 return CONTAINING_RECORD(iface, TMarshalDispatchChannel, IRpcChannelBuffer_iface);
1541 static HRESULT WINAPI TMarshalDispatchChannel_QueryInterface(LPRPCCHANNELBUFFER iface, REFIID riid, LPVOID *ppv)
1544 if (IsEqualIID(riid,&IID_IRpcChannelBuffer) || IsEqualIID(riid,&IID_IUnknown))
1547 IUnknown_AddRef(iface);
1550 return E_NOINTERFACE;
1553 static ULONG WINAPI TMarshalDispatchChannel_AddRef(LPRPCCHANNELBUFFER iface)
1555 TMarshalDispatchChannel *This = impl_from_IRpcChannelBuffer(iface);
1556 return InterlockedIncrement(&This->refs);
1559 static ULONG WINAPI TMarshalDispatchChannel_Release(LPRPCCHANNELBUFFER iface)
1561 TMarshalDispatchChannel *This = impl_from_IRpcChannelBuffer(iface);
1564 ref = InterlockedDecrement(&This->refs);
1568 IRpcChannelBuffer_Release(This->pDelegateChannel);
1569 HeapFree(GetProcessHeap(), 0, This);
1573 static HRESULT WINAPI TMarshalDispatchChannel_GetBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg, REFIID riid)
1575 TMarshalDispatchChannel *This = impl_from_IRpcChannelBuffer(iface);
1576 TRACE("(%p, %s)\n", olemsg, debugstr_guid(riid));
1577 /* Note: we are pretending to invoke a method on the interface identified
1578 * by tmarshal_iid so that we can re-use the IDispatch proxy/stub code
1579 * without the RPC runtime getting confused by not exporting an IDispatch interface */
1580 return IRpcChannelBuffer_GetBuffer(This->pDelegateChannel, olemsg, &This->tmarshal_iid);
1583 static HRESULT WINAPI TMarshalDispatchChannel_SendReceive(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE *olemsg, ULONG *pstatus)
1585 TMarshalDispatchChannel *This = impl_from_IRpcChannelBuffer(iface);
1586 TRACE("(%p, %p)\n", olemsg, pstatus);
1587 return IRpcChannelBuffer_SendReceive(This->pDelegateChannel, olemsg, pstatus);
1590 static HRESULT WINAPI TMarshalDispatchChannel_FreeBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg)
1592 TMarshalDispatchChannel *This = impl_from_IRpcChannelBuffer(iface);
1593 TRACE("(%p)\n", olemsg);
1594 return IRpcChannelBuffer_FreeBuffer(This->pDelegateChannel, olemsg);
1597 static HRESULT WINAPI TMarshalDispatchChannel_GetDestCtx(LPRPCCHANNELBUFFER iface, DWORD* pdwDestContext, void** ppvDestContext)
1599 TMarshalDispatchChannel *This = impl_from_IRpcChannelBuffer(iface);
1600 TRACE("(%p,%p)\n", pdwDestContext, ppvDestContext);
1601 return IRpcChannelBuffer_GetDestCtx(This->pDelegateChannel, pdwDestContext, ppvDestContext);
1604 static HRESULT WINAPI TMarshalDispatchChannel_IsConnected(LPRPCCHANNELBUFFER iface)
1606 TMarshalDispatchChannel *This = impl_from_IRpcChannelBuffer(iface);
1608 return IRpcChannelBuffer_IsConnected(This->pDelegateChannel);
1611 static const IRpcChannelBufferVtbl TMarshalDispatchChannelVtbl =
1613 TMarshalDispatchChannel_QueryInterface,
1614 TMarshalDispatchChannel_AddRef,
1615 TMarshalDispatchChannel_Release,
1616 TMarshalDispatchChannel_GetBuffer,
1617 TMarshalDispatchChannel_SendReceive,
1618 TMarshalDispatchChannel_FreeBuffer,
1619 TMarshalDispatchChannel_GetDestCtx,
1620 TMarshalDispatchChannel_IsConnected
1623 static HRESULT TMarshalDispatchChannel_Create(
1624 IRpcChannelBuffer *pDelegateChannel, REFIID tmarshal_riid,
1625 IRpcChannelBuffer **ppChannel)
1627 TMarshalDispatchChannel *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1629 return E_OUTOFMEMORY;
1631 This->IRpcChannelBuffer_iface.lpVtbl = &TMarshalDispatchChannelVtbl;
1633 IRpcChannelBuffer_AddRef(pDelegateChannel);
1634 This->pDelegateChannel = pDelegateChannel;
1635 This->tmarshal_iid = *tmarshal_riid;
1637 *ppChannel = &This->IRpcChannelBuffer_iface;
1642 static inline HRESULT get_facbuf_for_iid(REFIID riid, IPSFactoryBuffer **facbuf)
1647 if ((hr = CoGetPSClsid(riid, &clsid)))
1649 return CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL,
1650 &IID_IPSFactoryBuffer, (LPVOID*)facbuf);
1653 static HRESULT init_proxy_entry_point(TMProxyImpl *proxy, unsigned int num)
1656 /* nrofargs without This */
1659 TMAsmProxy *xasm = proxy->asmstubs + num;
1661 const FUNCDESC *fdesc;
1663 hres = get_funcdesc(proxy->tinfo, num, &tinfo2, &fdesc, NULL, NULL, NULL);
1665 ERR("GetFuncDesc %x should not fail here.\n",hres);
1668 ITypeInfo_Release(tinfo2);
1669 /* some args take more than 4 byte on the stack */
1671 for (j=0;j<fdesc->cParams;j++)
1672 nrofargs += _argsize(&fdesc->lprgelemdescParam[j].tdesc, proxy->tinfo);
1675 if (fdesc->callconv != CC_STDCALL) {
1676 ERR("calling convention is not stdcall????\n");
1679 /* popl %eax - return ptr
1686 * arg3 arg2 arg1 <method> <returnptr>
1688 xasm->popleax = 0x58;
1689 xasm->pushlval = 0x68;
1691 xasm->pushleax = 0x50;
1692 xasm->lcall = 0xe8; /* relative jump */
1693 xasm->xcall = (DWORD)xCall;
1694 xasm->xcall -= (DWORD)&(xasm->lret);
1696 xasm->bytestopop = (nrofargs+2)*4; /* pop args, This, iMethod */
1698 proxy->lpvtbl[num] = xasm;
1700 FIXME("not implemented on non i386\n");
1706 static HRESULT WINAPI
1707 PSFacBuf_CreateProxy(
1708 LPPSFACTORYBUFFER iface, IUnknown* pUnkOuter, REFIID riid,
1709 IRpcProxyBuffer **ppProxy, LPVOID *ppv)
1713 unsigned int i, nroffuncs;
1716 BOOL defer_to_dispatch = FALSE;
1718 TRACE("(...%s...)\n",debugstr_guid(riid));
1719 hres = _get_typeinfo_for_iid(riid,&tinfo);
1721 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
1725 hres = num_of_funcs(tinfo, &nroffuncs);
1727 ERR("Cannot get number of functions for typeinfo %s\n",debugstr_guid(riid));
1728 ITypeInfo_Release(tinfo);
1732 proxy = CoTaskMemAlloc(sizeof(TMProxyImpl));
1733 if (!proxy) return E_OUTOFMEMORY;
1735 assert(sizeof(TMAsmProxy) == 16);
1737 proxy->dispatch = NULL;
1738 proxy->dispatch_proxy = NULL;
1739 proxy->outerunknown = pUnkOuter;
1740 proxy->asmstubs = VirtualAlloc(NULL, sizeof(TMAsmProxy) * nroffuncs, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
1741 if (!proxy->asmstubs) {
1742 ERR("Could not commit pages for proxy thunks\n");
1743 CoTaskMemFree(proxy);
1744 return E_OUTOFMEMORY;
1746 proxy->IRpcProxyBuffer_iface.lpVtbl = &tmproxyvtable;
1747 /* one reference for the proxy */
1749 proxy->tinfo = tinfo;
1753 InitializeCriticalSection(&proxy->crit);
1754 proxy->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TMProxyImpl.crit");
1756 proxy->lpvtbl = HeapAlloc(GetProcessHeap(),0,sizeof(LPBYTE)*nroffuncs);
1758 /* if we derive from IDispatch then defer to its proxy for its methods */
1759 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
1762 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
1764 IPSFactoryBuffer *factory_buffer;
1765 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
1768 hres = IPSFactoryBuffer_CreateProxy(factory_buffer, NULL,
1769 &IID_IDispatch, &proxy->dispatch_proxy,
1770 (void **)&proxy->dispatch);
1771 IPSFactoryBuffer_Release(factory_buffer);
1773 if ((hres == S_OK) && (nroffuncs < 7))
1775 ERR("nroffuncs calculated incorrectly (%d)\n", nroffuncs);
1776 hres = E_UNEXPECTED;
1780 defer_to_dispatch = TRUE;
1783 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
1786 for (i=0;i<nroffuncs;i++) {
1789 proxy->lpvtbl[i] = ProxyIUnknown_QueryInterface;
1792 proxy->lpvtbl[i] = ProxyIUnknown_AddRef;
1795 proxy->lpvtbl[i] = ProxyIUnknown_Release;
1798 if(!defer_to_dispatch)
1800 hres = init_proxy_entry_point(proxy, i);
1801 if(FAILED(hres)) return hres;
1803 else proxy->lpvtbl[3] = ProxyIDispatch_GetTypeInfoCount;
1806 if(!defer_to_dispatch)
1808 hres = init_proxy_entry_point(proxy, i);
1809 if(FAILED(hres)) return hres;
1811 else proxy->lpvtbl[4] = ProxyIDispatch_GetTypeInfo;
1814 if(!defer_to_dispatch)
1816 hres = init_proxy_entry_point(proxy, i);
1817 if(FAILED(hres)) return hres;
1819 else proxy->lpvtbl[5] = ProxyIDispatch_GetIDsOfNames;
1822 if(!defer_to_dispatch)
1824 hres = init_proxy_entry_point(proxy, i);
1825 if(FAILED(hres)) return hres;
1827 else proxy->lpvtbl[6] = ProxyIDispatch_Invoke;
1830 hres = init_proxy_entry_point(proxy, i);
1831 if(FAILED(hres)) return hres;
1838 *ppProxy = &proxy->IRpcProxyBuffer_iface;
1839 IUnknown_AddRef((IUnknown *)*ppv);
1843 TMProxyImpl_Release(&proxy->IRpcProxyBuffer_iface);
1847 typedef struct _TMStubImpl {
1848 IRpcStubBuffer IRpcStubBuffer_iface;
1854 IRpcStubBuffer *dispatch_stub;
1855 BOOL dispatch_derivative;
1858 static inline TMStubImpl *impl_from_IRpcStubBuffer(IRpcStubBuffer *iface)
1860 return CONTAINING_RECORD(iface, TMStubImpl, IRpcStubBuffer_iface);
1863 static HRESULT WINAPI
1864 TMStubImpl_QueryInterface(LPRPCSTUBBUFFER iface, REFIID riid, LPVOID *ppv)
1866 if (IsEqualIID(riid,&IID_IRpcStubBuffer)||IsEqualIID(riid,&IID_IUnknown)){
1868 IRpcStubBuffer_AddRef(iface);
1871 FIXME("%s, not supported IID.\n",debugstr_guid(riid));
1872 return E_NOINTERFACE;
1876 TMStubImpl_AddRef(LPRPCSTUBBUFFER iface)
1878 TMStubImpl *This = impl_from_IRpcStubBuffer(iface);
1879 ULONG refCount = InterlockedIncrement(&This->ref);
1881 TRACE("(%p)->(ref before=%u)\n", This, refCount - 1);
1887 TMStubImpl_Release(LPRPCSTUBBUFFER iface)
1889 TMStubImpl *This = impl_from_IRpcStubBuffer(iface);
1890 ULONG refCount = InterlockedDecrement(&This->ref);
1892 TRACE("(%p)->(ref before=%u)\n", This, refCount + 1);
1896 IRpcStubBuffer_Disconnect(iface);
1897 ITypeInfo_Release(This->tinfo);
1898 if (This->dispatch_stub)
1899 IRpcStubBuffer_Release(This->dispatch_stub);
1900 CoTaskMemFree(This);
1905 static HRESULT WINAPI
1906 TMStubImpl_Connect(LPRPCSTUBBUFFER iface, LPUNKNOWN pUnkServer)
1908 TMStubImpl *This = impl_from_IRpcStubBuffer(iface);
1910 TRACE("(%p)->(%p)\n", This, pUnkServer);
1912 IUnknown_AddRef(pUnkServer);
1913 This->pUnk = pUnkServer;
1915 if (This->dispatch_stub)
1916 IRpcStubBuffer_Connect(This->dispatch_stub, pUnkServer);
1922 TMStubImpl_Disconnect(LPRPCSTUBBUFFER iface)
1924 TMStubImpl *This = impl_from_IRpcStubBuffer(iface);
1926 TRACE("(%p)->()\n", This);
1930 IUnknown_Release(This->pUnk);
1934 if (This->dispatch_stub)
1935 IRpcStubBuffer_Disconnect(This->dispatch_stub);
1938 static HRESULT WINAPI
1940 LPRPCSTUBBUFFER iface, RPCOLEMESSAGE* xmsg,IRpcChannelBuffer*rpcchanbuf)
1944 const FUNCDESC *fdesc;
1945 TMStubImpl *This = impl_from_IRpcStubBuffer(iface);
1947 DWORD *args = NULL, res, *xargs, nrofargs;
1952 ITypeInfo *tinfo = NULL;
1956 if (xmsg->iMethod < 3) {
1957 ERR("IUnknown methods cannot be marshaled by the typelib marshaler\n");
1958 return E_UNEXPECTED;
1961 if (This->dispatch_derivative && xmsg->iMethod < sizeof(IDispatchVtbl)/sizeof(void *))
1963 IPSFactoryBuffer *factory_buffer;
1964 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
1967 hres = IPSFactoryBuffer_CreateStub(factory_buffer, &IID_IDispatch,
1968 This->pUnk, &This->dispatch_stub);
1969 IPSFactoryBuffer_Release(factory_buffer);
1973 return IRpcStubBuffer_Invoke(This->dispatch_stub, xmsg, rpcchanbuf);
1976 memset(&buf,0,sizeof(buf));
1977 buf.size = xmsg->cbBuffer;
1978 buf.base = HeapAlloc(GetProcessHeap(), 0, xmsg->cbBuffer);
1979 memcpy(buf.base, xmsg->Buffer, xmsg->cbBuffer);
1982 hres = get_funcdesc(This->tinfo,xmsg->iMethod,&tinfo,&fdesc,&iname,NULL,NULL);
1984 ERR("GetFuncDesc on method %d failed with %x\n",xmsg->iMethod,hres);
1988 if (iname && !lstrcmpW(iname, IDispatchW))
1990 ERR("IDispatch cannot be marshaled by the typelib marshaler\n");
1991 hres = E_UNEXPECTED;
1992 SysFreeString (iname);
1996 SysFreeString (iname);
1998 /* Need them for hack below */
1999 memset(names,0,sizeof(names));
2000 ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames);
2001 if (nrofnames > sizeof(names)/sizeof(names[0])) {
2002 ERR("Need more names!\n");
2005 /*dump_FUNCDESC(fdesc);*/
2007 for (i=0;i<fdesc->cParams;i++)
2008 nrofargs += _argsize(&fdesc->lprgelemdescParam[i].tdesc, tinfo);
2009 args = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(nrofargs+1)*sizeof(DWORD));
2012 hres = E_OUTOFMEMORY;
2016 /* Allocate all stuff used by call. */
2018 for (i=0;i<fdesc->cParams;i++) {
2019 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2021 hres = deserialize_param(
2030 xargs += _argsize(&elem->tdesc, tinfo);
2032 ERR("Failed to deserialize param %s, hres %x\n",relaystr(names[i+1]),hres);
2037 args[0] = (DWORD)This->pUnk;
2042 (*((FARPROC**)args[0]))[fdesc->oVft/4],
2050 DWORD dwExceptionCode = GetExceptionCode();
2051 ERR("invoke call failed with exception 0x%08x (%d)\n", dwExceptionCode, dwExceptionCode);
2052 if (FAILED(dwExceptionCode))
2053 hres = dwExceptionCode;
2055 hres = HRESULT_FROM_WIN32(dwExceptionCode);
2065 for (i=0;i<fdesc->cParams;i++) {
2066 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2067 hres = serialize_param(
2076 xargs += _argsize(&elem->tdesc, tinfo);
2078 ERR("Failed to stuballoc param, hres %x\n",hres);
2083 hres = xbuf_add (&buf, (LPBYTE)&res, sizeof(DWORD));
2088 xmsg->cbBuffer = buf.curoff;
2089 hres = IRpcChannelBuffer_GetBuffer(rpcchanbuf, xmsg, &This->iid);
2091 ERR("IRpcChannelBuffer_GetBuffer failed with error 0x%08x\n", hres);
2094 memcpy(xmsg->Buffer, buf.base, buf.curoff);
2097 for (i = 0; i < nrofnames; i++)
2098 SysFreeString(names[i]);
2100 ITypeInfo_Release(tinfo);
2101 HeapFree(GetProcessHeap(), 0, args);
2103 HeapFree(GetProcessHeap(), 0, buf.base);
2105 TRACE("returning\n");
2108 FIXME( "not implemented on non-i386\n" );
2113 static LPRPCSTUBBUFFER WINAPI
2114 TMStubImpl_IsIIDSupported(LPRPCSTUBBUFFER iface, REFIID riid) {
2115 FIXME("Huh (%s)?\n",debugstr_guid(riid));
2120 TMStubImpl_CountRefs(LPRPCSTUBBUFFER iface) {
2121 TMStubImpl *This = impl_from_IRpcStubBuffer(iface);
2124 return This->ref; /*FIXME? */
2127 static HRESULT WINAPI
2128 TMStubImpl_DebugServerQueryInterface(LPRPCSTUBBUFFER iface, LPVOID *ppv) {
2133 TMStubImpl_DebugServerRelease(LPRPCSTUBBUFFER iface, LPVOID ppv) {
2137 static const IRpcStubBufferVtbl tmstubvtbl = {
2138 TMStubImpl_QueryInterface,
2142 TMStubImpl_Disconnect,
2144 TMStubImpl_IsIIDSupported,
2145 TMStubImpl_CountRefs,
2146 TMStubImpl_DebugServerQueryInterface,
2147 TMStubImpl_DebugServerRelease
2150 static HRESULT WINAPI
2151 PSFacBuf_CreateStub(
2152 LPPSFACTORYBUFFER iface, REFIID riid,IUnknown *pUnkServer,
2153 IRpcStubBuffer** ppStub
2160 TRACE("(%s,%p,%p)\n",debugstr_guid(riid),pUnkServer,ppStub);
2162 hres = _get_typeinfo_for_iid(riid,&tinfo);
2164 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
2168 stub = CoTaskMemAlloc(sizeof(TMStubImpl));
2170 return E_OUTOFMEMORY;
2171 stub->IRpcStubBuffer_iface.lpVtbl = &tmstubvtbl;
2173 stub->tinfo = tinfo;
2174 stub->dispatch_stub = NULL;
2175 stub->dispatch_derivative = FALSE;
2177 hres = IRpcStubBuffer_Connect(&stub->IRpcStubBuffer_iface,pUnkServer);
2178 *ppStub = &stub->IRpcStubBuffer_iface;
2179 TRACE("IRpcStubBuffer: %p\n", stub);
2181 ERR("Connect to pUnkServer failed?\n");
2183 /* if we derive from IDispatch then defer to its stub for some of its methods */
2184 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
2187 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
2188 stub->dispatch_derivative = TRUE;
2189 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
2195 static const IPSFactoryBufferVtbl psfacbufvtbl = {
2196 PSFacBuf_QueryInterface,
2199 PSFacBuf_CreateProxy,
2203 /* This is the whole PSFactoryBuffer object, just the vtableptr */
2204 static const IPSFactoryBufferVtbl *lppsfac = &psfacbufvtbl;
2206 /***********************************************************************
2207 * TMARSHAL_DllGetClassObject
2209 HRESULT TMARSHAL_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
2211 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)) {
2215 return E_NOINTERFACE;