4 * Copyright 2002,2005 Marcus Meissner
6 * The olerelay debug channel allows you to see calls marshalled by
7 * the typelib marshaller. It is not a generic COM relaying system.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
34 #define NONAMELESSUNION
35 #define NONAMELESSSTRUCT
45 #include "propidl.h" /* for LPSAFEARRAY_User* functions */
48 #include "wine/debug.h"
49 #include "wine/exception.h"
51 static const WCHAR IDispatchW[] = { 'I','D','i','s','p','a','t','c','h',0};
53 WINE_DEFAULT_DEBUG_CHANNEL(ole);
54 WINE_DECLARE_DEBUG_CHANNEL(olerelay);
56 #define ICOM_THIS_MULTI(impl,field,iface) impl* const This=(impl*)((char*)(iface) - offsetof(impl,field))
58 static HRESULT TMarshalDispatchChannel_Create(
59 IRpcChannelBuffer *pDelegateChannel, REFIID tmarshal_riid,
60 IRpcChannelBuffer **ppChannel);
62 typedef struct _marshal_state {
68 /* used in the olerelay code to avoid having the L"" stuff added by debugstr_w */
69 static char *relaystr(WCHAR *in) {
70 char *tmp = (char *)debugstr_w(in);
72 tmp[strlen(tmp)-1] = '\0';
77 xbuf_resize(marshal_state *buf, DWORD newsize)
79 if(buf->size >= newsize)
84 buf->base = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buf->base, newsize);
90 buf->base = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, newsize);
99 xbuf_add(marshal_state *buf, const BYTE *stuff, DWORD size)
103 if(buf->size - buf->curoff < size)
105 hr = xbuf_resize(buf, buf->size + size + 100);
106 if(FAILED(hr)) return hr;
108 memcpy(buf->base+buf->curoff,stuff,size);
114 xbuf_get(marshal_state *buf, LPBYTE stuff, DWORD size) {
115 if (buf->size < buf->curoff+size) return E_FAIL;
116 memcpy(stuff,buf->base+buf->curoff,size);
122 xbuf_skip(marshal_state *buf, DWORD size) {
123 if (buf->size < buf->curoff+size) return E_FAIL;
129 _unmarshal_interface(marshal_state *buf, REFIID riid, LPUNKNOWN *pUnk) {
131 ULARGE_INTEGER newpos;
132 LARGE_INTEGER seekto;
137 TRACE("...%s...\n",debugstr_guid(riid));
140 hres = xbuf_get(buf,(LPBYTE)&xsize,sizeof(xsize));
142 ERR("xbuf_get failed\n");
146 if (xsize == 0) return S_OK;
148 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
150 ERR("Stream create failed %x\n",hres);
154 hres = IStream_Write(pStm,buf->base+buf->curoff,xsize,&res);
156 ERR("stream write %x\n",hres);
160 memset(&seekto,0,sizeof(seekto));
161 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
163 ERR("Failed Seek %x\n",hres);
167 hres = CoUnmarshalInterface(pStm,riid,(LPVOID*)pUnk);
169 ERR("Unmarshalling interface %s failed with %x\n",debugstr_guid(riid),hres);
173 IStream_Release(pStm);
174 return xbuf_skip(buf,xsize);
178 _marshal_interface(marshal_state *buf, REFIID riid, LPUNKNOWN pUnk) {
179 LPBYTE tempbuf = NULL;
180 IStream *pStm = NULL;
182 ULARGE_INTEGER newpos;
183 LARGE_INTEGER seekto;
189 /* this is valid, if for instance we serialize
190 * a VT_DISPATCH with NULL ptr which apparently
191 * can happen. S_OK to make sure we continue
194 WARN("pUnk is NULL\n");
196 return xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
201 TRACE("...%s...\n",debugstr_guid(riid));
203 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
205 ERR("Stream create failed %x\n",hres);
209 hres = CoMarshalInterface(pStm,riid,pUnk,0,NULL,0);
211 ERR("Marshalling interface %s failed with %x\n", debugstr_guid(riid), hres);
215 hres = IStream_Stat(pStm,&ststg,0);
217 ERR("Stream stat failed\n");
221 tempbuf = HeapAlloc(GetProcessHeap(), 0, ststg.cbSize.u.LowPart);
222 memset(&seekto,0,sizeof(seekto));
223 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
225 ERR("Failed Seek %x\n",hres);
229 hres = IStream_Read(pStm,tempbuf,ststg.cbSize.u.LowPart,&res);
231 ERR("Failed Read %x\n",hres);
235 xsize = ststg.cbSize.u.LowPart;
236 xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
237 hres = xbuf_add(buf,tempbuf,ststg.cbSize.u.LowPart);
239 HeapFree(GetProcessHeap(),0,tempbuf);
240 IStream_Release(pStm);
246 xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
247 if (pStm) IUnknown_Release(pStm);
248 HeapFree(GetProcessHeap(), 0, tempbuf);
252 /********************* OLE Proxy/Stub Factory ********************************/
253 static HRESULT WINAPI
254 PSFacBuf_QueryInterface(LPPSFACTORYBUFFER iface, REFIID iid, LPVOID *ppv) {
255 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)||IsEqualIID(iid,&IID_IUnknown)) {
256 *ppv = (LPVOID)iface;
257 /* No ref counting, static class */
260 FIXME("(%s) unknown IID?\n",debugstr_guid(iid));
261 return E_NOINTERFACE;
264 static ULONG WINAPI PSFacBuf_AddRef(LPPSFACTORYBUFFER iface) { return 2; }
265 static ULONG WINAPI PSFacBuf_Release(LPPSFACTORYBUFFER iface) { return 1; }
268 _get_typeinfo_for_iid(REFIID riid, ITypeInfo**ti) {
271 char tlguid[200],typelibkey[300],interfacekey[300],ver[100];
274 DWORD tlguidlen, verlen, type;
278 sprintf( interfacekey, "Interface\\{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\Typelib",
279 riid->Data1, riid->Data2, riid->Data3,
280 riid->Data4[0], riid->Data4[1], riid->Data4[2], riid->Data4[3],
281 riid->Data4[4], riid->Data4[5], riid->Data4[6], riid->Data4[7]
284 if (RegOpenKeyA(HKEY_CLASSES_ROOT,interfacekey,&ikey)) {
285 ERR("No %s key found.\n",interfacekey);
288 tlguidlen = sizeof(tlguid);
289 if (RegQueryValueExA(ikey,NULL,NULL,&type,(LPBYTE)tlguid,&tlguidlen)) {
290 ERR("Getting typelib guid failed.\n");
294 verlen = sizeof(ver);
295 if (RegQueryValueExA(ikey,"Version",NULL,&type,(LPBYTE)ver,&verlen)) {
296 ERR("Could not get version value?\n");
301 sprintf(typelibkey,"Typelib\\%s\\%s\\0\\win32",tlguid,ver);
302 tlfnlen = sizeof(tlfn);
303 if (RegQueryValueA(HKEY_CLASSES_ROOT,typelibkey,tlfn,&tlfnlen)) {
304 ERR("Could not get typelib fn?\n");
307 MultiByteToWideChar(CP_ACP, 0, tlfn, -1, tlfnW, sizeof(tlfnW) / sizeof(tlfnW[0]));
308 hres = LoadTypeLib(tlfnW,&tl);
310 ERR("Failed to load typelib for %s, but it should be there.\n",debugstr_guid(riid));
313 hres = ITypeLib_GetTypeInfoOfGuid(tl,riid,ti);
315 ERR("typelib does not contain info for %s?\n",debugstr_guid(riid));
316 ITypeLib_Release(tl);
319 ITypeLib_Release(tl);
324 * Determine the number of functions including all inherited functions.
325 * Note for non-dual dispinterfaces we simply return the size of IDispatch.
327 static HRESULT num_of_funcs(ITypeInfo *tinfo, unsigned int *num)
334 hres = ITypeInfo_GetTypeAttr(tinfo, &attr);
336 ERR("GetTypeAttr failed with %x\n",hres);
340 if(attr->typekind == TKIND_DISPATCH && (attr->wTypeFlags & TYPEFLAG_FDUAL))
343 hres = ITypeInfo_GetRefTypeOfImplType(tinfo, -1, &href);
346 ERR("Unable to get interface href from dual dispinterface\n");
349 hres = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
352 ERR("Unable to get interface from dual dispinterface\n");
355 hres = num_of_funcs(tinfo2, num);
356 ITypeInfo_Release(tinfo2);
360 *num = attr->cbSizeVft / 4;
364 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
370 #include "pshpack1.h"
372 typedef struct _TMAsmProxy {
387 # warning You need to implement stubless proxies for your architecture
388 typedef struct _TMAsmProxy {
392 typedef struct _TMProxyImpl {
394 const IRpcProxyBufferVtbl *lpvtbl2;
397 TMAsmProxy *asmstubs;
399 IRpcChannelBuffer* chanbuf;
401 CRITICAL_SECTION crit;
402 IUnknown *outerunknown;
404 IRpcProxyBuffer *dispatch_proxy;
407 static HRESULT WINAPI
408 TMProxyImpl_QueryInterface(LPRPCPROXYBUFFER iface, REFIID riid, LPVOID *ppv)
411 if (IsEqualIID(riid,&IID_IUnknown)||IsEqualIID(riid,&IID_IRpcProxyBuffer)) {
412 *ppv = (LPVOID)iface;
413 IRpcProxyBuffer_AddRef(iface);
416 FIXME("no interface for %s\n",debugstr_guid(riid));
417 return E_NOINTERFACE;
421 TMProxyImpl_AddRef(LPRPCPROXYBUFFER iface)
423 ICOM_THIS_MULTI(TMProxyImpl,lpvtbl2,iface);
424 ULONG refCount = InterlockedIncrement(&This->ref);
426 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
432 TMProxyImpl_Release(LPRPCPROXYBUFFER iface)
434 ICOM_THIS_MULTI(TMProxyImpl,lpvtbl2,iface);
435 ULONG refCount = InterlockedDecrement(&This->ref);
437 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
441 if (This->dispatch_proxy) IRpcProxyBuffer_Release(This->dispatch_proxy);
442 This->crit.DebugInfo->Spare[0] = 0;
443 DeleteCriticalSection(&This->crit);
444 if (This->chanbuf) IRpcChannelBuffer_Release(This->chanbuf);
445 VirtualFree(This->asmstubs, 0, MEM_RELEASE);
446 HeapFree(GetProcessHeap(), 0, This->lpvtbl);
447 ITypeInfo_Release(This->tinfo);
453 static HRESULT WINAPI
455 LPRPCPROXYBUFFER iface,IRpcChannelBuffer* pRpcChannelBuffer)
457 ICOM_THIS_MULTI(TMProxyImpl, lpvtbl2, iface);
459 TRACE("(%p)\n", pRpcChannelBuffer);
461 EnterCriticalSection(&This->crit);
463 IRpcChannelBuffer_AddRef(pRpcChannelBuffer);
464 This->chanbuf = pRpcChannelBuffer;
466 LeaveCriticalSection(&This->crit);
468 if (This->dispatch_proxy)
470 IRpcChannelBuffer *pDelegateChannel;
471 HRESULT hr = TMarshalDispatchChannel_Create(pRpcChannelBuffer, &This->iid, &pDelegateChannel);
474 hr = IRpcProxyBuffer_Connect(This->dispatch_proxy, pDelegateChannel);
475 IRpcChannelBuffer_Release(pDelegateChannel);
483 TMProxyImpl_Disconnect(LPRPCPROXYBUFFER iface)
485 ICOM_THIS_MULTI(TMProxyImpl, lpvtbl2, iface);
489 EnterCriticalSection(&This->crit);
491 IRpcChannelBuffer_Release(This->chanbuf);
492 This->chanbuf = NULL;
494 LeaveCriticalSection(&This->crit);
496 if (This->dispatch_proxy)
497 IRpcProxyBuffer_Disconnect(This->dispatch_proxy);
501 static const IRpcProxyBufferVtbl tmproxyvtable = {
502 TMProxyImpl_QueryInterface,
506 TMProxyImpl_Disconnect
509 /* how much space do we use on stack in DWORD steps. */
514 return 8/sizeof(DWORD);
516 return sizeof(double)/sizeof(DWORD);
518 return sizeof(CY)/sizeof(DWORD);
520 return sizeof(DATE)/sizeof(DWORD);
522 return (sizeof(VARIANT)+3)/sizeof(DWORD);
529 _xsize(const TYPEDESC *td) {
534 return sizeof(VARIANT)+3;
537 const ARRAYDESC *adesc = td->u.lpadesc;
539 for (i=0;i<adesc->cDims;i++)
540 arrsize *= adesc->rgbounds[i].cElements;
541 return arrsize*_xsize(&adesc->tdescElem);
569 TRACE("(tdesc.vt %s)\n",debugstr_vt(tdesc->vt));
572 case VT_EMPTY: /* nothing. empty variant for instance */
579 if (debugout) TRACE_(olerelay)("%x%x\n",arg[0],arg[1]);
581 hres = xbuf_add(buf,(LPBYTE)arg,8);
591 if (debugout) TRACE_(olerelay)("%x\n",*arg);
593 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
598 if (debugout) TRACE_(olerelay)("%04x\n",*arg & 0xffff);
600 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
605 if (debugout) TRACE_(olerelay)("%02x\n",*arg & 0xff);
607 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
611 if (debugout) TRACE_(olerelay)("&0x%x\n",*arg);
613 hres = xbuf_add(buf,(LPBYTE)(DWORD*)*arg,sizeof(DWORD));
614 /* do not dealloc at this time */
618 VARIANT *vt = (VARIANT*)arg;
619 DWORD vttype = V_VT(vt);
621 if (debugout) TRACE_(olerelay)("Vt(%s%s)(",debugstr_vt(vttype),debugstr_vf(vttype));
624 hres = xbuf_add(buf,(LPBYTE)&vttype,sizeof(vttype));
625 if (hres) return hres;
627 /* need to recurse since we need to free the stuff */
628 hres = serialize_param(tinfo,writeit,debugout,dealloc,&tdesc2,(DWORD*)&(V_I4(vt)),buf);
629 if (debugout) TRACE_(olerelay)(")");
632 case VT_BSTR|VT_BYREF: {
633 if (debugout) TRACE_(olerelay)("[byref]'%s'", *(BSTR*)*arg ? relaystr(*((BSTR*)*arg)) : "<bstr NULL>");
635 /* ptr to ptr to magic widestring, basically */
636 BSTR *bstr = (BSTR *) *arg;
639 /* -1 means "null string" which is equivalent to empty string */
641 hres = xbuf_add(buf, (LPBYTE)&len,sizeof(DWORD));
642 if (hres) return hres;
644 len = *((DWORD*)*bstr-1)/sizeof(WCHAR);
645 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
646 if (hres) return hres;
647 hres = xbuf_add(buf,(LPBYTE)*bstr,len * sizeof(WCHAR));
648 if (hres) return hres;
652 if (dealloc && arg) {
653 BSTR *str = *((BSTR **)arg);
662 TRACE_(olerelay)("%s",relaystr((WCHAR*)*arg));
664 TRACE_(olerelay)("<bstr NULL>");
667 BSTR bstr = (BSTR)*arg;
671 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
672 if (hres) return hres;
674 len = *((DWORD*)bstr-1)/sizeof(WCHAR);
675 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
676 if (hres) return hres;
677 hres = xbuf_add(buf,(LPBYTE)bstr,len * sizeof(WCHAR));
678 if (hres) return hres;
683 SysFreeString((BSTR)*arg);
688 BOOL derefhere = TRUE;
690 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
694 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
696 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
699 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
700 switch (tattr->typekind) {
702 if (tattr->tdescAlias.vt == VT_USERDEFINED)
704 DWORD href = tattr->tdescAlias.u.hreftype;
705 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
706 ITypeInfo_Release(tinfo2);
707 hres = ITypeInfo_GetRefTypeInfo(tinfo,href,&tinfo2);
709 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
712 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
713 derefhere = (tattr->typekind != TKIND_DISPATCH && tattr->typekind != TKIND_INTERFACE);
716 case TKIND_ENUM: /* confirmed */
717 case TKIND_RECORD: /* FIXME: mostly untested */
719 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
720 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
724 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
728 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
729 ITypeInfo_Release(tinfo2);
732 if (debugout) TRACE_(olerelay)("*");
733 /* Write always, so the other side knows when it gets a NULL pointer.
735 cookie = *arg ? 0x42424242 : 0;
736 hres = xbuf_add(buf,(LPBYTE)&cookie,sizeof(cookie));
740 if (debugout) TRACE_(olerelay)("NULL");
743 hres = serialize_param(tinfo,writeit,debugout,dealloc,tdesc->u.lptdesc,(DWORD*)*arg,buf);
744 if (derefhere && dealloc) HeapFree(GetProcessHeap(),0,(LPVOID)*arg);
748 if (debugout) TRACE_(olerelay)("unk(0x%x)",*arg);
750 hres = _marshal_interface(buf,&IID_IUnknown,(LPUNKNOWN)*arg);
751 if (dealloc && *(IUnknown **)arg)
752 IUnknown_Release((LPUNKNOWN)*arg);
755 if (debugout) TRACE_(olerelay)("idisp(0x%x)",*arg);
757 hres = _marshal_interface(buf,&IID_IDispatch,(LPUNKNOWN)*arg);
758 if (dealloc && *(IUnknown **)arg)
759 IUnknown_Release((LPUNKNOWN)*arg);
762 if (debugout) TRACE_(olerelay)("<void>");
764 case VT_USERDEFINED: {
768 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
770 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
773 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
774 switch (tattr->typekind) {
776 case TKIND_INTERFACE:
778 hres=_marshal_interface(buf,&(tattr->guid),(LPUNKNOWN)arg);
780 IUnknown_Release((LPUNKNOWN)arg);
784 if (debugout) TRACE_(olerelay)("{");
785 for (i=0;i<tattr->cVars;i++) {
790 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
792 ERR("Could not get vardesc of %d\n",i);
795 elem2 = &vdesc->elemdescVar;
796 tdesc2 = &elem2->tdesc;
797 hres = serialize_param(
803 (DWORD*)(((LPBYTE)arg)+vdesc->u.oInst),
806 ITypeInfo_ReleaseVarDesc(tinfo2, vdesc);
809 if (debugout && (i<(tattr->cVars-1)))
810 TRACE_(olerelay)(",");
812 if (debugout) TRACE_(olerelay)("}");
816 hres = serialize_param(tinfo2,writeit,debugout,dealloc,&tattr->tdescAlias,arg,buf);
820 if (debugout) TRACE_(olerelay)("%x",*arg);
822 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
825 FIXME("Unhandled typekind %d\n",tattr->typekind);
829 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
830 ITypeInfo_Release(tinfo2);
834 ARRAYDESC *adesc = tdesc->u.lpadesc;
837 if (debugout) TRACE_(olerelay)("carr");
838 for (i=0;i<adesc->cDims;i++) {
839 if (debugout) TRACE_(olerelay)("[%d]",adesc->rgbounds[i].cElements);
840 arrsize *= adesc->rgbounds[i].cElements;
842 if (debugout) TRACE_(olerelay)("(vt %s)",debugstr_vt(adesc->tdescElem.vt));
843 if (debugout) TRACE_(olerelay)("[");
844 for (i=0;i<arrsize;i++) {
845 hres = serialize_param(tinfo, writeit, debugout, dealloc, &adesc->tdescElem, (DWORD*)((LPBYTE)arg+i*_xsize(&adesc->tdescElem)), buf);
848 if (debugout && (i<arrsize-1)) TRACE_(olerelay)(",");
850 if (debugout) TRACE_(olerelay)("]");
856 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
857 ULONG size = LPSAFEARRAY_UserSize(&flags, buf->curoff, (LPSAFEARRAY *)arg);
858 xbuf_resize(buf, size);
859 LPSAFEARRAY_UserMarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
865 ERR("Unhandled marshal type %d.\n",tdesc->vt);
882 TRACE("vt %s at %p\n",debugstr_vt(tdesc->vt),arg);
887 if (debugout) TRACE_(olerelay)("<empty>\n");
890 if (debugout) TRACE_(olerelay)("<null>\n");
893 VARIANT *vt = (VARIANT*)arg;
898 hres = xbuf_get(buf,(LPBYTE)&vttype,sizeof(vttype));
900 FIXME("vt type not read?\n");
903 memset(&tdesc2,0,sizeof(tdesc2));
906 if (debugout) TRACE_(olerelay)("Vt(%s%s)(",debugstr_vt(vttype),debugstr_vf(vttype));
907 hres = deserialize_param(tinfo, readit, debugout, alloc, &tdesc2, (DWORD*)&(V_I4(vt)), buf);
908 TRACE_(olerelay)(")");
920 hres = xbuf_get(buf,(LPBYTE)arg,8);
921 if (hres) ERR("Failed to read integer 8 byte\n");
923 if (debugout) TRACE_(olerelay)("%x%x",arg[0],arg[1]);
933 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
934 if (hres) ERR("Failed to read integer 4 byte\n");
936 if (debugout) TRACE_(olerelay)("%x",*arg);
942 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
943 if (hres) ERR("Failed to read integer 4 byte\n");
946 if (debugout) TRACE_(olerelay)("%04x",*arg & 0xffff);
952 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
953 if (hres) ERR("Failed to read integer 4 byte\n");
956 if (debugout) TRACE_(olerelay)("%02x",*arg & 0xff);
961 *arg = (DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
963 hres = xbuf_get(buf,(LPBYTE)*arg,sizeof(DWORD));
964 if (hres) ERR("Failed to read integer 4 byte\n");
966 if (debugout) TRACE_(olerelay)("&0x%x",*(DWORD*)*arg);
968 case VT_BSTR|VT_BYREF: {
969 BSTR **bstr = (BSTR **)arg;
974 hres = xbuf_get(buf,(LPBYTE)&len,sizeof(DWORD));
976 ERR("failed to read bstr klen\n");
980 *bstr = CoTaskMemAlloc(sizeof(BSTR *));
982 if (debugout) TRACE_(olerelay)("<bstr NULL>");
984 str = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(len+1)*sizeof(WCHAR));
985 hres = xbuf_get(buf,(LPBYTE)str,len*sizeof(WCHAR));
987 ERR("Failed to read BSTR.\n");
988 HeapFree(GetProcessHeap(),0,str);
991 *bstr = CoTaskMemAlloc(sizeof(BSTR *));
992 **bstr = SysAllocStringLen(str,len);
993 if (debugout) TRACE_(olerelay)("%s",relaystr(str));
994 HeapFree(GetProcessHeap(),0,str);
1006 hres = xbuf_get(buf,(LPBYTE)&len,sizeof(DWORD));
1008 ERR("failed to read bstr klen\n");
1013 if (debugout) TRACE_(olerelay)("<bstr NULL>");
1015 str = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(len+1)*sizeof(WCHAR));
1016 hres = xbuf_get(buf,(LPBYTE)str,len*sizeof(WCHAR));
1018 ERR("Failed to read BSTR.\n");
1019 HeapFree(GetProcessHeap(),0,str);
1022 *arg = (DWORD)SysAllocStringLen(str,len);
1023 if (debugout) TRACE_(olerelay)("%s",relaystr(str));
1024 HeapFree(GetProcessHeap(),0,str);
1033 BOOL derefhere = TRUE;
1035 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
1039 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
1041 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
1044 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1045 switch (tattr->typekind) {
1047 if (tattr->tdescAlias.vt == VT_USERDEFINED)
1049 DWORD href = tattr->tdescAlias.u.hreftype;
1050 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
1051 ITypeInfo_Release(tinfo2);
1052 hres = ITypeInfo_GetRefTypeInfo(tinfo,href,&tinfo2);
1054 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
1057 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1058 derefhere = (tattr->typekind != TKIND_DISPATCH && tattr->typekind != TKIND_INTERFACE);
1061 case TKIND_ENUM: /* confirmed */
1062 case TKIND_RECORD: /* FIXME: mostly untested */
1064 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
1065 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
1069 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
1073 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1074 ITypeInfo_Release(tinfo2);
1076 /* read it in all cases, we need to know if we have
1077 * NULL pointer or not.
1079 hres = xbuf_get(buf,(LPBYTE)&cookie,sizeof(cookie));
1081 ERR("Failed to load pointer cookie.\n");
1084 if (cookie != 0x42424242) {
1085 /* we read a NULL ptr from the remote side */
1086 if (debugout) TRACE_(olerelay)("NULL");
1090 if (debugout) TRACE_(olerelay)("*");
1092 /* Allocate space for the referenced struct */
1094 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,_xsize(tdesc->u.lptdesc));
1097 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, (LPDWORD)*arg, buf);
1099 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, arg, buf);
1102 /* FIXME: UNKNOWN is unknown ..., but allocate 4 byte for it */
1104 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
1107 hres = _unmarshal_interface(buf,&IID_IUnknown,(LPUNKNOWN*)arg);
1109 TRACE_(olerelay)("unk(%p)",arg);
1114 hres = _unmarshal_interface(buf,&IID_IDispatch,(LPUNKNOWN*)arg);
1116 TRACE_(olerelay)("idisp(%p)",arg);
1119 if (debugout) TRACE_(olerelay)("<void>");
1121 case VT_USERDEFINED: {
1125 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
1127 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
1130 hres = ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1132 ERR("Could not get typeattr in VT_USERDEFINED.\n");
1134 switch (tattr->typekind) {
1135 case TKIND_DISPATCH:
1136 case TKIND_INTERFACE:
1138 hres = _unmarshal_interface(buf,&(tattr->guid),(LPUNKNOWN*)arg);
1140 case TKIND_RECORD: {
1144 *arg = (DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,tattr->cbSizeInstance);
1146 if (debugout) TRACE_(olerelay)("{");
1147 for (i=0;i<tattr->cVars;i++) {
1150 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
1152 ERR("Could not get vardesc of %d\n",i);
1153 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1154 ITypeInfo_Release(tinfo2);
1157 hres = deserialize_param(
1162 &vdesc->elemdescVar.tdesc,
1163 (DWORD*)(((LPBYTE)*arg)+vdesc->u.oInst),
1166 ITypeInfo2_ReleaseVarDesc(tinfo2, vdesc);
1167 if (debugout && (i<tattr->cVars-1)) TRACE_(olerelay)(",");
1169 if (debugout) TRACE_(olerelay)("}");
1173 hres = deserialize_param(tinfo2,readit,debugout,alloc,&tattr->tdescAlias,arg,buf);
1177 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
1178 if (hres) ERR("Failed to read enum (4 byte)\n");
1180 if (debugout) TRACE_(olerelay)("%x",*arg);
1183 ERR("Unhandled typekind %d\n",tattr->typekind);
1187 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1190 ERR("failed to stuballoc in TKIND_RECORD.\n");
1191 ITypeInfo_Release(tinfo2);
1195 /* arg is pointing to the start of the array. */
1196 ARRAYDESC *adesc = tdesc->u.lpadesc;
1199 if (adesc->cDims > 1) FIXME("cDims > 1 in VT_CARRAY. Does it work?\n");
1200 for (i=0;i<adesc->cDims;i++)
1201 arrsize *= adesc->rgbounds[i].cElements;
1202 for (i=0;i<arrsize;i++)
1209 (DWORD*)((LPBYTE)(arg)+i*_xsize(&adesc->tdescElem)),
1214 case VT_SAFEARRAY: {
1217 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
1218 unsigned char *buffer;
1219 buffer = LPSAFEARRAY_UserUnmarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
1220 buf->curoff = buffer - buf->base;
1225 ERR("No handler for VT type %d!\n",tdesc->vt);
1231 /* Retrieves a function's funcdesc, searching back into inherited interfaces. */
1232 static HRESULT get_funcdesc(ITypeInfo *tinfo, int iMethod, ITypeInfo **tactual, const FUNCDESC **fdesc,
1233 BSTR *iname, BSTR *fname, UINT *num)
1237 UINT inherited_funcs = 0;
1240 if (fname) *fname = NULL;
1241 if (iname) *iname = NULL;
1245 hr = ITypeInfo_GetTypeAttr(tinfo, &attr);
1248 ERR("GetTypeAttr failed with %x\n",hr);
1252 if(attr->typekind == TKIND_DISPATCH)
1254 if(attr->wTypeFlags & TYPEFLAG_FDUAL)
1259 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, -1, &href);
1262 ERR("Cannot get interface href from dual dispinterface\n");
1263 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1266 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
1269 ERR("Cannot get interface from dual dispinterface\n");
1270 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1273 hr = get_funcdesc(tinfo2, iMethod, tactual, fdesc, iname, fname, num);
1274 ITypeInfo_Release(tinfo2);
1275 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1278 ERR("Shouldn't be called with a non-dual dispinterface\n");
1282 impl_types = attr->cImplTypes;
1283 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1285 for (i = 0; i < impl_types; i++)
1288 ITypeInfo *pSubTypeInfo;
1291 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, i, &href);
1292 if (FAILED(hr)) return hr;
1293 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &pSubTypeInfo);
1294 if (FAILED(hr)) return hr;
1296 hr = get_funcdesc(pSubTypeInfo, iMethod, tactual, fdesc, iname, fname, &sub_funcs);
1297 inherited_funcs += sub_funcs;
1298 ITypeInfo_Release(pSubTypeInfo);
1299 if(SUCCEEDED(hr)) return hr;
1301 if(iMethod < inherited_funcs)
1303 ERR("shouldn't be here\n");
1304 return E_INVALIDARG;
1307 for(i = inherited_funcs; i <= iMethod; i++)
1309 hr = ITypeInfoImpl_GetInternalFuncDesc(tinfo, i - inherited_funcs, fdesc);
1317 /* found it. We don't care about num so zero it */
1320 ITypeInfo_AddRef(*tactual);
1321 if (fname) ITypeInfo_GetDocumentation(tinfo,(*fdesc)->memid,fname,NULL,NULL,NULL);
1322 if (iname) ITypeInfo_GetDocumentation(tinfo,-1,iname,NULL,NULL,NULL);
1326 static inline BOOL is_in_elem(const ELEMDESC *elem)
1328 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN || !elem->u.paramdesc.wParamFlags);
1331 static inline BOOL is_out_elem(const ELEMDESC *elem)
1333 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT || !elem->u.paramdesc.wParamFlags);
1337 xCall(LPVOID retptr, int method, TMProxyImpl *tpinfo /*, args */)
1339 DWORD *args = ((DWORD*)&tpinfo)+1, *xargs;
1340 const FUNCDESC *fdesc;
1342 int i, relaydeb = TRACE_ON(olerelay);
1349 DWORD remoteresult = 0;
1351 IRpcChannelBuffer *chanbuf;
1353 EnterCriticalSection(&tpinfo->crit);
1355 hres = get_funcdesc(tpinfo->tinfo,method,&tinfo,&fdesc,&iname,&fname,NULL);
1357 ERR("Did not find typeinfo/funcdesc entry for method %d!\n",method);
1358 LeaveCriticalSection(&tpinfo->crit);
1362 if (!tpinfo->chanbuf)
1364 WARN("Tried to use disconnected proxy\n");
1365 ITypeInfo_Release(tinfo);
1366 LeaveCriticalSection(&tpinfo->crit);
1367 return RPC_E_DISCONNECTED;
1369 chanbuf = tpinfo->chanbuf;
1370 IRpcChannelBuffer_AddRef(chanbuf);
1372 LeaveCriticalSection(&tpinfo->crit);
1375 TRACE_(olerelay)("->");
1377 TRACE_(olerelay)("%s:",relaystr(iname));
1379 TRACE_(olerelay)("%s(%d)",relaystr(fname),method);
1381 TRACE_(olerelay)("%d",method);
1382 TRACE_(olerelay)("(");
1385 if (iname) SysFreeString(iname);
1386 if (fname) SysFreeString(fname);
1388 memset(&buf,0,sizeof(buf));
1390 /* normal typelib driven serializing */
1392 /* Need them for hack below */
1393 memset(names,0,sizeof(names));
1394 if (ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames))
1396 if (nrofnames > sizeof(names)/sizeof(names[0]))
1397 ERR("Need more names!\n");
1400 for (i=0;i<fdesc->cParams;i++) {
1401 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1403 if (i) TRACE_(olerelay)(",");
1404 if (i+1<nrofnames && names[i+1])
1405 TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1407 /* No need to marshal other data than FIN and any VT_PTR. */
1408 if (!is_in_elem(elem) && (elem->tdesc.vt != VT_PTR)) {
1409 xargs+=_argsize(elem->tdesc.vt);
1410 if (relaydeb) TRACE_(olerelay)("[out]");
1413 hres = serialize_param(
1424 ERR("Failed to serialize param, hres %x\n",hres);
1427 xargs+=_argsize(elem->tdesc.vt);
1429 if (relaydeb) TRACE_(olerelay)(")");
1431 memset(&msg,0,sizeof(msg));
1432 msg.cbBuffer = buf.curoff;
1433 msg.iMethod = method;
1434 hres = IRpcChannelBuffer_GetBuffer(chanbuf,&msg,&(tpinfo->iid));
1436 ERR("RpcChannelBuffer GetBuffer failed, %x\n",hres);
1439 memcpy(msg.Buffer,buf.base,buf.curoff);
1440 if (relaydeb) TRACE_(olerelay)("\n");
1441 hres = IRpcChannelBuffer_SendReceive(chanbuf,&msg,&status);
1443 ERR("RpcChannelBuffer SendReceive failed, %x\n",hres);
1447 if (relaydeb) TRACE_(olerelay)(" status = %08x (",status);
1449 buf.base = HeapReAlloc(GetProcessHeap(),0,buf.base,msg.cbBuffer);
1451 buf.base = HeapAlloc(GetProcessHeap(),0,msg.cbBuffer);
1452 buf.size = msg.cbBuffer;
1453 memcpy(buf.base,msg.Buffer,buf.size);
1456 /* generic deserializer using typelib description */
1459 for (i=0;i<fdesc->cParams;i++) {
1460 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1463 if (i) TRACE_(olerelay)(",");
1464 if (i+1<nrofnames && names[i+1]) TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1466 /* No need to marshal other data than FOUT and any VT_PTR */
1467 if (!is_out_elem(elem) && (elem->tdesc.vt != VT_PTR)) {
1468 xargs += _argsize(elem->tdesc.vt);
1469 if (relaydeb) TRACE_(olerelay)("[in]");
1472 hres = deserialize_param(
1482 ERR("Failed to unmarshall param, hres %x\n",hres);
1486 xargs += _argsize(elem->tdesc.vt);
1489 hres = xbuf_get(&buf, (LPBYTE)&remoteresult, sizeof(DWORD));
1492 if (relaydeb) TRACE_(olerelay)(") = %08x\n", remoteresult);
1494 hres = remoteresult;
1497 IRpcChannelBuffer_FreeBuffer(chanbuf,&msg);
1498 for (i = 0; i < nrofnames; i++)
1499 SysFreeString(names[i]);
1500 HeapFree(GetProcessHeap(),0,buf.base);
1501 IRpcChannelBuffer_Release(chanbuf);
1502 ITypeInfo_Release(tinfo);
1503 TRACE("-- 0x%08x\n", hres);
1507 static HRESULT WINAPI ProxyIUnknown_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
1509 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1511 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1513 if (proxy->outerunknown)
1514 return IUnknown_QueryInterface(proxy->outerunknown, riid, ppv);
1516 FIXME("No interface\n");
1517 return E_NOINTERFACE;
1520 static ULONG WINAPI ProxyIUnknown_AddRef(IUnknown *iface)
1522 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1526 if (proxy->outerunknown)
1527 return IUnknown_AddRef(proxy->outerunknown);
1529 return 2; /* FIXME */
1532 static ULONG WINAPI ProxyIUnknown_Release(IUnknown *iface)
1534 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1538 if (proxy->outerunknown)
1539 return IUnknown_Release(proxy->outerunknown);
1541 return 1; /* FIXME */
1544 static HRESULT WINAPI ProxyIDispatch_GetTypeInfoCount(LPDISPATCH iface, UINT * pctinfo)
1546 TMProxyImpl *This = (TMProxyImpl *)iface;
1548 TRACE("(%p)\n", pctinfo);
1550 return IDispatch_GetTypeInfoCount(This->dispatch, pctinfo);
1553 static HRESULT WINAPI ProxyIDispatch_GetTypeInfo(LPDISPATCH iface, UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo)
1555 TMProxyImpl *This = (TMProxyImpl *)iface;
1557 TRACE("(%d, %x, %p)\n", iTInfo, lcid, ppTInfo);
1559 return IDispatch_GetTypeInfo(This->dispatch, iTInfo, lcid, ppTInfo);
1562 static HRESULT WINAPI ProxyIDispatch_GetIDsOfNames(LPDISPATCH iface, REFIID riid, LPOLESTR * rgszNames, UINT cNames, LCID lcid, DISPID * rgDispId)
1564 TMProxyImpl *This = (TMProxyImpl *)iface;
1566 TRACE("(%s, %p, %d, 0x%x, %p)\n", debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);
1568 return IDispatch_GetIDsOfNames(This->dispatch, riid, rgszNames,
1569 cNames, lcid, rgDispId);
1572 static HRESULT WINAPI ProxyIDispatch_Invoke(LPDISPATCH iface, DISPID dispIdMember, REFIID riid, LCID lcid,
1573 WORD wFlags, DISPPARAMS * pDispParams, VARIANT * pVarResult,
1574 EXCEPINFO * pExcepInfo, UINT * puArgErr)
1576 TMProxyImpl *This = (TMProxyImpl *)iface;
1578 TRACE("(%d, %s, 0x%x, 0x%x, %p, %p, %p, %p)\n", dispIdMember,
1579 debugstr_guid(riid), lcid, wFlags, pDispParams, pVarResult,
1580 pExcepInfo, puArgErr);
1582 return IDispatch_Invoke(This->dispatch, dispIdMember, riid, lcid,
1583 wFlags, pDispParams, pVarResult, pExcepInfo,
1589 const IRpcChannelBufferVtbl *lpVtbl;
1591 /* the IDispatch-derived interface we are handling */
1593 IRpcChannelBuffer *pDelegateChannel;
1594 } TMarshalDispatchChannel;
1596 static HRESULT WINAPI TMarshalDispatchChannel_QueryInterface(LPRPCCHANNELBUFFER iface, REFIID riid, LPVOID *ppv)
1599 if (IsEqualIID(riid,&IID_IRpcChannelBuffer) || IsEqualIID(riid,&IID_IUnknown))
1601 *ppv = (LPVOID)iface;
1602 IUnknown_AddRef(iface);
1605 return E_NOINTERFACE;
1608 static ULONG WINAPI TMarshalDispatchChannel_AddRef(LPRPCCHANNELBUFFER iface)
1610 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1611 return InterlockedIncrement(&This->refs);
1614 static ULONG WINAPI TMarshalDispatchChannel_Release(LPRPCCHANNELBUFFER iface)
1616 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1619 ref = InterlockedDecrement(&This->refs);
1623 IRpcChannelBuffer_Release(This->pDelegateChannel);
1624 HeapFree(GetProcessHeap(), 0, This);
1628 static HRESULT WINAPI TMarshalDispatchChannel_GetBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg, REFIID riid)
1630 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1631 TRACE("(%p, %s)\n", olemsg, debugstr_guid(riid));
1632 /* Note: we are pretending to invoke a method on the interface identified
1633 * by tmarshal_iid so that we can re-use the IDispatch proxy/stub code
1634 * without the RPC runtime getting confused by not exporting an IDispatch interface */
1635 return IRpcChannelBuffer_GetBuffer(This->pDelegateChannel, olemsg, &This->tmarshal_iid);
1638 static HRESULT WINAPI TMarshalDispatchChannel_SendReceive(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE *olemsg, ULONG *pstatus)
1640 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1641 TRACE("(%p, %p)\n", olemsg, pstatus);
1642 return IRpcChannelBuffer_SendReceive(This->pDelegateChannel, olemsg, pstatus);
1645 static HRESULT WINAPI TMarshalDispatchChannel_FreeBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg)
1647 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1648 TRACE("(%p)\n", olemsg);
1649 return IRpcChannelBuffer_FreeBuffer(This->pDelegateChannel, olemsg);
1652 static HRESULT WINAPI TMarshalDispatchChannel_GetDestCtx(LPRPCCHANNELBUFFER iface, DWORD* pdwDestContext, void** ppvDestContext)
1654 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1655 TRACE("(%p,%p)\n", pdwDestContext, ppvDestContext);
1656 return IRpcChannelBuffer_GetDestCtx(This->pDelegateChannel, pdwDestContext, ppvDestContext);
1659 static HRESULT WINAPI TMarshalDispatchChannel_IsConnected(LPRPCCHANNELBUFFER iface)
1661 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1663 return IRpcChannelBuffer_IsConnected(This->pDelegateChannel);
1666 static const IRpcChannelBufferVtbl TMarshalDispatchChannelVtbl =
1668 TMarshalDispatchChannel_QueryInterface,
1669 TMarshalDispatchChannel_AddRef,
1670 TMarshalDispatchChannel_Release,
1671 TMarshalDispatchChannel_GetBuffer,
1672 TMarshalDispatchChannel_SendReceive,
1673 TMarshalDispatchChannel_FreeBuffer,
1674 TMarshalDispatchChannel_GetDestCtx,
1675 TMarshalDispatchChannel_IsConnected
1678 static HRESULT TMarshalDispatchChannel_Create(
1679 IRpcChannelBuffer *pDelegateChannel, REFIID tmarshal_riid,
1680 IRpcChannelBuffer **ppChannel)
1682 TMarshalDispatchChannel *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1684 return E_OUTOFMEMORY;
1686 This->lpVtbl = &TMarshalDispatchChannelVtbl;
1688 IRpcChannelBuffer_AddRef(pDelegateChannel);
1689 This->pDelegateChannel = pDelegateChannel;
1690 This->tmarshal_iid = *tmarshal_riid;
1692 *ppChannel = (IRpcChannelBuffer *)&This->lpVtbl;
1697 static inline HRESULT get_facbuf_for_iid(REFIID riid, IPSFactoryBuffer **facbuf)
1702 if ((hr = CoGetPSClsid(riid, &clsid)))
1704 return CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL,
1705 &IID_IPSFactoryBuffer, (LPVOID*)facbuf);
1708 static HRESULT init_proxy_entry_point(TMProxyImpl *proxy, unsigned int num)
1711 /* nrofargs without This */
1714 TMAsmProxy *xasm = proxy->asmstubs + num;
1716 const FUNCDESC *fdesc;
1718 hres = get_funcdesc(proxy->tinfo, num, &tinfo2, &fdesc, NULL, NULL, NULL);
1720 ERR("GetFuncDesc %x should not fail here.\n",hres);
1723 ITypeInfo_Release(tinfo2);
1724 /* some args take more than 4 byte on the stack */
1726 for (j=0;j<fdesc->cParams;j++)
1727 nrofargs += _argsize(fdesc->lprgelemdescParam[j].tdesc.vt);
1730 if (fdesc->callconv != CC_STDCALL) {
1731 ERR("calling convention is not stdcall????\n");
1734 /* popl %eax - return ptr
1741 * arg3 arg2 arg1 <method> <returnptr>
1743 xasm->popleax = 0x58;
1744 xasm->pushlval = 0x68;
1746 xasm->pushleax = 0x50;
1747 xasm->lcall = 0xe8; /* relative jump */
1748 xasm->xcall = (DWORD)xCall;
1749 xasm->xcall -= (DWORD)&(xasm->lret);
1751 xasm->bytestopop = (nrofargs+2)*4; /* pop args, This, iMethod */
1753 proxy->lpvtbl[num] = xasm;
1755 FIXME("not implemented on non i386\n");
1761 static HRESULT WINAPI
1762 PSFacBuf_CreateProxy(
1763 LPPSFACTORYBUFFER iface, IUnknown* pUnkOuter, REFIID riid,
1764 IRpcProxyBuffer **ppProxy, LPVOID *ppv)
1768 unsigned int i, nroffuncs;
1771 BOOL defer_to_dispatch = FALSE;
1773 TRACE("(...%s...)\n",debugstr_guid(riid));
1774 hres = _get_typeinfo_for_iid(riid,&tinfo);
1776 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
1780 hres = num_of_funcs(tinfo, &nroffuncs);
1782 ERR("Cannot get number of functions for typeinfo %s\n",debugstr_guid(riid));
1783 ITypeInfo_Release(tinfo);
1787 proxy = CoTaskMemAlloc(sizeof(TMProxyImpl));
1788 if (!proxy) return E_OUTOFMEMORY;
1790 assert(sizeof(TMAsmProxy) == 16);
1792 proxy->dispatch = NULL;
1793 proxy->dispatch_proxy = NULL;
1794 proxy->outerunknown = pUnkOuter;
1795 proxy->asmstubs = VirtualAlloc(NULL, sizeof(TMAsmProxy) * nroffuncs, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
1796 if (!proxy->asmstubs) {
1797 ERR("Could not commit pages for proxy thunks\n");
1798 CoTaskMemFree(proxy);
1799 return E_OUTOFMEMORY;
1801 proxy->lpvtbl2 = &tmproxyvtable;
1802 /* one reference for the proxy */
1804 proxy->tinfo = tinfo;
1808 InitializeCriticalSection(&proxy->crit);
1809 proxy->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TMProxyImpl.crit");
1811 proxy->lpvtbl = HeapAlloc(GetProcessHeap(),0,sizeof(LPBYTE)*nroffuncs);
1813 /* if we derive from IDispatch then defer to its proxy for its methods */
1814 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
1817 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
1819 IPSFactoryBuffer *factory_buffer;
1820 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
1823 hres = IPSFactoryBuffer_CreateProxy(factory_buffer, NULL,
1824 &IID_IDispatch, &proxy->dispatch_proxy,
1825 (void **)&proxy->dispatch);
1826 IPSFactoryBuffer_Release(factory_buffer);
1828 if ((hres == S_OK) && (nroffuncs < 7))
1830 ERR("nroffuncs calculated incorrectly (%d)\n", nroffuncs);
1831 hres = E_UNEXPECTED;
1835 defer_to_dispatch = TRUE;
1838 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
1841 for (i=0;i<nroffuncs;i++) {
1844 proxy->lpvtbl[i] = ProxyIUnknown_QueryInterface;
1847 proxy->lpvtbl[i] = ProxyIUnknown_AddRef;
1850 proxy->lpvtbl[i] = ProxyIUnknown_Release;
1853 if(!defer_to_dispatch)
1855 hres = init_proxy_entry_point(proxy, i);
1856 if(FAILED(hres)) return hres;
1858 else proxy->lpvtbl[3] = ProxyIDispatch_GetTypeInfoCount;
1861 if(!defer_to_dispatch)
1863 hres = init_proxy_entry_point(proxy, i);
1864 if(FAILED(hres)) return hres;
1866 else proxy->lpvtbl[4] = ProxyIDispatch_GetTypeInfo;
1869 if(!defer_to_dispatch)
1871 hres = init_proxy_entry_point(proxy, i);
1872 if(FAILED(hres)) return hres;
1874 else proxy->lpvtbl[5] = ProxyIDispatch_GetIDsOfNames;
1877 if(!defer_to_dispatch)
1879 hres = init_proxy_entry_point(proxy, i);
1880 if(FAILED(hres)) return hres;
1882 else proxy->lpvtbl[6] = ProxyIDispatch_Invoke;
1885 hres = init_proxy_entry_point(proxy, i);
1886 if(FAILED(hres)) return hres;
1892 *ppv = (LPVOID)proxy;
1893 *ppProxy = (IRpcProxyBuffer *)&(proxy->lpvtbl2);
1894 IUnknown_AddRef((IUnknown *)*ppv);
1898 TMProxyImpl_Release((IRpcProxyBuffer *)&proxy->lpvtbl2);
1902 typedef struct _TMStubImpl {
1903 const IRpcStubBufferVtbl *lpvtbl;
1909 IRpcStubBuffer *dispatch_stub;
1910 BOOL dispatch_derivative;
1913 static HRESULT WINAPI
1914 TMStubImpl_QueryInterface(LPRPCSTUBBUFFER iface, REFIID riid, LPVOID *ppv)
1916 if (IsEqualIID(riid,&IID_IRpcStubBuffer)||IsEqualIID(riid,&IID_IUnknown)){
1917 *ppv = (LPVOID)iface;
1918 IRpcStubBuffer_AddRef(iface);
1921 FIXME("%s, not supported IID.\n",debugstr_guid(riid));
1922 return E_NOINTERFACE;
1926 TMStubImpl_AddRef(LPRPCSTUBBUFFER iface)
1928 TMStubImpl *This = (TMStubImpl *)iface;
1929 ULONG refCount = InterlockedIncrement(&This->ref);
1931 TRACE("(%p)->(ref before=%u)\n", This, refCount - 1);
1937 TMStubImpl_Release(LPRPCSTUBBUFFER iface)
1939 TMStubImpl *This = (TMStubImpl *)iface;
1940 ULONG refCount = InterlockedDecrement(&This->ref);
1942 TRACE("(%p)->(ref before=%u)\n", This, refCount + 1);
1946 IRpcStubBuffer_Disconnect(iface);
1947 ITypeInfo_Release(This->tinfo);
1948 if (This->dispatch_stub)
1949 IRpcStubBuffer_Release(This->dispatch_stub);
1950 CoTaskMemFree(This);
1955 static HRESULT WINAPI
1956 TMStubImpl_Connect(LPRPCSTUBBUFFER iface, LPUNKNOWN pUnkServer)
1958 TMStubImpl *This = (TMStubImpl *)iface;
1960 TRACE("(%p)->(%p)\n", This, pUnkServer);
1962 IUnknown_AddRef(pUnkServer);
1963 This->pUnk = pUnkServer;
1965 if (This->dispatch_stub)
1966 IRpcStubBuffer_Connect(This->dispatch_stub, pUnkServer);
1972 TMStubImpl_Disconnect(LPRPCSTUBBUFFER iface)
1974 TMStubImpl *This = (TMStubImpl *)iface;
1976 TRACE("(%p)->()\n", This);
1980 IUnknown_Release(This->pUnk);
1984 if (This->dispatch_stub)
1985 IRpcStubBuffer_Disconnect(This->dispatch_stub);
1988 static HRESULT WINAPI
1990 LPRPCSTUBBUFFER iface, RPCOLEMESSAGE* xmsg,IRpcChannelBuffer*rpcchanbuf)
1993 const FUNCDESC *fdesc;
1994 TMStubImpl *This = (TMStubImpl *)iface;
1996 DWORD *args = NULL, res, *xargs, nrofargs;
2001 ITypeInfo *tinfo = NULL;
2005 if (xmsg->iMethod < 3) {
2006 ERR("IUnknown methods cannot be marshaled by the typelib marshaler\n");
2007 return E_UNEXPECTED;
2010 if (This->dispatch_derivative && xmsg->iMethod < sizeof(IDispatchVtbl)/sizeof(void *))
2012 IPSFactoryBuffer *factory_buffer;
2013 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
2016 hres = IPSFactoryBuffer_CreateStub(factory_buffer, &IID_IDispatch,
2017 This->pUnk, &This->dispatch_stub);
2018 IPSFactoryBuffer_Release(factory_buffer);
2022 return IRpcStubBuffer_Invoke(This->dispatch_stub, xmsg, rpcchanbuf);
2025 memset(&buf,0,sizeof(buf));
2026 buf.size = xmsg->cbBuffer;
2027 buf.base = HeapAlloc(GetProcessHeap(), 0, xmsg->cbBuffer);
2028 memcpy(buf.base, xmsg->Buffer, xmsg->cbBuffer);
2031 hres = get_funcdesc(This->tinfo,xmsg->iMethod,&tinfo,&fdesc,&iname,NULL,NULL);
2033 ERR("GetFuncDesc on method %d failed with %x\n",xmsg->iMethod,hres);
2037 if (iname && !lstrcmpW(iname, IDispatchW))
2039 ERR("IDispatch cannot be marshaled by the typelib marshaler\n");
2040 hres = E_UNEXPECTED;
2041 SysFreeString (iname);
2045 if (iname) SysFreeString (iname);
2047 /* Need them for hack below */
2048 memset(names,0,sizeof(names));
2049 ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames);
2050 if (nrofnames > sizeof(names)/sizeof(names[0])) {
2051 ERR("Need more names!\n");
2054 /*dump_FUNCDESC(fdesc);*/
2056 for (i=0;i<fdesc->cParams;i++)
2057 nrofargs += _argsize(fdesc->lprgelemdescParam[i].tdesc.vt);
2058 args = HeapAlloc(GetProcessHeap(),0,(nrofargs+1)*sizeof(DWORD));
2061 hres = E_OUTOFMEMORY;
2065 /* Allocate all stuff used by call. */
2067 for (i=0;i<fdesc->cParams;i++) {
2068 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2070 hres = deserialize_param(
2079 xargs += _argsize(elem->tdesc.vt);
2081 ERR("Failed to deserialize param %s, hres %x\n",relaystr(names[i+1]),hres);
2086 args[0] = (DWORD)This->pUnk;
2091 (*((FARPROC**)args[0]))[fdesc->oVft/4],
2099 DWORD dwExceptionCode = GetExceptionCode();
2100 ERR("invoke call failed with exception 0x%08x (%d)\n", dwExceptionCode, dwExceptionCode);
2101 if (FAILED(dwExceptionCode))
2102 hres = dwExceptionCode;
2104 hres = HRESULT_FROM_WIN32(dwExceptionCode);
2114 for (i=0;i<fdesc->cParams;i++) {
2115 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2116 hres = serialize_param(
2125 xargs += _argsize(elem->tdesc.vt);
2127 ERR("Failed to stuballoc param, hres %x\n",hres);
2132 hres = xbuf_add (&buf, (LPBYTE)&res, sizeof(DWORD));
2137 xmsg->cbBuffer = buf.curoff;
2138 hres = IRpcChannelBuffer_GetBuffer(rpcchanbuf, xmsg, &This->iid);
2140 ERR("IRpcChannelBuffer_GetBuffer failed with error 0x%08x\n", hres);
2143 memcpy(xmsg->Buffer, buf.base, buf.curoff);
2146 for (i = 0; i < nrofnames; i++)
2147 SysFreeString(names[i]);
2149 ITypeInfo_Release(tinfo);
2150 HeapFree(GetProcessHeap(), 0, args);
2152 HeapFree(GetProcessHeap(), 0, buf.base);
2154 TRACE("returning\n");
2158 static LPRPCSTUBBUFFER WINAPI
2159 TMStubImpl_IsIIDSupported(LPRPCSTUBBUFFER iface, REFIID riid) {
2160 FIXME("Huh (%s)?\n",debugstr_guid(riid));
2165 TMStubImpl_CountRefs(LPRPCSTUBBUFFER iface) {
2166 TMStubImpl *This = (TMStubImpl *)iface;
2169 return This->ref; /*FIXME? */
2172 static HRESULT WINAPI
2173 TMStubImpl_DebugServerQueryInterface(LPRPCSTUBBUFFER iface, LPVOID *ppv) {
2178 TMStubImpl_DebugServerRelease(LPRPCSTUBBUFFER iface, LPVOID ppv) {
2182 static const IRpcStubBufferVtbl tmstubvtbl = {
2183 TMStubImpl_QueryInterface,
2187 TMStubImpl_Disconnect,
2189 TMStubImpl_IsIIDSupported,
2190 TMStubImpl_CountRefs,
2191 TMStubImpl_DebugServerQueryInterface,
2192 TMStubImpl_DebugServerRelease
2195 static HRESULT WINAPI
2196 PSFacBuf_CreateStub(
2197 LPPSFACTORYBUFFER iface, REFIID riid,IUnknown *pUnkServer,
2198 IRpcStubBuffer** ppStub
2205 TRACE("(%s,%p,%p)\n",debugstr_guid(riid),pUnkServer,ppStub);
2207 hres = _get_typeinfo_for_iid(riid,&tinfo);
2209 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
2213 stub = CoTaskMemAlloc(sizeof(TMStubImpl));
2215 return E_OUTOFMEMORY;
2216 stub->lpvtbl = &tmstubvtbl;
2218 stub->tinfo = tinfo;
2219 stub->dispatch_stub = NULL;
2220 stub->dispatch_derivative = FALSE;
2222 hres = IRpcStubBuffer_Connect((LPRPCSTUBBUFFER)stub,pUnkServer);
2223 *ppStub = (LPRPCSTUBBUFFER)stub;
2224 TRACE("IRpcStubBuffer: %p\n", stub);
2226 ERR("Connect to pUnkServer failed?\n");
2228 /* if we derive from IDispatch then defer to its stub for some of its methods */
2229 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
2232 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
2233 stub->dispatch_derivative = TRUE;
2234 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
2240 static const IPSFactoryBufferVtbl psfacbufvtbl = {
2241 PSFacBuf_QueryInterface,
2244 PSFacBuf_CreateProxy,
2248 /* This is the whole PSFactoryBuffer object, just the vtableptr */
2249 static const IPSFactoryBufferVtbl *lppsfac = &psfacbufvtbl;
2251 /***********************************************************************
2252 * TMARSHAL_DllGetClassObject
2254 HRESULT TMARSHAL_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
2256 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)) {
2260 return E_NOINTERFACE;