4 * Copyright 2002,2005 Marcus Meissner
6 * The olerelay debug channel allows you to see calls marshalled by
7 * the typelib marshaller. It is not a generic COM relaying system.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "wine/port.h"
35 #define NONAMELESSUNION
36 #define NONAMELESSSTRUCT
46 #include "propidl.h" /* for LPSAFEARRAY_User* functions */
49 #include "wine/debug.h"
50 #include "wine/exception.h"
52 static const WCHAR IDispatchW[] = { 'I','D','i','s','p','a','t','c','h',0};
54 WINE_DEFAULT_DEBUG_CHANNEL(ole);
55 WINE_DECLARE_DEBUG_CHANNEL(olerelay);
57 #define ICOM_THIS_MULTI(impl,field,iface) impl* const This=(impl*)((char*)(iface) - offsetof(impl,field))
59 static HRESULT TMarshalDispatchChannel_Create(
60 IRpcChannelBuffer *pDelegateChannel, REFIID tmarshal_riid,
61 IRpcChannelBuffer **ppChannel);
63 typedef struct _marshal_state {
69 /* used in the olerelay code to avoid having the L"" stuff added by debugstr_w */
70 static char *relaystr(WCHAR *in) {
71 char *tmp = (char *)debugstr_w(in);
73 tmp[strlen(tmp)-1] = '\0';
78 xbuf_resize(marshal_state *buf, DWORD newsize)
80 if(buf->size >= newsize)
85 buf->base = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buf->base, newsize);
91 buf->base = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, newsize);
100 xbuf_add(marshal_state *buf, const BYTE *stuff, DWORD size)
104 if(buf->size - buf->curoff < size)
106 hr = xbuf_resize(buf, buf->size + size + 100);
107 if(FAILED(hr)) return hr;
109 memcpy(buf->base+buf->curoff,stuff,size);
115 xbuf_get(marshal_state *buf, LPBYTE stuff, DWORD size) {
116 if (buf->size < buf->curoff+size) return E_FAIL;
117 memcpy(stuff,buf->base+buf->curoff,size);
123 xbuf_skip(marshal_state *buf, DWORD size) {
124 if (buf->size < buf->curoff+size) return E_FAIL;
130 _unmarshal_interface(marshal_state *buf, REFIID riid, LPUNKNOWN *pUnk) {
132 ULARGE_INTEGER newpos;
133 LARGE_INTEGER seekto;
138 TRACE("...%s...\n",debugstr_guid(riid));
141 hres = xbuf_get(buf,(LPBYTE)&xsize,sizeof(xsize));
143 ERR("xbuf_get failed\n");
147 if (xsize == 0) return S_OK;
149 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
151 ERR("Stream create failed %x\n",hres);
155 hres = IStream_Write(pStm,buf->base+buf->curoff,xsize,&res);
157 ERR("stream write %x\n",hres);
161 memset(&seekto,0,sizeof(seekto));
162 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
164 ERR("Failed Seek %x\n",hres);
168 hres = CoUnmarshalInterface(pStm,riid,(LPVOID*)pUnk);
170 ERR("Unmarshalling interface %s failed with %x\n",debugstr_guid(riid),hres);
174 IStream_Release(pStm);
175 return xbuf_skip(buf,xsize);
179 _marshal_interface(marshal_state *buf, REFIID riid, LPUNKNOWN pUnk) {
180 LPBYTE tempbuf = NULL;
181 IStream *pStm = NULL;
183 ULARGE_INTEGER newpos;
184 LARGE_INTEGER seekto;
190 /* this is valid, if for instance we serialize
191 * a VT_DISPATCH with NULL ptr which apparently
192 * can happen. S_OK to make sure we continue
195 WARN("pUnk is NULL\n");
197 return xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
202 TRACE("...%s...\n",debugstr_guid(riid));
204 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
206 ERR("Stream create failed %x\n",hres);
210 hres = CoMarshalInterface(pStm,riid,pUnk,0,NULL,0);
212 ERR("Marshalling interface %s failed with %x\n", debugstr_guid(riid), hres);
216 hres = IStream_Stat(pStm,&ststg,STATFLAG_NONAME);
218 ERR("Stream stat failed\n");
222 tempbuf = HeapAlloc(GetProcessHeap(), 0, ststg.cbSize.u.LowPart);
223 memset(&seekto,0,sizeof(seekto));
224 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
226 ERR("Failed Seek %x\n",hres);
230 hres = IStream_Read(pStm,tempbuf,ststg.cbSize.u.LowPart,&res);
232 ERR("Failed Read %x\n",hres);
236 xsize = ststg.cbSize.u.LowPart;
237 xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
238 hres = xbuf_add(buf,tempbuf,ststg.cbSize.u.LowPart);
240 HeapFree(GetProcessHeap(),0,tempbuf);
241 IStream_Release(pStm);
247 xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
248 if (pStm) IUnknown_Release(pStm);
249 HeapFree(GetProcessHeap(), 0, tempbuf);
253 /********************* OLE Proxy/Stub Factory ********************************/
254 static HRESULT WINAPI
255 PSFacBuf_QueryInterface(LPPSFACTORYBUFFER iface, REFIID iid, LPVOID *ppv) {
256 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)||IsEqualIID(iid,&IID_IUnknown)) {
258 /* No ref counting, static class */
261 FIXME("(%s) unknown IID?\n",debugstr_guid(iid));
262 return E_NOINTERFACE;
265 static ULONG WINAPI PSFacBuf_AddRef(LPPSFACTORYBUFFER iface) { return 2; }
266 static ULONG WINAPI PSFacBuf_Release(LPPSFACTORYBUFFER iface) { return 1; }
269 _get_typeinfo_for_iid(REFIID riid, ITypeInfo**ti) {
272 char tlguid[200],typelibkey[300],interfacekey[300],ver[100];
275 DWORD tlguidlen, verlen, type;
279 sprintf( interfacekey, "Interface\\{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\Typelib",
280 riid->Data1, riid->Data2, riid->Data3,
281 riid->Data4[0], riid->Data4[1], riid->Data4[2], riid->Data4[3],
282 riid->Data4[4], riid->Data4[5], riid->Data4[6], riid->Data4[7]
285 if (RegOpenKeyA(HKEY_CLASSES_ROOT,interfacekey,&ikey)) {
286 ERR("No %s key found.\n",interfacekey);
289 tlguidlen = sizeof(tlguid);
290 if (RegQueryValueExA(ikey,NULL,NULL,&type,(LPBYTE)tlguid,&tlguidlen)) {
291 ERR("Getting typelib guid failed.\n");
295 verlen = sizeof(ver);
296 if (RegQueryValueExA(ikey,"Version",NULL,&type,(LPBYTE)ver,&verlen)) {
297 ERR("Could not get version value?\n");
302 sprintf(typelibkey,"Typelib\\%s\\%s\\0\\win%u",tlguid,ver,(sizeof(void*) == 8) ? 64 : 32);
303 tlfnlen = sizeof(tlfn);
304 if (RegQueryValueA(HKEY_CLASSES_ROOT,typelibkey,tlfn,&tlfnlen)) {
305 ERR("Could not get typelib fn?\n");
308 MultiByteToWideChar(CP_ACP, 0, tlfn, -1, tlfnW, sizeof(tlfnW) / sizeof(tlfnW[0]));
309 hres = LoadTypeLib(tlfnW,&tl);
311 ERR("Failed to load typelib for %s, but it should be there.\n",debugstr_guid(riid));
314 hres = ITypeLib_GetTypeInfoOfGuid(tl,riid,ti);
316 ERR("typelib does not contain info for %s?\n",debugstr_guid(riid));
317 ITypeLib_Release(tl);
320 ITypeLib_Release(tl);
325 * Determine the number of functions including all inherited functions.
326 * Note for non-dual dispinterfaces we simply return the size of IDispatch.
328 static HRESULT num_of_funcs(ITypeInfo *tinfo, unsigned int *num)
335 hres = ITypeInfo_GetTypeAttr(tinfo, &attr);
337 ERR("GetTypeAttr failed with %x\n",hres);
341 if(attr->typekind == TKIND_DISPATCH && (attr->wTypeFlags & TYPEFLAG_FDUAL))
344 hres = ITypeInfo_GetRefTypeOfImplType(tinfo, -1, &href);
347 ERR("Unable to get interface href from dual dispinterface\n");
350 hres = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
353 ERR("Unable to get interface from dual dispinterface\n");
356 hres = num_of_funcs(tinfo2, num);
357 ITypeInfo_Release(tinfo2);
361 *num = attr->cbSizeVft / 4;
365 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
371 #include "pshpack1.h"
373 typedef struct _TMAsmProxy {
388 # warning You need to implement stubless proxies for your architecture
389 typedef struct _TMAsmProxy {
393 typedef struct _TMProxyImpl {
395 const IRpcProxyBufferVtbl *lpvtbl2;
398 TMAsmProxy *asmstubs;
400 IRpcChannelBuffer* chanbuf;
402 CRITICAL_SECTION crit;
403 IUnknown *outerunknown;
405 IRpcProxyBuffer *dispatch_proxy;
408 static HRESULT WINAPI
409 TMProxyImpl_QueryInterface(LPRPCPROXYBUFFER iface, REFIID riid, LPVOID *ppv)
412 if (IsEqualIID(riid,&IID_IUnknown)||IsEqualIID(riid,&IID_IRpcProxyBuffer)) {
414 IRpcProxyBuffer_AddRef(iface);
417 FIXME("no interface for %s\n",debugstr_guid(riid));
418 return E_NOINTERFACE;
422 TMProxyImpl_AddRef(LPRPCPROXYBUFFER iface)
424 ICOM_THIS_MULTI(TMProxyImpl,lpvtbl2,iface);
425 ULONG refCount = InterlockedIncrement(&This->ref);
427 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
433 TMProxyImpl_Release(LPRPCPROXYBUFFER iface)
435 ICOM_THIS_MULTI(TMProxyImpl,lpvtbl2,iface);
436 ULONG refCount = InterlockedDecrement(&This->ref);
438 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
442 if (This->dispatch_proxy) IRpcProxyBuffer_Release(This->dispatch_proxy);
443 This->crit.DebugInfo->Spare[0] = 0;
444 DeleteCriticalSection(&This->crit);
445 if (This->chanbuf) IRpcChannelBuffer_Release(This->chanbuf);
446 VirtualFree(This->asmstubs, 0, MEM_RELEASE);
447 HeapFree(GetProcessHeap(), 0, This->lpvtbl);
448 ITypeInfo_Release(This->tinfo);
454 static HRESULT WINAPI
456 LPRPCPROXYBUFFER iface,IRpcChannelBuffer* pRpcChannelBuffer)
458 ICOM_THIS_MULTI(TMProxyImpl, lpvtbl2, iface);
460 TRACE("(%p)\n", pRpcChannelBuffer);
462 EnterCriticalSection(&This->crit);
464 IRpcChannelBuffer_AddRef(pRpcChannelBuffer);
465 This->chanbuf = pRpcChannelBuffer;
467 LeaveCriticalSection(&This->crit);
469 if (This->dispatch_proxy)
471 IRpcChannelBuffer *pDelegateChannel;
472 HRESULT hr = TMarshalDispatchChannel_Create(pRpcChannelBuffer, &This->iid, &pDelegateChannel);
475 hr = IRpcProxyBuffer_Connect(This->dispatch_proxy, pDelegateChannel);
476 IRpcChannelBuffer_Release(pDelegateChannel);
484 TMProxyImpl_Disconnect(LPRPCPROXYBUFFER iface)
486 ICOM_THIS_MULTI(TMProxyImpl, lpvtbl2, iface);
490 EnterCriticalSection(&This->crit);
492 IRpcChannelBuffer_Release(This->chanbuf);
493 This->chanbuf = NULL;
495 LeaveCriticalSection(&This->crit);
497 if (This->dispatch_proxy)
498 IRpcProxyBuffer_Disconnect(This->dispatch_proxy);
502 static const IRpcProxyBufferVtbl tmproxyvtable = {
503 TMProxyImpl_QueryInterface,
507 TMProxyImpl_Disconnect
510 /* how much space do we use on stack in DWORD steps. */
512 _argsize(TYPEDESC *tdesc, ITypeInfo *tinfo) {
516 return 8/sizeof(DWORD);
518 return sizeof(double)/sizeof(DWORD);
520 return sizeof(CY)/sizeof(DWORD);
522 return sizeof(DATE)/sizeof(DWORD);
524 return (sizeof(DECIMAL)+3)/sizeof(DWORD);
526 return (sizeof(VARIANT)+3)/sizeof(DWORD);
534 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
536 return 0; /* should fail critically in serialize_param */
537 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
538 ret = (tattr->cbSizeInstance+3)/sizeof(DWORD);
539 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
540 ITypeInfo_Release(tinfo2);
548 /* how much space do we use on the heap (in bytes) */
550 _xsize(const TYPEDESC *td, ITypeInfo *tinfo) {
556 /* FIXME: VT_BOOL should return 2? */
558 return sizeof(VARIANT)+3; /* FIXME: why the +3? */
561 const ARRAYDESC *adesc = td->u.lpadesc;
563 for (i=0;i<adesc->cDims;i++)
564 arrsize *= adesc->rgbounds[i].cElements;
565 return arrsize*_xsize(&adesc->tdescElem, tinfo);
584 hres = ITypeInfo_GetRefTypeInfo(tinfo,td->u.hreftype,&tinfo2);
587 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
588 ret = tattr->cbSizeInstance;
589 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
590 ITypeInfo_Release(tinfo2);
611 TRACE("(tdesc.vt %s)\n",debugstr_vt(tdesc->vt));
614 if ((vartype & 0xf000) == VT_ARRAY)
615 vartype = VT_SAFEARRAY;
623 if (debugout) TRACE_(olerelay)("%x%x\n",arg[0],arg[1]);
625 hres = xbuf_add(buf,(LPBYTE)arg,8);
635 if (debugout) TRACE_(olerelay)("%x\n",*arg);
637 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
642 if (debugout) TRACE_(olerelay)("%04x\n",*arg & 0xffff);
644 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
649 if (debugout) TRACE_(olerelay)("%02x\n",*arg & 0xff);
651 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
654 if (debugout) TRACE_(olerelay)("Vt(%s%s)(",debugstr_vt(V_VT((VARIANT *)arg)),debugstr_vf(V_VT((VARIANT *)arg)));
657 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
658 ULONG size = VARIANT_UserSize(&flags, buf->curoff, (VARIANT *)arg);
659 xbuf_resize(buf, size);
660 VARIANT_UserMarshal(&flags, buf->base + buf->curoff, (VARIANT *)arg);
665 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
666 VARIANT_UserFree(&flags, (VARIANT *)arg);
673 TRACE_(olerelay)("%s",relaystr((WCHAR*)*arg));
675 TRACE_(olerelay)("<bstr NULL>");
679 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
680 ULONG size = BSTR_UserSize(&flags, buf->curoff, (BSTR *)arg);
681 xbuf_resize(buf, size);
682 BSTR_UserMarshal(&flags, buf->base + buf->curoff, (BSTR *)arg);
687 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
688 BSTR_UserFree(&flags, (BSTR *)arg);
694 BOOL derefhere = TRUE;
696 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
700 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
702 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
705 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
706 switch (tattr->typekind) {
708 if (tattr->tdescAlias.vt == VT_USERDEFINED)
710 DWORD href = tattr->tdescAlias.u.hreftype;
711 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
712 ITypeInfo_Release(tinfo2);
713 hres = ITypeInfo_GetRefTypeInfo(tinfo,href,&tinfo2);
715 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
718 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
719 derefhere = (tattr->typekind != TKIND_DISPATCH && tattr->typekind != TKIND_INTERFACE);
722 case TKIND_ENUM: /* confirmed */
723 case TKIND_RECORD: /* FIXME: mostly untested */
725 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
726 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
730 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
734 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
735 ITypeInfo_Release(tinfo2);
738 if (debugout) TRACE_(olerelay)("*");
739 /* Write always, so the other side knows when it gets a NULL pointer.
741 cookie = *arg ? 0x42424242 : 0;
742 hres = xbuf_add(buf,(LPBYTE)&cookie,sizeof(cookie));
746 if (debugout) TRACE_(olerelay)("NULL");
749 hres = serialize_param(tinfo,writeit,debugout,dealloc,tdesc->u.lptdesc,(DWORD*)*arg,buf);
750 if (derefhere && dealloc) HeapFree(GetProcessHeap(),0,(LPVOID)*arg);
754 if (debugout) TRACE_(olerelay)("unk(0x%x)",*arg);
756 hres = _marshal_interface(buf,&IID_IUnknown,(LPUNKNOWN)*arg);
757 if (dealloc && *(IUnknown **)arg)
758 IUnknown_Release((LPUNKNOWN)*arg);
761 if (debugout) TRACE_(olerelay)("idisp(0x%x)",*arg);
763 hres = _marshal_interface(buf,&IID_IDispatch,(LPUNKNOWN)*arg);
764 if (dealloc && *(IUnknown **)arg)
765 IUnknown_Release((LPUNKNOWN)*arg);
768 if (debugout) TRACE_(olerelay)("<void>");
770 case VT_USERDEFINED: {
774 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
776 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
779 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
780 switch (tattr->typekind) {
782 case TKIND_INTERFACE:
784 hres=_marshal_interface(buf,&(tattr->guid),(LPUNKNOWN)arg);
786 IUnknown_Release((LPUNKNOWN)arg);
790 if (debugout) TRACE_(olerelay)("{");
791 for (i=0;i<tattr->cVars;i++) {
796 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
798 ERR("Could not get vardesc of %d\n",i);
801 elem2 = &vdesc->elemdescVar;
802 tdesc2 = &elem2->tdesc;
803 hres = serialize_param(
809 (DWORD*)(((LPBYTE)arg)+vdesc->u.oInst),
812 ITypeInfo_ReleaseVarDesc(tinfo2, vdesc);
815 if (debugout && (i<(tattr->cVars-1)))
816 TRACE_(olerelay)(",");
818 if (debugout) TRACE_(olerelay)("}");
822 hres = serialize_param(tinfo2,writeit,debugout,dealloc,&tattr->tdescAlias,arg,buf);
826 if (debugout) TRACE_(olerelay)("%x",*arg);
828 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
831 FIXME("Unhandled typekind %d\n",tattr->typekind);
835 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
836 ITypeInfo_Release(tinfo2);
840 ARRAYDESC *adesc = tdesc->u.lpadesc;
843 if (debugout) TRACE_(olerelay)("carr");
844 for (i=0;i<adesc->cDims;i++) {
845 if (debugout) TRACE_(olerelay)("[%d]",adesc->rgbounds[i].cElements);
846 arrsize *= adesc->rgbounds[i].cElements;
848 if (debugout) TRACE_(olerelay)("(vt %s)",debugstr_vt(adesc->tdescElem.vt));
849 if (debugout) TRACE_(olerelay)("[");
850 for (i=0;i<arrsize;i++) {
851 hres = serialize_param(tinfo, writeit, debugout, dealloc, &adesc->tdescElem, (DWORD*)((LPBYTE)(*arg)+i*_xsize(&adesc->tdescElem, tinfo)), buf);
854 if (debugout && (i<arrsize-1)) TRACE_(olerelay)(",");
856 if (debugout) TRACE_(olerelay)("]");
858 HeapFree(GetProcessHeap(), 0, *(void **)arg);
864 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
865 ULONG size = LPSAFEARRAY_UserSize(&flags, buf->curoff, (LPSAFEARRAY *)arg);
866 xbuf_resize(buf, size);
867 LPSAFEARRAY_UserMarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
872 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
873 LPSAFEARRAY_UserFree(&flags, (LPSAFEARRAY *)arg);
878 ERR("Unhandled marshal type %d.\n",tdesc->vt);
896 TRACE("vt %s at %p\n",debugstr_vt(tdesc->vt),arg);
899 if ((vartype & 0xf000) == VT_ARRAY)
900 vartype = VT_SAFEARRAY;
907 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
908 unsigned char *buffer;
909 buffer = VARIANT_UserUnmarshal(&flags, buf->base + buf->curoff, (VARIANT *)arg);
910 buf->curoff = buffer - buf->base;
919 hres = xbuf_get(buf,(LPBYTE)arg,8);
920 if (hres) ERR("Failed to read integer 8 byte\n");
922 if (debugout) TRACE_(olerelay)("%x%x",arg[0],arg[1]);
932 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
933 if (hres) ERR("Failed to read integer 4 byte\n");
935 if (debugout) TRACE_(olerelay)("%x",*arg);
941 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
942 if (hres) ERR("Failed to read integer 4 byte\n");
945 if (debugout) TRACE_(olerelay)("%04x",*arg & 0xffff);
951 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
952 if (hres) ERR("Failed to read integer 4 byte\n");
955 if (debugout) TRACE_(olerelay)("%02x",*arg & 0xff);
960 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
961 unsigned char *buffer;
962 buffer = BSTR_UserUnmarshal(&flags, buf->base + buf->curoff, (BSTR *)arg);
963 buf->curoff = buffer - buf->base;
964 if (debugout) TRACE_(olerelay)("%s",debugstr_w(*(BSTR *)arg));
970 BOOL derefhere = TRUE;
972 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
976 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
978 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
981 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
982 switch (tattr->typekind) {
984 if (tattr->tdescAlias.vt == VT_USERDEFINED)
986 DWORD href = tattr->tdescAlias.u.hreftype;
987 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
988 ITypeInfo_Release(tinfo2);
989 hres = ITypeInfo_GetRefTypeInfo(tinfo,href,&tinfo2);
991 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
994 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
995 derefhere = (tattr->typekind != TKIND_DISPATCH && tattr->typekind != TKIND_INTERFACE);
998 case TKIND_ENUM: /* confirmed */
999 case TKIND_RECORD: /* FIXME: mostly 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_ReleaseTypeAttr(tinfo2, tattr);
1011 ITypeInfo_Release(tinfo2);
1013 /* read it in all cases, we need to know if we have
1014 * NULL pointer or not.
1016 hres = xbuf_get(buf,(LPBYTE)&cookie,sizeof(cookie));
1018 ERR("Failed to load pointer cookie.\n");
1021 if (cookie != 0x42424242) {
1022 /* we read a NULL ptr from the remote side */
1023 if (debugout) TRACE_(olerelay)("NULL");
1027 if (debugout) TRACE_(olerelay)("*");
1029 /* Allocate space for the referenced struct */
1031 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,_xsize(tdesc->u.lptdesc, tinfo));
1034 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, (LPDWORD)*arg, buf);
1036 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, arg, buf);
1039 /* FIXME: UNKNOWN is unknown ..., but allocate 4 byte for it */
1041 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
1044 hres = _unmarshal_interface(buf,&IID_IUnknown,(LPUNKNOWN*)arg);
1046 TRACE_(olerelay)("unk(%p)",arg);
1051 hres = _unmarshal_interface(buf,&IID_IDispatch,(LPUNKNOWN*)arg);
1053 TRACE_(olerelay)("idisp(%p)",arg);
1056 if (debugout) TRACE_(olerelay)("<void>");
1058 case VT_USERDEFINED: {
1062 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
1064 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
1067 hres = ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1069 ERR("Could not get typeattr in VT_USERDEFINED.\n");
1071 switch (tattr->typekind) {
1072 case TKIND_DISPATCH:
1073 case TKIND_INTERFACE:
1075 hres = _unmarshal_interface(buf,&(tattr->guid),(LPUNKNOWN*)arg);
1077 case TKIND_RECORD: {
1080 if (debugout) TRACE_(olerelay)("{");
1081 for (i=0;i<tattr->cVars;i++) {
1084 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
1086 ERR("Could not get vardesc of %d\n",i);
1087 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1088 ITypeInfo_Release(tinfo2);
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 hres = deserialize_param(tinfo2,readit,debugout,alloc,&tattr->tdescAlias,arg,buf);
1111 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
1112 if (hres) ERR("Failed to read enum (4 byte)\n");
1114 if (debugout) TRACE_(olerelay)("%x",*arg);
1117 ERR("Unhandled typekind %d\n",tattr->typekind);
1121 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1124 ERR("failed to stuballoc in TKIND_RECORD.\n");
1125 ITypeInfo_Release(tinfo2);
1129 /* arg is pointing to the start of the array. */
1130 ARRAYDESC *adesc = tdesc->u.lpadesc;
1133 if (adesc->cDims > 1) FIXME("cDims > 1 in VT_CARRAY. Does it work?\n");
1134 for (i=0;i<adesc->cDims;i++)
1135 arrsize *= adesc->rgbounds[i].cElements;
1136 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,_xsize(tdesc->u.lptdesc, tinfo) * arrsize);
1137 for (i=0;i<arrsize;i++)
1144 (DWORD*)((LPBYTE)(*arg)+i*_xsize(&adesc->tdescElem, tinfo)),
1149 case VT_SAFEARRAY: {
1152 ULONG 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 /* Retrieves a function's funcdesc, searching back into inherited interfaces. */
1167 static HRESULT get_funcdesc(ITypeInfo *tinfo, int iMethod, ITypeInfo **tactual, const FUNCDESC **fdesc,
1168 BSTR *iname, BSTR *fname, UINT *num)
1172 UINT inherited_funcs = 0;
1175 if (fname) *fname = NULL;
1176 if (iname) *iname = NULL;
1180 hr = ITypeInfo_GetTypeAttr(tinfo, &attr);
1183 ERR("GetTypeAttr failed with %x\n",hr);
1187 if(attr->typekind == TKIND_DISPATCH)
1189 if(attr->wTypeFlags & TYPEFLAG_FDUAL)
1194 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, -1, &href);
1197 ERR("Cannot get interface href from dual dispinterface\n");
1198 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1201 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
1204 ERR("Cannot get interface from dual dispinterface\n");
1205 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1208 hr = get_funcdesc(tinfo2, iMethod, tactual, fdesc, iname, fname, num);
1209 ITypeInfo_Release(tinfo2);
1210 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1213 ERR("Shouldn't be called with a non-dual dispinterface\n");
1217 impl_types = attr->cImplTypes;
1218 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1220 for (i = 0; i < impl_types; i++)
1223 ITypeInfo *pSubTypeInfo;
1226 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, i, &href);
1227 if (FAILED(hr)) return hr;
1228 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &pSubTypeInfo);
1229 if (FAILED(hr)) return hr;
1231 hr = get_funcdesc(pSubTypeInfo, iMethod, tactual, fdesc, iname, fname, &sub_funcs);
1232 inherited_funcs += sub_funcs;
1233 ITypeInfo_Release(pSubTypeInfo);
1234 if(SUCCEEDED(hr)) return hr;
1236 if(iMethod < inherited_funcs)
1238 ERR("shouldn't be here\n");
1239 return E_INVALIDARG;
1242 for(i = inherited_funcs; i <= iMethod; i++)
1244 hr = ITypeInfoImpl_GetInternalFuncDesc(tinfo, i - inherited_funcs, fdesc);
1252 /* found it. We don't care about num so zero it */
1255 ITypeInfo_AddRef(*tactual);
1256 if (fname) ITypeInfo_GetDocumentation(tinfo,(*fdesc)->memid,fname,NULL,NULL,NULL);
1257 if (iname) ITypeInfo_GetDocumentation(tinfo,-1,iname,NULL,NULL,NULL);
1261 static inline BOOL is_in_elem(const ELEMDESC *elem)
1263 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN || !elem->u.paramdesc.wParamFlags);
1266 static inline BOOL is_out_elem(const ELEMDESC *elem)
1268 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT || !elem->u.paramdesc.wParamFlags);
1272 xCall(LPVOID retptr, int method, TMProxyImpl *tpinfo /*, args */)
1274 DWORD *args = ((DWORD*)&tpinfo)+1, *xargs;
1275 const FUNCDESC *fdesc;
1277 int i, relaydeb = TRACE_ON(olerelay);
1284 DWORD remoteresult = 0;
1286 IRpcChannelBuffer *chanbuf;
1288 EnterCriticalSection(&tpinfo->crit);
1290 hres = get_funcdesc(tpinfo->tinfo,method,&tinfo,&fdesc,&iname,&fname,NULL);
1292 ERR("Did not find typeinfo/funcdesc entry for method %d!\n",method);
1293 LeaveCriticalSection(&tpinfo->crit);
1297 if (!tpinfo->chanbuf)
1299 WARN("Tried to use disconnected proxy\n");
1300 ITypeInfo_Release(tinfo);
1301 LeaveCriticalSection(&tpinfo->crit);
1302 return RPC_E_DISCONNECTED;
1304 chanbuf = tpinfo->chanbuf;
1305 IRpcChannelBuffer_AddRef(chanbuf);
1307 LeaveCriticalSection(&tpinfo->crit);
1310 TRACE_(olerelay)("->");
1312 TRACE_(olerelay)("%s:",relaystr(iname));
1314 TRACE_(olerelay)("%s(%d)",relaystr(fname),method);
1316 TRACE_(olerelay)("%d",method);
1317 TRACE_(olerelay)("(");
1320 SysFreeString(iname);
1321 SysFreeString(fname);
1323 memset(&buf,0,sizeof(buf));
1325 /* normal typelib driven serializing */
1327 /* Need them for hack below */
1328 memset(names,0,sizeof(names));
1329 if (ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames))
1331 if (nrofnames > sizeof(names)/sizeof(names[0]))
1332 ERR("Need more names!\n");
1335 for (i=0;i<fdesc->cParams;i++) {
1336 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1338 if (i) TRACE_(olerelay)(",");
1339 if (i+1<nrofnames && names[i+1])
1340 TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1342 /* No need to marshal other data than FIN and any VT_PTR. */
1343 if (!is_in_elem(elem) && (elem->tdesc.vt != VT_PTR)) {
1344 xargs+=_argsize(&elem->tdesc, tinfo);
1345 if (relaydeb) TRACE_(olerelay)("[out]");
1348 hres = serialize_param(
1359 ERR("Failed to serialize param, hres %x\n",hres);
1362 xargs+=_argsize(&elem->tdesc, tinfo);
1364 if (relaydeb) TRACE_(olerelay)(")");
1366 memset(&msg,0,sizeof(msg));
1367 msg.cbBuffer = buf.curoff;
1368 msg.iMethod = method;
1369 hres = IRpcChannelBuffer_GetBuffer(chanbuf,&msg,&(tpinfo->iid));
1371 ERR("RpcChannelBuffer GetBuffer failed, %x\n",hres);
1374 memcpy(msg.Buffer,buf.base,buf.curoff);
1375 if (relaydeb) TRACE_(olerelay)("\n");
1376 hres = IRpcChannelBuffer_SendReceive(chanbuf,&msg,&status);
1378 ERR("RpcChannelBuffer SendReceive failed, %x\n",hres);
1382 if (relaydeb) TRACE_(olerelay)(" status = %08x (",status);
1384 buf.base = HeapReAlloc(GetProcessHeap(),0,buf.base,msg.cbBuffer);
1386 buf.base = HeapAlloc(GetProcessHeap(),0,msg.cbBuffer);
1387 buf.size = msg.cbBuffer;
1388 memcpy(buf.base,msg.Buffer,buf.size);
1391 /* generic deserializer using typelib description */
1394 for (i=0;i<fdesc->cParams;i++) {
1395 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1398 if (i) TRACE_(olerelay)(",");
1399 if (i+1<nrofnames && names[i+1]) TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1401 /* No need to marshal other data than FOUT and any VT_PTR */
1402 if (!is_out_elem(elem) && (elem->tdesc.vt != VT_PTR)) {
1403 xargs += _argsize(&elem->tdesc, tinfo);
1404 if (relaydeb) TRACE_(olerelay)("[in]");
1407 hres = deserialize_param(
1417 ERR("Failed to unmarshall param, hres %x\n",hres);
1421 xargs += _argsize(&elem->tdesc, tinfo);
1424 hres = xbuf_get(&buf, (LPBYTE)&remoteresult, sizeof(DWORD));
1427 if (relaydeb) TRACE_(olerelay)(") = %08x\n", remoteresult);
1429 hres = remoteresult;
1432 IRpcChannelBuffer_FreeBuffer(chanbuf,&msg);
1433 for (i = 0; i < nrofnames; i++)
1434 SysFreeString(names[i]);
1435 HeapFree(GetProcessHeap(),0,buf.base);
1436 IRpcChannelBuffer_Release(chanbuf);
1437 ITypeInfo_Release(tinfo);
1438 TRACE("-- 0x%08x\n", hres);
1442 static HRESULT WINAPI ProxyIUnknown_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
1444 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1446 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1448 if (proxy->outerunknown)
1449 return IUnknown_QueryInterface(proxy->outerunknown, riid, ppv);
1451 FIXME("No interface\n");
1452 return E_NOINTERFACE;
1455 static ULONG WINAPI ProxyIUnknown_AddRef(IUnknown *iface)
1457 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1461 if (proxy->outerunknown)
1462 return IUnknown_AddRef(proxy->outerunknown);
1464 return 2; /* FIXME */
1467 static ULONG WINAPI ProxyIUnknown_Release(IUnknown *iface)
1469 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1473 if (proxy->outerunknown)
1474 return IUnknown_Release(proxy->outerunknown);
1476 return 1; /* FIXME */
1479 static HRESULT WINAPI ProxyIDispatch_GetTypeInfoCount(LPDISPATCH iface, UINT * pctinfo)
1481 TMProxyImpl *This = (TMProxyImpl *)iface;
1483 TRACE("(%p)\n", pctinfo);
1485 return IDispatch_GetTypeInfoCount(This->dispatch, pctinfo);
1488 static HRESULT WINAPI ProxyIDispatch_GetTypeInfo(LPDISPATCH iface, UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo)
1490 TMProxyImpl *This = (TMProxyImpl *)iface;
1492 TRACE("(%d, %x, %p)\n", iTInfo, lcid, ppTInfo);
1494 return IDispatch_GetTypeInfo(This->dispatch, iTInfo, lcid, ppTInfo);
1497 static HRESULT WINAPI ProxyIDispatch_GetIDsOfNames(LPDISPATCH iface, REFIID riid, LPOLESTR * rgszNames, UINT cNames, LCID lcid, DISPID * rgDispId)
1499 TMProxyImpl *This = (TMProxyImpl *)iface;
1501 TRACE("(%s, %p, %d, 0x%x, %p)\n", debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);
1503 return IDispatch_GetIDsOfNames(This->dispatch, riid, rgszNames,
1504 cNames, lcid, rgDispId);
1507 static HRESULT WINAPI ProxyIDispatch_Invoke(LPDISPATCH iface, DISPID dispIdMember, REFIID riid, LCID lcid,
1508 WORD wFlags, DISPPARAMS * pDispParams, VARIANT * pVarResult,
1509 EXCEPINFO * pExcepInfo, UINT * puArgErr)
1511 TMProxyImpl *This = (TMProxyImpl *)iface;
1513 TRACE("(%d, %s, 0x%x, 0x%x, %p, %p, %p, %p)\n", dispIdMember,
1514 debugstr_guid(riid), lcid, wFlags, pDispParams, pVarResult,
1515 pExcepInfo, puArgErr);
1517 return IDispatch_Invoke(This->dispatch, dispIdMember, riid, lcid,
1518 wFlags, pDispParams, pVarResult, pExcepInfo,
1524 const IRpcChannelBufferVtbl *lpVtbl;
1526 /* the IDispatch-derived interface we are handling */
1528 IRpcChannelBuffer *pDelegateChannel;
1529 } TMarshalDispatchChannel;
1531 static HRESULT WINAPI TMarshalDispatchChannel_QueryInterface(LPRPCCHANNELBUFFER iface, REFIID riid, LPVOID *ppv)
1534 if (IsEqualIID(riid,&IID_IRpcChannelBuffer) || IsEqualIID(riid,&IID_IUnknown))
1537 IUnknown_AddRef(iface);
1540 return E_NOINTERFACE;
1543 static ULONG WINAPI TMarshalDispatchChannel_AddRef(LPRPCCHANNELBUFFER iface)
1545 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1546 return InterlockedIncrement(&This->refs);
1549 static ULONG WINAPI TMarshalDispatchChannel_Release(LPRPCCHANNELBUFFER iface)
1551 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1554 ref = InterlockedDecrement(&This->refs);
1558 IRpcChannelBuffer_Release(This->pDelegateChannel);
1559 HeapFree(GetProcessHeap(), 0, This);
1563 static HRESULT WINAPI TMarshalDispatchChannel_GetBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg, REFIID riid)
1565 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1566 TRACE("(%p, %s)\n", olemsg, debugstr_guid(riid));
1567 /* Note: we are pretending to invoke a method on the interface identified
1568 * by tmarshal_iid so that we can re-use the IDispatch proxy/stub code
1569 * without the RPC runtime getting confused by not exporting an IDispatch interface */
1570 return IRpcChannelBuffer_GetBuffer(This->pDelegateChannel, olemsg, &This->tmarshal_iid);
1573 static HRESULT WINAPI TMarshalDispatchChannel_SendReceive(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE *olemsg, ULONG *pstatus)
1575 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1576 TRACE("(%p, %p)\n", olemsg, pstatus);
1577 return IRpcChannelBuffer_SendReceive(This->pDelegateChannel, olemsg, pstatus);
1580 static HRESULT WINAPI TMarshalDispatchChannel_FreeBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg)
1582 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1583 TRACE("(%p)\n", olemsg);
1584 return IRpcChannelBuffer_FreeBuffer(This->pDelegateChannel, olemsg);
1587 static HRESULT WINAPI TMarshalDispatchChannel_GetDestCtx(LPRPCCHANNELBUFFER iface, DWORD* pdwDestContext, void** ppvDestContext)
1589 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1590 TRACE("(%p,%p)\n", pdwDestContext, ppvDestContext);
1591 return IRpcChannelBuffer_GetDestCtx(This->pDelegateChannel, pdwDestContext, ppvDestContext);
1594 static HRESULT WINAPI TMarshalDispatchChannel_IsConnected(LPRPCCHANNELBUFFER iface)
1596 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1598 return IRpcChannelBuffer_IsConnected(This->pDelegateChannel);
1601 static const IRpcChannelBufferVtbl TMarshalDispatchChannelVtbl =
1603 TMarshalDispatchChannel_QueryInterface,
1604 TMarshalDispatchChannel_AddRef,
1605 TMarshalDispatchChannel_Release,
1606 TMarshalDispatchChannel_GetBuffer,
1607 TMarshalDispatchChannel_SendReceive,
1608 TMarshalDispatchChannel_FreeBuffer,
1609 TMarshalDispatchChannel_GetDestCtx,
1610 TMarshalDispatchChannel_IsConnected
1613 static HRESULT TMarshalDispatchChannel_Create(
1614 IRpcChannelBuffer *pDelegateChannel, REFIID tmarshal_riid,
1615 IRpcChannelBuffer **ppChannel)
1617 TMarshalDispatchChannel *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1619 return E_OUTOFMEMORY;
1621 This->lpVtbl = &TMarshalDispatchChannelVtbl;
1623 IRpcChannelBuffer_AddRef(pDelegateChannel);
1624 This->pDelegateChannel = pDelegateChannel;
1625 This->tmarshal_iid = *tmarshal_riid;
1627 *ppChannel = (IRpcChannelBuffer *)&This->lpVtbl;
1632 static inline HRESULT get_facbuf_for_iid(REFIID riid, IPSFactoryBuffer **facbuf)
1637 if ((hr = CoGetPSClsid(riid, &clsid)))
1639 return CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL,
1640 &IID_IPSFactoryBuffer, (LPVOID*)facbuf);
1643 static HRESULT init_proxy_entry_point(TMProxyImpl *proxy, unsigned int num)
1646 /* nrofargs without This */
1649 TMAsmProxy *xasm = proxy->asmstubs + num;
1651 const FUNCDESC *fdesc;
1653 hres = get_funcdesc(proxy->tinfo, num, &tinfo2, &fdesc, NULL, NULL, NULL);
1655 ERR("GetFuncDesc %x should not fail here.\n",hres);
1658 ITypeInfo_Release(tinfo2);
1659 /* some args take more than 4 byte on the stack */
1661 for (j=0;j<fdesc->cParams;j++)
1662 nrofargs += _argsize(&fdesc->lprgelemdescParam[j].tdesc, proxy->tinfo);
1665 if (fdesc->callconv != CC_STDCALL) {
1666 ERR("calling convention is not stdcall????\n");
1669 /* popl %eax - return ptr
1676 * arg3 arg2 arg1 <method> <returnptr>
1678 xasm->popleax = 0x58;
1679 xasm->pushlval = 0x68;
1681 xasm->pushleax = 0x50;
1682 xasm->lcall = 0xe8; /* relative jump */
1683 xasm->xcall = (DWORD)xCall;
1684 xasm->xcall -= (DWORD)&(xasm->lret);
1686 xasm->bytestopop = (nrofargs+2)*4; /* pop args, This, iMethod */
1688 proxy->lpvtbl[num] = xasm;
1690 FIXME("not implemented on non i386\n");
1696 static HRESULT WINAPI
1697 PSFacBuf_CreateProxy(
1698 LPPSFACTORYBUFFER iface, IUnknown* pUnkOuter, REFIID riid,
1699 IRpcProxyBuffer **ppProxy, LPVOID *ppv)
1703 unsigned int i, nroffuncs;
1706 BOOL defer_to_dispatch = FALSE;
1708 TRACE("(...%s...)\n",debugstr_guid(riid));
1709 hres = _get_typeinfo_for_iid(riid,&tinfo);
1711 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
1715 hres = num_of_funcs(tinfo, &nroffuncs);
1717 ERR("Cannot get number of functions for typeinfo %s\n",debugstr_guid(riid));
1718 ITypeInfo_Release(tinfo);
1722 proxy = CoTaskMemAlloc(sizeof(TMProxyImpl));
1723 if (!proxy) return E_OUTOFMEMORY;
1725 assert(sizeof(TMAsmProxy) == 16);
1727 proxy->dispatch = NULL;
1728 proxy->dispatch_proxy = NULL;
1729 proxy->outerunknown = pUnkOuter;
1730 proxy->asmstubs = VirtualAlloc(NULL, sizeof(TMAsmProxy) * nroffuncs, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
1731 if (!proxy->asmstubs) {
1732 ERR("Could not commit pages for proxy thunks\n");
1733 CoTaskMemFree(proxy);
1734 return E_OUTOFMEMORY;
1736 proxy->lpvtbl2 = &tmproxyvtable;
1737 /* one reference for the proxy */
1739 proxy->tinfo = tinfo;
1743 InitializeCriticalSection(&proxy->crit);
1744 proxy->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TMProxyImpl.crit");
1746 proxy->lpvtbl = HeapAlloc(GetProcessHeap(),0,sizeof(LPBYTE)*nroffuncs);
1748 /* if we derive from IDispatch then defer to its proxy for its methods */
1749 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
1752 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
1754 IPSFactoryBuffer *factory_buffer;
1755 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
1758 hres = IPSFactoryBuffer_CreateProxy(factory_buffer, NULL,
1759 &IID_IDispatch, &proxy->dispatch_proxy,
1760 (void **)&proxy->dispatch);
1761 IPSFactoryBuffer_Release(factory_buffer);
1763 if ((hres == S_OK) && (nroffuncs < 7))
1765 ERR("nroffuncs calculated incorrectly (%d)\n", nroffuncs);
1766 hres = E_UNEXPECTED;
1770 defer_to_dispatch = TRUE;
1773 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
1776 for (i=0;i<nroffuncs;i++) {
1779 proxy->lpvtbl[i] = ProxyIUnknown_QueryInterface;
1782 proxy->lpvtbl[i] = ProxyIUnknown_AddRef;
1785 proxy->lpvtbl[i] = ProxyIUnknown_Release;
1788 if(!defer_to_dispatch)
1790 hres = init_proxy_entry_point(proxy, i);
1791 if(FAILED(hres)) return hres;
1793 else proxy->lpvtbl[3] = ProxyIDispatch_GetTypeInfoCount;
1796 if(!defer_to_dispatch)
1798 hres = init_proxy_entry_point(proxy, i);
1799 if(FAILED(hres)) return hres;
1801 else proxy->lpvtbl[4] = ProxyIDispatch_GetTypeInfo;
1804 if(!defer_to_dispatch)
1806 hres = init_proxy_entry_point(proxy, i);
1807 if(FAILED(hres)) return hres;
1809 else proxy->lpvtbl[5] = ProxyIDispatch_GetIDsOfNames;
1812 if(!defer_to_dispatch)
1814 hres = init_proxy_entry_point(proxy, i);
1815 if(FAILED(hres)) return hres;
1817 else proxy->lpvtbl[6] = ProxyIDispatch_Invoke;
1820 hres = init_proxy_entry_point(proxy, i);
1821 if(FAILED(hres)) return hres;
1828 *ppProxy = (IRpcProxyBuffer *)&(proxy->lpvtbl2);
1829 IUnknown_AddRef((IUnknown *)*ppv);
1833 TMProxyImpl_Release((IRpcProxyBuffer *)&proxy->lpvtbl2);
1837 typedef struct _TMStubImpl {
1838 const IRpcStubBufferVtbl *lpvtbl;
1844 IRpcStubBuffer *dispatch_stub;
1845 BOOL dispatch_derivative;
1848 static HRESULT WINAPI
1849 TMStubImpl_QueryInterface(LPRPCSTUBBUFFER iface, REFIID riid, LPVOID *ppv)
1851 if (IsEqualIID(riid,&IID_IRpcStubBuffer)||IsEqualIID(riid,&IID_IUnknown)){
1853 IRpcStubBuffer_AddRef(iface);
1856 FIXME("%s, not supported IID.\n",debugstr_guid(riid));
1857 return E_NOINTERFACE;
1861 TMStubImpl_AddRef(LPRPCSTUBBUFFER iface)
1863 TMStubImpl *This = (TMStubImpl *)iface;
1864 ULONG refCount = InterlockedIncrement(&This->ref);
1866 TRACE("(%p)->(ref before=%u)\n", This, refCount - 1);
1872 TMStubImpl_Release(LPRPCSTUBBUFFER iface)
1874 TMStubImpl *This = (TMStubImpl *)iface;
1875 ULONG refCount = InterlockedDecrement(&This->ref);
1877 TRACE("(%p)->(ref before=%u)\n", This, refCount + 1);
1881 IRpcStubBuffer_Disconnect(iface);
1882 ITypeInfo_Release(This->tinfo);
1883 if (This->dispatch_stub)
1884 IRpcStubBuffer_Release(This->dispatch_stub);
1885 CoTaskMemFree(This);
1890 static HRESULT WINAPI
1891 TMStubImpl_Connect(LPRPCSTUBBUFFER iface, LPUNKNOWN pUnkServer)
1893 TMStubImpl *This = (TMStubImpl *)iface;
1895 TRACE("(%p)->(%p)\n", This, pUnkServer);
1897 IUnknown_AddRef(pUnkServer);
1898 This->pUnk = pUnkServer;
1900 if (This->dispatch_stub)
1901 IRpcStubBuffer_Connect(This->dispatch_stub, pUnkServer);
1907 TMStubImpl_Disconnect(LPRPCSTUBBUFFER iface)
1909 TMStubImpl *This = (TMStubImpl *)iface;
1911 TRACE("(%p)->()\n", This);
1915 IUnknown_Release(This->pUnk);
1919 if (This->dispatch_stub)
1920 IRpcStubBuffer_Disconnect(This->dispatch_stub);
1923 static HRESULT WINAPI
1925 LPRPCSTUBBUFFER iface, RPCOLEMESSAGE* xmsg,IRpcChannelBuffer*rpcchanbuf)
1929 const FUNCDESC *fdesc;
1930 TMStubImpl *This = (TMStubImpl *)iface;
1932 DWORD *args = NULL, res, *xargs, nrofargs;
1937 ITypeInfo *tinfo = NULL;
1941 if (xmsg->iMethod < 3) {
1942 ERR("IUnknown methods cannot be marshaled by the typelib marshaler\n");
1943 return E_UNEXPECTED;
1946 if (This->dispatch_derivative && xmsg->iMethod < sizeof(IDispatchVtbl)/sizeof(void *))
1948 IPSFactoryBuffer *factory_buffer;
1949 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
1952 hres = IPSFactoryBuffer_CreateStub(factory_buffer, &IID_IDispatch,
1953 This->pUnk, &This->dispatch_stub);
1954 IPSFactoryBuffer_Release(factory_buffer);
1958 return IRpcStubBuffer_Invoke(This->dispatch_stub, xmsg, rpcchanbuf);
1961 memset(&buf,0,sizeof(buf));
1962 buf.size = xmsg->cbBuffer;
1963 buf.base = HeapAlloc(GetProcessHeap(), 0, xmsg->cbBuffer);
1964 memcpy(buf.base, xmsg->Buffer, xmsg->cbBuffer);
1967 hres = get_funcdesc(This->tinfo,xmsg->iMethod,&tinfo,&fdesc,&iname,NULL,NULL);
1969 ERR("GetFuncDesc on method %d failed with %x\n",xmsg->iMethod,hres);
1973 if (iname && !lstrcmpW(iname, IDispatchW))
1975 ERR("IDispatch cannot be marshaled by the typelib marshaler\n");
1976 hres = E_UNEXPECTED;
1977 SysFreeString (iname);
1981 SysFreeString (iname);
1983 /* Need them for hack below */
1984 memset(names,0,sizeof(names));
1985 ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames);
1986 if (nrofnames > sizeof(names)/sizeof(names[0])) {
1987 ERR("Need more names!\n");
1990 /*dump_FUNCDESC(fdesc);*/
1992 for (i=0;i<fdesc->cParams;i++)
1993 nrofargs += _argsize(&fdesc->lprgelemdescParam[i].tdesc, tinfo);
1994 args = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(nrofargs+1)*sizeof(DWORD));
1997 hres = E_OUTOFMEMORY;
2001 /* Allocate all stuff used by call. */
2003 for (i=0;i<fdesc->cParams;i++) {
2004 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2006 hres = deserialize_param(
2015 xargs += _argsize(&elem->tdesc, tinfo);
2017 ERR("Failed to deserialize param %s, hres %x\n",relaystr(names[i+1]),hres);
2022 args[0] = (DWORD)This->pUnk;
2027 (*((FARPROC**)args[0]))[fdesc->oVft/4],
2035 DWORD dwExceptionCode = GetExceptionCode();
2036 ERR("invoke call failed with exception 0x%08x (%d)\n", dwExceptionCode, dwExceptionCode);
2037 if (FAILED(dwExceptionCode))
2038 hres = dwExceptionCode;
2040 hres = HRESULT_FROM_WIN32(dwExceptionCode);
2050 for (i=0;i<fdesc->cParams;i++) {
2051 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2052 hres = serialize_param(
2061 xargs += _argsize(&elem->tdesc, tinfo);
2063 ERR("Failed to stuballoc param, hres %x\n",hres);
2068 hres = xbuf_add (&buf, (LPBYTE)&res, sizeof(DWORD));
2073 xmsg->cbBuffer = buf.curoff;
2074 hres = IRpcChannelBuffer_GetBuffer(rpcchanbuf, xmsg, &This->iid);
2076 ERR("IRpcChannelBuffer_GetBuffer failed with error 0x%08x\n", hres);
2079 memcpy(xmsg->Buffer, buf.base, buf.curoff);
2082 for (i = 0; i < nrofnames; i++)
2083 SysFreeString(names[i]);
2085 ITypeInfo_Release(tinfo);
2086 HeapFree(GetProcessHeap(), 0, args);
2088 HeapFree(GetProcessHeap(), 0, buf.base);
2090 TRACE("returning\n");
2093 FIXME( "not implemented on non-i386\n" );
2098 static LPRPCSTUBBUFFER WINAPI
2099 TMStubImpl_IsIIDSupported(LPRPCSTUBBUFFER iface, REFIID riid) {
2100 FIXME("Huh (%s)?\n",debugstr_guid(riid));
2105 TMStubImpl_CountRefs(LPRPCSTUBBUFFER iface) {
2106 TMStubImpl *This = (TMStubImpl *)iface;
2109 return This->ref; /*FIXME? */
2112 static HRESULT WINAPI
2113 TMStubImpl_DebugServerQueryInterface(LPRPCSTUBBUFFER iface, LPVOID *ppv) {
2118 TMStubImpl_DebugServerRelease(LPRPCSTUBBUFFER iface, LPVOID ppv) {
2122 static const IRpcStubBufferVtbl tmstubvtbl = {
2123 TMStubImpl_QueryInterface,
2127 TMStubImpl_Disconnect,
2129 TMStubImpl_IsIIDSupported,
2130 TMStubImpl_CountRefs,
2131 TMStubImpl_DebugServerQueryInterface,
2132 TMStubImpl_DebugServerRelease
2135 static HRESULT WINAPI
2136 PSFacBuf_CreateStub(
2137 LPPSFACTORYBUFFER iface, REFIID riid,IUnknown *pUnkServer,
2138 IRpcStubBuffer** ppStub
2145 TRACE("(%s,%p,%p)\n",debugstr_guid(riid),pUnkServer,ppStub);
2147 hres = _get_typeinfo_for_iid(riid,&tinfo);
2149 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
2153 stub = CoTaskMemAlloc(sizeof(TMStubImpl));
2155 return E_OUTOFMEMORY;
2156 stub->lpvtbl = &tmstubvtbl;
2158 stub->tinfo = tinfo;
2159 stub->dispatch_stub = NULL;
2160 stub->dispatch_derivative = FALSE;
2162 hres = IRpcStubBuffer_Connect((LPRPCSTUBBUFFER)stub,pUnkServer);
2163 *ppStub = (LPRPCSTUBBUFFER)stub;
2164 TRACE("IRpcStubBuffer: %p\n", stub);
2166 ERR("Connect to pUnkServer failed?\n");
2168 /* if we derive from IDispatch then defer to its stub for some of its methods */
2169 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
2172 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
2173 stub->dispatch_derivative = TRUE;
2174 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
2180 static const IPSFactoryBufferVtbl psfacbufvtbl = {
2181 PSFacBuf_QueryInterface,
2184 PSFacBuf_CreateProxy,
2188 /* This is the whole PSFactoryBuffer object, just the vtableptr */
2189 static const IPSFactoryBufferVtbl *lppsfac = &psfacbufvtbl;
2191 /***********************************************************************
2192 * TMARSHAL_DllGetClassObject
2194 HRESULT TMARSHAL_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
2196 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)) {
2200 return E_NOINTERFACE;