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 #define ICOM_THIS_MULTI(impl,field,iface) impl* const This=(impl*)((char*)(iface) - offsetof(impl,field))
59 static HRESULT TMarshalDispatchChannel_Create(
60 IRpcChannelBuffer *pDelegateChannel, REFIID tmarshal_riid,
61 IRpcChannelBuffer **ppChannel);
63 typedef struct _marshal_state {
69 /* used in the olerelay code to avoid having the L"" stuff added by debugstr_w */
70 static char *relaystr(WCHAR *in) {
71 char *tmp = (char *)debugstr_w(in);
73 tmp[strlen(tmp)-1] = '\0';
78 xbuf_resize(marshal_state *buf, DWORD newsize)
80 if(buf->size >= newsize)
85 buf->base = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buf->base, newsize);
91 buf->base = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, newsize);
100 xbuf_add(marshal_state *buf, const BYTE *stuff, DWORD size)
104 if(buf->size - buf->curoff < size)
106 hr = xbuf_resize(buf, buf->size + size + 100);
107 if(FAILED(hr)) return hr;
109 memcpy(buf->base+buf->curoff,stuff,size);
115 xbuf_get(marshal_state *buf, LPBYTE stuff, DWORD size) {
116 if (buf->size < buf->curoff+size) return E_FAIL;
117 memcpy(stuff,buf->base+buf->curoff,size);
123 xbuf_skip(marshal_state *buf, DWORD size) {
124 if (buf->size < buf->curoff+size) return E_FAIL;
130 _unmarshal_interface(marshal_state *buf, REFIID riid, LPUNKNOWN *pUnk) {
132 ULARGE_INTEGER newpos;
133 LARGE_INTEGER seekto;
138 TRACE("...%s...\n",debugstr_guid(riid));
141 hres = xbuf_get(buf,(LPBYTE)&xsize,sizeof(xsize));
143 ERR("xbuf_get failed\n");
147 if (xsize == 0) return S_OK;
149 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
151 ERR("Stream create failed %x\n",hres);
155 hres = IStream_Write(pStm,buf->base+buf->curoff,xsize,&res);
157 ERR("stream write %x\n",hres);
161 memset(&seekto,0,sizeof(seekto));
162 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
164 ERR("Failed Seek %x\n",hres);
168 hres = CoUnmarshalInterface(pStm,riid,(LPVOID*)pUnk);
170 ERR("Unmarshalling interface %s failed with %x\n",debugstr_guid(riid),hres);
174 IStream_Release(pStm);
175 return xbuf_skip(buf,xsize);
179 _marshal_interface(marshal_state *buf, REFIID riid, LPUNKNOWN pUnk) {
180 LPBYTE tempbuf = NULL;
181 IStream *pStm = NULL;
183 ULARGE_INTEGER newpos;
184 LARGE_INTEGER seekto;
190 /* this is valid, if for instance we serialize
191 * a VT_DISPATCH with NULL ptr which apparently
192 * can happen. S_OK to make sure we continue
195 WARN("pUnk is NULL\n");
197 return xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
202 TRACE("...%s...\n",debugstr_guid(riid));
204 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
206 ERR("Stream create failed %x\n",hres);
210 hres = CoMarshalInterface(pStm,riid,pUnk,0,NULL,0);
212 ERR("Marshalling interface %s failed with %x\n", debugstr_guid(riid), hres);
216 hres = IStream_Stat(pStm,&ststg,0);
218 ERR("Stream stat failed\n");
222 tempbuf = HeapAlloc(GetProcessHeap(), 0, ststg.cbSize.u.LowPart);
223 memset(&seekto,0,sizeof(seekto));
224 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
226 ERR("Failed Seek %x\n",hres);
230 hres = IStream_Read(pStm,tempbuf,ststg.cbSize.u.LowPart,&res);
232 ERR("Failed Read %x\n",hres);
236 xsize = ststg.cbSize.u.LowPart;
237 xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
238 hres = xbuf_add(buf,tempbuf,ststg.cbSize.u.LowPart);
240 HeapFree(GetProcessHeap(),0,tempbuf);
241 IStream_Release(pStm);
247 xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
248 if (pStm) IUnknown_Release(pStm);
249 HeapFree(GetProcessHeap(), 0, tempbuf);
253 /********************* OLE Proxy/Stub Factory ********************************/
254 static HRESULT WINAPI
255 PSFacBuf_QueryInterface(LPPSFACTORYBUFFER iface, REFIID iid, LPVOID *ppv) {
256 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)||IsEqualIID(iid,&IID_IUnknown)) {
257 *ppv = (LPVOID)iface;
258 /* No ref counting, static class */
261 FIXME("(%s) unknown IID?\n",debugstr_guid(iid));
262 return E_NOINTERFACE;
265 static ULONG WINAPI PSFacBuf_AddRef(LPPSFACTORYBUFFER iface) { return 2; }
266 static ULONG WINAPI PSFacBuf_Release(LPPSFACTORYBUFFER iface) { return 1; }
269 _get_typeinfo_for_iid(REFIID riid, ITypeInfo**ti) {
272 char tlguid[200],typelibkey[300],interfacekey[300],ver[100];
275 DWORD tlguidlen, verlen, type;
279 sprintf( interfacekey, "Interface\\{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\Typelib",
280 riid->Data1, riid->Data2, riid->Data3,
281 riid->Data4[0], riid->Data4[1], riid->Data4[2], riid->Data4[3],
282 riid->Data4[4], riid->Data4[5], riid->Data4[6], riid->Data4[7]
285 if (RegOpenKeyA(HKEY_CLASSES_ROOT,interfacekey,&ikey)) {
286 ERR("No %s key found.\n",interfacekey);
289 tlguidlen = sizeof(tlguid);
290 if (RegQueryValueExA(ikey,NULL,NULL,&type,(LPBYTE)tlguid,&tlguidlen)) {
291 ERR("Getting typelib guid failed.\n");
295 verlen = sizeof(ver);
296 if (RegQueryValueExA(ikey,"Version",NULL,&type,(LPBYTE)ver,&verlen)) {
297 ERR("Could not get version value?\n");
302 sprintf(typelibkey,"Typelib\\%s\\%s\\0\\win32",tlguid,ver);
303 tlfnlen = sizeof(tlfn);
304 if (RegQueryValueA(HKEY_CLASSES_ROOT,typelibkey,tlfn,&tlfnlen)) {
305 ERR("Could not get typelib fn?\n");
308 MultiByteToWideChar(CP_ACP, 0, tlfn, -1, tlfnW, sizeof(tlfnW) / sizeof(tlfnW[0]));
309 hres = LoadTypeLib(tlfnW,&tl);
311 ERR("Failed to load typelib for %s, but it should be there.\n",debugstr_guid(riid));
314 hres = ITypeLib_GetTypeInfoOfGuid(tl,riid,ti);
316 ERR("typelib does not contain info for %s?\n",debugstr_guid(riid));
317 ITypeLib_Release(tl);
320 ITypeLib_Release(tl);
325 * Determine the number of functions including all inherited functions.
326 * Note for non-dual dispinterfaces we simply return the size of IDispatch.
328 static HRESULT num_of_funcs(ITypeInfo *tinfo, unsigned int *num)
335 hres = ITypeInfo_GetTypeAttr(tinfo, &attr);
337 ERR("GetTypeAttr failed with %x\n",hres);
341 if(attr->typekind == TKIND_DISPATCH && (attr->wTypeFlags & TYPEFLAG_FDUAL))
344 hres = ITypeInfo_GetRefTypeOfImplType(tinfo, -1, &href);
347 ERR("Unable to get interface href from dual dispinterface\n");
350 hres = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
353 ERR("Unable to get interface from dual dispinterface\n");
356 hres = num_of_funcs(tinfo2, num);
357 ITypeInfo_Release(tinfo2);
361 *num = attr->cbSizeVft / 4;
365 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
371 #include "pshpack1.h"
373 typedef struct _TMAsmProxy {
388 # warning You need to implement stubless proxies for your architecture
389 typedef struct _TMAsmProxy {
393 typedef struct _TMProxyImpl {
395 const IRpcProxyBufferVtbl *lpvtbl2;
398 TMAsmProxy *asmstubs;
400 IRpcChannelBuffer* chanbuf;
402 CRITICAL_SECTION crit;
403 IUnknown *outerunknown;
405 IRpcProxyBuffer *dispatch_proxy;
408 static HRESULT WINAPI
409 TMProxyImpl_QueryInterface(LPRPCPROXYBUFFER iface, REFIID riid, LPVOID *ppv)
412 if (IsEqualIID(riid,&IID_IUnknown)||IsEqualIID(riid,&IID_IRpcProxyBuffer)) {
413 *ppv = (LPVOID)iface;
414 IRpcProxyBuffer_AddRef(iface);
417 FIXME("no interface for %s\n",debugstr_guid(riid));
418 return E_NOINTERFACE;
422 TMProxyImpl_AddRef(LPRPCPROXYBUFFER iface)
424 ICOM_THIS_MULTI(TMProxyImpl,lpvtbl2,iface);
425 ULONG refCount = InterlockedIncrement(&This->ref);
427 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
433 TMProxyImpl_Release(LPRPCPROXYBUFFER iface)
435 ICOM_THIS_MULTI(TMProxyImpl,lpvtbl2,iface);
436 ULONG refCount = InterlockedDecrement(&This->ref);
438 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
442 if (This->dispatch_proxy) IRpcProxyBuffer_Release(This->dispatch_proxy);
443 This->crit.DebugInfo->Spare[0] = 0;
444 DeleteCriticalSection(&This->crit);
445 if (This->chanbuf) IRpcChannelBuffer_Release(This->chanbuf);
446 VirtualFree(This->asmstubs, 0, MEM_RELEASE);
447 HeapFree(GetProcessHeap(), 0, This->lpvtbl);
448 ITypeInfo_Release(This->tinfo);
454 static HRESULT WINAPI
456 LPRPCPROXYBUFFER iface,IRpcChannelBuffer* pRpcChannelBuffer)
458 ICOM_THIS_MULTI(TMProxyImpl, lpvtbl2, iface);
460 TRACE("(%p)\n", pRpcChannelBuffer);
462 EnterCriticalSection(&This->crit);
464 IRpcChannelBuffer_AddRef(pRpcChannelBuffer);
465 This->chanbuf = pRpcChannelBuffer;
467 LeaveCriticalSection(&This->crit);
469 if (This->dispatch_proxy)
471 IRpcChannelBuffer *pDelegateChannel;
472 HRESULT hr = TMarshalDispatchChannel_Create(pRpcChannelBuffer, &This->iid, &pDelegateChannel);
475 hr = IRpcProxyBuffer_Connect(This->dispatch_proxy, pDelegateChannel);
476 IRpcChannelBuffer_Release(pDelegateChannel);
484 TMProxyImpl_Disconnect(LPRPCPROXYBUFFER iface)
486 ICOM_THIS_MULTI(TMProxyImpl, lpvtbl2, iface);
490 EnterCriticalSection(&This->crit);
492 IRpcChannelBuffer_Release(This->chanbuf);
493 This->chanbuf = NULL;
495 LeaveCriticalSection(&This->crit);
497 if (This->dispatch_proxy)
498 IRpcProxyBuffer_Disconnect(This->dispatch_proxy);
502 static const IRpcProxyBufferVtbl tmproxyvtable = {
503 TMProxyImpl_QueryInterface,
507 TMProxyImpl_Disconnect
510 /* how much space do we use on stack in DWORD steps. */
515 return 8/sizeof(DWORD);
517 return sizeof(double)/sizeof(DWORD);
519 return sizeof(CY)/sizeof(DWORD);
521 return sizeof(DATE)/sizeof(DWORD);
523 return (sizeof(VARIANT)+3)/sizeof(DWORD);
530 _xsize(const TYPEDESC *td) {
535 return sizeof(VARIANT)+3;
538 const ARRAYDESC *adesc = td->u.lpadesc;
540 for (i=0;i<adesc->cDims;i++)
541 arrsize *= adesc->rgbounds[i].cElements;
542 return arrsize*_xsize(&adesc->tdescElem);
570 TRACE("(tdesc.vt %s)\n",debugstr_vt(tdesc->vt));
573 case VT_EMPTY: /* nothing. empty variant for instance */
580 if (debugout) TRACE_(olerelay)("%x%x\n",arg[0],arg[1]);
582 hres = xbuf_add(buf,(LPBYTE)arg,8);
592 if (debugout) TRACE_(olerelay)("%x\n",*arg);
594 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
599 if (debugout) TRACE_(olerelay)("%04x\n",*arg & 0xffff);
601 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
606 if (debugout) TRACE_(olerelay)("%02x\n",*arg & 0xff);
608 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
612 if (debugout) TRACE_(olerelay)("&0x%x\n",*arg);
614 hres = xbuf_add(buf,(LPBYTE)(DWORD*)*arg,sizeof(DWORD));
615 /* do not dealloc at this time */
619 VARIANT *vt = (VARIANT*)arg;
620 DWORD vttype = V_VT(vt);
622 if (debugout) TRACE_(olerelay)("Vt(%s%s)(",debugstr_vt(vttype),debugstr_vf(vttype));
625 hres = xbuf_add(buf,(LPBYTE)&vttype,sizeof(vttype));
626 if (hres) return hres;
628 /* need to recurse since we need to free the stuff */
629 hres = serialize_param(tinfo,writeit,debugout,dealloc,&tdesc2,(DWORD*)&(V_I4(vt)),buf);
630 if (debugout) TRACE_(olerelay)(")");
633 case VT_BSTR|VT_BYREF: {
634 if (debugout) TRACE_(olerelay)("[byref]'%s'", *(BSTR*)*arg ? relaystr(*((BSTR*)*arg)) : "<bstr NULL>");
636 /* ptr to ptr to magic widestring, basically */
637 BSTR *bstr = (BSTR *) *arg;
640 /* -1 means "null string" which is equivalent to empty string */
642 hres = xbuf_add(buf, (LPBYTE)&len,sizeof(DWORD));
643 if (hres) return hres;
645 len = *((DWORD*)*bstr-1)/sizeof(WCHAR);
646 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
647 if (hres) return hres;
648 hres = xbuf_add(buf,(LPBYTE)*bstr,len * sizeof(WCHAR));
649 if (hres) return hres;
653 if (dealloc && arg) {
654 BSTR *str = *((BSTR **)arg);
663 TRACE_(olerelay)("%s",relaystr((WCHAR*)*arg));
665 TRACE_(olerelay)("<bstr NULL>");
668 BSTR bstr = (BSTR)*arg;
672 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
673 if (hres) return hres;
675 len = *((DWORD*)bstr-1)/sizeof(WCHAR);
676 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
677 if (hres) return hres;
678 hres = xbuf_add(buf,(LPBYTE)bstr,len * sizeof(WCHAR));
679 if (hres) return hres;
684 SysFreeString((BSTR)*arg);
689 BOOL derefhere = TRUE;
691 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
695 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
697 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
700 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
701 switch (tattr->typekind) {
703 if (tattr->tdescAlias.vt == VT_USERDEFINED)
705 DWORD href = tattr->tdescAlias.u.hreftype;
706 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
707 ITypeInfo_Release(tinfo2);
708 hres = ITypeInfo_GetRefTypeInfo(tinfo,href,&tinfo2);
710 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
713 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
714 derefhere = (tattr->typekind != TKIND_DISPATCH && tattr->typekind != TKIND_INTERFACE);
717 case TKIND_ENUM: /* confirmed */
718 case TKIND_RECORD: /* FIXME: mostly untested */
720 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
721 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
725 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
729 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
730 ITypeInfo_Release(tinfo2);
733 if (debugout) TRACE_(olerelay)("*");
734 /* Write always, so the other side knows when it gets a NULL pointer.
736 cookie = *arg ? 0x42424242 : 0;
737 hres = xbuf_add(buf,(LPBYTE)&cookie,sizeof(cookie));
741 if (debugout) TRACE_(olerelay)("NULL");
744 hres = serialize_param(tinfo,writeit,debugout,dealloc,tdesc->u.lptdesc,(DWORD*)*arg,buf);
745 if (derefhere && dealloc) HeapFree(GetProcessHeap(),0,(LPVOID)*arg);
749 if (debugout) TRACE_(olerelay)("unk(0x%x)",*arg);
751 hres = _marshal_interface(buf,&IID_IUnknown,(LPUNKNOWN)*arg);
752 if (dealloc && *(IUnknown **)arg)
753 IUnknown_Release((LPUNKNOWN)*arg);
756 if (debugout) TRACE_(olerelay)("idisp(0x%x)",*arg);
758 hres = _marshal_interface(buf,&IID_IDispatch,(LPUNKNOWN)*arg);
759 if (dealloc && *(IUnknown **)arg)
760 IUnknown_Release((LPUNKNOWN)*arg);
763 if (debugout) TRACE_(olerelay)("<void>");
765 case VT_USERDEFINED: {
769 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
771 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
774 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
775 switch (tattr->typekind) {
777 case TKIND_INTERFACE:
779 hres=_marshal_interface(buf,&(tattr->guid),(LPUNKNOWN)arg);
781 IUnknown_Release((LPUNKNOWN)arg);
785 if (debugout) TRACE_(olerelay)("{");
786 for (i=0;i<tattr->cVars;i++) {
791 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
793 ERR("Could not get vardesc of %d\n",i);
796 elem2 = &vdesc->elemdescVar;
797 tdesc2 = &elem2->tdesc;
798 hres = serialize_param(
804 (DWORD*)(((LPBYTE)arg)+vdesc->u.oInst),
807 ITypeInfo_ReleaseVarDesc(tinfo2, vdesc);
810 if (debugout && (i<(tattr->cVars-1)))
811 TRACE_(olerelay)(",");
813 if (debugout) TRACE_(olerelay)("}");
817 hres = serialize_param(tinfo2,writeit,debugout,dealloc,&tattr->tdescAlias,arg,buf);
821 if (debugout) TRACE_(olerelay)("%x",*arg);
823 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
826 FIXME("Unhandled typekind %d\n",tattr->typekind);
830 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
831 ITypeInfo_Release(tinfo2);
835 ARRAYDESC *adesc = tdesc->u.lpadesc;
838 if (debugout) TRACE_(olerelay)("carr");
839 for (i=0;i<adesc->cDims;i++) {
840 if (debugout) TRACE_(olerelay)("[%d]",adesc->rgbounds[i].cElements);
841 arrsize *= adesc->rgbounds[i].cElements;
843 if (debugout) TRACE_(olerelay)("(vt %s)",debugstr_vt(adesc->tdescElem.vt));
844 if (debugout) TRACE_(olerelay)("[");
845 for (i=0;i<arrsize;i++) {
846 hres = serialize_param(tinfo, writeit, debugout, dealloc, &adesc->tdescElem, (DWORD*)((LPBYTE)arg+i*_xsize(&adesc->tdescElem)), buf);
849 if (debugout && (i<arrsize-1)) TRACE_(olerelay)(",");
851 if (debugout) TRACE_(olerelay)("]");
857 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
858 ULONG size = LPSAFEARRAY_UserSize(&flags, buf->curoff, (LPSAFEARRAY *)arg);
859 xbuf_resize(buf, size);
860 LPSAFEARRAY_UserMarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
866 ERR("Unhandled marshal type %d.\n",tdesc->vt);
883 TRACE("vt %s at %p\n",debugstr_vt(tdesc->vt),arg);
888 if (debugout) TRACE_(olerelay)("<empty>\n");
891 if (debugout) TRACE_(olerelay)("<null>\n");
894 VARIANT *vt = (VARIANT*)arg;
899 hres = xbuf_get(buf,(LPBYTE)&vttype,sizeof(vttype));
901 FIXME("vt type not read?\n");
904 memset(&tdesc2,0,sizeof(tdesc2));
907 if (debugout) TRACE_(olerelay)("Vt(%s%s)(",debugstr_vt(vttype),debugstr_vf(vttype));
908 hres = deserialize_param(tinfo, readit, debugout, alloc, &tdesc2, (DWORD*)&(V_I4(vt)), buf);
909 TRACE_(olerelay)(")");
921 hres = xbuf_get(buf,(LPBYTE)arg,8);
922 if (hres) ERR("Failed to read integer 8 byte\n");
924 if (debugout) TRACE_(olerelay)("%x%x",arg[0],arg[1]);
934 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
935 if (hres) ERR("Failed to read integer 4 byte\n");
937 if (debugout) TRACE_(olerelay)("%x",*arg);
943 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
944 if (hres) ERR("Failed to read integer 4 byte\n");
947 if (debugout) TRACE_(olerelay)("%04x",*arg & 0xffff);
953 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
954 if (hres) ERR("Failed to read integer 4 byte\n");
957 if (debugout) TRACE_(olerelay)("%02x",*arg & 0xff);
962 *arg = (DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
964 hres = xbuf_get(buf,(LPBYTE)*arg,sizeof(DWORD));
965 if (hres) ERR("Failed to read integer 4 byte\n");
967 if (debugout) TRACE_(olerelay)("&0x%x",*(DWORD*)*arg);
969 case VT_BSTR|VT_BYREF: {
970 BSTR **bstr = (BSTR **)arg;
975 hres = xbuf_get(buf,(LPBYTE)&len,sizeof(DWORD));
977 ERR("failed to read bstr klen\n");
981 *bstr = CoTaskMemAlloc(sizeof(BSTR *));
983 if (debugout) TRACE_(olerelay)("<bstr NULL>");
985 str = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(len+1)*sizeof(WCHAR));
986 hres = xbuf_get(buf,(LPBYTE)str,len*sizeof(WCHAR));
988 ERR("Failed to read BSTR.\n");
989 HeapFree(GetProcessHeap(),0,str);
992 *bstr = CoTaskMemAlloc(sizeof(BSTR *));
993 **bstr = SysAllocStringLen(str,len);
994 if (debugout) TRACE_(olerelay)("%s",relaystr(str));
995 HeapFree(GetProcessHeap(),0,str);
1007 hres = xbuf_get(buf,(LPBYTE)&len,sizeof(DWORD));
1009 ERR("failed to read bstr klen\n");
1014 if (debugout) TRACE_(olerelay)("<bstr NULL>");
1016 str = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(len+1)*sizeof(WCHAR));
1017 hres = xbuf_get(buf,(LPBYTE)str,len*sizeof(WCHAR));
1019 ERR("Failed to read BSTR.\n");
1020 HeapFree(GetProcessHeap(),0,str);
1023 *arg = (DWORD)SysAllocStringLen(str,len);
1024 if (debugout) TRACE_(olerelay)("%s",relaystr(str));
1025 HeapFree(GetProcessHeap(),0,str);
1034 BOOL derefhere = TRUE;
1036 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
1040 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
1042 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
1045 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1046 switch (tattr->typekind) {
1048 if (tattr->tdescAlias.vt == VT_USERDEFINED)
1050 DWORD href = tattr->tdescAlias.u.hreftype;
1051 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
1052 ITypeInfo_Release(tinfo2);
1053 hres = ITypeInfo_GetRefTypeInfo(tinfo,href,&tinfo2);
1055 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
1058 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1059 derefhere = (tattr->typekind != TKIND_DISPATCH && tattr->typekind != TKIND_INTERFACE);
1062 case TKIND_ENUM: /* confirmed */
1063 case TKIND_RECORD: /* FIXME: mostly untested */
1065 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
1066 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
1070 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
1074 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1075 ITypeInfo_Release(tinfo2);
1077 /* read it in all cases, we need to know if we have
1078 * NULL pointer or not.
1080 hres = xbuf_get(buf,(LPBYTE)&cookie,sizeof(cookie));
1082 ERR("Failed to load pointer cookie.\n");
1085 if (cookie != 0x42424242) {
1086 /* we read a NULL ptr from the remote side */
1087 if (debugout) TRACE_(olerelay)("NULL");
1091 if (debugout) TRACE_(olerelay)("*");
1093 /* Allocate space for the referenced struct */
1095 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,_xsize(tdesc->u.lptdesc));
1098 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, (LPDWORD)*arg, buf);
1100 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, arg, buf);
1103 /* FIXME: UNKNOWN is unknown ..., but allocate 4 byte for it */
1105 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
1108 hres = _unmarshal_interface(buf,&IID_IUnknown,(LPUNKNOWN*)arg);
1110 TRACE_(olerelay)("unk(%p)",arg);
1115 hres = _unmarshal_interface(buf,&IID_IDispatch,(LPUNKNOWN*)arg);
1117 TRACE_(olerelay)("idisp(%p)",arg);
1120 if (debugout) TRACE_(olerelay)("<void>");
1122 case VT_USERDEFINED: {
1126 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
1128 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
1131 hres = ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1133 ERR("Could not get typeattr in VT_USERDEFINED.\n");
1135 switch (tattr->typekind) {
1136 case TKIND_DISPATCH:
1137 case TKIND_INTERFACE:
1139 hres = _unmarshal_interface(buf,&(tattr->guid),(LPUNKNOWN*)arg);
1141 case TKIND_RECORD: {
1145 *arg = (DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,tattr->cbSizeInstance);
1147 if (debugout) TRACE_(olerelay)("{");
1148 for (i=0;i<tattr->cVars;i++) {
1151 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
1153 ERR("Could not get vardesc of %d\n",i);
1154 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1155 ITypeInfo_Release(tinfo2);
1158 hres = deserialize_param(
1163 &vdesc->elemdescVar.tdesc,
1164 (DWORD*)(((LPBYTE)*arg)+vdesc->u.oInst),
1167 ITypeInfo2_ReleaseVarDesc(tinfo2, vdesc);
1168 if (debugout && (i<tattr->cVars-1)) TRACE_(olerelay)(",");
1170 if (debugout) TRACE_(olerelay)("}");
1174 hres = deserialize_param(tinfo2,readit,debugout,alloc,&tattr->tdescAlias,arg,buf);
1178 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
1179 if (hres) ERR("Failed to read enum (4 byte)\n");
1181 if (debugout) TRACE_(olerelay)("%x",*arg);
1184 ERR("Unhandled typekind %d\n",tattr->typekind);
1188 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1191 ERR("failed to stuballoc in TKIND_RECORD.\n");
1192 ITypeInfo_Release(tinfo2);
1196 /* arg is pointing to the start of the array. */
1197 ARRAYDESC *adesc = tdesc->u.lpadesc;
1200 if (adesc->cDims > 1) FIXME("cDims > 1 in VT_CARRAY. Does it work?\n");
1201 for (i=0;i<adesc->cDims;i++)
1202 arrsize *= adesc->rgbounds[i].cElements;
1203 for (i=0;i<arrsize;i++)
1210 (DWORD*)((LPBYTE)(arg)+i*_xsize(&adesc->tdescElem)),
1215 case VT_SAFEARRAY: {
1218 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
1219 unsigned char *buffer;
1220 buffer = LPSAFEARRAY_UserUnmarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
1221 buf->curoff = buffer - buf->base;
1226 ERR("No handler for VT type %d!\n",tdesc->vt);
1232 /* Retrieves a function's funcdesc, searching back into inherited interfaces. */
1233 static HRESULT get_funcdesc(ITypeInfo *tinfo, int iMethod, ITypeInfo **tactual, const FUNCDESC **fdesc,
1234 BSTR *iname, BSTR *fname, UINT *num)
1238 UINT inherited_funcs = 0;
1241 if (fname) *fname = NULL;
1242 if (iname) *iname = NULL;
1246 hr = ITypeInfo_GetTypeAttr(tinfo, &attr);
1249 ERR("GetTypeAttr failed with %x\n",hr);
1253 if(attr->typekind == TKIND_DISPATCH)
1255 if(attr->wTypeFlags & TYPEFLAG_FDUAL)
1260 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, -1, &href);
1263 ERR("Cannot get interface href from dual dispinterface\n");
1264 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1267 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
1270 ERR("Cannot get interface from dual dispinterface\n");
1271 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1274 hr = get_funcdesc(tinfo2, iMethod, tactual, fdesc, iname, fname, num);
1275 ITypeInfo_Release(tinfo2);
1276 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1279 ERR("Shouldn't be called with a non-dual dispinterface\n");
1283 impl_types = attr->cImplTypes;
1284 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1286 for (i = 0; i < impl_types; i++)
1289 ITypeInfo *pSubTypeInfo;
1292 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, i, &href);
1293 if (FAILED(hr)) return hr;
1294 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &pSubTypeInfo);
1295 if (FAILED(hr)) return hr;
1297 hr = get_funcdesc(pSubTypeInfo, iMethod, tactual, fdesc, iname, fname, &sub_funcs);
1298 inherited_funcs += sub_funcs;
1299 ITypeInfo_Release(pSubTypeInfo);
1300 if(SUCCEEDED(hr)) return hr;
1302 if(iMethod < inherited_funcs)
1304 ERR("shouldn't be here\n");
1305 return E_INVALIDARG;
1308 for(i = inherited_funcs; i <= iMethod; i++)
1310 hr = ITypeInfoImpl_GetInternalFuncDesc(tinfo, i - inherited_funcs, fdesc);
1318 /* found it. We don't care about num so zero it */
1321 ITypeInfo_AddRef(*tactual);
1322 if (fname) ITypeInfo_GetDocumentation(tinfo,(*fdesc)->memid,fname,NULL,NULL,NULL);
1323 if (iname) ITypeInfo_GetDocumentation(tinfo,-1,iname,NULL,NULL,NULL);
1327 static inline BOOL is_in_elem(const ELEMDESC *elem)
1329 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN || !elem->u.paramdesc.wParamFlags);
1332 static inline BOOL is_out_elem(const ELEMDESC *elem)
1334 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT || !elem->u.paramdesc.wParamFlags);
1338 xCall(LPVOID retptr, int method, TMProxyImpl *tpinfo /*, args */)
1340 DWORD *args = ((DWORD*)&tpinfo)+1, *xargs;
1341 const FUNCDESC *fdesc;
1343 int i, relaydeb = TRACE_ON(olerelay);
1350 DWORD remoteresult = 0;
1352 IRpcChannelBuffer *chanbuf;
1354 EnterCriticalSection(&tpinfo->crit);
1356 hres = get_funcdesc(tpinfo->tinfo,method,&tinfo,&fdesc,&iname,&fname,NULL);
1358 ERR("Did not find typeinfo/funcdesc entry for method %d!\n",method);
1359 LeaveCriticalSection(&tpinfo->crit);
1363 if (!tpinfo->chanbuf)
1365 WARN("Tried to use disconnected proxy\n");
1366 ITypeInfo_Release(tinfo);
1367 LeaveCriticalSection(&tpinfo->crit);
1368 return RPC_E_DISCONNECTED;
1370 chanbuf = tpinfo->chanbuf;
1371 IRpcChannelBuffer_AddRef(chanbuf);
1373 LeaveCriticalSection(&tpinfo->crit);
1376 TRACE_(olerelay)("->");
1378 TRACE_(olerelay)("%s:",relaystr(iname));
1380 TRACE_(olerelay)("%s(%d)",relaystr(fname),method);
1382 TRACE_(olerelay)("%d",method);
1383 TRACE_(olerelay)("(");
1386 if (iname) SysFreeString(iname);
1387 if (fname) SysFreeString(fname);
1389 memset(&buf,0,sizeof(buf));
1391 /* normal typelib driven serializing */
1393 /* Need them for hack below */
1394 memset(names,0,sizeof(names));
1395 if (ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames))
1397 if (nrofnames > sizeof(names)/sizeof(names[0]))
1398 ERR("Need more names!\n");
1401 for (i=0;i<fdesc->cParams;i++) {
1402 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1404 if (i) TRACE_(olerelay)(",");
1405 if (i+1<nrofnames && names[i+1])
1406 TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1408 /* No need to marshal other data than FIN and any VT_PTR. */
1409 if (!is_in_elem(elem) && (elem->tdesc.vt != VT_PTR)) {
1410 xargs+=_argsize(elem->tdesc.vt);
1411 if (relaydeb) TRACE_(olerelay)("[out]");
1414 hres = serialize_param(
1425 ERR("Failed to serialize param, hres %x\n",hres);
1428 xargs+=_argsize(elem->tdesc.vt);
1430 if (relaydeb) TRACE_(olerelay)(")");
1432 memset(&msg,0,sizeof(msg));
1433 msg.cbBuffer = buf.curoff;
1434 msg.iMethod = method;
1435 hres = IRpcChannelBuffer_GetBuffer(chanbuf,&msg,&(tpinfo->iid));
1437 ERR("RpcChannelBuffer GetBuffer failed, %x\n",hres);
1440 memcpy(msg.Buffer,buf.base,buf.curoff);
1441 if (relaydeb) TRACE_(olerelay)("\n");
1442 hres = IRpcChannelBuffer_SendReceive(chanbuf,&msg,&status);
1444 ERR("RpcChannelBuffer SendReceive failed, %x\n",hres);
1448 if (relaydeb) TRACE_(olerelay)(" status = %08x (",status);
1450 buf.base = HeapReAlloc(GetProcessHeap(),0,buf.base,msg.cbBuffer);
1452 buf.base = HeapAlloc(GetProcessHeap(),0,msg.cbBuffer);
1453 buf.size = msg.cbBuffer;
1454 memcpy(buf.base,msg.Buffer,buf.size);
1457 /* generic deserializer using typelib description */
1460 for (i=0;i<fdesc->cParams;i++) {
1461 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1464 if (i) TRACE_(olerelay)(",");
1465 if (i+1<nrofnames && names[i+1]) TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1467 /* No need to marshal other data than FOUT and any VT_PTR */
1468 if (!is_out_elem(elem) && (elem->tdesc.vt != VT_PTR)) {
1469 xargs += _argsize(elem->tdesc.vt);
1470 if (relaydeb) TRACE_(olerelay)("[in]");
1473 hres = deserialize_param(
1483 ERR("Failed to unmarshall param, hres %x\n",hres);
1487 xargs += _argsize(elem->tdesc.vt);
1490 hres = xbuf_get(&buf, (LPBYTE)&remoteresult, sizeof(DWORD));
1493 if (relaydeb) TRACE_(olerelay)(") = %08x\n", remoteresult);
1495 hres = remoteresult;
1498 IRpcChannelBuffer_FreeBuffer(chanbuf,&msg);
1499 for (i = 0; i < nrofnames; i++)
1500 SysFreeString(names[i]);
1501 HeapFree(GetProcessHeap(),0,buf.base);
1502 IRpcChannelBuffer_Release(chanbuf);
1503 ITypeInfo_Release(tinfo);
1504 TRACE("-- 0x%08x\n", hres);
1508 static HRESULT WINAPI ProxyIUnknown_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
1510 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1512 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1514 if (proxy->outerunknown)
1515 return IUnknown_QueryInterface(proxy->outerunknown, riid, ppv);
1517 FIXME("No interface\n");
1518 return E_NOINTERFACE;
1521 static ULONG WINAPI ProxyIUnknown_AddRef(IUnknown *iface)
1523 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1527 if (proxy->outerunknown)
1528 return IUnknown_AddRef(proxy->outerunknown);
1530 return 2; /* FIXME */
1533 static ULONG WINAPI ProxyIUnknown_Release(IUnknown *iface)
1535 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1539 if (proxy->outerunknown)
1540 return IUnknown_Release(proxy->outerunknown);
1542 return 1; /* FIXME */
1545 static HRESULT WINAPI ProxyIDispatch_GetTypeInfoCount(LPDISPATCH iface, UINT * pctinfo)
1547 TMProxyImpl *This = (TMProxyImpl *)iface;
1549 TRACE("(%p)\n", pctinfo);
1551 return IDispatch_GetTypeInfoCount(This->dispatch, pctinfo);
1554 static HRESULT WINAPI ProxyIDispatch_GetTypeInfo(LPDISPATCH iface, UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo)
1556 TMProxyImpl *This = (TMProxyImpl *)iface;
1558 TRACE("(%d, %x, %p)\n", iTInfo, lcid, ppTInfo);
1560 return IDispatch_GetTypeInfo(This->dispatch, iTInfo, lcid, ppTInfo);
1563 static HRESULT WINAPI ProxyIDispatch_GetIDsOfNames(LPDISPATCH iface, REFIID riid, LPOLESTR * rgszNames, UINT cNames, LCID lcid, DISPID * rgDispId)
1565 TMProxyImpl *This = (TMProxyImpl *)iface;
1567 TRACE("(%s, %p, %d, 0x%x, %p)\n", debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);
1569 return IDispatch_GetIDsOfNames(This->dispatch, riid, rgszNames,
1570 cNames, lcid, rgDispId);
1573 static HRESULT WINAPI ProxyIDispatch_Invoke(LPDISPATCH iface, DISPID dispIdMember, REFIID riid, LCID lcid,
1574 WORD wFlags, DISPPARAMS * pDispParams, VARIANT * pVarResult,
1575 EXCEPINFO * pExcepInfo, UINT * puArgErr)
1577 TMProxyImpl *This = (TMProxyImpl *)iface;
1579 TRACE("(%d, %s, 0x%x, 0x%x, %p, %p, %p, %p)\n", dispIdMember,
1580 debugstr_guid(riid), lcid, wFlags, pDispParams, pVarResult,
1581 pExcepInfo, puArgErr);
1583 return IDispatch_Invoke(This->dispatch, dispIdMember, riid, lcid,
1584 wFlags, pDispParams, pVarResult, pExcepInfo,
1590 const IRpcChannelBufferVtbl *lpVtbl;
1592 /* the IDispatch-derived interface we are handling */
1594 IRpcChannelBuffer *pDelegateChannel;
1595 } TMarshalDispatchChannel;
1597 static HRESULT WINAPI TMarshalDispatchChannel_QueryInterface(LPRPCCHANNELBUFFER iface, REFIID riid, LPVOID *ppv)
1600 if (IsEqualIID(riid,&IID_IRpcChannelBuffer) || IsEqualIID(riid,&IID_IUnknown))
1602 *ppv = (LPVOID)iface;
1603 IUnknown_AddRef(iface);
1606 return E_NOINTERFACE;
1609 static ULONG WINAPI TMarshalDispatchChannel_AddRef(LPRPCCHANNELBUFFER iface)
1611 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1612 return InterlockedIncrement(&This->refs);
1615 static ULONG WINAPI TMarshalDispatchChannel_Release(LPRPCCHANNELBUFFER iface)
1617 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1620 ref = InterlockedDecrement(&This->refs);
1624 IRpcChannelBuffer_Release(This->pDelegateChannel);
1625 HeapFree(GetProcessHeap(), 0, This);
1629 static HRESULT WINAPI TMarshalDispatchChannel_GetBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg, REFIID riid)
1631 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1632 TRACE("(%p, %s)\n", olemsg, debugstr_guid(riid));
1633 /* Note: we are pretending to invoke a method on the interface identified
1634 * by tmarshal_iid so that we can re-use the IDispatch proxy/stub code
1635 * without the RPC runtime getting confused by not exporting an IDispatch interface */
1636 return IRpcChannelBuffer_GetBuffer(This->pDelegateChannel, olemsg, &This->tmarshal_iid);
1639 static HRESULT WINAPI TMarshalDispatchChannel_SendReceive(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE *olemsg, ULONG *pstatus)
1641 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1642 TRACE("(%p, %p)\n", olemsg, pstatus);
1643 return IRpcChannelBuffer_SendReceive(This->pDelegateChannel, olemsg, pstatus);
1646 static HRESULT WINAPI TMarshalDispatchChannel_FreeBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg)
1648 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1649 TRACE("(%p)\n", olemsg);
1650 return IRpcChannelBuffer_FreeBuffer(This->pDelegateChannel, olemsg);
1653 static HRESULT WINAPI TMarshalDispatchChannel_GetDestCtx(LPRPCCHANNELBUFFER iface, DWORD* pdwDestContext, void** ppvDestContext)
1655 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1656 TRACE("(%p,%p)\n", pdwDestContext, ppvDestContext);
1657 return IRpcChannelBuffer_GetDestCtx(This->pDelegateChannel, pdwDestContext, ppvDestContext);
1660 static HRESULT WINAPI TMarshalDispatchChannel_IsConnected(LPRPCCHANNELBUFFER iface)
1662 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1664 return IRpcChannelBuffer_IsConnected(This->pDelegateChannel);
1667 static const IRpcChannelBufferVtbl TMarshalDispatchChannelVtbl =
1669 TMarshalDispatchChannel_QueryInterface,
1670 TMarshalDispatchChannel_AddRef,
1671 TMarshalDispatchChannel_Release,
1672 TMarshalDispatchChannel_GetBuffer,
1673 TMarshalDispatchChannel_SendReceive,
1674 TMarshalDispatchChannel_FreeBuffer,
1675 TMarshalDispatchChannel_GetDestCtx,
1676 TMarshalDispatchChannel_IsConnected
1679 static HRESULT TMarshalDispatchChannel_Create(
1680 IRpcChannelBuffer *pDelegateChannel, REFIID tmarshal_riid,
1681 IRpcChannelBuffer **ppChannel)
1683 TMarshalDispatchChannel *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1685 return E_OUTOFMEMORY;
1687 This->lpVtbl = &TMarshalDispatchChannelVtbl;
1689 IRpcChannelBuffer_AddRef(pDelegateChannel);
1690 This->pDelegateChannel = pDelegateChannel;
1691 This->tmarshal_iid = *tmarshal_riid;
1693 *ppChannel = (IRpcChannelBuffer *)&This->lpVtbl;
1698 static inline HRESULT get_facbuf_for_iid(REFIID riid, IPSFactoryBuffer **facbuf)
1703 if ((hr = CoGetPSClsid(riid, &clsid)))
1705 return CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL,
1706 &IID_IPSFactoryBuffer, (LPVOID*)facbuf);
1709 static HRESULT init_proxy_entry_point(TMProxyImpl *proxy, unsigned int num)
1712 /* nrofargs without This */
1715 TMAsmProxy *xasm = proxy->asmstubs + num;
1717 const FUNCDESC *fdesc;
1719 hres = get_funcdesc(proxy->tinfo, num, &tinfo2, &fdesc, NULL, NULL, NULL);
1721 ERR("GetFuncDesc %x should not fail here.\n",hres);
1724 ITypeInfo_Release(tinfo2);
1725 /* some args take more than 4 byte on the stack */
1727 for (j=0;j<fdesc->cParams;j++)
1728 nrofargs += _argsize(fdesc->lprgelemdescParam[j].tdesc.vt);
1731 if (fdesc->callconv != CC_STDCALL) {
1732 ERR("calling convention is not stdcall????\n");
1735 /* popl %eax - return ptr
1742 * arg3 arg2 arg1 <method> <returnptr>
1744 xasm->popleax = 0x58;
1745 xasm->pushlval = 0x68;
1747 xasm->pushleax = 0x50;
1748 xasm->lcall = 0xe8; /* relative jump */
1749 xasm->xcall = (DWORD)xCall;
1750 xasm->xcall -= (DWORD)&(xasm->lret);
1752 xasm->bytestopop = (nrofargs+2)*4; /* pop args, This, iMethod */
1754 proxy->lpvtbl[num] = xasm;
1756 FIXME("not implemented on non i386\n");
1762 static HRESULT WINAPI
1763 PSFacBuf_CreateProxy(
1764 LPPSFACTORYBUFFER iface, IUnknown* pUnkOuter, REFIID riid,
1765 IRpcProxyBuffer **ppProxy, LPVOID *ppv)
1769 unsigned int i, nroffuncs;
1772 BOOL defer_to_dispatch = FALSE;
1774 TRACE("(...%s...)\n",debugstr_guid(riid));
1775 hres = _get_typeinfo_for_iid(riid,&tinfo);
1777 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
1781 hres = num_of_funcs(tinfo, &nroffuncs);
1783 ERR("Cannot get number of functions for typeinfo %s\n",debugstr_guid(riid));
1784 ITypeInfo_Release(tinfo);
1788 proxy = CoTaskMemAlloc(sizeof(TMProxyImpl));
1789 if (!proxy) return E_OUTOFMEMORY;
1791 assert(sizeof(TMAsmProxy) == 16);
1793 proxy->dispatch = NULL;
1794 proxy->dispatch_proxy = NULL;
1795 proxy->outerunknown = pUnkOuter;
1796 proxy->asmstubs = VirtualAlloc(NULL, sizeof(TMAsmProxy) * nroffuncs, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
1797 if (!proxy->asmstubs) {
1798 ERR("Could not commit pages for proxy thunks\n");
1799 CoTaskMemFree(proxy);
1800 return E_OUTOFMEMORY;
1802 proxy->lpvtbl2 = &tmproxyvtable;
1803 /* one reference for the proxy */
1805 proxy->tinfo = tinfo;
1809 InitializeCriticalSection(&proxy->crit);
1810 proxy->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TMProxyImpl.crit");
1812 proxy->lpvtbl = HeapAlloc(GetProcessHeap(),0,sizeof(LPBYTE)*nroffuncs);
1814 /* if we derive from IDispatch then defer to its proxy for its methods */
1815 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
1818 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
1820 IPSFactoryBuffer *factory_buffer;
1821 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
1824 hres = IPSFactoryBuffer_CreateProxy(factory_buffer, NULL,
1825 &IID_IDispatch, &proxy->dispatch_proxy,
1826 (void **)&proxy->dispatch);
1827 IPSFactoryBuffer_Release(factory_buffer);
1829 if ((hres == S_OK) && (nroffuncs < 7))
1831 ERR("nroffuncs calculated incorrectly (%d)\n", nroffuncs);
1832 hres = E_UNEXPECTED;
1836 defer_to_dispatch = TRUE;
1839 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
1842 for (i=0;i<nroffuncs;i++) {
1845 proxy->lpvtbl[i] = ProxyIUnknown_QueryInterface;
1848 proxy->lpvtbl[i] = ProxyIUnknown_AddRef;
1851 proxy->lpvtbl[i] = ProxyIUnknown_Release;
1854 if(!defer_to_dispatch)
1856 hres = init_proxy_entry_point(proxy, i);
1857 if(FAILED(hres)) return hres;
1859 else proxy->lpvtbl[3] = ProxyIDispatch_GetTypeInfoCount;
1862 if(!defer_to_dispatch)
1864 hres = init_proxy_entry_point(proxy, i);
1865 if(FAILED(hres)) return hres;
1867 else proxy->lpvtbl[4] = ProxyIDispatch_GetTypeInfo;
1870 if(!defer_to_dispatch)
1872 hres = init_proxy_entry_point(proxy, i);
1873 if(FAILED(hres)) return hres;
1875 else proxy->lpvtbl[5] = ProxyIDispatch_GetIDsOfNames;
1878 if(!defer_to_dispatch)
1880 hres = init_proxy_entry_point(proxy, i);
1881 if(FAILED(hres)) return hres;
1883 else proxy->lpvtbl[6] = ProxyIDispatch_Invoke;
1886 hres = init_proxy_entry_point(proxy, i);
1887 if(FAILED(hres)) return hres;
1893 *ppv = (LPVOID)proxy;
1894 *ppProxy = (IRpcProxyBuffer *)&(proxy->lpvtbl2);
1895 IUnknown_AddRef((IUnknown *)*ppv);
1899 TMProxyImpl_Release((IRpcProxyBuffer *)&proxy->lpvtbl2);
1903 typedef struct _TMStubImpl {
1904 const IRpcStubBufferVtbl *lpvtbl;
1910 IRpcStubBuffer *dispatch_stub;
1911 BOOL dispatch_derivative;
1914 static HRESULT WINAPI
1915 TMStubImpl_QueryInterface(LPRPCSTUBBUFFER iface, REFIID riid, LPVOID *ppv)
1917 if (IsEqualIID(riid,&IID_IRpcStubBuffer)||IsEqualIID(riid,&IID_IUnknown)){
1918 *ppv = (LPVOID)iface;
1919 IRpcStubBuffer_AddRef(iface);
1922 FIXME("%s, not supported IID.\n",debugstr_guid(riid));
1923 return E_NOINTERFACE;
1927 TMStubImpl_AddRef(LPRPCSTUBBUFFER iface)
1929 TMStubImpl *This = (TMStubImpl *)iface;
1930 ULONG refCount = InterlockedIncrement(&This->ref);
1932 TRACE("(%p)->(ref before=%u)\n", This, refCount - 1);
1938 TMStubImpl_Release(LPRPCSTUBBUFFER iface)
1940 TMStubImpl *This = (TMStubImpl *)iface;
1941 ULONG refCount = InterlockedDecrement(&This->ref);
1943 TRACE("(%p)->(ref before=%u)\n", This, refCount + 1);
1947 IRpcStubBuffer_Disconnect(iface);
1948 ITypeInfo_Release(This->tinfo);
1949 if (This->dispatch_stub)
1950 IRpcStubBuffer_Release(This->dispatch_stub);
1951 CoTaskMemFree(This);
1956 static HRESULT WINAPI
1957 TMStubImpl_Connect(LPRPCSTUBBUFFER iface, LPUNKNOWN pUnkServer)
1959 TMStubImpl *This = (TMStubImpl *)iface;
1961 TRACE("(%p)->(%p)\n", This, pUnkServer);
1963 IUnknown_AddRef(pUnkServer);
1964 This->pUnk = pUnkServer;
1966 if (This->dispatch_stub)
1967 IRpcStubBuffer_Connect(This->dispatch_stub, pUnkServer);
1973 TMStubImpl_Disconnect(LPRPCSTUBBUFFER iface)
1975 TMStubImpl *This = (TMStubImpl *)iface;
1977 TRACE("(%p)->()\n", This);
1981 IUnknown_Release(This->pUnk);
1985 if (This->dispatch_stub)
1986 IRpcStubBuffer_Disconnect(This->dispatch_stub);
1989 static HRESULT WINAPI
1991 LPRPCSTUBBUFFER iface, RPCOLEMESSAGE* xmsg,IRpcChannelBuffer*rpcchanbuf)
1994 const FUNCDESC *fdesc;
1995 TMStubImpl *This = (TMStubImpl *)iface;
1997 DWORD *args = NULL, res, *xargs, nrofargs;
2002 ITypeInfo *tinfo = NULL;
2006 if (xmsg->iMethod < 3) {
2007 ERR("IUnknown methods cannot be marshaled by the typelib marshaler\n");
2008 return E_UNEXPECTED;
2011 if (This->dispatch_derivative && xmsg->iMethod < sizeof(IDispatchVtbl)/sizeof(void *))
2013 IPSFactoryBuffer *factory_buffer;
2014 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
2017 hres = IPSFactoryBuffer_CreateStub(factory_buffer, &IID_IDispatch,
2018 This->pUnk, &This->dispatch_stub);
2019 IPSFactoryBuffer_Release(factory_buffer);
2023 return IRpcStubBuffer_Invoke(This->dispatch_stub, xmsg, rpcchanbuf);
2026 memset(&buf,0,sizeof(buf));
2027 buf.size = xmsg->cbBuffer;
2028 buf.base = HeapAlloc(GetProcessHeap(), 0, xmsg->cbBuffer);
2029 memcpy(buf.base, xmsg->Buffer, xmsg->cbBuffer);
2032 hres = get_funcdesc(This->tinfo,xmsg->iMethod,&tinfo,&fdesc,&iname,NULL,NULL);
2034 ERR("GetFuncDesc on method %d failed with %x\n",xmsg->iMethod,hres);
2038 if (iname && !lstrcmpW(iname, IDispatchW))
2040 ERR("IDispatch cannot be marshaled by the typelib marshaler\n");
2041 hres = E_UNEXPECTED;
2042 SysFreeString (iname);
2046 if (iname) SysFreeString (iname);
2048 /* Need them for hack below */
2049 memset(names,0,sizeof(names));
2050 ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames);
2051 if (nrofnames > sizeof(names)/sizeof(names[0])) {
2052 ERR("Need more names!\n");
2055 /*dump_FUNCDESC(fdesc);*/
2057 for (i=0;i<fdesc->cParams;i++)
2058 nrofargs += _argsize(fdesc->lprgelemdescParam[i].tdesc.vt);
2059 args = HeapAlloc(GetProcessHeap(),0,(nrofargs+1)*sizeof(DWORD));
2062 hres = E_OUTOFMEMORY;
2066 /* Allocate all stuff used by call. */
2068 for (i=0;i<fdesc->cParams;i++) {
2069 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2071 hres = deserialize_param(
2080 xargs += _argsize(elem->tdesc.vt);
2082 ERR("Failed to deserialize param %s, hres %x\n",relaystr(names[i+1]),hres);
2087 args[0] = (DWORD)This->pUnk;
2092 (*((FARPROC**)args[0]))[fdesc->oVft/4],
2100 DWORD dwExceptionCode = GetExceptionCode();
2101 ERR("invoke call failed with exception 0x%08x (%d)\n", dwExceptionCode, dwExceptionCode);
2102 if (FAILED(dwExceptionCode))
2103 hres = dwExceptionCode;
2105 hres = HRESULT_FROM_WIN32(dwExceptionCode);
2115 for (i=0;i<fdesc->cParams;i++) {
2116 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2117 hres = serialize_param(
2126 xargs += _argsize(elem->tdesc.vt);
2128 ERR("Failed to stuballoc param, hres %x\n",hres);
2133 hres = xbuf_add (&buf, (LPBYTE)&res, sizeof(DWORD));
2138 xmsg->cbBuffer = buf.curoff;
2139 hres = IRpcChannelBuffer_GetBuffer(rpcchanbuf, xmsg, &This->iid);
2141 ERR("IRpcChannelBuffer_GetBuffer failed with error 0x%08x\n", hres);
2144 memcpy(xmsg->Buffer, buf.base, buf.curoff);
2147 for (i = 0; i < nrofnames; i++)
2148 SysFreeString(names[i]);
2150 ITypeInfo_Release(tinfo);
2151 HeapFree(GetProcessHeap(), 0, args);
2153 HeapFree(GetProcessHeap(), 0, buf.base);
2155 TRACE("returning\n");
2159 static LPRPCSTUBBUFFER WINAPI
2160 TMStubImpl_IsIIDSupported(LPRPCSTUBBUFFER iface, REFIID riid) {
2161 FIXME("Huh (%s)?\n",debugstr_guid(riid));
2166 TMStubImpl_CountRefs(LPRPCSTUBBUFFER iface) {
2167 TMStubImpl *This = (TMStubImpl *)iface;
2170 return This->ref; /*FIXME? */
2173 static HRESULT WINAPI
2174 TMStubImpl_DebugServerQueryInterface(LPRPCSTUBBUFFER iface, LPVOID *ppv) {
2179 TMStubImpl_DebugServerRelease(LPRPCSTUBBUFFER iface, LPVOID ppv) {
2183 static const IRpcStubBufferVtbl tmstubvtbl = {
2184 TMStubImpl_QueryInterface,
2188 TMStubImpl_Disconnect,
2190 TMStubImpl_IsIIDSupported,
2191 TMStubImpl_CountRefs,
2192 TMStubImpl_DebugServerQueryInterface,
2193 TMStubImpl_DebugServerRelease
2196 static HRESULT WINAPI
2197 PSFacBuf_CreateStub(
2198 LPPSFACTORYBUFFER iface, REFIID riid,IUnknown *pUnkServer,
2199 IRpcStubBuffer** ppStub
2206 TRACE("(%s,%p,%p)\n",debugstr_guid(riid),pUnkServer,ppStub);
2208 hres = _get_typeinfo_for_iid(riid,&tinfo);
2210 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
2214 stub = CoTaskMemAlloc(sizeof(TMStubImpl));
2216 return E_OUTOFMEMORY;
2217 stub->lpvtbl = &tmstubvtbl;
2219 stub->tinfo = tinfo;
2220 stub->dispatch_stub = NULL;
2221 stub->dispatch_derivative = FALSE;
2223 hres = IRpcStubBuffer_Connect((LPRPCSTUBBUFFER)stub,pUnkServer);
2224 *ppStub = (LPRPCSTUBBUFFER)stub;
2225 TRACE("IRpcStubBuffer: %p\n", stub);
2227 ERR("Connect to pUnkServer failed?\n");
2229 /* if we derive from IDispatch then defer to its stub for some of its methods */
2230 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
2233 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
2234 stub->dispatch_derivative = TRUE;
2235 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
2241 static const IPSFactoryBufferVtbl psfacbufvtbl = {
2242 PSFacBuf_QueryInterface,
2245 PSFacBuf_CreateProxy,
2249 /* This is the whole PSFactoryBuffer object, just the vtableptr */
2250 static const IPSFactoryBufferVtbl *lppsfac = &psfacbufvtbl;
2252 /***********************************************************************
2253 * TMARSHAL_DllGetClassObject
2255 HRESULT TMARSHAL_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
2257 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)) {
2261 return E_NOINTERFACE;