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
);
290 tlguidlen
= sizeof(tlguid
);
291 if (RegQueryValueExA(ikey
,NULL
,NULL
,&type
,(LPBYTE
)tlguid
,&tlguidlen
)) {
292 ERR("Getting typelib guid failed.\n");
297 verlen
= sizeof(ver
);
298 if (RegQueryValueExA(ikey
,"Version",NULL
,&type
,(LPBYTE
)ver
,&verlen
)) {
299 ERR("Could not get version value?\n");
304 sprintf(typelibkey
,"Typelib\\%s\\%s\\0\\win32",tlguid
,ver
);
305 tlfnlen
= sizeof(tlfn
);
306 if (RegQueryValueA(HKEY_CLASSES_ROOT
,typelibkey
,tlfn
,&tlfnlen
)) {
307 ERR("Could not get typelib fn?\n");
310 MultiByteToWideChar(CP_ACP
, 0, tlfn
, -1, tlfnW
, sizeof(tlfnW
) / sizeof(tlfnW
[0]));
311 hres
= LoadTypeLib(tlfnW
,&tl
);
313 ERR("Failed to load typelib for %s, but it should be there.\n",debugstr_guid(riid
));
316 hres
= ITypeLib_GetTypeInfoOfGuid(tl
,riid
,ti
);
318 ERR("typelib does not contain info for %s?\n",debugstr_guid(riid
));
319 ITypeLib_Release(tl
);
322 ITypeLib_Release(tl
);
326 /* Determine nr of functions. Since we use the toplevel interface and all
327 * inherited ones have lower numbers, we are ok to not to descent into
328 * the inheritance tree I think.
330 static int _nroffuncs(ITypeInfo
*tinfo
) {
332 const FUNCDESC
*fdesc
;
338 hres
= ITypeInfo_GetTypeAttr(tinfo
, &attr
);
340 ERR("GetTypeAttr failed with %x\n",hres
);
343 /* look in inherited ifaces. */
344 for (j
=0;j
<attr
->cImplTypes
;j
++) {
346 hres
= ITypeInfo_GetRefTypeOfImplType(tinfo
, j
, &href
);
348 ERR("Did not find a reftype for interface offset %d?\n",j
);
351 hres
= ITypeInfo_GetRefTypeInfo(tinfo
, href
, &tinfo2
);
353 ERR("Did not find a typeinfo for reftype %d?\n",href
);
356 n
+= _nroffuncs(tinfo2
);
357 ITypeInfo_Release(tinfo2
);
359 ITypeInfo_ReleaseTypeAttr(tinfo
, attr
);
362 hres
= ITypeInfoImpl_GetInternalFuncDesc(tinfo
,i
,&fdesc
);
373 #include "pshpack1.h"
375 typedef struct _TMAsmProxy
{
389 # warning You need to implement stubless proxies for your architecture
390 typedef struct _TMAsmProxy
{
394 typedef struct _TMProxyImpl
{
396 const IRpcProxyBufferVtbl
*lpvtbl2
;
399 TMAsmProxy
*asmstubs
;
401 IRpcChannelBuffer
* chanbuf
;
403 CRITICAL_SECTION crit
;
404 IUnknown
*outerunknown
;
406 IRpcProxyBuffer
*dispatch_proxy
;
409 static HRESULT WINAPI
410 TMProxyImpl_QueryInterface(LPRPCPROXYBUFFER iface
, REFIID riid
, LPVOID
*ppv
)
413 if (IsEqualIID(riid
,&IID_IUnknown
)||IsEqualIID(riid
,&IID_IRpcProxyBuffer
)) {
414 *ppv
= (LPVOID
)iface
;
415 IRpcProxyBuffer_AddRef(iface
);
418 FIXME("no interface for %s\n",debugstr_guid(riid
));
419 return E_NOINTERFACE
;
423 TMProxyImpl_AddRef(LPRPCPROXYBUFFER iface
)
425 ICOM_THIS_MULTI(TMProxyImpl
,lpvtbl2
,iface
);
426 ULONG refCount
= InterlockedIncrement(&This
->ref
);
428 TRACE("(%p)->(ref before=%u)\n",This
, refCount
- 1);
434 TMProxyImpl_Release(LPRPCPROXYBUFFER iface
)
436 ICOM_THIS_MULTI(TMProxyImpl
,lpvtbl2
,iface
);
437 ULONG refCount
= InterlockedDecrement(&This
->ref
);
439 TRACE("(%p)->(ref before=%u)\n",This
, refCount
+ 1);
443 if (This
->dispatch_proxy
) IRpcProxyBuffer_Release(This
->dispatch_proxy
);
444 This
->crit
.DebugInfo
->Spare
[0] = 0;
445 DeleteCriticalSection(&This
->crit
);
446 if (This
->chanbuf
) IRpcChannelBuffer_Release(This
->chanbuf
);
447 VirtualFree(This
->asmstubs
, 0, MEM_RELEASE
);
448 HeapFree(GetProcessHeap(), 0, This
->lpvtbl
);
449 ITypeInfo_Release(This
->tinfo
);
455 static HRESULT WINAPI
457 LPRPCPROXYBUFFER iface
,IRpcChannelBuffer
* pRpcChannelBuffer
)
459 ICOM_THIS_MULTI(TMProxyImpl
, lpvtbl2
, iface
);
461 TRACE("(%p)\n", pRpcChannelBuffer
);
463 EnterCriticalSection(&This
->crit
);
465 IRpcChannelBuffer_AddRef(pRpcChannelBuffer
);
466 This
->chanbuf
= pRpcChannelBuffer
;
468 LeaveCriticalSection(&This
->crit
);
470 if (This
->dispatch_proxy
)
472 IRpcChannelBuffer
*pDelegateChannel
;
473 HRESULT hr
= TMarshalDispatchChannel_Create(pRpcChannelBuffer
, &This
->iid
, &pDelegateChannel
);
476 return IRpcProxyBuffer_Connect(This
->dispatch_proxy
, pDelegateChannel
);
483 TMProxyImpl_Disconnect(LPRPCPROXYBUFFER iface
)
485 ICOM_THIS_MULTI(TMProxyImpl
, lpvtbl2
, iface
);
489 EnterCriticalSection(&This
->crit
);
491 IRpcChannelBuffer_Release(This
->chanbuf
);
492 This
->chanbuf
= NULL
;
494 LeaveCriticalSection(&This
->crit
);
496 if (This
->dispatch_proxy
)
497 IRpcProxyBuffer_Disconnect(This
->dispatch_proxy
);
501 static const IRpcProxyBufferVtbl tmproxyvtable
= {
502 TMProxyImpl_QueryInterface
,
506 TMProxyImpl_Disconnect
509 /* how much space do we use on stack in DWORD steps. */
514 return 8/sizeof(DWORD
);
516 return sizeof(double)/sizeof(DWORD
);
518 return sizeof(CY
)/sizeof(DWORD
);
520 return sizeof(DATE
)/sizeof(DWORD
);
522 return (sizeof(VARIANT
)+3)/sizeof(DWORD
);
529 _xsize(TYPEDESC
*td
) {
534 return sizeof(VARIANT
)+3;
537 ARRAYDESC
*adesc
= td
->u
.lpadesc
;
539 for (i
=0;i
<adesc
->cDims
;i
++)
540 arrsize
*= adesc
->rgbounds
[i
].cElements
;
541 return arrsize
*_xsize(&adesc
->tdescElem
);
569 TRACE("(tdesc.vt %s)\n",debugstr_vt(tdesc
->vt
));
572 case VT_EMPTY
: /* nothing. empty variant for instance */
578 if (debugout
) TRACE_(olerelay
)("%x%x\n",arg
[0],arg
[1]);
580 hres
= xbuf_add(buf
,(LPBYTE
)arg
,8);
590 if (debugout
) TRACE_(olerelay
)("%x\n",*arg
);
592 hres
= xbuf_add(buf
,(LPBYTE
)arg
,sizeof(DWORD
));
597 if (debugout
) TRACE_(olerelay
)("%04x\n",*arg
& 0xffff);
599 hres
= xbuf_add(buf
,(LPBYTE
)arg
,sizeof(DWORD
));
604 if (debugout
) TRACE_(olerelay
)("%02x\n",*arg
& 0xff);
606 hres
= xbuf_add(buf
,(LPBYTE
)arg
,sizeof(DWORD
));
610 if (debugout
) TRACE_(olerelay
)("&0x%x\n",*arg
);
612 hres
= xbuf_add(buf
,(LPBYTE
)(DWORD
*)*arg
,sizeof(DWORD
));
613 /* do not dealloc at this time */
617 VARIANT
*vt
= (VARIANT
*)arg
;
618 DWORD vttype
= V_VT(vt
);
620 if (debugout
) TRACE_(olerelay
)("Vt(%s%s)(",debugstr_vt(vttype
),debugstr_vf(vttype
));
623 hres
= xbuf_add(buf
,(LPBYTE
)&vttype
,sizeof(vttype
));
624 if (hres
) return hres
;
626 /* need to recurse since we need to free the stuff */
627 hres
= serialize_param(tinfo
,writeit
,debugout
,dealloc
,&tdesc2
,(DWORD
*)&(V_I4(vt
)),buf
);
628 if (debugout
) TRACE_(olerelay
)(")");
631 case VT_BSTR
|VT_BYREF
: {
632 if (debugout
) TRACE_(olerelay
)("[byref]'%s'", *(BSTR
*)*arg
? relaystr(*((BSTR
*)*arg
)) : "<bstr NULL>");
634 /* ptr to ptr to magic widestring, basically */
635 BSTR
*bstr
= (BSTR
*) *arg
;
638 /* -1 means "null string" which is equivalent to empty string */
640 hres
= xbuf_add(buf
, (LPBYTE
)&len
,sizeof(DWORD
));
641 if (hres
) return hres
;
643 len
= *((DWORD
*)*bstr
-1)/sizeof(WCHAR
);
644 hres
= xbuf_add(buf
,(LPBYTE
)&len
,sizeof(DWORD
));
645 if (hres
) return hres
;
646 hres
= xbuf_add(buf
,(LPBYTE
)*bstr
,len
* sizeof(WCHAR
));
647 if (hres
) return hres
;
651 if (dealloc
&& arg
) {
652 BSTR
*str
= *((BSTR
**)arg
);
661 TRACE_(olerelay
)("%s",relaystr((WCHAR
*)*arg
));
663 TRACE_(olerelay
)("<bstr NULL>");
666 BSTR bstr
= (BSTR
)*arg
;
670 hres
= xbuf_add(buf
,(LPBYTE
)&len
,sizeof(DWORD
));
671 if (hres
) return hres
;
673 len
= *((DWORD
*)bstr
-1)/sizeof(WCHAR
);
674 hres
= xbuf_add(buf
,(LPBYTE
)&len
,sizeof(DWORD
));
675 if (hres
) return hres
;
676 hres
= xbuf_add(buf
,(LPBYTE
)bstr
,len
* sizeof(WCHAR
));
677 if (hres
) return hres
;
682 SysFreeString((BSTR
)*arg
);
687 BOOL derefhere
= TRUE
;
689 if (tdesc
->u
.lptdesc
->vt
== VT_USERDEFINED
) {
693 hres
= ITypeInfo_GetRefTypeInfo(tinfo
,tdesc
->u
.lptdesc
->u
.hreftype
,&tinfo2
);
695 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc
->u
.lptdesc
->u
.hreftype
);
698 ITypeInfo_GetTypeAttr(tinfo2
,&tattr
);
699 switch (tattr
->typekind
) {
700 case TKIND_ENUM
: /* confirmed */
701 case TKIND_RECORD
: /* FIXME: mostly untested */
704 case TKIND_ALIAS
: /* FIXME: untested */
705 case TKIND_DISPATCH
: /* will be done in VT_USERDEFINED case */
706 case TKIND_INTERFACE
: /* will be done in VT_USERDEFINED case */
710 FIXME("unhandled switch cases tattr->typekind %d\n", tattr
->typekind
);
714 ITypeInfo_ReleaseTypeAttr(tinfo
, tattr
);
715 ITypeInfo_Release(tinfo2
);
718 if (debugout
) TRACE_(olerelay
)("*");
719 /* Write always, so the other side knows when it gets a NULL pointer.
721 cookie
= *arg
? 0x42424242 : 0;
722 hres
= xbuf_add(buf
,(LPBYTE
)&cookie
,sizeof(cookie
));
726 if (debugout
) TRACE_(olerelay
)("NULL");
729 hres
= serialize_param(tinfo
,writeit
,debugout
,dealloc
,tdesc
->u
.lptdesc
,(DWORD
*)*arg
,buf
);
730 if (derefhere
&& dealloc
) HeapFree(GetProcessHeap(),0,(LPVOID
)*arg
);
734 if (debugout
) TRACE_(olerelay
)("unk(0x%x)",*arg
);
736 hres
= _marshal_interface(buf
,&IID_IUnknown
,(LPUNKNOWN
)*arg
);
737 if (dealloc
&& *(IUnknown
**)arg
)
738 IUnknown_Release((LPUNKNOWN
)*arg
);
741 if (debugout
) TRACE_(olerelay
)("idisp(0x%x)",*arg
);
743 hres
= _marshal_interface(buf
,&IID_IDispatch
,(LPUNKNOWN
)*arg
);
744 if (dealloc
&& *(IUnknown
**)arg
)
745 IUnknown_Release((LPUNKNOWN
)*arg
);
748 if (debugout
) TRACE_(olerelay
)("<void>");
750 case VT_USERDEFINED
: {
754 hres
= ITypeInfo_GetRefTypeInfo(tinfo
,tdesc
->u
.hreftype
,&tinfo2
);
756 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc
->u
.hreftype
);
759 ITypeInfo_GetTypeAttr(tinfo2
,&tattr
);
760 switch (tattr
->typekind
) {
762 case TKIND_INTERFACE
:
764 hres
=_marshal_interface(buf
,&(tattr
->guid
),(LPUNKNOWN
)arg
);
766 IUnknown_Release((LPUNKNOWN
)arg
);
770 if (debugout
) TRACE_(olerelay
)("{");
771 for (i
=0;i
<tattr
->cVars
;i
++) {
776 hres
= ITypeInfo2_GetVarDesc(tinfo2
, i
, &vdesc
);
778 ERR("Could not get vardesc of %d\n",i
);
781 elem2
= &vdesc
->elemdescVar
;
782 tdesc2
= &elem2
->tdesc
;
783 hres
= serialize_param(
789 (DWORD
*)(((LPBYTE
)arg
)+vdesc
->u
.oInst
),
792 ITypeInfo_ReleaseVarDesc(tinfo2
, vdesc
);
795 if (debugout
&& (i
<(tattr
->cVars
-1)))
796 TRACE_(olerelay
)(",");
798 if (debugout
) TRACE_(olerelay
)("}");
802 hres
= serialize_param(tinfo2
,writeit
,debugout
,dealloc
,&tattr
->tdescAlias
,arg
,buf
);
806 if (debugout
) TRACE_(olerelay
)("%x",*arg
);
808 hres
= xbuf_add(buf
,(LPBYTE
)arg
,sizeof(DWORD
));
811 FIXME("Unhandled typekind %d\n",tattr
->typekind
);
815 ITypeInfo_ReleaseTypeAttr(tinfo2
, tattr
);
816 ITypeInfo_Release(tinfo2
);
820 ARRAYDESC
*adesc
= tdesc
->u
.lpadesc
;
823 if (debugout
) TRACE_(olerelay
)("carr");
824 for (i
=0;i
<adesc
->cDims
;i
++) {
825 if (debugout
) TRACE_(olerelay
)("[%d]",adesc
->rgbounds
[i
].cElements
);
826 arrsize
*= adesc
->rgbounds
[i
].cElements
;
828 if (debugout
) TRACE_(olerelay
)("(vt %s)",debugstr_vt(adesc
->tdescElem
.vt
));
829 if (debugout
) TRACE_(olerelay
)("[");
830 for (i
=0;i
<arrsize
;i
++) {
831 hres
= serialize_param(tinfo
, writeit
, debugout
, dealloc
, &adesc
->tdescElem
, (DWORD
*)((LPBYTE
)arg
+i
*_xsize(&adesc
->tdescElem
)), buf
);
834 if (debugout
&& (i
<arrsize
-1)) TRACE_(olerelay
)(",");
836 if (debugout
) TRACE_(olerelay
)("]");
842 ULONG flags
= MAKELONG(MSHCTX_DIFFERENTMACHINE
, NDR_LOCAL_DATA_REPRESENTATION
);
843 ULONG size
= LPSAFEARRAY_UserSize(&flags
, buf
->curoff
, (LPSAFEARRAY
*)arg
);
844 xbuf_resize(buf
, size
);
845 LPSAFEARRAY_UserMarshal(&flags
, buf
->base
+ buf
->curoff
, (LPSAFEARRAY
*)arg
);
851 ERR("Unhandled marshal type %d.\n",tdesc
->vt
);
868 TRACE("vt %s at %p\n",debugstr_vt(tdesc
->vt
),arg
);
873 if (debugout
) TRACE_(olerelay
)("<empty>\n");
876 if (debugout
) TRACE_(olerelay
)("<null>\n");
879 VARIANT
*vt
= (VARIANT
*)arg
;
884 hres
= xbuf_get(buf
,(LPBYTE
)&vttype
,sizeof(vttype
));
886 FIXME("vt type not read?\n");
889 memset(&tdesc2
,0,sizeof(tdesc2
));
892 if (debugout
) TRACE_(olerelay
)("Vt(%s%s)(",debugstr_vt(vttype
),debugstr_vf(vttype
));
893 hres
= deserialize_param(tinfo
, readit
, debugout
, alloc
, &tdesc2
, (DWORD
*)&(V_I4(vt
)), buf
);
894 TRACE_(olerelay
)(")");
905 hres
= xbuf_get(buf
,(LPBYTE
)arg
,8);
906 if (hres
) ERR("Failed to read integer 8 byte\n");
908 if (debugout
) TRACE_(olerelay
)("%x%x",arg
[0],arg
[1]);
918 hres
= xbuf_get(buf
,(LPBYTE
)arg
,sizeof(DWORD
));
919 if (hres
) ERR("Failed to read integer 4 byte\n");
921 if (debugout
) TRACE_(olerelay
)("%x",*arg
);
927 hres
= xbuf_get(buf
,(LPBYTE
)&x
,sizeof(DWORD
));
928 if (hres
) ERR("Failed to read integer 4 byte\n");
931 if (debugout
) TRACE_(olerelay
)("%04x",*arg
& 0xffff);
937 hres
= xbuf_get(buf
,(LPBYTE
)&x
,sizeof(DWORD
));
938 if (hres
) ERR("Failed to read integer 4 byte\n");
941 if (debugout
) TRACE_(olerelay
)("%02x",*arg
& 0xff);
946 *arg
= (DWORD
)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(DWORD
));
948 hres
= xbuf_get(buf
,(LPBYTE
)*arg
,sizeof(DWORD
));
949 if (hres
) ERR("Failed to read integer 4 byte\n");
951 if (debugout
) TRACE_(olerelay
)("&0x%x",*(DWORD
*)*arg
);
953 case VT_BSTR
|VT_BYREF
: {
954 BSTR
**bstr
= (BSTR
**)arg
;
959 hres
= xbuf_get(buf
,(LPBYTE
)&len
,sizeof(DWORD
));
961 ERR("failed to read bstr klen\n");
965 *bstr
= CoTaskMemAlloc(sizeof(BSTR
*));
967 if (debugout
) TRACE_(olerelay
)("<bstr NULL>");
969 str
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,(len
+1)*sizeof(WCHAR
));
970 hres
= xbuf_get(buf
,(LPBYTE
)str
,len
*sizeof(WCHAR
));
972 ERR("Failed to read BSTR.\n");
975 *bstr
= CoTaskMemAlloc(sizeof(BSTR
*));
976 **bstr
= SysAllocStringLen(str
,len
);
977 if (debugout
) TRACE_(olerelay
)("%s",relaystr(str
));
978 HeapFree(GetProcessHeap(),0,str
);
990 hres
= xbuf_get(buf
,(LPBYTE
)&len
,sizeof(DWORD
));
992 ERR("failed to read bstr klen\n");
997 if (debugout
) TRACE_(olerelay
)("<bstr NULL>");
999 str
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,(len
+1)*sizeof(WCHAR
));
1000 hres
= xbuf_get(buf
,(LPBYTE
)str
,len
*sizeof(WCHAR
));
1002 ERR("Failed to read BSTR.\n");
1005 *arg
= (DWORD
)SysAllocStringLen(str
,len
);
1006 if (debugout
) TRACE_(olerelay
)("%s",relaystr(str
));
1007 HeapFree(GetProcessHeap(),0,str
);
1016 BOOL derefhere
= TRUE
;
1018 if (tdesc
->u
.lptdesc
->vt
== VT_USERDEFINED
) {
1022 hres
= ITypeInfo_GetRefTypeInfo(tinfo
,tdesc
->u
.lptdesc
->u
.hreftype
,&tinfo2
);
1024 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc
->u
.lptdesc
->u
.hreftype
);
1027 ITypeInfo_GetTypeAttr(tinfo2
,&tattr
);
1028 switch (tattr
->typekind
) {
1029 case TKIND_ENUM
: /* confirmed */
1030 case TKIND_RECORD
: /* FIXME: mostly untested */
1033 case TKIND_ALIAS
: /* FIXME: untested */
1034 case TKIND_DISPATCH
: /* will be done in VT_USERDEFINED case */
1035 case TKIND_INTERFACE
: /* will be done in VT_USERDEFINED case */
1039 FIXME("unhandled switch cases tattr->typekind %d\n", tattr
->typekind
);
1043 ITypeInfo_ReleaseTypeAttr(tinfo2
, tattr
);
1044 ITypeInfo_Release(tinfo2
);
1046 /* read it in all cases, we need to know if we have
1047 * NULL pointer or not.
1049 hres
= xbuf_get(buf
,(LPBYTE
)&cookie
,sizeof(cookie
));
1051 ERR("Failed to load pointer cookie.\n");
1054 if (cookie
!= 0x42424242) {
1055 /* we read a NULL ptr from the remote side */
1056 if (debugout
) TRACE_(olerelay
)("NULL");
1060 if (debugout
) TRACE_(olerelay
)("*");
1062 /* Allocate space for the referenced struct */
1064 *arg
=(DWORD
)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,_xsize(tdesc
->u
.lptdesc
));
1067 return deserialize_param(tinfo
, readit
, debugout
, alloc
, tdesc
->u
.lptdesc
, (LPDWORD
)*arg
, buf
);
1069 return deserialize_param(tinfo
, readit
, debugout
, alloc
, tdesc
->u
.lptdesc
, arg
, buf
);
1072 /* FIXME: UNKNOWN is unknown ..., but allocate 4 byte for it */
1074 *arg
=(DWORD
)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(DWORD
));
1077 hres
= _unmarshal_interface(buf
,&IID_IUnknown
,(LPUNKNOWN
*)arg
);
1079 TRACE_(olerelay
)("unk(%p)",arg
);
1084 hres
= _unmarshal_interface(buf
,&IID_IDispatch
,(LPUNKNOWN
*)arg
);
1086 TRACE_(olerelay
)("idisp(%p)",arg
);
1089 if (debugout
) TRACE_(olerelay
)("<void>");
1091 case VT_USERDEFINED
: {
1095 hres
= ITypeInfo_GetRefTypeInfo(tinfo
,tdesc
->u
.hreftype
,&tinfo2
);
1097 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc
->u
.hreftype
);
1100 hres
= ITypeInfo_GetTypeAttr(tinfo2
,&tattr
);
1102 ERR("Could not get typeattr in VT_USERDEFINED.\n");
1104 switch (tattr
->typekind
) {
1105 case TKIND_DISPATCH
:
1106 case TKIND_INTERFACE
:
1108 hres
= _unmarshal_interface(buf
,&(tattr
->guid
),(LPUNKNOWN
*)arg
);
1110 case TKIND_RECORD
: {
1114 *arg
= (DWORD
)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,tattr
->cbSizeInstance
);
1116 if (debugout
) TRACE_(olerelay
)("{");
1117 for (i
=0;i
<tattr
->cVars
;i
++) {
1120 hres
= ITypeInfo2_GetVarDesc(tinfo2
, i
, &vdesc
);
1122 ERR("Could not get vardesc of %d\n",i
);
1123 ITypeInfo_ReleaseTypeAttr(tinfo2
, tattr
);
1124 ITypeInfo_Release(tinfo2
);
1127 hres
= deserialize_param(
1132 &vdesc
->elemdescVar
.tdesc
,
1133 (DWORD
*)(((LPBYTE
)*arg
)+vdesc
->u
.oInst
),
1136 ITypeInfo2_ReleaseVarDesc(tinfo2
, vdesc
);
1137 if (debugout
&& (i
<tattr
->cVars
-1)) TRACE_(olerelay
)(",");
1139 if (debugout
) TRACE_(olerelay
)("}");
1143 hres
= deserialize_param(tinfo2
,readit
,debugout
,alloc
,&tattr
->tdescAlias
,arg
,buf
);
1147 hres
= xbuf_get(buf
,(LPBYTE
)arg
,sizeof(DWORD
));
1148 if (hres
) ERR("Failed to read enum (4 byte)\n");
1150 if (debugout
) TRACE_(olerelay
)("%x",*arg
);
1153 ERR("Unhandled typekind %d\n",tattr
->typekind
);
1157 ITypeInfo_ReleaseTypeAttr(tinfo2
, tattr
);
1160 ERR("failed to stuballoc in TKIND_RECORD.\n");
1161 ITypeInfo_Release(tinfo2
);
1165 /* arg is pointing to the start of the array. */
1166 ARRAYDESC
*adesc
= tdesc
->u
.lpadesc
;
1169 if (adesc
->cDims
> 1) FIXME("cDims > 1 in VT_CARRAY. Does it work?\n");
1170 for (i
=0;i
<adesc
->cDims
;i
++)
1171 arrsize
*= adesc
->rgbounds
[i
].cElements
;
1172 for (i
=0;i
<arrsize
;i
++)
1179 (DWORD
*)((LPBYTE
)(arg
)+i
*_xsize(&adesc
->tdescElem
)),
1184 case VT_SAFEARRAY
: {
1187 ULONG flags
= MAKELONG(MSHCTX_DIFFERENTMACHINE
, NDR_LOCAL_DATA_REPRESENTATION
);
1188 unsigned char *buffer
;
1189 buffer
= LPSAFEARRAY_UserUnmarshal(&flags
, buf
->base
+ buf
->curoff
, (LPSAFEARRAY
*)arg
);
1190 buf
->curoff
= buffer
- buf
->base
;
1195 ERR("No handler for VT type %d!\n",tdesc
->vt
);
1201 /* Retrieves a function's funcdesc, searching back into inherited interfaces. */
1202 static HRESULT
get_funcdesc(ITypeInfo
*tinfo
, int iMethod
, ITypeInfo
**tactual
, const FUNCDESC
**fdesc
,
1203 BSTR
*iname
, BSTR
*fname
, UINT
*num
)
1207 UINT inherited_funcs
= 0;
1210 if (fname
) *fname
= NULL
;
1211 if (iname
) *iname
= NULL
;
1215 hr
= ITypeInfo_GetTypeAttr(tinfo
, &attr
);
1218 ERR("GetTypeAttr failed with %x\n",hr
);
1221 impl_types
= attr
->cImplTypes
;
1222 ITypeInfo_ReleaseTypeAttr(tinfo
, attr
);
1224 for (i
= 0; i
< impl_types
; i
++)
1227 ITypeInfo
*pSubTypeInfo
;
1230 hr
= ITypeInfo_GetRefTypeOfImplType(tinfo
, i
, &href
);
1231 if (FAILED(hr
)) return hr
;
1232 hr
= ITypeInfo_GetRefTypeInfo(tinfo
, href
, &pSubTypeInfo
);
1233 if (FAILED(hr
)) return hr
;
1235 hr
= get_funcdesc(pSubTypeInfo
, iMethod
, tactual
, fdesc
, iname
, fname
, &sub_funcs
);
1236 inherited_funcs
+= sub_funcs
;
1237 ITypeInfo_Release(pSubTypeInfo
);
1238 if(SUCCEEDED(hr
)) return hr
;
1240 if(iMethod
< inherited_funcs
)
1242 ERR("shouldn't be here\n");
1243 return E_INVALIDARG
;
1246 for(i
= inherited_funcs
; i
<= iMethod
; i
++)
1248 hr
= ITypeInfoImpl_GetInternalFuncDesc(tinfo
, i
- inherited_funcs
, fdesc
);
1256 /* found it. We don't care about num so zero it */
1259 ITypeInfo_AddRef(*tactual
);
1260 if (fname
) ITypeInfo_GetDocumentation(tinfo
,(*fdesc
)->memid
,fname
,NULL
,NULL
,NULL
);
1261 if (iname
) ITypeInfo_GetDocumentation(tinfo
,-1,iname
,NULL
,NULL
,NULL
);
1265 static inline BOOL
is_in_elem(const ELEMDESC
*elem
)
1267 return (elem
->u
.paramdesc
.wParamFlags
& PARAMFLAG_FIN
|| !elem
->u
.paramdesc
.wParamFlags
);
1270 static inline BOOL
is_out_elem(const ELEMDESC
*elem
)
1272 return (elem
->u
.paramdesc
.wParamFlags
& PARAMFLAG_FOUT
|| !elem
->u
.paramdesc
.wParamFlags
);
1276 xCall(LPVOID retptr
, int method
, TMProxyImpl
*tpinfo
/*, args */)
1278 DWORD
*args
= ((DWORD
*)&tpinfo
)+1, *xargs
;
1279 const FUNCDESC
*fdesc
;
1281 int i
, relaydeb
= TRACE_ON(olerelay
);
1288 DWORD remoteresult
= 0;
1290 IRpcChannelBuffer
*chanbuf
;
1292 EnterCriticalSection(&tpinfo
->crit
);
1294 hres
= get_funcdesc(tpinfo
->tinfo
,method
,&tinfo
,&fdesc
,&iname
,&fname
,NULL
);
1296 ERR("Did not find typeinfo/funcdesc entry for method %d!\n",method
);
1297 LeaveCriticalSection(&tpinfo
->crit
);
1301 if (!tpinfo
->chanbuf
)
1303 WARN("Tried to use disconnected proxy\n");
1304 ITypeInfo_Release(tinfo
);
1305 LeaveCriticalSection(&tpinfo
->crit
);
1306 return RPC_E_DISCONNECTED
;
1308 chanbuf
= tpinfo
->chanbuf
;
1309 IRpcChannelBuffer_AddRef(chanbuf
);
1311 LeaveCriticalSection(&tpinfo
->crit
);
1314 TRACE_(olerelay
)("->");
1316 TRACE_(olerelay
)("%s:",relaystr(iname
));
1318 TRACE_(olerelay
)("%s(%d)",relaystr(fname
),method
);
1320 TRACE_(olerelay
)("%d",method
);
1321 TRACE_(olerelay
)("(");
1324 if (iname
) SysFreeString(iname
);
1325 if (fname
) SysFreeString(fname
);
1327 memset(&buf
,0,sizeof(buf
));
1329 /* normal typelib driven serializing */
1331 /* Need them for hack below */
1332 memset(names
,0,sizeof(names
));
1333 if (ITypeInfo_GetNames(tinfo
,fdesc
->memid
,names
,sizeof(names
)/sizeof(names
[0]),&nrofnames
))
1335 if (nrofnames
> sizeof(names
)/sizeof(names
[0]))
1336 ERR("Need more names!\n");
1339 for (i
=0;i
<fdesc
->cParams
;i
++) {
1340 ELEMDESC
*elem
= fdesc
->lprgelemdescParam
+i
;
1342 if (i
) TRACE_(olerelay
)(",");
1343 if (i
+1<nrofnames
&& names
[i
+1])
1344 TRACE_(olerelay
)("%s=",relaystr(names
[i
+1]));
1346 /* No need to marshal other data than FIN and any VT_PTR. */
1347 if (!is_in_elem(elem
) && (elem
->tdesc
.vt
!= VT_PTR
)) {
1348 xargs
+=_argsize(elem
->tdesc
.vt
);
1349 if (relaydeb
) TRACE_(olerelay
)("[out]");
1352 hres
= serialize_param(
1363 ERR("Failed to serialize param, hres %x\n",hres
);
1366 xargs
+=_argsize(elem
->tdesc
.vt
);
1368 if (relaydeb
) TRACE_(olerelay
)(")");
1370 memset(&msg
,0,sizeof(msg
));
1371 msg
.cbBuffer
= buf
.curoff
;
1372 msg
.iMethod
= method
;
1373 hres
= IRpcChannelBuffer_GetBuffer(chanbuf
,&msg
,&(tpinfo
->iid
));
1375 ERR("RpcChannelBuffer GetBuffer failed, %x\n",hres
);
1378 memcpy(msg
.Buffer
,buf
.base
,buf
.curoff
);
1379 if (relaydeb
) TRACE_(olerelay
)("\n");
1380 hres
= IRpcChannelBuffer_SendReceive(chanbuf
,&msg
,&status
);
1382 ERR("RpcChannelBuffer SendReceive failed, %x\n",hres
);
1386 if (relaydeb
) TRACE_(olerelay
)(" status = %08x (",status
);
1388 buf
.base
= HeapReAlloc(GetProcessHeap(),0,buf
.base
,msg
.cbBuffer
);
1390 buf
.base
= HeapAlloc(GetProcessHeap(),0,msg
.cbBuffer
);
1391 buf
.size
= msg
.cbBuffer
;
1392 memcpy(buf
.base
,msg
.Buffer
,buf
.size
);
1395 /* generic deserializer using typelib description */
1398 for (i
=0;i
<fdesc
->cParams
;i
++) {
1399 ELEMDESC
*elem
= fdesc
->lprgelemdescParam
+i
;
1402 if (i
) TRACE_(olerelay
)(",");
1403 if (i
+1<nrofnames
&& names
[i
+1]) TRACE_(olerelay
)("%s=",relaystr(names
[i
+1]));
1405 /* No need to marshal other data than FOUT and any VT_PTR */
1406 if (!is_out_elem(elem
) && (elem
->tdesc
.vt
!= VT_PTR
)) {
1407 xargs
+= _argsize(elem
->tdesc
.vt
);
1408 if (relaydeb
) TRACE_(olerelay
)("[in]");
1411 hres
= deserialize_param(
1421 ERR("Failed to unmarshall param, hres %x\n",hres
);
1425 xargs
+= _argsize(elem
->tdesc
.vt
);
1428 hres
= xbuf_get(&buf
, (LPBYTE
)&remoteresult
, sizeof(DWORD
));
1431 if (relaydeb
) TRACE_(olerelay
)(") = %08x\n", remoteresult
);
1433 hres
= remoteresult
;
1436 for (i
= 0; i
< nrofnames
; i
++)
1437 SysFreeString(names
[i
]);
1438 HeapFree(GetProcessHeap(),0,buf
.base
);
1439 IRpcChannelBuffer_Release(chanbuf
);
1440 ITypeInfo_Release(tinfo
);
1441 TRACE("-- 0x%08x\n", hres
);
1445 static HRESULT WINAPI
ProxyIUnknown_QueryInterface(IUnknown
*iface
, REFIID riid
, void **ppv
)
1447 TMProxyImpl
*proxy
= (TMProxyImpl
*)iface
;
1449 TRACE("(%s, %p)\n", debugstr_guid(riid
), ppv
);
1451 if (proxy
->outerunknown
)
1452 return IUnknown_QueryInterface(proxy
->outerunknown
, riid
, ppv
);
1454 FIXME("No interface\n");
1455 return E_NOINTERFACE
;
1458 static ULONG WINAPI
ProxyIUnknown_AddRef(IUnknown
*iface
)
1460 TMProxyImpl
*proxy
= (TMProxyImpl
*)iface
;
1464 if (proxy
->outerunknown
)
1465 return IUnknown_AddRef(proxy
->outerunknown
);
1467 return 2; /* FIXME */
1470 static ULONG WINAPI
ProxyIUnknown_Release(IUnknown
*iface
)
1472 TMProxyImpl
*proxy
= (TMProxyImpl
*)iface
;
1476 if (proxy
->outerunknown
)
1477 return IUnknown_Release(proxy
->outerunknown
);
1479 return 1; /* FIXME */
1482 static HRESULT WINAPI
ProxyIDispatch_GetTypeInfoCount(LPDISPATCH iface
, UINT
* pctinfo
)
1484 TMProxyImpl
*This
= (TMProxyImpl
*)iface
;
1486 TRACE("(%p)\n", pctinfo
);
1488 return IDispatch_GetTypeInfoCount(This
->dispatch
, pctinfo
);
1491 static HRESULT WINAPI
ProxyIDispatch_GetTypeInfo(LPDISPATCH iface
, UINT iTInfo
, LCID lcid
, ITypeInfo
** ppTInfo
)
1493 TMProxyImpl
*This
= (TMProxyImpl
*)iface
;
1495 TRACE("(%d, %x, %p)\n", iTInfo
, lcid
, ppTInfo
);
1497 return IDispatch_GetTypeInfo(This
->dispatch
, iTInfo
, lcid
, ppTInfo
);
1500 static HRESULT WINAPI
ProxyIDispatch_GetIDsOfNames(LPDISPATCH iface
, REFIID riid
, LPOLESTR
* rgszNames
, UINT cNames
, LCID lcid
, DISPID
* rgDispId
)
1502 TMProxyImpl
*This
= (TMProxyImpl
*)iface
;
1504 TRACE("(%s, %p, %d, 0x%x, %p)\n", debugstr_guid(riid
), rgszNames
, cNames
, lcid
, rgDispId
);
1506 return IDispatch_GetIDsOfNames(This
->dispatch
, riid
, rgszNames
,
1507 cNames
, lcid
, rgDispId
);
1510 static HRESULT WINAPI
ProxyIDispatch_Invoke(LPDISPATCH iface
, DISPID dispIdMember
, REFIID riid
, LCID lcid
,
1511 WORD wFlags
, DISPPARAMS
* pDispParams
, VARIANT
* pVarResult
,
1512 EXCEPINFO
* pExcepInfo
, UINT
* puArgErr
)
1514 TMProxyImpl
*This
= (TMProxyImpl
*)iface
;
1516 TRACE("(%d, %s, 0x%x, 0x%x, %p, %p, %p, %p)\n", dispIdMember
,
1517 debugstr_guid(riid
), lcid
, wFlags
, pDispParams
, pVarResult
,
1518 pExcepInfo
, puArgErr
);
1520 return IDispatch_Invoke(This
->dispatch
, dispIdMember
, riid
, lcid
,
1521 wFlags
, pDispParams
, pVarResult
, pExcepInfo
,
1527 const IRpcChannelBufferVtbl
*lpVtbl
;
1529 /* the IDispatch-derived interface we are handling */
1531 IRpcChannelBuffer
*pDelegateChannel
;
1532 } TMarshalDispatchChannel
;
1534 static HRESULT WINAPI
TMarshalDispatchChannel_QueryInterface(LPRPCCHANNELBUFFER iface
, REFIID riid
, LPVOID
*ppv
)
1537 if (IsEqualIID(riid
,&IID_IRpcChannelBuffer
) || IsEqualIID(riid
,&IID_IUnknown
))
1539 *ppv
= (LPVOID
)iface
;
1540 IUnknown_AddRef(iface
);
1543 return E_NOINTERFACE
;
1546 static ULONG WINAPI
TMarshalDispatchChannel_AddRef(LPRPCCHANNELBUFFER iface
)
1548 TMarshalDispatchChannel
*This
= (TMarshalDispatchChannel
*)iface
;
1549 return InterlockedIncrement(&This
->refs
);
1552 static ULONG WINAPI
TMarshalDispatchChannel_Release(LPRPCCHANNELBUFFER iface
)
1554 TMarshalDispatchChannel
*This
= (TMarshalDispatchChannel
*)iface
;
1557 ref
= InterlockedDecrement(&This
->refs
);
1561 IRpcChannelBuffer_Release(This
->pDelegateChannel
);
1562 HeapFree(GetProcessHeap(), 0, This
);
1566 static HRESULT WINAPI
TMarshalDispatchChannel_GetBuffer(LPRPCCHANNELBUFFER iface
, RPCOLEMESSAGE
* olemsg
, REFIID riid
)
1568 TMarshalDispatchChannel
*This
= (TMarshalDispatchChannel
*)iface
;
1569 TRACE("(%p, %s)\n", olemsg
, debugstr_guid(riid
));
1570 /* Note: we are pretending to invoke a method on the interface identified
1571 * by tmarshal_iid so that we can re-use the IDispatch proxy/stub code
1572 * without the RPC runtime getting confused by not exporting an IDispatch interface */
1573 return IRpcChannelBuffer_GetBuffer(This
->pDelegateChannel
, olemsg
, &This
->tmarshal_iid
);
1576 static HRESULT WINAPI
TMarshalDispatchChannel_SendReceive(LPRPCCHANNELBUFFER iface
, RPCOLEMESSAGE
*olemsg
, ULONG
*pstatus
)
1578 TMarshalDispatchChannel
*This
= (TMarshalDispatchChannel
*)iface
;
1579 TRACE("(%p, %p)\n", olemsg
, pstatus
);
1580 return IRpcChannelBuffer_SendReceive(This
->pDelegateChannel
, olemsg
, pstatus
);
1583 static HRESULT WINAPI
TMarshalDispatchChannel_FreeBuffer(LPRPCCHANNELBUFFER iface
, RPCOLEMESSAGE
* olemsg
)
1585 TMarshalDispatchChannel
*This
= (TMarshalDispatchChannel
*)iface
;
1586 TRACE("(%p)\n", olemsg
);
1587 return IRpcChannelBuffer_FreeBuffer(This
->pDelegateChannel
, olemsg
);
1590 static HRESULT WINAPI
TMarshalDispatchChannel_GetDestCtx(LPRPCCHANNELBUFFER iface
, DWORD
* pdwDestContext
, void** ppvDestContext
)
1592 TMarshalDispatchChannel
*This
= (TMarshalDispatchChannel
*)iface
;
1593 TRACE("(%p,%p)\n", pdwDestContext
, ppvDestContext
);
1594 return IRpcChannelBuffer_GetDestCtx(This
->pDelegateChannel
, pdwDestContext
, ppvDestContext
);
1597 static HRESULT WINAPI
TMarshalDispatchChannel_IsConnected(LPRPCCHANNELBUFFER iface
)
1599 TMarshalDispatchChannel
*This
= (TMarshalDispatchChannel
*)iface
;
1601 return IRpcChannelBuffer_IsConnected(This
->pDelegateChannel
);
1604 static const IRpcChannelBufferVtbl TMarshalDispatchChannelVtbl
=
1606 TMarshalDispatchChannel_QueryInterface
,
1607 TMarshalDispatchChannel_AddRef
,
1608 TMarshalDispatchChannel_Release
,
1609 TMarshalDispatchChannel_GetBuffer
,
1610 TMarshalDispatchChannel_SendReceive
,
1611 TMarshalDispatchChannel_FreeBuffer
,
1612 TMarshalDispatchChannel_GetDestCtx
,
1613 TMarshalDispatchChannel_IsConnected
1616 static HRESULT
TMarshalDispatchChannel_Create(
1617 IRpcChannelBuffer
*pDelegateChannel
, REFIID tmarshal_riid
,
1618 IRpcChannelBuffer
**ppChannel
)
1620 TMarshalDispatchChannel
*This
= HeapAlloc(GetProcessHeap(), 0, sizeof(*This
));
1622 return E_OUTOFMEMORY
;
1624 This
->lpVtbl
= &TMarshalDispatchChannelVtbl
;
1626 IRpcChannelBuffer_AddRef(pDelegateChannel
);
1627 This
->pDelegateChannel
= pDelegateChannel
;
1628 This
->tmarshal_iid
= *tmarshal_riid
;
1630 *ppChannel
= (IRpcChannelBuffer
*)&This
->lpVtbl
;
1635 static inline HRESULT
get_facbuf_for_iid(REFIID riid
, IPSFactoryBuffer
**facbuf
)
1640 if ((hr
= CoGetPSClsid(riid
, &clsid
)))
1642 return CoGetClassObject(&clsid
, CLSCTX_INPROC_SERVER
, NULL
,
1643 &IID_IPSFactoryBuffer
, (LPVOID
*)facbuf
);
1646 static HRESULT WINAPI
1647 PSFacBuf_CreateProxy(
1648 LPPSFACTORYBUFFER iface
, IUnknown
* pUnkOuter
, REFIID riid
,
1649 IRpcProxyBuffer
**ppProxy
, LPVOID
*ppv
)
1654 const FUNCDESC
*fdesc
;
1658 TRACE("(...%s...)\n",debugstr_guid(riid
));
1659 hres
= _get_typeinfo_for_iid(riid
,&tinfo
);
1661 ERR("No typeinfo for %s?\n",debugstr_guid(riid
));
1664 nroffuncs
= _nroffuncs(tinfo
);
1665 proxy
= CoTaskMemAlloc(sizeof(TMProxyImpl
));
1666 if (!proxy
) return E_OUTOFMEMORY
;
1668 assert(sizeof(TMAsmProxy
) == 12);
1670 proxy
->dispatch
= NULL
;
1671 proxy
->dispatch_proxy
= NULL
;
1672 proxy
->outerunknown
= pUnkOuter
;
1673 proxy
->asmstubs
= VirtualAlloc(NULL
, sizeof(TMAsmProxy
) * nroffuncs
, MEM_COMMIT
, PAGE_EXECUTE_READWRITE
);
1674 if (!proxy
->asmstubs
) {
1675 ERR("Could not commit pages for proxy thunks\n");
1676 CoTaskMemFree(proxy
);
1677 return E_OUTOFMEMORY
;
1679 proxy
->lpvtbl2
= &tmproxyvtable
;
1680 /* one reference for the proxy */
1682 proxy
->tinfo
= tinfo
;
1683 memcpy(&proxy
->iid
,riid
,sizeof(*riid
));
1686 InitializeCriticalSection(&proxy
->crit
);
1687 proxy
->crit
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": TMProxyImpl.crit");
1689 proxy
->lpvtbl
= HeapAlloc(GetProcessHeap(),0,sizeof(LPBYTE
)*nroffuncs
);
1690 for (i
=0;i
<nroffuncs
;i
++) {
1691 TMAsmProxy
*xasm
= proxy
->asmstubs
+i
;
1695 proxy
->lpvtbl
[i
] = ProxyIUnknown_QueryInterface
;
1698 proxy
->lpvtbl
[i
] = ProxyIUnknown_AddRef
;
1701 proxy
->lpvtbl
[i
] = ProxyIUnknown_Release
;
1705 /* nrofargs without This */
1708 hres
= get_funcdesc(tinfo
,i
,&tinfo2
,&fdesc
,NULL
,NULL
,NULL
);
1710 ERR("GetFuncDesc %x should not fail here.\n",hres
);
1713 ITypeInfo_Release(tinfo2
);
1714 /* some args take more than 4 byte on the stack */
1716 for (j
=0;j
<fdesc
->cParams
;j
++)
1717 nrofargs
+= _argsize(fdesc
->lprgelemdescParam
[j
].tdesc
.vt
);
1720 if (fdesc
->callconv
!= CC_STDCALL
) {
1721 ERR("calling convention is not stdcall????\n");
1724 /* popl %eax - return ptr
1731 * arg3 arg2 arg1 <method> <returnptr>
1733 xasm
->popleax
= 0x58;
1734 xasm
->pushlval
= 0x6a;
1736 xasm
->pushleax
= 0x50;
1737 xasm
->lcall
= 0xe8; /* relative jump */
1738 xasm
->xcall
= (DWORD
)xCall
;
1739 xasm
->xcall
-= (DWORD
)&(xasm
->lret
);
1741 xasm
->bytestopop
= (nrofargs
+2)*4; /* pop args, This, iMethod */
1742 proxy
->lpvtbl
[i
] = xasm
;
1745 FIXME("not implemented on non i386\n");
1752 /* if we derive from IDispatch then defer to its proxy for its methods */
1753 hres
= ITypeInfo_GetTypeAttr(tinfo
, &typeattr
);
1756 if (typeattr
->wTypeFlags
& TYPEFLAG_FDISPATCHABLE
)
1758 IPSFactoryBuffer
*factory_buffer
;
1759 hres
= get_facbuf_for_iid(&IID_IDispatch
, &factory_buffer
);
1762 hres
= IPSFactoryBuffer_CreateProxy(factory_buffer
, NULL
,
1763 &IID_IDispatch
, &proxy
->dispatch_proxy
,
1764 (void **)&proxy
->dispatch
);
1765 IPSFactoryBuffer_Release(factory_buffer
);
1767 if ((hres
== S_OK
) && (nroffuncs
< 7))
1769 ERR("nroffuncs calculated incorrectly (%d)\n", nroffuncs
);
1770 hres
= E_UNEXPECTED
;
1774 proxy
->lpvtbl
[3] = ProxyIDispatch_GetTypeInfoCount
;
1775 proxy
->lpvtbl
[4] = ProxyIDispatch_GetTypeInfo
;
1776 proxy
->lpvtbl
[5] = ProxyIDispatch_GetIDsOfNames
;
1777 proxy
->lpvtbl
[6] = ProxyIDispatch_Invoke
;
1780 ITypeInfo_ReleaseTypeAttr(tinfo
, typeattr
);
1785 *ppv
= (LPVOID
)proxy
;
1786 *ppProxy
= (IRpcProxyBuffer
*)&(proxy
->lpvtbl2
);
1787 IUnknown_AddRef((IUnknown
*)*ppv
);
1791 TMProxyImpl_Release((IRpcProxyBuffer
*)&proxy
->lpvtbl2
);
1795 typedef struct _TMStubImpl
{
1796 const IRpcStubBufferVtbl
*lpvtbl
;
1802 IRpcStubBuffer
*dispatch_stub
;
1803 BOOL dispatch_derivative
;
1806 static HRESULT WINAPI
1807 TMStubImpl_QueryInterface(LPRPCSTUBBUFFER iface
, REFIID riid
, LPVOID
*ppv
)
1809 if (IsEqualIID(riid
,&IID_IRpcStubBuffer
)||IsEqualIID(riid
,&IID_IUnknown
)){
1810 *ppv
= (LPVOID
)iface
;
1811 IRpcStubBuffer_AddRef(iface
);
1814 FIXME("%s, not supported IID.\n",debugstr_guid(riid
));
1815 return E_NOINTERFACE
;
1819 TMStubImpl_AddRef(LPRPCSTUBBUFFER iface
)
1821 TMStubImpl
*This
= (TMStubImpl
*)iface
;
1822 ULONG refCount
= InterlockedIncrement(&This
->ref
);
1824 TRACE("(%p)->(ref before=%u)\n", This
, refCount
- 1);
1830 TMStubImpl_Release(LPRPCSTUBBUFFER iface
)
1832 TMStubImpl
*This
= (TMStubImpl
*)iface
;
1833 ULONG refCount
= InterlockedDecrement(&This
->ref
);
1835 TRACE("(%p)->(ref before=%u)\n", This
, refCount
+ 1);
1839 IRpcStubBuffer_Disconnect(iface
);
1840 ITypeInfo_Release(This
->tinfo
);
1841 if (This
->dispatch_stub
)
1842 IRpcStubBuffer_Release(This
->dispatch_stub
);
1843 CoTaskMemFree(This
);
1848 static HRESULT WINAPI
1849 TMStubImpl_Connect(LPRPCSTUBBUFFER iface
, LPUNKNOWN pUnkServer
)
1851 TMStubImpl
*This
= (TMStubImpl
*)iface
;
1853 TRACE("(%p)->(%p)\n", This
, pUnkServer
);
1855 IUnknown_AddRef(pUnkServer
);
1856 This
->pUnk
= pUnkServer
;
1858 if (This
->dispatch_stub
)
1859 IRpcStubBuffer_Connect(This
->dispatch_stub
, pUnkServer
);
1865 TMStubImpl_Disconnect(LPRPCSTUBBUFFER iface
)
1867 TMStubImpl
*This
= (TMStubImpl
*)iface
;
1869 TRACE("(%p)->()\n", This
);
1873 IUnknown_Release(This
->pUnk
);
1877 if (This
->dispatch_stub
)
1878 IRpcStubBuffer_Disconnect(This
->dispatch_stub
);
1881 static HRESULT WINAPI
1883 LPRPCSTUBBUFFER iface
, RPCOLEMESSAGE
* xmsg
,IRpcChannelBuffer
*rpcchanbuf
)
1886 const FUNCDESC
*fdesc
;
1887 TMStubImpl
*This
= (TMStubImpl
*)iface
;
1889 DWORD
*args
= NULL
, res
, *xargs
, nrofargs
;
1894 ITypeInfo
*tinfo
= NULL
;
1898 if (xmsg
->iMethod
< 3) {
1899 ERR("IUnknown methods cannot be marshaled by the typelib marshaler\n");
1900 return E_UNEXPECTED
;
1903 if (This
->dispatch_derivative
&& xmsg
->iMethod
< sizeof(IDispatchVtbl
)/sizeof(void *))
1905 IPSFactoryBuffer
*factory_buffer
;
1906 hres
= get_facbuf_for_iid(&IID_IDispatch
, &factory_buffer
);
1909 hres
= IPSFactoryBuffer_CreateStub(factory_buffer
, &IID_IDispatch
,
1910 This
->pUnk
, &This
->dispatch_stub
);
1911 IPSFactoryBuffer_Release(factory_buffer
);
1915 return IRpcStubBuffer_Invoke(This
->dispatch_stub
, xmsg
, rpcchanbuf
);
1918 memset(&buf
,0,sizeof(buf
));
1919 buf
.size
= xmsg
->cbBuffer
;
1920 buf
.base
= HeapAlloc(GetProcessHeap(), 0, xmsg
->cbBuffer
);
1921 memcpy(buf
.base
, xmsg
->Buffer
, xmsg
->cbBuffer
);
1924 hres
= get_funcdesc(This
->tinfo
,xmsg
->iMethod
,&tinfo
,&fdesc
,&iname
,NULL
,NULL
);
1926 ERR("GetFuncDesc on method %d failed with %x\n",xmsg
->iMethod
,hres
);
1930 if (iname
&& !lstrcmpW(iname
, IDispatchW
))
1932 ERR("IDispatch cannot be marshaled by the typelib marshaler\n");
1933 hres
= E_UNEXPECTED
;
1934 SysFreeString (iname
);
1938 if (iname
) SysFreeString (iname
);
1940 /* Need them for hack below */
1941 memset(names
,0,sizeof(names
));
1942 ITypeInfo_GetNames(tinfo
,fdesc
->memid
,names
,sizeof(names
)/sizeof(names
[0]),&nrofnames
);
1943 if (nrofnames
> sizeof(names
)/sizeof(names
[0])) {
1944 ERR("Need more names!\n");
1947 /*dump_FUNCDESC(fdesc);*/
1949 for (i
=0;i
<fdesc
->cParams
;i
++)
1950 nrofargs
+= _argsize(fdesc
->lprgelemdescParam
[i
].tdesc
.vt
);
1951 args
= HeapAlloc(GetProcessHeap(),0,(nrofargs
+1)*sizeof(DWORD
));
1954 hres
= E_OUTOFMEMORY
;
1958 /* Allocate all stuff used by call. */
1960 for (i
=0;i
<fdesc
->cParams
;i
++) {
1961 ELEMDESC
*elem
= fdesc
->lprgelemdescParam
+i
;
1963 hres
= deserialize_param(
1972 xargs
+= _argsize(elem
->tdesc
.vt
);
1974 ERR("Failed to deserialize param %s, hres %x\n",relaystr(names
[i
+1]),hres
);
1979 args
[0] = (DWORD
)This
->pUnk
;
1984 (*((FARPROC
**)args
[0]))[fdesc
->oVft
/4],
1992 DWORD dwExceptionCode
= GetExceptionCode();
1993 ERR("invoke call failed with exception 0x%08x (%d)\n", dwExceptionCode
, dwExceptionCode
);
1994 if (FAILED(dwExceptionCode
))
1995 hres
= dwExceptionCode
;
1997 hres
= HRESULT_FROM_WIN32(dwExceptionCode
);
2007 for (i
=0;i
<fdesc
->cParams
;i
++) {
2008 ELEMDESC
*elem
= fdesc
->lprgelemdescParam
+i
;
2009 hres
= serialize_param(
2018 xargs
+= _argsize(elem
->tdesc
.vt
);
2020 ERR("Failed to stuballoc param, hres %x\n",hres
);
2025 hres
= xbuf_add (&buf
, (LPBYTE
)&res
, sizeof(DWORD
));
2030 xmsg
->cbBuffer
= buf
.curoff
;
2031 hres
= IRpcChannelBuffer_GetBuffer(rpcchanbuf
, xmsg
, &This
->iid
);
2033 ERR("IRpcChannelBuffer_GetBuffer failed with error 0x%08x\n", hres
);
2036 memcpy(xmsg
->Buffer
, buf
.base
, buf
.curoff
);
2039 for (i
= 0; i
< nrofnames
; i
++)
2040 SysFreeString(names
[i
]);
2042 ITypeInfo_Release(tinfo
);
2043 HeapFree(GetProcessHeap(), 0, args
);
2045 HeapFree(GetProcessHeap(), 0, buf
.base
);
2047 TRACE("returning\n");
2051 static LPRPCSTUBBUFFER WINAPI
2052 TMStubImpl_IsIIDSupported(LPRPCSTUBBUFFER iface
, REFIID riid
) {
2053 FIXME("Huh (%s)?\n",debugstr_guid(riid
));
2058 TMStubImpl_CountRefs(LPRPCSTUBBUFFER iface
) {
2059 TMStubImpl
*This
= (TMStubImpl
*)iface
;
2062 return This
->ref
; /*FIXME? */
2065 static HRESULT WINAPI
2066 TMStubImpl_DebugServerQueryInterface(LPRPCSTUBBUFFER iface
, LPVOID
*ppv
) {
2071 TMStubImpl_DebugServerRelease(LPRPCSTUBBUFFER iface
, LPVOID ppv
) {
2075 static const IRpcStubBufferVtbl tmstubvtbl
= {
2076 TMStubImpl_QueryInterface
,
2080 TMStubImpl_Disconnect
,
2082 TMStubImpl_IsIIDSupported
,
2083 TMStubImpl_CountRefs
,
2084 TMStubImpl_DebugServerQueryInterface
,
2085 TMStubImpl_DebugServerRelease
2088 static HRESULT WINAPI
2089 PSFacBuf_CreateStub(
2090 LPPSFACTORYBUFFER iface
, REFIID riid
,IUnknown
*pUnkServer
,
2091 IRpcStubBuffer
** ppStub
2098 TRACE("(%s,%p,%p)\n",debugstr_guid(riid
),pUnkServer
,ppStub
);
2100 hres
= _get_typeinfo_for_iid(riid
,&tinfo
);
2102 ERR("No typeinfo for %s?\n",debugstr_guid(riid
));
2106 stub
= CoTaskMemAlloc(sizeof(TMStubImpl
));
2108 return E_OUTOFMEMORY
;
2109 stub
->lpvtbl
= &tmstubvtbl
;
2111 stub
->tinfo
= tinfo
;
2112 stub
->dispatch_stub
= NULL
;
2113 stub
->dispatch_derivative
= FALSE
;
2114 memcpy(&(stub
->iid
),riid
,sizeof(*riid
));
2115 hres
= IRpcStubBuffer_Connect((LPRPCSTUBBUFFER
)stub
,pUnkServer
);
2116 *ppStub
= (LPRPCSTUBBUFFER
)stub
;
2117 TRACE("IRpcStubBuffer: %p\n", stub
);
2119 ERR("Connect to pUnkServer failed?\n");
2121 /* if we derive from IDispatch then defer to its stub for some of its methods */
2122 hres
= ITypeInfo_GetTypeAttr(tinfo
, &typeattr
);
2125 if (typeattr
->wTypeFlags
& TYPEFLAG_FDISPATCHABLE
)
2126 stub
->dispatch_derivative
= TRUE
;
2127 ITypeInfo_ReleaseTypeAttr(tinfo
, typeattr
);
2133 static const IPSFactoryBufferVtbl psfacbufvtbl
= {
2134 PSFacBuf_QueryInterface
,
2137 PSFacBuf_CreateProxy
,
2141 /* This is the whole PSFactoryBuffer object, just the vtableptr */
2142 static const IPSFactoryBufferVtbl
*lppsfac
= &psfacbufvtbl
;
2144 /***********************************************************************
2145 * TMARSHAL_DllGetClassObject
2147 HRESULT
TMARSHAL_DllGetClassObject(REFCLSID rclsid
, REFIID iid
,LPVOID
*ppv
)
2149 if (IsEqualIID(iid
,&IID_IPSFactoryBuffer
)) {
2153 return E_NOINTERFACE
;