oleaut32: Rewrite debugstr_vt.
[wine/multimedia.git] / dlls / oleaut32 / tmarshal.c
blobee2a95b9372b66f81e07da719d0c88ab6e67d18e
1 /*
2 * TYPELIB Marshaler
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
24 #include "config.h"
25 #include "wine/port.h"
27 #include <assert.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <ctype.h>
34 #define COBJMACROS
35 #define NONAMELESSUNION
36 #define NONAMELESSSTRUCT
38 #include "winerror.h"
39 #include "windef.h"
40 #include "winbase.h"
41 #include "winnls.h"
42 #include "winreg.h"
43 #include "winuser.h"
45 #include "ole2.h"
46 #include "propidl.h" /* for LPSAFEARRAY_User* functions */
47 #include "typelib.h"
48 #include "variant.h"
49 #include "wine/debug.h"
50 #include "wine/exception.h"
52 static const WCHAR IDispatchW[] = { 'I','D','i','s','p','a','t','c','h',0};
54 WINE_DEFAULT_DEBUG_CHANNEL(ole);
55 WINE_DECLARE_DEBUG_CHANNEL(olerelay);
57 static HRESULT TMarshalDispatchChannel_Create(
58 IRpcChannelBuffer *pDelegateChannel, REFIID tmarshal_riid,
59 IRpcChannelBuffer **ppChannel);
61 typedef struct _marshal_state {
62 LPBYTE base;
63 int size;
64 int curoff;
65 } marshal_state;
67 /* used in the olerelay code to avoid having the L"" stuff added by debugstr_w */
68 static char *relaystr(WCHAR *in) {
69 char *tmp = (char *)debugstr_w(in);
70 tmp += 2;
71 tmp[strlen(tmp)-1] = '\0';
72 return tmp;
75 static HRESULT
76 xbuf_resize(marshal_state *buf, DWORD newsize)
78 if(buf->size >= newsize)
79 return S_FALSE;
81 if(buf->base)
83 buf->base = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buf->base, newsize);
84 if(!buf->base)
85 return E_OUTOFMEMORY;
87 else
89 buf->base = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, newsize);
90 if(!buf->base)
91 return E_OUTOFMEMORY;
93 buf->size = newsize;
94 return S_OK;
97 static HRESULT
98 xbuf_add(marshal_state *buf, const BYTE *stuff, DWORD size)
100 HRESULT hr;
102 if(buf->size - buf->curoff < size)
104 hr = xbuf_resize(buf, buf->size + size + 100);
105 if(FAILED(hr)) return hr;
107 memcpy(buf->base+buf->curoff,stuff,size);
108 buf->curoff += size;
109 return S_OK;
112 static HRESULT
113 xbuf_get(marshal_state *buf, LPBYTE stuff, DWORD size) {
114 if (buf->size < buf->curoff+size) return E_FAIL;
115 memcpy(stuff,buf->base+buf->curoff,size);
116 buf->curoff += size;
117 return S_OK;
120 static HRESULT
121 xbuf_skip(marshal_state *buf, DWORD size) {
122 if (buf->size < buf->curoff+size) return E_FAIL;
123 buf->curoff += size;
124 return S_OK;
127 static HRESULT
128 _unmarshal_interface(marshal_state *buf, REFIID riid, LPUNKNOWN *pUnk) {
129 IStream *pStm;
130 ULARGE_INTEGER newpos;
131 LARGE_INTEGER seekto;
132 ULONG res;
133 HRESULT hres;
134 DWORD xsize;
136 TRACE("...%s...\n",debugstr_guid(riid));
138 *pUnk = NULL;
139 hres = xbuf_get(buf,(LPBYTE)&xsize,sizeof(xsize));
140 if (hres) {
141 ERR("xbuf_get failed\n");
142 return hres;
145 if (xsize == 0) return S_OK;
147 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
148 if (hres) {
149 ERR("Stream create failed %x\n",hres);
150 return hres;
153 hres = IStream_Write(pStm,buf->base+buf->curoff,xsize,&res);
154 if (hres) {
155 ERR("stream write %x\n",hres);
156 IStream_Release(pStm);
157 return hres;
160 memset(&seekto,0,sizeof(seekto));
161 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
162 if (hres) {
163 ERR("Failed Seek %x\n",hres);
164 IStream_Release(pStm);
165 return hres;
168 hres = CoUnmarshalInterface(pStm,riid,(LPVOID*)pUnk);
169 if (hres) {
170 ERR("Unmarshalling interface %s failed with %x\n",debugstr_guid(riid),hres);
171 IStream_Release(pStm);
172 return hres;
175 IStream_Release(pStm);
176 return xbuf_skip(buf,xsize);
179 static HRESULT
180 _marshal_interface(marshal_state *buf, REFIID riid, LPUNKNOWN pUnk) {
181 LPBYTE tempbuf = NULL;
182 IStream *pStm = NULL;
183 STATSTG ststg;
184 ULARGE_INTEGER newpos;
185 LARGE_INTEGER seekto;
186 ULONG res;
187 DWORD xsize;
188 HRESULT hres;
190 if (!pUnk) {
191 /* this is valid, if for instance we serialize
192 * a VT_DISPATCH with NULL ptr which apparently
193 * can happen. S_OK to make sure we continue
194 * serializing.
196 WARN("pUnk is NULL\n");
197 xsize = 0;
198 return xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
201 hres = E_FAIL;
203 TRACE("...%s...\n",debugstr_guid(riid));
205 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
206 if (hres) {
207 ERR("Stream create failed %x\n",hres);
208 goto fail;
211 hres = CoMarshalInterface(pStm,riid,pUnk,0,NULL,0);
212 if (hres) {
213 ERR("Marshalling interface %s failed with %x\n", debugstr_guid(riid), hres);
214 goto fail;
217 hres = IStream_Stat(pStm,&ststg,STATFLAG_NONAME);
218 if (hres) {
219 ERR("Stream stat failed\n");
220 goto fail;
223 tempbuf = HeapAlloc(GetProcessHeap(), 0, ststg.cbSize.u.LowPart);
224 memset(&seekto,0,sizeof(seekto));
225 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
226 if (hres) {
227 ERR("Failed Seek %x\n",hres);
228 goto fail;
231 hres = IStream_Read(pStm,tempbuf,ststg.cbSize.u.LowPart,&res);
232 if (hres) {
233 ERR("Failed Read %x\n",hres);
234 goto fail;
237 xsize = ststg.cbSize.u.LowPart;
238 xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
239 hres = xbuf_add(buf,tempbuf,ststg.cbSize.u.LowPart);
241 HeapFree(GetProcessHeap(),0,tempbuf);
242 IStream_Release(pStm);
244 return hres;
246 fail:
247 xsize = 0;
248 xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
249 if (pStm) IStream_Release(pStm);
250 HeapFree(GetProcessHeap(), 0, tempbuf);
251 return hres;
254 /********************* OLE Proxy/Stub Factory ********************************/
255 static HRESULT WINAPI
256 PSFacBuf_QueryInterface(LPPSFACTORYBUFFER iface, REFIID iid, LPVOID *ppv) {
257 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)||IsEqualIID(iid,&IID_IUnknown)) {
258 *ppv = iface;
259 /* No ref counting, static class */
260 return S_OK;
262 FIXME("(%s) unknown IID?\n",debugstr_guid(iid));
263 return E_NOINTERFACE;
266 static ULONG WINAPI PSFacBuf_AddRef(LPPSFACTORYBUFFER iface) { return 2; }
267 static ULONG WINAPI PSFacBuf_Release(LPPSFACTORYBUFFER iface) { return 1; }
269 static HRESULT
270 _get_typeinfo_for_iid(REFIID riid, ITypeInfo**ti) {
271 HRESULT hres;
272 HKEY ikey;
273 char tlguid[200],typelibkey[300],interfacekey[300],ver[100];
274 char tlfn[260];
275 OLECHAR tlfnW[260];
276 DWORD tlguidlen, verlen, type;
277 LONG tlfnlen;
278 ITypeLib *tl;
280 sprintf( interfacekey, "Interface\\{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\Typelib",
281 riid->Data1, riid->Data2, riid->Data3,
282 riid->Data4[0], riid->Data4[1], riid->Data4[2], riid->Data4[3],
283 riid->Data4[4], riid->Data4[5], riid->Data4[6], riid->Data4[7]
286 if (RegOpenKeyA(HKEY_CLASSES_ROOT,interfacekey,&ikey)) {
287 ERR("No %s key found.\n",interfacekey);
288 return E_FAIL;
290 tlguidlen = sizeof(tlguid);
291 if (RegQueryValueExA(ikey,NULL,NULL,&type,(LPBYTE)tlguid,&tlguidlen)) {
292 ERR("Getting typelib guid failed.\n");
293 RegCloseKey(ikey);
294 return E_FAIL;
296 verlen = sizeof(ver);
297 if (RegQueryValueExA(ikey,"Version",NULL,&type,(LPBYTE)ver,&verlen)) {
298 ERR("Could not get version value?\n");
299 RegCloseKey(ikey);
300 return E_FAIL;
302 RegCloseKey(ikey);
303 sprintf(typelibkey,"Typelib\\%s\\%s\\0\\win%u",tlguid,ver,(sizeof(void*) == 8) ? 64 : 32);
304 tlfnlen = sizeof(tlfn);
305 if (RegQueryValueA(HKEY_CLASSES_ROOT,typelibkey,tlfn,&tlfnlen)) {
306 #ifdef _WIN64
307 sprintf(typelibkey,"Typelib\\%s\\%s\\0\\win32",tlguid,ver);
308 tlfnlen = sizeof(tlfn);
309 if (RegQueryValueA(HKEY_CLASSES_ROOT,typelibkey,tlfn,&tlfnlen)) {
310 #endif
311 ERR("Could not get typelib fn?\n");
312 return E_FAIL;
313 #ifdef _WIN64
315 #endif
317 MultiByteToWideChar(CP_ACP, 0, tlfn, -1, tlfnW, sizeof(tlfnW) / sizeof(tlfnW[0]));
318 hres = LoadTypeLib(tlfnW,&tl);
319 if (hres) {
320 ERR("Failed to load typelib for %s, but it should be there.\n",debugstr_guid(riid));
321 return hres;
323 hres = ITypeLib_GetTypeInfoOfGuid(tl,riid,ti);
324 if (hres) {
325 ERR("typelib does not contain info for %s?\n",debugstr_guid(riid));
326 ITypeLib_Release(tl);
327 return hres;
329 ITypeLib_Release(tl);
330 return hres;
334 * Determine the number of functions including all inherited functions
335 * and well as the size of the vtbl.
336 * Note for non-dual dispinterfaces we simply return the size of IDispatch.
338 static HRESULT num_of_funcs(ITypeInfo *tinfo, unsigned int *num,
339 unsigned int *vtbl_size)
341 HRESULT hr;
342 TYPEATTR *attr;
343 ITypeInfo *tinfo2;
344 UINT inherited_funcs = 0, i;
346 *num = 0;
347 if(vtbl_size) *vtbl_size = 0;
349 hr = ITypeInfo_GetTypeAttr(tinfo, &attr);
350 if (hr)
352 ERR("GetTypeAttr failed with %x\n", hr);
353 return hr;
356 if(attr->typekind == TKIND_DISPATCH)
358 if(attr->wTypeFlags & TYPEFLAG_FDUAL)
360 HREFTYPE href;
362 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
363 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, -1, &href);
364 if(FAILED(hr))
366 ERR("Unable to get interface href from dual dispinterface\n");
367 return hr;
369 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
370 if(FAILED(hr))
372 ERR("Unable to get interface from dual dispinterface\n");
373 return hr;
375 hr = num_of_funcs(tinfo2, num, vtbl_size);
376 ITypeInfo_Release(tinfo2);
377 return hr;
379 else /* non-dual dispinterface */
381 /* These will be the size of IDispatchVtbl */
382 *num = attr->cbSizeVft / sizeof(void *);
383 if(vtbl_size) *vtbl_size = attr->cbSizeVft;
384 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
385 return hr;
389 for (i = 0; i < attr->cImplTypes; i++)
391 HREFTYPE href;
392 ITypeInfo *pSubTypeInfo;
393 UINT sub_funcs;
395 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, i, &href);
396 if (FAILED(hr)) goto end;
397 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &pSubTypeInfo);
398 if (FAILED(hr)) goto end;
400 hr = num_of_funcs(pSubTypeInfo, &sub_funcs, NULL);
401 ITypeInfo_Release(pSubTypeInfo);
403 if(FAILED(hr)) goto end;
404 inherited_funcs += sub_funcs;
407 *num = inherited_funcs + attr->cFuncs;
408 if(vtbl_size) *vtbl_size = attr->cbSizeVft;
410 end:
411 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
412 return hr;
415 #ifdef __i386__
417 #include "pshpack1.h"
419 typedef struct _TMAsmProxy {
420 DWORD lealeax;
421 BYTE pushleax;
422 BYTE pushlval;
423 DWORD nr;
424 BYTE lcall;
425 DWORD xcall;
426 BYTE lret;
427 WORD bytestopop;
428 WORD nop;
429 } TMAsmProxy;
431 #include "poppack.h"
433 #else /* __i386__ */
434 # warning You need to implement stubless proxies for your architecture
435 typedef struct _TMAsmProxy {
436 } TMAsmProxy;
437 #endif
439 typedef struct _TMProxyImpl {
440 LPVOID *lpvtbl;
441 IRpcProxyBuffer IRpcProxyBuffer_iface;
442 LONG ref;
444 TMAsmProxy *asmstubs;
445 ITypeInfo* tinfo;
446 IRpcChannelBuffer* chanbuf;
447 IID iid;
448 CRITICAL_SECTION crit;
449 IUnknown *outerunknown;
450 IDispatch *dispatch;
451 IRpcProxyBuffer *dispatch_proxy;
452 } TMProxyImpl;
454 static inline TMProxyImpl *impl_from_IRpcProxyBuffer( IRpcProxyBuffer *iface )
456 return CONTAINING_RECORD(iface, TMProxyImpl, IRpcProxyBuffer_iface);
459 static HRESULT WINAPI
460 TMProxyImpl_QueryInterface(LPRPCPROXYBUFFER iface, REFIID riid, LPVOID *ppv)
462 TRACE("()\n");
463 if (IsEqualIID(riid,&IID_IUnknown)||IsEqualIID(riid,&IID_IRpcProxyBuffer)) {
464 *ppv = iface;
465 IRpcProxyBuffer_AddRef(iface);
466 return S_OK;
468 FIXME("no interface for %s\n",debugstr_guid(riid));
469 return E_NOINTERFACE;
472 static ULONG WINAPI
473 TMProxyImpl_AddRef(LPRPCPROXYBUFFER iface)
475 TMProxyImpl *This = impl_from_IRpcProxyBuffer( iface );
476 ULONG refCount = InterlockedIncrement(&This->ref);
478 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
480 return refCount;
483 static ULONG WINAPI
484 TMProxyImpl_Release(LPRPCPROXYBUFFER iface)
486 TMProxyImpl *This = impl_from_IRpcProxyBuffer( iface );
487 ULONG refCount = InterlockedDecrement(&This->ref);
489 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
491 if (!refCount)
493 if (This->dispatch_proxy) IRpcProxyBuffer_Release(This->dispatch_proxy);
494 This->crit.DebugInfo->Spare[0] = 0;
495 DeleteCriticalSection(&This->crit);
496 if (This->chanbuf) IRpcChannelBuffer_Release(This->chanbuf);
497 VirtualFree(This->asmstubs, 0, MEM_RELEASE);
498 HeapFree(GetProcessHeap(), 0, This->lpvtbl);
499 ITypeInfo_Release(This->tinfo);
500 CoTaskMemFree(This);
502 return refCount;
505 static HRESULT WINAPI
506 TMProxyImpl_Connect(
507 LPRPCPROXYBUFFER iface,IRpcChannelBuffer* pRpcChannelBuffer)
509 TMProxyImpl *This = impl_from_IRpcProxyBuffer( iface );
511 TRACE("(%p)\n", pRpcChannelBuffer);
513 EnterCriticalSection(&This->crit);
515 IRpcChannelBuffer_AddRef(pRpcChannelBuffer);
516 This->chanbuf = pRpcChannelBuffer;
518 LeaveCriticalSection(&This->crit);
520 if (This->dispatch_proxy)
522 IRpcChannelBuffer *pDelegateChannel;
523 HRESULT hr = TMarshalDispatchChannel_Create(pRpcChannelBuffer, &This->iid, &pDelegateChannel);
524 if (FAILED(hr))
525 return hr;
526 hr = IRpcProxyBuffer_Connect(This->dispatch_proxy, pDelegateChannel);
527 IRpcChannelBuffer_Release(pDelegateChannel);
528 return hr;
531 return S_OK;
534 static void WINAPI
535 TMProxyImpl_Disconnect(LPRPCPROXYBUFFER iface)
537 TMProxyImpl *This = impl_from_IRpcProxyBuffer( iface );
539 TRACE("()\n");
541 EnterCriticalSection(&This->crit);
543 IRpcChannelBuffer_Release(This->chanbuf);
544 This->chanbuf = NULL;
546 LeaveCriticalSection(&This->crit);
548 if (This->dispatch_proxy)
549 IRpcProxyBuffer_Disconnect(This->dispatch_proxy);
553 static const IRpcProxyBufferVtbl tmproxyvtable = {
554 TMProxyImpl_QueryInterface,
555 TMProxyImpl_AddRef,
556 TMProxyImpl_Release,
557 TMProxyImpl_Connect,
558 TMProxyImpl_Disconnect
561 /* how much space do we use on stack in DWORD steps. */
562 static int
563 _argsize(TYPEDESC *tdesc, ITypeInfo *tinfo) {
564 switch (tdesc->vt) {
565 case VT_I8:
566 case VT_UI8:
567 return 8/sizeof(DWORD);
568 case VT_R8:
569 return sizeof(double)/sizeof(DWORD);
570 case VT_CY:
571 return sizeof(CY)/sizeof(DWORD);
572 case VT_DATE:
573 return sizeof(DATE)/sizeof(DWORD);
574 case VT_DECIMAL:
575 return (sizeof(DECIMAL)+3)/sizeof(DWORD);
576 case VT_VARIANT:
577 return (sizeof(VARIANT)+3)/sizeof(DWORD);
578 case VT_USERDEFINED:
580 ITypeInfo *tinfo2;
581 TYPEATTR *tattr;
582 HRESULT hres;
583 DWORD ret;
585 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
586 if (FAILED(hres))
587 return 0; /* should fail critically in serialize_param */
588 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
589 ret = (tattr->cbSizeInstance+3)/sizeof(DWORD);
590 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
591 ITypeInfo_Release(tinfo2);
592 return ret;
594 default:
595 return 1;
599 /* how much space do we use on the heap (in bytes) */
600 static int
601 _xsize(const TYPEDESC *td, ITypeInfo *tinfo) {
602 switch (td->vt) {
603 case VT_DATE:
604 return sizeof(DATE);
605 case VT_CY:
606 return sizeof(CY);
607 case VT_VARIANT:
608 return sizeof(VARIANT);
609 case VT_CARRAY: {
610 int i, arrsize = 1;
611 const ARRAYDESC *adesc = td->u.lpadesc;
613 for (i=0;i<adesc->cDims;i++)
614 arrsize *= adesc->rgbounds[i].cElements;
615 return arrsize*_xsize(&adesc->tdescElem, tinfo);
617 case VT_UI8:
618 case VT_I8:
619 case VT_R8:
620 return 8;
621 case VT_UI2:
622 case VT_I2:
623 case VT_BOOL:
624 return 2;
625 case VT_UI1:
626 case VT_I1:
627 return 1;
628 case VT_USERDEFINED:
630 ITypeInfo *tinfo2;
631 TYPEATTR *tattr;
632 HRESULT hres;
633 DWORD ret;
635 hres = ITypeInfo_GetRefTypeInfo(tinfo,td->u.hreftype,&tinfo2);
636 if (FAILED(hres))
637 return 0;
638 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
639 ret = tattr->cbSizeInstance;
640 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
641 ITypeInfo_Release(tinfo2);
642 return ret;
644 default:
645 return 4;
649 /* Whether we pass this type by reference or by value */
650 static BOOL
651 _passbyref(const TYPEDESC *td, ITypeInfo *tinfo) {
652 return (td->vt == VT_USERDEFINED ||
653 td->vt == VT_VARIANT ||
654 td->vt == VT_PTR);
657 static HRESULT
658 serialize_param(
659 ITypeInfo *tinfo,
660 BOOL writeit,
661 BOOL debugout,
662 BOOL dealloc,
663 TYPEDESC *tdesc,
664 DWORD *arg,
665 marshal_state *buf)
667 HRESULT hres = S_OK;
668 VARTYPE vartype;
670 TRACE("(tdesc.vt %s)\n",debugstr_vt(tdesc->vt));
672 vartype = tdesc->vt;
673 if ((vartype & 0xf000) == VT_ARRAY)
674 vartype = VT_SAFEARRAY;
676 switch (vartype) {
677 case VT_DATE:
678 case VT_I8:
679 case VT_UI8:
680 case VT_R8:
681 case VT_CY:
682 hres = S_OK;
683 if (debugout) TRACE_(olerelay)("%x%x\n",arg[0],arg[1]);
684 if (writeit)
685 hres = xbuf_add(buf,(LPBYTE)arg,8);
686 return hres;
687 case VT_ERROR:
688 case VT_INT:
689 case VT_UINT:
690 case VT_I4:
691 case VT_R4:
692 case VT_UI4:
693 hres = S_OK;
694 if (debugout) TRACE_(olerelay)("%x\n",*arg);
695 if (writeit)
696 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
697 return hres;
698 case VT_I2:
699 case VT_UI2:
700 case VT_BOOL:
701 hres = S_OK;
702 if (debugout) TRACE_(olerelay)("%04x\n",*arg & 0xffff);
703 if (writeit)
704 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
705 return hres;
706 case VT_I1:
707 case VT_UI1:
708 hres = S_OK;
709 if (debugout) TRACE_(olerelay)("%02x\n",*arg & 0xff);
710 if (writeit)
711 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
712 return hres;
713 case VT_VARIANT: {
714 if (debugout) TRACE_(olerelay)("%s", debugstr_variant((VARIANT *)arg));
715 if (writeit)
717 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
718 ULONG size = VARIANT_UserSize(&flags, buf->curoff, (VARIANT *)arg);
719 xbuf_resize(buf, size);
720 VARIANT_UserMarshal(&flags, buf->base + buf->curoff, (VARIANT *)arg);
721 buf->curoff = size;
723 if (dealloc)
725 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
726 VARIANT_UserFree(&flags, (VARIANT *)arg);
728 return S_OK;
730 case VT_BSTR: {
731 if (writeit && debugout) {
732 if (*arg)
733 TRACE_(olerelay)("%s",relaystr((WCHAR*)*arg));
734 else
735 TRACE_(olerelay)("<bstr NULL>");
737 if (writeit)
739 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
740 ULONG size = BSTR_UserSize(&flags, buf->curoff, (BSTR *)arg);
741 xbuf_resize(buf, size);
742 BSTR_UserMarshal(&flags, buf->base + buf->curoff, (BSTR *)arg);
743 buf->curoff = size;
745 if (dealloc)
747 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
748 BSTR_UserFree(&flags, (BSTR *)arg);
750 return S_OK;
752 case VT_PTR: {
753 DWORD cookie;
754 BOOL derefhere = TRUE;
756 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
757 ITypeInfo *tinfo2;
758 TYPEATTR *tattr;
760 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
761 if (hres) {
762 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
763 return hres;
765 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
766 switch (tattr->typekind) {
767 case TKIND_ALIAS:
768 if (tattr->tdescAlias.vt == VT_USERDEFINED)
770 DWORD href = tattr->tdescAlias.u.hreftype;
771 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
772 ITypeInfo_Release(tinfo2);
773 hres = ITypeInfo_GetRefTypeInfo(tinfo,href,&tinfo2);
774 if (hres) {
775 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
776 return hres;
778 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
779 derefhere = (tattr->typekind != TKIND_DISPATCH && tattr->typekind != TKIND_INTERFACE);
781 break;
782 case TKIND_ENUM: /* confirmed */
783 case TKIND_RECORD: /* FIXME: mostly untested */
784 break;
785 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
786 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
787 derefhere=FALSE;
788 break;
789 default:
790 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
791 derefhere=FALSE;
792 break;
794 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
795 ITypeInfo_Release(tinfo2);
798 if (debugout) TRACE_(olerelay)("*");
799 /* Write always, so the other side knows when it gets a NULL pointer.
801 cookie = *arg ? 0x42424242 : 0;
802 hres = xbuf_add(buf,(LPBYTE)&cookie,sizeof(cookie));
803 if (hres)
804 return hres;
805 if (!*arg) {
806 if (debugout) TRACE_(olerelay)("NULL");
807 return S_OK;
809 hres = serialize_param(tinfo,writeit,debugout,dealloc,tdesc->u.lptdesc,(DWORD*)*arg,buf);
810 if (derefhere && dealloc) HeapFree(GetProcessHeap(),0,(LPVOID)*arg);
811 return hres;
813 case VT_UNKNOWN:
814 if (debugout) TRACE_(olerelay)("unk(0x%x)",*arg);
815 if (writeit)
816 hres = _marshal_interface(buf,&IID_IUnknown,(LPUNKNOWN)*arg);
817 if (dealloc && *(IUnknown **)arg)
818 IUnknown_Release((LPUNKNOWN)*arg);
819 return hres;
820 case VT_DISPATCH:
821 if (debugout) TRACE_(olerelay)("idisp(0x%x)",*arg);
822 if (writeit)
823 hres = _marshal_interface(buf,&IID_IDispatch,(LPUNKNOWN)*arg);
824 if (dealloc && *(IUnknown **)arg)
825 IUnknown_Release((LPUNKNOWN)*arg);
826 return hres;
827 case VT_VOID:
828 if (debugout) TRACE_(olerelay)("<void>");
829 return S_OK;
830 case VT_USERDEFINED: {
831 ITypeInfo *tinfo2;
832 TYPEATTR *tattr;
834 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
835 if (hres) {
836 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
837 return hres;
839 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
840 switch (tattr->typekind) {
841 case TKIND_DISPATCH:
842 case TKIND_INTERFACE:
843 if (writeit)
844 hres=_marshal_interface(buf,&(tattr->guid),(LPUNKNOWN)arg);
845 if (dealloc)
846 IUnknown_Release((LPUNKNOWN)arg);
847 break;
848 case TKIND_RECORD: {
849 int i;
850 if (debugout) TRACE_(olerelay)("{");
851 for (i=0;i<tattr->cVars;i++) {
852 VARDESC *vdesc;
853 ELEMDESC *elem2;
854 TYPEDESC *tdesc2;
856 hres = ITypeInfo_GetVarDesc(tinfo2, i, &vdesc);
857 if (hres) {
858 ERR("Could not get vardesc of %d\n",i);
859 return hres;
861 elem2 = &vdesc->elemdescVar;
862 tdesc2 = &elem2->tdesc;
863 hres = serialize_param(
864 tinfo2,
865 writeit,
866 debugout,
867 dealloc,
868 tdesc2,
869 (DWORD*)(((LPBYTE)arg)+vdesc->u.oInst),
872 ITypeInfo_ReleaseVarDesc(tinfo2, vdesc);
873 if (hres!=S_OK)
874 return hres;
875 if (debugout && (i<(tattr->cVars-1)))
876 TRACE_(olerelay)(",");
878 if (debugout) TRACE_(olerelay)("}");
879 break;
881 case TKIND_ALIAS:
882 hres = serialize_param(tinfo2,writeit,debugout,dealloc,&tattr->tdescAlias,arg,buf);
883 break;
884 case TKIND_ENUM:
885 hres = S_OK;
886 if (debugout) TRACE_(olerelay)("%x",*arg);
887 if (writeit)
888 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
889 break;
890 default:
891 FIXME("Unhandled typekind %d\n",tattr->typekind);
892 hres = E_FAIL;
893 break;
895 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
896 ITypeInfo_Release(tinfo2);
897 return hres;
899 case VT_CARRAY: {
900 ARRAYDESC *adesc = tdesc->u.lpadesc;
901 int i, arrsize = 1;
903 if (debugout) TRACE_(olerelay)("carr");
904 for (i=0;i<adesc->cDims;i++) {
905 if (debugout) TRACE_(olerelay)("[%d]",adesc->rgbounds[i].cElements);
906 arrsize *= adesc->rgbounds[i].cElements;
908 if (debugout) TRACE_(olerelay)("(vt %s)",debugstr_vt(adesc->tdescElem.vt));
909 if (debugout) TRACE_(olerelay)("[");
910 for (i=0;i<arrsize;i++) {
911 LPBYTE base = _passbyref(&adesc->tdescElem, tinfo) ? (LPBYTE) *arg : (LPBYTE) arg;
912 hres = serialize_param(tinfo, writeit, debugout, dealloc, &adesc->tdescElem, (DWORD*)((LPBYTE)base+i*_xsize(&adesc->tdescElem, tinfo)), buf);
913 if (hres)
914 return hres;
915 if (debugout && (i<arrsize-1)) TRACE_(olerelay)(",");
917 if (debugout) TRACE_(olerelay)("]");
918 if (dealloc)
919 HeapFree(GetProcessHeap(), 0, *(void **)arg);
920 return S_OK;
922 case VT_SAFEARRAY: {
923 if (writeit)
925 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
926 ULONG size = LPSAFEARRAY_UserSize(&flags, buf->curoff, (LPSAFEARRAY *)arg);
927 xbuf_resize(buf, size);
928 LPSAFEARRAY_UserMarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
929 buf->curoff = size;
931 if (dealloc)
933 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
934 LPSAFEARRAY_UserFree(&flags, (LPSAFEARRAY *)arg);
936 return S_OK;
938 default:
939 ERR("Unhandled marshal type %d.\n",tdesc->vt);
940 return S_OK;
944 static HRESULT
945 deserialize_param(
946 ITypeInfo *tinfo,
947 BOOL readit,
948 BOOL debugout,
949 BOOL alloc,
950 TYPEDESC *tdesc,
951 DWORD *arg,
952 marshal_state *buf)
954 HRESULT hres = S_OK;
955 VARTYPE vartype;
957 TRACE("vt %s at %p\n",debugstr_vt(tdesc->vt),arg);
959 vartype = tdesc->vt;
960 if ((vartype & 0xf000) == VT_ARRAY)
961 vartype = VT_SAFEARRAY;
963 while (1) {
964 switch (vartype) {
965 case VT_VARIANT: {
966 if (readit)
968 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
969 unsigned char *buffer;
970 buffer = VARIANT_UserUnmarshal(&flags, buf->base + buf->curoff, (VARIANT *)arg);
971 buf->curoff = buffer - buf->base;
973 return S_OK;
975 case VT_DATE:
976 case VT_I8:
977 case VT_UI8:
978 case VT_R8:
979 case VT_CY:
980 if (readit) {
981 hres = xbuf_get(buf,(LPBYTE)arg,8);
982 if (hres) ERR("Failed to read integer 8 byte\n");
984 if (debugout) TRACE_(olerelay)("%x%x",arg[0],arg[1]);
985 return hres;
986 case VT_ERROR:
987 case VT_I4:
988 case VT_INT:
989 case VT_UINT:
990 case VT_R4:
991 case VT_UI4:
992 if (readit) {
993 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
994 if (hres) ERR("Failed to read integer 4 byte\n");
996 if (debugout) TRACE_(olerelay)("%x",*arg);
997 return hres;
998 case VT_I2:
999 case VT_UI2:
1000 case VT_BOOL:
1001 if (readit) {
1002 DWORD x;
1003 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
1004 if (hres) ERR("Failed to read integer 4 byte\n");
1005 memcpy(arg,&x,2);
1007 if (debugout) TRACE_(olerelay)("%04x",*arg & 0xffff);
1008 return hres;
1009 case VT_I1:
1010 case VT_UI1:
1011 if (readit) {
1012 DWORD x;
1013 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
1014 if (hres) ERR("Failed to read integer 4 byte\n");
1015 memcpy(arg,&x,1);
1017 if (debugout) TRACE_(olerelay)("%02x",*arg & 0xff);
1018 return hres;
1019 case VT_BSTR: {
1020 if (readit)
1022 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
1023 unsigned char *buffer;
1024 buffer = BSTR_UserUnmarshal(&flags, buf->base + buf->curoff, (BSTR *)arg);
1025 buf->curoff = buffer - buf->base;
1026 if (debugout) TRACE_(olerelay)("%s",debugstr_w(*(BSTR *)arg));
1028 return S_OK;
1030 case VT_PTR: {
1031 DWORD cookie;
1032 BOOL derefhere = TRUE;
1034 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
1035 ITypeInfo *tinfo2;
1036 TYPEATTR *tattr;
1038 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
1039 if (hres) {
1040 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
1041 return hres;
1043 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1044 switch (tattr->typekind) {
1045 case TKIND_ALIAS:
1046 if (tattr->tdescAlias.vt == VT_USERDEFINED)
1048 DWORD href = tattr->tdescAlias.u.hreftype;
1049 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
1050 ITypeInfo_Release(tinfo2);
1051 hres = ITypeInfo_GetRefTypeInfo(tinfo,href,&tinfo2);
1052 if (hres) {
1053 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
1054 return hres;
1056 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1057 derefhere = (tattr->typekind != TKIND_DISPATCH && tattr->typekind != TKIND_INTERFACE);
1059 break;
1060 case TKIND_ENUM: /* confirmed */
1061 case TKIND_RECORD: /* FIXME: mostly untested */
1062 break;
1063 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
1064 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
1065 derefhere=FALSE;
1066 break;
1067 default:
1068 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
1069 derefhere=FALSE;
1070 break;
1072 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1073 ITypeInfo_Release(tinfo2);
1075 /* read it in all cases, we need to know if we have
1076 * NULL pointer or not.
1078 hres = xbuf_get(buf,(LPBYTE)&cookie,sizeof(cookie));
1079 if (hres) {
1080 ERR("Failed to load pointer cookie.\n");
1081 return hres;
1083 if (cookie != 0x42424242) {
1084 /* we read a NULL ptr from the remote side */
1085 if (debugout) TRACE_(olerelay)("NULL");
1086 *arg = 0;
1087 return S_OK;
1089 if (debugout) TRACE_(olerelay)("*");
1090 if (alloc) {
1091 /* Allocate space for the referenced struct */
1092 if (derefhere)
1093 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,_xsize(tdesc->u.lptdesc, tinfo));
1095 if (derefhere)
1096 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, (LPDWORD)*arg, buf);
1097 else
1098 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, arg, buf);
1100 case VT_UNKNOWN:
1101 /* FIXME: UNKNOWN is unknown ..., but allocate 4 byte for it */
1102 if (alloc)
1103 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
1104 hres = S_OK;
1105 if (readit)
1106 hres = _unmarshal_interface(buf,&IID_IUnknown,(LPUNKNOWN*)arg);
1107 if (debugout)
1108 TRACE_(olerelay)("unk(%p)",arg);
1109 return hres;
1110 case VT_DISPATCH:
1111 hres = S_OK;
1112 if (readit)
1113 hres = _unmarshal_interface(buf,&IID_IDispatch,(LPUNKNOWN*)arg);
1114 if (debugout)
1115 TRACE_(olerelay)("idisp(%p)",arg);
1116 return hres;
1117 case VT_VOID:
1118 if (debugout) TRACE_(olerelay)("<void>");
1119 return S_OK;
1120 case VT_USERDEFINED: {
1121 ITypeInfo *tinfo2;
1122 TYPEATTR *tattr;
1124 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
1125 if (hres) {
1126 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
1127 return hres;
1129 hres = ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1130 if (hres) {
1131 ERR("Could not get typeattr in VT_USERDEFINED.\n");
1132 } else {
1133 switch (tattr->typekind) {
1134 case TKIND_DISPATCH:
1135 case TKIND_INTERFACE:
1136 if (readit)
1137 hres = _unmarshal_interface(buf,&(tattr->guid),(LPUNKNOWN*)arg);
1138 break;
1139 case TKIND_RECORD: {
1140 int i;
1142 if (debugout) TRACE_(olerelay)("{");
1143 for (i=0;i<tattr->cVars;i++) {
1144 VARDESC *vdesc;
1146 hres = ITypeInfo_GetVarDesc(tinfo2, i, &vdesc);
1147 if (hres) {
1148 ERR("Could not get vardesc of %d\n",i);
1149 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1150 ITypeInfo_Release(tinfo2);
1151 return hres;
1153 hres = deserialize_param(
1154 tinfo2,
1155 readit,
1156 debugout,
1157 alloc,
1158 &vdesc->elemdescVar.tdesc,
1159 (DWORD*)(((LPBYTE)arg)+vdesc->u.oInst),
1162 ITypeInfo_ReleaseVarDesc(tinfo2, vdesc);
1163 if (debugout && (i<tattr->cVars-1)) TRACE_(olerelay)(",");
1165 if (debugout) TRACE_(olerelay)("}");
1166 break;
1168 case TKIND_ALIAS:
1169 hres = deserialize_param(tinfo2,readit,debugout,alloc,&tattr->tdescAlias,arg,buf);
1170 break;
1171 case TKIND_ENUM:
1172 if (readit) {
1173 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
1174 if (hres) ERR("Failed to read enum (4 byte)\n");
1176 if (debugout) TRACE_(olerelay)("%x",*arg);
1177 break;
1178 default:
1179 ERR("Unhandled typekind %d\n",tattr->typekind);
1180 hres = E_FAIL;
1181 break;
1183 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1185 if (hres)
1186 ERR("failed to stuballoc in TKIND_RECORD.\n");
1187 ITypeInfo_Release(tinfo2);
1188 return hres;
1190 case VT_CARRAY: {
1191 /* arg is pointing to the start of the array. */
1192 LPBYTE base = (LPBYTE) arg;
1193 ARRAYDESC *adesc = tdesc->u.lpadesc;
1194 int arrsize,i;
1195 arrsize = 1;
1196 if (adesc->cDims > 1) FIXME("cDims > 1 in VT_CARRAY. Does it work?\n");
1197 for (i=0;i<adesc->cDims;i++)
1198 arrsize *= adesc->rgbounds[i].cElements;
1199 if (_passbyref(&adesc->tdescElem, tinfo))
1201 base = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,_xsize(tdesc->u.lptdesc, tinfo) * arrsize);
1202 *arg = (DWORD) base;
1204 for (i=0;i<arrsize;i++)
1205 deserialize_param(
1206 tinfo,
1207 readit,
1208 debugout,
1209 alloc,
1210 &adesc->tdescElem,
1211 (DWORD*)(base + i*_xsize(&adesc->tdescElem, tinfo)),
1214 return S_OK;
1216 case VT_SAFEARRAY: {
1217 if (readit)
1219 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
1220 unsigned char *buffer;
1221 buffer = LPSAFEARRAY_UserUnmarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
1222 buf->curoff = buffer - buf->base;
1224 return S_OK;
1226 default:
1227 ERR("No handler for VT type %d!\n",tdesc->vt);
1228 return S_OK;
1233 /* Retrieves a function's funcdesc, searching back into inherited interfaces. */
1234 static HRESULT get_funcdesc(ITypeInfo *tinfo, int iMethod, ITypeInfo **tactual, const FUNCDESC **fdesc,
1235 BSTR *iname, BSTR *fname, UINT *num)
1237 HRESULT hr;
1238 UINT i, impl_types;
1239 UINT inherited_funcs = 0;
1240 TYPEATTR *attr;
1242 if (fname) *fname = NULL;
1243 if (iname) *iname = NULL;
1244 if (num) *num = 0;
1245 *tactual = NULL;
1247 hr = ITypeInfo_GetTypeAttr(tinfo, &attr);
1248 if (FAILED(hr))
1250 ERR("GetTypeAttr failed with %x\n",hr);
1251 return hr;
1254 if(attr->typekind == TKIND_DISPATCH)
1256 if(attr->wTypeFlags & TYPEFLAG_FDUAL)
1258 HREFTYPE href;
1259 ITypeInfo *tinfo2;
1261 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, -1, &href);
1262 if(FAILED(hr))
1264 ERR("Cannot get interface href from dual dispinterface\n");
1265 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1266 return hr;
1268 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
1269 if(FAILED(hr))
1271 ERR("Cannot get interface from dual dispinterface\n");
1272 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1273 return hr;
1275 hr = get_funcdesc(tinfo2, iMethod, tactual, fdesc, iname, fname, num);
1276 ITypeInfo_Release(tinfo2);
1277 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1278 return hr;
1280 ERR("Shouldn't be called with a non-dual dispinterface\n");
1281 return E_FAIL;
1284 impl_types = attr->cImplTypes;
1285 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1287 for (i = 0; i < impl_types; i++)
1289 HREFTYPE href;
1290 ITypeInfo *pSubTypeInfo;
1291 UINT sub_funcs;
1293 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, i, &href);
1294 if (FAILED(hr)) return hr;
1295 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &pSubTypeInfo);
1296 if (FAILED(hr)) return hr;
1298 hr = get_funcdesc(pSubTypeInfo, iMethod, tactual, fdesc, iname, fname, &sub_funcs);
1299 inherited_funcs += sub_funcs;
1300 ITypeInfo_Release(pSubTypeInfo);
1301 if(SUCCEEDED(hr)) return hr;
1303 if(iMethod < inherited_funcs)
1305 ERR("shouldn't be here\n");
1306 return E_INVALIDARG;
1309 for(i = inherited_funcs; i <= iMethod; i++)
1311 hr = ITypeInfoImpl_GetInternalFuncDesc(tinfo, i - inherited_funcs, fdesc);
1312 if(FAILED(hr))
1314 if(num) *num = i;
1315 return hr;
1319 /* found it. We don't care about num so zero it */
1320 if(num) *num = 0;
1321 *tactual = tinfo;
1322 ITypeInfo_AddRef(*tactual);
1323 if (fname) ITypeInfo_GetDocumentation(tinfo,(*fdesc)->memid,fname,NULL,NULL,NULL);
1324 if (iname) ITypeInfo_GetDocumentation(tinfo,-1,iname,NULL,NULL,NULL);
1325 return S_OK;
1328 static inline BOOL is_in_elem(const ELEMDESC *elem)
1330 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN || !elem->u.paramdesc.wParamFlags);
1333 static inline BOOL is_out_elem(const ELEMDESC *elem)
1335 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT || !elem->u.paramdesc.wParamFlags);
1338 static DWORD WINAPI xCall(int method, void **args)
1340 TMProxyImpl *tpinfo = args[0];
1341 DWORD *xargs;
1342 const FUNCDESC *fdesc;
1343 HRESULT hres;
1344 int i;
1345 marshal_state buf;
1346 RPCOLEMESSAGE msg;
1347 ULONG status;
1348 BSTR fname,iname;
1349 BSTR names[10];
1350 UINT nrofnames;
1351 DWORD remoteresult = 0;
1352 ITypeInfo *tinfo;
1353 IRpcChannelBuffer *chanbuf;
1355 EnterCriticalSection(&tpinfo->crit);
1357 hres = get_funcdesc(tpinfo->tinfo,method,&tinfo,&fdesc,&iname,&fname,NULL);
1358 if (hres) {
1359 ERR("Did not find typeinfo/funcdesc entry for method %d!\n",method);
1360 LeaveCriticalSection(&tpinfo->crit);
1361 return E_FAIL;
1364 if (!tpinfo->chanbuf)
1366 WARN("Tried to use disconnected proxy\n");
1367 ITypeInfo_Release(tinfo);
1368 LeaveCriticalSection(&tpinfo->crit);
1369 return RPC_E_DISCONNECTED;
1371 chanbuf = tpinfo->chanbuf;
1372 IRpcChannelBuffer_AddRef(chanbuf);
1374 LeaveCriticalSection(&tpinfo->crit);
1376 if (TRACE_ON(olerelay)) {
1377 TRACE_(olerelay)("->");
1378 if (iname)
1379 TRACE_(olerelay)("%s:",relaystr(iname));
1380 if (fname)
1381 TRACE_(olerelay)("%s(%d)",relaystr(fname),method);
1382 else
1383 TRACE_(olerelay)("%d",method);
1384 TRACE_(olerelay)("(");
1387 SysFreeString(iname);
1388 SysFreeString(fname);
1390 memset(&buf,0,sizeof(buf));
1392 /* normal typelib driven serializing */
1394 /* Need them for hack below */
1395 memset(names,0,sizeof(names));
1396 if (ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames))
1397 nrofnames = 0;
1398 if (nrofnames > sizeof(names)/sizeof(names[0]))
1399 ERR("Need more names!\n");
1401 xargs = (DWORD *)(args + 1);
1402 for (i=0;i<fdesc->cParams;i++) {
1403 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1404 if (TRACE_ON(olerelay)) {
1405 if (i) TRACE_(olerelay)(",");
1406 if (i+1<nrofnames && names[i+1])
1407 TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1409 /* No need to marshal other data than FIN and any VT_PTR. */
1410 if (!is_in_elem(elem))
1412 if (elem->tdesc.vt != VT_PTR)
1414 xargs+=_argsize(&elem->tdesc, tinfo);
1415 TRACE_(olerelay)("[out]");
1416 continue;
1418 else
1420 memset( *(void **)xargs, 0, _xsize( elem->tdesc.u.lptdesc, tinfo ) );
1424 hres = serialize_param(
1425 tinfo,
1426 is_in_elem(elem),
1427 TRACE_ON(olerelay),
1428 FALSE,
1429 &elem->tdesc,
1430 xargs,
1431 &buf
1434 if (hres) {
1435 ERR("Failed to serialize param, hres %x\n",hres);
1436 break;
1438 xargs+=_argsize(&elem->tdesc, tinfo);
1440 TRACE_(olerelay)(")");
1442 memset(&msg,0,sizeof(msg));
1443 msg.cbBuffer = buf.curoff;
1444 msg.iMethod = method;
1445 hres = IRpcChannelBuffer_GetBuffer(chanbuf,&msg,&(tpinfo->iid));
1446 if (hres) {
1447 ERR("RpcChannelBuffer GetBuffer failed, %x\n",hres);
1448 goto exit;
1450 memcpy(msg.Buffer,buf.base,buf.curoff);
1451 TRACE_(olerelay)("\n");
1452 hres = IRpcChannelBuffer_SendReceive(chanbuf,&msg,&status);
1453 if (hres) {
1454 ERR("RpcChannelBuffer SendReceive failed, %x\n",hres);
1455 goto exit;
1458 TRACE_(olerelay)(" status = %08x (",status);
1459 if (buf.base)
1460 buf.base = HeapReAlloc(GetProcessHeap(),0,buf.base,msg.cbBuffer);
1461 else
1462 buf.base = HeapAlloc(GetProcessHeap(),0,msg.cbBuffer);
1463 buf.size = msg.cbBuffer;
1464 memcpy(buf.base,msg.Buffer,buf.size);
1465 buf.curoff = 0;
1467 /* generic deserializer using typelib description */
1468 xargs = (DWORD *)(args + 1);
1469 status = S_OK;
1470 for (i=0;i<fdesc->cParams;i++) {
1471 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1473 if (i) TRACE_(olerelay)(",");
1474 if (i+1<nrofnames && names[i+1]) TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1476 /* No need to marshal other data than FOUT and any VT_PTR */
1477 if (!is_out_elem(elem) && (elem->tdesc.vt != VT_PTR)) {
1478 xargs += _argsize(&elem->tdesc, tinfo);
1479 TRACE_(olerelay)("[in]");
1480 continue;
1482 hres = deserialize_param(
1483 tinfo,
1484 is_out_elem(elem),
1485 TRACE_ON(olerelay),
1486 FALSE,
1487 &(elem->tdesc),
1488 xargs,
1489 &buf
1491 if (hres) {
1492 ERR("Failed to unmarshall param, hres %x\n",hres);
1493 status = hres;
1494 break;
1496 xargs += _argsize(&elem->tdesc, tinfo);
1499 hres = xbuf_get(&buf, (LPBYTE)&remoteresult, sizeof(DWORD));
1500 if (hres != S_OK)
1501 goto exit;
1502 TRACE_(olerelay)(") = %08x\n", remoteresult);
1504 hres = remoteresult;
1506 exit:
1507 IRpcChannelBuffer_FreeBuffer(chanbuf,&msg);
1508 for (i = 0; i < nrofnames; i++)
1509 SysFreeString(names[i]);
1510 HeapFree(GetProcessHeap(),0,buf.base);
1511 IRpcChannelBuffer_Release(chanbuf);
1512 ITypeInfo_Release(tinfo);
1513 TRACE("-- 0x%08x\n", hres);
1514 return hres;
1517 static HRESULT WINAPI ProxyIUnknown_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
1519 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1521 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1523 if (proxy->outerunknown)
1524 return IUnknown_QueryInterface(proxy->outerunknown, riid, ppv);
1526 FIXME("No interface\n");
1527 return E_NOINTERFACE;
1530 static ULONG WINAPI ProxyIUnknown_AddRef(IUnknown *iface)
1532 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1534 TRACE("\n");
1536 if (proxy->outerunknown)
1537 return IUnknown_AddRef(proxy->outerunknown);
1539 return 2; /* FIXME */
1542 static ULONG WINAPI ProxyIUnknown_Release(IUnknown *iface)
1544 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1546 TRACE("\n");
1548 if (proxy->outerunknown)
1549 return IUnknown_Release(proxy->outerunknown);
1551 return 1; /* FIXME */
1554 static HRESULT WINAPI ProxyIDispatch_GetTypeInfoCount(LPDISPATCH iface, UINT * pctinfo)
1556 TMProxyImpl *This = (TMProxyImpl *)iface;
1558 TRACE("(%p)\n", pctinfo);
1560 return IDispatch_GetTypeInfoCount(This->dispatch, pctinfo);
1563 static HRESULT WINAPI ProxyIDispatch_GetTypeInfo(LPDISPATCH iface, UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo)
1565 TMProxyImpl *This = (TMProxyImpl *)iface;
1567 TRACE("(%d, %x, %p)\n", iTInfo, lcid, ppTInfo);
1569 return IDispatch_GetTypeInfo(This->dispatch, iTInfo, lcid, ppTInfo);
1572 static HRESULT WINAPI ProxyIDispatch_GetIDsOfNames(LPDISPATCH iface, REFIID riid, LPOLESTR * rgszNames, UINT cNames, LCID lcid, DISPID * rgDispId)
1574 TMProxyImpl *This = (TMProxyImpl *)iface;
1576 TRACE("(%s, %p, %d, 0x%x, %p)\n", debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);
1578 return IDispatch_GetIDsOfNames(This->dispatch, riid, rgszNames,
1579 cNames, lcid, rgDispId);
1582 static HRESULT WINAPI ProxyIDispatch_Invoke(LPDISPATCH iface, DISPID dispIdMember, REFIID riid, LCID lcid,
1583 WORD wFlags, DISPPARAMS * pDispParams, VARIANT * pVarResult,
1584 EXCEPINFO * pExcepInfo, UINT * puArgErr)
1586 TMProxyImpl *This = (TMProxyImpl *)iface;
1588 TRACE("(%d, %s, 0x%x, 0x%x, %p, %p, %p, %p)\n", dispIdMember,
1589 debugstr_guid(riid), lcid, wFlags, pDispParams, pVarResult,
1590 pExcepInfo, puArgErr);
1592 return IDispatch_Invoke(This->dispatch, dispIdMember, riid, lcid,
1593 wFlags, pDispParams, pVarResult, pExcepInfo,
1594 puArgErr);
1597 typedef struct
1599 IRpcChannelBuffer IRpcChannelBuffer_iface;
1600 LONG refs;
1601 /* the IDispatch-derived interface we are handling */
1602 IID tmarshal_iid;
1603 IRpcChannelBuffer *pDelegateChannel;
1604 } TMarshalDispatchChannel;
1606 static inline TMarshalDispatchChannel *impl_from_IRpcChannelBuffer(IRpcChannelBuffer *iface)
1608 return CONTAINING_RECORD(iface, TMarshalDispatchChannel, IRpcChannelBuffer_iface);
1611 static HRESULT WINAPI TMarshalDispatchChannel_QueryInterface(IRpcChannelBuffer *iface, REFIID riid, LPVOID *ppv)
1613 *ppv = NULL;
1614 if (IsEqualIID(riid,&IID_IRpcChannelBuffer) || IsEqualIID(riid,&IID_IUnknown))
1616 *ppv = iface;
1617 IRpcChannelBuffer_AddRef(iface);
1618 return S_OK;
1620 return E_NOINTERFACE;
1623 static ULONG WINAPI TMarshalDispatchChannel_AddRef(LPRPCCHANNELBUFFER iface)
1625 TMarshalDispatchChannel *This = impl_from_IRpcChannelBuffer(iface);
1626 return InterlockedIncrement(&This->refs);
1629 static ULONG WINAPI TMarshalDispatchChannel_Release(LPRPCCHANNELBUFFER iface)
1631 TMarshalDispatchChannel *This = impl_from_IRpcChannelBuffer(iface);
1632 ULONG ref;
1634 ref = InterlockedDecrement(&This->refs);
1635 if (ref)
1636 return ref;
1638 IRpcChannelBuffer_Release(This->pDelegateChannel);
1639 HeapFree(GetProcessHeap(), 0, This);
1640 return 0;
1643 static HRESULT WINAPI TMarshalDispatchChannel_GetBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg, REFIID riid)
1645 TMarshalDispatchChannel *This = impl_from_IRpcChannelBuffer(iface);
1646 TRACE("(%p, %s)\n", olemsg, debugstr_guid(riid));
1647 /* Note: we are pretending to invoke a method on the interface identified
1648 * by tmarshal_iid so that we can re-use the IDispatch proxy/stub code
1649 * without the RPC runtime getting confused by not exporting an IDispatch interface */
1650 return IRpcChannelBuffer_GetBuffer(This->pDelegateChannel, olemsg, &This->tmarshal_iid);
1653 static HRESULT WINAPI TMarshalDispatchChannel_SendReceive(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE *olemsg, ULONG *pstatus)
1655 TMarshalDispatchChannel *This = impl_from_IRpcChannelBuffer(iface);
1656 TRACE("(%p, %p)\n", olemsg, pstatus);
1657 return IRpcChannelBuffer_SendReceive(This->pDelegateChannel, olemsg, pstatus);
1660 static HRESULT WINAPI TMarshalDispatchChannel_FreeBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg)
1662 TMarshalDispatchChannel *This = impl_from_IRpcChannelBuffer(iface);
1663 TRACE("(%p)\n", olemsg);
1664 return IRpcChannelBuffer_FreeBuffer(This->pDelegateChannel, olemsg);
1667 static HRESULT WINAPI TMarshalDispatchChannel_GetDestCtx(LPRPCCHANNELBUFFER iface, DWORD* pdwDestContext, void** ppvDestContext)
1669 TMarshalDispatchChannel *This = impl_from_IRpcChannelBuffer(iface);
1670 TRACE("(%p,%p)\n", pdwDestContext, ppvDestContext);
1671 return IRpcChannelBuffer_GetDestCtx(This->pDelegateChannel, pdwDestContext, ppvDestContext);
1674 static HRESULT WINAPI TMarshalDispatchChannel_IsConnected(LPRPCCHANNELBUFFER iface)
1676 TMarshalDispatchChannel *This = impl_from_IRpcChannelBuffer(iface);
1677 TRACE("()\n");
1678 return IRpcChannelBuffer_IsConnected(This->pDelegateChannel);
1681 static const IRpcChannelBufferVtbl TMarshalDispatchChannelVtbl =
1683 TMarshalDispatchChannel_QueryInterface,
1684 TMarshalDispatchChannel_AddRef,
1685 TMarshalDispatchChannel_Release,
1686 TMarshalDispatchChannel_GetBuffer,
1687 TMarshalDispatchChannel_SendReceive,
1688 TMarshalDispatchChannel_FreeBuffer,
1689 TMarshalDispatchChannel_GetDestCtx,
1690 TMarshalDispatchChannel_IsConnected
1693 static HRESULT TMarshalDispatchChannel_Create(
1694 IRpcChannelBuffer *pDelegateChannel, REFIID tmarshal_riid,
1695 IRpcChannelBuffer **ppChannel)
1697 TMarshalDispatchChannel *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1698 if (!This)
1699 return E_OUTOFMEMORY;
1701 This->IRpcChannelBuffer_iface.lpVtbl = &TMarshalDispatchChannelVtbl;
1702 This->refs = 1;
1703 IRpcChannelBuffer_AddRef(pDelegateChannel);
1704 This->pDelegateChannel = pDelegateChannel;
1705 This->tmarshal_iid = *tmarshal_riid;
1707 *ppChannel = &This->IRpcChannelBuffer_iface;
1708 return S_OK;
1712 static inline HRESULT get_facbuf_for_iid(REFIID riid, IPSFactoryBuffer **facbuf)
1714 HRESULT hr;
1715 CLSID clsid;
1717 if ((hr = CoGetPSClsid(riid, &clsid)))
1718 return hr;
1719 return CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL,
1720 &IID_IPSFactoryBuffer, (LPVOID*)facbuf);
1723 static HRESULT init_proxy_entry_point(TMProxyImpl *proxy, unsigned int num)
1725 int j;
1726 /* nrofargs including This */
1727 int nrofargs = 1;
1728 ITypeInfo *tinfo2;
1729 TMAsmProxy *xasm = proxy->asmstubs + num;
1730 HRESULT hres;
1731 const FUNCDESC *fdesc;
1733 hres = get_funcdesc(proxy->tinfo, num, &tinfo2, &fdesc, NULL, NULL, NULL);
1734 if (hres) {
1735 ERR("GetFuncDesc %x should not fail here.\n",hres);
1736 return hres;
1738 ITypeInfo_Release(tinfo2);
1739 /* some args take more than 4 byte on the stack */
1740 for (j=0;j<fdesc->cParams;j++)
1741 nrofargs += _argsize(&fdesc->lprgelemdescParam[j].tdesc, proxy->tinfo);
1743 #ifdef __i386__
1744 if (fdesc->callconv != CC_STDCALL) {
1745 ERR("calling convention is not stdcall????\n");
1746 return E_FAIL;
1748 /* leal 4(%esp),%eax
1749 * pushl %eax
1750 * pushl <nr>
1751 * call xCall
1752 * lret <nr>
1754 xasm->lealeax = 0x0424448d;
1755 xasm->pushleax = 0x50;
1756 xasm->pushlval = 0x68;
1757 xasm->nr = num;
1758 xasm->lcall = 0xe8;
1759 xasm->xcall = (char *)xCall - (char *)&xasm->lret;
1760 xasm->lret = 0xc2;
1761 xasm->bytestopop = nrofargs * 4;
1762 xasm->nop = 0x9090;
1763 proxy->lpvtbl[fdesc->oVft / sizeof(void *)] = xasm;
1764 #else
1765 FIXME("not implemented on non i386\n");
1766 return E_FAIL;
1767 #endif
1768 return S_OK;
1771 static HRESULT WINAPI
1772 PSFacBuf_CreateProxy(
1773 LPPSFACTORYBUFFER iface, IUnknown* pUnkOuter, REFIID riid,
1774 IRpcProxyBuffer **ppProxy, LPVOID *ppv)
1776 HRESULT hres;
1777 ITypeInfo *tinfo;
1778 unsigned int i, nroffuncs, vtbl_size;
1779 TMProxyImpl *proxy;
1780 TYPEATTR *typeattr;
1781 BOOL defer_to_dispatch = FALSE;
1783 TRACE("(...%s...)\n",debugstr_guid(riid));
1784 hres = _get_typeinfo_for_iid(riid,&tinfo);
1785 if (hres) {
1786 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
1787 return hres;
1790 hres = num_of_funcs(tinfo, &nroffuncs, &vtbl_size);
1791 TRACE("Got %d funcs, vtbl size %d\n", nroffuncs, vtbl_size);
1793 if (FAILED(hres)) {
1794 ERR("Cannot get number of functions for typeinfo %s\n",debugstr_guid(riid));
1795 ITypeInfo_Release(tinfo);
1796 return hres;
1799 proxy = CoTaskMemAlloc(sizeof(TMProxyImpl));
1800 if (!proxy) return E_OUTOFMEMORY;
1802 proxy->dispatch = NULL;
1803 proxy->dispatch_proxy = NULL;
1804 proxy->outerunknown = pUnkOuter;
1805 proxy->asmstubs = VirtualAlloc(NULL, sizeof(TMAsmProxy) * nroffuncs, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
1806 if (!proxy->asmstubs) {
1807 ERR("Could not commit pages for proxy thunks\n");
1808 CoTaskMemFree(proxy);
1809 return E_OUTOFMEMORY;
1811 proxy->IRpcProxyBuffer_iface.lpVtbl = &tmproxyvtable;
1812 /* one reference for the proxy */
1813 proxy->ref = 1;
1814 proxy->tinfo = tinfo;
1815 proxy->iid = *riid;
1816 proxy->chanbuf = 0;
1818 InitializeCriticalSection(&proxy->crit);
1819 proxy->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TMProxyImpl.crit");
1821 proxy->lpvtbl = HeapAlloc(GetProcessHeap(), 0, vtbl_size);
1823 /* if we derive from IDispatch then defer to its proxy for its methods */
1824 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
1825 if (hres == S_OK)
1827 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
1829 IPSFactoryBuffer *factory_buffer;
1830 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
1831 if (hres == S_OK)
1833 hres = IPSFactoryBuffer_CreateProxy(factory_buffer, NULL,
1834 &IID_IDispatch, &proxy->dispatch_proxy,
1835 (void **)&proxy->dispatch);
1836 IPSFactoryBuffer_Release(factory_buffer);
1838 if ((hres == S_OK) && (nroffuncs < 7))
1840 ERR("nroffuncs calculated incorrectly (%d)\n", nroffuncs);
1841 hres = E_UNEXPECTED;
1843 if (hres == S_OK)
1845 defer_to_dispatch = TRUE;
1848 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
1851 for (i=0;i<nroffuncs;i++) {
1852 switch (i) {
1853 case 0:
1854 proxy->lpvtbl[i] = ProxyIUnknown_QueryInterface;
1855 break;
1856 case 1:
1857 proxy->lpvtbl[i] = ProxyIUnknown_AddRef;
1858 break;
1859 case 2:
1860 proxy->lpvtbl[i] = ProxyIUnknown_Release;
1861 break;
1862 case 3:
1863 if(!defer_to_dispatch) hres = init_proxy_entry_point(proxy, i);
1864 else proxy->lpvtbl[3] = ProxyIDispatch_GetTypeInfoCount;
1865 break;
1866 case 4:
1867 if(!defer_to_dispatch) hres = init_proxy_entry_point(proxy, i);
1868 else proxy->lpvtbl[4] = ProxyIDispatch_GetTypeInfo;
1869 break;
1870 case 5:
1871 if(!defer_to_dispatch) hres = init_proxy_entry_point(proxy, i);
1872 else proxy->lpvtbl[5] = ProxyIDispatch_GetIDsOfNames;
1873 break;
1874 case 6:
1875 if(!defer_to_dispatch) hres = init_proxy_entry_point(proxy, i);
1876 else proxy->lpvtbl[6] = ProxyIDispatch_Invoke;
1877 break;
1878 default:
1879 hres = init_proxy_entry_point(proxy, i);
1883 if (hres == S_OK)
1885 *ppv = proxy;
1886 *ppProxy = &proxy->IRpcProxyBuffer_iface;
1887 IUnknown_AddRef((IUnknown *)*ppv);
1888 return S_OK;
1890 else
1891 TMProxyImpl_Release(&proxy->IRpcProxyBuffer_iface);
1892 return hres;
1895 typedef struct _TMStubImpl {
1896 IRpcStubBuffer IRpcStubBuffer_iface;
1897 LONG ref;
1899 LPUNKNOWN pUnk;
1900 ITypeInfo *tinfo;
1901 IID iid;
1902 IRpcStubBuffer *dispatch_stub;
1903 BOOL dispatch_derivative;
1904 } TMStubImpl;
1906 static inline TMStubImpl *impl_from_IRpcStubBuffer(IRpcStubBuffer *iface)
1908 return CONTAINING_RECORD(iface, TMStubImpl, IRpcStubBuffer_iface);
1911 static HRESULT WINAPI
1912 TMStubImpl_QueryInterface(LPRPCSTUBBUFFER iface, REFIID riid, LPVOID *ppv)
1914 if (IsEqualIID(riid,&IID_IRpcStubBuffer)||IsEqualIID(riid,&IID_IUnknown)){
1915 *ppv = iface;
1916 IRpcStubBuffer_AddRef(iface);
1917 return S_OK;
1919 FIXME("%s, not supported IID.\n",debugstr_guid(riid));
1920 return E_NOINTERFACE;
1923 static ULONG WINAPI
1924 TMStubImpl_AddRef(LPRPCSTUBBUFFER iface)
1926 TMStubImpl *This = impl_from_IRpcStubBuffer(iface);
1927 ULONG refCount = InterlockedIncrement(&This->ref);
1929 TRACE("(%p)->(ref before=%u)\n", This, refCount - 1);
1931 return refCount;
1934 static ULONG WINAPI
1935 TMStubImpl_Release(LPRPCSTUBBUFFER iface)
1937 TMStubImpl *This = impl_from_IRpcStubBuffer(iface);
1938 ULONG refCount = InterlockedDecrement(&This->ref);
1940 TRACE("(%p)->(ref before=%u)\n", This, refCount + 1);
1942 if (!refCount)
1944 IRpcStubBuffer_Disconnect(iface);
1945 ITypeInfo_Release(This->tinfo);
1946 if (This->dispatch_stub)
1947 IRpcStubBuffer_Release(This->dispatch_stub);
1948 CoTaskMemFree(This);
1950 return refCount;
1953 static HRESULT WINAPI
1954 TMStubImpl_Connect(LPRPCSTUBBUFFER iface, LPUNKNOWN pUnkServer)
1956 TMStubImpl *This = impl_from_IRpcStubBuffer(iface);
1958 TRACE("(%p)->(%p)\n", This, pUnkServer);
1960 IUnknown_AddRef(pUnkServer);
1961 This->pUnk = pUnkServer;
1963 if (This->dispatch_stub)
1964 IRpcStubBuffer_Connect(This->dispatch_stub, pUnkServer);
1966 return S_OK;
1969 static void WINAPI
1970 TMStubImpl_Disconnect(LPRPCSTUBBUFFER iface)
1972 TMStubImpl *This = impl_from_IRpcStubBuffer(iface);
1974 TRACE("(%p)->()\n", This);
1976 if (This->pUnk)
1978 IUnknown_Release(This->pUnk);
1979 This->pUnk = NULL;
1982 if (This->dispatch_stub)
1983 IRpcStubBuffer_Disconnect(This->dispatch_stub);
1986 static HRESULT WINAPI
1987 TMStubImpl_Invoke(
1988 LPRPCSTUBBUFFER iface, RPCOLEMESSAGE* xmsg,IRpcChannelBuffer*rpcchanbuf)
1990 #ifdef __i386__
1991 int i;
1992 const FUNCDESC *fdesc;
1993 TMStubImpl *This = impl_from_IRpcStubBuffer(iface);
1994 HRESULT hres;
1995 DWORD *args = NULL, res, *xargs, nrofargs;
1996 marshal_state buf;
1997 UINT nrofnames = 0;
1998 BSTR names[10];
1999 BSTR iname = NULL;
2000 ITypeInfo *tinfo = NULL;
2002 TRACE("...\n");
2004 if (xmsg->iMethod < 3) {
2005 ERR("IUnknown methods cannot be marshaled by the typelib marshaler\n");
2006 return E_UNEXPECTED;
2009 if (This->dispatch_derivative && xmsg->iMethod < sizeof(IDispatchVtbl)/sizeof(void *))
2011 if (!This->dispatch_stub)
2013 IPSFactoryBuffer *factory_buffer;
2014 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
2015 if (hres == S_OK)
2017 hres = IPSFactoryBuffer_CreateStub(factory_buffer, &IID_IDispatch,
2018 This->pUnk, &This->dispatch_stub);
2019 IPSFactoryBuffer_Release(factory_buffer);
2021 if (hres != S_OK)
2022 return hres;
2024 return IRpcStubBuffer_Invoke(This->dispatch_stub, xmsg, rpcchanbuf);
2027 memset(&buf,0,sizeof(buf));
2028 buf.size = xmsg->cbBuffer;
2029 buf.base = HeapAlloc(GetProcessHeap(), 0, xmsg->cbBuffer);
2030 memcpy(buf.base, xmsg->Buffer, xmsg->cbBuffer);
2031 buf.curoff = 0;
2033 hres = get_funcdesc(This->tinfo,xmsg->iMethod,&tinfo,&fdesc,&iname,NULL,NULL);
2034 if (hres) {
2035 ERR("GetFuncDesc on method %d failed with %x\n",xmsg->iMethod,hres);
2036 return hres;
2039 if (iname && !lstrcmpW(iname, IDispatchW))
2041 ERR("IDispatch cannot be marshaled by the typelib marshaler\n");
2042 hres = E_UNEXPECTED;
2043 SysFreeString (iname);
2044 goto exit;
2047 SysFreeString (iname);
2049 /* Need them for hack below */
2050 memset(names,0,sizeof(names));
2051 ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames);
2052 if (nrofnames > sizeof(names)/sizeof(names[0])) {
2053 ERR("Need more names!\n");
2056 /*dump_FUNCDESC(fdesc);*/
2057 nrofargs = 0;
2058 for (i=0;i<fdesc->cParams;i++)
2059 nrofargs += _argsize(&fdesc->lprgelemdescParam[i].tdesc, tinfo);
2060 args = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(nrofargs+1)*sizeof(DWORD));
2061 if (!args)
2063 hres = E_OUTOFMEMORY;
2064 goto exit;
2067 /* Allocate all stuff used by call. */
2068 xargs = args+1;
2069 for (i=0;i<fdesc->cParams;i++) {
2070 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2072 hres = deserialize_param(
2073 tinfo,
2074 is_in_elem(elem),
2075 FALSE,
2076 TRUE,
2077 &(elem->tdesc),
2078 xargs,
2079 &buf
2081 xargs += _argsize(&elem->tdesc, tinfo);
2082 if (hres) {
2083 ERR("Failed to deserialize param %s, hres %x\n",relaystr(names[i+1]),hres);
2084 break;
2088 args[0] = (DWORD)This->pUnk;
2090 __TRY
2092 res = _invoke(
2093 (*((FARPROC**)args[0]))[fdesc->oVft/4],
2094 fdesc->callconv,
2095 (xargs-args),
2096 args
2099 __EXCEPT_ALL
2101 DWORD dwExceptionCode = GetExceptionCode();
2102 ERR("invoke call failed with exception 0x%08x (%d)\n", dwExceptionCode, dwExceptionCode);
2103 if (FAILED(dwExceptionCode))
2104 hres = dwExceptionCode;
2105 else
2106 hres = HRESULT_FROM_WIN32(dwExceptionCode);
2108 __ENDTRY
2110 if (hres != S_OK)
2111 goto exit;
2113 buf.curoff = 0;
2115 xargs = args+1;
2116 for (i=0;i<fdesc->cParams;i++) {
2117 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2118 hres = serialize_param(
2119 tinfo,
2120 is_out_elem(elem),
2121 FALSE,
2122 TRUE,
2123 &elem->tdesc,
2124 xargs,
2125 &buf
2127 xargs += _argsize(&elem->tdesc, tinfo);
2128 if (hres) {
2129 ERR("Failed to stuballoc param, hres %x\n",hres);
2130 break;
2134 hres = xbuf_add (&buf, (LPBYTE)&res, sizeof(DWORD));
2136 if (hres != S_OK)
2137 goto exit;
2139 xmsg->cbBuffer = buf.curoff;
2140 hres = IRpcChannelBuffer_GetBuffer(rpcchanbuf, xmsg, &This->iid);
2141 if (hres != S_OK)
2142 ERR("IRpcChannelBuffer_GetBuffer failed with error 0x%08x\n", hres);
2144 if (hres == S_OK)
2145 memcpy(xmsg->Buffer, buf.base, buf.curoff);
2147 exit:
2148 for (i = 0; i < nrofnames; i++)
2149 SysFreeString(names[i]);
2151 ITypeInfo_Release(tinfo);
2152 HeapFree(GetProcessHeap(), 0, args);
2154 HeapFree(GetProcessHeap(), 0, buf.base);
2156 TRACE("returning\n");
2157 return hres;
2158 #else
2159 FIXME( "not implemented on non-i386\n" );
2160 return E_FAIL;
2161 #endif
2164 static LPRPCSTUBBUFFER WINAPI
2165 TMStubImpl_IsIIDSupported(LPRPCSTUBBUFFER iface, REFIID riid) {
2166 FIXME("Huh (%s)?\n",debugstr_guid(riid));
2167 return NULL;
2170 static ULONG WINAPI
2171 TMStubImpl_CountRefs(LPRPCSTUBBUFFER iface) {
2172 TMStubImpl *This = impl_from_IRpcStubBuffer(iface);
2174 FIXME("()\n");
2175 return This->ref; /*FIXME? */
2178 static HRESULT WINAPI
2179 TMStubImpl_DebugServerQueryInterface(LPRPCSTUBBUFFER iface, LPVOID *ppv) {
2180 return E_NOTIMPL;
2183 static void WINAPI
2184 TMStubImpl_DebugServerRelease(LPRPCSTUBBUFFER iface, LPVOID ppv) {
2185 return;
2188 static const IRpcStubBufferVtbl tmstubvtbl = {
2189 TMStubImpl_QueryInterface,
2190 TMStubImpl_AddRef,
2191 TMStubImpl_Release,
2192 TMStubImpl_Connect,
2193 TMStubImpl_Disconnect,
2194 TMStubImpl_Invoke,
2195 TMStubImpl_IsIIDSupported,
2196 TMStubImpl_CountRefs,
2197 TMStubImpl_DebugServerQueryInterface,
2198 TMStubImpl_DebugServerRelease
2201 static HRESULT WINAPI
2202 PSFacBuf_CreateStub(
2203 LPPSFACTORYBUFFER iface, REFIID riid,IUnknown *pUnkServer,
2204 IRpcStubBuffer** ppStub
2206 HRESULT hres;
2207 ITypeInfo *tinfo;
2208 TMStubImpl *stub;
2209 TYPEATTR *typeattr;
2211 TRACE("(%s,%p,%p)\n",debugstr_guid(riid),pUnkServer,ppStub);
2213 hres = _get_typeinfo_for_iid(riid,&tinfo);
2214 if (hres) {
2215 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
2216 return hres;
2219 stub = CoTaskMemAlloc(sizeof(TMStubImpl));
2220 if (!stub)
2221 return E_OUTOFMEMORY;
2222 stub->IRpcStubBuffer_iface.lpVtbl = &tmstubvtbl;
2223 stub->ref = 1;
2224 stub->tinfo = tinfo;
2225 stub->dispatch_stub = NULL;
2226 stub->dispatch_derivative = FALSE;
2227 stub->iid = *riid;
2228 hres = IRpcStubBuffer_Connect(&stub->IRpcStubBuffer_iface,pUnkServer);
2229 *ppStub = &stub->IRpcStubBuffer_iface;
2230 TRACE("IRpcStubBuffer: %p\n", stub);
2231 if (hres)
2232 ERR("Connect to pUnkServer failed?\n");
2234 /* if we derive from IDispatch then defer to its stub for some of its methods */
2235 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
2236 if (hres == S_OK)
2238 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
2239 stub->dispatch_derivative = TRUE;
2240 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
2243 return hres;
2246 static const IPSFactoryBufferVtbl psfacbufvtbl = {
2247 PSFacBuf_QueryInterface,
2248 PSFacBuf_AddRef,
2249 PSFacBuf_Release,
2250 PSFacBuf_CreateProxy,
2251 PSFacBuf_CreateStub
2254 /* This is the whole PSFactoryBuffer object, just the vtableptr */
2255 static const IPSFactoryBufferVtbl *lppsfac = &psfacbufvtbl;
2257 /***********************************************************************
2258 * TMARSHAL_DllGetClassObject
2260 HRESULT TMARSHAL_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
2262 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)) {
2263 *ppv = &lppsfac;
2264 return S_OK;
2266 return E_NOINTERFACE;