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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 #define NONAMELESSUNION
35 #define NONAMELESSSTRUCT
45 #include "propidl.h" /* for LPSAFEARRAY_User* functions */
48 #include "wine/debug.h"
50 static const WCHAR IDispatchW[] = { 'I','D','i','s','p','a','t','c','h',0};
52 WINE_DEFAULT_DEBUG_CHANNEL(ole);
53 WINE_DECLARE_DEBUG_CHANNEL(olerelay);
55 #define ICOM_THIS_MULTI(impl,field,iface) impl* const This=(impl*)((char*)(iface) - offsetof(impl,field))
57 typedef struct _marshal_state {
63 /* used in the olerelay code to avoid having the L"" stuff added by debugstr_w */
64 static char *relaystr(WCHAR *in) {
65 char *tmp = (char *)debugstr_w(in);
67 tmp[strlen(tmp)-1] = '\0';
72 xbuf_resize(marshal_state *buf, DWORD newsize)
74 if(buf->size >= newsize)
79 buf->base = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buf->base, newsize);
85 buf->base = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, newsize);
93 xbuf_add(marshal_state *buf, LPBYTE stuff, DWORD size)
97 if(buf->size - buf->curoff < size)
99 hr = xbuf_resize(buf, buf->size + size + 100);
100 if(FAILED(hr)) return hr;
102 memcpy(buf->base+buf->curoff,stuff,size);
108 xbuf_get(marshal_state *buf, LPBYTE stuff, DWORD size) {
109 if (buf->size < buf->curoff+size) return E_FAIL;
110 memcpy(stuff,buf->base+buf->curoff,size);
116 xbuf_skip(marshal_state *buf, DWORD size) {
117 if (buf->size < buf->curoff+size) return E_FAIL;
123 _unmarshal_interface(marshal_state *buf, REFIID riid, LPUNKNOWN *pUnk) {
125 ULARGE_INTEGER newpos;
126 LARGE_INTEGER seekto;
131 TRACE("...%s...\n",debugstr_guid(riid));
134 hres = xbuf_get(buf,(LPBYTE)&xsize,sizeof(xsize));
136 ERR("xbuf_get failed\n");
140 if (xsize == 0) return S_OK;
142 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
144 ERR("Stream create failed %lx\n",hres);
148 hres = IStream_Write(pStm,buf->base+buf->curoff,xsize,&res);
150 ERR("stream write %lx\n",hres);
154 memset(&seekto,0,sizeof(seekto));
155 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
157 ERR("Failed Seek %lx\n",hres);
161 hres = CoUnmarshalInterface(pStm,riid,(LPVOID*)pUnk);
163 ERR("Unmarshalling interface %s failed with %lx\n",debugstr_guid(riid),hres);
167 IStream_Release(pStm);
168 return xbuf_skip(buf,xsize);
172 _marshal_interface(marshal_state *buf, REFIID riid, LPUNKNOWN pUnk) {
173 LPBYTE tempbuf = NULL;
174 IStream *pStm = NULL;
176 ULARGE_INTEGER newpos;
177 LARGE_INTEGER seekto;
183 /* this is valid, if for instance we serialize
184 * a VT_DISPATCH with NULL ptr which apparently
185 * can happen. S_OK to make sure we continue
188 WARN("pUnk is NULL\n");
190 return xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
195 TRACE("...%s...\n",debugstr_guid(riid));
197 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
199 ERR("Stream create failed %lx\n",hres);
203 hres = CoMarshalInterface(pStm,riid,pUnk,0,NULL,0);
205 ERR("Marshalling interface %s failed with %lx\n", debugstr_guid(riid), hres);
209 hres = IStream_Stat(pStm,&ststg,0);
211 ERR("Stream stat failed\n");
215 tempbuf = HeapAlloc(GetProcessHeap(), 0, ststg.cbSize.u.LowPart);
216 memset(&seekto,0,sizeof(seekto));
217 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
219 ERR("Failed Seek %lx\n",hres);
223 hres = IStream_Read(pStm,tempbuf,ststg.cbSize.u.LowPart,&res);
225 ERR("Failed Read %lx\n",hres);
229 xsize = ststg.cbSize.u.LowPart;
230 xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
231 hres = xbuf_add(buf,tempbuf,ststg.cbSize.u.LowPart);
233 HeapFree(GetProcessHeap(),0,tempbuf);
234 IStream_Release(pStm);
240 xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
241 if (pStm) IUnknown_Release(pStm);
242 HeapFree(GetProcessHeap(), 0, tempbuf);
246 /********************* OLE Proxy/Stub Factory ********************************/
247 static HRESULT WINAPI
248 PSFacBuf_QueryInterface(LPPSFACTORYBUFFER iface, REFIID iid, LPVOID *ppv) {
249 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)||IsEqualIID(iid,&IID_IUnknown)) {
250 *ppv = (LPVOID)iface;
251 /* No ref counting, static class */
254 FIXME("(%s) unknown IID?\n",debugstr_guid(iid));
255 return E_NOINTERFACE;
258 static ULONG WINAPI PSFacBuf_AddRef(LPPSFACTORYBUFFER iface) { return 2; }
259 static ULONG WINAPI PSFacBuf_Release(LPPSFACTORYBUFFER iface) { return 1; }
262 _get_typeinfo_for_iid(REFIID riid, ITypeInfo**ti) {
265 char tlguid[200],typelibkey[300],interfacekey[300],ver[100];
268 DWORD tlguidlen, verlen, type;
272 sprintf( interfacekey, "Interface\\{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\Typelib",
273 riid->Data1, riid->Data2, riid->Data3,
274 riid->Data4[0], riid->Data4[1], riid->Data4[2], riid->Data4[3],
275 riid->Data4[4], riid->Data4[5], riid->Data4[6], riid->Data4[7]
278 if (RegOpenKeyA(HKEY_CLASSES_ROOT,interfacekey,&ikey)) {
279 ERR("No %s key found.\n",interfacekey);
283 tlguidlen = sizeof(tlguid);
284 if (RegQueryValueExA(ikey,NULL,NULL,&type,(LPBYTE)tlguid,&tlguidlen)) {
285 ERR("Getting typelib guid failed.\n");
290 verlen = sizeof(ver);
291 if (RegQueryValueExA(ikey,"Version",NULL,&type,(LPBYTE)ver,&verlen)) {
292 ERR("Could not get version value?\n");
297 sprintf(typelibkey,"Typelib\\%s\\%s\\0\\win32",tlguid,ver);
298 tlfnlen = sizeof(tlfn);
299 if (RegQueryValueA(HKEY_CLASSES_ROOT,typelibkey,tlfn,&tlfnlen)) {
300 ERR("Could not get typelib fn?\n");
303 MultiByteToWideChar(CP_ACP, 0, tlfn, -1, tlfnW, -1);
304 hres = LoadTypeLib(tlfnW,&tl);
306 ERR("Failed to load typelib for %s, but it should be there.\n",debugstr_guid(riid));
309 hres = ITypeLib_GetTypeInfoOfGuid(tl,riid,ti);
311 ERR("typelib does not contain info for %s?\n",debugstr_guid(riid));
312 ITypeLib_Release(tl);
315 /* FIXME: do this? ITypeLib_Release(tl); */
319 /* Determine nr of functions. Since we use the toplevel interface and all
320 * inherited ones have lower numbers, we are ok to not to descent into
321 * the inheritance tree I think.
323 static int _nroffuncs(ITypeInfo *tinfo) {
325 const FUNCDESC *fdesc;
330 hres = ITypeInfoImpl_GetInternalFuncDesc(tinfo,n,&fdesc);
333 if (fdesc->oVft/4 > max)
342 #include "pshpack1.h"
344 typedef struct _TMAsmProxy {
358 # warning You need to implement stubless proxies for your architecture
359 typedef struct _TMAsmProxy {
363 typedef struct _TMProxyImpl {
365 const IRpcProxyBufferVtbl *lpvtbl2;
368 TMAsmProxy *asmstubs;
370 IRpcChannelBuffer* chanbuf;
372 CRITICAL_SECTION crit;
373 IUnknown *outerunknown;
375 IRpcProxyBuffer *dispatch_proxy;
378 static HRESULT WINAPI
379 TMProxyImpl_QueryInterface(LPRPCPROXYBUFFER iface, REFIID riid, LPVOID *ppv)
382 if (IsEqualIID(riid,&IID_IUnknown)||IsEqualIID(riid,&IID_IRpcProxyBuffer)) {
383 *ppv = (LPVOID)iface;
384 IRpcProxyBuffer_AddRef(iface);
387 FIXME("no interface for %s\n",debugstr_guid(riid));
388 return E_NOINTERFACE;
392 TMProxyImpl_AddRef(LPRPCPROXYBUFFER iface)
394 ICOM_THIS_MULTI(TMProxyImpl,lpvtbl2,iface);
395 ULONG refCount = InterlockedIncrement(&This->ref);
397 TRACE("(%p)->(ref before=%lu)\n",This, refCount - 1);
403 TMProxyImpl_Release(LPRPCPROXYBUFFER iface)
405 ICOM_THIS_MULTI(TMProxyImpl,lpvtbl2,iface);
406 ULONG refCount = InterlockedDecrement(&This->ref);
408 TRACE("(%p)->(ref before=%lu)\n",This, refCount + 1);
412 if (This->dispatch_proxy) IRpcProxyBuffer_Release(This->dispatch_proxy);
413 DeleteCriticalSection(&This->crit);
414 if (This->chanbuf) IRpcChannelBuffer_Release(This->chanbuf);
415 VirtualFree(This->asmstubs, 0, MEM_RELEASE);
421 static HRESULT WINAPI
423 LPRPCPROXYBUFFER iface,IRpcChannelBuffer* pRpcChannelBuffer)
425 ICOM_THIS_MULTI(TMProxyImpl, lpvtbl2, iface);
427 TRACE("(%p)\n", pRpcChannelBuffer);
429 EnterCriticalSection(&This->crit);
431 IRpcChannelBuffer_AddRef(pRpcChannelBuffer);
432 This->chanbuf = pRpcChannelBuffer;
434 LeaveCriticalSection(&This->crit);
436 if (This->dispatch_proxy)
437 IRpcProxyBuffer_Connect(This->dispatch_proxy, pRpcChannelBuffer);
443 TMProxyImpl_Disconnect(LPRPCPROXYBUFFER iface)
445 ICOM_THIS_MULTI(TMProxyImpl, lpvtbl2, iface);
449 EnterCriticalSection(&This->crit);
451 IRpcChannelBuffer_Release(This->chanbuf);
452 This->chanbuf = NULL;
454 LeaveCriticalSection(&This->crit);
456 if (This->dispatch_proxy)
457 IRpcProxyBuffer_Disconnect(This->dispatch_proxy);
461 static const IRpcProxyBufferVtbl tmproxyvtable = {
462 TMProxyImpl_QueryInterface,
466 TMProxyImpl_Disconnect
469 /* how much space do we use on stack in DWORD steps. */
474 return 8/sizeof(DWORD);
476 return sizeof(double)/sizeof(DWORD);
478 return sizeof(CY)/sizeof(DWORD);
480 return sizeof(DATE)/sizeof(DWORD);
482 return (sizeof(VARIANT)+3)/sizeof(DWORD);
489 _xsize(TYPEDESC *td) {
494 return sizeof(VARIANT)+3;
497 ARRAYDESC *adesc = td->u.lpadesc;
499 for (i=0;i<adesc->cDims;i++)
500 arrsize *= adesc->rgbounds[i].cElements;
501 return arrsize*_xsize(&adesc->tdescElem);
529 TRACE("(tdesc.vt %s)\n",debugstr_vt(tdesc->vt));
532 case VT_EMPTY: /* nothing. empty variant for instance */
538 if (debugout) TRACE_(olerelay)("%lx%lx",arg[0],arg[1]);
540 hres = xbuf_add(buf,(LPBYTE)arg,8);
550 if (debugout) TRACE_(olerelay)("%lx",*arg);
552 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
557 if (debugout) TRACE_(olerelay)("%04lx",*arg & 0xffff);
559 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
564 if (debugout) TRACE_(olerelay)("%02lx",*arg & 0xff);
566 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
570 if (debugout) TRACE_(olerelay)("&0x%lx",*arg);
572 hres = xbuf_add(buf,(LPBYTE)(DWORD*)*arg,sizeof(DWORD));
573 /* do not dealloc at this time */
577 VARIANT *vt = (VARIANT*)arg;
578 DWORD vttype = V_VT(vt);
580 if (debugout) TRACE_(olerelay)("Vt(%s%s)(",debugstr_vt(vttype),debugstr_vf(vttype));
583 hres = xbuf_add(buf,(LPBYTE)&vttype,sizeof(vttype));
584 if (hres) return hres;
586 /* need to recurse since we need to free the stuff */
587 hres = serialize_param(tinfo,writeit,debugout,dealloc,&tdesc2,(DWORD*)&(V_I4(vt)),buf);
588 if (debugout) TRACE_(olerelay)(")");
591 case VT_BSTR|VT_BYREF: {
592 if (debugout) TRACE_(olerelay)("[byref]'%s'", *(BSTR*)*arg ? relaystr(*((BSTR*)*arg)) : "<bstr NULL>");
594 /* ptr to ptr to magic widestring, basically */
595 BSTR *bstr = (BSTR *) *arg;
598 /* -1 means "null string" which is equivalent to empty string */
600 hres = xbuf_add(buf, (LPBYTE)&len,sizeof(DWORD));
601 if (hres) return hres;
603 len = *((DWORD*)*bstr-1)/sizeof(WCHAR);
604 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
605 if (hres) return hres;
606 hres = xbuf_add(buf,(LPBYTE)*bstr,len * sizeof(WCHAR));
607 if (hres) return hres;
611 if (dealloc && arg) {
612 BSTR *str = *((BSTR **)arg);
621 TRACE_(olerelay)("%s",relaystr((WCHAR*)*arg));
623 TRACE_(olerelay)("<bstr NULL>");
626 BSTR bstr = (BSTR)*arg;
630 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
631 if (hres) return hres;
633 len = *((DWORD*)bstr-1)/sizeof(WCHAR);
634 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
635 if (hres) return hres;
636 hres = xbuf_add(buf,(LPBYTE)bstr,len * sizeof(WCHAR));
637 if (hres) return hres;
642 SysFreeString((BSTR)*arg);
647 BOOL derefhere = TRUE;
649 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
653 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
655 ERR("Could not get typeinfo of hreftype %lx for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
658 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
659 switch (tattr->typekind) {
660 case TKIND_ENUM: /* confirmed */
661 case TKIND_RECORD: /* FIXME: mostly untested */
664 case TKIND_ALIAS: /* FIXME: untested */
665 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
666 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
670 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
674 ITypeInfo_Release(tinfo2);
677 if (debugout) TRACE_(olerelay)("*");
678 /* Write always, so the other side knows when it gets a NULL pointer.
680 cookie = *arg ? 0x42424242 : 0;
681 hres = xbuf_add(buf,(LPBYTE)&cookie,sizeof(cookie));
685 if (debugout) TRACE_(olerelay)("NULL");
688 hres = serialize_param(tinfo,writeit,debugout,dealloc,tdesc->u.lptdesc,(DWORD*)*arg,buf);
689 if (derefhere && dealloc) HeapFree(GetProcessHeap(),0,(LPVOID)*arg);
693 if (debugout) TRACE_(olerelay)("unk(0x%lx)",*arg);
695 hres = _marshal_interface(buf,&IID_IUnknown,(LPUNKNOWN)*arg);
696 if (dealloc && *(IUnknown **)arg)
697 IUnknown_Release((LPUNKNOWN)*arg);
700 if (debugout) TRACE_(olerelay)("idisp(0x%lx)",*arg);
702 hres = _marshal_interface(buf,&IID_IDispatch,(LPUNKNOWN)*arg);
703 if (dealloc && *(IUnknown **)arg)
704 IUnknown_Release((LPUNKNOWN)*arg);
707 if (debugout) TRACE_(olerelay)("<void>");
709 case VT_USERDEFINED: {
713 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
715 ERR("Could not get typeinfo of hreftype %lx for VT_USERDEFINED.\n",tdesc->u.hreftype);
718 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
719 switch (tattr->typekind) {
721 case TKIND_INTERFACE:
723 hres=_marshal_interface(buf,&(tattr->guid),(LPUNKNOWN)arg);
725 IUnknown_Release((LPUNKNOWN)arg);
729 if (debugout) TRACE_(olerelay)("{");
730 for (i=0;i<tattr->cVars;i++) {
735 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
737 ERR("Could not get vardesc of %d\n",i);
740 /* Need them for hack below */
742 memset(names,0,sizeof(names));
743 hres = ITypeInfo_GetNames(tinfo2,vdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames);
744 if (nrofnames > sizeof(names)/sizeof(names[0])) {
745 ERR("Need more names!\n");
747 if (!hres && debugout)
748 TRACE_(olerelay)("%s=",relaystr(names[0]));
750 elem2 = &vdesc->elemdescVar;
751 tdesc2 = &elem2->tdesc;
752 hres = serialize_param(
758 (DWORD*)(((LPBYTE)arg)+vdesc->u.oInst),
761 ITypeInfo_ReleaseVarDesc(tinfo2, vdesc);
764 if (debugout && (i<(tattr->cVars-1)))
765 TRACE_(olerelay)(",");
767 if (debugout) TRACE_(olerelay)("}");
771 return serialize_param(tinfo2,writeit,debugout,dealloc,&tattr->tdescAlias,arg,buf);
774 if (debugout) TRACE_(olerelay)("%lx",*arg);
776 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
779 FIXME("Unhandled typekind %d\n",tattr->typekind);
783 ITypeInfo_Release(tinfo2);
787 ARRAYDESC *adesc = tdesc->u.lpadesc;
790 if (debugout) TRACE_(olerelay)("carr");
791 for (i=0;i<adesc->cDims;i++) {
792 if (debugout) TRACE_(olerelay)("[%ld]",adesc->rgbounds[i].cElements);
793 arrsize *= adesc->rgbounds[i].cElements;
795 if (debugout) TRACE_(olerelay)("(vt %s)",debugstr_vt(adesc->tdescElem.vt));
796 if (debugout) TRACE_(olerelay)("[");
797 for (i=0;i<arrsize;i++) {
798 hres = serialize_param(tinfo, writeit, debugout, dealloc, &adesc->tdescElem, (DWORD*)((LPBYTE)arg+i*_xsize(&adesc->tdescElem)), buf);
801 if (debugout && (i<arrsize-1)) TRACE_(olerelay)(",");
803 if (debugout) TRACE_(olerelay)("]");
809 unsigned long flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
810 unsigned long size = LPSAFEARRAY_UserSize(&flags, buf->curoff, (LPSAFEARRAY *)arg);
811 xbuf_resize(buf, size);
812 LPSAFEARRAY_UserMarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
818 ERR("Unhandled marshal type %d.\n",tdesc->vt);
835 TRACE("vt %s at %p\n",debugstr_vt(tdesc->vt),arg);
840 if (debugout) TRACE_(olerelay)("<empty>");
843 if (debugout) TRACE_(olerelay)("<null>");
846 VARIANT *vt = (VARIANT*)arg;
851 hres = xbuf_get(buf,(LPBYTE)&vttype,sizeof(vttype));
853 FIXME("vt type not read?\n");
856 memset(&tdesc2,0,sizeof(tdesc2));
859 if (debugout) TRACE_(olerelay)("Vt(%s%s)(",debugstr_vt(vttype),debugstr_vf(vttype));
860 hres = deserialize_param(tinfo, readit, debugout, alloc, &tdesc2, (DWORD*)&(V_I4(vt)), buf);
861 TRACE_(olerelay)(")");
872 hres = xbuf_get(buf,(LPBYTE)arg,8);
873 if (hres) ERR("Failed to read integer 8 byte\n");
875 if (debugout) TRACE_(olerelay)("%lx%lx",arg[0],arg[1]);
885 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
886 if (hres) ERR("Failed to read integer 4 byte\n");
888 if (debugout) TRACE_(olerelay)("%lx",*arg);
894 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
895 if (hres) ERR("Failed to read integer 4 byte\n");
898 if (debugout) TRACE_(olerelay)("%04lx",*arg & 0xffff);
904 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
905 if (hres) ERR("Failed to read integer 4 byte\n");
908 if (debugout) TRACE_(olerelay)("%02lx",*arg & 0xff);
913 *arg = (DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
915 hres = xbuf_get(buf,(LPBYTE)*arg,sizeof(DWORD));
916 if (hres) ERR("Failed to read integer 4 byte\n");
918 if (debugout) TRACE_(olerelay)("&0x%lx",*(DWORD*)*arg);
920 case VT_BSTR|VT_BYREF: {
921 BSTR **bstr = (BSTR **)arg;
926 hres = xbuf_get(buf,(LPBYTE)&len,sizeof(DWORD));
928 ERR("failed to read bstr klen\n");
932 *bstr = CoTaskMemAlloc(sizeof(BSTR *));
934 if (debugout) TRACE_(olerelay)("<bstr NULL>");
936 str = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(len+1)*sizeof(WCHAR));
937 hres = xbuf_get(buf,(LPBYTE)str,len*sizeof(WCHAR));
939 ERR("Failed to read BSTR.\n");
942 *bstr = CoTaskMemAlloc(sizeof(BSTR *));
943 **bstr = SysAllocStringLen(str,len);
944 if (debugout) TRACE_(olerelay)("%s",relaystr(str));
945 HeapFree(GetProcessHeap(),0,str);
957 hres = xbuf_get(buf,(LPBYTE)&len,sizeof(DWORD));
959 ERR("failed to read bstr klen\n");
964 if (debugout) TRACE_(olerelay)("<bstr NULL>");
966 str = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(len+1)*sizeof(WCHAR));
967 hres = xbuf_get(buf,(LPBYTE)str,len*sizeof(WCHAR));
969 ERR("Failed to read BSTR.\n");
972 *arg = (DWORD)SysAllocStringLen(str,len);
973 if (debugout) TRACE_(olerelay)("%s",relaystr(str));
974 HeapFree(GetProcessHeap(),0,str);
983 BOOL derefhere = TRUE;
985 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
989 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
991 ERR("Could not get typeinfo of hreftype %lx for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
994 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
995 switch (tattr->typekind) {
996 case TKIND_ENUM: /* confirmed */
997 case TKIND_RECORD: /* FIXME: mostly untested */
1000 case TKIND_ALIAS: /* FIXME: untested */
1001 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
1002 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
1006 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
1010 ITypeInfo_Release(tinfo2);
1012 /* read it in all cases, we need to know if we have
1013 * NULL pointer or not.
1015 hres = xbuf_get(buf,(LPBYTE)&cookie,sizeof(cookie));
1017 ERR("Failed to load pointer cookie.\n");
1020 if (cookie != 0x42424242) {
1021 /* we read a NULL ptr from the remote side */
1022 if (debugout) TRACE_(olerelay)("NULL");
1026 if (debugout) TRACE_(olerelay)("*");
1028 /* Allocate space for the referenced struct */
1030 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,_xsize(tdesc->u.lptdesc));
1033 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, (LPDWORD)*arg, buf);
1035 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, arg, buf);
1038 /* FIXME: UNKNOWN is unknown ..., but allocate 4 byte for it */
1040 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
1043 hres = _unmarshal_interface(buf,&IID_IUnknown,(LPUNKNOWN*)arg);
1045 TRACE_(olerelay)("unk(%p)",arg);
1050 hres = _unmarshal_interface(buf,&IID_IDispatch,(LPUNKNOWN*)arg);
1052 TRACE_(olerelay)("idisp(%p)",arg);
1055 if (debugout) TRACE_(olerelay)("<void>");
1057 case VT_USERDEFINED: {
1061 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
1063 ERR("Could not get typeinfo of hreftype %lx for VT_USERDEFINED.\n",tdesc->u.hreftype);
1066 hres = ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1068 ERR("Could not get typeattr in VT_USERDEFINED.\n");
1070 switch (tattr->typekind) {
1071 case TKIND_DISPATCH:
1072 case TKIND_INTERFACE:
1074 hres = _unmarshal_interface(buf,&(tattr->guid),(LPUNKNOWN*)arg);
1076 case TKIND_RECORD: {
1080 *arg = (DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,tattr->cbSizeInstance);
1082 if (debugout) TRACE_(olerelay)("{");
1083 for (i=0;i<tattr->cVars;i++) {
1086 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
1088 ERR("Could not get vardesc of %d\n",i);
1091 hres = deserialize_param(
1096 &vdesc->elemdescVar.tdesc,
1097 (DWORD*)(((LPBYTE)*arg)+vdesc->u.oInst),
1100 ITypeInfo2_ReleaseVarDesc(tinfo2, vdesc);
1101 if (debugout && (i<tattr->cVars-1)) TRACE_(olerelay)(",");
1103 if (debugout) TRACE_(olerelay)("}");
1107 return deserialize_param(tinfo2,readit,debugout,alloc,&tattr->tdescAlias,arg,buf);
1110 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
1111 if (hres) ERR("Failed to read enum (4 byte)\n");
1113 if (debugout) TRACE_(olerelay)("%lx",*arg);
1116 ERR("Unhandled typekind %d\n",tattr->typekind);
1122 ERR("failed to stuballoc in TKIND_RECORD.\n");
1123 ITypeInfo_Release(tinfo2);
1127 /* arg is pointing to the start of the array. */
1128 ARRAYDESC *adesc = tdesc->u.lpadesc;
1131 if (adesc->cDims > 1) FIXME("cDims > 1 in VT_CARRAY. Does it work?\n");
1132 for (i=0;i<adesc->cDims;i++)
1133 arrsize *= adesc->rgbounds[i].cElements;
1134 for (i=0;i<arrsize;i++)
1141 (DWORD*)((LPBYTE)(arg)+i*_xsize(&adesc->tdescElem)),
1146 case VT_SAFEARRAY: {
1149 unsigned long flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
1150 unsigned char *buffer;
1151 buffer = LPSAFEARRAY_UserUnmarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
1152 buf->curoff = buffer - buf->base;
1157 ERR("No handler for VT type %d!\n",tdesc->vt);
1163 /* Searches function, also in inherited interfaces */
1166 ITypeInfo *tinfo, int iMethod, ITypeInfo **tactual, const FUNCDESC **fdesc, BSTR *iname, BSTR *fname)
1171 if (fname) *fname = NULL;
1172 if (iname) *iname = NULL;
1175 ITypeInfo_AddRef(*tactual);
1178 hres = ITypeInfoImpl_GetInternalFuncDesc(tinfo, i, fdesc);
1185 hres = ITypeInfo_GetTypeAttr(tinfo, &attr);
1187 ERR("GetTypeAttr failed with %lx\n",hres);
1190 /* Not found, so look in inherited ifaces. */
1191 for (j=0;j<attr->cImplTypes;j++) {
1192 hres = ITypeInfo_GetRefTypeOfImplType(tinfo, j, &href);
1194 ERR("Did not find a reftype for interface offset %d?\n",j);
1197 hres = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
1199 ERR("Did not find a typeinfo for reftype %ld?\n",href);
1202 hres = _get_funcdesc(tinfo2,iMethod,tactual,fdesc,iname,fname);
1203 ITypeInfo_Release(tinfo2);
1204 if (!hres) return S_OK;
1208 if (((*fdesc)->oVft/4) == iMethod) {
1210 ITypeInfo_GetDocumentation(tinfo,(*fdesc)->memid,fname,NULL,NULL,NULL);
1212 ITypeInfo_GetDocumentation(tinfo,-1,iname,NULL,NULL,NULL);
1220 xCall(LPVOID retptr, int method, TMProxyImpl *tpinfo /*, args */)
1222 DWORD *args = ((DWORD*)&tpinfo)+1, *xargs;
1223 const FUNCDESC *fdesc;
1225 int i, relaydeb = TRACE_ON(olerelay);
1232 DWORD remoteresult = 0;
1234 IRpcChannelBuffer *chanbuf;
1236 EnterCriticalSection(&tpinfo->crit);
1238 hres = _get_funcdesc(tpinfo->tinfo,method,&tinfo,&fdesc,&iname,&fname);
1240 ERR("Did not find typeinfo/funcdesc entry for method %d!\n",method);
1241 ITypeInfo_Release(tinfo);
1242 LeaveCriticalSection(&tpinfo->crit);
1246 if (!tpinfo->chanbuf)
1248 WARN("Tried to use disconnected proxy\n");
1249 ITypeInfo_Release(tinfo);
1250 LeaveCriticalSection(&tpinfo->crit);
1251 return RPC_E_DISCONNECTED;
1253 chanbuf = tpinfo->chanbuf;
1254 IRpcChannelBuffer_AddRef(chanbuf);
1256 LeaveCriticalSection(&tpinfo->crit);
1259 TRACE_(olerelay)("->");
1261 TRACE_(olerelay)("%s:",relaystr(iname));
1263 TRACE_(olerelay)("%s(%d)",relaystr(fname),method);
1265 TRACE_(olerelay)("%d",method);
1266 TRACE_(olerelay)("(");
1269 if (iname) SysFreeString(iname);
1270 if (fname) SysFreeString(fname);
1272 memset(&buf,0,sizeof(buf));
1274 /* normal typelib driven serializing */
1276 /* Need them for hack below */
1277 memset(names,0,sizeof(names));
1278 if (ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames))
1280 if (nrofnames > sizeof(names)/sizeof(names[0]))
1281 ERR("Need more names!\n");
1284 for (i=0;i<fdesc->cParams;i++) {
1285 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1287 if (i) TRACE_(olerelay)(",");
1288 if (i+1<nrofnames && names[i+1])
1289 TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1291 /* No need to marshal other data than FIN and any VT_PTR. */
1292 if (!(elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN || !elem->u.paramdesc.wParamFlags) && (elem->tdesc.vt != VT_PTR)) {
1293 xargs+=_argsize(elem->tdesc.vt);
1294 if (relaydeb) TRACE_(olerelay)("[out]");
1297 hres = serialize_param(
1299 elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN || !elem->u.paramdesc.wParamFlags,
1308 ERR("Failed to serialize param, hres %lx\n",hres);
1311 xargs+=_argsize(elem->tdesc.vt);
1313 if (relaydeb) TRACE_(olerelay)(")");
1315 memset(&msg,0,sizeof(msg));
1316 msg.cbBuffer = buf.curoff;
1317 msg.iMethod = method;
1318 hres = IRpcChannelBuffer_GetBuffer(chanbuf,&msg,&(tpinfo->iid));
1320 ERR("RpcChannelBuffer GetBuffer failed, %lx\n",hres);
1323 memcpy(msg.Buffer,buf.base,buf.curoff);
1324 if (relaydeb) TRACE_(olerelay)("\n");
1325 hres = IRpcChannelBuffer_SendReceive(chanbuf,&msg,&status);
1327 ERR("RpcChannelBuffer SendReceive failed, %lx\n",hres);
1331 if (relaydeb) TRACE_(olerelay)(" status = %08lx (",status);
1333 buf.base = HeapReAlloc(GetProcessHeap(),0,buf.base,msg.cbBuffer);
1335 buf.base = HeapAlloc(GetProcessHeap(),0,msg.cbBuffer);
1336 buf.size = msg.cbBuffer;
1337 memcpy(buf.base,msg.Buffer,buf.size);
1340 /* generic deserializer using typelib description */
1343 for (i=0;i<fdesc->cParams;i++) {
1344 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1347 if (i) TRACE_(olerelay)(",");
1348 if (i+1<nrofnames && names[i+1]) TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1350 /* No need to marshal other data than FOUT and any VT_PTR */
1351 if (!(elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT) && (elem->tdesc.vt != VT_PTR)) {
1352 xargs += _argsize(elem->tdesc.vt);
1353 if (relaydeb) TRACE_(olerelay)("[in]");
1356 hres = deserialize_param(
1358 elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT,
1366 ERR("Failed to unmarshall param, hres %lx\n",hres);
1370 xargs += _argsize(elem->tdesc.vt);
1373 hres = xbuf_get(&buf, (LPBYTE)&remoteresult, sizeof(DWORD));
1376 if (relaydeb) TRACE_(olerelay)(") = %08lx\n", remoteresult);
1378 hres = remoteresult;
1381 HeapFree(GetProcessHeap(),0,buf.base);
1382 IRpcChannelBuffer_Release(chanbuf);
1383 ITypeInfo_Release(tinfo);
1384 TRACE("-- 0x%08lx\n", hres);
1388 HRESULT WINAPI ProxyIUnknown_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
1390 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1392 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1394 if (proxy->outerunknown)
1395 return IUnknown_QueryInterface(proxy->outerunknown, riid, ppv);
1397 FIXME("No interface\n");
1398 return E_NOINTERFACE;
1401 ULONG WINAPI ProxyIUnknown_AddRef(IUnknown *iface)
1403 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1407 if (proxy->outerunknown)
1408 return IUnknown_AddRef(proxy->outerunknown);
1410 return 2; /* FIXME */
1413 ULONG WINAPI ProxyIUnknown_Release(IUnknown *iface)
1415 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1419 if (proxy->outerunknown)
1420 return IUnknown_Release(proxy->outerunknown);
1422 return 1; /* FIXME */
1425 static HRESULT WINAPI ProxyIDispatch_GetTypeInfoCount(LPDISPATCH iface, UINT * pctinfo)
1427 TMProxyImpl *This = (TMProxyImpl *)iface;
1429 TRACE("(%p)\n", pctinfo);
1431 return IDispatch_GetTypeInfoCount(This->dispatch, pctinfo);
1434 static HRESULT WINAPI ProxyIDispatch_GetTypeInfo(LPDISPATCH iface, UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo)
1436 TMProxyImpl *This = (TMProxyImpl *)iface;
1438 TRACE("(%d, %lx, %p)\n", iTInfo, lcid, ppTInfo);
1440 return IDispatch_GetTypeInfo(This->dispatch, iTInfo, lcid, ppTInfo);
1443 static HRESULT WINAPI ProxyIDispatch_GetIDsOfNames(LPDISPATCH iface, REFIID riid, LPOLESTR * rgszNames, UINT cNames, LCID lcid, DISPID * rgDispId)
1445 TMProxyImpl *This = (TMProxyImpl *)iface;
1447 TRACE("(%s, %p, %d, 0x%lx, %p)\n", debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);
1449 return IDispatch_GetIDsOfNames(This->dispatch, riid, rgszNames,
1450 cNames, lcid, rgDispId);
1453 static HRESULT WINAPI ProxyIDispatch_Invoke(LPDISPATCH iface, DISPID dispIdMember, REFIID riid, LCID lcid,
1454 WORD wFlags, DISPPARAMS * pDispParams, VARIANT * pVarResult,
1455 EXCEPINFO * pExcepInfo, UINT * puArgErr)
1457 TMProxyImpl *This = (TMProxyImpl *)iface;
1459 TRACE("(%ld, %s, 0x%lx, 0x%x, %p, %p, %p, %p)\n", dispIdMember,
1460 debugstr_guid(riid), lcid, wFlags, pDispParams, pVarResult,
1461 pExcepInfo, puArgErr);
1463 return IDispatch_Invoke(This->dispatch, dispIdMember, riid, lcid,
1464 wFlags, pDispParams, pVarResult, pExcepInfo,
1468 static inline HRESULT get_facbuf_for_iid(REFIID riid, IPSFactoryBuffer **facbuf)
1473 if ((hr = CoGetPSClsid(riid, &clsid)))
1475 return CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL,
1476 &IID_IPSFactoryBuffer, (LPVOID*)facbuf);
1479 static HRESULT WINAPI
1480 PSFacBuf_CreateProxy(
1481 LPPSFACTORYBUFFER iface, IUnknown* pUnkOuter, REFIID riid,
1482 IRpcProxyBuffer **ppProxy, LPVOID *ppv)
1487 const FUNCDESC *fdesc;
1491 TRACE("(...%s...)\n",debugstr_guid(riid));
1492 hres = _get_typeinfo_for_iid(riid,&tinfo);
1494 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
1497 nroffuncs = _nroffuncs(tinfo);
1498 proxy = CoTaskMemAlloc(sizeof(TMProxyImpl));
1499 if (!proxy) return E_OUTOFMEMORY;
1501 assert(sizeof(TMAsmProxy) == 12);
1503 proxy->dispatch = NULL;
1504 proxy->dispatch_proxy = NULL;
1505 proxy->outerunknown = pUnkOuter;
1506 proxy->asmstubs = VirtualAlloc(NULL, sizeof(TMAsmProxy) * nroffuncs, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
1507 if (!proxy->asmstubs) {
1508 ERR("Could not commit pages for proxy thunks\n");
1509 CoTaskMemFree(proxy);
1510 return E_OUTOFMEMORY;
1513 InitializeCriticalSection(&proxy->crit);
1515 proxy->lpvtbl = HeapAlloc(GetProcessHeap(),0,sizeof(LPBYTE)*nroffuncs);
1516 for (i=0;i<nroffuncs;i++) {
1517 TMAsmProxy *xasm = proxy->asmstubs+i;
1521 proxy->lpvtbl[i] = ProxyIUnknown_QueryInterface;
1524 proxy->lpvtbl[i] = ProxyIUnknown_AddRef;
1527 proxy->lpvtbl[i] = ProxyIUnknown_Release;
1531 /* nrofargs without This */
1534 hres = _get_funcdesc(tinfo,i,&tinfo2,&fdesc,NULL,NULL);
1535 ITypeInfo_Release(tinfo2);
1537 ERR("GetFuncDesc %lx should not fail here.\n",hres);
1540 /* some args take more than 4 byte on the stack */
1542 for (j=0;j<fdesc->cParams;j++)
1543 nrofargs += _argsize(fdesc->lprgelemdescParam[j].tdesc.vt);
1546 if (fdesc->callconv != CC_STDCALL) {
1547 ERR("calling convention is not stdcall????\n");
1550 /* popl %eax - return ptr
1557 * arg3 arg2 arg1 <method> <returnptr>
1559 xasm->popleax = 0x58;
1560 xasm->pushlval = 0x6a;
1562 xasm->pushleax = 0x50;
1563 xasm->lcall = 0xe8; /* relative jump */
1564 xasm->xcall = (DWORD)xCall;
1565 xasm->xcall -= (DWORD)&(xasm->lret);
1567 xasm->bytestopop= (nrofargs+2)*4; /* pop args, This, iMethod */
1568 proxy->lpvtbl[i] = xasm;
1571 FIXME("not implemented on non i386\n");
1578 /* if we derive from IDispatch then defer to its proxy for its methods */
1579 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
1582 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
1584 IPSFactoryBuffer *factory_buffer;
1585 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
1588 hres = IPSFactoryBuffer_CreateProxy(factory_buffer, NULL,
1589 &IID_IDispatch, &proxy->dispatch_proxy,
1590 (void **)&proxy->dispatch);
1591 IPSFactoryBuffer_Release(factory_buffer);
1595 proxy->lpvtbl[3] = ProxyIDispatch_GetTypeInfoCount;
1596 proxy->lpvtbl[4] = ProxyIDispatch_GetTypeInfo;
1597 proxy->lpvtbl[5] = ProxyIDispatch_GetIDsOfNames;
1598 proxy->lpvtbl[6] = ProxyIDispatch_Invoke;
1601 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
1604 proxy->lpvtbl2 = &tmproxyvtable;
1605 /* one reference for the proxy */
1607 proxy->tinfo = tinfo;
1608 memcpy(&proxy->iid,riid,sizeof(*riid));
1612 *ppv = (LPVOID)proxy;
1613 *ppProxy = (IRpcProxyBuffer *)&(proxy->lpvtbl2);
1614 IUnknown_AddRef((IUnknown *)*ppv);
1618 TMProxyImpl_Release((IRpcProxyBuffer *)&proxy->lpvtbl2);
1622 typedef struct _TMStubImpl {
1623 const IRpcStubBufferVtbl *lpvtbl;
1629 IRpcStubBuffer *dispatch_stub;
1632 static HRESULT WINAPI
1633 TMStubImpl_QueryInterface(LPRPCSTUBBUFFER iface, REFIID riid, LPVOID *ppv)
1635 if (IsEqualIID(riid,&IID_IRpcStubBuffer)||IsEqualIID(riid,&IID_IUnknown)){
1636 *ppv = (LPVOID)iface;
1637 IRpcStubBuffer_AddRef(iface);
1640 FIXME("%s, not supported IID.\n",debugstr_guid(riid));
1641 return E_NOINTERFACE;
1645 TMStubImpl_AddRef(LPRPCSTUBBUFFER iface)
1647 TMStubImpl *This = (TMStubImpl *)iface;
1648 ULONG refCount = InterlockedIncrement(&This->ref);
1650 TRACE("(%p)->(ref before=%lu)\n", This, refCount - 1);
1656 TMStubImpl_Release(LPRPCSTUBBUFFER iface)
1658 TMStubImpl *This = (TMStubImpl *)iface;
1659 ULONG refCount = InterlockedDecrement(&This->ref);
1661 TRACE("(%p)->(ref before=%lu)\n", This, refCount + 1);
1665 IRpcStubBuffer_Disconnect(iface);
1666 ITypeInfo_Release(This->tinfo);
1667 CoTaskMemFree(This);
1672 static HRESULT WINAPI
1673 TMStubImpl_Connect(LPRPCSTUBBUFFER iface, LPUNKNOWN pUnkServer)
1675 TMStubImpl *This = (TMStubImpl *)iface;
1677 TRACE("(%p)->(%p)\n", This, pUnkServer);
1679 IUnknown_AddRef(pUnkServer);
1680 This->pUnk = pUnkServer;
1682 if (This->dispatch_stub)
1683 IRpcStubBuffer_Connect(This->dispatch_stub, pUnkServer);
1689 TMStubImpl_Disconnect(LPRPCSTUBBUFFER iface)
1691 TMStubImpl *This = (TMStubImpl *)iface;
1693 TRACE("(%p)->()\n", This);
1697 IUnknown_Release(This->pUnk);
1701 if (This->dispatch_stub)
1702 IRpcStubBuffer_Disconnect(This->dispatch_stub);
1705 static HRESULT WINAPI
1707 LPRPCSTUBBUFFER iface, RPCOLEMESSAGE* xmsg,IRpcChannelBuffer*rpcchanbuf)
1710 const FUNCDESC *fdesc;
1711 TMStubImpl *This = (TMStubImpl *)iface;
1713 DWORD *args, res, *xargs, nrofargs;
1722 if (xmsg->iMethod < 3) {
1723 ERR("IUnknown methods cannot be marshaled by the typelib marshaler\n");
1724 return E_UNEXPECTED;
1727 if (This->dispatch_stub && xmsg->iMethod < sizeof(IDispatchVtbl)/sizeof(void *))
1728 return IRpcStubBuffer_Invoke(This->dispatch_stub, xmsg, rpcchanbuf);
1730 memset(&buf,0,sizeof(buf));
1731 buf.size = xmsg->cbBuffer;
1732 buf.base = HeapAlloc(GetProcessHeap(), 0, xmsg->cbBuffer);
1733 memcpy(buf.base, xmsg->Buffer, xmsg->cbBuffer);
1736 hres = _get_funcdesc(This->tinfo,xmsg->iMethod,&tinfo,&fdesc,&iname,NULL);
1738 ERR("GetFuncDesc on method %ld failed with %lx\n",xmsg->iMethod,hres);
1742 if (iname && !lstrcmpW(iname, IDispatchW))
1744 ERR("IDispatch cannot be marshaled by the typelib marshaler\n");
1745 ITypeInfo_Release(tinfo);
1746 return E_UNEXPECTED;
1749 if (iname) SysFreeString (iname);
1751 /* Need them for hack below */
1752 memset(names,0,sizeof(names));
1753 ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames);
1754 if (nrofnames > sizeof(names)/sizeof(names[0])) {
1755 ERR("Need more names!\n");
1758 /*dump_FUNCDESC(fdesc);*/
1760 for (i=0;i<fdesc->cParams;i++)
1761 nrofargs += _argsize(fdesc->lprgelemdescParam[i].tdesc.vt);
1762 args = HeapAlloc(GetProcessHeap(),0,(nrofargs+1)*sizeof(DWORD));
1763 if (!args) return E_OUTOFMEMORY;
1765 /* Allocate all stuff used by call. */
1767 for (i=0;i<fdesc->cParams;i++) {
1768 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1770 hres = deserialize_param(
1772 elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN || !elem->u.paramdesc.wParamFlags,
1779 xargs += _argsize(elem->tdesc.vt);
1781 ERR("Failed to deserialize param %s, hres %lx\n",relaystr(names[i+1]),hres);
1786 args[0] = (DWORD)This->pUnk;
1788 (*((FARPROC**)args[0]))[fdesc->oVft/4],
1796 for (i=0;i<fdesc->cParams;i++) {
1797 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1798 hres = serialize_param(
1800 elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT,
1807 xargs += _argsize(elem->tdesc.vt);
1809 ERR("Failed to stuballoc param, hres %lx\n",hres);
1814 hres = xbuf_add (&buf, (LPBYTE)&res, sizeof(DWORD));
1818 ITypeInfo_Release(tinfo);
1819 HeapFree(GetProcessHeap(), 0, args);
1821 xmsg->cbBuffer = buf.curoff;
1824 hres = IRpcChannelBuffer_GetBuffer(rpcchanbuf, xmsg, &This->iid);
1826 ERR("IRpcChannelBuffer_GetBuffer failed with error 0x%08lx\n", hres);
1830 /* FIXME: remove this case when we start sending an IRpcChannelBuffer
1831 * object with builtin OLE */
1832 RPC_STATUS status = I_RpcGetBuffer((RPC_MESSAGE *)xmsg);
1833 if (status != RPC_S_OK)
1835 ERR("I_RpcGetBuffer failed with error %ld\n", status);
1841 memcpy(xmsg->Buffer, buf.base, buf.curoff);
1843 HeapFree(GetProcessHeap(), 0, buf.base);
1845 TRACE("returning\n");
1849 static LPRPCSTUBBUFFER WINAPI
1850 TMStubImpl_IsIIDSupported(LPRPCSTUBBUFFER iface, REFIID riid) {
1851 FIXME("Huh (%s)?\n",debugstr_guid(riid));
1856 TMStubImpl_CountRefs(LPRPCSTUBBUFFER iface) {
1857 TMStubImpl *This = (TMStubImpl *)iface;
1860 return This->ref; /*FIXME? */
1863 static HRESULT WINAPI
1864 TMStubImpl_DebugServerQueryInterface(LPRPCSTUBBUFFER iface, LPVOID *ppv) {
1869 TMStubImpl_DebugServerRelease(LPRPCSTUBBUFFER iface, LPVOID ppv) {
1873 static const IRpcStubBufferVtbl tmstubvtbl = {
1874 TMStubImpl_QueryInterface,
1878 TMStubImpl_Disconnect,
1880 TMStubImpl_IsIIDSupported,
1881 TMStubImpl_CountRefs,
1882 TMStubImpl_DebugServerQueryInterface,
1883 TMStubImpl_DebugServerRelease
1886 static HRESULT WINAPI
1887 PSFacBuf_CreateStub(
1888 LPPSFACTORYBUFFER iface, REFIID riid,IUnknown *pUnkServer,
1889 IRpcStubBuffer** ppStub
1896 TRACE("(%s,%p,%p)\n",debugstr_guid(riid),pUnkServer,ppStub);
1898 hres = _get_typeinfo_for_iid(riid,&tinfo);
1900 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
1904 stub = CoTaskMemAlloc(sizeof(TMStubImpl));
1906 return E_OUTOFMEMORY;
1907 stub->lpvtbl = &tmstubvtbl;
1909 stub->tinfo = tinfo;
1910 stub->dispatch_stub = NULL;
1911 memcpy(&(stub->iid),riid,sizeof(*riid));
1912 hres = IRpcStubBuffer_Connect((LPRPCSTUBBUFFER)stub,pUnkServer);
1913 *ppStub = (LPRPCSTUBBUFFER)stub;
1914 TRACE("IRpcStubBuffer: %p\n", stub);
1916 ERR("Connect to pUnkServer failed?\n");
1918 /* if we derive from IDispatch then defer to its stub for some of its methods */
1919 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
1922 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
1924 IPSFactoryBuffer *factory_buffer;
1925 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
1928 hres = IPSFactoryBuffer_CreateStub(factory_buffer, &IID_IDispatch,
1929 pUnkServer, &stub->dispatch_stub);
1930 IPSFactoryBuffer_Release(factory_buffer);
1933 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
1939 static const IPSFactoryBufferVtbl psfacbufvtbl = {
1940 PSFacBuf_QueryInterface,
1943 PSFacBuf_CreateProxy,
1947 /* This is the whole PSFactoryBuffer object, just the vtableptr */
1948 static const IPSFactoryBufferVtbl *lppsfac = &psfacbufvtbl;
1950 /***********************************************************************
1951 * TMARSHAL_DllGetClassObject
1953 HRESULT TMARSHAL_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
1955 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)) {
1959 return E_NOINTERFACE;