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 return IRpcProxyBuffer_Connect(This->dispatch_proxy, pDelegateChannel);
481 TMProxyImpl_Disconnect(LPRPCPROXYBUFFER iface)
483 ICOM_THIS_MULTI(TMProxyImpl, lpvtbl2, iface);
487 EnterCriticalSection(&This->crit);
489 IRpcChannelBuffer_Release(This->chanbuf);
490 This->chanbuf = NULL;
492 LeaveCriticalSection(&This->crit);
494 if (This->dispatch_proxy)
495 IRpcProxyBuffer_Disconnect(This->dispatch_proxy);
499 static const IRpcProxyBufferVtbl tmproxyvtable = {
500 TMProxyImpl_QueryInterface,
504 TMProxyImpl_Disconnect
507 /* how much space do we use on stack in DWORD steps. */
512 return 8/sizeof(DWORD);
514 return sizeof(double)/sizeof(DWORD);
516 return sizeof(CY)/sizeof(DWORD);
518 return sizeof(DATE)/sizeof(DWORD);
520 return (sizeof(VARIANT)+3)/sizeof(DWORD);
527 _xsize(const TYPEDESC *td) {
532 return sizeof(VARIANT)+3;
535 const ARRAYDESC *adesc = td->u.lpadesc;
537 for (i=0;i<adesc->cDims;i++)
538 arrsize *= adesc->rgbounds[i].cElements;
539 return arrsize*_xsize(&adesc->tdescElem);
567 TRACE("(tdesc.vt %s)\n",debugstr_vt(tdesc->vt));
570 case VT_EMPTY: /* nothing. empty variant for instance */
576 if (debugout) TRACE_(olerelay)("%x%x\n",arg[0],arg[1]);
578 hres = xbuf_add(buf,(LPBYTE)arg,8);
588 if (debugout) TRACE_(olerelay)("%x\n",*arg);
590 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
595 if (debugout) TRACE_(olerelay)("%04x\n",*arg & 0xffff);
597 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
602 if (debugout) TRACE_(olerelay)("%02x\n",*arg & 0xff);
604 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
608 if (debugout) TRACE_(olerelay)("&0x%x\n",*arg);
610 hres = xbuf_add(buf,(LPBYTE)(DWORD*)*arg,sizeof(DWORD));
611 /* do not dealloc at this time */
615 VARIANT *vt = (VARIANT*)arg;
616 DWORD vttype = V_VT(vt);
618 if (debugout) TRACE_(olerelay)("Vt(%s%s)(",debugstr_vt(vttype),debugstr_vf(vttype));
621 hres = xbuf_add(buf,(LPBYTE)&vttype,sizeof(vttype));
622 if (hres) return hres;
624 /* need to recurse since we need to free the stuff */
625 hres = serialize_param(tinfo,writeit,debugout,dealloc,&tdesc2,(DWORD*)&(V_I4(vt)),buf);
626 if (debugout) TRACE_(olerelay)(")");
629 case VT_BSTR|VT_BYREF: {
630 if (debugout) TRACE_(olerelay)("[byref]'%s'", *(BSTR*)*arg ? relaystr(*((BSTR*)*arg)) : "<bstr NULL>");
632 /* ptr to ptr to magic widestring, basically */
633 BSTR *bstr = (BSTR *) *arg;
636 /* -1 means "null string" which is equivalent to empty string */
638 hres = xbuf_add(buf, (LPBYTE)&len,sizeof(DWORD));
639 if (hres) return hres;
641 len = *((DWORD*)*bstr-1)/sizeof(WCHAR);
642 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
643 if (hres) return hres;
644 hres = xbuf_add(buf,(LPBYTE)*bstr,len * sizeof(WCHAR));
645 if (hres) return hres;
649 if (dealloc && arg) {
650 BSTR *str = *((BSTR **)arg);
659 TRACE_(olerelay)("%s",relaystr((WCHAR*)*arg));
661 TRACE_(olerelay)("<bstr NULL>");
664 BSTR bstr = (BSTR)*arg;
668 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
669 if (hres) return hres;
671 len = *((DWORD*)bstr-1)/sizeof(WCHAR);
672 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
673 if (hres) return hres;
674 hres = xbuf_add(buf,(LPBYTE)bstr,len * sizeof(WCHAR));
675 if (hres) return hres;
680 SysFreeString((BSTR)*arg);
685 BOOL derefhere = TRUE;
687 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
691 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
693 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
696 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
697 switch (tattr->typekind) {
698 case TKIND_ENUM: /* confirmed */
699 case TKIND_RECORD: /* FIXME: mostly untested */
702 case TKIND_ALIAS: /* FIXME: untested */
703 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
704 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
708 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
712 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
713 ITypeInfo_Release(tinfo2);
716 if (debugout) TRACE_(olerelay)("*");
717 /* Write always, so the other side knows when it gets a NULL pointer.
719 cookie = *arg ? 0x42424242 : 0;
720 hres = xbuf_add(buf,(LPBYTE)&cookie,sizeof(cookie));
724 if (debugout) TRACE_(olerelay)("NULL");
727 hres = serialize_param(tinfo,writeit,debugout,dealloc,tdesc->u.lptdesc,(DWORD*)*arg,buf);
728 if (derefhere && dealloc) HeapFree(GetProcessHeap(),0,(LPVOID)*arg);
732 if (debugout) TRACE_(olerelay)("unk(0x%x)",*arg);
734 hres = _marshal_interface(buf,&IID_IUnknown,(LPUNKNOWN)*arg);
735 if (dealloc && *(IUnknown **)arg)
736 IUnknown_Release((LPUNKNOWN)*arg);
739 if (debugout) TRACE_(olerelay)("idisp(0x%x)",*arg);
741 hres = _marshal_interface(buf,&IID_IDispatch,(LPUNKNOWN)*arg);
742 if (dealloc && *(IUnknown **)arg)
743 IUnknown_Release((LPUNKNOWN)*arg);
746 if (debugout) TRACE_(olerelay)("<void>");
748 case VT_USERDEFINED: {
752 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
754 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
757 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
758 switch (tattr->typekind) {
760 case TKIND_INTERFACE:
762 hres=_marshal_interface(buf,&(tattr->guid),(LPUNKNOWN)arg);
764 IUnknown_Release((LPUNKNOWN)arg);
768 if (debugout) TRACE_(olerelay)("{");
769 for (i=0;i<tattr->cVars;i++) {
774 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
776 ERR("Could not get vardesc of %d\n",i);
779 elem2 = &vdesc->elemdescVar;
780 tdesc2 = &elem2->tdesc;
781 hres = serialize_param(
787 (DWORD*)(((LPBYTE)arg)+vdesc->u.oInst),
790 ITypeInfo_ReleaseVarDesc(tinfo2, vdesc);
793 if (debugout && (i<(tattr->cVars-1)))
794 TRACE_(olerelay)(",");
796 if (debugout) TRACE_(olerelay)("}");
800 hres = serialize_param(tinfo2,writeit,debugout,dealloc,&tattr->tdescAlias,arg,buf);
804 if (debugout) TRACE_(olerelay)("%x",*arg);
806 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
809 FIXME("Unhandled typekind %d\n",tattr->typekind);
813 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
814 ITypeInfo_Release(tinfo2);
818 ARRAYDESC *adesc = tdesc->u.lpadesc;
821 if (debugout) TRACE_(olerelay)("carr");
822 for (i=0;i<adesc->cDims;i++) {
823 if (debugout) TRACE_(olerelay)("[%d]",adesc->rgbounds[i].cElements);
824 arrsize *= adesc->rgbounds[i].cElements;
826 if (debugout) TRACE_(olerelay)("(vt %s)",debugstr_vt(adesc->tdescElem.vt));
827 if (debugout) TRACE_(olerelay)("[");
828 for (i=0;i<arrsize;i++) {
829 hres = serialize_param(tinfo, writeit, debugout, dealloc, &adesc->tdescElem, (DWORD*)((LPBYTE)arg+i*_xsize(&adesc->tdescElem)), buf);
832 if (debugout && (i<arrsize-1)) TRACE_(olerelay)(",");
834 if (debugout) TRACE_(olerelay)("]");
840 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
841 ULONG size = LPSAFEARRAY_UserSize(&flags, buf->curoff, (LPSAFEARRAY *)arg);
842 xbuf_resize(buf, size);
843 LPSAFEARRAY_UserMarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
849 ERR("Unhandled marshal type %d.\n",tdesc->vt);
866 TRACE("vt %s at %p\n",debugstr_vt(tdesc->vt),arg);
871 if (debugout) TRACE_(olerelay)("<empty>\n");
874 if (debugout) TRACE_(olerelay)("<null>\n");
877 VARIANT *vt = (VARIANT*)arg;
882 hres = xbuf_get(buf,(LPBYTE)&vttype,sizeof(vttype));
884 FIXME("vt type not read?\n");
887 memset(&tdesc2,0,sizeof(tdesc2));
890 if (debugout) TRACE_(olerelay)("Vt(%s%s)(",debugstr_vt(vttype),debugstr_vf(vttype));
891 hres = deserialize_param(tinfo, readit, debugout, alloc, &tdesc2, (DWORD*)&(V_I4(vt)), buf);
892 TRACE_(olerelay)(")");
903 hres = xbuf_get(buf,(LPBYTE)arg,8);
904 if (hres) ERR("Failed to read integer 8 byte\n");
906 if (debugout) TRACE_(olerelay)("%x%x",arg[0],arg[1]);
916 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
917 if (hres) ERR("Failed to read integer 4 byte\n");
919 if (debugout) TRACE_(olerelay)("%x",*arg);
925 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
926 if (hres) ERR("Failed to read integer 4 byte\n");
929 if (debugout) TRACE_(olerelay)("%04x",*arg & 0xffff);
935 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
936 if (hres) ERR("Failed to read integer 4 byte\n");
939 if (debugout) TRACE_(olerelay)("%02x",*arg & 0xff);
944 *arg = (DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
946 hres = xbuf_get(buf,(LPBYTE)*arg,sizeof(DWORD));
947 if (hres) ERR("Failed to read integer 4 byte\n");
949 if (debugout) TRACE_(olerelay)("&0x%x",*(DWORD*)*arg);
951 case VT_BSTR|VT_BYREF: {
952 BSTR **bstr = (BSTR **)arg;
957 hres = xbuf_get(buf,(LPBYTE)&len,sizeof(DWORD));
959 ERR("failed to read bstr klen\n");
963 *bstr = CoTaskMemAlloc(sizeof(BSTR *));
965 if (debugout) TRACE_(olerelay)("<bstr NULL>");
967 str = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(len+1)*sizeof(WCHAR));
968 hres = xbuf_get(buf,(LPBYTE)str,len*sizeof(WCHAR));
970 ERR("Failed to read BSTR.\n");
971 HeapFree(GetProcessHeap(),0,str);
974 *bstr = CoTaskMemAlloc(sizeof(BSTR *));
975 **bstr = SysAllocStringLen(str,len);
976 if (debugout) TRACE_(olerelay)("%s",relaystr(str));
977 HeapFree(GetProcessHeap(),0,str);
989 hres = xbuf_get(buf,(LPBYTE)&len,sizeof(DWORD));
991 ERR("failed to read bstr klen\n");
996 if (debugout) TRACE_(olerelay)("<bstr NULL>");
998 str = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(len+1)*sizeof(WCHAR));
999 hres = xbuf_get(buf,(LPBYTE)str,len*sizeof(WCHAR));
1001 ERR("Failed to read BSTR.\n");
1002 HeapFree(GetProcessHeap(),0,str);
1005 *arg = (DWORD)SysAllocStringLen(str,len);
1006 if (debugout) TRACE_(olerelay)("%s",relaystr(str));
1007 HeapFree(GetProcessHeap(),0,str);
1016 BOOL derefhere = TRUE;
1018 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
1022 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
1024 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
1027 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1028 switch (tattr->typekind) {
1029 case TKIND_ENUM: /* confirmed */
1030 case TKIND_RECORD: /* FIXME: mostly untested */
1033 case TKIND_ALIAS: /* FIXME: untested */
1034 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
1035 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
1039 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
1043 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1044 ITypeInfo_Release(tinfo2);
1046 /* read it in all cases, we need to know if we have
1047 * NULL pointer or not.
1049 hres = xbuf_get(buf,(LPBYTE)&cookie,sizeof(cookie));
1051 ERR("Failed to load pointer cookie.\n");
1054 if (cookie != 0x42424242) {
1055 /* we read a NULL ptr from the remote side */
1056 if (debugout) TRACE_(olerelay)("NULL");
1060 if (debugout) TRACE_(olerelay)("*");
1062 /* Allocate space for the referenced struct */
1064 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,_xsize(tdesc->u.lptdesc));
1067 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, (LPDWORD)*arg, buf);
1069 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, arg, buf);
1072 /* FIXME: UNKNOWN is unknown ..., but allocate 4 byte for it */
1074 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
1077 hres = _unmarshal_interface(buf,&IID_IUnknown,(LPUNKNOWN*)arg);
1079 TRACE_(olerelay)("unk(%p)",arg);
1084 hres = _unmarshal_interface(buf,&IID_IDispatch,(LPUNKNOWN*)arg);
1086 TRACE_(olerelay)("idisp(%p)",arg);
1089 if (debugout) TRACE_(olerelay)("<void>");
1091 case VT_USERDEFINED: {
1095 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
1097 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
1100 hres = ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1102 ERR("Could not get typeattr in VT_USERDEFINED.\n");
1104 switch (tattr->typekind) {
1105 case TKIND_DISPATCH:
1106 case TKIND_INTERFACE:
1108 hres = _unmarshal_interface(buf,&(tattr->guid),(LPUNKNOWN*)arg);
1110 case TKIND_RECORD: {
1114 *arg = (DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,tattr->cbSizeInstance);
1116 if (debugout) TRACE_(olerelay)("{");
1117 for (i=0;i<tattr->cVars;i++) {
1120 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
1122 ERR("Could not get vardesc of %d\n",i);
1123 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1124 ITypeInfo_Release(tinfo2);
1127 hres = deserialize_param(
1132 &vdesc->elemdescVar.tdesc,
1133 (DWORD*)(((LPBYTE)*arg)+vdesc->u.oInst),
1136 ITypeInfo2_ReleaseVarDesc(tinfo2, vdesc);
1137 if (debugout && (i<tattr->cVars-1)) TRACE_(olerelay)(",");
1139 if (debugout) TRACE_(olerelay)("}");
1143 hres = deserialize_param(tinfo2,readit,debugout,alloc,&tattr->tdescAlias,arg,buf);
1147 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
1148 if (hres) ERR("Failed to read enum (4 byte)\n");
1150 if (debugout) TRACE_(olerelay)("%x",*arg);
1153 ERR("Unhandled typekind %d\n",tattr->typekind);
1157 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1160 ERR("failed to stuballoc in TKIND_RECORD.\n");
1161 ITypeInfo_Release(tinfo2);
1165 /* arg is pointing to the start of the array. */
1166 ARRAYDESC *adesc = tdesc->u.lpadesc;
1169 if (adesc->cDims > 1) FIXME("cDims > 1 in VT_CARRAY. Does it work?\n");
1170 for (i=0;i<adesc->cDims;i++)
1171 arrsize *= adesc->rgbounds[i].cElements;
1172 for (i=0;i<arrsize;i++)
1179 (DWORD*)((LPBYTE)(arg)+i*_xsize(&adesc->tdescElem)),
1184 case VT_SAFEARRAY: {
1187 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
1188 unsigned char *buffer;
1189 buffer = LPSAFEARRAY_UserUnmarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
1190 buf->curoff = buffer - buf->base;
1195 ERR("No handler for VT type %d!\n",tdesc->vt);
1201 /* Retrieves a function's funcdesc, searching back into inherited interfaces. */
1202 static HRESULT get_funcdesc(ITypeInfo *tinfo, int iMethod, ITypeInfo **tactual, const FUNCDESC **fdesc,
1203 BSTR *iname, BSTR *fname, UINT *num)
1207 UINT inherited_funcs = 0;
1210 if (fname) *fname = NULL;
1211 if (iname) *iname = NULL;
1215 hr = ITypeInfo_GetTypeAttr(tinfo, &attr);
1218 ERR("GetTypeAttr failed with %x\n",hr);
1222 if(attr->typekind == TKIND_DISPATCH)
1224 if(attr->wTypeFlags & TYPEFLAG_FDUAL)
1229 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, -1, &href);
1232 ERR("Cannot get interface href from dual dispinterface\n");
1233 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1236 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
1239 ERR("Cannot get interface from dual dispinterface\n");
1240 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1243 hr = get_funcdesc(tinfo2, iMethod, tactual, fdesc, iname, fname, num);
1244 ITypeInfo_Release(tinfo2);
1245 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1248 ERR("Shouldn't be called with a non-dual dispinterface\n");
1252 impl_types = attr->cImplTypes;
1253 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1255 for (i = 0; i < impl_types; i++)
1258 ITypeInfo *pSubTypeInfo;
1261 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, i, &href);
1262 if (FAILED(hr)) return hr;
1263 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &pSubTypeInfo);
1264 if (FAILED(hr)) return hr;
1266 hr = get_funcdesc(pSubTypeInfo, iMethod, tactual, fdesc, iname, fname, &sub_funcs);
1267 inherited_funcs += sub_funcs;
1268 ITypeInfo_Release(pSubTypeInfo);
1269 if(SUCCEEDED(hr)) return hr;
1271 if(iMethod < inherited_funcs)
1273 ERR("shouldn't be here\n");
1274 return E_INVALIDARG;
1277 for(i = inherited_funcs; i <= iMethod; i++)
1279 hr = ITypeInfoImpl_GetInternalFuncDesc(tinfo, i - inherited_funcs, fdesc);
1287 /* found it. We don't care about num so zero it */
1290 ITypeInfo_AddRef(*tactual);
1291 if (fname) ITypeInfo_GetDocumentation(tinfo,(*fdesc)->memid,fname,NULL,NULL,NULL);
1292 if (iname) ITypeInfo_GetDocumentation(tinfo,-1,iname,NULL,NULL,NULL);
1296 static inline BOOL is_in_elem(const ELEMDESC *elem)
1298 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN || !elem->u.paramdesc.wParamFlags);
1301 static inline BOOL is_out_elem(const ELEMDESC *elem)
1303 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT || !elem->u.paramdesc.wParamFlags);
1307 xCall(LPVOID retptr, int method, TMProxyImpl *tpinfo /*, args */)
1309 DWORD *args = ((DWORD*)&tpinfo)+1, *xargs;
1310 const FUNCDESC *fdesc;
1312 int i, relaydeb = TRACE_ON(olerelay);
1319 DWORD remoteresult = 0;
1321 IRpcChannelBuffer *chanbuf;
1323 EnterCriticalSection(&tpinfo->crit);
1325 hres = get_funcdesc(tpinfo->tinfo,method,&tinfo,&fdesc,&iname,&fname,NULL);
1327 ERR("Did not find typeinfo/funcdesc entry for method %d!\n",method);
1328 LeaveCriticalSection(&tpinfo->crit);
1332 if (!tpinfo->chanbuf)
1334 WARN("Tried to use disconnected proxy\n");
1335 ITypeInfo_Release(tinfo);
1336 LeaveCriticalSection(&tpinfo->crit);
1337 return RPC_E_DISCONNECTED;
1339 chanbuf = tpinfo->chanbuf;
1340 IRpcChannelBuffer_AddRef(chanbuf);
1342 LeaveCriticalSection(&tpinfo->crit);
1345 TRACE_(olerelay)("->");
1347 TRACE_(olerelay)("%s:",relaystr(iname));
1349 TRACE_(olerelay)("%s(%d)",relaystr(fname),method);
1351 TRACE_(olerelay)("%d",method);
1352 TRACE_(olerelay)("(");
1355 if (iname) SysFreeString(iname);
1356 if (fname) SysFreeString(fname);
1358 memset(&buf,0,sizeof(buf));
1360 /* normal typelib driven serializing */
1362 /* Need them for hack below */
1363 memset(names,0,sizeof(names));
1364 if (ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames))
1366 if (nrofnames > sizeof(names)/sizeof(names[0]))
1367 ERR("Need more names!\n");
1370 for (i=0;i<fdesc->cParams;i++) {
1371 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1373 if (i) TRACE_(olerelay)(",");
1374 if (i+1<nrofnames && names[i+1])
1375 TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1377 /* No need to marshal other data than FIN and any VT_PTR. */
1378 if (!is_in_elem(elem) && (elem->tdesc.vt != VT_PTR)) {
1379 xargs+=_argsize(elem->tdesc.vt);
1380 if (relaydeb) TRACE_(olerelay)("[out]");
1383 hres = serialize_param(
1394 ERR("Failed to serialize param, hres %x\n",hres);
1397 xargs+=_argsize(elem->tdesc.vt);
1399 if (relaydeb) TRACE_(olerelay)(")");
1401 memset(&msg,0,sizeof(msg));
1402 msg.cbBuffer = buf.curoff;
1403 msg.iMethod = method;
1404 hres = IRpcChannelBuffer_GetBuffer(chanbuf,&msg,&(tpinfo->iid));
1406 ERR("RpcChannelBuffer GetBuffer failed, %x\n",hres);
1409 memcpy(msg.Buffer,buf.base,buf.curoff);
1410 if (relaydeb) TRACE_(olerelay)("\n");
1411 hres = IRpcChannelBuffer_SendReceive(chanbuf,&msg,&status);
1413 ERR("RpcChannelBuffer SendReceive failed, %x\n",hres);
1417 if (relaydeb) TRACE_(olerelay)(" status = %08x (",status);
1419 buf.base = HeapReAlloc(GetProcessHeap(),0,buf.base,msg.cbBuffer);
1421 buf.base = HeapAlloc(GetProcessHeap(),0,msg.cbBuffer);
1422 buf.size = msg.cbBuffer;
1423 memcpy(buf.base,msg.Buffer,buf.size);
1426 /* generic deserializer using typelib description */
1429 for (i=0;i<fdesc->cParams;i++) {
1430 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1433 if (i) TRACE_(olerelay)(",");
1434 if (i+1<nrofnames && names[i+1]) TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1436 /* No need to marshal other data than FOUT and any VT_PTR */
1437 if (!is_out_elem(elem) && (elem->tdesc.vt != VT_PTR)) {
1438 xargs += _argsize(elem->tdesc.vt);
1439 if (relaydeb) TRACE_(olerelay)("[in]");
1442 hres = deserialize_param(
1452 ERR("Failed to unmarshall param, hres %x\n",hres);
1456 xargs += _argsize(elem->tdesc.vt);
1459 hres = xbuf_get(&buf, (LPBYTE)&remoteresult, sizeof(DWORD));
1462 if (relaydeb) TRACE_(olerelay)(") = %08x\n", remoteresult);
1464 hres = remoteresult;
1467 for (i = 0; i < nrofnames; i++)
1468 SysFreeString(names[i]);
1469 HeapFree(GetProcessHeap(),0,buf.base);
1470 IRpcChannelBuffer_Release(chanbuf);
1471 ITypeInfo_Release(tinfo);
1472 TRACE("-- 0x%08x\n", hres);
1476 static HRESULT WINAPI ProxyIUnknown_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
1478 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1480 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1482 if (proxy->outerunknown)
1483 return IUnknown_QueryInterface(proxy->outerunknown, riid, ppv);
1485 FIXME("No interface\n");
1486 return E_NOINTERFACE;
1489 static ULONG WINAPI ProxyIUnknown_AddRef(IUnknown *iface)
1491 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1495 if (proxy->outerunknown)
1496 return IUnknown_AddRef(proxy->outerunknown);
1498 return 2; /* FIXME */
1501 static ULONG WINAPI ProxyIUnknown_Release(IUnknown *iface)
1503 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1507 if (proxy->outerunknown)
1508 return IUnknown_Release(proxy->outerunknown);
1510 return 1; /* FIXME */
1513 static HRESULT WINAPI ProxyIDispatch_GetTypeInfoCount(LPDISPATCH iface, UINT * pctinfo)
1515 TMProxyImpl *This = (TMProxyImpl *)iface;
1517 TRACE("(%p)\n", pctinfo);
1519 return IDispatch_GetTypeInfoCount(This->dispatch, pctinfo);
1522 static HRESULT WINAPI ProxyIDispatch_GetTypeInfo(LPDISPATCH iface, UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo)
1524 TMProxyImpl *This = (TMProxyImpl *)iface;
1526 TRACE("(%d, %x, %p)\n", iTInfo, lcid, ppTInfo);
1528 return IDispatch_GetTypeInfo(This->dispatch, iTInfo, lcid, ppTInfo);
1531 static HRESULT WINAPI ProxyIDispatch_GetIDsOfNames(LPDISPATCH iface, REFIID riid, LPOLESTR * rgszNames, UINT cNames, LCID lcid, DISPID * rgDispId)
1533 TMProxyImpl *This = (TMProxyImpl *)iface;
1535 TRACE("(%s, %p, %d, 0x%x, %p)\n", debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);
1537 return IDispatch_GetIDsOfNames(This->dispatch, riid, rgszNames,
1538 cNames, lcid, rgDispId);
1541 static HRESULT WINAPI ProxyIDispatch_Invoke(LPDISPATCH iface, DISPID dispIdMember, REFIID riid, LCID lcid,
1542 WORD wFlags, DISPPARAMS * pDispParams, VARIANT * pVarResult,
1543 EXCEPINFO * pExcepInfo, UINT * puArgErr)
1545 TMProxyImpl *This = (TMProxyImpl *)iface;
1547 TRACE("(%d, %s, 0x%x, 0x%x, %p, %p, %p, %p)\n", dispIdMember,
1548 debugstr_guid(riid), lcid, wFlags, pDispParams, pVarResult,
1549 pExcepInfo, puArgErr);
1551 return IDispatch_Invoke(This->dispatch, dispIdMember, riid, lcid,
1552 wFlags, pDispParams, pVarResult, pExcepInfo,
1558 const IRpcChannelBufferVtbl *lpVtbl;
1560 /* the IDispatch-derived interface we are handling */
1562 IRpcChannelBuffer *pDelegateChannel;
1563 } TMarshalDispatchChannel;
1565 static HRESULT WINAPI TMarshalDispatchChannel_QueryInterface(LPRPCCHANNELBUFFER iface, REFIID riid, LPVOID *ppv)
1568 if (IsEqualIID(riid,&IID_IRpcChannelBuffer) || IsEqualIID(riid,&IID_IUnknown))
1570 *ppv = (LPVOID)iface;
1571 IUnknown_AddRef(iface);
1574 return E_NOINTERFACE;
1577 static ULONG WINAPI TMarshalDispatchChannel_AddRef(LPRPCCHANNELBUFFER iface)
1579 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1580 return InterlockedIncrement(&This->refs);
1583 static ULONG WINAPI TMarshalDispatchChannel_Release(LPRPCCHANNELBUFFER iface)
1585 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1588 ref = InterlockedDecrement(&This->refs);
1592 IRpcChannelBuffer_Release(This->pDelegateChannel);
1593 HeapFree(GetProcessHeap(), 0, This);
1597 static HRESULT WINAPI TMarshalDispatchChannel_GetBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg, REFIID riid)
1599 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1600 TRACE("(%p, %s)\n", olemsg, debugstr_guid(riid));
1601 /* Note: we are pretending to invoke a method on the interface identified
1602 * by tmarshal_iid so that we can re-use the IDispatch proxy/stub code
1603 * without the RPC runtime getting confused by not exporting an IDispatch interface */
1604 return IRpcChannelBuffer_GetBuffer(This->pDelegateChannel, olemsg, &This->tmarshal_iid);
1607 static HRESULT WINAPI TMarshalDispatchChannel_SendReceive(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE *olemsg, ULONG *pstatus)
1609 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1610 TRACE("(%p, %p)\n", olemsg, pstatus);
1611 return IRpcChannelBuffer_SendReceive(This->pDelegateChannel, olemsg, pstatus);
1614 static HRESULT WINAPI TMarshalDispatchChannel_FreeBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg)
1616 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1617 TRACE("(%p)\n", olemsg);
1618 return IRpcChannelBuffer_FreeBuffer(This->pDelegateChannel, olemsg);
1621 static HRESULT WINAPI TMarshalDispatchChannel_GetDestCtx(LPRPCCHANNELBUFFER iface, DWORD* pdwDestContext, void** ppvDestContext)
1623 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1624 TRACE("(%p,%p)\n", pdwDestContext, ppvDestContext);
1625 return IRpcChannelBuffer_GetDestCtx(This->pDelegateChannel, pdwDestContext, ppvDestContext);
1628 static HRESULT WINAPI TMarshalDispatchChannel_IsConnected(LPRPCCHANNELBUFFER iface)
1630 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1632 return IRpcChannelBuffer_IsConnected(This->pDelegateChannel);
1635 static const IRpcChannelBufferVtbl TMarshalDispatchChannelVtbl =
1637 TMarshalDispatchChannel_QueryInterface,
1638 TMarshalDispatchChannel_AddRef,
1639 TMarshalDispatchChannel_Release,
1640 TMarshalDispatchChannel_GetBuffer,
1641 TMarshalDispatchChannel_SendReceive,
1642 TMarshalDispatchChannel_FreeBuffer,
1643 TMarshalDispatchChannel_GetDestCtx,
1644 TMarshalDispatchChannel_IsConnected
1647 static HRESULT TMarshalDispatchChannel_Create(
1648 IRpcChannelBuffer *pDelegateChannel, REFIID tmarshal_riid,
1649 IRpcChannelBuffer **ppChannel)
1651 TMarshalDispatchChannel *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1653 return E_OUTOFMEMORY;
1655 This->lpVtbl = &TMarshalDispatchChannelVtbl;
1657 IRpcChannelBuffer_AddRef(pDelegateChannel);
1658 This->pDelegateChannel = pDelegateChannel;
1659 This->tmarshal_iid = *tmarshal_riid;
1661 *ppChannel = (IRpcChannelBuffer *)&This->lpVtbl;
1666 static inline HRESULT get_facbuf_for_iid(REFIID riid, IPSFactoryBuffer **facbuf)
1671 if ((hr = CoGetPSClsid(riid, &clsid)))
1673 return CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL,
1674 &IID_IPSFactoryBuffer, (LPVOID*)facbuf);
1677 static HRESULT init_proxy_entry_point(TMProxyImpl *proxy, unsigned int num)
1680 /* nrofargs without This */
1683 TMAsmProxy *xasm = proxy->asmstubs + num;
1685 const FUNCDESC *fdesc;
1687 hres = get_funcdesc(proxy->tinfo, num, &tinfo2, &fdesc, NULL, NULL, NULL);
1689 ERR("GetFuncDesc %x should not fail here.\n",hres);
1692 ITypeInfo_Release(tinfo2);
1693 /* some args take more than 4 byte on the stack */
1695 for (j=0;j<fdesc->cParams;j++)
1696 nrofargs += _argsize(fdesc->lprgelemdescParam[j].tdesc.vt);
1699 if (fdesc->callconv != CC_STDCALL) {
1700 ERR("calling convention is not stdcall????\n");
1703 /* popl %eax - return ptr
1710 * arg3 arg2 arg1 <method> <returnptr>
1712 xasm->popleax = 0x58;
1713 xasm->pushlval = 0x68;
1715 xasm->pushleax = 0x50;
1716 xasm->lcall = 0xe8; /* relative jump */
1717 xasm->xcall = (DWORD)xCall;
1718 xasm->xcall -= (DWORD)&(xasm->lret);
1720 xasm->bytestopop = (nrofargs+2)*4; /* pop args, This, iMethod */
1722 proxy->lpvtbl[num] = xasm;
1724 FIXME("not implemented on non i386\n");
1730 static HRESULT WINAPI
1731 PSFacBuf_CreateProxy(
1732 LPPSFACTORYBUFFER iface, IUnknown* pUnkOuter, REFIID riid,
1733 IRpcProxyBuffer **ppProxy, LPVOID *ppv)
1737 unsigned int i, nroffuncs;
1740 BOOL defer_to_dispatch = FALSE;
1742 TRACE("(...%s...)\n",debugstr_guid(riid));
1743 hres = _get_typeinfo_for_iid(riid,&tinfo);
1745 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
1749 hres = num_of_funcs(tinfo, &nroffuncs);
1751 ERR("Cannot get number of functions for typeinfo %s\n",debugstr_guid(riid));
1752 ITypeInfo_Release(tinfo);
1756 proxy = CoTaskMemAlloc(sizeof(TMProxyImpl));
1757 if (!proxy) return E_OUTOFMEMORY;
1759 assert(sizeof(TMAsmProxy) == 16);
1761 proxy->dispatch = NULL;
1762 proxy->dispatch_proxy = NULL;
1763 proxy->outerunknown = pUnkOuter;
1764 proxy->asmstubs = VirtualAlloc(NULL, sizeof(TMAsmProxy) * nroffuncs, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
1765 if (!proxy->asmstubs) {
1766 ERR("Could not commit pages for proxy thunks\n");
1767 CoTaskMemFree(proxy);
1768 return E_OUTOFMEMORY;
1770 proxy->lpvtbl2 = &tmproxyvtable;
1771 /* one reference for the proxy */
1773 proxy->tinfo = tinfo;
1774 memcpy(&proxy->iid,riid,sizeof(*riid));
1777 InitializeCriticalSection(&proxy->crit);
1778 proxy->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TMProxyImpl.crit");
1780 proxy->lpvtbl = HeapAlloc(GetProcessHeap(),0,sizeof(LPBYTE)*nroffuncs);
1782 /* if we derive from IDispatch then defer to its proxy for its methods */
1783 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
1786 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
1788 IPSFactoryBuffer *factory_buffer;
1789 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
1792 hres = IPSFactoryBuffer_CreateProxy(factory_buffer, NULL,
1793 &IID_IDispatch, &proxy->dispatch_proxy,
1794 (void **)&proxy->dispatch);
1795 IPSFactoryBuffer_Release(factory_buffer);
1797 if ((hres == S_OK) && (nroffuncs < 7))
1799 ERR("nroffuncs calculated incorrectly (%d)\n", nroffuncs);
1800 hres = E_UNEXPECTED;
1804 defer_to_dispatch = TRUE;
1807 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
1810 for (i=0;i<nroffuncs;i++) {
1813 proxy->lpvtbl[i] = ProxyIUnknown_QueryInterface;
1816 proxy->lpvtbl[i] = ProxyIUnknown_AddRef;
1819 proxy->lpvtbl[i] = ProxyIUnknown_Release;
1822 if(!defer_to_dispatch)
1824 hres = init_proxy_entry_point(proxy, i);
1825 if(FAILED(hres)) return hres;
1827 else proxy->lpvtbl[3] = ProxyIDispatch_GetTypeInfoCount;
1830 if(!defer_to_dispatch)
1832 hres = init_proxy_entry_point(proxy, i);
1833 if(FAILED(hres)) return hres;
1835 else proxy->lpvtbl[4] = ProxyIDispatch_GetTypeInfo;
1838 if(!defer_to_dispatch)
1840 hres = init_proxy_entry_point(proxy, i);
1841 if(FAILED(hres)) return hres;
1843 else proxy->lpvtbl[5] = ProxyIDispatch_GetIDsOfNames;
1846 if(!defer_to_dispatch)
1848 hres = init_proxy_entry_point(proxy, i);
1849 if(FAILED(hres)) return hres;
1851 else proxy->lpvtbl[6] = ProxyIDispatch_Invoke;
1854 hres = init_proxy_entry_point(proxy, i);
1855 if(FAILED(hres)) return hres;
1861 *ppv = (LPVOID)proxy;
1862 *ppProxy = (IRpcProxyBuffer *)&(proxy->lpvtbl2);
1863 IUnknown_AddRef((IUnknown *)*ppv);
1867 TMProxyImpl_Release((IRpcProxyBuffer *)&proxy->lpvtbl2);
1871 typedef struct _TMStubImpl {
1872 const IRpcStubBufferVtbl *lpvtbl;
1878 IRpcStubBuffer *dispatch_stub;
1879 BOOL dispatch_derivative;
1882 static HRESULT WINAPI
1883 TMStubImpl_QueryInterface(LPRPCSTUBBUFFER iface, REFIID riid, LPVOID *ppv)
1885 if (IsEqualIID(riid,&IID_IRpcStubBuffer)||IsEqualIID(riid,&IID_IUnknown)){
1886 *ppv = (LPVOID)iface;
1887 IRpcStubBuffer_AddRef(iface);
1890 FIXME("%s, not supported IID.\n",debugstr_guid(riid));
1891 return E_NOINTERFACE;
1895 TMStubImpl_AddRef(LPRPCSTUBBUFFER iface)
1897 TMStubImpl *This = (TMStubImpl *)iface;
1898 ULONG refCount = InterlockedIncrement(&This->ref);
1900 TRACE("(%p)->(ref before=%u)\n", This, refCount - 1);
1906 TMStubImpl_Release(LPRPCSTUBBUFFER iface)
1908 TMStubImpl *This = (TMStubImpl *)iface;
1909 ULONG refCount = InterlockedDecrement(&This->ref);
1911 TRACE("(%p)->(ref before=%u)\n", This, refCount + 1);
1915 IRpcStubBuffer_Disconnect(iface);
1916 ITypeInfo_Release(This->tinfo);
1917 if (This->dispatch_stub)
1918 IRpcStubBuffer_Release(This->dispatch_stub);
1919 CoTaskMemFree(This);
1924 static HRESULT WINAPI
1925 TMStubImpl_Connect(LPRPCSTUBBUFFER iface, LPUNKNOWN pUnkServer)
1927 TMStubImpl *This = (TMStubImpl *)iface;
1929 TRACE("(%p)->(%p)\n", This, pUnkServer);
1931 IUnknown_AddRef(pUnkServer);
1932 This->pUnk = pUnkServer;
1934 if (This->dispatch_stub)
1935 IRpcStubBuffer_Connect(This->dispatch_stub, pUnkServer);
1941 TMStubImpl_Disconnect(LPRPCSTUBBUFFER iface)
1943 TMStubImpl *This = (TMStubImpl *)iface;
1945 TRACE("(%p)->()\n", This);
1949 IUnknown_Release(This->pUnk);
1953 if (This->dispatch_stub)
1954 IRpcStubBuffer_Disconnect(This->dispatch_stub);
1957 static HRESULT WINAPI
1959 LPRPCSTUBBUFFER iface, RPCOLEMESSAGE* xmsg,IRpcChannelBuffer*rpcchanbuf)
1962 const FUNCDESC *fdesc;
1963 TMStubImpl *This = (TMStubImpl *)iface;
1965 DWORD *args = NULL, res, *xargs, nrofargs;
1970 ITypeInfo *tinfo = NULL;
1974 if (xmsg->iMethod < 3) {
1975 ERR("IUnknown methods cannot be marshaled by the typelib marshaler\n");
1976 return E_UNEXPECTED;
1979 if (This->dispatch_derivative && xmsg->iMethod < sizeof(IDispatchVtbl)/sizeof(void *))
1981 IPSFactoryBuffer *factory_buffer;
1982 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
1985 hres = IPSFactoryBuffer_CreateStub(factory_buffer, &IID_IDispatch,
1986 This->pUnk, &This->dispatch_stub);
1987 IPSFactoryBuffer_Release(factory_buffer);
1991 return IRpcStubBuffer_Invoke(This->dispatch_stub, xmsg, rpcchanbuf);
1994 memset(&buf,0,sizeof(buf));
1995 buf.size = xmsg->cbBuffer;
1996 buf.base = HeapAlloc(GetProcessHeap(), 0, xmsg->cbBuffer);
1997 memcpy(buf.base, xmsg->Buffer, xmsg->cbBuffer);
2000 hres = get_funcdesc(This->tinfo,xmsg->iMethod,&tinfo,&fdesc,&iname,NULL,NULL);
2002 ERR("GetFuncDesc on method %d failed with %x\n",xmsg->iMethod,hres);
2006 if (iname && !lstrcmpW(iname, IDispatchW))
2008 ERR("IDispatch cannot be marshaled by the typelib marshaler\n");
2009 hres = E_UNEXPECTED;
2010 SysFreeString (iname);
2014 if (iname) SysFreeString (iname);
2016 /* Need them for hack below */
2017 memset(names,0,sizeof(names));
2018 ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames);
2019 if (nrofnames > sizeof(names)/sizeof(names[0])) {
2020 ERR("Need more names!\n");
2023 /*dump_FUNCDESC(fdesc);*/
2025 for (i=0;i<fdesc->cParams;i++)
2026 nrofargs += _argsize(fdesc->lprgelemdescParam[i].tdesc.vt);
2027 args = HeapAlloc(GetProcessHeap(),0,(nrofargs+1)*sizeof(DWORD));
2030 hres = E_OUTOFMEMORY;
2034 /* Allocate all stuff used by call. */
2036 for (i=0;i<fdesc->cParams;i++) {
2037 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2039 hres = deserialize_param(
2048 xargs += _argsize(elem->tdesc.vt);
2050 ERR("Failed to deserialize param %s, hres %x\n",relaystr(names[i+1]),hres);
2055 args[0] = (DWORD)This->pUnk;
2060 (*((FARPROC**)args[0]))[fdesc->oVft/4],
2068 DWORD dwExceptionCode = GetExceptionCode();
2069 ERR("invoke call failed with exception 0x%08x (%d)\n", dwExceptionCode, dwExceptionCode);
2070 if (FAILED(dwExceptionCode))
2071 hres = dwExceptionCode;
2073 hres = HRESULT_FROM_WIN32(dwExceptionCode);
2083 for (i=0;i<fdesc->cParams;i++) {
2084 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2085 hres = serialize_param(
2094 xargs += _argsize(elem->tdesc.vt);
2096 ERR("Failed to stuballoc param, hres %x\n",hres);
2101 hres = xbuf_add (&buf, (LPBYTE)&res, sizeof(DWORD));
2106 xmsg->cbBuffer = buf.curoff;
2107 hres = IRpcChannelBuffer_GetBuffer(rpcchanbuf, xmsg, &This->iid);
2109 ERR("IRpcChannelBuffer_GetBuffer failed with error 0x%08x\n", hres);
2112 memcpy(xmsg->Buffer, buf.base, buf.curoff);
2115 for (i = 0; i < nrofnames; i++)
2116 SysFreeString(names[i]);
2118 ITypeInfo_Release(tinfo);
2119 HeapFree(GetProcessHeap(), 0, args);
2121 HeapFree(GetProcessHeap(), 0, buf.base);
2123 TRACE("returning\n");
2127 static LPRPCSTUBBUFFER WINAPI
2128 TMStubImpl_IsIIDSupported(LPRPCSTUBBUFFER iface, REFIID riid) {
2129 FIXME("Huh (%s)?\n",debugstr_guid(riid));
2134 TMStubImpl_CountRefs(LPRPCSTUBBUFFER iface) {
2135 TMStubImpl *This = (TMStubImpl *)iface;
2138 return This->ref; /*FIXME? */
2141 static HRESULT WINAPI
2142 TMStubImpl_DebugServerQueryInterface(LPRPCSTUBBUFFER iface, LPVOID *ppv) {
2147 TMStubImpl_DebugServerRelease(LPRPCSTUBBUFFER iface, LPVOID ppv) {
2151 static const IRpcStubBufferVtbl tmstubvtbl = {
2152 TMStubImpl_QueryInterface,
2156 TMStubImpl_Disconnect,
2158 TMStubImpl_IsIIDSupported,
2159 TMStubImpl_CountRefs,
2160 TMStubImpl_DebugServerQueryInterface,
2161 TMStubImpl_DebugServerRelease
2164 static HRESULT WINAPI
2165 PSFacBuf_CreateStub(
2166 LPPSFACTORYBUFFER iface, REFIID riid,IUnknown *pUnkServer,
2167 IRpcStubBuffer** ppStub
2174 TRACE("(%s,%p,%p)\n",debugstr_guid(riid),pUnkServer,ppStub);
2176 hres = _get_typeinfo_for_iid(riid,&tinfo);
2178 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
2182 stub = CoTaskMemAlloc(sizeof(TMStubImpl));
2184 return E_OUTOFMEMORY;
2185 stub->lpvtbl = &tmstubvtbl;
2187 stub->tinfo = tinfo;
2188 stub->dispatch_stub = NULL;
2189 stub->dispatch_derivative = FALSE;
2190 memcpy(&(stub->iid),riid,sizeof(*riid));
2191 hres = IRpcStubBuffer_Connect((LPRPCSTUBBUFFER)stub,pUnkServer);
2192 *ppStub = (LPRPCSTUBBUFFER)stub;
2193 TRACE("IRpcStubBuffer: %p\n", stub);
2195 ERR("Connect to pUnkServer failed?\n");
2197 /* if we derive from IDispatch then defer to its stub for some of its methods */
2198 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
2201 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
2202 stub->dispatch_derivative = TRUE;
2203 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
2209 static const IPSFactoryBufferVtbl psfacbufvtbl = {
2210 PSFacBuf_QueryInterface,
2213 PSFacBuf_CreateProxy,
2217 /* This is the whole PSFactoryBuffer object, just the vtableptr */
2218 static const IPSFactoryBufferVtbl *lppsfac = &psfacbufvtbl;
2220 /***********************************************************************
2221 * TMARSHAL_DllGetClassObject
2223 HRESULT TMARSHAL_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
2225 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)) {
2229 return E_NOINTERFACE;