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
34 #define NONAMELESSUNION
35 #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 typedef struct _marshal_state {
65 /* used in the olerelay code to avoid having the L"" stuff added by debugstr_w */
66 static char *relaystr(WCHAR *in) {
67 char *tmp = (char *)debugstr_w(in);
69 tmp[strlen(tmp)-1] = '\0';
74 xbuf_resize(marshal_state *buf, DWORD newsize)
76 if(buf->size >= newsize)
81 buf->base = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buf->base, newsize);
87 buf->base = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, newsize);
96 xbuf_add(marshal_state *buf, LPBYTE stuff, DWORD size)
100 if(buf->size - buf->curoff < size)
102 hr = xbuf_resize(buf, buf->size + size + 100);
103 if(FAILED(hr)) return hr;
105 memcpy(buf->base+buf->curoff,stuff,size);
111 xbuf_get(marshal_state *buf, LPBYTE stuff, DWORD size) {
112 if (buf->size < buf->curoff+size) return E_FAIL;
113 memcpy(stuff,buf->base+buf->curoff,size);
119 xbuf_skip(marshal_state *buf, DWORD size) {
120 if (buf->size < buf->curoff+size) return E_FAIL;
126 _unmarshal_interface(marshal_state *buf, REFIID riid, LPUNKNOWN *pUnk) {
128 ULARGE_INTEGER newpos;
129 LARGE_INTEGER seekto;
134 TRACE("...%s...\n",debugstr_guid(riid));
137 hres = xbuf_get(buf,(LPBYTE)&xsize,sizeof(xsize));
139 ERR("xbuf_get failed\n");
143 if (xsize == 0) return S_OK;
145 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
147 ERR("Stream create failed %x\n",hres);
151 hres = IStream_Write(pStm,buf->base+buf->curoff,xsize,&res);
153 ERR("stream write %x\n",hres);
157 memset(&seekto,0,sizeof(seekto));
158 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
160 ERR("Failed Seek %x\n",hres);
164 hres = CoUnmarshalInterface(pStm,riid,(LPVOID*)pUnk);
166 ERR("Unmarshalling interface %s failed with %x\n",debugstr_guid(riid),hres);
170 IStream_Release(pStm);
171 return xbuf_skip(buf,xsize);
175 _marshal_interface(marshal_state *buf, REFIID riid, LPUNKNOWN pUnk) {
176 LPBYTE tempbuf = NULL;
177 IStream *pStm = NULL;
179 ULARGE_INTEGER newpos;
180 LARGE_INTEGER seekto;
186 /* this is valid, if for instance we serialize
187 * a VT_DISPATCH with NULL ptr which apparently
188 * can happen. S_OK to make sure we continue
191 WARN("pUnk is NULL\n");
193 return xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
198 TRACE("...%s...\n",debugstr_guid(riid));
200 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
202 ERR("Stream create failed %x\n",hres);
206 hres = CoMarshalInterface(pStm,riid,pUnk,0,NULL,0);
208 ERR("Marshalling interface %s failed with %x\n", debugstr_guid(riid), hres);
212 hres = IStream_Stat(pStm,&ststg,0);
214 ERR("Stream stat failed\n");
218 tempbuf = HeapAlloc(GetProcessHeap(), 0, ststg.cbSize.u.LowPart);
219 memset(&seekto,0,sizeof(seekto));
220 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
222 ERR("Failed Seek %x\n",hres);
226 hres = IStream_Read(pStm,tempbuf,ststg.cbSize.u.LowPart,&res);
228 ERR("Failed Read %x\n",hres);
232 xsize = ststg.cbSize.u.LowPart;
233 xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
234 hres = xbuf_add(buf,tempbuf,ststg.cbSize.u.LowPart);
236 HeapFree(GetProcessHeap(),0,tempbuf);
237 IStream_Release(pStm);
243 xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
244 if (pStm) IUnknown_Release(pStm);
245 HeapFree(GetProcessHeap(), 0, tempbuf);
249 /********************* OLE Proxy/Stub Factory ********************************/
250 static HRESULT WINAPI
251 PSFacBuf_QueryInterface(LPPSFACTORYBUFFER iface, REFIID iid, LPVOID *ppv) {
252 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)||IsEqualIID(iid,&IID_IUnknown)) {
253 *ppv = (LPVOID)iface;
254 /* No ref counting, static class */
257 FIXME("(%s) unknown IID?\n",debugstr_guid(iid));
258 return E_NOINTERFACE;
261 static ULONG WINAPI PSFacBuf_AddRef(LPPSFACTORYBUFFER iface) { return 2; }
262 static ULONG WINAPI PSFacBuf_Release(LPPSFACTORYBUFFER iface) { return 1; }
265 _get_typeinfo_for_iid(REFIID riid, ITypeInfo**ti) {
268 char tlguid[200],typelibkey[300],interfacekey[300],ver[100];
271 DWORD tlguidlen, verlen, type;
275 sprintf( interfacekey, "Interface\\{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\Typelib",
276 riid->Data1, riid->Data2, riid->Data3,
277 riid->Data4[0], riid->Data4[1], riid->Data4[2], riid->Data4[3],
278 riid->Data4[4], riid->Data4[5], riid->Data4[6], riid->Data4[7]
281 if (RegOpenKeyA(HKEY_CLASSES_ROOT,interfacekey,&ikey)) {
282 ERR("No %s key found.\n",interfacekey);
286 tlguidlen = sizeof(tlguid);
287 if (RegQueryValueExA(ikey,NULL,NULL,&type,(LPBYTE)tlguid,&tlguidlen)) {
288 ERR("Getting typelib guid failed.\n");
293 verlen = sizeof(ver);
294 if (RegQueryValueExA(ikey,"Version",NULL,&type,(LPBYTE)ver,&verlen)) {
295 ERR("Could not get version value?\n");
300 sprintf(typelibkey,"Typelib\\%s\\%s\\0\\win32",tlguid,ver);
301 tlfnlen = sizeof(tlfn);
302 if (RegQueryValueA(HKEY_CLASSES_ROOT,typelibkey,tlfn,&tlfnlen)) {
303 ERR("Could not get typelib fn?\n");
306 MultiByteToWideChar(CP_ACP, 0, tlfn, -1, tlfnW, sizeof(tlfnW) / sizeof(tlfnW[0]));
307 hres = LoadTypeLib(tlfnW,&tl);
309 ERR("Failed to load typelib for %s, but it should be there.\n",debugstr_guid(riid));
312 hres = ITypeLib_GetTypeInfoOfGuid(tl,riid,ti);
314 ERR("typelib does not contain info for %s?\n",debugstr_guid(riid));
315 ITypeLib_Release(tl);
318 /* FIXME: do this? ITypeLib_Release(tl); */
322 /* Determine nr of functions. Since we use the toplevel interface and all
323 * inherited ones have lower numbers, we are ok to not to descent into
324 * the inheritance tree I think.
326 static int _nroffuncs(ITypeInfo *tinfo) {
328 const FUNCDESC *fdesc;
333 hres = ITypeInfoImpl_GetInternalFuncDesc(tinfo,n,&fdesc);
336 if (fdesc->oVft/4 > max)
345 #include "pshpack1.h"
347 typedef struct _TMAsmProxy {
361 # warning You need to implement stubless proxies for your architecture
362 typedef struct _TMAsmProxy {
366 typedef struct _TMProxyImpl {
368 const IRpcProxyBufferVtbl *lpvtbl2;
371 TMAsmProxy *asmstubs;
373 IRpcChannelBuffer* chanbuf;
375 CRITICAL_SECTION crit;
376 IUnknown *outerunknown;
378 IRpcProxyBuffer *dispatch_proxy;
381 static HRESULT WINAPI
382 TMProxyImpl_QueryInterface(LPRPCPROXYBUFFER iface, REFIID riid, LPVOID *ppv)
385 if (IsEqualIID(riid,&IID_IUnknown)||IsEqualIID(riid,&IID_IRpcProxyBuffer)) {
386 *ppv = (LPVOID)iface;
387 IRpcProxyBuffer_AddRef(iface);
390 FIXME("no interface for %s\n",debugstr_guid(riid));
391 return E_NOINTERFACE;
395 TMProxyImpl_AddRef(LPRPCPROXYBUFFER iface)
397 ICOM_THIS_MULTI(TMProxyImpl,lpvtbl2,iface);
398 ULONG refCount = InterlockedIncrement(&This->ref);
400 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
406 TMProxyImpl_Release(LPRPCPROXYBUFFER iface)
408 ICOM_THIS_MULTI(TMProxyImpl,lpvtbl2,iface);
409 ULONG refCount = InterlockedDecrement(&This->ref);
411 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
415 if (This->dispatch_proxy) IRpcProxyBuffer_Release(This->dispatch_proxy);
416 DeleteCriticalSection(&This->crit);
417 if (This->chanbuf) IRpcChannelBuffer_Release(This->chanbuf);
418 VirtualFree(This->asmstubs, 0, MEM_RELEASE);
419 HeapFree(GetProcessHeap(), 0, This->lpvtbl);
425 static HRESULT WINAPI
427 LPRPCPROXYBUFFER iface,IRpcChannelBuffer* pRpcChannelBuffer)
429 ICOM_THIS_MULTI(TMProxyImpl, lpvtbl2, iface);
431 TRACE("(%p)\n", pRpcChannelBuffer);
433 EnterCriticalSection(&This->crit);
435 IRpcChannelBuffer_AddRef(pRpcChannelBuffer);
436 This->chanbuf = pRpcChannelBuffer;
438 LeaveCriticalSection(&This->crit);
440 if (This->dispatch_proxy)
441 IRpcProxyBuffer_Connect(This->dispatch_proxy, pRpcChannelBuffer);
447 TMProxyImpl_Disconnect(LPRPCPROXYBUFFER iface)
449 ICOM_THIS_MULTI(TMProxyImpl, lpvtbl2, iface);
453 EnterCriticalSection(&This->crit);
455 IRpcChannelBuffer_Release(This->chanbuf);
456 This->chanbuf = NULL;
458 LeaveCriticalSection(&This->crit);
460 if (This->dispatch_proxy)
461 IRpcProxyBuffer_Disconnect(This->dispatch_proxy);
465 static const IRpcProxyBufferVtbl tmproxyvtable = {
466 TMProxyImpl_QueryInterface,
470 TMProxyImpl_Disconnect
473 /* how much space do we use on stack in DWORD steps. */
478 return 8/sizeof(DWORD);
480 return sizeof(double)/sizeof(DWORD);
482 return sizeof(CY)/sizeof(DWORD);
484 return sizeof(DATE)/sizeof(DWORD);
486 return (sizeof(VARIANT)+3)/sizeof(DWORD);
493 _xsize(TYPEDESC *td) {
498 return sizeof(VARIANT)+3;
501 ARRAYDESC *adesc = td->u.lpadesc;
503 for (i=0;i<adesc->cDims;i++)
504 arrsize *= adesc->rgbounds[i].cElements;
505 return arrsize*_xsize(&adesc->tdescElem);
533 TRACE("(tdesc.vt %s)\n",debugstr_vt(tdesc->vt));
536 case VT_EMPTY: /* nothing. empty variant for instance */
542 if (debugout) TRACE_(olerelay)("%x%x",arg[0],arg[1]);
544 hres = xbuf_add(buf,(LPBYTE)arg,8);
554 if (debugout) TRACE_(olerelay)("%x",*arg);
556 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
561 if (debugout) TRACE_(olerelay)("%04x",*arg & 0xffff);
563 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
568 if (debugout) TRACE_(olerelay)("%02x",*arg & 0xff);
570 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
574 if (debugout) TRACE_(olerelay)("&0x%x",*arg);
576 hres = xbuf_add(buf,(LPBYTE)(DWORD*)*arg,sizeof(DWORD));
577 /* do not dealloc at this time */
581 VARIANT *vt = (VARIANT*)arg;
582 DWORD vttype = V_VT(vt);
584 if (debugout) TRACE_(olerelay)("Vt(%s%s)(",debugstr_vt(vttype),debugstr_vf(vttype));
587 hres = xbuf_add(buf,(LPBYTE)&vttype,sizeof(vttype));
588 if (hres) return hres;
590 /* need to recurse since we need to free the stuff */
591 hres = serialize_param(tinfo,writeit,debugout,dealloc,&tdesc2,(DWORD*)&(V_I4(vt)),buf);
592 if (debugout) TRACE_(olerelay)(")");
595 case VT_BSTR|VT_BYREF: {
596 if (debugout) TRACE_(olerelay)("[byref]'%s'", *(BSTR*)*arg ? relaystr(*((BSTR*)*arg)) : "<bstr NULL>");
598 /* ptr to ptr to magic widestring, basically */
599 BSTR *bstr = (BSTR *) *arg;
602 /* -1 means "null string" which is equivalent to empty string */
604 hres = xbuf_add(buf, (LPBYTE)&len,sizeof(DWORD));
605 if (hres) return hres;
607 len = *((DWORD*)*bstr-1)/sizeof(WCHAR);
608 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
609 if (hres) return hres;
610 hres = xbuf_add(buf,(LPBYTE)*bstr,len * sizeof(WCHAR));
611 if (hres) return hres;
615 if (dealloc && arg) {
616 BSTR *str = *((BSTR **)arg);
625 TRACE_(olerelay)("%s",relaystr((WCHAR*)*arg));
627 TRACE_(olerelay)("<bstr NULL>");
630 BSTR bstr = (BSTR)*arg;
634 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
635 if (hres) return hres;
637 len = *((DWORD*)bstr-1)/sizeof(WCHAR);
638 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
639 if (hres) return hres;
640 hres = xbuf_add(buf,(LPBYTE)bstr,len * sizeof(WCHAR));
641 if (hres) return hres;
646 SysFreeString((BSTR)*arg);
651 BOOL derefhere = TRUE;
653 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
657 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
659 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
662 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
663 switch (tattr->typekind) {
664 case TKIND_ENUM: /* confirmed */
665 case TKIND_RECORD: /* FIXME: mostly untested */
668 case TKIND_ALIAS: /* FIXME: untested */
669 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
670 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
674 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
678 ITypeInfo_Release(tinfo2);
681 if (debugout) TRACE_(olerelay)("*");
682 /* Write always, so the other side knows when it gets a NULL pointer.
684 cookie = *arg ? 0x42424242 : 0;
685 hres = xbuf_add(buf,(LPBYTE)&cookie,sizeof(cookie));
689 if (debugout) TRACE_(olerelay)("NULL");
692 hres = serialize_param(tinfo,writeit,debugout,dealloc,tdesc->u.lptdesc,(DWORD*)*arg,buf);
693 if (derefhere && dealloc) HeapFree(GetProcessHeap(),0,(LPVOID)*arg);
697 if (debugout) TRACE_(olerelay)("unk(0x%x)",*arg);
699 hres = _marshal_interface(buf,&IID_IUnknown,(LPUNKNOWN)*arg);
700 if (dealloc && *(IUnknown **)arg)
701 IUnknown_Release((LPUNKNOWN)*arg);
704 if (debugout) TRACE_(olerelay)("idisp(0x%x)",*arg);
706 hres = _marshal_interface(buf,&IID_IDispatch,(LPUNKNOWN)*arg);
707 if (dealloc && *(IUnknown **)arg)
708 IUnknown_Release((LPUNKNOWN)*arg);
711 if (debugout) TRACE_(olerelay)("<void>");
713 case VT_USERDEFINED: {
717 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
719 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
722 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
723 switch (tattr->typekind) {
725 case TKIND_INTERFACE:
727 hres=_marshal_interface(buf,&(tattr->guid),(LPUNKNOWN)arg);
729 IUnknown_Release((LPUNKNOWN)arg);
733 if (debugout) TRACE_(olerelay)("{");
734 for (i=0;i<tattr->cVars;i++) {
739 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
741 ERR("Could not get vardesc of %d\n",i);
744 /* Need them for hack below */
746 memset(names,0,sizeof(names));
747 hres = ITypeInfo_GetNames(tinfo2,vdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames);
748 if (nrofnames > sizeof(names)/sizeof(names[0])) {
749 ERR("Need more names!\n");
751 if (!hres && debugout)
752 TRACE_(olerelay)("%s=",relaystr(names[0]));
754 elem2 = &vdesc->elemdescVar;
755 tdesc2 = &elem2->tdesc;
756 hres = serialize_param(
762 (DWORD*)(((LPBYTE)arg)+vdesc->u.oInst),
765 ITypeInfo_ReleaseVarDesc(tinfo2, vdesc);
768 if (debugout && (i<(tattr->cVars-1)))
769 TRACE_(olerelay)(",");
771 if (debugout) TRACE_(olerelay)("}");
775 return serialize_param(tinfo2,writeit,debugout,dealloc,&tattr->tdescAlias,arg,buf);
778 if (debugout) TRACE_(olerelay)("%x",*arg);
780 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
783 FIXME("Unhandled typekind %d\n",tattr->typekind);
787 ITypeInfo_Release(tinfo2);
791 ARRAYDESC *adesc = tdesc->u.lpadesc;
794 if (debugout) TRACE_(olerelay)("carr");
795 for (i=0;i<adesc->cDims;i++) {
796 if (debugout) TRACE_(olerelay)("[%d]",adesc->rgbounds[i].cElements);
797 arrsize *= adesc->rgbounds[i].cElements;
799 if (debugout) TRACE_(olerelay)("(vt %s)",debugstr_vt(adesc->tdescElem.vt));
800 if (debugout) TRACE_(olerelay)("[");
801 for (i=0;i<arrsize;i++) {
802 hres = serialize_param(tinfo, writeit, debugout, dealloc, &adesc->tdescElem, (DWORD*)((LPBYTE)arg+i*_xsize(&adesc->tdescElem)), buf);
805 if (debugout && (i<arrsize-1)) TRACE_(olerelay)(",");
807 if (debugout) TRACE_(olerelay)("]");
813 unsigned long flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
814 unsigned long size = LPSAFEARRAY_UserSize(&flags, buf->curoff, (LPSAFEARRAY *)arg);
815 xbuf_resize(buf, size);
816 LPSAFEARRAY_UserMarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
822 ERR("Unhandled marshal type %d.\n",tdesc->vt);
839 TRACE("vt %s at %p\n",debugstr_vt(tdesc->vt),arg);
844 if (debugout) TRACE_(olerelay)("<empty>");
847 if (debugout) TRACE_(olerelay)("<null>");
850 VARIANT *vt = (VARIANT*)arg;
855 hres = xbuf_get(buf,(LPBYTE)&vttype,sizeof(vttype));
857 FIXME("vt type not read?\n");
860 memset(&tdesc2,0,sizeof(tdesc2));
863 if (debugout) TRACE_(olerelay)("Vt(%s%s)(",debugstr_vt(vttype),debugstr_vf(vttype));
864 hres = deserialize_param(tinfo, readit, debugout, alloc, &tdesc2, (DWORD*)&(V_I4(vt)), buf);
865 TRACE_(olerelay)(")");
876 hres = xbuf_get(buf,(LPBYTE)arg,8);
877 if (hres) ERR("Failed to read integer 8 byte\n");
879 if (debugout) TRACE_(olerelay)("%x%x",arg[0],arg[1]);
889 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
890 if (hres) ERR("Failed to read integer 4 byte\n");
892 if (debugout) TRACE_(olerelay)("%x",*arg);
898 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
899 if (hres) ERR("Failed to read integer 4 byte\n");
902 if (debugout) TRACE_(olerelay)("%04x",*arg & 0xffff);
908 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
909 if (hres) ERR("Failed to read integer 4 byte\n");
912 if (debugout) TRACE_(olerelay)("%02x",*arg & 0xff);
917 *arg = (DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
919 hres = xbuf_get(buf,(LPBYTE)*arg,sizeof(DWORD));
920 if (hres) ERR("Failed to read integer 4 byte\n");
922 if (debugout) TRACE_(olerelay)("&0x%x",*(DWORD*)*arg);
924 case VT_BSTR|VT_BYREF: {
925 BSTR **bstr = (BSTR **)arg;
930 hres = xbuf_get(buf,(LPBYTE)&len,sizeof(DWORD));
932 ERR("failed to read bstr klen\n");
936 *bstr = CoTaskMemAlloc(sizeof(BSTR *));
938 if (debugout) TRACE_(olerelay)("<bstr NULL>");
940 str = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(len+1)*sizeof(WCHAR));
941 hres = xbuf_get(buf,(LPBYTE)str,len*sizeof(WCHAR));
943 ERR("Failed to read BSTR.\n");
946 *bstr = CoTaskMemAlloc(sizeof(BSTR *));
947 **bstr = SysAllocStringLen(str,len);
948 if (debugout) TRACE_(olerelay)("%s",relaystr(str));
949 HeapFree(GetProcessHeap(),0,str);
961 hres = xbuf_get(buf,(LPBYTE)&len,sizeof(DWORD));
963 ERR("failed to read bstr klen\n");
968 if (debugout) TRACE_(olerelay)("<bstr NULL>");
970 str = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(len+1)*sizeof(WCHAR));
971 hres = xbuf_get(buf,(LPBYTE)str,len*sizeof(WCHAR));
973 ERR("Failed to read BSTR.\n");
976 *arg = (DWORD)SysAllocStringLen(str,len);
977 if (debugout) TRACE_(olerelay)("%s",relaystr(str));
978 HeapFree(GetProcessHeap(),0,str);
987 BOOL derefhere = TRUE;
989 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
993 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
995 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
998 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
999 switch (tattr->typekind) {
1000 case TKIND_ENUM: /* confirmed */
1001 case TKIND_RECORD: /* FIXME: mostly untested */
1004 case TKIND_ALIAS: /* FIXME: untested */
1005 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
1006 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
1010 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
1014 ITypeInfo_Release(tinfo2);
1016 /* read it in all cases, we need to know if we have
1017 * NULL pointer or not.
1019 hres = xbuf_get(buf,(LPBYTE)&cookie,sizeof(cookie));
1021 ERR("Failed to load pointer cookie.\n");
1024 if (cookie != 0x42424242) {
1025 /* we read a NULL ptr from the remote side */
1026 if (debugout) TRACE_(olerelay)("NULL");
1030 if (debugout) TRACE_(olerelay)("*");
1032 /* Allocate space for the referenced struct */
1034 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,_xsize(tdesc->u.lptdesc));
1037 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, (LPDWORD)*arg, buf);
1039 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, arg, buf);
1042 /* FIXME: UNKNOWN is unknown ..., but allocate 4 byte for it */
1044 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
1047 hres = _unmarshal_interface(buf,&IID_IUnknown,(LPUNKNOWN*)arg);
1049 TRACE_(olerelay)("unk(%p)",arg);
1054 hres = _unmarshal_interface(buf,&IID_IDispatch,(LPUNKNOWN*)arg);
1056 TRACE_(olerelay)("idisp(%p)",arg);
1059 if (debugout) TRACE_(olerelay)("<void>");
1061 case VT_USERDEFINED: {
1065 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
1067 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
1070 hres = ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1072 ERR("Could not get typeattr in VT_USERDEFINED.\n");
1074 switch (tattr->typekind) {
1075 case TKIND_DISPATCH:
1076 case TKIND_INTERFACE:
1078 hres = _unmarshal_interface(buf,&(tattr->guid),(LPUNKNOWN*)arg);
1080 case TKIND_RECORD: {
1084 *arg = (DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,tattr->cbSizeInstance);
1086 if (debugout) TRACE_(olerelay)("{");
1087 for (i=0;i<tattr->cVars;i++) {
1090 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
1092 ERR("Could not get vardesc of %d\n",i);
1095 hres = deserialize_param(
1100 &vdesc->elemdescVar.tdesc,
1101 (DWORD*)(((LPBYTE)*arg)+vdesc->u.oInst),
1104 ITypeInfo2_ReleaseVarDesc(tinfo2, vdesc);
1105 if (debugout && (i<tattr->cVars-1)) TRACE_(olerelay)(",");
1107 if (debugout) TRACE_(olerelay)("}");
1111 return deserialize_param(tinfo2,readit,debugout,alloc,&tattr->tdescAlias,arg,buf);
1114 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
1115 if (hres) ERR("Failed to read enum (4 byte)\n");
1117 if (debugout) TRACE_(olerelay)("%x",*arg);
1120 ERR("Unhandled typekind %d\n",tattr->typekind);
1126 ERR("failed to stuballoc in TKIND_RECORD.\n");
1127 ITypeInfo_Release(tinfo2);
1131 /* arg is pointing to the start of the array. */
1132 ARRAYDESC *adesc = tdesc->u.lpadesc;
1135 if (adesc->cDims > 1) FIXME("cDims > 1 in VT_CARRAY. Does it work?\n");
1136 for (i=0;i<adesc->cDims;i++)
1137 arrsize *= adesc->rgbounds[i].cElements;
1138 for (i=0;i<arrsize;i++)
1145 (DWORD*)((LPBYTE)(arg)+i*_xsize(&adesc->tdescElem)),
1150 case VT_SAFEARRAY: {
1153 unsigned long flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
1154 unsigned char *buffer;
1155 buffer = LPSAFEARRAY_UserUnmarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
1156 buf->curoff = buffer - buf->base;
1161 ERR("No handler for VT type %d!\n",tdesc->vt);
1167 /* Searches function, also in inherited interfaces */
1170 ITypeInfo *tinfo, int iMethod, ITypeInfo **tactual, const FUNCDESC **fdesc, BSTR *iname, BSTR *fname)
1175 if (fname) *fname = NULL;
1176 if (iname) *iname = NULL;
1179 ITypeInfo_AddRef(*tactual);
1182 hres = ITypeInfoImpl_GetInternalFuncDesc(tinfo, i, fdesc);
1189 hres = ITypeInfo_GetTypeAttr(tinfo, &attr);
1191 ERR("GetTypeAttr failed with %x\n",hres);
1194 /* Not found, so look in inherited ifaces. */
1195 for (j=0;j<attr->cImplTypes;j++) {
1196 hres = ITypeInfo_GetRefTypeOfImplType(tinfo, j, &href);
1198 ERR("Did not find a reftype for interface offset %d?\n",j);
1201 hres = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
1203 ERR("Did not find a typeinfo for reftype %d?\n",href);
1206 hres = _get_funcdesc(tinfo2,iMethod,tactual,fdesc,iname,fname);
1207 ITypeInfo_Release(tinfo2);
1208 if (!hres) return S_OK;
1212 if (((*fdesc)->oVft/4) == iMethod) {
1214 ITypeInfo_GetDocumentation(tinfo,(*fdesc)->memid,fname,NULL,NULL,NULL);
1216 ITypeInfo_GetDocumentation(tinfo,-1,iname,NULL,NULL,NULL);
1224 xCall(LPVOID retptr, int method, TMProxyImpl *tpinfo /*, args */)
1226 DWORD *args = ((DWORD*)&tpinfo)+1, *xargs;
1227 const FUNCDESC *fdesc;
1229 int i, relaydeb = TRACE_ON(olerelay);
1236 DWORD remoteresult = 0;
1238 IRpcChannelBuffer *chanbuf;
1240 EnterCriticalSection(&tpinfo->crit);
1242 hres = _get_funcdesc(tpinfo->tinfo,method,&tinfo,&fdesc,&iname,&fname);
1244 ERR("Did not find typeinfo/funcdesc entry for method %d!\n",method);
1245 ITypeInfo_Release(tinfo);
1246 LeaveCriticalSection(&tpinfo->crit);
1250 if (!tpinfo->chanbuf)
1252 WARN("Tried to use disconnected proxy\n");
1253 ITypeInfo_Release(tinfo);
1254 LeaveCriticalSection(&tpinfo->crit);
1255 return RPC_E_DISCONNECTED;
1257 chanbuf = tpinfo->chanbuf;
1258 IRpcChannelBuffer_AddRef(chanbuf);
1260 LeaveCriticalSection(&tpinfo->crit);
1263 TRACE_(olerelay)("->");
1265 TRACE_(olerelay)("%s:",relaystr(iname));
1267 TRACE_(olerelay)("%s(%d)",relaystr(fname),method);
1269 TRACE_(olerelay)("%d",method);
1270 TRACE_(olerelay)("(");
1273 if (iname) SysFreeString(iname);
1274 if (fname) SysFreeString(fname);
1276 memset(&buf,0,sizeof(buf));
1278 /* normal typelib driven serializing */
1280 /* Need them for hack below */
1281 memset(names,0,sizeof(names));
1282 if (ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames))
1284 if (nrofnames > sizeof(names)/sizeof(names[0]))
1285 ERR("Need more names!\n");
1288 for (i=0;i<fdesc->cParams;i++) {
1289 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1291 if (i) TRACE_(olerelay)(",");
1292 if (i+1<nrofnames && names[i+1])
1293 TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1295 /* No need to marshal other data than FIN and any VT_PTR. */
1296 if (!(elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN || !elem->u.paramdesc.wParamFlags) && (elem->tdesc.vt != VT_PTR)) {
1297 xargs+=_argsize(elem->tdesc.vt);
1298 if (relaydeb) TRACE_(olerelay)("[out]");
1301 hres = serialize_param(
1303 elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN || !elem->u.paramdesc.wParamFlags,
1312 ERR("Failed to serialize param, hres %x\n",hres);
1315 xargs+=_argsize(elem->tdesc.vt);
1317 if (relaydeb) TRACE_(olerelay)(")");
1319 memset(&msg,0,sizeof(msg));
1320 msg.cbBuffer = buf.curoff;
1321 msg.iMethod = method;
1322 hres = IRpcChannelBuffer_GetBuffer(chanbuf,&msg,&(tpinfo->iid));
1324 ERR("RpcChannelBuffer GetBuffer failed, %x\n",hres);
1327 memcpy(msg.Buffer,buf.base,buf.curoff);
1328 if (relaydeb) TRACE_(olerelay)("\n");
1329 hres = IRpcChannelBuffer_SendReceive(chanbuf,&msg,&status);
1331 ERR("RpcChannelBuffer SendReceive failed, %x\n",hres);
1335 if (relaydeb) TRACE_(olerelay)(" status = %08x (",status);
1337 buf.base = HeapReAlloc(GetProcessHeap(),0,buf.base,msg.cbBuffer);
1339 buf.base = HeapAlloc(GetProcessHeap(),0,msg.cbBuffer);
1340 buf.size = msg.cbBuffer;
1341 memcpy(buf.base,msg.Buffer,buf.size);
1344 /* generic deserializer using typelib description */
1347 for (i=0;i<fdesc->cParams;i++) {
1348 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1351 if (i) TRACE_(olerelay)(",");
1352 if (i+1<nrofnames && names[i+1]) TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1354 /* No need to marshal other data than FOUT and any VT_PTR */
1355 if (!(elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT) && (elem->tdesc.vt != VT_PTR)) {
1356 xargs += _argsize(elem->tdesc.vt);
1357 if (relaydeb) TRACE_(olerelay)("[in]");
1360 hres = deserialize_param(
1362 elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT,
1370 ERR("Failed to unmarshall param, hres %x\n",hres);
1374 xargs += _argsize(elem->tdesc.vt);
1377 hres = xbuf_get(&buf, (LPBYTE)&remoteresult, sizeof(DWORD));
1380 if (relaydeb) TRACE_(olerelay)(") = %08x\n", remoteresult);
1382 hres = remoteresult;
1385 HeapFree(GetProcessHeap(),0,buf.base);
1386 IRpcChannelBuffer_Release(chanbuf);
1387 ITypeInfo_Release(tinfo);
1388 TRACE("-- 0x%08x\n", hres);
1392 static HRESULT WINAPI ProxyIUnknown_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
1394 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1396 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1398 if (proxy->outerunknown)
1399 return IUnknown_QueryInterface(proxy->outerunknown, riid, ppv);
1401 FIXME("No interface\n");
1402 return E_NOINTERFACE;
1405 static ULONG WINAPI ProxyIUnknown_AddRef(IUnknown *iface)
1407 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1411 if (proxy->outerunknown)
1412 return IUnknown_AddRef(proxy->outerunknown);
1414 return 2; /* FIXME */
1417 static ULONG WINAPI ProxyIUnknown_Release(IUnknown *iface)
1419 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1423 if (proxy->outerunknown)
1424 return IUnknown_Release(proxy->outerunknown);
1426 return 1; /* FIXME */
1429 static HRESULT WINAPI ProxyIDispatch_GetTypeInfoCount(LPDISPATCH iface, UINT * pctinfo)
1431 TMProxyImpl *This = (TMProxyImpl *)iface;
1433 TRACE("(%p)\n", pctinfo);
1435 return IDispatch_GetTypeInfoCount(This->dispatch, pctinfo);
1438 static HRESULT WINAPI ProxyIDispatch_GetTypeInfo(LPDISPATCH iface, UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo)
1440 TMProxyImpl *This = (TMProxyImpl *)iface;
1442 TRACE("(%d, %x, %p)\n", iTInfo, lcid, ppTInfo);
1444 return IDispatch_GetTypeInfo(This->dispatch, iTInfo, lcid, ppTInfo);
1447 static HRESULT WINAPI ProxyIDispatch_GetIDsOfNames(LPDISPATCH iface, REFIID riid, LPOLESTR * rgszNames, UINT cNames, LCID lcid, DISPID * rgDispId)
1449 TMProxyImpl *This = (TMProxyImpl *)iface;
1451 TRACE("(%s, %p, %d, 0x%x, %p)\n", debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);
1453 return IDispatch_GetIDsOfNames(This->dispatch, riid, rgszNames,
1454 cNames, lcid, rgDispId);
1457 static HRESULT WINAPI ProxyIDispatch_Invoke(LPDISPATCH iface, DISPID dispIdMember, REFIID riid, LCID lcid,
1458 WORD wFlags, DISPPARAMS * pDispParams, VARIANT * pVarResult,
1459 EXCEPINFO * pExcepInfo, UINT * puArgErr)
1461 TMProxyImpl *This = (TMProxyImpl *)iface;
1463 TRACE("(%d, %s, 0x%x, 0x%x, %p, %p, %p, %p)\n", dispIdMember,
1464 debugstr_guid(riid), lcid, wFlags, pDispParams, pVarResult,
1465 pExcepInfo, puArgErr);
1467 return IDispatch_Invoke(This->dispatch, dispIdMember, riid, lcid,
1468 wFlags, pDispParams, pVarResult, pExcepInfo,
1472 static inline HRESULT get_facbuf_for_iid(REFIID riid, IPSFactoryBuffer **facbuf)
1477 if ((hr = CoGetPSClsid(riid, &clsid)))
1479 return CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL,
1480 &IID_IPSFactoryBuffer, (LPVOID*)facbuf);
1483 static HRESULT WINAPI
1484 PSFacBuf_CreateProxy(
1485 LPPSFACTORYBUFFER iface, IUnknown* pUnkOuter, REFIID riid,
1486 IRpcProxyBuffer **ppProxy, LPVOID *ppv)
1491 const FUNCDESC *fdesc;
1495 TRACE("(...%s...)\n",debugstr_guid(riid));
1496 hres = _get_typeinfo_for_iid(riid,&tinfo);
1498 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
1501 nroffuncs = _nroffuncs(tinfo);
1502 proxy = CoTaskMemAlloc(sizeof(TMProxyImpl));
1503 if (!proxy) return E_OUTOFMEMORY;
1505 assert(sizeof(TMAsmProxy) == 12);
1507 proxy->dispatch = NULL;
1508 proxy->dispatch_proxy = NULL;
1509 proxy->outerunknown = pUnkOuter;
1510 proxy->asmstubs = VirtualAlloc(NULL, sizeof(TMAsmProxy) * nroffuncs, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
1511 if (!proxy->asmstubs) {
1512 ERR("Could not commit pages for proxy thunks\n");
1513 CoTaskMemFree(proxy);
1514 return E_OUTOFMEMORY;
1517 InitializeCriticalSection(&proxy->crit);
1519 proxy->lpvtbl = HeapAlloc(GetProcessHeap(),0,sizeof(LPBYTE)*nroffuncs);
1520 for (i=0;i<nroffuncs;i++) {
1521 TMAsmProxy *xasm = proxy->asmstubs+i;
1525 proxy->lpvtbl[i] = ProxyIUnknown_QueryInterface;
1528 proxy->lpvtbl[i] = ProxyIUnknown_AddRef;
1531 proxy->lpvtbl[i] = ProxyIUnknown_Release;
1535 /* nrofargs without This */
1538 hres = _get_funcdesc(tinfo,i,&tinfo2,&fdesc,NULL,NULL);
1539 ITypeInfo_Release(tinfo2);
1541 ERR("GetFuncDesc %x should not fail here.\n",hres);
1544 /* some args take more than 4 byte on the stack */
1546 for (j=0;j<fdesc->cParams;j++)
1547 nrofargs += _argsize(fdesc->lprgelemdescParam[j].tdesc.vt);
1550 if (fdesc->callconv != CC_STDCALL) {
1551 ERR("calling convention is not stdcall????\n");
1554 /* popl %eax - return ptr
1561 * arg3 arg2 arg1 <method> <returnptr>
1563 xasm->popleax = 0x58;
1564 xasm->pushlval = 0x6a;
1566 xasm->pushleax = 0x50;
1567 xasm->lcall = 0xe8; /* relative jump */
1568 xasm->xcall = (DWORD)xCall;
1569 xasm->xcall -= (DWORD)&(xasm->lret);
1571 xasm->bytestopop= (nrofargs+2)*4; /* pop args, This, iMethod */
1572 proxy->lpvtbl[i] = xasm;
1575 FIXME("not implemented on non i386\n");
1582 /* if we derive from IDispatch then defer to its proxy for its methods */
1583 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
1586 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
1588 IPSFactoryBuffer *factory_buffer;
1589 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
1592 hres = IPSFactoryBuffer_CreateProxy(factory_buffer, NULL,
1593 &IID_IDispatch, &proxy->dispatch_proxy,
1594 (void **)&proxy->dispatch);
1595 IPSFactoryBuffer_Release(factory_buffer);
1599 proxy->lpvtbl[3] = ProxyIDispatch_GetTypeInfoCount;
1600 proxy->lpvtbl[4] = ProxyIDispatch_GetTypeInfo;
1601 proxy->lpvtbl[5] = ProxyIDispatch_GetIDsOfNames;
1602 proxy->lpvtbl[6] = ProxyIDispatch_Invoke;
1605 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
1608 proxy->lpvtbl2 = &tmproxyvtable;
1609 /* one reference for the proxy */
1611 proxy->tinfo = tinfo;
1612 memcpy(&proxy->iid,riid,sizeof(*riid));
1616 *ppv = (LPVOID)proxy;
1617 *ppProxy = (IRpcProxyBuffer *)&(proxy->lpvtbl2);
1618 IUnknown_AddRef((IUnknown *)*ppv);
1622 TMProxyImpl_Release((IRpcProxyBuffer *)&proxy->lpvtbl2);
1626 typedef struct _TMStubImpl {
1627 const IRpcStubBufferVtbl *lpvtbl;
1633 IRpcStubBuffer *dispatch_stub;
1634 BOOL dispatch_derivative;
1637 static HRESULT WINAPI
1638 TMStubImpl_QueryInterface(LPRPCSTUBBUFFER iface, REFIID riid, LPVOID *ppv)
1640 if (IsEqualIID(riid,&IID_IRpcStubBuffer)||IsEqualIID(riid,&IID_IUnknown)){
1641 *ppv = (LPVOID)iface;
1642 IRpcStubBuffer_AddRef(iface);
1645 FIXME("%s, not supported IID.\n",debugstr_guid(riid));
1646 return E_NOINTERFACE;
1650 TMStubImpl_AddRef(LPRPCSTUBBUFFER iface)
1652 TMStubImpl *This = (TMStubImpl *)iface;
1653 ULONG refCount = InterlockedIncrement(&This->ref);
1655 TRACE("(%p)->(ref before=%u)\n", This, refCount - 1);
1661 TMStubImpl_Release(LPRPCSTUBBUFFER iface)
1663 TMStubImpl *This = (TMStubImpl *)iface;
1664 ULONG refCount = InterlockedDecrement(&This->ref);
1666 TRACE("(%p)->(ref before=%u)\n", This, refCount + 1);
1670 IRpcStubBuffer_Disconnect(iface);
1671 ITypeInfo_Release(This->tinfo);
1672 if (This->dispatch_stub)
1673 IRpcStubBuffer_Release(This->dispatch_stub);
1674 CoTaskMemFree(This);
1679 static HRESULT WINAPI
1680 TMStubImpl_Connect(LPRPCSTUBBUFFER iface, LPUNKNOWN pUnkServer)
1682 TMStubImpl *This = (TMStubImpl *)iface;
1684 TRACE("(%p)->(%p)\n", This, pUnkServer);
1686 IUnknown_AddRef(pUnkServer);
1687 This->pUnk = pUnkServer;
1689 if (This->dispatch_stub)
1690 IRpcStubBuffer_Connect(This->dispatch_stub, pUnkServer);
1696 TMStubImpl_Disconnect(LPRPCSTUBBUFFER iface)
1698 TMStubImpl *This = (TMStubImpl *)iface;
1700 TRACE("(%p)->()\n", This);
1704 IUnknown_Release(This->pUnk);
1708 if (This->dispatch_stub)
1709 IRpcStubBuffer_Disconnect(This->dispatch_stub);
1712 static HRESULT WINAPI
1714 LPRPCSTUBBUFFER iface, RPCOLEMESSAGE* xmsg,IRpcChannelBuffer*rpcchanbuf)
1717 const FUNCDESC *fdesc;
1718 TMStubImpl *This = (TMStubImpl *)iface;
1720 DWORD *args, res, *xargs, nrofargs;
1729 if (xmsg->iMethod < 3) {
1730 ERR("IUnknown methods cannot be marshaled by the typelib marshaler\n");
1731 return E_UNEXPECTED;
1734 if (This->dispatch_derivative && xmsg->iMethod < sizeof(IDispatchVtbl)/sizeof(void *))
1736 IPSFactoryBuffer *factory_buffer;
1737 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
1740 hres = IPSFactoryBuffer_CreateStub(factory_buffer, &IID_IDispatch,
1741 This->pUnk, &This->dispatch_stub);
1742 IPSFactoryBuffer_Release(factory_buffer);
1746 return IRpcStubBuffer_Invoke(This->dispatch_stub, xmsg, rpcchanbuf);
1749 memset(&buf,0,sizeof(buf));
1750 buf.size = xmsg->cbBuffer;
1751 buf.base = HeapAlloc(GetProcessHeap(), 0, xmsg->cbBuffer);
1752 memcpy(buf.base, xmsg->Buffer, xmsg->cbBuffer);
1755 hres = _get_funcdesc(This->tinfo,xmsg->iMethod,&tinfo,&fdesc,&iname,NULL);
1757 ERR("GetFuncDesc on method %d failed with %x\n",xmsg->iMethod,hres);
1761 if (iname && !lstrcmpW(iname, IDispatchW))
1763 ERR("IDispatch cannot be marshaled by the typelib marshaler\n");
1764 ITypeInfo_Release(tinfo);
1765 return E_UNEXPECTED;
1768 if (iname) SysFreeString (iname);
1770 /* Need them for hack below */
1771 memset(names,0,sizeof(names));
1772 ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames);
1773 if (nrofnames > sizeof(names)/sizeof(names[0])) {
1774 ERR("Need more names!\n");
1777 /*dump_FUNCDESC(fdesc);*/
1779 for (i=0;i<fdesc->cParams;i++)
1780 nrofargs += _argsize(fdesc->lprgelemdescParam[i].tdesc.vt);
1781 args = HeapAlloc(GetProcessHeap(),0,(nrofargs+1)*sizeof(DWORD));
1782 if (!args) return E_OUTOFMEMORY;
1784 /* Allocate all stuff used by call. */
1786 for (i=0;i<fdesc->cParams;i++) {
1787 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1789 hres = deserialize_param(
1791 elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN || !elem->u.paramdesc.wParamFlags,
1798 xargs += _argsize(elem->tdesc.vt);
1800 ERR("Failed to deserialize param %s, hres %x\n",relaystr(names[i+1]),hres);
1805 args[0] = (DWORD)This->pUnk;
1810 (*((FARPROC**)args[0]))[fdesc->oVft/4],
1818 DWORD dwExceptionCode = GetExceptionCode();
1819 ERR("invoke call failed with exception 0x%08x (%d)\n", dwExceptionCode, dwExceptionCode);
1820 if (FAILED(dwExceptionCode))
1821 hres = dwExceptionCode;
1823 hres = HRESULT_FROM_WIN32(dwExceptionCode);
1833 for (i=0;i<fdesc->cParams;i++) {
1834 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1835 hres = serialize_param(
1837 elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT,
1844 xargs += _argsize(elem->tdesc.vt);
1846 ERR("Failed to stuballoc param, hres %x\n",hres);
1851 hres = xbuf_add (&buf, (LPBYTE)&res, sizeof(DWORD));
1855 ITypeInfo_Release(tinfo);
1856 HeapFree(GetProcessHeap(), 0, args);
1858 xmsg->cbBuffer = buf.curoff;
1859 hres = IRpcChannelBuffer_GetBuffer(rpcchanbuf, xmsg, &This->iid);
1861 ERR("IRpcChannelBuffer_GetBuffer failed with error 0x%08x\n", hres);
1864 memcpy(xmsg->Buffer, buf.base, buf.curoff);
1866 HeapFree(GetProcessHeap(), 0, buf.base);
1868 TRACE("returning\n");
1872 static LPRPCSTUBBUFFER WINAPI
1873 TMStubImpl_IsIIDSupported(LPRPCSTUBBUFFER iface, REFIID riid) {
1874 FIXME("Huh (%s)?\n",debugstr_guid(riid));
1879 TMStubImpl_CountRefs(LPRPCSTUBBUFFER iface) {
1880 TMStubImpl *This = (TMStubImpl *)iface;
1883 return This->ref; /*FIXME? */
1886 static HRESULT WINAPI
1887 TMStubImpl_DebugServerQueryInterface(LPRPCSTUBBUFFER iface, LPVOID *ppv) {
1892 TMStubImpl_DebugServerRelease(LPRPCSTUBBUFFER iface, LPVOID ppv) {
1896 static const IRpcStubBufferVtbl tmstubvtbl = {
1897 TMStubImpl_QueryInterface,
1901 TMStubImpl_Disconnect,
1903 TMStubImpl_IsIIDSupported,
1904 TMStubImpl_CountRefs,
1905 TMStubImpl_DebugServerQueryInterface,
1906 TMStubImpl_DebugServerRelease
1909 static HRESULT WINAPI
1910 PSFacBuf_CreateStub(
1911 LPPSFACTORYBUFFER iface, REFIID riid,IUnknown *pUnkServer,
1912 IRpcStubBuffer** ppStub
1919 TRACE("(%s,%p,%p)\n",debugstr_guid(riid),pUnkServer,ppStub);
1921 hres = _get_typeinfo_for_iid(riid,&tinfo);
1923 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
1927 stub = CoTaskMemAlloc(sizeof(TMStubImpl));
1929 return E_OUTOFMEMORY;
1930 stub->lpvtbl = &tmstubvtbl;
1932 stub->tinfo = tinfo;
1933 stub->dispatch_stub = NULL;
1934 stub->dispatch_derivative = FALSE;
1935 memcpy(&(stub->iid),riid,sizeof(*riid));
1936 hres = IRpcStubBuffer_Connect((LPRPCSTUBBUFFER)stub,pUnkServer);
1937 *ppStub = (LPRPCSTUBBUFFER)stub;
1938 TRACE("IRpcStubBuffer: %p\n", stub);
1940 ERR("Connect to pUnkServer failed?\n");
1942 /* if we derive from IDispatch then defer to its stub for some of its methods */
1943 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
1946 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
1947 stub->dispatch_derivative = TRUE;
1948 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
1954 static const IPSFactoryBufferVtbl psfacbufvtbl = {
1955 PSFacBuf_QueryInterface,
1958 PSFacBuf_CreateProxy,
1962 /* This is the whole PSFactoryBuffer object, just the vtableptr */
1963 static const IPSFactoryBufferVtbl *lppsfac = &psfacbufvtbl;
1965 /***********************************************************************
1966 * TMARSHAL_DllGetClassObject
1968 HRESULT TMARSHAL_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
1970 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)) {
1974 return E_NOINTERFACE;