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 for (i=0;i<arrsize;i++)
1257 (DWORD*)((LPBYTE)(arg)+i*_xsize(&adesc->tdescElem, tinfo)),
1262 case VT_SAFEARRAY: {
1265 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
1266 unsigned char *buffer;
1267 buffer = LPSAFEARRAY_UserUnmarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
1268 buf->curoff = buffer - buf->base;
1273 ERR("No handler for VT type %d!\n",tdesc->vt);
1279 /* Retrieves a function's funcdesc, searching back into inherited interfaces. */
1280 static HRESULT get_funcdesc(ITypeInfo *tinfo, int iMethod, ITypeInfo **tactual, const FUNCDESC **fdesc,
1281 BSTR *iname, BSTR *fname, UINT *num)
1285 UINT inherited_funcs = 0;
1288 if (fname) *fname = NULL;
1289 if (iname) *iname = NULL;
1293 hr = ITypeInfo_GetTypeAttr(tinfo, &attr);
1296 ERR("GetTypeAttr failed with %x\n",hr);
1300 if(attr->typekind == TKIND_DISPATCH)
1302 if(attr->wTypeFlags & TYPEFLAG_FDUAL)
1307 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, -1, &href);
1310 ERR("Cannot get interface href from dual dispinterface\n");
1311 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1314 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
1317 ERR("Cannot get interface from dual dispinterface\n");
1318 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1321 hr = get_funcdesc(tinfo2, iMethod, tactual, fdesc, iname, fname, num);
1322 ITypeInfo_Release(tinfo2);
1323 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1326 ERR("Shouldn't be called with a non-dual dispinterface\n");
1330 impl_types = attr->cImplTypes;
1331 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1333 for (i = 0; i < impl_types; i++)
1336 ITypeInfo *pSubTypeInfo;
1339 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, i, &href);
1340 if (FAILED(hr)) return hr;
1341 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &pSubTypeInfo);
1342 if (FAILED(hr)) return hr;
1344 hr = get_funcdesc(pSubTypeInfo, iMethod, tactual, fdesc, iname, fname, &sub_funcs);
1345 inherited_funcs += sub_funcs;
1346 ITypeInfo_Release(pSubTypeInfo);
1347 if(SUCCEEDED(hr)) return hr;
1349 if(iMethod < inherited_funcs)
1351 ERR("shouldn't be here\n");
1352 return E_INVALIDARG;
1355 for(i = inherited_funcs; i <= iMethod; i++)
1357 hr = ITypeInfoImpl_GetInternalFuncDesc(tinfo, i - inherited_funcs, fdesc);
1365 /* found it. We don't care about num so zero it */
1368 ITypeInfo_AddRef(*tactual);
1369 if (fname) ITypeInfo_GetDocumentation(tinfo,(*fdesc)->memid,fname,NULL,NULL,NULL);
1370 if (iname) ITypeInfo_GetDocumentation(tinfo,-1,iname,NULL,NULL,NULL);
1374 static inline BOOL is_in_elem(const ELEMDESC *elem)
1376 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN || !elem->u.paramdesc.wParamFlags);
1379 static inline BOOL is_out_elem(const ELEMDESC *elem)
1381 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT || !elem->u.paramdesc.wParamFlags);
1385 xCall(LPVOID retptr, int method, TMProxyImpl *tpinfo /*, args */)
1387 DWORD *args = ((DWORD*)&tpinfo)+1, *xargs;
1388 const FUNCDESC *fdesc;
1390 int i, relaydeb = TRACE_ON(olerelay);
1397 DWORD remoteresult = 0;
1399 IRpcChannelBuffer *chanbuf;
1401 EnterCriticalSection(&tpinfo->crit);
1403 hres = get_funcdesc(tpinfo->tinfo,method,&tinfo,&fdesc,&iname,&fname,NULL);
1405 ERR("Did not find typeinfo/funcdesc entry for method %d!\n",method);
1406 LeaveCriticalSection(&tpinfo->crit);
1410 if (!tpinfo->chanbuf)
1412 WARN("Tried to use disconnected proxy\n");
1413 ITypeInfo_Release(tinfo);
1414 LeaveCriticalSection(&tpinfo->crit);
1415 return RPC_E_DISCONNECTED;
1417 chanbuf = tpinfo->chanbuf;
1418 IRpcChannelBuffer_AddRef(chanbuf);
1420 LeaveCriticalSection(&tpinfo->crit);
1423 TRACE_(olerelay)("->");
1425 TRACE_(olerelay)("%s:",relaystr(iname));
1427 TRACE_(olerelay)("%s(%d)",relaystr(fname),method);
1429 TRACE_(olerelay)("%d",method);
1430 TRACE_(olerelay)("(");
1433 SysFreeString(iname);
1434 SysFreeString(fname);
1436 memset(&buf,0,sizeof(buf));
1438 /* normal typelib driven serializing */
1440 /* Need them for hack below */
1441 memset(names,0,sizeof(names));
1442 if (ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames))
1444 if (nrofnames > sizeof(names)/sizeof(names[0]))
1445 ERR("Need more names!\n");
1448 for (i=0;i<fdesc->cParams;i++) {
1449 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1451 if (i) TRACE_(olerelay)(",");
1452 if (i+1<nrofnames && names[i+1])
1453 TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1455 /* No need to marshal other data than FIN and any VT_PTR. */
1456 if (!is_in_elem(elem) && (elem->tdesc.vt != VT_PTR)) {
1457 xargs+=_argsize(&elem->tdesc, tinfo);
1458 if (relaydeb) TRACE_(olerelay)("[out]");
1461 hres = serialize_param(
1472 ERR("Failed to serialize param, hres %x\n",hres);
1475 xargs+=_argsize(&elem->tdesc, tinfo);
1477 if (relaydeb) TRACE_(olerelay)(")");
1479 memset(&msg,0,sizeof(msg));
1480 msg.cbBuffer = buf.curoff;
1481 msg.iMethod = method;
1482 hres = IRpcChannelBuffer_GetBuffer(chanbuf,&msg,&(tpinfo->iid));
1484 ERR("RpcChannelBuffer GetBuffer failed, %x\n",hres);
1487 memcpy(msg.Buffer,buf.base,buf.curoff);
1488 if (relaydeb) TRACE_(olerelay)("\n");
1489 hres = IRpcChannelBuffer_SendReceive(chanbuf,&msg,&status);
1491 ERR("RpcChannelBuffer SendReceive failed, %x\n",hres);
1495 if (relaydeb) TRACE_(olerelay)(" status = %08x (",status);
1497 buf.base = HeapReAlloc(GetProcessHeap(),0,buf.base,msg.cbBuffer);
1499 buf.base = HeapAlloc(GetProcessHeap(),0,msg.cbBuffer);
1500 buf.size = msg.cbBuffer;
1501 memcpy(buf.base,msg.Buffer,buf.size);
1504 /* generic deserializer using typelib description */
1507 for (i=0;i<fdesc->cParams;i++) {
1508 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1511 if (i) TRACE_(olerelay)(",");
1512 if (i+1<nrofnames && names[i+1]) TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1514 /* No need to marshal other data than FOUT and any VT_PTR */
1515 if (!is_out_elem(elem) && (elem->tdesc.vt != VT_PTR)) {
1516 xargs += _argsize(&elem->tdesc, tinfo);
1517 if (relaydeb) TRACE_(olerelay)("[in]");
1520 hres = deserialize_param(
1530 ERR("Failed to unmarshall param, hres %x\n",hres);
1534 xargs += _argsize(&elem->tdesc, tinfo);
1537 hres = xbuf_get(&buf, (LPBYTE)&remoteresult, sizeof(DWORD));
1540 if (relaydeb) TRACE_(olerelay)(") = %08x\n", remoteresult);
1542 hres = remoteresult;
1545 IRpcChannelBuffer_FreeBuffer(chanbuf,&msg);
1546 for (i = 0; i < nrofnames; i++)
1547 SysFreeString(names[i]);
1548 HeapFree(GetProcessHeap(),0,buf.base);
1549 IRpcChannelBuffer_Release(chanbuf);
1550 ITypeInfo_Release(tinfo);
1551 TRACE("-- 0x%08x\n", hres);
1555 static HRESULT WINAPI ProxyIUnknown_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
1557 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1559 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1561 if (proxy->outerunknown)
1562 return IUnknown_QueryInterface(proxy->outerunknown, riid, ppv);
1564 FIXME("No interface\n");
1565 return E_NOINTERFACE;
1568 static ULONG WINAPI ProxyIUnknown_AddRef(IUnknown *iface)
1570 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1574 if (proxy->outerunknown)
1575 return IUnknown_AddRef(proxy->outerunknown);
1577 return 2; /* FIXME */
1580 static ULONG WINAPI ProxyIUnknown_Release(IUnknown *iface)
1582 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1586 if (proxy->outerunknown)
1587 return IUnknown_Release(proxy->outerunknown);
1589 return 1; /* FIXME */
1592 static HRESULT WINAPI ProxyIDispatch_GetTypeInfoCount(LPDISPATCH iface, UINT * pctinfo)
1594 TMProxyImpl *This = (TMProxyImpl *)iface;
1596 TRACE("(%p)\n", pctinfo);
1598 return IDispatch_GetTypeInfoCount(This->dispatch, pctinfo);
1601 static HRESULT WINAPI ProxyIDispatch_GetTypeInfo(LPDISPATCH iface, UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo)
1603 TMProxyImpl *This = (TMProxyImpl *)iface;
1605 TRACE("(%d, %x, %p)\n", iTInfo, lcid, ppTInfo);
1607 return IDispatch_GetTypeInfo(This->dispatch, iTInfo, lcid, ppTInfo);
1610 static HRESULT WINAPI ProxyIDispatch_GetIDsOfNames(LPDISPATCH iface, REFIID riid, LPOLESTR * rgszNames, UINT cNames, LCID lcid, DISPID * rgDispId)
1612 TMProxyImpl *This = (TMProxyImpl *)iface;
1614 TRACE("(%s, %p, %d, 0x%x, %p)\n", debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);
1616 return IDispatch_GetIDsOfNames(This->dispatch, riid, rgszNames,
1617 cNames, lcid, rgDispId);
1620 static HRESULT WINAPI ProxyIDispatch_Invoke(LPDISPATCH iface, DISPID dispIdMember, REFIID riid, LCID lcid,
1621 WORD wFlags, DISPPARAMS * pDispParams, VARIANT * pVarResult,
1622 EXCEPINFO * pExcepInfo, UINT * puArgErr)
1624 TMProxyImpl *This = (TMProxyImpl *)iface;
1626 TRACE("(%d, %s, 0x%x, 0x%x, %p, %p, %p, %p)\n", dispIdMember,
1627 debugstr_guid(riid), lcid, wFlags, pDispParams, pVarResult,
1628 pExcepInfo, puArgErr);
1630 return IDispatch_Invoke(This->dispatch, dispIdMember, riid, lcid,
1631 wFlags, pDispParams, pVarResult, pExcepInfo,
1637 const IRpcChannelBufferVtbl *lpVtbl;
1639 /* the IDispatch-derived interface we are handling */
1641 IRpcChannelBuffer *pDelegateChannel;
1642 } TMarshalDispatchChannel;
1644 static HRESULT WINAPI TMarshalDispatchChannel_QueryInterface(LPRPCCHANNELBUFFER iface, REFIID riid, LPVOID *ppv)
1647 if (IsEqualIID(riid,&IID_IRpcChannelBuffer) || IsEqualIID(riid,&IID_IUnknown))
1650 IUnknown_AddRef(iface);
1653 return E_NOINTERFACE;
1656 static ULONG WINAPI TMarshalDispatchChannel_AddRef(LPRPCCHANNELBUFFER iface)
1658 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1659 return InterlockedIncrement(&This->refs);
1662 static ULONG WINAPI TMarshalDispatchChannel_Release(LPRPCCHANNELBUFFER iface)
1664 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1667 ref = InterlockedDecrement(&This->refs);
1671 IRpcChannelBuffer_Release(This->pDelegateChannel);
1672 HeapFree(GetProcessHeap(), 0, This);
1676 static HRESULT WINAPI TMarshalDispatchChannel_GetBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg, REFIID riid)
1678 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1679 TRACE("(%p, %s)\n", olemsg, debugstr_guid(riid));
1680 /* Note: we are pretending to invoke a method on the interface identified
1681 * by tmarshal_iid so that we can re-use the IDispatch proxy/stub code
1682 * without the RPC runtime getting confused by not exporting an IDispatch interface */
1683 return IRpcChannelBuffer_GetBuffer(This->pDelegateChannel, olemsg, &This->tmarshal_iid);
1686 static HRESULT WINAPI TMarshalDispatchChannel_SendReceive(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE *olemsg, ULONG *pstatus)
1688 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1689 TRACE("(%p, %p)\n", olemsg, pstatus);
1690 return IRpcChannelBuffer_SendReceive(This->pDelegateChannel, olemsg, pstatus);
1693 static HRESULT WINAPI TMarshalDispatchChannel_FreeBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg)
1695 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1696 TRACE("(%p)\n", olemsg);
1697 return IRpcChannelBuffer_FreeBuffer(This->pDelegateChannel, olemsg);
1700 static HRESULT WINAPI TMarshalDispatchChannel_GetDestCtx(LPRPCCHANNELBUFFER iface, DWORD* pdwDestContext, void** ppvDestContext)
1702 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1703 TRACE("(%p,%p)\n", pdwDestContext, ppvDestContext);
1704 return IRpcChannelBuffer_GetDestCtx(This->pDelegateChannel, pdwDestContext, ppvDestContext);
1707 static HRESULT WINAPI TMarshalDispatchChannel_IsConnected(LPRPCCHANNELBUFFER iface)
1709 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1711 return IRpcChannelBuffer_IsConnected(This->pDelegateChannel);
1714 static const IRpcChannelBufferVtbl TMarshalDispatchChannelVtbl =
1716 TMarshalDispatchChannel_QueryInterface,
1717 TMarshalDispatchChannel_AddRef,
1718 TMarshalDispatchChannel_Release,
1719 TMarshalDispatchChannel_GetBuffer,
1720 TMarshalDispatchChannel_SendReceive,
1721 TMarshalDispatchChannel_FreeBuffer,
1722 TMarshalDispatchChannel_GetDestCtx,
1723 TMarshalDispatchChannel_IsConnected
1726 static HRESULT TMarshalDispatchChannel_Create(
1727 IRpcChannelBuffer *pDelegateChannel, REFIID tmarshal_riid,
1728 IRpcChannelBuffer **ppChannel)
1730 TMarshalDispatchChannel *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1732 return E_OUTOFMEMORY;
1734 This->lpVtbl = &TMarshalDispatchChannelVtbl;
1736 IRpcChannelBuffer_AddRef(pDelegateChannel);
1737 This->pDelegateChannel = pDelegateChannel;
1738 This->tmarshal_iid = *tmarshal_riid;
1740 *ppChannel = (IRpcChannelBuffer *)&This->lpVtbl;
1745 static inline HRESULT get_facbuf_for_iid(REFIID riid, IPSFactoryBuffer **facbuf)
1750 if ((hr = CoGetPSClsid(riid, &clsid)))
1752 return CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL,
1753 &IID_IPSFactoryBuffer, (LPVOID*)facbuf);
1756 static HRESULT init_proxy_entry_point(TMProxyImpl *proxy, unsigned int num)
1759 /* nrofargs without This */
1762 TMAsmProxy *xasm = proxy->asmstubs + num;
1764 const FUNCDESC *fdesc;
1766 hres = get_funcdesc(proxy->tinfo, num, &tinfo2, &fdesc, NULL, NULL, NULL);
1768 ERR("GetFuncDesc %x should not fail here.\n",hres);
1771 ITypeInfo_Release(tinfo2);
1772 /* some args take more than 4 byte on the stack */
1774 for (j=0;j<fdesc->cParams;j++)
1775 nrofargs += _argsize(&fdesc->lprgelemdescParam[j].tdesc, proxy->tinfo);
1778 if (fdesc->callconv != CC_STDCALL) {
1779 ERR("calling convention is not stdcall????\n");
1782 /* popl %eax - return ptr
1789 * arg3 arg2 arg1 <method> <returnptr>
1791 xasm->popleax = 0x58;
1792 xasm->pushlval = 0x68;
1794 xasm->pushleax = 0x50;
1795 xasm->lcall = 0xe8; /* relative jump */
1796 xasm->xcall = (DWORD)xCall;
1797 xasm->xcall -= (DWORD)&(xasm->lret);
1799 xasm->bytestopop = (nrofargs+2)*4; /* pop args, This, iMethod */
1801 proxy->lpvtbl[num] = xasm;
1803 FIXME("not implemented on non i386\n");
1809 static HRESULT WINAPI
1810 PSFacBuf_CreateProxy(
1811 LPPSFACTORYBUFFER iface, IUnknown* pUnkOuter, REFIID riid,
1812 IRpcProxyBuffer **ppProxy, LPVOID *ppv)
1816 unsigned int i, nroffuncs;
1819 BOOL defer_to_dispatch = FALSE;
1821 TRACE("(...%s...)\n",debugstr_guid(riid));
1822 hres = _get_typeinfo_for_iid(riid,&tinfo);
1824 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
1828 hres = num_of_funcs(tinfo, &nroffuncs);
1830 ERR("Cannot get number of functions for typeinfo %s\n",debugstr_guid(riid));
1831 ITypeInfo_Release(tinfo);
1835 proxy = CoTaskMemAlloc(sizeof(TMProxyImpl));
1836 if (!proxy) return E_OUTOFMEMORY;
1838 assert(sizeof(TMAsmProxy) == 16);
1840 proxy->dispatch = NULL;
1841 proxy->dispatch_proxy = NULL;
1842 proxy->outerunknown = pUnkOuter;
1843 proxy->asmstubs = VirtualAlloc(NULL, sizeof(TMAsmProxy) * nroffuncs, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
1844 if (!proxy->asmstubs) {
1845 ERR("Could not commit pages for proxy thunks\n");
1846 CoTaskMemFree(proxy);
1847 return E_OUTOFMEMORY;
1849 proxy->lpvtbl2 = &tmproxyvtable;
1850 /* one reference for the proxy */
1852 proxy->tinfo = tinfo;
1856 InitializeCriticalSection(&proxy->crit);
1857 proxy->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TMProxyImpl.crit");
1859 proxy->lpvtbl = HeapAlloc(GetProcessHeap(),0,sizeof(LPBYTE)*nroffuncs);
1861 /* if we derive from IDispatch then defer to its proxy for its methods */
1862 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
1865 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
1867 IPSFactoryBuffer *factory_buffer;
1868 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
1871 hres = IPSFactoryBuffer_CreateProxy(factory_buffer, NULL,
1872 &IID_IDispatch, &proxy->dispatch_proxy,
1873 (void **)&proxy->dispatch);
1874 IPSFactoryBuffer_Release(factory_buffer);
1876 if ((hres == S_OK) && (nroffuncs < 7))
1878 ERR("nroffuncs calculated incorrectly (%d)\n", nroffuncs);
1879 hres = E_UNEXPECTED;
1883 defer_to_dispatch = TRUE;
1886 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
1889 for (i=0;i<nroffuncs;i++) {
1892 proxy->lpvtbl[i] = ProxyIUnknown_QueryInterface;
1895 proxy->lpvtbl[i] = ProxyIUnknown_AddRef;
1898 proxy->lpvtbl[i] = ProxyIUnknown_Release;
1901 if(!defer_to_dispatch)
1903 hres = init_proxy_entry_point(proxy, i);
1904 if(FAILED(hres)) return hres;
1906 else proxy->lpvtbl[3] = ProxyIDispatch_GetTypeInfoCount;
1909 if(!defer_to_dispatch)
1911 hres = init_proxy_entry_point(proxy, i);
1912 if(FAILED(hres)) return hres;
1914 else proxy->lpvtbl[4] = ProxyIDispatch_GetTypeInfo;
1917 if(!defer_to_dispatch)
1919 hres = init_proxy_entry_point(proxy, i);
1920 if(FAILED(hres)) return hres;
1922 else proxy->lpvtbl[5] = ProxyIDispatch_GetIDsOfNames;
1925 if(!defer_to_dispatch)
1927 hres = init_proxy_entry_point(proxy, i);
1928 if(FAILED(hres)) return hres;
1930 else proxy->lpvtbl[6] = ProxyIDispatch_Invoke;
1933 hres = init_proxy_entry_point(proxy, i);
1934 if(FAILED(hres)) return hres;
1941 *ppProxy = (IRpcProxyBuffer *)&(proxy->lpvtbl2);
1942 IUnknown_AddRef((IUnknown *)*ppv);
1946 TMProxyImpl_Release((IRpcProxyBuffer *)&proxy->lpvtbl2);
1950 typedef struct _TMStubImpl {
1951 const IRpcStubBufferVtbl *lpvtbl;
1957 IRpcStubBuffer *dispatch_stub;
1958 BOOL dispatch_derivative;
1961 static HRESULT WINAPI
1962 TMStubImpl_QueryInterface(LPRPCSTUBBUFFER iface, REFIID riid, LPVOID *ppv)
1964 if (IsEqualIID(riid,&IID_IRpcStubBuffer)||IsEqualIID(riid,&IID_IUnknown)){
1966 IRpcStubBuffer_AddRef(iface);
1969 FIXME("%s, not supported IID.\n",debugstr_guid(riid));
1970 return E_NOINTERFACE;
1974 TMStubImpl_AddRef(LPRPCSTUBBUFFER iface)
1976 TMStubImpl *This = (TMStubImpl *)iface;
1977 ULONG refCount = InterlockedIncrement(&This->ref);
1979 TRACE("(%p)->(ref before=%u)\n", This, refCount - 1);
1985 TMStubImpl_Release(LPRPCSTUBBUFFER iface)
1987 TMStubImpl *This = (TMStubImpl *)iface;
1988 ULONG refCount = InterlockedDecrement(&This->ref);
1990 TRACE("(%p)->(ref before=%u)\n", This, refCount + 1);
1994 IRpcStubBuffer_Disconnect(iface);
1995 ITypeInfo_Release(This->tinfo);
1996 if (This->dispatch_stub)
1997 IRpcStubBuffer_Release(This->dispatch_stub);
1998 CoTaskMemFree(This);
2003 static HRESULT WINAPI
2004 TMStubImpl_Connect(LPRPCSTUBBUFFER iface, LPUNKNOWN pUnkServer)
2006 TMStubImpl *This = (TMStubImpl *)iface;
2008 TRACE("(%p)->(%p)\n", This, pUnkServer);
2010 IUnknown_AddRef(pUnkServer);
2011 This->pUnk = pUnkServer;
2013 if (This->dispatch_stub)
2014 IRpcStubBuffer_Connect(This->dispatch_stub, pUnkServer);
2020 TMStubImpl_Disconnect(LPRPCSTUBBUFFER iface)
2022 TMStubImpl *This = (TMStubImpl *)iface;
2024 TRACE("(%p)->()\n", This);
2028 IUnknown_Release(This->pUnk);
2032 if (This->dispatch_stub)
2033 IRpcStubBuffer_Disconnect(This->dispatch_stub);
2036 static HRESULT WINAPI
2038 LPRPCSTUBBUFFER iface, RPCOLEMESSAGE* xmsg,IRpcChannelBuffer*rpcchanbuf)
2041 const FUNCDESC *fdesc;
2042 TMStubImpl *This = (TMStubImpl *)iface;
2044 DWORD *args = NULL, res, *xargs, nrofargs;
2049 ITypeInfo *tinfo = NULL;
2053 if (xmsg->iMethod < 3) {
2054 ERR("IUnknown methods cannot be marshaled by the typelib marshaler\n");
2055 return E_UNEXPECTED;
2058 if (This->dispatch_derivative && xmsg->iMethod < sizeof(IDispatchVtbl)/sizeof(void *))
2060 IPSFactoryBuffer *factory_buffer;
2061 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
2064 hres = IPSFactoryBuffer_CreateStub(factory_buffer, &IID_IDispatch,
2065 This->pUnk, &This->dispatch_stub);
2066 IPSFactoryBuffer_Release(factory_buffer);
2070 return IRpcStubBuffer_Invoke(This->dispatch_stub, xmsg, rpcchanbuf);
2073 memset(&buf,0,sizeof(buf));
2074 buf.size = xmsg->cbBuffer;
2075 buf.base = HeapAlloc(GetProcessHeap(), 0, xmsg->cbBuffer);
2076 memcpy(buf.base, xmsg->Buffer, xmsg->cbBuffer);
2079 hres = get_funcdesc(This->tinfo,xmsg->iMethod,&tinfo,&fdesc,&iname,NULL,NULL);
2081 ERR("GetFuncDesc on method %d failed with %x\n",xmsg->iMethod,hres);
2085 if (iname && !lstrcmpW(iname, IDispatchW))
2087 ERR("IDispatch cannot be marshaled by the typelib marshaler\n");
2088 hres = E_UNEXPECTED;
2089 SysFreeString (iname);
2093 SysFreeString (iname);
2095 /* Need them for hack below */
2096 memset(names,0,sizeof(names));
2097 ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames);
2098 if (nrofnames > sizeof(names)/sizeof(names[0])) {
2099 ERR("Need more names!\n");
2102 /*dump_FUNCDESC(fdesc);*/
2104 for (i=0;i<fdesc->cParams;i++)
2105 nrofargs += _argsize(&fdesc->lprgelemdescParam[i].tdesc, tinfo);
2106 args = HeapAlloc(GetProcessHeap(),0,(nrofargs+1)*sizeof(DWORD));
2109 hres = E_OUTOFMEMORY;
2113 /* Allocate all stuff used by call. */
2115 for (i=0;i<fdesc->cParams;i++) {
2116 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2118 hres = deserialize_param(
2127 xargs += _argsize(&elem->tdesc, tinfo);
2129 ERR("Failed to deserialize param %s, hres %x\n",relaystr(names[i+1]),hres);
2134 args[0] = (DWORD)This->pUnk;
2139 (*((FARPROC**)args[0]))[fdesc->oVft/4],
2147 DWORD dwExceptionCode = GetExceptionCode();
2148 ERR("invoke call failed with exception 0x%08x (%d)\n", dwExceptionCode, dwExceptionCode);
2149 if (FAILED(dwExceptionCode))
2150 hres = dwExceptionCode;
2152 hres = HRESULT_FROM_WIN32(dwExceptionCode);
2162 for (i=0;i<fdesc->cParams;i++) {
2163 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2164 hres = serialize_param(
2173 xargs += _argsize(&elem->tdesc, tinfo);
2175 ERR("Failed to stuballoc param, hres %x\n",hres);
2180 hres = xbuf_add (&buf, (LPBYTE)&res, sizeof(DWORD));
2185 xmsg->cbBuffer = buf.curoff;
2186 hres = IRpcChannelBuffer_GetBuffer(rpcchanbuf, xmsg, &This->iid);
2188 ERR("IRpcChannelBuffer_GetBuffer failed with error 0x%08x\n", hres);
2191 memcpy(xmsg->Buffer, buf.base, buf.curoff);
2194 for (i = 0; i < nrofnames; i++)
2195 SysFreeString(names[i]);
2197 ITypeInfo_Release(tinfo);
2198 HeapFree(GetProcessHeap(), 0, args);
2200 HeapFree(GetProcessHeap(), 0, buf.base);
2202 TRACE("returning\n");
2206 static LPRPCSTUBBUFFER WINAPI
2207 TMStubImpl_IsIIDSupported(LPRPCSTUBBUFFER iface, REFIID riid) {
2208 FIXME("Huh (%s)?\n",debugstr_guid(riid));
2213 TMStubImpl_CountRefs(LPRPCSTUBBUFFER iface) {
2214 TMStubImpl *This = (TMStubImpl *)iface;
2217 return This->ref; /*FIXME? */
2220 static HRESULT WINAPI
2221 TMStubImpl_DebugServerQueryInterface(LPRPCSTUBBUFFER iface, LPVOID *ppv) {
2226 TMStubImpl_DebugServerRelease(LPRPCSTUBBUFFER iface, LPVOID ppv) {
2230 static const IRpcStubBufferVtbl tmstubvtbl = {
2231 TMStubImpl_QueryInterface,
2235 TMStubImpl_Disconnect,
2237 TMStubImpl_IsIIDSupported,
2238 TMStubImpl_CountRefs,
2239 TMStubImpl_DebugServerQueryInterface,
2240 TMStubImpl_DebugServerRelease
2243 static HRESULT WINAPI
2244 PSFacBuf_CreateStub(
2245 LPPSFACTORYBUFFER iface, REFIID riid,IUnknown *pUnkServer,
2246 IRpcStubBuffer** ppStub
2253 TRACE("(%s,%p,%p)\n",debugstr_guid(riid),pUnkServer,ppStub);
2255 hres = _get_typeinfo_for_iid(riid,&tinfo);
2257 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
2261 stub = CoTaskMemAlloc(sizeof(TMStubImpl));
2263 return E_OUTOFMEMORY;
2264 stub->lpvtbl = &tmstubvtbl;
2266 stub->tinfo = tinfo;
2267 stub->dispatch_stub = NULL;
2268 stub->dispatch_derivative = FALSE;
2270 hres = IRpcStubBuffer_Connect((LPRPCSTUBBUFFER)stub,pUnkServer);
2271 *ppStub = (LPRPCSTUBBUFFER)stub;
2272 TRACE("IRpcStubBuffer: %p\n", stub);
2274 ERR("Connect to pUnkServer failed?\n");
2276 /* if we derive from IDispatch then defer to its stub for some of its methods */
2277 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
2280 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
2281 stub->dispatch_derivative = TRUE;
2282 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
2288 static const IPSFactoryBufferVtbl psfacbufvtbl = {
2289 PSFacBuf_QueryInterface,
2292 PSFacBuf_CreateProxy,
2296 /* This is the whole PSFactoryBuffer object, just the vtableptr */
2297 static const IPSFactoryBufferVtbl *lppsfac = &psfacbufvtbl;
2299 /***********************************************************************
2300 * TMARSHAL_DllGetClassObject
2302 HRESULT TMARSHAL_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
2304 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)) {
2308 return E_NOINTERFACE;