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 %lx\n",hres);
151 hres = IStream_Write(pStm,buf->base+buf->curoff,xsize,&res);
153 ERR("stream write %lx\n",hres);
157 memset(&seekto,0,sizeof(seekto));
158 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
160 ERR("Failed Seek %lx\n",hres);
164 hres = CoUnmarshalInterface(pStm,riid,(LPVOID*)pUnk);
166 ERR("Unmarshalling interface %s failed with %lx\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 %lx\n",hres);
206 hres = CoMarshalInterface(pStm,riid,pUnk,0,NULL,0);
208 ERR("Marshalling interface %s failed with %lx\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 %lx\n",hres);
226 hres = IStream_Read(pStm,tempbuf,ststg.cbSize.u.LowPart,&res);
228 ERR("Failed Read %lx\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=%lu)\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=%lu)\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);
424 static HRESULT WINAPI
426 LPRPCPROXYBUFFER iface,IRpcChannelBuffer* pRpcChannelBuffer)
428 ICOM_THIS_MULTI(TMProxyImpl, lpvtbl2, iface);
430 TRACE("(%p)\n", pRpcChannelBuffer);
432 EnterCriticalSection(&This->crit);
434 IRpcChannelBuffer_AddRef(pRpcChannelBuffer);
435 This->chanbuf = pRpcChannelBuffer;
437 LeaveCriticalSection(&This->crit);
439 if (This->dispatch_proxy)
440 IRpcProxyBuffer_Connect(This->dispatch_proxy, pRpcChannelBuffer);
446 TMProxyImpl_Disconnect(LPRPCPROXYBUFFER iface)
448 ICOM_THIS_MULTI(TMProxyImpl, lpvtbl2, iface);
452 EnterCriticalSection(&This->crit);
454 IRpcChannelBuffer_Release(This->chanbuf);
455 This->chanbuf = NULL;
457 LeaveCriticalSection(&This->crit);
459 if (This->dispatch_proxy)
460 IRpcProxyBuffer_Disconnect(This->dispatch_proxy);
464 static const IRpcProxyBufferVtbl tmproxyvtable = {
465 TMProxyImpl_QueryInterface,
469 TMProxyImpl_Disconnect
472 /* how much space do we use on stack in DWORD steps. */
477 return 8/sizeof(DWORD);
479 return sizeof(double)/sizeof(DWORD);
481 return sizeof(CY)/sizeof(DWORD);
483 return sizeof(DATE)/sizeof(DWORD);
485 return (sizeof(VARIANT)+3)/sizeof(DWORD);
492 _xsize(TYPEDESC *td) {
497 return sizeof(VARIANT)+3;
500 ARRAYDESC *adesc = td->u.lpadesc;
502 for (i=0;i<adesc->cDims;i++)
503 arrsize *= adesc->rgbounds[i].cElements;
504 return arrsize*_xsize(&adesc->tdescElem);
532 TRACE("(tdesc.vt %s)\n",debugstr_vt(tdesc->vt));
535 case VT_EMPTY: /* nothing. empty variant for instance */
541 if (debugout) TRACE_(olerelay)("%lx%lx",arg[0],arg[1]);
543 hres = xbuf_add(buf,(LPBYTE)arg,8);
553 if (debugout) TRACE_(olerelay)("%lx",*arg);
555 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
560 if (debugout) TRACE_(olerelay)("%04lx",*arg & 0xffff);
562 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
567 if (debugout) TRACE_(olerelay)("%02lx",*arg & 0xff);
569 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
573 if (debugout) TRACE_(olerelay)("&0x%lx",*arg);
575 hres = xbuf_add(buf,(LPBYTE)(DWORD*)*arg,sizeof(DWORD));
576 /* do not dealloc at this time */
580 VARIANT *vt = (VARIANT*)arg;
581 DWORD vttype = V_VT(vt);
583 if (debugout) TRACE_(olerelay)("Vt(%s%s)(",debugstr_vt(vttype),debugstr_vf(vttype));
586 hres = xbuf_add(buf,(LPBYTE)&vttype,sizeof(vttype));
587 if (hres) return hres;
589 /* need to recurse since we need to free the stuff */
590 hres = serialize_param(tinfo,writeit,debugout,dealloc,&tdesc2,(DWORD*)&(V_I4(vt)),buf);
591 if (debugout) TRACE_(olerelay)(")");
594 case VT_BSTR|VT_BYREF: {
595 if (debugout) TRACE_(olerelay)("[byref]'%s'", *(BSTR*)*arg ? relaystr(*((BSTR*)*arg)) : "<bstr NULL>");
597 /* ptr to ptr to magic widestring, basically */
598 BSTR *bstr = (BSTR *) *arg;
601 /* -1 means "null string" which is equivalent to empty string */
603 hres = xbuf_add(buf, (LPBYTE)&len,sizeof(DWORD));
604 if (hres) return hres;
606 len = *((DWORD*)*bstr-1)/sizeof(WCHAR);
607 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
608 if (hres) return hres;
609 hres = xbuf_add(buf,(LPBYTE)*bstr,len * sizeof(WCHAR));
610 if (hres) return hres;
614 if (dealloc && arg) {
615 BSTR *str = *((BSTR **)arg);
624 TRACE_(olerelay)("%s",relaystr((WCHAR*)*arg));
626 TRACE_(olerelay)("<bstr NULL>");
629 BSTR bstr = (BSTR)*arg;
633 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
634 if (hres) return hres;
636 len = *((DWORD*)bstr-1)/sizeof(WCHAR);
637 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
638 if (hres) return hres;
639 hres = xbuf_add(buf,(LPBYTE)bstr,len * sizeof(WCHAR));
640 if (hres) return hres;
645 SysFreeString((BSTR)*arg);
650 BOOL derefhere = TRUE;
652 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
656 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
658 ERR("Could not get typeinfo of hreftype %lx for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
661 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
662 switch (tattr->typekind) {
663 case TKIND_ENUM: /* confirmed */
664 case TKIND_RECORD: /* FIXME: mostly untested */
667 case TKIND_ALIAS: /* FIXME: untested */
668 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
669 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
673 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
677 ITypeInfo_Release(tinfo2);
680 if (debugout) TRACE_(olerelay)("*");
681 /* Write always, so the other side knows when it gets a NULL pointer.
683 cookie = *arg ? 0x42424242 : 0;
684 hres = xbuf_add(buf,(LPBYTE)&cookie,sizeof(cookie));
688 if (debugout) TRACE_(olerelay)("NULL");
691 hres = serialize_param(tinfo,writeit,debugout,dealloc,tdesc->u.lptdesc,(DWORD*)*arg,buf);
692 if (derefhere && dealloc) HeapFree(GetProcessHeap(),0,(LPVOID)*arg);
696 if (debugout) TRACE_(olerelay)("unk(0x%lx)",*arg);
698 hres = _marshal_interface(buf,&IID_IUnknown,(LPUNKNOWN)*arg);
699 if (dealloc && *(IUnknown **)arg)
700 IUnknown_Release((LPUNKNOWN)*arg);
703 if (debugout) TRACE_(olerelay)("idisp(0x%lx)",*arg);
705 hres = _marshal_interface(buf,&IID_IDispatch,(LPUNKNOWN)*arg);
706 if (dealloc && *(IUnknown **)arg)
707 IUnknown_Release((LPUNKNOWN)*arg);
710 if (debugout) TRACE_(olerelay)("<void>");
712 case VT_USERDEFINED: {
716 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
718 ERR("Could not get typeinfo of hreftype %lx for VT_USERDEFINED.\n",tdesc->u.hreftype);
721 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
722 switch (tattr->typekind) {
724 case TKIND_INTERFACE:
726 hres=_marshal_interface(buf,&(tattr->guid),(LPUNKNOWN)arg);
728 IUnknown_Release((LPUNKNOWN)arg);
732 if (debugout) TRACE_(olerelay)("{");
733 for (i=0;i<tattr->cVars;i++) {
738 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
740 ERR("Could not get vardesc of %d\n",i);
743 /* Need them for hack below */
745 memset(names,0,sizeof(names));
746 hres = ITypeInfo_GetNames(tinfo2,vdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames);
747 if (nrofnames > sizeof(names)/sizeof(names[0])) {
748 ERR("Need more names!\n");
750 if (!hres && debugout)
751 TRACE_(olerelay)("%s=",relaystr(names[0]));
753 elem2 = &vdesc->elemdescVar;
754 tdesc2 = &elem2->tdesc;
755 hres = serialize_param(
761 (DWORD*)(((LPBYTE)arg)+vdesc->u.oInst),
764 ITypeInfo_ReleaseVarDesc(tinfo2, vdesc);
767 if (debugout && (i<(tattr->cVars-1)))
768 TRACE_(olerelay)(",");
770 if (debugout) TRACE_(olerelay)("}");
774 return serialize_param(tinfo2,writeit,debugout,dealloc,&tattr->tdescAlias,arg,buf);
777 if (debugout) TRACE_(olerelay)("%lx",*arg);
779 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
782 FIXME("Unhandled typekind %d\n",tattr->typekind);
786 ITypeInfo_Release(tinfo2);
790 ARRAYDESC *adesc = tdesc->u.lpadesc;
793 if (debugout) TRACE_(olerelay)("carr");
794 for (i=0;i<adesc->cDims;i++) {
795 if (debugout) TRACE_(olerelay)("[%ld]",adesc->rgbounds[i].cElements);
796 arrsize *= adesc->rgbounds[i].cElements;
798 if (debugout) TRACE_(olerelay)("(vt %s)",debugstr_vt(adesc->tdescElem.vt));
799 if (debugout) TRACE_(olerelay)("[");
800 for (i=0;i<arrsize;i++) {
801 hres = serialize_param(tinfo, writeit, debugout, dealloc, &adesc->tdescElem, (DWORD*)((LPBYTE)arg+i*_xsize(&adesc->tdescElem)), buf);
804 if (debugout && (i<arrsize-1)) TRACE_(olerelay)(",");
806 if (debugout) TRACE_(olerelay)("]");
812 unsigned long flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
813 unsigned long size = LPSAFEARRAY_UserSize(&flags, buf->curoff, (LPSAFEARRAY *)arg);
814 xbuf_resize(buf, size);
815 LPSAFEARRAY_UserMarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
821 ERR("Unhandled marshal type %d.\n",tdesc->vt);
838 TRACE("vt %s at %p\n",debugstr_vt(tdesc->vt),arg);
843 if (debugout) TRACE_(olerelay)("<empty>");
846 if (debugout) TRACE_(olerelay)("<null>");
849 VARIANT *vt = (VARIANT*)arg;
854 hres = xbuf_get(buf,(LPBYTE)&vttype,sizeof(vttype));
856 FIXME("vt type not read?\n");
859 memset(&tdesc2,0,sizeof(tdesc2));
862 if (debugout) TRACE_(olerelay)("Vt(%s%s)(",debugstr_vt(vttype),debugstr_vf(vttype));
863 hres = deserialize_param(tinfo, readit, debugout, alloc, &tdesc2, (DWORD*)&(V_I4(vt)), buf);
864 TRACE_(olerelay)(")");
875 hres = xbuf_get(buf,(LPBYTE)arg,8);
876 if (hres) ERR("Failed to read integer 8 byte\n");
878 if (debugout) TRACE_(olerelay)("%lx%lx",arg[0],arg[1]);
888 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
889 if (hres) ERR("Failed to read integer 4 byte\n");
891 if (debugout) TRACE_(olerelay)("%lx",*arg);
897 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
898 if (hres) ERR("Failed to read integer 4 byte\n");
901 if (debugout) TRACE_(olerelay)("%04lx",*arg & 0xffff);
907 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
908 if (hres) ERR("Failed to read integer 4 byte\n");
911 if (debugout) TRACE_(olerelay)("%02lx",*arg & 0xff);
916 *arg = (DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
918 hres = xbuf_get(buf,(LPBYTE)*arg,sizeof(DWORD));
919 if (hres) ERR("Failed to read integer 4 byte\n");
921 if (debugout) TRACE_(olerelay)("&0x%lx",*(DWORD*)*arg);
923 case VT_BSTR|VT_BYREF: {
924 BSTR **bstr = (BSTR **)arg;
929 hres = xbuf_get(buf,(LPBYTE)&len,sizeof(DWORD));
931 ERR("failed to read bstr klen\n");
935 *bstr = CoTaskMemAlloc(sizeof(BSTR *));
937 if (debugout) TRACE_(olerelay)("<bstr NULL>");
939 str = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(len+1)*sizeof(WCHAR));
940 hres = xbuf_get(buf,(LPBYTE)str,len*sizeof(WCHAR));
942 ERR("Failed to read BSTR.\n");
945 *bstr = CoTaskMemAlloc(sizeof(BSTR *));
946 **bstr = SysAllocStringLen(str,len);
947 if (debugout) TRACE_(olerelay)("%s",relaystr(str));
948 HeapFree(GetProcessHeap(),0,str);
960 hres = xbuf_get(buf,(LPBYTE)&len,sizeof(DWORD));
962 ERR("failed to read bstr klen\n");
967 if (debugout) TRACE_(olerelay)("<bstr NULL>");
969 str = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(len+1)*sizeof(WCHAR));
970 hres = xbuf_get(buf,(LPBYTE)str,len*sizeof(WCHAR));
972 ERR("Failed to read BSTR.\n");
975 *arg = (DWORD)SysAllocStringLen(str,len);
976 if (debugout) TRACE_(olerelay)("%s",relaystr(str));
977 HeapFree(GetProcessHeap(),0,str);
986 BOOL derefhere = TRUE;
988 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
992 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
994 ERR("Could not get typeinfo of hreftype %lx for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
997 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
998 switch (tattr->typekind) {
999 case TKIND_ENUM: /* confirmed */
1000 case TKIND_RECORD: /* FIXME: mostly untested */
1003 case TKIND_ALIAS: /* FIXME: untested */
1004 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
1005 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
1009 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
1013 ITypeInfo_Release(tinfo2);
1015 /* read it in all cases, we need to know if we have
1016 * NULL pointer or not.
1018 hres = xbuf_get(buf,(LPBYTE)&cookie,sizeof(cookie));
1020 ERR("Failed to load pointer cookie.\n");
1023 if (cookie != 0x42424242) {
1024 /* we read a NULL ptr from the remote side */
1025 if (debugout) TRACE_(olerelay)("NULL");
1029 if (debugout) TRACE_(olerelay)("*");
1031 /* Allocate space for the referenced struct */
1033 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,_xsize(tdesc->u.lptdesc));
1036 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, (LPDWORD)*arg, buf);
1038 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, arg, buf);
1041 /* FIXME: UNKNOWN is unknown ..., but allocate 4 byte for it */
1043 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
1046 hres = _unmarshal_interface(buf,&IID_IUnknown,(LPUNKNOWN*)arg);
1048 TRACE_(olerelay)("unk(%p)",arg);
1053 hres = _unmarshal_interface(buf,&IID_IDispatch,(LPUNKNOWN*)arg);
1055 TRACE_(olerelay)("idisp(%p)",arg);
1058 if (debugout) TRACE_(olerelay)("<void>");
1060 case VT_USERDEFINED: {
1064 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
1066 ERR("Could not get typeinfo of hreftype %lx for VT_USERDEFINED.\n",tdesc->u.hreftype);
1069 hres = ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1071 ERR("Could not get typeattr in VT_USERDEFINED.\n");
1073 switch (tattr->typekind) {
1074 case TKIND_DISPATCH:
1075 case TKIND_INTERFACE:
1077 hres = _unmarshal_interface(buf,&(tattr->guid),(LPUNKNOWN*)arg);
1079 case TKIND_RECORD: {
1083 *arg = (DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,tattr->cbSizeInstance);
1085 if (debugout) TRACE_(olerelay)("{");
1086 for (i=0;i<tattr->cVars;i++) {
1089 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
1091 ERR("Could not get vardesc of %d\n",i);
1094 hres = deserialize_param(
1099 &vdesc->elemdescVar.tdesc,
1100 (DWORD*)(((LPBYTE)*arg)+vdesc->u.oInst),
1103 ITypeInfo2_ReleaseVarDesc(tinfo2, vdesc);
1104 if (debugout && (i<tattr->cVars-1)) TRACE_(olerelay)(",");
1106 if (debugout) TRACE_(olerelay)("}");
1110 return deserialize_param(tinfo2,readit,debugout,alloc,&tattr->tdescAlias,arg,buf);
1113 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
1114 if (hres) ERR("Failed to read enum (4 byte)\n");
1116 if (debugout) TRACE_(olerelay)("%lx",*arg);
1119 ERR("Unhandled typekind %d\n",tattr->typekind);
1125 ERR("failed to stuballoc in TKIND_RECORD.\n");
1126 ITypeInfo_Release(tinfo2);
1130 /* arg is pointing to the start of the array. */
1131 ARRAYDESC *adesc = tdesc->u.lpadesc;
1134 if (adesc->cDims > 1) FIXME("cDims > 1 in VT_CARRAY. Does it work?\n");
1135 for (i=0;i<adesc->cDims;i++)
1136 arrsize *= adesc->rgbounds[i].cElements;
1137 for (i=0;i<arrsize;i++)
1144 (DWORD*)((LPBYTE)(arg)+i*_xsize(&adesc->tdescElem)),
1149 case VT_SAFEARRAY: {
1152 unsigned long flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
1153 unsigned char *buffer;
1154 buffer = LPSAFEARRAY_UserUnmarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
1155 buf->curoff = buffer - buf->base;
1160 ERR("No handler for VT type %d!\n",tdesc->vt);
1166 /* Searches function, also in inherited interfaces */
1169 ITypeInfo *tinfo, int iMethod, ITypeInfo **tactual, const FUNCDESC **fdesc, BSTR *iname, BSTR *fname)
1174 if (fname) *fname = NULL;
1175 if (iname) *iname = NULL;
1178 ITypeInfo_AddRef(*tactual);
1181 hres = ITypeInfoImpl_GetInternalFuncDesc(tinfo, i, fdesc);
1188 hres = ITypeInfo_GetTypeAttr(tinfo, &attr);
1190 ERR("GetTypeAttr failed with %lx\n",hres);
1193 /* Not found, so look in inherited ifaces. */
1194 for (j=0;j<attr->cImplTypes;j++) {
1195 hres = ITypeInfo_GetRefTypeOfImplType(tinfo, j, &href);
1197 ERR("Did not find a reftype for interface offset %d?\n",j);
1200 hres = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
1202 ERR("Did not find a typeinfo for reftype %ld?\n",href);
1205 hres = _get_funcdesc(tinfo2,iMethod,tactual,fdesc,iname,fname);
1206 ITypeInfo_Release(tinfo2);
1207 if (!hres) return S_OK;
1211 if (((*fdesc)->oVft/4) == iMethod) {
1213 ITypeInfo_GetDocumentation(tinfo,(*fdesc)->memid,fname,NULL,NULL,NULL);
1215 ITypeInfo_GetDocumentation(tinfo,-1,iname,NULL,NULL,NULL);
1223 xCall(LPVOID retptr, int method, TMProxyImpl *tpinfo /*, args */)
1225 DWORD *args = ((DWORD*)&tpinfo)+1, *xargs;
1226 const FUNCDESC *fdesc;
1228 int i, relaydeb = TRACE_ON(olerelay);
1235 DWORD remoteresult = 0;
1237 IRpcChannelBuffer *chanbuf;
1239 EnterCriticalSection(&tpinfo->crit);
1241 hres = _get_funcdesc(tpinfo->tinfo,method,&tinfo,&fdesc,&iname,&fname);
1243 ERR("Did not find typeinfo/funcdesc entry for method %d!\n",method);
1244 ITypeInfo_Release(tinfo);
1245 LeaveCriticalSection(&tpinfo->crit);
1249 if (!tpinfo->chanbuf)
1251 WARN("Tried to use disconnected proxy\n");
1252 ITypeInfo_Release(tinfo);
1253 LeaveCriticalSection(&tpinfo->crit);
1254 return RPC_E_DISCONNECTED;
1256 chanbuf = tpinfo->chanbuf;
1257 IRpcChannelBuffer_AddRef(chanbuf);
1259 LeaveCriticalSection(&tpinfo->crit);
1262 TRACE_(olerelay)("->");
1264 TRACE_(olerelay)("%s:",relaystr(iname));
1266 TRACE_(olerelay)("%s(%d)",relaystr(fname),method);
1268 TRACE_(olerelay)("%d",method);
1269 TRACE_(olerelay)("(");
1272 if (iname) SysFreeString(iname);
1273 if (fname) SysFreeString(fname);
1275 memset(&buf,0,sizeof(buf));
1277 /* normal typelib driven serializing */
1279 /* Need them for hack below */
1280 memset(names,0,sizeof(names));
1281 if (ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames))
1283 if (nrofnames > sizeof(names)/sizeof(names[0]))
1284 ERR("Need more names!\n");
1287 for (i=0;i<fdesc->cParams;i++) {
1288 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1290 if (i) TRACE_(olerelay)(",");
1291 if (i+1<nrofnames && names[i+1])
1292 TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1294 /* No need to marshal other data than FIN and any VT_PTR. */
1295 if (!(elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN || !elem->u.paramdesc.wParamFlags) && (elem->tdesc.vt != VT_PTR)) {
1296 xargs+=_argsize(elem->tdesc.vt);
1297 if (relaydeb) TRACE_(olerelay)("[out]");
1300 hres = serialize_param(
1302 elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN || !elem->u.paramdesc.wParamFlags,
1311 ERR("Failed to serialize param, hres %lx\n",hres);
1314 xargs+=_argsize(elem->tdesc.vt);
1316 if (relaydeb) TRACE_(olerelay)(")");
1318 memset(&msg,0,sizeof(msg));
1319 msg.cbBuffer = buf.curoff;
1320 msg.iMethod = method;
1321 hres = IRpcChannelBuffer_GetBuffer(chanbuf,&msg,&(tpinfo->iid));
1323 ERR("RpcChannelBuffer GetBuffer failed, %lx\n",hres);
1326 memcpy(msg.Buffer,buf.base,buf.curoff);
1327 if (relaydeb) TRACE_(olerelay)("\n");
1328 hres = IRpcChannelBuffer_SendReceive(chanbuf,&msg,&status);
1330 ERR("RpcChannelBuffer SendReceive failed, %lx\n",hres);
1334 if (relaydeb) TRACE_(olerelay)(" status = %08lx (",status);
1336 buf.base = HeapReAlloc(GetProcessHeap(),0,buf.base,msg.cbBuffer);
1338 buf.base = HeapAlloc(GetProcessHeap(),0,msg.cbBuffer);
1339 buf.size = msg.cbBuffer;
1340 memcpy(buf.base,msg.Buffer,buf.size);
1343 /* generic deserializer using typelib description */
1346 for (i=0;i<fdesc->cParams;i++) {
1347 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1350 if (i) TRACE_(olerelay)(",");
1351 if (i+1<nrofnames && names[i+1]) TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1353 /* No need to marshal other data than FOUT and any VT_PTR */
1354 if (!(elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT) && (elem->tdesc.vt != VT_PTR)) {
1355 xargs += _argsize(elem->tdesc.vt);
1356 if (relaydeb) TRACE_(olerelay)("[in]");
1359 hres = deserialize_param(
1361 elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT,
1369 ERR("Failed to unmarshall param, hres %lx\n",hres);
1373 xargs += _argsize(elem->tdesc.vt);
1376 hres = xbuf_get(&buf, (LPBYTE)&remoteresult, sizeof(DWORD));
1379 if (relaydeb) TRACE_(olerelay)(") = %08lx\n", remoteresult);
1381 hres = remoteresult;
1384 HeapFree(GetProcessHeap(),0,buf.base);
1385 IRpcChannelBuffer_Release(chanbuf);
1386 ITypeInfo_Release(tinfo);
1387 TRACE("-- 0x%08lx\n", hres);
1391 static HRESULT WINAPI ProxyIUnknown_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
1393 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1395 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1397 if (proxy->outerunknown)
1398 return IUnknown_QueryInterface(proxy->outerunknown, riid, ppv);
1400 FIXME("No interface\n");
1401 return E_NOINTERFACE;
1404 static ULONG WINAPI ProxyIUnknown_AddRef(IUnknown *iface)
1406 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1410 if (proxy->outerunknown)
1411 return IUnknown_AddRef(proxy->outerunknown);
1413 return 2; /* FIXME */
1416 static ULONG WINAPI ProxyIUnknown_Release(IUnknown *iface)
1418 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1422 if (proxy->outerunknown)
1423 return IUnknown_Release(proxy->outerunknown);
1425 return 1; /* FIXME */
1428 static HRESULT WINAPI ProxyIDispatch_GetTypeInfoCount(LPDISPATCH iface, UINT * pctinfo)
1430 TMProxyImpl *This = (TMProxyImpl *)iface;
1432 TRACE("(%p)\n", pctinfo);
1434 return IDispatch_GetTypeInfoCount(This->dispatch, pctinfo);
1437 static HRESULT WINAPI ProxyIDispatch_GetTypeInfo(LPDISPATCH iface, UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo)
1439 TMProxyImpl *This = (TMProxyImpl *)iface;
1441 TRACE("(%d, %lx, %p)\n", iTInfo, lcid, ppTInfo);
1443 return IDispatch_GetTypeInfo(This->dispatch, iTInfo, lcid, ppTInfo);
1446 static HRESULT WINAPI ProxyIDispatch_GetIDsOfNames(LPDISPATCH iface, REFIID riid, LPOLESTR * rgszNames, UINT cNames, LCID lcid, DISPID * rgDispId)
1448 TMProxyImpl *This = (TMProxyImpl *)iface;
1450 TRACE("(%s, %p, %d, 0x%lx, %p)\n", debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);
1452 return IDispatch_GetIDsOfNames(This->dispatch, riid, rgszNames,
1453 cNames, lcid, rgDispId);
1456 static HRESULT WINAPI ProxyIDispatch_Invoke(LPDISPATCH iface, DISPID dispIdMember, REFIID riid, LCID lcid,
1457 WORD wFlags, DISPPARAMS * pDispParams, VARIANT * pVarResult,
1458 EXCEPINFO * pExcepInfo, UINT * puArgErr)
1460 TMProxyImpl *This = (TMProxyImpl *)iface;
1462 TRACE("(%ld, %s, 0x%lx, 0x%x, %p, %p, %p, %p)\n", dispIdMember,
1463 debugstr_guid(riid), lcid, wFlags, pDispParams, pVarResult,
1464 pExcepInfo, puArgErr);
1466 return IDispatch_Invoke(This->dispatch, dispIdMember, riid, lcid,
1467 wFlags, pDispParams, pVarResult, pExcepInfo,
1471 static inline HRESULT get_facbuf_for_iid(REFIID riid, IPSFactoryBuffer **facbuf)
1476 if ((hr = CoGetPSClsid(riid, &clsid)))
1478 return CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL,
1479 &IID_IPSFactoryBuffer, (LPVOID*)facbuf);
1482 static HRESULT WINAPI
1483 PSFacBuf_CreateProxy(
1484 LPPSFACTORYBUFFER iface, IUnknown* pUnkOuter, REFIID riid,
1485 IRpcProxyBuffer **ppProxy, LPVOID *ppv)
1490 const FUNCDESC *fdesc;
1494 TRACE("(...%s...)\n",debugstr_guid(riid));
1495 hres = _get_typeinfo_for_iid(riid,&tinfo);
1497 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
1500 nroffuncs = _nroffuncs(tinfo);
1501 proxy = CoTaskMemAlloc(sizeof(TMProxyImpl));
1502 if (!proxy) return E_OUTOFMEMORY;
1504 assert(sizeof(TMAsmProxy) == 12);
1506 proxy->dispatch = NULL;
1507 proxy->dispatch_proxy = NULL;
1508 proxy->outerunknown = pUnkOuter;
1509 proxy->asmstubs = VirtualAlloc(NULL, sizeof(TMAsmProxy) * nroffuncs, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
1510 if (!proxy->asmstubs) {
1511 ERR("Could not commit pages for proxy thunks\n");
1512 CoTaskMemFree(proxy);
1513 return E_OUTOFMEMORY;
1516 InitializeCriticalSection(&proxy->crit);
1518 proxy->lpvtbl = HeapAlloc(GetProcessHeap(),0,sizeof(LPBYTE)*nroffuncs);
1519 for (i=0;i<nroffuncs;i++) {
1520 TMAsmProxy *xasm = proxy->asmstubs+i;
1524 proxy->lpvtbl[i] = ProxyIUnknown_QueryInterface;
1527 proxy->lpvtbl[i] = ProxyIUnknown_AddRef;
1530 proxy->lpvtbl[i] = ProxyIUnknown_Release;
1534 /* nrofargs without This */
1537 hres = _get_funcdesc(tinfo,i,&tinfo2,&fdesc,NULL,NULL);
1538 ITypeInfo_Release(tinfo2);
1540 ERR("GetFuncDesc %lx should not fail here.\n",hres);
1543 /* some args take more than 4 byte on the stack */
1545 for (j=0;j<fdesc->cParams;j++)
1546 nrofargs += _argsize(fdesc->lprgelemdescParam[j].tdesc.vt);
1549 if (fdesc->callconv != CC_STDCALL) {
1550 ERR("calling convention is not stdcall????\n");
1553 /* popl %eax - return ptr
1560 * arg3 arg2 arg1 <method> <returnptr>
1562 xasm->popleax = 0x58;
1563 xasm->pushlval = 0x6a;
1565 xasm->pushleax = 0x50;
1566 xasm->lcall = 0xe8; /* relative jump */
1567 xasm->xcall = (DWORD)xCall;
1568 xasm->xcall -= (DWORD)&(xasm->lret);
1570 xasm->bytestopop= (nrofargs+2)*4; /* pop args, This, iMethod */
1571 proxy->lpvtbl[i] = xasm;
1574 FIXME("not implemented on non i386\n");
1581 /* if we derive from IDispatch then defer to its proxy for its methods */
1582 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
1585 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
1587 IPSFactoryBuffer *factory_buffer;
1588 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
1591 hres = IPSFactoryBuffer_CreateProxy(factory_buffer, NULL,
1592 &IID_IDispatch, &proxy->dispatch_proxy,
1593 (void **)&proxy->dispatch);
1594 IPSFactoryBuffer_Release(factory_buffer);
1598 proxy->lpvtbl[3] = ProxyIDispatch_GetTypeInfoCount;
1599 proxy->lpvtbl[4] = ProxyIDispatch_GetTypeInfo;
1600 proxy->lpvtbl[5] = ProxyIDispatch_GetIDsOfNames;
1601 proxy->lpvtbl[6] = ProxyIDispatch_Invoke;
1604 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
1607 proxy->lpvtbl2 = &tmproxyvtable;
1608 /* one reference for the proxy */
1610 proxy->tinfo = tinfo;
1611 memcpy(&proxy->iid,riid,sizeof(*riid));
1615 *ppv = (LPVOID)proxy;
1616 *ppProxy = (IRpcProxyBuffer *)&(proxy->lpvtbl2);
1617 IUnknown_AddRef((IUnknown *)*ppv);
1621 TMProxyImpl_Release((IRpcProxyBuffer *)&proxy->lpvtbl2);
1625 typedef struct _TMStubImpl {
1626 const IRpcStubBufferVtbl *lpvtbl;
1632 IRpcStubBuffer *dispatch_stub;
1633 BOOL dispatch_derivative;
1636 static HRESULT WINAPI
1637 TMStubImpl_QueryInterface(LPRPCSTUBBUFFER iface, REFIID riid, LPVOID *ppv)
1639 if (IsEqualIID(riid,&IID_IRpcStubBuffer)||IsEqualIID(riid,&IID_IUnknown)){
1640 *ppv = (LPVOID)iface;
1641 IRpcStubBuffer_AddRef(iface);
1644 FIXME("%s, not supported IID.\n",debugstr_guid(riid));
1645 return E_NOINTERFACE;
1649 TMStubImpl_AddRef(LPRPCSTUBBUFFER iface)
1651 TMStubImpl *This = (TMStubImpl *)iface;
1652 ULONG refCount = InterlockedIncrement(&This->ref);
1654 TRACE("(%p)->(ref before=%lu)\n", This, refCount - 1);
1660 TMStubImpl_Release(LPRPCSTUBBUFFER iface)
1662 TMStubImpl *This = (TMStubImpl *)iface;
1663 ULONG refCount = InterlockedDecrement(&This->ref);
1665 TRACE("(%p)->(ref before=%lu)\n", This, refCount + 1);
1669 IRpcStubBuffer_Disconnect(iface);
1670 ITypeInfo_Release(This->tinfo);
1671 if (This->dispatch_stub)
1672 IRpcStubBuffer_Release(This->dispatch_stub);
1673 CoTaskMemFree(This);
1678 static HRESULT WINAPI
1679 TMStubImpl_Connect(LPRPCSTUBBUFFER iface, LPUNKNOWN pUnkServer)
1681 TMStubImpl *This = (TMStubImpl *)iface;
1683 TRACE("(%p)->(%p)\n", This, pUnkServer);
1685 IUnknown_AddRef(pUnkServer);
1686 This->pUnk = pUnkServer;
1688 if (This->dispatch_stub)
1689 IRpcStubBuffer_Connect(This->dispatch_stub, pUnkServer);
1695 TMStubImpl_Disconnect(LPRPCSTUBBUFFER iface)
1697 TMStubImpl *This = (TMStubImpl *)iface;
1699 TRACE("(%p)->()\n", This);
1703 IUnknown_Release(This->pUnk);
1707 if (This->dispatch_stub)
1708 IRpcStubBuffer_Disconnect(This->dispatch_stub);
1711 static HRESULT WINAPI
1713 LPRPCSTUBBUFFER iface, RPCOLEMESSAGE* xmsg,IRpcChannelBuffer*rpcchanbuf)
1716 const FUNCDESC *fdesc;
1717 TMStubImpl *This = (TMStubImpl *)iface;
1719 DWORD *args, res, *xargs, nrofargs;
1728 if (xmsg->iMethod < 3) {
1729 ERR("IUnknown methods cannot be marshaled by the typelib marshaler\n");
1730 return E_UNEXPECTED;
1733 if (This->dispatch_derivative && xmsg->iMethod < sizeof(IDispatchVtbl)/sizeof(void *))
1735 IPSFactoryBuffer *factory_buffer;
1736 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
1739 hres = IPSFactoryBuffer_CreateStub(factory_buffer, &IID_IDispatch,
1740 This->pUnk, &This->dispatch_stub);
1741 IPSFactoryBuffer_Release(factory_buffer);
1745 return IRpcStubBuffer_Invoke(This->dispatch_stub, xmsg, rpcchanbuf);
1748 memset(&buf,0,sizeof(buf));
1749 buf.size = xmsg->cbBuffer;
1750 buf.base = HeapAlloc(GetProcessHeap(), 0, xmsg->cbBuffer);
1751 memcpy(buf.base, xmsg->Buffer, xmsg->cbBuffer);
1754 hres = _get_funcdesc(This->tinfo,xmsg->iMethod,&tinfo,&fdesc,&iname,NULL);
1756 ERR("GetFuncDesc on method %ld failed with %lx\n",xmsg->iMethod,hres);
1760 if (iname && !lstrcmpW(iname, IDispatchW))
1762 ERR("IDispatch cannot be marshaled by the typelib marshaler\n");
1763 ITypeInfo_Release(tinfo);
1764 return E_UNEXPECTED;
1767 if (iname) SysFreeString (iname);
1769 /* Need them for hack below */
1770 memset(names,0,sizeof(names));
1771 ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames);
1772 if (nrofnames > sizeof(names)/sizeof(names[0])) {
1773 ERR("Need more names!\n");
1776 /*dump_FUNCDESC(fdesc);*/
1778 for (i=0;i<fdesc->cParams;i++)
1779 nrofargs += _argsize(fdesc->lprgelemdescParam[i].tdesc.vt);
1780 args = HeapAlloc(GetProcessHeap(),0,(nrofargs+1)*sizeof(DWORD));
1781 if (!args) return E_OUTOFMEMORY;
1783 /* Allocate all stuff used by call. */
1785 for (i=0;i<fdesc->cParams;i++) {
1786 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1788 hres = deserialize_param(
1790 elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN || !elem->u.paramdesc.wParamFlags,
1797 xargs += _argsize(elem->tdesc.vt);
1799 ERR("Failed to deserialize param %s, hres %lx\n",relaystr(names[i+1]),hres);
1804 args[0] = (DWORD)This->pUnk;
1809 (*((FARPROC**)args[0]))[fdesc->oVft/4],
1817 DWORD dwExceptionCode = GetExceptionCode();
1818 ERR("invoke call failed with exception 0x%08lx (%ld)\n", dwExceptionCode, dwExceptionCode);
1819 if (FAILED(dwExceptionCode))
1820 hres = dwExceptionCode;
1822 hres = HRESULT_FROM_WIN32(dwExceptionCode);
1832 for (i=0;i<fdesc->cParams;i++) {
1833 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1834 hres = serialize_param(
1836 elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT,
1843 xargs += _argsize(elem->tdesc.vt);
1845 ERR("Failed to stuballoc param, hres %lx\n",hres);
1850 hres = xbuf_add (&buf, (LPBYTE)&res, sizeof(DWORD));
1854 ITypeInfo_Release(tinfo);
1855 HeapFree(GetProcessHeap(), 0, args);
1857 xmsg->cbBuffer = buf.curoff;
1858 hres = IRpcChannelBuffer_GetBuffer(rpcchanbuf, xmsg, &This->iid);
1860 ERR("IRpcChannelBuffer_GetBuffer failed with error 0x%08lx\n", hres);
1863 memcpy(xmsg->Buffer, buf.base, buf.curoff);
1865 HeapFree(GetProcessHeap(), 0, buf.base);
1867 TRACE("returning\n");
1871 static LPRPCSTUBBUFFER WINAPI
1872 TMStubImpl_IsIIDSupported(LPRPCSTUBBUFFER iface, REFIID riid) {
1873 FIXME("Huh (%s)?\n",debugstr_guid(riid));
1878 TMStubImpl_CountRefs(LPRPCSTUBBUFFER iface) {
1879 TMStubImpl *This = (TMStubImpl *)iface;
1882 return This->ref; /*FIXME? */
1885 static HRESULT WINAPI
1886 TMStubImpl_DebugServerQueryInterface(LPRPCSTUBBUFFER iface, LPVOID *ppv) {
1891 TMStubImpl_DebugServerRelease(LPRPCSTUBBUFFER iface, LPVOID ppv) {
1895 static const IRpcStubBufferVtbl tmstubvtbl = {
1896 TMStubImpl_QueryInterface,
1900 TMStubImpl_Disconnect,
1902 TMStubImpl_IsIIDSupported,
1903 TMStubImpl_CountRefs,
1904 TMStubImpl_DebugServerQueryInterface,
1905 TMStubImpl_DebugServerRelease
1908 static HRESULT WINAPI
1909 PSFacBuf_CreateStub(
1910 LPPSFACTORYBUFFER iface, REFIID riid,IUnknown *pUnkServer,
1911 IRpcStubBuffer** ppStub
1918 TRACE("(%s,%p,%p)\n",debugstr_guid(riid),pUnkServer,ppStub);
1920 hres = _get_typeinfo_for_iid(riid,&tinfo);
1922 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
1926 stub = CoTaskMemAlloc(sizeof(TMStubImpl));
1928 return E_OUTOFMEMORY;
1929 stub->lpvtbl = &tmstubvtbl;
1931 stub->tinfo = tinfo;
1932 stub->dispatch_stub = NULL;
1933 stub->dispatch_derivative = FALSE;
1934 memcpy(&(stub->iid),riid,sizeof(*riid));
1935 hres = IRpcStubBuffer_Connect((LPRPCSTUBBUFFER)stub,pUnkServer);
1936 *ppStub = (LPRPCSTUBBUFFER)stub;
1937 TRACE("IRpcStubBuffer: %p\n", stub);
1939 ERR("Connect to pUnkServer failed?\n");
1941 /* if we derive from IDispatch then defer to its stub for some of its methods */
1942 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
1945 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
1946 stub->dispatch_derivative = TRUE;
1947 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
1953 static const IPSFactoryBufferVtbl psfacbufvtbl = {
1954 PSFacBuf_QueryInterface,
1957 PSFacBuf_CreateProxy,
1961 /* This is the whole PSFactoryBuffer object, just the vtableptr */
1962 static const IPSFactoryBufferVtbl *lppsfac = &psfacbufvtbl;
1964 /***********************************************************************
1965 * TMARSHAL_DllGetClassObject
1967 HRESULT TMARSHAL_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
1969 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)) {
1973 return E_NOINTERFACE;