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. */
512 _argsize(TYPEDESC *tdesc, ITypeInfo *tinfo) {
516 return 8/sizeof(DWORD);
518 return sizeof(double)/sizeof(DWORD);
520 return sizeof(CY)/sizeof(DWORD);
522 return sizeof(DATE)/sizeof(DWORD);
524 return (sizeof(DECIMAL)+3)/sizeof(DWORD);
526 return (sizeof(VARIANT)+3)/sizeof(DWORD);
532 /* how much space do we use on the heap (in bytes) */
534 _xsize(const TYPEDESC *td, ITypeInfo *tinfo) {
540 /* FIXME: VT_BOOL should return 2? */
542 return sizeof(VARIANT)+3; /* FIXME: why the +3? */
545 const ARRAYDESC *adesc = td->u.lpadesc;
547 for (i=0;i<adesc->cDims;i++)
548 arrsize *= adesc->rgbounds[i].cElements;
549 return arrsize*_xsize(&adesc->tdescElem, tinfo);
578 TRACE("(tdesc.vt %s)\n",debugstr_vt(tdesc->vt));
581 case VT_EMPTY: /* nothing. empty variant for instance */
588 if (debugout) TRACE_(olerelay)("%x%x\n",arg[0],arg[1]);
590 hres = xbuf_add(buf,(LPBYTE)arg,8);
600 if (debugout) TRACE_(olerelay)("%x\n",*arg);
602 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
607 if (debugout) TRACE_(olerelay)("%04x\n",*arg & 0xffff);
609 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
614 if (debugout) TRACE_(olerelay)("%02x\n",*arg & 0xff);
616 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
620 if (debugout) TRACE_(olerelay)("&0x%x\n",*arg);
622 hres = xbuf_add(buf,(LPBYTE)(DWORD*)*arg,sizeof(DWORD));
623 /* do not dealloc at this time */
627 VARIANT *vt = (VARIANT*)arg;
628 DWORD vttype = V_VT(vt);
630 if (debugout) TRACE_(olerelay)("Vt(%s%s)(",debugstr_vt(vttype),debugstr_vf(vttype));
633 hres = xbuf_add(buf,(LPBYTE)&vttype,sizeof(vttype));
634 if (hres) return hres;
636 /* need to recurse since we need to free the stuff */
637 hres = serialize_param(tinfo,writeit,debugout,dealloc,&tdesc2,(DWORD*)&(V_I4(vt)),buf);
638 if (debugout) TRACE_(olerelay)(")");
641 case VT_BSTR|VT_BYREF: {
642 if (debugout) TRACE_(olerelay)("[byref]'%s'", *(BSTR*)*arg ? relaystr(*((BSTR*)*arg)) : "<bstr NULL>");
644 /* ptr to ptr to magic widestring, basically */
645 BSTR *bstr = (BSTR *) *arg;
648 /* -1 means "null string" which is equivalent to empty string */
650 hres = xbuf_add(buf, (LPBYTE)&len,sizeof(DWORD));
651 if (hres) return hres;
653 len = *((DWORD*)*bstr-1)/sizeof(WCHAR);
654 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
655 if (hres) return hres;
656 hres = xbuf_add(buf,(LPBYTE)*bstr,len * sizeof(WCHAR));
657 if (hres) return hres;
661 if (dealloc && arg) {
662 BSTR *str = *((BSTR **)arg);
671 TRACE_(olerelay)("%s",relaystr((WCHAR*)*arg));
673 TRACE_(olerelay)("<bstr NULL>");
676 BSTR bstr = (BSTR)*arg;
680 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
681 if (hres) return hres;
683 len = *((DWORD*)bstr-1)/sizeof(WCHAR);
684 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
685 if (hres) return hres;
686 hres = xbuf_add(buf,(LPBYTE)bstr,len * sizeof(WCHAR));
687 if (hres) return hres;
692 SysFreeString((BSTR)*arg);
697 BOOL derefhere = TRUE;
699 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
703 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
705 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
708 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
709 switch (tattr->typekind) {
711 if (tattr->tdescAlias.vt == VT_USERDEFINED)
713 DWORD href = tattr->tdescAlias.u.hreftype;
714 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
715 ITypeInfo_Release(tinfo2);
716 hres = ITypeInfo_GetRefTypeInfo(tinfo,href,&tinfo2);
718 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
721 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
722 derefhere = (tattr->typekind != TKIND_DISPATCH && tattr->typekind != TKIND_INTERFACE);
725 case TKIND_ENUM: /* confirmed */
726 case TKIND_RECORD: /* FIXME: mostly untested */
728 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
729 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
733 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
737 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
738 ITypeInfo_Release(tinfo2);
741 if (debugout) TRACE_(olerelay)("*");
742 /* Write always, so the other side knows when it gets a NULL pointer.
744 cookie = *arg ? 0x42424242 : 0;
745 hres = xbuf_add(buf,(LPBYTE)&cookie,sizeof(cookie));
749 if (debugout) TRACE_(olerelay)("NULL");
752 hres = serialize_param(tinfo,writeit,debugout,dealloc,tdesc->u.lptdesc,(DWORD*)*arg,buf);
753 if (derefhere && dealloc) HeapFree(GetProcessHeap(),0,(LPVOID)*arg);
757 if (debugout) TRACE_(olerelay)("unk(0x%x)",*arg);
759 hres = _marshal_interface(buf,&IID_IUnknown,(LPUNKNOWN)*arg);
760 if (dealloc && *(IUnknown **)arg)
761 IUnknown_Release((LPUNKNOWN)*arg);
764 if (debugout) TRACE_(olerelay)("idisp(0x%x)",*arg);
766 hres = _marshal_interface(buf,&IID_IDispatch,(LPUNKNOWN)*arg);
767 if (dealloc && *(IUnknown **)arg)
768 IUnknown_Release((LPUNKNOWN)*arg);
771 if (debugout) TRACE_(olerelay)("<void>");
773 case VT_USERDEFINED: {
777 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
779 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
782 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
783 switch (tattr->typekind) {
785 case TKIND_INTERFACE:
787 hres=_marshal_interface(buf,&(tattr->guid),(LPUNKNOWN)arg);
789 IUnknown_Release((LPUNKNOWN)arg);
793 if (debugout) TRACE_(olerelay)("{");
794 for (i=0;i<tattr->cVars;i++) {
799 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
801 ERR("Could not get vardesc of %d\n",i);
804 elem2 = &vdesc->elemdescVar;
805 tdesc2 = &elem2->tdesc;
806 hres = serialize_param(
812 (DWORD*)(((LPBYTE)arg)+vdesc->u.oInst),
815 ITypeInfo_ReleaseVarDesc(tinfo2, vdesc);
818 if (debugout && (i<(tattr->cVars-1)))
819 TRACE_(olerelay)(",");
821 if (debugout) TRACE_(olerelay)("}");
825 hres = serialize_param(tinfo2,writeit,debugout,dealloc,&tattr->tdescAlias,arg,buf);
829 if (debugout) TRACE_(olerelay)("%x",*arg);
831 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
834 FIXME("Unhandled typekind %d\n",tattr->typekind);
838 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
839 ITypeInfo_Release(tinfo2);
843 ARRAYDESC *adesc = tdesc->u.lpadesc;
846 if (debugout) TRACE_(olerelay)("carr");
847 for (i=0;i<adesc->cDims;i++) {
848 if (debugout) TRACE_(olerelay)("[%d]",adesc->rgbounds[i].cElements);
849 arrsize *= adesc->rgbounds[i].cElements;
851 if (debugout) TRACE_(olerelay)("(vt %s)",debugstr_vt(adesc->tdescElem.vt));
852 if (debugout) TRACE_(olerelay)("[");
853 for (i=0;i<arrsize;i++) {
854 hres = serialize_param(tinfo, writeit, debugout, dealloc, &adesc->tdescElem, (DWORD*)((LPBYTE)arg+i*_xsize(&adesc->tdescElem, tinfo)), buf);
857 if (debugout && (i<arrsize-1)) TRACE_(olerelay)(",");
859 if (debugout) TRACE_(olerelay)("]");
865 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
866 ULONG size = LPSAFEARRAY_UserSize(&flags, buf->curoff, (LPSAFEARRAY *)arg);
867 xbuf_resize(buf, size);
868 LPSAFEARRAY_UserMarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
874 ERR("Unhandled marshal type %d.\n",tdesc->vt);
891 TRACE("vt %s at %p\n",debugstr_vt(tdesc->vt),arg);
896 if (debugout) TRACE_(olerelay)("<empty>\n");
899 if (debugout) TRACE_(olerelay)("<null>\n");
902 VARIANT *vt = (VARIANT*)arg;
907 hres = xbuf_get(buf,(LPBYTE)&vttype,sizeof(vttype));
909 FIXME("vt type not read?\n");
912 memset(&tdesc2,0,sizeof(tdesc2));
915 if (debugout) TRACE_(olerelay)("Vt(%s%s)(",debugstr_vt(vttype),debugstr_vf(vttype));
916 hres = deserialize_param(tinfo, readit, debugout, alloc, &tdesc2, (DWORD*)&(V_I4(vt)), buf);
917 TRACE_(olerelay)(")");
929 hres = xbuf_get(buf,(LPBYTE)arg,8);
930 if (hres) ERR("Failed to read integer 8 byte\n");
932 if (debugout) TRACE_(olerelay)("%x%x",arg[0],arg[1]);
942 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
943 if (hres) ERR("Failed to read integer 4 byte\n");
945 if (debugout) TRACE_(olerelay)("%x",*arg);
951 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
952 if (hres) ERR("Failed to read integer 4 byte\n");
955 if (debugout) TRACE_(olerelay)("%04x",*arg & 0xffff);
961 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
962 if (hres) ERR("Failed to read integer 4 byte\n");
965 if (debugout) TRACE_(olerelay)("%02x",*arg & 0xff);
970 *arg = (DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
972 hres = xbuf_get(buf,(LPBYTE)*arg,sizeof(DWORD));
973 if (hres) ERR("Failed to read integer 4 byte\n");
975 if (debugout) TRACE_(olerelay)("&0x%x",*(DWORD*)*arg);
977 case VT_BSTR|VT_BYREF: {
978 BSTR **bstr = (BSTR **)arg;
983 hres = xbuf_get(buf,(LPBYTE)&len,sizeof(DWORD));
985 ERR("failed to read bstr klen\n");
989 *bstr = CoTaskMemAlloc(sizeof(BSTR *));
991 if (debugout) TRACE_(olerelay)("<bstr NULL>");
993 str = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(len+1)*sizeof(WCHAR));
994 hres = xbuf_get(buf,(LPBYTE)str,len*sizeof(WCHAR));
996 ERR("Failed to read BSTR.\n");
997 HeapFree(GetProcessHeap(),0,str);
1000 *bstr = CoTaskMemAlloc(sizeof(BSTR *));
1001 **bstr = SysAllocStringLen(str,len);
1002 if (debugout) TRACE_(olerelay)("%s",relaystr(str));
1003 HeapFree(GetProcessHeap(),0,str);
1015 hres = xbuf_get(buf,(LPBYTE)&len,sizeof(DWORD));
1017 ERR("failed to read bstr klen\n");
1022 if (debugout) TRACE_(olerelay)("<bstr NULL>");
1024 str = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(len+1)*sizeof(WCHAR));
1025 hres = xbuf_get(buf,(LPBYTE)str,len*sizeof(WCHAR));
1027 ERR("Failed to read BSTR.\n");
1028 HeapFree(GetProcessHeap(),0,str);
1031 *arg = (DWORD)SysAllocStringLen(str,len);
1032 if (debugout) TRACE_(olerelay)("%s",relaystr(str));
1033 HeapFree(GetProcessHeap(),0,str);
1042 BOOL derefhere = TRUE;
1044 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
1048 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
1050 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
1053 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1054 switch (tattr->typekind) {
1056 if (tattr->tdescAlias.vt == VT_USERDEFINED)
1058 DWORD href = tattr->tdescAlias.u.hreftype;
1059 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
1060 ITypeInfo_Release(tinfo2);
1061 hres = ITypeInfo_GetRefTypeInfo(tinfo,href,&tinfo2);
1063 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
1066 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1067 derefhere = (tattr->typekind != TKIND_DISPATCH && tattr->typekind != TKIND_INTERFACE);
1070 case TKIND_ENUM: /* confirmed */
1071 case TKIND_RECORD: /* FIXME: mostly untested */
1073 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
1074 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
1078 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
1082 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1083 ITypeInfo_Release(tinfo2);
1085 /* read it in all cases, we need to know if we have
1086 * NULL pointer or not.
1088 hres = xbuf_get(buf,(LPBYTE)&cookie,sizeof(cookie));
1090 ERR("Failed to load pointer cookie.\n");
1093 if (cookie != 0x42424242) {
1094 /* we read a NULL ptr from the remote side */
1095 if (debugout) TRACE_(olerelay)("NULL");
1099 if (debugout) TRACE_(olerelay)("*");
1101 /* Allocate space for the referenced struct */
1103 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,_xsize(tdesc->u.lptdesc, tinfo));
1106 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, (LPDWORD)*arg, buf);
1108 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, arg, buf);
1111 /* FIXME: UNKNOWN is unknown ..., but allocate 4 byte for it */
1113 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
1116 hres = _unmarshal_interface(buf,&IID_IUnknown,(LPUNKNOWN*)arg);
1118 TRACE_(olerelay)("unk(%p)",arg);
1123 hres = _unmarshal_interface(buf,&IID_IDispatch,(LPUNKNOWN*)arg);
1125 TRACE_(olerelay)("idisp(%p)",arg);
1128 if (debugout) TRACE_(olerelay)("<void>");
1130 case VT_USERDEFINED: {
1134 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
1136 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
1139 hres = ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1141 ERR("Could not get typeattr in VT_USERDEFINED.\n");
1143 switch (tattr->typekind) {
1144 case TKIND_DISPATCH:
1145 case TKIND_INTERFACE:
1147 hres = _unmarshal_interface(buf,&(tattr->guid),(LPUNKNOWN*)arg);
1149 case TKIND_RECORD: {
1153 *arg = (DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,tattr->cbSizeInstance);
1155 if (debugout) TRACE_(olerelay)("{");
1156 for (i=0;i<tattr->cVars;i++) {
1159 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
1161 ERR("Could not get vardesc of %d\n",i);
1162 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1163 ITypeInfo_Release(tinfo2);
1166 hres = deserialize_param(
1171 &vdesc->elemdescVar.tdesc,
1172 (DWORD*)(((LPBYTE)*arg)+vdesc->u.oInst),
1175 ITypeInfo2_ReleaseVarDesc(tinfo2, vdesc);
1176 if (debugout && (i<tattr->cVars-1)) TRACE_(olerelay)(",");
1178 if (debugout) TRACE_(olerelay)("}");
1182 hres = deserialize_param(tinfo2,readit,debugout,alloc,&tattr->tdescAlias,arg,buf);
1186 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
1187 if (hres) ERR("Failed to read enum (4 byte)\n");
1189 if (debugout) TRACE_(olerelay)("%x",*arg);
1192 ERR("Unhandled typekind %d\n",tattr->typekind);
1196 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1199 ERR("failed to stuballoc in TKIND_RECORD.\n");
1200 ITypeInfo_Release(tinfo2);
1204 /* arg is pointing to the start of the array. */
1205 ARRAYDESC *adesc = tdesc->u.lpadesc;
1208 if (adesc->cDims > 1) FIXME("cDims > 1 in VT_CARRAY. Does it work?\n");
1209 for (i=0;i<adesc->cDims;i++)
1210 arrsize *= adesc->rgbounds[i].cElements;
1211 for (i=0;i<arrsize;i++)
1218 (DWORD*)((LPBYTE)(arg)+i*_xsize(&adesc->tdescElem, tinfo)),
1223 case VT_SAFEARRAY: {
1226 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
1227 unsigned char *buffer;
1228 buffer = LPSAFEARRAY_UserUnmarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
1229 buf->curoff = buffer - buf->base;
1234 ERR("No handler for VT type %d!\n",tdesc->vt);
1240 /* Retrieves a function's funcdesc, searching back into inherited interfaces. */
1241 static HRESULT get_funcdesc(ITypeInfo *tinfo, int iMethod, ITypeInfo **tactual, const FUNCDESC **fdesc,
1242 BSTR *iname, BSTR *fname, UINT *num)
1246 UINT inherited_funcs = 0;
1249 if (fname) *fname = NULL;
1250 if (iname) *iname = NULL;
1254 hr = ITypeInfo_GetTypeAttr(tinfo, &attr);
1257 ERR("GetTypeAttr failed with %x\n",hr);
1261 if(attr->typekind == TKIND_DISPATCH)
1263 if(attr->wTypeFlags & TYPEFLAG_FDUAL)
1268 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, -1, &href);
1271 ERR("Cannot get interface href from dual dispinterface\n");
1272 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1275 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
1278 ERR("Cannot get interface from dual dispinterface\n");
1279 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1282 hr = get_funcdesc(tinfo2, iMethod, tactual, fdesc, iname, fname, num);
1283 ITypeInfo_Release(tinfo2);
1284 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1287 ERR("Shouldn't be called with a non-dual dispinterface\n");
1291 impl_types = attr->cImplTypes;
1292 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1294 for (i = 0; i < impl_types; i++)
1297 ITypeInfo *pSubTypeInfo;
1300 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, i, &href);
1301 if (FAILED(hr)) return hr;
1302 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &pSubTypeInfo);
1303 if (FAILED(hr)) return hr;
1305 hr = get_funcdesc(pSubTypeInfo, iMethod, tactual, fdesc, iname, fname, &sub_funcs);
1306 inherited_funcs += sub_funcs;
1307 ITypeInfo_Release(pSubTypeInfo);
1308 if(SUCCEEDED(hr)) return hr;
1310 if(iMethod < inherited_funcs)
1312 ERR("shouldn't be here\n");
1313 return E_INVALIDARG;
1316 for(i = inherited_funcs; i <= iMethod; i++)
1318 hr = ITypeInfoImpl_GetInternalFuncDesc(tinfo, i - inherited_funcs, fdesc);
1326 /* found it. We don't care about num so zero it */
1329 ITypeInfo_AddRef(*tactual);
1330 if (fname) ITypeInfo_GetDocumentation(tinfo,(*fdesc)->memid,fname,NULL,NULL,NULL);
1331 if (iname) ITypeInfo_GetDocumentation(tinfo,-1,iname,NULL,NULL,NULL);
1335 static inline BOOL is_in_elem(const ELEMDESC *elem)
1337 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN || !elem->u.paramdesc.wParamFlags);
1340 static inline BOOL is_out_elem(const ELEMDESC *elem)
1342 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT || !elem->u.paramdesc.wParamFlags);
1346 xCall(LPVOID retptr, int method, TMProxyImpl *tpinfo /*, args */)
1348 DWORD *args = ((DWORD*)&tpinfo)+1, *xargs;
1349 const FUNCDESC *fdesc;
1351 int i, relaydeb = TRACE_ON(olerelay);
1358 DWORD remoteresult = 0;
1360 IRpcChannelBuffer *chanbuf;
1362 EnterCriticalSection(&tpinfo->crit);
1364 hres = get_funcdesc(tpinfo->tinfo,method,&tinfo,&fdesc,&iname,&fname,NULL);
1366 ERR("Did not find typeinfo/funcdesc entry for method %d!\n",method);
1367 LeaveCriticalSection(&tpinfo->crit);
1371 if (!tpinfo->chanbuf)
1373 WARN("Tried to use disconnected proxy\n");
1374 ITypeInfo_Release(tinfo);
1375 LeaveCriticalSection(&tpinfo->crit);
1376 return RPC_E_DISCONNECTED;
1378 chanbuf = tpinfo->chanbuf;
1379 IRpcChannelBuffer_AddRef(chanbuf);
1381 LeaveCriticalSection(&tpinfo->crit);
1384 TRACE_(olerelay)("->");
1386 TRACE_(olerelay)("%s:",relaystr(iname));
1388 TRACE_(olerelay)("%s(%d)",relaystr(fname),method);
1390 TRACE_(olerelay)("%d",method);
1391 TRACE_(olerelay)("(");
1394 if (iname) SysFreeString(iname);
1395 if (fname) SysFreeString(fname);
1397 memset(&buf,0,sizeof(buf));
1399 /* normal typelib driven serializing */
1401 /* Need them for hack below */
1402 memset(names,0,sizeof(names));
1403 if (ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames))
1405 if (nrofnames > sizeof(names)/sizeof(names[0]))
1406 ERR("Need more names!\n");
1409 for (i=0;i<fdesc->cParams;i++) {
1410 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1412 if (i) TRACE_(olerelay)(",");
1413 if (i+1<nrofnames && names[i+1])
1414 TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1416 /* No need to marshal other data than FIN and any VT_PTR. */
1417 if (!is_in_elem(elem) && (elem->tdesc.vt != VT_PTR)) {
1418 xargs+=_argsize(&elem->tdesc, tinfo);
1419 if (relaydeb) TRACE_(olerelay)("[out]");
1422 hres = serialize_param(
1433 ERR("Failed to serialize param, hres %x\n",hres);
1436 xargs+=_argsize(&elem->tdesc, tinfo);
1438 if (relaydeb) TRACE_(olerelay)(")");
1440 memset(&msg,0,sizeof(msg));
1441 msg.cbBuffer = buf.curoff;
1442 msg.iMethod = method;
1443 hres = IRpcChannelBuffer_GetBuffer(chanbuf,&msg,&(tpinfo->iid));
1445 ERR("RpcChannelBuffer GetBuffer failed, %x\n",hres);
1448 memcpy(msg.Buffer,buf.base,buf.curoff);
1449 if (relaydeb) TRACE_(olerelay)("\n");
1450 hres = IRpcChannelBuffer_SendReceive(chanbuf,&msg,&status);
1452 ERR("RpcChannelBuffer SendReceive failed, %x\n",hres);
1456 if (relaydeb) TRACE_(olerelay)(" status = %08x (",status);
1458 buf.base = HeapReAlloc(GetProcessHeap(),0,buf.base,msg.cbBuffer);
1460 buf.base = HeapAlloc(GetProcessHeap(),0,msg.cbBuffer);
1461 buf.size = msg.cbBuffer;
1462 memcpy(buf.base,msg.Buffer,buf.size);
1465 /* generic deserializer using typelib description */
1468 for (i=0;i<fdesc->cParams;i++) {
1469 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1472 if (i) TRACE_(olerelay)(",");
1473 if (i+1<nrofnames && names[i+1]) TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1475 /* No need to marshal other data than FOUT and any VT_PTR */
1476 if (!is_out_elem(elem) && (elem->tdesc.vt != VT_PTR)) {
1477 xargs += _argsize(&elem->tdesc, tinfo);
1478 if (relaydeb) TRACE_(olerelay)("[in]");
1481 hres = deserialize_param(
1491 ERR("Failed to unmarshall param, hres %x\n",hres);
1495 xargs += _argsize(&elem->tdesc, tinfo);
1498 hres = xbuf_get(&buf, (LPBYTE)&remoteresult, sizeof(DWORD));
1501 if (relaydeb) TRACE_(olerelay)(") = %08x\n", remoteresult);
1503 hres = remoteresult;
1506 IRpcChannelBuffer_FreeBuffer(chanbuf,&msg);
1507 for (i = 0; i < nrofnames; i++)
1508 SysFreeString(names[i]);
1509 HeapFree(GetProcessHeap(),0,buf.base);
1510 IRpcChannelBuffer_Release(chanbuf);
1511 ITypeInfo_Release(tinfo);
1512 TRACE("-- 0x%08x\n", hres);
1516 static HRESULT WINAPI ProxyIUnknown_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
1518 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1520 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1522 if (proxy->outerunknown)
1523 return IUnknown_QueryInterface(proxy->outerunknown, riid, ppv);
1525 FIXME("No interface\n");
1526 return E_NOINTERFACE;
1529 static ULONG WINAPI ProxyIUnknown_AddRef(IUnknown *iface)
1531 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1535 if (proxy->outerunknown)
1536 return IUnknown_AddRef(proxy->outerunknown);
1538 return 2; /* FIXME */
1541 static ULONG WINAPI ProxyIUnknown_Release(IUnknown *iface)
1543 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1547 if (proxy->outerunknown)
1548 return IUnknown_Release(proxy->outerunknown);
1550 return 1; /* FIXME */
1553 static HRESULT WINAPI ProxyIDispatch_GetTypeInfoCount(LPDISPATCH iface, UINT * pctinfo)
1555 TMProxyImpl *This = (TMProxyImpl *)iface;
1557 TRACE("(%p)\n", pctinfo);
1559 return IDispatch_GetTypeInfoCount(This->dispatch, pctinfo);
1562 static HRESULT WINAPI ProxyIDispatch_GetTypeInfo(LPDISPATCH iface, UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo)
1564 TMProxyImpl *This = (TMProxyImpl *)iface;
1566 TRACE("(%d, %x, %p)\n", iTInfo, lcid, ppTInfo);
1568 return IDispatch_GetTypeInfo(This->dispatch, iTInfo, lcid, ppTInfo);
1571 static HRESULT WINAPI ProxyIDispatch_GetIDsOfNames(LPDISPATCH iface, REFIID riid, LPOLESTR * rgszNames, UINT cNames, LCID lcid, DISPID * rgDispId)
1573 TMProxyImpl *This = (TMProxyImpl *)iface;
1575 TRACE("(%s, %p, %d, 0x%x, %p)\n", debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);
1577 return IDispatch_GetIDsOfNames(This->dispatch, riid, rgszNames,
1578 cNames, lcid, rgDispId);
1581 static HRESULT WINAPI ProxyIDispatch_Invoke(LPDISPATCH iface, DISPID dispIdMember, REFIID riid, LCID lcid,
1582 WORD wFlags, DISPPARAMS * pDispParams, VARIANT * pVarResult,
1583 EXCEPINFO * pExcepInfo, UINT * puArgErr)
1585 TMProxyImpl *This = (TMProxyImpl *)iface;
1587 TRACE("(%d, %s, 0x%x, 0x%x, %p, %p, %p, %p)\n", dispIdMember,
1588 debugstr_guid(riid), lcid, wFlags, pDispParams, pVarResult,
1589 pExcepInfo, puArgErr);
1591 return IDispatch_Invoke(This->dispatch, dispIdMember, riid, lcid,
1592 wFlags, pDispParams, pVarResult, pExcepInfo,
1598 const IRpcChannelBufferVtbl *lpVtbl;
1600 /* the IDispatch-derived interface we are handling */
1602 IRpcChannelBuffer *pDelegateChannel;
1603 } TMarshalDispatchChannel;
1605 static HRESULT WINAPI TMarshalDispatchChannel_QueryInterface(LPRPCCHANNELBUFFER iface, REFIID riid, LPVOID *ppv)
1608 if (IsEqualIID(riid,&IID_IRpcChannelBuffer) || IsEqualIID(riid,&IID_IUnknown))
1610 *ppv = (LPVOID)iface;
1611 IUnknown_AddRef(iface);
1614 return E_NOINTERFACE;
1617 static ULONG WINAPI TMarshalDispatchChannel_AddRef(LPRPCCHANNELBUFFER iface)
1619 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1620 return InterlockedIncrement(&This->refs);
1623 static ULONG WINAPI TMarshalDispatchChannel_Release(LPRPCCHANNELBUFFER iface)
1625 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1628 ref = InterlockedDecrement(&This->refs);
1632 IRpcChannelBuffer_Release(This->pDelegateChannel);
1633 HeapFree(GetProcessHeap(), 0, This);
1637 static HRESULT WINAPI TMarshalDispatchChannel_GetBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg, REFIID riid)
1639 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1640 TRACE("(%p, %s)\n", olemsg, debugstr_guid(riid));
1641 /* Note: we are pretending to invoke a method on the interface identified
1642 * by tmarshal_iid so that we can re-use the IDispatch proxy/stub code
1643 * without the RPC runtime getting confused by not exporting an IDispatch interface */
1644 return IRpcChannelBuffer_GetBuffer(This->pDelegateChannel, olemsg, &This->tmarshal_iid);
1647 static HRESULT WINAPI TMarshalDispatchChannel_SendReceive(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE *olemsg, ULONG *pstatus)
1649 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1650 TRACE("(%p, %p)\n", olemsg, pstatus);
1651 return IRpcChannelBuffer_SendReceive(This->pDelegateChannel, olemsg, pstatus);
1654 static HRESULT WINAPI TMarshalDispatchChannel_FreeBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg)
1656 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1657 TRACE("(%p)\n", olemsg);
1658 return IRpcChannelBuffer_FreeBuffer(This->pDelegateChannel, olemsg);
1661 static HRESULT WINAPI TMarshalDispatchChannel_GetDestCtx(LPRPCCHANNELBUFFER iface, DWORD* pdwDestContext, void** ppvDestContext)
1663 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1664 TRACE("(%p,%p)\n", pdwDestContext, ppvDestContext);
1665 return IRpcChannelBuffer_GetDestCtx(This->pDelegateChannel, pdwDestContext, ppvDestContext);
1668 static HRESULT WINAPI TMarshalDispatchChannel_IsConnected(LPRPCCHANNELBUFFER iface)
1670 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1672 return IRpcChannelBuffer_IsConnected(This->pDelegateChannel);
1675 static const IRpcChannelBufferVtbl TMarshalDispatchChannelVtbl =
1677 TMarshalDispatchChannel_QueryInterface,
1678 TMarshalDispatchChannel_AddRef,
1679 TMarshalDispatchChannel_Release,
1680 TMarshalDispatchChannel_GetBuffer,
1681 TMarshalDispatchChannel_SendReceive,
1682 TMarshalDispatchChannel_FreeBuffer,
1683 TMarshalDispatchChannel_GetDestCtx,
1684 TMarshalDispatchChannel_IsConnected
1687 static HRESULT TMarshalDispatchChannel_Create(
1688 IRpcChannelBuffer *pDelegateChannel, REFIID tmarshal_riid,
1689 IRpcChannelBuffer **ppChannel)
1691 TMarshalDispatchChannel *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1693 return E_OUTOFMEMORY;
1695 This->lpVtbl = &TMarshalDispatchChannelVtbl;
1697 IRpcChannelBuffer_AddRef(pDelegateChannel);
1698 This->pDelegateChannel = pDelegateChannel;
1699 This->tmarshal_iid = *tmarshal_riid;
1701 *ppChannel = (IRpcChannelBuffer *)&This->lpVtbl;
1706 static inline HRESULT get_facbuf_for_iid(REFIID riid, IPSFactoryBuffer **facbuf)
1711 if ((hr = CoGetPSClsid(riid, &clsid)))
1713 return CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL,
1714 &IID_IPSFactoryBuffer, (LPVOID*)facbuf);
1717 static HRESULT init_proxy_entry_point(TMProxyImpl *proxy, unsigned int num)
1720 /* nrofargs without This */
1723 TMAsmProxy *xasm = proxy->asmstubs + num;
1725 const FUNCDESC *fdesc;
1727 hres = get_funcdesc(proxy->tinfo, num, &tinfo2, &fdesc, NULL, NULL, NULL);
1729 ERR("GetFuncDesc %x should not fail here.\n",hres);
1732 ITypeInfo_Release(tinfo2);
1733 /* some args take more than 4 byte on the stack */
1735 for (j=0;j<fdesc->cParams;j++)
1736 nrofargs += _argsize(&fdesc->lprgelemdescParam[j].tdesc, proxy->tinfo);
1739 if (fdesc->callconv != CC_STDCALL) {
1740 ERR("calling convention is not stdcall????\n");
1743 /* popl %eax - return ptr
1750 * arg3 arg2 arg1 <method> <returnptr>
1752 xasm->popleax = 0x58;
1753 xasm->pushlval = 0x68;
1755 xasm->pushleax = 0x50;
1756 xasm->lcall = 0xe8; /* relative jump */
1757 xasm->xcall = (DWORD)xCall;
1758 xasm->xcall -= (DWORD)&(xasm->lret);
1760 xasm->bytestopop = (nrofargs+2)*4; /* pop args, This, iMethod */
1762 proxy->lpvtbl[num] = xasm;
1764 FIXME("not implemented on non i386\n");
1770 static HRESULT WINAPI
1771 PSFacBuf_CreateProxy(
1772 LPPSFACTORYBUFFER iface, IUnknown* pUnkOuter, REFIID riid,
1773 IRpcProxyBuffer **ppProxy, LPVOID *ppv)
1777 unsigned int i, nroffuncs;
1780 BOOL defer_to_dispatch = FALSE;
1782 TRACE("(...%s...)\n",debugstr_guid(riid));
1783 hres = _get_typeinfo_for_iid(riid,&tinfo);
1785 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
1789 hres = num_of_funcs(tinfo, &nroffuncs);
1791 ERR("Cannot get number of functions for typeinfo %s\n",debugstr_guid(riid));
1792 ITypeInfo_Release(tinfo);
1796 proxy = CoTaskMemAlloc(sizeof(TMProxyImpl));
1797 if (!proxy) return E_OUTOFMEMORY;
1799 assert(sizeof(TMAsmProxy) == 16);
1801 proxy->dispatch = NULL;
1802 proxy->dispatch_proxy = NULL;
1803 proxy->outerunknown = pUnkOuter;
1804 proxy->asmstubs = VirtualAlloc(NULL, sizeof(TMAsmProxy) * nroffuncs, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
1805 if (!proxy->asmstubs) {
1806 ERR("Could not commit pages for proxy thunks\n");
1807 CoTaskMemFree(proxy);
1808 return E_OUTOFMEMORY;
1810 proxy->lpvtbl2 = &tmproxyvtable;
1811 /* one reference for the proxy */
1813 proxy->tinfo = tinfo;
1817 InitializeCriticalSection(&proxy->crit);
1818 proxy->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TMProxyImpl.crit");
1820 proxy->lpvtbl = HeapAlloc(GetProcessHeap(),0,sizeof(LPBYTE)*nroffuncs);
1822 /* if we derive from IDispatch then defer to its proxy for its methods */
1823 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
1826 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
1828 IPSFactoryBuffer *factory_buffer;
1829 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
1832 hres = IPSFactoryBuffer_CreateProxy(factory_buffer, NULL,
1833 &IID_IDispatch, &proxy->dispatch_proxy,
1834 (void **)&proxy->dispatch);
1835 IPSFactoryBuffer_Release(factory_buffer);
1837 if ((hres == S_OK) && (nroffuncs < 7))
1839 ERR("nroffuncs calculated incorrectly (%d)\n", nroffuncs);
1840 hres = E_UNEXPECTED;
1844 defer_to_dispatch = TRUE;
1847 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
1850 for (i=0;i<nroffuncs;i++) {
1853 proxy->lpvtbl[i] = ProxyIUnknown_QueryInterface;
1856 proxy->lpvtbl[i] = ProxyIUnknown_AddRef;
1859 proxy->lpvtbl[i] = ProxyIUnknown_Release;
1862 if(!defer_to_dispatch)
1864 hres = init_proxy_entry_point(proxy, i);
1865 if(FAILED(hres)) return hres;
1867 else proxy->lpvtbl[3] = ProxyIDispatch_GetTypeInfoCount;
1870 if(!defer_to_dispatch)
1872 hres = init_proxy_entry_point(proxy, i);
1873 if(FAILED(hres)) return hres;
1875 else proxy->lpvtbl[4] = ProxyIDispatch_GetTypeInfo;
1878 if(!defer_to_dispatch)
1880 hres = init_proxy_entry_point(proxy, i);
1881 if(FAILED(hres)) return hres;
1883 else proxy->lpvtbl[5] = ProxyIDispatch_GetIDsOfNames;
1886 if(!defer_to_dispatch)
1888 hres = init_proxy_entry_point(proxy, i);
1889 if(FAILED(hres)) return hres;
1891 else proxy->lpvtbl[6] = ProxyIDispatch_Invoke;
1894 hres = init_proxy_entry_point(proxy, i);
1895 if(FAILED(hres)) return hres;
1901 *ppv = (LPVOID)proxy;
1902 *ppProxy = (IRpcProxyBuffer *)&(proxy->lpvtbl2);
1903 IUnknown_AddRef((IUnknown *)*ppv);
1907 TMProxyImpl_Release((IRpcProxyBuffer *)&proxy->lpvtbl2);
1911 typedef struct _TMStubImpl {
1912 const IRpcStubBufferVtbl *lpvtbl;
1918 IRpcStubBuffer *dispatch_stub;
1919 BOOL dispatch_derivative;
1922 static HRESULT WINAPI
1923 TMStubImpl_QueryInterface(LPRPCSTUBBUFFER iface, REFIID riid, LPVOID *ppv)
1925 if (IsEqualIID(riid,&IID_IRpcStubBuffer)||IsEqualIID(riid,&IID_IUnknown)){
1926 *ppv = (LPVOID)iface;
1927 IRpcStubBuffer_AddRef(iface);
1930 FIXME("%s, not supported IID.\n",debugstr_guid(riid));
1931 return E_NOINTERFACE;
1935 TMStubImpl_AddRef(LPRPCSTUBBUFFER iface)
1937 TMStubImpl *This = (TMStubImpl *)iface;
1938 ULONG refCount = InterlockedIncrement(&This->ref);
1940 TRACE("(%p)->(ref before=%u)\n", This, refCount - 1);
1946 TMStubImpl_Release(LPRPCSTUBBUFFER iface)
1948 TMStubImpl *This = (TMStubImpl *)iface;
1949 ULONG refCount = InterlockedDecrement(&This->ref);
1951 TRACE("(%p)->(ref before=%u)\n", This, refCount + 1);
1955 IRpcStubBuffer_Disconnect(iface);
1956 ITypeInfo_Release(This->tinfo);
1957 if (This->dispatch_stub)
1958 IRpcStubBuffer_Release(This->dispatch_stub);
1959 CoTaskMemFree(This);
1964 static HRESULT WINAPI
1965 TMStubImpl_Connect(LPRPCSTUBBUFFER iface, LPUNKNOWN pUnkServer)
1967 TMStubImpl *This = (TMStubImpl *)iface;
1969 TRACE("(%p)->(%p)\n", This, pUnkServer);
1971 IUnknown_AddRef(pUnkServer);
1972 This->pUnk = pUnkServer;
1974 if (This->dispatch_stub)
1975 IRpcStubBuffer_Connect(This->dispatch_stub, pUnkServer);
1981 TMStubImpl_Disconnect(LPRPCSTUBBUFFER iface)
1983 TMStubImpl *This = (TMStubImpl *)iface;
1985 TRACE("(%p)->()\n", This);
1989 IUnknown_Release(This->pUnk);
1993 if (This->dispatch_stub)
1994 IRpcStubBuffer_Disconnect(This->dispatch_stub);
1997 static HRESULT WINAPI
1999 LPRPCSTUBBUFFER iface, RPCOLEMESSAGE* xmsg,IRpcChannelBuffer*rpcchanbuf)
2002 const FUNCDESC *fdesc;
2003 TMStubImpl *This = (TMStubImpl *)iface;
2005 DWORD *args = NULL, res, *xargs, nrofargs;
2010 ITypeInfo *tinfo = NULL;
2014 if (xmsg->iMethod < 3) {
2015 ERR("IUnknown methods cannot be marshaled by the typelib marshaler\n");
2016 return E_UNEXPECTED;
2019 if (This->dispatch_derivative && xmsg->iMethod < sizeof(IDispatchVtbl)/sizeof(void *))
2021 IPSFactoryBuffer *factory_buffer;
2022 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
2025 hres = IPSFactoryBuffer_CreateStub(factory_buffer, &IID_IDispatch,
2026 This->pUnk, &This->dispatch_stub);
2027 IPSFactoryBuffer_Release(factory_buffer);
2031 return IRpcStubBuffer_Invoke(This->dispatch_stub, xmsg, rpcchanbuf);
2034 memset(&buf,0,sizeof(buf));
2035 buf.size = xmsg->cbBuffer;
2036 buf.base = HeapAlloc(GetProcessHeap(), 0, xmsg->cbBuffer);
2037 memcpy(buf.base, xmsg->Buffer, xmsg->cbBuffer);
2040 hres = get_funcdesc(This->tinfo,xmsg->iMethod,&tinfo,&fdesc,&iname,NULL,NULL);
2042 ERR("GetFuncDesc on method %d failed with %x\n",xmsg->iMethod,hres);
2046 if (iname && !lstrcmpW(iname, IDispatchW))
2048 ERR("IDispatch cannot be marshaled by the typelib marshaler\n");
2049 hres = E_UNEXPECTED;
2050 SysFreeString (iname);
2054 if (iname) SysFreeString (iname);
2056 /* Need them for hack below */
2057 memset(names,0,sizeof(names));
2058 ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames);
2059 if (nrofnames > sizeof(names)/sizeof(names[0])) {
2060 ERR("Need more names!\n");
2063 /*dump_FUNCDESC(fdesc);*/
2065 for (i=0;i<fdesc->cParams;i++)
2066 nrofargs += _argsize(&fdesc->lprgelemdescParam[i].tdesc, tinfo);
2067 args = HeapAlloc(GetProcessHeap(),0,(nrofargs+1)*sizeof(DWORD));
2070 hres = E_OUTOFMEMORY;
2074 /* Allocate all stuff used by call. */
2076 for (i=0;i<fdesc->cParams;i++) {
2077 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2079 hres = deserialize_param(
2088 xargs += _argsize(&elem->tdesc, tinfo);
2090 ERR("Failed to deserialize param %s, hres %x\n",relaystr(names[i+1]),hres);
2095 args[0] = (DWORD)This->pUnk;
2100 (*((FARPROC**)args[0]))[fdesc->oVft/4],
2108 DWORD dwExceptionCode = GetExceptionCode();
2109 ERR("invoke call failed with exception 0x%08x (%d)\n", dwExceptionCode, dwExceptionCode);
2110 if (FAILED(dwExceptionCode))
2111 hres = dwExceptionCode;
2113 hres = HRESULT_FROM_WIN32(dwExceptionCode);
2123 for (i=0;i<fdesc->cParams;i++) {
2124 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2125 hres = serialize_param(
2134 xargs += _argsize(&elem->tdesc, tinfo);
2136 ERR("Failed to stuballoc param, hres %x\n",hres);
2141 hres = xbuf_add (&buf, (LPBYTE)&res, sizeof(DWORD));
2146 xmsg->cbBuffer = buf.curoff;
2147 hres = IRpcChannelBuffer_GetBuffer(rpcchanbuf, xmsg, &This->iid);
2149 ERR("IRpcChannelBuffer_GetBuffer failed with error 0x%08x\n", hres);
2152 memcpy(xmsg->Buffer, buf.base, buf.curoff);
2155 for (i = 0; i < nrofnames; i++)
2156 SysFreeString(names[i]);
2158 ITypeInfo_Release(tinfo);
2159 HeapFree(GetProcessHeap(), 0, args);
2161 HeapFree(GetProcessHeap(), 0, buf.base);
2163 TRACE("returning\n");
2167 static LPRPCSTUBBUFFER WINAPI
2168 TMStubImpl_IsIIDSupported(LPRPCSTUBBUFFER iface, REFIID riid) {
2169 FIXME("Huh (%s)?\n",debugstr_guid(riid));
2174 TMStubImpl_CountRefs(LPRPCSTUBBUFFER iface) {
2175 TMStubImpl *This = (TMStubImpl *)iface;
2178 return This->ref; /*FIXME? */
2181 static HRESULT WINAPI
2182 TMStubImpl_DebugServerQueryInterface(LPRPCSTUBBUFFER iface, LPVOID *ppv) {
2187 TMStubImpl_DebugServerRelease(LPRPCSTUBBUFFER iface, LPVOID ppv) {
2191 static const IRpcStubBufferVtbl tmstubvtbl = {
2192 TMStubImpl_QueryInterface,
2196 TMStubImpl_Disconnect,
2198 TMStubImpl_IsIIDSupported,
2199 TMStubImpl_CountRefs,
2200 TMStubImpl_DebugServerQueryInterface,
2201 TMStubImpl_DebugServerRelease
2204 static HRESULT WINAPI
2205 PSFacBuf_CreateStub(
2206 LPPSFACTORYBUFFER iface, REFIID riid,IUnknown *pUnkServer,
2207 IRpcStubBuffer** ppStub
2214 TRACE("(%s,%p,%p)\n",debugstr_guid(riid),pUnkServer,ppStub);
2216 hres = _get_typeinfo_for_iid(riid,&tinfo);
2218 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
2222 stub = CoTaskMemAlloc(sizeof(TMStubImpl));
2224 return E_OUTOFMEMORY;
2225 stub->lpvtbl = &tmstubvtbl;
2227 stub->tinfo = tinfo;
2228 stub->dispatch_stub = NULL;
2229 stub->dispatch_derivative = FALSE;
2231 hres = IRpcStubBuffer_Connect((LPRPCSTUBBUFFER)stub,pUnkServer);
2232 *ppStub = (LPRPCSTUBBUFFER)stub;
2233 TRACE("IRpcStubBuffer: %p\n", stub);
2235 ERR("Connect to pUnkServer failed?\n");
2237 /* if we derive from IDispatch then defer to its stub for some of its methods */
2238 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
2241 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
2242 stub->dispatch_derivative = TRUE;
2243 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
2249 static const IPSFactoryBufferVtbl psfacbufvtbl = {
2250 PSFacBuf_QueryInterface,
2253 PSFacBuf_CreateProxy,
2257 /* This is the whole PSFactoryBuffer object, just the vtableptr */
2258 static const IPSFactoryBufferVtbl *lppsfac = &psfacbufvtbl;
2260 /***********************************************************************
2261 * TMARSHAL_DllGetClassObject
2263 HRESULT TMARSHAL_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
2265 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)) {
2269 return E_NOINTERFACE;