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 /* Searches function, also in inherited interfaces */
1204 ITypeInfo
*tinfo
, int iMethod
, ITypeInfo
**tactual
, const FUNCDESC
**fdesc
, BSTR
*iname
, BSTR
*fname
)
1209 if (fname
) *fname
= NULL
;
1210 if (iname
) *iname
= NULL
;
1213 hres
= ITypeInfoImpl_GetInternalFuncDesc(tinfo
, i
, fdesc
);
1220 hres
= ITypeInfo_GetTypeAttr(tinfo
, &attr
);
1222 ERR("GetTypeAttr failed with %x\n",hres
);
1225 /* Not found, so look in inherited ifaces. */
1226 for (j
=0;j
<attr
->cImplTypes
;j
++) {
1227 hres
= ITypeInfo_GetRefTypeOfImplType(tinfo
, j
, &href
);
1229 ERR("Did not find a reftype for interface offset %d?\n",j
);
1232 hres
= ITypeInfo_GetRefTypeInfo(tinfo
, href
, &tinfo2
);
1234 ERR("Did not find a typeinfo for reftype %d?\n",href
);
1237 hres
= _get_funcdesc(tinfo2
,iMethod
,tactual
,fdesc
,iname
,fname
);
1238 ITypeInfo_Release(tinfo2
);
1240 ITypeInfo_ReleaseTypeAttr(tinfo
, attr
);
1244 ITypeInfo_ReleaseTypeAttr(tinfo
, attr
);
1247 if (((*fdesc
)->oVft
/4) == iMethod
) {
1249 ITypeInfo_GetDocumentation(tinfo
,(*fdesc
)->memid
,fname
,NULL
,NULL
,NULL
);
1251 ITypeInfo_GetDocumentation(tinfo
,-1,iname
,NULL
,NULL
,NULL
);
1253 ITypeInfo_AddRef(*tactual
);
1260 static inline BOOL
is_in_elem(const ELEMDESC
*elem
)
1262 return (elem
->u
.paramdesc
.wParamFlags
& PARAMFLAG_FIN
|| !elem
->u
.paramdesc
.wParamFlags
);
1265 static inline BOOL
is_out_elem(const ELEMDESC
*elem
)
1267 return (elem
->u
.paramdesc
.wParamFlags
& PARAMFLAG_FOUT
|| !elem
->u
.paramdesc
.wParamFlags
);
1271 xCall(LPVOID retptr
, int method
, TMProxyImpl
*tpinfo
/*, args */)
1273 DWORD
*args
= ((DWORD
*)&tpinfo
)+1, *xargs
;
1274 const FUNCDESC
*fdesc
;
1276 int i
, relaydeb
= TRACE_ON(olerelay
);
1283 DWORD remoteresult
= 0;
1285 IRpcChannelBuffer
*chanbuf
;
1287 EnterCriticalSection(&tpinfo
->crit
);
1289 hres
= _get_funcdesc(tpinfo
->tinfo
,method
,&tinfo
,&fdesc
,&iname
,&fname
);
1291 ERR("Did not find typeinfo/funcdesc entry for method %d!\n",method
);
1292 ITypeInfo_Release(tinfo
);
1293 LeaveCriticalSection(&tpinfo
->crit
);
1297 if (!tpinfo
->chanbuf
)
1299 WARN("Tried to use disconnected proxy\n");
1300 ITypeInfo_Release(tinfo
);
1301 LeaveCriticalSection(&tpinfo
->crit
);
1302 return RPC_E_DISCONNECTED
;
1304 chanbuf
= tpinfo
->chanbuf
;
1305 IRpcChannelBuffer_AddRef(chanbuf
);
1307 LeaveCriticalSection(&tpinfo
->crit
);
1310 TRACE_(olerelay
)("->");
1312 TRACE_(olerelay
)("%s:",relaystr(iname
));
1314 TRACE_(olerelay
)("%s(%d)",relaystr(fname
),method
);
1316 TRACE_(olerelay
)("%d",method
);
1317 TRACE_(olerelay
)("(");
1320 if (iname
) SysFreeString(iname
);
1321 if (fname
) SysFreeString(fname
);
1323 memset(&buf
,0,sizeof(buf
));
1325 /* normal typelib driven serializing */
1327 /* Need them for hack below */
1328 memset(names
,0,sizeof(names
));
1329 if (ITypeInfo_GetNames(tinfo
,fdesc
->memid
,names
,sizeof(names
)/sizeof(names
[0]),&nrofnames
))
1331 if (nrofnames
> sizeof(names
)/sizeof(names
[0]))
1332 ERR("Need more names!\n");
1335 for (i
=0;i
<fdesc
->cParams
;i
++) {
1336 ELEMDESC
*elem
= fdesc
->lprgelemdescParam
+i
;
1338 if (i
) TRACE_(olerelay
)(",");
1339 if (i
+1<nrofnames
&& names
[i
+1])
1340 TRACE_(olerelay
)("%s=",relaystr(names
[i
+1]));
1342 /* No need to marshal other data than FIN and any VT_PTR. */
1343 if (!is_in_elem(elem
) && (elem
->tdesc
.vt
!= VT_PTR
)) {
1344 xargs
+=_argsize(elem
->tdesc
.vt
);
1345 if (relaydeb
) TRACE_(olerelay
)("[out]");
1348 hres
= serialize_param(
1359 ERR("Failed to serialize param, hres %x\n",hres
);
1362 xargs
+=_argsize(elem
->tdesc
.vt
);
1364 if (relaydeb
) TRACE_(olerelay
)(")");
1366 memset(&msg
,0,sizeof(msg
));
1367 msg
.cbBuffer
= buf
.curoff
;
1368 msg
.iMethod
= method
;
1369 hres
= IRpcChannelBuffer_GetBuffer(chanbuf
,&msg
,&(tpinfo
->iid
));
1371 ERR("RpcChannelBuffer GetBuffer failed, %x\n",hres
);
1374 memcpy(msg
.Buffer
,buf
.base
,buf
.curoff
);
1375 if (relaydeb
) TRACE_(olerelay
)("\n");
1376 hres
= IRpcChannelBuffer_SendReceive(chanbuf
,&msg
,&status
);
1378 ERR("RpcChannelBuffer SendReceive failed, %x\n",hres
);
1382 if (relaydeb
) TRACE_(olerelay
)(" status = %08x (",status
);
1384 buf
.base
= HeapReAlloc(GetProcessHeap(),0,buf
.base
,msg
.cbBuffer
);
1386 buf
.base
= HeapAlloc(GetProcessHeap(),0,msg
.cbBuffer
);
1387 buf
.size
= msg
.cbBuffer
;
1388 memcpy(buf
.base
,msg
.Buffer
,buf
.size
);
1391 /* generic deserializer using typelib description */
1394 for (i
=0;i
<fdesc
->cParams
;i
++) {
1395 ELEMDESC
*elem
= fdesc
->lprgelemdescParam
+i
;
1398 if (i
) TRACE_(olerelay
)(",");
1399 if (i
+1<nrofnames
&& names
[i
+1]) TRACE_(olerelay
)("%s=",relaystr(names
[i
+1]));
1401 /* No need to marshal other data than FOUT and any VT_PTR */
1402 if (!is_out_elem(elem
) && (elem
->tdesc
.vt
!= VT_PTR
)) {
1403 xargs
+= _argsize(elem
->tdesc
.vt
);
1404 if (relaydeb
) TRACE_(olerelay
)("[in]");
1407 hres
= deserialize_param(
1417 ERR("Failed to unmarshall param, hres %x\n",hres
);
1421 xargs
+= _argsize(elem
->tdesc
.vt
);
1424 hres
= xbuf_get(&buf
, (LPBYTE
)&remoteresult
, sizeof(DWORD
));
1427 if (relaydeb
) TRACE_(olerelay
)(") = %08x\n", remoteresult
);
1429 hres
= remoteresult
;
1432 for (i
= 0; i
< nrofnames
; i
++)
1433 SysFreeString(names
[i
]);
1434 HeapFree(GetProcessHeap(),0,buf
.base
);
1435 IRpcChannelBuffer_Release(chanbuf
);
1436 ITypeInfo_Release(tinfo
);
1437 TRACE("-- 0x%08x\n", hres
);
1441 static HRESULT WINAPI
ProxyIUnknown_QueryInterface(IUnknown
*iface
, REFIID riid
, void **ppv
)
1443 TMProxyImpl
*proxy
= (TMProxyImpl
*)iface
;
1445 TRACE("(%s, %p)\n", debugstr_guid(riid
), ppv
);
1447 if (proxy
->outerunknown
)
1448 return IUnknown_QueryInterface(proxy
->outerunknown
, riid
, ppv
);
1450 FIXME("No interface\n");
1451 return E_NOINTERFACE
;
1454 static ULONG WINAPI
ProxyIUnknown_AddRef(IUnknown
*iface
)
1456 TMProxyImpl
*proxy
= (TMProxyImpl
*)iface
;
1460 if (proxy
->outerunknown
)
1461 return IUnknown_AddRef(proxy
->outerunknown
);
1463 return 2; /* FIXME */
1466 static ULONG WINAPI
ProxyIUnknown_Release(IUnknown
*iface
)
1468 TMProxyImpl
*proxy
= (TMProxyImpl
*)iface
;
1472 if (proxy
->outerunknown
)
1473 return IUnknown_Release(proxy
->outerunknown
);
1475 return 1; /* FIXME */
1478 static HRESULT WINAPI
ProxyIDispatch_GetTypeInfoCount(LPDISPATCH iface
, UINT
* pctinfo
)
1480 TMProxyImpl
*This
= (TMProxyImpl
*)iface
;
1482 TRACE("(%p)\n", pctinfo
);
1484 return IDispatch_GetTypeInfoCount(This
->dispatch
, pctinfo
);
1487 static HRESULT WINAPI
ProxyIDispatch_GetTypeInfo(LPDISPATCH iface
, UINT iTInfo
, LCID lcid
, ITypeInfo
** ppTInfo
)
1489 TMProxyImpl
*This
= (TMProxyImpl
*)iface
;
1491 TRACE("(%d, %x, %p)\n", iTInfo
, lcid
, ppTInfo
);
1493 return IDispatch_GetTypeInfo(This
->dispatch
, iTInfo
, lcid
, ppTInfo
);
1496 static HRESULT WINAPI
ProxyIDispatch_GetIDsOfNames(LPDISPATCH iface
, REFIID riid
, LPOLESTR
* rgszNames
, UINT cNames
, LCID lcid
, DISPID
* rgDispId
)
1498 TMProxyImpl
*This
= (TMProxyImpl
*)iface
;
1500 TRACE("(%s, %p, %d, 0x%x, %p)\n", debugstr_guid(riid
), rgszNames
, cNames
, lcid
, rgDispId
);
1502 return IDispatch_GetIDsOfNames(This
->dispatch
, riid
, rgszNames
,
1503 cNames
, lcid
, rgDispId
);
1506 static HRESULT WINAPI
ProxyIDispatch_Invoke(LPDISPATCH iface
, DISPID dispIdMember
, REFIID riid
, LCID lcid
,
1507 WORD wFlags
, DISPPARAMS
* pDispParams
, VARIANT
* pVarResult
,
1508 EXCEPINFO
* pExcepInfo
, UINT
* puArgErr
)
1510 TMProxyImpl
*This
= (TMProxyImpl
*)iface
;
1512 TRACE("(%d, %s, 0x%x, 0x%x, %p, %p, %p, %p)\n", dispIdMember
,
1513 debugstr_guid(riid
), lcid
, wFlags
, pDispParams
, pVarResult
,
1514 pExcepInfo
, puArgErr
);
1516 return IDispatch_Invoke(This
->dispatch
, dispIdMember
, riid
, lcid
,
1517 wFlags
, pDispParams
, pVarResult
, pExcepInfo
,
1523 const IRpcChannelBufferVtbl
*lpVtbl
;
1525 /* the IDispatch-derived interface we are handling */
1527 IRpcChannelBuffer
*pDelegateChannel
;
1528 } TMarshalDispatchChannel
;
1530 static HRESULT WINAPI
TMarshalDispatchChannel_QueryInterface(LPRPCCHANNELBUFFER iface
, REFIID riid
, LPVOID
*ppv
)
1533 if (IsEqualIID(riid
,&IID_IRpcChannelBuffer
) || IsEqualIID(riid
,&IID_IUnknown
))
1535 *ppv
= (LPVOID
)iface
;
1536 IUnknown_AddRef(iface
);
1539 return E_NOINTERFACE
;
1542 static ULONG WINAPI
TMarshalDispatchChannel_AddRef(LPRPCCHANNELBUFFER iface
)
1544 TMarshalDispatchChannel
*This
= (TMarshalDispatchChannel
*)iface
;
1545 return InterlockedIncrement(&This
->refs
);
1548 static ULONG WINAPI
TMarshalDispatchChannel_Release(LPRPCCHANNELBUFFER iface
)
1550 TMarshalDispatchChannel
*This
= (TMarshalDispatchChannel
*)iface
;
1553 ref
= InterlockedDecrement(&This
->refs
);
1557 IRpcChannelBuffer_Release(This
->pDelegateChannel
);
1558 HeapFree(GetProcessHeap(), 0, This
);
1562 static HRESULT WINAPI
TMarshalDispatchChannel_GetBuffer(LPRPCCHANNELBUFFER iface
, RPCOLEMESSAGE
* olemsg
, REFIID riid
)
1564 TMarshalDispatchChannel
*This
= (TMarshalDispatchChannel
*)iface
;
1565 TRACE("(%p, %s)\n", olemsg
, debugstr_guid(riid
));
1566 /* Note: we are pretending to invoke a method on the interface identified
1567 * by tmarshal_iid so that we can re-use the IDispatch proxy/stub code
1568 * without the RPC runtime getting confused by not exporting an IDispatch interface */
1569 return IRpcChannelBuffer_GetBuffer(This
->pDelegateChannel
, olemsg
, &This
->tmarshal_iid
);
1572 static HRESULT WINAPI
TMarshalDispatchChannel_SendReceive(LPRPCCHANNELBUFFER iface
, RPCOLEMESSAGE
*olemsg
, ULONG
*pstatus
)
1574 TMarshalDispatchChannel
*This
= (TMarshalDispatchChannel
*)iface
;
1575 TRACE("(%p, %p)\n", olemsg
, pstatus
);
1576 return IRpcChannelBuffer_SendReceive(This
->pDelegateChannel
, olemsg
, pstatus
);
1579 static HRESULT WINAPI
TMarshalDispatchChannel_FreeBuffer(LPRPCCHANNELBUFFER iface
, RPCOLEMESSAGE
* olemsg
)
1581 TMarshalDispatchChannel
*This
= (TMarshalDispatchChannel
*)iface
;
1582 TRACE("(%p)\n", olemsg
);
1583 return IRpcChannelBuffer_FreeBuffer(This
->pDelegateChannel
, olemsg
);
1586 static HRESULT WINAPI
TMarshalDispatchChannel_GetDestCtx(LPRPCCHANNELBUFFER iface
, DWORD
* pdwDestContext
, void** ppvDestContext
)
1588 TMarshalDispatchChannel
*This
= (TMarshalDispatchChannel
*)iface
;
1589 TRACE("(%p,%p)\n", pdwDestContext
, ppvDestContext
);
1590 return IRpcChannelBuffer_GetDestCtx(This
->pDelegateChannel
, pdwDestContext
, ppvDestContext
);
1593 static HRESULT WINAPI
TMarshalDispatchChannel_IsConnected(LPRPCCHANNELBUFFER iface
)
1595 TMarshalDispatchChannel
*This
= (TMarshalDispatchChannel
*)iface
;
1597 return IRpcChannelBuffer_IsConnected(This
->pDelegateChannel
);
1600 static const IRpcChannelBufferVtbl TMarshalDispatchChannelVtbl
=
1602 TMarshalDispatchChannel_QueryInterface
,
1603 TMarshalDispatchChannel_AddRef
,
1604 TMarshalDispatchChannel_Release
,
1605 TMarshalDispatchChannel_GetBuffer
,
1606 TMarshalDispatchChannel_SendReceive
,
1607 TMarshalDispatchChannel_FreeBuffer
,
1608 TMarshalDispatchChannel_GetDestCtx
,
1609 TMarshalDispatchChannel_IsConnected
1612 static HRESULT
TMarshalDispatchChannel_Create(
1613 IRpcChannelBuffer
*pDelegateChannel
, REFIID tmarshal_riid
,
1614 IRpcChannelBuffer
**ppChannel
)
1616 TMarshalDispatchChannel
*This
= HeapAlloc(GetProcessHeap(), 0, sizeof(*This
));
1618 return E_OUTOFMEMORY
;
1620 This
->lpVtbl
= &TMarshalDispatchChannelVtbl
;
1622 IRpcChannelBuffer_AddRef(pDelegateChannel
);
1623 This
->pDelegateChannel
= pDelegateChannel
;
1624 This
->tmarshal_iid
= *tmarshal_riid
;
1626 *ppChannel
= (IRpcChannelBuffer
*)&This
->lpVtbl
;
1631 static inline HRESULT
get_facbuf_for_iid(REFIID riid
, IPSFactoryBuffer
**facbuf
)
1636 if ((hr
= CoGetPSClsid(riid
, &clsid
)))
1638 return CoGetClassObject(&clsid
, CLSCTX_INPROC_SERVER
, NULL
,
1639 &IID_IPSFactoryBuffer
, (LPVOID
*)facbuf
);
1642 static HRESULT WINAPI
1643 PSFacBuf_CreateProxy(
1644 LPPSFACTORYBUFFER iface
, IUnknown
* pUnkOuter
, REFIID riid
,
1645 IRpcProxyBuffer
**ppProxy
, LPVOID
*ppv
)
1650 const FUNCDESC
*fdesc
;
1654 TRACE("(...%s...)\n",debugstr_guid(riid
));
1655 hres
= _get_typeinfo_for_iid(riid
,&tinfo
);
1657 ERR("No typeinfo for %s?\n",debugstr_guid(riid
));
1660 nroffuncs
= _nroffuncs(tinfo
);
1661 proxy
= CoTaskMemAlloc(sizeof(TMProxyImpl
));
1662 if (!proxy
) return E_OUTOFMEMORY
;
1664 assert(sizeof(TMAsmProxy
) == 12);
1666 proxy
->dispatch
= NULL
;
1667 proxy
->dispatch_proxy
= NULL
;
1668 proxy
->outerunknown
= pUnkOuter
;
1669 proxy
->asmstubs
= VirtualAlloc(NULL
, sizeof(TMAsmProxy
) * nroffuncs
, MEM_COMMIT
, PAGE_EXECUTE_READWRITE
);
1670 if (!proxy
->asmstubs
) {
1671 ERR("Could not commit pages for proxy thunks\n");
1672 CoTaskMemFree(proxy
);
1673 return E_OUTOFMEMORY
;
1675 proxy
->lpvtbl2
= &tmproxyvtable
;
1676 /* one reference for the proxy */
1678 proxy
->tinfo
= tinfo
;
1679 memcpy(&proxy
->iid
,riid
,sizeof(*riid
));
1682 InitializeCriticalSection(&proxy
->crit
);
1683 proxy
->crit
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": TMProxyImpl.crit");
1685 proxy
->lpvtbl
= HeapAlloc(GetProcessHeap(),0,sizeof(LPBYTE
)*nroffuncs
);
1686 for (i
=0;i
<nroffuncs
;i
++) {
1687 TMAsmProxy
*xasm
= proxy
->asmstubs
+i
;
1691 proxy
->lpvtbl
[i
] = ProxyIUnknown_QueryInterface
;
1694 proxy
->lpvtbl
[i
] = ProxyIUnknown_AddRef
;
1697 proxy
->lpvtbl
[i
] = ProxyIUnknown_Release
;
1701 /* nrofargs without This */
1704 hres
= _get_funcdesc(tinfo
,i
,&tinfo2
,&fdesc
,NULL
,NULL
);
1705 ITypeInfo_Release(tinfo2
);
1707 ERR("GetFuncDesc %x should not fail here.\n",hres
);
1710 /* some args take more than 4 byte on the stack */
1712 for (j
=0;j
<fdesc
->cParams
;j
++)
1713 nrofargs
+= _argsize(fdesc
->lprgelemdescParam
[j
].tdesc
.vt
);
1716 if (fdesc
->callconv
!= CC_STDCALL
) {
1717 ERR("calling convention is not stdcall????\n");
1720 /* popl %eax - return ptr
1727 * arg3 arg2 arg1 <method> <returnptr>
1729 xasm
->popleax
= 0x58;
1730 xasm
->pushlval
= 0x6a;
1732 xasm
->pushleax
= 0x50;
1733 xasm
->lcall
= 0xe8; /* relative jump */
1734 xasm
->xcall
= (DWORD
)xCall
;
1735 xasm
->xcall
-= (DWORD
)&(xasm
->lret
);
1737 xasm
->bytestopop
= (nrofargs
+2)*4; /* pop args, This, iMethod */
1738 proxy
->lpvtbl
[i
] = xasm
;
1741 FIXME("not implemented on non i386\n");
1748 /* if we derive from IDispatch then defer to its proxy for its methods */
1749 hres
= ITypeInfo_GetTypeAttr(tinfo
, &typeattr
);
1752 if (typeattr
->wTypeFlags
& TYPEFLAG_FDISPATCHABLE
)
1754 IPSFactoryBuffer
*factory_buffer
;
1755 hres
= get_facbuf_for_iid(&IID_IDispatch
, &factory_buffer
);
1758 hres
= IPSFactoryBuffer_CreateProxy(factory_buffer
, NULL
,
1759 &IID_IDispatch
, &proxy
->dispatch_proxy
,
1760 (void **)&proxy
->dispatch
);
1761 IPSFactoryBuffer_Release(factory_buffer
);
1763 if ((hres
== S_OK
) && (nroffuncs
< 7))
1765 ERR("nroffuncs calculated incorrectly (%d)\n", nroffuncs
);
1766 hres
= E_UNEXPECTED
;
1770 proxy
->lpvtbl
[3] = ProxyIDispatch_GetTypeInfoCount
;
1771 proxy
->lpvtbl
[4] = ProxyIDispatch_GetTypeInfo
;
1772 proxy
->lpvtbl
[5] = ProxyIDispatch_GetIDsOfNames
;
1773 proxy
->lpvtbl
[6] = ProxyIDispatch_Invoke
;
1776 ITypeInfo_ReleaseTypeAttr(tinfo
, typeattr
);
1781 *ppv
= (LPVOID
)proxy
;
1782 *ppProxy
= (IRpcProxyBuffer
*)&(proxy
->lpvtbl2
);
1783 IUnknown_AddRef((IUnknown
*)*ppv
);
1787 TMProxyImpl_Release((IRpcProxyBuffer
*)&proxy
->lpvtbl2
);
1791 typedef struct _TMStubImpl
{
1792 const IRpcStubBufferVtbl
*lpvtbl
;
1798 IRpcStubBuffer
*dispatch_stub
;
1799 BOOL dispatch_derivative
;
1802 static HRESULT WINAPI
1803 TMStubImpl_QueryInterface(LPRPCSTUBBUFFER iface
, REFIID riid
, LPVOID
*ppv
)
1805 if (IsEqualIID(riid
,&IID_IRpcStubBuffer
)||IsEqualIID(riid
,&IID_IUnknown
)){
1806 *ppv
= (LPVOID
)iface
;
1807 IRpcStubBuffer_AddRef(iface
);
1810 FIXME("%s, not supported IID.\n",debugstr_guid(riid
));
1811 return E_NOINTERFACE
;
1815 TMStubImpl_AddRef(LPRPCSTUBBUFFER iface
)
1817 TMStubImpl
*This
= (TMStubImpl
*)iface
;
1818 ULONG refCount
= InterlockedIncrement(&This
->ref
);
1820 TRACE("(%p)->(ref before=%u)\n", This
, refCount
- 1);
1826 TMStubImpl_Release(LPRPCSTUBBUFFER iface
)
1828 TMStubImpl
*This
= (TMStubImpl
*)iface
;
1829 ULONG refCount
= InterlockedDecrement(&This
->ref
);
1831 TRACE("(%p)->(ref before=%u)\n", This
, refCount
+ 1);
1835 IRpcStubBuffer_Disconnect(iface
);
1836 ITypeInfo_Release(This
->tinfo
);
1837 if (This
->dispatch_stub
)
1838 IRpcStubBuffer_Release(This
->dispatch_stub
);
1839 CoTaskMemFree(This
);
1844 static HRESULT WINAPI
1845 TMStubImpl_Connect(LPRPCSTUBBUFFER iface
, LPUNKNOWN pUnkServer
)
1847 TMStubImpl
*This
= (TMStubImpl
*)iface
;
1849 TRACE("(%p)->(%p)\n", This
, pUnkServer
);
1851 IUnknown_AddRef(pUnkServer
);
1852 This
->pUnk
= pUnkServer
;
1854 if (This
->dispatch_stub
)
1855 IRpcStubBuffer_Connect(This
->dispatch_stub
, pUnkServer
);
1861 TMStubImpl_Disconnect(LPRPCSTUBBUFFER iface
)
1863 TMStubImpl
*This
= (TMStubImpl
*)iface
;
1865 TRACE("(%p)->()\n", This
);
1869 IUnknown_Release(This
->pUnk
);
1873 if (This
->dispatch_stub
)
1874 IRpcStubBuffer_Disconnect(This
->dispatch_stub
);
1877 static HRESULT WINAPI
1879 LPRPCSTUBBUFFER iface
, RPCOLEMESSAGE
* xmsg
,IRpcChannelBuffer
*rpcchanbuf
)
1882 const FUNCDESC
*fdesc
;
1883 TMStubImpl
*This
= (TMStubImpl
*)iface
;
1885 DWORD
*args
= NULL
, res
, *xargs
, nrofargs
;
1890 ITypeInfo
*tinfo
= NULL
;
1894 if (xmsg
->iMethod
< 3) {
1895 ERR("IUnknown methods cannot be marshaled by the typelib marshaler\n");
1896 return E_UNEXPECTED
;
1899 if (This
->dispatch_derivative
&& xmsg
->iMethod
< sizeof(IDispatchVtbl
)/sizeof(void *))
1901 IPSFactoryBuffer
*factory_buffer
;
1902 hres
= get_facbuf_for_iid(&IID_IDispatch
, &factory_buffer
);
1905 hres
= IPSFactoryBuffer_CreateStub(factory_buffer
, &IID_IDispatch
,
1906 This
->pUnk
, &This
->dispatch_stub
);
1907 IPSFactoryBuffer_Release(factory_buffer
);
1911 return IRpcStubBuffer_Invoke(This
->dispatch_stub
, xmsg
, rpcchanbuf
);
1914 memset(&buf
,0,sizeof(buf
));
1915 buf
.size
= xmsg
->cbBuffer
;
1916 buf
.base
= HeapAlloc(GetProcessHeap(), 0, xmsg
->cbBuffer
);
1917 memcpy(buf
.base
, xmsg
->Buffer
, xmsg
->cbBuffer
);
1920 hres
= _get_funcdesc(This
->tinfo
,xmsg
->iMethod
,&tinfo
,&fdesc
,&iname
,NULL
);
1922 ERR("GetFuncDesc on method %d failed with %x\n",xmsg
->iMethod
,hres
);
1926 if (iname
&& !lstrcmpW(iname
, IDispatchW
))
1928 ERR("IDispatch cannot be marshaled by the typelib marshaler\n");
1929 hres
= E_UNEXPECTED
;
1930 SysFreeString (iname
);
1934 if (iname
) SysFreeString (iname
);
1936 /* Need them for hack below */
1937 memset(names
,0,sizeof(names
));
1938 ITypeInfo_GetNames(tinfo
,fdesc
->memid
,names
,sizeof(names
)/sizeof(names
[0]),&nrofnames
);
1939 if (nrofnames
> sizeof(names
)/sizeof(names
[0])) {
1940 ERR("Need more names!\n");
1943 /*dump_FUNCDESC(fdesc);*/
1945 for (i
=0;i
<fdesc
->cParams
;i
++)
1946 nrofargs
+= _argsize(fdesc
->lprgelemdescParam
[i
].tdesc
.vt
);
1947 args
= HeapAlloc(GetProcessHeap(),0,(nrofargs
+1)*sizeof(DWORD
));
1950 hres
= E_OUTOFMEMORY
;
1954 /* Allocate all stuff used by call. */
1956 for (i
=0;i
<fdesc
->cParams
;i
++) {
1957 ELEMDESC
*elem
= fdesc
->lprgelemdescParam
+i
;
1959 hres
= deserialize_param(
1968 xargs
+= _argsize(elem
->tdesc
.vt
);
1970 ERR("Failed to deserialize param %s, hres %x\n",relaystr(names
[i
+1]),hres
);
1975 args
[0] = (DWORD
)This
->pUnk
;
1980 (*((FARPROC
**)args
[0]))[fdesc
->oVft
/4],
1988 DWORD dwExceptionCode
= GetExceptionCode();
1989 ERR("invoke call failed with exception 0x%08x (%d)\n", dwExceptionCode
, dwExceptionCode
);
1990 if (FAILED(dwExceptionCode
))
1991 hres
= dwExceptionCode
;
1993 hres
= HRESULT_FROM_WIN32(dwExceptionCode
);
2003 for (i
=0;i
<fdesc
->cParams
;i
++) {
2004 ELEMDESC
*elem
= fdesc
->lprgelemdescParam
+i
;
2005 hres
= serialize_param(
2014 xargs
+= _argsize(elem
->tdesc
.vt
);
2016 ERR("Failed to stuballoc param, hres %x\n",hres
);
2021 hres
= xbuf_add (&buf
, (LPBYTE
)&res
, sizeof(DWORD
));
2026 xmsg
->cbBuffer
= buf
.curoff
;
2027 hres
= IRpcChannelBuffer_GetBuffer(rpcchanbuf
, xmsg
, &This
->iid
);
2029 ERR("IRpcChannelBuffer_GetBuffer failed with error 0x%08x\n", hres
);
2032 memcpy(xmsg
->Buffer
, buf
.base
, buf
.curoff
);
2035 for (i
= 0; i
< nrofnames
; i
++)
2036 SysFreeString(names
[i
]);
2038 ITypeInfo_Release(tinfo
);
2039 HeapFree(GetProcessHeap(), 0, args
);
2041 HeapFree(GetProcessHeap(), 0, buf
.base
);
2043 TRACE("returning\n");
2047 static LPRPCSTUBBUFFER WINAPI
2048 TMStubImpl_IsIIDSupported(LPRPCSTUBBUFFER iface
, REFIID riid
) {
2049 FIXME("Huh (%s)?\n",debugstr_guid(riid
));
2054 TMStubImpl_CountRefs(LPRPCSTUBBUFFER iface
) {
2055 TMStubImpl
*This
= (TMStubImpl
*)iface
;
2058 return This
->ref
; /*FIXME? */
2061 static HRESULT WINAPI
2062 TMStubImpl_DebugServerQueryInterface(LPRPCSTUBBUFFER iface
, LPVOID
*ppv
) {
2067 TMStubImpl_DebugServerRelease(LPRPCSTUBBUFFER iface
, LPVOID ppv
) {
2071 static const IRpcStubBufferVtbl tmstubvtbl
= {
2072 TMStubImpl_QueryInterface
,
2076 TMStubImpl_Disconnect
,
2078 TMStubImpl_IsIIDSupported
,
2079 TMStubImpl_CountRefs
,
2080 TMStubImpl_DebugServerQueryInterface
,
2081 TMStubImpl_DebugServerRelease
2084 static HRESULT WINAPI
2085 PSFacBuf_CreateStub(
2086 LPPSFACTORYBUFFER iface
, REFIID riid
,IUnknown
*pUnkServer
,
2087 IRpcStubBuffer
** ppStub
2094 TRACE("(%s,%p,%p)\n",debugstr_guid(riid
),pUnkServer
,ppStub
);
2096 hres
= _get_typeinfo_for_iid(riid
,&tinfo
);
2098 ERR("No typeinfo for %s?\n",debugstr_guid(riid
));
2102 stub
= CoTaskMemAlloc(sizeof(TMStubImpl
));
2104 return E_OUTOFMEMORY
;
2105 stub
->lpvtbl
= &tmstubvtbl
;
2107 stub
->tinfo
= tinfo
;
2108 stub
->dispatch_stub
= NULL
;
2109 stub
->dispatch_derivative
= FALSE
;
2110 memcpy(&(stub
->iid
),riid
,sizeof(*riid
));
2111 hres
= IRpcStubBuffer_Connect((LPRPCSTUBBUFFER
)stub
,pUnkServer
);
2112 *ppStub
= (LPRPCSTUBBUFFER
)stub
;
2113 TRACE("IRpcStubBuffer: %p\n", stub
);
2115 ERR("Connect to pUnkServer failed?\n");
2117 /* if we derive from IDispatch then defer to its stub for some of its methods */
2118 hres
= ITypeInfo_GetTypeAttr(tinfo
, &typeattr
);
2121 if (typeattr
->wTypeFlags
& TYPEFLAG_FDISPATCHABLE
)
2122 stub
->dispatch_derivative
= TRUE
;
2123 ITypeInfo_ReleaseTypeAttr(tinfo
, typeattr
);
2129 static const IPSFactoryBufferVtbl psfacbufvtbl
= {
2130 PSFacBuf_QueryInterface
,
2133 PSFacBuf_CreateProxy
,
2137 /* This is the whole PSFactoryBuffer object, just the vtableptr */
2138 static const IPSFactoryBufferVtbl
*lppsfac
= &psfacbufvtbl
;
2140 /***********************************************************************
2141 * TMARSHAL_DllGetClassObject
2143 HRESULT
TMARSHAL_DllGetClassObject(REFCLSID rclsid
, REFIID iid
,LPVOID
*ppv
)
2145 if (IsEqualIID(iid
,&IID_IPSFactoryBuffer
)) {
2149 return E_NOINTERFACE
;