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);
359 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
362 hres = ITypeInfoImpl_GetInternalFuncDesc(tinfo,i,&fdesc);
373 #include "pshpack1.h"
375 typedef struct _TMAsmProxy {
389 # warning You need to implement stubless proxies for your architecture
390 typedef struct _TMAsmProxy {
394 typedef struct _TMProxyImpl {
396 const IRpcProxyBufferVtbl *lpvtbl2;
399 TMAsmProxy *asmstubs;
401 IRpcChannelBuffer* chanbuf;
403 CRITICAL_SECTION crit;
404 IUnknown *outerunknown;
406 IRpcProxyBuffer *dispatch_proxy;
409 static HRESULT WINAPI
410 TMProxyImpl_QueryInterface(LPRPCPROXYBUFFER iface, REFIID riid, LPVOID *ppv)
413 if (IsEqualIID(riid,&IID_IUnknown)||IsEqualIID(riid,&IID_IRpcProxyBuffer)) {
414 *ppv = (LPVOID)iface;
415 IRpcProxyBuffer_AddRef(iface);
418 FIXME("no interface for %s\n",debugstr_guid(riid));
419 return E_NOINTERFACE;
423 TMProxyImpl_AddRef(LPRPCPROXYBUFFER iface)
425 ICOM_THIS_MULTI(TMProxyImpl,lpvtbl2,iface);
426 ULONG refCount = InterlockedIncrement(&This->ref);
428 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
434 TMProxyImpl_Release(LPRPCPROXYBUFFER iface)
436 ICOM_THIS_MULTI(TMProxyImpl,lpvtbl2,iface);
437 ULONG refCount = InterlockedDecrement(&This->ref);
439 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
443 if (This->dispatch_proxy) IRpcProxyBuffer_Release(This->dispatch_proxy);
444 DeleteCriticalSection(&This->crit);
445 if (This->chanbuf) IRpcChannelBuffer_Release(This->chanbuf);
446 VirtualFree(This->asmstubs, 0, MEM_RELEASE);
447 HeapFree(GetProcessHeap(), 0, This->lpvtbl);
448 ITypeInfo_Release(This->tinfo);
454 static HRESULT WINAPI
456 LPRPCPROXYBUFFER iface,IRpcChannelBuffer* pRpcChannelBuffer)
458 ICOM_THIS_MULTI(TMProxyImpl, lpvtbl2, iface);
460 TRACE("(%p)\n", pRpcChannelBuffer);
462 EnterCriticalSection(&This->crit);
464 IRpcChannelBuffer_AddRef(pRpcChannelBuffer);
465 This->chanbuf = pRpcChannelBuffer;
467 LeaveCriticalSection(&This->crit);
469 if (This->dispatch_proxy)
471 IRpcChannelBuffer *pDelegateChannel;
472 HRESULT hr = TMarshalDispatchChannel_Create(pRpcChannelBuffer, &This->iid, &pDelegateChannel);
475 return IRpcProxyBuffer_Connect(This->dispatch_proxy, pDelegateChannel);
482 TMProxyImpl_Disconnect(LPRPCPROXYBUFFER iface)
484 ICOM_THIS_MULTI(TMProxyImpl, lpvtbl2, iface);
488 EnterCriticalSection(&This->crit);
490 IRpcChannelBuffer_Release(This->chanbuf);
491 This->chanbuf = NULL;
493 LeaveCriticalSection(&This->crit);
495 if (This->dispatch_proxy)
496 IRpcProxyBuffer_Disconnect(This->dispatch_proxy);
500 static const IRpcProxyBufferVtbl tmproxyvtable = {
501 TMProxyImpl_QueryInterface,
505 TMProxyImpl_Disconnect
508 /* how much space do we use on stack in DWORD steps. */
513 return 8/sizeof(DWORD);
515 return sizeof(double)/sizeof(DWORD);
517 return sizeof(CY)/sizeof(DWORD);
519 return sizeof(DATE)/sizeof(DWORD);
521 return (sizeof(VARIANT)+3)/sizeof(DWORD);
528 _xsize(TYPEDESC *td) {
533 return sizeof(VARIANT)+3;
536 ARRAYDESC *adesc = td->u.lpadesc;
538 for (i=0;i<adesc->cDims;i++)
539 arrsize *= adesc->rgbounds[i].cElements;
540 return arrsize*_xsize(&adesc->tdescElem);
568 TRACE("(tdesc.vt %s)\n",debugstr_vt(tdesc->vt));
571 case VT_EMPTY: /* nothing. empty variant for instance */
577 if (debugout) TRACE_(olerelay)("%x%x\n",arg[0],arg[1]);
579 hres = xbuf_add(buf,(LPBYTE)arg,8);
589 if (debugout) TRACE_(olerelay)("%x\n",*arg);
591 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
596 if (debugout) TRACE_(olerelay)("%04x\n",*arg & 0xffff);
598 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
603 if (debugout) TRACE_(olerelay)("%02x\n",*arg & 0xff);
605 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
609 if (debugout) TRACE_(olerelay)("&0x%x\n",*arg);
611 hres = xbuf_add(buf,(LPBYTE)(DWORD*)*arg,sizeof(DWORD));
612 /* do not dealloc at this time */
616 VARIANT *vt = (VARIANT*)arg;
617 DWORD vttype = V_VT(vt);
619 if (debugout) TRACE_(olerelay)("Vt(%s%s)(",debugstr_vt(vttype),debugstr_vf(vttype));
622 hres = xbuf_add(buf,(LPBYTE)&vttype,sizeof(vttype));
623 if (hres) return hres;
625 /* need to recurse since we need to free the stuff */
626 hres = serialize_param(tinfo,writeit,debugout,dealloc,&tdesc2,(DWORD*)&(V_I4(vt)),buf);
627 if (debugout) TRACE_(olerelay)(")");
630 case VT_BSTR|VT_BYREF: {
631 if (debugout) TRACE_(olerelay)("[byref]'%s'", *(BSTR*)*arg ? relaystr(*((BSTR*)*arg)) : "<bstr NULL>");
633 /* ptr to ptr to magic widestring, basically */
634 BSTR *bstr = (BSTR *) *arg;
637 /* -1 means "null string" which is equivalent to empty string */
639 hres = xbuf_add(buf, (LPBYTE)&len,sizeof(DWORD));
640 if (hres) return hres;
642 len = *((DWORD*)*bstr-1)/sizeof(WCHAR);
643 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
644 if (hres) return hres;
645 hres = xbuf_add(buf,(LPBYTE)*bstr,len * sizeof(WCHAR));
646 if (hres) return hres;
650 if (dealloc && arg) {
651 BSTR *str = *((BSTR **)arg);
660 TRACE_(olerelay)("%s",relaystr((WCHAR*)*arg));
662 TRACE_(olerelay)("<bstr NULL>");
665 BSTR bstr = (BSTR)*arg;
669 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
670 if (hres) return hres;
672 len = *((DWORD*)bstr-1)/sizeof(WCHAR);
673 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
674 if (hres) return hres;
675 hres = xbuf_add(buf,(LPBYTE)bstr,len * sizeof(WCHAR));
676 if (hres) return hres;
681 SysFreeString((BSTR)*arg);
686 BOOL derefhere = TRUE;
688 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
692 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
694 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
697 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
698 switch (tattr->typekind) {
699 case TKIND_ENUM: /* confirmed */
700 case TKIND_RECORD: /* FIXME: mostly untested */
703 case TKIND_ALIAS: /* FIXME: untested */
704 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
705 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
709 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
713 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
714 ITypeInfo_Release(tinfo2);
717 if (debugout) TRACE_(olerelay)("*");
718 /* Write always, so the other side knows when it gets a NULL pointer.
720 cookie = *arg ? 0x42424242 : 0;
721 hres = xbuf_add(buf,(LPBYTE)&cookie,sizeof(cookie));
725 if (debugout) TRACE_(olerelay)("NULL");
728 hres = serialize_param(tinfo,writeit,debugout,dealloc,tdesc->u.lptdesc,(DWORD*)*arg,buf);
729 if (derefhere && dealloc) HeapFree(GetProcessHeap(),0,(LPVOID)*arg);
733 if (debugout) TRACE_(olerelay)("unk(0x%x)",*arg);
735 hres = _marshal_interface(buf,&IID_IUnknown,(LPUNKNOWN)*arg);
736 if (dealloc && *(IUnknown **)arg)
737 IUnknown_Release((LPUNKNOWN)*arg);
740 if (debugout) TRACE_(olerelay)("idisp(0x%x)",*arg);
742 hres = _marshal_interface(buf,&IID_IDispatch,(LPUNKNOWN)*arg);
743 if (dealloc && *(IUnknown **)arg)
744 IUnknown_Release((LPUNKNOWN)*arg);
747 if (debugout) TRACE_(olerelay)("<void>");
749 case VT_USERDEFINED: {
753 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
755 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
758 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
759 switch (tattr->typekind) {
761 case TKIND_INTERFACE:
763 hres=_marshal_interface(buf,&(tattr->guid),(LPUNKNOWN)arg);
765 IUnknown_Release((LPUNKNOWN)arg);
769 if (debugout) TRACE_(olerelay)("{");
770 for (i=0;i<tattr->cVars;i++) {
775 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
777 ERR("Could not get vardesc of %d\n",i);
780 elem2 = &vdesc->elemdescVar;
781 tdesc2 = &elem2->tdesc;
782 hres = serialize_param(
788 (DWORD*)(((LPBYTE)arg)+vdesc->u.oInst),
791 ITypeInfo_ReleaseVarDesc(tinfo2, vdesc);
794 if (debugout && (i<(tattr->cVars-1)))
795 TRACE_(olerelay)(",");
797 if (debugout) TRACE_(olerelay)("}");
801 hres = serialize_param(tinfo2,writeit,debugout,dealloc,&tattr->tdescAlias,arg,buf);
805 if (debugout) TRACE_(olerelay)("%x",*arg);
807 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
810 FIXME("Unhandled typekind %d\n",tattr->typekind);
814 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
815 ITypeInfo_Release(tinfo2);
819 ARRAYDESC *adesc = tdesc->u.lpadesc;
822 if (debugout) TRACE_(olerelay)("carr");
823 for (i=0;i<adesc->cDims;i++) {
824 if (debugout) TRACE_(olerelay)("[%d]",adesc->rgbounds[i].cElements);
825 arrsize *= adesc->rgbounds[i].cElements;
827 if (debugout) TRACE_(olerelay)("(vt %s)",debugstr_vt(adesc->tdescElem.vt));
828 if (debugout) TRACE_(olerelay)("[");
829 for (i=0;i<arrsize;i++) {
830 hres = serialize_param(tinfo, writeit, debugout, dealloc, &adesc->tdescElem, (DWORD*)((LPBYTE)arg+i*_xsize(&adesc->tdescElem)), buf);
833 if (debugout && (i<arrsize-1)) TRACE_(olerelay)(",");
835 if (debugout) TRACE_(olerelay)("]");
841 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
842 ULONG size = LPSAFEARRAY_UserSize(&flags, buf->curoff, (LPSAFEARRAY *)arg);
843 xbuf_resize(buf, size);
844 LPSAFEARRAY_UserMarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
850 ERR("Unhandled marshal type %d.\n",tdesc->vt);
867 TRACE("vt %s at %p\n",debugstr_vt(tdesc->vt),arg);
872 if (debugout) TRACE_(olerelay)("<empty>\n");
875 if (debugout) TRACE_(olerelay)("<null>\n");
878 VARIANT *vt = (VARIANT*)arg;
883 hres = xbuf_get(buf,(LPBYTE)&vttype,sizeof(vttype));
885 FIXME("vt type not read?\n");
888 memset(&tdesc2,0,sizeof(tdesc2));
891 if (debugout) TRACE_(olerelay)("Vt(%s%s)(",debugstr_vt(vttype),debugstr_vf(vttype));
892 hres = deserialize_param(tinfo, readit, debugout, alloc, &tdesc2, (DWORD*)&(V_I4(vt)), buf);
893 TRACE_(olerelay)(")");
904 hres = xbuf_get(buf,(LPBYTE)arg,8);
905 if (hres) ERR("Failed to read integer 8 byte\n");
907 if (debugout) TRACE_(olerelay)("%x%x",arg[0],arg[1]);
917 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
918 if (hres) ERR("Failed to read integer 4 byte\n");
920 if (debugout) TRACE_(olerelay)("%x",*arg);
926 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
927 if (hres) ERR("Failed to read integer 4 byte\n");
930 if (debugout) TRACE_(olerelay)("%04x",*arg & 0xffff);
936 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
937 if (hres) ERR("Failed to read integer 4 byte\n");
940 if (debugout) TRACE_(olerelay)("%02x",*arg & 0xff);
945 *arg = (DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
947 hres = xbuf_get(buf,(LPBYTE)*arg,sizeof(DWORD));
948 if (hres) ERR("Failed to read integer 4 byte\n");
950 if (debugout) TRACE_(olerelay)("&0x%x",*(DWORD*)*arg);
952 case VT_BSTR|VT_BYREF: {
953 BSTR **bstr = (BSTR **)arg;
958 hres = xbuf_get(buf,(LPBYTE)&len,sizeof(DWORD));
960 ERR("failed to read bstr klen\n");
964 *bstr = CoTaskMemAlloc(sizeof(BSTR *));
966 if (debugout) TRACE_(olerelay)("<bstr NULL>");
968 str = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(len+1)*sizeof(WCHAR));
969 hres = xbuf_get(buf,(LPBYTE)str,len*sizeof(WCHAR));
971 ERR("Failed to read BSTR.\n");
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");
1004 *arg = (DWORD)SysAllocStringLen(str,len);
1005 if (debugout) TRACE_(olerelay)("%s",relaystr(str));
1006 HeapFree(GetProcessHeap(),0,str);
1015 BOOL derefhere = TRUE;
1017 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
1021 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
1023 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
1026 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1027 switch (tattr->typekind) {
1028 case TKIND_ENUM: /* confirmed */
1029 case TKIND_RECORD: /* FIXME: mostly untested */
1032 case TKIND_ALIAS: /* FIXME: untested */
1033 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
1034 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
1038 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
1042 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1043 ITypeInfo_Release(tinfo2);
1045 /* read it in all cases, we need to know if we have
1046 * NULL pointer or not.
1048 hres = xbuf_get(buf,(LPBYTE)&cookie,sizeof(cookie));
1050 ERR("Failed to load pointer cookie.\n");
1053 if (cookie != 0x42424242) {
1054 /* we read a NULL ptr from the remote side */
1055 if (debugout) TRACE_(olerelay)("NULL");
1059 if (debugout) TRACE_(olerelay)("*");
1061 /* Allocate space for the referenced struct */
1063 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,_xsize(tdesc->u.lptdesc));
1066 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, (LPDWORD)*arg, buf);
1068 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, arg, buf);
1071 /* FIXME: UNKNOWN is unknown ..., but allocate 4 byte for it */
1073 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
1076 hres = _unmarshal_interface(buf,&IID_IUnknown,(LPUNKNOWN*)arg);
1078 TRACE_(olerelay)("unk(%p)",arg);
1083 hres = _unmarshal_interface(buf,&IID_IDispatch,(LPUNKNOWN*)arg);
1085 TRACE_(olerelay)("idisp(%p)",arg);
1088 if (debugout) TRACE_(olerelay)("<void>");
1090 case VT_USERDEFINED: {
1094 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
1096 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
1099 hres = ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1101 ERR("Could not get typeattr in VT_USERDEFINED.\n");
1103 switch (tattr->typekind) {
1104 case TKIND_DISPATCH:
1105 case TKIND_INTERFACE:
1107 hres = _unmarshal_interface(buf,&(tattr->guid),(LPUNKNOWN*)arg);
1109 case TKIND_RECORD: {
1113 *arg = (DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,tattr->cbSizeInstance);
1115 if (debugout) TRACE_(olerelay)("{");
1116 for (i=0;i<tattr->cVars;i++) {
1119 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
1121 ERR("Could not get vardesc of %d\n",i);
1122 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1123 ITypeInfo_Release(tinfo2);
1126 hres = deserialize_param(
1131 &vdesc->elemdescVar.tdesc,
1132 (DWORD*)(((LPBYTE)*arg)+vdesc->u.oInst),
1135 ITypeInfo2_ReleaseVarDesc(tinfo2, vdesc);
1136 if (debugout && (i<tattr->cVars-1)) TRACE_(olerelay)(",");
1138 if (debugout) TRACE_(olerelay)("}");
1142 hres = deserialize_param(tinfo2,readit,debugout,alloc,&tattr->tdescAlias,arg,buf);
1146 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
1147 if (hres) ERR("Failed to read enum (4 byte)\n");
1149 if (debugout) TRACE_(olerelay)("%x",*arg);
1152 ERR("Unhandled typekind %d\n",tattr->typekind);
1156 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1159 ERR("failed to stuballoc in TKIND_RECORD.\n");
1160 ITypeInfo_Release(tinfo2);
1164 /* arg is pointing to the start of the array. */
1165 ARRAYDESC *adesc = tdesc->u.lpadesc;
1168 if (adesc->cDims > 1) FIXME("cDims > 1 in VT_CARRAY. Does it work?\n");
1169 for (i=0;i<adesc->cDims;i++)
1170 arrsize *= adesc->rgbounds[i].cElements;
1171 for (i=0;i<arrsize;i++)
1178 (DWORD*)((LPBYTE)(arg)+i*_xsize(&adesc->tdescElem)),
1183 case VT_SAFEARRAY: {
1186 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
1187 unsigned char *buffer;
1188 buffer = LPSAFEARRAY_UserUnmarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
1189 buf->curoff = buffer - buf->base;
1194 ERR("No handler for VT type %d!\n",tdesc->vt);
1200 /* Searches function, also in inherited interfaces */
1203 ITypeInfo *tinfo, int iMethod, ITypeInfo **tactual, const FUNCDESC **fdesc, BSTR *iname, BSTR *fname)
1208 if (fname) *fname = NULL;
1209 if (iname) *iname = NULL;
1212 hres = ITypeInfoImpl_GetInternalFuncDesc(tinfo, i, fdesc);
1219 hres = ITypeInfo_GetTypeAttr(tinfo, &attr);
1221 ERR("GetTypeAttr failed with %x\n",hres);
1224 /* Not found, so look in inherited ifaces. */
1225 for (j=0;j<attr->cImplTypes;j++) {
1226 hres = ITypeInfo_GetRefTypeOfImplType(tinfo, j, &href);
1228 ERR("Did not find a reftype for interface offset %d?\n",j);
1231 hres = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
1233 ERR("Did not find a typeinfo for reftype %d?\n",href);
1236 hres = _get_funcdesc(tinfo2,iMethod,tactual,fdesc,iname,fname);
1237 ITypeInfo_Release(tinfo2);
1239 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1243 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1246 if (((*fdesc)->oVft/4) == iMethod) {
1248 ITypeInfo_GetDocumentation(tinfo,(*fdesc)->memid,fname,NULL,NULL,NULL);
1250 ITypeInfo_GetDocumentation(tinfo,-1,iname,NULL,NULL,NULL);
1252 ITypeInfo_AddRef(*tactual);
1259 static inline BOOL is_in_elem(const ELEMDESC *elem)
1261 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN || !elem->u.paramdesc.wParamFlags);
1264 static inline BOOL is_out_elem(const ELEMDESC *elem)
1266 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT || !elem->u.paramdesc.wParamFlags);
1270 xCall(LPVOID retptr, int method, TMProxyImpl *tpinfo /*, args */)
1272 DWORD *args = ((DWORD*)&tpinfo)+1, *xargs;
1273 const FUNCDESC *fdesc;
1275 int i, relaydeb = TRACE_ON(olerelay);
1282 DWORD remoteresult = 0;
1284 IRpcChannelBuffer *chanbuf;
1286 EnterCriticalSection(&tpinfo->crit);
1288 hres = _get_funcdesc(tpinfo->tinfo,method,&tinfo,&fdesc,&iname,&fname);
1290 ERR("Did not find typeinfo/funcdesc entry for method %d!\n",method);
1291 ITypeInfo_Release(tinfo);
1292 LeaveCriticalSection(&tpinfo->crit);
1296 if (!tpinfo->chanbuf)
1298 WARN("Tried to use disconnected proxy\n");
1299 ITypeInfo_Release(tinfo);
1300 LeaveCriticalSection(&tpinfo->crit);
1301 return RPC_E_DISCONNECTED;
1303 chanbuf = tpinfo->chanbuf;
1304 IRpcChannelBuffer_AddRef(chanbuf);
1306 LeaveCriticalSection(&tpinfo->crit);
1309 TRACE_(olerelay)("->");
1311 TRACE_(olerelay)("%s:",relaystr(iname));
1313 TRACE_(olerelay)("%s(%d)",relaystr(fname),method);
1315 TRACE_(olerelay)("%d",method);
1316 TRACE_(olerelay)("(");
1319 if (iname) SysFreeString(iname);
1320 if (fname) SysFreeString(fname);
1322 memset(&buf,0,sizeof(buf));
1324 /* normal typelib driven serializing */
1326 /* Need them for hack below */
1327 memset(names,0,sizeof(names));
1328 if (ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames))
1330 if (nrofnames > sizeof(names)/sizeof(names[0]))
1331 ERR("Need more names!\n");
1334 for (i=0;i<fdesc->cParams;i++) {
1335 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1337 if (i) TRACE_(olerelay)(",");
1338 if (i+1<nrofnames && names[i+1])
1339 TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1341 /* No need to marshal other data than FIN and any VT_PTR. */
1342 if (!is_in_elem(elem) && (elem->tdesc.vt != VT_PTR)) {
1343 xargs+=_argsize(elem->tdesc.vt);
1344 if (relaydeb) TRACE_(olerelay)("[out]");
1347 hres = serialize_param(
1358 ERR("Failed to serialize param, hres %x\n",hres);
1361 xargs+=_argsize(elem->tdesc.vt);
1363 if (relaydeb) TRACE_(olerelay)(")");
1365 memset(&msg,0,sizeof(msg));
1366 msg.cbBuffer = buf.curoff;
1367 msg.iMethod = method;
1368 hres = IRpcChannelBuffer_GetBuffer(chanbuf,&msg,&(tpinfo->iid));
1370 ERR("RpcChannelBuffer GetBuffer failed, %x\n",hres);
1373 memcpy(msg.Buffer,buf.base,buf.curoff);
1374 if (relaydeb) TRACE_(olerelay)("\n");
1375 hres = IRpcChannelBuffer_SendReceive(chanbuf,&msg,&status);
1377 ERR("RpcChannelBuffer SendReceive failed, %x\n",hres);
1381 if (relaydeb) TRACE_(olerelay)(" status = %08x (",status);
1383 buf.base = HeapReAlloc(GetProcessHeap(),0,buf.base,msg.cbBuffer);
1385 buf.base = HeapAlloc(GetProcessHeap(),0,msg.cbBuffer);
1386 buf.size = msg.cbBuffer;
1387 memcpy(buf.base,msg.Buffer,buf.size);
1390 /* generic deserializer using typelib description */
1393 for (i=0;i<fdesc->cParams;i++) {
1394 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1397 if (i) TRACE_(olerelay)(",");
1398 if (i+1<nrofnames && names[i+1]) TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1400 /* No need to marshal other data than FOUT and any VT_PTR */
1401 if (!is_out_elem(elem) && (elem->tdesc.vt != VT_PTR)) {
1402 xargs += _argsize(elem->tdesc.vt);
1403 if (relaydeb) TRACE_(olerelay)("[in]");
1406 hres = deserialize_param(
1416 ERR("Failed to unmarshall param, hres %x\n",hres);
1420 xargs += _argsize(elem->tdesc.vt);
1423 hres = xbuf_get(&buf, (LPBYTE)&remoteresult, sizeof(DWORD));
1426 if (relaydeb) TRACE_(olerelay)(") = %08x\n", remoteresult);
1428 hres = remoteresult;
1431 for (i = 0; i < nrofnames; i++)
1432 SysFreeString(names[i]);
1433 HeapFree(GetProcessHeap(),0,buf.base);
1434 IRpcChannelBuffer_Release(chanbuf);
1435 ITypeInfo_Release(tinfo);
1436 TRACE("-- 0x%08x\n", hres);
1440 static HRESULT WINAPI ProxyIUnknown_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
1442 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1444 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1446 if (proxy->outerunknown)
1447 return IUnknown_QueryInterface(proxy->outerunknown, riid, ppv);
1449 FIXME("No interface\n");
1450 return E_NOINTERFACE;
1453 static ULONG WINAPI ProxyIUnknown_AddRef(IUnknown *iface)
1455 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1459 if (proxy->outerunknown)
1460 return IUnknown_AddRef(proxy->outerunknown);
1462 return 2; /* FIXME */
1465 static ULONG WINAPI ProxyIUnknown_Release(IUnknown *iface)
1467 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1471 if (proxy->outerunknown)
1472 return IUnknown_Release(proxy->outerunknown);
1474 return 1; /* FIXME */
1477 static HRESULT WINAPI ProxyIDispatch_GetTypeInfoCount(LPDISPATCH iface, UINT * pctinfo)
1479 TMProxyImpl *This = (TMProxyImpl *)iface;
1481 TRACE("(%p)\n", pctinfo);
1483 return IDispatch_GetTypeInfoCount(This->dispatch, pctinfo);
1486 static HRESULT WINAPI ProxyIDispatch_GetTypeInfo(LPDISPATCH iface, UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo)
1488 TMProxyImpl *This = (TMProxyImpl *)iface;
1490 TRACE("(%d, %x, %p)\n", iTInfo, lcid, ppTInfo);
1492 return IDispatch_GetTypeInfo(This->dispatch, iTInfo, lcid, ppTInfo);
1495 static HRESULT WINAPI ProxyIDispatch_GetIDsOfNames(LPDISPATCH iface, REFIID riid, LPOLESTR * rgszNames, UINT cNames, LCID lcid, DISPID * rgDispId)
1497 TMProxyImpl *This = (TMProxyImpl *)iface;
1499 TRACE("(%s, %p, %d, 0x%x, %p)\n", debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);
1501 return IDispatch_GetIDsOfNames(This->dispatch, riid, rgszNames,
1502 cNames, lcid, rgDispId);
1505 static HRESULT WINAPI ProxyIDispatch_Invoke(LPDISPATCH iface, DISPID dispIdMember, REFIID riid, LCID lcid,
1506 WORD wFlags, DISPPARAMS * pDispParams, VARIANT * pVarResult,
1507 EXCEPINFO * pExcepInfo, UINT * puArgErr)
1509 TMProxyImpl *This = (TMProxyImpl *)iface;
1511 TRACE("(%d, %s, 0x%x, 0x%x, %p, %p, %p, %p)\n", dispIdMember,
1512 debugstr_guid(riid), lcid, wFlags, pDispParams, pVarResult,
1513 pExcepInfo, puArgErr);
1515 return IDispatch_Invoke(This->dispatch, dispIdMember, riid, lcid,
1516 wFlags, pDispParams, pVarResult, pExcepInfo,
1522 const IRpcChannelBufferVtbl *lpVtbl;
1524 /* the IDispatch-derived interface we are handling */
1526 IRpcChannelBuffer *pDelegateChannel;
1527 } TMarshalDispatchChannel;
1529 static HRESULT WINAPI TMarshalDispatchChannel_QueryInterface(LPRPCCHANNELBUFFER iface, REFIID riid, LPVOID *ppv)
1532 if (IsEqualIID(riid,&IID_IRpcChannelBuffer) || IsEqualIID(riid,&IID_IUnknown))
1534 *ppv = (LPVOID)iface;
1535 IUnknown_AddRef(iface);
1538 return E_NOINTERFACE;
1541 static ULONG WINAPI TMarshalDispatchChannel_AddRef(LPRPCCHANNELBUFFER iface)
1543 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1544 return InterlockedIncrement(&This->refs);
1547 static ULONG WINAPI TMarshalDispatchChannel_Release(LPRPCCHANNELBUFFER iface)
1549 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1552 ref = InterlockedDecrement(&This->refs);
1556 IRpcChannelBuffer_Release(This->pDelegateChannel);
1557 HeapFree(GetProcessHeap(), 0, This);
1561 static HRESULT WINAPI TMarshalDispatchChannel_GetBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg, REFIID riid)
1563 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1564 TRACE("(%p, %s)\n", olemsg, debugstr_guid(riid));
1565 /* Note: we are pretending to invoke a method on the interface identified
1566 * by tmarshal_iid so that we can re-use the IDispatch proxy/stub code
1567 * without the RPC runtime getting confused by not exporting an IDispatch interface */
1568 return IRpcChannelBuffer_GetBuffer(This->pDelegateChannel, olemsg, &This->tmarshal_iid);
1571 static HRESULT WINAPI TMarshalDispatchChannel_SendReceive(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE *olemsg, ULONG *pstatus)
1573 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1574 TRACE("(%p, %p)\n", olemsg, pstatus);
1575 return IRpcChannelBuffer_SendReceive(This->pDelegateChannel, olemsg, pstatus);
1578 static HRESULT WINAPI TMarshalDispatchChannel_FreeBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg)
1580 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1581 TRACE("(%p)\n", olemsg);
1582 return IRpcChannelBuffer_FreeBuffer(This->pDelegateChannel, olemsg);
1585 static HRESULT WINAPI TMarshalDispatchChannel_GetDestCtx(LPRPCCHANNELBUFFER iface, DWORD* pdwDestContext, void** ppvDestContext)
1587 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1588 TRACE("(%p,%p)\n", pdwDestContext, ppvDestContext);
1589 return IRpcChannelBuffer_GetDestCtx(This->pDelegateChannel, pdwDestContext, ppvDestContext);
1592 static HRESULT WINAPI TMarshalDispatchChannel_IsConnected(LPRPCCHANNELBUFFER iface)
1594 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1596 return IRpcChannelBuffer_IsConnected(This->pDelegateChannel);
1599 static const IRpcChannelBufferVtbl TMarshalDispatchChannelVtbl =
1601 TMarshalDispatchChannel_QueryInterface,
1602 TMarshalDispatchChannel_AddRef,
1603 TMarshalDispatchChannel_Release,
1604 TMarshalDispatchChannel_GetBuffer,
1605 TMarshalDispatchChannel_SendReceive,
1606 TMarshalDispatchChannel_FreeBuffer,
1607 TMarshalDispatchChannel_GetDestCtx,
1608 TMarshalDispatchChannel_IsConnected
1611 static HRESULT TMarshalDispatchChannel_Create(
1612 IRpcChannelBuffer *pDelegateChannel, REFIID tmarshal_riid,
1613 IRpcChannelBuffer **ppChannel)
1615 TMarshalDispatchChannel *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1617 return E_OUTOFMEMORY;
1619 This->lpVtbl = &TMarshalDispatchChannelVtbl;
1621 IRpcChannelBuffer_AddRef(pDelegateChannel);
1622 This->pDelegateChannel = pDelegateChannel;
1623 This->tmarshal_iid = *tmarshal_riid;
1625 *ppChannel = (IRpcChannelBuffer *)&This->lpVtbl;
1630 static inline HRESULT get_facbuf_for_iid(REFIID riid, IPSFactoryBuffer **facbuf)
1635 if ((hr = CoGetPSClsid(riid, &clsid)))
1637 return CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL,
1638 &IID_IPSFactoryBuffer, (LPVOID*)facbuf);
1641 static HRESULT WINAPI
1642 PSFacBuf_CreateProxy(
1643 LPPSFACTORYBUFFER iface, IUnknown* pUnkOuter, REFIID riid,
1644 IRpcProxyBuffer **ppProxy, LPVOID *ppv)
1649 const FUNCDESC *fdesc;
1653 TRACE("(...%s...)\n",debugstr_guid(riid));
1654 hres = _get_typeinfo_for_iid(riid,&tinfo);
1656 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
1659 nroffuncs = _nroffuncs(tinfo);
1660 proxy = CoTaskMemAlloc(sizeof(TMProxyImpl));
1661 if (!proxy) return E_OUTOFMEMORY;
1663 assert(sizeof(TMAsmProxy) == 12);
1665 proxy->dispatch = NULL;
1666 proxy->dispatch_proxy = NULL;
1667 proxy->outerunknown = pUnkOuter;
1668 proxy->asmstubs = VirtualAlloc(NULL, sizeof(TMAsmProxy) * nroffuncs, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
1669 if (!proxy->asmstubs) {
1670 ERR("Could not commit pages for proxy thunks\n");
1671 CoTaskMemFree(proxy);
1672 return E_OUTOFMEMORY;
1674 proxy->lpvtbl2 = &tmproxyvtable;
1675 /* one reference for the proxy */
1677 proxy->tinfo = tinfo;
1678 memcpy(&proxy->iid,riid,sizeof(*riid));
1681 InitializeCriticalSection(&proxy->crit);
1683 proxy->lpvtbl = HeapAlloc(GetProcessHeap(),0,sizeof(LPBYTE)*nroffuncs);
1684 for (i=0;i<nroffuncs;i++) {
1685 TMAsmProxy *xasm = proxy->asmstubs+i;
1689 proxy->lpvtbl[i] = ProxyIUnknown_QueryInterface;
1692 proxy->lpvtbl[i] = ProxyIUnknown_AddRef;
1695 proxy->lpvtbl[i] = ProxyIUnknown_Release;
1699 /* nrofargs without This */
1702 hres = _get_funcdesc(tinfo,i,&tinfo2,&fdesc,NULL,NULL);
1703 ITypeInfo_Release(tinfo2);
1705 ERR("GetFuncDesc %x should not fail here.\n",hres);
1708 /* some args take more than 4 byte on the stack */
1710 for (j=0;j<fdesc->cParams;j++)
1711 nrofargs += _argsize(fdesc->lprgelemdescParam[j].tdesc.vt);
1714 if (fdesc->callconv != CC_STDCALL) {
1715 ERR("calling convention is not stdcall????\n");
1718 /* popl %eax - return ptr
1725 * arg3 arg2 arg1 <method> <returnptr>
1727 xasm->popleax = 0x58;
1728 xasm->pushlval = 0x6a;
1730 xasm->pushleax = 0x50;
1731 xasm->lcall = 0xe8; /* relative jump */
1732 xasm->xcall = (DWORD)xCall;
1733 xasm->xcall -= (DWORD)&(xasm->lret);
1735 xasm->bytestopop= (nrofargs+2)*4; /* pop args, This, iMethod */
1736 proxy->lpvtbl[i] = xasm;
1739 FIXME("not implemented on non i386\n");
1746 /* if we derive from IDispatch then defer to its proxy for its methods */
1747 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
1750 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
1752 IPSFactoryBuffer *factory_buffer;
1753 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
1756 hres = IPSFactoryBuffer_CreateProxy(factory_buffer, NULL,
1757 &IID_IDispatch, &proxy->dispatch_proxy,
1758 (void **)&proxy->dispatch);
1759 IPSFactoryBuffer_Release(factory_buffer);
1761 if ((hres == S_OK) && (nroffuncs < 7))
1763 ERR("nroffuncs calculated incorrectly (%d)\n", nroffuncs);
1764 hres = E_UNEXPECTED;
1768 proxy->lpvtbl[3] = ProxyIDispatch_GetTypeInfoCount;
1769 proxy->lpvtbl[4] = ProxyIDispatch_GetTypeInfo;
1770 proxy->lpvtbl[5] = ProxyIDispatch_GetIDsOfNames;
1771 proxy->lpvtbl[6] = ProxyIDispatch_Invoke;
1774 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
1779 *ppv = (LPVOID)proxy;
1780 *ppProxy = (IRpcProxyBuffer *)&(proxy->lpvtbl2);
1781 IUnknown_AddRef((IUnknown *)*ppv);
1785 TMProxyImpl_Release((IRpcProxyBuffer *)&proxy->lpvtbl2);
1789 typedef struct _TMStubImpl {
1790 const IRpcStubBufferVtbl *lpvtbl;
1796 IRpcStubBuffer *dispatch_stub;
1797 BOOL dispatch_derivative;
1800 static HRESULT WINAPI
1801 TMStubImpl_QueryInterface(LPRPCSTUBBUFFER iface, REFIID riid, LPVOID *ppv)
1803 if (IsEqualIID(riid,&IID_IRpcStubBuffer)||IsEqualIID(riid,&IID_IUnknown)){
1804 *ppv = (LPVOID)iface;
1805 IRpcStubBuffer_AddRef(iface);
1808 FIXME("%s, not supported IID.\n",debugstr_guid(riid));
1809 return E_NOINTERFACE;
1813 TMStubImpl_AddRef(LPRPCSTUBBUFFER iface)
1815 TMStubImpl *This = (TMStubImpl *)iface;
1816 ULONG refCount = InterlockedIncrement(&This->ref);
1818 TRACE("(%p)->(ref before=%u)\n", This, refCount - 1);
1824 TMStubImpl_Release(LPRPCSTUBBUFFER iface)
1826 TMStubImpl *This = (TMStubImpl *)iface;
1827 ULONG refCount = InterlockedDecrement(&This->ref);
1829 TRACE("(%p)->(ref before=%u)\n", This, refCount + 1);
1833 IRpcStubBuffer_Disconnect(iface);
1834 ITypeInfo_Release(This->tinfo);
1835 if (This->dispatch_stub)
1836 IRpcStubBuffer_Release(This->dispatch_stub);
1837 CoTaskMemFree(This);
1842 static HRESULT WINAPI
1843 TMStubImpl_Connect(LPRPCSTUBBUFFER iface, LPUNKNOWN pUnkServer)
1845 TMStubImpl *This = (TMStubImpl *)iface;
1847 TRACE("(%p)->(%p)\n", This, pUnkServer);
1849 IUnknown_AddRef(pUnkServer);
1850 This->pUnk = pUnkServer;
1852 if (This->dispatch_stub)
1853 IRpcStubBuffer_Connect(This->dispatch_stub, pUnkServer);
1859 TMStubImpl_Disconnect(LPRPCSTUBBUFFER iface)
1861 TMStubImpl *This = (TMStubImpl *)iface;
1863 TRACE("(%p)->()\n", This);
1867 IUnknown_Release(This->pUnk);
1871 if (This->dispatch_stub)
1872 IRpcStubBuffer_Disconnect(This->dispatch_stub);
1875 static HRESULT WINAPI
1877 LPRPCSTUBBUFFER iface, RPCOLEMESSAGE* xmsg,IRpcChannelBuffer*rpcchanbuf)
1880 const FUNCDESC *fdesc;
1881 TMStubImpl *This = (TMStubImpl *)iface;
1883 DWORD *args = NULL, res, *xargs, nrofargs;
1888 ITypeInfo *tinfo = NULL;
1892 if (xmsg->iMethod < 3) {
1893 ERR("IUnknown methods cannot be marshaled by the typelib marshaler\n");
1894 return E_UNEXPECTED;
1897 if (This->dispatch_derivative && xmsg->iMethod < sizeof(IDispatchVtbl)/sizeof(void *))
1899 IPSFactoryBuffer *factory_buffer;
1900 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
1903 hres = IPSFactoryBuffer_CreateStub(factory_buffer, &IID_IDispatch,
1904 This->pUnk, &This->dispatch_stub);
1905 IPSFactoryBuffer_Release(factory_buffer);
1909 return IRpcStubBuffer_Invoke(This->dispatch_stub, xmsg, rpcchanbuf);
1912 memset(&buf,0,sizeof(buf));
1913 buf.size = xmsg->cbBuffer;
1914 buf.base = HeapAlloc(GetProcessHeap(), 0, xmsg->cbBuffer);
1915 memcpy(buf.base, xmsg->Buffer, xmsg->cbBuffer);
1918 hres = _get_funcdesc(This->tinfo,xmsg->iMethod,&tinfo,&fdesc,&iname,NULL);
1920 ERR("GetFuncDesc on method %d failed with %x\n",xmsg->iMethod,hres);
1924 if (iname && !lstrcmpW(iname, IDispatchW))
1926 ERR("IDispatch cannot be marshaled by the typelib marshaler\n");
1927 hres = E_UNEXPECTED;
1928 SysFreeString (iname);
1932 if (iname) SysFreeString (iname);
1934 /* Need them for hack below */
1935 memset(names,0,sizeof(names));
1936 ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames);
1937 if (nrofnames > sizeof(names)/sizeof(names[0])) {
1938 ERR("Need more names!\n");
1941 /*dump_FUNCDESC(fdesc);*/
1943 for (i=0;i<fdesc->cParams;i++)
1944 nrofargs += _argsize(fdesc->lprgelemdescParam[i].tdesc.vt);
1945 args = HeapAlloc(GetProcessHeap(),0,(nrofargs+1)*sizeof(DWORD));
1948 hres = E_OUTOFMEMORY;
1952 /* Allocate all stuff used by call. */
1954 for (i=0;i<fdesc->cParams;i++) {
1955 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1957 hres = deserialize_param(
1966 xargs += _argsize(elem->tdesc.vt);
1968 ERR("Failed to deserialize param %s, hres %x\n",relaystr(names[i+1]),hres);
1973 args[0] = (DWORD)This->pUnk;
1978 (*((FARPROC**)args[0]))[fdesc->oVft/4],
1986 DWORD dwExceptionCode = GetExceptionCode();
1987 ERR("invoke call failed with exception 0x%08x (%d)\n", dwExceptionCode, dwExceptionCode);
1988 if (FAILED(dwExceptionCode))
1989 hres = dwExceptionCode;
1991 hres = HRESULT_FROM_WIN32(dwExceptionCode);
2001 for (i=0;i<fdesc->cParams;i++) {
2002 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2003 hres = serialize_param(
2012 xargs += _argsize(elem->tdesc.vt);
2014 ERR("Failed to stuballoc param, hres %x\n",hres);
2019 hres = xbuf_add (&buf, (LPBYTE)&res, sizeof(DWORD));
2024 xmsg->cbBuffer = buf.curoff;
2025 hres = IRpcChannelBuffer_GetBuffer(rpcchanbuf, xmsg, &This->iid);
2027 ERR("IRpcChannelBuffer_GetBuffer failed with error 0x%08x\n", hres);
2030 memcpy(xmsg->Buffer, buf.base, buf.curoff);
2033 for (i = 0; i < nrofnames; i++)
2034 SysFreeString(names[i]);
2036 ITypeInfo_Release(tinfo);
2037 HeapFree(GetProcessHeap(), 0, args);
2039 HeapFree(GetProcessHeap(), 0, buf.base);
2041 TRACE("returning\n");
2045 static LPRPCSTUBBUFFER WINAPI
2046 TMStubImpl_IsIIDSupported(LPRPCSTUBBUFFER iface, REFIID riid) {
2047 FIXME("Huh (%s)?\n",debugstr_guid(riid));
2052 TMStubImpl_CountRefs(LPRPCSTUBBUFFER iface) {
2053 TMStubImpl *This = (TMStubImpl *)iface;
2056 return This->ref; /*FIXME? */
2059 static HRESULT WINAPI
2060 TMStubImpl_DebugServerQueryInterface(LPRPCSTUBBUFFER iface, LPVOID *ppv) {
2065 TMStubImpl_DebugServerRelease(LPRPCSTUBBUFFER iface, LPVOID ppv) {
2069 static const IRpcStubBufferVtbl tmstubvtbl = {
2070 TMStubImpl_QueryInterface,
2074 TMStubImpl_Disconnect,
2076 TMStubImpl_IsIIDSupported,
2077 TMStubImpl_CountRefs,
2078 TMStubImpl_DebugServerQueryInterface,
2079 TMStubImpl_DebugServerRelease
2082 static HRESULT WINAPI
2083 PSFacBuf_CreateStub(
2084 LPPSFACTORYBUFFER iface, REFIID riid,IUnknown *pUnkServer,
2085 IRpcStubBuffer** ppStub
2092 TRACE("(%s,%p,%p)\n",debugstr_guid(riid),pUnkServer,ppStub);
2094 hres = _get_typeinfo_for_iid(riid,&tinfo);
2096 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
2100 stub = CoTaskMemAlloc(sizeof(TMStubImpl));
2102 return E_OUTOFMEMORY;
2103 stub->lpvtbl = &tmstubvtbl;
2105 stub->tinfo = tinfo;
2106 stub->dispatch_stub = NULL;
2107 stub->dispatch_derivative = FALSE;
2108 memcpy(&(stub->iid),riid,sizeof(*riid));
2109 hres = IRpcStubBuffer_Connect((LPRPCSTUBBUFFER)stub,pUnkServer);
2110 *ppStub = (LPRPCSTUBBUFFER)stub;
2111 TRACE("IRpcStubBuffer: %p\n", stub);
2113 ERR("Connect to pUnkServer failed?\n");
2115 /* if we derive from IDispatch then defer to its stub for some of its methods */
2116 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
2119 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
2120 stub->dispatch_derivative = TRUE;
2121 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
2127 static const IPSFactoryBufferVtbl psfacbufvtbl = {
2128 PSFacBuf_QueryInterface,
2131 PSFacBuf_CreateProxy,
2135 /* This is the whole PSFactoryBuffer object, just the vtableptr */
2136 static const IPSFactoryBufferVtbl *lppsfac = &psfacbufvtbl;
2138 /***********************************************************************
2139 * TMARSHAL_DllGetClassObject
2141 HRESULT TMARSHAL_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
2143 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)) {
2147 return E_NOINTERFACE;