4 * Copyright 2002,2005 Marcus Meissner
6 * The olerelay debug channel allows you to see calls marshalled by
7 * the typelib marshaller. It is not a generic COM relaying system.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "wine/port.h"
35 #define NONAMELESSUNION
36 #define NONAMELESSSTRUCT
46 #include "propidl.h" /* for LPSAFEARRAY_User* functions */
49 #include "wine/debug.h"
50 #include "wine/exception.h"
52 static const WCHAR IDispatchW[] = { 'I','D','i','s','p','a','t','c','h',0};
54 WINE_DEFAULT_DEBUG_CHANNEL(ole);
55 WINE_DECLARE_DEBUG_CHANNEL(olerelay);
57 #define ICOM_THIS_MULTI(impl,field,iface) impl* const This=(impl*)((char*)(iface) - offsetof(impl,field))
59 static HRESULT TMarshalDispatchChannel_Create(
60 IRpcChannelBuffer *pDelegateChannel, REFIID tmarshal_riid,
61 IRpcChannelBuffer **ppChannel);
63 typedef struct _marshal_state {
69 /* used in the olerelay code to avoid having the L"" stuff added by debugstr_w */
70 static char *relaystr(WCHAR *in) {
71 char *tmp = (char *)debugstr_w(in);
73 tmp[strlen(tmp)-1] = '\0';
78 xbuf_resize(marshal_state *buf, DWORD newsize)
80 if(buf->size >= newsize)
85 buf->base = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buf->base, newsize);
91 buf->base = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, newsize);
100 xbuf_add(marshal_state *buf, const BYTE *stuff, DWORD size)
104 if(buf->size - buf->curoff < size)
106 hr = xbuf_resize(buf, buf->size + size + 100);
107 if(FAILED(hr)) return hr;
109 memcpy(buf->base+buf->curoff,stuff,size);
115 xbuf_get(marshal_state *buf, LPBYTE stuff, DWORD size) {
116 if (buf->size < buf->curoff+size) return E_FAIL;
117 memcpy(stuff,buf->base+buf->curoff,size);
123 xbuf_skip(marshal_state *buf, DWORD size) {
124 if (buf->size < buf->curoff+size) return E_FAIL;
130 _unmarshal_interface(marshal_state *buf, REFIID riid, LPUNKNOWN *pUnk) {
132 ULARGE_INTEGER newpos;
133 LARGE_INTEGER seekto;
138 TRACE("...%s...\n",debugstr_guid(riid));
141 hres = xbuf_get(buf,(LPBYTE)&xsize,sizeof(xsize));
143 ERR("xbuf_get failed\n");
147 if (xsize == 0) return S_OK;
149 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
151 ERR("Stream create failed %x\n",hres);
155 hres = IStream_Write(pStm,buf->base+buf->curoff,xsize,&res);
157 ERR("stream write %x\n",hres);
161 memset(&seekto,0,sizeof(seekto));
162 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
164 ERR("Failed Seek %x\n",hres);
168 hres = CoUnmarshalInterface(pStm,riid,(LPVOID*)pUnk);
170 ERR("Unmarshalling interface %s failed with %x\n",debugstr_guid(riid),hres);
174 IStream_Release(pStm);
175 return xbuf_skip(buf,xsize);
179 _marshal_interface(marshal_state *buf, REFIID riid, LPUNKNOWN pUnk) {
180 LPBYTE tempbuf = NULL;
181 IStream *pStm = NULL;
183 ULARGE_INTEGER newpos;
184 LARGE_INTEGER seekto;
190 /* this is valid, if for instance we serialize
191 * a VT_DISPATCH with NULL ptr which apparently
192 * can happen. S_OK to make sure we continue
195 WARN("pUnk is NULL\n");
197 return xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
202 TRACE("...%s...\n",debugstr_guid(riid));
204 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
206 ERR("Stream create failed %x\n",hres);
210 hres = CoMarshalInterface(pStm,riid,pUnk,0,NULL,0);
212 ERR("Marshalling interface %s failed with %x\n", debugstr_guid(riid), hres);
216 hres = IStream_Stat(pStm,&ststg,0);
218 ERR("Stream stat failed\n");
222 tempbuf = HeapAlloc(GetProcessHeap(), 0, ststg.cbSize.u.LowPart);
223 memset(&seekto,0,sizeof(seekto));
224 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
226 ERR("Failed Seek %x\n",hres);
230 hres = IStream_Read(pStm,tempbuf,ststg.cbSize.u.LowPart,&res);
232 ERR("Failed Read %x\n",hres);
236 xsize = ststg.cbSize.u.LowPart;
237 xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
238 hres = xbuf_add(buf,tempbuf,ststg.cbSize.u.LowPart);
240 HeapFree(GetProcessHeap(),0,tempbuf);
241 IStream_Release(pStm);
247 xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
248 if (pStm) IUnknown_Release(pStm);
249 HeapFree(GetProcessHeap(), 0, tempbuf);
253 /********************* OLE Proxy/Stub Factory ********************************/
254 static HRESULT WINAPI
255 PSFacBuf_QueryInterface(LPPSFACTORYBUFFER iface, REFIID iid, LPVOID *ppv) {
256 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)||IsEqualIID(iid,&IID_IUnknown)) {
257 *ppv = (LPVOID)iface;
258 /* No ref counting, static class */
261 FIXME("(%s) unknown IID?\n",debugstr_guid(iid));
262 return E_NOINTERFACE;
265 static ULONG WINAPI PSFacBuf_AddRef(LPPSFACTORYBUFFER iface) { return 2; }
266 static ULONG WINAPI PSFacBuf_Release(LPPSFACTORYBUFFER iface) { return 1; }
269 _get_typeinfo_for_iid(REFIID riid, ITypeInfo**ti) {
272 char tlguid[200],typelibkey[300],interfacekey[300],ver[100];
275 DWORD tlguidlen, verlen, type;
279 sprintf( interfacekey, "Interface\\{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\Typelib",
280 riid->Data1, riid->Data2, riid->Data3,
281 riid->Data4[0], riid->Data4[1], riid->Data4[2], riid->Data4[3],
282 riid->Data4[4], riid->Data4[5], riid->Data4[6], riid->Data4[7]
285 if (RegOpenKeyA(HKEY_CLASSES_ROOT,interfacekey,&ikey)) {
286 ERR("No %s key found.\n",interfacekey);
289 tlguidlen = sizeof(tlguid);
290 if (RegQueryValueExA(ikey,NULL,NULL,&type,(LPBYTE)tlguid,&tlguidlen)) {
291 ERR("Getting typelib guid failed.\n");
295 verlen = sizeof(ver);
296 if (RegQueryValueExA(ikey,"Version",NULL,&type,(LPBYTE)ver,&verlen)) {
297 ERR("Could not get version value?\n");
302 sprintf(typelibkey,"Typelib\\%s\\%s\\0\\win32",tlguid,ver);
303 tlfnlen = sizeof(tlfn);
304 if (RegQueryValueA(HKEY_CLASSES_ROOT,typelibkey,tlfn,&tlfnlen)) {
305 ERR("Could not get typelib fn?\n");
308 MultiByteToWideChar(CP_ACP, 0, tlfn, -1, tlfnW, sizeof(tlfnW) / sizeof(tlfnW[0]));
309 hres = LoadTypeLib(tlfnW,&tl);
311 ERR("Failed to load typelib for %s, but it should be there.\n",debugstr_guid(riid));
314 hres = ITypeLib_GetTypeInfoOfGuid(tl,riid,ti);
316 ERR("typelib does not contain info for %s?\n",debugstr_guid(riid));
317 ITypeLib_Release(tl);
320 ITypeLib_Release(tl);
325 * Determine the number of functions including all inherited functions.
326 * Note for non-dual dispinterfaces we simply return the size of IDispatch.
328 static HRESULT num_of_funcs(ITypeInfo *tinfo, unsigned int *num)
335 hres = ITypeInfo_GetTypeAttr(tinfo, &attr);
337 ERR("GetTypeAttr failed with %x\n",hres);
341 if(attr->typekind == TKIND_DISPATCH && (attr->wTypeFlags & TYPEFLAG_FDUAL))
344 hres = ITypeInfo_GetRefTypeOfImplType(tinfo, -1, &href);
347 ERR("Unable to get interface href from dual dispinterface\n");
350 hres = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
353 ERR("Unable to get interface from dual dispinterface\n");
356 hres = num_of_funcs(tinfo2, num);
357 ITypeInfo_Release(tinfo2);
361 *num = attr->cbSizeVft / 4;
365 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
371 #include "pshpack1.h"
373 typedef struct _TMAsmProxy {
388 # warning You need to implement stubless proxies for your architecture
389 typedef struct _TMAsmProxy {
393 typedef struct _TMProxyImpl {
395 const IRpcProxyBufferVtbl *lpvtbl2;
398 TMAsmProxy *asmstubs;
400 IRpcChannelBuffer* chanbuf;
402 CRITICAL_SECTION crit;
403 IUnknown *outerunknown;
405 IRpcProxyBuffer *dispatch_proxy;
408 static HRESULT WINAPI
409 TMProxyImpl_QueryInterface(LPRPCPROXYBUFFER iface, REFIID riid, LPVOID *ppv)
412 if (IsEqualIID(riid,&IID_IUnknown)||IsEqualIID(riid,&IID_IRpcProxyBuffer)) {
413 *ppv = (LPVOID)iface;
414 IRpcProxyBuffer_AddRef(iface);
417 FIXME("no interface for %s\n",debugstr_guid(riid));
418 return E_NOINTERFACE;
422 TMProxyImpl_AddRef(LPRPCPROXYBUFFER iface)
424 ICOM_THIS_MULTI(TMProxyImpl,lpvtbl2,iface);
425 ULONG refCount = InterlockedIncrement(&This->ref);
427 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
433 TMProxyImpl_Release(LPRPCPROXYBUFFER iface)
435 ICOM_THIS_MULTI(TMProxyImpl,lpvtbl2,iface);
436 ULONG refCount = InterlockedDecrement(&This->ref);
438 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
442 if (This->dispatch_proxy) IRpcProxyBuffer_Release(This->dispatch_proxy);
443 This->crit.DebugInfo->Spare[0] = 0;
444 DeleteCriticalSection(&This->crit);
445 if (This->chanbuf) IRpcChannelBuffer_Release(This->chanbuf);
446 VirtualFree(This->asmstubs, 0, MEM_RELEASE);
447 HeapFree(GetProcessHeap(), 0, This->lpvtbl);
448 ITypeInfo_Release(This->tinfo);
454 static HRESULT WINAPI
456 LPRPCPROXYBUFFER iface,IRpcChannelBuffer* pRpcChannelBuffer)
458 ICOM_THIS_MULTI(TMProxyImpl, lpvtbl2, iface);
460 TRACE("(%p)\n", pRpcChannelBuffer);
462 EnterCriticalSection(&This->crit);
464 IRpcChannelBuffer_AddRef(pRpcChannelBuffer);
465 This->chanbuf = pRpcChannelBuffer;
467 LeaveCriticalSection(&This->crit);
469 if (This->dispatch_proxy)
471 IRpcChannelBuffer *pDelegateChannel;
472 HRESULT hr = TMarshalDispatchChannel_Create(pRpcChannelBuffer, &This->iid, &pDelegateChannel);
475 hr = IRpcProxyBuffer_Connect(This->dispatch_proxy, pDelegateChannel);
476 IRpcChannelBuffer_Release(pDelegateChannel);
484 TMProxyImpl_Disconnect(LPRPCPROXYBUFFER iface)
486 ICOM_THIS_MULTI(TMProxyImpl, lpvtbl2, iface);
490 EnterCriticalSection(&This->crit);
492 IRpcChannelBuffer_Release(This->chanbuf);
493 This->chanbuf = NULL;
495 LeaveCriticalSection(&This->crit);
497 if (This->dispatch_proxy)
498 IRpcProxyBuffer_Disconnect(This->dispatch_proxy);
502 static const IRpcProxyBufferVtbl tmproxyvtable = {
503 TMProxyImpl_QueryInterface,
507 TMProxyImpl_Disconnect
510 /* how much space do we use on stack in DWORD steps. */
512 _argsize(TYPEDESC *tdesc, ITypeInfo *tinfo) {
516 return 8/sizeof(DWORD);
518 return sizeof(double)/sizeof(DWORD);
520 return sizeof(CY)/sizeof(DWORD);
522 return sizeof(DATE)/sizeof(DWORD);
524 return (sizeof(DECIMAL)+3)/sizeof(DWORD);
526 return (sizeof(VARIANT)+3)/sizeof(DWORD);
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);
610 TRACE("(tdesc.vt %s)\n",debugstr_vt(tdesc->vt));
613 case VT_EMPTY: /* nothing. empty variant for instance */
620 if (debugout) TRACE_(olerelay)("%x%x\n",arg[0],arg[1]);
622 hres = xbuf_add(buf,(LPBYTE)arg,8);
632 if (debugout) TRACE_(olerelay)("%x\n",*arg);
634 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
639 if (debugout) TRACE_(olerelay)("%04x\n",*arg & 0xffff);
641 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
646 if (debugout) TRACE_(olerelay)("%02x\n",*arg & 0xff);
648 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
652 if (debugout) TRACE_(olerelay)("&0x%x\n",*arg);
654 hres = xbuf_add(buf,(LPBYTE)(DWORD*)*arg,sizeof(DWORD));
655 /* do not dealloc at this time */
659 VARIANT *vt = (VARIANT*)arg;
660 DWORD vttype = V_VT(vt);
662 if (debugout) TRACE_(olerelay)("Vt(%s%s)(",debugstr_vt(vttype),debugstr_vf(vttype));
665 hres = xbuf_add(buf,(LPBYTE)&vttype,sizeof(vttype));
666 if (hres) return hres;
668 /* need to recurse since we need to free the stuff */
669 hres = serialize_param(tinfo,writeit,debugout,dealloc,&tdesc2,(DWORD*)&(V_I4(vt)),buf);
670 if (debugout) TRACE_(olerelay)(")");
673 case VT_BSTR|VT_BYREF: {
674 if (debugout) TRACE_(olerelay)("[byref]'%s'", *(BSTR*)*arg ? relaystr(*((BSTR*)*arg)) : "<bstr NULL>");
676 /* ptr to ptr to magic widestring, basically */
677 BSTR *bstr = (BSTR *) *arg;
680 /* -1 means "null string" which is equivalent to empty string */
682 hres = xbuf_add(buf, (LPBYTE)&len,sizeof(DWORD));
683 if (hres) return hres;
685 len = *((DWORD*)*bstr-1)/sizeof(WCHAR);
686 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
687 if (hres) return hres;
688 hres = xbuf_add(buf,(LPBYTE)*bstr,len * sizeof(WCHAR));
689 if (hres) return hres;
693 if (dealloc && arg) {
694 BSTR *str = *((BSTR **)arg);
703 TRACE_(olerelay)("%s",relaystr((WCHAR*)*arg));
705 TRACE_(olerelay)("<bstr NULL>");
708 BSTR bstr = (BSTR)*arg;
712 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
713 if (hres) return hres;
715 len = *((DWORD*)bstr-1)/sizeof(WCHAR);
716 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
717 if (hres) return hres;
718 hres = xbuf_add(buf,(LPBYTE)bstr,len * sizeof(WCHAR));
719 if (hres) return hres;
724 SysFreeString((BSTR)*arg);
729 BOOL derefhere = TRUE;
731 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
735 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
737 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
740 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
741 switch (tattr->typekind) {
743 if (tattr->tdescAlias.vt == VT_USERDEFINED)
745 DWORD href = tattr->tdescAlias.u.hreftype;
746 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
747 ITypeInfo_Release(tinfo2);
748 hres = ITypeInfo_GetRefTypeInfo(tinfo,href,&tinfo2);
750 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
753 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
754 derefhere = (tattr->typekind != TKIND_DISPATCH && tattr->typekind != TKIND_INTERFACE);
757 case TKIND_ENUM: /* confirmed */
758 case TKIND_RECORD: /* FIXME: mostly untested */
760 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
761 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
765 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
769 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
770 ITypeInfo_Release(tinfo2);
773 if (debugout) TRACE_(olerelay)("*");
774 /* Write always, so the other side knows when it gets a NULL pointer.
776 cookie = *arg ? 0x42424242 : 0;
777 hres = xbuf_add(buf,(LPBYTE)&cookie,sizeof(cookie));
781 if (debugout) TRACE_(olerelay)("NULL");
784 hres = serialize_param(tinfo,writeit,debugout,dealloc,tdesc->u.lptdesc,(DWORD*)*arg,buf);
785 if (derefhere && dealloc) HeapFree(GetProcessHeap(),0,(LPVOID)*arg);
789 if (debugout) TRACE_(olerelay)("unk(0x%x)",*arg);
791 hres = _marshal_interface(buf,&IID_IUnknown,(LPUNKNOWN)*arg);
792 if (dealloc && *(IUnknown **)arg)
793 IUnknown_Release((LPUNKNOWN)*arg);
796 if (debugout) TRACE_(olerelay)("idisp(0x%x)",*arg);
798 hres = _marshal_interface(buf,&IID_IDispatch,(LPUNKNOWN)*arg);
799 if (dealloc && *(IUnknown **)arg)
800 IUnknown_Release((LPUNKNOWN)*arg);
803 if (debugout) TRACE_(olerelay)("<void>");
805 case VT_USERDEFINED: {
809 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
811 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
814 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
815 switch (tattr->typekind) {
817 case TKIND_INTERFACE:
819 hres=_marshal_interface(buf,&(tattr->guid),(LPUNKNOWN)arg);
821 IUnknown_Release((LPUNKNOWN)arg);
825 if (debugout) TRACE_(olerelay)("{");
826 for (i=0;i<tattr->cVars;i++) {
831 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
833 ERR("Could not get vardesc of %d\n",i);
836 elem2 = &vdesc->elemdescVar;
837 tdesc2 = &elem2->tdesc;
838 hres = serialize_param(
844 (DWORD*)(((LPBYTE)arg)+vdesc->u.oInst),
847 ITypeInfo_ReleaseVarDesc(tinfo2, vdesc);
850 if (debugout && (i<(tattr->cVars-1)))
851 TRACE_(olerelay)(",");
853 if (debugout) TRACE_(olerelay)("}");
857 hres = serialize_param(tinfo2,writeit,debugout,dealloc,&tattr->tdescAlias,arg,buf);
861 if (debugout) TRACE_(olerelay)("%x",*arg);
863 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
866 FIXME("Unhandled typekind %d\n",tattr->typekind);
870 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
871 ITypeInfo_Release(tinfo2);
875 ARRAYDESC *adesc = tdesc->u.lpadesc;
878 if (debugout) TRACE_(olerelay)("carr");
879 for (i=0;i<adesc->cDims;i++) {
880 if (debugout) TRACE_(olerelay)("[%d]",adesc->rgbounds[i].cElements);
881 arrsize *= adesc->rgbounds[i].cElements;
883 if (debugout) TRACE_(olerelay)("(vt %s)",debugstr_vt(adesc->tdescElem.vt));
884 if (debugout) TRACE_(olerelay)("[");
885 for (i=0;i<arrsize;i++) {
886 hres = serialize_param(tinfo, writeit, debugout, dealloc, &adesc->tdescElem, (DWORD*)((LPBYTE)arg+i*_xsize(&adesc->tdescElem, tinfo)), buf);
889 if (debugout && (i<arrsize-1)) TRACE_(olerelay)(",");
891 if (debugout) TRACE_(olerelay)("]");
897 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
898 ULONG size = LPSAFEARRAY_UserSize(&flags, buf->curoff, (LPSAFEARRAY *)arg);
899 xbuf_resize(buf, size);
900 LPSAFEARRAY_UserMarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
906 ERR("Unhandled marshal type %d.\n",tdesc->vt);
923 TRACE("vt %s at %p\n",debugstr_vt(tdesc->vt),arg);
928 if (debugout) TRACE_(olerelay)("<empty>\n");
931 if (debugout) TRACE_(olerelay)("<null>\n");
934 VARIANT *vt = (VARIANT*)arg;
939 hres = xbuf_get(buf,(LPBYTE)&vttype,sizeof(vttype));
941 FIXME("vt type not read?\n");
944 memset(&tdesc2,0,sizeof(tdesc2));
947 if (debugout) TRACE_(olerelay)("Vt(%s%s)(",debugstr_vt(vttype),debugstr_vf(vttype));
948 hres = deserialize_param(tinfo, readit, debugout, alloc, &tdesc2, (DWORD*)&(V_I4(vt)), buf);
949 TRACE_(olerelay)(")");
961 hres = xbuf_get(buf,(LPBYTE)arg,8);
962 if (hres) ERR("Failed to read integer 8 byte\n");
964 if (debugout) TRACE_(olerelay)("%x%x",arg[0],arg[1]);
974 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
975 if (hres) ERR("Failed to read integer 4 byte\n");
977 if (debugout) TRACE_(olerelay)("%x",*arg);
983 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
984 if (hres) ERR("Failed to read integer 4 byte\n");
987 if (debugout) TRACE_(olerelay)("%04x",*arg & 0xffff);
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)("%02x",*arg & 0xff);
1002 *arg = (DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
1004 hres = xbuf_get(buf,(LPBYTE)*arg,sizeof(DWORD));
1005 if (hres) ERR("Failed to read integer 4 byte\n");
1007 if (debugout) TRACE_(olerelay)("&0x%x",*(DWORD*)*arg);
1009 case VT_BSTR|VT_BYREF: {
1010 BSTR **bstr = (BSTR **)arg;
1015 hres = xbuf_get(buf,(LPBYTE)&len,sizeof(DWORD));
1017 ERR("failed to read bstr klen\n");
1021 *bstr = CoTaskMemAlloc(sizeof(BSTR *));
1023 if (debugout) TRACE_(olerelay)("<bstr NULL>");
1025 str = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(len+1)*sizeof(WCHAR));
1026 hres = xbuf_get(buf,(LPBYTE)str,len*sizeof(WCHAR));
1028 ERR("Failed to read BSTR.\n");
1029 HeapFree(GetProcessHeap(),0,str);
1032 *bstr = CoTaskMemAlloc(sizeof(BSTR *));
1033 **bstr = SysAllocStringLen(str,len);
1034 if (debugout) TRACE_(olerelay)("%s",relaystr(str));
1035 HeapFree(GetProcessHeap(),0,str);
1047 hres = xbuf_get(buf,(LPBYTE)&len,sizeof(DWORD));
1049 ERR("failed to read bstr klen\n");
1054 if (debugout) TRACE_(olerelay)("<bstr NULL>");
1056 str = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(len+1)*sizeof(WCHAR));
1057 hres = xbuf_get(buf,(LPBYTE)str,len*sizeof(WCHAR));
1059 ERR("Failed to read BSTR.\n");
1060 HeapFree(GetProcessHeap(),0,str);
1063 *arg = (DWORD)SysAllocStringLen(str,len);
1064 if (debugout) TRACE_(olerelay)("%s",relaystr(str));
1065 HeapFree(GetProcessHeap(),0,str);
1074 BOOL derefhere = TRUE;
1076 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
1080 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
1082 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
1085 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1086 switch (tattr->typekind) {
1088 if (tattr->tdescAlias.vt == VT_USERDEFINED)
1090 DWORD href = tattr->tdescAlias.u.hreftype;
1091 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
1092 ITypeInfo_Release(tinfo2);
1093 hres = ITypeInfo_GetRefTypeInfo(tinfo,href,&tinfo2);
1095 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
1098 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1099 derefhere = (tattr->typekind != TKIND_DISPATCH && tattr->typekind != TKIND_INTERFACE);
1102 case TKIND_ENUM: /* confirmed */
1103 case TKIND_RECORD: /* FIXME: mostly untested */
1105 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
1106 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
1110 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
1114 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1115 ITypeInfo_Release(tinfo2);
1117 /* read it in all cases, we need to know if we have
1118 * NULL pointer or not.
1120 hres = xbuf_get(buf,(LPBYTE)&cookie,sizeof(cookie));
1122 ERR("Failed to load pointer cookie.\n");
1125 if (cookie != 0x42424242) {
1126 /* we read a NULL ptr from the remote side */
1127 if (debugout) TRACE_(olerelay)("NULL");
1131 if (debugout) TRACE_(olerelay)("*");
1133 /* Allocate space for the referenced struct */
1135 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,_xsize(tdesc->u.lptdesc, tinfo));
1138 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, (LPDWORD)*arg, buf);
1140 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, arg, buf);
1143 /* FIXME: UNKNOWN is unknown ..., but allocate 4 byte for it */
1145 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
1148 hres = _unmarshal_interface(buf,&IID_IUnknown,(LPUNKNOWN*)arg);
1150 TRACE_(olerelay)("unk(%p)",arg);
1155 hres = _unmarshal_interface(buf,&IID_IDispatch,(LPUNKNOWN*)arg);
1157 TRACE_(olerelay)("idisp(%p)",arg);
1160 if (debugout) TRACE_(olerelay)("<void>");
1162 case VT_USERDEFINED: {
1166 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
1168 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
1171 hres = ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1173 ERR("Could not get typeattr in VT_USERDEFINED.\n");
1175 switch (tattr->typekind) {
1176 case TKIND_DISPATCH:
1177 case TKIND_INTERFACE:
1179 hres = _unmarshal_interface(buf,&(tattr->guid),(LPUNKNOWN*)arg);
1181 case TKIND_RECORD: {
1184 if (debugout) TRACE_(olerelay)("{");
1185 for (i=0;i<tattr->cVars;i++) {
1188 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
1190 ERR("Could not get vardesc of %d\n",i);
1191 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1192 ITypeInfo_Release(tinfo2);
1195 hres = deserialize_param(
1200 &vdesc->elemdescVar.tdesc,
1201 (DWORD*)(((LPBYTE)arg)+vdesc->u.oInst),
1204 ITypeInfo2_ReleaseVarDesc(tinfo2, vdesc);
1205 if (debugout && (i<tattr->cVars-1)) TRACE_(olerelay)(",");
1207 if (debugout) TRACE_(olerelay)("}");
1211 hres = deserialize_param(tinfo2,readit,debugout,alloc,&tattr->tdescAlias,arg,buf);
1215 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
1216 if (hres) ERR("Failed to read enum (4 byte)\n");
1218 if (debugout) TRACE_(olerelay)("%x",*arg);
1221 ERR("Unhandled typekind %d\n",tattr->typekind);
1225 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1228 ERR("failed to stuballoc in TKIND_RECORD.\n");
1229 ITypeInfo_Release(tinfo2);
1233 /* arg is pointing to the start of the array. */
1234 ARRAYDESC *adesc = tdesc->u.lpadesc;
1237 if (adesc->cDims > 1) FIXME("cDims > 1 in VT_CARRAY. Does it work?\n");
1238 for (i=0;i<adesc->cDims;i++)
1239 arrsize *= adesc->rgbounds[i].cElements;
1240 for (i=0;i<arrsize;i++)
1247 (DWORD*)((LPBYTE)(arg)+i*_xsize(&adesc->tdescElem, tinfo)),
1252 case VT_SAFEARRAY: {
1255 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
1256 unsigned char *buffer;
1257 buffer = LPSAFEARRAY_UserUnmarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
1258 buf->curoff = buffer - buf->base;
1263 ERR("No handler for VT type %d!\n",tdesc->vt);
1269 /* Retrieves a function's funcdesc, searching back into inherited interfaces. */
1270 static HRESULT get_funcdesc(ITypeInfo *tinfo, int iMethod, ITypeInfo **tactual, const FUNCDESC **fdesc,
1271 BSTR *iname, BSTR *fname, UINT *num)
1275 UINT inherited_funcs = 0;
1278 if (fname) *fname = NULL;
1279 if (iname) *iname = NULL;
1283 hr = ITypeInfo_GetTypeAttr(tinfo, &attr);
1286 ERR("GetTypeAttr failed with %x\n",hr);
1290 if(attr->typekind == TKIND_DISPATCH)
1292 if(attr->wTypeFlags & TYPEFLAG_FDUAL)
1297 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, -1, &href);
1300 ERR("Cannot get interface href from dual dispinterface\n");
1301 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1304 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
1307 ERR("Cannot get interface from dual dispinterface\n");
1308 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1311 hr = get_funcdesc(tinfo2, iMethod, tactual, fdesc, iname, fname, num);
1312 ITypeInfo_Release(tinfo2);
1313 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1316 ERR("Shouldn't be called with a non-dual dispinterface\n");
1320 impl_types = attr->cImplTypes;
1321 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1323 for (i = 0; i < impl_types; i++)
1326 ITypeInfo *pSubTypeInfo;
1329 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, i, &href);
1330 if (FAILED(hr)) return hr;
1331 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &pSubTypeInfo);
1332 if (FAILED(hr)) return hr;
1334 hr = get_funcdesc(pSubTypeInfo, iMethod, tactual, fdesc, iname, fname, &sub_funcs);
1335 inherited_funcs += sub_funcs;
1336 ITypeInfo_Release(pSubTypeInfo);
1337 if(SUCCEEDED(hr)) return hr;
1339 if(iMethod < inherited_funcs)
1341 ERR("shouldn't be here\n");
1342 return E_INVALIDARG;
1345 for(i = inherited_funcs; i <= iMethod; i++)
1347 hr = ITypeInfoImpl_GetInternalFuncDesc(tinfo, i - inherited_funcs, fdesc);
1355 /* found it. We don't care about num so zero it */
1358 ITypeInfo_AddRef(*tactual);
1359 if (fname) ITypeInfo_GetDocumentation(tinfo,(*fdesc)->memid,fname,NULL,NULL,NULL);
1360 if (iname) ITypeInfo_GetDocumentation(tinfo,-1,iname,NULL,NULL,NULL);
1364 static inline BOOL is_in_elem(const ELEMDESC *elem)
1366 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN || !elem->u.paramdesc.wParamFlags);
1369 static inline BOOL is_out_elem(const ELEMDESC *elem)
1371 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT || !elem->u.paramdesc.wParamFlags);
1375 xCall(LPVOID retptr, int method, TMProxyImpl *tpinfo /*, args */)
1377 DWORD *args = ((DWORD*)&tpinfo)+1, *xargs;
1378 const FUNCDESC *fdesc;
1380 int i, relaydeb = TRACE_ON(olerelay);
1387 DWORD remoteresult = 0;
1389 IRpcChannelBuffer *chanbuf;
1391 EnterCriticalSection(&tpinfo->crit);
1393 hres = get_funcdesc(tpinfo->tinfo,method,&tinfo,&fdesc,&iname,&fname,NULL);
1395 ERR("Did not find typeinfo/funcdesc entry for method %d!\n",method);
1396 LeaveCriticalSection(&tpinfo->crit);
1400 if (!tpinfo->chanbuf)
1402 WARN("Tried to use disconnected proxy\n");
1403 ITypeInfo_Release(tinfo);
1404 LeaveCriticalSection(&tpinfo->crit);
1405 return RPC_E_DISCONNECTED;
1407 chanbuf = tpinfo->chanbuf;
1408 IRpcChannelBuffer_AddRef(chanbuf);
1410 LeaveCriticalSection(&tpinfo->crit);
1413 TRACE_(olerelay)("->");
1415 TRACE_(olerelay)("%s:",relaystr(iname));
1417 TRACE_(olerelay)("%s(%d)",relaystr(fname),method);
1419 TRACE_(olerelay)("%d",method);
1420 TRACE_(olerelay)("(");
1423 SysFreeString(iname);
1424 SysFreeString(fname);
1426 memset(&buf,0,sizeof(buf));
1428 /* normal typelib driven serializing */
1430 /* Need them for hack below */
1431 memset(names,0,sizeof(names));
1432 if (ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames))
1434 if (nrofnames > sizeof(names)/sizeof(names[0]))
1435 ERR("Need more names!\n");
1438 for (i=0;i<fdesc->cParams;i++) {
1439 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1441 if (i) TRACE_(olerelay)(",");
1442 if (i+1<nrofnames && names[i+1])
1443 TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1445 /* No need to marshal other data than FIN and any VT_PTR. */
1446 if (!is_in_elem(elem) && (elem->tdesc.vt != VT_PTR)) {
1447 xargs+=_argsize(&elem->tdesc, tinfo);
1448 if (relaydeb) TRACE_(olerelay)("[out]");
1451 hres = serialize_param(
1462 ERR("Failed to serialize param, hres %x\n",hres);
1465 xargs+=_argsize(&elem->tdesc, tinfo);
1467 if (relaydeb) TRACE_(olerelay)(")");
1469 memset(&msg,0,sizeof(msg));
1470 msg.cbBuffer = buf.curoff;
1471 msg.iMethod = method;
1472 hres = IRpcChannelBuffer_GetBuffer(chanbuf,&msg,&(tpinfo->iid));
1474 ERR("RpcChannelBuffer GetBuffer failed, %x\n",hres);
1477 memcpy(msg.Buffer,buf.base,buf.curoff);
1478 if (relaydeb) TRACE_(olerelay)("\n");
1479 hres = IRpcChannelBuffer_SendReceive(chanbuf,&msg,&status);
1481 ERR("RpcChannelBuffer SendReceive failed, %x\n",hres);
1485 if (relaydeb) TRACE_(olerelay)(" status = %08x (",status);
1487 buf.base = HeapReAlloc(GetProcessHeap(),0,buf.base,msg.cbBuffer);
1489 buf.base = HeapAlloc(GetProcessHeap(),0,msg.cbBuffer);
1490 buf.size = msg.cbBuffer;
1491 memcpy(buf.base,msg.Buffer,buf.size);
1494 /* generic deserializer using typelib description */
1497 for (i=0;i<fdesc->cParams;i++) {
1498 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1501 if (i) TRACE_(olerelay)(",");
1502 if (i+1<nrofnames && names[i+1]) TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1504 /* No need to marshal other data than FOUT and any VT_PTR */
1505 if (!is_out_elem(elem) && (elem->tdesc.vt != VT_PTR)) {
1506 xargs += _argsize(&elem->tdesc, tinfo);
1507 if (relaydeb) TRACE_(olerelay)("[in]");
1510 hres = deserialize_param(
1520 ERR("Failed to unmarshall param, hres %x\n",hres);
1524 xargs += _argsize(&elem->tdesc, tinfo);
1527 hres = xbuf_get(&buf, (LPBYTE)&remoteresult, sizeof(DWORD));
1530 if (relaydeb) TRACE_(olerelay)(") = %08x\n", remoteresult);
1532 hres = remoteresult;
1535 IRpcChannelBuffer_FreeBuffer(chanbuf,&msg);
1536 for (i = 0; i < nrofnames; i++)
1537 SysFreeString(names[i]);
1538 HeapFree(GetProcessHeap(),0,buf.base);
1539 IRpcChannelBuffer_Release(chanbuf);
1540 ITypeInfo_Release(tinfo);
1541 TRACE("-- 0x%08x\n", hres);
1545 static HRESULT WINAPI ProxyIUnknown_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
1547 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1549 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1551 if (proxy->outerunknown)
1552 return IUnknown_QueryInterface(proxy->outerunknown, riid, ppv);
1554 FIXME("No interface\n");
1555 return E_NOINTERFACE;
1558 static ULONG WINAPI ProxyIUnknown_AddRef(IUnknown *iface)
1560 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1564 if (proxy->outerunknown)
1565 return IUnknown_AddRef(proxy->outerunknown);
1567 return 2; /* FIXME */
1570 static ULONG WINAPI ProxyIUnknown_Release(IUnknown *iface)
1572 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1576 if (proxy->outerunknown)
1577 return IUnknown_Release(proxy->outerunknown);
1579 return 1; /* FIXME */
1582 static HRESULT WINAPI ProxyIDispatch_GetTypeInfoCount(LPDISPATCH iface, UINT * pctinfo)
1584 TMProxyImpl *This = (TMProxyImpl *)iface;
1586 TRACE("(%p)\n", pctinfo);
1588 return IDispatch_GetTypeInfoCount(This->dispatch, pctinfo);
1591 static HRESULT WINAPI ProxyIDispatch_GetTypeInfo(LPDISPATCH iface, UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo)
1593 TMProxyImpl *This = (TMProxyImpl *)iface;
1595 TRACE("(%d, %x, %p)\n", iTInfo, lcid, ppTInfo);
1597 return IDispatch_GetTypeInfo(This->dispatch, iTInfo, lcid, ppTInfo);
1600 static HRESULT WINAPI ProxyIDispatch_GetIDsOfNames(LPDISPATCH iface, REFIID riid, LPOLESTR * rgszNames, UINT cNames, LCID lcid, DISPID * rgDispId)
1602 TMProxyImpl *This = (TMProxyImpl *)iface;
1604 TRACE("(%s, %p, %d, 0x%x, %p)\n", debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);
1606 return IDispatch_GetIDsOfNames(This->dispatch, riid, rgszNames,
1607 cNames, lcid, rgDispId);
1610 static HRESULT WINAPI ProxyIDispatch_Invoke(LPDISPATCH iface, DISPID dispIdMember, REFIID riid, LCID lcid,
1611 WORD wFlags, DISPPARAMS * pDispParams, VARIANT * pVarResult,
1612 EXCEPINFO * pExcepInfo, UINT * puArgErr)
1614 TMProxyImpl *This = (TMProxyImpl *)iface;
1616 TRACE("(%d, %s, 0x%x, 0x%x, %p, %p, %p, %p)\n", dispIdMember,
1617 debugstr_guid(riid), lcid, wFlags, pDispParams, pVarResult,
1618 pExcepInfo, puArgErr);
1620 return IDispatch_Invoke(This->dispatch, dispIdMember, riid, lcid,
1621 wFlags, pDispParams, pVarResult, pExcepInfo,
1627 const IRpcChannelBufferVtbl *lpVtbl;
1629 /* the IDispatch-derived interface we are handling */
1631 IRpcChannelBuffer *pDelegateChannel;
1632 } TMarshalDispatchChannel;
1634 static HRESULT WINAPI TMarshalDispatchChannel_QueryInterface(LPRPCCHANNELBUFFER iface, REFIID riid, LPVOID *ppv)
1637 if (IsEqualIID(riid,&IID_IRpcChannelBuffer) || IsEqualIID(riid,&IID_IUnknown))
1639 *ppv = (LPVOID)iface;
1640 IUnknown_AddRef(iface);
1643 return E_NOINTERFACE;
1646 static ULONG WINAPI TMarshalDispatchChannel_AddRef(LPRPCCHANNELBUFFER iface)
1648 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1649 return InterlockedIncrement(&This->refs);
1652 static ULONG WINAPI TMarshalDispatchChannel_Release(LPRPCCHANNELBUFFER iface)
1654 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1657 ref = InterlockedDecrement(&This->refs);
1661 IRpcChannelBuffer_Release(This->pDelegateChannel);
1662 HeapFree(GetProcessHeap(), 0, This);
1666 static HRESULT WINAPI TMarshalDispatchChannel_GetBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg, REFIID riid)
1668 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1669 TRACE("(%p, %s)\n", olemsg, debugstr_guid(riid));
1670 /* Note: we are pretending to invoke a method on the interface identified
1671 * by tmarshal_iid so that we can re-use the IDispatch proxy/stub code
1672 * without the RPC runtime getting confused by not exporting an IDispatch interface */
1673 return IRpcChannelBuffer_GetBuffer(This->pDelegateChannel, olemsg, &This->tmarshal_iid);
1676 static HRESULT WINAPI TMarshalDispatchChannel_SendReceive(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE *olemsg, ULONG *pstatus)
1678 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1679 TRACE("(%p, %p)\n", olemsg, pstatus);
1680 return IRpcChannelBuffer_SendReceive(This->pDelegateChannel, olemsg, pstatus);
1683 static HRESULT WINAPI TMarshalDispatchChannel_FreeBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg)
1685 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1686 TRACE("(%p)\n", olemsg);
1687 return IRpcChannelBuffer_FreeBuffer(This->pDelegateChannel, olemsg);
1690 static HRESULT WINAPI TMarshalDispatchChannel_GetDestCtx(LPRPCCHANNELBUFFER iface, DWORD* pdwDestContext, void** ppvDestContext)
1692 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1693 TRACE("(%p,%p)\n", pdwDestContext, ppvDestContext);
1694 return IRpcChannelBuffer_GetDestCtx(This->pDelegateChannel, pdwDestContext, ppvDestContext);
1697 static HRESULT WINAPI TMarshalDispatchChannel_IsConnected(LPRPCCHANNELBUFFER iface)
1699 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1701 return IRpcChannelBuffer_IsConnected(This->pDelegateChannel);
1704 static const IRpcChannelBufferVtbl TMarshalDispatchChannelVtbl =
1706 TMarshalDispatchChannel_QueryInterface,
1707 TMarshalDispatchChannel_AddRef,
1708 TMarshalDispatchChannel_Release,
1709 TMarshalDispatchChannel_GetBuffer,
1710 TMarshalDispatchChannel_SendReceive,
1711 TMarshalDispatchChannel_FreeBuffer,
1712 TMarshalDispatchChannel_GetDestCtx,
1713 TMarshalDispatchChannel_IsConnected
1716 static HRESULT TMarshalDispatchChannel_Create(
1717 IRpcChannelBuffer *pDelegateChannel, REFIID tmarshal_riid,
1718 IRpcChannelBuffer **ppChannel)
1720 TMarshalDispatchChannel *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1722 return E_OUTOFMEMORY;
1724 This->lpVtbl = &TMarshalDispatchChannelVtbl;
1726 IRpcChannelBuffer_AddRef(pDelegateChannel);
1727 This->pDelegateChannel = pDelegateChannel;
1728 This->tmarshal_iid = *tmarshal_riid;
1730 *ppChannel = (IRpcChannelBuffer *)&This->lpVtbl;
1735 static inline HRESULT get_facbuf_for_iid(REFIID riid, IPSFactoryBuffer **facbuf)
1740 if ((hr = CoGetPSClsid(riid, &clsid)))
1742 return CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL,
1743 &IID_IPSFactoryBuffer, (LPVOID*)facbuf);
1746 static HRESULT init_proxy_entry_point(TMProxyImpl *proxy, unsigned int num)
1749 /* nrofargs without This */
1752 TMAsmProxy *xasm = proxy->asmstubs + num;
1754 const FUNCDESC *fdesc;
1756 hres = get_funcdesc(proxy->tinfo, num, &tinfo2, &fdesc, NULL, NULL, NULL);
1758 ERR("GetFuncDesc %x should not fail here.\n",hres);
1761 ITypeInfo_Release(tinfo2);
1762 /* some args take more than 4 byte on the stack */
1764 for (j=0;j<fdesc->cParams;j++)
1765 nrofargs += _argsize(&fdesc->lprgelemdescParam[j].tdesc, proxy->tinfo);
1768 if (fdesc->callconv != CC_STDCALL) {
1769 ERR("calling convention is not stdcall????\n");
1772 /* popl %eax - return ptr
1779 * arg3 arg2 arg1 <method> <returnptr>
1781 xasm->popleax = 0x58;
1782 xasm->pushlval = 0x68;
1784 xasm->pushleax = 0x50;
1785 xasm->lcall = 0xe8; /* relative jump */
1786 xasm->xcall = (DWORD)xCall;
1787 xasm->xcall -= (DWORD)&(xasm->lret);
1789 xasm->bytestopop = (nrofargs+2)*4; /* pop args, This, iMethod */
1791 proxy->lpvtbl[num] = xasm;
1793 FIXME("not implemented on non i386\n");
1799 static HRESULT WINAPI
1800 PSFacBuf_CreateProxy(
1801 LPPSFACTORYBUFFER iface, IUnknown* pUnkOuter, REFIID riid,
1802 IRpcProxyBuffer **ppProxy, LPVOID *ppv)
1806 unsigned int i, nroffuncs;
1809 BOOL defer_to_dispatch = FALSE;
1811 TRACE("(...%s...)\n",debugstr_guid(riid));
1812 hres = _get_typeinfo_for_iid(riid,&tinfo);
1814 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
1818 hres = num_of_funcs(tinfo, &nroffuncs);
1820 ERR("Cannot get number of functions for typeinfo %s\n",debugstr_guid(riid));
1821 ITypeInfo_Release(tinfo);
1825 proxy = CoTaskMemAlloc(sizeof(TMProxyImpl));
1826 if (!proxy) return E_OUTOFMEMORY;
1828 assert(sizeof(TMAsmProxy) == 16);
1830 proxy->dispatch = NULL;
1831 proxy->dispatch_proxy = NULL;
1832 proxy->outerunknown = pUnkOuter;
1833 proxy->asmstubs = VirtualAlloc(NULL, sizeof(TMAsmProxy) * nroffuncs, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
1834 if (!proxy->asmstubs) {
1835 ERR("Could not commit pages for proxy thunks\n");
1836 CoTaskMemFree(proxy);
1837 return E_OUTOFMEMORY;
1839 proxy->lpvtbl2 = &tmproxyvtable;
1840 /* one reference for the proxy */
1842 proxy->tinfo = tinfo;
1846 InitializeCriticalSection(&proxy->crit);
1847 proxy->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TMProxyImpl.crit");
1849 proxy->lpvtbl = HeapAlloc(GetProcessHeap(),0,sizeof(LPBYTE)*nroffuncs);
1851 /* if we derive from IDispatch then defer to its proxy for its methods */
1852 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
1855 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
1857 IPSFactoryBuffer *factory_buffer;
1858 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
1861 hres = IPSFactoryBuffer_CreateProxy(factory_buffer, NULL,
1862 &IID_IDispatch, &proxy->dispatch_proxy,
1863 (void **)&proxy->dispatch);
1864 IPSFactoryBuffer_Release(factory_buffer);
1866 if ((hres == S_OK) && (nroffuncs < 7))
1868 ERR("nroffuncs calculated incorrectly (%d)\n", nroffuncs);
1869 hres = E_UNEXPECTED;
1873 defer_to_dispatch = TRUE;
1876 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
1879 for (i=0;i<nroffuncs;i++) {
1882 proxy->lpvtbl[i] = ProxyIUnknown_QueryInterface;
1885 proxy->lpvtbl[i] = ProxyIUnknown_AddRef;
1888 proxy->lpvtbl[i] = ProxyIUnknown_Release;
1891 if(!defer_to_dispatch)
1893 hres = init_proxy_entry_point(proxy, i);
1894 if(FAILED(hres)) return hres;
1896 else proxy->lpvtbl[3] = ProxyIDispatch_GetTypeInfoCount;
1899 if(!defer_to_dispatch)
1901 hres = init_proxy_entry_point(proxy, i);
1902 if(FAILED(hres)) return hres;
1904 else proxy->lpvtbl[4] = ProxyIDispatch_GetTypeInfo;
1907 if(!defer_to_dispatch)
1909 hres = init_proxy_entry_point(proxy, i);
1910 if(FAILED(hres)) return hres;
1912 else proxy->lpvtbl[5] = ProxyIDispatch_GetIDsOfNames;
1915 if(!defer_to_dispatch)
1917 hres = init_proxy_entry_point(proxy, i);
1918 if(FAILED(hres)) return hres;
1920 else proxy->lpvtbl[6] = ProxyIDispatch_Invoke;
1923 hres = init_proxy_entry_point(proxy, i);
1924 if(FAILED(hres)) return hres;
1930 *ppv = (LPVOID)proxy;
1931 *ppProxy = (IRpcProxyBuffer *)&(proxy->lpvtbl2);
1932 IUnknown_AddRef((IUnknown *)*ppv);
1936 TMProxyImpl_Release((IRpcProxyBuffer *)&proxy->lpvtbl2);
1940 typedef struct _TMStubImpl {
1941 const IRpcStubBufferVtbl *lpvtbl;
1947 IRpcStubBuffer *dispatch_stub;
1948 BOOL dispatch_derivative;
1951 static HRESULT WINAPI
1952 TMStubImpl_QueryInterface(LPRPCSTUBBUFFER iface, REFIID riid, LPVOID *ppv)
1954 if (IsEqualIID(riid,&IID_IRpcStubBuffer)||IsEqualIID(riid,&IID_IUnknown)){
1955 *ppv = (LPVOID)iface;
1956 IRpcStubBuffer_AddRef(iface);
1959 FIXME("%s, not supported IID.\n",debugstr_guid(riid));
1960 return E_NOINTERFACE;
1964 TMStubImpl_AddRef(LPRPCSTUBBUFFER iface)
1966 TMStubImpl *This = (TMStubImpl *)iface;
1967 ULONG refCount = InterlockedIncrement(&This->ref);
1969 TRACE("(%p)->(ref before=%u)\n", This, refCount - 1);
1975 TMStubImpl_Release(LPRPCSTUBBUFFER iface)
1977 TMStubImpl *This = (TMStubImpl *)iface;
1978 ULONG refCount = InterlockedDecrement(&This->ref);
1980 TRACE("(%p)->(ref before=%u)\n", This, refCount + 1);
1984 IRpcStubBuffer_Disconnect(iface);
1985 ITypeInfo_Release(This->tinfo);
1986 if (This->dispatch_stub)
1987 IRpcStubBuffer_Release(This->dispatch_stub);
1988 CoTaskMemFree(This);
1993 static HRESULT WINAPI
1994 TMStubImpl_Connect(LPRPCSTUBBUFFER iface, LPUNKNOWN pUnkServer)
1996 TMStubImpl *This = (TMStubImpl *)iface;
1998 TRACE("(%p)->(%p)\n", This, pUnkServer);
2000 IUnknown_AddRef(pUnkServer);
2001 This->pUnk = pUnkServer;
2003 if (This->dispatch_stub)
2004 IRpcStubBuffer_Connect(This->dispatch_stub, pUnkServer);
2010 TMStubImpl_Disconnect(LPRPCSTUBBUFFER iface)
2012 TMStubImpl *This = (TMStubImpl *)iface;
2014 TRACE("(%p)->()\n", This);
2018 IUnknown_Release(This->pUnk);
2022 if (This->dispatch_stub)
2023 IRpcStubBuffer_Disconnect(This->dispatch_stub);
2026 static HRESULT WINAPI
2028 LPRPCSTUBBUFFER iface, RPCOLEMESSAGE* xmsg,IRpcChannelBuffer*rpcchanbuf)
2031 const FUNCDESC *fdesc;
2032 TMStubImpl *This = (TMStubImpl *)iface;
2034 DWORD *args = NULL, res, *xargs, nrofargs;
2039 ITypeInfo *tinfo = NULL;
2043 if (xmsg->iMethod < 3) {
2044 ERR("IUnknown methods cannot be marshaled by the typelib marshaler\n");
2045 return E_UNEXPECTED;
2048 if (This->dispatch_derivative && xmsg->iMethod < sizeof(IDispatchVtbl)/sizeof(void *))
2050 IPSFactoryBuffer *factory_buffer;
2051 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
2054 hres = IPSFactoryBuffer_CreateStub(factory_buffer, &IID_IDispatch,
2055 This->pUnk, &This->dispatch_stub);
2056 IPSFactoryBuffer_Release(factory_buffer);
2060 return IRpcStubBuffer_Invoke(This->dispatch_stub, xmsg, rpcchanbuf);
2063 memset(&buf,0,sizeof(buf));
2064 buf.size = xmsg->cbBuffer;
2065 buf.base = HeapAlloc(GetProcessHeap(), 0, xmsg->cbBuffer);
2066 memcpy(buf.base, xmsg->Buffer, xmsg->cbBuffer);
2069 hres = get_funcdesc(This->tinfo,xmsg->iMethod,&tinfo,&fdesc,&iname,NULL,NULL);
2071 ERR("GetFuncDesc on method %d failed with %x\n",xmsg->iMethod,hres);
2075 if (iname && !lstrcmpW(iname, IDispatchW))
2077 ERR("IDispatch cannot be marshaled by the typelib marshaler\n");
2078 hres = E_UNEXPECTED;
2079 SysFreeString (iname);
2083 SysFreeString (iname);
2085 /* Need them for hack below */
2086 memset(names,0,sizeof(names));
2087 ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames);
2088 if (nrofnames > sizeof(names)/sizeof(names[0])) {
2089 ERR("Need more names!\n");
2092 /*dump_FUNCDESC(fdesc);*/
2094 for (i=0;i<fdesc->cParams;i++)
2095 nrofargs += _argsize(&fdesc->lprgelemdescParam[i].tdesc, tinfo);
2096 args = HeapAlloc(GetProcessHeap(),0,(nrofargs+1)*sizeof(DWORD));
2099 hres = E_OUTOFMEMORY;
2103 /* Allocate all stuff used by call. */
2105 for (i=0;i<fdesc->cParams;i++) {
2106 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2108 hres = deserialize_param(
2117 xargs += _argsize(&elem->tdesc, tinfo);
2119 ERR("Failed to deserialize param %s, hres %x\n",relaystr(names[i+1]),hres);
2124 args[0] = (DWORD)This->pUnk;
2129 (*((FARPROC**)args[0]))[fdesc->oVft/4],
2137 DWORD dwExceptionCode = GetExceptionCode();
2138 ERR("invoke call failed with exception 0x%08x (%d)\n", dwExceptionCode, dwExceptionCode);
2139 if (FAILED(dwExceptionCode))
2140 hres = dwExceptionCode;
2142 hres = HRESULT_FROM_WIN32(dwExceptionCode);
2152 for (i=0;i<fdesc->cParams;i++) {
2153 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2154 hres = serialize_param(
2163 xargs += _argsize(&elem->tdesc, tinfo);
2165 ERR("Failed to stuballoc param, hres %x\n",hres);
2170 hres = xbuf_add (&buf, (LPBYTE)&res, sizeof(DWORD));
2175 xmsg->cbBuffer = buf.curoff;
2176 hres = IRpcChannelBuffer_GetBuffer(rpcchanbuf, xmsg, &This->iid);
2178 ERR("IRpcChannelBuffer_GetBuffer failed with error 0x%08x\n", hres);
2181 memcpy(xmsg->Buffer, buf.base, buf.curoff);
2184 for (i = 0; i < nrofnames; i++)
2185 SysFreeString(names[i]);
2187 ITypeInfo_Release(tinfo);
2188 HeapFree(GetProcessHeap(), 0, args);
2190 HeapFree(GetProcessHeap(), 0, buf.base);
2192 TRACE("returning\n");
2196 static LPRPCSTUBBUFFER WINAPI
2197 TMStubImpl_IsIIDSupported(LPRPCSTUBBUFFER iface, REFIID riid) {
2198 FIXME("Huh (%s)?\n",debugstr_guid(riid));
2203 TMStubImpl_CountRefs(LPRPCSTUBBUFFER iface) {
2204 TMStubImpl *This = (TMStubImpl *)iface;
2207 return This->ref; /*FIXME? */
2210 static HRESULT WINAPI
2211 TMStubImpl_DebugServerQueryInterface(LPRPCSTUBBUFFER iface, LPVOID *ppv) {
2216 TMStubImpl_DebugServerRelease(LPRPCSTUBBUFFER iface, LPVOID ppv) {
2220 static const IRpcStubBufferVtbl tmstubvtbl = {
2221 TMStubImpl_QueryInterface,
2225 TMStubImpl_Disconnect,
2227 TMStubImpl_IsIIDSupported,
2228 TMStubImpl_CountRefs,
2229 TMStubImpl_DebugServerQueryInterface,
2230 TMStubImpl_DebugServerRelease
2233 static HRESULT WINAPI
2234 PSFacBuf_CreateStub(
2235 LPPSFACTORYBUFFER iface, REFIID riid,IUnknown *pUnkServer,
2236 IRpcStubBuffer** ppStub
2243 TRACE("(%s,%p,%p)\n",debugstr_guid(riid),pUnkServer,ppStub);
2245 hres = _get_typeinfo_for_iid(riid,&tinfo);
2247 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
2251 stub = CoTaskMemAlloc(sizeof(TMStubImpl));
2253 return E_OUTOFMEMORY;
2254 stub->lpvtbl = &tmstubvtbl;
2256 stub->tinfo = tinfo;
2257 stub->dispatch_stub = NULL;
2258 stub->dispatch_derivative = FALSE;
2260 hres = IRpcStubBuffer_Connect((LPRPCSTUBBUFFER)stub,pUnkServer);
2261 *ppStub = (LPRPCSTUBBUFFER)stub;
2262 TRACE("IRpcStubBuffer: %p\n", stub);
2264 ERR("Connect to pUnkServer failed?\n");
2266 /* if we derive from IDispatch then defer to its stub for some of its methods */
2267 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
2270 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
2271 stub->dispatch_derivative = TRUE;
2272 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
2278 static const IPSFactoryBufferVtbl psfacbufvtbl = {
2279 PSFacBuf_QueryInterface,
2282 PSFacBuf_CreateProxy,
2286 /* This is the whole PSFactoryBuffer object, just the vtableptr */
2287 static const IPSFactoryBufferVtbl *lppsfac = &psfacbufvtbl;
2289 /***********************************************************************
2290 * TMARSHAL_DllGetClassObject
2292 HRESULT TMARSHAL_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
2294 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)) {
2298 return E_NOINTERFACE;