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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 #define NONAMELESSUNION
35 #define NONAMELESSSTRUCT
46 #include "wine/debug.h"
48 static const WCHAR riidW[5] = {'r','i','i','d',0};
49 static const WCHAR pdispparamsW[] = {'p','d','i','s','p','p','a','r','a','m','s',0};
50 static const WCHAR ppvObjectW[] = {'p','p','v','O','b','j','e','c','t',0};
51 static const WCHAR IDispatchW[] = { 'I','D','i','s','p','a','t','c','h',0};
52 static const WCHAR GetIDsOfNamesW[] = { 'G','e','t','I','D','s','O','f','N','a','m','e','s',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 typedef struct _marshal_state {
65 IID iid; /* HACK: for VT_VOID */
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_add(marshal_state *buf, LPBYTE stuff, DWORD size) {
78 while (buf->size - buf->curoff < size) {
81 buf->base = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,buf->base,buf->size);
85 buf->base = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,32);
91 memcpy(buf->base+buf->curoff,stuff,size);
97 xbuf_get(marshal_state *buf, LPBYTE stuff, DWORD size) {
98 if (buf->size < buf->curoff+size) return E_FAIL;
99 memcpy(stuff,buf->base+buf->curoff,size);
105 xbuf_skip(marshal_state *buf, DWORD size) {
106 if (buf->size < buf->curoff+size) return E_FAIL;
112 _unmarshal_interface(marshal_state *buf, REFIID riid, LPUNKNOWN *pUnk) {
114 ULARGE_INTEGER newpos;
115 LARGE_INTEGER seekto;
120 TRACE("...%s...\n",debugstr_guid(riid));
123 hres = xbuf_get(buf,(LPBYTE)&xsize,sizeof(xsize));
125 ERR("xbuf_get failed\n");
129 if (xsize == 0) return S_OK;
131 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
133 ERR("Stream create failed %lx\n",hres);
137 hres = IStream_Write(pStm,buf->base+buf->curoff,xsize,&res);
139 ERR("stream write %lx\n",hres);
143 memset(&seekto,0,sizeof(seekto));
144 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
146 ERR("Failed Seek %lx\n",hres);
150 hres = CoUnmarshalInterface(pStm,riid,(LPVOID*)pUnk);
152 ERR("Unmarshalling interface %s failed with %lx\n",debugstr_guid(riid),hres);
156 IStream_Release(pStm);
157 return xbuf_skip(buf,xsize);
161 _marshal_interface(marshal_state *buf, REFIID riid, LPUNKNOWN pUnk) {
162 LPUNKNOWN newiface = NULL;
163 LPBYTE tempbuf = NULL;
164 IStream *pStm = NULL;
166 ULARGE_INTEGER newpos;
167 LARGE_INTEGER seekto;
173 /* this is valid, if for instance we serialize
174 * a VT_DISPATCH with NULL ptr which apparently
175 * can happen. S_OK to make sure we continue
178 ERR("pUnk is NULL?\n");
180 return xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
185 TRACE("...%s...\n",debugstr_guid(riid));
186 hres = IUnknown_QueryInterface(pUnk,riid,(LPVOID*)&newiface);
188 WARN("%p does not support iface %s\n",pUnk,debugstr_guid(riid));
192 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
194 ERR("Stream create failed %lx\n",hres);
198 hres = CoMarshalInterface(pStm,riid,newiface,0,NULL,0);
200 ERR("Marshalling interface %s failed with %lx\n", debugstr_guid(riid), hres);
204 hres = IStream_Stat(pStm,&ststg,0);
206 ERR("Stream stat failed\n");
210 tempbuf = HeapAlloc(GetProcessHeap(), 0, ststg.cbSize.u.LowPart);
211 memset(&seekto,0,sizeof(seekto));
212 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
214 ERR("Failed Seek %lx\n",hres);
218 hres = IStream_Read(pStm,tempbuf,ststg.cbSize.u.LowPart,&res);
220 ERR("Failed Read %lx\n",hres);
224 xsize = ststg.cbSize.u.LowPart;
225 xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
226 hres = xbuf_add(buf,tempbuf,ststg.cbSize.u.LowPart);
228 HeapFree(GetProcessHeap(),0,tempbuf);
229 IUnknown_Release(newiface);
230 IStream_Release(pStm);
236 xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
237 if (pStm) IUnknown_Release(pStm);
238 if (newiface) IUnknown_Release(newiface);
239 HeapFree(GetProcessHeap(), 0, tempbuf);
243 /********************* OLE Proxy/Stub Factory ********************************/
244 static HRESULT WINAPI
245 PSFacBuf_QueryInterface(LPPSFACTORYBUFFER iface, REFIID iid, LPVOID *ppv) {
246 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)||IsEqualIID(iid,&IID_IUnknown)) {
247 *ppv = (LPVOID)iface;
248 /* No ref counting, static class */
251 FIXME("(%s) unknown IID?\n",debugstr_guid(iid));
252 return E_NOINTERFACE;
255 static ULONG WINAPI PSFacBuf_AddRef(LPPSFACTORYBUFFER iface) { return 2; }
256 static ULONG WINAPI PSFacBuf_Release(LPPSFACTORYBUFFER iface) { return 1; }
259 _get_typeinfo_for_iid(REFIID riid, ITypeInfo**ti) {
262 char tlguid[200],typelibkey[300],interfacekey[300],ver[100];
265 DWORD tlguidlen, verlen, type;
269 sprintf( interfacekey, "Interface\\{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\Typelib",
270 riid->Data1, riid->Data2, riid->Data3,
271 riid->Data4[0], riid->Data4[1], riid->Data4[2], riid->Data4[3],
272 riid->Data4[4], riid->Data4[5], riid->Data4[6], riid->Data4[7]
275 if (RegOpenKeyA(HKEY_CLASSES_ROOT,interfacekey,&ikey)) {
276 ERR("No %s key found.\n",interfacekey);
280 tlguidlen = sizeof(tlguid);
281 if (RegQueryValueExA(ikey,NULL,NULL,&type,(LPBYTE)tlguid,&tlguidlen)) {
282 ERR("Getting typelib guid failed.\n");
287 verlen = sizeof(ver);
288 if (RegQueryValueExA(ikey,"Version",NULL,&type,(LPBYTE)ver,&verlen)) {
289 ERR("Could not get version value?\n");
294 sprintf(typelibkey,"Typelib\\%s\\%s\\0\\win32",tlguid,ver);
295 tlfnlen = sizeof(tlfn);
296 if (RegQueryValueA(HKEY_CLASSES_ROOT,typelibkey,tlfn,&tlfnlen)) {
297 ERR("Could not get typelib fn?\n");
300 MultiByteToWideChar(CP_ACP, 0, tlfn, -1, tlfnW, -1);
301 hres = LoadTypeLib(tlfnW,&tl);
303 ERR("Failed to load typelib for %s, but it should be there.\n",debugstr_guid(riid));
306 hres = ITypeLib_GetTypeInfoOfGuid(tl,riid,ti);
308 ERR("typelib does not contain info for %s?\n",debugstr_guid(riid));
309 ITypeLib_Release(tl);
312 /* FIXME: do this? ITypeLib_Release(tl); */
316 /* Determine nr of functions. Since we use the toplevel interface and all
317 * inherited ones have lower numbers, we are ok to not to descent into
318 * the inheritance tree I think.
320 static int _nroffuncs(ITypeInfo *tinfo) {
327 hres = ITypeInfo_GetFuncDesc(tinfo,n,&fdesc);
330 if (fdesc->oVft/4 > max)
339 #include "pshpack1.h"
341 typedef struct _TMAsmProxy {
355 # error You need to implement stubless proxies for your architecture
358 typedef struct _TMProxyImpl {
360 const IRpcProxyBufferVtbl *lpvtbl2;
363 TMAsmProxy *asmstubs;
365 IRpcChannelBuffer* chanbuf;
367 CRITICAL_SECTION crit;
368 IUnknown *outerunknown;
371 static HRESULT WINAPI
372 TMProxyImpl_QueryInterface(LPRPCPROXYBUFFER iface, REFIID riid, LPVOID *ppv)
375 if (IsEqualIID(riid,&IID_IUnknown)||IsEqualIID(riid,&IID_IRpcProxyBuffer)) {
376 *ppv = (LPVOID)iface;
377 IRpcProxyBuffer_AddRef(iface);
380 FIXME("no interface for %s\n",debugstr_guid(riid));
381 return E_NOINTERFACE;
385 TMProxyImpl_AddRef(LPRPCPROXYBUFFER iface)
387 ICOM_THIS_MULTI(TMProxyImpl,lpvtbl2,iface);
388 ULONG refCount = InterlockedIncrement(&This->ref);
390 TRACE("(%p)->(ref before=%lu)\n",This, refCount - 1);
396 TMProxyImpl_Release(LPRPCPROXYBUFFER iface)
398 ICOM_THIS_MULTI(TMProxyImpl,lpvtbl2,iface);
399 ULONG refCount = InterlockedDecrement(&This->ref);
401 TRACE("(%p)->(ref before=%lu)\n",This, refCount + 1);
405 DeleteCriticalSection(&This->crit);
406 if (This->chanbuf) IRpcChannelBuffer_Release(This->chanbuf);
407 VirtualFree(This->asmstubs, 0, MEM_RELEASE);
413 static HRESULT WINAPI
415 LPRPCPROXYBUFFER iface,IRpcChannelBuffer* pRpcChannelBuffer)
417 ICOM_THIS_MULTI(TMProxyImpl, lpvtbl2, iface);
419 TRACE("(%p)\n", pRpcChannelBuffer);
421 EnterCriticalSection(&This->crit);
423 IRpcChannelBuffer_AddRef(pRpcChannelBuffer);
424 This->chanbuf = pRpcChannelBuffer;
426 LeaveCriticalSection(&This->crit);
432 TMProxyImpl_Disconnect(LPRPCPROXYBUFFER iface)
434 ICOM_THIS_MULTI(TMProxyImpl, lpvtbl2, iface);
438 EnterCriticalSection(&This->crit);
440 IRpcChannelBuffer_Release(This->chanbuf);
441 This->chanbuf = NULL;
443 LeaveCriticalSection(&This->crit);
447 static const IRpcProxyBufferVtbl tmproxyvtable = {
448 TMProxyImpl_QueryInterface,
452 TMProxyImpl_Disconnect
455 /* how much space do we use on stack in DWORD steps. */
460 return sizeof(double)/sizeof(DWORD);
462 return sizeof(CY)/sizeof(DWORD);
464 return sizeof(DATE)/sizeof(DWORD);
466 return (sizeof(VARIANT)+3)/sizeof(DWORD);
473 _xsize(TYPEDESC *td) {
478 return sizeof(VARIANT)+3;
481 ARRAYDESC *adesc = td->u.lpadesc;
483 for (i=0;i<adesc->cDims;i++)
484 arrsize *= adesc->rgbounds[i].cElements;
485 return arrsize*_xsize(&adesc->tdescElem);
510 TRACE("(tdesc.vt %d)\n",tdesc->vt);
513 case VT_EMPTY: /* nothing. empty variant for instance */
522 if (debugout) TRACE_(olerelay)("%lx",*arg);
524 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
529 if (debugout) TRACE_(olerelay)("%04lx",*arg & 0xffff);
531 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
536 if (debugout) TRACE_(olerelay)("%02lx",*arg & 0xff);
538 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
542 if (debugout) TRACE_(olerelay)("&0x%lx",*arg);
544 hres = xbuf_add(buf,(LPBYTE)(DWORD*)*arg,sizeof(DWORD));
545 /* do not dealloc at this time */
549 VARIANT *vt = (VARIANT*)arg;
550 DWORD vttype = V_VT(vt);
552 if (debugout) TRACE_(olerelay)("Vt(%ld)(",vttype);
555 hres = xbuf_add(buf,(LPBYTE)&vttype,sizeof(vttype));
556 if (hres) return hres;
558 /* need to recurse since we need to free the stuff */
559 hres = serialize_param(tinfo,writeit,debugout,dealloc,&tdesc2,(DWORD*)&(V_I4(vt)),buf);
560 if (debugout) TRACE_(olerelay)(")");
563 case VT_BSTR|VT_BYREF: {
564 if (debugout) TRACE_(olerelay)("[byref]'%s'", *(BSTR*)*arg ? relaystr(*((BSTR*)*arg)) : "<bstr NULL>");
566 /* ptr to ptr to magic widestring, basically */
567 BSTR *bstr = (BSTR *) *arg;
570 /* -1 means "null string" which is equivalent to empty string */
572 hres = xbuf_add(buf, (LPBYTE)&len,sizeof(DWORD));
573 if (hres) return hres;
575 len = *((DWORD*)*bstr-1)/sizeof(WCHAR);
576 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
577 if (hres) return hres;
578 hres = xbuf_add(buf,(LPBYTE)*bstr,len * sizeof(WCHAR));
579 if (hres) return hres;
583 if (dealloc && arg) {
584 BSTR *str = *((BSTR **)arg);
593 TRACE_(olerelay)("%s",relaystr((WCHAR*)*arg));
595 TRACE_(olerelay)("<bstr NULL>");
598 BSTR bstr = (BSTR)*arg;
602 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
603 if (hres) return hres;
605 len = *((DWORD*)bstr-1)/sizeof(WCHAR);
606 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
607 if (hres) return hres;
608 hres = xbuf_add(buf,(LPBYTE)bstr,len * sizeof(WCHAR));
609 if (hres) return hres;
614 SysFreeString((BSTR)*arg);
621 derefhere = (tdesc->u.lptdesc->vt != VT_USERDEFINED);
623 if (debugout) TRACE_(olerelay)("*");
624 /* Write always, so the other side knows when it gets a NULL pointer.
626 cookie = *arg ? 0x42424242 : 0;
627 hres = xbuf_add(buf,(LPBYTE)&cookie,sizeof(cookie));
631 if (debugout) TRACE_(olerelay)("NULL");
634 hres = serialize_param(tinfo,writeit,debugout,dealloc,tdesc->u.lptdesc,(DWORD*)*arg,buf);
635 if (derefhere && dealloc) HeapFree(GetProcessHeap(),0,(LPVOID)*arg);
639 if (debugout) TRACE_(olerelay)("unk(0x%lx)",*arg);
641 hres = _marshal_interface(buf,&IID_IUnknown,(LPUNKNOWN)*arg);
644 if (debugout) TRACE_(olerelay)("idisp(0x%lx)",*arg);
646 hres = _marshal_interface(buf,&IID_IDispatch,(LPUNKNOWN)*arg);
649 if (debugout) TRACE_(olerelay)("<void>");
651 case VT_USERDEFINED: {
655 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
657 ERR("Could not get typeinfo of hreftype %lx for VT_USERDEFINED.\n",tdesc->u.hreftype);
660 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
661 switch (tattr->typekind) {
663 case TKIND_INTERFACE:
665 hres=_marshal_interface(buf,&(tattr->guid),(LPUNKNOWN)arg);
669 if (debugout) TRACE_(olerelay)("{");
670 for (i=0;i<tattr->cVars;i++) {
675 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
677 ERR("Could not get vardesc of %d\n",i);
680 /* Need them for hack below */
682 memset(names,0,sizeof(names));
683 hres = ITypeInfo_GetNames(tinfo2,vdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames);
684 if (nrofnames > sizeof(names)/sizeof(names[0])) {
685 ERR("Need more names!\n");
687 if (!hres && debugout)
688 TRACE_(olerelay)("%s=",relaystr(names[0]));
690 elem2 = &vdesc->elemdescVar;
691 tdesc2 = &elem2->tdesc;
692 hres = serialize_param(
698 (DWORD*)(((LPBYTE)arg)+vdesc->u.oInst),
701 ITypeInfo_ReleaseVarDesc(tinfo2, vdesc);
704 if (debugout && (i<(tattr->cVars-1)))
705 TRACE_(olerelay)(",");
707 if (buf->thisisiid && (tattr->cbSizeInstance==sizeof(GUID)))
708 memcpy(&(buf->iid),arg,sizeof(buf->iid));
709 if (debugout) TRACE_(olerelay)("}");
713 return serialize_param(tinfo2,writeit,debugout,dealloc,&tattr->tdescAlias,arg,buf);
716 if (debugout) TRACE_(olerelay)("%lx",*arg);
718 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
721 FIXME("Unhandled typekind %d\n",tattr->typekind);
725 ITypeInfo_Release(tinfo2);
729 ARRAYDESC *adesc = tdesc->u.lpadesc;
732 if (debugout) TRACE_(olerelay)("carr");
733 for (i=0;i<adesc->cDims;i++) {
734 if (debugout) TRACE_(olerelay)("[%ld]",adesc->rgbounds[i].cElements);
735 arrsize *= adesc->rgbounds[i].cElements;
737 if (debugout) TRACE_(olerelay)("(vt %d)",adesc->tdescElem.vt);
738 if (debugout) TRACE_(olerelay)("[");
739 for (i=0;i<arrsize;i++) {
740 hres = serialize_param(tinfo, writeit, debugout, dealloc, &adesc->tdescElem, (DWORD*)((LPBYTE)arg+i*_xsize(&adesc->tdescElem)), buf);
743 if (debugout && (i<arrsize-1)) TRACE_(olerelay)(",");
745 if (debugout) TRACE_(olerelay)("]");
749 ERR("Unhandled marshal type %d.\n",tdesc->vt);
755 * HRESULT GetIDsOfNames(
756 * [in] REFIID riid, args[1]
757 * [in, size_is(cNames)] LPOLESTR *rgszNames, args[2]
758 * [in] UINT cNames, args[3]
759 * [in] LCID lcid, args[4]
760 * [out, size_is(cNames)] DISPID *rgDispId); args[5]
765 * LPOLESTR rgszNames[cNames];
766 * DWORD bytestrlen (incl 0)
767 * BYTE data[bytestrlen] (incl 0)
771 serialize_IDispatch_GetIDsOfNames(
778 DWORD cNames = args[2];
779 LPOLESTR *rgszNames = (LPOLESTR*)args[1];
783 if (debugout) TRACE_(olerelay)("riid=%s,",debugstr_guid((REFIID)args[0]));
784 hres = xbuf_add(buf, (LPBYTE)args[0], sizeof(IID));
786 FIXME("serialize of IID failed.\n");
789 if (debugout) TRACE_(olerelay)("cNames=%ld,",cNames);
790 hres = xbuf_add(buf, (LPBYTE)&cNames, sizeof(DWORD));
792 FIXME("serialize of cNames failed.\n");
795 if (debugout) TRACE_(olerelay)("rgszNames=[");
796 for (i=0;i<cNames;i++) {
797 DWORD len = 2*(lstrlenW(rgszNames[i])+1);
799 if (debugout) TRACE_(olerelay)("%s,",relaystr(rgszNames[i]));
800 hres = xbuf_add(buf, (LPBYTE)&len, sizeof(DWORD));
802 FIXME("serialize of len failed.\n");
805 hres = xbuf_add(buf, (LPBYTE)rgszNames[i], len);
807 FIXME("serialize of rgszNames[i] failed.\n");
811 if (debugout) TRACE_(olerelay)("],lcid=%04lx)",args[3]);
812 hres = xbuf_add(buf, (LPBYTE)&args[3], sizeof(DWORD));
814 FIXME("serialize of lcid failed.\n");
818 DISPID *rgDispId = (DISPID*)args[4];
820 hres = xbuf_add(buf, (LPBYTE)rgDispId, sizeof(DISPID) * cNames);
822 FIXME("serialize of rgDispId failed.\n");
826 TRACE_(olerelay)("riid=[in],rgszNames=[in],cNames=[in],rgDispId=[");
827 for (i=0;i<cNames;i++)
828 TRACE_(olerelay)("%08lx,",rgDispId[i]);
829 TRACE_(olerelay)("])");
831 HeapFree(GetProcessHeap(),0,(IID*)args[0]);
832 rgszNames = (LPOLESTR*)args[1];
833 for (i=0;i<cNames;i++) HeapFree(GetProcessHeap(),0,rgszNames[i]);
834 HeapFree(GetProcessHeap(),0,rgszNames);
835 HeapFree(GetProcessHeap(),0,rgDispId);
841 deserialize_IDispatch_GetIDsOfNames(
853 args[0] = (DWORD)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IID));
854 if (!args[0]) return E_FAIL;
855 hres = xbuf_get(buf, (LPBYTE)args[0], sizeof(IID));
857 FIXME("deserialize of IID failed.\n");
860 if (debugout) TRACE_(olerelay)("riid=%s,",debugstr_guid((REFIID)args[0]));
862 hres = xbuf_get(buf, (LPBYTE)&cNames, sizeof(DWORD));
864 FIXME("deserialize of cNames failed.\n");
868 if (debugout) TRACE_(olerelay)("cNames=%ld,",cNames);
869 if (debugout) TRACE_(olerelay)("rgszNames=[");
870 rgszNames = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(LPOLESTR) * cNames);
871 if (!rgszNames) return E_FAIL;
872 args[1] = (DWORD)rgszNames;
873 for (i=0;i<cNames;i++) {
876 hres = xbuf_get(buf, (LPBYTE)&len, sizeof(DWORD));
878 FIXME("serialize of len failed.\n");
881 rgszNames[i] = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
883 FIXME("heapalloc of %ld bytes failed\n", len);
886 hres = xbuf_get(buf, (LPBYTE)rgszNames[i], len);
888 FIXME("serialize of rgszNames[i] failed.\n");
891 if (debugout) TRACE_(olerelay)("%s,",relaystr(rgszNames[i]));
893 hres = xbuf_get(buf, (LPBYTE)&args[3], sizeof(DWORD));
895 FIXME("deserialize of lcid failed.\n");
898 if (debugout) TRACE_(olerelay)("],lcid=%04lx,rgDispId=[out])",args[3]);
899 args[4] = (DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DISPID) * cNames);
901 hres = xbuf_get(buf, (LPBYTE)args[4], sizeof(DISPID) * args[2]);
903 FIXME("serialize of rgDispId failed.\n");
907 TRACE_(olerelay)("dispid=[");
908 for (i=0;i<args[2];i++)
909 TRACE_(olerelay)("%08lx,",((DISPID*)args[4])[i]);
910 TRACE_(olerelay)("])");
917 serialize_LPVOID_ptr(
929 if ((tdesc->vt != VT_PTR) ||
930 (tdesc->u.lptdesc->vt != VT_PTR) ||
931 (tdesc->u.lptdesc->u.lptdesc->vt != VT_VOID)
933 FIXME("ppvObject not expressed as VT_PTR -> VT_PTR -> VT_VOID?\n");
936 cookie = (*(DWORD*)*arg) ? 0x42424242: 0x0;
938 hres = xbuf_add(buf, (LPVOID)&cookie, sizeof(cookie));
942 if (!*(DWORD*)*arg) {
943 if (debugout) TRACE_(olerelay)("<lpvoid NULL>");
947 TRACE_(olerelay)("ppv(%p)",*(LPUNKNOWN*)*arg);
949 hres = _marshal_interface(buf,&(buf->iid),*(LPUNKNOWN*)*arg);
954 HeapFree(GetProcessHeap(),0,(LPVOID)*arg);
959 serialize_DISPPARAM_ptr(
973 if ((tdesc->vt != VT_PTR) || (tdesc->u.lptdesc->vt != VT_USERDEFINED)) {
974 FIXME("DISPPARAMS not expressed as VT_PTR -> VT_USERDEFINED?\n");
978 cookie = *arg ? 0x42424242 : 0x0;
980 hres = xbuf_add(buf,(LPBYTE)&cookie,sizeof(cookie));
985 if (debugout) TRACE_(olerelay)("<DISPPARAMS NULL>");
988 disp = (DISPPARAMS*)*arg;
990 hres = xbuf_add(buf,(LPBYTE)&disp->cArgs,sizeof(disp->cArgs));
994 if (debugout) TRACE_(olerelay)("D{");
995 for (i=0;i<disp->cArgs;i++) {
998 vtdesc.vt = VT_VARIANT;
1005 (DWORD*)(disp->rgvarg+i),
1008 if (debugout && (i<disp->cArgs-1))
1009 TRACE_(olerelay)(",");
1012 HeapFree(GetProcessHeap(),0,disp->rgvarg);
1014 hres = xbuf_add(buf,(LPBYTE)&disp->cNamedArgs,sizeof(disp->cNamedArgs));
1018 if (debugout) TRACE_(olerelay)("}{");
1019 for (i=0;i<disp->cNamedArgs;i++) {
1022 vtdesc.vt = VT_UINT;
1029 (DWORD*)(disp->rgdispidNamedArgs+i),
1032 if (debugout && (i<disp->cNamedArgs-1))
1033 TRACE_(olerelay)(",");
1035 if (debugout) TRACE_(olerelay)("}");
1037 HeapFree(GetProcessHeap(),0,disp->rgdispidNamedArgs);
1038 HeapFree(GetProcessHeap(),0,disp);
1053 HRESULT hres = S_OK;
1055 TRACE("vt %d at %p\n",tdesc->vt,arg);
1058 switch (tdesc->vt) {
1060 if (debugout) TRACE_(olerelay)("<empty>");
1063 if (debugout) TRACE_(olerelay)("<null>");
1066 VARIANT *vt = (VARIANT*)arg;
1071 hres = xbuf_get(buf,(LPBYTE)&vttype,sizeof(vttype));
1073 FIXME("vt type not read?\n");
1076 memset(&tdesc2,0,sizeof(tdesc2));
1079 if (debugout) TRACE_(olerelay)("Vt(%ld)(",vttype);
1080 hres = deserialize_param(tinfo, readit, debugout, alloc, &tdesc2, (DWORD*)&(V_I4(vt)), buf);
1081 TRACE_(olerelay)(")");
1095 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
1096 if (hres) ERR("Failed to read integer 4 byte\n");
1098 if (debugout) TRACE_(olerelay)("%lx",*arg);
1104 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
1105 if (hres) ERR("Failed to read integer 4 byte\n");
1108 if (debugout) TRACE_(olerelay)("%04lx",*arg & 0xffff);
1114 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
1115 if (hres) ERR("Failed to read integer 4 byte\n");
1118 if (debugout) TRACE_(olerelay)("%02lx",*arg & 0xff);
1120 case VT_I4|VT_BYREF:
1123 *arg = (DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
1125 hres = xbuf_get(buf,(LPBYTE)*arg,sizeof(DWORD));
1126 if (hres) ERR("Failed to read integer 4 byte\n");
1128 if (debugout) TRACE_(olerelay)("&0x%lx",*(DWORD*)*arg);
1130 case VT_BSTR|VT_BYREF: {
1131 BSTR **bstr = (BSTR **)arg;
1136 hres = xbuf_get(buf,(LPBYTE)&len,sizeof(DWORD));
1138 ERR("failed to read bstr klen\n");
1142 *bstr = CoTaskMemAlloc(sizeof(BSTR *));
1144 if (debugout) TRACE_(olerelay)("<bstr NULL>");
1146 str = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(len+1)*sizeof(WCHAR));
1147 hres = xbuf_get(buf,(LPBYTE)str,len*sizeof(WCHAR));
1149 ERR("Failed to read BSTR.\n");
1152 *bstr = CoTaskMemAlloc(sizeof(BSTR *));
1153 **bstr = SysAllocStringLen(str,len);
1154 if (debugout) TRACE_(olerelay)("%s",relaystr(str));
1155 HeapFree(GetProcessHeap(),0,str);
1167 hres = xbuf_get(buf,(LPBYTE)&len,sizeof(DWORD));
1169 ERR("failed to read bstr klen\n");
1174 if (debugout) TRACE_(olerelay)("<bstr NULL>");
1176 str = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(len+1)*sizeof(WCHAR));
1177 hres = xbuf_get(buf,(LPBYTE)str,len*sizeof(WCHAR));
1179 ERR("Failed to read BSTR.\n");
1182 *arg = (DWORD)SysAllocStringLen(str,len);
1183 if (debugout) TRACE_(olerelay)("%s",relaystr(str));
1184 HeapFree(GetProcessHeap(),0,str);
1195 derefhere = (tdesc->u.lptdesc->vt != VT_USERDEFINED);
1196 /* read it in all cases, we need to know if we have
1197 * NULL pointer or not.
1199 hres = xbuf_get(buf,(LPBYTE)&cookie,sizeof(cookie));
1201 ERR("Failed to load pointer cookie.\n");
1204 if (cookie != 0x42424242) {
1205 /* we read a NULL ptr from the remote side */
1206 if (debugout) TRACE_(olerelay)("NULL");
1210 if (debugout) TRACE_(olerelay)("*");
1212 /* Allocate space for the referenced struct */
1214 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,_xsize(tdesc->u.lptdesc));
1217 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, (LPDWORD)*arg, buf);
1219 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, arg, buf);
1222 /* FIXME: UNKNOWN is unknown ..., but allocate 4 byte for it */
1224 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
1227 hres = _unmarshal_interface(buf,&IID_IUnknown,(LPUNKNOWN*)arg);
1229 TRACE_(olerelay)("unk(%p)",arg);
1234 hres = _unmarshal_interface(buf,&IID_IDispatch,(LPUNKNOWN*)arg);
1236 TRACE_(olerelay)("idisp(%p)",arg);
1239 if (debugout) TRACE_(olerelay)("<void>");
1241 case VT_USERDEFINED: {
1245 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
1247 ERR("Could not get typeinfo of hreftype %lx for VT_USERDEFINED.\n",tdesc->u.hreftype);
1250 hres = ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1252 ERR("Could not get typeattr in VT_USERDEFINED.\n");
1254 switch (tattr->typekind) {
1255 case TKIND_DISPATCH:
1256 case TKIND_INTERFACE:
1258 hres = _unmarshal_interface(buf,&(tattr->guid),(LPUNKNOWN*)arg);
1260 case TKIND_RECORD: {
1264 *arg = (DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,tattr->cbSizeInstance);
1266 if (debugout) TRACE_(olerelay)("{");
1267 for (i=0;i<tattr->cVars;i++) {
1270 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
1272 ERR("Could not get vardesc of %d\n",i);
1275 hres = deserialize_param(
1280 &vdesc->elemdescVar.tdesc,
1281 (DWORD*)(((LPBYTE)*arg)+vdesc->u.oInst),
1284 if (debugout && (i<tattr->cVars-1)) TRACE_(olerelay)(",");
1286 if (buf->thisisiid && (tattr->cbSizeInstance==sizeof(GUID)))
1287 memcpy(&(buf->iid),(LPBYTE)*arg,sizeof(buf->iid));
1288 if (debugout) TRACE_(olerelay)("}");
1292 return deserialize_param(tinfo2,readit,debugout,alloc,&tattr->tdescAlias,arg,buf);
1295 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
1296 if (hres) ERR("Failed to read enum (4 byte)\n");
1298 if (debugout) TRACE_(olerelay)("%lx",*arg);
1301 ERR("Unhandled typekind %d\n",tattr->typekind);
1307 ERR("failed to stuballoc in TKIND_RECORD.\n");
1308 ITypeInfo_Release(tinfo2);
1312 /* arg is pointing to the start of the array. */
1313 ARRAYDESC *adesc = tdesc->u.lpadesc;
1316 if (adesc->cDims > 1) FIXME("cDims > 1 in VT_CARRAY. Does it work?\n");
1317 for (i=0;i<adesc->cDims;i++)
1318 arrsize *= adesc->rgbounds[i].cElements;
1319 for (i=0;i<arrsize;i++)
1326 (DWORD*)((LPBYTE)(arg)+i*_xsize(&adesc->tdescElem)),
1332 ERR("No handler for VT type %d!\n",tdesc->vt);
1339 deserialize_LPVOID_ptr(
1351 if ((tdesc->vt != VT_PTR) ||
1352 (tdesc->u.lptdesc->vt != VT_PTR) ||
1353 (tdesc->u.lptdesc->u.lptdesc->vt != VT_VOID)
1355 FIXME("ppvObject not expressed as VT_PTR -> VT_PTR -> VT_VOID?\n");
1359 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(LPVOID));
1361 hres = xbuf_get(buf, (LPVOID)&cookie, sizeof(cookie));
1364 if (cookie != 0x42424242) {
1366 if (debugout) TRACE_(olerelay)("<lpvoid NULL>");
1371 hres = _unmarshal_interface(buf,&buf->iid,(LPUNKNOWN*)*arg);
1373 FIXME("_unmarshal_interface of %s , %p failed with %lx\n", debugstr_guid(&buf->iid), (LPUNKNOWN*)*arg, hres);
1377 if (debugout) TRACE_(olerelay)("ppv(%p)",(LPVOID)*arg);
1382 deserialize_DISPPARAM_ptr(
1396 if ((tdesc->vt != VT_PTR) || (tdesc->u.lptdesc->vt != VT_USERDEFINED)) {
1397 FIXME("DISPPARAMS not expressed as VT_PTR -> VT_USERDEFINED?\n");
1401 hres = xbuf_get(buf,(LPBYTE)&cookie,sizeof(cookie));
1406 if (debugout) TRACE_(olerelay)("<DISPPARAMS NULL>");
1411 *arg = (DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DISPPARAMS));
1412 disps = (DISPPARAMS*)*arg;
1415 hres = xbuf_get(buf, (LPBYTE)&disps->cArgs, sizeof(disps->cArgs));
1419 disps->rgvarg = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(VARIANT)*disps->cArgs);
1420 if (debugout) TRACE_(olerelay)("D{");
1421 for (i=0; i< disps->cArgs; i++) {
1424 vdesc.vt = VT_VARIANT;
1425 hres = deserialize_param(
1431 (DWORD*)(disps->rgvarg+i),
1435 if (debugout) TRACE_(olerelay)("}{");
1436 hres = xbuf_get(buf, (LPBYTE)&disps->cNamedArgs, sizeof(disps->cNamedArgs));
1439 if (disps->cNamedArgs) {
1441 disps->rgdispidNamedArgs = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DISPID)*disps->cNamedArgs);
1442 for (i=0; i< disps->cNamedArgs; i++) {
1446 hres = deserialize_param(
1452 (DWORD*)(disps->rgdispidNamedArgs+i),
1455 if (debugout && i<(disps->cNamedArgs-1)) TRACE_(olerelay)(",");
1458 if (debugout) TRACE_(olerelay)("}");
1462 /* Searches function, also in inherited interfaces */
1465 ITypeInfo *tinfo, int iMethod, ITypeInfo **tactual, FUNCDESC **fdesc, BSTR *iname, BSTR *fname)
1470 if (fname) *fname = NULL;
1471 if (iname) *iname = NULL;
1474 ITypeInfo_AddRef(*tactual);
1477 hres = ITypeInfo_GetFuncDesc(tinfo, i, fdesc);
1483 hres = ITypeInfo_GetTypeAttr(tinfo, &attr);
1485 ERR("GetTypeAttr failed with %lx\n",hres);
1488 /* Not found, so look in inherited ifaces. */
1489 for (j=0;j<attr->cImplTypes;j++) {
1490 hres = ITypeInfo_GetRefTypeOfImplType(tinfo, j, &href);
1492 ERR("Did not find a reftype for interface offset %d?\n",j);
1495 hres = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
1497 ERR("Did not find a typeinfo for reftype %ld?\n",href);
1500 hres = _get_funcdesc(tinfo2,iMethod,tactual,fdesc,iname,fname);
1501 ITypeInfo_Release(tinfo2);
1502 if (!hres) return S_OK;
1506 if (((*fdesc)->oVft/4) == iMethod) {
1508 ITypeInfo_GetDocumentation(tinfo,(*fdesc)->memid,fname,NULL,NULL,NULL);
1510 ITypeInfo_GetDocumentation(tinfo,-1,iname,NULL,NULL,NULL);
1518 xCall(LPVOID retptr, int method, TMProxyImpl *tpinfo /*, args */)
1520 DWORD *args = ((DWORD*)&tpinfo)+1, *xargs;
1523 int i, relaydeb = TRACE_ON(olerelay);
1530 int is_idispatch_getidsofnames = 0;
1531 DWORD remoteresult = 0;
1534 EnterCriticalSection(&tpinfo->crit);
1536 hres = _get_funcdesc(tpinfo->tinfo,method,&tinfo,&fdesc,&iname,&fname);
1538 ERR("Did not find typeinfo/funcdesc entry for method %d!\n",method);
1539 ITypeInfo_Release(tinfo);
1540 LeaveCriticalSection(&tpinfo->crit);
1544 if (!tpinfo->chanbuf)
1546 WARN("Tried to use disconnected proxy\n");
1547 ITypeInfo_Release(tinfo);
1548 LeaveCriticalSection(&tpinfo->crit);
1549 return RPC_E_DISCONNECTED;
1553 TRACE_(olerelay)("->");
1555 TRACE_(olerelay)("%s:",relaystr(iname));
1557 TRACE_(olerelay)("%s(%d)",relaystr(fname),method);
1559 TRACE_(olerelay)("%d",method);
1560 TRACE_(olerelay)("(");
1562 if (iname && fname && !lstrcmpW(iname,IDispatchW) && !lstrcmpW(fname,GetIDsOfNamesW))
1563 is_idispatch_getidsofnames = 1;
1565 if (iname) SysFreeString(iname);
1566 if (fname) SysFreeString(fname);
1568 memset(&buf,0,sizeof(buf));
1569 buf.iid = IID_IUnknown;
1571 /* Special IDispatch::GetIDsOfNames() serializer */
1572 if (is_idispatch_getidsofnames) {
1573 hres = serialize_IDispatch_GetIDsOfNames(TRUE,relaydeb,args,&buf);
1575 FIXME("serialize of IDispatch::GetIDsOfNames failed!\n");
1576 ITypeInfo_Release(tinfo);
1577 LeaveCriticalSection(&tpinfo->crit);
1580 goto afterserialize;
1583 /* special QueryInterface serialize */
1585 xbuf_add(&buf,(LPBYTE)args[0],sizeof(IID));
1586 if (relaydeb) TRACE_(olerelay)("riid=%s,[out])",debugstr_guid((REFIID)args[0]));
1587 goto afterserialize;
1590 /* normal typelib driven serializing */
1592 /* Need them for hack below */
1593 memset(names,0,sizeof(names));
1594 if (ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames))
1596 if (nrofnames > sizeof(names)/sizeof(names[0]))
1597 ERR("Need more names!\n");
1600 for (i=0;i<fdesc->cParams;i++) {
1601 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1602 BOOL isserialized = FALSE;
1604 if (i) TRACE_(olerelay)(",");
1605 if (i+1<nrofnames && names[i+1])
1606 TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1608 /* No need to marshal other data than FIN and any VT_PTR. */
1609 if (!(elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN) && (elem->tdesc.vt != VT_PTR)) {
1610 xargs+=_argsize(elem->tdesc.vt);
1611 if (relaydeb) TRACE_(olerelay)("[out]");
1614 if (((i+1)<nrofnames) && !IsBadStringPtrW(names[i+1],1)) {
1615 /* If the parameter is 'riid', we use it as interface IID
1616 * for a later ppvObject serialization.
1618 buf.thisisiid = !lstrcmpW(names[i+1],riidW);
1620 /* DISPPARAMS* needs special serializer */
1621 if (!lstrcmpW(names[i+1],pdispparamsW)) {
1622 hres = serialize_DISPPARAM_ptr(
1624 elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN,
1631 isserialized = TRUE;
1633 if (!lstrcmpW(names[i+1],ppvObjectW)) {
1634 hres = serialize_LPVOID_ptr(
1636 elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN,
1644 isserialized = TRUE;
1648 hres = serialize_param(
1650 elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN,
1659 ERR("Failed to serialize param, hres %lx\n",hres);
1662 xargs+=_argsize(elem->tdesc.vt);
1664 if (relaydeb) TRACE_(olerelay)(")");
1667 memset(&msg,0,sizeof(msg));
1668 msg.cbBuffer = buf.curoff;
1669 msg.iMethod = method;
1670 hres = IRpcChannelBuffer_GetBuffer(tpinfo->chanbuf,&msg,&(tpinfo->iid));
1672 ERR("RpcChannelBuffer GetBuffer failed, %lx\n",hres);
1673 LeaveCriticalSection(&tpinfo->crit);
1676 memcpy(msg.Buffer,buf.base,buf.curoff);
1677 if (relaydeb) TRACE_(olerelay)("\n");
1678 hres = IRpcChannelBuffer_SendReceive(tpinfo->chanbuf,&msg,&status);
1680 ERR("RpcChannelBuffer SendReceive failed, %lx\n",hres);
1681 LeaveCriticalSection(&tpinfo->crit);
1685 if (relaydeb) TRACE_(olerelay)(" status = %08lx (",status);
1687 buf.base = HeapReAlloc(GetProcessHeap(),0,buf.base,msg.cbBuffer);
1689 buf.base = HeapAlloc(GetProcessHeap(),0,msg.cbBuffer);
1690 buf.size = msg.cbBuffer;
1691 memcpy(buf.base,msg.Buffer,buf.size);
1694 /* Special IDispatch::GetIDsOfNames() deserializer */
1695 if (is_idispatch_getidsofnames) {
1696 hres = deserialize_IDispatch_GetIDsOfNames(FALSE,relaydeb,args,&buf);
1698 FIXME("deserialize of IDispatch::GetIDsOfNames failed!\n");
1701 goto after_deserialize;
1703 /* Special QueryInterface deserializer */
1705 _unmarshal_interface(&buf,(REFIID)args[0],(LPUNKNOWN*)args[1]);
1706 if (relaydeb) TRACE_(olerelay)("[in],%p",*((DWORD**)args[1]));
1707 goto after_deserialize;
1710 /* generic deserializer using typelib description */
1713 for (i=0;i<fdesc->cParams;i++) {
1714 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1715 BOOL isdeserialized = FALSE;
1718 if (i) TRACE_(olerelay)(",");
1719 if (i+1<nrofnames && names[i+1]) TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1721 /* No need to marshal other data than FOUT and any VT_PTR */
1722 if (!(elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT) && (elem->tdesc.vt != VT_PTR)) {
1723 xargs += _argsize(elem->tdesc.vt);
1724 if (relaydeb) TRACE_(olerelay)("[in]");
1727 if (((i+1)<nrofnames) && !IsBadStringPtrW(names[i+1],1)) {
1728 /* If the parameter is 'riid', we use it as interface IID
1729 * for a later ppvObject serialization.
1731 buf.thisisiid = !lstrcmpW(names[i+1],riidW);
1733 /* deserialize DISPPARAM */
1734 if (!lstrcmpW(names[i+1],pdispparamsW)) {
1735 hres = deserialize_DISPPARAM_ptr(
1737 elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT,
1745 ERR("Failed to deserialize DISPPARAM*, hres %lx\n",hres);
1748 isdeserialized = TRUE;
1750 if (!lstrcmpW(names[i+1],ppvObjectW)) {
1751 hres = deserialize_LPVOID_ptr(
1753 elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT,
1761 isdeserialized = TRUE;
1764 if (!isdeserialized)
1765 hres = deserialize_param(
1767 elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT,
1775 ERR("Failed to unmarshall param, hres %lx\n",hres);
1779 xargs += _argsize(elem->tdesc.vt);
1782 hres = xbuf_get(&buf, (LPBYTE)&remoteresult, sizeof(DWORD));
1785 if (relaydeb) TRACE_(olerelay)(") = %08lx\n", remoteresult);
1787 if (status != S_OK) /* OLE/COM internal error */
1790 HeapFree(GetProcessHeap(),0,buf.base);
1791 ITypeInfo_Release(tinfo);
1792 LeaveCriticalSection(&tpinfo->crit);
1793 return remoteresult;
1796 HRESULT WINAPI ProxyIUnknown_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
1798 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1800 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1802 if (proxy->outerunknown)
1803 return IUnknown_QueryInterface(proxy->outerunknown, riid, ppv);
1805 FIXME("No interface\n");
1806 return E_NOINTERFACE;
1809 ULONG WINAPI ProxyIUnknown_AddRef(IUnknown *iface)
1811 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1815 if (proxy->outerunknown)
1816 return IUnknown_AddRef(proxy->outerunknown);
1818 return 2; /* FIXME */
1821 ULONG WINAPI ProxyIUnknown_Release(IUnknown *iface)
1823 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1827 if (proxy->outerunknown)
1828 return IUnknown_Release(proxy->outerunknown);
1830 return 1; /* FIXME */
1833 static HRESULT WINAPI
1834 PSFacBuf_CreateProxy(
1835 LPPSFACTORYBUFFER iface, IUnknown* pUnkOuter, REFIID riid,
1836 IRpcProxyBuffer **ppProxy, LPVOID *ppv)
1844 TRACE("(...%s...)\n",debugstr_guid(riid));
1845 hres = _get_typeinfo_for_iid(riid,&tinfo);
1847 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
1850 nroffuncs = _nroffuncs(tinfo);
1851 proxy = CoTaskMemAlloc(sizeof(TMProxyImpl));
1852 if (!proxy) return E_OUTOFMEMORY;
1854 assert(sizeof(TMAsmProxy) == 12);
1856 proxy->outerunknown = pUnkOuter;
1857 proxy->asmstubs = VirtualAlloc(NULL, sizeof(TMAsmProxy) * nroffuncs, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
1858 if (!proxy->asmstubs) {
1859 ERR("Could not commit pages for proxy thunks\n");
1860 CoTaskMemFree(proxy);
1861 return E_OUTOFMEMORY;
1864 InitializeCriticalSection(&proxy->crit);
1866 proxy->lpvtbl = HeapAlloc(GetProcessHeap(),0,sizeof(LPBYTE)*nroffuncs);
1867 for (i=0;i<nroffuncs;i++) {
1868 TMAsmProxy *xasm = proxy->asmstubs+i;
1872 proxy->lpvtbl[i] = ProxyIUnknown_QueryInterface;
1875 proxy->lpvtbl[i] = ProxyIUnknown_AddRef;
1878 proxy->lpvtbl[i] = ProxyIUnknown_Release;
1882 /* nrofargs without This */
1885 hres = _get_funcdesc(tinfo,i,&tinfo2,&fdesc,NULL,NULL);
1886 ITypeInfo_Release(tinfo2);
1888 ERR("GetFuncDesc %lx should not fail here.\n",hres);
1891 /* some args take more than 4 byte on the stack */
1893 for (j=0;j<fdesc->cParams;j++)
1894 nrofargs += _argsize(fdesc->lprgelemdescParam[j].tdesc.vt);
1896 if (fdesc->callconv != CC_STDCALL) {
1897 ERR("calling convention is not stdcall????\n");
1900 /* popl %eax - return ptr
1907 * arg3 arg2 arg1 <method> <returnptr>
1909 xasm->popleax = 0x58;
1910 xasm->pushlval = 0x6a;
1912 xasm->pushleax = 0x50;
1913 xasm->lcall = 0xe8; /* relative jump */
1914 xasm->xcall = (DWORD)xCall;
1915 xasm->xcall -= (DWORD)&(xasm->lret);
1917 xasm->bytestopop= (nrofargs+2)*4; /* pop args, This, iMethod */
1918 proxy->lpvtbl[i] = xasm;
1923 proxy->lpvtbl2 = &tmproxyvtable;
1924 /* 1 reference for the proxy and 1 for the object */
1926 proxy->tinfo = tinfo;
1927 memcpy(&proxy->iid,riid,sizeof(*riid));
1929 *ppv = (LPVOID)proxy;
1930 *ppProxy = (IRpcProxyBuffer *)&(proxy->lpvtbl2);
1934 typedef struct _TMStubImpl {
1935 const IRpcStubBufferVtbl *lpvtbl;
1943 static HRESULT WINAPI
1944 TMStubImpl_QueryInterface(LPRPCSTUBBUFFER iface, REFIID riid, LPVOID *ppv)
1946 if (IsEqualIID(riid,&IID_IRpcStubBuffer)||IsEqualIID(riid,&IID_IUnknown)){
1947 *ppv = (LPVOID)iface;
1948 IRpcStubBuffer_AddRef(iface);
1951 FIXME("%s, not supported IID.\n",debugstr_guid(riid));
1952 return E_NOINTERFACE;
1956 TMStubImpl_AddRef(LPRPCSTUBBUFFER iface)
1958 TMStubImpl *This = (TMStubImpl *)iface;
1959 ULONG refCount = InterlockedIncrement(&This->ref);
1961 TRACE("(%p)->(ref before=%lu)\n", This, refCount - 1);
1967 TMStubImpl_Release(LPRPCSTUBBUFFER iface)
1969 TMStubImpl *This = (TMStubImpl *)iface;
1970 ULONG refCount = InterlockedDecrement(&This->ref);
1972 TRACE("(%p)->(ref before=%lu)\n", This, refCount + 1);
1976 IRpcStubBuffer_Disconnect(iface);
1977 ITypeInfo_Release(This->tinfo);
1978 CoTaskMemFree(This);
1983 static HRESULT WINAPI
1984 TMStubImpl_Connect(LPRPCSTUBBUFFER iface, LPUNKNOWN pUnkServer)
1986 TMStubImpl *This = (TMStubImpl *)iface;
1988 TRACE("(%p)->(%p)\n", This, pUnkServer);
1990 IUnknown_AddRef(pUnkServer);
1991 This->pUnk = pUnkServer;
1996 TMStubImpl_Disconnect(LPRPCSTUBBUFFER iface)
1998 TMStubImpl *This = (TMStubImpl *)iface;
2000 TRACE("(%p)->()\n", This);
2004 IUnknown_Release(This->pUnk);
2009 static HRESULT WINAPI
2011 LPRPCSTUBBUFFER iface, RPCOLEMESSAGE* xmsg,IRpcChannelBuffer*rpcchanbuf)
2015 TMStubImpl *This = (TMStubImpl *)iface;
2017 DWORD *args, res, *xargs, nrofargs;
2021 BSTR fname = NULL,iname = NULL;
2022 BOOL is_idispatch_getidsofnames = 0;
2025 memset(&buf,0,sizeof(buf));
2026 buf.size = xmsg->cbBuffer;
2027 buf.base = HeapAlloc(GetProcessHeap(), 0, xmsg->cbBuffer);
2028 memcpy(buf.base, xmsg->Buffer, xmsg->cbBuffer);
2030 buf.iid = IID_IUnknown;
2033 if (xmsg->iMethod == 0) { /* QI */
2035 /* in: IID, out: <iface> */
2037 xbuf_get(&buf,(LPBYTE)&xiid,sizeof(xiid));
2039 hres = _marshal_interface(&buf,&xiid,This->pUnk);
2040 xmsg->Buffer = buf.base; /* Might have been reallocated */
2041 xmsg->cbBuffer = buf.size;
2044 hres = _get_funcdesc(This->tinfo,xmsg->iMethod,&tinfo,&fdesc,&iname,&fname);
2046 ERR("GetFuncDesc on method %ld failed with %lx\n",xmsg->iMethod,hres);
2050 if (iname && fname && !lstrcmpW(iname, IDispatchW) && !lstrcmpW(fname, GetIDsOfNamesW))
2051 is_idispatch_getidsofnames = 1;
2053 if (iname) SysFreeString (iname);
2054 if (fname) SysFreeString (fname);
2056 /* Need them for hack below */
2057 memset(names,0,sizeof(names));
2058 ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames);
2059 if (nrofnames > sizeof(names)/sizeof(names[0])) {
2060 ERR("Need more names!\n");
2063 /*dump_FUNCDESC(fdesc);*/
2065 for (i=0;i<fdesc->cParams;i++)
2066 nrofargs += _argsize(fdesc->lprgelemdescParam[i].tdesc.vt);
2067 args = HeapAlloc(GetProcessHeap(),0,(nrofargs+1)*sizeof(DWORD));
2068 if (!args) return E_OUTOFMEMORY;
2070 if (is_idispatch_getidsofnames) {
2071 hres = deserialize_IDispatch_GetIDsOfNames(TRUE,FALSE,args+1,&buf);
2073 FIXME("deserialize_IDispatch_GetIDsOfNames failed!\n");
2077 goto afterdeserialize;
2080 /* Allocate all stuff used by call. */
2082 for (i=0;i<fdesc->cParams;i++) {
2083 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2084 BOOL isdeserialized = FALSE;
2086 if (((i+1)<nrofnames) && !IsBadStringPtrW(names[i+1],1)) {
2087 /* If the parameter is 'riid', we use it as interface IID
2088 * for a later ppvObject serialization.
2090 buf.thisisiid = !lstrcmpW(names[i+1],riidW);
2092 /* deserialize DISPPARAM */
2093 if (!lstrcmpW(names[i+1],pdispparamsW)) {
2094 hres = deserialize_DISPPARAM_ptr(
2096 elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN,
2104 ERR("Failed to deserialize DISPPARAM*, hres %lx\n",hres);
2107 isdeserialized = TRUE;
2109 if (!lstrcmpW(names[i+1],ppvObjectW)) {
2110 hres = deserialize_LPVOID_ptr(
2112 elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN,
2120 isdeserialized = TRUE;
2123 if (!isdeserialized)
2124 hres = deserialize_param(
2126 elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN,
2133 xargs += _argsize(elem->tdesc.vt);
2135 ERR("Failed to deserialize param %s, hres %lx\n",relaystr(names[i+1]),hres);
2140 hres = IUnknown_QueryInterface(This->pUnk,&(This->iid),(LPVOID*)&(args[0]));
2142 ERR("Does not support iface %s, returning %lx\n",debugstr_guid(&(This->iid)), hres);
2146 (*((FARPROC**)args[0]))[fdesc->oVft/4],
2151 IUnknown_Release((LPUNKNOWN)args[0]);
2154 /* special IDispatch::GetIDsOfNames serializer */
2155 if (is_idispatch_getidsofnames) {
2156 hres = serialize_IDispatch_GetIDsOfNames(FALSE,FALSE,args+1,&buf);
2158 FIXME("serialize of IDispatch::GetIDsOfNames failed!\n");
2161 goto afterserialize;
2164 for (i=0;i<fdesc->cParams;i++) {
2165 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2166 BOOL isserialized = FALSE;
2168 if (((i+1)<nrofnames) && !IsBadStringPtrW(names[i+1],1)) {
2169 /* If the parameter is 'riid', we use it as interface IID
2170 * for a later ppvObject serialization.
2172 buf.thisisiid = !lstrcmpW(names[i+1],riidW);
2174 /* DISPPARAMS* needs special serializer */
2175 if (!lstrcmpW(names[i+1],pdispparamsW)) {
2176 hres = serialize_DISPPARAM_ptr(
2178 elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT,
2185 isserialized = TRUE;
2187 if (!lstrcmpW(names[i+1],ppvObjectW)) {
2188 hres = serialize_LPVOID_ptr(
2190 elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT,
2198 isserialized = TRUE;
2202 hres = serialize_param(
2204 elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT,
2211 xargs += _argsize(elem->tdesc.vt);
2213 ERR("Failed to stuballoc param, hres %lx\n",hres);
2218 hres = xbuf_add (&buf, (LPBYTE)&res, sizeof(DWORD));
2222 ITypeInfo_Release(tinfo);
2223 xmsg->cbBuffer = buf.curoff;
2224 I_RpcGetBuffer((RPC_MESSAGE *)xmsg);
2225 memcpy(xmsg->Buffer, buf.base, buf.curoff);
2226 HeapFree(GetProcessHeap(),0,args);
2230 static LPRPCSTUBBUFFER WINAPI
2231 TMStubImpl_IsIIDSupported(LPRPCSTUBBUFFER iface, REFIID riid) {
2232 FIXME("Huh (%s)?\n",debugstr_guid(riid));
2237 TMStubImpl_CountRefs(LPRPCSTUBBUFFER iface) {
2238 TMStubImpl *This = (TMStubImpl *)iface;
2240 return This->ref; /*FIXME? */
2243 static HRESULT WINAPI
2244 TMStubImpl_DebugServerQueryInterface(LPRPCSTUBBUFFER iface, LPVOID *ppv) {
2249 TMStubImpl_DebugServerRelease(LPRPCSTUBBUFFER iface, LPVOID ppv) {
2253 static const IRpcStubBufferVtbl tmstubvtbl = {
2254 TMStubImpl_QueryInterface,
2258 TMStubImpl_Disconnect,
2260 TMStubImpl_IsIIDSupported,
2261 TMStubImpl_CountRefs,
2262 TMStubImpl_DebugServerQueryInterface,
2263 TMStubImpl_DebugServerRelease
2266 static HRESULT WINAPI
2267 PSFacBuf_CreateStub(
2268 LPPSFACTORYBUFFER iface, REFIID riid,IUnknown *pUnkServer,
2269 IRpcStubBuffer** ppStub
2275 TRACE("(%s,%p,%p)\n",debugstr_guid(riid),pUnkServer,ppStub);
2276 hres = _get_typeinfo_for_iid(riid,&tinfo);
2278 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
2281 stub = CoTaskMemAlloc(sizeof(TMStubImpl));
2283 return E_OUTOFMEMORY;
2284 stub->lpvtbl = &tmstubvtbl;
2286 stub->tinfo = tinfo;
2287 memcpy(&(stub->iid),riid,sizeof(*riid));
2288 hres = IRpcStubBuffer_Connect((LPRPCSTUBBUFFER)stub,pUnkServer);
2289 *ppStub = (LPRPCSTUBBUFFER)stub;
2290 TRACE("IRpcStubBuffer: %p\n", stub);
2292 ERR("Connect to pUnkServer failed?\n");
2296 static const IPSFactoryBufferVtbl psfacbufvtbl = {
2297 PSFacBuf_QueryInterface,
2300 PSFacBuf_CreateProxy,
2304 /* This is the whole PSFactoryBuffer object, just the vtableptr */
2305 static const IPSFactoryBufferVtbl *lppsfac = &psfacbufvtbl;
2307 /***********************************************************************
2308 * TMARSHAL_DllGetClassObject
2310 HRESULT TMARSHAL_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
2312 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)) {
2316 return E_NOINTERFACE;