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) {
701 case TKIND_ENUM: /* confirmed */
702 case TKIND_RECORD: /* FIXME: mostly untested */
705 case TKIND_ALIAS: /* FIXME: untested */
706 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
707 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
711 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
715 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
716 ITypeInfo_Release(tinfo2);
719 if (debugout) TRACE_(olerelay)("*");
720 /* Write always, so the other side knows when it gets a NULL pointer.
722 cookie = *arg ? 0x42424242 : 0;
723 hres = xbuf_add(buf,(LPBYTE)&cookie,sizeof(cookie));
727 if (debugout) TRACE_(olerelay)("NULL");
730 hres = serialize_param(tinfo,writeit,debugout,dealloc,tdesc->u.lptdesc,(DWORD*)*arg,buf);
731 if (derefhere && dealloc) HeapFree(GetProcessHeap(),0,(LPVOID)*arg);
735 if (debugout) TRACE_(olerelay)("unk(0x%x)",*arg);
737 hres = _marshal_interface(buf,&IID_IUnknown,(LPUNKNOWN)*arg);
738 if (dealloc && *(IUnknown **)arg)
739 IUnknown_Release((LPUNKNOWN)*arg);
742 if (debugout) TRACE_(olerelay)("idisp(0x%x)",*arg);
744 hres = _marshal_interface(buf,&IID_IDispatch,(LPUNKNOWN)*arg);
745 if (dealloc && *(IUnknown **)arg)
746 IUnknown_Release((LPUNKNOWN)*arg);
749 if (debugout) TRACE_(olerelay)("<void>");
751 case VT_USERDEFINED: {
755 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
757 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
760 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
761 switch (tattr->typekind) {
763 case TKIND_INTERFACE:
765 hres=_marshal_interface(buf,&(tattr->guid),(LPUNKNOWN)arg);
767 IUnknown_Release((LPUNKNOWN)arg);
771 if (debugout) TRACE_(olerelay)("{");
772 for (i=0;i<tattr->cVars;i++) {
777 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
779 ERR("Could not get vardesc of %d\n",i);
782 elem2 = &vdesc->elemdescVar;
783 tdesc2 = &elem2->tdesc;
784 hres = serialize_param(
790 (DWORD*)(((LPBYTE)arg)+vdesc->u.oInst),
793 ITypeInfo_ReleaseVarDesc(tinfo2, vdesc);
796 if (debugout && (i<(tattr->cVars-1)))
797 TRACE_(olerelay)(",");
799 if (debugout) TRACE_(olerelay)("}");
803 hres = serialize_param(tinfo2,writeit,debugout,dealloc,&tattr->tdescAlias,arg,buf);
807 if (debugout) TRACE_(olerelay)("%x",*arg);
809 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
812 FIXME("Unhandled typekind %d\n",tattr->typekind);
816 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
817 ITypeInfo_Release(tinfo2);
821 ARRAYDESC *adesc = tdesc->u.lpadesc;
824 if (debugout) TRACE_(olerelay)("carr");
825 for (i=0;i<adesc->cDims;i++) {
826 if (debugout) TRACE_(olerelay)("[%d]",adesc->rgbounds[i].cElements);
827 arrsize *= adesc->rgbounds[i].cElements;
829 if (debugout) TRACE_(olerelay)("(vt %s)",debugstr_vt(adesc->tdescElem.vt));
830 if (debugout) TRACE_(olerelay)("[");
831 for (i=0;i<arrsize;i++) {
832 hres = serialize_param(tinfo, writeit, debugout, dealloc, &adesc->tdescElem, (DWORD*)((LPBYTE)arg+i*_xsize(&adesc->tdescElem)), buf);
835 if (debugout && (i<arrsize-1)) TRACE_(olerelay)(",");
837 if (debugout) TRACE_(olerelay)("]");
843 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
844 ULONG size = LPSAFEARRAY_UserSize(&flags, buf->curoff, (LPSAFEARRAY *)arg);
845 xbuf_resize(buf, size);
846 LPSAFEARRAY_UserMarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
852 ERR("Unhandled marshal type %d.\n",tdesc->vt);
869 TRACE("vt %s at %p\n",debugstr_vt(tdesc->vt),arg);
874 if (debugout) TRACE_(olerelay)("<empty>\n");
877 if (debugout) TRACE_(olerelay)("<null>\n");
880 VARIANT *vt = (VARIANT*)arg;
885 hres = xbuf_get(buf,(LPBYTE)&vttype,sizeof(vttype));
887 FIXME("vt type not read?\n");
890 memset(&tdesc2,0,sizeof(tdesc2));
893 if (debugout) TRACE_(olerelay)("Vt(%s%s)(",debugstr_vt(vttype),debugstr_vf(vttype));
894 hres = deserialize_param(tinfo, readit, debugout, alloc, &tdesc2, (DWORD*)&(V_I4(vt)), buf);
895 TRACE_(olerelay)(")");
907 hres = xbuf_get(buf,(LPBYTE)arg,8);
908 if (hres) ERR("Failed to read integer 8 byte\n");
910 if (debugout) TRACE_(olerelay)("%x%x",arg[0],arg[1]);
920 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
921 if (hres) ERR("Failed to read integer 4 byte\n");
923 if (debugout) TRACE_(olerelay)("%x",*arg);
929 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
930 if (hres) ERR("Failed to read integer 4 byte\n");
933 if (debugout) TRACE_(olerelay)("%04x",*arg & 0xffff);
939 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
940 if (hres) ERR("Failed to read integer 4 byte\n");
943 if (debugout) TRACE_(olerelay)("%02x",*arg & 0xff);
948 *arg = (DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
950 hres = xbuf_get(buf,(LPBYTE)*arg,sizeof(DWORD));
951 if (hres) ERR("Failed to read integer 4 byte\n");
953 if (debugout) TRACE_(olerelay)("&0x%x",*(DWORD*)*arg);
955 case VT_BSTR|VT_BYREF: {
956 BSTR **bstr = (BSTR **)arg;
961 hres = xbuf_get(buf,(LPBYTE)&len,sizeof(DWORD));
963 ERR("failed to read bstr klen\n");
967 *bstr = CoTaskMemAlloc(sizeof(BSTR *));
969 if (debugout) TRACE_(olerelay)("<bstr NULL>");
971 str = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(len+1)*sizeof(WCHAR));
972 hres = xbuf_get(buf,(LPBYTE)str,len*sizeof(WCHAR));
974 ERR("Failed to read BSTR.\n");
975 HeapFree(GetProcessHeap(),0,str);
978 *bstr = CoTaskMemAlloc(sizeof(BSTR *));
979 **bstr = SysAllocStringLen(str,len);
980 if (debugout) TRACE_(olerelay)("%s",relaystr(str));
981 HeapFree(GetProcessHeap(),0,str);
993 hres = xbuf_get(buf,(LPBYTE)&len,sizeof(DWORD));
995 ERR("failed to read bstr klen\n");
1000 if (debugout) TRACE_(olerelay)("<bstr NULL>");
1002 str = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(len+1)*sizeof(WCHAR));
1003 hres = xbuf_get(buf,(LPBYTE)str,len*sizeof(WCHAR));
1005 ERR("Failed to read BSTR.\n");
1006 HeapFree(GetProcessHeap(),0,str);
1009 *arg = (DWORD)SysAllocStringLen(str,len);
1010 if (debugout) TRACE_(olerelay)("%s",relaystr(str));
1011 HeapFree(GetProcessHeap(),0,str);
1020 BOOL derefhere = TRUE;
1022 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
1026 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
1028 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
1031 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1032 switch (tattr->typekind) {
1033 case TKIND_ENUM: /* confirmed */
1034 case TKIND_RECORD: /* FIXME: mostly untested */
1037 case TKIND_ALIAS: /* FIXME: untested */
1038 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
1039 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
1043 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
1047 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1048 ITypeInfo_Release(tinfo2);
1050 /* read it in all cases, we need to know if we have
1051 * NULL pointer or not.
1053 hres = xbuf_get(buf,(LPBYTE)&cookie,sizeof(cookie));
1055 ERR("Failed to load pointer cookie.\n");
1058 if (cookie != 0x42424242) {
1059 /* we read a NULL ptr from the remote side */
1060 if (debugout) TRACE_(olerelay)("NULL");
1064 if (debugout) TRACE_(olerelay)("*");
1066 /* Allocate space for the referenced struct */
1068 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,_xsize(tdesc->u.lptdesc));
1071 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, (LPDWORD)*arg, buf);
1073 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, arg, buf);
1076 /* FIXME: UNKNOWN is unknown ..., but allocate 4 byte for it */
1078 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
1081 hres = _unmarshal_interface(buf,&IID_IUnknown,(LPUNKNOWN*)arg);
1083 TRACE_(olerelay)("unk(%p)",arg);
1088 hres = _unmarshal_interface(buf,&IID_IDispatch,(LPUNKNOWN*)arg);
1090 TRACE_(olerelay)("idisp(%p)",arg);
1093 if (debugout) TRACE_(olerelay)("<void>");
1095 case VT_USERDEFINED: {
1099 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
1101 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
1104 hres = ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1106 ERR("Could not get typeattr in VT_USERDEFINED.\n");
1108 switch (tattr->typekind) {
1109 case TKIND_DISPATCH:
1110 case TKIND_INTERFACE:
1112 hres = _unmarshal_interface(buf,&(tattr->guid),(LPUNKNOWN*)arg);
1114 case TKIND_RECORD: {
1118 *arg = (DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,tattr->cbSizeInstance);
1120 if (debugout) TRACE_(olerelay)("{");
1121 for (i=0;i<tattr->cVars;i++) {
1124 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
1126 ERR("Could not get vardesc of %d\n",i);
1127 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1128 ITypeInfo_Release(tinfo2);
1131 hres = deserialize_param(
1136 &vdesc->elemdescVar.tdesc,
1137 (DWORD*)(((LPBYTE)*arg)+vdesc->u.oInst),
1140 ITypeInfo2_ReleaseVarDesc(tinfo2, vdesc);
1141 if (debugout && (i<tattr->cVars-1)) TRACE_(olerelay)(",");
1143 if (debugout) TRACE_(olerelay)("}");
1147 hres = deserialize_param(tinfo2,readit,debugout,alloc,&tattr->tdescAlias,arg,buf);
1151 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
1152 if (hres) ERR("Failed to read enum (4 byte)\n");
1154 if (debugout) TRACE_(olerelay)("%x",*arg);
1157 ERR("Unhandled typekind %d\n",tattr->typekind);
1161 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1164 ERR("failed to stuballoc in TKIND_RECORD.\n");
1165 ITypeInfo_Release(tinfo2);
1169 /* arg is pointing to the start of the array. */
1170 ARRAYDESC *adesc = tdesc->u.lpadesc;
1173 if (adesc->cDims > 1) FIXME("cDims > 1 in VT_CARRAY. Does it work?\n");
1174 for (i=0;i<adesc->cDims;i++)
1175 arrsize *= adesc->rgbounds[i].cElements;
1176 for (i=0;i<arrsize;i++)
1183 (DWORD*)((LPBYTE)(arg)+i*_xsize(&adesc->tdescElem)),
1188 case VT_SAFEARRAY: {
1191 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
1192 unsigned char *buffer;
1193 buffer = LPSAFEARRAY_UserUnmarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
1194 buf->curoff = buffer - buf->base;
1199 ERR("No handler for VT type %d!\n",tdesc->vt);
1205 /* Retrieves a function's funcdesc, searching back into inherited interfaces. */
1206 static HRESULT get_funcdesc(ITypeInfo *tinfo, int iMethod, ITypeInfo **tactual, const FUNCDESC **fdesc,
1207 BSTR *iname, BSTR *fname, UINT *num)
1211 UINT inherited_funcs = 0;
1214 if (fname) *fname = NULL;
1215 if (iname) *iname = NULL;
1219 hr = ITypeInfo_GetTypeAttr(tinfo, &attr);
1222 ERR("GetTypeAttr failed with %x\n",hr);
1226 if(attr->typekind == TKIND_DISPATCH)
1228 if(attr->wTypeFlags & TYPEFLAG_FDUAL)
1233 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, -1, &href);
1236 ERR("Cannot get interface href from dual dispinterface\n");
1237 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1240 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
1243 ERR("Cannot get interface from dual dispinterface\n");
1244 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1247 hr = get_funcdesc(tinfo2, iMethod, tactual, fdesc, iname, fname, num);
1248 ITypeInfo_Release(tinfo2);
1249 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1252 ERR("Shouldn't be called with a non-dual dispinterface\n");
1256 impl_types = attr->cImplTypes;
1257 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1259 for (i = 0; i < impl_types; i++)
1262 ITypeInfo *pSubTypeInfo;
1265 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, i, &href);
1266 if (FAILED(hr)) return hr;
1267 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &pSubTypeInfo);
1268 if (FAILED(hr)) return hr;
1270 hr = get_funcdesc(pSubTypeInfo, iMethod, tactual, fdesc, iname, fname, &sub_funcs);
1271 inherited_funcs += sub_funcs;
1272 ITypeInfo_Release(pSubTypeInfo);
1273 if(SUCCEEDED(hr)) return hr;
1275 if(iMethod < inherited_funcs)
1277 ERR("shouldn't be here\n");
1278 return E_INVALIDARG;
1281 for(i = inherited_funcs; i <= iMethod; i++)
1283 hr = ITypeInfoImpl_GetInternalFuncDesc(tinfo, i - inherited_funcs, fdesc);
1291 /* found it. We don't care about num so zero it */
1294 ITypeInfo_AddRef(*tactual);
1295 if (fname) ITypeInfo_GetDocumentation(tinfo,(*fdesc)->memid,fname,NULL,NULL,NULL);
1296 if (iname) ITypeInfo_GetDocumentation(tinfo,-1,iname,NULL,NULL,NULL);
1300 static inline BOOL is_in_elem(const ELEMDESC *elem)
1302 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN || !elem->u.paramdesc.wParamFlags);
1305 static inline BOOL is_out_elem(const ELEMDESC *elem)
1307 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT || !elem->u.paramdesc.wParamFlags);
1311 xCall(LPVOID retptr, int method, TMProxyImpl *tpinfo /*, args */)
1313 DWORD *args = ((DWORD*)&tpinfo)+1, *xargs;
1314 const FUNCDESC *fdesc;
1316 int i, relaydeb = TRACE_ON(olerelay);
1323 DWORD remoteresult = 0;
1325 IRpcChannelBuffer *chanbuf;
1327 EnterCriticalSection(&tpinfo->crit);
1329 hres = get_funcdesc(tpinfo->tinfo,method,&tinfo,&fdesc,&iname,&fname,NULL);
1331 ERR("Did not find typeinfo/funcdesc entry for method %d!\n",method);
1332 LeaveCriticalSection(&tpinfo->crit);
1336 if (!tpinfo->chanbuf)
1338 WARN("Tried to use disconnected proxy\n");
1339 ITypeInfo_Release(tinfo);
1340 LeaveCriticalSection(&tpinfo->crit);
1341 return RPC_E_DISCONNECTED;
1343 chanbuf = tpinfo->chanbuf;
1344 IRpcChannelBuffer_AddRef(chanbuf);
1346 LeaveCriticalSection(&tpinfo->crit);
1349 TRACE_(olerelay)("->");
1351 TRACE_(olerelay)("%s:",relaystr(iname));
1353 TRACE_(olerelay)("%s(%d)",relaystr(fname),method);
1355 TRACE_(olerelay)("%d",method);
1356 TRACE_(olerelay)("(");
1359 if (iname) SysFreeString(iname);
1360 if (fname) SysFreeString(fname);
1362 memset(&buf,0,sizeof(buf));
1364 /* normal typelib driven serializing */
1366 /* Need them for hack below */
1367 memset(names,0,sizeof(names));
1368 if (ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames))
1370 if (nrofnames > sizeof(names)/sizeof(names[0]))
1371 ERR("Need more names!\n");
1374 for (i=0;i<fdesc->cParams;i++) {
1375 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1377 if (i) TRACE_(olerelay)(",");
1378 if (i+1<nrofnames && names[i+1])
1379 TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1381 /* No need to marshal other data than FIN and any VT_PTR. */
1382 if (!is_in_elem(elem) && (elem->tdesc.vt != VT_PTR)) {
1383 xargs+=_argsize(elem->tdesc.vt);
1384 if (relaydeb) TRACE_(olerelay)("[out]");
1387 hres = serialize_param(
1398 ERR("Failed to serialize param, hres %x\n",hres);
1401 xargs+=_argsize(elem->tdesc.vt);
1403 if (relaydeb) TRACE_(olerelay)(")");
1405 memset(&msg,0,sizeof(msg));
1406 msg.cbBuffer = buf.curoff;
1407 msg.iMethod = method;
1408 hres = IRpcChannelBuffer_GetBuffer(chanbuf,&msg,&(tpinfo->iid));
1410 ERR("RpcChannelBuffer GetBuffer failed, %x\n",hres);
1413 memcpy(msg.Buffer,buf.base,buf.curoff);
1414 if (relaydeb) TRACE_(olerelay)("\n");
1415 hres = IRpcChannelBuffer_SendReceive(chanbuf,&msg,&status);
1417 ERR("RpcChannelBuffer SendReceive failed, %x\n",hres);
1421 if (relaydeb) TRACE_(olerelay)(" status = %08x (",status);
1423 buf.base = HeapReAlloc(GetProcessHeap(),0,buf.base,msg.cbBuffer);
1425 buf.base = HeapAlloc(GetProcessHeap(),0,msg.cbBuffer);
1426 buf.size = msg.cbBuffer;
1427 memcpy(buf.base,msg.Buffer,buf.size);
1430 /* generic deserializer using typelib description */
1433 for (i=0;i<fdesc->cParams;i++) {
1434 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1437 if (i) TRACE_(olerelay)(",");
1438 if (i+1<nrofnames && names[i+1]) TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1440 /* No need to marshal other data than FOUT and any VT_PTR */
1441 if (!is_out_elem(elem) && (elem->tdesc.vt != VT_PTR)) {
1442 xargs += _argsize(elem->tdesc.vt);
1443 if (relaydeb) TRACE_(olerelay)("[in]");
1446 hres = deserialize_param(
1456 ERR("Failed to unmarshall param, hres %x\n",hres);
1460 xargs += _argsize(elem->tdesc.vt);
1463 hres = xbuf_get(&buf, (LPBYTE)&remoteresult, sizeof(DWORD));
1466 if (relaydeb) TRACE_(olerelay)(") = %08x\n", remoteresult);
1468 hres = remoteresult;
1471 IRpcChannelBuffer_FreeBuffer(chanbuf,&msg);
1472 for (i = 0; i < nrofnames; i++)
1473 SysFreeString(names[i]);
1474 HeapFree(GetProcessHeap(),0,buf.base);
1475 IRpcChannelBuffer_Release(chanbuf);
1476 ITypeInfo_Release(tinfo);
1477 TRACE("-- 0x%08x\n", hres);
1481 static HRESULT WINAPI ProxyIUnknown_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
1483 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1485 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1487 if (proxy->outerunknown)
1488 return IUnknown_QueryInterface(proxy->outerunknown, riid, ppv);
1490 FIXME("No interface\n");
1491 return E_NOINTERFACE;
1494 static ULONG WINAPI ProxyIUnknown_AddRef(IUnknown *iface)
1496 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1500 if (proxy->outerunknown)
1501 return IUnknown_AddRef(proxy->outerunknown);
1503 return 2; /* FIXME */
1506 static ULONG WINAPI ProxyIUnknown_Release(IUnknown *iface)
1508 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1512 if (proxy->outerunknown)
1513 return IUnknown_Release(proxy->outerunknown);
1515 return 1; /* FIXME */
1518 static HRESULT WINAPI ProxyIDispatch_GetTypeInfoCount(LPDISPATCH iface, UINT * pctinfo)
1520 TMProxyImpl *This = (TMProxyImpl *)iface;
1522 TRACE("(%p)\n", pctinfo);
1524 return IDispatch_GetTypeInfoCount(This->dispatch, pctinfo);
1527 static HRESULT WINAPI ProxyIDispatch_GetTypeInfo(LPDISPATCH iface, UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo)
1529 TMProxyImpl *This = (TMProxyImpl *)iface;
1531 TRACE("(%d, %x, %p)\n", iTInfo, lcid, ppTInfo);
1533 return IDispatch_GetTypeInfo(This->dispatch, iTInfo, lcid, ppTInfo);
1536 static HRESULT WINAPI ProxyIDispatch_GetIDsOfNames(LPDISPATCH iface, REFIID riid, LPOLESTR * rgszNames, UINT cNames, LCID lcid, DISPID * rgDispId)
1538 TMProxyImpl *This = (TMProxyImpl *)iface;
1540 TRACE("(%s, %p, %d, 0x%x, %p)\n", debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);
1542 return IDispatch_GetIDsOfNames(This->dispatch, riid, rgszNames,
1543 cNames, lcid, rgDispId);
1546 static HRESULT WINAPI ProxyIDispatch_Invoke(LPDISPATCH iface, DISPID dispIdMember, REFIID riid, LCID lcid,
1547 WORD wFlags, DISPPARAMS * pDispParams, VARIANT * pVarResult,
1548 EXCEPINFO * pExcepInfo, UINT * puArgErr)
1550 TMProxyImpl *This = (TMProxyImpl *)iface;
1552 TRACE("(%d, %s, 0x%x, 0x%x, %p, %p, %p, %p)\n", dispIdMember,
1553 debugstr_guid(riid), lcid, wFlags, pDispParams, pVarResult,
1554 pExcepInfo, puArgErr);
1556 return IDispatch_Invoke(This->dispatch, dispIdMember, riid, lcid,
1557 wFlags, pDispParams, pVarResult, pExcepInfo,
1563 const IRpcChannelBufferVtbl *lpVtbl;
1565 /* the IDispatch-derived interface we are handling */
1567 IRpcChannelBuffer *pDelegateChannel;
1568 } TMarshalDispatchChannel;
1570 static HRESULT WINAPI TMarshalDispatchChannel_QueryInterface(LPRPCCHANNELBUFFER iface, REFIID riid, LPVOID *ppv)
1573 if (IsEqualIID(riid,&IID_IRpcChannelBuffer) || IsEqualIID(riid,&IID_IUnknown))
1575 *ppv = (LPVOID)iface;
1576 IUnknown_AddRef(iface);
1579 return E_NOINTERFACE;
1582 static ULONG WINAPI TMarshalDispatchChannel_AddRef(LPRPCCHANNELBUFFER iface)
1584 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1585 return InterlockedIncrement(&This->refs);
1588 static ULONG WINAPI TMarshalDispatchChannel_Release(LPRPCCHANNELBUFFER iface)
1590 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1593 ref = InterlockedDecrement(&This->refs);
1597 IRpcChannelBuffer_Release(This->pDelegateChannel);
1598 HeapFree(GetProcessHeap(), 0, This);
1602 static HRESULT WINAPI TMarshalDispatchChannel_GetBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg, REFIID riid)
1604 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1605 TRACE("(%p, %s)\n", olemsg, debugstr_guid(riid));
1606 /* Note: we are pretending to invoke a method on the interface identified
1607 * by tmarshal_iid so that we can re-use the IDispatch proxy/stub code
1608 * without the RPC runtime getting confused by not exporting an IDispatch interface */
1609 return IRpcChannelBuffer_GetBuffer(This->pDelegateChannel, olemsg, &This->tmarshal_iid);
1612 static HRESULT WINAPI TMarshalDispatchChannel_SendReceive(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE *olemsg, ULONG *pstatus)
1614 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1615 TRACE("(%p, %p)\n", olemsg, pstatus);
1616 return IRpcChannelBuffer_SendReceive(This->pDelegateChannel, olemsg, pstatus);
1619 static HRESULT WINAPI TMarshalDispatchChannel_FreeBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg)
1621 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1622 TRACE("(%p)\n", olemsg);
1623 return IRpcChannelBuffer_FreeBuffer(This->pDelegateChannel, olemsg);
1626 static HRESULT WINAPI TMarshalDispatchChannel_GetDestCtx(LPRPCCHANNELBUFFER iface, DWORD* pdwDestContext, void** ppvDestContext)
1628 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1629 TRACE("(%p,%p)\n", pdwDestContext, ppvDestContext);
1630 return IRpcChannelBuffer_GetDestCtx(This->pDelegateChannel, pdwDestContext, ppvDestContext);
1633 static HRESULT WINAPI TMarshalDispatchChannel_IsConnected(LPRPCCHANNELBUFFER iface)
1635 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1637 return IRpcChannelBuffer_IsConnected(This->pDelegateChannel);
1640 static const IRpcChannelBufferVtbl TMarshalDispatchChannelVtbl =
1642 TMarshalDispatchChannel_QueryInterface,
1643 TMarshalDispatchChannel_AddRef,
1644 TMarshalDispatchChannel_Release,
1645 TMarshalDispatchChannel_GetBuffer,
1646 TMarshalDispatchChannel_SendReceive,
1647 TMarshalDispatchChannel_FreeBuffer,
1648 TMarshalDispatchChannel_GetDestCtx,
1649 TMarshalDispatchChannel_IsConnected
1652 static HRESULT TMarshalDispatchChannel_Create(
1653 IRpcChannelBuffer *pDelegateChannel, REFIID tmarshal_riid,
1654 IRpcChannelBuffer **ppChannel)
1656 TMarshalDispatchChannel *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1658 return E_OUTOFMEMORY;
1660 This->lpVtbl = &TMarshalDispatchChannelVtbl;
1662 IRpcChannelBuffer_AddRef(pDelegateChannel);
1663 This->pDelegateChannel = pDelegateChannel;
1664 This->tmarshal_iid = *tmarshal_riid;
1666 *ppChannel = (IRpcChannelBuffer *)&This->lpVtbl;
1671 static inline HRESULT get_facbuf_for_iid(REFIID riid, IPSFactoryBuffer **facbuf)
1676 if ((hr = CoGetPSClsid(riid, &clsid)))
1678 return CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL,
1679 &IID_IPSFactoryBuffer, (LPVOID*)facbuf);
1682 static HRESULT init_proxy_entry_point(TMProxyImpl *proxy, unsigned int num)
1685 /* nrofargs without This */
1688 TMAsmProxy *xasm = proxy->asmstubs + num;
1690 const FUNCDESC *fdesc;
1692 hres = get_funcdesc(proxy->tinfo, num, &tinfo2, &fdesc, NULL, NULL, NULL);
1694 ERR("GetFuncDesc %x should not fail here.\n",hres);
1697 ITypeInfo_Release(tinfo2);
1698 /* some args take more than 4 byte on the stack */
1700 for (j=0;j<fdesc->cParams;j++)
1701 nrofargs += _argsize(fdesc->lprgelemdescParam[j].tdesc.vt);
1704 if (fdesc->callconv != CC_STDCALL) {
1705 ERR("calling convention is not stdcall????\n");
1708 /* popl %eax - return ptr
1715 * arg3 arg2 arg1 <method> <returnptr>
1717 xasm->popleax = 0x58;
1718 xasm->pushlval = 0x68;
1720 xasm->pushleax = 0x50;
1721 xasm->lcall = 0xe8; /* relative jump */
1722 xasm->xcall = (DWORD)xCall;
1723 xasm->xcall -= (DWORD)&(xasm->lret);
1725 xasm->bytestopop = (nrofargs+2)*4; /* pop args, This, iMethod */
1727 proxy->lpvtbl[num] = xasm;
1729 FIXME("not implemented on non i386\n");
1735 static HRESULT WINAPI
1736 PSFacBuf_CreateProxy(
1737 LPPSFACTORYBUFFER iface, IUnknown* pUnkOuter, REFIID riid,
1738 IRpcProxyBuffer **ppProxy, LPVOID *ppv)
1742 unsigned int i, nroffuncs;
1745 BOOL defer_to_dispatch = FALSE;
1747 TRACE("(...%s...)\n",debugstr_guid(riid));
1748 hres = _get_typeinfo_for_iid(riid,&tinfo);
1750 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
1754 hres = num_of_funcs(tinfo, &nroffuncs);
1756 ERR("Cannot get number of functions for typeinfo %s\n",debugstr_guid(riid));
1757 ITypeInfo_Release(tinfo);
1761 proxy = CoTaskMemAlloc(sizeof(TMProxyImpl));
1762 if (!proxy) return E_OUTOFMEMORY;
1764 assert(sizeof(TMAsmProxy) == 16);
1766 proxy->dispatch = NULL;
1767 proxy->dispatch_proxy = NULL;
1768 proxy->outerunknown = pUnkOuter;
1769 proxy->asmstubs = VirtualAlloc(NULL, sizeof(TMAsmProxy) * nroffuncs, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
1770 if (!proxy->asmstubs) {
1771 ERR("Could not commit pages for proxy thunks\n");
1772 CoTaskMemFree(proxy);
1773 return E_OUTOFMEMORY;
1775 proxy->lpvtbl2 = &tmproxyvtable;
1776 /* one reference for the proxy */
1778 proxy->tinfo = tinfo;
1779 memcpy(&proxy->iid,riid,sizeof(*riid));
1782 InitializeCriticalSection(&proxy->crit);
1783 proxy->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TMProxyImpl.crit");
1785 proxy->lpvtbl = HeapAlloc(GetProcessHeap(),0,sizeof(LPBYTE)*nroffuncs);
1787 /* if we derive from IDispatch then defer to its proxy for its methods */
1788 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
1791 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
1793 IPSFactoryBuffer *factory_buffer;
1794 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
1797 hres = IPSFactoryBuffer_CreateProxy(factory_buffer, NULL,
1798 &IID_IDispatch, &proxy->dispatch_proxy,
1799 (void **)&proxy->dispatch);
1800 IPSFactoryBuffer_Release(factory_buffer);
1802 if ((hres == S_OK) && (nroffuncs < 7))
1804 ERR("nroffuncs calculated incorrectly (%d)\n", nroffuncs);
1805 hres = E_UNEXPECTED;
1809 defer_to_dispatch = TRUE;
1812 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
1815 for (i=0;i<nroffuncs;i++) {
1818 proxy->lpvtbl[i] = ProxyIUnknown_QueryInterface;
1821 proxy->lpvtbl[i] = ProxyIUnknown_AddRef;
1824 proxy->lpvtbl[i] = ProxyIUnknown_Release;
1827 if(!defer_to_dispatch)
1829 hres = init_proxy_entry_point(proxy, i);
1830 if(FAILED(hres)) return hres;
1832 else proxy->lpvtbl[3] = ProxyIDispatch_GetTypeInfoCount;
1835 if(!defer_to_dispatch)
1837 hres = init_proxy_entry_point(proxy, i);
1838 if(FAILED(hres)) return hres;
1840 else proxy->lpvtbl[4] = ProxyIDispatch_GetTypeInfo;
1843 if(!defer_to_dispatch)
1845 hres = init_proxy_entry_point(proxy, i);
1846 if(FAILED(hres)) return hres;
1848 else proxy->lpvtbl[5] = ProxyIDispatch_GetIDsOfNames;
1851 if(!defer_to_dispatch)
1853 hres = init_proxy_entry_point(proxy, i);
1854 if(FAILED(hres)) return hres;
1856 else proxy->lpvtbl[6] = ProxyIDispatch_Invoke;
1859 hres = init_proxy_entry_point(proxy, i);
1860 if(FAILED(hres)) return hres;
1866 *ppv = (LPVOID)proxy;
1867 *ppProxy = (IRpcProxyBuffer *)&(proxy->lpvtbl2);
1868 IUnknown_AddRef((IUnknown *)*ppv);
1872 TMProxyImpl_Release((IRpcProxyBuffer *)&proxy->lpvtbl2);
1876 typedef struct _TMStubImpl {
1877 const IRpcStubBufferVtbl *lpvtbl;
1883 IRpcStubBuffer *dispatch_stub;
1884 BOOL dispatch_derivative;
1887 static HRESULT WINAPI
1888 TMStubImpl_QueryInterface(LPRPCSTUBBUFFER iface, REFIID riid, LPVOID *ppv)
1890 if (IsEqualIID(riid,&IID_IRpcStubBuffer)||IsEqualIID(riid,&IID_IUnknown)){
1891 *ppv = (LPVOID)iface;
1892 IRpcStubBuffer_AddRef(iface);
1895 FIXME("%s, not supported IID.\n",debugstr_guid(riid));
1896 return E_NOINTERFACE;
1900 TMStubImpl_AddRef(LPRPCSTUBBUFFER iface)
1902 TMStubImpl *This = (TMStubImpl *)iface;
1903 ULONG refCount = InterlockedIncrement(&This->ref);
1905 TRACE("(%p)->(ref before=%u)\n", This, refCount - 1);
1911 TMStubImpl_Release(LPRPCSTUBBUFFER iface)
1913 TMStubImpl *This = (TMStubImpl *)iface;
1914 ULONG refCount = InterlockedDecrement(&This->ref);
1916 TRACE("(%p)->(ref before=%u)\n", This, refCount + 1);
1920 IRpcStubBuffer_Disconnect(iface);
1921 ITypeInfo_Release(This->tinfo);
1922 if (This->dispatch_stub)
1923 IRpcStubBuffer_Release(This->dispatch_stub);
1924 CoTaskMemFree(This);
1929 static HRESULT WINAPI
1930 TMStubImpl_Connect(LPRPCSTUBBUFFER iface, LPUNKNOWN pUnkServer)
1932 TMStubImpl *This = (TMStubImpl *)iface;
1934 TRACE("(%p)->(%p)\n", This, pUnkServer);
1936 IUnknown_AddRef(pUnkServer);
1937 This->pUnk = pUnkServer;
1939 if (This->dispatch_stub)
1940 IRpcStubBuffer_Connect(This->dispatch_stub, pUnkServer);
1946 TMStubImpl_Disconnect(LPRPCSTUBBUFFER iface)
1948 TMStubImpl *This = (TMStubImpl *)iface;
1950 TRACE("(%p)->()\n", This);
1954 IUnknown_Release(This->pUnk);
1958 if (This->dispatch_stub)
1959 IRpcStubBuffer_Disconnect(This->dispatch_stub);
1962 static HRESULT WINAPI
1964 LPRPCSTUBBUFFER iface, RPCOLEMESSAGE* xmsg,IRpcChannelBuffer*rpcchanbuf)
1967 const FUNCDESC *fdesc;
1968 TMStubImpl *This = (TMStubImpl *)iface;
1970 DWORD *args = NULL, res, *xargs, nrofargs;
1975 ITypeInfo *tinfo = NULL;
1979 if (xmsg->iMethod < 3) {
1980 ERR("IUnknown methods cannot be marshaled by the typelib marshaler\n");
1981 return E_UNEXPECTED;
1984 if (This->dispatch_derivative && xmsg->iMethod < sizeof(IDispatchVtbl)/sizeof(void *))
1986 IPSFactoryBuffer *factory_buffer;
1987 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
1990 hres = IPSFactoryBuffer_CreateStub(factory_buffer, &IID_IDispatch,
1991 This->pUnk, &This->dispatch_stub);
1992 IPSFactoryBuffer_Release(factory_buffer);
1996 return IRpcStubBuffer_Invoke(This->dispatch_stub, xmsg, rpcchanbuf);
1999 memset(&buf,0,sizeof(buf));
2000 buf.size = xmsg->cbBuffer;
2001 buf.base = HeapAlloc(GetProcessHeap(), 0, xmsg->cbBuffer);
2002 memcpy(buf.base, xmsg->Buffer, xmsg->cbBuffer);
2005 hres = get_funcdesc(This->tinfo,xmsg->iMethod,&tinfo,&fdesc,&iname,NULL,NULL);
2007 ERR("GetFuncDesc on method %d failed with %x\n",xmsg->iMethod,hres);
2011 if (iname && !lstrcmpW(iname, IDispatchW))
2013 ERR("IDispatch cannot be marshaled by the typelib marshaler\n");
2014 hres = E_UNEXPECTED;
2015 SysFreeString (iname);
2019 if (iname) SysFreeString (iname);
2021 /* Need them for hack below */
2022 memset(names,0,sizeof(names));
2023 ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames);
2024 if (nrofnames > sizeof(names)/sizeof(names[0])) {
2025 ERR("Need more names!\n");
2028 /*dump_FUNCDESC(fdesc);*/
2030 for (i=0;i<fdesc->cParams;i++)
2031 nrofargs += _argsize(fdesc->lprgelemdescParam[i].tdesc.vt);
2032 args = HeapAlloc(GetProcessHeap(),0,(nrofargs+1)*sizeof(DWORD));
2035 hres = E_OUTOFMEMORY;
2039 /* Allocate all stuff used by call. */
2041 for (i=0;i<fdesc->cParams;i++) {
2042 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2044 hres = deserialize_param(
2053 xargs += _argsize(elem->tdesc.vt);
2055 ERR("Failed to deserialize param %s, hres %x\n",relaystr(names[i+1]),hres);
2060 args[0] = (DWORD)This->pUnk;
2065 (*((FARPROC**)args[0]))[fdesc->oVft/4],
2073 DWORD dwExceptionCode = GetExceptionCode();
2074 ERR("invoke call failed with exception 0x%08x (%d)\n", dwExceptionCode, dwExceptionCode);
2075 if (FAILED(dwExceptionCode))
2076 hres = dwExceptionCode;
2078 hres = HRESULT_FROM_WIN32(dwExceptionCode);
2088 for (i=0;i<fdesc->cParams;i++) {
2089 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2090 hres = serialize_param(
2099 xargs += _argsize(elem->tdesc.vt);
2101 ERR("Failed to stuballoc param, hres %x\n",hres);
2106 hres = xbuf_add (&buf, (LPBYTE)&res, sizeof(DWORD));
2111 xmsg->cbBuffer = buf.curoff;
2112 hres = IRpcChannelBuffer_GetBuffer(rpcchanbuf, xmsg, &This->iid);
2114 ERR("IRpcChannelBuffer_GetBuffer failed with error 0x%08x\n", hres);
2117 memcpy(xmsg->Buffer, buf.base, buf.curoff);
2120 for (i = 0; i < nrofnames; i++)
2121 SysFreeString(names[i]);
2123 ITypeInfo_Release(tinfo);
2124 HeapFree(GetProcessHeap(), 0, args);
2126 HeapFree(GetProcessHeap(), 0, buf.base);
2128 TRACE("returning\n");
2132 static LPRPCSTUBBUFFER WINAPI
2133 TMStubImpl_IsIIDSupported(LPRPCSTUBBUFFER iface, REFIID riid) {
2134 FIXME("Huh (%s)?\n",debugstr_guid(riid));
2139 TMStubImpl_CountRefs(LPRPCSTUBBUFFER iface) {
2140 TMStubImpl *This = (TMStubImpl *)iface;
2143 return This->ref; /*FIXME? */
2146 static HRESULT WINAPI
2147 TMStubImpl_DebugServerQueryInterface(LPRPCSTUBBUFFER iface, LPVOID *ppv) {
2152 TMStubImpl_DebugServerRelease(LPRPCSTUBBUFFER iface, LPVOID ppv) {
2156 static const IRpcStubBufferVtbl tmstubvtbl = {
2157 TMStubImpl_QueryInterface,
2161 TMStubImpl_Disconnect,
2163 TMStubImpl_IsIIDSupported,
2164 TMStubImpl_CountRefs,
2165 TMStubImpl_DebugServerQueryInterface,
2166 TMStubImpl_DebugServerRelease
2169 static HRESULT WINAPI
2170 PSFacBuf_CreateStub(
2171 LPPSFACTORYBUFFER iface, REFIID riid,IUnknown *pUnkServer,
2172 IRpcStubBuffer** ppStub
2179 TRACE("(%s,%p,%p)\n",debugstr_guid(riid),pUnkServer,ppStub);
2181 hres = _get_typeinfo_for_iid(riid,&tinfo);
2183 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
2187 stub = CoTaskMemAlloc(sizeof(TMStubImpl));
2189 return E_OUTOFMEMORY;
2190 stub->lpvtbl = &tmstubvtbl;
2192 stub->tinfo = tinfo;
2193 stub->dispatch_stub = NULL;
2194 stub->dispatch_derivative = FALSE;
2195 memcpy(&(stub->iid),riid,sizeof(*riid));
2196 hres = IRpcStubBuffer_Connect((LPRPCSTUBBUFFER)stub,pUnkServer);
2197 *ppStub = (LPRPCSTUBBUFFER)stub;
2198 TRACE("IRpcStubBuffer: %p\n", stub);
2200 ERR("Connect to pUnkServer failed?\n");
2202 /* if we derive from IDispatch then defer to its stub for some of its methods */
2203 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
2206 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
2207 stub->dispatch_derivative = TRUE;
2208 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
2214 static const IPSFactoryBufferVtbl psfacbufvtbl = {
2215 PSFacBuf_QueryInterface,
2218 PSFacBuf_CreateProxy,
2222 /* This is the whole PSFactoryBuffer object, just the vtableptr */
2223 static const IPSFactoryBufferVtbl *lppsfac = &psfacbufvtbl;
2225 /***********************************************************************
2226 * TMARSHAL_DllGetClassObject
2228 HRESULT TMARSHAL_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
2230 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)) {
2234 return E_NOINTERFACE;