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, LPBYTE 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);
323 /* Determine nr of functions. Since we use the toplevel interface and all
324 * inherited ones have lower numbers, we are ok to not to descent into
325 * the inheritance tree I think.
327 static int _nroffuncs(ITypeInfo *tinfo) {
329 const FUNCDESC *fdesc;
335 hres = ITypeInfo_GetTypeAttr(tinfo, &attr);
337 ERR("GetTypeAttr failed with %x\n",hres);
340 /* look in inherited ifaces. */
341 for (j=0;j<attr->cImplTypes;j++) {
343 hres = ITypeInfo_GetRefTypeOfImplType(tinfo, j, &href);
345 ERR("Did not find a reftype for interface offset %d?\n",j);
348 hres = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
350 ERR("Did not find a typeinfo for reftype %d?\n",href);
353 n += _nroffuncs(tinfo2);
354 ITypeInfo_Release(tinfo2);
356 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
359 hres = ITypeInfoImpl_GetInternalFuncDesc(tinfo,i,&fdesc);
370 #include "pshpack1.h"
372 typedef struct _TMAsmProxy {
386 # warning You need to implement stubless proxies for your architecture
387 typedef struct _TMAsmProxy {
391 typedef struct _TMProxyImpl {
393 const IRpcProxyBufferVtbl *lpvtbl2;
396 TMAsmProxy *asmstubs;
398 IRpcChannelBuffer* chanbuf;
400 CRITICAL_SECTION crit;
401 IUnknown *outerunknown;
403 IRpcProxyBuffer *dispatch_proxy;
406 static HRESULT WINAPI
407 TMProxyImpl_QueryInterface(LPRPCPROXYBUFFER iface, REFIID riid, LPVOID *ppv)
410 if (IsEqualIID(riid,&IID_IUnknown)||IsEqualIID(riid,&IID_IRpcProxyBuffer)) {
411 *ppv = (LPVOID)iface;
412 IRpcProxyBuffer_AddRef(iface);
415 FIXME("no interface for %s\n",debugstr_guid(riid));
416 return E_NOINTERFACE;
420 TMProxyImpl_AddRef(LPRPCPROXYBUFFER iface)
422 ICOM_THIS_MULTI(TMProxyImpl,lpvtbl2,iface);
423 ULONG refCount = InterlockedIncrement(&This->ref);
425 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
431 TMProxyImpl_Release(LPRPCPROXYBUFFER iface)
433 ICOM_THIS_MULTI(TMProxyImpl,lpvtbl2,iface);
434 ULONG refCount = InterlockedDecrement(&This->ref);
436 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
440 if (This->dispatch_proxy) IRpcProxyBuffer_Release(This->dispatch_proxy);
441 This->crit.DebugInfo->Spare[0] = 0;
442 DeleteCriticalSection(&This->crit);
443 if (This->chanbuf) IRpcChannelBuffer_Release(This->chanbuf);
444 VirtualFree(This->asmstubs, 0, MEM_RELEASE);
445 HeapFree(GetProcessHeap(), 0, This->lpvtbl);
446 ITypeInfo_Release(This->tinfo);
452 static HRESULT WINAPI
454 LPRPCPROXYBUFFER iface,IRpcChannelBuffer* pRpcChannelBuffer)
456 ICOM_THIS_MULTI(TMProxyImpl, lpvtbl2, iface);
458 TRACE("(%p)\n", pRpcChannelBuffer);
460 EnterCriticalSection(&This->crit);
462 IRpcChannelBuffer_AddRef(pRpcChannelBuffer);
463 This->chanbuf = pRpcChannelBuffer;
465 LeaveCriticalSection(&This->crit);
467 if (This->dispatch_proxy)
469 IRpcChannelBuffer *pDelegateChannel;
470 HRESULT hr = TMarshalDispatchChannel_Create(pRpcChannelBuffer, &This->iid, &pDelegateChannel);
473 return IRpcProxyBuffer_Connect(This->dispatch_proxy, pDelegateChannel);
480 TMProxyImpl_Disconnect(LPRPCPROXYBUFFER iface)
482 ICOM_THIS_MULTI(TMProxyImpl, lpvtbl2, iface);
486 EnterCriticalSection(&This->crit);
488 IRpcChannelBuffer_Release(This->chanbuf);
489 This->chanbuf = NULL;
491 LeaveCriticalSection(&This->crit);
493 if (This->dispatch_proxy)
494 IRpcProxyBuffer_Disconnect(This->dispatch_proxy);
498 static const IRpcProxyBufferVtbl tmproxyvtable = {
499 TMProxyImpl_QueryInterface,
503 TMProxyImpl_Disconnect
506 /* how much space do we use on stack in DWORD steps. */
511 return 8/sizeof(DWORD);
513 return sizeof(double)/sizeof(DWORD);
515 return sizeof(CY)/sizeof(DWORD);
517 return sizeof(DATE)/sizeof(DWORD);
519 return (sizeof(VARIANT)+3)/sizeof(DWORD);
526 _xsize(TYPEDESC *td) {
531 return sizeof(VARIANT)+3;
534 ARRAYDESC *adesc = td->u.lpadesc;
536 for (i=0;i<adesc->cDims;i++)
537 arrsize *= adesc->rgbounds[i].cElements;
538 return arrsize*_xsize(&adesc->tdescElem);
566 TRACE("(tdesc.vt %s)\n",debugstr_vt(tdesc->vt));
569 case VT_EMPTY: /* nothing. empty variant for instance */
575 if (debugout) TRACE_(olerelay)("%x%x\n",arg[0],arg[1]);
577 hres = xbuf_add(buf,(LPBYTE)arg,8);
587 if (debugout) TRACE_(olerelay)("%x\n",*arg);
589 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
594 if (debugout) TRACE_(olerelay)("%04x\n",*arg & 0xffff);
596 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
601 if (debugout) TRACE_(olerelay)("%02x\n",*arg & 0xff);
603 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
607 if (debugout) TRACE_(olerelay)("&0x%x\n",*arg);
609 hres = xbuf_add(buf,(LPBYTE)(DWORD*)*arg,sizeof(DWORD));
610 /* do not dealloc at this time */
614 VARIANT *vt = (VARIANT*)arg;
615 DWORD vttype = V_VT(vt);
617 if (debugout) TRACE_(olerelay)("Vt(%s%s)(",debugstr_vt(vttype),debugstr_vf(vttype));
620 hres = xbuf_add(buf,(LPBYTE)&vttype,sizeof(vttype));
621 if (hres) return hres;
623 /* need to recurse since we need to free the stuff */
624 hres = serialize_param(tinfo,writeit,debugout,dealloc,&tdesc2,(DWORD*)&(V_I4(vt)),buf);
625 if (debugout) TRACE_(olerelay)(")");
628 case VT_BSTR|VT_BYREF: {
629 if (debugout) TRACE_(olerelay)("[byref]'%s'", *(BSTR*)*arg ? relaystr(*((BSTR*)*arg)) : "<bstr NULL>");
631 /* ptr to ptr to magic widestring, basically */
632 BSTR *bstr = (BSTR *) *arg;
635 /* -1 means "null string" which is equivalent to empty string */
637 hres = xbuf_add(buf, (LPBYTE)&len,sizeof(DWORD));
638 if (hres) return hres;
640 len = *((DWORD*)*bstr-1)/sizeof(WCHAR);
641 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
642 if (hres) return hres;
643 hres = xbuf_add(buf,(LPBYTE)*bstr,len * sizeof(WCHAR));
644 if (hres) return hres;
648 if (dealloc && arg) {
649 BSTR *str = *((BSTR **)arg);
658 TRACE_(olerelay)("%s",relaystr((WCHAR*)*arg));
660 TRACE_(olerelay)("<bstr NULL>");
663 BSTR bstr = (BSTR)*arg;
667 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
668 if (hres) return hres;
670 len = *((DWORD*)bstr-1)/sizeof(WCHAR);
671 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
672 if (hres) return hres;
673 hres = xbuf_add(buf,(LPBYTE)bstr,len * sizeof(WCHAR));
674 if (hres) return hres;
679 SysFreeString((BSTR)*arg);
684 BOOL derefhere = TRUE;
686 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
690 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
692 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
695 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
696 switch (tattr->typekind) {
697 case TKIND_ENUM: /* confirmed */
698 case TKIND_RECORD: /* FIXME: mostly untested */
701 case TKIND_ALIAS: /* FIXME: untested */
702 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
703 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
707 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
711 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
712 ITypeInfo_Release(tinfo2);
715 if (debugout) TRACE_(olerelay)("*");
716 /* Write always, so the other side knows when it gets a NULL pointer.
718 cookie = *arg ? 0x42424242 : 0;
719 hres = xbuf_add(buf,(LPBYTE)&cookie,sizeof(cookie));
723 if (debugout) TRACE_(olerelay)("NULL");
726 hres = serialize_param(tinfo,writeit,debugout,dealloc,tdesc->u.lptdesc,(DWORD*)*arg,buf);
727 if (derefhere && dealloc) HeapFree(GetProcessHeap(),0,(LPVOID)*arg);
731 if (debugout) TRACE_(olerelay)("unk(0x%x)",*arg);
733 hres = _marshal_interface(buf,&IID_IUnknown,(LPUNKNOWN)*arg);
734 if (dealloc && *(IUnknown **)arg)
735 IUnknown_Release((LPUNKNOWN)*arg);
738 if (debugout) TRACE_(olerelay)("idisp(0x%x)",*arg);
740 hres = _marshal_interface(buf,&IID_IDispatch,(LPUNKNOWN)*arg);
741 if (dealloc && *(IUnknown **)arg)
742 IUnknown_Release((LPUNKNOWN)*arg);
745 if (debugout) TRACE_(olerelay)("<void>");
747 case VT_USERDEFINED: {
751 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
753 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
756 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
757 switch (tattr->typekind) {
759 case TKIND_INTERFACE:
761 hres=_marshal_interface(buf,&(tattr->guid),(LPUNKNOWN)arg);
763 IUnknown_Release((LPUNKNOWN)arg);
767 if (debugout) TRACE_(olerelay)("{");
768 for (i=0;i<tattr->cVars;i++) {
773 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
775 ERR("Could not get vardesc of %d\n",i);
778 elem2 = &vdesc->elemdescVar;
779 tdesc2 = &elem2->tdesc;
780 hres = serialize_param(
786 (DWORD*)(((LPBYTE)arg)+vdesc->u.oInst),
789 ITypeInfo_ReleaseVarDesc(tinfo2, vdesc);
792 if (debugout && (i<(tattr->cVars-1)))
793 TRACE_(olerelay)(",");
795 if (debugout) TRACE_(olerelay)("}");
799 hres = serialize_param(tinfo2,writeit,debugout,dealloc,&tattr->tdescAlias,arg,buf);
803 if (debugout) TRACE_(olerelay)("%x",*arg);
805 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
808 FIXME("Unhandled typekind %d\n",tattr->typekind);
812 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
813 ITypeInfo_Release(tinfo2);
817 ARRAYDESC *adesc = tdesc->u.lpadesc;
820 if (debugout) TRACE_(olerelay)("carr");
821 for (i=0;i<adesc->cDims;i++) {
822 if (debugout) TRACE_(olerelay)("[%d]",adesc->rgbounds[i].cElements);
823 arrsize *= adesc->rgbounds[i].cElements;
825 if (debugout) TRACE_(olerelay)("(vt %s)",debugstr_vt(adesc->tdescElem.vt));
826 if (debugout) TRACE_(olerelay)("[");
827 for (i=0;i<arrsize;i++) {
828 hres = serialize_param(tinfo, writeit, debugout, dealloc, &adesc->tdescElem, (DWORD*)((LPBYTE)arg+i*_xsize(&adesc->tdescElem)), buf);
831 if (debugout && (i<arrsize-1)) TRACE_(olerelay)(",");
833 if (debugout) TRACE_(olerelay)("]");
839 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
840 ULONG size = LPSAFEARRAY_UserSize(&flags, buf->curoff, (LPSAFEARRAY *)arg);
841 xbuf_resize(buf, size);
842 LPSAFEARRAY_UserMarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
848 ERR("Unhandled marshal type %d.\n",tdesc->vt);
865 TRACE("vt %s at %p\n",debugstr_vt(tdesc->vt),arg);
870 if (debugout) TRACE_(olerelay)("<empty>\n");
873 if (debugout) TRACE_(olerelay)("<null>\n");
876 VARIANT *vt = (VARIANT*)arg;
881 hres = xbuf_get(buf,(LPBYTE)&vttype,sizeof(vttype));
883 FIXME("vt type not read?\n");
886 memset(&tdesc2,0,sizeof(tdesc2));
889 if (debugout) TRACE_(olerelay)("Vt(%s%s)(",debugstr_vt(vttype),debugstr_vf(vttype));
890 hres = deserialize_param(tinfo, readit, debugout, alloc, &tdesc2, (DWORD*)&(V_I4(vt)), buf);
891 TRACE_(olerelay)(")");
902 hres = xbuf_get(buf,(LPBYTE)arg,8);
903 if (hres) ERR("Failed to read integer 8 byte\n");
905 if (debugout) TRACE_(olerelay)("%x%x",arg[0],arg[1]);
915 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
916 if (hres) ERR("Failed to read integer 4 byte\n");
918 if (debugout) TRACE_(olerelay)("%x",*arg);
924 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
925 if (hres) ERR("Failed to read integer 4 byte\n");
928 if (debugout) TRACE_(olerelay)("%04x",*arg & 0xffff);
934 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
935 if (hres) ERR("Failed to read integer 4 byte\n");
938 if (debugout) TRACE_(olerelay)("%02x",*arg & 0xff);
943 *arg = (DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
945 hres = xbuf_get(buf,(LPBYTE)*arg,sizeof(DWORD));
946 if (hres) ERR("Failed to read integer 4 byte\n");
948 if (debugout) TRACE_(olerelay)("&0x%x",*(DWORD*)*arg);
950 case VT_BSTR|VT_BYREF: {
951 BSTR **bstr = (BSTR **)arg;
956 hres = xbuf_get(buf,(LPBYTE)&len,sizeof(DWORD));
958 ERR("failed to read bstr klen\n");
962 *bstr = CoTaskMemAlloc(sizeof(BSTR *));
964 if (debugout) TRACE_(olerelay)("<bstr NULL>");
966 str = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(len+1)*sizeof(WCHAR));
967 hres = xbuf_get(buf,(LPBYTE)str,len*sizeof(WCHAR));
969 ERR("Failed to read BSTR.\n");
972 *bstr = CoTaskMemAlloc(sizeof(BSTR *));
973 **bstr = SysAllocStringLen(str,len);
974 if (debugout) TRACE_(olerelay)("%s",relaystr(str));
975 HeapFree(GetProcessHeap(),0,str);
987 hres = xbuf_get(buf,(LPBYTE)&len,sizeof(DWORD));
989 ERR("failed to read bstr klen\n");
994 if (debugout) TRACE_(olerelay)("<bstr NULL>");
996 str = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(len+1)*sizeof(WCHAR));
997 hres = xbuf_get(buf,(LPBYTE)str,len*sizeof(WCHAR));
999 ERR("Failed to read BSTR.\n");
1002 *arg = (DWORD)SysAllocStringLen(str,len);
1003 if (debugout) TRACE_(olerelay)("%s",relaystr(str));
1004 HeapFree(GetProcessHeap(),0,str);
1013 BOOL derefhere = TRUE;
1015 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
1019 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
1021 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
1024 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1025 switch (tattr->typekind) {
1026 case TKIND_ENUM: /* confirmed */
1027 case TKIND_RECORD: /* FIXME: mostly untested */
1030 case TKIND_ALIAS: /* FIXME: untested */
1031 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
1032 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
1036 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
1040 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1041 ITypeInfo_Release(tinfo2);
1043 /* read it in all cases, we need to know if we have
1044 * NULL pointer or not.
1046 hres = xbuf_get(buf,(LPBYTE)&cookie,sizeof(cookie));
1048 ERR("Failed to load pointer cookie.\n");
1051 if (cookie != 0x42424242) {
1052 /* we read a NULL ptr from the remote side */
1053 if (debugout) TRACE_(olerelay)("NULL");
1057 if (debugout) TRACE_(olerelay)("*");
1059 /* Allocate space for the referenced struct */
1061 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,_xsize(tdesc->u.lptdesc));
1064 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, (LPDWORD)*arg, buf);
1066 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, arg, buf);
1069 /* FIXME: UNKNOWN is unknown ..., but allocate 4 byte for it */
1071 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
1074 hres = _unmarshal_interface(buf,&IID_IUnknown,(LPUNKNOWN*)arg);
1076 TRACE_(olerelay)("unk(%p)",arg);
1081 hres = _unmarshal_interface(buf,&IID_IDispatch,(LPUNKNOWN*)arg);
1083 TRACE_(olerelay)("idisp(%p)",arg);
1086 if (debugout) TRACE_(olerelay)("<void>");
1088 case VT_USERDEFINED: {
1092 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
1094 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
1097 hres = ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1099 ERR("Could not get typeattr in VT_USERDEFINED.\n");
1101 switch (tattr->typekind) {
1102 case TKIND_DISPATCH:
1103 case TKIND_INTERFACE:
1105 hres = _unmarshal_interface(buf,&(tattr->guid),(LPUNKNOWN*)arg);
1107 case TKIND_RECORD: {
1111 *arg = (DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,tattr->cbSizeInstance);
1113 if (debugout) TRACE_(olerelay)("{");
1114 for (i=0;i<tattr->cVars;i++) {
1117 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
1119 ERR("Could not get vardesc of %d\n",i);
1120 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1121 ITypeInfo_Release(tinfo2);
1124 hres = deserialize_param(
1129 &vdesc->elemdescVar.tdesc,
1130 (DWORD*)(((LPBYTE)*arg)+vdesc->u.oInst),
1133 ITypeInfo2_ReleaseVarDesc(tinfo2, vdesc);
1134 if (debugout && (i<tattr->cVars-1)) TRACE_(olerelay)(",");
1136 if (debugout) TRACE_(olerelay)("}");
1140 hres = deserialize_param(tinfo2,readit,debugout,alloc,&tattr->tdescAlias,arg,buf);
1144 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
1145 if (hres) ERR("Failed to read enum (4 byte)\n");
1147 if (debugout) TRACE_(olerelay)("%x",*arg);
1150 ERR("Unhandled typekind %d\n",tattr->typekind);
1154 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1157 ERR("failed to stuballoc in TKIND_RECORD.\n");
1158 ITypeInfo_Release(tinfo2);
1162 /* arg is pointing to the start of the array. */
1163 ARRAYDESC *adesc = tdesc->u.lpadesc;
1166 if (adesc->cDims > 1) FIXME("cDims > 1 in VT_CARRAY. Does it work?\n");
1167 for (i=0;i<adesc->cDims;i++)
1168 arrsize *= adesc->rgbounds[i].cElements;
1169 for (i=0;i<arrsize;i++)
1176 (DWORD*)((LPBYTE)(arg)+i*_xsize(&adesc->tdescElem)),
1181 case VT_SAFEARRAY: {
1184 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
1185 unsigned char *buffer;
1186 buffer = LPSAFEARRAY_UserUnmarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
1187 buf->curoff = buffer - buf->base;
1192 ERR("No handler for VT type %d!\n",tdesc->vt);
1198 /* Retrieves a function's funcdesc, searching back into inherited interfaces. */
1199 static HRESULT get_funcdesc(ITypeInfo *tinfo, int iMethod, ITypeInfo **tactual, const FUNCDESC **fdesc,
1200 BSTR *iname, BSTR *fname, UINT *num)
1204 UINT inherited_funcs = 0;
1207 if (fname) *fname = NULL;
1208 if (iname) *iname = NULL;
1212 hr = ITypeInfo_GetTypeAttr(tinfo, &attr);
1215 ERR("GetTypeAttr failed with %x\n",hr);
1218 impl_types = attr->cImplTypes;
1219 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1221 for (i = 0; i < impl_types; i++)
1224 ITypeInfo *pSubTypeInfo;
1227 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, i, &href);
1228 if (FAILED(hr)) return hr;
1229 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &pSubTypeInfo);
1230 if (FAILED(hr)) return hr;
1232 hr = get_funcdesc(pSubTypeInfo, iMethod, tactual, fdesc, iname, fname, &sub_funcs);
1233 inherited_funcs += sub_funcs;
1234 ITypeInfo_Release(pSubTypeInfo);
1235 if(SUCCEEDED(hr)) return hr;
1237 if(iMethod < inherited_funcs)
1239 ERR("shouldn't be here\n");
1240 return E_INVALIDARG;
1243 for(i = inherited_funcs; i <= iMethod; i++)
1245 hr = ITypeInfoImpl_GetInternalFuncDesc(tinfo, i - inherited_funcs, fdesc);
1253 /* found it. We don't care about num so zero it */
1256 ITypeInfo_AddRef(*tactual);
1257 if (fname) ITypeInfo_GetDocumentation(tinfo,(*fdesc)->memid,fname,NULL,NULL,NULL);
1258 if (iname) ITypeInfo_GetDocumentation(tinfo,-1,iname,NULL,NULL,NULL);
1262 static inline BOOL is_in_elem(const ELEMDESC *elem)
1264 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN || !elem->u.paramdesc.wParamFlags);
1267 static inline BOOL is_out_elem(const ELEMDESC *elem)
1269 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT || !elem->u.paramdesc.wParamFlags);
1273 xCall(LPVOID retptr, int method, TMProxyImpl *tpinfo /*, args */)
1275 DWORD *args = ((DWORD*)&tpinfo)+1, *xargs;
1276 const FUNCDESC *fdesc;
1278 int i, relaydeb = TRACE_ON(olerelay);
1285 DWORD remoteresult = 0;
1287 IRpcChannelBuffer *chanbuf;
1289 EnterCriticalSection(&tpinfo->crit);
1291 hres = get_funcdesc(tpinfo->tinfo,method,&tinfo,&fdesc,&iname,&fname,NULL);
1293 ERR("Did not find typeinfo/funcdesc entry for method %d!\n",method);
1294 LeaveCriticalSection(&tpinfo->crit);
1298 if (!tpinfo->chanbuf)
1300 WARN("Tried to use disconnected proxy\n");
1301 ITypeInfo_Release(tinfo);
1302 LeaveCriticalSection(&tpinfo->crit);
1303 return RPC_E_DISCONNECTED;
1305 chanbuf = tpinfo->chanbuf;
1306 IRpcChannelBuffer_AddRef(chanbuf);
1308 LeaveCriticalSection(&tpinfo->crit);
1311 TRACE_(olerelay)("->");
1313 TRACE_(olerelay)("%s:",relaystr(iname));
1315 TRACE_(olerelay)("%s(%d)",relaystr(fname),method);
1317 TRACE_(olerelay)("%d",method);
1318 TRACE_(olerelay)("(");
1321 if (iname) SysFreeString(iname);
1322 if (fname) SysFreeString(fname);
1324 memset(&buf,0,sizeof(buf));
1326 /* normal typelib driven serializing */
1328 /* Need them for hack below */
1329 memset(names,0,sizeof(names));
1330 if (ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames))
1332 if (nrofnames > sizeof(names)/sizeof(names[0]))
1333 ERR("Need more names!\n");
1336 for (i=0;i<fdesc->cParams;i++) {
1337 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1339 if (i) TRACE_(olerelay)(",");
1340 if (i+1<nrofnames && names[i+1])
1341 TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1343 /* No need to marshal other data than FIN and any VT_PTR. */
1344 if (!is_in_elem(elem) && (elem->tdesc.vt != VT_PTR)) {
1345 xargs+=_argsize(elem->tdesc.vt);
1346 if (relaydeb) TRACE_(olerelay)("[out]");
1349 hres = serialize_param(
1360 ERR("Failed to serialize param, hres %x\n",hres);
1363 xargs+=_argsize(elem->tdesc.vt);
1365 if (relaydeb) TRACE_(olerelay)(")");
1367 memset(&msg,0,sizeof(msg));
1368 msg.cbBuffer = buf.curoff;
1369 msg.iMethod = method;
1370 hres = IRpcChannelBuffer_GetBuffer(chanbuf,&msg,&(tpinfo->iid));
1372 ERR("RpcChannelBuffer GetBuffer failed, %x\n",hres);
1375 memcpy(msg.Buffer,buf.base,buf.curoff);
1376 if (relaydeb) TRACE_(olerelay)("\n");
1377 hres = IRpcChannelBuffer_SendReceive(chanbuf,&msg,&status);
1379 ERR("RpcChannelBuffer SendReceive failed, %x\n",hres);
1383 if (relaydeb) TRACE_(olerelay)(" status = %08x (",status);
1385 buf.base = HeapReAlloc(GetProcessHeap(),0,buf.base,msg.cbBuffer);
1387 buf.base = HeapAlloc(GetProcessHeap(),0,msg.cbBuffer);
1388 buf.size = msg.cbBuffer;
1389 memcpy(buf.base,msg.Buffer,buf.size);
1392 /* generic deserializer using typelib description */
1395 for (i=0;i<fdesc->cParams;i++) {
1396 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1399 if (i) TRACE_(olerelay)(",");
1400 if (i+1<nrofnames && names[i+1]) TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1402 /* No need to marshal other data than FOUT and any VT_PTR */
1403 if (!is_out_elem(elem) && (elem->tdesc.vt != VT_PTR)) {
1404 xargs += _argsize(elem->tdesc.vt);
1405 if (relaydeb) TRACE_(olerelay)("[in]");
1408 hres = deserialize_param(
1418 ERR("Failed to unmarshall param, hres %x\n",hres);
1422 xargs += _argsize(elem->tdesc.vt);
1425 hres = xbuf_get(&buf, (LPBYTE)&remoteresult, sizeof(DWORD));
1428 if (relaydeb) TRACE_(olerelay)(") = %08x\n", remoteresult);
1430 hres = remoteresult;
1433 for (i = 0; i < nrofnames; i++)
1434 SysFreeString(names[i]);
1435 HeapFree(GetProcessHeap(),0,buf.base);
1436 IRpcChannelBuffer_Release(chanbuf);
1437 ITypeInfo_Release(tinfo);
1438 TRACE("-- 0x%08x\n", hres);
1442 static HRESULT WINAPI ProxyIUnknown_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
1444 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1446 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1448 if (proxy->outerunknown)
1449 return IUnknown_QueryInterface(proxy->outerunknown, riid, ppv);
1451 FIXME("No interface\n");
1452 return E_NOINTERFACE;
1455 static ULONG WINAPI ProxyIUnknown_AddRef(IUnknown *iface)
1457 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1461 if (proxy->outerunknown)
1462 return IUnknown_AddRef(proxy->outerunknown);
1464 return 2; /* FIXME */
1467 static ULONG WINAPI ProxyIUnknown_Release(IUnknown *iface)
1469 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1473 if (proxy->outerunknown)
1474 return IUnknown_Release(proxy->outerunknown);
1476 return 1; /* FIXME */
1479 static HRESULT WINAPI ProxyIDispatch_GetTypeInfoCount(LPDISPATCH iface, UINT * pctinfo)
1481 TMProxyImpl *This = (TMProxyImpl *)iface;
1483 TRACE("(%p)\n", pctinfo);
1485 return IDispatch_GetTypeInfoCount(This->dispatch, pctinfo);
1488 static HRESULT WINAPI ProxyIDispatch_GetTypeInfo(LPDISPATCH iface, UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo)
1490 TMProxyImpl *This = (TMProxyImpl *)iface;
1492 TRACE("(%d, %x, %p)\n", iTInfo, lcid, ppTInfo);
1494 return IDispatch_GetTypeInfo(This->dispatch, iTInfo, lcid, ppTInfo);
1497 static HRESULT WINAPI ProxyIDispatch_GetIDsOfNames(LPDISPATCH iface, REFIID riid, LPOLESTR * rgszNames, UINT cNames, LCID lcid, DISPID * rgDispId)
1499 TMProxyImpl *This = (TMProxyImpl *)iface;
1501 TRACE("(%s, %p, %d, 0x%x, %p)\n", debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);
1503 return IDispatch_GetIDsOfNames(This->dispatch, riid, rgszNames,
1504 cNames, lcid, rgDispId);
1507 static HRESULT WINAPI ProxyIDispatch_Invoke(LPDISPATCH iface, DISPID dispIdMember, REFIID riid, LCID lcid,
1508 WORD wFlags, DISPPARAMS * pDispParams, VARIANT * pVarResult,
1509 EXCEPINFO * pExcepInfo, UINT * puArgErr)
1511 TMProxyImpl *This = (TMProxyImpl *)iface;
1513 TRACE("(%d, %s, 0x%x, 0x%x, %p, %p, %p, %p)\n", dispIdMember,
1514 debugstr_guid(riid), lcid, wFlags, pDispParams, pVarResult,
1515 pExcepInfo, puArgErr);
1517 return IDispatch_Invoke(This->dispatch, dispIdMember, riid, lcid,
1518 wFlags, pDispParams, pVarResult, pExcepInfo,
1524 const IRpcChannelBufferVtbl *lpVtbl;
1526 /* the IDispatch-derived interface we are handling */
1528 IRpcChannelBuffer *pDelegateChannel;
1529 } TMarshalDispatchChannel;
1531 static HRESULT WINAPI TMarshalDispatchChannel_QueryInterface(LPRPCCHANNELBUFFER iface, REFIID riid, LPVOID *ppv)
1534 if (IsEqualIID(riid,&IID_IRpcChannelBuffer) || IsEqualIID(riid,&IID_IUnknown))
1536 *ppv = (LPVOID)iface;
1537 IUnknown_AddRef(iface);
1540 return E_NOINTERFACE;
1543 static ULONG WINAPI TMarshalDispatchChannel_AddRef(LPRPCCHANNELBUFFER iface)
1545 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1546 return InterlockedIncrement(&This->refs);
1549 static ULONG WINAPI TMarshalDispatchChannel_Release(LPRPCCHANNELBUFFER iface)
1551 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1554 ref = InterlockedDecrement(&This->refs);
1558 IRpcChannelBuffer_Release(This->pDelegateChannel);
1559 HeapFree(GetProcessHeap(), 0, This);
1563 static HRESULT WINAPI TMarshalDispatchChannel_GetBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg, REFIID riid)
1565 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1566 TRACE("(%p, %s)\n", olemsg, debugstr_guid(riid));
1567 /* Note: we are pretending to invoke a method on the interface identified
1568 * by tmarshal_iid so that we can re-use the IDispatch proxy/stub code
1569 * without the RPC runtime getting confused by not exporting an IDispatch interface */
1570 return IRpcChannelBuffer_GetBuffer(This->pDelegateChannel, olemsg, &This->tmarshal_iid);
1573 static HRESULT WINAPI TMarshalDispatchChannel_SendReceive(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE *olemsg, ULONG *pstatus)
1575 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1576 TRACE("(%p, %p)\n", olemsg, pstatus);
1577 return IRpcChannelBuffer_SendReceive(This->pDelegateChannel, olemsg, pstatus);
1580 static HRESULT WINAPI TMarshalDispatchChannel_FreeBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg)
1582 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1583 TRACE("(%p)\n", olemsg);
1584 return IRpcChannelBuffer_FreeBuffer(This->pDelegateChannel, olemsg);
1587 static HRESULT WINAPI TMarshalDispatchChannel_GetDestCtx(LPRPCCHANNELBUFFER iface, DWORD* pdwDestContext, void** ppvDestContext)
1589 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1590 TRACE("(%p,%p)\n", pdwDestContext, ppvDestContext);
1591 return IRpcChannelBuffer_GetDestCtx(This->pDelegateChannel, pdwDestContext, ppvDestContext);
1594 static HRESULT WINAPI TMarshalDispatchChannel_IsConnected(LPRPCCHANNELBUFFER iface)
1596 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1598 return IRpcChannelBuffer_IsConnected(This->pDelegateChannel);
1601 static const IRpcChannelBufferVtbl TMarshalDispatchChannelVtbl =
1603 TMarshalDispatchChannel_QueryInterface,
1604 TMarshalDispatchChannel_AddRef,
1605 TMarshalDispatchChannel_Release,
1606 TMarshalDispatchChannel_GetBuffer,
1607 TMarshalDispatchChannel_SendReceive,
1608 TMarshalDispatchChannel_FreeBuffer,
1609 TMarshalDispatchChannel_GetDestCtx,
1610 TMarshalDispatchChannel_IsConnected
1613 static HRESULT TMarshalDispatchChannel_Create(
1614 IRpcChannelBuffer *pDelegateChannel, REFIID tmarshal_riid,
1615 IRpcChannelBuffer **ppChannel)
1617 TMarshalDispatchChannel *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1619 return E_OUTOFMEMORY;
1621 This->lpVtbl = &TMarshalDispatchChannelVtbl;
1623 IRpcChannelBuffer_AddRef(pDelegateChannel);
1624 This->pDelegateChannel = pDelegateChannel;
1625 This->tmarshal_iid = *tmarshal_riid;
1627 *ppChannel = (IRpcChannelBuffer *)&This->lpVtbl;
1632 static inline HRESULT get_facbuf_for_iid(REFIID riid, IPSFactoryBuffer **facbuf)
1637 if ((hr = CoGetPSClsid(riid, &clsid)))
1639 return CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL,
1640 &IID_IPSFactoryBuffer, (LPVOID*)facbuf);
1643 static HRESULT WINAPI
1644 PSFacBuf_CreateProxy(
1645 LPPSFACTORYBUFFER iface, IUnknown* pUnkOuter, REFIID riid,
1646 IRpcProxyBuffer **ppProxy, LPVOID *ppv)
1651 const FUNCDESC *fdesc;
1655 TRACE("(...%s...)\n",debugstr_guid(riid));
1656 hres = _get_typeinfo_for_iid(riid,&tinfo);
1658 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
1661 nroffuncs = _nroffuncs(tinfo);
1662 proxy = CoTaskMemAlloc(sizeof(TMProxyImpl));
1663 if (!proxy) return E_OUTOFMEMORY;
1665 assert(sizeof(TMAsmProxy) == 12);
1667 proxy->dispatch = NULL;
1668 proxy->dispatch_proxy = NULL;
1669 proxy->outerunknown = pUnkOuter;
1670 proxy->asmstubs = VirtualAlloc(NULL, sizeof(TMAsmProxy) * nroffuncs, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
1671 if (!proxy->asmstubs) {
1672 ERR("Could not commit pages for proxy thunks\n");
1673 CoTaskMemFree(proxy);
1674 return E_OUTOFMEMORY;
1676 proxy->lpvtbl2 = &tmproxyvtable;
1677 /* one reference for the proxy */
1679 proxy->tinfo = tinfo;
1680 memcpy(&proxy->iid,riid,sizeof(*riid));
1683 InitializeCriticalSection(&proxy->crit);
1684 proxy->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TMProxyImpl.crit");
1686 proxy->lpvtbl = HeapAlloc(GetProcessHeap(),0,sizeof(LPBYTE)*nroffuncs);
1687 for (i=0;i<nroffuncs;i++) {
1688 TMAsmProxy *xasm = proxy->asmstubs+i;
1692 proxy->lpvtbl[i] = ProxyIUnknown_QueryInterface;
1695 proxy->lpvtbl[i] = ProxyIUnknown_AddRef;
1698 proxy->lpvtbl[i] = ProxyIUnknown_Release;
1702 /* nrofargs without This */
1705 hres = get_funcdesc(tinfo,i,&tinfo2,&fdesc,NULL,NULL,NULL);
1707 ERR("GetFuncDesc %x should not fail here.\n",hres);
1710 ITypeInfo_Release(tinfo2);
1711 /* some args take more than 4 byte on the stack */
1713 for (j=0;j<fdesc->cParams;j++)
1714 nrofargs += _argsize(fdesc->lprgelemdescParam[j].tdesc.vt);
1717 if (fdesc->callconv != CC_STDCALL) {
1718 ERR("calling convention is not stdcall????\n");
1721 /* popl %eax - return ptr
1728 * arg3 arg2 arg1 <method> <returnptr>
1730 xasm->popleax = 0x58;
1731 xasm->pushlval = 0x6a;
1733 xasm->pushleax = 0x50;
1734 xasm->lcall = 0xe8; /* relative jump */
1735 xasm->xcall = (DWORD)xCall;
1736 xasm->xcall -= (DWORD)&(xasm->lret);
1738 xasm->bytestopop= (nrofargs+2)*4; /* pop args, This, iMethod */
1739 proxy->lpvtbl[i] = xasm;
1742 FIXME("not implemented on non i386\n");
1749 /* if we derive from IDispatch then defer to its proxy for its methods */
1750 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
1753 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
1755 IPSFactoryBuffer *factory_buffer;
1756 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
1759 hres = IPSFactoryBuffer_CreateProxy(factory_buffer, NULL,
1760 &IID_IDispatch, &proxy->dispatch_proxy,
1761 (void **)&proxy->dispatch);
1762 IPSFactoryBuffer_Release(factory_buffer);
1764 if ((hres == S_OK) && (nroffuncs < 7))
1766 ERR("nroffuncs calculated incorrectly (%d)\n", nroffuncs);
1767 hres = E_UNEXPECTED;
1771 proxy->lpvtbl[3] = ProxyIDispatch_GetTypeInfoCount;
1772 proxy->lpvtbl[4] = ProxyIDispatch_GetTypeInfo;
1773 proxy->lpvtbl[5] = ProxyIDispatch_GetIDsOfNames;
1774 proxy->lpvtbl[6] = ProxyIDispatch_Invoke;
1777 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
1782 *ppv = (LPVOID)proxy;
1783 *ppProxy = (IRpcProxyBuffer *)&(proxy->lpvtbl2);
1784 IUnknown_AddRef((IUnknown *)*ppv);
1788 TMProxyImpl_Release((IRpcProxyBuffer *)&proxy->lpvtbl2);
1792 typedef struct _TMStubImpl {
1793 const IRpcStubBufferVtbl *lpvtbl;
1799 IRpcStubBuffer *dispatch_stub;
1800 BOOL dispatch_derivative;
1803 static HRESULT WINAPI
1804 TMStubImpl_QueryInterface(LPRPCSTUBBUFFER iface, REFIID riid, LPVOID *ppv)
1806 if (IsEqualIID(riid,&IID_IRpcStubBuffer)||IsEqualIID(riid,&IID_IUnknown)){
1807 *ppv = (LPVOID)iface;
1808 IRpcStubBuffer_AddRef(iface);
1811 FIXME("%s, not supported IID.\n",debugstr_guid(riid));
1812 return E_NOINTERFACE;
1816 TMStubImpl_AddRef(LPRPCSTUBBUFFER iface)
1818 TMStubImpl *This = (TMStubImpl *)iface;
1819 ULONG refCount = InterlockedIncrement(&This->ref);
1821 TRACE("(%p)->(ref before=%u)\n", This, refCount - 1);
1827 TMStubImpl_Release(LPRPCSTUBBUFFER iface)
1829 TMStubImpl *This = (TMStubImpl *)iface;
1830 ULONG refCount = InterlockedDecrement(&This->ref);
1832 TRACE("(%p)->(ref before=%u)\n", This, refCount + 1);
1836 IRpcStubBuffer_Disconnect(iface);
1837 ITypeInfo_Release(This->tinfo);
1838 if (This->dispatch_stub)
1839 IRpcStubBuffer_Release(This->dispatch_stub);
1840 CoTaskMemFree(This);
1845 static HRESULT WINAPI
1846 TMStubImpl_Connect(LPRPCSTUBBUFFER iface, LPUNKNOWN pUnkServer)
1848 TMStubImpl *This = (TMStubImpl *)iface;
1850 TRACE("(%p)->(%p)\n", This, pUnkServer);
1852 IUnknown_AddRef(pUnkServer);
1853 This->pUnk = pUnkServer;
1855 if (This->dispatch_stub)
1856 IRpcStubBuffer_Connect(This->dispatch_stub, pUnkServer);
1862 TMStubImpl_Disconnect(LPRPCSTUBBUFFER iface)
1864 TMStubImpl *This = (TMStubImpl *)iface;
1866 TRACE("(%p)->()\n", This);
1870 IUnknown_Release(This->pUnk);
1874 if (This->dispatch_stub)
1875 IRpcStubBuffer_Disconnect(This->dispatch_stub);
1878 static HRESULT WINAPI
1880 LPRPCSTUBBUFFER iface, RPCOLEMESSAGE* xmsg,IRpcChannelBuffer*rpcchanbuf)
1883 const FUNCDESC *fdesc;
1884 TMStubImpl *This = (TMStubImpl *)iface;
1886 DWORD *args = NULL, res, *xargs, nrofargs;
1891 ITypeInfo *tinfo = NULL;
1895 if (xmsg->iMethod < 3) {
1896 ERR("IUnknown methods cannot be marshaled by the typelib marshaler\n");
1897 return E_UNEXPECTED;
1900 if (This->dispatch_derivative && xmsg->iMethod < sizeof(IDispatchVtbl)/sizeof(void *))
1902 IPSFactoryBuffer *factory_buffer;
1903 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
1906 hres = IPSFactoryBuffer_CreateStub(factory_buffer, &IID_IDispatch,
1907 This->pUnk, &This->dispatch_stub);
1908 IPSFactoryBuffer_Release(factory_buffer);
1912 return IRpcStubBuffer_Invoke(This->dispatch_stub, xmsg, rpcchanbuf);
1915 memset(&buf,0,sizeof(buf));
1916 buf.size = xmsg->cbBuffer;
1917 buf.base = HeapAlloc(GetProcessHeap(), 0, xmsg->cbBuffer);
1918 memcpy(buf.base, xmsg->Buffer, xmsg->cbBuffer);
1921 hres = get_funcdesc(This->tinfo,xmsg->iMethod,&tinfo,&fdesc,&iname,NULL,NULL);
1923 ERR("GetFuncDesc on method %d failed with %x\n",xmsg->iMethod,hres);
1927 if (iname && !lstrcmpW(iname, IDispatchW))
1929 ERR("IDispatch cannot be marshaled by the typelib marshaler\n");
1930 hres = E_UNEXPECTED;
1931 SysFreeString (iname);
1935 if (iname) SysFreeString (iname);
1937 /* Need them for hack below */
1938 memset(names,0,sizeof(names));
1939 ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames);
1940 if (nrofnames > sizeof(names)/sizeof(names[0])) {
1941 ERR("Need more names!\n");
1944 /*dump_FUNCDESC(fdesc);*/
1946 for (i=0;i<fdesc->cParams;i++)
1947 nrofargs += _argsize(fdesc->lprgelemdescParam[i].tdesc.vt);
1948 args = HeapAlloc(GetProcessHeap(),0,(nrofargs+1)*sizeof(DWORD));
1951 hres = E_OUTOFMEMORY;
1955 /* Allocate all stuff used by call. */
1957 for (i=0;i<fdesc->cParams;i++) {
1958 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1960 hres = deserialize_param(
1969 xargs += _argsize(elem->tdesc.vt);
1971 ERR("Failed to deserialize param %s, hres %x\n",relaystr(names[i+1]),hres);
1976 args[0] = (DWORD)This->pUnk;
1981 (*((FARPROC**)args[0]))[fdesc->oVft/4],
1989 DWORD dwExceptionCode = GetExceptionCode();
1990 ERR("invoke call failed with exception 0x%08x (%d)\n", dwExceptionCode, dwExceptionCode);
1991 if (FAILED(dwExceptionCode))
1992 hres = dwExceptionCode;
1994 hres = HRESULT_FROM_WIN32(dwExceptionCode);
2004 for (i=0;i<fdesc->cParams;i++) {
2005 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2006 hres = serialize_param(
2015 xargs += _argsize(elem->tdesc.vt);
2017 ERR("Failed to stuballoc param, hres %x\n",hres);
2022 hres = xbuf_add (&buf, (LPBYTE)&res, sizeof(DWORD));
2027 xmsg->cbBuffer = buf.curoff;
2028 hres = IRpcChannelBuffer_GetBuffer(rpcchanbuf, xmsg, &This->iid);
2030 ERR("IRpcChannelBuffer_GetBuffer failed with error 0x%08x\n", hres);
2033 memcpy(xmsg->Buffer, buf.base, buf.curoff);
2036 for (i = 0; i < nrofnames; i++)
2037 SysFreeString(names[i]);
2039 ITypeInfo_Release(tinfo);
2040 HeapFree(GetProcessHeap(), 0, args);
2042 HeapFree(GetProcessHeap(), 0, buf.base);
2044 TRACE("returning\n");
2048 static LPRPCSTUBBUFFER WINAPI
2049 TMStubImpl_IsIIDSupported(LPRPCSTUBBUFFER iface, REFIID riid) {
2050 FIXME("Huh (%s)?\n",debugstr_guid(riid));
2055 TMStubImpl_CountRefs(LPRPCSTUBBUFFER iface) {
2056 TMStubImpl *This = (TMStubImpl *)iface;
2059 return This->ref; /*FIXME? */
2062 static HRESULT WINAPI
2063 TMStubImpl_DebugServerQueryInterface(LPRPCSTUBBUFFER iface, LPVOID *ppv) {
2068 TMStubImpl_DebugServerRelease(LPRPCSTUBBUFFER iface, LPVOID ppv) {
2072 static const IRpcStubBufferVtbl tmstubvtbl = {
2073 TMStubImpl_QueryInterface,
2077 TMStubImpl_Disconnect,
2079 TMStubImpl_IsIIDSupported,
2080 TMStubImpl_CountRefs,
2081 TMStubImpl_DebugServerQueryInterface,
2082 TMStubImpl_DebugServerRelease
2085 static HRESULT WINAPI
2086 PSFacBuf_CreateStub(
2087 LPPSFACTORYBUFFER iface, REFIID riid,IUnknown *pUnkServer,
2088 IRpcStubBuffer** ppStub
2095 TRACE("(%s,%p,%p)\n",debugstr_guid(riid),pUnkServer,ppStub);
2097 hres = _get_typeinfo_for_iid(riid,&tinfo);
2099 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
2103 stub = CoTaskMemAlloc(sizeof(TMStubImpl));
2105 return E_OUTOFMEMORY;
2106 stub->lpvtbl = &tmstubvtbl;
2108 stub->tinfo = tinfo;
2109 stub->dispatch_stub = NULL;
2110 stub->dispatch_derivative = FALSE;
2111 memcpy(&(stub->iid),riid,sizeof(*riid));
2112 hres = IRpcStubBuffer_Connect((LPRPCSTUBBUFFER)stub,pUnkServer);
2113 *ppStub = (LPRPCSTUBBUFFER)stub;
2114 TRACE("IRpcStubBuffer: %p\n", stub);
2116 ERR("Connect to pUnkServer failed?\n");
2118 /* if we derive from IDispatch then defer to its stub for some of its methods */
2119 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
2122 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
2123 stub->dispatch_derivative = TRUE;
2124 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
2130 static const IPSFactoryBufferVtbl psfacbufvtbl = {
2131 PSFacBuf_QueryInterface,
2134 PSFacBuf_CreateProxy,
2138 /* This is the whole PSFactoryBuffer object, just the vtableptr */
2139 static const IPSFactoryBufferVtbl *lppsfac = &psfacbufvtbl;
2141 /***********************************************************************
2142 * TMARSHAL_DllGetClassObject
2144 HRESULT TMARSHAL_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
2146 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)) {
2150 return E_NOINTERFACE;