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;
624 if (debugout) TRACE_(olerelay)("%x%x\n",arg[0],arg[1]);
626 hres = xbuf_add(buf,(LPBYTE)arg,8);
636 if (debugout) TRACE_(olerelay)("%x\n",*arg);
638 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
643 if (debugout) TRACE_(olerelay)("%04x\n",*arg & 0xffff);
645 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
650 if (debugout) TRACE_(olerelay)("%02x\n",*arg & 0xff);
652 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
655 if (debugout) TRACE_(olerelay)("Vt(%s%s)(",debugstr_vt(V_VT((VARIANT *)arg)),debugstr_vf(V_VT((VARIANT *)arg)));
658 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
659 ULONG size = VARIANT_UserSize(&flags, buf->curoff, (VARIANT *)arg);
660 xbuf_resize(buf, size);
661 VARIANT_UserMarshal(&flags, buf->base + buf->curoff, (VARIANT *)arg);
666 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
667 VARIANT_UserFree(&flags, (VARIANT *)arg);
674 TRACE_(olerelay)("%s",relaystr((WCHAR*)*arg));
676 TRACE_(olerelay)("<bstr NULL>");
680 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
681 ULONG size = BSTR_UserSize(&flags, buf->curoff, (BSTR *)arg);
682 xbuf_resize(buf, size);
683 BSTR_UserMarshal(&flags, buf->base + buf->curoff, (BSTR *)arg);
688 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
689 BSTR_UserFree(&flags, (BSTR *)arg);
695 BOOL derefhere = TRUE;
697 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
701 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
703 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
706 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
707 switch (tattr->typekind) {
709 if (tattr->tdescAlias.vt == VT_USERDEFINED)
711 DWORD href = tattr->tdescAlias.u.hreftype;
712 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
713 ITypeInfo_Release(tinfo2);
714 hres = ITypeInfo_GetRefTypeInfo(tinfo,href,&tinfo2);
716 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
719 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
720 derefhere = (tattr->typekind != TKIND_DISPATCH && tattr->typekind != TKIND_INTERFACE);
723 case TKIND_ENUM: /* confirmed */
724 case TKIND_RECORD: /* FIXME: mostly untested */
726 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
727 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
731 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
735 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
736 ITypeInfo_Release(tinfo2);
739 if (debugout) TRACE_(olerelay)("*");
740 /* Write always, so the other side knows when it gets a NULL pointer.
742 cookie = *arg ? 0x42424242 : 0;
743 hres = xbuf_add(buf,(LPBYTE)&cookie,sizeof(cookie));
747 if (debugout) TRACE_(olerelay)("NULL");
750 hres = serialize_param(tinfo,writeit,debugout,dealloc,tdesc->u.lptdesc,(DWORD*)*arg,buf);
751 if (derefhere && dealloc) HeapFree(GetProcessHeap(),0,(LPVOID)*arg);
755 if (debugout) TRACE_(olerelay)("unk(0x%x)",*arg);
757 hres = _marshal_interface(buf,&IID_IUnknown,(LPUNKNOWN)*arg);
758 if (dealloc && *(IUnknown **)arg)
759 IUnknown_Release((LPUNKNOWN)*arg);
762 if (debugout) TRACE_(olerelay)("idisp(0x%x)",*arg);
764 hres = _marshal_interface(buf,&IID_IDispatch,(LPUNKNOWN)*arg);
765 if (dealloc && *(IUnknown **)arg)
766 IUnknown_Release((LPUNKNOWN)*arg);
769 if (debugout) TRACE_(olerelay)("<void>");
771 case VT_USERDEFINED: {
775 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
777 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
780 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
781 switch (tattr->typekind) {
783 case TKIND_INTERFACE:
785 hres=_marshal_interface(buf,&(tattr->guid),(LPUNKNOWN)arg);
787 IUnknown_Release((LPUNKNOWN)arg);
791 if (debugout) TRACE_(olerelay)("{");
792 for (i=0;i<tattr->cVars;i++) {
797 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
799 ERR("Could not get vardesc of %d\n",i);
802 elem2 = &vdesc->elemdescVar;
803 tdesc2 = &elem2->tdesc;
804 hres = serialize_param(
810 (DWORD*)(((LPBYTE)arg)+vdesc->u.oInst),
813 ITypeInfo_ReleaseVarDesc(tinfo2, vdesc);
816 if (debugout && (i<(tattr->cVars-1)))
817 TRACE_(olerelay)(",");
819 if (debugout) TRACE_(olerelay)("}");
823 hres = serialize_param(tinfo2,writeit,debugout,dealloc,&tattr->tdescAlias,arg,buf);
827 if (debugout) TRACE_(olerelay)("%x",*arg);
829 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
832 FIXME("Unhandled typekind %d\n",tattr->typekind);
836 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
837 ITypeInfo_Release(tinfo2);
841 ARRAYDESC *adesc = tdesc->u.lpadesc;
844 if (debugout) TRACE_(olerelay)("carr");
845 for (i=0;i<adesc->cDims;i++) {
846 if (debugout) TRACE_(olerelay)("[%d]",adesc->rgbounds[i].cElements);
847 arrsize *= adesc->rgbounds[i].cElements;
849 if (debugout) TRACE_(olerelay)("(vt %s)",debugstr_vt(adesc->tdescElem.vt));
850 if (debugout) TRACE_(olerelay)("[");
851 for (i=0;i<arrsize;i++) {
852 hres = serialize_param(tinfo, writeit, debugout, dealloc, &adesc->tdescElem, (DWORD*)((LPBYTE)(*arg)+i*_xsize(&adesc->tdescElem, tinfo)), buf);
855 if (debugout && (i<arrsize-1)) TRACE_(olerelay)(",");
857 if (debugout) TRACE_(olerelay)("]");
859 HeapFree(GetProcessHeap(), 0, *(void **)arg);
865 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
866 ULONG size = LPSAFEARRAY_UserSize(&flags, buf->curoff, (LPSAFEARRAY *)arg);
867 xbuf_resize(buf, size);
868 LPSAFEARRAY_UserMarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
873 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
874 LPSAFEARRAY_UserFree(&flags, (LPSAFEARRAY *)arg);
879 ERR("Unhandled marshal type %d.\n",tdesc->vt);
897 TRACE("vt %s at %p\n",debugstr_vt(tdesc->vt),arg);
900 if ((vartype & 0xf000) == VT_ARRAY)
901 vartype = VT_SAFEARRAY;
908 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
909 unsigned char *buffer;
910 buffer = VARIANT_UserUnmarshal(&flags, buf->base + buf->curoff, (VARIANT *)arg);
911 buf->curoff = buffer - buf->base;
921 hres = xbuf_get(buf,(LPBYTE)arg,8);
922 if (hres) ERR("Failed to read integer 8 byte\n");
924 if (debugout) TRACE_(olerelay)("%x%x",arg[0],arg[1]);
934 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
935 if (hres) ERR("Failed to read integer 4 byte\n");
937 if (debugout) TRACE_(olerelay)("%x",*arg);
943 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
944 if (hres) ERR("Failed to read integer 4 byte\n");
947 if (debugout) TRACE_(olerelay)("%04x",*arg & 0xffff);
953 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
954 if (hres) ERR("Failed to read integer 4 byte\n");
957 if (debugout) TRACE_(olerelay)("%02x",*arg & 0xff);
962 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
963 unsigned char *buffer;
964 buffer = BSTR_UserUnmarshal(&flags, buf->base + buf->curoff, (BSTR *)arg);
965 buf->curoff = buffer - buf->base;
966 if (debugout) TRACE_(olerelay)("%s",debugstr_w(*(BSTR *)arg));
972 BOOL derefhere = TRUE;
974 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
978 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
980 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
983 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
984 switch (tattr->typekind) {
986 if (tattr->tdescAlias.vt == VT_USERDEFINED)
988 DWORD href = tattr->tdescAlias.u.hreftype;
989 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
990 ITypeInfo_Release(tinfo2);
991 hres = ITypeInfo_GetRefTypeInfo(tinfo,href,&tinfo2);
993 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
996 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
997 derefhere = (tattr->typekind != TKIND_DISPATCH && tattr->typekind != TKIND_INTERFACE);
1000 case TKIND_ENUM: /* confirmed */
1001 case TKIND_RECORD: /* FIXME: mostly untested */
1003 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
1004 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
1008 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
1012 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
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, tinfo));
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 %x 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: {
1082 if (debugout) TRACE_(olerelay)("{");
1083 for (i=0;i<tattr->cVars;i++) {
1086 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
1088 ERR("Could not get vardesc of %d\n",i);
1089 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1090 ITypeInfo_Release(tinfo2);
1093 hres = deserialize_param(
1098 &vdesc->elemdescVar.tdesc,
1099 (DWORD*)(((LPBYTE)arg)+vdesc->u.oInst),
1102 ITypeInfo2_ReleaseVarDesc(tinfo2, vdesc);
1103 if (debugout && (i<tattr->cVars-1)) TRACE_(olerelay)(",");
1105 if (debugout) TRACE_(olerelay)("}");
1109 hres = 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)("%x",*arg);
1119 ERR("Unhandled typekind %d\n",tattr->typekind);
1123 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
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 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,_xsize(tdesc->u.lptdesc, tinfo) * arrsize);
1139 for (i=0;i<arrsize;i++)
1146 (DWORD*)((LPBYTE)(*arg)+i*_xsize(&adesc->tdescElem, tinfo)),
1151 case VT_SAFEARRAY: {
1154 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
1155 unsigned char *buffer;
1156 buffer = LPSAFEARRAY_UserUnmarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
1157 buf->curoff = buffer - buf->base;
1162 ERR("No handler for VT type %d!\n",tdesc->vt);
1168 /* Retrieves a function's funcdesc, searching back into inherited interfaces. */
1169 static HRESULT get_funcdesc(ITypeInfo *tinfo, int iMethod, ITypeInfo **tactual, const FUNCDESC **fdesc,
1170 BSTR *iname, BSTR *fname, UINT *num)
1174 UINT inherited_funcs = 0;
1177 if (fname) *fname = NULL;
1178 if (iname) *iname = NULL;
1182 hr = ITypeInfo_GetTypeAttr(tinfo, &attr);
1185 ERR("GetTypeAttr failed with %x\n",hr);
1189 if(attr->typekind == TKIND_DISPATCH)
1191 if(attr->wTypeFlags & TYPEFLAG_FDUAL)
1196 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, -1, &href);
1199 ERR("Cannot get interface href from dual dispinterface\n");
1200 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1203 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
1206 ERR("Cannot get interface from dual dispinterface\n");
1207 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1210 hr = get_funcdesc(tinfo2, iMethod, tactual, fdesc, iname, fname, num);
1211 ITypeInfo_Release(tinfo2);
1212 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1215 ERR("Shouldn't be called with a non-dual dispinterface\n");
1219 impl_types = attr->cImplTypes;
1220 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1222 for (i = 0; i < impl_types; i++)
1225 ITypeInfo *pSubTypeInfo;
1228 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, i, &href);
1229 if (FAILED(hr)) return hr;
1230 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &pSubTypeInfo);
1231 if (FAILED(hr)) return hr;
1233 hr = get_funcdesc(pSubTypeInfo, iMethod, tactual, fdesc, iname, fname, &sub_funcs);
1234 inherited_funcs += sub_funcs;
1235 ITypeInfo_Release(pSubTypeInfo);
1236 if(SUCCEEDED(hr)) return hr;
1238 if(iMethod < inherited_funcs)
1240 ERR("shouldn't be here\n");
1241 return E_INVALIDARG;
1244 for(i = inherited_funcs; i <= iMethod; i++)
1246 hr = ITypeInfoImpl_GetInternalFuncDesc(tinfo, i - inherited_funcs, fdesc);
1254 /* found it. We don't care about num so zero it */
1257 ITypeInfo_AddRef(*tactual);
1258 if (fname) ITypeInfo_GetDocumentation(tinfo,(*fdesc)->memid,fname,NULL,NULL,NULL);
1259 if (iname) ITypeInfo_GetDocumentation(tinfo,-1,iname,NULL,NULL,NULL);
1263 static inline BOOL is_in_elem(const ELEMDESC *elem)
1265 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN || !elem->u.paramdesc.wParamFlags);
1268 static inline BOOL is_out_elem(const ELEMDESC *elem)
1270 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT || !elem->u.paramdesc.wParamFlags);
1274 xCall(LPVOID retptr, int method, TMProxyImpl *tpinfo /*, args */)
1276 DWORD *args = ((DWORD*)&tpinfo)+1, *xargs;
1277 const FUNCDESC *fdesc;
1279 int i, relaydeb = TRACE_ON(olerelay);
1286 DWORD remoteresult = 0;
1288 IRpcChannelBuffer *chanbuf;
1290 EnterCriticalSection(&tpinfo->crit);
1292 hres = get_funcdesc(tpinfo->tinfo,method,&tinfo,&fdesc,&iname,&fname,NULL);
1294 ERR("Did not find typeinfo/funcdesc entry for method %d!\n",method);
1295 LeaveCriticalSection(&tpinfo->crit);
1299 if (!tpinfo->chanbuf)
1301 WARN("Tried to use disconnected proxy\n");
1302 ITypeInfo_Release(tinfo);
1303 LeaveCriticalSection(&tpinfo->crit);
1304 return RPC_E_DISCONNECTED;
1306 chanbuf = tpinfo->chanbuf;
1307 IRpcChannelBuffer_AddRef(chanbuf);
1309 LeaveCriticalSection(&tpinfo->crit);
1312 TRACE_(olerelay)("->");
1314 TRACE_(olerelay)("%s:",relaystr(iname));
1316 TRACE_(olerelay)("%s(%d)",relaystr(fname),method);
1318 TRACE_(olerelay)("%d",method);
1319 TRACE_(olerelay)("(");
1322 SysFreeString(iname);
1323 SysFreeString(fname);
1325 memset(&buf,0,sizeof(buf));
1327 /* normal typelib driven serializing */
1329 /* Need them for hack below */
1330 memset(names,0,sizeof(names));
1331 if (ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames))
1333 if (nrofnames > sizeof(names)/sizeof(names[0]))
1334 ERR("Need more names!\n");
1337 for (i=0;i<fdesc->cParams;i++) {
1338 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1340 if (i) TRACE_(olerelay)(",");
1341 if (i+1<nrofnames && names[i+1])
1342 TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1344 /* No need to marshal other data than FIN and any VT_PTR. */
1345 if (!is_in_elem(elem) && (elem->tdesc.vt != VT_PTR)) {
1346 xargs+=_argsize(&elem->tdesc, tinfo);
1347 if (relaydeb) TRACE_(olerelay)("[out]");
1350 hres = serialize_param(
1361 ERR("Failed to serialize param, hres %x\n",hres);
1364 xargs+=_argsize(&elem->tdesc, tinfo);
1366 if (relaydeb) TRACE_(olerelay)(")");
1368 memset(&msg,0,sizeof(msg));
1369 msg.cbBuffer = buf.curoff;
1370 msg.iMethod = method;
1371 hres = IRpcChannelBuffer_GetBuffer(chanbuf,&msg,&(tpinfo->iid));
1373 ERR("RpcChannelBuffer GetBuffer failed, %x\n",hres);
1376 memcpy(msg.Buffer,buf.base,buf.curoff);
1377 if (relaydeb) TRACE_(olerelay)("\n");
1378 hres = IRpcChannelBuffer_SendReceive(chanbuf,&msg,&status);
1380 ERR("RpcChannelBuffer SendReceive failed, %x\n",hres);
1384 if (relaydeb) TRACE_(olerelay)(" status = %08x (",status);
1386 buf.base = HeapReAlloc(GetProcessHeap(),0,buf.base,msg.cbBuffer);
1388 buf.base = HeapAlloc(GetProcessHeap(),0,msg.cbBuffer);
1389 buf.size = msg.cbBuffer;
1390 memcpy(buf.base,msg.Buffer,buf.size);
1393 /* generic deserializer using typelib description */
1396 for (i=0;i<fdesc->cParams;i++) {
1397 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1400 if (i) TRACE_(olerelay)(",");
1401 if (i+1<nrofnames && names[i+1]) TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1403 /* No need to marshal other data than FOUT and any VT_PTR */
1404 if (!is_out_elem(elem) && (elem->tdesc.vt != VT_PTR)) {
1405 xargs += _argsize(&elem->tdesc, tinfo);
1406 if (relaydeb) TRACE_(olerelay)("[in]");
1409 hres = deserialize_param(
1419 ERR("Failed to unmarshall param, hres %x\n",hres);
1423 xargs += _argsize(&elem->tdesc, tinfo);
1426 hres = xbuf_get(&buf, (LPBYTE)&remoteresult, sizeof(DWORD));
1429 if (relaydeb) TRACE_(olerelay)(") = %08x\n", remoteresult);
1431 hres = remoteresult;
1434 IRpcChannelBuffer_FreeBuffer(chanbuf,&msg);
1435 for (i = 0; i < nrofnames; i++)
1436 SysFreeString(names[i]);
1437 HeapFree(GetProcessHeap(),0,buf.base);
1438 IRpcChannelBuffer_Release(chanbuf);
1439 ITypeInfo_Release(tinfo);
1440 TRACE("-- 0x%08x\n", hres);
1444 static HRESULT WINAPI ProxyIUnknown_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
1446 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1448 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1450 if (proxy->outerunknown)
1451 return IUnknown_QueryInterface(proxy->outerunknown, riid, ppv);
1453 FIXME("No interface\n");
1454 return E_NOINTERFACE;
1457 static ULONG WINAPI ProxyIUnknown_AddRef(IUnknown *iface)
1459 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1463 if (proxy->outerunknown)
1464 return IUnknown_AddRef(proxy->outerunknown);
1466 return 2; /* FIXME */
1469 static ULONG WINAPI ProxyIUnknown_Release(IUnknown *iface)
1471 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1475 if (proxy->outerunknown)
1476 return IUnknown_Release(proxy->outerunknown);
1478 return 1; /* FIXME */
1481 static HRESULT WINAPI ProxyIDispatch_GetTypeInfoCount(LPDISPATCH iface, UINT * pctinfo)
1483 TMProxyImpl *This = (TMProxyImpl *)iface;
1485 TRACE("(%p)\n", pctinfo);
1487 return IDispatch_GetTypeInfoCount(This->dispatch, pctinfo);
1490 static HRESULT WINAPI ProxyIDispatch_GetTypeInfo(LPDISPATCH iface, UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo)
1492 TMProxyImpl *This = (TMProxyImpl *)iface;
1494 TRACE("(%d, %x, %p)\n", iTInfo, lcid, ppTInfo);
1496 return IDispatch_GetTypeInfo(This->dispatch, iTInfo, lcid, ppTInfo);
1499 static HRESULT WINAPI ProxyIDispatch_GetIDsOfNames(LPDISPATCH iface, REFIID riid, LPOLESTR * rgszNames, UINT cNames, LCID lcid, DISPID * rgDispId)
1501 TMProxyImpl *This = (TMProxyImpl *)iface;
1503 TRACE("(%s, %p, %d, 0x%x, %p)\n", debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);
1505 return IDispatch_GetIDsOfNames(This->dispatch, riid, rgszNames,
1506 cNames, lcid, rgDispId);
1509 static HRESULT WINAPI ProxyIDispatch_Invoke(LPDISPATCH iface, DISPID dispIdMember, REFIID riid, LCID lcid,
1510 WORD wFlags, DISPPARAMS * pDispParams, VARIANT * pVarResult,
1511 EXCEPINFO * pExcepInfo, UINT * puArgErr)
1513 TMProxyImpl *This = (TMProxyImpl *)iface;
1515 TRACE("(%d, %s, 0x%x, 0x%x, %p, %p, %p, %p)\n", dispIdMember,
1516 debugstr_guid(riid), lcid, wFlags, pDispParams, pVarResult,
1517 pExcepInfo, puArgErr);
1519 return IDispatch_Invoke(This->dispatch, dispIdMember, riid, lcid,
1520 wFlags, pDispParams, pVarResult, pExcepInfo,
1526 const IRpcChannelBufferVtbl *lpVtbl;
1528 /* the IDispatch-derived interface we are handling */
1530 IRpcChannelBuffer *pDelegateChannel;
1531 } TMarshalDispatchChannel;
1533 static HRESULT WINAPI TMarshalDispatchChannel_QueryInterface(LPRPCCHANNELBUFFER iface, REFIID riid, LPVOID *ppv)
1536 if (IsEqualIID(riid,&IID_IRpcChannelBuffer) || IsEqualIID(riid,&IID_IUnknown))
1539 IUnknown_AddRef(iface);
1542 return E_NOINTERFACE;
1545 static ULONG WINAPI TMarshalDispatchChannel_AddRef(LPRPCCHANNELBUFFER iface)
1547 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1548 return InterlockedIncrement(&This->refs);
1551 static ULONG WINAPI TMarshalDispatchChannel_Release(LPRPCCHANNELBUFFER iface)
1553 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1556 ref = InterlockedDecrement(&This->refs);
1560 IRpcChannelBuffer_Release(This->pDelegateChannel);
1561 HeapFree(GetProcessHeap(), 0, This);
1565 static HRESULT WINAPI TMarshalDispatchChannel_GetBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg, REFIID riid)
1567 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1568 TRACE("(%p, %s)\n", olemsg, debugstr_guid(riid));
1569 /* Note: we are pretending to invoke a method on the interface identified
1570 * by tmarshal_iid so that we can re-use the IDispatch proxy/stub code
1571 * without the RPC runtime getting confused by not exporting an IDispatch interface */
1572 return IRpcChannelBuffer_GetBuffer(This->pDelegateChannel, olemsg, &This->tmarshal_iid);
1575 static HRESULT WINAPI TMarshalDispatchChannel_SendReceive(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE *olemsg, ULONG *pstatus)
1577 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1578 TRACE("(%p, %p)\n", olemsg, pstatus);
1579 return IRpcChannelBuffer_SendReceive(This->pDelegateChannel, olemsg, pstatus);
1582 static HRESULT WINAPI TMarshalDispatchChannel_FreeBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg)
1584 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1585 TRACE("(%p)\n", olemsg);
1586 return IRpcChannelBuffer_FreeBuffer(This->pDelegateChannel, olemsg);
1589 static HRESULT WINAPI TMarshalDispatchChannel_GetDestCtx(LPRPCCHANNELBUFFER iface, DWORD* pdwDestContext, void** ppvDestContext)
1591 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1592 TRACE("(%p,%p)\n", pdwDestContext, ppvDestContext);
1593 return IRpcChannelBuffer_GetDestCtx(This->pDelegateChannel, pdwDestContext, ppvDestContext);
1596 static HRESULT WINAPI TMarshalDispatchChannel_IsConnected(LPRPCCHANNELBUFFER iface)
1598 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1600 return IRpcChannelBuffer_IsConnected(This->pDelegateChannel);
1603 static const IRpcChannelBufferVtbl TMarshalDispatchChannelVtbl =
1605 TMarshalDispatchChannel_QueryInterface,
1606 TMarshalDispatchChannel_AddRef,
1607 TMarshalDispatchChannel_Release,
1608 TMarshalDispatchChannel_GetBuffer,
1609 TMarshalDispatchChannel_SendReceive,
1610 TMarshalDispatchChannel_FreeBuffer,
1611 TMarshalDispatchChannel_GetDestCtx,
1612 TMarshalDispatchChannel_IsConnected
1615 static HRESULT TMarshalDispatchChannel_Create(
1616 IRpcChannelBuffer *pDelegateChannel, REFIID tmarshal_riid,
1617 IRpcChannelBuffer **ppChannel)
1619 TMarshalDispatchChannel *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1621 return E_OUTOFMEMORY;
1623 This->lpVtbl = &TMarshalDispatchChannelVtbl;
1625 IRpcChannelBuffer_AddRef(pDelegateChannel);
1626 This->pDelegateChannel = pDelegateChannel;
1627 This->tmarshal_iid = *tmarshal_riid;
1629 *ppChannel = (IRpcChannelBuffer *)&This->lpVtbl;
1634 static inline HRESULT get_facbuf_for_iid(REFIID riid, IPSFactoryBuffer **facbuf)
1639 if ((hr = CoGetPSClsid(riid, &clsid)))
1641 return CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL,
1642 &IID_IPSFactoryBuffer, (LPVOID*)facbuf);
1645 static HRESULT init_proxy_entry_point(TMProxyImpl *proxy, unsigned int num)
1648 /* nrofargs without This */
1651 TMAsmProxy *xasm = proxy->asmstubs + num;
1653 const FUNCDESC *fdesc;
1655 hres = get_funcdesc(proxy->tinfo, num, &tinfo2, &fdesc, NULL, NULL, NULL);
1657 ERR("GetFuncDesc %x should not fail here.\n",hres);
1660 ITypeInfo_Release(tinfo2);
1661 /* some args take more than 4 byte on the stack */
1663 for (j=0;j<fdesc->cParams;j++)
1664 nrofargs += _argsize(&fdesc->lprgelemdescParam[j].tdesc, proxy->tinfo);
1667 if (fdesc->callconv != CC_STDCALL) {
1668 ERR("calling convention is not stdcall????\n");
1671 /* popl %eax - return ptr
1678 * arg3 arg2 arg1 <method> <returnptr>
1680 xasm->popleax = 0x58;
1681 xasm->pushlval = 0x68;
1683 xasm->pushleax = 0x50;
1684 xasm->lcall = 0xe8; /* relative jump */
1685 xasm->xcall = (DWORD)xCall;
1686 xasm->xcall -= (DWORD)&(xasm->lret);
1688 xasm->bytestopop = (nrofargs+2)*4; /* pop args, This, iMethod */
1690 proxy->lpvtbl[num] = xasm;
1692 FIXME("not implemented on non i386\n");
1698 static HRESULT WINAPI
1699 PSFacBuf_CreateProxy(
1700 LPPSFACTORYBUFFER iface, IUnknown* pUnkOuter, REFIID riid,
1701 IRpcProxyBuffer **ppProxy, LPVOID *ppv)
1705 unsigned int i, nroffuncs;
1708 BOOL defer_to_dispatch = FALSE;
1710 TRACE("(...%s...)\n",debugstr_guid(riid));
1711 hres = _get_typeinfo_for_iid(riid,&tinfo);
1713 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
1717 hres = num_of_funcs(tinfo, &nroffuncs);
1719 ERR("Cannot get number of functions for typeinfo %s\n",debugstr_guid(riid));
1720 ITypeInfo_Release(tinfo);
1724 proxy = CoTaskMemAlloc(sizeof(TMProxyImpl));
1725 if (!proxy) return E_OUTOFMEMORY;
1727 assert(sizeof(TMAsmProxy) == 16);
1729 proxy->dispatch = NULL;
1730 proxy->dispatch_proxy = NULL;
1731 proxy->outerunknown = pUnkOuter;
1732 proxy->asmstubs = VirtualAlloc(NULL, sizeof(TMAsmProxy) * nroffuncs, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
1733 if (!proxy->asmstubs) {
1734 ERR("Could not commit pages for proxy thunks\n");
1735 CoTaskMemFree(proxy);
1736 return E_OUTOFMEMORY;
1738 proxy->lpvtbl2 = &tmproxyvtable;
1739 /* one reference for the proxy */
1741 proxy->tinfo = tinfo;
1745 InitializeCriticalSection(&proxy->crit);
1746 proxy->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TMProxyImpl.crit");
1748 proxy->lpvtbl = HeapAlloc(GetProcessHeap(),0,sizeof(LPBYTE)*nroffuncs);
1750 /* if we derive from IDispatch then defer to its proxy for its methods */
1751 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
1754 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
1756 IPSFactoryBuffer *factory_buffer;
1757 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
1760 hres = IPSFactoryBuffer_CreateProxy(factory_buffer, NULL,
1761 &IID_IDispatch, &proxy->dispatch_proxy,
1762 (void **)&proxy->dispatch);
1763 IPSFactoryBuffer_Release(factory_buffer);
1765 if ((hres == S_OK) && (nroffuncs < 7))
1767 ERR("nroffuncs calculated incorrectly (%d)\n", nroffuncs);
1768 hres = E_UNEXPECTED;
1772 defer_to_dispatch = TRUE;
1775 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
1778 for (i=0;i<nroffuncs;i++) {
1781 proxy->lpvtbl[i] = ProxyIUnknown_QueryInterface;
1784 proxy->lpvtbl[i] = ProxyIUnknown_AddRef;
1787 proxy->lpvtbl[i] = ProxyIUnknown_Release;
1790 if(!defer_to_dispatch)
1792 hres = init_proxy_entry_point(proxy, i);
1793 if(FAILED(hres)) return hres;
1795 else proxy->lpvtbl[3] = ProxyIDispatch_GetTypeInfoCount;
1798 if(!defer_to_dispatch)
1800 hres = init_proxy_entry_point(proxy, i);
1801 if(FAILED(hres)) return hres;
1803 else proxy->lpvtbl[4] = ProxyIDispatch_GetTypeInfo;
1806 if(!defer_to_dispatch)
1808 hres = init_proxy_entry_point(proxy, i);
1809 if(FAILED(hres)) return hres;
1811 else proxy->lpvtbl[5] = ProxyIDispatch_GetIDsOfNames;
1814 if(!defer_to_dispatch)
1816 hres = init_proxy_entry_point(proxy, i);
1817 if(FAILED(hres)) return hres;
1819 else proxy->lpvtbl[6] = ProxyIDispatch_Invoke;
1822 hres = init_proxy_entry_point(proxy, i);
1823 if(FAILED(hres)) return hres;
1830 *ppProxy = (IRpcProxyBuffer *)&(proxy->lpvtbl2);
1831 IUnknown_AddRef((IUnknown *)*ppv);
1835 TMProxyImpl_Release((IRpcProxyBuffer *)&proxy->lpvtbl2);
1839 typedef struct _TMStubImpl {
1840 const IRpcStubBufferVtbl *lpvtbl;
1846 IRpcStubBuffer *dispatch_stub;
1847 BOOL dispatch_derivative;
1850 static HRESULT WINAPI
1851 TMStubImpl_QueryInterface(LPRPCSTUBBUFFER iface, REFIID riid, LPVOID *ppv)
1853 if (IsEqualIID(riid,&IID_IRpcStubBuffer)||IsEqualIID(riid,&IID_IUnknown)){
1855 IRpcStubBuffer_AddRef(iface);
1858 FIXME("%s, not supported IID.\n",debugstr_guid(riid));
1859 return E_NOINTERFACE;
1863 TMStubImpl_AddRef(LPRPCSTUBBUFFER iface)
1865 TMStubImpl *This = (TMStubImpl *)iface;
1866 ULONG refCount = InterlockedIncrement(&This->ref);
1868 TRACE("(%p)->(ref before=%u)\n", This, refCount - 1);
1874 TMStubImpl_Release(LPRPCSTUBBUFFER iface)
1876 TMStubImpl *This = (TMStubImpl *)iface;
1877 ULONG refCount = InterlockedDecrement(&This->ref);
1879 TRACE("(%p)->(ref before=%u)\n", This, refCount + 1);
1883 IRpcStubBuffer_Disconnect(iface);
1884 ITypeInfo_Release(This->tinfo);
1885 if (This->dispatch_stub)
1886 IRpcStubBuffer_Release(This->dispatch_stub);
1887 CoTaskMemFree(This);
1892 static HRESULT WINAPI
1893 TMStubImpl_Connect(LPRPCSTUBBUFFER iface, LPUNKNOWN pUnkServer)
1895 TMStubImpl *This = (TMStubImpl *)iface;
1897 TRACE("(%p)->(%p)\n", This, pUnkServer);
1899 IUnknown_AddRef(pUnkServer);
1900 This->pUnk = pUnkServer;
1902 if (This->dispatch_stub)
1903 IRpcStubBuffer_Connect(This->dispatch_stub, pUnkServer);
1909 TMStubImpl_Disconnect(LPRPCSTUBBUFFER iface)
1911 TMStubImpl *This = (TMStubImpl *)iface;
1913 TRACE("(%p)->()\n", This);
1917 IUnknown_Release(This->pUnk);
1921 if (This->dispatch_stub)
1922 IRpcStubBuffer_Disconnect(This->dispatch_stub);
1925 static HRESULT WINAPI
1927 LPRPCSTUBBUFFER iface, RPCOLEMESSAGE* xmsg,IRpcChannelBuffer*rpcchanbuf)
1931 const FUNCDESC *fdesc;
1932 TMStubImpl *This = (TMStubImpl *)iface;
1934 DWORD *args = NULL, res, *xargs, nrofargs;
1939 ITypeInfo *tinfo = NULL;
1943 if (xmsg->iMethod < 3) {
1944 ERR("IUnknown methods cannot be marshaled by the typelib marshaler\n");
1945 return E_UNEXPECTED;
1948 if (This->dispatch_derivative && xmsg->iMethod < sizeof(IDispatchVtbl)/sizeof(void *))
1950 IPSFactoryBuffer *factory_buffer;
1951 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
1954 hres = IPSFactoryBuffer_CreateStub(factory_buffer, &IID_IDispatch,
1955 This->pUnk, &This->dispatch_stub);
1956 IPSFactoryBuffer_Release(factory_buffer);
1960 return IRpcStubBuffer_Invoke(This->dispatch_stub, xmsg, rpcchanbuf);
1963 memset(&buf,0,sizeof(buf));
1964 buf.size = xmsg->cbBuffer;
1965 buf.base = HeapAlloc(GetProcessHeap(), 0, xmsg->cbBuffer);
1966 memcpy(buf.base, xmsg->Buffer, xmsg->cbBuffer);
1969 hres = get_funcdesc(This->tinfo,xmsg->iMethod,&tinfo,&fdesc,&iname,NULL,NULL);
1971 ERR("GetFuncDesc on method %d failed with %x\n",xmsg->iMethod,hres);
1975 if (iname && !lstrcmpW(iname, IDispatchW))
1977 ERR("IDispatch cannot be marshaled by the typelib marshaler\n");
1978 hres = E_UNEXPECTED;
1979 SysFreeString (iname);
1983 SysFreeString (iname);
1985 /* Need them for hack below */
1986 memset(names,0,sizeof(names));
1987 ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames);
1988 if (nrofnames > sizeof(names)/sizeof(names[0])) {
1989 ERR("Need more names!\n");
1992 /*dump_FUNCDESC(fdesc);*/
1994 for (i=0;i<fdesc->cParams;i++)
1995 nrofargs += _argsize(&fdesc->lprgelemdescParam[i].tdesc, tinfo);
1996 args = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(nrofargs+1)*sizeof(DWORD));
1999 hres = E_OUTOFMEMORY;
2003 /* Allocate all stuff used by call. */
2005 for (i=0;i<fdesc->cParams;i++) {
2006 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2008 hres = deserialize_param(
2017 xargs += _argsize(&elem->tdesc, tinfo);
2019 ERR("Failed to deserialize param %s, hres %x\n",relaystr(names[i+1]),hres);
2024 args[0] = (DWORD)This->pUnk;
2029 (*((FARPROC**)args[0]))[fdesc->oVft/4],
2037 DWORD dwExceptionCode = GetExceptionCode();
2038 ERR("invoke call failed with exception 0x%08x (%d)\n", dwExceptionCode, dwExceptionCode);
2039 if (FAILED(dwExceptionCode))
2040 hres = dwExceptionCode;
2042 hres = HRESULT_FROM_WIN32(dwExceptionCode);
2052 for (i=0;i<fdesc->cParams;i++) {
2053 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2054 hres = serialize_param(
2063 xargs += _argsize(&elem->tdesc, tinfo);
2065 ERR("Failed to stuballoc param, hres %x\n",hres);
2070 hres = xbuf_add (&buf, (LPBYTE)&res, sizeof(DWORD));
2075 xmsg->cbBuffer = buf.curoff;
2076 hres = IRpcChannelBuffer_GetBuffer(rpcchanbuf, xmsg, &This->iid);
2078 ERR("IRpcChannelBuffer_GetBuffer failed with error 0x%08x\n", hres);
2081 memcpy(xmsg->Buffer, buf.base, buf.curoff);
2084 for (i = 0; i < nrofnames; i++)
2085 SysFreeString(names[i]);
2087 ITypeInfo_Release(tinfo);
2088 HeapFree(GetProcessHeap(), 0, args);
2090 HeapFree(GetProcessHeap(), 0, buf.base);
2092 TRACE("returning\n");
2095 FIXME( "not implemented on non-i386\n" );
2100 static LPRPCSTUBBUFFER WINAPI
2101 TMStubImpl_IsIIDSupported(LPRPCSTUBBUFFER iface, REFIID riid) {
2102 FIXME("Huh (%s)?\n",debugstr_guid(riid));
2107 TMStubImpl_CountRefs(LPRPCSTUBBUFFER iface) {
2108 TMStubImpl *This = (TMStubImpl *)iface;
2111 return This->ref; /*FIXME? */
2114 static HRESULT WINAPI
2115 TMStubImpl_DebugServerQueryInterface(LPRPCSTUBBUFFER iface, LPVOID *ppv) {
2120 TMStubImpl_DebugServerRelease(LPRPCSTUBBUFFER iface, LPVOID ppv) {
2124 static const IRpcStubBufferVtbl tmstubvtbl = {
2125 TMStubImpl_QueryInterface,
2129 TMStubImpl_Disconnect,
2131 TMStubImpl_IsIIDSupported,
2132 TMStubImpl_CountRefs,
2133 TMStubImpl_DebugServerQueryInterface,
2134 TMStubImpl_DebugServerRelease
2137 static HRESULT WINAPI
2138 PSFacBuf_CreateStub(
2139 LPPSFACTORYBUFFER iface, REFIID riid,IUnknown *pUnkServer,
2140 IRpcStubBuffer** ppStub
2147 TRACE("(%s,%p,%p)\n",debugstr_guid(riid),pUnkServer,ppStub);
2149 hres = _get_typeinfo_for_iid(riid,&tinfo);
2151 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
2155 stub = CoTaskMemAlloc(sizeof(TMStubImpl));
2157 return E_OUTOFMEMORY;
2158 stub->lpvtbl = &tmstubvtbl;
2160 stub->tinfo = tinfo;
2161 stub->dispatch_stub = NULL;
2162 stub->dispatch_derivative = FALSE;
2164 hres = IRpcStubBuffer_Connect((LPRPCSTUBBUFFER)stub,pUnkServer);
2165 *ppStub = (LPRPCSTUBBUFFER)stub;
2166 TRACE("IRpcStubBuffer: %p\n", stub);
2168 ERR("Connect to pUnkServer failed?\n");
2170 /* if we derive from IDispatch then defer to its stub for some of its methods */
2171 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
2174 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
2175 stub->dispatch_derivative = TRUE;
2176 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
2182 static const IPSFactoryBufferVtbl psfacbufvtbl = {
2183 PSFacBuf_QueryInterface,
2186 PSFacBuf_CreateProxy,
2190 /* This is the whole PSFactoryBuffer object, just the vtableptr */
2191 static const IPSFactoryBufferVtbl *lppsfac = &psfacbufvtbl;
2193 /***********************************************************************
2194 * TMARSHAL_DllGetClassObject
2196 HRESULT TMARSHAL_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
2198 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)) {
2202 return E_NOINTERFACE;