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
46 #include "propidl.h" /* for LPSAFEARRAY_User* functions */
49 #include "wine/debug.h"
50 #include "wine/exception.h"
52 static const WCHAR IDispatchW[] = { 'I','D','i','s','p','a','t','c','h',0};
54 WINE_DEFAULT_DEBUG_CHANNEL(ole);
55 WINE_DECLARE_DEBUG_CHANNEL(olerelay);
57 #define ICOM_THIS_MULTI(impl,field,iface) impl* const This=(impl*)((char*)(iface) - offsetof(impl,field))
59 static HRESULT TMarshalDispatchChannel_Create(
60 IRpcChannelBuffer *pDelegateChannel, REFIID tmarshal_riid,
61 IRpcChannelBuffer **ppChannel);
63 typedef struct _marshal_state {
69 /* used in the olerelay code to avoid having the L"" stuff added by debugstr_w */
70 static char *relaystr(WCHAR *in) {
71 char *tmp = (char *)debugstr_w(in);
73 tmp[strlen(tmp)-1] = '\0';
78 xbuf_resize(marshal_state *buf, DWORD newsize)
80 if(buf->size >= newsize)
85 buf->base = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buf->base, newsize);
91 buf->base = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, newsize);
100 xbuf_add(marshal_state *buf, LPBYTE stuff, DWORD size)
104 if(buf->size - buf->curoff < size)
106 hr = xbuf_resize(buf, buf->size + size + 100);
107 if(FAILED(hr)) return hr;
109 memcpy(buf->base+buf->curoff,stuff,size);
115 xbuf_get(marshal_state *buf, LPBYTE stuff, DWORD size) {
116 if (buf->size < buf->curoff+size) return E_FAIL;
117 memcpy(stuff,buf->base+buf->curoff,size);
123 xbuf_skip(marshal_state *buf, DWORD size) {
124 if (buf->size < buf->curoff+size) return E_FAIL;
130 _unmarshal_interface(marshal_state *buf, REFIID riid, LPUNKNOWN *pUnk) {
132 ULARGE_INTEGER newpos;
133 LARGE_INTEGER seekto;
138 TRACE("...%s...\n",debugstr_guid(riid));
141 hres = xbuf_get(buf,(LPBYTE)&xsize,sizeof(xsize));
143 ERR("xbuf_get failed\n");
147 if (xsize == 0) return S_OK;
149 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
151 ERR("Stream create failed %x\n",hres);
155 hres = IStream_Write(pStm,buf->base+buf->curoff,xsize,&res);
157 ERR("stream write %x\n",hres);
161 memset(&seekto,0,sizeof(seekto));
162 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
164 ERR("Failed Seek %x\n",hres);
168 hres = CoUnmarshalInterface(pStm,riid,(LPVOID*)pUnk);
170 ERR("Unmarshalling interface %s failed with %x\n",debugstr_guid(riid),hres);
174 IStream_Release(pStm);
175 return xbuf_skip(buf,xsize);
179 _marshal_interface(marshal_state *buf, REFIID riid, LPUNKNOWN pUnk) {
180 LPBYTE tempbuf = NULL;
181 IStream *pStm = NULL;
183 ULARGE_INTEGER newpos;
184 LARGE_INTEGER seekto;
190 /* this is valid, if for instance we serialize
191 * a VT_DISPATCH with NULL ptr which apparently
192 * can happen. S_OK to make sure we continue
195 WARN("pUnk is NULL\n");
197 return xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
202 TRACE("...%s...\n",debugstr_guid(riid));
204 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
206 ERR("Stream create failed %x\n",hres);
210 hres = CoMarshalInterface(pStm,riid,pUnk,0,NULL,0);
212 ERR("Marshalling interface %s failed with %x\n", debugstr_guid(riid), hres);
216 hres = IStream_Stat(pStm,&ststg,0);
218 ERR("Stream stat failed\n");
222 tempbuf = HeapAlloc(GetProcessHeap(), 0, ststg.cbSize.u.LowPart);
223 memset(&seekto,0,sizeof(seekto));
224 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
226 ERR("Failed Seek %x\n",hres);
230 hres = IStream_Read(pStm,tempbuf,ststg.cbSize.u.LowPart,&res);
232 ERR("Failed Read %x\n",hres);
236 xsize = ststg.cbSize.u.LowPart;
237 xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
238 hres = xbuf_add(buf,tempbuf,ststg.cbSize.u.LowPart);
240 HeapFree(GetProcessHeap(),0,tempbuf);
241 IStream_Release(pStm);
247 xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
248 if (pStm) IUnknown_Release(pStm);
249 HeapFree(GetProcessHeap(), 0, tempbuf);
253 /********************* OLE Proxy/Stub Factory ********************************/
254 static HRESULT WINAPI
255 PSFacBuf_QueryInterface(LPPSFACTORYBUFFER iface, REFIID iid, LPVOID *ppv) {
256 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)||IsEqualIID(iid,&IID_IUnknown)) {
257 *ppv = (LPVOID)iface;
258 /* No ref counting, static class */
261 FIXME("(%s) unknown IID?\n",debugstr_guid(iid));
262 return E_NOINTERFACE;
265 static ULONG WINAPI PSFacBuf_AddRef(LPPSFACTORYBUFFER iface) { return 2; }
266 static ULONG WINAPI PSFacBuf_Release(LPPSFACTORYBUFFER iface) { return 1; }
269 _get_typeinfo_for_iid(REFIID riid, ITypeInfo**ti) {
272 char tlguid[200],typelibkey[300],interfacekey[300],ver[100];
275 DWORD tlguidlen, verlen, type;
279 sprintf( interfacekey, "Interface\\{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\Typelib",
280 riid->Data1, riid->Data2, riid->Data3,
281 riid->Data4[0], riid->Data4[1], riid->Data4[2], riid->Data4[3],
282 riid->Data4[4], riid->Data4[5], riid->Data4[6], riid->Data4[7]
285 if (RegOpenKeyA(HKEY_CLASSES_ROOT,interfacekey,&ikey)) {
286 ERR("No %s key found.\n",interfacekey);
290 tlguidlen = sizeof(tlguid);
291 if (RegQueryValueExA(ikey,NULL,NULL,&type,(LPBYTE)tlguid,&tlguidlen)) {
292 ERR("Getting typelib guid failed.\n");
297 verlen = sizeof(ver);
298 if (RegQueryValueExA(ikey,"Version",NULL,&type,(LPBYTE)ver,&verlen)) {
299 ERR("Could not get version value?\n");
304 sprintf(typelibkey,"Typelib\\%s\\%s\\0\\win32",tlguid,ver);
305 tlfnlen = sizeof(tlfn);
306 if (RegQueryValueA(HKEY_CLASSES_ROOT,typelibkey,tlfn,&tlfnlen)) {
307 ERR("Could not get typelib fn?\n");
310 MultiByteToWideChar(CP_ACP, 0, tlfn, -1, tlfnW, sizeof(tlfnW) / sizeof(tlfnW[0]));
311 hres = LoadTypeLib(tlfnW,&tl);
313 ERR("Failed to load typelib for %s, but it should be there.\n",debugstr_guid(riid));
316 hres = ITypeLib_GetTypeInfoOfGuid(tl,riid,ti);
318 ERR("typelib does not contain info for %s?\n",debugstr_guid(riid));
319 ITypeLib_Release(tl);
322 ITypeLib_Release(tl);
326 /* Determine nr of functions. Since we use the toplevel interface and all
327 * inherited ones have lower numbers, we are ok to not to descent into
328 * the inheritance tree I think.
330 static int _nroffuncs(ITypeInfo *tinfo) {
332 const FUNCDESC *fdesc;
338 hres = ITypeInfo_GetTypeAttr(tinfo, &attr);
340 ERR("GetTypeAttr failed with %x\n",hres);
343 /* look in inherited ifaces. */
344 for (j=0;j<attr->cImplTypes;j++) {
346 hres = ITypeInfo_GetRefTypeOfImplType(tinfo, j, &href);
348 ERR("Did not find a reftype for interface offset %d?\n",j);
351 hres = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
353 ERR("Did not find a typeinfo for reftype %d?\n",href);
356 n += _nroffuncs(tinfo2);
357 ITypeInfo_Release(tinfo2);
361 hres = ITypeInfoImpl_GetInternalFuncDesc(tinfo,i,&fdesc);
372 #include "pshpack1.h"
374 typedef struct _TMAsmProxy {
388 # warning You need to implement stubless proxies for your architecture
389 typedef struct _TMAsmProxy {
393 typedef struct _TMProxyImpl {
395 const IRpcProxyBufferVtbl *lpvtbl2;
398 TMAsmProxy *asmstubs;
400 IRpcChannelBuffer* chanbuf;
402 CRITICAL_SECTION crit;
403 IUnknown *outerunknown;
405 IRpcProxyBuffer *dispatch_proxy;
408 static HRESULT WINAPI
409 TMProxyImpl_QueryInterface(LPRPCPROXYBUFFER iface, REFIID riid, LPVOID *ppv)
412 if (IsEqualIID(riid,&IID_IUnknown)||IsEqualIID(riid,&IID_IRpcProxyBuffer)) {
413 *ppv = (LPVOID)iface;
414 IRpcProxyBuffer_AddRef(iface);
417 FIXME("no interface for %s\n",debugstr_guid(riid));
418 return E_NOINTERFACE;
422 TMProxyImpl_AddRef(LPRPCPROXYBUFFER iface)
424 ICOM_THIS_MULTI(TMProxyImpl,lpvtbl2,iface);
425 ULONG refCount = InterlockedIncrement(&This->ref);
427 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
433 TMProxyImpl_Release(LPRPCPROXYBUFFER iface)
435 ICOM_THIS_MULTI(TMProxyImpl,lpvtbl2,iface);
436 ULONG refCount = InterlockedDecrement(&This->ref);
438 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
442 if (This->dispatch_proxy) IRpcProxyBuffer_Release(This->dispatch_proxy);
443 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(TYPEDESC *td) {
532 return sizeof(VARIANT)+3;
535 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");
973 *bstr = CoTaskMemAlloc(sizeof(BSTR *));
974 **bstr = SysAllocStringLen(str,len);
975 if (debugout) TRACE_(olerelay)("%s",relaystr(str));
976 HeapFree(GetProcessHeap(),0,str);
988 hres = xbuf_get(buf,(LPBYTE)&len,sizeof(DWORD));
990 ERR("failed to read bstr klen\n");
995 if (debugout) TRACE_(olerelay)("<bstr NULL>");
997 str = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(len+1)*sizeof(WCHAR));
998 hres = xbuf_get(buf,(LPBYTE)str,len*sizeof(WCHAR));
1000 ERR("Failed to read BSTR.\n");
1003 *arg = (DWORD)SysAllocStringLen(str,len);
1004 if (debugout) TRACE_(olerelay)("%s",relaystr(str));
1005 HeapFree(GetProcessHeap(),0,str);
1014 BOOL derefhere = TRUE;
1016 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
1020 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
1022 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
1025 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1026 switch (tattr->typekind) {
1027 case TKIND_ENUM: /* confirmed */
1028 case TKIND_RECORD: /* FIXME: mostly untested */
1031 case TKIND_ALIAS: /* FIXME: untested */
1032 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
1033 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
1037 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
1041 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1042 ITypeInfo_Release(tinfo2);
1044 /* read it in all cases, we need to know if we have
1045 * NULL pointer or not.
1047 hres = xbuf_get(buf,(LPBYTE)&cookie,sizeof(cookie));
1049 ERR("Failed to load pointer cookie.\n");
1052 if (cookie != 0x42424242) {
1053 /* we read a NULL ptr from the remote side */
1054 if (debugout) TRACE_(olerelay)("NULL");
1058 if (debugout) TRACE_(olerelay)("*");
1060 /* Allocate space for the referenced struct */
1062 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,_xsize(tdesc->u.lptdesc));
1065 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, (LPDWORD)*arg, buf);
1067 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, arg, buf);
1070 /* FIXME: UNKNOWN is unknown ..., but allocate 4 byte for it */
1072 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
1075 hres = _unmarshal_interface(buf,&IID_IUnknown,(LPUNKNOWN*)arg);
1077 TRACE_(olerelay)("unk(%p)",arg);
1082 hres = _unmarshal_interface(buf,&IID_IDispatch,(LPUNKNOWN*)arg);
1084 TRACE_(olerelay)("idisp(%p)",arg);
1087 if (debugout) TRACE_(olerelay)("<void>");
1089 case VT_USERDEFINED: {
1093 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
1095 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
1098 hres = ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1100 ERR("Could not get typeattr in VT_USERDEFINED.\n");
1102 switch (tattr->typekind) {
1103 case TKIND_DISPATCH:
1104 case TKIND_INTERFACE:
1106 hres = _unmarshal_interface(buf,&(tattr->guid),(LPUNKNOWN*)arg);
1108 case TKIND_RECORD: {
1112 *arg = (DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,tattr->cbSizeInstance);
1114 if (debugout) TRACE_(olerelay)("{");
1115 for (i=0;i<tattr->cVars;i++) {
1118 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
1120 ERR("Could not get vardesc of %d\n",i);
1121 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1122 ITypeInfo_Release(tinfo2);
1125 hres = deserialize_param(
1130 &vdesc->elemdescVar.tdesc,
1131 (DWORD*)(((LPBYTE)*arg)+vdesc->u.oInst),
1134 ITypeInfo2_ReleaseVarDesc(tinfo2, vdesc);
1135 if (debugout && (i<tattr->cVars-1)) TRACE_(olerelay)(",");
1137 if (debugout) TRACE_(olerelay)("}");
1141 hres = deserialize_param(tinfo2,readit,debugout,alloc,&tattr->tdescAlias,arg,buf);
1145 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
1146 if (hres) ERR("Failed to read enum (4 byte)\n");
1148 if (debugout) TRACE_(olerelay)("%x",*arg);
1151 ERR("Unhandled typekind %d\n",tattr->typekind);
1155 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1158 ERR("failed to stuballoc in TKIND_RECORD.\n");
1159 ITypeInfo_Release(tinfo2);
1163 /* arg is pointing to the start of the array. */
1164 ARRAYDESC *adesc = tdesc->u.lpadesc;
1167 if (adesc->cDims > 1) FIXME("cDims > 1 in VT_CARRAY. Does it work?\n");
1168 for (i=0;i<adesc->cDims;i++)
1169 arrsize *= adesc->rgbounds[i].cElements;
1170 for (i=0;i<arrsize;i++)
1177 (DWORD*)((LPBYTE)(arg)+i*_xsize(&adesc->tdescElem)),
1182 case VT_SAFEARRAY: {
1185 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
1186 unsigned char *buffer;
1187 buffer = LPSAFEARRAY_UserUnmarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
1188 buf->curoff = buffer - buf->base;
1193 ERR("No handler for VT type %d!\n",tdesc->vt);
1199 /* Searches function, also in inherited interfaces */
1202 ITypeInfo *tinfo, int iMethod, ITypeInfo **tactual, const FUNCDESC **fdesc, BSTR *iname, BSTR *fname)
1207 if (fname) *fname = NULL;
1208 if (iname) *iname = NULL;
1211 hres = ITypeInfoImpl_GetInternalFuncDesc(tinfo, i, fdesc);
1218 hres = ITypeInfo_GetTypeAttr(tinfo, &attr);
1220 ERR("GetTypeAttr failed with %x\n",hres);
1223 /* Not found, so look in inherited ifaces. */
1224 for (j=0;j<attr->cImplTypes;j++) {
1225 hres = ITypeInfo_GetRefTypeOfImplType(tinfo, j, &href);
1227 ERR("Did not find a reftype for interface offset %d?\n",j);
1230 hres = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
1232 ERR("Did not find a typeinfo for reftype %d?\n",href);
1235 hres = _get_funcdesc(tinfo2,iMethod,tactual,fdesc,iname,fname);
1236 ITypeInfo_Release(tinfo2);
1238 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1242 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1245 if (((*fdesc)->oVft/4) == iMethod) {
1247 ITypeInfo_GetDocumentation(tinfo,(*fdesc)->memid,fname,NULL,NULL,NULL);
1249 ITypeInfo_GetDocumentation(tinfo,-1,iname,NULL,NULL,NULL);
1251 ITypeInfo_AddRef(*tactual);
1258 static inline BOOL is_in_elem(const ELEMDESC *elem)
1260 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN || !elem->u.paramdesc.wParamFlags);
1263 static inline BOOL is_out_elem(const ELEMDESC *elem)
1265 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT || !elem->u.paramdesc.wParamFlags);
1269 xCall(LPVOID retptr, int method, TMProxyImpl *tpinfo /*, args */)
1271 DWORD *args = ((DWORD*)&tpinfo)+1, *xargs;
1272 const FUNCDESC *fdesc;
1274 int i, relaydeb = TRACE_ON(olerelay);
1281 DWORD remoteresult = 0;
1283 IRpcChannelBuffer *chanbuf;
1285 EnterCriticalSection(&tpinfo->crit);
1287 hres = _get_funcdesc(tpinfo->tinfo,method,&tinfo,&fdesc,&iname,&fname);
1289 ERR("Did not find typeinfo/funcdesc entry for method %d!\n",method);
1290 ITypeInfo_Release(tinfo);
1291 LeaveCriticalSection(&tpinfo->crit);
1295 if (!tpinfo->chanbuf)
1297 WARN("Tried to use disconnected proxy\n");
1298 ITypeInfo_Release(tinfo);
1299 LeaveCriticalSection(&tpinfo->crit);
1300 return RPC_E_DISCONNECTED;
1302 chanbuf = tpinfo->chanbuf;
1303 IRpcChannelBuffer_AddRef(chanbuf);
1305 LeaveCriticalSection(&tpinfo->crit);
1308 TRACE_(olerelay)("->");
1310 TRACE_(olerelay)("%s:",relaystr(iname));
1312 TRACE_(olerelay)("%s(%d)",relaystr(fname),method);
1314 TRACE_(olerelay)("%d",method);
1315 TRACE_(olerelay)("(");
1318 if (iname) SysFreeString(iname);
1319 if (fname) SysFreeString(fname);
1321 memset(&buf,0,sizeof(buf));
1323 /* normal typelib driven serializing */
1325 /* Need them for hack below */
1326 memset(names,0,sizeof(names));
1327 if (ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames))
1329 if (nrofnames > sizeof(names)/sizeof(names[0]))
1330 ERR("Need more names!\n");
1333 for (i=0;i<fdesc->cParams;i++) {
1334 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1336 if (i) TRACE_(olerelay)(",");
1337 if (i+1<nrofnames && names[i+1])
1338 TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1340 /* No need to marshal other data than FIN and any VT_PTR. */
1341 if (!is_in_elem(elem) && (elem->tdesc.vt != VT_PTR)) {
1342 xargs+=_argsize(elem->tdesc.vt);
1343 if (relaydeb) TRACE_(olerelay)("[out]");
1346 hres = serialize_param(
1357 ERR("Failed to serialize param, hres %x\n",hres);
1360 xargs+=_argsize(elem->tdesc.vt);
1362 if (relaydeb) TRACE_(olerelay)(")");
1364 memset(&msg,0,sizeof(msg));
1365 msg.cbBuffer = buf.curoff;
1366 msg.iMethod = method;
1367 hres = IRpcChannelBuffer_GetBuffer(chanbuf,&msg,&(tpinfo->iid));
1369 ERR("RpcChannelBuffer GetBuffer failed, %x\n",hres);
1372 memcpy(msg.Buffer,buf.base,buf.curoff);
1373 if (relaydeb) TRACE_(olerelay)("\n");
1374 hres = IRpcChannelBuffer_SendReceive(chanbuf,&msg,&status);
1376 ERR("RpcChannelBuffer SendReceive failed, %x\n",hres);
1380 if (relaydeb) TRACE_(olerelay)(" status = %08x (",status);
1382 buf.base = HeapReAlloc(GetProcessHeap(),0,buf.base,msg.cbBuffer);
1384 buf.base = HeapAlloc(GetProcessHeap(),0,msg.cbBuffer);
1385 buf.size = msg.cbBuffer;
1386 memcpy(buf.base,msg.Buffer,buf.size);
1389 /* generic deserializer using typelib description */
1392 for (i=0;i<fdesc->cParams;i++) {
1393 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1396 if (i) TRACE_(olerelay)(",");
1397 if (i+1<nrofnames && names[i+1]) TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1399 /* No need to marshal other data than FOUT and any VT_PTR */
1400 if (!is_out_elem(elem) && (elem->tdesc.vt != VT_PTR)) {
1401 xargs += _argsize(elem->tdesc.vt);
1402 if (relaydeb) TRACE_(olerelay)("[in]");
1405 hres = deserialize_param(
1415 ERR("Failed to unmarshall param, hres %x\n",hres);
1419 xargs += _argsize(elem->tdesc.vt);
1422 hres = xbuf_get(&buf, (LPBYTE)&remoteresult, sizeof(DWORD));
1425 if (relaydeb) TRACE_(olerelay)(") = %08x\n", remoteresult);
1427 hres = remoteresult;
1430 for (i = 0; i < nrofnames; i++)
1431 SysFreeString(names[i]);
1432 HeapFree(GetProcessHeap(),0,buf.base);
1433 IRpcChannelBuffer_Release(chanbuf);
1434 ITypeInfo_Release(tinfo);
1435 TRACE("-- 0x%08x\n", hres);
1439 static HRESULT WINAPI ProxyIUnknown_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
1441 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1443 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1445 if (proxy->outerunknown)
1446 return IUnknown_QueryInterface(proxy->outerunknown, riid, ppv);
1448 FIXME("No interface\n");
1449 return E_NOINTERFACE;
1452 static ULONG WINAPI ProxyIUnknown_AddRef(IUnknown *iface)
1454 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1458 if (proxy->outerunknown)
1459 return IUnknown_AddRef(proxy->outerunknown);
1461 return 2; /* FIXME */
1464 static ULONG WINAPI ProxyIUnknown_Release(IUnknown *iface)
1466 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1470 if (proxy->outerunknown)
1471 return IUnknown_Release(proxy->outerunknown);
1473 return 1; /* FIXME */
1476 static HRESULT WINAPI ProxyIDispatch_GetTypeInfoCount(LPDISPATCH iface, UINT * pctinfo)
1478 TMProxyImpl *This = (TMProxyImpl *)iface;
1480 TRACE("(%p)\n", pctinfo);
1482 return IDispatch_GetTypeInfoCount(This->dispatch, pctinfo);
1485 static HRESULT WINAPI ProxyIDispatch_GetTypeInfo(LPDISPATCH iface, UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo)
1487 TMProxyImpl *This = (TMProxyImpl *)iface;
1489 TRACE("(%d, %x, %p)\n", iTInfo, lcid, ppTInfo);
1491 return IDispatch_GetTypeInfo(This->dispatch, iTInfo, lcid, ppTInfo);
1494 static HRESULT WINAPI ProxyIDispatch_GetIDsOfNames(LPDISPATCH iface, REFIID riid, LPOLESTR * rgszNames, UINT cNames, LCID lcid, DISPID * rgDispId)
1496 TMProxyImpl *This = (TMProxyImpl *)iface;
1498 TRACE("(%s, %p, %d, 0x%x, %p)\n", debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);
1500 return IDispatch_GetIDsOfNames(This->dispatch, riid, rgszNames,
1501 cNames, lcid, rgDispId);
1504 static HRESULT WINAPI ProxyIDispatch_Invoke(LPDISPATCH iface, DISPID dispIdMember, REFIID riid, LCID lcid,
1505 WORD wFlags, DISPPARAMS * pDispParams, VARIANT * pVarResult,
1506 EXCEPINFO * pExcepInfo, UINT * puArgErr)
1508 TMProxyImpl *This = (TMProxyImpl *)iface;
1510 TRACE("(%d, %s, 0x%x, 0x%x, %p, %p, %p, %p)\n", dispIdMember,
1511 debugstr_guid(riid), lcid, wFlags, pDispParams, pVarResult,
1512 pExcepInfo, puArgErr);
1514 return IDispatch_Invoke(This->dispatch, dispIdMember, riid, lcid,
1515 wFlags, pDispParams, pVarResult, pExcepInfo,
1521 const IRpcChannelBufferVtbl *lpVtbl;
1523 /* the IDispatch-derived interface we are handling */
1525 IRpcChannelBuffer *pDelegateChannel;
1526 } TMarshalDispatchChannel;
1528 static HRESULT WINAPI TMarshalDispatchChannel_QueryInterface(LPRPCCHANNELBUFFER iface, REFIID riid, LPVOID *ppv)
1531 if (IsEqualIID(riid,&IID_IRpcChannelBuffer) || IsEqualIID(riid,&IID_IUnknown))
1533 *ppv = (LPVOID)iface;
1534 IUnknown_AddRef(iface);
1537 return E_NOINTERFACE;
1540 static ULONG WINAPI TMarshalDispatchChannel_AddRef(LPRPCCHANNELBUFFER iface)
1542 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1543 return InterlockedIncrement(&This->refs);
1546 static ULONG WINAPI TMarshalDispatchChannel_Release(LPRPCCHANNELBUFFER iface)
1548 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1551 ref = InterlockedDecrement(&This->refs);
1555 IRpcChannelBuffer_Release(This->pDelegateChannel);
1556 HeapFree(GetProcessHeap(), 0, This);
1560 static HRESULT WINAPI TMarshalDispatchChannel_GetBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg, REFIID riid)
1562 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1563 TRACE("(%p, %s)\n", olemsg, debugstr_guid(riid));
1564 /* Note: we are pretending to invoke a method on the interface identified
1565 * by tmarshal_iid so that we can re-use the IDispatch proxy/stub code
1566 * without the RPC runtime getting confused by not exporting an IDispatch interface */
1567 return IRpcChannelBuffer_GetBuffer(This->pDelegateChannel, olemsg, &This->tmarshal_iid);
1570 static HRESULT WINAPI TMarshalDispatchChannel_SendReceive(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE *olemsg, ULONG *pstatus)
1572 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1573 TRACE("(%p, %p)\n", olemsg, pstatus);
1574 return IRpcChannelBuffer_SendReceive(This->pDelegateChannel, olemsg, pstatus);
1577 static HRESULT WINAPI TMarshalDispatchChannel_FreeBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg)
1579 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1580 TRACE("(%p)\n", olemsg);
1581 return IRpcChannelBuffer_FreeBuffer(This->pDelegateChannel, olemsg);
1584 static HRESULT WINAPI TMarshalDispatchChannel_GetDestCtx(LPRPCCHANNELBUFFER iface, DWORD* pdwDestContext, void** ppvDestContext)
1586 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1587 TRACE("(%p,%p)\n", pdwDestContext, ppvDestContext);
1588 return IRpcChannelBuffer_GetDestCtx(This->pDelegateChannel, pdwDestContext, ppvDestContext);
1591 static HRESULT WINAPI TMarshalDispatchChannel_IsConnected(LPRPCCHANNELBUFFER iface)
1593 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1595 return IRpcChannelBuffer_IsConnected(This->pDelegateChannel);
1598 static const IRpcChannelBufferVtbl TMarshalDispatchChannelVtbl =
1600 TMarshalDispatchChannel_QueryInterface,
1601 TMarshalDispatchChannel_AddRef,
1602 TMarshalDispatchChannel_Release,
1603 TMarshalDispatchChannel_GetBuffer,
1604 TMarshalDispatchChannel_SendReceive,
1605 TMarshalDispatchChannel_FreeBuffer,
1606 TMarshalDispatchChannel_GetDestCtx,
1607 TMarshalDispatchChannel_IsConnected
1610 static HRESULT TMarshalDispatchChannel_Create(
1611 IRpcChannelBuffer *pDelegateChannel, REFIID tmarshal_riid,
1612 IRpcChannelBuffer **ppChannel)
1614 TMarshalDispatchChannel *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1616 return E_OUTOFMEMORY;
1618 This->lpVtbl = &TMarshalDispatchChannelVtbl;
1620 IRpcChannelBuffer_AddRef(pDelegateChannel);
1621 This->pDelegateChannel = pDelegateChannel;
1622 This->tmarshal_iid = *tmarshal_riid;
1624 *ppChannel = (IRpcChannelBuffer *)&This->lpVtbl;
1629 static inline HRESULT get_facbuf_for_iid(REFIID riid, IPSFactoryBuffer **facbuf)
1634 if ((hr = CoGetPSClsid(riid, &clsid)))
1636 return CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL,
1637 &IID_IPSFactoryBuffer, (LPVOID*)facbuf);
1640 static HRESULT WINAPI
1641 PSFacBuf_CreateProxy(
1642 LPPSFACTORYBUFFER iface, IUnknown* pUnkOuter, REFIID riid,
1643 IRpcProxyBuffer **ppProxy, LPVOID *ppv)
1648 const FUNCDESC *fdesc;
1652 TRACE("(...%s...)\n",debugstr_guid(riid));
1653 hres = _get_typeinfo_for_iid(riid,&tinfo);
1655 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
1658 nroffuncs = _nroffuncs(tinfo);
1659 proxy = CoTaskMemAlloc(sizeof(TMProxyImpl));
1660 if (!proxy) return E_OUTOFMEMORY;
1662 assert(sizeof(TMAsmProxy) == 12);
1664 proxy->dispatch = NULL;
1665 proxy->dispatch_proxy = NULL;
1666 proxy->outerunknown = pUnkOuter;
1667 proxy->asmstubs = VirtualAlloc(NULL, sizeof(TMAsmProxy) * nroffuncs, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
1668 if (!proxy->asmstubs) {
1669 ERR("Could not commit pages for proxy thunks\n");
1670 CoTaskMemFree(proxy);
1671 return E_OUTOFMEMORY;
1673 proxy->lpvtbl2 = &tmproxyvtable;
1674 /* one reference for the proxy */
1676 proxy->tinfo = tinfo;
1677 memcpy(&proxy->iid,riid,sizeof(*riid));
1680 InitializeCriticalSection(&proxy->crit);
1682 proxy->lpvtbl = HeapAlloc(GetProcessHeap(),0,sizeof(LPBYTE)*nroffuncs);
1683 for (i=0;i<nroffuncs;i++) {
1684 TMAsmProxy *xasm = proxy->asmstubs+i;
1688 proxy->lpvtbl[i] = ProxyIUnknown_QueryInterface;
1691 proxy->lpvtbl[i] = ProxyIUnknown_AddRef;
1694 proxy->lpvtbl[i] = ProxyIUnknown_Release;
1698 /* nrofargs without This */
1701 hres = _get_funcdesc(tinfo,i,&tinfo2,&fdesc,NULL,NULL);
1702 ITypeInfo_Release(tinfo2);
1704 ERR("GetFuncDesc %x should not fail here.\n",hres);
1707 /* some args take more than 4 byte on the stack */
1709 for (j=0;j<fdesc->cParams;j++)
1710 nrofargs += _argsize(fdesc->lprgelemdescParam[j].tdesc.vt);
1713 if (fdesc->callconv != CC_STDCALL) {
1714 ERR("calling convention is not stdcall????\n");
1717 /* popl %eax - return ptr
1724 * arg3 arg2 arg1 <method> <returnptr>
1726 xasm->popleax = 0x58;
1727 xasm->pushlval = 0x6a;
1729 xasm->pushleax = 0x50;
1730 xasm->lcall = 0xe8; /* relative jump */
1731 xasm->xcall = (DWORD)xCall;
1732 xasm->xcall -= (DWORD)&(xasm->lret);
1734 xasm->bytestopop= (nrofargs+2)*4; /* pop args, This, iMethod */
1735 proxy->lpvtbl[i] = xasm;
1738 FIXME("not implemented on non i386\n");
1745 /* if we derive from IDispatch then defer to its proxy for its methods */
1746 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
1749 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
1751 IPSFactoryBuffer *factory_buffer;
1752 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
1755 hres = IPSFactoryBuffer_CreateProxy(factory_buffer, NULL,
1756 &IID_IDispatch, &proxy->dispatch_proxy,
1757 (void **)&proxy->dispatch);
1758 IPSFactoryBuffer_Release(factory_buffer);
1760 if ((hres == S_OK) && (nroffuncs < 7))
1762 ERR("nroffuncs calculated incorrectly (%d)\n", nroffuncs);
1763 hres = E_UNEXPECTED;
1767 proxy->lpvtbl[3] = ProxyIDispatch_GetTypeInfoCount;
1768 proxy->lpvtbl[4] = ProxyIDispatch_GetTypeInfo;
1769 proxy->lpvtbl[5] = ProxyIDispatch_GetIDsOfNames;
1770 proxy->lpvtbl[6] = ProxyIDispatch_Invoke;
1773 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
1778 *ppv = (LPVOID)proxy;
1779 *ppProxy = (IRpcProxyBuffer *)&(proxy->lpvtbl2);
1780 IUnknown_AddRef((IUnknown *)*ppv);
1784 TMProxyImpl_Release((IRpcProxyBuffer *)&proxy->lpvtbl2);
1788 typedef struct _TMStubImpl {
1789 const IRpcStubBufferVtbl *lpvtbl;
1795 IRpcStubBuffer *dispatch_stub;
1796 BOOL dispatch_derivative;
1799 static HRESULT WINAPI
1800 TMStubImpl_QueryInterface(LPRPCSTUBBUFFER iface, REFIID riid, LPVOID *ppv)
1802 if (IsEqualIID(riid,&IID_IRpcStubBuffer)||IsEqualIID(riid,&IID_IUnknown)){
1803 *ppv = (LPVOID)iface;
1804 IRpcStubBuffer_AddRef(iface);
1807 FIXME("%s, not supported IID.\n",debugstr_guid(riid));
1808 return E_NOINTERFACE;
1812 TMStubImpl_AddRef(LPRPCSTUBBUFFER iface)
1814 TMStubImpl *This = (TMStubImpl *)iface;
1815 ULONG refCount = InterlockedIncrement(&This->ref);
1817 TRACE("(%p)->(ref before=%u)\n", This, refCount - 1);
1823 TMStubImpl_Release(LPRPCSTUBBUFFER iface)
1825 TMStubImpl *This = (TMStubImpl *)iface;
1826 ULONG refCount = InterlockedDecrement(&This->ref);
1828 TRACE("(%p)->(ref before=%u)\n", This, refCount + 1);
1832 IRpcStubBuffer_Disconnect(iface);
1833 ITypeInfo_Release(This->tinfo);
1834 if (This->dispatch_stub)
1835 IRpcStubBuffer_Release(This->dispatch_stub);
1836 CoTaskMemFree(This);
1841 static HRESULT WINAPI
1842 TMStubImpl_Connect(LPRPCSTUBBUFFER iface, LPUNKNOWN pUnkServer)
1844 TMStubImpl *This = (TMStubImpl *)iface;
1846 TRACE("(%p)->(%p)\n", This, pUnkServer);
1848 IUnknown_AddRef(pUnkServer);
1849 This->pUnk = pUnkServer;
1851 if (This->dispatch_stub)
1852 IRpcStubBuffer_Connect(This->dispatch_stub, pUnkServer);
1858 TMStubImpl_Disconnect(LPRPCSTUBBUFFER iface)
1860 TMStubImpl *This = (TMStubImpl *)iface;
1862 TRACE("(%p)->()\n", This);
1866 IUnknown_Release(This->pUnk);
1870 if (This->dispatch_stub)
1871 IRpcStubBuffer_Disconnect(This->dispatch_stub);
1874 static HRESULT WINAPI
1876 LPRPCSTUBBUFFER iface, RPCOLEMESSAGE* xmsg,IRpcChannelBuffer*rpcchanbuf)
1879 const FUNCDESC *fdesc;
1880 TMStubImpl *This = (TMStubImpl *)iface;
1882 DWORD *args = NULL, res, *xargs, nrofargs;
1887 ITypeInfo *tinfo = NULL;
1891 if (xmsg->iMethod < 3) {
1892 ERR("IUnknown methods cannot be marshaled by the typelib marshaler\n");
1893 return E_UNEXPECTED;
1896 if (This->dispatch_derivative && xmsg->iMethod < sizeof(IDispatchVtbl)/sizeof(void *))
1898 IPSFactoryBuffer *factory_buffer;
1899 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
1902 hres = IPSFactoryBuffer_CreateStub(factory_buffer, &IID_IDispatch,
1903 This->pUnk, &This->dispatch_stub);
1904 IPSFactoryBuffer_Release(factory_buffer);
1908 return IRpcStubBuffer_Invoke(This->dispatch_stub, xmsg, rpcchanbuf);
1911 memset(&buf,0,sizeof(buf));
1912 buf.size = xmsg->cbBuffer;
1913 buf.base = HeapAlloc(GetProcessHeap(), 0, xmsg->cbBuffer);
1914 memcpy(buf.base, xmsg->Buffer, xmsg->cbBuffer);
1917 hres = _get_funcdesc(This->tinfo,xmsg->iMethod,&tinfo,&fdesc,&iname,NULL);
1919 ERR("GetFuncDesc on method %d failed with %x\n",xmsg->iMethod,hres);
1923 if (iname && !lstrcmpW(iname, IDispatchW))
1925 ERR("IDispatch cannot be marshaled by the typelib marshaler\n");
1926 hres = E_UNEXPECTED;
1927 SysFreeString (iname);
1931 if (iname) SysFreeString (iname);
1933 /* Need them for hack below */
1934 memset(names,0,sizeof(names));
1935 ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames);
1936 if (nrofnames > sizeof(names)/sizeof(names[0])) {
1937 ERR("Need more names!\n");
1940 /*dump_FUNCDESC(fdesc);*/
1942 for (i=0;i<fdesc->cParams;i++)
1943 nrofargs += _argsize(fdesc->lprgelemdescParam[i].tdesc.vt);
1944 args = HeapAlloc(GetProcessHeap(),0,(nrofargs+1)*sizeof(DWORD));
1947 hres = E_OUTOFMEMORY;
1951 /* Allocate all stuff used by call. */
1953 for (i=0;i<fdesc->cParams;i++) {
1954 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1956 hres = deserialize_param(
1965 xargs += _argsize(elem->tdesc.vt);
1967 ERR("Failed to deserialize param %s, hres %x\n",relaystr(names[i+1]),hres);
1972 args[0] = (DWORD)This->pUnk;
1977 (*((FARPROC**)args[0]))[fdesc->oVft/4],
1985 DWORD dwExceptionCode = GetExceptionCode();
1986 ERR("invoke call failed with exception 0x%08x (%d)\n", dwExceptionCode, dwExceptionCode);
1987 if (FAILED(dwExceptionCode))
1988 hres = dwExceptionCode;
1990 hres = HRESULT_FROM_WIN32(dwExceptionCode);
2000 for (i=0;i<fdesc->cParams;i++) {
2001 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2002 hres = serialize_param(
2011 xargs += _argsize(elem->tdesc.vt);
2013 ERR("Failed to stuballoc param, hres %x\n",hres);
2018 hres = xbuf_add (&buf, (LPBYTE)&res, sizeof(DWORD));
2023 xmsg->cbBuffer = buf.curoff;
2024 hres = IRpcChannelBuffer_GetBuffer(rpcchanbuf, xmsg, &This->iid);
2026 ERR("IRpcChannelBuffer_GetBuffer failed with error 0x%08x\n", hres);
2029 memcpy(xmsg->Buffer, buf.base, buf.curoff);
2032 for (i = 0; i < nrofnames; i++)
2033 SysFreeString(names[i]);
2035 ITypeInfo_Release(tinfo);
2036 HeapFree(GetProcessHeap(), 0, args);
2038 HeapFree(GetProcessHeap(), 0, buf.base);
2040 TRACE("returning\n");
2044 static LPRPCSTUBBUFFER WINAPI
2045 TMStubImpl_IsIIDSupported(LPRPCSTUBBUFFER iface, REFIID riid) {
2046 FIXME("Huh (%s)?\n",debugstr_guid(riid));
2051 TMStubImpl_CountRefs(LPRPCSTUBBUFFER iface) {
2052 TMStubImpl *This = (TMStubImpl *)iface;
2055 return This->ref; /*FIXME? */
2058 static HRESULT WINAPI
2059 TMStubImpl_DebugServerQueryInterface(LPRPCSTUBBUFFER iface, LPVOID *ppv) {
2064 TMStubImpl_DebugServerRelease(LPRPCSTUBBUFFER iface, LPVOID ppv) {
2068 static const IRpcStubBufferVtbl tmstubvtbl = {
2069 TMStubImpl_QueryInterface,
2073 TMStubImpl_Disconnect,
2075 TMStubImpl_IsIIDSupported,
2076 TMStubImpl_CountRefs,
2077 TMStubImpl_DebugServerQueryInterface,
2078 TMStubImpl_DebugServerRelease
2081 static HRESULT WINAPI
2082 PSFacBuf_CreateStub(
2083 LPPSFACTORYBUFFER iface, REFIID riid,IUnknown *pUnkServer,
2084 IRpcStubBuffer** ppStub
2091 TRACE("(%s,%p,%p)\n",debugstr_guid(riid),pUnkServer,ppStub);
2093 hres = _get_typeinfo_for_iid(riid,&tinfo);
2095 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
2099 stub = CoTaskMemAlloc(sizeof(TMStubImpl));
2101 return E_OUTOFMEMORY;
2102 stub->lpvtbl = &tmstubvtbl;
2104 stub->tinfo = tinfo;
2105 stub->dispatch_stub = NULL;
2106 stub->dispatch_derivative = FALSE;
2107 memcpy(&(stub->iid),riid,sizeof(*riid));
2108 hres = IRpcStubBuffer_Connect((LPRPCSTUBBUFFER)stub,pUnkServer);
2109 *ppStub = (LPRPCSTUBBUFFER)stub;
2110 TRACE("IRpcStubBuffer: %p\n", stub);
2112 ERR("Connect to pUnkServer failed?\n");
2114 /* if we derive from IDispatch then defer to its stub for some of its methods */
2115 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
2118 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
2119 stub->dispatch_derivative = TRUE;
2120 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
2126 static const IPSFactoryBufferVtbl psfacbufvtbl = {
2127 PSFacBuf_QueryInterface,
2130 PSFacBuf_CreateProxy,
2134 /* This is the whole PSFactoryBuffer object, just the vtableptr */
2135 static const IPSFactoryBufferVtbl *lppsfac = &psfacbufvtbl;
2137 /***********************************************************************
2138 * TMARSHAL_DllGetClassObject
2140 HRESULT TMARSHAL_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
2142 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)) {
2146 return E_NOINTERFACE;