oleaut32: Rewrite get_funcdesc to avoid using the funcdesc's oVft member so that...
[wine/gsoc_dplay.git] / dlls / oleaut32 / tmarshal.c
blob7d0637ef7e217cf39318c62500fa1405313d3961
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"
26 #include <assert.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <ctype.h>
33 #define COBJMACROS
34 #define NONAMELESSUNION
35 #define NONAMELESSSTRUCT
37 #include "winerror.h"
38 #include "windef.h"
39 #include "winbase.h"
40 #include "winnls.h"
41 #include "winreg.h"
42 #include "winuser.h"
43 #include "excpt.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 #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 {
64 LPBYTE base;
65 int size;
66 int curoff;
67 } 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);
72 tmp += 2;
73 tmp[strlen(tmp)-1] = '\0';
74 return tmp;
77 static HRESULT
78 xbuf_resize(marshal_state *buf, DWORD newsize)
80 if(buf->size >= newsize)
81 return S_FALSE;
83 if(buf->base)
85 buf->base = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buf->base, newsize);
86 if(!buf->base)
87 return E_OUTOFMEMORY;
89 else
91 buf->base = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, newsize);
92 if(!buf->base)
93 return E_OUTOFMEMORY;
95 buf->size = newsize;
96 return S_OK;
99 static HRESULT
100 xbuf_add(marshal_state *buf, LPBYTE stuff, DWORD size)
102 HRESULT hr;
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);
110 buf->curoff += size;
111 return S_OK;
114 static HRESULT
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);
118 buf->curoff += size;
119 return S_OK;
122 static HRESULT
123 xbuf_skip(marshal_state *buf, DWORD size) {
124 if (buf->size < buf->curoff+size) return E_FAIL;
125 buf->curoff += size;
126 return S_OK;
129 static HRESULT
130 _unmarshal_interface(marshal_state *buf, REFIID riid, LPUNKNOWN *pUnk) {
131 IStream *pStm;
132 ULARGE_INTEGER newpos;
133 LARGE_INTEGER seekto;
134 ULONG res;
135 HRESULT hres;
136 DWORD xsize;
138 TRACE("...%s...\n",debugstr_guid(riid));
140 *pUnk = NULL;
141 hres = xbuf_get(buf,(LPBYTE)&xsize,sizeof(xsize));
142 if (hres) {
143 ERR("xbuf_get failed\n");
144 return hres;
147 if (xsize == 0) return S_OK;
149 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
150 if (hres) {
151 ERR("Stream create failed %x\n",hres);
152 return hres;
155 hres = IStream_Write(pStm,buf->base+buf->curoff,xsize,&res);
156 if (hres) {
157 ERR("stream write %x\n",hres);
158 return hres;
161 memset(&seekto,0,sizeof(seekto));
162 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
163 if (hres) {
164 ERR("Failed Seek %x\n",hres);
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 return hres;
174 IStream_Release(pStm);
175 return xbuf_skip(buf,xsize);
178 static HRESULT
179 _marshal_interface(marshal_state *buf, REFIID riid, LPUNKNOWN pUnk) {
180 LPBYTE tempbuf = NULL;
181 IStream *pStm = NULL;
182 STATSTG ststg;
183 ULARGE_INTEGER newpos;
184 LARGE_INTEGER seekto;
185 ULONG res;
186 DWORD xsize;
187 HRESULT hres;
189 if (!pUnk) {
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
193 * serializing.
195 WARN("pUnk is NULL\n");
196 xsize = 0;
197 return xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
200 hres = E_FAIL;
202 TRACE("...%s...\n",debugstr_guid(riid));
204 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
205 if (hres) {
206 ERR("Stream create failed %x\n",hres);
207 goto fail;
210 hres = CoMarshalInterface(pStm,riid,pUnk,0,NULL,0);
211 if (hres) {
212 ERR("Marshalling interface %s failed with %x\n", debugstr_guid(riid), hres);
213 goto fail;
216 hres = IStream_Stat(pStm,&ststg,0);
217 if (hres) {
218 ERR("Stream stat failed\n");
219 goto fail;
222 tempbuf = HeapAlloc(GetProcessHeap(), 0, ststg.cbSize.u.LowPart);
223 memset(&seekto,0,sizeof(seekto));
224 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
225 if (hres) {
226 ERR("Failed Seek %x\n",hres);
227 goto fail;
230 hres = IStream_Read(pStm,tempbuf,ststg.cbSize.u.LowPart,&res);
231 if (hres) {
232 ERR("Failed Read %x\n",hres);
233 goto fail;
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);
243 return hres;
245 fail:
246 xsize = 0;
247 xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
248 if (pStm) IUnknown_Release(pStm);
249 HeapFree(GetProcessHeap(), 0, tempbuf);
250 return hres;
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 */
259 return S_OK;
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; }
268 static HRESULT
269 _get_typeinfo_for_iid(REFIID riid, ITypeInfo**ti) {
270 HRESULT hres;
271 HKEY ikey;
272 char tlguid[200],typelibkey[300],interfacekey[300],ver[100];
273 char tlfn[260];
274 OLECHAR tlfnW[260];
275 DWORD tlguidlen, verlen, type;
276 LONG tlfnlen;
277 ITypeLib *tl;
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);
287 return E_FAIL;
289 type = (1<<REG_SZ);
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 type = (1<<REG_SZ);
297 verlen = sizeof(ver);
298 if (RegQueryValueExA(ikey,"Version",NULL,&type,(LPBYTE)ver,&verlen)) {
299 ERR("Could not get version value?\n");
300 RegCloseKey(ikey);
301 return E_FAIL;
303 RegCloseKey(ikey);
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");
308 return E_FAIL;
310 MultiByteToWideChar(CP_ACP, 0, tlfn, -1, tlfnW, sizeof(tlfnW) / sizeof(tlfnW[0]));
311 hres = LoadTypeLib(tlfnW,&tl);
312 if (hres) {
313 ERR("Failed to load typelib for %s, but it should be there.\n",debugstr_guid(riid));
314 return hres;
316 hres = ITypeLib_GetTypeInfoOfGuid(tl,riid,ti);
317 if (hres) {
318 ERR("typelib does not contain info for %s?\n",debugstr_guid(riid));
319 ITypeLib_Release(tl);
320 return hres;
322 ITypeLib_Release(tl);
323 return hres;
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) {
331 int n, i, j;
332 const FUNCDESC *fdesc;
333 HRESULT hres;
334 TYPEATTR *attr;
335 ITypeInfo *tinfo2;
337 n=0;
338 hres = ITypeInfo_GetTypeAttr(tinfo, &attr);
339 if (hres) {
340 ERR("GetTypeAttr failed with %x\n",hres);
341 return hres;
343 /* look in inherited ifaces. */
344 for (j=0;j<attr->cImplTypes;j++) {
345 HREFTYPE href;
346 hres = ITypeInfo_GetRefTypeOfImplType(tinfo, j, &href);
347 if (hres) {
348 ERR("Did not find a reftype for interface offset %d?\n",j);
349 break;
351 hres = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
352 if (hres) {
353 ERR("Did not find a typeinfo for reftype %d?\n",href);
354 continue;
356 n += _nroffuncs(tinfo2);
357 ITypeInfo_Release(tinfo2);
359 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
360 i = 0;
361 while (1) {
362 hres = ITypeInfoImpl_GetInternalFuncDesc(tinfo,i,&fdesc);
363 if (hres)
364 return n;
365 n++;
366 i++;
368 /*NOTREACHED*/
371 #ifdef __i386__
373 #include "pshpack1.h"
375 typedef struct _TMAsmProxy {
376 BYTE popleax;
377 BYTE pushlval;
378 BYTE nr;
379 BYTE pushleax;
380 BYTE lcall;
381 DWORD xcall;
382 BYTE lret;
383 WORD bytestopop;
384 } TMAsmProxy;
386 #include "poppack.h"
388 #else /* __i386__ */
389 # warning You need to implement stubless proxies for your architecture
390 typedef struct _TMAsmProxy {
391 } TMAsmProxy;
392 #endif
394 typedef struct _TMProxyImpl {
395 LPVOID *lpvtbl;
396 const IRpcProxyBufferVtbl *lpvtbl2;
397 LONG ref;
399 TMAsmProxy *asmstubs;
400 ITypeInfo* tinfo;
401 IRpcChannelBuffer* chanbuf;
402 IID iid;
403 CRITICAL_SECTION crit;
404 IUnknown *outerunknown;
405 IDispatch *dispatch;
406 IRpcProxyBuffer *dispatch_proxy;
407 } TMProxyImpl;
409 static HRESULT WINAPI
410 TMProxyImpl_QueryInterface(LPRPCPROXYBUFFER iface, REFIID riid, LPVOID *ppv)
412 TRACE("()\n");
413 if (IsEqualIID(riid,&IID_IUnknown)||IsEqualIID(riid,&IID_IRpcProxyBuffer)) {
414 *ppv = (LPVOID)iface;
415 IRpcProxyBuffer_AddRef(iface);
416 return S_OK;
418 FIXME("no interface for %s\n",debugstr_guid(riid));
419 return E_NOINTERFACE;
422 static ULONG WINAPI
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);
430 return refCount;
433 static ULONG WINAPI
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);
441 if (!refCount)
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);
450 CoTaskMemFree(This);
452 return refCount;
455 static HRESULT WINAPI
456 TMProxyImpl_Connect(
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);
474 if (FAILED(hr))
475 return hr;
476 return IRpcProxyBuffer_Connect(This->dispatch_proxy, pDelegateChannel);
479 return S_OK;
482 static void WINAPI
483 TMProxyImpl_Disconnect(LPRPCPROXYBUFFER iface)
485 ICOM_THIS_MULTI(TMProxyImpl, lpvtbl2, iface);
487 TRACE("()\n");
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,
503 TMProxyImpl_AddRef,
504 TMProxyImpl_Release,
505 TMProxyImpl_Connect,
506 TMProxyImpl_Disconnect
509 /* how much space do we use on stack in DWORD steps. */
511 _argsize(DWORD vt) {
512 switch (vt) {
513 case VT_UI8:
514 return 8/sizeof(DWORD);
515 case VT_R8:
516 return sizeof(double)/sizeof(DWORD);
517 case VT_CY:
518 return sizeof(CY)/sizeof(DWORD);
519 case VT_DATE:
520 return sizeof(DATE)/sizeof(DWORD);
521 case VT_VARIANT:
522 return (sizeof(VARIANT)+3)/sizeof(DWORD);
523 default:
524 return 1;
528 static int
529 _xsize(TYPEDESC *td) {
530 switch (td->vt) {
531 case VT_DATE:
532 return sizeof(DATE);
533 case VT_VARIANT:
534 return sizeof(VARIANT)+3;
535 case VT_CARRAY: {
536 int i, arrsize = 1;
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);
543 case VT_UI8:
544 case VT_I8:
545 return 8;
546 case VT_UI2:
547 case VT_I2:
548 return 2;
549 case VT_UI1:
550 case VT_I1:
551 return 1;
552 default:
553 return 4;
557 static HRESULT
558 serialize_param(
559 ITypeInfo *tinfo,
560 BOOL writeit,
561 BOOL debugout,
562 BOOL dealloc,
563 TYPEDESC *tdesc,
564 DWORD *arg,
565 marshal_state *buf)
567 HRESULT hres = S_OK;
569 TRACE("(tdesc.vt %s)\n",debugstr_vt(tdesc->vt));
571 switch (tdesc->vt) {
572 case VT_EMPTY: /* nothing. empty variant for instance */
573 return S_OK;
574 case VT_I8:
575 case VT_UI8:
576 case VT_CY:
577 hres = S_OK;
578 if (debugout) TRACE_(olerelay)("%x%x\n",arg[0],arg[1]);
579 if (writeit)
580 hres = xbuf_add(buf,(LPBYTE)arg,8);
581 return hres;
582 case VT_BOOL:
583 case VT_ERROR:
584 case VT_INT:
585 case VT_UINT:
586 case VT_I4:
587 case VT_R4:
588 case VT_UI4:
589 hres = S_OK;
590 if (debugout) TRACE_(olerelay)("%x\n",*arg);
591 if (writeit)
592 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
593 return hres;
594 case VT_I2:
595 case VT_UI2:
596 hres = S_OK;
597 if (debugout) TRACE_(olerelay)("%04x\n",*arg & 0xffff);
598 if (writeit)
599 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
600 return hres;
601 case VT_I1:
602 case VT_UI1:
603 hres = S_OK;
604 if (debugout) TRACE_(olerelay)("%02x\n",*arg & 0xff);
605 if (writeit)
606 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
607 return hres;
608 case VT_I4|VT_BYREF:
609 hres = S_OK;
610 if (debugout) TRACE_(olerelay)("&0x%x\n",*arg);
611 if (writeit)
612 hres = xbuf_add(buf,(LPBYTE)(DWORD*)*arg,sizeof(DWORD));
613 /* do not dealloc at this time */
614 return hres;
615 case VT_VARIANT: {
616 TYPEDESC tdesc2;
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));
621 tdesc2.vt = vttype;
622 if (writeit) {
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)(")");
629 return hres;
631 case VT_BSTR|VT_BYREF: {
632 if (debugout) TRACE_(olerelay)("[byref]'%s'", *(BSTR*)*arg ? relaystr(*((BSTR*)*arg)) : "<bstr NULL>");
633 if (writeit) {
634 /* ptr to ptr to magic widestring, basically */
635 BSTR *bstr = (BSTR *) *arg;
636 DWORD len;
637 if (!*bstr) {
638 /* -1 means "null string" which is equivalent to empty string */
639 len = -1;
640 hres = xbuf_add(buf, (LPBYTE)&len,sizeof(DWORD));
641 if (hres) return hres;
642 } else {
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);
653 SysFreeString(*str);
655 return S_OK;
658 case VT_BSTR: {
659 if (debugout) {
660 if (*arg)
661 TRACE_(olerelay)("%s",relaystr((WCHAR*)*arg));
662 else
663 TRACE_(olerelay)("<bstr NULL>");
665 if (writeit) {
666 BSTR bstr = (BSTR)*arg;
667 DWORD len;
668 if (!bstr) {
669 len = -1;
670 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
671 if (hres) return hres;
672 } else {
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;
681 if (dealloc && arg)
682 SysFreeString((BSTR)*arg);
683 return S_OK;
685 case VT_PTR: {
686 DWORD cookie;
687 BOOL derefhere = TRUE;
689 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
690 ITypeInfo *tinfo2;
691 TYPEATTR *tattr;
693 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
694 if (hres) {
695 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
696 return hres;
698 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
699 switch (tattr->typekind) {
700 case TKIND_ENUM: /* confirmed */
701 case TKIND_RECORD: /* FIXME: mostly untested */
702 derefhere=TRUE;
703 break;
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 */
707 derefhere=FALSE;
708 break;
709 default:
710 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
711 derefhere=FALSE;
712 break;
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));
723 if (hres)
724 return hres;
725 if (!*arg) {
726 if (debugout) TRACE_(olerelay)("NULL");
727 return S_OK;
729 hres = serialize_param(tinfo,writeit,debugout,dealloc,tdesc->u.lptdesc,(DWORD*)*arg,buf);
730 if (derefhere && dealloc) HeapFree(GetProcessHeap(),0,(LPVOID)*arg);
731 return hres;
733 case VT_UNKNOWN:
734 if (debugout) TRACE_(olerelay)("unk(0x%x)",*arg);
735 if (writeit)
736 hres = _marshal_interface(buf,&IID_IUnknown,(LPUNKNOWN)*arg);
737 if (dealloc && *(IUnknown **)arg)
738 IUnknown_Release((LPUNKNOWN)*arg);
739 return hres;
740 case VT_DISPATCH:
741 if (debugout) TRACE_(olerelay)("idisp(0x%x)",*arg);
742 if (writeit)
743 hres = _marshal_interface(buf,&IID_IDispatch,(LPUNKNOWN)*arg);
744 if (dealloc && *(IUnknown **)arg)
745 IUnknown_Release((LPUNKNOWN)*arg);
746 return hres;
747 case VT_VOID:
748 if (debugout) TRACE_(olerelay)("<void>");
749 return S_OK;
750 case VT_USERDEFINED: {
751 ITypeInfo *tinfo2;
752 TYPEATTR *tattr;
754 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
755 if (hres) {
756 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
757 return hres;
759 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
760 switch (tattr->typekind) {
761 case TKIND_DISPATCH:
762 case TKIND_INTERFACE:
763 if (writeit)
764 hres=_marshal_interface(buf,&(tattr->guid),(LPUNKNOWN)arg);
765 if (dealloc)
766 IUnknown_Release((LPUNKNOWN)arg);
767 break;
768 case TKIND_RECORD: {
769 int i;
770 if (debugout) TRACE_(olerelay)("{");
771 for (i=0;i<tattr->cVars;i++) {
772 VARDESC *vdesc;
773 ELEMDESC *elem2;
774 TYPEDESC *tdesc2;
776 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
777 if (hres) {
778 ERR("Could not get vardesc of %d\n",i);
779 return hres;
781 elem2 = &vdesc->elemdescVar;
782 tdesc2 = &elem2->tdesc;
783 hres = serialize_param(
784 tinfo2,
785 writeit,
786 debugout,
787 dealloc,
788 tdesc2,
789 (DWORD*)(((LPBYTE)arg)+vdesc->u.oInst),
792 ITypeInfo_ReleaseVarDesc(tinfo2, vdesc);
793 if (hres!=S_OK)
794 return hres;
795 if (debugout && (i<(tattr->cVars-1)))
796 TRACE_(olerelay)(",");
798 if (debugout) TRACE_(olerelay)("}");
799 break;
801 case TKIND_ALIAS:
802 hres = serialize_param(tinfo2,writeit,debugout,dealloc,&tattr->tdescAlias,arg,buf);
803 break;
804 case TKIND_ENUM:
805 hres = S_OK;
806 if (debugout) TRACE_(olerelay)("%x",*arg);
807 if (writeit)
808 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
809 break;
810 default:
811 FIXME("Unhandled typekind %d\n",tattr->typekind);
812 hres = E_FAIL;
813 break;
815 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
816 ITypeInfo_Release(tinfo2);
817 return hres;
819 case VT_CARRAY: {
820 ARRAYDESC *adesc = tdesc->u.lpadesc;
821 int i, arrsize = 1;
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);
832 if (hres)
833 return hres;
834 if (debugout && (i<arrsize-1)) TRACE_(olerelay)(",");
836 if (debugout) TRACE_(olerelay)("]");
837 return S_OK;
839 case VT_SAFEARRAY: {
840 if (writeit)
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);
846 buf->curoff = size;
848 return S_OK;
850 default:
851 ERR("Unhandled marshal type %d.\n",tdesc->vt);
852 return S_OK;
856 static HRESULT
857 deserialize_param(
858 ITypeInfo *tinfo,
859 BOOL readit,
860 BOOL debugout,
861 BOOL alloc,
862 TYPEDESC *tdesc,
863 DWORD *arg,
864 marshal_state *buf)
866 HRESULT hres = S_OK;
868 TRACE("vt %s at %p\n",debugstr_vt(tdesc->vt),arg);
870 while (1) {
871 switch (tdesc->vt) {
872 case VT_EMPTY:
873 if (debugout) TRACE_(olerelay)("<empty>\n");
874 return S_OK;
875 case VT_NULL:
876 if (debugout) TRACE_(olerelay)("<null>\n");
877 return S_OK;
878 case VT_VARIANT: {
879 VARIANT *vt = (VARIANT*)arg;
881 if (readit) {
882 DWORD vttype;
883 TYPEDESC tdesc2;
884 hres = xbuf_get(buf,(LPBYTE)&vttype,sizeof(vttype));
885 if (hres) {
886 FIXME("vt type not read?\n");
887 return hres;
889 memset(&tdesc2,0,sizeof(tdesc2));
890 tdesc2.vt = vttype;
891 V_VT(vt) = vttype;
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)(")");
895 return hres;
896 } else {
897 VariantInit(vt);
898 return S_OK;
901 case VT_I8:
902 case VT_UI8:
903 case VT_CY:
904 if (readit) {
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]);
909 return hres;
910 case VT_ERROR:
911 case VT_BOOL:
912 case VT_I4:
913 case VT_INT:
914 case VT_UINT:
915 case VT_R4:
916 case VT_UI4:
917 if (readit) {
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);
922 return hres;
923 case VT_I2:
924 case VT_UI2:
925 if (readit) {
926 DWORD x;
927 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
928 if (hres) ERR("Failed to read integer 4 byte\n");
929 memcpy(arg,&x,2);
931 if (debugout) TRACE_(olerelay)("%04x",*arg & 0xffff);
932 return hres;
933 case VT_I1:
934 case VT_UI1:
935 if (readit) {
936 DWORD x;
937 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
938 if (hres) ERR("Failed to read integer 4 byte\n");
939 memcpy(arg,&x,1);
941 if (debugout) TRACE_(olerelay)("%02x",*arg & 0xff);
942 return hres;
943 case VT_I4|VT_BYREF:
944 hres = S_OK;
945 if (alloc)
946 *arg = (DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
947 if (readit) {
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);
952 return hres;
953 case VT_BSTR|VT_BYREF: {
954 BSTR **bstr = (BSTR **)arg;
955 WCHAR *str;
956 DWORD len;
958 if (readit) {
959 hres = xbuf_get(buf,(LPBYTE)&len,sizeof(DWORD));
960 if (hres) {
961 ERR("failed to read bstr klen\n");
962 return hres;
964 if (len == -1) {
965 *bstr = CoTaskMemAlloc(sizeof(BSTR *));
966 **bstr = NULL;
967 if (debugout) TRACE_(olerelay)("<bstr NULL>");
968 } else {
969 str = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(len+1)*sizeof(WCHAR));
970 hres = xbuf_get(buf,(LPBYTE)str,len*sizeof(WCHAR));
971 if (hres) {
972 ERR("Failed to read BSTR.\n");
973 return hres;
975 *bstr = CoTaskMemAlloc(sizeof(BSTR *));
976 **bstr = SysAllocStringLen(str,len);
977 if (debugout) TRACE_(olerelay)("%s",relaystr(str));
978 HeapFree(GetProcessHeap(),0,str);
980 } else {
981 *bstr = NULL;
983 return S_OK;
985 case VT_BSTR: {
986 WCHAR *str;
987 DWORD len;
989 if (readit) {
990 hres = xbuf_get(buf,(LPBYTE)&len,sizeof(DWORD));
991 if (hres) {
992 ERR("failed to read bstr klen\n");
993 return hres;
995 if (len == -1) {
996 *arg = 0;
997 if (debugout) TRACE_(olerelay)("<bstr NULL>");
998 } else {
999 str = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(len+1)*sizeof(WCHAR));
1000 hres = xbuf_get(buf,(LPBYTE)str,len*sizeof(WCHAR));
1001 if (hres) {
1002 ERR("Failed to read BSTR.\n");
1003 return hres;
1005 *arg = (DWORD)SysAllocStringLen(str,len);
1006 if (debugout) TRACE_(olerelay)("%s",relaystr(str));
1007 HeapFree(GetProcessHeap(),0,str);
1009 } else {
1010 *arg = 0;
1012 return S_OK;
1014 case VT_PTR: {
1015 DWORD cookie;
1016 BOOL derefhere = TRUE;
1018 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
1019 ITypeInfo *tinfo2;
1020 TYPEATTR *tattr;
1022 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
1023 if (hres) {
1024 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
1025 return hres;
1027 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1028 switch (tattr->typekind) {
1029 case TKIND_ENUM: /* confirmed */
1030 case TKIND_RECORD: /* FIXME: mostly untested */
1031 derefhere=TRUE;
1032 break;
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 */
1036 derefhere=FALSE;
1037 break;
1038 default:
1039 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
1040 derefhere=FALSE;
1041 break;
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));
1050 if (hres) {
1051 ERR("Failed to load pointer cookie.\n");
1052 return hres;
1054 if (cookie != 0x42424242) {
1055 /* we read a NULL ptr from the remote side */
1056 if (debugout) TRACE_(olerelay)("NULL");
1057 *arg = 0;
1058 return S_OK;
1060 if (debugout) TRACE_(olerelay)("*");
1061 if (alloc) {
1062 /* Allocate space for the referenced struct */
1063 if (derefhere)
1064 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,_xsize(tdesc->u.lptdesc));
1066 if (derefhere)
1067 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, (LPDWORD)*arg, buf);
1068 else
1069 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, arg, buf);
1071 case VT_UNKNOWN:
1072 /* FIXME: UNKNOWN is unknown ..., but allocate 4 byte for it */
1073 if (alloc)
1074 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
1075 hres = S_OK;
1076 if (readit)
1077 hres = _unmarshal_interface(buf,&IID_IUnknown,(LPUNKNOWN*)arg);
1078 if (debugout)
1079 TRACE_(olerelay)("unk(%p)",arg);
1080 return hres;
1081 case VT_DISPATCH:
1082 hres = S_OK;
1083 if (readit)
1084 hres = _unmarshal_interface(buf,&IID_IDispatch,(LPUNKNOWN*)arg);
1085 if (debugout)
1086 TRACE_(olerelay)("idisp(%p)",arg);
1087 return hres;
1088 case VT_VOID:
1089 if (debugout) TRACE_(olerelay)("<void>");
1090 return S_OK;
1091 case VT_USERDEFINED: {
1092 ITypeInfo *tinfo2;
1093 TYPEATTR *tattr;
1095 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
1096 if (hres) {
1097 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
1098 return hres;
1100 hres = ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1101 if (hres) {
1102 ERR("Could not get typeattr in VT_USERDEFINED.\n");
1103 } else {
1104 switch (tattr->typekind) {
1105 case TKIND_DISPATCH:
1106 case TKIND_INTERFACE:
1107 if (readit)
1108 hres = _unmarshal_interface(buf,&(tattr->guid),(LPUNKNOWN*)arg);
1109 break;
1110 case TKIND_RECORD: {
1111 int i;
1113 if (alloc)
1114 *arg = (DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,tattr->cbSizeInstance);
1116 if (debugout) TRACE_(olerelay)("{");
1117 for (i=0;i<tattr->cVars;i++) {
1118 VARDESC *vdesc;
1120 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
1121 if (hres) {
1122 ERR("Could not get vardesc of %d\n",i);
1123 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1124 ITypeInfo_Release(tinfo2);
1125 return hres;
1127 hres = deserialize_param(
1128 tinfo2,
1129 readit,
1130 debugout,
1131 alloc,
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)("}");
1140 break;
1142 case TKIND_ALIAS:
1143 hres = deserialize_param(tinfo2,readit,debugout,alloc,&tattr->tdescAlias,arg,buf);
1144 break;
1145 case TKIND_ENUM:
1146 if (readit) {
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);
1151 break;
1152 default:
1153 ERR("Unhandled typekind %d\n",tattr->typekind);
1154 hres = E_FAIL;
1155 break;
1157 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1159 if (hres)
1160 ERR("failed to stuballoc in TKIND_RECORD.\n");
1161 ITypeInfo_Release(tinfo2);
1162 return hres;
1164 case VT_CARRAY: {
1165 /* arg is pointing to the start of the array. */
1166 ARRAYDESC *adesc = tdesc->u.lpadesc;
1167 int arrsize,i;
1168 arrsize = 1;
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++)
1173 deserialize_param(
1174 tinfo,
1175 readit,
1176 debugout,
1177 alloc,
1178 &adesc->tdescElem,
1179 (DWORD*)((LPBYTE)(arg)+i*_xsize(&adesc->tdescElem)),
1182 return S_OK;
1184 case VT_SAFEARRAY: {
1185 if (readit)
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;
1192 return S_OK;
1194 default:
1195 ERR("No handler for VT type %d!\n",tdesc->vt);
1196 return S_OK;
1201 /* Retrieves a function's funcdesc, searching back into inherited interfaces. */
1202 static HRESULT get_funcdesc(ITypeInfo *tinfo, int iMethod, ITypeInfo **tactual, const FUNCDESC **fdesc,
1203 BSTR *iname, BSTR *fname, UINT *num)
1205 HRESULT hr;
1206 UINT i, impl_types;
1207 UINT inherited_funcs = 0;
1208 TYPEATTR *attr;
1210 if (fname) *fname = NULL;
1211 if (iname) *iname = NULL;
1212 if (num) *num = 0;
1213 *tactual = NULL;
1215 hr = ITypeInfo_GetTypeAttr(tinfo, &attr);
1216 if (FAILED(hr))
1218 ERR("GetTypeAttr failed with %x\n",hr);
1219 return hr;
1221 impl_types = attr->cImplTypes;
1222 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1224 for (i = 0; i < impl_types; i++)
1226 HREFTYPE href;
1227 ITypeInfo *pSubTypeInfo;
1228 UINT sub_funcs;
1230 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, i, &href);
1231 if (FAILED(hr)) return hr;
1232 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &pSubTypeInfo);
1233 if (FAILED(hr)) return hr;
1235 hr = get_funcdesc(pSubTypeInfo, iMethod, tactual, fdesc, iname, fname, &sub_funcs);
1236 inherited_funcs += sub_funcs;
1237 ITypeInfo_Release(pSubTypeInfo);
1238 if(SUCCEEDED(hr)) return hr;
1240 if(iMethod < inherited_funcs)
1242 ERR("shouldn't be here\n");
1243 return E_INVALIDARG;
1246 for(i = inherited_funcs; i <= iMethod; i++)
1248 hr = ITypeInfoImpl_GetInternalFuncDesc(tinfo, i - inherited_funcs, fdesc);
1249 if(FAILED(hr))
1251 if(num) *num = i;
1252 return hr;
1256 /* found it. We don't care about num so zero it */
1257 if(num) *num = 0;
1258 *tactual = tinfo;
1259 ITypeInfo_AddRef(*tactual);
1260 if (fname) ITypeInfo_GetDocumentation(tinfo,(*fdesc)->memid,fname,NULL,NULL,NULL);
1261 if (iname) ITypeInfo_GetDocumentation(tinfo,-1,iname,NULL,NULL,NULL);
1262 return S_OK;
1265 static inline BOOL is_in_elem(const ELEMDESC *elem)
1267 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN || !elem->u.paramdesc.wParamFlags);
1270 static inline BOOL is_out_elem(const ELEMDESC *elem)
1272 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT || !elem->u.paramdesc.wParamFlags);
1275 static DWORD
1276 xCall(LPVOID retptr, int method, TMProxyImpl *tpinfo /*, args */)
1278 DWORD *args = ((DWORD*)&tpinfo)+1, *xargs;
1279 const FUNCDESC *fdesc;
1280 HRESULT hres;
1281 int i, relaydeb = TRACE_ON(olerelay);
1282 marshal_state buf;
1283 RPCOLEMESSAGE msg;
1284 ULONG status;
1285 BSTR fname,iname;
1286 BSTR names[10];
1287 UINT nrofnames;
1288 DWORD remoteresult = 0;
1289 ITypeInfo *tinfo;
1290 IRpcChannelBuffer *chanbuf;
1292 EnterCriticalSection(&tpinfo->crit);
1294 hres = get_funcdesc(tpinfo->tinfo,method,&tinfo,&fdesc,&iname,&fname,NULL);
1295 if (hres) {
1296 ERR("Did not find typeinfo/funcdesc entry for method %d!\n",method);
1297 LeaveCriticalSection(&tpinfo->crit);
1298 return E_FAIL;
1301 if (!tpinfo->chanbuf)
1303 WARN("Tried to use disconnected proxy\n");
1304 ITypeInfo_Release(tinfo);
1305 LeaveCriticalSection(&tpinfo->crit);
1306 return RPC_E_DISCONNECTED;
1308 chanbuf = tpinfo->chanbuf;
1309 IRpcChannelBuffer_AddRef(chanbuf);
1311 LeaveCriticalSection(&tpinfo->crit);
1313 if (relaydeb) {
1314 TRACE_(olerelay)("->");
1315 if (iname)
1316 TRACE_(olerelay)("%s:",relaystr(iname));
1317 if (fname)
1318 TRACE_(olerelay)("%s(%d)",relaystr(fname),method);
1319 else
1320 TRACE_(olerelay)("%d",method);
1321 TRACE_(olerelay)("(");
1324 if (iname) SysFreeString(iname);
1325 if (fname) SysFreeString(fname);
1327 memset(&buf,0,sizeof(buf));
1329 /* normal typelib driven serializing */
1331 /* Need them for hack below */
1332 memset(names,0,sizeof(names));
1333 if (ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames))
1334 nrofnames = 0;
1335 if (nrofnames > sizeof(names)/sizeof(names[0]))
1336 ERR("Need more names!\n");
1338 xargs = args;
1339 for (i=0;i<fdesc->cParams;i++) {
1340 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1341 if (relaydeb) {
1342 if (i) TRACE_(olerelay)(",");
1343 if (i+1<nrofnames && names[i+1])
1344 TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1346 /* No need to marshal other data than FIN and any VT_PTR. */
1347 if (!is_in_elem(elem) && (elem->tdesc.vt != VT_PTR)) {
1348 xargs+=_argsize(elem->tdesc.vt);
1349 if (relaydeb) TRACE_(olerelay)("[out]");
1350 continue;
1352 hres = serialize_param(
1353 tinfo,
1354 is_in_elem(elem),
1355 relaydeb,
1356 FALSE,
1357 &elem->tdesc,
1358 xargs,
1359 &buf
1362 if (hres) {
1363 ERR("Failed to serialize param, hres %x\n",hres);
1364 break;
1366 xargs+=_argsize(elem->tdesc.vt);
1368 if (relaydeb) TRACE_(olerelay)(")");
1370 memset(&msg,0,sizeof(msg));
1371 msg.cbBuffer = buf.curoff;
1372 msg.iMethod = method;
1373 hres = IRpcChannelBuffer_GetBuffer(chanbuf,&msg,&(tpinfo->iid));
1374 if (hres) {
1375 ERR("RpcChannelBuffer GetBuffer failed, %x\n",hres);
1376 goto exit;
1378 memcpy(msg.Buffer,buf.base,buf.curoff);
1379 if (relaydeb) TRACE_(olerelay)("\n");
1380 hres = IRpcChannelBuffer_SendReceive(chanbuf,&msg,&status);
1381 if (hres) {
1382 ERR("RpcChannelBuffer SendReceive failed, %x\n",hres);
1383 goto exit;
1386 if (relaydeb) TRACE_(olerelay)(" status = %08x (",status);
1387 if (buf.base)
1388 buf.base = HeapReAlloc(GetProcessHeap(),0,buf.base,msg.cbBuffer);
1389 else
1390 buf.base = HeapAlloc(GetProcessHeap(),0,msg.cbBuffer);
1391 buf.size = msg.cbBuffer;
1392 memcpy(buf.base,msg.Buffer,buf.size);
1393 buf.curoff = 0;
1395 /* generic deserializer using typelib description */
1396 xargs = args;
1397 status = S_OK;
1398 for (i=0;i<fdesc->cParams;i++) {
1399 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1401 if (relaydeb) {
1402 if (i) TRACE_(olerelay)(",");
1403 if (i+1<nrofnames && names[i+1]) TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1405 /* No need to marshal other data than FOUT and any VT_PTR */
1406 if (!is_out_elem(elem) && (elem->tdesc.vt != VT_PTR)) {
1407 xargs += _argsize(elem->tdesc.vt);
1408 if (relaydeb) TRACE_(olerelay)("[in]");
1409 continue;
1411 hres = deserialize_param(
1412 tinfo,
1413 is_out_elem(elem),
1414 relaydeb,
1415 FALSE,
1416 &(elem->tdesc),
1417 xargs,
1418 &buf
1420 if (hres) {
1421 ERR("Failed to unmarshall param, hres %x\n",hres);
1422 status = hres;
1423 break;
1425 xargs += _argsize(elem->tdesc.vt);
1428 hres = xbuf_get(&buf, (LPBYTE)&remoteresult, sizeof(DWORD));
1429 if (hres != S_OK)
1430 goto exit;
1431 if (relaydeb) TRACE_(olerelay)(") = %08x\n", remoteresult);
1433 hres = remoteresult;
1435 exit:
1436 for (i = 0; i < nrofnames; i++)
1437 SysFreeString(names[i]);
1438 HeapFree(GetProcessHeap(),0,buf.base);
1439 IRpcChannelBuffer_Release(chanbuf);
1440 ITypeInfo_Release(tinfo);
1441 TRACE("-- 0x%08x\n", hres);
1442 return hres;
1445 static HRESULT WINAPI ProxyIUnknown_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
1447 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1449 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1451 if (proxy->outerunknown)
1452 return IUnknown_QueryInterface(proxy->outerunknown, riid, ppv);
1454 FIXME("No interface\n");
1455 return E_NOINTERFACE;
1458 static ULONG WINAPI ProxyIUnknown_AddRef(IUnknown *iface)
1460 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1462 TRACE("\n");
1464 if (proxy->outerunknown)
1465 return IUnknown_AddRef(proxy->outerunknown);
1467 return 2; /* FIXME */
1470 static ULONG WINAPI ProxyIUnknown_Release(IUnknown *iface)
1472 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1474 TRACE("\n");
1476 if (proxy->outerunknown)
1477 return IUnknown_Release(proxy->outerunknown);
1479 return 1; /* FIXME */
1482 static HRESULT WINAPI ProxyIDispatch_GetTypeInfoCount(LPDISPATCH iface, UINT * pctinfo)
1484 TMProxyImpl *This = (TMProxyImpl *)iface;
1486 TRACE("(%p)\n", pctinfo);
1488 return IDispatch_GetTypeInfoCount(This->dispatch, pctinfo);
1491 static HRESULT WINAPI ProxyIDispatch_GetTypeInfo(LPDISPATCH iface, UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo)
1493 TMProxyImpl *This = (TMProxyImpl *)iface;
1495 TRACE("(%d, %x, %p)\n", iTInfo, lcid, ppTInfo);
1497 return IDispatch_GetTypeInfo(This->dispatch, iTInfo, lcid, ppTInfo);
1500 static HRESULT WINAPI ProxyIDispatch_GetIDsOfNames(LPDISPATCH iface, REFIID riid, LPOLESTR * rgszNames, UINT cNames, LCID lcid, DISPID * rgDispId)
1502 TMProxyImpl *This = (TMProxyImpl *)iface;
1504 TRACE("(%s, %p, %d, 0x%x, %p)\n", debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);
1506 return IDispatch_GetIDsOfNames(This->dispatch, riid, rgszNames,
1507 cNames, lcid, rgDispId);
1510 static HRESULT WINAPI ProxyIDispatch_Invoke(LPDISPATCH iface, DISPID dispIdMember, REFIID riid, LCID lcid,
1511 WORD wFlags, DISPPARAMS * pDispParams, VARIANT * pVarResult,
1512 EXCEPINFO * pExcepInfo, UINT * puArgErr)
1514 TMProxyImpl *This = (TMProxyImpl *)iface;
1516 TRACE("(%d, %s, 0x%x, 0x%x, %p, %p, %p, %p)\n", dispIdMember,
1517 debugstr_guid(riid), lcid, wFlags, pDispParams, pVarResult,
1518 pExcepInfo, puArgErr);
1520 return IDispatch_Invoke(This->dispatch, dispIdMember, riid, lcid,
1521 wFlags, pDispParams, pVarResult, pExcepInfo,
1522 puArgErr);
1525 typedef struct
1527 const IRpcChannelBufferVtbl *lpVtbl;
1528 LONG refs;
1529 /* the IDispatch-derived interface we are handling */
1530 IID tmarshal_iid;
1531 IRpcChannelBuffer *pDelegateChannel;
1532 } TMarshalDispatchChannel;
1534 static HRESULT WINAPI TMarshalDispatchChannel_QueryInterface(LPRPCCHANNELBUFFER iface, REFIID riid, LPVOID *ppv)
1536 *ppv = NULL;
1537 if (IsEqualIID(riid,&IID_IRpcChannelBuffer) || IsEqualIID(riid,&IID_IUnknown))
1539 *ppv = (LPVOID)iface;
1540 IUnknown_AddRef(iface);
1541 return S_OK;
1543 return E_NOINTERFACE;
1546 static ULONG WINAPI TMarshalDispatchChannel_AddRef(LPRPCCHANNELBUFFER iface)
1548 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1549 return InterlockedIncrement(&This->refs);
1552 static ULONG WINAPI TMarshalDispatchChannel_Release(LPRPCCHANNELBUFFER iface)
1554 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1555 ULONG ref;
1557 ref = InterlockedDecrement(&This->refs);
1558 if (ref)
1559 return ref;
1561 IRpcChannelBuffer_Release(This->pDelegateChannel);
1562 HeapFree(GetProcessHeap(), 0, This);
1563 return 0;
1566 static HRESULT WINAPI TMarshalDispatchChannel_GetBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg, REFIID riid)
1568 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1569 TRACE("(%p, %s)\n", olemsg, debugstr_guid(riid));
1570 /* Note: we are pretending to invoke a method on the interface identified
1571 * by tmarshal_iid so that we can re-use the IDispatch proxy/stub code
1572 * without the RPC runtime getting confused by not exporting an IDispatch interface */
1573 return IRpcChannelBuffer_GetBuffer(This->pDelegateChannel, olemsg, &This->tmarshal_iid);
1576 static HRESULT WINAPI TMarshalDispatchChannel_SendReceive(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE *olemsg, ULONG *pstatus)
1578 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1579 TRACE("(%p, %p)\n", olemsg, pstatus);
1580 return IRpcChannelBuffer_SendReceive(This->pDelegateChannel, olemsg, pstatus);
1583 static HRESULT WINAPI TMarshalDispatchChannel_FreeBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg)
1585 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1586 TRACE("(%p)\n", olemsg);
1587 return IRpcChannelBuffer_FreeBuffer(This->pDelegateChannel, olemsg);
1590 static HRESULT WINAPI TMarshalDispatchChannel_GetDestCtx(LPRPCCHANNELBUFFER iface, DWORD* pdwDestContext, void** ppvDestContext)
1592 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1593 TRACE("(%p,%p)\n", pdwDestContext, ppvDestContext);
1594 return IRpcChannelBuffer_GetDestCtx(This->pDelegateChannel, pdwDestContext, ppvDestContext);
1597 static HRESULT WINAPI TMarshalDispatchChannel_IsConnected(LPRPCCHANNELBUFFER iface)
1599 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1600 TRACE("()\n");
1601 return IRpcChannelBuffer_IsConnected(This->pDelegateChannel);
1604 static const IRpcChannelBufferVtbl TMarshalDispatchChannelVtbl =
1606 TMarshalDispatchChannel_QueryInterface,
1607 TMarshalDispatchChannel_AddRef,
1608 TMarshalDispatchChannel_Release,
1609 TMarshalDispatchChannel_GetBuffer,
1610 TMarshalDispatchChannel_SendReceive,
1611 TMarshalDispatchChannel_FreeBuffer,
1612 TMarshalDispatchChannel_GetDestCtx,
1613 TMarshalDispatchChannel_IsConnected
1616 static HRESULT TMarshalDispatchChannel_Create(
1617 IRpcChannelBuffer *pDelegateChannel, REFIID tmarshal_riid,
1618 IRpcChannelBuffer **ppChannel)
1620 TMarshalDispatchChannel *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1621 if (!This)
1622 return E_OUTOFMEMORY;
1624 This->lpVtbl = &TMarshalDispatchChannelVtbl;
1625 This->refs = 1;
1626 IRpcChannelBuffer_AddRef(pDelegateChannel);
1627 This->pDelegateChannel = pDelegateChannel;
1628 This->tmarshal_iid = *tmarshal_riid;
1630 *ppChannel = (IRpcChannelBuffer *)&This->lpVtbl;
1631 return S_OK;
1635 static inline HRESULT get_facbuf_for_iid(REFIID riid, IPSFactoryBuffer **facbuf)
1637 HRESULT hr;
1638 CLSID clsid;
1640 if ((hr = CoGetPSClsid(riid, &clsid)))
1641 return hr;
1642 return CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL,
1643 &IID_IPSFactoryBuffer, (LPVOID*)facbuf);
1646 static HRESULT WINAPI
1647 PSFacBuf_CreateProxy(
1648 LPPSFACTORYBUFFER iface, IUnknown* pUnkOuter, REFIID riid,
1649 IRpcProxyBuffer **ppProxy, LPVOID *ppv)
1651 HRESULT hres;
1652 ITypeInfo *tinfo;
1653 int i, nroffuncs;
1654 const FUNCDESC *fdesc;
1655 TMProxyImpl *proxy;
1656 TYPEATTR *typeattr;
1658 TRACE("(...%s...)\n",debugstr_guid(riid));
1659 hres = _get_typeinfo_for_iid(riid,&tinfo);
1660 if (hres) {
1661 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
1662 return hres;
1664 nroffuncs = _nroffuncs(tinfo);
1665 proxy = CoTaskMemAlloc(sizeof(TMProxyImpl));
1666 if (!proxy) return E_OUTOFMEMORY;
1668 assert(sizeof(TMAsmProxy) == 12);
1670 proxy->dispatch = NULL;
1671 proxy->dispatch_proxy = NULL;
1672 proxy->outerunknown = pUnkOuter;
1673 proxy->asmstubs = VirtualAlloc(NULL, sizeof(TMAsmProxy) * nroffuncs, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
1674 if (!proxy->asmstubs) {
1675 ERR("Could not commit pages for proxy thunks\n");
1676 CoTaskMemFree(proxy);
1677 return E_OUTOFMEMORY;
1679 proxy->lpvtbl2 = &tmproxyvtable;
1680 /* one reference for the proxy */
1681 proxy->ref = 1;
1682 proxy->tinfo = tinfo;
1683 memcpy(&proxy->iid,riid,sizeof(*riid));
1684 proxy->chanbuf = 0;
1686 InitializeCriticalSection(&proxy->crit);
1687 proxy->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TMProxyImpl.crit");
1689 proxy->lpvtbl = HeapAlloc(GetProcessHeap(),0,sizeof(LPBYTE)*nroffuncs);
1690 for (i=0;i<nroffuncs;i++) {
1691 TMAsmProxy *xasm = proxy->asmstubs+i;
1693 switch (i) {
1694 case 0:
1695 proxy->lpvtbl[i] = ProxyIUnknown_QueryInterface;
1696 break;
1697 case 1:
1698 proxy->lpvtbl[i] = ProxyIUnknown_AddRef;
1699 break;
1700 case 2:
1701 proxy->lpvtbl[i] = ProxyIUnknown_Release;
1702 break;
1703 default: {
1704 int j;
1705 /* nrofargs without This */
1706 int nrofargs;
1707 ITypeInfo *tinfo2;
1708 hres = get_funcdesc(tinfo,i,&tinfo2,&fdesc,NULL,NULL,NULL);
1709 if (hres) {
1710 ERR("GetFuncDesc %x should not fail here.\n",hres);
1711 return hres;
1713 ITypeInfo_Release(tinfo2);
1714 /* some args take more than 4 byte on the stack */
1715 nrofargs = 0;
1716 for (j=0;j<fdesc->cParams;j++)
1717 nrofargs += _argsize(fdesc->lprgelemdescParam[j].tdesc.vt);
1719 #ifdef __i386__
1720 if (fdesc->callconv != CC_STDCALL) {
1721 ERR("calling convention is not stdcall????\n");
1722 return E_FAIL;
1724 /* popl %eax - return ptr
1725 * pushl <nr>
1726 * pushl %eax
1727 * call xCall
1728 * lret <nr> (+4)
1731 * arg3 arg2 arg1 <method> <returnptr>
1733 xasm->popleax = 0x58;
1734 xasm->pushlval = 0x6a;
1735 xasm->nr = i;
1736 xasm->pushleax = 0x50;
1737 xasm->lcall = 0xe8; /* relative jump */
1738 xasm->xcall = (DWORD)xCall;
1739 xasm->xcall -= (DWORD)&(xasm->lret);
1740 xasm->lret = 0xc2;
1741 xasm->bytestopop= (nrofargs+2)*4; /* pop args, This, iMethod */
1742 proxy->lpvtbl[i] = xasm;
1743 break;
1744 #else
1745 FIXME("not implemented on non i386\n");
1746 return E_FAIL;
1747 #endif
1752 /* if we derive from IDispatch then defer to its proxy for its methods */
1753 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
1754 if (hres == S_OK)
1756 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
1758 IPSFactoryBuffer *factory_buffer;
1759 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
1760 if (hres == S_OK)
1762 hres = IPSFactoryBuffer_CreateProxy(factory_buffer, NULL,
1763 &IID_IDispatch, &proxy->dispatch_proxy,
1764 (void **)&proxy->dispatch);
1765 IPSFactoryBuffer_Release(factory_buffer);
1767 if ((hres == S_OK) && (nroffuncs < 7))
1769 ERR("nroffuncs calculated incorrectly (%d)\n", nroffuncs);
1770 hres = E_UNEXPECTED;
1772 if (hres == S_OK)
1774 proxy->lpvtbl[3] = ProxyIDispatch_GetTypeInfoCount;
1775 proxy->lpvtbl[4] = ProxyIDispatch_GetTypeInfo;
1776 proxy->lpvtbl[5] = ProxyIDispatch_GetIDsOfNames;
1777 proxy->lpvtbl[6] = ProxyIDispatch_Invoke;
1780 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
1783 if (hres == S_OK)
1785 *ppv = (LPVOID)proxy;
1786 *ppProxy = (IRpcProxyBuffer *)&(proxy->lpvtbl2);
1787 IUnknown_AddRef((IUnknown *)*ppv);
1788 return S_OK;
1790 else
1791 TMProxyImpl_Release((IRpcProxyBuffer *)&proxy->lpvtbl2);
1792 return hres;
1795 typedef struct _TMStubImpl {
1796 const IRpcStubBufferVtbl *lpvtbl;
1797 LONG ref;
1799 LPUNKNOWN pUnk;
1800 ITypeInfo *tinfo;
1801 IID iid;
1802 IRpcStubBuffer *dispatch_stub;
1803 BOOL dispatch_derivative;
1804 } TMStubImpl;
1806 static HRESULT WINAPI
1807 TMStubImpl_QueryInterface(LPRPCSTUBBUFFER iface, REFIID riid, LPVOID *ppv)
1809 if (IsEqualIID(riid,&IID_IRpcStubBuffer)||IsEqualIID(riid,&IID_IUnknown)){
1810 *ppv = (LPVOID)iface;
1811 IRpcStubBuffer_AddRef(iface);
1812 return S_OK;
1814 FIXME("%s, not supported IID.\n",debugstr_guid(riid));
1815 return E_NOINTERFACE;
1818 static ULONG WINAPI
1819 TMStubImpl_AddRef(LPRPCSTUBBUFFER iface)
1821 TMStubImpl *This = (TMStubImpl *)iface;
1822 ULONG refCount = InterlockedIncrement(&This->ref);
1824 TRACE("(%p)->(ref before=%u)\n", This, refCount - 1);
1826 return refCount;
1829 static ULONG WINAPI
1830 TMStubImpl_Release(LPRPCSTUBBUFFER iface)
1832 TMStubImpl *This = (TMStubImpl *)iface;
1833 ULONG refCount = InterlockedDecrement(&This->ref);
1835 TRACE("(%p)->(ref before=%u)\n", This, refCount + 1);
1837 if (!refCount)
1839 IRpcStubBuffer_Disconnect(iface);
1840 ITypeInfo_Release(This->tinfo);
1841 if (This->dispatch_stub)
1842 IRpcStubBuffer_Release(This->dispatch_stub);
1843 CoTaskMemFree(This);
1845 return refCount;
1848 static HRESULT WINAPI
1849 TMStubImpl_Connect(LPRPCSTUBBUFFER iface, LPUNKNOWN pUnkServer)
1851 TMStubImpl *This = (TMStubImpl *)iface;
1853 TRACE("(%p)->(%p)\n", This, pUnkServer);
1855 IUnknown_AddRef(pUnkServer);
1856 This->pUnk = pUnkServer;
1858 if (This->dispatch_stub)
1859 IRpcStubBuffer_Connect(This->dispatch_stub, pUnkServer);
1861 return S_OK;
1864 static void WINAPI
1865 TMStubImpl_Disconnect(LPRPCSTUBBUFFER iface)
1867 TMStubImpl *This = (TMStubImpl *)iface;
1869 TRACE("(%p)->()\n", This);
1871 if (This->pUnk)
1873 IUnknown_Release(This->pUnk);
1874 This->pUnk = NULL;
1877 if (This->dispatch_stub)
1878 IRpcStubBuffer_Disconnect(This->dispatch_stub);
1881 static HRESULT WINAPI
1882 TMStubImpl_Invoke(
1883 LPRPCSTUBBUFFER iface, RPCOLEMESSAGE* xmsg,IRpcChannelBuffer*rpcchanbuf)
1885 int i;
1886 const FUNCDESC *fdesc;
1887 TMStubImpl *This = (TMStubImpl *)iface;
1888 HRESULT hres;
1889 DWORD *args = NULL, res, *xargs, nrofargs;
1890 marshal_state buf;
1891 UINT nrofnames = 0;
1892 BSTR names[10];
1893 BSTR iname = NULL;
1894 ITypeInfo *tinfo = NULL;
1896 TRACE("...\n");
1898 if (xmsg->iMethod < 3) {
1899 ERR("IUnknown methods cannot be marshaled by the typelib marshaler\n");
1900 return E_UNEXPECTED;
1903 if (This->dispatch_derivative && xmsg->iMethod < sizeof(IDispatchVtbl)/sizeof(void *))
1905 IPSFactoryBuffer *factory_buffer;
1906 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
1907 if (hres == S_OK)
1909 hres = IPSFactoryBuffer_CreateStub(factory_buffer, &IID_IDispatch,
1910 This->pUnk, &This->dispatch_stub);
1911 IPSFactoryBuffer_Release(factory_buffer);
1913 if (hres != S_OK)
1914 return hres;
1915 return IRpcStubBuffer_Invoke(This->dispatch_stub, xmsg, rpcchanbuf);
1918 memset(&buf,0,sizeof(buf));
1919 buf.size = xmsg->cbBuffer;
1920 buf.base = HeapAlloc(GetProcessHeap(), 0, xmsg->cbBuffer);
1921 memcpy(buf.base, xmsg->Buffer, xmsg->cbBuffer);
1922 buf.curoff = 0;
1924 hres = get_funcdesc(This->tinfo,xmsg->iMethod,&tinfo,&fdesc,&iname,NULL,NULL);
1925 if (hres) {
1926 ERR("GetFuncDesc on method %d failed with %x\n",xmsg->iMethod,hres);
1927 return hres;
1930 if (iname && !lstrcmpW(iname, IDispatchW))
1932 ERR("IDispatch cannot be marshaled by the typelib marshaler\n");
1933 hres = E_UNEXPECTED;
1934 SysFreeString (iname);
1935 goto exit;
1938 if (iname) SysFreeString (iname);
1940 /* Need them for hack below */
1941 memset(names,0,sizeof(names));
1942 ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames);
1943 if (nrofnames > sizeof(names)/sizeof(names[0])) {
1944 ERR("Need more names!\n");
1947 /*dump_FUNCDESC(fdesc);*/
1948 nrofargs = 0;
1949 for (i=0;i<fdesc->cParams;i++)
1950 nrofargs += _argsize(fdesc->lprgelemdescParam[i].tdesc.vt);
1951 args = HeapAlloc(GetProcessHeap(),0,(nrofargs+1)*sizeof(DWORD));
1952 if (!args)
1954 hres = E_OUTOFMEMORY;
1955 goto exit;
1958 /* Allocate all stuff used by call. */
1959 xargs = args+1;
1960 for (i=0;i<fdesc->cParams;i++) {
1961 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1963 hres = deserialize_param(
1964 tinfo,
1965 is_in_elem(elem),
1966 FALSE,
1967 TRUE,
1968 &(elem->tdesc),
1969 xargs,
1970 &buf
1972 xargs += _argsize(elem->tdesc.vt);
1973 if (hres) {
1974 ERR("Failed to deserialize param %s, hres %x\n",relaystr(names[i+1]),hres);
1975 break;
1979 args[0] = (DWORD)This->pUnk;
1981 __TRY
1983 res = _invoke(
1984 (*((FARPROC**)args[0]))[fdesc->oVft/4],
1985 fdesc->callconv,
1986 (xargs-args),
1987 args
1990 __EXCEPT(NULL)
1992 DWORD dwExceptionCode = GetExceptionCode();
1993 ERR("invoke call failed with exception 0x%08x (%d)\n", dwExceptionCode, dwExceptionCode);
1994 if (FAILED(dwExceptionCode))
1995 hres = dwExceptionCode;
1996 else
1997 hres = HRESULT_FROM_WIN32(dwExceptionCode);
1999 __ENDTRY
2001 if (hres != S_OK)
2002 goto exit;
2004 buf.curoff = 0;
2006 xargs = args+1;
2007 for (i=0;i<fdesc->cParams;i++) {
2008 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2009 hres = serialize_param(
2010 tinfo,
2011 is_out_elem(elem),
2012 FALSE,
2013 TRUE,
2014 &elem->tdesc,
2015 xargs,
2016 &buf
2018 xargs += _argsize(elem->tdesc.vt);
2019 if (hres) {
2020 ERR("Failed to stuballoc param, hres %x\n",hres);
2021 break;
2025 hres = xbuf_add (&buf, (LPBYTE)&res, sizeof(DWORD));
2027 if (hres != S_OK)
2028 goto exit;
2030 xmsg->cbBuffer = buf.curoff;
2031 hres = IRpcChannelBuffer_GetBuffer(rpcchanbuf, xmsg, &This->iid);
2032 if (hres != S_OK)
2033 ERR("IRpcChannelBuffer_GetBuffer failed with error 0x%08x\n", hres);
2035 if (hres == S_OK)
2036 memcpy(xmsg->Buffer, buf.base, buf.curoff);
2038 exit:
2039 for (i = 0; i < nrofnames; i++)
2040 SysFreeString(names[i]);
2042 ITypeInfo_Release(tinfo);
2043 HeapFree(GetProcessHeap(), 0, args);
2045 HeapFree(GetProcessHeap(), 0, buf.base);
2047 TRACE("returning\n");
2048 return hres;
2051 static LPRPCSTUBBUFFER WINAPI
2052 TMStubImpl_IsIIDSupported(LPRPCSTUBBUFFER iface, REFIID riid) {
2053 FIXME("Huh (%s)?\n",debugstr_guid(riid));
2054 return NULL;
2057 static ULONG WINAPI
2058 TMStubImpl_CountRefs(LPRPCSTUBBUFFER iface) {
2059 TMStubImpl *This = (TMStubImpl *)iface;
2061 FIXME("()\n");
2062 return This->ref; /*FIXME? */
2065 static HRESULT WINAPI
2066 TMStubImpl_DebugServerQueryInterface(LPRPCSTUBBUFFER iface, LPVOID *ppv) {
2067 return E_NOTIMPL;
2070 static void WINAPI
2071 TMStubImpl_DebugServerRelease(LPRPCSTUBBUFFER iface, LPVOID ppv) {
2072 return;
2075 static const IRpcStubBufferVtbl tmstubvtbl = {
2076 TMStubImpl_QueryInterface,
2077 TMStubImpl_AddRef,
2078 TMStubImpl_Release,
2079 TMStubImpl_Connect,
2080 TMStubImpl_Disconnect,
2081 TMStubImpl_Invoke,
2082 TMStubImpl_IsIIDSupported,
2083 TMStubImpl_CountRefs,
2084 TMStubImpl_DebugServerQueryInterface,
2085 TMStubImpl_DebugServerRelease
2088 static HRESULT WINAPI
2089 PSFacBuf_CreateStub(
2090 LPPSFACTORYBUFFER iface, REFIID riid,IUnknown *pUnkServer,
2091 IRpcStubBuffer** ppStub
2093 HRESULT hres;
2094 ITypeInfo *tinfo;
2095 TMStubImpl *stub;
2096 TYPEATTR *typeattr;
2098 TRACE("(%s,%p,%p)\n",debugstr_guid(riid),pUnkServer,ppStub);
2100 hres = _get_typeinfo_for_iid(riid,&tinfo);
2101 if (hres) {
2102 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
2103 return hres;
2106 stub = CoTaskMemAlloc(sizeof(TMStubImpl));
2107 if (!stub)
2108 return E_OUTOFMEMORY;
2109 stub->lpvtbl = &tmstubvtbl;
2110 stub->ref = 1;
2111 stub->tinfo = tinfo;
2112 stub->dispatch_stub = NULL;
2113 stub->dispatch_derivative = FALSE;
2114 memcpy(&(stub->iid),riid,sizeof(*riid));
2115 hres = IRpcStubBuffer_Connect((LPRPCSTUBBUFFER)stub,pUnkServer);
2116 *ppStub = (LPRPCSTUBBUFFER)stub;
2117 TRACE("IRpcStubBuffer: %p\n", stub);
2118 if (hres)
2119 ERR("Connect to pUnkServer failed?\n");
2121 /* if we derive from IDispatch then defer to its stub for some of its methods */
2122 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
2123 if (hres == S_OK)
2125 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
2126 stub->dispatch_derivative = TRUE;
2127 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
2130 return hres;
2133 static const IPSFactoryBufferVtbl psfacbufvtbl = {
2134 PSFacBuf_QueryInterface,
2135 PSFacBuf_AddRef,
2136 PSFacBuf_Release,
2137 PSFacBuf_CreateProxy,
2138 PSFacBuf_CreateStub
2141 /* This is the whole PSFactoryBuffer object, just the vtableptr */
2142 static const IPSFactoryBufferVtbl *lppsfac = &psfacbufvtbl;
2144 /***********************************************************************
2145 * TMARSHAL_DllGetClassObject
2147 HRESULT TMARSHAL_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
2149 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)) {
2150 *ppv = &lppsfac;
2151 return S_OK;
2153 return E_NOINTERFACE;