4 * Copyright 2002,2005 Marcus Meissner
6 * The olerelay debug channel allows you to see calls marshalled by
7 * the typelib marshaller. It is not a generic COM relaying system.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
34 #define NONAMELESSUNION
35 #define NONAMELESSSTRUCT
45 #include "propidl.h" /* for LPSAFEARRAY_User* functions */
48 #include "wine/debug.h"
49 #include "wine/exception.h"
51 static const WCHAR IDispatchW[] = { 'I','D','i','s','p','a','t','c','h',0};
53 WINE_DEFAULT_DEBUG_CHANNEL(ole);
54 WINE_DECLARE_DEBUG_CHANNEL(olerelay);
56 #define ICOM_THIS_MULTI(impl,field,iface) impl* const This=(impl*)((char*)(iface) - offsetof(impl,field))
58 static HRESULT TMarshalDispatchChannel_Create(
59 IRpcChannelBuffer *pDelegateChannel, REFIID tmarshal_riid,
60 IRpcChannelBuffer **ppChannel);
62 typedef struct _marshal_state {
68 /* used in the olerelay code to avoid having the L"" stuff added by debugstr_w */
69 static char *relaystr(WCHAR *in) {
70 char *tmp = (char *)debugstr_w(in);
72 tmp[strlen(tmp)-1] = '\0';
77 xbuf_resize(marshal_state *buf, DWORD newsize)
79 if(buf->size >= newsize)
84 buf->base = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buf->base, newsize);
90 buf->base = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, newsize);
99 xbuf_add(marshal_state *buf, const BYTE *stuff, DWORD size)
103 if(buf->size - buf->curoff < size)
105 hr = xbuf_resize(buf, buf->size + size + 100);
106 if(FAILED(hr)) return hr;
108 memcpy(buf->base+buf->curoff,stuff,size);
114 xbuf_get(marshal_state *buf, LPBYTE stuff, DWORD size) {
115 if (buf->size < buf->curoff+size) return E_FAIL;
116 memcpy(stuff,buf->base+buf->curoff,size);
122 xbuf_skip(marshal_state *buf, DWORD size) {
123 if (buf->size < buf->curoff+size) return E_FAIL;
129 _unmarshal_interface(marshal_state *buf, REFIID riid, LPUNKNOWN *pUnk) {
131 ULARGE_INTEGER newpos;
132 LARGE_INTEGER seekto;
137 TRACE("...%s...\n",debugstr_guid(riid));
140 hres = xbuf_get(buf,(LPBYTE)&xsize,sizeof(xsize));
142 ERR("xbuf_get failed\n");
146 if (xsize == 0) return S_OK;
148 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
150 ERR("Stream create failed %x\n",hres);
154 hres = IStream_Write(pStm,buf->base+buf->curoff,xsize,&res);
156 ERR("stream write %x\n",hres);
160 memset(&seekto,0,sizeof(seekto));
161 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
163 ERR("Failed Seek %x\n",hres);
167 hres = CoUnmarshalInterface(pStm,riid,(LPVOID*)pUnk);
169 ERR("Unmarshalling interface %s failed with %x\n",debugstr_guid(riid),hres);
173 IStream_Release(pStm);
174 return xbuf_skip(buf,xsize);
178 _marshal_interface(marshal_state *buf, REFIID riid, LPUNKNOWN pUnk) {
179 LPBYTE tempbuf = NULL;
180 IStream *pStm = NULL;
182 ULARGE_INTEGER newpos;
183 LARGE_INTEGER seekto;
189 /* this is valid, if for instance we serialize
190 * a VT_DISPATCH with NULL ptr which apparently
191 * can happen. S_OK to make sure we continue
194 WARN("pUnk is NULL\n");
196 return xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
201 TRACE("...%s...\n",debugstr_guid(riid));
203 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
205 ERR("Stream create failed %x\n",hres);
209 hres = CoMarshalInterface(pStm,riid,pUnk,0,NULL,0);
211 ERR("Marshalling interface %s failed with %x\n", debugstr_guid(riid), hres);
215 hres = IStream_Stat(pStm,&ststg,0);
217 ERR("Stream stat failed\n");
221 tempbuf = HeapAlloc(GetProcessHeap(), 0, ststg.cbSize.u.LowPart);
222 memset(&seekto,0,sizeof(seekto));
223 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
225 ERR("Failed Seek %x\n",hres);
229 hres = IStream_Read(pStm,tempbuf,ststg.cbSize.u.LowPart,&res);
231 ERR("Failed Read %x\n",hres);
235 xsize = ststg.cbSize.u.LowPart;
236 xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
237 hres = xbuf_add(buf,tempbuf,ststg.cbSize.u.LowPart);
239 HeapFree(GetProcessHeap(),0,tempbuf);
240 IStream_Release(pStm);
246 xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
247 if (pStm) IUnknown_Release(pStm);
248 HeapFree(GetProcessHeap(), 0, tempbuf);
252 /********************* OLE Proxy/Stub Factory ********************************/
253 static HRESULT WINAPI
254 PSFacBuf_QueryInterface(LPPSFACTORYBUFFER iface, REFIID iid, LPVOID *ppv) {
255 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)||IsEqualIID(iid,&IID_IUnknown)) {
256 *ppv = (LPVOID)iface;
257 /* No ref counting, static class */
260 FIXME("(%s) unknown IID?\n",debugstr_guid(iid));
261 return E_NOINTERFACE;
264 static ULONG WINAPI PSFacBuf_AddRef(LPPSFACTORYBUFFER iface) { return 2; }
265 static ULONG WINAPI PSFacBuf_Release(LPPSFACTORYBUFFER iface) { return 1; }
268 _get_typeinfo_for_iid(REFIID riid, ITypeInfo**ti) {
271 char tlguid[200],typelibkey[300],interfacekey[300],ver[100];
274 DWORD tlguidlen, verlen, type;
278 sprintf( interfacekey, "Interface\\{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\Typelib",
279 riid->Data1, riid->Data2, riid->Data3,
280 riid->Data4[0], riid->Data4[1], riid->Data4[2], riid->Data4[3],
281 riid->Data4[4], riid->Data4[5], riid->Data4[6], riid->Data4[7]
284 if (RegOpenKeyA(HKEY_CLASSES_ROOT,interfacekey,&ikey)) {
285 ERR("No %s key found.\n",interfacekey);
288 tlguidlen = sizeof(tlguid);
289 if (RegQueryValueExA(ikey,NULL,NULL,&type,(LPBYTE)tlguid,&tlguidlen)) {
290 ERR("Getting typelib guid failed.\n");
294 verlen = sizeof(ver);
295 if (RegQueryValueExA(ikey,"Version",NULL,&type,(LPBYTE)ver,&verlen)) {
296 ERR("Could not get version value?\n");
301 sprintf(typelibkey,"Typelib\\%s\\%s\\0\\win32",tlguid,ver);
302 tlfnlen = sizeof(tlfn);
303 if (RegQueryValueA(HKEY_CLASSES_ROOT,typelibkey,tlfn,&tlfnlen)) {
304 ERR("Could not get typelib fn?\n");
307 MultiByteToWideChar(CP_ACP, 0, tlfn, -1, tlfnW, sizeof(tlfnW) / sizeof(tlfnW[0]));
308 hres = LoadTypeLib(tlfnW,&tl);
310 ERR("Failed to load typelib for %s, but it should be there.\n",debugstr_guid(riid));
313 hres = ITypeLib_GetTypeInfoOfGuid(tl,riid,ti);
315 ERR("typelib does not contain info for %s?\n",debugstr_guid(riid));
316 ITypeLib_Release(tl);
319 ITypeLib_Release(tl);
324 * Determine the number of functions including all inherited functions.
325 * Note for non-dual dispinterfaces we simply return the size of IDispatch.
327 static HRESULT num_of_funcs(ITypeInfo *tinfo, unsigned int *num)
334 hres = ITypeInfo_GetTypeAttr(tinfo, &attr);
336 ERR("GetTypeAttr failed with %x\n",hres);
340 if(attr->typekind == TKIND_DISPATCH && (attr->wTypeFlags & TYPEFLAG_FDUAL))
343 hres = ITypeInfo_GetRefTypeOfImplType(tinfo, -1, &href);
346 ERR("Unable to get interface href from dual dispinterface\n");
349 hres = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
352 ERR("Unable to get interface from dual dispinterface\n");
355 hres = num_of_funcs(tinfo2, num);
356 ITypeInfo_Release(tinfo2);
360 *num = attr->cbSizeVft / 4;
364 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
370 #include "pshpack1.h"
372 typedef struct _TMAsmProxy {
387 # warning You need to implement stubless proxies for your architecture
388 typedef struct _TMAsmProxy {
392 typedef struct _TMProxyImpl {
394 const IRpcProxyBufferVtbl *lpvtbl2;
397 TMAsmProxy *asmstubs;
399 IRpcChannelBuffer* chanbuf;
401 CRITICAL_SECTION crit;
402 IUnknown *outerunknown;
404 IRpcProxyBuffer *dispatch_proxy;
407 static HRESULT WINAPI
408 TMProxyImpl_QueryInterface(LPRPCPROXYBUFFER iface, REFIID riid, LPVOID *ppv)
411 if (IsEqualIID(riid,&IID_IUnknown)||IsEqualIID(riid,&IID_IRpcProxyBuffer)) {
412 *ppv = (LPVOID)iface;
413 IRpcProxyBuffer_AddRef(iface);
416 FIXME("no interface for %s\n",debugstr_guid(riid));
417 return E_NOINTERFACE;
421 TMProxyImpl_AddRef(LPRPCPROXYBUFFER iface)
423 ICOM_THIS_MULTI(TMProxyImpl,lpvtbl2,iface);
424 ULONG refCount = InterlockedIncrement(&This->ref);
426 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
432 TMProxyImpl_Release(LPRPCPROXYBUFFER iface)
434 ICOM_THIS_MULTI(TMProxyImpl,lpvtbl2,iface);
435 ULONG refCount = InterlockedDecrement(&This->ref);
437 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
441 if (This->dispatch_proxy) IRpcProxyBuffer_Release(This->dispatch_proxy);
442 This->crit.DebugInfo->Spare[0] = 0;
443 DeleteCriticalSection(&This->crit);
444 if (This->chanbuf) IRpcChannelBuffer_Release(This->chanbuf);
445 VirtualFree(This->asmstubs, 0, MEM_RELEASE);
446 HeapFree(GetProcessHeap(), 0, This->lpvtbl);
447 ITypeInfo_Release(This->tinfo);
453 static HRESULT WINAPI
455 LPRPCPROXYBUFFER iface,IRpcChannelBuffer* pRpcChannelBuffer)
457 ICOM_THIS_MULTI(TMProxyImpl, lpvtbl2, iface);
459 TRACE("(%p)\n", pRpcChannelBuffer);
461 EnterCriticalSection(&This->crit);
463 IRpcChannelBuffer_AddRef(pRpcChannelBuffer);
464 This->chanbuf = pRpcChannelBuffer;
466 LeaveCriticalSection(&This->crit);
468 if (This->dispatch_proxy)
470 IRpcChannelBuffer *pDelegateChannel;
471 HRESULT hr = TMarshalDispatchChannel_Create(pRpcChannelBuffer, &This->iid, &pDelegateChannel);
474 hr = IRpcProxyBuffer_Connect(This->dispatch_proxy, pDelegateChannel);
475 IRpcChannelBuffer_Release(pDelegateChannel);
483 TMProxyImpl_Disconnect(LPRPCPROXYBUFFER iface)
485 ICOM_THIS_MULTI(TMProxyImpl, lpvtbl2, iface);
489 EnterCriticalSection(&This->crit);
491 IRpcChannelBuffer_Release(This->chanbuf);
492 This->chanbuf = NULL;
494 LeaveCriticalSection(&This->crit);
496 if (This->dispatch_proxy)
497 IRpcProxyBuffer_Disconnect(This->dispatch_proxy);
501 static const IRpcProxyBufferVtbl tmproxyvtable = {
502 TMProxyImpl_QueryInterface,
506 TMProxyImpl_Disconnect
509 /* how much space do we use on stack in DWORD steps. */
514 return 8/sizeof(DWORD);
516 return sizeof(double)/sizeof(DWORD);
518 return sizeof(CY)/sizeof(DWORD);
520 return sizeof(DATE)/sizeof(DWORD);
522 return (sizeof(VARIANT)+3)/sizeof(DWORD);
529 _xsize(const TYPEDESC *td) {
534 return sizeof(VARIANT)+3;
537 const ARRAYDESC *adesc = td->u.lpadesc;
539 for (i=0;i<adesc->cDims;i++)
540 arrsize *= adesc->rgbounds[i].cElements;
541 return arrsize*_xsize(&adesc->tdescElem);
569 TRACE("(tdesc.vt %s)\n",debugstr_vt(tdesc->vt));
572 case VT_EMPTY: /* nothing. empty variant for instance */
578 if (debugout) TRACE_(olerelay)("%x%x\n",arg[0],arg[1]);
580 hres = xbuf_add(buf,(LPBYTE)arg,8);
590 if (debugout) TRACE_(olerelay)("%x\n",*arg);
592 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
597 if (debugout) TRACE_(olerelay)("%04x\n",*arg & 0xffff);
599 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
604 if (debugout) TRACE_(olerelay)("%02x\n",*arg & 0xff);
606 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
610 if (debugout) TRACE_(olerelay)("&0x%x\n",*arg);
612 hres = xbuf_add(buf,(LPBYTE)(DWORD*)*arg,sizeof(DWORD));
613 /* do not dealloc at this time */
617 VARIANT *vt = (VARIANT*)arg;
618 DWORD vttype = V_VT(vt);
620 if (debugout) TRACE_(olerelay)("Vt(%s%s)(",debugstr_vt(vttype),debugstr_vf(vttype));
623 hres = xbuf_add(buf,(LPBYTE)&vttype,sizeof(vttype));
624 if (hres) return hres;
626 /* need to recurse since we need to free the stuff */
627 hres = serialize_param(tinfo,writeit,debugout,dealloc,&tdesc2,(DWORD*)&(V_I4(vt)),buf);
628 if (debugout) TRACE_(olerelay)(")");
631 case VT_BSTR|VT_BYREF: {
632 if (debugout) TRACE_(olerelay)("[byref]'%s'", *(BSTR*)*arg ? relaystr(*((BSTR*)*arg)) : "<bstr NULL>");
634 /* ptr to ptr to magic widestring, basically */
635 BSTR *bstr = (BSTR *) *arg;
638 /* -1 means "null string" which is equivalent to empty string */
640 hres = xbuf_add(buf, (LPBYTE)&len,sizeof(DWORD));
641 if (hres) return hres;
643 len = *((DWORD*)*bstr-1)/sizeof(WCHAR);
644 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
645 if (hres) return hres;
646 hres = xbuf_add(buf,(LPBYTE)*bstr,len * sizeof(WCHAR));
647 if (hres) return hres;
651 if (dealloc && arg) {
652 BSTR *str = *((BSTR **)arg);
661 TRACE_(olerelay)("%s",relaystr((WCHAR*)*arg));
663 TRACE_(olerelay)("<bstr NULL>");
666 BSTR bstr = (BSTR)*arg;
670 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
671 if (hres) return hres;
673 len = *((DWORD*)bstr-1)/sizeof(WCHAR);
674 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
675 if (hres) return hres;
676 hres = xbuf_add(buf,(LPBYTE)bstr,len * sizeof(WCHAR));
677 if (hres) return hres;
682 SysFreeString((BSTR)*arg);
687 BOOL derefhere = TRUE;
689 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
693 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
695 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
698 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
699 switch (tattr->typekind) {
700 case TKIND_ENUM: /* confirmed */
701 case TKIND_RECORD: /* FIXME: mostly untested */
704 case TKIND_ALIAS: /* FIXME: untested */
705 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
706 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
710 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
714 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
715 ITypeInfo_Release(tinfo2);
718 if (debugout) TRACE_(olerelay)("*");
719 /* Write always, so the other side knows when it gets a NULL pointer.
721 cookie = *arg ? 0x42424242 : 0;
722 hres = xbuf_add(buf,(LPBYTE)&cookie,sizeof(cookie));
726 if (debugout) TRACE_(olerelay)("NULL");
729 hres = serialize_param(tinfo,writeit,debugout,dealloc,tdesc->u.lptdesc,(DWORD*)*arg,buf);
730 if (derefhere && dealloc) HeapFree(GetProcessHeap(),0,(LPVOID)*arg);
734 if (debugout) TRACE_(olerelay)("unk(0x%x)",*arg);
736 hres = _marshal_interface(buf,&IID_IUnknown,(LPUNKNOWN)*arg);
737 if (dealloc && *(IUnknown **)arg)
738 IUnknown_Release((LPUNKNOWN)*arg);
741 if (debugout) TRACE_(olerelay)("idisp(0x%x)",*arg);
743 hres = _marshal_interface(buf,&IID_IDispatch,(LPUNKNOWN)*arg);
744 if (dealloc && *(IUnknown **)arg)
745 IUnknown_Release((LPUNKNOWN)*arg);
748 if (debugout) TRACE_(olerelay)("<void>");
750 case VT_USERDEFINED: {
754 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
756 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
759 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
760 switch (tattr->typekind) {
762 case TKIND_INTERFACE:
764 hres=_marshal_interface(buf,&(tattr->guid),(LPUNKNOWN)arg);
766 IUnknown_Release((LPUNKNOWN)arg);
770 if (debugout) TRACE_(olerelay)("{");
771 for (i=0;i<tattr->cVars;i++) {
776 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
778 ERR("Could not get vardesc of %d\n",i);
781 elem2 = &vdesc->elemdescVar;
782 tdesc2 = &elem2->tdesc;
783 hres = serialize_param(
789 (DWORD*)(((LPBYTE)arg)+vdesc->u.oInst),
792 ITypeInfo_ReleaseVarDesc(tinfo2, vdesc);
795 if (debugout && (i<(tattr->cVars-1)))
796 TRACE_(olerelay)(",");
798 if (debugout) TRACE_(olerelay)("}");
802 hres = serialize_param(tinfo2,writeit,debugout,dealloc,&tattr->tdescAlias,arg,buf);
806 if (debugout) TRACE_(olerelay)("%x",*arg);
808 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
811 FIXME("Unhandled typekind %d\n",tattr->typekind);
815 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
816 ITypeInfo_Release(tinfo2);
820 ARRAYDESC *adesc = tdesc->u.lpadesc;
823 if (debugout) TRACE_(olerelay)("carr");
824 for (i=0;i<adesc->cDims;i++) {
825 if (debugout) TRACE_(olerelay)("[%d]",adesc->rgbounds[i].cElements);
826 arrsize *= adesc->rgbounds[i].cElements;
828 if (debugout) TRACE_(olerelay)("(vt %s)",debugstr_vt(adesc->tdescElem.vt));
829 if (debugout) TRACE_(olerelay)("[");
830 for (i=0;i<arrsize;i++) {
831 hres = serialize_param(tinfo, writeit, debugout, dealloc, &adesc->tdescElem, (DWORD*)((LPBYTE)arg+i*_xsize(&adesc->tdescElem)), buf);
834 if (debugout && (i<arrsize-1)) TRACE_(olerelay)(",");
836 if (debugout) TRACE_(olerelay)("]");
842 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
843 ULONG size = LPSAFEARRAY_UserSize(&flags, buf->curoff, (LPSAFEARRAY *)arg);
844 xbuf_resize(buf, size);
845 LPSAFEARRAY_UserMarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
851 ERR("Unhandled marshal type %d.\n",tdesc->vt);
868 TRACE("vt %s at %p\n",debugstr_vt(tdesc->vt),arg);
873 if (debugout) TRACE_(olerelay)("<empty>\n");
876 if (debugout) TRACE_(olerelay)("<null>\n");
879 VARIANT *vt = (VARIANT*)arg;
884 hres = xbuf_get(buf,(LPBYTE)&vttype,sizeof(vttype));
886 FIXME("vt type not read?\n");
889 memset(&tdesc2,0,sizeof(tdesc2));
892 if (debugout) TRACE_(olerelay)("Vt(%s%s)(",debugstr_vt(vttype),debugstr_vf(vttype));
893 hres = deserialize_param(tinfo, readit, debugout, alloc, &tdesc2, (DWORD*)&(V_I4(vt)), buf);
894 TRACE_(olerelay)(")");
905 hres = xbuf_get(buf,(LPBYTE)arg,8);
906 if (hres) ERR("Failed to read integer 8 byte\n");
908 if (debugout) TRACE_(olerelay)("%x%x",arg[0],arg[1]);
918 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
919 if (hres) ERR("Failed to read integer 4 byte\n");
921 if (debugout) TRACE_(olerelay)("%x",*arg);
927 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
928 if (hres) ERR("Failed to read integer 4 byte\n");
931 if (debugout) TRACE_(olerelay)("%04x",*arg & 0xffff);
937 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
938 if (hres) ERR("Failed to read integer 4 byte\n");
941 if (debugout) TRACE_(olerelay)("%02x",*arg & 0xff);
946 *arg = (DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
948 hres = xbuf_get(buf,(LPBYTE)*arg,sizeof(DWORD));
949 if (hres) ERR("Failed to read integer 4 byte\n");
951 if (debugout) TRACE_(olerelay)("&0x%x",*(DWORD*)*arg);
953 case VT_BSTR|VT_BYREF: {
954 BSTR **bstr = (BSTR **)arg;
959 hres = xbuf_get(buf,(LPBYTE)&len,sizeof(DWORD));
961 ERR("failed to read bstr klen\n");
965 *bstr = CoTaskMemAlloc(sizeof(BSTR *));
967 if (debugout) TRACE_(olerelay)("<bstr NULL>");
969 str = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(len+1)*sizeof(WCHAR));
970 hres = xbuf_get(buf,(LPBYTE)str,len*sizeof(WCHAR));
972 ERR("Failed to read BSTR.\n");
973 HeapFree(GetProcessHeap(),0,str);
976 *bstr = CoTaskMemAlloc(sizeof(BSTR *));
977 **bstr = SysAllocStringLen(str,len);
978 if (debugout) TRACE_(olerelay)("%s",relaystr(str));
979 HeapFree(GetProcessHeap(),0,str);
991 hres = xbuf_get(buf,(LPBYTE)&len,sizeof(DWORD));
993 ERR("failed to read bstr klen\n");
998 if (debugout) TRACE_(olerelay)("<bstr NULL>");
1000 str = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(len+1)*sizeof(WCHAR));
1001 hres = xbuf_get(buf,(LPBYTE)str,len*sizeof(WCHAR));
1003 ERR("Failed to read BSTR.\n");
1004 HeapFree(GetProcessHeap(),0,str);
1007 *arg = (DWORD)SysAllocStringLen(str,len);
1008 if (debugout) TRACE_(olerelay)("%s",relaystr(str));
1009 HeapFree(GetProcessHeap(),0,str);
1018 BOOL derefhere = TRUE;
1020 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
1024 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
1026 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
1029 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1030 switch (tattr->typekind) {
1031 case TKIND_ENUM: /* confirmed */
1032 case TKIND_RECORD: /* FIXME: mostly untested */
1035 case TKIND_ALIAS: /* FIXME: untested */
1036 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
1037 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
1041 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
1045 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1046 ITypeInfo_Release(tinfo2);
1048 /* read it in all cases, we need to know if we have
1049 * NULL pointer or not.
1051 hres = xbuf_get(buf,(LPBYTE)&cookie,sizeof(cookie));
1053 ERR("Failed to load pointer cookie.\n");
1056 if (cookie != 0x42424242) {
1057 /* we read a NULL ptr from the remote side */
1058 if (debugout) TRACE_(olerelay)("NULL");
1062 if (debugout) TRACE_(olerelay)("*");
1064 /* Allocate space for the referenced struct */
1066 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,_xsize(tdesc->u.lptdesc));
1069 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, (LPDWORD)*arg, buf);
1071 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, arg, buf);
1074 /* FIXME: UNKNOWN is unknown ..., but allocate 4 byte for it */
1076 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
1079 hres = _unmarshal_interface(buf,&IID_IUnknown,(LPUNKNOWN*)arg);
1081 TRACE_(olerelay)("unk(%p)",arg);
1086 hres = _unmarshal_interface(buf,&IID_IDispatch,(LPUNKNOWN*)arg);
1088 TRACE_(olerelay)("idisp(%p)",arg);
1091 if (debugout) TRACE_(olerelay)("<void>");
1093 case VT_USERDEFINED: {
1097 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
1099 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
1102 hres = ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1104 ERR("Could not get typeattr in VT_USERDEFINED.\n");
1106 switch (tattr->typekind) {
1107 case TKIND_DISPATCH:
1108 case TKIND_INTERFACE:
1110 hres = _unmarshal_interface(buf,&(tattr->guid),(LPUNKNOWN*)arg);
1112 case TKIND_RECORD: {
1116 *arg = (DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,tattr->cbSizeInstance);
1118 if (debugout) TRACE_(olerelay)("{");
1119 for (i=0;i<tattr->cVars;i++) {
1122 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
1124 ERR("Could not get vardesc of %d\n",i);
1125 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1126 ITypeInfo_Release(tinfo2);
1129 hres = deserialize_param(
1134 &vdesc->elemdescVar.tdesc,
1135 (DWORD*)(((LPBYTE)*arg)+vdesc->u.oInst),
1138 ITypeInfo2_ReleaseVarDesc(tinfo2, vdesc);
1139 if (debugout && (i<tattr->cVars-1)) TRACE_(olerelay)(",");
1141 if (debugout) TRACE_(olerelay)("}");
1145 hres = deserialize_param(tinfo2,readit,debugout,alloc,&tattr->tdescAlias,arg,buf);
1149 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
1150 if (hres) ERR("Failed to read enum (4 byte)\n");
1152 if (debugout) TRACE_(olerelay)("%x",*arg);
1155 ERR("Unhandled typekind %d\n",tattr->typekind);
1159 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1162 ERR("failed to stuballoc in TKIND_RECORD.\n");
1163 ITypeInfo_Release(tinfo2);
1167 /* arg is pointing to the start of the array. */
1168 ARRAYDESC *adesc = tdesc->u.lpadesc;
1171 if (adesc->cDims > 1) FIXME("cDims > 1 in VT_CARRAY. Does it work?\n");
1172 for (i=0;i<adesc->cDims;i++)
1173 arrsize *= adesc->rgbounds[i].cElements;
1174 for (i=0;i<arrsize;i++)
1181 (DWORD*)((LPBYTE)(arg)+i*_xsize(&adesc->tdescElem)),
1186 case VT_SAFEARRAY: {
1189 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
1190 unsigned char *buffer;
1191 buffer = LPSAFEARRAY_UserUnmarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
1192 buf->curoff = buffer - buf->base;
1197 ERR("No handler for VT type %d!\n",tdesc->vt);
1203 /* Retrieves a function's funcdesc, searching back into inherited interfaces. */
1204 static HRESULT get_funcdesc(ITypeInfo *tinfo, int iMethod, ITypeInfo **tactual, const FUNCDESC **fdesc,
1205 BSTR *iname, BSTR *fname, UINT *num)
1209 UINT inherited_funcs = 0;
1212 if (fname) *fname = NULL;
1213 if (iname) *iname = NULL;
1217 hr = ITypeInfo_GetTypeAttr(tinfo, &attr);
1220 ERR("GetTypeAttr failed with %x\n",hr);
1224 if(attr->typekind == TKIND_DISPATCH)
1226 if(attr->wTypeFlags & TYPEFLAG_FDUAL)
1231 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, -1, &href);
1234 ERR("Cannot get interface href from dual dispinterface\n");
1235 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1238 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
1241 ERR("Cannot get interface from dual dispinterface\n");
1242 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1245 hr = get_funcdesc(tinfo2, iMethod, tactual, fdesc, iname, fname, num);
1246 ITypeInfo_Release(tinfo2);
1247 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1250 ERR("Shouldn't be called with a non-dual dispinterface\n");
1254 impl_types = attr->cImplTypes;
1255 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1257 for (i = 0; i < impl_types; i++)
1260 ITypeInfo *pSubTypeInfo;
1263 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, i, &href);
1264 if (FAILED(hr)) return hr;
1265 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &pSubTypeInfo);
1266 if (FAILED(hr)) return hr;
1268 hr = get_funcdesc(pSubTypeInfo, iMethod, tactual, fdesc, iname, fname, &sub_funcs);
1269 inherited_funcs += sub_funcs;
1270 ITypeInfo_Release(pSubTypeInfo);
1271 if(SUCCEEDED(hr)) return hr;
1273 if(iMethod < inherited_funcs)
1275 ERR("shouldn't be here\n");
1276 return E_INVALIDARG;
1279 for(i = inherited_funcs; i <= iMethod; i++)
1281 hr = ITypeInfoImpl_GetInternalFuncDesc(tinfo, i - inherited_funcs, fdesc);
1289 /* found it. We don't care about num so zero it */
1292 ITypeInfo_AddRef(*tactual);
1293 if (fname) ITypeInfo_GetDocumentation(tinfo,(*fdesc)->memid,fname,NULL,NULL,NULL);
1294 if (iname) ITypeInfo_GetDocumentation(tinfo,-1,iname,NULL,NULL,NULL);
1298 static inline BOOL is_in_elem(const ELEMDESC *elem)
1300 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN || !elem->u.paramdesc.wParamFlags);
1303 static inline BOOL is_out_elem(const ELEMDESC *elem)
1305 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT || !elem->u.paramdesc.wParamFlags);
1309 xCall(LPVOID retptr, int method, TMProxyImpl *tpinfo /*, args */)
1311 DWORD *args = ((DWORD*)&tpinfo)+1, *xargs;
1312 const FUNCDESC *fdesc;
1314 int i, relaydeb = TRACE_ON(olerelay);
1321 DWORD remoteresult = 0;
1323 IRpcChannelBuffer *chanbuf;
1325 EnterCriticalSection(&tpinfo->crit);
1327 hres = get_funcdesc(tpinfo->tinfo,method,&tinfo,&fdesc,&iname,&fname,NULL);
1329 ERR("Did not find typeinfo/funcdesc entry for method %d!\n",method);
1330 LeaveCriticalSection(&tpinfo->crit);
1334 if (!tpinfo->chanbuf)
1336 WARN("Tried to use disconnected proxy\n");
1337 ITypeInfo_Release(tinfo);
1338 LeaveCriticalSection(&tpinfo->crit);
1339 return RPC_E_DISCONNECTED;
1341 chanbuf = tpinfo->chanbuf;
1342 IRpcChannelBuffer_AddRef(chanbuf);
1344 LeaveCriticalSection(&tpinfo->crit);
1347 TRACE_(olerelay)("->");
1349 TRACE_(olerelay)("%s:",relaystr(iname));
1351 TRACE_(olerelay)("%s(%d)",relaystr(fname),method);
1353 TRACE_(olerelay)("%d",method);
1354 TRACE_(olerelay)("(");
1357 if (iname) SysFreeString(iname);
1358 if (fname) SysFreeString(fname);
1360 memset(&buf,0,sizeof(buf));
1362 /* normal typelib driven serializing */
1364 /* Need them for hack below */
1365 memset(names,0,sizeof(names));
1366 if (ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames))
1368 if (nrofnames > sizeof(names)/sizeof(names[0]))
1369 ERR("Need more names!\n");
1372 for (i=0;i<fdesc->cParams;i++) {
1373 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1375 if (i) TRACE_(olerelay)(",");
1376 if (i+1<nrofnames && names[i+1])
1377 TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1379 /* No need to marshal other data than FIN and any VT_PTR. */
1380 if (!is_in_elem(elem) && (elem->tdesc.vt != VT_PTR)) {
1381 xargs+=_argsize(elem->tdesc.vt);
1382 if (relaydeb) TRACE_(olerelay)("[out]");
1385 hres = serialize_param(
1396 ERR("Failed to serialize param, hres %x\n",hres);
1399 xargs+=_argsize(elem->tdesc.vt);
1401 if (relaydeb) TRACE_(olerelay)(")");
1403 memset(&msg,0,sizeof(msg));
1404 msg.cbBuffer = buf.curoff;
1405 msg.iMethod = method;
1406 hres = IRpcChannelBuffer_GetBuffer(chanbuf,&msg,&(tpinfo->iid));
1408 ERR("RpcChannelBuffer GetBuffer failed, %x\n",hres);
1411 memcpy(msg.Buffer,buf.base,buf.curoff);
1412 if (relaydeb) TRACE_(olerelay)("\n");
1413 hres = IRpcChannelBuffer_SendReceive(chanbuf,&msg,&status);
1415 ERR("RpcChannelBuffer SendReceive failed, %x\n",hres);
1419 if (relaydeb) TRACE_(olerelay)(" status = %08x (",status);
1421 buf.base = HeapReAlloc(GetProcessHeap(),0,buf.base,msg.cbBuffer);
1423 buf.base = HeapAlloc(GetProcessHeap(),0,msg.cbBuffer);
1424 buf.size = msg.cbBuffer;
1425 memcpy(buf.base,msg.Buffer,buf.size);
1428 /* generic deserializer using typelib description */
1431 for (i=0;i<fdesc->cParams;i++) {
1432 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1435 if (i) TRACE_(olerelay)(",");
1436 if (i+1<nrofnames && names[i+1]) TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1438 /* No need to marshal other data than FOUT and any VT_PTR */
1439 if (!is_out_elem(elem) && (elem->tdesc.vt != VT_PTR)) {
1440 xargs += _argsize(elem->tdesc.vt);
1441 if (relaydeb) TRACE_(olerelay)("[in]");
1444 hres = deserialize_param(
1454 ERR("Failed to unmarshall param, hres %x\n",hres);
1458 xargs += _argsize(elem->tdesc.vt);
1461 hres = xbuf_get(&buf, (LPBYTE)&remoteresult, sizeof(DWORD));
1464 if (relaydeb) TRACE_(olerelay)(") = %08x\n", remoteresult);
1466 hres = remoteresult;
1469 for (i = 0; i < nrofnames; i++)
1470 SysFreeString(names[i]);
1471 HeapFree(GetProcessHeap(),0,buf.base);
1472 IRpcChannelBuffer_Release(chanbuf);
1473 ITypeInfo_Release(tinfo);
1474 TRACE("-- 0x%08x\n", hres);
1478 static HRESULT WINAPI ProxyIUnknown_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
1480 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1482 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1484 if (proxy->outerunknown)
1485 return IUnknown_QueryInterface(proxy->outerunknown, riid, ppv);
1487 FIXME("No interface\n");
1488 return E_NOINTERFACE;
1491 static ULONG WINAPI ProxyIUnknown_AddRef(IUnknown *iface)
1493 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1497 if (proxy->outerunknown)
1498 return IUnknown_AddRef(proxy->outerunknown);
1500 return 2; /* FIXME */
1503 static ULONG WINAPI ProxyIUnknown_Release(IUnknown *iface)
1505 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1509 if (proxy->outerunknown)
1510 return IUnknown_Release(proxy->outerunknown);
1512 return 1; /* FIXME */
1515 static HRESULT WINAPI ProxyIDispatch_GetTypeInfoCount(LPDISPATCH iface, UINT * pctinfo)
1517 TMProxyImpl *This = (TMProxyImpl *)iface;
1519 TRACE("(%p)\n", pctinfo);
1521 return IDispatch_GetTypeInfoCount(This->dispatch, pctinfo);
1524 static HRESULT WINAPI ProxyIDispatch_GetTypeInfo(LPDISPATCH iface, UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo)
1526 TMProxyImpl *This = (TMProxyImpl *)iface;
1528 TRACE("(%d, %x, %p)\n", iTInfo, lcid, ppTInfo);
1530 return IDispatch_GetTypeInfo(This->dispatch, iTInfo, lcid, ppTInfo);
1533 static HRESULT WINAPI ProxyIDispatch_GetIDsOfNames(LPDISPATCH iface, REFIID riid, LPOLESTR * rgszNames, UINT cNames, LCID lcid, DISPID * rgDispId)
1535 TMProxyImpl *This = (TMProxyImpl *)iface;
1537 TRACE("(%s, %p, %d, 0x%x, %p)\n", debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);
1539 return IDispatch_GetIDsOfNames(This->dispatch, riid, rgszNames,
1540 cNames, lcid, rgDispId);
1543 static HRESULT WINAPI ProxyIDispatch_Invoke(LPDISPATCH iface, DISPID dispIdMember, REFIID riid, LCID lcid,
1544 WORD wFlags, DISPPARAMS * pDispParams, VARIANT * pVarResult,
1545 EXCEPINFO * pExcepInfo, UINT * puArgErr)
1547 TMProxyImpl *This = (TMProxyImpl *)iface;
1549 TRACE("(%d, %s, 0x%x, 0x%x, %p, %p, %p, %p)\n", dispIdMember,
1550 debugstr_guid(riid), lcid, wFlags, pDispParams, pVarResult,
1551 pExcepInfo, puArgErr);
1553 return IDispatch_Invoke(This->dispatch, dispIdMember, riid, lcid,
1554 wFlags, pDispParams, pVarResult, pExcepInfo,
1560 const IRpcChannelBufferVtbl *lpVtbl;
1562 /* the IDispatch-derived interface we are handling */
1564 IRpcChannelBuffer *pDelegateChannel;
1565 } TMarshalDispatchChannel;
1567 static HRESULT WINAPI TMarshalDispatchChannel_QueryInterface(LPRPCCHANNELBUFFER iface, REFIID riid, LPVOID *ppv)
1570 if (IsEqualIID(riid,&IID_IRpcChannelBuffer) || IsEqualIID(riid,&IID_IUnknown))
1572 *ppv = (LPVOID)iface;
1573 IUnknown_AddRef(iface);
1576 return E_NOINTERFACE;
1579 static ULONG WINAPI TMarshalDispatchChannel_AddRef(LPRPCCHANNELBUFFER iface)
1581 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1582 return InterlockedIncrement(&This->refs);
1585 static ULONG WINAPI TMarshalDispatchChannel_Release(LPRPCCHANNELBUFFER iface)
1587 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1590 ref = InterlockedDecrement(&This->refs);
1594 IRpcChannelBuffer_Release(This->pDelegateChannel);
1595 HeapFree(GetProcessHeap(), 0, This);
1599 static HRESULT WINAPI TMarshalDispatchChannel_GetBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg, REFIID riid)
1601 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1602 TRACE("(%p, %s)\n", olemsg, debugstr_guid(riid));
1603 /* Note: we are pretending to invoke a method on the interface identified
1604 * by tmarshal_iid so that we can re-use the IDispatch proxy/stub code
1605 * without the RPC runtime getting confused by not exporting an IDispatch interface */
1606 return IRpcChannelBuffer_GetBuffer(This->pDelegateChannel, olemsg, &This->tmarshal_iid);
1609 static HRESULT WINAPI TMarshalDispatchChannel_SendReceive(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE *olemsg, ULONG *pstatus)
1611 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1612 TRACE("(%p, %p)\n", olemsg, pstatus);
1613 return IRpcChannelBuffer_SendReceive(This->pDelegateChannel, olemsg, pstatus);
1616 static HRESULT WINAPI TMarshalDispatchChannel_FreeBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg)
1618 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1619 TRACE("(%p)\n", olemsg);
1620 return IRpcChannelBuffer_FreeBuffer(This->pDelegateChannel, olemsg);
1623 static HRESULT WINAPI TMarshalDispatchChannel_GetDestCtx(LPRPCCHANNELBUFFER iface, DWORD* pdwDestContext, void** ppvDestContext)
1625 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1626 TRACE("(%p,%p)\n", pdwDestContext, ppvDestContext);
1627 return IRpcChannelBuffer_GetDestCtx(This->pDelegateChannel, pdwDestContext, ppvDestContext);
1630 static HRESULT WINAPI TMarshalDispatchChannel_IsConnected(LPRPCCHANNELBUFFER iface)
1632 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1634 return IRpcChannelBuffer_IsConnected(This->pDelegateChannel);
1637 static const IRpcChannelBufferVtbl TMarshalDispatchChannelVtbl =
1639 TMarshalDispatchChannel_QueryInterface,
1640 TMarshalDispatchChannel_AddRef,
1641 TMarshalDispatchChannel_Release,
1642 TMarshalDispatchChannel_GetBuffer,
1643 TMarshalDispatchChannel_SendReceive,
1644 TMarshalDispatchChannel_FreeBuffer,
1645 TMarshalDispatchChannel_GetDestCtx,
1646 TMarshalDispatchChannel_IsConnected
1649 static HRESULT TMarshalDispatchChannel_Create(
1650 IRpcChannelBuffer *pDelegateChannel, REFIID tmarshal_riid,
1651 IRpcChannelBuffer **ppChannel)
1653 TMarshalDispatchChannel *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1655 return E_OUTOFMEMORY;
1657 This->lpVtbl = &TMarshalDispatchChannelVtbl;
1659 IRpcChannelBuffer_AddRef(pDelegateChannel);
1660 This->pDelegateChannel = pDelegateChannel;
1661 This->tmarshal_iid = *tmarshal_riid;
1663 *ppChannel = (IRpcChannelBuffer *)&This->lpVtbl;
1668 static inline HRESULT get_facbuf_for_iid(REFIID riid, IPSFactoryBuffer **facbuf)
1673 if ((hr = CoGetPSClsid(riid, &clsid)))
1675 return CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL,
1676 &IID_IPSFactoryBuffer, (LPVOID*)facbuf);
1679 static HRESULT init_proxy_entry_point(TMProxyImpl *proxy, unsigned int num)
1682 /* nrofargs without This */
1685 TMAsmProxy *xasm = proxy->asmstubs + num;
1687 const FUNCDESC *fdesc;
1689 hres = get_funcdesc(proxy->tinfo, num, &tinfo2, &fdesc, NULL, NULL, NULL);
1691 ERR("GetFuncDesc %x should not fail here.\n",hres);
1694 ITypeInfo_Release(tinfo2);
1695 /* some args take more than 4 byte on the stack */
1697 for (j=0;j<fdesc->cParams;j++)
1698 nrofargs += _argsize(fdesc->lprgelemdescParam[j].tdesc.vt);
1701 if (fdesc->callconv != CC_STDCALL) {
1702 ERR("calling convention is not stdcall????\n");
1705 /* popl %eax - return ptr
1712 * arg3 arg2 arg1 <method> <returnptr>
1714 xasm->popleax = 0x58;
1715 xasm->pushlval = 0x68;
1717 xasm->pushleax = 0x50;
1718 xasm->lcall = 0xe8; /* relative jump */
1719 xasm->xcall = (DWORD)xCall;
1720 xasm->xcall -= (DWORD)&(xasm->lret);
1722 xasm->bytestopop = (nrofargs+2)*4; /* pop args, This, iMethod */
1724 proxy->lpvtbl[num] = xasm;
1726 FIXME("not implemented on non i386\n");
1732 static HRESULT WINAPI
1733 PSFacBuf_CreateProxy(
1734 LPPSFACTORYBUFFER iface, IUnknown* pUnkOuter, REFIID riid,
1735 IRpcProxyBuffer **ppProxy, LPVOID *ppv)
1739 unsigned int i, nroffuncs;
1742 BOOL defer_to_dispatch = FALSE;
1744 TRACE("(...%s...)\n",debugstr_guid(riid));
1745 hres = _get_typeinfo_for_iid(riid,&tinfo);
1747 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
1751 hres = num_of_funcs(tinfo, &nroffuncs);
1753 ERR("Cannot get number of functions for typeinfo %s\n",debugstr_guid(riid));
1754 ITypeInfo_Release(tinfo);
1758 proxy = CoTaskMemAlloc(sizeof(TMProxyImpl));
1759 if (!proxy) return E_OUTOFMEMORY;
1761 assert(sizeof(TMAsmProxy) == 16);
1763 proxy->dispatch = NULL;
1764 proxy->dispatch_proxy = NULL;
1765 proxy->outerunknown = pUnkOuter;
1766 proxy->asmstubs = VirtualAlloc(NULL, sizeof(TMAsmProxy) * nroffuncs, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
1767 if (!proxy->asmstubs) {
1768 ERR("Could not commit pages for proxy thunks\n");
1769 CoTaskMemFree(proxy);
1770 return E_OUTOFMEMORY;
1772 proxy->lpvtbl2 = &tmproxyvtable;
1773 /* one reference for the proxy */
1775 proxy->tinfo = tinfo;
1776 memcpy(&proxy->iid,riid,sizeof(*riid));
1779 InitializeCriticalSection(&proxy->crit);
1780 proxy->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TMProxyImpl.crit");
1782 proxy->lpvtbl = HeapAlloc(GetProcessHeap(),0,sizeof(LPBYTE)*nroffuncs);
1784 /* if we derive from IDispatch then defer to its proxy for its methods */
1785 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
1788 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
1790 IPSFactoryBuffer *factory_buffer;
1791 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
1794 hres = IPSFactoryBuffer_CreateProxy(factory_buffer, NULL,
1795 &IID_IDispatch, &proxy->dispatch_proxy,
1796 (void **)&proxy->dispatch);
1797 IPSFactoryBuffer_Release(factory_buffer);
1799 if ((hres == S_OK) && (nroffuncs < 7))
1801 ERR("nroffuncs calculated incorrectly (%d)\n", nroffuncs);
1802 hres = E_UNEXPECTED;
1806 defer_to_dispatch = TRUE;
1809 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
1812 for (i=0;i<nroffuncs;i++) {
1815 proxy->lpvtbl[i] = ProxyIUnknown_QueryInterface;
1818 proxy->lpvtbl[i] = ProxyIUnknown_AddRef;
1821 proxy->lpvtbl[i] = ProxyIUnknown_Release;
1824 if(!defer_to_dispatch)
1826 hres = init_proxy_entry_point(proxy, i);
1827 if(FAILED(hres)) return hres;
1829 else proxy->lpvtbl[3] = ProxyIDispatch_GetTypeInfoCount;
1832 if(!defer_to_dispatch)
1834 hres = init_proxy_entry_point(proxy, i);
1835 if(FAILED(hres)) return hres;
1837 else proxy->lpvtbl[4] = ProxyIDispatch_GetTypeInfo;
1840 if(!defer_to_dispatch)
1842 hres = init_proxy_entry_point(proxy, i);
1843 if(FAILED(hres)) return hres;
1845 else proxy->lpvtbl[5] = ProxyIDispatch_GetIDsOfNames;
1848 if(!defer_to_dispatch)
1850 hres = init_proxy_entry_point(proxy, i);
1851 if(FAILED(hres)) return hres;
1853 else proxy->lpvtbl[6] = ProxyIDispatch_Invoke;
1856 hres = init_proxy_entry_point(proxy, i);
1857 if(FAILED(hres)) return hres;
1863 *ppv = (LPVOID)proxy;
1864 *ppProxy = (IRpcProxyBuffer *)&(proxy->lpvtbl2);
1865 IUnknown_AddRef((IUnknown *)*ppv);
1869 TMProxyImpl_Release((IRpcProxyBuffer *)&proxy->lpvtbl2);
1873 typedef struct _TMStubImpl {
1874 const IRpcStubBufferVtbl *lpvtbl;
1880 IRpcStubBuffer *dispatch_stub;
1881 BOOL dispatch_derivative;
1884 static HRESULT WINAPI
1885 TMStubImpl_QueryInterface(LPRPCSTUBBUFFER iface, REFIID riid, LPVOID *ppv)
1887 if (IsEqualIID(riid,&IID_IRpcStubBuffer)||IsEqualIID(riid,&IID_IUnknown)){
1888 *ppv = (LPVOID)iface;
1889 IRpcStubBuffer_AddRef(iface);
1892 FIXME("%s, not supported IID.\n",debugstr_guid(riid));
1893 return E_NOINTERFACE;
1897 TMStubImpl_AddRef(LPRPCSTUBBUFFER iface)
1899 TMStubImpl *This = (TMStubImpl *)iface;
1900 ULONG refCount = InterlockedIncrement(&This->ref);
1902 TRACE("(%p)->(ref before=%u)\n", This, refCount - 1);
1908 TMStubImpl_Release(LPRPCSTUBBUFFER iface)
1910 TMStubImpl *This = (TMStubImpl *)iface;
1911 ULONG refCount = InterlockedDecrement(&This->ref);
1913 TRACE("(%p)->(ref before=%u)\n", This, refCount + 1);
1917 IRpcStubBuffer_Disconnect(iface);
1918 ITypeInfo_Release(This->tinfo);
1919 if (This->dispatch_stub)
1920 IRpcStubBuffer_Release(This->dispatch_stub);
1921 CoTaskMemFree(This);
1926 static HRESULT WINAPI
1927 TMStubImpl_Connect(LPRPCSTUBBUFFER iface, LPUNKNOWN pUnkServer)
1929 TMStubImpl *This = (TMStubImpl *)iface;
1931 TRACE("(%p)->(%p)\n", This, pUnkServer);
1933 IUnknown_AddRef(pUnkServer);
1934 This->pUnk = pUnkServer;
1936 if (This->dispatch_stub)
1937 IRpcStubBuffer_Connect(This->dispatch_stub, pUnkServer);
1943 TMStubImpl_Disconnect(LPRPCSTUBBUFFER iface)
1945 TMStubImpl *This = (TMStubImpl *)iface;
1947 TRACE("(%p)->()\n", This);
1951 IUnknown_Release(This->pUnk);
1955 if (This->dispatch_stub)
1956 IRpcStubBuffer_Disconnect(This->dispatch_stub);
1959 static HRESULT WINAPI
1961 LPRPCSTUBBUFFER iface, RPCOLEMESSAGE* xmsg,IRpcChannelBuffer*rpcchanbuf)
1964 const FUNCDESC *fdesc;
1965 TMStubImpl *This = (TMStubImpl *)iface;
1967 DWORD *args = NULL, res, *xargs, nrofargs;
1972 ITypeInfo *tinfo = NULL;
1976 if (xmsg->iMethod < 3) {
1977 ERR("IUnknown methods cannot be marshaled by the typelib marshaler\n");
1978 return E_UNEXPECTED;
1981 if (This->dispatch_derivative && xmsg->iMethod < sizeof(IDispatchVtbl)/sizeof(void *))
1983 IPSFactoryBuffer *factory_buffer;
1984 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
1987 hres = IPSFactoryBuffer_CreateStub(factory_buffer, &IID_IDispatch,
1988 This->pUnk, &This->dispatch_stub);
1989 IPSFactoryBuffer_Release(factory_buffer);
1993 return IRpcStubBuffer_Invoke(This->dispatch_stub, xmsg, rpcchanbuf);
1996 memset(&buf,0,sizeof(buf));
1997 buf.size = xmsg->cbBuffer;
1998 buf.base = HeapAlloc(GetProcessHeap(), 0, xmsg->cbBuffer);
1999 memcpy(buf.base, xmsg->Buffer, xmsg->cbBuffer);
2002 hres = get_funcdesc(This->tinfo,xmsg->iMethod,&tinfo,&fdesc,&iname,NULL,NULL);
2004 ERR("GetFuncDesc on method %d failed with %x\n",xmsg->iMethod,hres);
2008 if (iname && !lstrcmpW(iname, IDispatchW))
2010 ERR("IDispatch cannot be marshaled by the typelib marshaler\n");
2011 hres = E_UNEXPECTED;
2012 SysFreeString (iname);
2016 if (iname) SysFreeString (iname);
2018 /* Need them for hack below */
2019 memset(names,0,sizeof(names));
2020 ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames);
2021 if (nrofnames > sizeof(names)/sizeof(names[0])) {
2022 ERR("Need more names!\n");
2025 /*dump_FUNCDESC(fdesc);*/
2027 for (i=0;i<fdesc->cParams;i++)
2028 nrofargs += _argsize(fdesc->lprgelemdescParam[i].tdesc.vt);
2029 args = HeapAlloc(GetProcessHeap(),0,(nrofargs+1)*sizeof(DWORD));
2032 hres = E_OUTOFMEMORY;
2036 /* Allocate all stuff used by call. */
2038 for (i=0;i<fdesc->cParams;i++) {
2039 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2041 hres = deserialize_param(
2050 xargs += _argsize(elem->tdesc.vt);
2052 ERR("Failed to deserialize param %s, hres %x\n",relaystr(names[i+1]),hres);
2057 args[0] = (DWORD)This->pUnk;
2062 (*((FARPROC**)args[0]))[fdesc->oVft/4],
2070 DWORD dwExceptionCode = GetExceptionCode();
2071 ERR("invoke call failed with exception 0x%08x (%d)\n", dwExceptionCode, dwExceptionCode);
2072 if (FAILED(dwExceptionCode))
2073 hres = dwExceptionCode;
2075 hres = HRESULT_FROM_WIN32(dwExceptionCode);
2085 for (i=0;i<fdesc->cParams;i++) {
2086 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2087 hres = serialize_param(
2096 xargs += _argsize(elem->tdesc.vt);
2098 ERR("Failed to stuballoc param, hres %x\n",hres);
2103 hres = xbuf_add (&buf, (LPBYTE)&res, sizeof(DWORD));
2108 xmsg->cbBuffer = buf.curoff;
2109 hres = IRpcChannelBuffer_GetBuffer(rpcchanbuf, xmsg, &This->iid);
2111 ERR("IRpcChannelBuffer_GetBuffer failed with error 0x%08x\n", hres);
2114 memcpy(xmsg->Buffer, buf.base, buf.curoff);
2117 for (i = 0; i < nrofnames; i++)
2118 SysFreeString(names[i]);
2120 ITypeInfo_Release(tinfo);
2121 HeapFree(GetProcessHeap(), 0, args);
2123 HeapFree(GetProcessHeap(), 0, buf.base);
2125 TRACE("returning\n");
2129 static LPRPCSTUBBUFFER WINAPI
2130 TMStubImpl_IsIIDSupported(LPRPCSTUBBUFFER iface, REFIID riid) {
2131 FIXME("Huh (%s)?\n",debugstr_guid(riid));
2136 TMStubImpl_CountRefs(LPRPCSTUBBUFFER iface) {
2137 TMStubImpl *This = (TMStubImpl *)iface;
2140 return This->ref; /*FIXME? */
2143 static HRESULT WINAPI
2144 TMStubImpl_DebugServerQueryInterface(LPRPCSTUBBUFFER iface, LPVOID *ppv) {
2149 TMStubImpl_DebugServerRelease(LPRPCSTUBBUFFER iface, LPVOID ppv) {
2153 static const IRpcStubBufferVtbl tmstubvtbl = {
2154 TMStubImpl_QueryInterface,
2158 TMStubImpl_Disconnect,
2160 TMStubImpl_IsIIDSupported,
2161 TMStubImpl_CountRefs,
2162 TMStubImpl_DebugServerQueryInterface,
2163 TMStubImpl_DebugServerRelease
2166 static HRESULT WINAPI
2167 PSFacBuf_CreateStub(
2168 LPPSFACTORYBUFFER iface, REFIID riid,IUnknown *pUnkServer,
2169 IRpcStubBuffer** ppStub
2176 TRACE("(%s,%p,%p)\n",debugstr_guid(riid),pUnkServer,ppStub);
2178 hres = _get_typeinfo_for_iid(riid,&tinfo);
2180 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
2184 stub = CoTaskMemAlloc(sizeof(TMStubImpl));
2186 return E_OUTOFMEMORY;
2187 stub->lpvtbl = &tmstubvtbl;
2189 stub->tinfo = tinfo;
2190 stub->dispatch_stub = NULL;
2191 stub->dispatch_derivative = FALSE;
2192 memcpy(&(stub->iid),riid,sizeof(*riid));
2193 hres = IRpcStubBuffer_Connect((LPRPCSTUBBUFFER)stub,pUnkServer);
2194 *ppStub = (LPRPCSTUBBUFFER)stub;
2195 TRACE("IRpcStubBuffer: %p\n", stub);
2197 ERR("Connect to pUnkServer failed?\n");
2199 /* if we derive from IDispatch then defer to its stub for some of its methods */
2200 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
2203 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
2204 stub->dispatch_derivative = TRUE;
2205 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
2211 static const IPSFactoryBufferVtbl psfacbufvtbl = {
2212 PSFacBuf_QueryInterface,
2215 PSFacBuf_CreateProxy,
2219 /* This is the whole PSFactoryBuffer object, just the vtableptr */
2220 static const IPSFactoryBufferVtbl *lppsfac = &psfacbufvtbl;
2222 /***********************************************************************
2223 * TMARSHAL_DllGetClassObject
2225 HRESULT TMARSHAL_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
2227 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)) {
2231 return E_NOINTERFACE;