4 * Copyright 2002,2005 Marcus Meissner
6 * The olerelay debug channel allows you to see calls marshalled by
7 * the typelib marshaller. It is not a generic COM relaying system.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
34 #define NONAMELESSUNION
35 #define NONAMELESSSTRUCT
46 #include "propidl.h" /* for LPSAFEARRAY_User* functions */
49 #include "wine/debug.h"
50 #include "wine/exception.h"
52 static const WCHAR IDispatchW
[] = { 'I','D','i','s','p','a','t','c','h',0};
54 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
55 WINE_DECLARE_DEBUG_CHANNEL(olerelay
);
57 #define ICOM_THIS_MULTI(impl,field,iface) impl* const This=(impl*)((char*)(iface) - offsetof(impl,field))
59 static HRESULT
TMarshalDispatchChannel_Create(
60 IRpcChannelBuffer
*pDelegateChannel
, REFIID tmarshal_riid
,
61 IRpcChannelBuffer
**ppChannel
);
63 typedef struct _marshal_state
{
69 /* used in the olerelay code to avoid having the L"" stuff added by debugstr_w */
70 static char *relaystr(WCHAR
*in
) {
71 char *tmp
= (char *)debugstr_w(in
);
73 tmp
[strlen(tmp
)-1] = '\0';
78 xbuf_resize(marshal_state
*buf
, DWORD newsize
)
80 if(buf
->size
>= newsize
)
85 buf
->base
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, buf
->base
, newsize
);
91 buf
->base
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, newsize
);
100 xbuf_add(marshal_state
*buf
, LPBYTE stuff
, DWORD size
)
104 if(buf
->size
- buf
->curoff
< size
)
106 hr
= xbuf_resize(buf
, buf
->size
+ size
+ 100);
107 if(FAILED(hr
)) return hr
;
109 memcpy(buf
->base
+buf
->curoff
,stuff
,size
);
115 xbuf_get(marshal_state
*buf
, LPBYTE stuff
, DWORD size
) {
116 if (buf
->size
< buf
->curoff
+size
) return E_FAIL
;
117 memcpy(stuff
,buf
->base
+buf
->curoff
,size
);
123 xbuf_skip(marshal_state
*buf
, DWORD size
) {
124 if (buf
->size
< buf
->curoff
+size
) return E_FAIL
;
130 _unmarshal_interface(marshal_state
*buf
, REFIID riid
, LPUNKNOWN
*pUnk
) {
132 ULARGE_INTEGER newpos
;
133 LARGE_INTEGER seekto
;
138 TRACE("...%s...\n",debugstr_guid(riid
));
141 hres
= xbuf_get(buf
,(LPBYTE
)&xsize
,sizeof(xsize
));
143 ERR("xbuf_get failed\n");
147 if (xsize
== 0) return S_OK
;
149 hres
= CreateStreamOnHGlobal(0,TRUE
,&pStm
);
151 ERR("Stream create failed %x\n",hres
);
155 hres
= IStream_Write(pStm
,buf
->base
+buf
->curoff
,xsize
,&res
);
157 ERR("stream write %x\n",hres
);
161 memset(&seekto
,0,sizeof(seekto
));
162 hres
= IStream_Seek(pStm
,seekto
,SEEK_SET
,&newpos
);
164 ERR("Failed Seek %x\n",hres
);
168 hres
= CoUnmarshalInterface(pStm
,riid
,(LPVOID
*)pUnk
);
170 ERR("Unmarshalling interface %s failed with %x\n",debugstr_guid(riid
),hres
);
174 IStream_Release(pStm
);
175 return xbuf_skip(buf
,xsize
);
179 _marshal_interface(marshal_state
*buf
, REFIID riid
, LPUNKNOWN pUnk
) {
180 LPBYTE tempbuf
= NULL
;
181 IStream
*pStm
= NULL
;
183 ULARGE_INTEGER newpos
;
184 LARGE_INTEGER seekto
;
190 /* this is valid, if for instance we serialize
191 * a VT_DISPATCH with NULL ptr which apparently
192 * can happen. S_OK to make sure we continue
195 WARN("pUnk is NULL\n");
197 return xbuf_add(buf
,(LPBYTE
)&xsize
,sizeof(xsize
));
202 TRACE("...%s...\n",debugstr_guid(riid
));
204 hres
= CreateStreamOnHGlobal(0,TRUE
,&pStm
);
206 ERR("Stream create failed %x\n",hres
);
210 hres
= CoMarshalInterface(pStm
,riid
,pUnk
,0,NULL
,0);
212 ERR("Marshalling interface %s failed with %x\n", debugstr_guid(riid
), hres
);
216 hres
= IStream_Stat(pStm
,&ststg
,0);
218 ERR("Stream stat failed\n");
222 tempbuf
= HeapAlloc(GetProcessHeap(), 0, ststg
.cbSize
.u
.LowPart
);
223 memset(&seekto
,0,sizeof(seekto
));
224 hres
= IStream_Seek(pStm
,seekto
,SEEK_SET
,&newpos
);
226 ERR("Failed Seek %x\n",hres
);
230 hres
= IStream_Read(pStm
,tempbuf
,ststg
.cbSize
.u
.LowPart
,&res
);
232 ERR("Failed Read %x\n",hres
);
236 xsize
= ststg
.cbSize
.u
.LowPart
;
237 xbuf_add(buf
,(LPBYTE
)&xsize
,sizeof(xsize
));
238 hres
= xbuf_add(buf
,tempbuf
,ststg
.cbSize
.u
.LowPart
);
240 HeapFree(GetProcessHeap(),0,tempbuf
);
241 IStream_Release(pStm
);
247 xbuf_add(buf
,(LPBYTE
)&xsize
,sizeof(xsize
));
248 if (pStm
) IUnknown_Release(pStm
);
249 HeapFree(GetProcessHeap(), 0, tempbuf
);
253 /********************* OLE Proxy/Stub Factory ********************************/
254 static HRESULT WINAPI
255 PSFacBuf_QueryInterface(LPPSFACTORYBUFFER iface
, REFIID iid
, LPVOID
*ppv
) {
256 if (IsEqualIID(iid
,&IID_IPSFactoryBuffer
)||IsEqualIID(iid
,&IID_IUnknown
)) {
257 *ppv
= (LPVOID
)iface
;
258 /* No ref counting, static class */
261 FIXME("(%s) unknown IID?\n",debugstr_guid(iid
));
262 return E_NOINTERFACE
;
265 static ULONG WINAPI
PSFacBuf_AddRef(LPPSFACTORYBUFFER iface
) { return 2; }
266 static ULONG WINAPI
PSFacBuf_Release(LPPSFACTORYBUFFER iface
) { return 1; }
269 _get_typeinfo_for_iid(REFIID riid
, ITypeInfo
**ti
) {
272 char tlguid
[200],typelibkey
[300],interfacekey
[300],ver
[100];
275 DWORD tlguidlen
, verlen
, type
;
279 sprintf( interfacekey
, "Interface\\{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\Typelib",
280 riid
->Data1
, riid
->Data2
, riid
->Data3
,
281 riid
->Data4
[0], riid
->Data4
[1], riid
->Data4
[2], riid
->Data4
[3],
282 riid
->Data4
[4], riid
->Data4
[5], riid
->Data4
[6], riid
->Data4
[7]
285 if (RegOpenKeyA(HKEY_CLASSES_ROOT
,interfacekey
,&ikey
)) {
286 ERR("No %s key found.\n",interfacekey
);
289 tlguidlen
= sizeof(tlguid
);
290 if (RegQueryValueExA(ikey
,NULL
,NULL
,&type
,(LPBYTE
)tlguid
,&tlguidlen
)) {
291 ERR("Getting typelib guid failed.\n");
295 verlen
= sizeof(ver
);
296 if (RegQueryValueExA(ikey
,"Version",NULL
,&type
,(LPBYTE
)ver
,&verlen
)) {
297 ERR("Could not get version value?\n");
302 sprintf(typelibkey
,"Typelib\\%s\\%s\\0\\win32",tlguid
,ver
);
303 tlfnlen
= sizeof(tlfn
);
304 if (RegQueryValueA(HKEY_CLASSES_ROOT
,typelibkey
,tlfn
,&tlfnlen
)) {
305 ERR("Could not get typelib fn?\n");
308 MultiByteToWideChar(CP_ACP
, 0, tlfn
, -1, tlfnW
, sizeof(tlfnW
) / sizeof(tlfnW
[0]));
309 hres
= LoadTypeLib(tlfnW
,&tl
);
311 ERR("Failed to load typelib for %s, but it should be there.\n",debugstr_guid(riid
));
314 hres
= ITypeLib_GetTypeInfoOfGuid(tl
,riid
,ti
);
316 ERR("typelib does not contain info for %s?\n",debugstr_guid(riid
));
317 ITypeLib_Release(tl
);
320 ITypeLib_Release(tl
);
324 /* Determine nr of functions. Since we use the toplevel interface and all
325 * inherited ones have lower numbers, we are ok to not to descent into
326 * the inheritance tree I think.
328 static int _nroffuncs(ITypeInfo
*tinfo
) {
330 const FUNCDESC
*fdesc
;
336 hres
= ITypeInfo_GetTypeAttr(tinfo
, &attr
);
338 ERR("GetTypeAttr failed with %x\n",hres
);
341 /* look in inherited ifaces. */
342 for (j
=0;j
<attr
->cImplTypes
;j
++) {
344 hres
= ITypeInfo_GetRefTypeOfImplType(tinfo
, j
, &href
);
346 ERR("Did not find a reftype for interface offset %d?\n",j
);
349 hres
= ITypeInfo_GetRefTypeInfo(tinfo
, href
, &tinfo2
);
351 ERR("Did not find a typeinfo for reftype %d?\n",href
);
354 n
+= _nroffuncs(tinfo2
);
355 ITypeInfo_Release(tinfo2
);
357 ITypeInfo_ReleaseTypeAttr(tinfo
, attr
);
360 hres
= ITypeInfoImpl_GetInternalFuncDesc(tinfo
,i
,&fdesc
);
371 #include "pshpack1.h"
373 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 return IRpcProxyBuffer_Connect(This
->dispatch_proxy
, pDelegateChannel
);
481 TMProxyImpl_Disconnect(LPRPCPROXYBUFFER iface
)
483 ICOM_THIS_MULTI(TMProxyImpl
, lpvtbl2
, iface
);
487 EnterCriticalSection(&This
->crit
);
489 IRpcChannelBuffer_Release(This
->chanbuf
);
490 This
->chanbuf
= NULL
;
492 LeaveCriticalSection(&This
->crit
);
494 if (This
->dispatch_proxy
)
495 IRpcProxyBuffer_Disconnect(This
->dispatch_proxy
);
499 static const IRpcProxyBufferVtbl tmproxyvtable
= {
500 TMProxyImpl_QueryInterface
,
504 TMProxyImpl_Disconnect
507 /* how much space do we use on stack in DWORD steps. */
512 return 8/sizeof(DWORD
);
514 return sizeof(double)/sizeof(DWORD
);
516 return sizeof(CY
)/sizeof(DWORD
);
518 return sizeof(DATE
)/sizeof(DWORD
);
520 return (sizeof(VARIANT
)+3)/sizeof(DWORD
);
527 _xsize(TYPEDESC
*td
) {
532 return sizeof(VARIANT
)+3;
535 ARRAYDESC
*adesc
= td
->u
.lpadesc
;
537 for (i
=0;i
<adesc
->cDims
;i
++)
538 arrsize
*= adesc
->rgbounds
[i
].cElements
;
539 return arrsize
*_xsize(&adesc
->tdescElem
);
567 TRACE("(tdesc.vt %s)\n",debugstr_vt(tdesc
->vt
));
570 case VT_EMPTY
: /* nothing. empty variant for instance */
576 if (debugout
) TRACE_(olerelay
)("%x%x\n",arg
[0],arg
[1]);
578 hres
= xbuf_add(buf
,(LPBYTE
)arg
,8);
588 if (debugout
) TRACE_(olerelay
)("%x\n",*arg
);
590 hres
= xbuf_add(buf
,(LPBYTE
)arg
,sizeof(DWORD
));
595 if (debugout
) TRACE_(olerelay
)("%04x\n",*arg
& 0xffff);
597 hres
= xbuf_add(buf
,(LPBYTE
)arg
,sizeof(DWORD
));
602 if (debugout
) TRACE_(olerelay
)("%02x\n",*arg
& 0xff);
604 hres
= xbuf_add(buf
,(LPBYTE
)arg
,sizeof(DWORD
));
608 if (debugout
) TRACE_(olerelay
)("&0x%x\n",*arg
);
610 hres
= xbuf_add(buf
,(LPBYTE
)(DWORD
*)*arg
,sizeof(DWORD
));
611 /* do not dealloc at this time */
615 VARIANT
*vt
= (VARIANT
*)arg
;
616 DWORD vttype
= V_VT(vt
);
618 if (debugout
) TRACE_(olerelay
)("Vt(%s%s)(",debugstr_vt(vttype
),debugstr_vf(vttype
));
621 hres
= xbuf_add(buf
,(LPBYTE
)&vttype
,sizeof(vttype
));
622 if (hres
) return hres
;
624 /* need to recurse since we need to free the stuff */
625 hres
= serialize_param(tinfo
,writeit
,debugout
,dealloc
,&tdesc2
,(DWORD
*)&(V_I4(vt
)),buf
);
626 if (debugout
) TRACE_(olerelay
)(")");
629 case VT_BSTR
|VT_BYREF
: {
630 if (debugout
) TRACE_(olerelay
)("[byref]'%s'", *(BSTR
*)*arg
? relaystr(*((BSTR
*)*arg
)) : "<bstr NULL>");
632 /* ptr to ptr to magic widestring, basically */
633 BSTR
*bstr
= (BSTR
*) *arg
;
636 /* -1 means "null string" which is equivalent to empty string */
638 hres
= xbuf_add(buf
, (LPBYTE
)&len
,sizeof(DWORD
));
639 if (hres
) return hres
;
641 len
= *((DWORD
*)*bstr
-1)/sizeof(WCHAR
);
642 hres
= xbuf_add(buf
,(LPBYTE
)&len
,sizeof(DWORD
));
643 if (hres
) return hres
;
644 hres
= xbuf_add(buf
,(LPBYTE
)*bstr
,len
* sizeof(WCHAR
));
645 if (hres
) return hres
;
649 if (dealloc
&& arg
) {
650 BSTR
*str
= *((BSTR
**)arg
);
659 TRACE_(olerelay
)("%s",relaystr((WCHAR
*)*arg
));
661 TRACE_(olerelay
)("<bstr NULL>");
664 BSTR bstr
= (BSTR
)*arg
;
668 hres
= xbuf_add(buf
,(LPBYTE
)&len
,sizeof(DWORD
));
669 if (hres
) return hres
;
671 len
= *((DWORD
*)bstr
-1)/sizeof(WCHAR
);
672 hres
= xbuf_add(buf
,(LPBYTE
)&len
,sizeof(DWORD
));
673 if (hres
) return hres
;
674 hres
= xbuf_add(buf
,(LPBYTE
)bstr
,len
* sizeof(WCHAR
));
675 if (hres
) return hres
;
680 SysFreeString((BSTR
)*arg
);
685 BOOL derefhere
= TRUE
;
687 if (tdesc
->u
.lptdesc
->vt
== VT_USERDEFINED
) {
691 hres
= ITypeInfo_GetRefTypeInfo(tinfo
,tdesc
->u
.lptdesc
->u
.hreftype
,&tinfo2
);
693 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc
->u
.lptdesc
->u
.hreftype
);
696 ITypeInfo_GetTypeAttr(tinfo2
,&tattr
);
697 switch (tattr
->typekind
) {
698 case TKIND_ENUM
: /* confirmed */
699 case TKIND_RECORD
: /* FIXME: mostly untested */
702 case TKIND_ALIAS
: /* FIXME: untested */
703 case TKIND_DISPATCH
: /* will be done in VT_USERDEFINED case */
704 case TKIND_INTERFACE
: /* will be done in VT_USERDEFINED case */
708 FIXME("unhandled switch cases tattr->typekind %d\n", tattr
->typekind
);
712 ITypeInfo_ReleaseTypeAttr(tinfo
, tattr
);
713 ITypeInfo_Release(tinfo2
);
716 if (debugout
) TRACE_(olerelay
)("*");
717 /* Write always, so the other side knows when it gets a NULL pointer.
719 cookie
= *arg
? 0x42424242 : 0;
720 hres
= xbuf_add(buf
,(LPBYTE
)&cookie
,sizeof(cookie
));
724 if (debugout
) TRACE_(olerelay
)("NULL");
727 hres
= serialize_param(tinfo
,writeit
,debugout
,dealloc
,tdesc
->u
.lptdesc
,(DWORD
*)*arg
,buf
);
728 if (derefhere
&& dealloc
) HeapFree(GetProcessHeap(),0,(LPVOID
)*arg
);
732 if (debugout
) TRACE_(olerelay
)("unk(0x%x)",*arg
);
734 hres
= _marshal_interface(buf
,&IID_IUnknown
,(LPUNKNOWN
)*arg
);
735 if (dealloc
&& *(IUnknown
**)arg
)
736 IUnknown_Release((LPUNKNOWN
)*arg
);
739 if (debugout
) TRACE_(olerelay
)("idisp(0x%x)",*arg
);
741 hres
= _marshal_interface(buf
,&IID_IDispatch
,(LPUNKNOWN
)*arg
);
742 if (dealloc
&& *(IUnknown
**)arg
)
743 IUnknown_Release((LPUNKNOWN
)*arg
);
746 if (debugout
) TRACE_(olerelay
)("<void>");
748 case VT_USERDEFINED
: {
752 hres
= ITypeInfo_GetRefTypeInfo(tinfo
,tdesc
->u
.hreftype
,&tinfo2
);
754 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc
->u
.hreftype
);
757 ITypeInfo_GetTypeAttr(tinfo2
,&tattr
);
758 switch (tattr
->typekind
) {
760 case TKIND_INTERFACE
:
762 hres
=_marshal_interface(buf
,&(tattr
->guid
),(LPUNKNOWN
)arg
);
764 IUnknown_Release((LPUNKNOWN
)arg
);
768 if (debugout
) TRACE_(olerelay
)("{");
769 for (i
=0;i
<tattr
->cVars
;i
++) {
774 hres
= ITypeInfo2_GetVarDesc(tinfo2
, i
, &vdesc
);
776 ERR("Could not get vardesc of %d\n",i
);
779 elem2
= &vdesc
->elemdescVar
;
780 tdesc2
= &elem2
->tdesc
;
781 hres
= serialize_param(
787 (DWORD
*)(((LPBYTE
)arg
)+vdesc
->u
.oInst
),
790 ITypeInfo_ReleaseVarDesc(tinfo2
, vdesc
);
793 if (debugout
&& (i
<(tattr
->cVars
-1)))
794 TRACE_(olerelay
)(",");
796 if (debugout
) TRACE_(olerelay
)("}");
800 hres
= serialize_param(tinfo2
,writeit
,debugout
,dealloc
,&tattr
->tdescAlias
,arg
,buf
);
804 if (debugout
) TRACE_(olerelay
)("%x",*arg
);
806 hres
= xbuf_add(buf
,(LPBYTE
)arg
,sizeof(DWORD
));
809 FIXME("Unhandled typekind %d\n",tattr
->typekind
);
813 ITypeInfo_ReleaseTypeAttr(tinfo2
, tattr
);
814 ITypeInfo_Release(tinfo2
);
818 ARRAYDESC
*adesc
= tdesc
->u
.lpadesc
;
821 if (debugout
) TRACE_(olerelay
)("carr");
822 for (i
=0;i
<adesc
->cDims
;i
++) {
823 if (debugout
) TRACE_(olerelay
)("[%d]",adesc
->rgbounds
[i
].cElements
);
824 arrsize
*= adesc
->rgbounds
[i
].cElements
;
826 if (debugout
) TRACE_(olerelay
)("(vt %s)",debugstr_vt(adesc
->tdescElem
.vt
));
827 if (debugout
) TRACE_(olerelay
)("[");
828 for (i
=0;i
<arrsize
;i
++) {
829 hres
= serialize_param(tinfo
, writeit
, debugout
, dealloc
, &adesc
->tdescElem
, (DWORD
*)((LPBYTE
)arg
+i
*_xsize(&adesc
->tdescElem
)), buf
);
832 if (debugout
&& (i
<arrsize
-1)) TRACE_(olerelay
)(",");
834 if (debugout
) TRACE_(olerelay
)("]");
840 ULONG flags
= MAKELONG(MSHCTX_DIFFERENTMACHINE
, NDR_LOCAL_DATA_REPRESENTATION
);
841 ULONG size
= LPSAFEARRAY_UserSize(&flags
, buf
->curoff
, (LPSAFEARRAY
*)arg
);
842 xbuf_resize(buf
, size
);
843 LPSAFEARRAY_UserMarshal(&flags
, buf
->base
+ buf
->curoff
, (LPSAFEARRAY
*)arg
);
849 ERR("Unhandled marshal type %d.\n",tdesc
->vt
);
866 TRACE("vt %s at %p\n",debugstr_vt(tdesc
->vt
),arg
);
871 if (debugout
) TRACE_(olerelay
)("<empty>\n");
874 if (debugout
) TRACE_(olerelay
)("<null>\n");
877 VARIANT
*vt
= (VARIANT
*)arg
;
882 hres
= xbuf_get(buf
,(LPBYTE
)&vttype
,sizeof(vttype
));
884 FIXME("vt type not read?\n");
887 memset(&tdesc2
,0,sizeof(tdesc2
));
890 if (debugout
) TRACE_(olerelay
)("Vt(%s%s)(",debugstr_vt(vttype
),debugstr_vf(vttype
));
891 hres
= deserialize_param(tinfo
, readit
, debugout
, alloc
, &tdesc2
, (DWORD
*)&(V_I4(vt
)), buf
);
892 TRACE_(olerelay
)(")");
903 hres
= xbuf_get(buf
,(LPBYTE
)arg
,8);
904 if (hres
) ERR("Failed to read integer 8 byte\n");
906 if (debugout
) TRACE_(olerelay
)("%x%x",arg
[0],arg
[1]);
916 hres
= xbuf_get(buf
,(LPBYTE
)arg
,sizeof(DWORD
));
917 if (hres
) ERR("Failed to read integer 4 byte\n");
919 if (debugout
) TRACE_(olerelay
)("%x",*arg
);
925 hres
= xbuf_get(buf
,(LPBYTE
)&x
,sizeof(DWORD
));
926 if (hres
) ERR("Failed to read integer 4 byte\n");
929 if (debugout
) TRACE_(olerelay
)("%04x",*arg
& 0xffff);
935 hres
= xbuf_get(buf
,(LPBYTE
)&x
,sizeof(DWORD
));
936 if (hres
) ERR("Failed to read integer 4 byte\n");
939 if (debugout
) TRACE_(olerelay
)("%02x",*arg
& 0xff);
944 *arg
= (DWORD
)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(DWORD
));
946 hres
= xbuf_get(buf
,(LPBYTE
)*arg
,sizeof(DWORD
));
947 if (hres
) ERR("Failed to read integer 4 byte\n");
949 if (debugout
) TRACE_(olerelay
)("&0x%x",*(DWORD
*)*arg
);
951 case VT_BSTR
|VT_BYREF
: {
952 BSTR
**bstr
= (BSTR
**)arg
;
957 hres
= xbuf_get(buf
,(LPBYTE
)&len
,sizeof(DWORD
));
959 ERR("failed to read bstr klen\n");
963 *bstr
= CoTaskMemAlloc(sizeof(BSTR
*));
965 if (debugout
) TRACE_(olerelay
)("<bstr NULL>");
967 str
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,(len
+1)*sizeof(WCHAR
));
968 hres
= xbuf_get(buf
,(LPBYTE
)str
,len
*sizeof(WCHAR
));
970 ERR("Failed to read BSTR.\n");
973 *bstr
= CoTaskMemAlloc(sizeof(BSTR
*));
974 **bstr
= SysAllocStringLen(str
,len
);
975 if (debugout
) TRACE_(olerelay
)("%s",relaystr(str
));
976 HeapFree(GetProcessHeap(),0,str
);
988 hres
= xbuf_get(buf
,(LPBYTE
)&len
,sizeof(DWORD
));
990 ERR("failed to read bstr klen\n");
995 if (debugout
) TRACE_(olerelay
)("<bstr NULL>");
997 str
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,(len
+1)*sizeof(WCHAR
));
998 hres
= xbuf_get(buf
,(LPBYTE
)str
,len
*sizeof(WCHAR
));
1000 ERR("Failed to read BSTR.\n");
1003 *arg
= (DWORD
)SysAllocStringLen(str
,len
);
1004 if (debugout
) TRACE_(olerelay
)("%s",relaystr(str
));
1005 HeapFree(GetProcessHeap(),0,str
);
1014 BOOL derefhere
= TRUE
;
1016 if (tdesc
->u
.lptdesc
->vt
== VT_USERDEFINED
) {
1020 hres
= ITypeInfo_GetRefTypeInfo(tinfo
,tdesc
->u
.lptdesc
->u
.hreftype
,&tinfo2
);
1022 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc
->u
.lptdesc
->u
.hreftype
);
1025 ITypeInfo_GetTypeAttr(tinfo2
,&tattr
);
1026 switch (tattr
->typekind
) {
1027 case TKIND_ENUM
: /* confirmed */
1028 case TKIND_RECORD
: /* FIXME: mostly untested */
1031 case TKIND_ALIAS
: /* FIXME: untested */
1032 case TKIND_DISPATCH
: /* will be done in VT_USERDEFINED case */
1033 case TKIND_INTERFACE
: /* will be done in VT_USERDEFINED case */
1037 FIXME("unhandled switch cases tattr->typekind %d\n", tattr
->typekind
);
1041 ITypeInfo_ReleaseTypeAttr(tinfo2
, tattr
);
1042 ITypeInfo_Release(tinfo2
);
1044 /* read it in all cases, we need to know if we have
1045 * NULL pointer or not.
1047 hres
= xbuf_get(buf
,(LPBYTE
)&cookie
,sizeof(cookie
));
1049 ERR("Failed to load pointer cookie.\n");
1052 if (cookie
!= 0x42424242) {
1053 /* we read a NULL ptr from the remote side */
1054 if (debugout
) TRACE_(olerelay
)("NULL");
1058 if (debugout
) TRACE_(olerelay
)("*");
1060 /* Allocate space for the referenced struct */
1062 *arg
=(DWORD
)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,_xsize(tdesc
->u
.lptdesc
));
1065 return deserialize_param(tinfo
, readit
, debugout
, alloc
, tdesc
->u
.lptdesc
, (LPDWORD
)*arg
, buf
);
1067 return deserialize_param(tinfo
, readit
, debugout
, alloc
, tdesc
->u
.lptdesc
, arg
, buf
);
1070 /* FIXME: UNKNOWN is unknown ..., but allocate 4 byte for it */
1072 *arg
=(DWORD
)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(DWORD
));
1075 hres
= _unmarshal_interface(buf
,&IID_IUnknown
,(LPUNKNOWN
*)arg
);
1077 TRACE_(olerelay
)("unk(%p)",arg
);
1082 hres
= _unmarshal_interface(buf
,&IID_IDispatch
,(LPUNKNOWN
*)arg
);
1084 TRACE_(olerelay
)("idisp(%p)",arg
);
1087 if (debugout
) TRACE_(olerelay
)("<void>");
1089 case VT_USERDEFINED
: {
1093 hres
= ITypeInfo_GetRefTypeInfo(tinfo
,tdesc
->u
.hreftype
,&tinfo2
);
1095 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc
->u
.hreftype
);
1098 hres
= ITypeInfo_GetTypeAttr(tinfo2
,&tattr
);
1100 ERR("Could not get typeattr in VT_USERDEFINED.\n");
1102 switch (tattr
->typekind
) {
1103 case TKIND_DISPATCH
:
1104 case TKIND_INTERFACE
:
1106 hres
= _unmarshal_interface(buf
,&(tattr
->guid
),(LPUNKNOWN
*)arg
);
1108 case TKIND_RECORD
: {
1112 *arg
= (DWORD
)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,tattr
->cbSizeInstance
);
1114 if (debugout
) TRACE_(olerelay
)("{");
1115 for (i
=0;i
<tattr
->cVars
;i
++) {
1118 hres
= ITypeInfo2_GetVarDesc(tinfo2
, i
, &vdesc
);
1120 ERR("Could not get vardesc of %d\n",i
);
1121 ITypeInfo_ReleaseTypeAttr(tinfo2
, tattr
);
1122 ITypeInfo_Release(tinfo2
);
1125 hres
= deserialize_param(
1130 &vdesc
->elemdescVar
.tdesc
,
1131 (DWORD
*)(((LPBYTE
)*arg
)+vdesc
->u
.oInst
),
1134 ITypeInfo2_ReleaseVarDesc(tinfo2
, vdesc
);
1135 if (debugout
&& (i
<tattr
->cVars
-1)) TRACE_(olerelay
)(",");
1137 if (debugout
) TRACE_(olerelay
)("}");
1141 hres
= deserialize_param(tinfo2
,readit
,debugout
,alloc
,&tattr
->tdescAlias
,arg
,buf
);
1145 hres
= xbuf_get(buf
,(LPBYTE
)arg
,sizeof(DWORD
));
1146 if (hres
) ERR("Failed to read enum (4 byte)\n");
1148 if (debugout
) TRACE_(olerelay
)("%x",*arg
);
1151 ERR("Unhandled typekind %d\n",tattr
->typekind
);
1155 ITypeInfo_ReleaseTypeAttr(tinfo2
, tattr
);
1158 ERR("failed to stuballoc in TKIND_RECORD.\n");
1159 ITypeInfo_Release(tinfo2
);
1163 /* arg is pointing to the start of the array. */
1164 ARRAYDESC
*adesc
= tdesc
->u
.lpadesc
;
1167 if (adesc
->cDims
> 1) FIXME("cDims > 1 in VT_CARRAY. Does it work?\n");
1168 for (i
=0;i
<adesc
->cDims
;i
++)
1169 arrsize
*= adesc
->rgbounds
[i
].cElements
;
1170 for (i
=0;i
<arrsize
;i
++)
1177 (DWORD
*)((LPBYTE
)(arg
)+i
*_xsize(&adesc
->tdescElem
)),
1182 case VT_SAFEARRAY
: {
1185 ULONG flags
= MAKELONG(MSHCTX_DIFFERENTMACHINE
, NDR_LOCAL_DATA_REPRESENTATION
);
1186 unsigned char *buffer
;
1187 buffer
= LPSAFEARRAY_UserUnmarshal(&flags
, buf
->base
+ buf
->curoff
, (LPSAFEARRAY
*)arg
);
1188 buf
->curoff
= buffer
- buf
->base
;
1193 ERR("No handler for VT type %d!\n",tdesc
->vt
);
1199 /* Retrieves a function's funcdesc, searching back into inherited interfaces. */
1200 static HRESULT
get_funcdesc(ITypeInfo
*tinfo
, int iMethod
, ITypeInfo
**tactual
, const FUNCDESC
**fdesc
,
1201 BSTR
*iname
, BSTR
*fname
, UINT
*num
)
1205 UINT inherited_funcs
= 0;
1208 if (fname
) *fname
= NULL
;
1209 if (iname
) *iname
= NULL
;
1213 hr
= ITypeInfo_GetTypeAttr(tinfo
, &attr
);
1216 ERR("GetTypeAttr failed with %x\n",hr
);
1219 impl_types
= attr
->cImplTypes
;
1220 ITypeInfo_ReleaseTypeAttr(tinfo
, attr
);
1222 for (i
= 0; i
< impl_types
; i
++)
1225 ITypeInfo
*pSubTypeInfo
;
1228 hr
= ITypeInfo_GetRefTypeOfImplType(tinfo
, i
, &href
);
1229 if (FAILED(hr
)) return hr
;
1230 hr
= ITypeInfo_GetRefTypeInfo(tinfo
, href
, &pSubTypeInfo
);
1231 if (FAILED(hr
)) return hr
;
1233 hr
= get_funcdesc(pSubTypeInfo
, iMethod
, tactual
, fdesc
, iname
, fname
, &sub_funcs
);
1234 inherited_funcs
+= sub_funcs
;
1235 ITypeInfo_Release(pSubTypeInfo
);
1236 if(SUCCEEDED(hr
)) return hr
;
1238 if(iMethod
< inherited_funcs
)
1240 ERR("shouldn't be here\n");
1241 return E_INVALIDARG
;
1244 for(i
= inherited_funcs
; i
<= iMethod
; i
++)
1246 hr
= ITypeInfoImpl_GetInternalFuncDesc(tinfo
, i
- inherited_funcs
, fdesc
);
1254 /* found it. We don't care about num so zero it */
1257 ITypeInfo_AddRef(*tactual
);
1258 if (fname
) ITypeInfo_GetDocumentation(tinfo
,(*fdesc
)->memid
,fname
,NULL
,NULL
,NULL
);
1259 if (iname
) ITypeInfo_GetDocumentation(tinfo
,-1,iname
,NULL
,NULL
,NULL
);
1263 static inline BOOL
is_in_elem(const ELEMDESC
*elem
)
1265 return (elem
->u
.paramdesc
.wParamFlags
& PARAMFLAG_FIN
|| !elem
->u
.paramdesc
.wParamFlags
);
1268 static inline BOOL
is_out_elem(const ELEMDESC
*elem
)
1270 return (elem
->u
.paramdesc
.wParamFlags
& PARAMFLAG_FOUT
|| !elem
->u
.paramdesc
.wParamFlags
);
1274 xCall(LPVOID retptr
, int method
, TMProxyImpl
*tpinfo
/*, args */)
1276 DWORD
*args
= ((DWORD
*)&tpinfo
)+1, *xargs
;
1277 const FUNCDESC
*fdesc
;
1279 int i
, relaydeb
= TRACE_ON(olerelay
);
1286 DWORD remoteresult
= 0;
1288 IRpcChannelBuffer
*chanbuf
;
1290 EnterCriticalSection(&tpinfo
->crit
);
1292 hres
= get_funcdesc(tpinfo
->tinfo
,method
,&tinfo
,&fdesc
,&iname
,&fname
,NULL
);
1294 ERR("Did not find typeinfo/funcdesc entry for method %d!\n",method
);
1295 LeaveCriticalSection(&tpinfo
->crit
);
1299 if (!tpinfo
->chanbuf
)
1301 WARN("Tried to use disconnected proxy\n");
1302 ITypeInfo_Release(tinfo
);
1303 LeaveCriticalSection(&tpinfo
->crit
);
1304 return RPC_E_DISCONNECTED
;
1306 chanbuf
= tpinfo
->chanbuf
;
1307 IRpcChannelBuffer_AddRef(chanbuf
);
1309 LeaveCriticalSection(&tpinfo
->crit
);
1312 TRACE_(olerelay
)("->");
1314 TRACE_(olerelay
)("%s:",relaystr(iname
));
1316 TRACE_(olerelay
)("%s(%d)",relaystr(fname
),method
);
1318 TRACE_(olerelay
)("%d",method
);
1319 TRACE_(olerelay
)("(");
1322 if (iname
) SysFreeString(iname
);
1323 if (fname
) SysFreeString(fname
);
1325 memset(&buf
,0,sizeof(buf
));
1327 /* normal typelib driven serializing */
1329 /* Need them for hack below */
1330 memset(names
,0,sizeof(names
));
1331 if (ITypeInfo_GetNames(tinfo
,fdesc
->memid
,names
,sizeof(names
)/sizeof(names
[0]),&nrofnames
))
1333 if (nrofnames
> sizeof(names
)/sizeof(names
[0]))
1334 ERR("Need more names!\n");
1337 for (i
=0;i
<fdesc
->cParams
;i
++) {
1338 ELEMDESC
*elem
= fdesc
->lprgelemdescParam
+i
;
1340 if (i
) TRACE_(olerelay
)(",");
1341 if (i
+1<nrofnames
&& names
[i
+1])
1342 TRACE_(olerelay
)("%s=",relaystr(names
[i
+1]));
1344 /* No need to marshal other data than FIN and any VT_PTR. */
1345 if (!is_in_elem(elem
) && (elem
->tdesc
.vt
!= VT_PTR
)) {
1346 xargs
+=_argsize(elem
->tdesc
.vt
);
1347 if (relaydeb
) TRACE_(olerelay
)("[out]");
1350 hres
= serialize_param(
1361 ERR("Failed to serialize param, hres %x\n",hres
);
1364 xargs
+=_argsize(elem
->tdesc
.vt
);
1366 if (relaydeb
) TRACE_(olerelay
)(")");
1368 memset(&msg
,0,sizeof(msg
));
1369 msg
.cbBuffer
= buf
.curoff
;
1370 msg
.iMethod
= method
;
1371 hres
= IRpcChannelBuffer_GetBuffer(chanbuf
,&msg
,&(tpinfo
->iid
));
1373 ERR("RpcChannelBuffer GetBuffer failed, %x\n",hres
);
1376 memcpy(msg
.Buffer
,buf
.base
,buf
.curoff
);
1377 if (relaydeb
) TRACE_(olerelay
)("\n");
1378 hres
= IRpcChannelBuffer_SendReceive(chanbuf
,&msg
,&status
);
1380 ERR("RpcChannelBuffer SendReceive failed, %x\n",hres
);
1384 if (relaydeb
) TRACE_(olerelay
)(" status = %08x (",status
);
1386 buf
.base
= HeapReAlloc(GetProcessHeap(),0,buf
.base
,msg
.cbBuffer
);
1388 buf
.base
= HeapAlloc(GetProcessHeap(),0,msg
.cbBuffer
);
1389 buf
.size
= msg
.cbBuffer
;
1390 memcpy(buf
.base
,msg
.Buffer
,buf
.size
);
1393 /* generic deserializer using typelib description */
1396 for (i
=0;i
<fdesc
->cParams
;i
++) {
1397 ELEMDESC
*elem
= fdesc
->lprgelemdescParam
+i
;
1400 if (i
) TRACE_(olerelay
)(",");
1401 if (i
+1<nrofnames
&& names
[i
+1]) TRACE_(olerelay
)("%s=",relaystr(names
[i
+1]));
1403 /* No need to marshal other data than FOUT and any VT_PTR */
1404 if (!is_out_elem(elem
) && (elem
->tdesc
.vt
!= VT_PTR
)) {
1405 xargs
+= _argsize(elem
->tdesc
.vt
);
1406 if (relaydeb
) TRACE_(olerelay
)("[in]");
1409 hres
= deserialize_param(
1419 ERR("Failed to unmarshall param, hres %x\n",hres
);
1423 xargs
+= _argsize(elem
->tdesc
.vt
);
1426 hres
= xbuf_get(&buf
, (LPBYTE
)&remoteresult
, sizeof(DWORD
));
1429 if (relaydeb
) TRACE_(olerelay
)(") = %08x\n", remoteresult
);
1431 hres
= remoteresult
;
1434 for (i
= 0; i
< nrofnames
; i
++)
1435 SysFreeString(names
[i
]);
1436 HeapFree(GetProcessHeap(),0,buf
.base
);
1437 IRpcChannelBuffer_Release(chanbuf
);
1438 ITypeInfo_Release(tinfo
);
1439 TRACE("-- 0x%08x\n", hres
);
1443 static HRESULT WINAPI
ProxyIUnknown_QueryInterface(IUnknown
*iface
, REFIID riid
, void **ppv
)
1445 TMProxyImpl
*proxy
= (TMProxyImpl
*)iface
;
1447 TRACE("(%s, %p)\n", debugstr_guid(riid
), ppv
);
1449 if (proxy
->outerunknown
)
1450 return IUnknown_QueryInterface(proxy
->outerunknown
, riid
, ppv
);
1452 FIXME("No interface\n");
1453 return E_NOINTERFACE
;
1456 static ULONG WINAPI
ProxyIUnknown_AddRef(IUnknown
*iface
)
1458 TMProxyImpl
*proxy
= (TMProxyImpl
*)iface
;
1462 if (proxy
->outerunknown
)
1463 return IUnknown_AddRef(proxy
->outerunknown
);
1465 return 2; /* FIXME */
1468 static ULONG WINAPI
ProxyIUnknown_Release(IUnknown
*iface
)
1470 TMProxyImpl
*proxy
= (TMProxyImpl
*)iface
;
1474 if (proxy
->outerunknown
)
1475 return IUnknown_Release(proxy
->outerunknown
);
1477 return 1; /* FIXME */
1480 static HRESULT WINAPI
ProxyIDispatch_GetTypeInfoCount(LPDISPATCH iface
, UINT
* pctinfo
)
1482 TMProxyImpl
*This
= (TMProxyImpl
*)iface
;
1484 TRACE("(%p)\n", pctinfo
);
1486 return IDispatch_GetTypeInfoCount(This
->dispatch
, pctinfo
);
1489 static HRESULT WINAPI
ProxyIDispatch_GetTypeInfo(LPDISPATCH iface
, UINT iTInfo
, LCID lcid
, ITypeInfo
** ppTInfo
)
1491 TMProxyImpl
*This
= (TMProxyImpl
*)iface
;
1493 TRACE("(%d, %x, %p)\n", iTInfo
, lcid
, ppTInfo
);
1495 return IDispatch_GetTypeInfo(This
->dispatch
, iTInfo
, lcid
, ppTInfo
);
1498 static HRESULT WINAPI
ProxyIDispatch_GetIDsOfNames(LPDISPATCH iface
, REFIID riid
, LPOLESTR
* rgszNames
, UINT cNames
, LCID lcid
, DISPID
* rgDispId
)
1500 TMProxyImpl
*This
= (TMProxyImpl
*)iface
;
1502 TRACE("(%s, %p, %d, 0x%x, %p)\n", debugstr_guid(riid
), rgszNames
, cNames
, lcid
, rgDispId
);
1504 return IDispatch_GetIDsOfNames(This
->dispatch
, riid
, rgszNames
,
1505 cNames
, lcid
, rgDispId
);
1508 static HRESULT WINAPI
ProxyIDispatch_Invoke(LPDISPATCH iface
, DISPID dispIdMember
, REFIID riid
, LCID lcid
,
1509 WORD wFlags
, DISPPARAMS
* pDispParams
, VARIANT
* pVarResult
,
1510 EXCEPINFO
* pExcepInfo
, UINT
* puArgErr
)
1512 TMProxyImpl
*This
= (TMProxyImpl
*)iface
;
1514 TRACE("(%d, %s, 0x%x, 0x%x, %p, %p, %p, %p)\n", dispIdMember
,
1515 debugstr_guid(riid
), lcid
, wFlags
, pDispParams
, pVarResult
,
1516 pExcepInfo
, puArgErr
);
1518 return IDispatch_Invoke(This
->dispatch
, dispIdMember
, riid
, lcid
,
1519 wFlags
, pDispParams
, pVarResult
, pExcepInfo
,
1525 const IRpcChannelBufferVtbl
*lpVtbl
;
1527 /* the IDispatch-derived interface we are handling */
1529 IRpcChannelBuffer
*pDelegateChannel
;
1530 } TMarshalDispatchChannel
;
1532 static HRESULT WINAPI
TMarshalDispatchChannel_QueryInterface(LPRPCCHANNELBUFFER iface
, REFIID riid
, LPVOID
*ppv
)
1535 if (IsEqualIID(riid
,&IID_IRpcChannelBuffer
) || IsEqualIID(riid
,&IID_IUnknown
))
1537 *ppv
= (LPVOID
)iface
;
1538 IUnknown_AddRef(iface
);
1541 return E_NOINTERFACE
;
1544 static ULONG WINAPI
TMarshalDispatchChannel_AddRef(LPRPCCHANNELBUFFER iface
)
1546 TMarshalDispatchChannel
*This
= (TMarshalDispatchChannel
*)iface
;
1547 return InterlockedIncrement(&This
->refs
);
1550 static ULONG WINAPI
TMarshalDispatchChannel_Release(LPRPCCHANNELBUFFER iface
)
1552 TMarshalDispatchChannel
*This
= (TMarshalDispatchChannel
*)iface
;
1555 ref
= InterlockedDecrement(&This
->refs
);
1559 IRpcChannelBuffer_Release(This
->pDelegateChannel
);
1560 HeapFree(GetProcessHeap(), 0, This
);
1564 static HRESULT WINAPI
TMarshalDispatchChannel_GetBuffer(LPRPCCHANNELBUFFER iface
, RPCOLEMESSAGE
* olemsg
, REFIID riid
)
1566 TMarshalDispatchChannel
*This
= (TMarshalDispatchChannel
*)iface
;
1567 TRACE("(%p, %s)\n", olemsg
, debugstr_guid(riid
));
1568 /* Note: we are pretending to invoke a method on the interface identified
1569 * by tmarshal_iid so that we can re-use the IDispatch proxy/stub code
1570 * without the RPC runtime getting confused by not exporting an IDispatch interface */
1571 return IRpcChannelBuffer_GetBuffer(This
->pDelegateChannel
, olemsg
, &This
->tmarshal_iid
);
1574 static HRESULT WINAPI
TMarshalDispatchChannel_SendReceive(LPRPCCHANNELBUFFER iface
, RPCOLEMESSAGE
*olemsg
, ULONG
*pstatus
)
1576 TMarshalDispatchChannel
*This
= (TMarshalDispatchChannel
*)iface
;
1577 TRACE("(%p, %p)\n", olemsg
, pstatus
);
1578 return IRpcChannelBuffer_SendReceive(This
->pDelegateChannel
, olemsg
, pstatus
);
1581 static HRESULT WINAPI
TMarshalDispatchChannel_FreeBuffer(LPRPCCHANNELBUFFER iface
, RPCOLEMESSAGE
* olemsg
)
1583 TMarshalDispatchChannel
*This
= (TMarshalDispatchChannel
*)iface
;
1584 TRACE("(%p)\n", olemsg
);
1585 return IRpcChannelBuffer_FreeBuffer(This
->pDelegateChannel
, olemsg
);
1588 static HRESULT WINAPI
TMarshalDispatchChannel_GetDestCtx(LPRPCCHANNELBUFFER iface
, DWORD
* pdwDestContext
, void** ppvDestContext
)
1590 TMarshalDispatchChannel
*This
= (TMarshalDispatchChannel
*)iface
;
1591 TRACE("(%p,%p)\n", pdwDestContext
, ppvDestContext
);
1592 return IRpcChannelBuffer_GetDestCtx(This
->pDelegateChannel
, pdwDestContext
, ppvDestContext
);
1595 static HRESULT WINAPI
TMarshalDispatchChannel_IsConnected(LPRPCCHANNELBUFFER iface
)
1597 TMarshalDispatchChannel
*This
= (TMarshalDispatchChannel
*)iface
;
1599 return IRpcChannelBuffer_IsConnected(This
->pDelegateChannel
);
1602 static const IRpcChannelBufferVtbl TMarshalDispatchChannelVtbl
=
1604 TMarshalDispatchChannel_QueryInterface
,
1605 TMarshalDispatchChannel_AddRef
,
1606 TMarshalDispatchChannel_Release
,
1607 TMarshalDispatchChannel_GetBuffer
,
1608 TMarshalDispatchChannel_SendReceive
,
1609 TMarshalDispatchChannel_FreeBuffer
,
1610 TMarshalDispatchChannel_GetDestCtx
,
1611 TMarshalDispatchChannel_IsConnected
1614 static HRESULT
TMarshalDispatchChannel_Create(
1615 IRpcChannelBuffer
*pDelegateChannel
, REFIID tmarshal_riid
,
1616 IRpcChannelBuffer
**ppChannel
)
1618 TMarshalDispatchChannel
*This
= HeapAlloc(GetProcessHeap(), 0, sizeof(*This
));
1620 return E_OUTOFMEMORY
;
1622 This
->lpVtbl
= &TMarshalDispatchChannelVtbl
;
1624 IRpcChannelBuffer_AddRef(pDelegateChannel
);
1625 This
->pDelegateChannel
= pDelegateChannel
;
1626 This
->tmarshal_iid
= *tmarshal_riid
;
1628 *ppChannel
= (IRpcChannelBuffer
*)&This
->lpVtbl
;
1633 static inline HRESULT
get_facbuf_for_iid(REFIID riid
, IPSFactoryBuffer
**facbuf
)
1638 if ((hr
= CoGetPSClsid(riid
, &clsid
)))
1640 return CoGetClassObject(&clsid
, CLSCTX_INPROC_SERVER
, NULL
,
1641 &IID_IPSFactoryBuffer
, (LPVOID
*)facbuf
);
1644 static HRESULT WINAPI
1645 PSFacBuf_CreateProxy(
1646 LPPSFACTORYBUFFER iface
, IUnknown
* pUnkOuter
, REFIID riid
,
1647 IRpcProxyBuffer
**ppProxy
, LPVOID
*ppv
)
1652 const FUNCDESC
*fdesc
;
1656 TRACE("(...%s...)\n",debugstr_guid(riid
));
1657 hres
= _get_typeinfo_for_iid(riid
,&tinfo
);
1659 ERR("No typeinfo for %s?\n",debugstr_guid(riid
));
1662 nroffuncs
= _nroffuncs(tinfo
);
1663 proxy
= CoTaskMemAlloc(sizeof(TMProxyImpl
));
1664 if (!proxy
) return E_OUTOFMEMORY
;
1666 assert(sizeof(TMAsmProxy
) == 12);
1668 proxy
->dispatch
= NULL
;
1669 proxy
->dispatch_proxy
= NULL
;
1670 proxy
->outerunknown
= pUnkOuter
;
1671 proxy
->asmstubs
= VirtualAlloc(NULL
, sizeof(TMAsmProxy
) * nroffuncs
, MEM_COMMIT
, PAGE_EXECUTE_READWRITE
);
1672 if (!proxy
->asmstubs
) {
1673 ERR("Could not commit pages for proxy thunks\n");
1674 CoTaskMemFree(proxy
);
1675 return E_OUTOFMEMORY
;
1677 proxy
->lpvtbl2
= &tmproxyvtable
;
1678 /* one reference for the proxy */
1680 proxy
->tinfo
= tinfo
;
1681 memcpy(&proxy
->iid
,riid
,sizeof(*riid
));
1684 InitializeCriticalSection(&proxy
->crit
);
1685 proxy
->crit
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": TMProxyImpl.crit");
1687 proxy
->lpvtbl
= HeapAlloc(GetProcessHeap(),0,sizeof(LPBYTE
)*nroffuncs
);
1688 for (i
=0;i
<nroffuncs
;i
++) {
1689 TMAsmProxy
*xasm
= proxy
->asmstubs
+i
;
1693 proxy
->lpvtbl
[i
] = ProxyIUnknown_QueryInterface
;
1696 proxy
->lpvtbl
[i
] = ProxyIUnknown_AddRef
;
1699 proxy
->lpvtbl
[i
] = ProxyIUnknown_Release
;
1703 /* nrofargs without This */
1706 hres
= get_funcdesc(tinfo
,i
,&tinfo2
,&fdesc
,NULL
,NULL
,NULL
);
1708 ERR("GetFuncDesc %x should not fail here.\n",hres
);
1711 ITypeInfo_Release(tinfo2
);
1712 /* some args take more than 4 byte on the stack */
1714 for (j
=0;j
<fdesc
->cParams
;j
++)
1715 nrofargs
+= _argsize(fdesc
->lprgelemdescParam
[j
].tdesc
.vt
);
1718 if (fdesc
->callconv
!= CC_STDCALL
) {
1719 ERR("calling convention is not stdcall????\n");
1722 /* popl %eax - return ptr
1729 * arg3 arg2 arg1 <method> <returnptr>
1731 xasm
->popleax
= 0x58;
1732 xasm
->pushlval
= 0x6a;
1734 xasm
->pushleax
= 0x50;
1735 xasm
->lcall
= 0xe8; /* relative jump */
1736 xasm
->xcall
= (DWORD
)xCall
;
1737 xasm
->xcall
-= (DWORD
)&(xasm
->lret
);
1739 xasm
->bytestopop
= (nrofargs
+2)*4; /* pop args, This, iMethod */
1740 proxy
->lpvtbl
[i
] = xasm
;
1743 FIXME("not implemented on non i386\n");
1750 /* if we derive from IDispatch then defer to its proxy for its methods */
1751 hres
= ITypeInfo_GetTypeAttr(tinfo
, &typeattr
);
1754 if (typeattr
->wTypeFlags
& TYPEFLAG_FDISPATCHABLE
)
1756 IPSFactoryBuffer
*factory_buffer
;
1757 hres
= get_facbuf_for_iid(&IID_IDispatch
, &factory_buffer
);
1760 hres
= IPSFactoryBuffer_CreateProxy(factory_buffer
, NULL
,
1761 &IID_IDispatch
, &proxy
->dispatch_proxy
,
1762 (void **)&proxy
->dispatch
);
1763 IPSFactoryBuffer_Release(factory_buffer
);
1765 if ((hres
== S_OK
) && (nroffuncs
< 7))
1767 ERR("nroffuncs calculated incorrectly (%d)\n", nroffuncs
);
1768 hres
= E_UNEXPECTED
;
1772 proxy
->lpvtbl
[3] = ProxyIDispatch_GetTypeInfoCount
;
1773 proxy
->lpvtbl
[4] = ProxyIDispatch_GetTypeInfo
;
1774 proxy
->lpvtbl
[5] = ProxyIDispatch_GetIDsOfNames
;
1775 proxy
->lpvtbl
[6] = ProxyIDispatch_Invoke
;
1778 ITypeInfo_ReleaseTypeAttr(tinfo
, typeattr
);
1783 *ppv
= (LPVOID
)proxy
;
1784 *ppProxy
= (IRpcProxyBuffer
*)&(proxy
->lpvtbl2
);
1785 IUnknown_AddRef((IUnknown
*)*ppv
);
1789 TMProxyImpl_Release((IRpcProxyBuffer
*)&proxy
->lpvtbl2
);
1793 typedef struct _TMStubImpl
{
1794 const IRpcStubBufferVtbl
*lpvtbl
;
1800 IRpcStubBuffer
*dispatch_stub
;
1801 BOOL dispatch_derivative
;
1804 static HRESULT WINAPI
1805 TMStubImpl_QueryInterface(LPRPCSTUBBUFFER iface
, REFIID riid
, LPVOID
*ppv
)
1807 if (IsEqualIID(riid
,&IID_IRpcStubBuffer
)||IsEqualIID(riid
,&IID_IUnknown
)){
1808 *ppv
= (LPVOID
)iface
;
1809 IRpcStubBuffer_AddRef(iface
);
1812 FIXME("%s, not supported IID.\n",debugstr_guid(riid
));
1813 return E_NOINTERFACE
;
1817 TMStubImpl_AddRef(LPRPCSTUBBUFFER iface
)
1819 TMStubImpl
*This
= (TMStubImpl
*)iface
;
1820 ULONG refCount
= InterlockedIncrement(&This
->ref
);
1822 TRACE("(%p)->(ref before=%u)\n", This
, refCount
- 1);
1828 TMStubImpl_Release(LPRPCSTUBBUFFER iface
)
1830 TMStubImpl
*This
= (TMStubImpl
*)iface
;
1831 ULONG refCount
= InterlockedDecrement(&This
->ref
);
1833 TRACE("(%p)->(ref before=%u)\n", This
, refCount
+ 1);
1837 IRpcStubBuffer_Disconnect(iface
);
1838 ITypeInfo_Release(This
->tinfo
);
1839 if (This
->dispatch_stub
)
1840 IRpcStubBuffer_Release(This
->dispatch_stub
);
1841 CoTaskMemFree(This
);
1846 static HRESULT WINAPI
1847 TMStubImpl_Connect(LPRPCSTUBBUFFER iface
, LPUNKNOWN pUnkServer
)
1849 TMStubImpl
*This
= (TMStubImpl
*)iface
;
1851 TRACE("(%p)->(%p)\n", This
, pUnkServer
);
1853 IUnknown_AddRef(pUnkServer
);
1854 This
->pUnk
= pUnkServer
;
1856 if (This
->dispatch_stub
)
1857 IRpcStubBuffer_Connect(This
->dispatch_stub
, pUnkServer
);
1863 TMStubImpl_Disconnect(LPRPCSTUBBUFFER iface
)
1865 TMStubImpl
*This
= (TMStubImpl
*)iface
;
1867 TRACE("(%p)->()\n", This
);
1871 IUnknown_Release(This
->pUnk
);
1875 if (This
->dispatch_stub
)
1876 IRpcStubBuffer_Disconnect(This
->dispatch_stub
);
1879 static HRESULT WINAPI
1881 LPRPCSTUBBUFFER iface
, RPCOLEMESSAGE
* xmsg
,IRpcChannelBuffer
*rpcchanbuf
)
1884 const FUNCDESC
*fdesc
;
1885 TMStubImpl
*This
= (TMStubImpl
*)iface
;
1887 DWORD
*args
= NULL
, res
, *xargs
, nrofargs
;
1892 ITypeInfo
*tinfo
= NULL
;
1896 if (xmsg
->iMethod
< 3) {
1897 ERR("IUnknown methods cannot be marshaled by the typelib marshaler\n");
1898 return E_UNEXPECTED
;
1901 if (This
->dispatch_derivative
&& xmsg
->iMethod
< sizeof(IDispatchVtbl
)/sizeof(void *))
1903 IPSFactoryBuffer
*factory_buffer
;
1904 hres
= get_facbuf_for_iid(&IID_IDispatch
, &factory_buffer
);
1907 hres
= IPSFactoryBuffer_CreateStub(factory_buffer
, &IID_IDispatch
,
1908 This
->pUnk
, &This
->dispatch_stub
);
1909 IPSFactoryBuffer_Release(factory_buffer
);
1913 return IRpcStubBuffer_Invoke(This
->dispatch_stub
, xmsg
, rpcchanbuf
);
1916 memset(&buf
,0,sizeof(buf
));
1917 buf
.size
= xmsg
->cbBuffer
;
1918 buf
.base
= HeapAlloc(GetProcessHeap(), 0, xmsg
->cbBuffer
);
1919 memcpy(buf
.base
, xmsg
->Buffer
, xmsg
->cbBuffer
);
1922 hres
= get_funcdesc(This
->tinfo
,xmsg
->iMethod
,&tinfo
,&fdesc
,&iname
,NULL
,NULL
);
1924 ERR("GetFuncDesc on method %d failed with %x\n",xmsg
->iMethod
,hres
);
1928 if (iname
&& !lstrcmpW(iname
, IDispatchW
))
1930 ERR("IDispatch cannot be marshaled by the typelib marshaler\n");
1931 hres
= E_UNEXPECTED
;
1932 SysFreeString (iname
);
1936 if (iname
) SysFreeString (iname
);
1938 /* Need them for hack below */
1939 memset(names
,0,sizeof(names
));
1940 ITypeInfo_GetNames(tinfo
,fdesc
->memid
,names
,sizeof(names
)/sizeof(names
[0]),&nrofnames
);
1941 if (nrofnames
> sizeof(names
)/sizeof(names
[0])) {
1942 ERR("Need more names!\n");
1945 /*dump_FUNCDESC(fdesc);*/
1947 for (i
=0;i
<fdesc
->cParams
;i
++)
1948 nrofargs
+= _argsize(fdesc
->lprgelemdescParam
[i
].tdesc
.vt
);
1949 args
= HeapAlloc(GetProcessHeap(),0,(nrofargs
+1)*sizeof(DWORD
));
1952 hres
= E_OUTOFMEMORY
;
1956 /* Allocate all stuff used by call. */
1958 for (i
=0;i
<fdesc
->cParams
;i
++) {
1959 ELEMDESC
*elem
= fdesc
->lprgelemdescParam
+i
;
1961 hres
= deserialize_param(
1970 xargs
+= _argsize(elem
->tdesc
.vt
);
1972 ERR("Failed to deserialize param %s, hres %x\n",relaystr(names
[i
+1]),hres
);
1977 args
[0] = (DWORD
)This
->pUnk
;
1982 (*((FARPROC
**)args
[0]))[fdesc
->oVft
/4],
1990 DWORD dwExceptionCode
= GetExceptionCode();
1991 ERR("invoke call failed with exception 0x%08x (%d)\n", dwExceptionCode
, dwExceptionCode
);
1992 if (FAILED(dwExceptionCode
))
1993 hres
= dwExceptionCode
;
1995 hres
= HRESULT_FROM_WIN32(dwExceptionCode
);
2005 for (i
=0;i
<fdesc
->cParams
;i
++) {
2006 ELEMDESC
*elem
= fdesc
->lprgelemdescParam
+i
;
2007 hres
= serialize_param(
2016 xargs
+= _argsize(elem
->tdesc
.vt
);
2018 ERR("Failed to stuballoc param, hres %x\n",hres
);
2023 hres
= xbuf_add (&buf
, (LPBYTE
)&res
, sizeof(DWORD
));
2028 xmsg
->cbBuffer
= buf
.curoff
;
2029 hres
= IRpcChannelBuffer_GetBuffer(rpcchanbuf
, xmsg
, &This
->iid
);
2031 ERR("IRpcChannelBuffer_GetBuffer failed with error 0x%08x\n", hres
);
2034 memcpy(xmsg
->Buffer
, buf
.base
, buf
.curoff
);
2037 for (i
= 0; i
< nrofnames
; i
++)
2038 SysFreeString(names
[i
]);
2040 ITypeInfo_Release(tinfo
);
2041 HeapFree(GetProcessHeap(), 0, args
);
2043 HeapFree(GetProcessHeap(), 0, buf
.base
);
2045 TRACE("returning\n");
2049 static LPRPCSTUBBUFFER WINAPI
2050 TMStubImpl_IsIIDSupported(LPRPCSTUBBUFFER iface
, REFIID riid
) {
2051 FIXME("Huh (%s)?\n",debugstr_guid(riid
));
2056 TMStubImpl_CountRefs(LPRPCSTUBBUFFER iface
) {
2057 TMStubImpl
*This
= (TMStubImpl
*)iface
;
2060 return This
->ref
; /*FIXME? */
2063 static HRESULT WINAPI
2064 TMStubImpl_DebugServerQueryInterface(LPRPCSTUBBUFFER iface
, LPVOID
*ppv
) {
2069 TMStubImpl_DebugServerRelease(LPRPCSTUBBUFFER iface
, LPVOID ppv
) {
2073 static const IRpcStubBufferVtbl tmstubvtbl
= {
2074 TMStubImpl_QueryInterface
,
2078 TMStubImpl_Disconnect
,
2080 TMStubImpl_IsIIDSupported
,
2081 TMStubImpl_CountRefs
,
2082 TMStubImpl_DebugServerQueryInterface
,
2083 TMStubImpl_DebugServerRelease
2086 static HRESULT WINAPI
2087 PSFacBuf_CreateStub(
2088 LPPSFACTORYBUFFER iface
, REFIID riid
,IUnknown
*pUnkServer
,
2089 IRpcStubBuffer
** ppStub
2096 TRACE("(%s,%p,%p)\n",debugstr_guid(riid
),pUnkServer
,ppStub
);
2098 hres
= _get_typeinfo_for_iid(riid
,&tinfo
);
2100 ERR("No typeinfo for %s?\n",debugstr_guid(riid
));
2104 stub
= CoTaskMemAlloc(sizeof(TMStubImpl
));
2106 return E_OUTOFMEMORY
;
2107 stub
->lpvtbl
= &tmstubvtbl
;
2109 stub
->tinfo
= tinfo
;
2110 stub
->dispatch_stub
= NULL
;
2111 stub
->dispatch_derivative
= FALSE
;
2112 memcpy(&(stub
->iid
),riid
,sizeof(*riid
));
2113 hres
= IRpcStubBuffer_Connect((LPRPCSTUBBUFFER
)stub
,pUnkServer
);
2114 *ppStub
= (LPRPCSTUBBUFFER
)stub
;
2115 TRACE("IRpcStubBuffer: %p\n", stub
);
2117 ERR("Connect to pUnkServer failed?\n");
2119 /* if we derive from IDispatch then defer to its stub for some of its methods */
2120 hres
= ITypeInfo_GetTypeAttr(tinfo
, &typeattr
);
2123 if (typeattr
->wTypeFlags
& TYPEFLAG_FDISPATCHABLE
)
2124 stub
->dispatch_derivative
= TRUE
;
2125 ITypeInfo_ReleaseTypeAttr(tinfo
, typeattr
);
2131 static const IPSFactoryBufferVtbl psfacbufvtbl
= {
2132 PSFacBuf_QueryInterface
,
2135 PSFacBuf_CreateProxy
,
2139 /* This is the whole PSFactoryBuffer object, just the vtableptr */
2140 static const IPSFactoryBufferVtbl
*lppsfac
= &psfacbufvtbl
;
2142 /***********************************************************************
2143 * TMARSHAL_DllGetClassObject
2145 HRESULT
TMARSHAL_DllGetClassObject(REFCLSID rclsid
, REFIID iid
,LPVOID
*ppv
)
2147 if (IsEqualIID(iid
,&IID_IPSFactoryBuffer
)) {
2151 return E_NOINTERFACE
;