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,STATFLAG_NONAME);
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)) {
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\\win%u",tlguid,ver,(sizeof(void*) == 8) ? 64 : 32);
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)) {
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);
534 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
536 return 0; /* should fail critically in serialize_param */
537 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
538 ret = (tattr->cbSizeInstance+3)/sizeof(DWORD);
539 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
540 ITypeInfo_Release(tinfo2);
548 /* how much space do we use on the heap (in bytes) */
550 _xsize(const TYPEDESC *td, ITypeInfo *tinfo) {
556 /* FIXME: VT_BOOL should return 2? */
558 return sizeof(VARIANT)+3; /* FIXME: why the +3? */
561 const ARRAYDESC *adesc = td->u.lpadesc;
563 for (i=0;i<adesc->cDims;i++)
564 arrsize *= adesc->rgbounds[i].cElements;
565 return arrsize*_xsize(&adesc->tdescElem, tinfo);
584 hres = ITypeInfo_GetRefTypeInfo(tinfo,td->u.hreftype,&tinfo2);
587 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
588 ret = tattr->cbSizeInstance;
589 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
590 ITypeInfo_Release(tinfo2);
611 TRACE("(tdesc.vt %s)\n",debugstr_vt(tdesc->vt));
614 if ((vartype & 0xf000) == VT_ARRAY)
615 vartype = VT_SAFEARRAY;
618 case VT_EMPTY: /* nothing. empty variant for instance */
625 if (debugout) TRACE_(olerelay)("%x%x\n",arg[0],arg[1]);
627 hres = xbuf_add(buf,(LPBYTE)arg,8);
637 if (debugout) TRACE_(olerelay)("%x\n",*arg);
639 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
644 if (debugout) TRACE_(olerelay)("%04x\n",*arg & 0xffff);
646 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
651 if (debugout) TRACE_(olerelay)("%02x\n",*arg & 0xff);
653 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
657 if (debugout) TRACE_(olerelay)("&0x%x\n",*arg);
659 hres = xbuf_add(buf,(LPBYTE)(DWORD*)*arg,sizeof(DWORD));
660 /* do not dealloc at this time */
664 VARIANT *vt = (VARIANT*)arg;
665 DWORD vttype = V_VT(vt);
667 if (debugout) TRACE_(olerelay)("Vt(%s%s)(",debugstr_vt(vttype),debugstr_vf(vttype));
670 hres = xbuf_add(buf,(LPBYTE)&vttype,sizeof(vttype));
671 if (hres) return hres;
673 /* need to recurse since we need to free the stuff */
674 hres = serialize_param(tinfo,writeit,debugout,dealloc,&tdesc2,(DWORD*)&(V_I4(vt)),buf);
675 if (debugout) TRACE_(olerelay)(")");
678 case VT_BSTR|VT_BYREF: {
679 if (debugout) TRACE_(olerelay)("[byref]'%s'", *(BSTR*)*arg ? relaystr(*((BSTR*)*arg)) : "<bstr NULL>");
681 /* ptr to ptr to magic widestring, basically */
682 BSTR *bstr = (BSTR *) *arg;
685 /* -1 means "null string" which is equivalent to empty string */
687 hres = xbuf_add(buf, (LPBYTE)&len,sizeof(DWORD));
688 if (hres) return hres;
690 len = *((DWORD*)*bstr-1)/sizeof(WCHAR);
691 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
692 if (hres) return hres;
693 hres = xbuf_add(buf,(LPBYTE)*bstr,len * sizeof(WCHAR));
694 if (hres) return hres;
698 if (dealloc && arg) {
699 BSTR *str = *((BSTR **)arg);
708 TRACE_(olerelay)("%s",relaystr((WCHAR*)*arg));
710 TRACE_(olerelay)("<bstr NULL>");
713 BSTR bstr = (BSTR)*arg;
717 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
718 if (hres) return hres;
720 len = *((DWORD*)bstr-1)/sizeof(WCHAR);
721 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
722 if (hres) return hres;
723 hres = xbuf_add(buf,(LPBYTE)bstr,len * sizeof(WCHAR));
724 if (hres) return hres;
729 SysFreeString((BSTR)*arg);
734 BOOL derefhere = TRUE;
736 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
740 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
742 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
745 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
746 switch (tattr->typekind) {
748 if (tattr->tdescAlias.vt == VT_USERDEFINED)
750 DWORD href = tattr->tdescAlias.u.hreftype;
751 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
752 ITypeInfo_Release(tinfo2);
753 hres = ITypeInfo_GetRefTypeInfo(tinfo,href,&tinfo2);
755 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
758 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
759 derefhere = (tattr->typekind != TKIND_DISPATCH && tattr->typekind != TKIND_INTERFACE);
762 case TKIND_ENUM: /* confirmed */
763 case TKIND_RECORD: /* FIXME: mostly untested */
765 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
766 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
770 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
774 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
775 ITypeInfo_Release(tinfo2);
778 if (debugout) TRACE_(olerelay)("*");
779 /* Write always, so the other side knows when it gets a NULL pointer.
781 cookie = *arg ? 0x42424242 : 0;
782 hres = xbuf_add(buf,(LPBYTE)&cookie,sizeof(cookie));
786 if (debugout) TRACE_(olerelay)("NULL");
789 hres = serialize_param(tinfo,writeit,debugout,dealloc,tdesc->u.lptdesc,(DWORD*)*arg,buf);
790 if (derefhere && dealloc) HeapFree(GetProcessHeap(),0,(LPVOID)*arg);
794 if (debugout) TRACE_(olerelay)("unk(0x%x)",*arg);
796 hres = _marshal_interface(buf,&IID_IUnknown,(LPUNKNOWN)*arg);
797 if (dealloc && *(IUnknown **)arg)
798 IUnknown_Release((LPUNKNOWN)*arg);
801 if (debugout) TRACE_(olerelay)("idisp(0x%x)",*arg);
803 hres = _marshal_interface(buf,&IID_IDispatch,(LPUNKNOWN)*arg);
804 if (dealloc && *(IUnknown **)arg)
805 IUnknown_Release((LPUNKNOWN)*arg);
808 if (debugout) TRACE_(olerelay)("<void>");
810 case VT_USERDEFINED: {
814 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
816 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
819 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
820 switch (tattr->typekind) {
822 case TKIND_INTERFACE:
824 hres=_marshal_interface(buf,&(tattr->guid),(LPUNKNOWN)arg);
826 IUnknown_Release((LPUNKNOWN)arg);
830 if (debugout) TRACE_(olerelay)("{");
831 for (i=0;i<tattr->cVars;i++) {
836 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
838 ERR("Could not get vardesc of %d\n",i);
841 elem2 = &vdesc->elemdescVar;
842 tdesc2 = &elem2->tdesc;
843 hres = serialize_param(
849 (DWORD*)(((LPBYTE)arg)+vdesc->u.oInst),
852 ITypeInfo_ReleaseVarDesc(tinfo2, vdesc);
855 if (debugout && (i<(tattr->cVars-1)))
856 TRACE_(olerelay)(",");
858 if (debugout) TRACE_(olerelay)("}");
862 hres = serialize_param(tinfo2,writeit,debugout,dealloc,&tattr->tdescAlias,arg,buf);
866 if (debugout) TRACE_(olerelay)("%x",*arg);
868 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
871 FIXME("Unhandled typekind %d\n",tattr->typekind);
875 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
876 ITypeInfo_Release(tinfo2);
880 ARRAYDESC *adesc = tdesc->u.lpadesc;
883 if (debugout) TRACE_(olerelay)("carr");
884 for (i=0;i<adesc->cDims;i++) {
885 if (debugout) TRACE_(olerelay)("[%d]",adesc->rgbounds[i].cElements);
886 arrsize *= adesc->rgbounds[i].cElements;
888 if (debugout) TRACE_(olerelay)("(vt %s)",debugstr_vt(adesc->tdescElem.vt));
889 if (debugout) TRACE_(olerelay)("[");
890 for (i=0;i<arrsize;i++) {
891 hres = serialize_param(tinfo, writeit, debugout, dealloc, &adesc->tdescElem, (DWORD*)((LPBYTE)(*arg)+i*_xsize(&adesc->tdescElem, tinfo)), buf);
894 if (debugout && (i<arrsize-1)) TRACE_(olerelay)(",");
896 if (debugout) TRACE_(olerelay)("]");
902 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
903 ULONG size = LPSAFEARRAY_UserSize(&flags, buf->curoff, (LPSAFEARRAY *)arg);
904 xbuf_resize(buf, size);
905 LPSAFEARRAY_UserMarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
911 ERR("Unhandled marshal type %d.\n",tdesc->vt);
929 TRACE("vt %s at %p\n",debugstr_vt(tdesc->vt),arg);
932 if ((vartype & 0xf000) == VT_ARRAY)
933 vartype = VT_SAFEARRAY;
938 if (debugout) TRACE_(olerelay)("<empty>\n");
941 if (debugout) TRACE_(olerelay)("<null>\n");
944 VARIANT *vt = (VARIANT*)arg;
949 hres = xbuf_get(buf,(LPBYTE)&vttype,sizeof(vttype));
951 FIXME("vt type not read?\n");
954 memset(&tdesc2,0,sizeof(tdesc2));
957 if (debugout) TRACE_(olerelay)("Vt(%s%s)(",debugstr_vt(vttype),debugstr_vf(vttype));
958 hres = deserialize_param(tinfo, readit, debugout, alloc, &tdesc2, (DWORD*)&(V_I4(vt)), buf);
959 TRACE_(olerelay)(")");
971 hres = xbuf_get(buf,(LPBYTE)arg,8);
972 if (hres) ERR("Failed to read integer 8 byte\n");
974 if (debugout) TRACE_(olerelay)("%x%x",arg[0],arg[1]);
984 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
985 if (hres) ERR("Failed to read integer 4 byte\n");
987 if (debugout) TRACE_(olerelay)("%x",*arg);
993 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
994 if (hres) ERR("Failed to read integer 4 byte\n");
997 if (debugout) TRACE_(olerelay)("%04x",*arg & 0xffff);
1003 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
1004 if (hres) ERR("Failed to read integer 4 byte\n");
1007 if (debugout) TRACE_(olerelay)("%02x",*arg & 0xff);
1009 case VT_I4|VT_BYREF:
1012 *arg = (DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
1014 hres = xbuf_get(buf,(LPBYTE)*arg,sizeof(DWORD));
1015 if (hres) ERR("Failed to read integer 4 byte\n");
1017 if (debugout) TRACE_(olerelay)("&0x%x",*(DWORD*)*arg);
1019 case VT_BSTR|VT_BYREF: {
1020 BSTR **bstr = (BSTR **)arg;
1025 hres = xbuf_get(buf,(LPBYTE)&len,sizeof(DWORD));
1027 ERR("failed to read bstr klen\n");
1031 *bstr = CoTaskMemAlloc(sizeof(BSTR *));
1033 if (debugout) TRACE_(olerelay)("<bstr NULL>");
1035 str = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(len+1)*sizeof(WCHAR));
1036 hres = xbuf_get(buf,(LPBYTE)str,len*sizeof(WCHAR));
1038 ERR("Failed to read BSTR.\n");
1039 HeapFree(GetProcessHeap(),0,str);
1042 *bstr = CoTaskMemAlloc(sizeof(BSTR *));
1043 **bstr = SysAllocStringLen(str,len);
1044 if (debugout) TRACE_(olerelay)("%s",relaystr(str));
1045 HeapFree(GetProcessHeap(),0,str);
1057 hres = xbuf_get(buf,(LPBYTE)&len,sizeof(DWORD));
1059 ERR("failed to read bstr klen\n");
1064 if (debugout) TRACE_(olerelay)("<bstr NULL>");
1066 str = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(len+1)*sizeof(WCHAR));
1067 hres = xbuf_get(buf,(LPBYTE)str,len*sizeof(WCHAR));
1069 ERR("Failed to read BSTR.\n");
1070 HeapFree(GetProcessHeap(),0,str);
1073 *arg = (DWORD)SysAllocStringLen(str,len);
1074 if (debugout) TRACE_(olerelay)("%s",relaystr(str));
1075 HeapFree(GetProcessHeap(),0,str);
1084 BOOL derefhere = TRUE;
1086 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
1090 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
1092 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
1095 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1096 switch (tattr->typekind) {
1098 if (tattr->tdescAlias.vt == VT_USERDEFINED)
1100 DWORD href = tattr->tdescAlias.u.hreftype;
1101 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
1102 ITypeInfo_Release(tinfo2);
1103 hres = ITypeInfo_GetRefTypeInfo(tinfo,href,&tinfo2);
1105 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
1108 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1109 derefhere = (tattr->typekind != TKIND_DISPATCH && tattr->typekind != TKIND_INTERFACE);
1112 case TKIND_ENUM: /* confirmed */
1113 case TKIND_RECORD: /* FIXME: mostly untested */
1115 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
1116 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
1120 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
1124 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1125 ITypeInfo_Release(tinfo2);
1127 /* read it in all cases, we need to know if we have
1128 * NULL pointer or not.
1130 hres = xbuf_get(buf,(LPBYTE)&cookie,sizeof(cookie));
1132 ERR("Failed to load pointer cookie.\n");
1135 if (cookie != 0x42424242) {
1136 /* we read a NULL ptr from the remote side */
1137 if (debugout) TRACE_(olerelay)("NULL");
1141 if (debugout) TRACE_(olerelay)("*");
1143 /* Allocate space for the referenced struct */
1145 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,_xsize(tdesc->u.lptdesc, tinfo));
1148 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, (LPDWORD)*arg, buf);
1150 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, arg, buf);
1153 /* FIXME: UNKNOWN is unknown ..., but allocate 4 byte for it */
1155 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
1158 hres = _unmarshal_interface(buf,&IID_IUnknown,(LPUNKNOWN*)arg);
1160 TRACE_(olerelay)("unk(%p)",arg);
1165 hres = _unmarshal_interface(buf,&IID_IDispatch,(LPUNKNOWN*)arg);
1167 TRACE_(olerelay)("idisp(%p)",arg);
1170 if (debugout) TRACE_(olerelay)("<void>");
1172 case VT_USERDEFINED: {
1176 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
1178 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
1181 hres = ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1183 ERR("Could not get typeattr in VT_USERDEFINED.\n");
1185 switch (tattr->typekind) {
1186 case TKIND_DISPATCH:
1187 case TKIND_INTERFACE:
1189 hres = _unmarshal_interface(buf,&(tattr->guid),(LPUNKNOWN*)arg);
1191 case TKIND_RECORD: {
1194 if (debugout) TRACE_(olerelay)("{");
1195 for (i=0;i<tattr->cVars;i++) {
1198 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
1200 ERR("Could not get vardesc of %d\n",i);
1201 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1202 ITypeInfo_Release(tinfo2);
1205 hres = deserialize_param(
1210 &vdesc->elemdescVar.tdesc,
1211 (DWORD*)(((LPBYTE)arg)+vdesc->u.oInst),
1214 ITypeInfo2_ReleaseVarDesc(tinfo2, vdesc);
1215 if (debugout && (i<tattr->cVars-1)) TRACE_(olerelay)(",");
1217 if (debugout) TRACE_(olerelay)("}");
1221 hres = deserialize_param(tinfo2,readit,debugout,alloc,&tattr->tdescAlias,arg,buf);
1225 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
1226 if (hres) ERR("Failed to read enum (4 byte)\n");
1228 if (debugout) TRACE_(olerelay)("%x",*arg);
1231 ERR("Unhandled typekind %d\n",tattr->typekind);
1235 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1238 ERR("failed to stuballoc in TKIND_RECORD.\n");
1239 ITypeInfo_Release(tinfo2);
1243 /* arg is pointing to the start of the array. */
1244 ARRAYDESC *adesc = tdesc->u.lpadesc;
1247 if (adesc->cDims > 1) FIXME("cDims > 1 in VT_CARRAY. Does it work?\n");
1248 for (i=0;i<adesc->cDims;i++)
1249 arrsize *= adesc->rgbounds[i].cElements;
1250 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,_xsize(tdesc->u.lptdesc, tinfo) * arrsize);
1251 for (i=0;i<arrsize;i++)
1258 (DWORD*)((LPBYTE)(*arg)+i*_xsize(&adesc->tdescElem, tinfo)),
1263 case VT_SAFEARRAY: {
1266 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
1267 unsigned char *buffer;
1268 buffer = LPSAFEARRAY_UserUnmarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
1269 buf->curoff = buffer - buf->base;
1274 ERR("No handler for VT type %d!\n",tdesc->vt);
1280 /* Retrieves a function's funcdesc, searching back into inherited interfaces. */
1281 static HRESULT get_funcdesc(ITypeInfo *tinfo, int iMethod, ITypeInfo **tactual, const FUNCDESC **fdesc,
1282 BSTR *iname, BSTR *fname, UINT *num)
1286 UINT inherited_funcs = 0;
1289 if (fname) *fname = NULL;
1290 if (iname) *iname = NULL;
1294 hr = ITypeInfo_GetTypeAttr(tinfo, &attr);
1297 ERR("GetTypeAttr failed with %x\n",hr);
1301 if(attr->typekind == TKIND_DISPATCH)
1303 if(attr->wTypeFlags & TYPEFLAG_FDUAL)
1308 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, -1, &href);
1311 ERR("Cannot get interface href from dual dispinterface\n");
1312 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1315 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
1318 ERR("Cannot get interface from dual dispinterface\n");
1319 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1322 hr = get_funcdesc(tinfo2, iMethod, tactual, fdesc, iname, fname, num);
1323 ITypeInfo_Release(tinfo2);
1324 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1327 ERR("Shouldn't be called with a non-dual dispinterface\n");
1331 impl_types = attr->cImplTypes;
1332 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1334 for (i = 0; i < impl_types; i++)
1337 ITypeInfo *pSubTypeInfo;
1340 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, i, &href);
1341 if (FAILED(hr)) return hr;
1342 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &pSubTypeInfo);
1343 if (FAILED(hr)) return hr;
1345 hr = get_funcdesc(pSubTypeInfo, iMethod, tactual, fdesc, iname, fname, &sub_funcs);
1346 inherited_funcs += sub_funcs;
1347 ITypeInfo_Release(pSubTypeInfo);
1348 if(SUCCEEDED(hr)) return hr;
1350 if(iMethod < inherited_funcs)
1352 ERR("shouldn't be here\n");
1353 return E_INVALIDARG;
1356 for(i = inherited_funcs; i <= iMethod; i++)
1358 hr = ITypeInfoImpl_GetInternalFuncDesc(tinfo, i - inherited_funcs, fdesc);
1366 /* found it. We don't care about num so zero it */
1369 ITypeInfo_AddRef(*tactual);
1370 if (fname) ITypeInfo_GetDocumentation(tinfo,(*fdesc)->memid,fname,NULL,NULL,NULL);
1371 if (iname) ITypeInfo_GetDocumentation(tinfo,-1,iname,NULL,NULL,NULL);
1375 static inline BOOL is_in_elem(const ELEMDESC *elem)
1377 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN || !elem->u.paramdesc.wParamFlags);
1380 static inline BOOL is_out_elem(const ELEMDESC *elem)
1382 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT || !elem->u.paramdesc.wParamFlags);
1386 xCall(LPVOID retptr, int method, TMProxyImpl *tpinfo /*, args */)
1388 DWORD *args = ((DWORD*)&tpinfo)+1, *xargs;
1389 const FUNCDESC *fdesc;
1391 int i, relaydeb = TRACE_ON(olerelay);
1398 DWORD remoteresult = 0;
1400 IRpcChannelBuffer *chanbuf;
1402 EnterCriticalSection(&tpinfo->crit);
1404 hres = get_funcdesc(tpinfo->tinfo,method,&tinfo,&fdesc,&iname,&fname,NULL);
1406 ERR("Did not find typeinfo/funcdesc entry for method %d!\n",method);
1407 LeaveCriticalSection(&tpinfo->crit);
1411 if (!tpinfo->chanbuf)
1413 WARN("Tried to use disconnected proxy\n");
1414 ITypeInfo_Release(tinfo);
1415 LeaveCriticalSection(&tpinfo->crit);
1416 return RPC_E_DISCONNECTED;
1418 chanbuf = tpinfo->chanbuf;
1419 IRpcChannelBuffer_AddRef(chanbuf);
1421 LeaveCriticalSection(&tpinfo->crit);
1424 TRACE_(olerelay)("->");
1426 TRACE_(olerelay)("%s:",relaystr(iname));
1428 TRACE_(olerelay)("%s(%d)",relaystr(fname),method);
1430 TRACE_(olerelay)("%d",method);
1431 TRACE_(olerelay)("(");
1434 SysFreeString(iname);
1435 SysFreeString(fname);
1437 memset(&buf,0,sizeof(buf));
1439 /* normal typelib driven serializing */
1441 /* Need them for hack below */
1442 memset(names,0,sizeof(names));
1443 if (ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames))
1445 if (nrofnames > sizeof(names)/sizeof(names[0]))
1446 ERR("Need more names!\n");
1449 for (i=0;i<fdesc->cParams;i++) {
1450 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1452 if (i) TRACE_(olerelay)(",");
1453 if (i+1<nrofnames && names[i+1])
1454 TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1456 /* No need to marshal other data than FIN and any VT_PTR. */
1457 if (!is_in_elem(elem) && (elem->tdesc.vt != VT_PTR)) {
1458 xargs+=_argsize(&elem->tdesc, tinfo);
1459 if (relaydeb) TRACE_(olerelay)("[out]");
1462 hres = serialize_param(
1473 ERR("Failed to serialize param, hres %x\n",hres);
1476 xargs+=_argsize(&elem->tdesc, tinfo);
1478 if (relaydeb) TRACE_(olerelay)(")");
1480 memset(&msg,0,sizeof(msg));
1481 msg.cbBuffer = buf.curoff;
1482 msg.iMethod = method;
1483 hres = IRpcChannelBuffer_GetBuffer(chanbuf,&msg,&(tpinfo->iid));
1485 ERR("RpcChannelBuffer GetBuffer failed, %x\n",hres);
1488 memcpy(msg.Buffer,buf.base,buf.curoff);
1489 if (relaydeb) TRACE_(olerelay)("\n");
1490 hres = IRpcChannelBuffer_SendReceive(chanbuf,&msg,&status);
1492 ERR("RpcChannelBuffer SendReceive failed, %x\n",hres);
1496 if (relaydeb) TRACE_(olerelay)(" status = %08x (",status);
1498 buf.base = HeapReAlloc(GetProcessHeap(),0,buf.base,msg.cbBuffer);
1500 buf.base = HeapAlloc(GetProcessHeap(),0,msg.cbBuffer);
1501 buf.size = msg.cbBuffer;
1502 memcpy(buf.base,msg.Buffer,buf.size);
1505 /* generic deserializer using typelib description */
1508 for (i=0;i<fdesc->cParams;i++) {
1509 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1512 if (i) TRACE_(olerelay)(",");
1513 if (i+1<nrofnames && names[i+1]) TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1515 /* No need to marshal other data than FOUT and any VT_PTR */
1516 if (!is_out_elem(elem) && (elem->tdesc.vt != VT_PTR)) {
1517 xargs += _argsize(&elem->tdesc, tinfo);
1518 if (relaydeb) TRACE_(olerelay)("[in]");
1521 hres = deserialize_param(
1531 ERR("Failed to unmarshall param, hres %x\n",hres);
1535 xargs += _argsize(&elem->tdesc, tinfo);
1538 hres = xbuf_get(&buf, (LPBYTE)&remoteresult, sizeof(DWORD));
1541 if (relaydeb) TRACE_(olerelay)(") = %08x\n", remoteresult);
1543 hres = remoteresult;
1546 IRpcChannelBuffer_FreeBuffer(chanbuf,&msg);
1547 for (i = 0; i < nrofnames; i++)
1548 SysFreeString(names[i]);
1549 HeapFree(GetProcessHeap(),0,buf.base);
1550 IRpcChannelBuffer_Release(chanbuf);
1551 ITypeInfo_Release(tinfo);
1552 TRACE("-- 0x%08x\n", hres);
1556 static HRESULT WINAPI ProxyIUnknown_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
1558 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1560 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1562 if (proxy->outerunknown)
1563 return IUnknown_QueryInterface(proxy->outerunknown, riid, ppv);
1565 FIXME("No interface\n");
1566 return E_NOINTERFACE;
1569 static ULONG WINAPI ProxyIUnknown_AddRef(IUnknown *iface)
1571 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1575 if (proxy->outerunknown)
1576 return IUnknown_AddRef(proxy->outerunknown);
1578 return 2; /* FIXME */
1581 static ULONG WINAPI ProxyIUnknown_Release(IUnknown *iface)
1583 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1587 if (proxy->outerunknown)
1588 return IUnknown_Release(proxy->outerunknown);
1590 return 1; /* FIXME */
1593 static HRESULT WINAPI ProxyIDispatch_GetTypeInfoCount(LPDISPATCH iface, UINT * pctinfo)
1595 TMProxyImpl *This = (TMProxyImpl *)iface;
1597 TRACE("(%p)\n", pctinfo);
1599 return IDispatch_GetTypeInfoCount(This->dispatch, pctinfo);
1602 static HRESULT WINAPI ProxyIDispatch_GetTypeInfo(LPDISPATCH iface, UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo)
1604 TMProxyImpl *This = (TMProxyImpl *)iface;
1606 TRACE("(%d, %x, %p)\n", iTInfo, lcid, ppTInfo);
1608 return IDispatch_GetTypeInfo(This->dispatch, iTInfo, lcid, ppTInfo);
1611 static HRESULT WINAPI ProxyIDispatch_GetIDsOfNames(LPDISPATCH iface, REFIID riid, LPOLESTR * rgszNames, UINT cNames, LCID lcid, DISPID * rgDispId)
1613 TMProxyImpl *This = (TMProxyImpl *)iface;
1615 TRACE("(%s, %p, %d, 0x%x, %p)\n", debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);
1617 return IDispatch_GetIDsOfNames(This->dispatch, riid, rgszNames,
1618 cNames, lcid, rgDispId);
1621 static HRESULT WINAPI ProxyIDispatch_Invoke(LPDISPATCH iface, DISPID dispIdMember, REFIID riid, LCID lcid,
1622 WORD wFlags, DISPPARAMS * pDispParams, VARIANT * pVarResult,
1623 EXCEPINFO * pExcepInfo, UINT * puArgErr)
1625 TMProxyImpl *This = (TMProxyImpl *)iface;
1627 TRACE("(%d, %s, 0x%x, 0x%x, %p, %p, %p, %p)\n", dispIdMember,
1628 debugstr_guid(riid), lcid, wFlags, pDispParams, pVarResult,
1629 pExcepInfo, puArgErr);
1631 return IDispatch_Invoke(This->dispatch, dispIdMember, riid, lcid,
1632 wFlags, pDispParams, pVarResult, pExcepInfo,
1638 const IRpcChannelBufferVtbl *lpVtbl;
1640 /* the IDispatch-derived interface we are handling */
1642 IRpcChannelBuffer *pDelegateChannel;
1643 } TMarshalDispatchChannel;
1645 static HRESULT WINAPI TMarshalDispatchChannel_QueryInterface(LPRPCCHANNELBUFFER iface, REFIID riid, LPVOID *ppv)
1648 if (IsEqualIID(riid,&IID_IRpcChannelBuffer) || IsEqualIID(riid,&IID_IUnknown))
1651 IUnknown_AddRef(iface);
1654 return E_NOINTERFACE;
1657 static ULONG WINAPI TMarshalDispatchChannel_AddRef(LPRPCCHANNELBUFFER iface)
1659 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1660 return InterlockedIncrement(&This->refs);
1663 static ULONG WINAPI TMarshalDispatchChannel_Release(LPRPCCHANNELBUFFER iface)
1665 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1668 ref = InterlockedDecrement(&This->refs);
1672 IRpcChannelBuffer_Release(This->pDelegateChannel);
1673 HeapFree(GetProcessHeap(), 0, This);
1677 static HRESULT WINAPI TMarshalDispatchChannel_GetBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg, REFIID riid)
1679 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1680 TRACE("(%p, %s)\n", olemsg, debugstr_guid(riid));
1681 /* Note: we are pretending to invoke a method on the interface identified
1682 * by tmarshal_iid so that we can re-use the IDispatch proxy/stub code
1683 * without the RPC runtime getting confused by not exporting an IDispatch interface */
1684 return IRpcChannelBuffer_GetBuffer(This->pDelegateChannel, olemsg, &This->tmarshal_iid);
1687 static HRESULT WINAPI TMarshalDispatchChannel_SendReceive(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE *olemsg, ULONG *pstatus)
1689 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1690 TRACE("(%p, %p)\n", olemsg, pstatus);
1691 return IRpcChannelBuffer_SendReceive(This->pDelegateChannel, olemsg, pstatus);
1694 static HRESULT WINAPI TMarshalDispatchChannel_FreeBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg)
1696 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1697 TRACE("(%p)\n", olemsg);
1698 return IRpcChannelBuffer_FreeBuffer(This->pDelegateChannel, olemsg);
1701 static HRESULT WINAPI TMarshalDispatchChannel_GetDestCtx(LPRPCCHANNELBUFFER iface, DWORD* pdwDestContext, void** ppvDestContext)
1703 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1704 TRACE("(%p,%p)\n", pdwDestContext, ppvDestContext);
1705 return IRpcChannelBuffer_GetDestCtx(This->pDelegateChannel, pdwDestContext, ppvDestContext);
1708 static HRESULT WINAPI TMarshalDispatchChannel_IsConnected(LPRPCCHANNELBUFFER iface)
1710 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1712 return IRpcChannelBuffer_IsConnected(This->pDelegateChannel);
1715 static const IRpcChannelBufferVtbl TMarshalDispatchChannelVtbl =
1717 TMarshalDispatchChannel_QueryInterface,
1718 TMarshalDispatchChannel_AddRef,
1719 TMarshalDispatchChannel_Release,
1720 TMarshalDispatchChannel_GetBuffer,
1721 TMarshalDispatchChannel_SendReceive,
1722 TMarshalDispatchChannel_FreeBuffer,
1723 TMarshalDispatchChannel_GetDestCtx,
1724 TMarshalDispatchChannel_IsConnected
1727 static HRESULT TMarshalDispatchChannel_Create(
1728 IRpcChannelBuffer *pDelegateChannel, REFIID tmarshal_riid,
1729 IRpcChannelBuffer **ppChannel)
1731 TMarshalDispatchChannel *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1733 return E_OUTOFMEMORY;
1735 This->lpVtbl = &TMarshalDispatchChannelVtbl;
1737 IRpcChannelBuffer_AddRef(pDelegateChannel);
1738 This->pDelegateChannel = pDelegateChannel;
1739 This->tmarshal_iid = *tmarshal_riid;
1741 *ppChannel = (IRpcChannelBuffer *)&This->lpVtbl;
1746 static inline HRESULT get_facbuf_for_iid(REFIID riid, IPSFactoryBuffer **facbuf)
1751 if ((hr = CoGetPSClsid(riid, &clsid)))
1753 return CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL,
1754 &IID_IPSFactoryBuffer, (LPVOID*)facbuf);
1757 static HRESULT init_proxy_entry_point(TMProxyImpl *proxy, unsigned int num)
1760 /* nrofargs without This */
1763 TMAsmProxy *xasm = proxy->asmstubs + num;
1765 const FUNCDESC *fdesc;
1767 hres = get_funcdesc(proxy->tinfo, num, &tinfo2, &fdesc, NULL, NULL, NULL);
1769 ERR("GetFuncDesc %x should not fail here.\n",hres);
1772 ITypeInfo_Release(tinfo2);
1773 /* some args take more than 4 byte on the stack */
1775 for (j=0;j<fdesc->cParams;j++)
1776 nrofargs += _argsize(&fdesc->lprgelemdescParam[j].tdesc, proxy->tinfo);
1779 if (fdesc->callconv != CC_STDCALL) {
1780 ERR("calling convention is not stdcall????\n");
1783 /* popl %eax - return ptr
1790 * arg3 arg2 arg1 <method> <returnptr>
1792 xasm->popleax = 0x58;
1793 xasm->pushlval = 0x68;
1795 xasm->pushleax = 0x50;
1796 xasm->lcall = 0xe8; /* relative jump */
1797 xasm->xcall = (DWORD)xCall;
1798 xasm->xcall -= (DWORD)&(xasm->lret);
1800 xasm->bytestopop = (nrofargs+2)*4; /* pop args, This, iMethod */
1802 proxy->lpvtbl[num] = xasm;
1804 FIXME("not implemented on non i386\n");
1810 static HRESULT WINAPI
1811 PSFacBuf_CreateProxy(
1812 LPPSFACTORYBUFFER iface, IUnknown* pUnkOuter, REFIID riid,
1813 IRpcProxyBuffer **ppProxy, LPVOID *ppv)
1817 unsigned int i, nroffuncs;
1820 BOOL defer_to_dispatch = FALSE;
1822 TRACE("(...%s...)\n",debugstr_guid(riid));
1823 hres = _get_typeinfo_for_iid(riid,&tinfo);
1825 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
1829 hres = num_of_funcs(tinfo, &nroffuncs);
1831 ERR("Cannot get number of functions for typeinfo %s\n",debugstr_guid(riid));
1832 ITypeInfo_Release(tinfo);
1836 proxy = CoTaskMemAlloc(sizeof(TMProxyImpl));
1837 if (!proxy) return E_OUTOFMEMORY;
1839 assert(sizeof(TMAsmProxy) == 16);
1841 proxy->dispatch = NULL;
1842 proxy->dispatch_proxy = NULL;
1843 proxy->outerunknown = pUnkOuter;
1844 proxy->asmstubs = VirtualAlloc(NULL, sizeof(TMAsmProxy) * nroffuncs, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
1845 if (!proxy->asmstubs) {
1846 ERR("Could not commit pages for proxy thunks\n");
1847 CoTaskMemFree(proxy);
1848 return E_OUTOFMEMORY;
1850 proxy->lpvtbl2 = &tmproxyvtable;
1851 /* one reference for the proxy */
1853 proxy->tinfo = tinfo;
1857 InitializeCriticalSection(&proxy->crit);
1858 proxy->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TMProxyImpl.crit");
1860 proxy->lpvtbl = HeapAlloc(GetProcessHeap(),0,sizeof(LPBYTE)*nroffuncs);
1862 /* if we derive from IDispatch then defer to its proxy for its methods */
1863 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
1866 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
1868 IPSFactoryBuffer *factory_buffer;
1869 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
1872 hres = IPSFactoryBuffer_CreateProxy(factory_buffer, NULL,
1873 &IID_IDispatch, &proxy->dispatch_proxy,
1874 (void **)&proxy->dispatch);
1875 IPSFactoryBuffer_Release(factory_buffer);
1877 if ((hres == S_OK) && (nroffuncs < 7))
1879 ERR("nroffuncs calculated incorrectly (%d)\n", nroffuncs);
1880 hres = E_UNEXPECTED;
1884 defer_to_dispatch = TRUE;
1887 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
1890 for (i=0;i<nroffuncs;i++) {
1893 proxy->lpvtbl[i] = ProxyIUnknown_QueryInterface;
1896 proxy->lpvtbl[i] = ProxyIUnknown_AddRef;
1899 proxy->lpvtbl[i] = ProxyIUnknown_Release;
1902 if(!defer_to_dispatch)
1904 hres = init_proxy_entry_point(proxy, i);
1905 if(FAILED(hres)) return hres;
1907 else proxy->lpvtbl[3] = ProxyIDispatch_GetTypeInfoCount;
1910 if(!defer_to_dispatch)
1912 hres = init_proxy_entry_point(proxy, i);
1913 if(FAILED(hres)) return hres;
1915 else proxy->lpvtbl[4] = ProxyIDispatch_GetTypeInfo;
1918 if(!defer_to_dispatch)
1920 hres = init_proxy_entry_point(proxy, i);
1921 if(FAILED(hres)) return hres;
1923 else proxy->lpvtbl[5] = ProxyIDispatch_GetIDsOfNames;
1926 if(!defer_to_dispatch)
1928 hres = init_proxy_entry_point(proxy, i);
1929 if(FAILED(hres)) return hres;
1931 else proxy->lpvtbl[6] = ProxyIDispatch_Invoke;
1934 hres = init_proxy_entry_point(proxy, i);
1935 if(FAILED(hres)) return hres;
1942 *ppProxy = (IRpcProxyBuffer *)&(proxy->lpvtbl2);
1943 IUnknown_AddRef((IUnknown *)*ppv);
1947 TMProxyImpl_Release((IRpcProxyBuffer *)&proxy->lpvtbl2);
1951 typedef struct _TMStubImpl {
1952 const IRpcStubBufferVtbl *lpvtbl;
1958 IRpcStubBuffer *dispatch_stub;
1959 BOOL dispatch_derivative;
1962 static HRESULT WINAPI
1963 TMStubImpl_QueryInterface(LPRPCSTUBBUFFER iface, REFIID riid, LPVOID *ppv)
1965 if (IsEqualIID(riid,&IID_IRpcStubBuffer)||IsEqualIID(riid,&IID_IUnknown)){
1967 IRpcStubBuffer_AddRef(iface);
1970 FIXME("%s, not supported IID.\n",debugstr_guid(riid));
1971 return E_NOINTERFACE;
1975 TMStubImpl_AddRef(LPRPCSTUBBUFFER iface)
1977 TMStubImpl *This = (TMStubImpl *)iface;
1978 ULONG refCount = InterlockedIncrement(&This->ref);
1980 TRACE("(%p)->(ref before=%u)\n", This, refCount - 1);
1986 TMStubImpl_Release(LPRPCSTUBBUFFER iface)
1988 TMStubImpl *This = (TMStubImpl *)iface;
1989 ULONG refCount = InterlockedDecrement(&This->ref);
1991 TRACE("(%p)->(ref before=%u)\n", This, refCount + 1);
1995 IRpcStubBuffer_Disconnect(iface);
1996 ITypeInfo_Release(This->tinfo);
1997 if (This->dispatch_stub)
1998 IRpcStubBuffer_Release(This->dispatch_stub);
1999 CoTaskMemFree(This);
2004 static HRESULT WINAPI
2005 TMStubImpl_Connect(LPRPCSTUBBUFFER iface, LPUNKNOWN pUnkServer)
2007 TMStubImpl *This = (TMStubImpl *)iface;
2009 TRACE("(%p)->(%p)\n", This, pUnkServer);
2011 IUnknown_AddRef(pUnkServer);
2012 This->pUnk = pUnkServer;
2014 if (This->dispatch_stub)
2015 IRpcStubBuffer_Connect(This->dispatch_stub, pUnkServer);
2021 TMStubImpl_Disconnect(LPRPCSTUBBUFFER iface)
2023 TMStubImpl *This = (TMStubImpl *)iface;
2025 TRACE("(%p)->()\n", This);
2029 IUnknown_Release(This->pUnk);
2033 if (This->dispatch_stub)
2034 IRpcStubBuffer_Disconnect(This->dispatch_stub);
2037 static HRESULT WINAPI
2039 LPRPCSTUBBUFFER iface, RPCOLEMESSAGE* xmsg,IRpcChannelBuffer*rpcchanbuf)
2043 const FUNCDESC *fdesc;
2044 TMStubImpl *This = (TMStubImpl *)iface;
2046 DWORD *args = NULL, res, *xargs, nrofargs;
2051 ITypeInfo *tinfo = NULL;
2055 if (xmsg->iMethod < 3) {
2056 ERR("IUnknown methods cannot be marshaled by the typelib marshaler\n");
2057 return E_UNEXPECTED;
2060 if (This->dispatch_derivative && xmsg->iMethod < sizeof(IDispatchVtbl)/sizeof(void *))
2062 IPSFactoryBuffer *factory_buffer;
2063 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
2066 hres = IPSFactoryBuffer_CreateStub(factory_buffer, &IID_IDispatch,
2067 This->pUnk, &This->dispatch_stub);
2068 IPSFactoryBuffer_Release(factory_buffer);
2072 return IRpcStubBuffer_Invoke(This->dispatch_stub, xmsg, rpcchanbuf);
2075 memset(&buf,0,sizeof(buf));
2076 buf.size = xmsg->cbBuffer;
2077 buf.base = HeapAlloc(GetProcessHeap(), 0, xmsg->cbBuffer);
2078 memcpy(buf.base, xmsg->Buffer, xmsg->cbBuffer);
2081 hres = get_funcdesc(This->tinfo,xmsg->iMethod,&tinfo,&fdesc,&iname,NULL,NULL);
2083 ERR("GetFuncDesc on method %d failed with %x\n",xmsg->iMethod,hres);
2087 if (iname && !lstrcmpW(iname, IDispatchW))
2089 ERR("IDispatch cannot be marshaled by the typelib marshaler\n");
2090 hres = E_UNEXPECTED;
2091 SysFreeString (iname);
2095 SysFreeString (iname);
2097 /* Need them for hack below */
2098 memset(names,0,sizeof(names));
2099 ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames);
2100 if (nrofnames > sizeof(names)/sizeof(names[0])) {
2101 ERR("Need more names!\n");
2104 /*dump_FUNCDESC(fdesc);*/
2106 for (i=0;i<fdesc->cParams;i++)
2107 nrofargs += _argsize(&fdesc->lprgelemdescParam[i].tdesc, tinfo);
2108 args = HeapAlloc(GetProcessHeap(),0,(nrofargs+1)*sizeof(DWORD));
2111 hres = E_OUTOFMEMORY;
2115 /* Allocate all stuff used by call. */
2117 for (i=0;i<fdesc->cParams;i++) {
2118 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2120 hres = deserialize_param(
2129 xargs += _argsize(&elem->tdesc, tinfo);
2131 ERR("Failed to deserialize param %s, hres %x\n",relaystr(names[i+1]),hres);
2136 args[0] = (DWORD)This->pUnk;
2141 (*((FARPROC**)args[0]))[fdesc->oVft/4],
2149 DWORD dwExceptionCode = GetExceptionCode();
2150 ERR("invoke call failed with exception 0x%08x (%d)\n", dwExceptionCode, dwExceptionCode);
2151 if (FAILED(dwExceptionCode))
2152 hres = dwExceptionCode;
2154 hres = HRESULT_FROM_WIN32(dwExceptionCode);
2164 for (i=0;i<fdesc->cParams;i++) {
2165 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2166 hres = serialize_param(
2175 xargs += _argsize(&elem->tdesc, tinfo);
2177 ERR("Failed to stuballoc param, hres %x\n",hres);
2182 hres = xbuf_add (&buf, (LPBYTE)&res, sizeof(DWORD));
2187 xmsg->cbBuffer = buf.curoff;
2188 hres = IRpcChannelBuffer_GetBuffer(rpcchanbuf, xmsg, &This->iid);
2190 ERR("IRpcChannelBuffer_GetBuffer failed with error 0x%08x\n", hres);
2193 memcpy(xmsg->Buffer, buf.base, buf.curoff);
2196 for (i = 0; i < nrofnames; i++)
2197 SysFreeString(names[i]);
2199 ITypeInfo_Release(tinfo);
2200 HeapFree(GetProcessHeap(), 0, args);
2202 HeapFree(GetProcessHeap(), 0, buf.base);
2204 TRACE("returning\n");
2207 FIXME( "not implemented on non-i386\n" );
2212 static LPRPCSTUBBUFFER WINAPI
2213 TMStubImpl_IsIIDSupported(LPRPCSTUBBUFFER iface, REFIID riid) {
2214 FIXME("Huh (%s)?\n",debugstr_guid(riid));
2219 TMStubImpl_CountRefs(LPRPCSTUBBUFFER iface) {
2220 TMStubImpl *This = (TMStubImpl *)iface;
2223 return This->ref; /*FIXME? */
2226 static HRESULT WINAPI
2227 TMStubImpl_DebugServerQueryInterface(LPRPCSTUBBUFFER iface, LPVOID *ppv) {
2232 TMStubImpl_DebugServerRelease(LPRPCSTUBBUFFER iface, LPVOID ppv) {
2236 static const IRpcStubBufferVtbl tmstubvtbl = {
2237 TMStubImpl_QueryInterface,
2241 TMStubImpl_Disconnect,
2243 TMStubImpl_IsIIDSupported,
2244 TMStubImpl_CountRefs,
2245 TMStubImpl_DebugServerQueryInterface,
2246 TMStubImpl_DebugServerRelease
2249 static HRESULT WINAPI
2250 PSFacBuf_CreateStub(
2251 LPPSFACTORYBUFFER iface, REFIID riid,IUnknown *pUnkServer,
2252 IRpcStubBuffer** ppStub
2259 TRACE("(%s,%p,%p)\n",debugstr_guid(riid),pUnkServer,ppStub);
2261 hres = _get_typeinfo_for_iid(riid,&tinfo);
2263 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
2267 stub = CoTaskMemAlloc(sizeof(TMStubImpl));
2269 return E_OUTOFMEMORY;
2270 stub->lpvtbl = &tmstubvtbl;
2272 stub->tinfo = tinfo;
2273 stub->dispatch_stub = NULL;
2274 stub->dispatch_derivative = FALSE;
2276 hres = IRpcStubBuffer_Connect((LPRPCSTUBBUFFER)stub,pUnkServer);
2277 *ppStub = (LPRPCSTUBBUFFER)stub;
2278 TRACE("IRpcStubBuffer: %p\n", stub);
2280 ERR("Connect to pUnkServer failed?\n");
2282 /* if we derive from IDispatch then defer to its stub for some of its methods */
2283 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
2286 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
2287 stub->dispatch_derivative = TRUE;
2288 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
2294 static const IPSFactoryBufferVtbl psfacbufvtbl = {
2295 PSFacBuf_QueryInterface,
2298 PSFacBuf_CreateProxy,
2302 /* This is the whole PSFactoryBuffer object, just the vtableptr */
2303 static const IPSFactoryBufferVtbl *lppsfac = &psfacbufvtbl;
2305 /***********************************************************************
2306 * TMARSHAL_DllGetClassObject
2308 HRESULT TMARSHAL_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
2310 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)) {
2314 return E_NOINTERFACE;