4 * Copyright 2002 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};
52 WINE_DEFAULT_DEBUG_CHANNEL(ole);
53 WINE_DECLARE_DEBUG_CHANNEL(olerelay);
55 #define ICOM_THIS_MULTI(impl,field,iface) impl* const This=(impl*)((char*)(iface) - offsetof(impl,field))
57 typedef struct _marshal_state {
63 IID iid; /* HACK: for VT_VOID */
66 /* used in the olerelay code to avoid having the L"" stuff added by debugstr_w */
67 static char *relaystr(WCHAR *in) {
68 char *tmp = (char *)debugstr_w(in);
70 tmp[strlen(tmp)-1] = '\0';
75 xbuf_add(marshal_state *buf, LPBYTE stuff, DWORD size) {
76 while (buf->size - buf->curoff < size) {
79 buf->base = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,buf->base,buf->size);
83 buf->base = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,32);
89 memcpy(buf->base+buf->curoff,stuff,size);
95 xbuf_get(marshal_state *buf, LPBYTE stuff, DWORD size) {
96 if (buf->size < buf->curoff+size) return E_FAIL;
97 memcpy(stuff,buf->base+buf->curoff,size);
103 xbuf_skip(marshal_state *buf, DWORD size) {
104 if (buf->size < buf->curoff+size) return E_FAIL;
110 _unmarshal_interface(marshal_state *buf, REFIID riid, LPUNKNOWN *pUnk) {
112 ULARGE_INTEGER newpos;
113 LARGE_INTEGER seekto;
118 TRACE("...%s...\n",debugstr_guid(riid));
120 hres = xbuf_get(buf,(LPBYTE)&xsize,sizeof(xsize));
121 if (hres) return hres;
122 if (xsize == 0) return S_OK;
123 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
125 FIXME("Stream create failed %lx\n",hres);
128 hres = IStream_Write(pStm,buf->base+buf->curoff,xsize,&res);
129 if (hres) { FIXME("stream write %lx\n",hres); return hres; }
130 memset(&seekto,0,sizeof(seekto));
131 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
132 if (hres) { FIXME("Failed Seek %lx\n",hres); return hres;}
133 hres = CoUnmarshalInterface(pStm,riid,(LPVOID*)pUnk);
135 FIXME("Marshalling interface %s failed with %lx\n",debugstr_guid(riid),hres);
138 IStream_Release(pStm);
139 return xbuf_skip(buf,xsize);
143 _marshal_interface(marshal_state *buf, REFIID riid, LPUNKNOWN pUnk) {
148 ULARGE_INTEGER newpos;
149 LARGE_INTEGER seekto;
158 TRACE("...%s...\n",debugstr_guid(riid));
159 hres=IUnknown_QueryInterface(pUnk,riid,(LPVOID*)&newiface);
161 TRACE("%p does not support iface %s\n",pUnk,debugstr_guid(riid));
164 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
166 FIXME("Stream create failed %lx\n",hres);
169 hres = CoMarshalInterface(pStm,riid,newiface,0,NULL,0);
170 IUnknown_Release(newiface);
172 FIXME("Marshalling interface %s failed with %lx\n",
173 debugstr_guid(riid),hres
177 hres = IStream_Stat(pStm,&ststg,0);
178 tempbuf = HeapAlloc(GetProcessHeap(), 0, ststg.cbSize.u.LowPart);
179 memset(&seekto,0,sizeof(seekto));
180 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
181 if (hres) { FIXME("Failed Seek %lx\n",hres); goto fail;}
182 hres = IStream_Read(pStm,tempbuf,ststg.cbSize.u.LowPart,&res);
183 if (hres) { FIXME("Failed Read %lx\n",hres); goto fail;}
184 IStream_Release(pStm);
185 xsize = ststg.cbSize.u.LowPart;
186 xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
187 hres = xbuf_add(buf,tempbuf,ststg.cbSize.u.LowPart);
188 HeapFree(GetProcessHeap(),0,tempbuf);
192 xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
196 /********************* OLE Proxy/Stub Factory ********************************/
197 static HRESULT WINAPI
198 PSFacBuf_QueryInterface(LPPSFACTORYBUFFER iface, REFIID iid, LPVOID *ppv) {
199 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)||IsEqualIID(iid,&IID_IUnknown)) {
200 *ppv = (LPVOID)iface;
201 /* No ref counting, static class */
204 FIXME("(%s) unknown IID?\n",debugstr_guid(iid));
205 return E_NOINTERFACE;
208 static ULONG WINAPI PSFacBuf_AddRef(LPPSFACTORYBUFFER iface) { return 2; }
209 static ULONG WINAPI PSFacBuf_Release(LPPSFACTORYBUFFER iface) { return 1; }
212 _get_typeinfo_for_iid(REFIID riid, ITypeInfo**ti) {
215 char tlguid[200],typelibkey[300],interfacekey[300],ver[100];
218 DWORD tlguidlen, verlen, type, tlfnlen;
221 sprintf( interfacekey, "Interface\\{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\Typelib",
222 riid->Data1, riid->Data2, riid->Data3,
223 riid->Data4[0], riid->Data4[1], riid->Data4[2], riid->Data4[3],
224 riid->Data4[4], riid->Data4[5], riid->Data4[6], riid->Data4[7]
227 if (RegOpenKeyA(HKEY_CLASSES_ROOT,interfacekey,&ikey)) {
228 FIXME("No %s key found.\n",interfacekey);
232 tlguidlen = sizeof(tlguid);
233 if (RegQueryValueExA(ikey,NULL,NULL,&type,tlguid,&tlguidlen)) {
234 FIXME("Getting typelib guid failed.\n");
239 verlen = sizeof(ver);
240 if (RegQueryValueExA(ikey,"Version",NULL,&type,ver,&verlen)) {
241 FIXME("Could not get version value?\n");
246 sprintf(typelibkey,"Typelib\\%s\\%s\\0\\win32",tlguid,ver);
247 tlfnlen = sizeof(tlfn);
248 if (RegQueryValueA(HKEY_CLASSES_ROOT,typelibkey,tlfn,&tlfnlen)) {
249 FIXME("Could not get typelib fn?\n");
252 MultiByteToWideChar(CP_ACP, 0, tlfn, -1, tlfnW, -1);
253 hres = LoadTypeLib(tlfnW,&tl);
255 ERR("Failed to load typelib for %s, but it should be there.\n",debugstr_guid(riid));
258 hres = ITypeLib_GetTypeInfoOfGuid(tl,riid,ti);
260 ERR("typelib does not contain info for %s?\n",debugstr_guid(riid));
261 ITypeLib_Release(tl);
264 /* FIXME: do this? ITypeLib_Release(tl); */
268 /* Determine nr of functions. Since we use the toplevel interface and all
269 * inherited ones have lower numbers, we are ok to not to descent into
270 * the inheritance tree I think.
272 static int _nroffuncs(ITypeInfo *tinfo) {
279 hres = ITypeInfo_GetFuncDesc(tinfo,n,&fdesc);
282 if (fdesc->oVft/4 > max)
291 #include "pshpack1.h"
293 typedef struct _TMAsmProxy {
307 # error You need to implement stubless proxies for your architecture
310 typedef struct _TMProxyImpl {
312 IRpcProxyBufferVtbl *lpvtbl2;
315 TMAsmProxy *asmstubs;
317 IRpcChannelBuffer* chanbuf;
319 CRITICAL_SECTION crit;
322 static HRESULT WINAPI
323 TMProxyImpl_QueryInterface(LPRPCPROXYBUFFER iface, REFIID riid, LPVOID *ppv)
326 if (IsEqualIID(riid,&IID_IUnknown)||IsEqualIID(riid,&IID_IRpcProxyBuffer)) {
327 *ppv = (LPVOID)iface;
328 IRpcProxyBuffer_AddRef(iface);
331 FIXME("no interface for %s\n",debugstr_guid(riid));
332 return E_NOINTERFACE;
336 TMProxyImpl_AddRef(LPRPCPROXYBUFFER iface)
338 ICOM_THIS_MULTI(TMProxyImpl,lpvtbl2,iface);
339 ULONG refCount = InterlockedIncrement(&This->ref);
341 TRACE("(%p)->(ref before=%lu)\n",This, refCount - 1);
347 TMProxyImpl_Release(LPRPCPROXYBUFFER iface)
349 ICOM_THIS_MULTI(TMProxyImpl,lpvtbl2,iface);
350 ULONG refCount = InterlockedDecrement(&This->ref);
352 TRACE("(%p)->(ref before=%lu)\n",This, refCount + 1);
356 DeleteCriticalSection(&This->crit);
357 if (This->chanbuf) IRpcChannelBuffer_Release(This->chanbuf);
358 VirtualFree(This->asmstubs, 0, MEM_RELEASE);
364 static HRESULT WINAPI
366 LPRPCPROXYBUFFER iface,IRpcChannelBuffer* pRpcChannelBuffer)
368 ICOM_THIS_MULTI(TMProxyImpl, lpvtbl2, iface);
370 TRACE("(%p)\n", pRpcChannelBuffer);
372 EnterCriticalSection(&This->crit);
374 IRpcChannelBuffer_AddRef(pRpcChannelBuffer);
375 This->chanbuf = pRpcChannelBuffer;
377 LeaveCriticalSection(&This->crit);
383 TMProxyImpl_Disconnect(LPRPCPROXYBUFFER iface)
385 ICOM_THIS_MULTI(TMProxyImpl, lpvtbl2, iface);
389 EnterCriticalSection(&This->crit);
391 IRpcChannelBuffer_Release(This->chanbuf);
392 This->chanbuf = NULL;
394 LeaveCriticalSection(&This->crit);
398 static IRpcProxyBufferVtbl tmproxyvtable = {
399 TMProxyImpl_QueryInterface,
403 TMProxyImpl_Disconnect
406 /* how much space do we use on stack in DWORD steps. */
411 return sizeof(DATE)/sizeof(DWORD);
413 return (sizeof(VARIANT)+3)/sizeof(DWORD);
420 _xsize(TYPEDESC *td) {
425 return sizeof(VARIANT)+3;
428 ARRAYDESC *adesc = td->u.lpadesc;
430 for (i=0;i<adesc->cDims;i++)
431 arrsize *= adesc->rgbounds[i].cElements;
432 return arrsize*_xsize(&adesc->tdescElem);
457 TRACE("(tdesc.vt %d)\n",tdesc->vt);
460 case VT_EMPTY: /* nothing. empty variant for instance */
471 if (debugout) TRACE_(olerelay)("%lx",*arg);
473 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
477 VARIANT *vt = (VARIANT*)arg;
478 DWORD vttype = V_VT(vt);
480 if (debugout) TRACE_(olerelay)("Vt(%ld)(",vttype);
483 hres = xbuf_add(buf,(LPBYTE)&vttype,sizeof(vttype));
484 if (hres) return hres;
486 /* need to recurse since we need to free the stuff */
487 hres = serialize_param(tinfo,writeit,debugout,dealloc,&tdesc2,&(V_I4(vt)),buf);
488 if (debugout) TRACE_(olerelay)(")");
494 TRACE_(olerelay)("%s",relaystr((BSTR)*arg));
496 TRACE_(olerelay)("<bstr NULL>");
501 hres = xbuf_add(buf,(LPBYTE)&fakelen,4);
505 DWORD *bstr = ((DWORD*)(*arg))-1;
507 hres = xbuf_add(buf,(LPBYTE)bstr,bstr[0]+4);
514 SysFreeString((BSTR)*arg);
520 if (debugout) TRACE_(olerelay)("*");
522 cookie = *arg ? 0x42424242 : 0;
523 hres = xbuf_add(buf,(LPBYTE)&cookie,sizeof(cookie));
528 if (debugout) TRACE_(olerelay)("NULL");
531 hres = serialize_param(tinfo,writeit,debugout,dealloc,tdesc->u.lptdesc,(DWORD*)*arg,buf);
532 if (dealloc) HeapFree(GetProcessHeap(),0,(LPVOID)arg);
536 if (debugout) TRACE_(olerelay)("unk(0x%lx)",*arg);
538 hres = _marshal_interface(buf,&IID_IUnknown,(LPUNKNOWN)*arg);
541 if (debugout) TRACE_(olerelay)("idisp(0x%lx)",*arg);
543 hres = _marshal_interface(buf,&IID_IDispatch,(LPUNKNOWN)*arg);
546 if (debugout) TRACE_(olerelay)("<void>");
548 case VT_USERDEFINED: {
552 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
554 FIXME("Could not get typeinfo of hreftype %lx for VT_USERDEFINED.\n",tdesc->u.hreftype);
557 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
558 switch (tattr->typekind) {
560 case TKIND_INTERFACE:
562 hres=_marshal_interface(buf,&(tattr->guid),(LPUNKNOWN)arg);
566 if (debugout) TRACE_(olerelay)("{");
567 for (i=0;i<tattr->cVars;i++) {
572 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
574 FIXME("Could not get vardesc of %d\n",i);
577 /* Need them for hack below */
579 memset(names,0,sizeof(names));
580 hres = ITypeInfo_GetNames(tinfo2,vdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames);
581 if (nrofnames > sizeof(names)/sizeof(names[0])) {
582 ERR("Need more names!\n");
584 if (!hres && debugout)
585 TRACE_(olerelay)("%s=",relaystr(names[0]));
587 elem2 = &vdesc->elemdescVar;
588 tdesc2 = &elem2->tdesc;
589 hres = serialize_param(
595 (DWORD*)(((LPBYTE)arg)+vdesc->u.oInst),
600 if (debugout && (i<(tattr->cVars-1)))
601 TRACE_(olerelay)(",");
603 if (buf->thisisiid && (tattr->cbSizeInstance==sizeof(GUID)))
604 memcpy(&(buf->iid),arg,sizeof(buf->iid));
605 if (debugout) TRACE_(olerelay)("}");
609 FIXME("Unhandled typekind %d\n",tattr->typekind);
613 ITypeInfo_Release(tinfo2);
617 ARRAYDESC *adesc = tdesc->u.lpadesc;
620 if (debugout) TRACE_(olerelay)("carr");
621 for (i=0;i<adesc->cDims;i++) {
622 if (debugout) TRACE_(olerelay)("[%ld]",adesc->rgbounds[i].cElements);
623 arrsize *= adesc->rgbounds[i].cElements;
625 if (debugout) TRACE_(olerelay)("[");
626 for (i=0;i<arrsize;i++) {
627 hres = serialize_param(tinfo, writeit, debugout, dealloc, &adesc->tdescElem, (DWORD*)((LPBYTE)arg+i*_xsize(&adesc->tdescElem)), buf);
630 if (debugout && (i<arrsize-1)) TRACE_(olerelay)(",");
632 if (debugout) TRACE_(olerelay)("]");
636 ERR("Unhandled marshal type %d.\n",tdesc->vt);
642 serialize_LPVOID_ptr(
654 if ((tdesc->vt != VT_PTR) ||
655 (tdesc->u.lptdesc->vt != VT_PTR) ||
656 (tdesc->u.lptdesc->u.lptdesc->vt != VT_VOID)
658 FIXME("ppvObject not expressed as VT_PTR -> VT_PTR -> VT_VOID?\n");
661 cookie = (*arg) ? 0x42424242: 0x0;
663 hres = xbuf_add(buf, (LPVOID)&cookie, sizeof(cookie));
668 if (debugout) TRACE_(olerelay)("<lpvoid NULL>");
672 TRACE_(olerelay)("ppv(%p)",*(LPUNKNOWN*)*arg);
674 hres = _marshal_interface(buf,&(buf->iid),*(LPUNKNOWN*)*arg);
679 HeapFree(GetProcessHeap(),0,(LPVOID)*arg);
684 serialize_DISPPARAM_ptr(
698 if ((tdesc->vt != VT_PTR) || (tdesc->u.lptdesc->vt != VT_USERDEFINED)) {
699 FIXME("DISPPARAMS not expressed as VT_PTR -> VT_USERDEFINED?\n");
703 cookie = *arg ? 0x42424242 : 0x0;
705 hres = xbuf_add(buf,(LPBYTE)&cookie,sizeof(cookie));
710 if (debugout) TRACE_(olerelay)("<DISPPARAMS NULL>");
713 disp = (DISPPARAMS*)*arg;
715 hres = xbuf_add(buf,(LPBYTE)&disp->cArgs,sizeof(disp->cArgs));
719 if (debugout) TRACE_(olerelay)("D{");
720 for (i=0;i<disp->cArgs;i++) {
723 vtdesc.vt = VT_VARIANT;
730 (DWORD*)(disp->rgvarg+i),
733 if (debugout && (i<disp->cArgs-1))
734 TRACE_(olerelay)(",");
737 HeapFree(GetProcessHeap(),0,disp->rgvarg);
739 hres = xbuf_add(buf,(LPBYTE)&disp->cNamedArgs,sizeof(disp->cNamedArgs));
743 if (debugout) TRACE_(olerelay)("}{");
744 for (i=0;i<disp->cNamedArgs;i++) {
754 (DWORD*)(disp->rgdispidNamedArgs+i),
757 if (debugout && (i<disp->cNamedArgs-1))
758 TRACE_(olerelay)(",");
760 if (debugout) TRACE_(olerelay)("}");
762 HeapFree(GetProcessHeap(),0,disp->rgdispidNamedArgs);
763 HeapFree(GetProcessHeap(),0,disp);
780 TRACE("vt %d at %p\n",tdesc->vt,arg);
785 if (debugout) TRACE_(olerelay)("<empty>");
788 if (debugout) TRACE_(olerelay)("<null>");
791 VARIANT *vt = (VARIANT*)arg;
796 hres = xbuf_get(buf,(LPBYTE)&vttype,sizeof(vttype));
798 FIXME("vt type not read?\n");
801 memset(&tdesc2,0,sizeof(tdesc2));
804 if (debugout) TRACE_(olerelay)("Vt(%ld)(",vttype);
805 hres = deserialize_param(tinfo, readit, debugout, alloc, &tdesc2, &(V_I4(vt)), buf);
806 TRACE_(olerelay)(")");
814 case VT_BOOL: case VT_I4: case VT_UI4: case VT_UINT: case VT_R4:
818 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
819 if (hres) FIXME("Failed to read integer 4 byte\n");
821 if (debugout) TRACE_(olerelay)("%lx",*arg);
828 hres = xbuf_get(buf,(LPBYTE)&len,sizeof(DWORD));
830 FIXME("failed to read bstr klen\n");
835 if (debugout) TRACE_(olerelay)("<bstr NULL>");
837 str = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,len+sizeof(WCHAR));
838 hres = xbuf_get(buf,(LPBYTE)str,len);
840 FIXME("Failed to read BSTR.\n");
843 *arg = (DWORD)SysAllocStringLen(str,len);
844 if (debugout) TRACE_(olerelay)("%s",relaystr(str));
845 HeapFree(GetProcessHeap(),0,str);
856 derefhere = (tdesc->u.lptdesc->vt != VT_USERDEFINED);
859 hres = xbuf_get(buf,(LPBYTE)&cookie,sizeof(cookie));
861 FIXME("Failed to load pointer cookie.\n");
864 if (cookie != 0x42424242) {
865 if (debugout) TRACE_(olerelay)("NULL");
869 if (debugout) TRACE_(olerelay)("*");
873 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,_xsize(tdesc->u.lptdesc));
876 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, (LPDWORD)*arg, buf);
878 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, arg, buf);
881 /* FIXME: UNKNOWN is unknown ..., but allocate 4 byte for it */
883 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
886 hres = _unmarshal_interface(buf,&IID_IUnknown,(LPUNKNOWN*)arg);
888 TRACE_(olerelay)("unk(%p)",arg);
893 hres = _unmarshal_interface(buf,&IID_IDispatch,(LPUNKNOWN*)arg);
895 TRACE_(olerelay)("idisp(%p)",arg);
898 if (debugout) TRACE_(olerelay)("<void>");
900 case VT_USERDEFINED: {
904 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
906 FIXME("Could not get typeinfo of hreftype %lx for VT_USERDEFINED.\n",tdesc->u.hreftype);
909 hres = ITypeInfo_GetTypeAttr(tinfo2,&tattr);
911 FIXME("Could not get typeattr in VT_USERDEFINED.\n");
914 *arg = (DWORD)HeapAlloc(GetProcessHeap(),0,tattr->cbSizeInstance);
915 switch (tattr->typekind) {
917 case TKIND_INTERFACE:
919 hres = _unmarshal_interface(buf,&(tattr->guid),(LPUNKNOWN*)arg);
924 if (debugout) TRACE_(olerelay)("{");
925 for (i=0;i<tattr->cVars;i++) {
928 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
930 FIXME("Could not get vardesc of %d\n",i);
933 hres = deserialize_param(
938 &vdesc->elemdescVar.tdesc,
939 (DWORD*)(((LPBYTE)*arg)+vdesc->u.oInst),
942 if (debugout && (i<tattr->cVars-1)) TRACE_(olerelay)(",");
944 if (buf->thisisiid && (tattr->cbSizeInstance==sizeof(GUID)))
945 memcpy(&(buf->iid),(LPBYTE)*arg,sizeof(buf->iid));
946 if (debugout) TRACE_(olerelay)("}");
950 ERR("Unhandled typekind %d\n",tattr->typekind);
956 FIXME("failed to stuballoc in TKIND_RECORD.\n");
957 ITypeInfo_Release(tinfo2);
961 /* arg is pointing to the start of the array. */
962 ARRAYDESC *adesc = tdesc->u.lpadesc;
965 if (adesc->cDims > 1) FIXME("cDims > 1 in VT_CARRAY. Does it work?\n");
966 for (i=0;i<adesc->cDims;i++)
967 arrsize *= adesc->rgbounds[i].cElements;
968 for (i=0;i<arrsize;i++)
975 (DWORD*)((LPBYTE)(arg)+i*_xsize(&adesc->tdescElem)),
981 ERR("No handler for VT type %d!\n",tdesc->vt);
988 deserialize_LPVOID_ptr(
1000 if ((tdesc->vt != VT_PTR) ||
1001 (tdesc->u.lptdesc->vt != VT_PTR) ||
1002 (tdesc->u.lptdesc->u.lptdesc->vt != VT_VOID)
1004 FIXME("ppvObject not expressed as VT_PTR -> VT_PTR -> VT_VOID?\n");
1008 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(LPVOID));
1010 hres = xbuf_get(buf, (LPVOID)&cookie, sizeof(cookie));
1013 if (cookie != 0x42424242) {
1015 if (debugout) TRACE_(olerelay)("<lpvoid NULL>");
1020 hres = _unmarshal_interface(buf,&buf->iid,(LPUNKNOWN*)*arg);
1024 if (debugout) TRACE_(olerelay)("ppv(%p)",(LPVOID)*arg);
1029 deserialize_DISPPARAM_ptr(
1043 if ((tdesc->vt != VT_PTR) || (tdesc->u.lptdesc->vt != VT_USERDEFINED)) {
1044 FIXME("DISPPARAMS not expressed as VT_PTR -> VT_USERDEFINED?\n");
1048 hres = xbuf_get(buf,(LPBYTE)&cookie,sizeof(cookie));
1053 if (debugout) TRACE_(olerelay)("<DISPPARAMS NULL>");
1058 *arg = (DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DISPPARAMS));
1059 disps = (DISPPARAMS*)*arg;
1062 hres = xbuf_get(buf, (LPBYTE)&disps->cArgs, sizeof(disps->cArgs));
1066 disps->rgvarg = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(VARIANT)*disps->cArgs);
1067 if (debugout) TRACE_(olerelay)("D{");
1068 for (i=0; i< disps->cArgs; i++) {
1071 vdesc.vt = VT_VARIANT;
1072 hres = deserialize_param(
1078 (DWORD*)(disps->rgvarg+i),
1082 if (debugout) TRACE_(olerelay)("}{");
1083 hres = xbuf_get(buf, (LPBYTE)&disps->cNamedArgs, sizeof(disps->cNamedArgs));
1086 if (disps->cNamedArgs) {
1088 disps->rgdispidNamedArgs = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DISPID)*disps->cNamedArgs);
1089 for (i=0; i< disps->cNamedArgs; i++) {
1093 hres = deserialize_param(
1099 (DWORD*)(disps->rgdispidNamedArgs+i),
1102 if (debugout && i<(disps->cNamedArgs-1)) TRACE_(olerelay)(",");
1105 if (debugout) TRACE_(olerelay)("}");
1109 /* Searches function, also in inherited interfaces */
1112 ITypeInfo *tinfo, int iMethod, FUNCDESC **fdesc, BSTR *iname, BSTR *fname)
1117 if (fname) *fname = NULL;
1118 if (iname) *iname = NULL;
1121 hres = ITypeInfo_GetFuncDesc(tinfo, i, fdesc);
1127 hres = ITypeInfo_GetTypeAttr(tinfo, &attr);
1129 FIXME("GetTypeAttr failed with %lx\n",hres);
1132 /* Not found, so look in inherited ifaces. */
1133 for (j=0;j<attr->cImplTypes;j++) {
1134 hres = ITypeInfo_GetRefTypeOfImplType(tinfo, j, &href);
1136 FIXME("Did not find a reftype for interface offset %d?\n",j);
1139 hres = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
1141 FIXME("Did not find a typeinfo for reftype %ld?\n",href);
1144 hres = _get_funcdesc(tinfo2,iMethod,fdesc,iname,fname);
1145 ITypeInfo_Release(tinfo2);
1146 if (!hres) return S_OK;
1150 if (((*fdesc)->oVft/4) == iMethod) {
1152 ITypeInfo_GetDocumentation(tinfo,(*fdesc)->memid,fname,NULL,NULL,NULL);
1154 ITypeInfo_GetDocumentation(tinfo,-1,iname,NULL,NULL,NULL);
1162 xCall(LPVOID retptr, int method, TMProxyImpl *tpinfo /*, args */)
1164 DWORD *args = ((DWORD*)&tpinfo)+1, *xargs;
1167 int i, relaydeb = TRACE_ON(olerelay);
1175 EnterCriticalSection(&tpinfo->crit);
1177 hres = _get_funcdesc(tpinfo->tinfo,method,&fdesc,&iname,&fname);
1179 ERR("Did not find typeinfo/funcdesc entry for method %d!\n",method);
1180 LeaveCriticalSection(&tpinfo->crit);
1184 if (!tpinfo->chanbuf)
1186 WARN("Tried to use disconnected proxy\n");
1187 LeaveCriticalSection(&tpinfo->crit);
1188 return RPC_E_DISCONNECTED;
1192 TRACE_(olerelay)("->");
1194 TRACE_(olerelay)("%s:",relaystr(iname));
1196 TRACE_(olerelay)("%s(%d)",relaystr(fname),method);
1198 TRACE_(olerelay)("%d",method);
1199 TRACE_(olerelay)("(");
1200 if (iname) SysFreeString(iname);
1201 if (fname) SysFreeString(fname);
1203 /* Need them for hack below */
1204 memset(names,0,sizeof(names));
1205 if (ITypeInfo_GetNames(tpinfo->tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames))
1207 if (nrofnames > sizeof(names)/sizeof(names[0]))
1208 ERR("Need more names!\n");
1210 memset(&buf,0,sizeof(buf));
1211 buf.iid = IID_IUnknown;
1213 xbuf_add(&buf,(LPBYTE)args[0],sizeof(IID));
1214 if (relaydeb) TRACE_(olerelay)("riid=%s,[out]",debugstr_guid((REFIID)args[0]));
1217 for (i=0;i<fdesc->cParams;i++) {
1218 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1219 BOOL isserialized = FALSE;
1221 if (i) TRACE_(olerelay)(",");
1222 if (i+1<nrofnames && names[i+1])
1223 TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1225 /* No need to marshal other data than FIN */
1226 if (!(elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN)) {
1227 xargs+=_argsize(elem->tdesc.vt);
1228 if (relaydeb) TRACE_(olerelay)("[out]");
1231 if (((i+1)<nrofnames) && !IsBadStringPtrW(names[i+1],1)) {
1232 /* If the parameter is 'riid', we use it as interface IID
1233 * for a later ppvObject serialization.
1235 buf.thisisiid = !lstrcmpW(names[i+1],riidW);
1237 /* DISPPARAMS* needs special serializer */
1238 if (!lstrcmpW(names[i+1],pdispparamsW)) {
1239 hres = serialize_DISPPARAM_ptr(
1241 elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN,
1248 isserialized = TRUE;
1250 if (!lstrcmpW(names[i+1],ppvObjectW)) {
1251 hres = serialize_LPVOID_ptr(
1253 elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN,
1261 isserialized = TRUE;
1265 hres = serialize_param(
1267 elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN,
1276 FIXME("Failed to serialize param, hres %lx\n",hres);
1279 xargs+=_argsize(elem->tdesc.vt);
1282 if (relaydeb) TRACE_(olerelay)(")");
1283 memset(&msg,0,sizeof(msg));
1284 msg.cbBuffer = buf.curoff;
1285 msg.iMethod = method;
1286 hres = IRpcChannelBuffer_GetBuffer(tpinfo->chanbuf,&msg,&(tpinfo->iid));
1288 FIXME("RpcChannelBuffer GetBuffer failed, %lx\n",hres);
1289 LeaveCriticalSection(&tpinfo->crit);
1292 memcpy(msg.Buffer,buf.base,buf.curoff);
1293 if (relaydeb) TRACE_(olerelay)("\n");
1294 hres = IRpcChannelBuffer_SendReceive(tpinfo->chanbuf,&msg,&status);
1296 FIXME("RpcChannelBuffer SendReceive failed, %lx\n",hres);
1297 LeaveCriticalSection(&tpinfo->crit);
1301 if (relaydeb) TRACE_(olerelay)(" = %08lx (",status);
1303 buf.base = HeapReAlloc(GetProcessHeap(),0,buf.base,msg.cbBuffer);
1305 buf.base = HeapAlloc(GetProcessHeap(),0,msg.cbBuffer);
1306 buf.size = msg.cbBuffer;
1307 memcpy(buf.base,msg.Buffer,buf.size);
1310 _unmarshal_interface(&buf,(REFIID)args[0],(LPUNKNOWN*)args[1]);
1311 if (relaydeb) TRACE_(olerelay)("[in],%p",*((DWORD**)args[1]));
1314 for (i=0;i<fdesc->cParams;i++) {
1315 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1316 BOOL isdeserialized = FALSE;
1319 if (i) TRACE_(olerelay)(",");
1320 if (i+1<nrofnames && names[i+1]) TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1322 /* No need to marshal other data than FOUT I think */
1323 if (!(elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT)) {
1324 xargs += _argsize(elem->tdesc.vt);
1325 if (relaydeb) TRACE_(olerelay)("[in]");
1328 if (((i+1)<nrofnames) && !IsBadStringPtrW(names[i+1],1)) {
1329 /* If the parameter is 'riid', we use it as interface IID
1330 * for a later ppvObject serialization.
1332 buf.thisisiid = !lstrcmpW(names[i+1],riidW);
1334 /* deserialize DISPPARAM */
1335 if (!lstrcmpW(names[i+1],pdispparamsW)) {
1336 hres = deserialize_DISPPARAM_ptr(
1338 elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT,
1346 FIXME("Failed to deserialize DISPPARAM*, hres %lx\n",hres);
1349 isdeserialized = TRUE;
1351 if (!lstrcmpW(names[i+1],ppvObjectW)) {
1352 hres = deserialize_LPVOID_ptr(
1354 elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT,
1362 isdeserialized = TRUE;
1365 if (!isdeserialized)
1366 hres = deserialize_param(
1368 elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT,
1376 FIXME("Failed to unmarshall param, hres %lx\n",hres);
1379 xargs += _argsize(elem->tdesc.vt);
1382 if (relaydeb) TRACE_(olerelay)(")\n");
1383 HeapFree(GetProcessHeap(),0,buf.base);
1385 LeaveCriticalSection(&tpinfo->crit);
1390 static HRESULT WINAPI
1391 PSFacBuf_CreateProxy(
1392 LPPSFACTORYBUFFER iface, IUnknown* pUnkOuter, REFIID riid,
1393 IRpcProxyBuffer **ppProxy, LPVOID *ppv)
1401 TRACE("(...%s...)\n",debugstr_guid(riid));
1402 hres = _get_typeinfo_for_iid(riid,&tinfo);
1404 FIXME("No typeinfo for %s?\n",debugstr_guid(riid));
1407 nroffuncs = _nroffuncs(tinfo);
1408 proxy = CoTaskMemAlloc(sizeof(TMProxyImpl));
1409 if (!proxy) return E_OUTOFMEMORY;
1411 assert(sizeof(TMAsmProxy) == 12);
1413 proxy->asmstubs = VirtualAlloc(NULL, sizeof(TMAsmProxy) * nroffuncs, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
1414 if (!proxy->asmstubs) {
1415 ERR("Could not commit pages for proxy thunks\n");
1416 CoTaskMemFree(proxy);
1417 return E_OUTOFMEMORY;
1420 InitializeCriticalSection(&proxy->crit);
1422 proxy->lpvtbl = HeapAlloc(GetProcessHeap(),0,sizeof(LPBYTE)*nroffuncs);
1423 for (i=0;i<nroffuncs;i++) {
1425 TMAsmProxy *xasm = proxy->asmstubs+i;
1427 /* nrofargs without This */
1429 case 0: nrofargs = 2;
1431 case 1: case 2: nrofargs = 0;
1435 hres = _get_funcdesc(tinfo,i,&fdesc,NULL,NULL);
1437 FIXME("GetFuncDesc %lx should not fail here.\n",hres);
1440 /* some args take more than 4 byte on the stack */
1442 for (j=0;j<fdesc->cParams;j++)
1443 nrofargs += _argsize(fdesc->lprgelemdescParam[j].tdesc.vt);
1445 if (fdesc->callconv != CC_STDCALL) {
1446 ERR("calling convention is not stdcall????\n");
1452 /* popl %eax - return ptr
1459 * arg3 arg2 arg1 <method> <returnptr>
1461 xasm->popleax = 0x58;
1462 xasm->pushlval = 0x6a;
1464 xasm->pushleax = 0x50;
1465 xasm->lcall = 0xe8; /* relative jump */
1466 xasm->xcall = (DWORD)xCall;
1467 xasm->xcall -= (DWORD)&(xasm->lret);
1469 xasm->bytestopop= (nrofargs+2)*4; /* pop args, This, iMethod */
1470 proxy->lpvtbl[i] = xasm;
1472 proxy->lpvtbl2 = &tmproxyvtable;
1473 /* 1 reference for the proxy and 1 for the object */
1475 proxy->tinfo = tinfo;
1476 memcpy(&proxy->iid,riid,sizeof(*riid));
1478 *ppv = (LPVOID)proxy;
1479 *ppProxy = (IRpcProxyBuffer *)&(proxy->lpvtbl2);
1483 typedef struct _TMStubImpl {
1484 IRpcStubBufferVtbl *lpvtbl;
1492 static HRESULT WINAPI
1493 TMStubImpl_QueryInterface(LPRPCSTUBBUFFER iface, REFIID riid, LPVOID *ppv)
1495 if (IsEqualIID(riid,&IID_IRpcStubBuffer)||IsEqualIID(riid,&IID_IUnknown)){
1496 *ppv = (LPVOID)iface;
1497 IRpcStubBuffer_AddRef(iface);
1500 FIXME("%s, not supported IID.\n",debugstr_guid(riid));
1501 return E_NOINTERFACE;
1505 TMStubImpl_AddRef(LPRPCSTUBBUFFER iface)
1507 TMStubImpl *This = (TMStubImpl *)iface;
1508 ULONG refCount = InterlockedIncrement(&This->ref);
1510 TRACE("(%p)->(ref before=%lu)\n", This, refCount - 1);
1516 TMStubImpl_Release(LPRPCSTUBBUFFER iface)
1518 TMStubImpl *This = (TMStubImpl *)iface;
1519 ULONG refCount = InterlockedDecrement(&This->ref);
1521 TRACE("(%p)->(ref before=%lu)\n", This, refCount + 1);
1525 IRpcStubBuffer_Disconnect(iface);
1526 CoTaskMemFree(This);
1531 static HRESULT WINAPI
1532 TMStubImpl_Connect(LPRPCSTUBBUFFER iface, LPUNKNOWN pUnkServer)
1534 TMStubImpl *This = (TMStubImpl *)iface;
1536 TRACE("(%p)->(%p)\n", This, pUnkServer);
1538 IUnknown_AddRef(pUnkServer);
1539 This->pUnk = pUnkServer;
1544 TMStubImpl_Disconnect(LPRPCSTUBBUFFER iface)
1546 TMStubImpl *This = (TMStubImpl *)iface;
1548 TRACE("(%p)->()\n", This);
1550 IUnknown_Release(This->pUnk);
1555 static HRESULT WINAPI
1557 LPRPCSTUBBUFFER iface, RPCOLEMESSAGE* xmsg,IRpcChannelBuffer*rpcchanbuf)
1561 TMStubImpl *This = (TMStubImpl *)iface;
1563 DWORD *args, res, *xargs, nrofargs;
1568 memset(&buf,0,sizeof(buf));
1569 buf.size = xmsg->cbBuffer;
1570 buf.base = xmsg->Buffer;
1572 buf.iid = IID_IUnknown;
1575 if (xmsg->iMethod == 0) { /* QI */
1577 /* in: IID, out: <iface> */
1579 xbuf_get(&buf,(LPBYTE)&xiid,sizeof(xiid));
1581 hres = _marshal_interface(&buf,&xiid,This->pUnk);
1582 xmsg->Buffer = buf.base; /* Might have been reallocated */
1583 xmsg->cbBuffer = buf.size;
1586 hres = _get_funcdesc(This->tinfo,xmsg->iMethod,&fdesc,NULL,NULL);
1588 FIXME("GetFuncDesc on method %ld failed with %lx\n",xmsg->iMethod,hres);
1591 /* Need them for hack below */
1592 memset(names,0,sizeof(names));
1593 ITypeInfo_GetNames(This->tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames);
1594 if (nrofnames > sizeof(names)/sizeof(names[0])) {
1595 ERR("Need more names!\n");
1598 /*dump_FUNCDESC(fdesc);*/
1600 for (i=0;i<fdesc->cParams;i++)
1601 nrofargs += _argsize(fdesc->lprgelemdescParam[i].tdesc.vt);
1602 args = HeapAlloc(GetProcessHeap(),0,(nrofargs+1)*sizeof(DWORD));
1603 if (!args) return E_OUTOFMEMORY;
1605 /* Allocate all stuff used by call. */
1607 for (i=0;i<fdesc->cParams;i++) {
1608 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1609 BOOL isdeserialized = FALSE;
1611 if (((i+1)<nrofnames) && !IsBadStringPtrW(names[i+1],1)) {
1612 /* If the parameter is 'riid', we use it as interface IID
1613 * for a later ppvObject serialization.
1615 buf.thisisiid = !lstrcmpW(names[i+1],riidW);
1617 /* deserialize DISPPARAM */
1618 if (!lstrcmpW(names[i+1],pdispparamsW)) {
1619 hres = deserialize_DISPPARAM_ptr(
1621 elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN,
1629 FIXME("Failed to deserialize DISPPARAM*, hres %lx\n",hres);
1632 isdeserialized = TRUE;
1634 if (!lstrcmpW(names[i+1],ppvObjectW)) {
1635 hres = deserialize_LPVOID_ptr(
1637 elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT,
1645 isdeserialized = TRUE;
1648 if (!isdeserialized)
1649 hres = deserialize_param(
1651 elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN,
1658 xargs += _argsize(elem->tdesc.vt);
1660 FIXME("Failed to deserialize param %s, hres %lx\n",relaystr(names[i+1]),hres);
1664 hres = IUnknown_QueryInterface(This->pUnk,&(This->iid),(LPVOID*)&(args[0]));
1666 ERR("Does not support iface %s\n",debugstr_guid(&(This->iid)));
1670 (*((FARPROC**)args[0]))[fdesc->oVft/4],
1675 IUnknown_Release((LPUNKNOWN)args[0]);
1678 for (i=0;i<fdesc->cParams;i++) {
1679 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1680 BOOL isserialized = FALSE;
1682 if (((i+1)<nrofnames) && !IsBadStringPtrW(names[i+1],1)) {
1683 /* If the parameter is 'riid', we use it as interface IID
1684 * for a later ppvObject serialization.
1686 buf.thisisiid = !lstrcmpW(names[i+1],riidW);
1688 /* DISPPARAMS* needs special serializer */
1689 if (!lstrcmpW(names[i+1],pdispparamsW)) {
1690 hres = serialize_DISPPARAM_ptr(
1692 elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT,
1699 isserialized = TRUE;
1701 if (!lstrcmpW(names[i+1],ppvObjectW)) {
1702 hres = serialize_LPVOID_ptr(
1704 elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT,
1712 isserialized = TRUE;
1716 hres = serialize_param(
1718 elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT,
1725 xargs += _argsize(elem->tdesc.vt);
1727 FIXME("Failed to stuballoc param, hres %lx\n",hres);
1731 /* might need to use IRpcChannelBuffer_GetBuffer ? */
1732 xmsg->cbBuffer = buf.curoff;
1733 xmsg->Buffer = buf.base;
1734 HeapFree(GetProcessHeap(),0,args);
1738 static LPRPCSTUBBUFFER WINAPI
1739 TMStubImpl_IsIIDSupported(LPRPCSTUBBUFFER iface, REFIID riid) {
1740 FIXME("Huh (%s)?\n",debugstr_guid(riid));
1745 TMStubImpl_CountRefs(LPRPCSTUBBUFFER iface) {
1746 TMStubImpl *This = (TMStubImpl *)iface;
1748 return This->ref; /*FIXME? */
1751 static HRESULT WINAPI
1752 TMStubImpl_DebugServerQueryInterface(LPRPCSTUBBUFFER iface, LPVOID *ppv) {
1757 TMStubImpl_DebugServerRelease(LPRPCSTUBBUFFER iface, LPVOID ppv) {
1761 IRpcStubBufferVtbl tmstubvtbl = {
1762 TMStubImpl_QueryInterface,
1766 TMStubImpl_Disconnect,
1768 TMStubImpl_IsIIDSupported,
1769 TMStubImpl_CountRefs,
1770 TMStubImpl_DebugServerQueryInterface,
1771 TMStubImpl_DebugServerRelease
1774 static HRESULT WINAPI
1775 PSFacBuf_CreateStub(
1776 LPPSFACTORYBUFFER iface, REFIID riid,IUnknown *pUnkServer,
1777 IRpcStubBuffer** ppStub
1783 TRACE("(%s,%p,%p)\n",debugstr_guid(riid),pUnkServer,ppStub);
1784 hres = _get_typeinfo_for_iid(riid,&tinfo);
1786 FIXME("No typeinfo for %s?\n",debugstr_guid(riid));
1789 stub = CoTaskMemAlloc(sizeof(TMStubImpl));
1791 return E_OUTOFMEMORY;
1792 stub->lpvtbl = &tmstubvtbl;
1794 stub->tinfo = tinfo;
1795 memcpy(&(stub->iid),riid,sizeof(*riid));
1796 hres = IRpcStubBuffer_Connect((LPRPCSTUBBUFFER)stub,pUnkServer);
1797 *ppStub = (LPRPCSTUBBUFFER)stub;
1798 TRACE("IRpcStubBuffer: %p\n", stub);
1800 FIXME("Connect to pUnkServer failed?\n");
1804 static IPSFactoryBufferVtbl psfacbufvtbl = {
1805 PSFacBuf_QueryInterface,
1808 PSFacBuf_CreateProxy,
1812 /* This is the whole PSFactoryBuffer object, just the vtableptr */
1813 static IPSFactoryBufferVtbl *lppsfac = &psfacbufvtbl;
1815 /***********************************************************************
1816 * DllGetClassObject [OLE32.63]
1819 TypeLibFac_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
1821 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)) {
1825 return E_NOINTERFACE;