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 static HRESULT TMarshalDispatchChannel_Create(
58 IRpcChannelBuffer *pDelegateChannel, REFIID tmarshal_riid,
59 IRpcChannelBuffer **ppChannel);
61 typedef struct _marshal_state {
67 /* used in the olerelay code to avoid having the L"" stuff added by debugstr_w */
68 static char *relaystr(WCHAR *in) {
69 char *tmp = (char *)debugstr_w(in);
71 tmp[strlen(tmp)-1] = '\0';
76 xbuf_resize(marshal_state *buf, DWORD newsize)
78 if(buf->size >= newsize)
83 buf->base = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buf->base, newsize);
89 buf->base = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, newsize);
98 xbuf_add(marshal_state *buf, const BYTE *stuff, DWORD size)
102 if(buf->size - buf->curoff < size)
104 hr = xbuf_resize(buf, buf->size + size + 100);
105 if(FAILED(hr)) return hr;
107 memcpy(buf->base+buf->curoff,stuff,size);
113 xbuf_get(marshal_state *buf, LPBYTE stuff, DWORD size) {
114 if (buf->size < buf->curoff+size) return E_FAIL;
115 memcpy(stuff,buf->base+buf->curoff,size);
121 xbuf_skip(marshal_state *buf, DWORD size) {
122 if (buf->size < buf->curoff+size) return E_FAIL;
128 _unmarshal_interface(marshal_state *buf, REFIID riid, LPUNKNOWN *pUnk) {
130 ULARGE_INTEGER newpos;
131 LARGE_INTEGER seekto;
136 TRACE("...%s...\n",debugstr_guid(riid));
139 hres = xbuf_get(buf,(LPBYTE)&xsize,sizeof(xsize));
141 ERR("xbuf_get failed\n");
145 if (xsize == 0) return S_OK;
147 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
149 ERR("Stream create failed %x\n",hres);
153 hres = IStream_Write(pStm,buf->base+buf->curoff,xsize,&res);
155 ERR("stream write %x\n",hres);
156 IStream_Release(pStm);
160 memset(&seekto,0,sizeof(seekto));
161 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
163 ERR("Failed Seek %x\n",hres);
164 IStream_Release(pStm);
168 hres = CoUnmarshalInterface(pStm,riid,(LPVOID*)pUnk);
170 ERR("Unmarshalling interface %s failed with %x\n",debugstr_guid(riid),hres);
171 IStream_Release(pStm);
175 IStream_Release(pStm);
176 return xbuf_skip(buf,xsize);
180 _marshal_interface(marshal_state *buf, REFIID riid, LPUNKNOWN pUnk) {
181 LPBYTE tempbuf = NULL;
182 IStream *pStm = NULL;
184 ULARGE_INTEGER newpos;
185 LARGE_INTEGER seekto;
191 /* this is valid, if for instance we serialize
192 * a VT_DISPATCH with NULL ptr which apparently
193 * can happen. S_OK to make sure we continue
196 WARN("pUnk is NULL\n");
198 return xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
203 TRACE("...%s...\n",debugstr_guid(riid));
205 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
207 ERR("Stream create failed %x\n",hres);
211 hres = CoMarshalInterface(pStm,riid,pUnk,0,NULL,0);
213 ERR("Marshalling interface %s failed with %x\n", debugstr_guid(riid), hres);
217 hres = IStream_Stat(pStm,&ststg,STATFLAG_NONAME);
219 ERR("Stream stat failed\n");
223 tempbuf = HeapAlloc(GetProcessHeap(), 0, ststg.cbSize.u.LowPart);
224 memset(&seekto,0,sizeof(seekto));
225 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
227 ERR("Failed Seek %x\n",hres);
231 hres = IStream_Read(pStm,tempbuf,ststg.cbSize.u.LowPart,&res);
233 ERR("Failed Read %x\n",hres);
237 xsize = ststg.cbSize.u.LowPart;
238 xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
239 hres = xbuf_add(buf,tempbuf,ststg.cbSize.u.LowPart);
241 HeapFree(GetProcessHeap(),0,tempbuf);
242 IStream_Release(pStm);
248 xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
249 if (pStm) IStream_Release(pStm);
250 HeapFree(GetProcessHeap(), 0, tempbuf);
254 /********************* OLE Proxy/Stub Factory ********************************/
255 static HRESULT WINAPI
256 PSFacBuf_QueryInterface(LPPSFACTORYBUFFER iface, REFIID iid, LPVOID *ppv) {
257 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)||IsEqualIID(iid,&IID_IUnknown)) {
259 /* No ref counting, static class */
262 FIXME("(%s) unknown IID?\n",debugstr_guid(iid));
263 return E_NOINTERFACE;
266 static ULONG WINAPI PSFacBuf_AddRef(LPPSFACTORYBUFFER iface) { return 2; }
267 static ULONG WINAPI PSFacBuf_Release(LPPSFACTORYBUFFER iface) { return 1; }
270 _get_typeinfo_for_iid(REFIID riid, ITypeInfo**ti) {
273 char tlguid[200],typelibkey[300],interfacekey[300],ver[100];
276 DWORD tlguidlen, verlen, type;
280 sprintf( interfacekey, "Interface\\{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\Typelib",
281 riid->Data1, riid->Data2, riid->Data3,
282 riid->Data4[0], riid->Data4[1], riid->Data4[2], riid->Data4[3],
283 riid->Data4[4], riid->Data4[5], riid->Data4[6], riid->Data4[7]
286 if (RegOpenKeyA(HKEY_CLASSES_ROOT,interfacekey,&ikey)) {
287 ERR("No %s key found.\n",interfacekey);
290 tlguidlen = sizeof(tlguid);
291 if (RegQueryValueExA(ikey,NULL,NULL,&type,(LPBYTE)tlguid,&tlguidlen)) {
292 ERR("Getting typelib guid failed.\n");
296 verlen = sizeof(ver);
297 if (RegQueryValueExA(ikey,"Version",NULL,&type,(LPBYTE)ver,&verlen)) {
298 ERR("Could not get version value?\n");
303 sprintf(typelibkey,"Typelib\\%s\\%s\\0\\win%u",tlguid,ver,(sizeof(void*) == 8) ? 64 : 32);
304 tlfnlen = sizeof(tlfn);
305 if (RegQueryValueA(HKEY_CLASSES_ROOT,typelibkey,tlfn,&tlfnlen)) {
306 ERR("Could not get typelib fn?\n");
309 MultiByteToWideChar(CP_ACP, 0, tlfn, -1, tlfnW, sizeof(tlfnW) / sizeof(tlfnW[0]));
310 hres = LoadTypeLib(tlfnW,&tl);
312 ERR("Failed to load typelib for %s, but it should be there.\n",debugstr_guid(riid));
315 hres = ITypeLib_GetTypeInfoOfGuid(tl,riid,ti);
317 ERR("typelib does not contain info for %s?\n",debugstr_guid(riid));
318 ITypeLib_Release(tl);
321 ITypeLib_Release(tl);
326 * Determine the number of functions including all inherited functions
327 * and well as the size of the vtbl.
328 * Note for non-dual dispinterfaces we simply return the size of IDispatch.
330 static HRESULT num_of_funcs(ITypeInfo *tinfo, unsigned int *num,
331 unsigned int *vtbl_size)
336 UINT inherited_funcs = 0, i;
339 if(vtbl_size) *vtbl_size = 0;
341 hr = ITypeInfo_GetTypeAttr(tinfo, &attr);
344 ERR("GetTypeAttr failed with %x\n", hr);
348 if(attr->typekind == TKIND_DISPATCH)
350 if(attr->wTypeFlags & TYPEFLAG_FDUAL)
354 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
355 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, -1, &href);
358 ERR("Unable to get interface href from dual dispinterface\n");
361 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
364 ERR("Unable to get interface from dual dispinterface\n");
367 hr = num_of_funcs(tinfo2, num, vtbl_size);
368 ITypeInfo_Release(tinfo2);
371 else /* non-dual dispinterface */
373 /* These will be the size of IDispatchVtbl */
374 *num = attr->cbSizeVft / sizeof(void *);
375 if(vtbl_size) *vtbl_size = attr->cbSizeVft;
376 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
381 for (i = 0; i < attr->cImplTypes; i++)
384 ITypeInfo *pSubTypeInfo;
387 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, i, &href);
388 if (FAILED(hr)) goto end;
389 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &pSubTypeInfo);
390 if (FAILED(hr)) goto end;
392 hr = num_of_funcs(pSubTypeInfo, &sub_funcs, NULL);
393 ITypeInfo_Release(pSubTypeInfo);
395 if(FAILED(hr)) goto end;
396 inherited_funcs += sub_funcs;
399 *num = inherited_funcs + attr->cFuncs;
400 if(vtbl_size) *vtbl_size = attr->cbSizeVft;
403 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
409 #include "pshpack1.h"
411 typedef struct _TMAsmProxy {
426 # warning You need to implement stubless proxies for your architecture
427 typedef struct _TMAsmProxy {
431 typedef struct _TMProxyImpl {
433 IRpcProxyBuffer IRpcProxyBuffer_iface;
436 TMAsmProxy *asmstubs;
438 IRpcChannelBuffer* chanbuf;
440 CRITICAL_SECTION crit;
441 IUnknown *outerunknown;
443 IRpcProxyBuffer *dispatch_proxy;
446 static inline TMProxyImpl *impl_from_IRpcProxyBuffer( IRpcProxyBuffer *iface )
448 return CONTAINING_RECORD(iface, TMProxyImpl, IRpcProxyBuffer_iface);
451 static HRESULT WINAPI
452 TMProxyImpl_QueryInterface(LPRPCPROXYBUFFER iface, REFIID riid, LPVOID *ppv)
455 if (IsEqualIID(riid,&IID_IUnknown)||IsEqualIID(riid,&IID_IRpcProxyBuffer)) {
457 IRpcProxyBuffer_AddRef(iface);
460 FIXME("no interface for %s\n",debugstr_guid(riid));
461 return E_NOINTERFACE;
465 TMProxyImpl_AddRef(LPRPCPROXYBUFFER iface)
467 TMProxyImpl *This = impl_from_IRpcProxyBuffer( iface );
468 ULONG refCount = InterlockedIncrement(&This->ref);
470 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
476 TMProxyImpl_Release(LPRPCPROXYBUFFER iface)
478 TMProxyImpl *This = impl_from_IRpcProxyBuffer( iface );
479 ULONG refCount = InterlockedDecrement(&This->ref);
481 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
485 if (This->dispatch_proxy) IRpcProxyBuffer_Release(This->dispatch_proxy);
486 This->crit.DebugInfo->Spare[0] = 0;
487 DeleteCriticalSection(&This->crit);
488 if (This->chanbuf) IRpcChannelBuffer_Release(This->chanbuf);
489 VirtualFree(This->asmstubs, 0, MEM_RELEASE);
490 HeapFree(GetProcessHeap(), 0, This->lpvtbl);
491 ITypeInfo_Release(This->tinfo);
497 static HRESULT WINAPI
499 LPRPCPROXYBUFFER iface,IRpcChannelBuffer* pRpcChannelBuffer)
501 TMProxyImpl *This = impl_from_IRpcProxyBuffer( iface );
503 TRACE("(%p)\n", pRpcChannelBuffer);
505 EnterCriticalSection(&This->crit);
507 IRpcChannelBuffer_AddRef(pRpcChannelBuffer);
508 This->chanbuf = pRpcChannelBuffer;
510 LeaveCriticalSection(&This->crit);
512 if (This->dispatch_proxy)
514 IRpcChannelBuffer *pDelegateChannel;
515 HRESULT hr = TMarshalDispatchChannel_Create(pRpcChannelBuffer, &This->iid, &pDelegateChannel);
518 hr = IRpcProxyBuffer_Connect(This->dispatch_proxy, pDelegateChannel);
519 IRpcChannelBuffer_Release(pDelegateChannel);
527 TMProxyImpl_Disconnect(LPRPCPROXYBUFFER iface)
529 TMProxyImpl *This = impl_from_IRpcProxyBuffer( iface );
533 EnterCriticalSection(&This->crit);
535 IRpcChannelBuffer_Release(This->chanbuf);
536 This->chanbuf = NULL;
538 LeaveCriticalSection(&This->crit);
540 if (This->dispatch_proxy)
541 IRpcProxyBuffer_Disconnect(This->dispatch_proxy);
545 static const IRpcProxyBufferVtbl tmproxyvtable = {
546 TMProxyImpl_QueryInterface,
550 TMProxyImpl_Disconnect
553 /* how much space do we use on stack in DWORD steps. */
555 _argsize(TYPEDESC *tdesc, ITypeInfo *tinfo) {
559 return 8/sizeof(DWORD);
561 return sizeof(double)/sizeof(DWORD);
563 return sizeof(CY)/sizeof(DWORD);
565 return sizeof(DATE)/sizeof(DWORD);
567 return (sizeof(DECIMAL)+3)/sizeof(DWORD);
569 return (sizeof(VARIANT)+3)/sizeof(DWORD);
577 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
579 return 0; /* should fail critically in serialize_param */
580 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
581 ret = (tattr->cbSizeInstance+3)/sizeof(DWORD);
582 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
583 ITypeInfo_Release(tinfo2);
591 /* how much space do we use on the heap (in bytes) */
593 _xsize(const TYPEDESC *td, ITypeInfo *tinfo) {
600 return sizeof(VARIANT);
603 const ARRAYDESC *adesc = td->u.lpadesc;
605 for (i=0;i<adesc->cDims;i++)
606 arrsize *= adesc->rgbounds[i].cElements;
607 return arrsize*_xsize(&adesc->tdescElem, tinfo);
627 hres = ITypeInfo_GetRefTypeInfo(tinfo,td->u.hreftype,&tinfo2);
630 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
631 ret = tattr->cbSizeInstance;
632 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
633 ITypeInfo_Release(tinfo2);
641 /* Whether we pass this type by reference or by value */
643 _passbyref(const TYPEDESC *td, ITypeInfo *tinfo) {
644 if (td->vt == VT_USERDEFINED ||
645 td->vt == VT_VARIANT ||
665 TRACE("(tdesc.vt %s)\n",debugstr_vt(tdesc->vt));
668 if ((vartype & 0xf000) == VT_ARRAY)
669 vartype = VT_SAFEARRAY;
678 if (debugout) TRACE_(olerelay)("%x%x\n",arg[0],arg[1]);
680 hres = xbuf_add(buf,(LPBYTE)arg,8);
689 if (debugout) TRACE_(olerelay)("%x\n",*arg);
691 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
697 if (debugout) TRACE_(olerelay)("%04x\n",*arg & 0xffff);
699 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
704 if (debugout) TRACE_(olerelay)("%02x\n",*arg & 0xff);
706 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
709 if (debugout) TRACE_(olerelay)("Vt(%s%s)(",debugstr_vt(V_VT((VARIANT *)arg)),debugstr_vf(V_VT((VARIANT *)arg)));
712 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
713 ULONG size = VARIANT_UserSize(&flags, buf->curoff, (VARIANT *)arg);
714 xbuf_resize(buf, size);
715 VARIANT_UserMarshal(&flags, buf->base + buf->curoff, (VARIANT *)arg);
720 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
721 VARIANT_UserFree(&flags, (VARIANT *)arg);
726 if (writeit && debugout) {
728 TRACE_(olerelay)("%s",relaystr((WCHAR*)*arg));
730 TRACE_(olerelay)("<bstr NULL>");
734 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
735 ULONG size = BSTR_UserSize(&flags, buf->curoff, (BSTR *)arg);
736 xbuf_resize(buf, size);
737 BSTR_UserMarshal(&flags, buf->base + buf->curoff, (BSTR *)arg);
742 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
743 BSTR_UserFree(&flags, (BSTR *)arg);
749 BOOL derefhere = TRUE;
751 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
755 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
757 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
760 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
761 switch (tattr->typekind) {
763 if (tattr->tdescAlias.vt == VT_USERDEFINED)
765 DWORD href = tattr->tdescAlias.u.hreftype;
766 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
767 ITypeInfo_Release(tinfo2);
768 hres = ITypeInfo_GetRefTypeInfo(tinfo,href,&tinfo2);
770 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
773 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
774 derefhere = (tattr->typekind != TKIND_DISPATCH && tattr->typekind != TKIND_INTERFACE);
777 case TKIND_ENUM: /* confirmed */
778 case TKIND_RECORD: /* FIXME: mostly untested */
780 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
781 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
785 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
789 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
790 ITypeInfo_Release(tinfo2);
793 if (debugout) TRACE_(olerelay)("*");
794 /* Write always, so the other side knows when it gets a NULL pointer.
796 cookie = *arg ? 0x42424242 : 0;
797 hres = xbuf_add(buf,(LPBYTE)&cookie,sizeof(cookie));
801 if (debugout) TRACE_(olerelay)("NULL");
804 hres = serialize_param(tinfo,writeit,debugout,dealloc,tdesc->u.lptdesc,(DWORD*)*arg,buf);
805 if (derefhere && dealloc) HeapFree(GetProcessHeap(),0,(LPVOID)*arg);
809 if (debugout) TRACE_(olerelay)("unk(0x%x)",*arg);
811 hres = _marshal_interface(buf,&IID_IUnknown,(LPUNKNOWN)*arg);
812 if (dealloc && *(IUnknown **)arg)
813 IUnknown_Release((LPUNKNOWN)*arg);
816 if (debugout) TRACE_(olerelay)("idisp(0x%x)",*arg);
818 hres = _marshal_interface(buf,&IID_IDispatch,(LPUNKNOWN)*arg);
819 if (dealloc && *(IUnknown **)arg)
820 IUnknown_Release((LPUNKNOWN)*arg);
823 if (debugout) TRACE_(olerelay)("<void>");
825 case VT_USERDEFINED: {
829 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
831 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
834 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
835 switch (tattr->typekind) {
837 case TKIND_INTERFACE:
839 hres=_marshal_interface(buf,&(tattr->guid),(LPUNKNOWN)arg);
841 IUnknown_Release((LPUNKNOWN)arg);
845 if (debugout) TRACE_(olerelay)("{");
846 for (i=0;i<tattr->cVars;i++) {
851 hres = ITypeInfo_GetVarDesc(tinfo2, i, &vdesc);
853 ERR("Could not get vardesc of %d\n",i);
856 elem2 = &vdesc->elemdescVar;
857 tdesc2 = &elem2->tdesc;
858 hres = serialize_param(
864 (DWORD*)(((LPBYTE)arg)+vdesc->u.oInst),
867 ITypeInfo_ReleaseVarDesc(tinfo2, vdesc);
870 if (debugout && (i<(tattr->cVars-1)))
871 TRACE_(olerelay)(",");
873 if (debugout) TRACE_(olerelay)("}");
877 hres = serialize_param(tinfo2,writeit,debugout,dealloc,&tattr->tdescAlias,arg,buf);
881 if (debugout) TRACE_(olerelay)("%x",*arg);
883 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
886 FIXME("Unhandled typekind %d\n",tattr->typekind);
890 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
891 ITypeInfo_Release(tinfo2);
895 ARRAYDESC *adesc = tdesc->u.lpadesc;
898 if (debugout) TRACE_(olerelay)("carr");
899 for (i=0;i<adesc->cDims;i++) {
900 if (debugout) TRACE_(olerelay)("[%d]",adesc->rgbounds[i].cElements);
901 arrsize *= adesc->rgbounds[i].cElements;
903 if (debugout) TRACE_(olerelay)("(vt %s)",debugstr_vt(adesc->tdescElem.vt));
904 if (debugout) TRACE_(olerelay)("[");
905 for (i=0;i<arrsize;i++) {
906 LPBYTE base = _passbyref(&adesc->tdescElem, tinfo) ? (LPBYTE) *arg : (LPBYTE) arg;
907 hres = serialize_param(tinfo, writeit, debugout, dealloc, &adesc->tdescElem, (DWORD*)((LPBYTE)base+i*_xsize(&adesc->tdescElem, tinfo)), buf);
910 if (debugout && (i<arrsize-1)) TRACE_(olerelay)(",");
912 if (debugout) TRACE_(olerelay)("]");
914 HeapFree(GetProcessHeap(), 0, *(void **)arg);
920 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
921 ULONG size = LPSAFEARRAY_UserSize(&flags, buf->curoff, (LPSAFEARRAY *)arg);
922 xbuf_resize(buf, size);
923 LPSAFEARRAY_UserMarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
928 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
929 LPSAFEARRAY_UserFree(&flags, (LPSAFEARRAY *)arg);
934 ERR("Unhandled marshal type %d.\n",tdesc->vt);
952 TRACE("vt %s at %p\n",debugstr_vt(tdesc->vt),arg);
955 if ((vartype & 0xf000) == VT_ARRAY)
956 vartype = VT_SAFEARRAY;
963 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
964 unsigned char *buffer;
965 buffer = VARIANT_UserUnmarshal(&flags, buf->base + buf->curoff, (VARIANT *)arg);
966 buf->curoff = buffer - buf->base;
976 hres = xbuf_get(buf,(LPBYTE)arg,8);
977 if (hres) ERR("Failed to read integer 8 byte\n");
979 if (debugout) TRACE_(olerelay)("%x%x",arg[0],arg[1]);
988 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
989 if (hres) ERR("Failed to read integer 4 byte\n");
991 if (debugout) TRACE_(olerelay)("%x",*arg);
998 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
999 if (hres) ERR("Failed to read integer 4 byte\n");
1002 if (debugout) TRACE_(olerelay)("%04x",*arg & 0xffff);
1008 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
1009 if (hres) ERR("Failed to read integer 4 byte\n");
1012 if (debugout) TRACE_(olerelay)("%02x",*arg & 0xff);
1017 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
1018 unsigned char *buffer;
1019 buffer = BSTR_UserUnmarshal(&flags, buf->base + buf->curoff, (BSTR *)arg);
1020 buf->curoff = buffer - buf->base;
1021 if (debugout) TRACE_(olerelay)("%s",debugstr_w(*(BSTR *)arg));
1027 BOOL derefhere = TRUE;
1029 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
1033 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
1035 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
1038 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1039 switch (tattr->typekind) {
1041 if (tattr->tdescAlias.vt == VT_USERDEFINED)
1043 DWORD href = tattr->tdescAlias.u.hreftype;
1044 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
1045 ITypeInfo_Release(tinfo2);
1046 hres = ITypeInfo_GetRefTypeInfo(tinfo,href,&tinfo2);
1048 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
1051 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1052 derefhere = (tattr->typekind != TKIND_DISPATCH && tattr->typekind != TKIND_INTERFACE);
1055 case TKIND_ENUM: /* confirmed */
1056 case TKIND_RECORD: /* FIXME: mostly untested */
1058 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
1059 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
1063 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
1067 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1068 ITypeInfo_Release(tinfo2);
1070 /* read it in all cases, we need to know if we have
1071 * NULL pointer or not.
1073 hres = xbuf_get(buf,(LPBYTE)&cookie,sizeof(cookie));
1075 ERR("Failed to load pointer cookie.\n");
1078 if (cookie != 0x42424242) {
1079 /* we read a NULL ptr from the remote side */
1080 if (debugout) TRACE_(olerelay)("NULL");
1084 if (debugout) TRACE_(olerelay)("*");
1086 /* Allocate space for the referenced struct */
1088 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,_xsize(tdesc->u.lptdesc, tinfo));
1091 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, (LPDWORD)*arg, buf);
1093 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, arg, buf);
1096 /* FIXME: UNKNOWN is unknown ..., but allocate 4 byte for it */
1098 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
1101 hres = _unmarshal_interface(buf,&IID_IUnknown,(LPUNKNOWN*)arg);
1103 TRACE_(olerelay)("unk(%p)",arg);
1108 hres = _unmarshal_interface(buf,&IID_IDispatch,(LPUNKNOWN*)arg);
1110 TRACE_(olerelay)("idisp(%p)",arg);
1113 if (debugout) TRACE_(olerelay)("<void>");
1115 case VT_USERDEFINED: {
1119 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
1121 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
1124 hres = ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1126 ERR("Could not get typeattr in VT_USERDEFINED.\n");
1128 switch (tattr->typekind) {
1129 case TKIND_DISPATCH:
1130 case TKIND_INTERFACE:
1132 hres = _unmarshal_interface(buf,&(tattr->guid),(LPUNKNOWN*)arg);
1134 case TKIND_RECORD: {
1137 if (debugout) TRACE_(olerelay)("{");
1138 for (i=0;i<tattr->cVars;i++) {
1141 hres = ITypeInfo_GetVarDesc(tinfo2, i, &vdesc);
1143 ERR("Could not get vardesc of %d\n",i);
1144 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1145 ITypeInfo_Release(tinfo2);
1148 hres = deserialize_param(
1153 &vdesc->elemdescVar.tdesc,
1154 (DWORD*)(((LPBYTE)arg)+vdesc->u.oInst),
1157 ITypeInfo_ReleaseVarDesc(tinfo2, vdesc);
1158 if (debugout && (i<tattr->cVars-1)) TRACE_(olerelay)(",");
1160 if (debugout) TRACE_(olerelay)("}");
1164 hres = deserialize_param(tinfo2,readit,debugout,alloc,&tattr->tdescAlias,arg,buf);
1168 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
1169 if (hres) ERR("Failed to read enum (4 byte)\n");
1171 if (debugout) TRACE_(olerelay)("%x",*arg);
1174 ERR("Unhandled typekind %d\n",tattr->typekind);
1178 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1181 ERR("failed to stuballoc in TKIND_RECORD.\n");
1182 ITypeInfo_Release(tinfo2);
1186 /* arg is pointing to the start of the array. */
1187 LPBYTE base = (LPBYTE) arg;
1188 ARRAYDESC *adesc = tdesc->u.lpadesc;
1191 if (adesc->cDims > 1) FIXME("cDims > 1 in VT_CARRAY. Does it work?\n");
1192 for (i=0;i<adesc->cDims;i++)
1193 arrsize *= adesc->rgbounds[i].cElements;
1194 if (_passbyref(&adesc->tdescElem, tinfo))
1196 base = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,_xsize(tdesc->u.lptdesc, tinfo) * arrsize);
1197 *arg = (DWORD) base;
1199 for (i=0;i<arrsize;i++)
1206 (DWORD*)(base + i*_xsize(&adesc->tdescElem, tinfo)),
1211 case VT_SAFEARRAY: {
1214 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
1215 unsigned char *buffer;
1216 buffer = LPSAFEARRAY_UserUnmarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
1217 buf->curoff = buffer - buf->base;
1222 ERR("No handler for VT type %d!\n",tdesc->vt);
1228 /* Retrieves a function's funcdesc, searching back into inherited interfaces. */
1229 static HRESULT get_funcdesc(ITypeInfo *tinfo, int iMethod, ITypeInfo **tactual, const FUNCDESC **fdesc,
1230 BSTR *iname, BSTR *fname, UINT *num)
1234 UINT inherited_funcs = 0;
1237 if (fname) *fname = NULL;
1238 if (iname) *iname = NULL;
1242 hr = ITypeInfo_GetTypeAttr(tinfo, &attr);
1245 ERR("GetTypeAttr failed with %x\n",hr);
1249 if(attr->typekind == TKIND_DISPATCH)
1251 if(attr->wTypeFlags & TYPEFLAG_FDUAL)
1256 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, -1, &href);
1259 ERR("Cannot get interface href from dual dispinterface\n");
1260 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1263 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
1266 ERR("Cannot get interface from dual dispinterface\n");
1267 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1270 hr = get_funcdesc(tinfo2, iMethod, tactual, fdesc, iname, fname, num);
1271 ITypeInfo_Release(tinfo2);
1272 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1275 ERR("Shouldn't be called with a non-dual dispinterface\n");
1279 impl_types = attr->cImplTypes;
1280 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1282 for (i = 0; i < impl_types; i++)
1285 ITypeInfo *pSubTypeInfo;
1288 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, i, &href);
1289 if (FAILED(hr)) return hr;
1290 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &pSubTypeInfo);
1291 if (FAILED(hr)) return hr;
1293 hr = get_funcdesc(pSubTypeInfo, iMethod, tactual, fdesc, iname, fname, &sub_funcs);
1294 inherited_funcs += sub_funcs;
1295 ITypeInfo_Release(pSubTypeInfo);
1296 if(SUCCEEDED(hr)) return hr;
1298 if(iMethod < inherited_funcs)
1300 ERR("shouldn't be here\n");
1301 return E_INVALIDARG;
1304 for(i = inherited_funcs; i <= iMethod; i++)
1306 hr = ITypeInfoImpl_GetInternalFuncDesc(tinfo, i - inherited_funcs, fdesc);
1314 /* found it. We don't care about num so zero it */
1317 ITypeInfo_AddRef(*tactual);
1318 if (fname) ITypeInfo_GetDocumentation(tinfo,(*fdesc)->memid,fname,NULL,NULL,NULL);
1319 if (iname) ITypeInfo_GetDocumentation(tinfo,-1,iname,NULL,NULL,NULL);
1323 static inline BOOL is_in_elem(const ELEMDESC *elem)
1325 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN || !elem->u.paramdesc.wParamFlags);
1328 static inline BOOL is_out_elem(const ELEMDESC *elem)
1330 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT || !elem->u.paramdesc.wParamFlags);
1333 static DWORD WINAPI xCall(int method, void **args)
1335 TMProxyImpl *tpinfo = args[0];
1337 const FUNCDESC *fdesc;
1346 DWORD remoteresult = 0;
1348 IRpcChannelBuffer *chanbuf;
1350 EnterCriticalSection(&tpinfo->crit);
1352 hres = get_funcdesc(tpinfo->tinfo,method,&tinfo,&fdesc,&iname,&fname,NULL);
1354 ERR("Did not find typeinfo/funcdesc entry for method %d!\n",method);
1355 LeaveCriticalSection(&tpinfo->crit);
1359 if (!tpinfo->chanbuf)
1361 WARN("Tried to use disconnected proxy\n");
1362 ITypeInfo_Release(tinfo);
1363 LeaveCriticalSection(&tpinfo->crit);
1364 return RPC_E_DISCONNECTED;
1366 chanbuf = tpinfo->chanbuf;
1367 IRpcChannelBuffer_AddRef(chanbuf);
1369 LeaveCriticalSection(&tpinfo->crit);
1371 if (TRACE_ON(olerelay)) {
1372 TRACE_(olerelay)("->");
1374 TRACE_(olerelay)("%s:",relaystr(iname));
1376 TRACE_(olerelay)("%s(%d)",relaystr(fname),method);
1378 TRACE_(olerelay)("%d",method);
1379 TRACE_(olerelay)("(");
1382 SysFreeString(iname);
1383 SysFreeString(fname);
1385 memset(&buf,0,sizeof(buf));
1387 /* normal typelib driven serializing */
1389 /* Need them for hack below */
1390 memset(names,0,sizeof(names));
1391 if (ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames))
1393 if (nrofnames > sizeof(names)/sizeof(names[0]))
1394 ERR("Need more names!\n");
1396 xargs = (DWORD *)(args + 1);
1397 for (i=0;i<fdesc->cParams;i++) {
1398 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1399 if (TRACE_ON(olerelay)) {
1400 if (i) TRACE_(olerelay)(",");
1401 if (i+1<nrofnames && names[i+1])
1402 TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1404 /* No need to marshal other data than FIN and any VT_PTR. */
1405 if (!is_in_elem(elem))
1407 if (elem->tdesc.vt != VT_PTR)
1409 xargs+=_argsize(&elem->tdesc, tinfo);
1410 TRACE_(olerelay)("[out]");
1415 memset( *(void **)xargs, 0, _xsize( elem->tdesc.u.lptdesc, tinfo ) );
1419 hres = serialize_param(
1430 ERR("Failed to serialize param, hres %x\n",hres);
1433 xargs+=_argsize(&elem->tdesc, tinfo);
1435 TRACE_(olerelay)(")");
1437 memset(&msg,0,sizeof(msg));
1438 msg.cbBuffer = buf.curoff;
1439 msg.iMethod = method;
1440 hres = IRpcChannelBuffer_GetBuffer(chanbuf,&msg,&(tpinfo->iid));
1442 ERR("RpcChannelBuffer GetBuffer failed, %x\n",hres);
1445 memcpy(msg.Buffer,buf.base,buf.curoff);
1446 TRACE_(olerelay)("\n");
1447 hres = IRpcChannelBuffer_SendReceive(chanbuf,&msg,&status);
1449 ERR("RpcChannelBuffer SendReceive failed, %x\n",hres);
1453 TRACE_(olerelay)(" status = %08x (",status);
1455 buf.base = HeapReAlloc(GetProcessHeap(),0,buf.base,msg.cbBuffer);
1457 buf.base = HeapAlloc(GetProcessHeap(),0,msg.cbBuffer);
1458 buf.size = msg.cbBuffer;
1459 memcpy(buf.base,msg.Buffer,buf.size);
1462 /* generic deserializer using typelib description */
1463 xargs = (DWORD *)(args + 1);
1465 for (i=0;i<fdesc->cParams;i++) {
1466 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1468 if (i) TRACE_(olerelay)(",");
1469 if (i+1<nrofnames && names[i+1]) TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1471 /* No need to marshal other data than FOUT and any VT_PTR */
1472 if (!is_out_elem(elem) && (elem->tdesc.vt != VT_PTR)) {
1473 xargs += _argsize(&elem->tdesc, tinfo);
1474 TRACE_(olerelay)("[in]");
1477 hres = deserialize_param(
1487 ERR("Failed to unmarshall param, hres %x\n",hres);
1491 xargs += _argsize(&elem->tdesc, tinfo);
1494 hres = xbuf_get(&buf, (LPBYTE)&remoteresult, sizeof(DWORD));
1497 TRACE_(olerelay)(") = %08x\n", remoteresult);
1499 hres = remoteresult;
1502 IRpcChannelBuffer_FreeBuffer(chanbuf,&msg);
1503 for (i = 0; i < nrofnames; i++)
1504 SysFreeString(names[i]);
1505 HeapFree(GetProcessHeap(),0,buf.base);
1506 IRpcChannelBuffer_Release(chanbuf);
1507 ITypeInfo_Release(tinfo);
1508 TRACE("-- 0x%08x\n", hres);
1512 static HRESULT WINAPI ProxyIUnknown_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
1514 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1516 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1518 if (proxy->outerunknown)
1519 return IUnknown_QueryInterface(proxy->outerunknown, riid, ppv);
1521 FIXME("No interface\n");
1522 return E_NOINTERFACE;
1525 static ULONG WINAPI ProxyIUnknown_AddRef(IUnknown *iface)
1527 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1531 if (proxy->outerunknown)
1532 return IUnknown_AddRef(proxy->outerunknown);
1534 return 2; /* FIXME */
1537 static ULONG WINAPI ProxyIUnknown_Release(IUnknown *iface)
1539 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1543 if (proxy->outerunknown)
1544 return IUnknown_Release(proxy->outerunknown);
1546 return 1; /* FIXME */
1549 static HRESULT WINAPI ProxyIDispatch_GetTypeInfoCount(LPDISPATCH iface, UINT * pctinfo)
1551 TMProxyImpl *This = (TMProxyImpl *)iface;
1553 TRACE("(%p)\n", pctinfo);
1555 return IDispatch_GetTypeInfoCount(This->dispatch, pctinfo);
1558 static HRESULT WINAPI ProxyIDispatch_GetTypeInfo(LPDISPATCH iface, UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo)
1560 TMProxyImpl *This = (TMProxyImpl *)iface;
1562 TRACE("(%d, %x, %p)\n", iTInfo, lcid, ppTInfo);
1564 return IDispatch_GetTypeInfo(This->dispatch, iTInfo, lcid, ppTInfo);
1567 static HRESULT WINAPI ProxyIDispatch_GetIDsOfNames(LPDISPATCH iface, REFIID riid, LPOLESTR * rgszNames, UINT cNames, LCID lcid, DISPID * rgDispId)
1569 TMProxyImpl *This = (TMProxyImpl *)iface;
1571 TRACE("(%s, %p, %d, 0x%x, %p)\n", debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);
1573 return IDispatch_GetIDsOfNames(This->dispatch, riid, rgszNames,
1574 cNames, lcid, rgDispId);
1577 static HRESULT WINAPI ProxyIDispatch_Invoke(LPDISPATCH iface, DISPID dispIdMember, REFIID riid, LCID lcid,
1578 WORD wFlags, DISPPARAMS * pDispParams, VARIANT * pVarResult,
1579 EXCEPINFO * pExcepInfo, UINT * puArgErr)
1581 TMProxyImpl *This = (TMProxyImpl *)iface;
1583 TRACE("(%d, %s, 0x%x, 0x%x, %p, %p, %p, %p)\n", dispIdMember,
1584 debugstr_guid(riid), lcid, wFlags, pDispParams, pVarResult,
1585 pExcepInfo, puArgErr);
1587 return IDispatch_Invoke(This->dispatch, dispIdMember, riid, lcid,
1588 wFlags, pDispParams, pVarResult, pExcepInfo,
1594 IRpcChannelBuffer IRpcChannelBuffer_iface;
1596 /* the IDispatch-derived interface we are handling */
1598 IRpcChannelBuffer *pDelegateChannel;
1599 } TMarshalDispatchChannel;
1601 static inline TMarshalDispatchChannel *impl_from_IRpcChannelBuffer(IRpcChannelBuffer *iface)
1603 return CONTAINING_RECORD(iface, TMarshalDispatchChannel, IRpcChannelBuffer_iface);
1606 static HRESULT WINAPI TMarshalDispatchChannel_QueryInterface(IRpcChannelBuffer *iface, REFIID riid, LPVOID *ppv)
1609 if (IsEqualIID(riid,&IID_IRpcChannelBuffer) || IsEqualIID(riid,&IID_IUnknown))
1612 IRpcChannelBuffer_AddRef(iface);
1615 return E_NOINTERFACE;
1618 static ULONG WINAPI TMarshalDispatchChannel_AddRef(LPRPCCHANNELBUFFER iface)
1620 TMarshalDispatchChannel *This = impl_from_IRpcChannelBuffer(iface);
1621 return InterlockedIncrement(&This->refs);
1624 static ULONG WINAPI TMarshalDispatchChannel_Release(LPRPCCHANNELBUFFER iface)
1626 TMarshalDispatchChannel *This = impl_from_IRpcChannelBuffer(iface);
1629 ref = InterlockedDecrement(&This->refs);
1633 IRpcChannelBuffer_Release(This->pDelegateChannel);
1634 HeapFree(GetProcessHeap(), 0, This);
1638 static HRESULT WINAPI TMarshalDispatchChannel_GetBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg, REFIID riid)
1640 TMarshalDispatchChannel *This = impl_from_IRpcChannelBuffer(iface);
1641 TRACE("(%p, %s)\n", olemsg, debugstr_guid(riid));
1642 /* Note: we are pretending to invoke a method on the interface identified
1643 * by tmarshal_iid so that we can re-use the IDispatch proxy/stub code
1644 * without the RPC runtime getting confused by not exporting an IDispatch interface */
1645 return IRpcChannelBuffer_GetBuffer(This->pDelegateChannel, olemsg, &This->tmarshal_iid);
1648 static HRESULT WINAPI TMarshalDispatchChannel_SendReceive(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE *olemsg, ULONG *pstatus)
1650 TMarshalDispatchChannel *This = impl_from_IRpcChannelBuffer(iface);
1651 TRACE("(%p, %p)\n", olemsg, pstatus);
1652 return IRpcChannelBuffer_SendReceive(This->pDelegateChannel, olemsg, pstatus);
1655 static HRESULT WINAPI TMarshalDispatchChannel_FreeBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg)
1657 TMarshalDispatchChannel *This = impl_from_IRpcChannelBuffer(iface);
1658 TRACE("(%p)\n", olemsg);
1659 return IRpcChannelBuffer_FreeBuffer(This->pDelegateChannel, olemsg);
1662 static HRESULT WINAPI TMarshalDispatchChannel_GetDestCtx(LPRPCCHANNELBUFFER iface, DWORD* pdwDestContext, void** ppvDestContext)
1664 TMarshalDispatchChannel *This = impl_from_IRpcChannelBuffer(iface);
1665 TRACE("(%p,%p)\n", pdwDestContext, ppvDestContext);
1666 return IRpcChannelBuffer_GetDestCtx(This->pDelegateChannel, pdwDestContext, ppvDestContext);
1669 static HRESULT WINAPI TMarshalDispatchChannel_IsConnected(LPRPCCHANNELBUFFER iface)
1671 TMarshalDispatchChannel *This = impl_from_IRpcChannelBuffer(iface);
1673 return IRpcChannelBuffer_IsConnected(This->pDelegateChannel);
1676 static const IRpcChannelBufferVtbl TMarshalDispatchChannelVtbl =
1678 TMarshalDispatchChannel_QueryInterface,
1679 TMarshalDispatchChannel_AddRef,
1680 TMarshalDispatchChannel_Release,
1681 TMarshalDispatchChannel_GetBuffer,
1682 TMarshalDispatchChannel_SendReceive,
1683 TMarshalDispatchChannel_FreeBuffer,
1684 TMarshalDispatchChannel_GetDestCtx,
1685 TMarshalDispatchChannel_IsConnected
1688 static HRESULT TMarshalDispatchChannel_Create(
1689 IRpcChannelBuffer *pDelegateChannel, REFIID tmarshal_riid,
1690 IRpcChannelBuffer **ppChannel)
1692 TMarshalDispatchChannel *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1694 return E_OUTOFMEMORY;
1696 This->IRpcChannelBuffer_iface.lpVtbl = &TMarshalDispatchChannelVtbl;
1698 IRpcChannelBuffer_AddRef(pDelegateChannel);
1699 This->pDelegateChannel = pDelegateChannel;
1700 This->tmarshal_iid = *tmarshal_riid;
1702 *ppChannel = &This->IRpcChannelBuffer_iface;
1707 static inline HRESULT get_facbuf_for_iid(REFIID riid, IPSFactoryBuffer **facbuf)
1712 if ((hr = CoGetPSClsid(riid, &clsid)))
1714 return CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL,
1715 &IID_IPSFactoryBuffer, (LPVOID*)facbuf);
1718 static HRESULT init_proxy_entry_point(TMProxyImpl *proxy, unsigned int num)
1721 /* nrofargs including This */
1724 TMAsmProxy *xasm = proxy->asmstubs + num;
1726 const FUNCDESC *fdesc;
1728 hres = get_funcdesc(proxy->tinfo, num, &tinfo2, &fdesc, NULL, NULL, NULL);
1730 ERR("GetFuncDesc %x should not fail here.\n",hres);
1733 ITypeInfo_Release(tinfo2);
1734 /* some args take more than 4 byte on the stack */
1735 for (j=0;j<fdesc->cParams;j++)
1736 nrofargs += _argsize(&fdesc->lprgelemdescParam[j].tdesc, proxy->tinfo);
1739 if (fdesc->callconv != CC_STDCALL) {
1740 ERR("calling convention is not stdcall????\n");
1743 /* leal 4(%esp),%eax
1749 xasm->lealeax = 0x0424448d;
1750 xasm->pushleax = 0x50;
1751 xasm->pushlval = 0x68;
1754 xasm->xcall = (char *)xCall - (char *)&xasm->lret;
1756 xasm->bytestopop = nrofargs * 4;
1758 proxy->lpvtbl[fdesc->oVft / sizeof(void *)] = xasm;
1760 FIXME("not implemented on non i386\n");
1766 static HRESULT WINAPI
1767 PSFacBuf_CreateProxy(
1768 LPPSFACTORYBUFFER iface, IUnknown* pUnkOuter, REFIID riid,
1769 IRpcProxyBuffer **ppProxy, LPVOID *ppv)
1773 unsigned int i, nroffuncs, vtbl_size;
1776 BOOL defer_to_dispatch = FALSE;
1778 TRACE("(...%s...)\n",debugstr_guid(riid));
1779 hres = _get_typeinfo_for_iid(riid,&tinfo);
1781 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
1785 hres = num_of_funcs(tinfo, &nroffuncs, &vtbl_size);
1786 TRACE("Got %d funcs, vtbl size %d\n", nroffuncs, vtbl_size);
1789 ERR("Cannot get number of functions for typeinfo %s\n",debugstr_guid(riid));
1790 ITypeInfo_Release(tinfo);
1794 proxy = CoTaskMemAlloc(sizeof(TMProxyImpl));
1795 if (!proxy) return E_OUTOFMEMORY;
1797 proxy->dispatch = NULL;
1798 proxy->dispatch_proxy = NULL;
1799 proxy->outerunknown = pUnkOuter;
1800 proxy->asmstubs = VirtualAlloc(NULL, sizeof(TMAsmProxy) * nroffuncs, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
1801 if (!proxy->asmstubs) {
1802 ERR("Could not commit pages for proxy thunks\n");
1803 CoTaskMemFree(proxy);
1804 return E_OUTOFMEMORY;
1806 proxy->IRpcProxyBuffer_iface.lpVtbl = &tmproxyvtable;
1807 /* one reference for the proxy */
1809 proxy->tinfo = tinfo;
1813 InitializeCriticalSection(&proxy->crit);
1814 proxy->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TMProxyImpl.crit");
1816 proxy->lpvtbl = HeapAlloc(GetProcessHeap(), 0, vtbl_size);
1818 /* if we derive from IDispatch then defer to its proxy for its methods */
1819 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
1822 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
1824 IPSFactoryBuffer *factory_buffer;
1825 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
1828 hres = IPSFactoryBuffer_CreateProxy(factory_buffer, NULL,
1829 &IID_IDispatch, &proxy->dispatch_proxy,
1830 (void **)&proxy->dispatch);
1831 IPSFactoryBuffer_Release(factory_buffer);
1833 if ((hres == S_OK) && (nroffuncs < 7))
1835 ERR("nroffuncs calculated incorrectly (%d)\n", nroffuncs);
1836 hres = E_UNEXPECTED;
1840 defer_to_dispatch = TRUE;
1843 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
1846 for (i=0;i<nroffuncs;i++) {
1849 proxy->lpvtbl[i] = ProxyIUnknown_QueryInterface;
1852 proxy->lpvtbl[i] = ProxyIUnknown_AddRef;
1855 proxy->lpvtbl[i] = ProxyIUnknown_Release;
1858 if(!defer_to_dispatch) hres = init_proxy_entry_point(proxy, i);
1859 else proxy->lpvtbl[3] = ProxyIDispatch_GetTypeInfoCount;
1862 if(!defer_to_dispatch) hres = init_proxy_entry_point(proxy, i);
1863 else proxy->lpvtbl[4] = ProxyIDispatch_GetTypeInfo;
1866 if(!defer_to_dispatch) hres = init_proxy_entry_point(proxy, i);
1867 else proxy->lpvtbl[5] = ProxyIDispatch_GetIDsOfNames;
1870 if(!defer_to_dispatch) hres = init_proxy_entry_point(proxy, i);
1871 else proxy->lpvtbl[6] = ProxyIDispatch_Invoke;
1874 hres = init_proxy_entry_point(proxy, i);
1881 *ppProxy = &proxy->IRpcProxyBuffer_iface;
1882 IUnknown_AddRef((IUnknown *)*ppv);
1886 TMProxyImpl_Release(&proxy->IRpcProxyBuffer_iface);
1890 typedef struct _TMStubImpl {
1891 IRpcStubBuffer IRpcStubBuffer_iface;
1897 IRpcStubBuffer *dispatch_stub;
1898 BOOL dispatch_derivative;
1901 static inline TMStubImpl *impl_from_IRpcStubBuffer(IRpcStubBuffer *iface)
1903 return CONTAINING_RECORD(iface, TMStubImpl, IRpcStubBuffer_iface);
1906 static HRESULT WINAPI
1907 TMStubImpl_QueryInterface(LPRPCSTUBBUFFER iface, REFIID riid, LPVOID *ppv)
1909 if (IsEqualIID(riid,&IID_IRpcStubBuffer)||IsEqualIID(riid,&IID_IUnknown)){
1911 IRpcStubBuffer_AddRef(iface);
1914 FIXME("%s, not supported IID.\n",debugstr_guid(riid));
1915 return E_NOINTERFACE;
1919 TMStubImpl_AddRef(LPRPCSTUBBUFFER iface)
1921 TMStubImpl *This = impl_from_IRpcStubBuffer(iface);
1922 ULONG refCount = InterlockedIncrement(&This->ref);
1924 TRACE("(%p)->(ref before=%u)\n", This, refCount - 1);
1930 TMStubImpl_Release(LPRPCSTUBBUFFER iface)
1932 TMStubImpl *This = impl_from_IRpcStubBuffer(iface);
1933 ULONG refCount = InterlockedDecrement(&This->ref);
1935 TRACE("(%p)->(ref before=%u)\n", This, refCount + 1);
1939 IRpcStubBuffer_Disconnect(iface);
1940 ITypeInfo_Release(This->tinfo);
1941 if (This->dispatch_stub)
1942 IRpcStubBuffer_Release(This->dispatch_stub);
1943 CoTaskMemFree(This);
1948 static HRESULT WINAPI
1949 TMStubImpl_Connect(LPRPCSTUBBUFFER iface, LPUNKNOWN pUnkServer)
1951 TMStubImpl *This = impl_from_IRpcStubBuffer(iface);
1953 TRACE("(%p)->(%p)\n", This, pUnkServer);
1955 IUnknown_AddRef(pUnkServer);
1956 This->pUnk = pUnkServer;
1958 if (This->dispatch_stub)
1959 IRpcStubBuffer_Connect(This->dispatch_stub, pUnkServer);
1965 TMStubImpl_Disconnect(LPRPCSTUBBUFFER iface)
1967 TMStubImpl *This = impl_from_IRpcStubBuffer(iface);
1969 TRACE("(%p)->()\n", This);
1973 IUnknown_Release(This->pUnk);
1977 if (This->dispatch_stub)
1978 IRpcStubBuffer_Disconnect(This->dispatch_stub);
1981 static HRESULT WINAPI
1983 LPRPCSTUBBUFFER iface, RPCOLEMESSAGE* xmsg,IRpcChannelBuffer*rpcchanbuf)
1987 const FUNCDESC *fdesc;
1988 TMStubImpl *This = impl_from_IRpcStubBuffer(iface);
1990 DWORD *args = NULL, res, *xargs, nrofargs;
1995 ITypeInfo *tinfo = NULL;
1999 if (xmsg->iMethod < 3) {
2000 ERR("IUnknown methods cannot be marshaled by the typelib marshaler\n");
2001 return E_UNEXPECTED;
2004 if (This->dispatch_derivative && xmsg->iMethod < sizeof(IDispatchVtbl)/sizeof(void *))
2006 if (!This->dispatch_stub)
2008 IPSFactoryBuffer *factory_buffer;
2009 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
2012 hres = IPSFactoryBuffer_CreateStub(factory_buffer, &IID_IDispatch,
2013 This->pUnk, &This->dispatch_stub);
2014 IPSFactoryBuffer_Release(factory_buffer);
2019 return IRpcStubBuffer_Invoke(This->dispatch_stub, xmsg, rpcchanbuf);
2022 memset(&buf,0,sizeof(buf));
2023 buf.size = xmsg->cbBuffer;
2024 buf.base = HeapAlloc(GetProcessHeap(), 0, xmsg->cbBuffer);
2025 memcpy(buf.base, xmsg->Buffer, xmsg->cbBuffer);
2028 hres = get_funcdesc(This->tinfo,xmsg->iMethod,&tinfo,&fdesc,&iname,NULL,NULL);
2030 ERR("GetFuncDesc on method %d failed with %x\n",xmsg->iMethod,hres);
2034 if (iname && !lstrcmpW(iname, IDispatchW))
2036 ERR("IDispatch cannot be marshaled by the typelib marshaler\n");
2037 hres = E_UNEXPECTED;
2038 SysFreeString (iname);
2042 SysFreeString (iname);
2044 /* Need them for hack below */
2045 memset(names,0,sizeof(names));
2046 ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames);
2047 if (nrofnames > sizeof(names)/sizeof(names[0])) {
2048 ERR("Need more names!\n");
2051 /*dump_FUNCDESC(fdesc);*/
2053 for (i=0;i<fdesc->cParams;i++)
2054 nrofargs += _argsize(&fdesc->lprgelemdescParam[i].tdesc, tinfo);
2055 args = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(nrofargs+1)*sizeof(DWORD));
2058 hres = E_OUTOFMEMORY;
2062 /* Allocate all stuff used by call. */
2064 for (i=0;i<fdesc->cParams;i++) {
2065 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2067 hres = deserialize_param(
2076 xargs += _argsize(&elem->tdesc, tinfo);
2078 ERR("Failed to deserialize param %s, hres %x\n",relaystr(names[i+1]),hres);
2083 args[0] = (DWORD)This->pUnk;
2088 (*((FARPROC**)args[0]))[fdesc->oVft/4],
2096 DWORD dwExceptionCode = GetExceptionCode();
2097 ERR("invoke call failed with exception 0x%08x (%d)\n", dwExceptionCode, dwExceptionCode);
2098 if (FAILED(dwExceptionCode))
2099 hres = dwExceptionCode;
2101 hres = HRESULT_FROM_WIN32(dwExceptionCode);
2111 for (i=0;i<fdesc->cParams;i++) {
2112 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2113 hres = serialize_param(
2122 xargs += _argsize(&elem->tdesc, tinfo);
2124 ERR("Failed to stuballoc param, hres %x\n",hres);
2129 hres = xbuf_add (&buf, (LPBYTE)&res, sizeof(DWORD));
2134 xmsg->cbBuffer = buf.curoff;
2135 hres = IRpcChannelBuffer_GetBuffer(rpcchanbuf, xmsg, &This->iid);
2137 ERR("IRpcChannelBuffer_GetBuffer failed with error 0x%08x\n", hres);
2140 memcpy(xmsg->Buffer, buf.base, buf.curoff);
2143 for (i = 0; i < nrofnames; i++)
2144 SysFreeString(names[i]);
2146 ITypeInfo_Release(tinfo);
2147 HeapFree(GetProcessHeap(), 0, args);
2149 HeapFree(GetProcessHeap(), 0, buf.base);
2151 TRACE("returning\n");
2154 FIXME( "not implemented on non-i386\n" );
2159 static LPRPCSTUBBUFFER WINAPI
2160 TMStubImpl_IsIIDSupported(LPRPCSTUBBUFFER iface, REFIID riid) {
2161 FIXME("Huh (%s)?\n",debugstr_guid(riid));
2166 TMStubImpl_CountRefs(LPRPCSTUBBUFFER iface) {
2167 TMStubImpl *This = impl_from_IRpcStubBuffer(iface);
2170 return This->ref; /*FIXME? */
2173 static HRESULT WINAPI
2174 TMStubImpl_DebugServerQueryInterface(LPRPCSTUBBUFFER iface, LPVOID *ppv) {
2179 TMStubImpl_DebugServerRelease(LPRPCSTUBBUFFER iface, LPVOID ppv) {
2183 static const IRpcStubBufferVtbl tmstubvtbl = {
2184 TMStubImpl_QueryInterface,
2188 TMStubImpl_Disconnect,
2190 TMStubImpl_IsIIDSupported,
2191 TMStubImpl_CountRefs,
2192 TMStubImpl_DebugServerQueryInterface,
2193 TMStubImpl_DebugServerRelease
2196 static HRESULT WINAPI
2197 PSFacBuf_CreateStub(
2198 LPPSFACTORYBUFFER iface, REFIID riid,IUnknown *pUnkServer,
2199 IRpcStubBuffer** ppStub
2206 TRACE("(%s,%p,%p)\n",debugstr_guid(riid),pUnkServer,ppStub);
2208 hres = _get_typeinfo_for_iid(riid,&tinfo);
2210 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
2214 stub = CoTaskMemAlloc(sizeof(TMStubImpl));
2216 return E_OUTOFMEMORY;
2217 stub->IRpcStubBuffer_iface.lpVtbl = &tmstubvtbl;
2219 stub->tinfo = tinfo;
2220 stub->dispatch_stub = NULL;
2221 stub->dispatch_derivative = FALSE;
2223 hres = IRpcStubBuffer_Connect(&stub->IRpcStubBuffer_iface,pUnkServer);
2224 *ppStub = &stub->IRpcStubBuffer_iface;
2225 TRACE("IRpcStubBuffer: %p\n", stub);
2227 ERR("Connect to pUnkServer failed?\n");
2229 /* if we derive from IDispatch then defer to its stub for some of its methods */
2230 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
2233 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
2234 stub->dispatch_derivative = TRUE;
2235 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
2241 static const IPSFactoryBufferVtbl psfacbufvtbl = {
2242 PSFacBuf_QueryInterface,
2245 PSFacBuf_CreateProxy,
2249 /* This is the whole PSFactoryBuffer object, just the vtableptr */
2250 static const IPSFactoryBufferVtbl *lppsfac = &psfacbufvtbl;
2252 /***********************************************************************
2253 * TMARSHAL_DllGetClassObject
2255 HRESULT TMARSHAL_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
2257 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)) {
2261 return E_NOINTERFACE;