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
25 #include "wine/port.h"
35 #define NONAMELESSUNION
36 #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 static HRESULT
TMarshalDispatchChannel_Create(
58 IRpcChannelBuffer
*pDelegateChannel
, REFIID tmarshal_riid
,
59 IRpcChannelBuffer
**ppChannel
);
61 typedef struct _marshal_state
{
67 /* used in the olerelay code to avoid having the L"" stuff added by debugstr_w */
68 static char *relaystr(WCHAR
*in
) {
69 char *tmp
= (char *)debugstr_w(in
);
71 tmp
[strlen(tmp
)-1] = '\0';
76 xbuf_resize(marshal_state
*buf
, DWORD newsize
)
78 if(buf
->size
>= newsize
)
83 buf
->base
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, buf
->base
, newsize
);
89 buf
->base
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, newsize
);
98 xbuf_add(marshal_state
*buf
, const BYTE
*stuff
, DWORD size
)
102 if(buf
->size
- buf
->curoff
< size
)
104 hr
= xbuf_resize(buf
, buf
->size
+ size
+ 100);
105 if(FAILED(hr
)) return hr
;
107 memcpy(buf
->base
+buf
->curoff
,stuff
,size
);
113 xbuf_get(marshal_state
*buf
, LPBYTE stuff
, DWORD size
) {
114 if (buf
->size
< buf
->curoff
+size
) return E_FAIL
;
115 memcpy(stuff
,buf
->base
+buf
->curoff
,size
);
121 xbuf_skip(marshal_state
*buf
, DWORD size
) {
122 if (buf
->size
< buf
->curoff
+size
) return E_FAIL
;
128 _unmarshal_interface(marshal_state
*buf
, REFIID riid
, LPUNKNOWN
*pUnk
) {
130 ULARGE_INTEGER newpos
;
131 LARGE_INTEGER seekto
;
136 TRACE("...%s...\n",debugstr_guid(riid
));
139 hres
= xbuf_get(buf
,(LPBYTE
)&xsize
,sizeof(xsize
));
141 ERR("xbuf_get failed\n");
145 if (xsize
== 0) return S_OK
;
147 hres
= CreateStreamOnHGlobal(0,TRUE
,&pStm
);
149 ERR("Stream create failed %x\n",hres
);
153 hres
= IStream_Write(pStm
,buf
->base
+buf
->curoff
,xsize
,&res
);
155 ERR("stream write %x\n",hres
);
159 memset(&seekto
,0,sizeof(seekto
));
160 hres
= IStream_Seek(pStm
,seekto
,SEEK_SET
,&newpos
);
162 ERR("Failed Seek %x\n",hres
);
166 hres
= CoUnmarshalInterface(pStm
,riid
,(LPVOID
*)pUnk
);
168 ERR("Unmarshalling interface %s failed with %x\n",debugstr_guid(riid
),hres
);
172 IStream_Release(pStm
);
173 return xbuf_skip(buf
,xsize
);
177 _marshal_interface(marshal_state
*buf
, REFIID riid
, LPUNKNOWN pUnk
) {
178 LPBYTE tempbuf
= NULL
;
179 IStream
*pStm
= NULL
;
181 ULARGE_INTEGER newpos
;
182 LARGE_INTEGER seekto
;
188 /* this is valid, if for instance we serialize
189 * a VT_DISPATCH with NULL ptr which apparently
190 * can happen. S_OK to make sure we continue
193 WARN("pUnk is NULL\n");
195 return xbuf_add(buf
,(LPBYTE
)&xsize
,sizeof(xsize
));
200 TRACE("...%s...\n",debugstr_guid(riid
));
202 hres
= CreateStreamOnHGlobal(0,TRUE
,&pStm
);
204 ERR("Stream create failed %x\n",hres
);
208 hres
= CoMarshalInterface(pStm
,riid
,pUnk
,0,NULL
,0);
210 ERR("Marshalling interface %s failed with %x\n", debugstr_guid(riid
), hres
);
214 hres
= IStream_Stat(pStm
,&ststg
,STATFLAG_NONAME
);
216 ERR("Stream stat failed\n");
220 tempbuf
= HeapAlloc(GetProcessHeap(), 0, ststg
.cbSize
.u
.LowPart
);
221 memset(&seekto
,0,sizeof(seekto
));
222 hres
= IStream_Seek(pStm
,seekto
,SEEK_SET
,&newpos
);
224 ERR("Failed Seek %x\n",hres
);
228 hres
= IStream_Read(pStm
,tempbuf
,ststg
.cbSize
.u
.LowPart
,&res
);
230 ERR("Failed Read %x\n",hres
);
234 xsize
= ststg
.cbSize
.u
.LowPart
;
235 xbuf_add(buf
,(LPBYTE
)&xsize
,sizeof(xsize
));
236 hres
= xbuf_add(buf
,tempbuf
,ststg
.cbSize
.u
.LowPart
);
238 HeapFree(GetProcessHeap(),0,tempbuf
);
239 IStream_Release(pStm
);
245 xbuf_add(buf
,(LPBYTE
)&xsize
,sizeof(xsize
));
246 if (pStm
) IUnknown_Release(pStm
);
247 HeapFree(GetProcessHeap(), 0, tempbuf
);
251 /********************* OLE Proxy/Stub Factory ********************************/
252 static HRESULT WINAPI
253 PSFacBuf_QueryInterface(LPPSFACTORYBUFFER iface
, REFIID iid
, LPVOID
*ppv
) {
254 if (IsEqualIID(iid
,&IID_IPSFactoryBuffer
)||IsEqualIID(iid
,&IID_IUnknown
)) {
256 /* No ref counting, static class */
259 FIXME("(%s) unknown IID?\n",debugstr_guid(iid
));
260 return E_NOINTERFACE
;
263 static ULONG WINAPI
PSFacBuf_AddRef(LPPSFACTORYBUFFER iface
) { return 2; }
264 static ULONG WINAPI
PSFacBuf_Release(LPPSFACTORYBUFFER iface
) { return 1; }
267 _get_typeinfo_for_iid(REFIID riid
, ITypeInfo
**ti
) {
270 char tlguid
[200],typelibkey
[300],interfacekey
[300],ver
[100];
273 DWORD tlguidlen
, verlen
, type
;
277 sprintf( interfacekey
, "Interface\\{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\Typelib",
278 riid
->Data1
, riid
->Data2
, riid
->Data3
,
279 riid
->Data4
[0], riid
->Data4
[1], riid
->Data4
[2], riid
->Data4
[3],
280 riid
->Data4
[4], riid
->Data4
[5], riid
->Data4
[6], riid
->Data4
[7]
283 if (RegOpenKeyA(HKEY_CLASSES_ROOT
,interfacekey
,&ikey
)) {
284 ERR("No %s key found.\n",interfacekey
);
287 tlguidlen
= sizeof(tlguid
);
288 if (RegQueryValueExA(ikey
,NULL
,NULL
,&type
,(LPBYTE
)tlguid
,&tlguidlen
)) {
289 ERR("Getting typelib guid failed.\n");
293 verlen
= sizeof(ver
);
294 if (RegQueryValueExA(ikey
,"Version",NULL
,&type
,(LPBYTE
)ver
,&verlen
)) {
295 ERR("Could not get version value?\n");
300 sprintf(typelibkey
,"Typelib\\%s\\%s\\0\\win%u",tlguid
,ver
,(sizeof(void*) == 8) ? 64 : 32);
301 tlfnlen
= sizeof(tlfn
);
302 if (RegQueryValueA(HKEY_CLASSES_ROOT
,typelibkey
,tlfn
,&tlfnlen
)) {
303 ERR("Could not get typelib fn?\n");
306 MultiByteToWideChar(CP_ACP
, 0, tlfn
, -1, tlfnW
, sizeof(tlfnW
) / sizeof(tlfnW
[0]));
307 hres
= LoadTypeLib(tlfnW
,&tl
);
309 ERR("Failed to load typelib for %s, but it should be there.\n",debugstr_guid(riid
));
312 hres
= ITypeLib_GetTypeInfoOfGuid(tl
,riid
,ti
);
314 ERR("typelib does not contain info for %s?\n",debugstr_guid(riid
));
315 ITypeLib_Release(tl
);
318 ITypeLib_Release(tl
);
323 * Determine the number of functions including all inherited functions.
324 * Note for non-dual dispinterfaces we simply return the size of IDispatch.
326 static HRESULT
num_of_funcs(ITypeInfo
*tinfo
, unsigned int *num
)
333 hres
= ITypeInfo_GetTypeAttr(tinfo
, &attr
);
335 ERR("GetTypeAttr failed with %x\n",hres
);
339 if(attr
->typekind
== TKIND_DISPATCH
&& (attr
->wTypeFlags
& TYPEFLAG_FDUAL
))
342 hres
= ITypeInfo_GetRefTypeOfImplType(tinfo
, -1, &href
);
345 ERR("Unable to get interface href from dual dispinterface\n");
348 hres
= ITypeInfo_GetRefTypeInfo(tinfo
, href
, &tinfo2
);
351 ERR("Unable to get interface from dual dispinterface\n");
354 hres
= num_of_funcs(tinfo2
, num
);
355 ITypeInfo_Release(tinfo2
);
359 *num
= attr
->cbSizeVft
/ 4;
363 ITypeInfo_ReleaseTypeAttr(tinfo
, attr
);
369 #include "pshpack1.h"
371 typedef struct _TMAsmProxy
{
386 # warning You need to implement stubless proxies for your architecture
387 typedef struct _TMAsmProxy
{
391 typedef struct _TMProxyImpl
{
393 const IRpcProxyBufferVtbl
*lpvtbl2
;
396 TMAsmProxy
*asmstubs
;
398 IRpcChannelBuffer
* chanbuf
;
400 CRITICAL_SECTION crit
;
401 IUnknown
*outerunknown
;
403 IRpcProxyBuffer
*dispatch_proxy
;
406 static inline TMProxyImpl
*impl_from_IRpcProxyBuffer( IRpcProxyBuffer
*iface
)
408 return (TMProxyImpl
*)((char*)iface
- FIELD_OFFSET(TMProxyImpl
, lpvtbl2
));
411 static HRESULT WINAPI
412 TMProxyImpl_QueryInterface(LPRPCPROXYBUFFER iface
, REFIID riid
, LPVOID
*ppv
)
415 if (IsEqualIID(riid
,&IID_IUnknown
)||IsEqualIID(riid
,&IID_IRpcProxyBuffer
)) {
417 IRpcProxyBuffer_AddRef(iface
);
420 FIXME("no interface for %s\n",debugstr_guid(riid
));
421 return E_NOINTERFACE
;
425 TMProxyImpl_AddRef(LPRPCPROXYBUFFER iface
)
427 TMProxyImpl
*This
= impl_from_IRpcProxyBuffer( iface
);
428 ULONG refCount
= InterlockedIncrement(&This
->ref
);
430 TRACE("(%p)->(ref before=%u)\n",This
, refCount
- 1);
436 TMProxyImpl_Release(LPRPCPROXYBUFFER iface
)
438 TMProxyImpl
*This
= impl_from_IRpcProxyBuffer( iface
);
439 ULONG refCount
= InterlockedDecrement(&This
->ref
);
441 TRACE("(%p)->(ref before=%u)\n",This
, refCount
+ 1);
445 if (This
->dispatch_proxy
) IRpcProxyBuffer_Release(This
->dispatch_proxy
);
446 This
->crit
.DebugInfo
->Spare
[0] = 0;
447 DeleteCriticalSection(&This
->crit
);
448 if (This
->chanbuf
) IRpcChannelBuffer_Release(This
->chanbuf
);
449 VirtualFree(This
->asmstubs
, 0, MEM_RELEASE
);
450 HeapFree(GetProcessHeap(), 0, This
->lpvtbl
);
451 ITypeInfo_Release(This
->tinfo
);
457 static HRESULT WINAPI
459 LPRPCPROXYBUFFER iface
,IRpcChannelBuffer
* pRpcChannelBuffer
)
461 TMProxyImpl
*This
= impl_from_IRpcProxyBuffer( iface
);
463 TRACE("(%p)\n", pRpcChannelBuffer
);
465 EnterCriticalSection(&This
->crit
);
467 IRpcChannelBuffer_AddRef(pRpcChannelBuffer
);
468 This
->chanbuf
= pRpcChannelBuffer
;
470 LeaveCriticalSection(&This
->crit
);
472 if (This
->dispatch_proxy
)
474 IRpcChannelBuffer
*pDelegateChannel
;
475 HRESULT hr
= TMarshalDispatchChannel_Create(pRpcChannelBuffer
, &This
->iid
, &pDelegateChannel
);
478 hr
= IRpcProxyBuffer_Connect(This
->dispatch_proxy
, pDelegateChannel
);
479 IRpcChannelBuffer_Release(pDelegateChannel
);
487 TMProxyImpl_Disconnect(LPRPCPROXYBUFFER iface
)
489 TMProxyImpl
*This
= impl_from_IRpcProxyBuffer( iface
);
493 EnterCriticalSection(&This
->crit
);
495 IRpcChannelBuffer_Release(This
->chanbuf
);
496 This
->chanbuf
= NULL
;
498 LeaveCriticalSection(&This
->crit
);
500 if (This
->dispatch_proxy
)
501 IRpcProxyBuffer_Disconnect(This
->dispatch_proxy
);
505 static const IRpcProxyBufferVtbl tmproxyvtable
= {
506 TMProxyImpl_QueryInterface
,
510 TMProxyImpl_Disconnect
513 /* how much space do we use on stack in DWORD steps. */
515 _argsize(TYPEDESC
*tdesc
, ITypeInfo
*tinfo
) {
519 return 8/sizeof(DWORD
);
521 return sizeof(double)/sizeof(DWORD
);
523 return sizeof(CY
)/sizeof(DWORD
);
525 return sizeof(DATE
)/sizeof(DWORD
);
527 return (sizeof(DECIMAL
)+3)/sizeof(DWORD
);
529 return (sizeof(VARIANT
)+3)/sizeof(DWORD
);
537 hres
= ITypeInfo_GetRefTypeInfo(tinfo
,tdesc
->u
.hreftype
,&tinfo2
);
539 return 0; /* should fail critically in serialize_param */
540 ITypeInfo_GetTypeAttr(tinfo2
,&tattr
);
541 ret
= (tattr
->cbSizeInstance
+3)/sizeof(DWORD
);
542 ITypeInfo_ReleaseTypeAttr(tinfo2
, tattr
);
543 ITypeInfo_Release(tinfo2
);
551 /* how much space do we use on the heap (in bytes) */
553 _xsize(const TYPEDESC
*td
, ITypeInfo
*tinfo
) {
559 /* FIXME: VT_BOOL should return 2? */
561 return sizeof(VARIANT
)+3; /* FIXME: why the +3? */
564 const ARRAYDESC
*adesc
= td
->u
.lpadesc
;
566 for (i
=0;i
<adesc
->cDims
;i
++)
567 arrsize
*= adesc
->rgbounds
[i
].cElements
;
568 return arrsize
*_xsize(&adesc
->tdescElem
, tinfo
);
587 hres
= ITypeInfo_GetRefTypeInfo(tinfo
,td
->u
.hreftype
,&tinfo2
);
590 ITypeInfo_GetTypeAttr(tinfo2
,&tattr
);
591 ret
= tattr
->cbSizeInstance
;
592 ITypeInfo_ReleaseTypeAttr(tinfo2
, tattr
);
593 ITypeInfo_Release(tinfo2
);
614 TRACE("(tdesc.vt %s)\n",debugstr_vt(tdesc
->vt
));
617 if ((vartype
& 0xf000) == VT_ARRAY
)
618 vartype
= VT_SAFEARRAY
;
627 if (debugout
) TRACE_(olerelay
)("%x%x\n",arg
[0],arg
[1]);
629 hres
= xbuf_add(buf
,(LPBYTE
)arg
,8);
639 if (debugout
) TRACE_(olerelay
)("%x\n",*arg
);
641 hres
= xbuf_add(buf
,(LPBYTE
)arg
,sizeof(DWORD
));
646 if (debugout
) TRACE_(olerelay
)("%04x\n",*arg
& 0xffff);
648 hres
= xbuf_add(buf
,(LPBYTE
)arg
,sizeof(DWORD
));
653 if (debugout
) TRACE_(olerelay
)("%02x\n",*arg
& 0xff);
655 hres
= xbuf_add(buf
,(LPBYTE
)arg
,sizeof(DWORD
));
658 if (debugout
) TRACE_(olerelay
)("Vt(%s%s)(",debugstr_vt(V_VT((VARIANT
*)arg
)),debugstr_vf(V_VT((VARIANT
*)arg
)));
661 ULONG flags
= MAKELONG(MSHCTX_DIFFERENTMACHINE
, NDR_LOCAL_DATA_REPRESENTATION
);
662 ULONG size
= VARIANT_UserSize(&flags
, buf
->curoff
, (VARIANT
*)arg
);
663 xbuf_resize(buf
, size
);
664 VARIANT_UserMarshal(&flags
, buf
->base
+ buf
->curoff
, (VARIANT
*)arg
);
669 ULONG flags
= MAKELONG(MSHCTX_DIFFERENTMACHINE
, NDR_LOCAL_DATA_REPRESENTATION
);
670 VARIANT_UserFree(&flags
, (VARIANT
*)arg
);
677 TRACE_(olerelay
)("%s",relaystr((WCHAR
*)*arg
));
679 TRACE_(olerelay
)("<bstr NULL>");
683 ULONG flags
= MAKELONG(MSHCTX_DIFFERENTMACHINE
, NDR_LOCAL_DATA_REPRESENTATION
);
684 ULONG size
= BSTR_UserSize(&flags
, buf
->curoff
, (BSTR
*)arg
);
685 xbuf_resize(buf
, size
);
686 BSTR_UserMarshal(&flags
, buf
->base
+ buf
->curoff
, (BSTR
*)arg
);
691 ULONG flags
= MAKELONG(MSHCTX_DIFFERENTMACHINE
, NDR_LOCAL_DATA_REPRESENTATION
);
692 BSTR_UserFree(&flags
, (BSTR
*)arg
);
698 BOOL derefhere
= TRUE
;
700 if (tdesc
->u
.lptdesc
->vt
== VT_USERDEFINED
) {
704 hres
= ITypeInfo_GetRefTypeInfo(tinfo
,tdesc
->u
.lptdesc
->u
.hreftype
,&tinfo2
);
706 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc
->u
.lptdesc
->u
.hreftype
);
709 ITypeInfo_GetTypeAttr(tinfo2
,&tattr
);
710 switch (tattr
->typekind
) {
712 if (tattr
->tdescAlias
.vt
== VT_USERDEFINED
)
714 DWORD href
= tattr
->tdescAlias
.u
.hreftype
;
715 ITypeInfo_ReleaseTypeAttr(tinfo
, tattr
);
716 ITypeInfo_Release(tinfo2
);
717 hres
= ITypeInfo_GetRefTypeInfo(tinfo
,href
,&tinfo2
);
719 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc
->u
.lptdesc
->u
.hreftype
);
722 ITypeInfo_GetTypeAttr(tinfo2
,&tattr
);
723 derefhere
= (tattr
->typekind
!= TKIND_DISPATCH
&& tattr
->typekind
!= TKIND_INTERFACE
);
726 case TKIND_ENUM
: /* confirmed */
727 case TKIND_RECORD
: /* FIXME: mostly untested */
729 case TKIND_DISPATCH
: /* will be done in VT_USERDEFINED case */
730 case TKIND_INTERFACE
: /* will be done in VT_USERDEFINED case */
734 FIXME("unhandled switch cases tattr->typekind %d\n", tattr
->typekind
);
738 ITypeInfo_ReleaseTypeAttr(tinfo
, tattr
);
739 ITypeInfo_Release(tinfo2
);
742 if (debugout
) TRACE_(olerelay
)("*");
743 /* Write always, so the other side knows when it gets a NULL pointer.
745 cookie
= *arg
? 0x42424242 : 0;
746 hres
= xbuf_add(buf
,(LPBYTE
)&cookie
,sizeof(cookie
));
750 if (debugout
) TRACE_(olerelay
)("NULL");
753 hres
= serialize_param(tinfo
,writeit
,debugout
,dealloc
,tdesc
->u
.lptdesc
,(DWORD
*)*arg
,buf
);
754 if (derefhere
&& dealloc
) HeapFree(GetProcessHeap(),0,(LPVOID
)*arg
);
758 if (debugout
) TRACE_(olerelay
)("unk(0x%x)",*arg
);
760 hres
= _marshal_interface(buf
,&IID_IUnknown
,(LPUNKNOWN
)*arg
);
761 if (dealloc
&& *(IUnknown
**)arg
)
762 IUnknown_Release((LPUNKNOWN
)*arg
);
765 if (debugout
) TRACE_(olerelay
)("idisp(0x%x)",*arg
);
767 hres
= _marshal_interface(buf
,&IID_IDispatch
,(LPUNKNOWN
)*arg
);
768 if (dealloc
&& *(IUnknown
**)arg
)
769 IUnknown_Release((LPUNKNOWN
)*arg
);
772 if (debugout
) TRACE_(olerelay
)("<void>");
774 case VT_USERDEFINED
: {
778 hres
= ITypeInfo_GetRefTypeInfo(tinfo
,tdesc
->u
.hreftype
,&tinfo2
);
780 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc
->u
.hreftype
);
783 ITypeInfo_GetTypeAttr(tinfo2
,&tattr
);
784 switch (tattr
->typekind
) {
786 case TKIND_INTERFACE
:
788 hres
=_marshal_interface(buf
,&(tattr
->guid
),(LPUNKNOWN
)arg
);
790 IUnknown_Release((LPUNKNOWN
)arg
);
794 if (debugout
) TRACE_(olerelay
)("{");
795 for (i
=0;i
<tattr
->cVars
;i
++) {
800 hres
= ITypeInfo2_GetVarDesc(tinfo2
, i
, &vdesc
);
802 ERR("Could not get vardesc of %d\n",i
);
805 elem2
= &vdesc
->elemdescVar
;
806 tdesc2
= &elem2
->tdesc
;
807 hres
= serialize_param(
813 (DWORD
*)(((LPBYTE
)arg
)+vdesc
->u
.oInst
),
816 ITypeInfo_ReleaseVarDesc(tinfo2
, vdesc
);
819 if (debugout
&& (i
<(tattr
->cVars
-1)))
820 TRACE_(olerelay
)(",");
822 if (debugout
) TRACE_(olerelay
)("}");
826 hres
= serialize_param(tinfo2
,writeit
,debugout
,dealloc
,&tattr
->tdescAlias
,arg
,buf
);
830 if (debugout
) TRACE_(olerelay
)("%x",*arg
);
832 hres
= xbuf_add(buf
,(LPBYTE
)arg
,sizeof(DWORD
));
835 FIXME("Unhandled typekind %d\n",tattr
->typekind
);
839 ITypeInfo_ReleaseTypeAttr(tinfo2
, tattr
);
840 ITypeInfo_Release(tinfo2
);
844 ARRAYDESC
*adesc
= tdesc
->u
.lpadesc
;
847 if (debugout
) TRACE_(olerelay
)("carr");
848 for (i
=0;i
<adesc
->cDims
;i
++) {
849 if (debugout
) TRACE_(olerelay
)("[%d]",adesc
->rgbounds
[i
].cElements
);
850 arrsize
*= adesc
->rgbounds
[i
].cElements
;
852 if (debugout
) TRACE_(olerelay
)("(vt %s)",debugstr_vt(adesc
->tdescElem
.vt
));
853 if (debugout
) TRACE_(olerelay
)("[");
854 for (i
=0;i
<arrsize
;i
++) {
855 hres
= serialize_param(tinfo
, writeit
, debugout
, dealloc
, &adesc
->tdescElem
, (DWORD
*)((LPBYTE
)(*arg
)+i
*_xsize(&adesc
->tdescElem
, tinfo
)), buf
);
858 if (debugout
&& (i
<arrsize
-1)) TRACE_(olerelay
)(",");
860 if (debugout
) TRACE_(olerelay
)("]");
862 HeapFree(GetProcessHeap(), 0, *(void **)arg
);
868 ULONG flags
= MAKELONG(MSHCTX_DIFFERENTMACHINE
, NDR_LOCAL_DATA_REPRESENTATION
);
869 ULONG size
= LPSAFEARRAY_UserSize(&flags
, buf
->curoff
, (LPSAFEARRAY
*)arg
);
870 xbuf_resize(buf
, size
);
871 LPSAFEARRAY_UserMarshal(&flags
, buf
->base
+ buf
->curoff
, (LPSAFEARRAY
*)arg
);
876 ULONG flags
= MAKELONG(MSHCTX_DIFFERENTMACHINE
, NDR_LOCAL_DATA_REPRESENTATION
);
877 LPSAFEARRAY_UserFree(&flags
, (LPSAFEARRAY
*)arg
);
882 ERR("Unhandled marshal type %d.\n",tdesc
->vt
);
900 TRACE("vt %s at %p\n",debugstr_vt(tdesc
->vt
),arg
);
903 if ((vartype
& 0xf000) == VT_ARRAY
)
904 vartype
= VT_SAFEARRAY
;
911 ULONG flags
= MAKELONG(MSHCTX_DIFFERENTMACHINE
, NDR_LOCAL_DATA_REPRESENTATION
);
912 unsigned char *buffer
;
913 buffer
= VARIANT_UserUnmarshal(&flags
, buf
->base
+ buf
->curoff
, (VARIANT
*)arg
);
914 buf
->curoff
= buffer
- buf
->base
;
924 hres
= xbuf_get(buf
,(LPBYTE
)arg
,8);
925 if (hres
) ERR("Failed to read integer 8 byte\n");
927 if (debugout
) TRACE_(olerelay
)("%x%x",arg
[0],arg
[1]);
937 hres
= xbuf_get(buf
,(LPBYTE
)arg
,sizeof(DWORD
));
938 if (hres
) ERR("Failed to read integer 4 byte\n");
940 if (debugout
) TRACE_(olerelay
)("%x",*arg
);
946 hres
= xbuf_get(buf
,(LPBYTE
)&x
,sizeof(DWORD
));
947 if (hres
) ERR("Failed to read integer 4 byte\n");
950 if (debugout
) TRACE_(olerelay
)("%04x",*arg
& 0xffff);
956 hres
= xbuf_get(buf
,(LPBYTE
)&x
,sizeof(DWORD
));
957 if (hres
) ERR("Failed to read integer 4 byte\n");
960 if (debugout
) TRACE_(olerelay
)("%02x",*arg
& 0xff);
965 ULONG flags
= MAKELONG(MSHCTX_DIFFERENTMACHINE
, NDR_LOCAL_DATA_REPRESENTATION
);
966 unsigned char *buffer
;
967 buffer
= BSTR_UserUnmarshal(&flags
, buf
->base
+ buf
->curoff
, (BSTR
*)arg
);
968 buf
->curoff
= buffer
- buf
->base
;
969 if (debugout
) TRACE_(olerelay
)("%s",debugstr_w(*(BSTR
*)arg
));
975 BOOL derefhere
= TRUE
;
977 if (tdesc
->u
.lptdesc
->vt
== VT_USERDEFINED
) {
981 hres
= ITypeInfo_GetRefTypeInfo(tinfo
,tdesc
->u
.lptdesc
->u
.hreftype
,&tinfo2
);
983 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc
->u
.lptdesc
->u
.hreftype
);
986 ITypeInfo_GetTypeAttr(tinfo2
,&tattr
);
987 switch (tattr
->typekind
) {
989 if (tattr
->tdescAlias
.vt
== VT_USERDEFINED
)
991 DWORD href
= tattr
->tdescAlias
.u
.hreftype
;
992 ITypeInfo_ReleaseTypeAttr(tinfo
, tattr
);
993 ITypeInfo_Release(tinfo2
);
994 hres
= ITypeInfo_GetRefTypeInfo(tinfo
,href
,&tinfo2
);
996 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc
->u
.lptdesc
->u
.hreftype
);
999 ITypeInfo_GetTypeAttr(tinfo2
,&tattr
);
1000 derefhere
= (tattr
->typekind
!= TKIND_DISPATCH
&& tattr
->typekind
!= TKIND_INTERFACE
);
1003 case TKIND_ENUM
: /* confirmed */
1004 case TKIND_RECORD
: /* FIXME: mostly untested */
1006 case TKIND_DISPATCH
: /* will be done in VT_USERDEFINED case */
1007 case TKIND_INTERFACE
: /* will be done in VT_USERDEFINED case */
1011 FIXME("unhandled switch cases tattr->typekind %d\n", tattr
->typekind
);
1015 ITypeInfo_ReleaseTypeAttr(tinfo2
, tattr
);
1016 ITypeInfo_Release(tinfo2
);
1018 /* read it in all cases, we need to know if we have
1019 * NULL pointer or not.
1021 hres
= xbuf_get(buf
,(LPBYTE
)&cookie
,sizeof(cookie
));
1023 ERR("Failed to load pointer cookie.\n");
1026 if (cookie
!= 0x42424242) {
1027 /* we read a NULL ptr from the remote side */
1028 if (debugout
) TRACE_(olerelay
)("NULL");
1032 if (debugout
) TRACE_(olerelay
)("*");
1034 /* Allocate space for the referenced struct */
1036 *arg
=(DWORD
)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,_xsize(tdesc
->u
.lptdesc
, tinfo
));
1039 return deserialize_param(tinfo
, readit
, debugout
, alloc
, tdesc
->u
.lptdesc
, (LPDWORD
)*arg
, buf
);
1041 return deserialize_param(tinfo
, readit
, debugout
, alloc
, tdesc
->u
.lptdesc
, arg
, buf
);
1044 /* FIXME: UNKNOWN is unknown ..., but allocate 4 byte for it */
1046 *arg
=(DWORD
)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(DWORD
));
1049 hres
= _unmarshal_interface(buf
,&IID_IUnknown
,(LPUNKNOWN
*)arg
);
1051 TRACE_(olerelay
)("unk(%p)",arg
);
1056 hres
= _unmarshal_interface(buf
,&IID_IDispatch
,(LPUNKNOWN
*)arg
);
1058 TRACE_(olerelay
)("idisp(%p)",arg
);
1061 if (debugout
) TRACE_(olerelay
)("<void>");
1063 case VT_USERDEFINED
: {
1067 hres
= ITypeInfo_GetRefTypeInfo(tinfo
,tdesc
->u
.hreftype
,&tinfo2
);
1069 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc
->u
.hreftype
);
1072 hres
= ITypeInfo_GetTypeAttr(tinfo2
,&tattr
);
1074 ERR("Could not get typeattr in VT_USERDEFINED.\n");
1076 switch (tattr
->typekind
) {
1077 case TKIND_DISPATCH
:
1078 case TKIND_INTERFACE
:
1080 hres
= _unmarshal_interface(buf
,&(tattr
->guid
),(LPUNKNOWN
*)arg
);
1082 case TKIND_RECORD
: {
1085 if (debugout
) TRACE_(olerelay
)("{");
1086 for (i
=0;i
<tattr
->cVars
;i
++) {
1089 hres
= ITypeInfo2_GetVarDesc(tinfo2
, i
, &vdesc
);
1091 ERR("Could not get vardesc of %d\n",i
);
1092 ITypeInfo_ReleaseTypeAttr(tinfo2
, tattr
);
1093 ITypeInfo_Release(tinfo2
);
1096 hres
= deserialize_param(
1101 &vdesc
->elemdescVar
.tdesc
,
1102 (DWORD
*)(((LPBYTE
)arg
)+vdesc
->u
.oInst
),
1105 ITypeInfo2_ReleaseVarDesc(tinfo2
, vdesc
);
1106 if (debugout
&& (i
<tattr
->cVars
-1)) TRACE_(olerelay
)(",");
1108 if (debugout
) TRACE_(olerelay
)("}");
1112 hres
= deserialize_param(tinfo2
,readit
,debugout
,alloc
,&tattr
->tdescAlias
,arg
,buf
);
1116 hres
= xbuf_get(buf
,(LPBYTE
)arg
,sizeof(DWORD
));
1117 if (hres
) ERR("Failed to read enum (4 byte)\n");
1119 if (debugout
) TRACE_(olerelay
)("%x",*arg
);
1122 ERR("Unhandled typekind %d\n",tattr
->typekind
);
1126 ITypeInfo_ReleaseTypeAttr(tinfo2
, tattr
);
1129 ERR("failed to stuballoc in TKIND_RECORD.\n");
1130 ITypeInfo_Release(tinfo2
);
1134 /* arg is pointing to the start of the array. */
1135 ARRAYDESC
*adesc
= tdesc
->u
.lpadesc
;
1138 if (adesc
->cDims
> 1) FIXME("cDims > 1 in VT_CARRAY. Does it work?\n");
1139 for (i
=0;i
<adesc
->cDims
;i
++)
1140 arrsize
*= adesc
->rgbounds
[i
].cElements
;
1141 *arg
=(DWORD
)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,_xsize(tdesc
->u
.lptdesc
, tinfo
) * arrsize
);
1142 for (i
=0;i
<arrsize
;i
++)
1149 (DWORD
*)((LPBYTE
)(*arg
)+i
*_xsize(&adesc
->tdescElem
, tinfo
)),
1154 case VT_SAFEARRAY
: {
1157 ULONG flags
= MAKELONG(MSHCTX_DIFFERENTMACHINE
, NDR_LOCAL_DATA_REPRESENTATION
);
1158 unsigned char *buffer
;
1159 buffer
= LPSAFEARRAY_UserUnmarshal(&flags
, buf
->base
+ buf
->curoff
, (LPSAFEARRAY
*)arg
);
1160 buf
->curoff
= buffer
- buf
->base
;
1165 ERR("No handler for VT type %d!\n",tdesc
->vt
);
1171 /* Retrieves a function's funcdesc, searching back into inherited interfaces. */
1172 static HRESULT
get_funcdesc(ITypeInfo
*tinfo
, int iMethod
, ITypeInfo
**tactual
, const FUNCDESC
**fdesc
,
1173 BSTR
*iname
, BSTR
*fname
, UINT
*num
)
1177 UINT inherited_funcs
= 0;
1180 if (fname
) *fname
= NULL
;
1181 if (iname
) *iname
= NULL
;
1185 hr
= ITypeInfo_GetTypeAttr(tinfo
, &attr
);
1188 ERR("GetTypeAttr failed with %x\n",hr
);
1192 if(attr
->typekind
== TKIND_DISPATCH
)
1194 if(attr
->wTypeFlags
& TYPEFLAG_FDUAL
)
1199 hr
= ITypeInfo_GetRefTypeOfImplType(tinfo
, -1, &href
);
1202 ERR("Cannot get interface href from dual dispinterface\n");
1203 ITypeInfo_ReleaseTypeAttr(tinfo
, attr
);
1206 hr
= ITypeInfo_GetRefTypeInfo(tinfo
, href
, &tinfo2
);
1209 ERR("Cannot get interface from dual dispinterface\n");
1210 ITypeInfo_ReleaseTypeAttr(tinfo
, attr
);
1213 hr
= get_funcdesc(tinfo2
, iMethod
, tactual
, fdesc
, iname
, fname
, num
);
1214 ITypeInfo_Release(tinfo2
);
1215 ITypeInfo_ReleaseTypeAttr(tinfo
, attr
);
1218 ERR("Shouldn't be called with a non-dual dispinterface\n");
1222 impl_types
= attr
->cImplTypes
;
1223 ITypeInfo_ReleaseTypeAttr(tinfo
, attr
);
1225 for (i
= 0; i
< impl_types
; i
++)
1228 ITypeInfo
*pSubTypeInfo
;
1231 hr
= ITypeInfo_GetRefTypeOfImplType(tinfo
, i
, &href
);
1232 if (FAILED(hr
)) return hr
;
1233 hr
= ITypeInfo_GetRefTypeInfo(tinfo
, href
, &pSubTypeInfo
);
1234 if (FAILED(hr
)) return hr
;
1236 hr
= get_funcdesc(pSubTypeInfo
, iMethod
, tactual
, fdesc
, iname
, fname
, &sub_funcs
);
1237 inherited_funcs
+= sub_funcs
;
1238 ITypeInfo_Release(pSubTypeInfo
);
1239 if(SUCCEEDED(hr
)) return hr
;
1241 if(iMethod
< inherited_funcs
)
1243 ERR("shouldn't be here\n");
1244 return E_INVALIDARG
;
1247 for(i
= inherited_funcs
; i
<= iMethod
; i
++)
1249 hr
= ITypeInfoImpl_GetInternalFuncDesc(tinfo
, i
- inherited_funcs
, fdesc
);
1257 /* found it. We don't care about num so zero it */
1260 ITypeInfo_AddRef(*tactual
);
1261 if (fname
) ITypeInfo_GetDocumentation(tinfo
,(*fdesc
)->memid
,fname
,NULL
,NULL
,NULL
);
1262 if (iname
) ITypeInfo_GetDocumentation(tinfo
,-1,iname
,NULL
,NULL
,NULL
);
1266 static inline BOOL
is_in_elem(const ELEMDESC
*elem
)
1268 return (elem
->u
.paramdesc
.wParamFlags
& PARAMFLAG_FIN
|| !elem
->u
.paramdesc
.wParamFlags
);
1271 static inline BOOL
is_out_elem(const ELEMDESC
*elem
)
1273 return (elem
->u
.paramdesc
.wParamFlags
& PARAMFLAG_FOUT
|| !elem
->u
.paramdesc
.wParamFlags
);
1277 xCall(LPVOID retptr
, int method
, TMProxyImpl
*tpinfo
/*, args */)
1279 DWORD
*args
= ((DWORD
*)&tpinfo
)+1, *xargs
;
1280 const FUNCDESC
*fdesc
;
1282 int i
, relaydeb
= TRACE_ON(olerelay
);
1289 DWORD remoteresult
= 0;
1291 IRpcChannelBuffer
*chanbuf
;
1293 EnterCriticalSection(&tpinfo
->crit
);
1295 hres
= get_funcdesc(tpinfo
->tinfo
,method
,&tinfo
,&fdesc
,&iname
,&fname
,NULL
);
1297 ERR("Did not find typeinfo/funcdesc entry for method %d!\n",method
);
1298 LeaveCriticalSection(&tpinfo
->crit
);
1302 if (!tpinfo
->chanbuf
)
1304 WARN("Tried to use disconnected proxy\n");
1305 ITypeInfo_Release(tinfo
);
1306 LeaveCriticalSection(&tpinfo
->crit
);
1307 return RPC_E_DISCONNECTED
;
1309 chanbuf
= tpinfo
->chanbuf
;
1310 IRpcChannelBuffer_AddRef(chanbuf
);
1312 LeaveCriticalSection(&tpinfo
->crit
);
1315 TRACE_(olerelay
)("->");
1317 TRACE_(olerelay
)("%s:",relaystr(iname
));
1319 TRACE_(olerelay
)("%s(%d)",relaystr(fname
),method
);
1321 TRACE_(olerelay
)("%d",method
);
1322 TRACE_(olerelay
)("(");
1325 SysFreeString(iname
);
1326 SysFreeString(fname
);
1328 memset(&buf
,0,sizeof(buf
));
1330 /* normal typelib driven serializing */
1332 /* Need them for hack below */
1333 memset(names
,0,sizeof(names
));
1334 if (ITypeInfo_GetNames(tinfo
,fdesc
->memid
,names
,sizeof(names
)/sizeof(names
[0]),&nrofnames
))
1336 if (nrofnames
> sizeof(names
)/sizeof(names
[0]))
1337 ERR("Need more names!\n");
1340 for (i
=0;i
<fdesc
->cParams
;i
++) {
1341 ELEMDESC
*elem
= fdesc
->lprgelemdescParam
+i
;
1343 if (i
) TRACE_(olerelay
)(",");
1344 if (i
+1<nrofnames
&& names
[i
+1])
1345 TRACE_(olerelay
)("%s=",relaystr(names
[i
+1]));
1347 /* No need to marshal other data than FIN and any VT_PTR. */
1348 if (!is_in_elem(elem
) && (elem
->tdesc
.vt
!= VT_PTR
)) {
1349 xargs
+=_argsize(&elem
->tdesc
, tinfo
);
1350 if (relaydeb
) TRACE_(olerelay
)("[out]");
1353 hres
= serialize_param(
1364 ERR("Failed to serialize param, hres %x\n",hres
);
1367 xargs
+=_argsize(&elem
->tdesc
, tinfo
);
1369 if (relaydeb
) TRACE_(olerelay
)(")");
1371 memset(&msg
,0,sizeof(msg
));
1372 msg
.cbBuffer
= buf
.curoff
;
1373 msg
.iMethod
= method
;
1374 hres
= IRpcChannelBuffer_GetBuffer(chanbuf
,&msg
,&(tpinfo
->iid
));
1376 ERR("RpcChannelBuffer GetBuffer failed, %x\n",hres
);
1379 memcpy(msg
.Buffer
,buf
.base
,buf
.curoff
);
1380 if (relaydeb
) TRACE_(olerelay
)("\n");
1381 hres
= IRpcChannelBuffer_SendReceive(chanbuf
,&msg
,&status
);
1383 ERR("RpcChannelBuffer SendReceive failed, %x\n",hres
);
1387 if (relaydeb
) TRACE_(olerelay
)(" status = %08x (",status
);
1389 buf
.base
= HeapReAlloc(GetProcessHeap(),0,buf
.base
,msg
.cbBuffer
);
1391 buf
.base
= HeapAlloc(GetProcessHeap(),0,msg
.cbBuffer
);
1392 buf
.size
= msg
.cbBuffer
;
1393 memcpy(buf
.base
,msg
.Buffer
,buf
.size
);
1396 /* generic deserializer using typelib description */
1399 for (i
=0;i
<fdesc
->cParams
;i
++) {
1400 ELEMDESC
*elem
= fdesc
->lprgelemdescParam
+i
;
1403 if (i
) TRACE_(olerelay
)(",");
1404 if (i
+1<nrofnames
&& names
[i
+1]) TRACE_(olerelay
)("%s=",relaystr(names
[i
+1]));
1406 /* No need to marshal other data than FOUT and any VT_PTR */
1407 if (!is_out_elem(elem
) && (elem
->tdesc
.vt
!= VT_PTR
)) {
1408 xargs
+= _argsize(&elem
->tdesc
, tinfo
);
1409 if (relaydeb
) TRACE_(olerelay
)("[in]");
1412 hres
= deserialize_param(
1422 ERR("Failed to unmarshall param, hres %x\n",hres
);
1426 xargs
+= _argsize(&elem
->tdesc
, tinfo
);
1429 hres
= xbuf_get(&buf
, (LPBYTE
)&remoteresult
, sizeof(DWORD
));
1432 if (relaydeb
) TRACE_(olerelay
)(") = %08x\n", remoteresult
);
1434 hres
= remoteresult
;
1437 IRpcChannelBuffer_FreeBuffer(chanbuf
,&msg
);
1438 for (i
= 0; i
< nrofnames
; i
++)
1439 SysFreeString(names
[i
]);
1440 HeapFree(GetProcessHeap(),0,buf
.base
);
1441 IRpcChannelBuffer_Release(chanbuf
);
1442 ITypeInfo_Release(tinfo
);
1443 TRACE("-- 0x%08x\n", hres
);
1447 static HRESULT WINAPI
ProxyIUnknown_QueryInterface(IUnknown
*iface
, REFIID riid
, void **ppv
)
1449 TMProxyImpl
*proxy
= (TMProxyImpl
*)iface
;
1451 TRACE("(%s, %p)\n", debugstr_guid(riid
), ppv
);
1453 if (proxy
->outerunknown
)
1454 return IUnknown_QueryInterface(proxy
->outerunknown
, riid
, ppv
);
1456 FIXME("No interface\n");
1457 return E_NOINTERFACE
;
1460 static ULONG WINAPI
ProxyIUnknown_AddRef(IUnknown
*iface
)
1462 TMProxyImpl
*proxy
= (TMProxyImpl
*)iface
;
1466 if (proxy
->outerunknown
)
1467 return IUnknown_AddRef(proxy
->outerunknown
);
1469 return 2; /* FIXME */
1472 static ULONG WINAPI
ProxyIUnknown_Release(IUnknown
*iface
)
1474 TMProxyImpl
*proxy
= (TMProxyImpl
*)iface
;
1478 if (proxy
->outerunknown
)
1479 return IUnknown_Release(proxy
->outerunknown
);
1481 return 1; /* FIXME */
1484 static HRESULT WINAPI
ProxyIDispatch_GetTypeInfoCount(LPDISPATCH iface
, UINT
* pctinfo
)
1486 TMProxyImpl
*This
= (TMProxyImpl
*)iface
;
1488 TRACE("(%p)\n", pctinfo
);
1490 return IDispatch_GetTypeInfoCount(This
->dispatch
, pctinfo
);
1493 static HRESULT WINAPI
ProxyIDispatch_GetTypeInfo(LPDISPATCH iface
, UINT iTInfo
, LCID lcid
, ITypeInfo
** ppTInfo
)
1495 TMProxyImpl
*This
= (TMProxyImpl
*)iface
;
1497 TRACE("(%d, %x, %p)\n", iTInfo
, lcid
, ppTInfo
);
1499 return IDispatch_GetTypeInfo(This
->dispatch
, iTInfo
, lcid
, ppTInfo
);
1502 static HRESULT WINAPI
ProxyIDispatch_GetIDsOfNames(LPDISPATCH iface
, REFIID riid
, LPOLESTR
* rgszNames
, UINT cNames
, LCID lcid
, DISPID
* rgDispId
)
1504 TMProxyImpl
*This
= (TMProxyImpl
*)iface
;
1506 TRACE("(%s, %p, %d, 0x%x, %p)\n", debugstr_guid(riid
), rgszNames
, cNames
, lcid
, rgDispId
);
1508 return IDispatch_GetIDsOfNames(This
->dispatch
, riid
, rgszNames
,
1509 cNames
, lcid
, rgDispId
);
1512 static HRESULT WINAPI
ProxyIDispatch_Invoke(LPDISPATCH iface
, DISPID dispIdMember
, REFIID riid
, LCID lcid
,
1513 WORD wFlags
, DISPPARAMS
* pDispParams
, VARIANT
* pVarResult
,
1514 EXCEPINFO
* pExcepInfo
, UINT
* puArgErr
)
1516 TMProxyImpl
*This
= (TMProxyImpl
*)iface
;
1518 TRACE("(%d, %s, 0x%x, 0x%x, %p, %p, %p, %p)\n", dispIdMember
,
1519 debugstr_guid(riid
), lcid
, wFlags
, pDispParams
, pVarResult
,
1520 pExcepInfo
, puArgErr
);
1522 return IDispatch_Invoke(This
->dispatch
, dispIdMember
, riid
, lcid
,
1523 wFlags
, pDispParams
, pVarResult
, pExcepInfo
,
1529 const IRpcChannelBufferVtbl
*lpVtbl
;
1531 /* the IDispatch-derived interface we are handling */
1533 IRpcChannelBuffer
*pDelegateChannel
;
1534 } TMarshalDispatchChannel
;
1536 static HRESULT WINAPI
TMarshalDispatchChannel_QueryInterface(LPRPCCHANNELBUFFER iface
, REFIID riid
, LPVOID
*ppv
)
1539 if (IsEqualIID(riid
,&IID_IRpcChannelBuffer
) || IsEqualIID(riid
,&IID_IUnknown
))
1542 IUnknown_AddRef(iface
);
1545 return E_NOINTERFACE
;
1548 static ULONG WINAPI
TMarshalDispatchChannel_AddRef(LPRPCCHANNELBUFFER iface
)
1550 TMarshalDispatchChannel
*This
= (TMarshalDispatchChannel
*)iface
;
1551 return InterlockedIncrement(&This
->refs
);
1554 static ULONG WINAPI
TMarshalDispatchChannel_Release(LPRPCCHANNELBUFFER iface
)
1556 TMarshalDispatchChannel
*This
= (TMarshalDispatchChannel
*)iface
;
1559 ref
= InterlockedDecrement(&This
->refs
);
1563 IRpcChannelBuffer_Release(This
->pDelegateChannel
);
1564 HeapFree(GetProcessHeap(), 0, This
);
1568 static HRESULT WINAPI
TMarshalDispatchChannel_GetBuffer(LPRPCCHANNELBUFFER iface
, RPCOLEMESSAGE
* olemsg
, REFIID riid
)
1570 TMarshalDispatchChannel
*This
= (TMarshalDispatchChannel
*)iface
;
1571 TRACE("(%p, %s)\n", olemsg
, debugstr_guid(riid
));
1572 /* Note: we are pretending to invoke a method on the interface identified
1573 * by tmarshal_iid so that we can re-use the IDispatch proxy/stub code
1574 * without the RPC runtime getting confused by not exporting an IDispatch interface */
1575 return IRpcChannelBuffer_GetBuffer(This
->pDelegateChannel
, olemsg
, &This
->tmarshal_iid
);
1578 static HRESULT WINAPI
TMarshalDispatchChannel_SendReceive(LPRPCCHANNELBUFFER iface
, RPCOLEMESSAGE
*olemsg
, ULONG
*pstatus
)
1580 TMarshalDispatchChannel
*This
= (TMarshalDispatchChannel
*)iface
;
1581 TRACE("(%p, %p)\n", olemsg
, pstatus
);
1582 return IRpcChannelBuffer_SendReceive(This
->pDelegateChannel
, olemsg
, pstatus
);
1585 static HRESULT WINAPI
TMarshalDispatchChannel_FreeBuffer(LPRPCCHANNELBUFFER iface
, RPCOLEMESSAGE
* olemsg
)
1587 TMarshalDispatchChannel
*This
= (TMarshalDispatchChannel
*)iface
;
1588 TRACE("(%p)\n", olemsg
);
1589 return IRpcChannelBuffer_FreeBuffer(This
->pDelegateChannel
, olemsg
);
1592 static HRESULT WINAPI
TMarshalDispatchChannel_GetDestCtx(LPRPCCHANNELBUFFER iface
, DWORD
* pdwDestContext
, void** ppvDestContext
)
1594 TMarshalDispatchChannel
*This
= (TMarshalDispatchChannel
*)iface
;
1595 TRACE("(%p,%p)\n", pdwDestContext
, ppvDestContext
);
1596 return IRpcChannelBuffer_GetDestCtx(This
->pDelegateChannel
, pdwDestContext
, ppvDestContext
);
1599 static HRESULT WINAPI
TMarshalDispatchChannel_IsConnected(LPRPCCHANNELBUFFER iface
)
1601 TMarshalDispatchChannel
*This
= (TMarshalDispatchChannel
*)iface
;
1603 return IRpcChannelBuffer_IsConnected(This
->pDelegateChannel
);
1606 static const IRpcChannelBufferVtbl TMarshalDispatchChannelVtbl
=
1608 TMarshalDispatchChannel_QueryInterface
,
1609 TMarshalDispatchChannel_AddRef
,
1610 TMarshalDispatchChannel_Release
,
1611 TMarshalDispatchChannel_GetBuffer
,
1612 TMarshalDispatchChannel_SendReceive
,
1613 TMarshalDispatchChannel_FreeBuffer
,
1614 TMarshalDispatchChannel_GetDestCtx
,
1615 TMarshalDispatchChannel_IsConnected
1618 static HRESULT
TMarshalDispatchChannel_Create(
1619 IRpcChannelBuffer
*pDelegateChannel
, REFIID tmarshal_riid
,
1620 IRpcChannelBuffer
**ppChannel
)
1622 TMarshalDispatchChannel
*This
= HeapAlloc(GetProcessHeap(), 0, sizeof(*This
));
1624 return E_OUTOFMEMORY
;
1626 This
->lpVtbl
= &TMarshalDispatchChannelVtbl
;
1628 IRpcChannelBuffer_AddRef(pDelegateChannel
);
1629 This
->pDelegateChannel
= pDelegateChannel
;
1630 This
->tmarshal_iid
= *tmarshal_riid
;
1632 *ppChannel
= (IRpcChannelBuffer
*)&This
->lpVtbl
;
1637 static inline HRESULT
get_facbuf_for_iid(REFIID riid
, IPSFactoryBuffer
**facbuf
)
1642 if ((hr
= CoGetPSClsid(riid
, &clsid
)))
1644 return CoGetClassObject(&clsid
, CLSCTX_INPROC_SERVER
, NULL
,
1645 &IID_IPSFactoryBuffer
, (LPVOID
*)facbuf
);
1648 static HRESULT
init_proxy_entry_point(TMProxyImpl
*proxy
, unsigned int num
)
1651 /* nrofargs without This */
1654 TMAsmProxy
*xasm
= proxy
->asmstubs
+ num
;
1656 const FUNCDESC
*fdesc
;
1658 hres
= get_funcdesc(proxy
->tinfo
, num
, &tinfo2
, &fdesc
, NULL
, NULL
, NULL
);
1660 ERR("GetFuncDesc %x should not fail here.\n",hres
);
1663 ITypeInfo_Release(tinfo2
);
1664 /* some args take more than 4 byte on the stack */
1666 for (j
=0;j
<fdesc
->cParams
;j
++)
1667 nrofargs
+= _argsize(&fdesc
->lprgelemdescParam
[j
].tdesc
, proxy
->tinfo
);
1670 if (fdesc
->callconv
!= CC_STDCALL
) {
1671 ERR("calling convention is not stdcall????\n");
1674 /* popl %eax - return ptr
1681 * arg3 arg2 arg1 <method> <returnptr>
1683 xasm
->popleax
= 0x58;
1684 xasm
->pushlval
= 0x68;
1686 xasm
->pushleax
= 0x50;
1687 xasm
->lcall
= 0xe8; /* relative jump */
1688 xasm
->xcall
= (DWORD
)xCall
;
1689 xasm
->xcall
-= (DWORD
)&(xasm
->lret
);
1691 xasm
->bytestopop
= (nrofargs
+2)*4; /* pop args, This, iMethod */
1693 proxy
->lpvtbl
[num
] = xasm
;
1695 FIXME("not implemented on non i386\n");
1701 static HRESULT WINAPI
1702 PSFacBuf_CreateProxy(
1703 LPPSFACTORYBUFFER iface
, IUnknown
* pUnkOuter
, REFIID riid
,
1704 IRpcProxyBuffer
**ppProxy
, LPVOID
*ppv
)
1708 unsigned int i
, nroffuncs
;
1711 BOOL defer_to_dispatch
= FALSE
;
1713 TRACE("(...%s...)\n",debugstr_guid(riid
));
1714 hres
= _get_typeinfo_for_iid(riid
,&tinfo
);
1716 ERR("No typeinfo for %s?\n",debugstr_guid(riid
));
1720 hres
= num_of_funcs(tinfo
, &nroffuncs
);
1722 ERR("Cannot get number of functions for typeinfo %s\n",debugstr_guid(riid
));
1723 ITypeInfo_Release(tinfo
);
1727 proxy
= CoTaskMemAlloc(sizeof(TMProxyImpl
));
1728 if (!proxy
) return E_OUTOFMEMORY
;
1730 assert(sizeof(TMAsmProxy
) == 16);
1732 proxy
->dispatch
= NULL
;
1733 proxy
->dispatch_proxy
= NULL
;
1734 proxy
->outerunknown
= pUnkOuter
;
1735 proxy
->asmstubs
= VirtualAlloc(NULL
, sizeof(TMAsmProxy
) * nroffuncs
, MEM_COMMIT
, PAGE_EXECUTE_READWRITE
);
1736 if (!proxy
->asmstubs
) {
1737 ERR("Could not commit pages for proxy thunks\n");
1738 CoTaskMemFree(proxy
);
1739 return E_OUTOFMEMORY
;
1741 proxy
->lpvtbl2
= &tmproxyvtable
;
1742 /* one reference for the proxy */
1744 proxy
->tinfo
= tinfo
;
1748 InitializeCriticalSection(&proxy
->crit
);
1749 proxy
->crit
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": TMProxyImpl.crit");
1751 proxy
->lpvtbl
= HeapAlloc(GetProcessHeap(),0,sizeof(LPBYTE
)*nroffuncs
);
1753 /* if we derive from IDispatch then defer to its proxy for its methods */
1754 hres
= ITypeInfo_GetTypeAttr(tinfo
, &typeattr
);
1757 if (typeattr
->wTypeFlags
& TYPEFLAG_FDISPATCHABLE
)
1759 IPSFactoryBuffer
*factory_buffer
;
1760 hres
= get_facbuf_for_iid(&IID_IDispatch
, &factory_buffer
);
1763 hres
= IPSFactoryBuffer_CreateProxy(factory_buffer
, NULL
,
1764 &IID_IDispatch
, &proxy
->dispatch_proxy
,
1765 (void **)&proxy
->dispatch
);
1766 IPSFactoryBuffer_Release(factory_buffer
);
1768 if ((hres
== S_OK
) && (nroffuncs
< 7))
1770 ERR("nroffuncs calculated incorrectly (%d)\n", nroffuncs
);
1771 hres
= E_UNEXPECTED
;
1775 defer_to_dispatch
= TRUE
;
1778 ITypeInfo_ReleaseTypeAttr(tinfo
, typeattr
);
1781 for (i
=0;i
<nroffuncs
;i
++) {
1784 proxy
->lpvtbl
[i
] = ProxyIUnknown_QueryInterface
;
1787 proxy
->lpvtbl
[i
] = ProxyIUnknown_AddRef
;
1790 proxy
->lpvtbl
[i
] = ProxyIUnknown_Release
;
1793 if(!defer_to_dispatch
)
1795 hres
= init_proxy_entry_point(proxy
, i
);
1796 if(FAILED(hres
)) return hres
;
1798 else proxy
->lpvtbl
[3] = ProxyIDispatch_GetTypeInfoCount
;
1801 if(!defer_to_dispatch
)
1803 hres
= init_proxy_entry_point(proxy
, i
);
1804 if(FAILED(hres
)) return hres
;
1806 else proxy
->lpvtbl
[4] = ProxyIDispatch_GetTypeInfo
;
1809 if(!defer_to_dispatch
)
1811 hres
= init_proxy_entry_point(proxy
, i
);
1812 if(FAILED(hres
)) return hres
;
1814 else proxy
->lpvtbl
[5] = ProxyIDispatch_GetIDsOfNames
;
1817 if(!defer_to_dispatch
)
1819 hres
= init_proxy_entry_point(proxy
, i
);
1820 if(FAILED(hres
)) return hres
;
1822 else proxy
->lpvtbl
[6] = ProxyIDispatch_Invoke
;
1825 hres
= init_proxy_entry_point(proxy
, i
);
1826 if(FAILED(hres
)) return hres
;
1833 *ppProxy
= (IRpcProxyBuffer
*)&(proxy
->lpvtbl2
);
1834 IUnknown_AddRef((IUnknown
*)*ppv
);
1838 TMProxyImpl_Release((IRpcProxyBuffer
*)&proxy
->lpvtbl2
);
1842 typedef struct _TMStubImpl
{
1843 const IRpcStubBufferVtbl
*lpvtbl
;
1849 IRpcStubBuffer
*dispatch_stub
;
1850 BOOL dispatch_derivative
;
1853 static HRESULT WINAPI
1854 TMStubImpl_QueryInterface(LPRPCSTUBBUFFER iface
, REFIID riid
, LPVOID
*ppv
)
1856 if (IsEqualIID(riid
,&IID_IRpcStubBuffer
)||IsEqualIID(riid
,&IID_IUnknown
)){
1858 IRpcStubBuffer_AddRef(iface
);
1861 FIXME("%s, not supported IID.\n",debugstr_guid(riid
));
1862 return E_NOINTERFACE
;
1866 TMStubImpl_AddRef(LPRPCSTUBBUFFER iface
)
1868 TMStubImpl
*This
= (TMStubImpl
*)iface
;
1869 ULONG refCount
= InterlockedIncrement(&This
->ref
);
1871 TRACE("(%p)->(ref before=%u)\n", This
, refCount
- 1);
1877 TMStubImpl_Release(LPRPCSTUBBUFFER iface
)
1879 TMStubImpl
*This
= (TMStubImpl
*)iface
;
1880 ULONG refCount
= InterlockedDecrement(&This
->ref
);
1882 TRACE("(%p)->(ref before=%u)\n", This
, refCount
+ 1);
1886 IRpcStubBuffer_Disconnect(iface
);
1887 ITypeInfo_Release(This
->tinfo
);
1888 if (This
->dispatch_stub
)
1889 IRpcStubBuffer_Release(This
->dispatch_stub
);
1890 CoTaskMemFree(This
);
1895 static HRESULT WINAPI
1896 TMStubImpl_Connect(LPRPCSTUBBUFFER iface
, LPUNKNOWN pUnkServer
)
1898 TMStubImpl
*This
= (TMStubImpl
*)iface
;
1900 TRACE("(%p)->(%p)\n", This
, pUnkServer
);
1902 IUnknown_AddRef(pUnkServer
);
1903 This
->pUnk
= pUnkServer
;
1905 if (This
->dispatch_stub
)
1906 IRpcStubBuffer_Connect(This
->dispatch_stub
, pUnkServer
);
1912 TMStubImpl_Disconnect(LPRPCSTUBBUFFER iface
)
1914 TMStubImpl
*This
= (TMStubImpl
*)iface
;
1916 TRACE("(%p)->()\n", This
);
1920 IUnknown_Release(This
->pUnk
);
1924 if (This
->dispatch_stub
)
1925 IRpcStubBuffer_Disconnect(This
->dispatch_stub
);
1928 static HRESULT WINAPI
1930 LPRPCSTUBBUFFER iface
, RPCOLEMESSAGE
* xmsg
,IRpcChannelBuffer
*rpcchanbuf
)
1934 const FUNCDESC
*fdesc
;
1935 TMStubImpl
*This
= (TMStubImpl
*)iface
;
1937 DWORD
*args
= NULL
, res
, *xargs
, nrofargs
;
1942 ITypeInfo
*tinfo
= NULL
;
1946 if (xmsg
->iMethod
< 3) {
1947 ERR("IUnknown methods cannot be marshaled by the typelib marshaler\n");
1948 return E_UNEXPECTED
;
1951 if (This
->dispatch_derivative
&& xmsg
->iMethod
< sizeof(IDispatchVtbl
)/sizeof(void *))
1953 IPSFactoryBuffer
*factory_buffer
;
1954 hres
= get_facbuf_for_iid(&IID_IDispatch
, &factory_buffer
);
1957 hres
= IPSFactoryBuffer_CreateStub(factory_buffer
, &IID_IDispatch
,
1958 This
->pUnk
, &This
->dispatch_stub
);
1959 IPSFactoryBuffer_Release(factory_buffer
);
1963 return IRpcStubBuffer_Invoke(This
->dispatch_stub
, xmsg
, rpcchanbuf
);
1966 memset(&buf
,0,sizeof(buf
));
1967 buf
.size
= xmsg
->cbBuffer
;
1968 buf
.base
= HeapAlloc(GetProcessHeap(), 0, xmsg
->cbBuffer
);
1969 memcpy(buf
.base
, xmsg
->Buffer
, xmsg
->cbBuffer
);
1972 hres
= get_funcdesc(This
->tinfo
,xmsg
->iMethod
,&tinfo
,&fdesc
,&iname
,NULL
,NULL
);
1974 ERR("GetFuncDesc on method %d failed with %x\n",xmsg
->iMethod
,hres
);
1978 if (iname
&& !lstrcmpW(iname
, IDispatchW
))
1980 ERR("IDispatch cannot be marshaled by the typelib marshaler\n");
1981 hres
= E_UNEXPECTED
;
1982 SysFreeString (iname
);
1986 SysFreeString (iname
);
1988 /* Need them for hack below */
1989 memset(names
,0,sizeof(names
));
1990 ITypeInfo_GetNames(tinfo
,fdesc
->memid
,names
,sizeof(names
)/sizeof(names
[0]),&nrofnames
);
1991 if (nrofnames
> sizeof(names
)/sizeof(names
[0])) {
1992 ERR("Need more names!\n");
1995 /*dump_FUNCDESC(fdesc);*/
1997 for (i
=0;i
<fdesc
->cParams
;i
++)
1998 nrofargs
+= _argsize(&fdesc
->lprgelemdescParam
[i
].tdesc
, tinfo
);
1999 args
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,(nrofargs
+1)*sizeof(DWORD
));
2002 hres
= E_OUTOFMEMORY
;
2006 /* Allocate all stuff used by call. */
2008 for (i
=0;i
<fdesc
->cParams
;i
++) {
2009 ELEMDESC
*elem
= fdesc
->lprgelemdescParam
+i
;
2011 hres
= deserialize_param(
2020 xargs
+= _argsize(&elem
->tdesc
, tinfo
);
2022 ERR("Failed to deserialize param %s, hres %x\n",relaystr(names
[i
+1]),hres
);
2027 args
[0] = (DWORD
)This
->pUnk
;
2032 (*((FARPROC
**)args
[0]))[fdesc
->oVft
/4],
2040 DWORD dwExceptionCode
= GetExceptionCode();
2041 ERR("invoke call failed with exception 0x%08x (%d)\n", dwExceptionCode
, dwExceptionCode
);
2042 if (FAILED(dwExceptionCode
))
2043 hres
= dwExceptionCode
;
2045 hres
= HRESULT_FROM_WIN32(dwExceptionCode
);
2055 for (i
=0;i
<fdesc
->cParams
;i
++) {
2056 ELEMDESC
*elem
= fdesc
->lprgelemdescParam
+i
;
2057 hres
= serialize_param(
2066 xargs
+= _argsize(&elem
->tdesc
, tinfo
);
2068 ERR("Failed to stuballoc param, hres %x\n",hres
);
2073 hres
= xbuf_add (&buf
, (LPBYTE
)&res
, sizeof(DWORD
));
2078 xmsg
->cbBuffer
= buf
.curoff
;
2079 hres
= IRpcChannelBuffer_GetBuffer(rpcchanbuf
, xmsg
, &This
->iid
);
2081 ERR("IRpcChannelBuffer_GetBuffer failed with error 0x%08x\n", hres
);
2084 memcpy(xmsg
->Buffer
, buf
.base
, buf
.curoff
);
2087 for (i
= 0; i
< nrofnames
; i
++)
2088 SysFreeString(names
[i
]);
2090 ITypeInfo_Release(tinfo
);
2091 HeapFree(GetProcessHeap(), 0, args
);
2093 HeapFree(GetProcessHeap(), 0, buf
.base
);
2095 TRACE("returning\n");
2098 FIXME( "not implemented on non-i386\n" );
2103 static LPRPCSTUBBUFFER WINAPI
2104 TMStubImpl_IsIIDSupported(LPRPCSTUBBUFFER iface
, REFIID riid
) {
2105 FIXME("Huh (%s)?\n",debugstr_guid(riid
));
2110 TMStubImpl_CountRefs(LPRPCSTUBBUFFER iface
) {
2111 TMStubImpl
*This
= (TMStubImpl
*)iface
;
2114 return This
->ref
; /*FIXME? */
2117 static HRESULT WINAPI
2118 TMStubImpl_DebugServerQueryInterface(LPRPCSTUBBUFFER iface
, LPVOID
*ppv
) {
2123 TMStubImpl_DebugServerRelease(LPRPCSTUBBUFFER iface
, LPVOID ppv
) {
2127 static const IRpcStubBufferVtbl tmstubvtbl
= {
2128 TMStubImpl_QueryInterface
,
2132 TMStubImpl_Disconnect
,
2134 TMStubImpl_IsIIDSupported
,
2135 TMStubImpl_CountRefs
,
2136 TMStubImpl_DebugServerQueryInterface
,
2137 TMStubImpl_DebugServerRelease
2140 static HRESULT WINAPI
2141 PSFacBuf_CreateStub(
2142 LPPSFACTORYBUFFER iface
, REFIID riid
,IUnknown
*pUnkServer
,
2143 IRpcStubBuffer
** ppStub
2150 TRACE("(%s,%p,%p)\n",debugstr_guid(riid
),pUnkServer
,ppStub
);
2152 hres
= _get_typeinfo_for_iid(riid
,&tinfo
);
2154 ERR("No typeinfo for %s?\n",debugstr_guid(riid
));
2158 stub
= CoTaskMemAlloc(sizeof(TMStubImpl
));
2160 return E_OUTOFMEMORY
;
2161 stub
->lpvtbl
= &tmstubvtbl
;
2163 stub
->tinfo
= tinfo
;
2164 stub
->dispatch_stub
= NULL
;
2165 stub
->dispatch_derivative
= FALSE
;
2167 hres
= IRpcStubBuffer_Connect((LPRPCSTUBBUFFER
)stub
,pUnkServer
);
2168 *ppStub
= (LPRPCSTUBBUFFER
)stub
;
2169 TRACE("IRpcStubBuffer: %p\n", stub
);
2171 ERR("Connect to pUnkServer failed?\n");
2173 /* if we derive from IDispatch then defer to its stub for some of its methods */
2174 hres
= ITypeInfo_GetTypeAttr(tinfo
, &typeattr
);
2177 if (typeattr
->wTypeFlags
& TYPEFLAG_FDISPATCHABLE
)
2178 stub
->dispatch_derivative
= TRUE
;
2179 ITypeInfo_ReleaseTypeAttr(tinfo
, typeattr
);
2185 static const IPSFactoryBufferVtbl psfacbufvtbl
= {
2186 PSFacBuf_QueryInterface
,
2189 PSFacBuf_CreateProxy
,
2193 /* This is the whole PSFactoryBuffer object, just the vtableptr */
2194 static const IPSFactoryBufferVtbl
*lppsfac
= &psfacbufvtbl
;
2196 /***********************************************************************
2197 * TMARSHAL_DllGetClassObject
2199 HRESULT
TMARSHAL_DllGetClassObject(REFCLSID rclsid
, REFIID iid
,LPVOID
*ppv
)
2201 if (IsEqualIID(iid
,&IID_IPSFactoryBuffer
)) {
2205 return E_NOINTERFACE
;