push(c) 7dcddf6204ed5518706a76fe66fd836d81e70dd4
[wine/hacks.git] / dlls / oleaut32 / tmarshal.c
blob4a5ad89649c073031540e20211dfcab0a3dd060c
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"
44 #include "ole2.h"
45 #include "propidl.h" /* for LPSAFEARRAY_User* functions */
46 #include "typelib.h"
47 #include "variant.h"
48 #include "wine/debug.h"
49 #include "wine/exception.h"
51 static const WCHAR IDispatchW[] = { 'I','D','i','s','p','a','t','c','h',0};
53 WINE_DEFAULT_DEBUG_CHANNEL(ole);
54 WINE_DECLARE_DEBUG_CHANNEL(olerelay);
56 #define ICOM_THIS_MULTI(impl,field,iface) impl* const This=(impl*)((char*)(iface) - offsetof(impl,field))
58 static HRESULT TMarshalDispatchChannel_Create(
59 IRpcChannelBuffer *pDelegateChannel, REFIID tmarshal_riid,
60 IRpcChannelBuffer **ppChannel);
62 typedef struct _marshal_state {
63 LPBYTE base;
64 int size;
65 int curoff;
66 } marshal_state;
68 /* used in the olerelay code to avoid having the L"" stuff added by debugstr_w */
69 static char *relaystr(WCHAR *in) {
70 char *tmp = (char *)debugstr_w(in);
71 tmp += 2;
72 tmp[strlen(tmp)-1] = '\0';
73 return tmp;
76 static HRESULT
77 xbuf_resize(marshal_state *buf, DWORD newsize)
79 if(buf->size >= newsize)
80 return S_FALSE;
82 if(buf->base)
84 buf->base = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buf->base, newsize);
85 if(!buf->base)
86 return E_OUTOFMEMORY;
88 else
90 buf->base = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, newsize);
91 if(!buf->base)
92 return E_OUTOFMEMORY;
94 buf->size = newsize;
95 return S_OK;
98 static HRESULT
99 xbuf_add(marshal_state *buf, const BYTE *stuff, DWORD size)
101 HRESULT hr;
103 if(buf->size - buf->curoff < size)
105 hr = xbuf_resize(buf, buf->size + size + 100);
106 if(FAILED(hr)) return hr;
108 memcpy(buf->base+buf->curoff,stuff,size);
109 buf->curoff += size;
110 return S_OK;
113 static HRESULT
114 xbuf_get(marshal_state *buf, LPBYTE stuff, DWORD size) {
115 if (buf->size < buf->curoff+size) return E_FAIL;
116 memcpy(stuff,buf->base+buf->curoff,size);
117 buf->curoff += size;
118 return S_OK;
121 static HRESULT
122 xbuf_skip(marshal_state *buf, DWORD size) {
123 if (buf->size < buf->curoff+size) return E_FAIL;
124 buf->curoff += size;
125 return S_OK;
128 static HRESULT
129 _unmarshal_interface(marshal_state *buf, REFIID riid, LPUNKNOWN *pUnk) {
130 IStream *pStm;
131 ULARGE_INTEGER newpos;
132 LARGE_INTEGER seekto;
133 ULONG res;
134 HRESULT hres;
135 DWORD xsize;
137 TRACE("...%s...\n",debugstr_guid(riid));
139 *pUnk = NULL;
140 hres = xbuf_get(buf,(LPBYTE)&xsize,sizeof(xsize));
141 if (hres) {
142 ERR("xbuf_get failed\n");
143 return hres;
146 if (xsize == 0) return S_OK;
148 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
149 if (hres) {
150 ERR("Stream create failed %x\n",hres);
151 return hres;
154 hres = IStream_Write(pStm,buf->base+buf->curoff,xsize,&res);
155 if (hres) {
156 ERR("stream write %x\n",hres);
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 return hres;
167 hres = CoUnmarshalInterface(pStm,riid,(LPVOID*)pUnk);
168 if (hres) {
169 ERR("Unmarshalling interface %s failed with %x\n",debugstr_guid(riid),hres);
170 return hres;
173 IStream_Release(pStm);
174 return xbuf_skip(buf,xsize);
177 static HRESULT
178 _marshal_interface(marshal_state *buf, REFIID riid, LPUNKNOWN pUnk) {
179 LPBYTE tempbuf = NULL;
180 IStream *pStm = NULL;
181 STATSTG ststg;
182 ULARGE_INTEGER newpos;
183 LARGE_INTEGER seekto;
184 ULONG res;
185 DWORD xsize;
186 HRESULT hres;
188 if (!pUnk) {
189 /* this is valid, if for instance we serialize
190 * a VT_DISPATCH with NULL ptr which apparently
191 * can happen. S_OK to make sure we continue
192 * serializing.
194 WARN("pUnk is NULL\n");
195 xsize = 0;
196 return xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
199 hres = E_FAIL;
201 TRACE("...%s...\n",debugstr_guid(riid));
203 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
204 if (hres) {
205 ERR("Stream create failed %x\n",hres);
206 goto fail;
209 hres = CoMarshalInterface(pStm,riid,pUnk,0,NULL,0);
210 if (hres) {
211 ERR("Marshalling interface %s failed with %x\n", debugstr_guid(riid), hres);
212 goto fail;
215 hres = IStream_Stat(pStm,&ststg,0);
216 if (hres) {
217 ERR("Stream stat failed\n");
218 goto fail;
221 tempbuf = HeapAlloc(GetProcessHeap(), 0, ststg.cbSize.u.LowPart);
222 memset(&seekto,0,sizeof(seekto));
223 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
224 if (hres) {
225 ERR("Failed Seek %x\n",hres);
226 goto fail;
229 hres = IStream_Read(pStm,tempbuf,ststg.cbSize.u.LowPart,&res);
230 if (hres) {
231 ERR("Failed Read %x\n",hres);
232 goto fail;
235 xsize = ststg.cbSize.u.LowPart;
236 xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
237 hres = xbuf_add(buf,tempbuf,ststg.cbSize.u.LowPart);
239 HeapFree(GetProcessHeap(),0,tempbuf);
240 IStream_Release(pStm);
242 return hres;
244 fail:
245 xsize = 0;
246 xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
247 if (pStm) IUnknown_Release(pStm);
248 HeapFree(GetProcessHeap(), 0, tempbuf);
249 return hres;
252 /********************* OLE Proxy/Stub Factory ********************************/
253 static HRESULT WINAPI
254 PSFacBuf_QueryInterface(LPPSFACTORYBUFFER iface, REFIID iid, LPVOID *ppv) {
255 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)||IsEqualIID(iid,&IID_IUnknown)) {
256 *ppv = (LPVOID)iface;
257 /* No ref counting, static class */
258 return S_OK;
260 FIXME("(%s) unknown IID?\n",debugstr_guid(iid));
261 return E_NOINTERFACE;
264 static ULONG WINAPI PSFacBuf_AddRef(LPPSFACTORYBUFFER iface) { return 2; }
265 static ULONG WINAPI PSFacBuf_Release(LPPSFACTORYBUFFER iface) { return 1; }
267 static HRESULT
268 _get_typeinfo_for_iid(REFIID riid, ITypeInfo**ti) {
269 HRESULT hres;
270 HKEY ikey;
271 char tlguid[200],typelibkey[300],interfacekey[300],ver[100];
272 char tlfn[260];
273 OLECHAR tlfnW[260];
274 DWORD tlguidlen, verlen, type;
275 LONG tlfnlen;
276 ITypeLib *tl;
278 sprintf( interfacekey, "Interface\\{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\Typelib",
279 riid->Data1, riid->Data2, riid->Data3,
280 riid->Data4[0], riid->Data4[1], riid->Data4[2], riid->Data4[3],
281 riid->Data4[4], riid->Data4[5], riid->Data4[6], riid->Data4[7]
284 if (RegOpenKeyA(HKEY_CLASSES_ROOT,interfacekey,&ikey)) {
285 ERR("No %s key found.\n",interfacekey);
286 return E_FAIL;
288 tlguidlen = sizeof(tlguid);
289 if (RegQueryValueExA(ikey,NULL,NULL,&type,(LPBYTE)tlguid,&tlguidlen)) {
290 ERR("Getting typelib guid failed.\n");
291 RegCloseKey(ikey);
292 return E_FAIL;
294 verlen = sizeof(ver);
295 if (RegQueryValueExA(ikey,"Version",NULL,&type,(LPBYTE)ver,&verlen)) {
296 ERR("Could not get version value?\n");
297 RegCloseKey(ikey);
298 return E_FAIL;
300 RegCloseKey(ikey);
301 sprintf(typelibkey,"Typelib\\%s\\%s\\0\\win32",tlguid,ver);
302 tlfnlen = sizeof(tlfn);
303 if (RegQueryValueA(HKEY_CLASSES_ROOT,typelibkey,tlfn,&tlfnlen)) {
304 ERR("Could not get typelib fn?\n");
305 return E_FAIL;
307 MultiByteToWideChar(CP_ACP, 0, tlfn, -1, tlfnW, sizeof(tlfnW) / sizeof(tlfnW[0]));
308 hres = LoadTypeLib(tlfnW,&tl);
309 if (hres) {
310 ERR("Failed to load typelib for %s, but it should be there.\n",debugstr_guid(riid));
311 return hres;
313 hres = ITypeLib_GetTypeInfoOfGuid(tl,riid,ti);
314 if (hres) {
315 ERR("typelib does not contain info for %s?\n",debugstr_guid(riid));
316 ITypeLib_Release(tl);
317 return hres;
319 ITypeLib_Release(tl);
320 return hres;
324 * Determine the number of functions including all inherited functions.
325 * Note for non-dual dispinterfaces we simply return the size of IDispatch.
327 static HRESULT num_of_funcs(ITypeInfo *tinfo, unsigned int *num)
329 HRESULT hres;
330 TYPEATTR *attr;
331 ITypeInfo *tinfo2;
333 *num = 0;
334 hres = ITypeInfo_GetTypeAttr(tinfo, &attr);
335 if (hres) {
336 ERR("GetTypeAttr failed with %x\n",hres);
337 return hres;
340 if(attr->typekind == TKIND_DISPATCH && (attr->wTypeFlags & TYPEFLAG_FDUAL))
342 HREFTYPE href;
343 hres = ITypeInfo_GetRefTypeOfImplType(tinfo, -1, &href);
344 if(FAILED(hres))
346 ERR("Unable to get interface href from dual dispinterface\n");
347 goto end;
349 hres = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
350 if(FAILED(hres))
352 ERR("Unable to get interface from dual dispinterface\n");
353 goto end;
355 hres = num_of_funcs(tinfo2, num);
356 ITypeInfo_Release(tinfo2);
358 else
360 *num = attr->cbSizeVft / 4;
363 end:
364 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
365 return hres;
368 #ifdef __i386__
370 #include "pshpack1.h"
372 typedef struct _TMAsmProxy {
373 BYTE popleax;
374 BYTE pushlval;
375 BYTE nr;
376 BYTE pushleax;
377 BYTE lcall;
378 DWORD xcall;
379 BYTE lret;
380 WORD bytestopop;
381 } TMAsmProxy;
383 #include "poppack.h"
385 #else /* __i386__ */
386 # warning You need to implement stubless proxies for your architecture
387 typedef struct _TMAsmProxy {
388 } TMAsmProxy;
389 #endif
391 typedef struct _TMProxyImpl {
392 LPVOID *lpvtbl;
393 const IRpcProxyBufferVtbl *lpvtbl2;
394 LONG ref;
396 TMAsmProxy *asmstubs;
397 ITypeInfo* tinfo;
398 IRpcChannelBuffer* chanbuf;
399 IID iid;
400 CRITICAL_SECTION crit;
401 IUnknown *outerunknown;
402 IDispatch *dispatch;
403 IRpcProxyBuffer *dispatch_proxy;
404 } TMProxyImpl;
406 static HRESULT WINAPI
407 TMProxyImpl_QueryInterface(LPRPCPROXYBUFFER iface, REFIID riid, LPVOID *ppv)
409 TRACE("()\n");
410 if (IsEqualIID(riid,&IID_IUnknown)||IsEqualIID(riid,&IID_IRpcProxyBuffer)) {
411 *ppv = (LPVOID)iface;
412 IRpcProxyBuffer_AddRef(iface);
413 return S_OK;
415 FIXME("no interface for %s\n",debugstr_guid(riid));
416 return E_NOINTERFACE;
419 static ULONG WINAPI
420 TMProxyImpl_AddRef(LPRPCPROXYBUFFER iface)
422 ICOM_THIS_MULTI(TMProxyImpl,lpvtbl2,iface);
423 ULONG refCount = InterlockedIncrement(&This->ref);
425 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
427 return refCount;
430 static ULONG WINAPI
431 TMProxyImpl_Release(LPRPCPROXYBUFFER iface)
433 ICOM_THIS_MULTI(TMProxyImpl,lpvtbl2,iface);
434 ULONG refCount = InterlockedDecrement(&This->ref);
436 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
438 if (!refCount)
440 if (This->dispatch_proxy) IRpcProxyBuffer_Release(This->dispatch_proxy);
441 This->crit.DebugInfo->Spare[0] = 0;
442 DeleteCriticalSection(&This->crit);
443 if (This->chanbuf) IRpcChannelBuffer_Release(This->chanbuf);
444 VirtualFree(This->asmstubs, 0, MEM_RELEASE);
445 HeapFree(GetProcessHeap(), 0, This->lpvtbl);
446 ITypeInfo_Release(This->tinfo);
447 CoTaskMemFree(This);
449 return refCount;
452 static HRESULT WINAPI
453 TMProxyImpl_Connect(
454 LPRPCPROXYBUFFER iface,IRpcChannelBuffer* pRpcChannelBuffer)
456 ICOM_THIS_MULTI(TMProxyImpl, lpvtbl2, iface);
458 TRACE("(%p)\n", pRpcChannelBuffer);
460 EnterCriticalSection(&This->crit);
462 IRpcChannelBuffer_AddRef(pRpcChannelBuffer);
463 This->chanbuf = pRpcChannelBuffer;
465 LeaveCriticalSection(&This->crit);
467 if (This->dispatch_proxy)
469 IRpcChannelBuffer *pDelegateChannel;
470 HRESULT hr = TMarshalDispatchChannel_Create(pRpcChannelBuffer, &This->iid, &pDelegateChannel);
471 if (FAILED(hr))
472 return hr;
473 return IRpcProxyBuffer_Connect(This->dispatch_proxy, pDelegateChannel);
476 return S_OK;
479 static void WINAPI
480 TMProxyImpl_Disconnect(LPRPCPROXYBUFFER iface)
482 ICOM_THIS_MULTI(TMProxyImpl, lpvtbl2, iface);
484 TRACE("()\n");
486 EnterCriticalSection(&This->crit);
488 IRpcChannelBuffer_Release(This->chanbuf);
489 This->chanbuf = NULL;
491 LeaveCriticalSection(&This->crit);
493 if (This->dispatch_proxy)
494 IRpcProxyBuffer_Disconnect(This->dispatch_proxy);
498 static const IRpcProxyBufferVtbl tmproxyvtable = {
499 TMProxyImpl_QueryInterface,
500 TMProxyImpl_AddRef,
501 TMProxyImpl_Release,
502 TMProxyImpl_Connect,
503 TMProxyImpl_Disconnect
506 /* how much space do we use on stack in DWORD steps. */
508 _argsize(DWORD vt) {
509 switch (vt) {
510 case VT_UI8:
511 return 8/sizeof(DWORD);
512 case VT_R8:
513 return sizeof(double)/sizeof(DWORD);
514 case VT_CY:
515 return sizeof(CY)/sizeof(DWORD);
516 case VT_DATE:
517 return sizeof(DATE)/sizeof(DWORD);
518 case VT_VARIANT:
519 return (sizeof(VARIANT)+3)/sizeof(DWORD);
520 default:
521 return 1;
525 static int
526 _xsize(const TYPEDESC *td) {
527 switch (td->vt) {
528 case VT_DATE:
529 return sizeof(DATE);
530 case VT_VARIANT:
531 return sizeof(VARIANT)+3;
532 case VT_CARRAY: {
533 int i, arrsize = 1;
534 const ARRAYDESC *adesc = td->u.lpadesc;
536 for (i=0;i<adesc->cDims;i++)
537 arrsize *= adesc->rgbounds[i].cElements;
538 return arrsize*_xsize(&adesc->tdescElem);
540 case VT_UI8:
541 case VT_I8:
542 return 8;
543 case VT_UI2:
544 case VT_I2:
545 return 2;
546 case VT_UI1:
547 case VT_I1:
548 return 1;
549 default:
550 return 4;
554 static HRESULT
555 serialize_param(
556 ITypeInfo *tinfo,
557 BOOL writeit,
558 BOOL debugout,
559 BOOL dealloc,
560 TYPEDESC *tdesc,
561 DWORD *arg,
562 marshal_state *buf)
564 HRESULT hres = S_OK;
566 TRACE("(tdesc.vt %s)\n",debugstr_vt(tdesc->vt));
568 switch (tdesc->vt) {
569 case VT_EMPTY: /* nothing. empty variant for instance */
570 return S_OK;
571 case VT_I8:
572 case VT_UI8:
573 case VT_CY:
574 hres = S_OK;
575 if (debugout) TRACE_(olerelay)("%x%x\n",arg[0],arg[1]);
576 if (writeit)
577 hres = xbuf_add(buf,(LPBYTE)arg,8);
578 return hres;
579 case VT_BOOL:
580 case VT_ERROR:
581 case VT_INT:
582 case VT_UINT:
583 case VT_I4:
584 case VT_R4:
585 case VT_UI4:
586 hres = S_OK;
587 if (debugout) TRACE_(olerelay)("%x\n",*arg);
588 if (writeit)
589 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
590 return hres;
591 case VT_I2:
592 case VT_UI2:
593 hres = S_OK;
594 if (debugout) TRACE_(olerelay)("%04x\n",*arg & 0xffff);
595 if (writeit)
596 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
597 return hres;
598 case VT_I1:
599 case VT_UI1:
600 hres = S_OK;
601 if (debugout) TRACE_(olerelay)("%02x\n",*arg & 0xff);
602 if (writeit)
603 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
604 return hres;
605 case VT_I4|VT_BYREF:
606 hres = S_OK;
607 if (debugout) TRACE_(olerelay)("&0x%x\n",*arg);
608 if (writeit)
609 hres = xbuf_add(buf,(LPBYTE)(DWORD*)*arg,sizeof(DWORD));
610 /* do not dealloc at this time */
611 return hres;
612 case VT_VARIANT: {
613 TYPEDESC tdesc2;
614 VARIANT *vt = (VARIANT*)arg;
615 DWORD vttype = V_VT(vt);
617 if (debugout) TRACE_(olerelay)("Vt(%s%s)(",debugstr_vt(vttype),debugstr_vf(vttype));
618 tdesc2.vt = vttype;
619 if (writeit) {
620 hres = xbuf_add(buf,(LPBYTE)&vttype,sizeof(vttype));
621 if (hres) return hres;
623 /* need to recurse since we need to free the stuff */
624 hres = serialize_param(tinfo,writeit,debugout,dealloc,&tdesc2,(DWORD*)&(V_I4(vt)),buf);
625 if (debugout) TRACE_(olerelay)(")");
626 return hres;
628 case VT_BSTR|VT_BYREF: {
629 if (debugout) TRACE_(olerelay)("[byref]'%s'", *(BSTR*)*arg ? relaystr(*((BSTR*)*arg)) : "<bstr NULL>");
630 if (writeit) {
631 /* ptr to ptr to magic widestring, basically */
632 BSTR *bstr = (BSTR *) *arg;
633 DWORD len;
634 if (!*bstr) {
635 /* -1 means "null string" which is equivalent to empty string */
636 len = -1;
637 hres = xbuf_add(buf, (LPBYTE)&len,sizeof(DWORD));
638 if (hres) return hres;
639 } else {
640 len = *((DWORD*)*bstr-1)/sizeof(WCHAR);
641 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
642 if (hres) return hres;
643 hres = xbuf_add(buf,(LPBYTE)*bstr,len * sizeof(WCHAR));
644 if (hres) return hres;
648 if (dealloc && arg) {
649 BSTR *str = *((BSTR **)arg);
650 SysFreeString(*str);
652 return S_OK;
655 case VT_BSTR: {
656 if (debugout) {
657 if (*arg)
658 TRACE_(olerelay)("%s",relaystr((WCHAR*)*arg));
659 else
660 TRACE_(olerelay)("<bstr NULL>");
662 if (writeit) {
663 BSTR bstr = (BSTR)*arg;
664 DWORD len;
665 if (!bstr) {
666 len = -1;
667 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
668 if (hres) return hres;
669 } else {
670 len = *((DWORD*)bstr-1)/sizeof(WCHAR);
671 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
672 if (hres) return hres;
673 hres = xbuf_add(buf,(LPBYTE)bstr,len * sizeof(WCHAR));
674 if (hres) return hres;
678 if (dealloc && arg)
679 SysFreeString((BSTR)*arg);
680 return S_OK;
682 case VT_PTR: {
683 DWORD cookie;
684 BOOL derefhere = TRUE;
686 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
687 ITypeInfo *tinfo2;
688 TYPEATTR *tattr;
690 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
691 if (hres) {
692 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
693 return hres;
695 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
696 switch (tattr->typekind) {
697 case TKIND_ENUM: /* confirmed */
698 case TKIND_RECORD: /* FIXME: mostly untested */
699 derefhere=TRUE;
700 break;
701 case TKIND_ALIAS: /* FIXME: untested */
702 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
703 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
704 derefhere=FALSE;
705 break;
706 default:
707 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
708 derefhere=FALSE;
709 break;
711 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
712 ITypeInfo_Release(tinfo2);
715 if (debugout) TRACE_(olerelay)("*");
716 /* Write always, so the other side knows when it gets a NULL pointer.
718 cookie = *arg ? 0x42424242 : 0;
719 hres = xbuf_add(buf,(LPBYTE)&cookie,sizeof(cookie));
720 if (hres)
721 return hres;
722 if (!*arg) {
723 if (debugout) TRACE_(olerelay)("NULL");
724 return S_OK;
726 hres = serialize_param(tinfo,writeit,debugout,dealloc,tdesc->u.lptdesc,(DWORD*)*arg,buf);
727 if (derefhere && dealloc) HeapFree(GetProcessHeap(),0,(LPVOID)*arg);
728 return hres;
730 case VT_UNKNOWN:
731 if (debugout) TRACE_(olerelay)("unk(0x%x)",*arg);
732 if (writeit)
733 hres = _marshal_interface(buf,&IID_IUnknown,(LPUNKNOWN)*arg);
734 if (dealloc && *(IUnknown **)arg)
735 IUnknown_Release((LPUNKNOWN)*arg);
736 return hres;
737 case VT_DISPATCH:
738 if (debugout) TRACE_(olerelay)("idisp(0x%x)",*arg);
739 if (writeit)
740 hres = _marshal_interface(buf,&IID_IDispatch,(LPUNKNOWN)*arg);
741 if (dealloc && *(IUnknown **)arg)
742 IUnknown_Release((LPUNKNOWN)*arg);
743 return hres;
744 case VT_VOID:
745 if (debugout) TRACE_(olerelay)("<void>");
746 return S_OK;
747 case VT_USERDEFINED: {
748 ITypeInfo *tinfo2;
749 TYPEATTR *tattr;
751 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
752 if (hres) {
753 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
754 return hres;
756 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
757 switch (tattr->typekind) {
758 case TKIND_DISPATCH:
759 case TKIND_INTERFACE:
760 if (writeit)
761 hres=_marshal_interface(buf,&(tattr->guid),(LPUNKNOWN)arg);
762 if (dealloc)
763 IUnknown_Release((LPUNKNOWN)arg);
764 break;
765 case TKIND_RECORD: {
766 int i;
767 if (debugout) TRACE_(olerelay)("{");
768 for (i=0;i<tattr->cVars;i++) {
769 VARDESC *vdesc;
770 ELEMDESC *elem2;
771 TYPEDESC *tdesc2;
773 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
774 if (hres) {
775 ERR("Could not get vardesc of %d\n",i);
776 return hres;
778 elem2 = &vdesc->elemdescVar;
779 tdesc2 = &elem2->tdesc;
780 hres = serialize_param(
781 tinfo2,
782 writeit,
783 debugout,
784 dealloc,
785 tdesc2,
786 (DWORD*)(((LPBYTE)arg)+vdesc->u.oInst),
789 ITypeInfo_ReleaseVarDesc(tinfo2, vdesc);
790 if (hres!=S_OK)
791 return hres;
792 if (debugout && (i<(tattr->cVars-1)))
793 TRACE_(olerelay)(",");
795 if (debugout) TRACE_(olerelay)("}");
796 break;
798 case TKIND_ALIAS:
799 hres = serialize_param(tinfo2,writeit,debugout,dealloc,&tattr->tdescAlias,arg,buf);
800 break;
801 case TKIND_ENUM:
802 hres = S_OK;
803 if (debugout) TRACE_(olerelay)("%x",*arg);
804 if (writeit)
805 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
806 break;
807 default:
808 FIXME("Unhandled typekind %d\n",tattr->typekind);
809 hres = E_FAIL;
810 break;
812 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
813 ITypeInfo_Release(tinfo2);
814 return hres;
816 case VT_CARRAY: {
817 ARRAYDESC *adesc = tdesc->u.lpadesc;
818 int i, arrsize = 1;
820 if (debugout) TRACE_(olerelay)("carr");
821 for (i=0;i<adesc->cDims;i++) {
822 if (debugout) TRACE_(olerelay)("[%d]",adesc->rgbounds[i].cElements);
823 arrsize *= adesc->rgbounds[i].cElements;
825 if (debugout) TRACE_(olerelay)("(vt %s)",debugstr_vt(adesc->tdescElem.vt));
826 if (debugout) TRACE_(olerelay)("[");
827 for (i=0;i<arrsize;i++) {
828 hres = serialize_param(tinfo, writeit, debugout, dealloc, &adesc->tdescElem, (DWORD*)((LPBYTE)arg+i*_xsize(&adesc->tdescElem)), buf);
829 if (hres)
830 return hres;
831 if (debugout && (i<arrsize-1)) TRACE_(olerelay)(",");
833 if (debugout) TRACE_(olerelay)("]");
834 return S_OK;
836 case VT_SAFEARRAY: {
837 if (writeit)
839 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
840 ULONG size = LPSAFEARRAY_UserSize(&flags, buf->curoff, (LPSAFEARRAY *)arg);
841 xbuf_resize(buf, size);
842 LPSAFEARRAY_UserMarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
843 buf->curoff = size;
845 return S_OK;
847 default:
848 ERR("Unhandled marshal type %d.\n",tdesc->vt);
849 return S_OK;
853 static HRESULT
854 deserialize_param(
855 ITypeInfo *tinfo,
856 BOOL readit,
857 BOOL debugout,
858 BOOL alloc,
859 TYPEDESC *tdesc,
860 DWORD *arg,
861 marshal_state *buf)
863 HRESULT hres = S_OK;
865 TRACE("vt %s at %p\n",debugstr_vt(tdesc->vt),arg);
867 while (1) {
868 switch (tdesc->vt) {
869 case VT_EMPTY:
870 if (debugout) TRACE_(olerelay)("<empty>\n");
871 return S_OK;
872 case VT_NULL:
873 if (debugout) TRACE_(olerelay)("<null>\n");
874 return S_OK;
875 case VT_VARIANT: {
876 VARIANT *vt = (VARIANT*)arg;
878 if (readit) {
879 DWORD vttype;
880 TYPEDESC tdesc2;
881 hres = xbuf_get(buf,(LPBYTE)&vttype,sizeof(vttype));
882 if (hres) {
883 FIXME("vt type not read?\n");
884 return hres;
886 memset(&tdesc2,0,sizeof(tdesc2));
887 tdesc2.vt = vttype;
888 V_VT(vt) = vttype;
889 if (debugout) TRACE_(olerelay)("Vt(%s%s)(",debugstr_vt(vttype),debugstr_vf(vttype));
890 hres = deserialize_param(tinfo, readit, debugout, alloc, &tdesc2, (DWORD*)&(V_I4(vt)), buf);
891 TRACE_(olerelay)(")");
892 return hres;
893 } else {
894 VariantInit(vt);
895 return S_OK;
898 case VT_I8:
899 case VT_UI8:
900 case VT_CY:
901 if (readit) {
902 hres = xbuf_get(buf,(LPBYTE)arg,8);
903 if (hres) ERR("Failed to read integer 8 byte\n");
905 if (debugout) TRACE_(olerelay)("%x%x",arg[0],arg[1]);
906 return hres;
907 case VT_ERROR:
908 case VT_BOOL:
909 case VT_I4:
910 case VT_INT:
911 case VT_UINT:
912 case VT_R4:
913 case VT_UI4:
914 if (readit) {
915 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
916 if (hres) ERR("Failed to read integer 4 byte\n");
918 if (debugout) TRACE_(olerelay)("%x",*arg);
919 return hres;
920 case VT_I2:
921 case VT_UI2:
922 if (readit) {
923 DWORD x;
924 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
925 if (hres) ERR("Failed to read integer 4 byte\n");
926 memcpy(arg,&x,2);
928 if (debugout) TRACE_(olerelay)("%04x",*arg & 0xffff);
929 return hres;
930 case VT_I1:
931 case VT_UI1:
932 if (readit) {
933 DWORD x;
934 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
935 if (hres) ERR("Failed to read integer 4 byte\n");
936 memcpy(arg,&x,1);
938 if (debugout) TRACE_(olerelay)("%02x",*arg & 0xff);
939 return hres;
940 case VT_I4|VT_BYREF:
941 hres = S_OK;
942 if (alloc)
943 *arg = (DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
944 if (readit) {
945 hres = xbuf_get(buf,(LPBYTE)*arg,sizeof(DWORD));
946 if (hres) ERR("Failed to read integer 4 byte\n");
948 if (debugout) TRACE_(olerelay)("&0x%x",*(DWORD*)*arg);
949 return hres;
950 case VT_BSTR|VT_BYREF: {
951 BSTR **bstr = (BSTR **)arg;
952 WCHAR *str;
953 DWORD len;
955 if (readit) {
956 hres = xbuf_get(buf,(LPBYTE)&len,sizeof(DWORD));
957 if (hres) {
958 ERR("failed to read bstr klen\n");
959 return hres;
961 if (len == -1) {
962 *bstr = CoTaskMemAlloc(sizeof(BSTR *));
963 **bstr = NULL;
964 if (debugout) TRACE_(olerelay)("<bstr NULL>");
965 } else {
966 str = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(len+1)*sizeof(WCHAR));
967 hres = xbuf_get(buf,(LPBYTE)str,len*sizeof(WCHAR));
968 if (hres) {
969 ERR("Failed to read BSTR.\n");
970 HeapFree(GetProcessHeap(),0,str);
971 return hres;
973 *bstr = CoTaskMemAlloc(sizeof(BSTR *));
974 **bstr = SysAllocStringLen(str,len);
975 if (debugout) TRACE_(olerelay)("%s",relaystr(str));
976 HeapFree(GetProcessHeap(),0,str);
978 } else {
979 *bstr = NULL;
981 return S_OK;
983 case VT_BSTR: {
984 WCHAR *str;
985 DWORD len;
987 if (readit) {
988 hres = xbuf_get(buf,(LPBYTE)&len,sizeof(DWORD));
989 if (hres) {
990 ERR("failed to read bstr klen\n");
991 return hres;
993 if (len == -1) {
994 *arg = 0;
995 if (debugout) TRACE_(olerelay)("<bstr NULL>");
996 } else {
997 str = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(len+1)*sizeof(WCHAR));
998 hres = xbuf_get(buf,(LPBYTE)str,len*sizeof(WCHAR));
999 if (hres) {
1000 ERR("Failed to read BSTR.\n");
1001 HeapFree(GetProcessHeap(),0,str);
1002 return hres;
1004 *arg = (DWORD)SysAllocStringLen(str,len);
1005 if (debugout) TRACE_(olerelay)("%s",relaystr(str));
1006 HeapFree(GetProcessHeap(),0,str);
1008 } else {
1009 *arg = 0;
1011 return S_OK;
1013 case VT_PTR: {
1014 DWORD cookie;
1015 BOOL derefhere = TRUE;
1017 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
1018 ITypeInfo *tinfo2;
1019 TYPEATTR *tattr;
1021 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
1022 if (hres) {
1023 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
1024 return hres;
1026 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1027 switch (tattr->typekind) {
1028 case TKIND_ENUM: /* confirmed */
1029 case TKIND_RECORD: /* FIXME: mostly untested */
1030 derefhere=TRUE;
1031 break;
1032 case TKIND_ALIAS: /* FIXME: untested */
1033 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
1034 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
1035 derefhere=FALSE;
1036 break;
1037 default:
1038 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
1039 derefhere=FALSE;
1040 break;
1042 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1043 ITypeInfo_Release(tinfo2);
1045 /* read it in all cases, we need to know if we have
1046 * NULL pointer or not.
1048 hres = xbuf_get(buf,(LPBYTE)&cookie,sizeof(cookie));
1049 if (hres) {
1050 ERR("Failed to load pointer cookie.\n");
1051 return hres;
1053 if (cookie != 0x42424242) {
1054 /* we read a NULL ptr from the remote side */
1055 if (debugout) TRACE_(olerelay)("NULL");
1056 *arg = 0;
1057 return S_OK;
1059 if (debugout) TRACE_(olerelay)("*");
1060 if (alloc) {
1061 /* Allocate space for the referenced struct */
1062 if (derefhere)
1063 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,_xsize(tdesc->u.lptdesc));
1065 if (derefhere)
1066 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, (LPDWORD)*arg, buf);
1067 else
1068 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, arg, buf);
1070 case VT_UNKNOWN:
1071 /* FIXME: UNKNOWN is unknown ..., but allocate 4 byte for it */
1072 if (alloc)
1073 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
1074 hres = S_OK;
1075 if (readit)
1076 hres = _unmarshal_interface(buf,&IID_IUnknown,(LPUNKNOWN*)arg);
1077 if (debugout)
1078 TRACE_(olerelay)("unk(%p)",arg);
1079 return hres;
1080 case VT_DISPATCH:
1081 hres = S_OK;
1082 if (readit)
1083 hres = _unmarshal_interface(buf,&IID_IDispatch,(LPUNKNOWN*)arg);
1084 if (debugout)
1085 TRACE_(olerelay)("idisp(%p)",arg);
1086 return hres;
1087 case VT_VOID:
1088 if (debugout) TRACE_(olerelay)("<void>");
1089 return S_OK;
1090 case VT_USERDEFINED: {
1091 ITypeInfo *tinfo2;
1092 TYPEATTR *tattr;
1094 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
1095 if (hres) {
1096 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
1097 return hres;
1099 hres = ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1100 if (hres) {
1101 ERR("Could not get typeattr in VT_USERDEFINED.\n");
1102 } else {
1103 switch (tattr->typekind) {
1104 case TKIND_DISPATCH:
1105 case TKIND_INTERFACE:
1106 if (readit)
1107 hres = _unmarshal_interface(buf,&(tattr->guid),(LPUNKNOWN*)arg);
1108 break;
1109 case TKIND_RECORD: {
1110 int i;
1112 if (alloc)
1113 *arg = (DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,tattr->cbSizeInstance);
1115 if (debugout) TRACE_(olerelay)("{");
1116 for (i=0;i<tattr->cVars;i++) {
1117 VARDESC *vdesc;
1119 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
1120 if (hres) {
1121 ERR("Could not get vardesc of %d\n",i);
1122 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1123 ITypeInfo_Release(tinfo2);
1124 return hres;
1126 hres = deserialize_param(
1127 tinfo2,
1128 readit,
1129 debugout,
1130 alloc,
1131 &vdesc->elemdescVar.tdesc,
1132 (DWORD*)(((LPBYTE)*arg)+vdesc->u.oInst),
1135 ITypeInfo2_ReleaseVarDesc(tinfo2, vdesc);
1136 if (debugout && (i<tattr->cVars-1)) TRACE_(olerelay)(",");
1138 if (debugout) TRACE_(olerelay)("}");
1139 break;
1141 case TKIND_ALIAS:
1142 hres = deserialize_param(tinfo2,readit,debugout,alloc,&tattr->tdescAlias,arg,buf);
1143 break;
1144 case TKIND_ENUM:
1145 if (readit) {
1146 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
1147 if (hres) ERR("Failed to read enum (4 byte)\n");
1149 if (debugout) TRACE_(olerelay)("%x",*arg);
1150 break;
1151 default:
1152 ERR("Unhandled typekind %d\n",tattr->typekind);
1153 hres = E_FAIL;
1154 break;
1156 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1158 if (hres)
1159 ERR("failed to stuballoc in TKIND_RECORD.\n");
1160 ITypeInfo_Release(tinfo2);
1161 return hres;
1163 case VT_CARRAY: {
1164 /* arg is pointing to the start of the array. */
1165 ARRAYDESC *adesc = tdesc->u.lpadesc;
1166 int arrsize,i;
1167 arrsize = 1;
1168 if (adesc->cDims > 1) FIXME("cDims > 1 in VT_CARRAY. Does it work?\n");
1169 for (i=0;i<adesc->cDims;i++)
1170 arrsize *= adesc->rgbounds[i].cElements;
1171 for (i=0;i<arrsize;i++)
1172 deserialize_param(
1173 tinfo,
1174 readit,
1175 debugout,
1176 alloc,
1177 &adesc->tdescElem,
1178 (DWORD*)((LPBYTE)(arg)+i*_xsize(&adesc->tdescElem)),
1181 return S_OK;
1183 case VT_SAFEARRAY: {
1184 if (readit)
1186 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
1187 unsigned char *buffer;
1188 buffer = LPSAFEARRAY_UserUnmarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
1189 buf->curoff = buffer - buf->base;
1191 return S_OK;
1193 default:
1194 ERR("No handler for VT type %d!\n",tdesc->vt);
1195 return S_OK;
1200 /* Retrieves a function's funcdesc, searching back into inherited interfaces. */
1201 static HRESULT get_funcdesc(ITypeInfo *tinfo, int iMethod, ITypeInfo **tactual, const FUNCDESC **fdesc,
1202 BSTR *iname, BSTR *fname, UINT *num)
1204 HRESULT hr;
1205 UINT i, impl_types;
1206 UINT inherited_funcs = 0;
1207 TYPEATTR *attr;
1209 if (fname) *fname = NULL;
1210 if (iname) *iname = NULL;
1211 if (num) *num = 0;
1212 *tactual = NULL;
1214 hr = ITypeInfo_GetTypeAttr(tinfo, &attr);
1215 if (FAILED(hr))
1217 ERR("GetTypeAttr failed with %x\n",hr);
1218 return hr;
1221 if(attr->typekind == TKIND_DISPATCH)
1223 if(attr->wTypeFlags & TYPEFLAG_FDUAL)
1225 HREFTYPE href;
1226 ITypeInfo *tinfo2;
1228 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, -1, &href);
1229 if(FAILED(hr))
1231 ERR("Cannot get interface href from dual dispinterface\n");
1232 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1233 return hr;
1235 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
1236 if(FAILED(hr))
1238 ERR("Cannot get interface from dual dispinterface\n");
1239 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1240 return hr;
1242 hr = get_funcdesc(tinfo2, iMethod, tactual, fdesc, iname, fname, num);
1243 ITypeInfo_Release(tinfo2);
1244 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1245 return hr;
1247 ERR("Shouldn't be called with a non-dual dispinterface\n");
1248 return E_FAIL;
1251 impl_types = attr->cImplTypes;
1252 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1254 for (i = 0; i < impl_types; i++)
1256 HREFTYPE href;
1257 ITypeInfo *pSubTypeInfo;
1258 UINT sub_funcs;
1260 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, i, &href);
1261 if (FAILED(hr)) return hr;
1262 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &pSubTypeInfo);
1263 if (FAILED(hr)) return hr;
1265 hr = get_funcdesc(pSubTypeInfo, iMethod, tactual, fdesc, iname, fname, &sub_funcs);
1266 inherited_funcs += sub_funcs;
1267 ITypeInfo_Release(pSubTypeInfo);
1268 if(SUCCEEDED(hr)) return hr;
1270 if(iMethod < inherited_funcs)
1272 ERR("shouldn't be here\n");
1273 return E_INVALIDARG;
1276 for(i = inherited_funcs; i <= iMethod; i++)
1278 hr = ITypeInfoImpl_GetInternalFuncDesc(tinfo, i - inherited_funcs, fdesc);
1279 if(FAILED(hr))
1281 if(num) *num = i;
1282 return hr;
1286 /* found it. We don't care about num so zero it */
1287 if(num) *num = 0;
1288 *tactual = tinfo;
1289 ITypeInfo_AddRef(*tactual);
1290 if (fname) ITypeInfo_GetDocumentation(tinfo,(*fdesc)->memid,fname,NULL,NULL,NULL);
1291 if (iname) ITypeInfo_GetDocumentation(tinfo,-1,iname,NULL,NULL,NULL);
1292 return S_OK;
1295 static inline BOOL is_in_elem(const ELEMDESC *elem)
1297 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN || !elem->u.paramdesc.wParamFlags);
1300 static inline BOOL is_out_elem(const ELEMDESC *elem)
1302 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT || !elem->u.paramdesc.wParamFlags);
1305 static DWORD
1306 xCall(LPVOID retptr, int method, TMProxyImpl *tpinfo /*, args */)
1308 DWORD *args = ((DWORD*)&tpinfo)+1, *xargs;
1309 const FUNCDESC *fdesc;
1310 HRESULT hres;
1311 int i, relaydeb = TRACE_ON(olerelay);
1312 marshal_state buf;
1313 RPCOLEMESSAGE msg;
1314 ULONG status;
1315 BSTR fname,iname;
1316 BSTR names[10];
1317 UINT nrofnames;
1318 DWORD remoteresult = 0;
1319 ITypeInfo *tinfo;
1320 IRpcChannelBuffer *chanbuf;
1322 EnterCriticalSection(&tpinfo->crit);
1324 hres = get_funcdesc(tpinfo->tinfo,method,&tinfo,&fdesc,&iname,&fname,NULL);
1325 if (hres) {
1326 ERR("Did not find typeinfo/funcdesc entry for method %d!\n",method);
1327 LeaveCriticalSection(&tpinfo->crit);
1328 return E_FAIL;
1331 if (!tpinfo->chanbuf)
1333 WARN("Tried to use disconnected proxy\n");
1334 ITypeInfo_Release(tinfo);
1335 LeaveCriticalSection(&tpinfo->crit);
1336 return RPC_E_DISCONNECTED;
1338 chanbuf = tpinfo->chanbuf;
1339 IRpcChannelBuffer_AddRef(chanbuf);
1341 LeaveCriticalSection(&tpinfo->crit);
1343 if (relaydeb) {
1344 TRACE_(olerelay)("->");
1345 if (iname)
1346 TRACE_(olerelay)("%s:",relaystr(iname));
1347 if (fname)
1348 TRACE_(olerelay)("%s(%d)",relaystr(fname),method);
1349 else
1350 TRACE_(olerelay)("%d",method);
1351 TRACE_(olerelay)("(");
1354 if (iname) SysFreeString(iname);
1355 if (fname) SysFreeString(fname);
1357 memset(&buf,0,sizeof(buf));
1359 /* normal typelib driven serializing */
1361 /* Need them for hack below */
1362 memset(names,0,sizeof(names));
1363 if (ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames))
1364 nrofnames = 0;
1365 if (nrofnames > sizeof(names)/sizeof(names[0]))
1366 ERR("Need more names!\n");
1368 xargs = args;
1369 for (i=0;i<fdesc->cParams;i++) {
1370 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1371 if (relaydeb) {
1372 if (i) TRACE_(olerelay)(",");
1373 if (i+1<nrofnames && names[i+1])
1374 TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1376 /* No need to marshal other data than FIN and any VT_PTR. */
1377 if (!is_in_elem(elem) && (elem->tdesc.vt != VT_PTR)) {
1378 xargs+=_argsize(elem->tdesc.vt);
1379 if (relaydeb) TRACE_(olerelay)("[out]");
1380 continue;
1382 hres = serialize_param(
1383 tinfo,
1384 is_in_elem(elem),
1385 relaydeb,
1386 FALSE,
1387 &elem->tdesc,
1388 xargs,
1389 &buf
1392 if (hres) {
1393 ERR("Failed to serialize param, hres %x\n",hres);
1394 break;
1396 xargs+=_argsize(elem->tdesc.vt);
1398 if (relaydeb) TRACE_(olerelay)(")");
1400 memset(&msg,0,sizeof(msg));
1401 msg.cbBuffer = buf.curoff;
1402 msg.iMethod = method;
1403 hres = IRpcChannelBuffer_GetBuffer(chanbuf,&msg,&(tpinfo->iid));
1404 if (hres) {
1405 ERR("RpcChannelBuffer GetBuffer failed, %x\n",hres);
1406 goto exit;
1408 memcpy(msg.Buffer,buf.base,buf.curoff);
1409 if (relaydeb) TRACE_(olerelay)("\n");
1410 hres = IRpcChannelBuffer_SendReceive(chanbuf,&msg,&status);
1411 if (hres) {
1412 ERR("RpcChannelBuffer SendReceive failed, %x\n",hres);
1413 goto exit;
1416 if (relaydeb) TRACE_(olerelay)(" status = %08x (",status);
1417 if (buf.base)
1418 buf.base = HeapReAlloc(GetProcessHeap(),0,buf.base,msg.cbBuffer);
1419 else
1420 buf.base = HeapAlloc(GetProcessHeap(),0,msg.cbBuffer);
1421 buf.size = msg.cbBuffer;
1422 memcpy(buf.base,msg.Buffer,buf.size);
1423 buf.curoff = 0;
1425 /* generic deserializer using typelib description */
1426 xargs = args;
1427 status = S_OK;
1428 for (i=0;i<fdesc->cParams;i++) {
1429 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1431 if (relaydeb) {
1432 if (i) TRACE_(olerelay)(",");
1433 if (i+1<nrofnames && names[i+1]) TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1435 /* No need to marshal other data than FOUT and any VT_PTR */
1436 if (!is_out_elem(elem) && (elem->tdesc.vt != VT_PTR)) {
1437 xargs += _argsize(elem->tdesc.vt);
1438 if (relaydeb) TRACE_(olerelay)("[in]");
1439 continue;
1441 hres = deserialize_param(
1442 tinfo,
1443 is_out_elem(elem),
1444 relaydeb,
1445 FALSE,
1446 &(elem->tdesc),
1447 xargs,
1448 &buf
1450 if (hres) {
1451 ERR("Failed to unmarshall param, hres %x\n",hres);
1452 status = hres;
1453 break;
1455 xargs += _argsize(elem->tdesc.vt);
1458 hres = xbuf_get(&buf, (LPBYTE)&remoteresult, sizeof(DWORD));
1459 if (hres != S_OK)
1460 goto exit;
1461 if (relaydeb) TRACE_(olerelay)(") = %08x\n", remoteresult);
1463 hres = remoteresult;
1465 exit:
1466 for (i = 0; i < nrofnames; i++)
1467 SysFreeString(names[i]);
1468 HeapFree(GetProcessHeap(),0,buf.base);
1469 IRpcChannelBuffer_Release(chanbuf);
1470 ITypeInfo_Release(tinfo);
1471 TRACE("-- 0x%08x\n", hres);
1472 return hres;
1475 static HRESULT WINAPI ProxyIUnknown_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
1477 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1479 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1481 if (proxy->outerunknown)
1482 return IUnknown_QueryInterface(proxy->outerunknown, riid, ppv);
1484 FIXME("No interface\n");
1485 return E_NOINTERFACE;
1488 static ULONG WINAPI ProxyIUnknown_AddRef(IUnknown *iface)
1490 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1492 TRACE("\n");
1494 if (proxy->outerunknown)
1495 return IUnknown_AddRef(proxy->outerunknown);
1497 return 2; /* FIXME */
1500 static ULONG WINAPI ProxyIUnknown_Release(IUnknown *iface)
1502 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1504 TRACE("\n");
1506 if (proxy->outerunknown)
1507 return IUnknown_Release(proxy->outerunknown);
1509 return 1; /* FIXME */
1512 static HRESULT WINAPI ProxyIDispatch_GetTypeInfoCount(LPDISPATCH iface, UINT * pctinfo)
1514 TMProxyImpl *This = (TMProxyImpl *)iface;
1516 TRACE("(%p)\n", pctinfo);
1518 return IDispatch_GetTypeInfoCount(This->dispatch, pctinfo);
1521 static HRESULT WINAPI ProxyIDispatch_GetTypeInfo(LPDISPATCH iface, UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo)
1523 TMProxyImpl *This = (TMProxyImpl *)iface;
1525 TRACE("(%d, %x, %p)\n", iTInfo, lcid, ppTInfo);
1527 return IDispatch_GetTypeInfo(This->dispatch, iTInfo, lcid, ppTInfo);
1530 static HRESULT WINAPI ProxyIDispatch_GetIDsOfNames(LPDISPATCH iface, REFIID riid, LPOLESTR * rgszNames, UINT cNames, LCID lcid, DISPID * rgDispId)
1532 TMProxyImpl *This = (TMProxyImpl *)iface;
1534 TRACE("(%s, %p, %d, 0x%x, %p)\n", debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);
1536 return IDispatch_GetIDsOfNames(This->dispatch, riid, rgszNames,
1537 cNames, lcid, rgDispId);
1540 static HRESULT WINAPI ProxyIDispatch_Invoke(LPDISPATCH iface, DISPID dispIdMember, REFIID riid, LCID lcid,
1541 WORD wFlags, DISPPARAMS * pDispParams, VARIANT * pVarResult,
1542 EXCEPINFO * pExcepInfo, UINT * puArgErr)
1544 TMProxyImpl *This = (TMProxyImpl *)iface;
1546 TRACE("(%d, %s, 0x%x, 0x%x, %p, %p, %p, %p)\n", dispIdMember,
1547 debugstr_guid(riid), lcid, wFlags, pDispParams, pVarResult,
1548 pExcepInfo, puArgErr);
1550 return IDispatch_Invoke(This->dispatch, dispIdMember, riid, lcid,
1551 wFlags, pDispParams, pVarResult, pExcepInfo,
1552 puArgErr);
1555 typedef struct
1557 const IRpcChannelBufferVtbl *lpVtbl;
1558 LONG refs;
1559 /* the IDispatch-derived interface we are handling */
1560 IID tmarshal_iid;
1561 IRpcChannelBuffer *pDelegateChannel;
1562 } TMarshalDispatchChannel;
1564 static HRESULT WINAPI TMarshalDispatchChannel_QueryInterface(LPRPCCHANNELBUFFER iface, REFIID riid, LPVOID *ppv)
1566 *ppv = NULL;
1567 if (IsEqualIID(riid,&IID_IRpcChannelBuffer) || IsEqualIID(riid,&IID_IUnknown))
1569 *ppv = (LPVOID)iface;
1570 IUnknown_AddRef(iface);
1571 return S_OK;
1573 return E_NOINTERFACE;
1576 static ULONG WINAPI TMarshalDispatchChannel_AddRef(LPRPCCHANNELBUFFER iface)
1578 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1579 return InterlockedIncrement(&This->refs);
1582 static ULONG WINAPI TMarshalDispatchChannel_Release(LPRPCCHANNELBUFFER iface)
1584 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1585 ULONG ref;
1587 ref = InterlockedDecrement(&This->refs);
1588 if (ref)
1589 return ref;
1591 IRpcChannelBuffer_Release(This->pDelegateChannel);
1592 HeapFree(GetProcessHeap(), 0, This);
1593 return 0;
1596 static HRESULT WINAPI TMarshalDispatchChannel_GetBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg, REFIID riid)
1598 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1599 TRACE("(%p, %s)\n", olemsg, debugstr_guid(riid));
1600 /* Note: we are pretending to invoke a method on the interface identified
1601 * by tmarshal_iid so that we can re-use the IDispatch proxy/stub code
1602 * without the RPC runtime getting confused by not exporting an IDispatch interface */
1603 return IRpcChannelBuffer_GetBuffer(This->pDelegateChannel, olemsg, &This->tmarshal_iid);
1606 static HRESULT WINAPI TMarshalDispatchChannel_SendReceive(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE *olemsg, ULONG *pstatus)
1608 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1609 TRACE("(%p, %p)\n", olemsg, pstatus);
1610 return IRpcChannelBuffer_SendReceive(This->pDelegateChannel, olemsg, pstatus);
1613 static HRESULT WINAPI TMarshalDispatchChannel_FreeBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg)
1615 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1616 TRACE("(%p)\n", olemsg);
1617 return IRpcChannelBuffer_FreeBuffer(This->pDelegateChannel, olemsg);
1620 static HRESULT WINAPI TMarshalDispatchChannel_GetDestCtx(LPRPCCHANNELBUFFER iface, DWORD* pdwDestContext, void** ppvDestContext)
1622 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1623 TRACE("(%p,%p)\n", pdwDestContext, ppvDestContext);
1624 return IRpcChannelBuffer_GetDestCtx(This->pDelegateChannel, pdwDestContext, ppvDestContext);
1627 static HRESULT WINAPI TMarshalDispatchChannel_IsConnected(LPRPCCHANNELBUFFER iface)
1629 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1630 TRACE("()\n");
1631 return IRpcChannelBuffer_IsConnected(This->pDelegateChannel);
1634 static const IRpcChannelBufferVtbl TMarshalDispatchChannelVtbl =
1636 TMarshalDispatchChannel_QueryInterface,
1637 TMarshalDispatchChannel_AddRef,
1638 TMarshalDispatchChannel_Release,
1639 TMarshalDispatchChannel_GetBuffer,
1640 TMarshalDispatchChannel_SendReceive,
1641 TMarshalDispatchChannel_FreeBuffer,
1642 TMarshalDispatchChannel_GetDestCtx,
1643 TMarshalDispatchChannel_IsConnected
1646 static HRESULT TMarshalDispatchChannel_Create(
1647 IRpcChannelBuffer *pDelegateChannel, REFIID tmarshal_riid,
1648 IRpcChannelBuffer **ppChannel)
1650 TMarshalDispatchChannel *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1651 if (!This)
1652 return E_OUTOFMEMORY;
1654 This->lpVtbl = &TMarshalDispatchChannelVtbl;
1655 This->refs = 1;
1656 IRpcChannelBuffer_AddRef(pDelegateChannel);
1657 This->pDelegateChannel = pDelegateChannel;
1658 This->tmarshal_iid = *tmarshal_riid;
1660 *ppChannel = (IRpcChannelBuffer *)&This->lpVtbl;
1661 return S_OK;
1665 static inline HRESULT get_facbuf_for_iid(REFIID riid, IPSFactoryBuffer **facbuf)
1667 HRESULT hr;
1668 CLSID clsid;
1670 if ((hr = CoGetPSClsid(riid, &clsid)))
1671 return hr;
1672 return CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL,
1673 &IID_IPSFactoryBuffer, (LPVOID*)facbuf);
1676 static HRESULT init_proxy_entry_point(TMProxyImpl *proxy, unsigned int num)
1678 int j;
1679 /* nrofargs without This */
1680 int nrofargs;
1681 ITypeInfo *tinfo2;
1682 TMAsmProxy *xasm = proxy->asmstubs + num;
1683 HRESULT hres;
1684 const FUNCDESC *fdesc;
1686 hres = get_funcdesc(proxy->tinfo, num, &tinfo2, &fdesc, NULL, NULL, NULL);
1687 if (hres) {
1688 ERR("GetFuncDesc %x should not fail here.\n",hres);
1689 return hres;
1691 ITypeInfo_Release(tinfo2);
1692 /* some args take more than 4 byte on the stack */
1693 nrofargs = 0;
1694 for (j=0;j<fdesc->cParams;j++)
1695 nrofargs += _argsize(fdesc->lprgelemdescParam[j].tdesc.vt);
1697 #ifdef __i386__
1698 if (fdesc->callconv != CC_STDCALL) {
1699 ERR("calling convention is not stdcall????\n");
1700 return E_FAIL;
1702 /* popl %eax - return ptr
1703 * pushl <nr>
1704 * pushl %eax
1705 * call xCall
1706 * lret <nr> (+4)
1709 * arg3 arg2 arg1 <method> <returnptr>
1711 xasm->popleax = 0x58;
1712 xasm->pushlval = 0x6a;
1713 xasm->nr = num;
1714 xasm->pushleax = 0x50;
1715 xasm->lcall = 0xe8; /* relative jump */
1716 xasm->xcall = (DWORD)xCall;
1717 xasm->xcall -= (DWORD)&(xasm->lret);
1718 xasm->lret = 0xc2;
1719 xasm->bytestopop = (nrofargs+2)*4; /* pop args, This, iMethod */
1720 proxy->lpvtbl[num] = xasm;
1721 #else
1722 FIXME("not implemented on non i386\n");
1723 return E_FAIL;
1724 #endif
1725 return S_OK;
1728 static HRESULT WINAPI
1729 PSFacBuf_CreateProxy(
1730 LPPSFACTORYBUFFER iface, IUnknown* pUnkOuter, REFIID riid,
1731 IRpcProxyBuffer **ppProxy, LPVOID *ppv)
1733 HRESULT hres;
1734 ITypeInfo *tinfo;
1735 unsigned int i, nroffuncs;
1736 TMProxyImpl *proxy;
1737 TYPEATTR *typeattr;
1738 BOOL defer_to_dispatch = FALSE;
1740 TRACE("(...%s...)\n",debugstr_guid(riid));
1741 hres = _get_typeinfo_for_iid(riid,&tinfo);
1742 if (hres) {
1743 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
1744 return hres;
1747 hres = num_of_funcs(tinfo, &nroffuncs);
1748 if (FAILED(hres)) {
1749 ERR("Cannot get number of functions for typeinfo %s\n",debugstr_guid(riid));
1750 ITypeInfo_Release(tinfo);
1751 return hres;
1754 proxy = CoTaskMemAlloc(sizeof(TMProxyImpl));
1755 if (!proxy) return E_OUTOFMEMORY;
1757 assert(sizeof(TMAsmProxy) == 12);
1759 proxy->dispatch = NULL;
1760 proxy->dispatch_proxy = NULL;
1761 proxy->outerunknown = pUnkOuter;
1762 proxy->asmstubs = VirtualAlloc(NULL, sizeof(TMAsmProxy) * nroffuncs, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
1763 if (!proxy->asmstubs) {
1764 ERR("Could not commit pages for proxy thunks\n");
1765 CoTaskMemFree(proxy);
1766 return E_OUTOFMEMORY;
1768 proxy->lpvtbl2 = &tmproxyvtable;
1769 /* one reference for the proxy */
1770 proxy->ref = 1;
1771 proxy->tinfo = tinfo;
1772 memcpy(&proxy->iid,riid,sizeof(*riid));
1773 proxy->chanbuf = 0;
1775 InitializeCriticalSection(&proxy->crit);
1776 proxy->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TMProxyImpl.crit");
1778 proxy->lpvtbl = HeapAlloc(GetProcessHeap(),0,sizeof(LPBYTE)*nroffuncs);
1780 /* if we derive from IDispatch then defer to its proxy for its methods */
1781 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
1782 if (hres == S_OK)
1784 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
1786 IPSFactoryBuffer *factory_buffer;
1787 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
1788 if (hres == S_OK)
1790 hres = IPSFactoryBuffer_CreateProxy(factory_buffer, NULL,
1791 &IID_IDispatch, &proxy->dispatch_proxy,
1792 (void **)&proxy->dispatch);
1793 IPSFactoryBuffer_Release(factory_buffer);
1795 if ((hres == S_OK) && (nroffuncs < 7))
1797 ERR("nroffuncs calculated incorrectly (%d)\n", nroffuncs);
1798 hres = E_UNEXPECTED;
1800 if (hres == S_OK)
1802 defer_to_dispatch = TRUE;
1805 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
1808 for (i=0;i<nroffuncs;i++) {
1809 switch (i) {
1810 case 0:
1811 proxy->lpvtbl[i] = ProxyIUnknown_QueryInterface;
1812 break;
1813 case 1:
1814 proxy->lpvtbl[i] = ProxyIUnknown_AddRef;
1815 break;
1816 case 2:
1817 proxy->lpvtbl[i] = ProxyIUnknown_Release;
1818 break;
1819 case 3:
1820 if(!defer_to_dispatch)
1822 hres = init_proxy_entry_point(proxy, i);
1823 if(FAILED(hres)) return hres;
1825 else proxy->lpvtbl[3] = ProxyIDispatch_GetTypeInfoCount;
1826 break;
1827 case 4:
1828 if(!defer_to_dispatch)
1830 hres = init_proxy_entry_point(proxy, i);
1831 if(FAILED(hres)) return hres;
1833 else proxy->lpvtbl[4] = ProxyIDispatch_GetTypeInfo;
1834 break;
1835 case 5:
1836 if(!defer_to_dispatch)
1838 hres = init_proxy_entry_point(proxy, i);
1839 if(FAILED(hres)) return hres;
1841 else proxy->lpvtbl[5] = ProxyIDispatch_GetIDsOfNames;
1842 break;
1843 case 6:
1844 if(!defer_to_dispatch)
1846 hres = init_proxy_entry_point(proxy, i);
1847 if(FAILED(hres)) return hres;
1849 else proxy->lpvtbl[6] = ProxyIDispatch_Invoke;
1850 break;
1851 default:
1852 hres = init_proxy_entry_point(proxy, i);
1853 if(FAILED(hres)) return hres;
1857 if (hres == S_OK)
1859 *ppv = (LPVOID)proxy;
1860 *ppProxy = (IRpcProxyBuffer *)&(proxy->lpvtbl2);
1861 IUnknown_AddRef((IUnknown *)*ppv);
1862 return S_OK;
1864 else
1865 TMProxyImpl_Release((IRpcProxyBuffer *)&proxy->lpvtbl2);
1866 return hres;
1869 typedef struct _TMStubImpl {
1870 const IRpcStubBufferVtbl *lpvtbl;
1871 LONG ref;
1873 LPUNKNOWN pUnk;
1874 ITypeInfo *tinfo;
1875 IID iid;
1876 IRpcStubBuffer *dispatch_stub;
1877 BOOL dispatch_derivative;
1878 } TMStubImpl;
1880 static HRESULT WINAPI
1881 TMStubImpl_QueryInterface(LPRPCSTUBBUFFER iface, REFIID riid, LPVOID *ppv)
1883 if (IsEqualIID(riid,&IID_IRpcStubBuffer)||IsEqualIID(riid,&IID_IUnknown)){
1884 *ppv = (LPVOID)iface;
1885 IRpcStubBuffer_AddRef(iface);
1886 return S_OK;
1888 FIXME("%s, not supported IID.\n",debugstr_guid(riid));
1889 return E_NOINTERFACE;
1892 static ULONG WINAPI
1893 TMStubImpl_AddRef(LPRPCSTUBBUFFER iface)
1895 TMStubImpl *This = (TMStubImpl *)iface;
1896 ULONG refCount = InterlockedIncrement(&This->ref);
1898 TRACE("(%p)->(ref before=%u)\n", This, refCount - 1);
1900 return refCount;
1903 static ULONG WINAPI
1904 TMStubImpl_Release(LPRPCSTUBBUFFER iface)
1906 TMStubImpl *This = (TMStubImpl *)iface;
1907 ULONG refCount = InterlockedDecrement(&This->ref);
1909 TRACE("(%p)->(ref before=%u)\n", This, refCount + 1);
1911 if (!refCount)
1913 IRpcStubBuffer_Disconnect(iface);
1914 ITypeInfo_Release(This->tinfo);
1915 if (This->dispatch_stub)
1916 IRpcStubBuffer_Release(This->dispatch_stub);
1917 CoTaskMemFree(This);
1919 return refCount;
1922 static HRESULT WINAPI
1923 TMStubImpl_Connect(LPRPCSTUBBUFFER iface, LPUNKNOWN pUnkServer)
1925 TMStubImpl *This = (TMStubImpl *)iface;
1927 TRACE("(%p)->(%p)\n", This, pUnkServer);
1929 IUnknown_AddRef(pUnkServer);
1930 This->pUnk = pUnkServer;
1932 if (This->dispatch_stub)
1933 IRpcStubBuffer_Connect(This->dispatch_stub, pUnkServer);
1935 return S_OK;
1938 static void WINAPI
1939 TMStubImpl_Disconnect(LPRPCSTUBBUFFER iface)
1941 TMStubImpl *This = (TMStubImpl *)iface;
1943 TRACE("(%p)->()\n", This);
1945 if (This->pUnk)
1947 IUnknown_Release(This->pUnk);
1948 This->pUnk = NULL;
1951 if (This->dispatch_stub)
1952 IRpcStubBuffer_Disconnect(This->dispatch_stub);
1955 static HRESULT WINAPI
1956 TMStubImpl_Invoke(
1957 LPRPCSTUBBUFFER iface, RPCOLEMESSAGE* xmsg,IRpcChannelBuffer*rpcchanbuf)
1959 int i;
1960 const FUNCDESC *fdesc;
1961 TMStubImpl *This = (TMStubImpl *)iface;
1962 HRESULT hres;
1963 DWORD *args = NULL, res, *xargs, nrofargs;
1964 marshal_state buf;
1965 UINT nrofnames = 0;
1966 BSTR names[10];
1967 BSTR iname = NULL;
1968 ITypeInfo *tinfo = NULL;
1970 TRACE("...\n");
1972 if (xmsg->iMethod < 3) {
1973 ERR("IUnknown methods cannot be marshaled by the typelib marshaler\n");
1974 return E_UNEXPECTED;
1977 if (This->dispatch_derivative && xmsg->iMethod < sizeof(IDispatchVtbl)/sizeof(void *))
1979 IPSFactoryBuffer *factory_buffer;
1980 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
1981 if (hres == S_OK)
1983 hres = IPSFactoryBuffer_CreateStub(factory_buffer, &IID_IDispatch,
1984 This->pUnk, &This->dispatch_stub);
1985 IPSFactoryBuffer_Release(factory_buffer);
1987 if (hres != S_OK)
1988 return hres;
1989 return IRpcStubBuffer_Invoke(This->dispatch_stub, xmsg, rpcchanbuf);
1992 memset(&buf,0,sizeof(buf));
1993 buf.size = xmsg->cbBuffer;
1994 buf.base = HeapAlloc(GetProcessHeap(), 0, xmsg->cbBuffer);
1995 memcpy(buf.base, xmsg->Buffer, xmsg->cbBuffer);
1996 buf.curoff = 0;
1998 hres = get_funcdesc(This->tinfo,xmsg->iMethod,&tinfo,&fdesc,&iname,NULL,NULL);
1999 if (hres) {
2000 ERR("GetFuncDesc on method %d failed with %x\n",xmsg->iMethod,hres);
2001 return hres;
2004 if (iname && !lstrcmpW(iname, IDispatchW))
2006 ERR("IDispatch cannot be marshaled by the typelib marshaler\n");
2007 hres = E_UNEXPECTED;
2008 SysFreeString (iname);
2009 goto exit;
2012 if (iname) SysFreeString (iname);
2014 /* Need them for hack below */
2015 memset(names,0,sizeof(names));
2016 ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames);
2017 if (nrofnames > sizeof(names)/sizeof(names[0])) {
2018 ERR("Need more names!\n");
2021 /*dump_FUNCDESC(fdesc);*/
2022 nrofargs = 0;
2023 for (i=0;i<fdesc->cParams;i++)
2024 nrofargs += _argsize(fdesc->lprgelemdescParam[i].tdesc.vt);
2025 args = HeapAlloc(GetProcessHeap(),0,(nrofargs+1)*sizeof(DWORD));
2026 if (!args)
2028 hres = E_OUTOFMEMORY;
2029 goto exit;
2032 /* Allocate all stuff used by call. */
2033 xargs = args+1;
2034 for (i=0;i<fdesc->cParams;i++) {
2035 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2037 hres = deserialize_param(
2038 tinfo,
2039 is_in_elem(elem),
2040 FALSE,
2041 TRUE,
2042 &(elem->tdesc),
2043 xargs,
2044 &buf
2046 xargs += _argsize(elem->tdesc.vt);
2047 if (hres) {
2048 ERR("Failed to deserialize param %s, hres %x\n",relaystr(names[i+1]),hres);
2049 break;
2053 args[0] = (DWORD)This->pUnk;
2055 __TRY
2057 res = _invoke(
2058 (*((FARPROC**)args[0]))[fdesc->oVft/4],
2059 fdesc->callconv,
2060 (xargs-args),
2061 args
2064 __EXCEPT(NULL)
2066 DWORD dwExceptionCode = GetExceptionCode();
2067 ERR("invoke call failed with exception 0x%08x (%d)\n", dwExceptionCode, dwExceptionCode);
2068 if (FAILED(dwExceptionCode))
2069 hres = dwExceptionCode;
2070 else
2071 hres = HRESULT_FROM_WIN32(dwExceptionCode);
2073 __ENDTRY
2075 if (hres != S_OK)
2076 goto exit;
2078 buf.curoff = 0;
2080 xargs = args+1;
2081 for (i=0;i<fdesc->cParams;i++) {
2082 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2083 hres = serialize_param(
2084 tinfo,
2085 is_out_elem(elem),
2086 FALSE,
2087 TRUE,
2088 &elem->tdesc,
2089 xargs,
2090 &buf
2092 xargs += _argsize(elem->tdesc.vt);
2093 if (hres) {
2094 ERR("Failed to stuballoc param, hres %x\n",hres);
2095 break;
2099 hres = xbuf_add (&buf, (LPBYTE)&res, sizeof(DWORD));
2101 if (hres != S_OK)
2102 goto exit;
2104 xmsg->cbBuffer = buf.curoff;
2105 hres = IRpcChannelBuffer_GetBuffer(rpcchanbuf, xmsg, &This->iid);
2106 if (hres != S_OK)
2107 ERR("IRpcChannelBuffer_GetBuffer failed with error 0x%08x\n", hres);
2109 if (hres == S_OK)
2110 memcpy(xmsg->Buffer, buf.base, buf.curoff);
2112 exit:
2113 for (i = 0; i < nrofnames; i++)
2114 SysFreeString(names[i]);
2116 ITypeInfo_Release(tinfo);
2117 HeapFree(GetProcessHeap(), 0, args);
2119 HeapFree(GetProcessHeap(), 0, buf.base);
2121 TRACE("returning\n");
2122 return hres;
2125 static LPRPCSTUBBUFFER WINAPI
2126 TMStubImpl_IsIIDSupported(LPRPCSTUBBUFFER iface, REFIID riid) {
2127 FIXME("Huh (%s)?\n",debugstr_guid(riid));
2128 return NULL;
2131 static ULONG WINAPI
2132 TMStubImpl_CountRefs(LPRPCSTUBBUFFER iface) {
2133 TMStubImpl *This = (TMStubImpl *)iface;
2135 FIXME("()\n");
2136 return This->ref; /*FIXME? */
2139 static HRESULT WINAPI
2140 TMStubImpl_DebugServerQueryInterface(LPRPCSTUBBUFFER iface, LPVOID *ppv) {
2141 return E_NOTIMPL;
2144 static void WINAPI
2145 TMStubImpl_DebugServerRelease(LPRPCSTUBBUFFER iface, LPVOID ppv) {
2146 return;
2149 static const IRpcStubBufferVtbl tmstubvtbl = {
2150 TMStubImpl_QueryInterface,
2151 TMStubImpl_AddRef,
2152 TMStubImpl_Release,
2153 TMStubImpl_Connect,
2154 TMStubImpl_Disconnect,
2155 TMStubImpl_Invoke,
2156 TMStubImpl_IsIIDSupported,
2157 TMStubImpl_CountRefs,
2158 TMStubImpl_DebugServerQueryInterface,
2159 TMStubImpl_DebugServerRelease
2162 static HRESULT WINAPI
2163 PSFacBuf_CreateStub(
2164 LPPSFACTORYBUFFER iface, REFIID riid,IUnknown *pUnkServer,
2165 IRpcStubBuffer** ppStub
2167 HRESULT hres;
2168 ITypeInfo *tinfo;
2169 TMStubImpl *stub;
2170 TYPEATTR *typeattr;
2172 TRACE("(%s,%p,%p)\n",debugstr_guid(riid),pUnkServer,ppStub);
2174 hres = _get_typeinfo_for_iid(riid,&tinfo);
2175 if (hres) {
2176 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
2177 return hres;
2180 stub = CoTaskMemAlloc(sizeof(TMStubImpl));
2181 if (!stub)
2182 return E_OUTOFMEMORY;
2183 stub->lpvtbl = &tmstubvtbl;
2184 stub->ref = 1;
2185 stub->tinfo = tinfo;
2186 stub->dispatch_stub = NULL;
2187 stub->dispatch_derivative = FALSE;
2188 memcpy(&(stub->iid),riid,sizeof(*riid));
2189 hres = IRpcStubBuffer_Connect((LPRPCSTUBBUFFER)stub,pUnkServer);
2190 *ppStub = (LPRPCSTUBBUFFER)stub;
2191 TRACE("IRpcStubBuffer: %p\n", stub);
2192 if (hres)
2193 ERR("Connect to pUnkServer failed?\n");
2195 /* if we derive from IDispatch then defer to its stub for some of its methods */
2196 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
2197 if (hres == S_OK)
2199 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
2200 stub->dispatch_derivative = TRUE;
2201 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
2204 return hres;
2207 static const IPSFactoryBufferVtbl psfacbufvtbl = {
2208 PSFacBuf_QueryInterface,
2209 PSFacBuf_AddRef,
2210 PSFacBuf_Release,
2211 PSFacBuf_CreateProxy,
2212 PSFacBuf_CreateStub
2215 /* This is the whole PSFactoryBuffer object, just the vtableptr */
2216 static const IPSFactoryBufferVtbl *lppsfac = &psfacbufvtbl;
2218 /***********************************************************************
2219 * TMARSHAL_DllGetClassObject
2221 HRESULT TMARSHAL_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
2223 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)) {
2224 *ppv = &lppsfac;
2225 return S_OK;
2227 return E_NOINTERFACE;