oleaut32: Return FALSE from SystemTimeToVariantTime if day > 31 or year is negative.
[wine.git] / dlls / oleaut32 / tmarshal.c
blob3856f32fba80a6d7e190f2435fe91a78eca6bd67
1 /*
2 * TYPELIB Marshaler
4 * Copyright 2002,2005 Marcus Meissner
6 * The olerelay debug channel allows you to see calls marshalled by
7 * the typelib marshaller. It is not a generic COM relaying system.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "config.h"
25 #include "wine/port.h"
27 #include <assert.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <ctype.h>
34 #define COBJMACROS
35 #define NONAMELESSUNION
36 #define NONAMELESSSTRUCT
38 #include "winerror.h"
39 #include "windef.h"
40 #include "winbase.h"
41 #include "winnls.h"
42 #include "winreg.h"
43 #include "winuser.h"
45 #include "ole2.h"
46 #include "propidl.h" /* for LPSAFEARRAY_User* functions */
47 #include "typelib.h"
48 #include "variant.h"
49 #include "wine/debug.h"
50 #include "wine/exception.h"
52 static const WCHAR IDispatchW[] = { 'I','D','i','s','p','a','t','c','h',0};
54 WINE_DEFAULT_DEBUG_CHANNEL(ole);
55 WINE_DECLARE_DEBUG_CHANNEL(olerelay);
57 static HRESULT TMarshalDispatchChannel_Create(
58 IRpcChannelBuffer *pDelegateChannel, REFIID tmarshal_riid,
59 IRpcChannelBuffer **ppChannel);
61 typedef struct _marshal_state {
62 LPBYTE base;
63 int size;
64 int curoff;
65 } marshal_state;
67 /* used in the olerelay code to avoid having the L"" stuff added by debugstr_w */
68 static char *relaystr(WCHAR *in) {
69 char *tmp = (char *)debugstr_w(in);
70 tmp += 2;
71 tmp[strlen(tmp)-1] = '\0';
72 return tmp;
75 static HRESULT
76 xbuf_resize(marshal_state *buf, DWORD newsize)
78 if(buf->size >= newsize)
79 return S_FALSE;
81 if(buf->base)
83 buf->base = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buf->base, newsize);
84 if(!buf->base)
85 return E_OUTOFMEMORY;
87 else
89 buf->base = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, newsize);
90 if(!buf->base)
91 return E_OUTOFMEMORY;
93 buf->size = newsize;
94 return S_OK;
97 static HRESULT
98 xbuf_add(marshal_state *buf, const BYTE *stuff, DWORD size)
100 HRESULT hr;
102 if(buf->size - buf->curoff < size)
104 hr = xbuf_resize(buf, buf->size + size + 100);
105 if(FAILED(hr)) return hr;
107 memcpy(buf->base+buf->curoff,stuff,size);
108 buf->curoff += size;
109 return S_OK;
112 static HRESULT
113 xbuf_get(marshal_state *buf, LPBYTE stuff, DWORD size) {
114 if (buf->size < buf->curoff+size) return E_FAIL;
115 memcpy(stuff,buf->base+buf->curoff,size);
116 buf->curoff += size;
117 return S_OK;
120 static HRESULT
121 xbuf_skip(marshal_state *buf, DWORD size) {
122 if (buf->size < buf->curoff+size) return E_FAIL;
123 buf->curoff += size;
124 return S_OK;
127 static HRESULT
128 _unmarshal_interface(marshal_state *buf, REFIID riid, LPUNKNOWN *pUnk) {
129 IStream *pStm;
130 ULARGE_INTEGER newpos;
131 LARGE_INTEGER seekto;
132 ULONG res;
133 HRESULT hres;
134 DWORD xsize;
136 TRACE("...%s...\n",debugstr_guid(riid));
138 *pUnk = NULL;
139 hres = xbuf_get(buf,(LPBYTE)&xsize,sizeof(xsize));
140 if (hres) {
141 ERR("xbuf_get failed\n");
142 return hres;
145 if (xsize == 0) return S_OK;
147 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
148 if (hres) {
149 ERR("Stream create failed %x\n",hres);
150 return hres;
153 hres = IStream_Write(pStm,buf->base+buf->curoff,xsize,&res);
154 if (hres) {
155 ERR("stream write %x\n",hres);
156 IStream_Release(pStm);
157 return hres;
160 memset(&seekto,0,sizeof(seekto));
161 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
162 if (hres) {
163 ERR("Failed Seek %x\n",hres);
164 IStream_Release(pStm);
165 return hres;
168 hres = CoUnmarshalInterface(pStm,riid,(LPVOID*)pUnk);
169 if (hres) {
170 ERR("Unmarshalling interface %s failed with %x\n",debugstr_guid(riid),hres);
171 IStream_Release(pStm);
172 return hres;
175 IStream_Release(pStm);
176 return xbuf_skip(buf,xsize);
179 static HRESULT
180 _marshal_interface(marshal_state *buf, REFIID riid, LPUNKNOWN pUnk) {
181 LPBYTE tempbuf = NULL;
182 IStream *pStm = NULL;
183 STATSTG ststg;
184 ULARGE_INTEGER newpos;
185 LARGE_INTEGER seekto;
186 ULONG res;
187 DWORD xsize;
188 HRESULT hres;
190 if (!pUnk) {
191 /* this is valid, if for instance we serialize
192 * a VT_DISPATCH with NULL ptr which apparently
193 * can happen. S_OK to make sure we continue
194 * serializing.
196 WARN("pUnk is NULL\n");
197 xsize = 0;
198 return xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
201 hres = E_FAIL;
203 TRACE("...%s...\n",debugstr_guid(riid));
205 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
206 if (hres) {
207 ERR("Stream create failed %x\n",hres);
208 goto fail;
211 hres = CoMarshalInterface(pStm,riid,pUnk,0,NULL,0);
212 if (hres) {
213 ERR("Marshalling interface %s failed with %x\n", debugstr_guid(riid), hres);
214 goto fail;
217 hres = IStream_Stat(pStm,&ststg,STATFLAG_NONAME);
218 if (hres) {
219 ERR("Stream stat failed\n");
220 goto fail;
223 tempbuf = HeapAlloc(GetProcessHeap(), 0, ststg.cbSize.u.LowPart);
224 memset(&seekto,0,sizeof(seekto));
225 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
226 if (hres) {
227 ERR("Failed Seek %x\n",hres);
228 goto fail;
231 hres = IStream_Read(pStm,tempbuf,ststg.cbSize.u.LowPart,&res);
232 if (hres) {
233 ERR("Failed Read %x\n",hres);
234 goto fail;
237 xsize = ststg.cbSize.u.LowPart;
238 xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
239 hres = xbuf_add(buf,tempbuf,ststg.cbSize.u.LowPart);
241 HeapFree(GetProcessHeap(),0,tempbuf);
242 IStream_Release(pStm);
244 return hres;
246 fail:
247 xsize = 0;
248 xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
249 if (pStm) IStream_Release(pStm);
250 HeapFree(GetProcessHeap(), 0, tempbuf);
251 return hres;
254 /********************* OLE Proxy/Stub Factory ********************************/
255 static HRESULT WINAPI
256 PSFacBuf_QueryInterface(LPPSFACTORYBUFFER iface, REFIID iid, LPVOID *ppv) {
257 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)||IsEqualIID(iid,&IID_IUnknown)) {
258 *ppv = iface;
259 /* No ref counting, static class */
260 return S_OK;
262 FIXME("(%s) unknown IID?\n",debugstr_guid(iid));
263 return E_NOINTERFACE;
266 static ULONG WINAPI PSFacBuf_AddRef(LPPSFACTORYBUFFER iface) { return 2; }
267 static ULONG WINAPI PSFacBuf_Release(LPPSFACTORYBUFFER iface) { return 1; }
269 static HRESULT
270 _get_typeinfo_for_iid(REFIID riid, ITypeInfo**ti) {
271 HRESULT hres;
272 HKEY ikey;
273 char tlguid[200],typelibkey[300],interfacekey[300],ver[100];
274 char tlfn[260];
275 OLECHAR tlfnW[260];
276 DWORD tlguidlen, verlen, type;
277 LONG tlfnlen;
278 ITypeLib *tl;
280 sprintf( interfacekey, "Interface\\{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\Typelib",
281 riid->Data1, riid->Data2, riid->Data3,
282 riid->Data4[0], riid->Data4[1], riid->Data4[2], riid->Data4[3],
283 riid->Data4[4], riid->Data4[5], riid->Data4[6], riid->Data4[7]
286 if (RegOpenKeyA(HKEY_CLASSES_ROOT,interfacekey,&ikey)) {
287 ERR("No %s key found.\n",interfacekey);
288 return E_FAIL;
290 tlguidlen = sizeof(tlguid);
291 if (RegQueryValueExA(ikey,NULL,NULL,&type,(LPBYTE)tlguid,&tlguidlen)) {
292 ERR("Getting typelib guid failed.\n");
293 RegCloseKey(ikey);
294 return E_FAIL;
296 verlen = sizeof(ver);
297 if (RegQueryValueExA(ikey,"Version",NULL,&type,(LPBYTE)ver,&verlen)) {
298 ERR("Could not get version value?\n");
299 RegCloseKey(ikey);
300 return E_FAIL;
302 RegCloseKey(ikey);
303 sprintf(typelibkey,"Typelib\\%s\\%s\\0\\win%u",tlguid,ver,(sizeof(void*) == 8) ? 64 : 32);
304 tlfnlen = sizeof(tlfn);
305 if (RegQueryValueA(HKEY_CLASSES_ROOT,typelibkey,tlfn,&tlfnlen)) {
306 #ifdef _WIN64
307 sprintf(typelibkey,"Typelib\\%s\\%s\\0\\win32",tlguid,ver);
308 tlfnlen = sizeof(tlfn);
309 if (RegQueryValueA(HKEY_CLASSES_ROOT,typelibkey,tlfn,&tlfnlen)) {
310 #endif
311 ERR("Could not get typelib fn?\n");
312 return E_FAIL;
313 #ifdef _WIN64
315 #endif
317 MultiByteToWideChar(CP_ACP, 0, tlfn, -1, tlfnW, sizeof(tlfnW) / sizeof(tlfnW[0]));
318 hres = LoadTypeLib(tlfnW,&tl);
319 if (hres) {
320 ERR("Failed to load typelib for %s, but it should be there.\n",debugstr_guid(riid));
321 return hres;
323 hres = ITypeLib_GetTypeInfoOfGuid(tl,riid,ti);
324 if (hres) {
325 ERR("typelib does not contain info for %s?\n",debugstr_guid(riid));
326 ITypeLib_Release(tl);
327 return hres;
329 ITypeLib_Release(tl);
330 return hres;
334 * Determine the number of functions including all inherited functions
335 * and well as the size of the vtbl.
336 * Note for non-dual dispinterfaces we simply return the size of IDispatch.
338 static HRESULT num_of_funcs(ITypeInfo *tinfo, unsigned int *num,
339 unsigned int *vtbl_size)
341 HRESULT hr;
342 TYPEATTR *attr;
343 ITypeInfo *tinfo2;
344 UINT inherited_funcs = 0, i;
346 *num = 0;
347 if(vtbl_size) *vtbl_size = 0;
349 hr = ITypeInfo_GetTypeAttr(tinfo, &attr);
350 if (hr)
352 ERR("GetTypeAttr failed with %x\n", hr);
353 return hr;
356 if(attr->typekind == TKIND_DISPATCH)
358 if(attr->wTypeFlags & TYPEFLAG_FDUAL)
360 HREFTYPE href;
362 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
363 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, -1, &href);
364 if(FAILED(hr))
366 ERR("Unable to get interface href from dual dispinterface\n");
367 return hr;
369 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
370 if(FAILED(hr))
372 ERR("Unable to get interface from dual dispinterface\n");
373 return hr;
375 hr = num_of_funcs(tinfo2, num, vtbl_size);
376 ITypeInfo_Release(tinfo2);
377 return hr;
379 else /* non-dual dispinterface */
381 /* These will be the size of IDispatchVtbl */
382 *num = attr->cbSizeVft / sizeof(void *);
383 if(vtbl_size) *vtbl_size = attr->cbSizeVft;
384 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
385 return hr;
389 for (i = 0; i < attr->cImplTypes; i++)
391 HREFTYPE href;
392 ITypeInfo *pSubTypeInfo;
393 UINT sub_funcs;
395 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, i, &href);
396 if (FAILED(hr)) goto end;
397 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &pSubTypeInfo);
398 if (FAILED(hr)) goto end;
400 hr = num_of_funcs(pSubTypeInfo, &sub_funcs, NULL);
401 ITypeInfo_Release(pSubTypeInfo);
403 if(FAILED(hr)) goto end;
404 inherited_funcs += sub_funcs;
407 *num = inherited_funcs + attr->cFuncs;
408 if(vtbl_size) *vtbl_size = attr->cbSizeVft;
410 end:
411 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
412 return hr;
415 #ifdef __i386__
417 #include "pshpack1.h"
419 typedef struct _TMAsmProxy {
420 DWORD lealeax;
421 BYTE pushleax;
422 BYTE pushlval;
423 DWORD nr;
424 BYTE lcall;
425 DWORD xcall;
426 BYTE lret;
427 WORD bytestopop;
428 WORD nop;
429 } TMAsmProxy;
431 #include "poppack.h"
433 #else /* __i386__ */
434 # warning You need to implement stubless proxies for your architecture
435 typedef struct _TMAsmProxy {
436 } TMAsmProxy;
437 #endif
439 typedef struct _TMProxyImpl {
440 LPVOID *lpvtbl;
441 IRpcProxyBuffer IRpcProxyBuffer_iface;
442 LONG ref;
444 TMAsmProxy *asmstubs;
445 ITypeInfo* tinfo;
446 IRpcChannelBuffer* chanbuf;
447 IID iid;
448 CRITICAL_SECTION crit;
449 IUnknown *outerunknown;
450 IDispatch *dispatch;
451 IRpcProxyBuffer *dispatch_proxy;
452 } TMProxyImpl;
454 static inline TMProxyImpl *impl_from_IRpcProxyBuffer( IRpcProxyBuffer *iface )
456 return CONTAINING_RECORD(iface, TMProxyImpl, IRpcProxyBuffer_iface);
459 static HRESULT WINAPI
460 TMProxyImpl_QueryInterface(LPRPCPROXYBUFFER iface, REFIID riid, LPVOID *ppv)
462 TRACE("()\n");
463 if (IsEqualIID(riid,&IID_IUnknown)||IsEqualIID(riid,&IID_IRpcProxyBuffer)) {
464 *ppv = iface;
465 IRpcProxyBuffer_AddRef(iface);
466 return S_OK;
468 FIXME("no interface for %s\n",debugstr_guid(riid));
469 return E_NOINTERFACE;
472 static ULONG WINAPI
473 TMProxyImpl_AddRef(LPRPCPROXYBUFFER iface)
475 TMProxyImpl *This = impl_from_IRpcProxyBuffer( iface );
476 ULONG refCount = InterlockedIncrement(&This->ref);
478 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
480 return refCount;
483 static ULONG WINAPI
484 TMProxyImpl_Release(LPRPCPROXYBUFFER iface)
486 TMProxyImpl *This = impl_from_IRpcProxyBuffer( iface );
487 ULONG refCount = InterlockedDecrement(&This->ref);
489 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
491 if (!refCount)
493 if (This->dispatch_proxy) IRpcProxyBuffer_Release(This->dispatch_proxy);
494 This->crit.DebugInfo->Spare[0] = 0;
495 DeleteCriticalSection(&This->crit);
496 if (This->chanbuf) IRpcChannelBuffer_Release(This->chanbuf);
497 VirtualFree(This->asmstubs, 0, MEM_RELEASE);
498 HeapFree(GetProcessHeap(), 0, This->lpvtbl);
499 ITypeInfo_Release(This->tinfo);
500 CoTaskMemFree(This);
502 return refCount;
505 static HRESULT WINAPI
506 TMProxyImpl_Connect(
507 LPRPCPROXYBUFFER iface,IRpcChannelBuffer* pRpcChannelBuffer)
509 TMProxyImpl *This = impl_from_IRpcProxyBuffer( iface );
511 TRACE("(%p)\n", pRpcChannelBuffer);
513 EnterCriticalSection(&This->crit);
515 IRpcChannelBuffer_AddRef(pRpcChannelBuffer);
516 This->chanbuf = pRpcChannelBuffer;
518 LeaveCriticalSection(&This->crit);
520 if (This->dispatch_proxy)
522 IRpcChannelBuffer *pDelegateChannel;
523 HRESULT hr = TMarshalDispatchChannel_Create(pRpcChannelBuffer, &This->iid, &pDelegateChannel);
524 if (FAILED(hr))
525 return hr;
526 hr = IRpcProxyBuffer_Connect(This->dispatch_proxy, pDelegateChannel);
527 IRpcChannelBuffer_Release(pDelegateChannel);
528 return hr;
531 return S_OK;
534 static void WINAPI
535 TMProxyImpl_Disconnect(LPRPCPROXYBUFFER iface)
537 TMProxyImpl *This = impl_from_IRpcProxyBuffer( iface );
539 TRACE("()\n");
541 EnterCriticalSection(&This->crit);
543 IRpcChannelBuffer_Release(This->chanbuf);
544 This->chanbuf = NULL;
546 LeaveCriticalSection(&This->crit);
548 if (This->dispatch_proxy)
549 IRpcProxyBuffer_Disconnect(This->dispatch_proxy);
553 static const IRpcProxyBufferVtbl tmproxyvtable = {
554 TMProxyImpl_QueryInterface,
555 TMProxyImpl_AddRef,
556 TMProxyImpl_Release,
557 TMProxyImpl_Connect,
558 TMProxyImpl_Disconnect
561 /* how much space do we use on stack in DWORD steps. */
562 static int
563 _argsize(TYPEDESC *tdesc, ITypeInfo *tinfo) {
564 switch (tdesc->vt) {
565 case VT_I8:
566 case VT_UI8:
567 return 8/sizeof(DWORD);
568 case VT_R8:
569 return sizeof(double)/sizeof(DWORD);
570 case VT_CY:
571 return sizeof(CY)/sizeof(DWORD);
572 case VT_DATE:
573 return sizeof(DATE)/sizeof(DWORD);
574 case VT_DECIMAL:
575 return (sizeof(DECIMAL)+3)/sizeof(DWORD);
576 case VT_VARIANT:
577 return (sizeof(VARIANT)+3)/sizeof(DWORD);
578 case VT_USERDEFINED:
580 ITypeInfo *tinfo2;
581 TYPEATTR *tattr;
582 HRESULT hres;
583 DWORD ret;
585 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
586 if (FAILED(hres))
587 return 0; /* should fail critically in serialize_param */
588 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
589 ret = (tattr->cbSizeInstance+3)/sizeof(DWORD);
590 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
591 ITypeInfo_Release(tinfo2);
592 return ret;
594 default:
595 return 1;
599 /* how much space do we use on the heap (in bytes) */
600 static int
601 _xsize(const TYPEDESC *td, ITypeInfo *tinfo) {
602 switch (td->vt) {
603 case VT_DATE:
604 return sizeof(DATE);
605 case VT_CY:
606 return sizeof(CY);
607 case VT_VARIANT:
608 return sizeof(VARIANT);
609 case VT_CARRAY: {
610 int i, arrsize = 1;
611 const ARRAYDESC *adesc = td->u.lpadesc;
613 for (i=0;i<adesc->cDims;i++)
614 arrsize *= adesc->rgbounds[i].cElements;
615 return arrsize*_xsize(&adesc->tdescElem, tinfo);
617 case VT_UI8:
618 case VT_I8:
619 case VT_R8:
620 return 8;
621 case VT_UI2:
622 case VT_I2:
623 case VT_BOOL:
624 return 2;
625 case VT_UI1:
626 case VT_I1:
627 return 1;
628 case VT_USERDEFINED:
630 ITypeInfo *tinfo2;
631 TYPEATTR *tattr;
632 HRESULT hres;
633 DWORD ret;
635 hres = ITypeInfo_GetRefTypeInfo(tinfo,td->u.hreftype,&tinfo2);
636 if (FAILED(hres))
637 return 0;
638 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
639 ret = tattr->cbSizeInstance;
640 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
641 ITypeInfo_Release(tinfo2);
642 return ret;
644 default:
645 return 4;
649 /* Whether we pass this type by reference or by value */
650 static int
651 _passbyref(const TYPEDESC *td, ITypeInfo *tinfo) {
652 if (td->vt == VT_USERDEFINED ||
653 td->vt == VT_VARIANT ||
654 td->vt == VT_PTR)
655 return 1;
657 return 0;
660 static HRESULT
661 serialize_param(
662 ITypeInfo *tinfo,
663 BOOL writeit,
664 BOOL debugout,
665 BOOL dealloc,
666 TYPEDESC *tdesc,
667 DWORD *arg,
668 marshal_state *buf)
670 HRESULT hres = S_OK;
671 VARTYPE vartype;
673 TRACE("(tdesc.vt %s)\n",debugstr_vt(tdesc->vt));
675 vartype = tdesc->vt;
676 if ((vartype & 0xf000) == VT_ARRAY)
677 vartype = VT_SAFEARRAY;
679 switch (vartype) {
680 case VT_DATE:
681 case VT_I8:
682 case VT_UI8:
683 case VT_R8:
684 case VT_CY:
685 hres = S_OK;
686 if (debugout) TRACE_(olerelay)("%x%x\n",arg[0],arg[1]);
687 if (writeit)
688 hres = xbuf_add(buf,(LPBYTE)arg,8);
689 return hres;
690 case VT_ERROR:
691 case VT_INT:
692 case VT_UINT:
693 case VT_I4:
694 case VT_R4:
695 case VT_UI4:
696 hres = S_OK;
697 if (debugout) TRACE_(olerelay)("%x\n",*arg);
698 if (writeit)
699 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
700 return hres;
701 case VT_I2:
702 case VT_UI2:
703 case VT_BOOL:
704 hres = S_OK;
705 if (debugout) TRACE_(olerelay)("%04x\n",*arg & 0xffff);
706 if (writeit)
707 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
708 return hres;
709 case VT_I1:
710 case VT_UI1:
711 hres = S_OK;
712 if (debugout) TRACE_(olerelay)("%02x\n",*arg & 0xff);
713 if (writeit)
714 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
715 return hres;
716 case VT_VARIANT: {
717 if (debugout) TRACE_(olerelay)("Vt(%s%s)(",debugstr_vt(V_VT((VARIANT *)arg)),debugstr_vf(V_VT((VARIANT *)arg)));
718 if (writeit)
720 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
721 ULONG size = VARIANT_UserSize(&flags, buf->curoff, (VARIANT *)arg);
722 xbuf_resize(buf, size);
723 VARIANT_UserMarshal(&flags, buf->base + buf->curoff, (VARIANT *)arg);
724 buf->curoff = size;
726 if (dealloc)
728 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
729 VARIANT_UserFree(&flags, (VARIANT *)arg);
731 return S_OK;
733 case VT_BSTR: {
734 if (writeit && debugout) {
735 if (*arg)
736 TRACE_(olerelay)("%s",relaystr((WCHAR*)*arg));
737 else
738 TRACE_(olerelay)("<bstr NULL>");
740 if (writeit)
742 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
743 ULONG size = BSTR_UserSize(&flags, buf->curoff, (BSTR *)arg);
744 xbuf_resize(buf, size);
745 BSTR_UserMarshal(&flags, buf->base + buf->curoff, (BSTR *)arg);
746 buf->curoff = size;
748 if (dealloc)
750 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
751 BSTR_UserFree(&flags, (BSTR *)arg);
753 return S_OK;
755 case VT_PTR: {
756 DWORD cookie;
757 BOOL derefhere = TRUE;
759 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
760 ITypeInfo *tinfo2;
761 TYPEATTR *tattr;
763 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
764 if (hres) {
765 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
766 return hres;
768 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
769 switch (tattr->typekind) {
770 case TKIND_ALIAS:
771 if (tattr->tdescAlias.vt == VT_USERDEFINED)
773 DWORD href = tattr->tdescAlias.u.hreftype;
774 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
775 ITypeInfo_Release(tinfo2);
776 hres = ITypeInfo_GetRefTypeInfo(tinfo,href,&tinfo2);
777 if (hres) {
778 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
779 return hres;
781 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
782 derefhere = (tattr->typekind != TKIND_DISPATCH && tattr->typekind != TKIND_INTERFACE);
784 break;
785 case TKIND_ENUM: /* confirmed */
786 case TKIND_RECORD: /* FIXME: mostly untested */
787 break;
788 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
789 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
790 derefhere=FALSE;
791 break;
792 default:
793 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
794 derefhere=FALSE;
795 break;
797 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
798 ITypeInfo_Release(tinfo2);
801 if (debugout) TRACE_(olerelay)("*");
802 /* Write always, so the other side knows when it gets a NULL pointer.
804 cookie = *arg ? 0x42424242 : 0;
805 hres = xbuf_add(buf,(LPBYTE)&cookie,sizeof(cookie));
806 if (hres)
807 return hres;
808 if (!*arg) {
809 if (debugout) TRACE_(olerelay)("NULL");
810 return S_OK;
812 hres = serialize_param(tinfo,writeit,debugout,dealloc,tdesc->u.lptdesc,(DWORD*)*arg,buf);
813 if (derefhere && dealloc) HeapFree(GetProcessHeap(),0,(LPVOID)*arg);
814 return hres;
816 case VT_UNKNOWN:
817 if (debugout) TRACE_(olerelay)("unk(0x%x)",*arg);
818 if (writeit)
819 hres = _marshal_interface(buf,&IID_IUnknown,(LPUNKNOWN)*arg);
820 if (dealloc && *(IUnknown **)arg)
821 IUnknown_Release((LPUNKNOWN)*arg);
822 return hres;
823 case VT_DISPATCH:
824 if (debugout) TRACE_(olerelay)("idisp(0x%x)",*arg);
825 if (writeit)
826 hres = _marshal_interface(buf,&IID_IDispatch,(LPUNKNOWN)*arg);
827 if (dealloc && *(IUnknown **)arg)
828 IUnknown_Release((LPUNKNOWN)*arg);
829 return hres;
830 case VT_VOID:
831 if (debugout) TRACE_(olerelay)("<void>");
832 return S_OK;
833 case VT_USERDEFINED: {
834 ITypeInfo *tinfo2;
835 TYPEATTR *tattr;
837 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
838 if (hres) {
839 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
840 return hres;
842 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
843 switch (tattr->typekind) {
844 case TKIND_DISPATCH:
845 case TKIND_INTERFACE:
846 if (writeit)
847 hres=_marshal_interface(buf,&(tattr->guid),(LPUNKNOWN)arg);
848 if (dealloc)
849 IUnknown_Release((LPUNKNOWN)arg);
850 break;
851 case TKIND_RECORD: {
852 int i;
853 if (debugout) TRACE_(olerelay)("{");
854 for (i=0;i<tattr->cVars;i++) {
855 VARDESC *vdesc;
856 ELEMDESC *elem2;
857 TYPEDESC *tdesc2;
859 hres = ITypeInfo_GetVarDesc(tinfo2, i, &vdesc);
860 if (hres) {
861 ERR("Could not get vardesc of %d\n",i);
862 return hres;
864 elem2 = &vdesc->elemdescVar;
865 tdesc2 = &elem2->tdesc;
866 hres = serialize_param(
867 tinfo2,
868 writeit,
869 debugout,
870 dealloc,
871 tdesc2,
872 (DWORD*)(((LPBYTE)arg)+vdesc->u.oInst),
875 ITypeInfo_ReleaseVarDesc(tinfo2, vdesc);
876 if (hres!=S_OK)
877 return hres;
878 if (debugout && (i<(tattr->cVars-1)))
879 TRACE_(olerelay)(",");
881 if (debugout) TRACE_(olerelay)("}");
882 break;
884 case TKIND_ALIAS:
885 hres = serialize_param(tinfo2,writeit,debugout,dealloc,&tattr->tdescAlias,arg,buf);
886 break;
887 case TKIND_ENUM:
888 hres = S_OK;
889 if (debugout) TRACE_(olerelay)("%x",*arg);
890 if (writeit)
891 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
892 break;
893 default:
894 FIXME("Unhandled typekind %d\n",tattr->typekind);
895 hres = E_FAIL;
896 break;
898 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
899 ITypeInfo_Release(tinfo2);
900 return hres;
902 case VT_CARRAY: {
903 ARRAYDESC *adesc = tdesc->u.lpadesc;
904 int i, arrsize = 1;
906 if (debugout) TRACE_(olerelay)("carr");
907 for (i=0;i<adesc->cDims;i++) {
908 if (debugout) TRACE_(olerelay)("[%d]",adesc->rgbounds[i].cElements);
909 arrsize *= adesc->rgbounds[i].cElements;
911 if (debugout) TRACE_(olerelay)("(vt %s)",debugstr_vt(adesc->tdescElem.vt));
912 if (debugout) TRACE_(olerelay)("[");
913 for (i=0;i<arrsize;i++) {
914 LPBYTE base = _passbyref(&adesc->tdescElem, tinfo) ? (LPBYTE) *arg : (LPBYTE) arg;
915 hres = serialize_param(tinfo, writeit, debugout, dealloc, &adesc->tdescElem, (DWORD*)((LPBYTE)base+i*_xsize(&adesc->tdescElem, tinfo)), buf);
916 if (hres)
917 return hres;
918 if (debugout && (i<arrsize-1)) TRACE_(olerelay)(",");
920 if (debugout) TRACE_(olerelay)("]");
921 if (dealloc)
922 HeapFree(GetProcessHeap(), 0, *(void **)arg);
923 return S_OK;
925 case VT_SAFEARRAY: {
926 if (writeit)
928 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
929 ULONG size = LPSAFEARRAY_UserSize(&flags, buf->curoff, (LPSAFEARRAY *)arg);
930 xbuf_resize(buf, size);
931 LPSAFEARRAY_UserMarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
932 buf->curoff = size;
934 if (dealloc)
936 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
937 LPSAFEARRAY_UserFree(&flags, (LPSAFEARRAY *)arg);
939 return S_OK;
941 default:
942 ERR("Unhandled marshal type %d.\n",tdesc->vt);
943 return S_OK;
947 static HRESULT
948 deserialize_param(
949 ITypeInfo *tinfo,
950 BOOL readit,
951 BOOL debugout,
952 BOOL alloc,
953 TYPEDESC *tdesc,
954 DWORD *arg,
955 marshal_state *buf)
957 HRESULT hres = S_OK;
958 VARTYPE vartype;
960 TRACE("vt %s at %p\n",debugstr_vt(tdesc->vt),arg);
962 vartype = tdesc->vt;
963 if ((vartype & 0xf000) == VT_ARRAY)
964 vartype = VT_SAFEARRAY;
966 while (1) {
967 switch (vartype) {
968 case VT_VARIANT: {
969 if (readit)
971 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
972 unsigned char *buffer;
973 buffer = VARIANT_UserUnmarshal(&flags, buf->base + buf->curoff, (VARIANT *)arg);
974 buf->curoff = buffer - buf->base;
976 return S_OK;
978 case VT_DATE:
979 case VT_I8:
980 case VT_UI8:
981 case VT_R8:
982 case VT_CY:
983 if (readit) {
984 hres = xbuf_get(buf,(LPBYTE)arg,8);
985 if (hres) ERR("Failed to read integer 8 byte\n");
987 if (debugout) TRACE_(olerelay)("%x%x",arg[0],arg[1]);
988 return hres;
989 case VT_ERROR:
990 case VT_I4:
991 case VT_INT:
992 case VT_UINT:
993 case VT_R4:
994 case VT_UI4:
995 if (readit) {
996 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
997 if (hres) ERR("Failed to read integer 4 byte\n");
999 if (debugout) TRACE_(olerelay)("%x",*arg);
1000 return hres;
1001 case VT_I2:
1002 case VT_UI2:
1003 case VT_BOOL:
1004 if (readit) {
1005 DWORD x;
1006 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
1007 if (hres) ERR("Failed to read integer 4 byte\n");
1008 memcpy(arg,&x,2);
1010 if (debugout) TRACE_(olerelay)("%04x",*arg & 0xffff);
1011 return hres;
1012 case VT_I1:
1013 case VT_UI1:
1014 if (readit) {
1015 DWORD x;
1016 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
1017 if (hres) ERR("Failed to read integer 4 byte\n");
1018 memcpy(arg,&x,1);
1020 if (debugout) TRACE_(olerelay)("%02x",*arg & 0xff);
1021 return hres;
1022 case VT_BSTR: {
1023 if (readit)
1025 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
1026 unsigned char *buffer;
1027 buffer = BSTR_UserUnmarshal(&flags, buf->base + buf->curoff, (BSTR *)arg);
1028 buf->curoff = buffer - buf->base;
1029 if (debugout) TRACE_(olerelay)("%s",debugstr_w(*(BSTR *)arg));
1031 return S_OK;
1033 case VT_PTR: {
1034 DWORD cookie;
1035 BOOL derefhere = TRUE;
1037 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
1038 ITypeInfo *tinfo2;
1039 TYPEATTR *tattr;
1041 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
1042 if (hres) {
1043 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
1044 return hres;
1046 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1047 switch (tattr->typekind) {
1048 case TKIND_ALIAS:
1049 if (tattr->tdescAlias.vt == VT_USERDEFINED)
1051 DWORD href = tattr->tdescAlias.u.hreftype;
1052 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
1053 ITypeInfo_Release(tinfo2);
1054 hres = ITypeInfo_GetRefTypeInfo(tinfo,href,&tinfo2);
1055 if (hres) {
1056 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
1057 return hres;
1059 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1060 derefhere = (tattr->typekind != TKIND_DISPATCH && tattr->typekind != TKIND_INTERFACE);
1062 break;
1063 case TKIND_ENUM: /* confirmed */
1064 case TKIND_RECORD: /* FIXME: mostly untested */
1065 break;
1066 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
1067 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
1068 derefhere=FALSE;
1069 break;
1070 default:
1071 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
1072 derefhere=FALSE;
1073 break;
1075 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1076 ITypeInfo_Release(tinfo2);
1078 /* read it in all cases, we need to know if we have
1079 * NULL pointer or not.
1081 hres = xbuf_get(buf,(LPBYTE)&cookie,sizeof(cookie));
1082 if (hres) {
1083 ERR("Failed to load pointer cookie.\n");
1084 return hres;
1086 if (cookie != 0x42424242) {
1087 /* we read a NULL ptr from the remote side */
1088 if (debugout) TRACE_(olerelay)("NULL");
1089 *arg = 0;
1090 return S_OK;
1092 if (debugout) TRACE_(olerelay)("*");
1093 if (alloc) {
1094 /* Allocate space for the referenced struct */
1095 if (derefhere)
1096 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,_xsize(tdesc->u.lptdesc, tinfo));
1098 if (derefhere)
1099 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, (LPDWORD)*arg, buf);
1100 else
1101 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, arg, buf);
1103 case VT_UNKNOWN:
1104 /* FIXME: UNKNOWN is unknown ..., but allocate 4 byte for it */
1105 if (alloc)
1106 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
1107 hres = S_OK;
1108 if (readit)
1109 hres = _unmarshal_interface(buf,&IID_IUnknown,(LPUNKNOWN*)arg);
1110 if (debugout)
1111 TRACE_(olerelay)("unk(%p)",arg);
1112 return hres;
1113 case VT_DISPATCH:
1114 hres = S_OK;
1115 if (readit)
1116 hres = _unmarshal_interface(buf,&IID_IDispatch,(LPUNKNOWN*)arg);
1117 if (debugout)
1118 TRACE_(olerelay)("idisp(%p)",arg);
1119 return hres;
1120 case VT_VOID:
1121 if (debugout) TRACE_(olerelay)("<void>");
1122 return S_OK;
1123 case VT_USERDEFINED: {
1124 ITypeInfo *tinfo2;
1125 TYPEATTR *tattr;
1127 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
1128 if (hres) {
1129 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
1130 return hres;
1132 hres = ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1133 if (hres) {
1134 ERR("Could not get typeattr in VT_USERDEFINED.\n");
1135 } else {
1136 switch (tattr->typekind) {
1137 case TKIND_DISPATCH:
1138 case TKIND_INTERFACE:
1139 if (readit)
1140 hres = _unmarshal_interface(buf,&(tattr->guid),(LPUNKNOWN*)arg);
1141 break;
1142 case TKIND_RECORD: {
1143 int i;
1145 if (debugout) TRACE_(olerelay)("{");
1146 for (i=0;i<tattr->cVars;i++) {
1147 VARDESC *vdesc;
1149 hres = ITypeInfo_GetVarDesc(tinfo2, i, &vdesc);
1150 if (hres) {
1151 ERR("Could not get vardesc of %d\n",i);
1152 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1153 ITypeInfo_Release(tinfo2);
1154 return hres;
1156 hres = deserialize_param(
1157 tinfo2,
1158 readit,
1159 debugout,
1160 alloc,
1161 &vdesc->elemdescVar.tdesc,
1162 (DWORD*)(((LPBYTE)arg)+vdesc->u.oInst),
1165 ITypeInfo_ReleaseVarDesc(tinfo2, vdesc);
1166 if (debugout && (i<tattr->cVars-1)) TRACE_(olerelay)(",");
1168 if (debugout) TRACE_(olerelay)("}");
1169 break;
1171 case TKIND_ALIAS:
1172 hres = deserialize_param(tinfo2,readit,debugout,alloc,&tattr->tdescAlias,arg,buf);
1173 break;
1174 case TKIND_ENUM:
1175 if (readit) {
1176 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
1177 if (hres) ERR("Failed to read enum (4 byte)\n");
1179 if (debugout) TRACE_(olerelay)("%x",*arg);
1180 break;
1181 default:
1182 ERR("Unhandled typekind %d\n",tattr->typekind);
1183 hres = E_FAIL;
1184 break;
1186 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1188 if (hres)
1189 ERR("failed to stuballoc in TKIND_RECORD.\n");
1190 ITypeInfo_Release(tinfo2);
1191 return hres;
1193 case VT_CARRAY: {
1194 /* arg is pointing to the start of the array. */
1195 LPBYTE base = (LPBYTE) arg;
1196 ARRAYDESC *adesc = tdesc->u.lpadesc;
1197 int arrsize,i;
1198 arrsize = 1;
1199 if (adesc->cDims > 1) FIXME("cDims > 1 in VT_CARRAY. Does it work?\n");
1200 for (i=0;i<adesc->cDims;i++)
1201 arrsize *= adesc->rgbounds[i].cElements;
1202 if (_passbyref(&adesc->tdescElem, tinfo))
1204 base = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,_xsize(tdesc->u.lptdesc, tinfo) * arrsize);
1205 *arg = (DWORD) base;
1207 for (i=0;i<arrsize;i++)
1208 deserialize_param(
1209 tinfo,
1210 readit,
1211 debugout,
1212 alloc,
1213 &adesc->tdescElem,
1214 (DWORD*)(base + i*_xsize(&adesc->tdescElem, tinfo)),
1217 return S_OK;
1219 case VT_SAFEARRAY: {
1220 if (readit)
1222 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
1223 unsigned char *buffer;
1224 buffer = LPSAFEARRAY_UserUnmarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
1225 buf->curoff = buffer - buf->base;
1227 return S_OK;
1229 default:
1230 ERR("No handler for VT type %d!\n",tdesc->vt);
1231 return S_OK;
1236 /* Retrieves a function's funcdesc, searching back into inherited interfaces. */
1237 static HRESULT get_funcdesc(ITypeInfo *tinfo, int iMethod, ITypeInfo **tactual, const FUNCDESC **fdesc,
1238 BSTR *iname, BSTR *fname, UINT *num)
1240 HRESULT hr;
1241 UINT i, impl_types;
1242 UINT inherited_funcs = 0;
1243 TYPEATTR *attr;
1245 if (fname) *fname = NULL;
1246 if (iname) *iname = NULL;
1247 if (num) *num = 0;
1248 *tactual = NULL;
1250 hr = ITypeInfo_GetTypeAttr(tinfo, &attr);
1251 if (FAILED(hr))
1253 ERR("GetTypeAttr failed with %x\n",hr);
1254 return hr;
1257 if(attr->typekind == TKIND_DISPATCH)
1259 if(attr->wTypeFlags & TYPEFLAG_FDUAL)
1261 HREFTYPE href;
1262 ITypeInfo *tinfo2;
1264 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, -1, &href);
1265 if(FAILED(hr))
1267 ERR("Cannot get interface href from dual dispinterface\n");
1268 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1269 return hr;
1271 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
1272 if(FAILED(hr))
1274 ERR("Cannot get interface from dual dispinterface\n");
1275 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1276 return hr;
1278 hr = get_funcdesc(tinfo2, iMethod, tactual, fdesc, iname, fname, num);
1279 ITypeInfo_Release(tinfo2);
1280 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1281 return hr;
1283 ERR("Shouldn't be called with a non-dual dispinterface\n");
1284 return E_FAIL;
1287 impl_types = attr->cImplTypes;
1288 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1290 for (i = 0; i < impl_types; i++)
1292 HREFTYPE href;
1293 ITypeInfo *pSubTypeInfo;
1294 UINT sub_funcs;
1296 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, i, &href);
1297 if (FAILED(hr)) return hr;
1298 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &pSubTypeInfo);
1299 if (FAILED(hr)) return hr;
1301 hr = get_funcdesc(pSubTypeInfo, iMethod, tactual, fdesc, iname, fname, &sub_funcs);
1302 inherited_funcs += sub_funcs;
1303 ITypeInfo_Release(pSubTypeInfo);
1304 if(SUCCEEDED(hr)) return hr;
1306 if(iMethod < inherited_funcs)
1308 ERR("shouldn't be here\n");
1309 return E_INVALIDARG;
1312 for(i = inherited_funcs; i <= iMethod; i++)
1314 hr = ITypeInfoImpl_GetInternalFuncDesc(tinfo, i - inherited_funcs, fdesc);
1315 if(FAILED(hr))
1317 if(num) *num = i;
1318 return hr;
1322 /* found it. We don't care about num so zero it */
1323 if(num) *num = 0;
1324 *tactual = tinfo;
1325 ITypeInfo_AddRef(*tactual);
1326 if (fname) ITypeInfo_GetDocumentation(tinfo,(*fdesc)->memid,fname,NULL,NULL,NULL);
1327 if (iname) ITypeInfo_GetDocumentation(tinfo,-1,iname,NULL,NULL,NULL);
1328 return S_OK;
1331 static inline BOOL is_in_elem(const ELEMDESC *elem)
1333 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN || !elem->u.paramdesc.wParamFlags);
1336 static inline BOOL is_out_elem(const ELEMDESC *elem)
1338 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT || !elem->u.paramdesc.wParamFlags);
1341 static DWORD WINAPI xCall(int method, void **args)
1343 TMProxyImpl *tpinfo = args[0];
1344 DWORD *xargs;
1345 const FUNCDESC *fdesc;
1346 HRESULT hres;
1347 int i;
1348 marshal_state buf;
1349 RPCOLEMESSAGE msg;
1350 ULONG status;
1351 BSTR fname,iname;
1352 BSTR names[10];
1353 UINT nrofnames;
1354 DWORD remoteresult = 0;
1355 ITypeInfo *tinfo;
1356 IRpcChannelBuffer *chanbuf;
1358 EnterCriticalSection(&tpinfo->crit);
1360 hres = get_funcdesc(tpinfo->tinfo,method,&tinfo,&fdesc,&iname,&fname,NULL);
1361 if (hres) {
1362 ERR("Did not find typeinfo/funcdesc entry for method %d!\n",method);
1363 LeaveCriticalSection(&tpinfo->crit);
1364 return E_FAIL;
1367 if (!tpinfo->chanbuf)
1369 WARN("Tried to use disconnected proxy\n");
1370 ITypeInfo_Release(tinfo);
1371 LeaveCriticalSection(&tpinfo->crit);
1372 return RPC_E_DISCONNECTED;
1374 chanbuf = tpinfo->chanbuf;
1375 IRpcChannelBuffer_AddRef(chanbuf);
1377 LeaveCriticalSection(&tpinfo->crit);
1379 if (TRACE_ON(olerelay)) {
1380 TRACE_(olerelay)("->");
1381 if (iname)
1382 TRACE_(olerelay)("%s:",relaystr(iname));
1383 if (fname)
1384 TRACE_(olerelay)("%s(%d)",relaystr(fname),method);
1385 else
1386 TRACE_(olerelay)("%d",method);
1387 TRACE_(olerelay)("(");
1390 SysFreeString(iname);
1391 SysFreeString(fname);
1393 memset(&buf,0,sizeof(buf));
1395 /* normal typelib driven serializing */
1397 /* Need them for hack below */
1398 memset(names,0,sizeof(names));
1399 if (ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames))
1400 nrofnames = 0;
1401 if (nrofnames > sizeof(names)/sizeof(names[0]))
1402 ERR("Need more names!\n");
1404 xargs = (DWORD *)(args + 1);
1405 for (i=0;i<fdesc->cParams;i++) {
1406 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1407 if (TRACE_ON(olerelay)) {
1408 if (i) TRACE_(olerelay)(",");
1409 if (i+1<nrofnames && names[i+1])
1410 TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1412 /* No need to marshal other data than FIN and any VT_PTR. */
1413 if (!is_in_elem(elem))
1415 if (elem->tdesc.vt != VT_PTR)
1417 xargs+=_argsize(&elem->tdesc, tinfo);
1418 TRACE_(olerelay)("[out]");
1419 continue;
1421 else
1423 memset( *(void **)xargs, 0, _xsize( elem->tdesc.u.lptdesc, tinfo ) );
1427 hres = serialize_param(
1428 tinfo,
1429 is_in_elem(elem),
1430 TRACE_ON(olerelay),
1431 FALSE,
1432 &elem->tdesc,
1433 xargs,
1434 &buf
1437 if (hres) {
1438 ERR("Failed to serialize param, hres %x\n",hres);
1439 break;
1441 xargs+=_argsize(&elem->tdesc, tinfo);
1443 TRACE_(olerelay)(")");
1445 memset(&msg,0,sizeof(msg));
1446 msg.cbBuffer = buf.curoff;
1447 msg.iMethod = method;
1448 hres = IRpcChannelBuffer_GetBuffer(chanbuf,&msg,&(tpinfo->iid));
1449 if (hres) {
1450 ERR("RpcChannelBuffer GetBuffer failed, %x\n",hres);
1451 goto exit;
1453 memcpy(msg.Buffer,buf.base,buf.curoff);
1454 TRACE_(olerelay)("\n");
1455 hres = IRpcChannelBuffer_SendReceive(chanbuf,&msg,&status);
1456 if (hres) {
1457 ERR("RpcChannelBuffer SendReceive failed, %x\n",hres);
1458 goto exit;
1461 TRACE_(olerelay)(" status = %08x (",status);
1462 if (buf.base)
1463 buf.base = HeapReAlloc(GetProcessHeap(),0,buf.base,msg.cbBuffer);
1464 else
1465 buf.base = HeapAlloc(GetProcessHeap(),0,msg.cbBuffer);
1466 buf.size = msg.cbBuffer;
1467 memcpy(buf.base,msg.Buffer,buf.size);
1468 buf.curoff = 0;
1470 /* generic deserializer using typelib description */
1471 xargs = (DWORD *)(args + 1);
1472 status = S_OK;
1473 for (i=0;i<fdesc->cParams;i++) {
1474 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1476 if (i) TRACE_(olerelay)(",");
1477 if (i+1<nrofnames && names[i+1]) TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1479 /* No need to marshal other data than FOUT and any VT_PTR */
1480 if (!is_out_elem(elem) && (elem->tdesc.vt != VT_PTR)) {
1481 xargs += _argsize(&elem->tdesc, tinfo);
1482 TRACE_(olerelay)("[in]");
1483 continue;
1485 hres = deserialize_param(
1486 tinfo,
1487 is_out_elem(elem),
1488 TRACE_ON(olerelay),
1489 FALSE,
1490 &(elem->tdesc),
1491 xargs,
1492 &buf
1494 if (hres) {
1495 ERR("Failed to unmarshall param, hres %x\n",hres);
1496 status = hres;
1497 break;
1499 xargs += _argsize(&elem->tdesc, tinfo);
1502 hres = xbuf_get(&buf, (LPBYTE)&remoteresult, sizeof(DWORD));
1503 if (hres != S_OK)
1504 goto exit;
1505 TRACE_(olerelay)(") = %08x\n", remoteresult);
1507 hres = remoteresult;
1509 exit:
1510 IRpcChannelBuffer_FreeBuffer(chanbuf,&msg);
1511 for (i = 0; i < nrofnames; i++)
1512 SysFreeString(names[i]);
1513 HeapFree(GetProcessHeap(),0,buf.base);
1514 IRpcChannelBuffer_Release(chanbuf);
1515 ITypeInfo_Release(tinfo);
1516 TRACE("-- 0x%08x\n", hres);
1517 return hres;
1520 static HRESULT WINAPI ProxyIUnknown_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
1522 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1524 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1526 if (proxy->outerunknown)
1527 return IUnknown_QueryInterface(proxy->outerunknown, riid, ppv);
1529 FIXME("No interface\n");
1530 return E_NOINTERFACE;
1533 static ULONG WINAPI ProxyIUnknown_AddRef(IUnknown *iface)
1535 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1537 TRACE("\n");
1539 if (proxy->outerunknown)
1540 return IUnknown_AddRef(proxy->outerunknown);
1542 return 2; /* FIXME */
1545 static ULONG WINAPI ProxyIUnknown_Release(IUnknown *iface)
1547 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1549 TRACE("\n");
1551 if (proxy->outerunknown)
1552 return IUnknown_Release(proxy->outerunknown);
1554 return 1; /* FIXME */
1557 static HRESULT WINAPI ProxyIDispatch_GetTypeInfoCount(LPDISPATCH iface, UINT * pctinfo)
1559 TMProxyImpl *This = (TMProxyImpl *)iface;
1561 TRACE("(%p)\n", pctinfo);
1563 return IDispatch_GetTypeInfoCount(This->dispatch, pctinfo);
1566 static HRESULT WINAPI ProxyIDispatch_GetTypeInfo(LPDISPATCH iface, UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo)
1568 TMProxyImpl *This = (TMProxyImpl *)iface;
1570 TRACE("(%d, %x, %p)\n", iTInfo, lcid, ppTInfo);
1572 return IDispatch_GetTypeInfo(This->dispatch, iTInfo, lcid, ppTInfo);
1575 static HRESULT WINAPI ProxyIDispatch_GetIDsOfNames(LPDISPATCH iface, REFIID riid, LPOLESTR * rgszNames, UINT cNames, LCID lcid, DISPID * rgDispId)
1577 TMProxyImpl *This = (TMProxyImpl *)iface;
1579 TRACE("(%s, %p, %d, 0x%x, %p)\n", debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);
1581 return IDispatch_GetIDsOfNames(This->dispatch, riid, rgszNames,
1582 cNames, lcid, rgDispId);
1585 static HRESULT WINAPI ProxyIDispatch_Invoke(LPDISPATCH iface, DISPID dispIdMember, REFIID riid, LCID lcid,
1586 WORD wFlags, DISPPARAMS * pDispParams, VARIANT * pVarResult,
1587 EXCEPINFO * pExcepInfo, UINT * puArgErr)
1589 TMProxyImpl *This = (TMProxyImpl *)iface;
1591 TRACE("(%d, %s, 0x%x, 0x%x, %p, %p, %p, %p)\n", dispIdMember,
1592 debugstr_guid(riid), lcid, wFlags, pDispParams, pVarResult,
1593 pExcepInfo, puArgErr);
1595 return IDispatch_Invoke(This->dispatch, dispIdMember, riid, lcid,
1596 wFlags, pDispParams, pVarResult, pExcepInfo,
1597 puArgErr);
1600 typedef struct
1602 IRpcChannelBuffer IRpcChannelBuffer_iface;
1603 LONG refs;
1604 /* the IDispatch-derived interface we are handling */
1605 IID tmarshal_iid;
1606 IRpcChannelBuffer *pDelegateChannel;
1607 } TMarshalDispatchChannel;
1609 static inline TMarshalDispatchChannel *impl_from_IRpcChannelBuffer(IRpcChannelBuffer *iface)
1611 return CONTAINING_RECORD(iface, TMarshalDispatchChannel, IRpcChannelBuffer_iface);
1614 static HRESULT WINAPI TMarshalDispatchChannel_QueryInterface(IRpcChannelBuffer *iface, REFIID riid, LPVOID *ppv)
1616 *ppv = NULL;
1617 if (IsEqualIID(riid,&IID_IRpcChannelBuffer) || IsEqualIID(riid,&IID_IUnknown))
1619 *ppv = iface;
1620 IRpcChannelBuffer_AddRef(iface);
1621 return S_OK;
1623 return E_NOINTERFACE;
1626 static ULONG WINAPI TMarshalDispatchChannel_AddRef(LPRPCCHANNELBUFFER iface)
1628 TMarshalDispatchChannel *This = impl_from_IRpcChannelBuffer(iface);
1629 return InterlockedIncrement(&This->refs);
1632 static ULONG WINAPI TMarshalDispatchChannel_Release(LPRPCCHANNELBUFFER iface)
1634 TMarshalDispatchChannel *This = impl_from_IRpcChannelBuffer(iface);
1635 ULONG ref;
1637 ref = InterlockedDecrement(&This->refs);
1638 if (ref)
1639 return ref;
1641 IRpcChannelBuffer_Release(This->pDelegateChannel);
1642 HeapFree(GetProcessHeap(), 0, This);
1643 return 0;
1646 static HRESULT WINAPI TMarshalDispatchChannel_GetBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg, REFIID riid)
1648 TMarshalDispatchChannel *This = impl_from_IRpcChannelBuffer(iface);
1649 TRACE("(%p, %s)\n", olemsg, debugstr_guid(riid));
1650 /* Note: we are pretending to invoke a method on the interface identified
1651 * by tmarshal_iid so that we can re-use the IDispatch proxy/stub code
1652 * without the RPC runtime getting confused by not exporting an IDispatch interface */
1653 return IRpcChannelBuffer_GetBuffer(This->pDelegateChannel, olemsg, &This->tmarshal_iid);
1656 static HRESULT WINAPI TMarshalDispatchChannel_SendReceive(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE *olemsg, ULONG *pstatus)
1658 TMarshalDispatchChannel *This = impl_from_IRpcChannelBuffer(iface);
1659 TRACE("(%p, %p)\n", olemsg, pstatus);
1660 return IRpcChannelBuffer_SendReceive(This->pDelegateChannel, olemsg, pstatus);
1663 static HRESULT WINAPI TMarshalDispatchChannel_FreeBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg)
1665 TMarshalDispatchChannel *This = impl_from_IRpcChannelBuffer(iface);
1666 TRACE("(%p)\n", olemsg);
1667 return IRpcChannelBuffer_FreeBuffer(This->pDelegateChannel, olemsg);
1670 static HRESULT WINAPI TMarshalDispatchChannel_GetDestCtx(LPRPCCHANNELBUFFER iface, DWORD* pdwDestContext, void** ppvDestContext)
1672 TMarshalDispatchChannel *This = impl_from_IRpcChannelBuffer(iface);
1673 TRACE("(%p,%p)\n", pdwDestContext, ppvDestContext);
1674 return IRpcChannelBuffer_GetDestCtx(This->pDelegateChannel, pdwDestContext, ppvDestContext);
1677 static HRESULT WINAPI TMarshalDispatchChannel_IsConnected(LPRPCCHANNELBUFFER iface)
1679 TMarshalDispatchChannel *This = impl_from_IRpcChannelBuffer(iface);
1680 TRACE("()\n");
1681 return IRpcChannelBuffer_IsConnected(This->pDelegateChannel);
1684 static const IRpcChannelBufferVtbl TMarshalDispatchChannelVtbl =
1686 TMarshalDispatchChannel_QueryInterface,
1687 TMarshalDispatchChannel_AddRef,
1688 TMarshalDispatchChannel_Release,
1689 TMarshalDispatchChannel_GetBuffer,
1690 TMarshalDispatchChannel_SendReceive,
1691 TMarshalDispatchChannel_FreeBuffer,
1692 TMarshalDispatchChannel_GetDestCtx,
1693 TMarshalDispatchChannel_IsConnected
1696 static HRESULT TMarshalDispatchChannel_Create(
1697 IRpcChannelBuffer *pDelegateChannel, REFIID tmarshal_riid,
1698 IRpcChannelBuffer **ppChannel)
1700 TMarshalDispatchChannel *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1701 if (!This)
1702 return E_OUTOFMEMORY;
1704 This->IRpcChannelBuffer_iface.lpVtbl = &TMarshalDispatchChannelVtbl;
1705 This->refs = 1;
1706 IRpcChannelBuffer_AddRef(pDelegateChannel);
1707 This->pDelegateChannel = pDelegateChannel;
1708 This->tmarshal_iid = *tmarshal_riid;
1710 *ppChannel = &This->IRpcChannelBuffer_iface;
1711 return S_OK;
1715 static inline HRESULT get_facbuf_for_iid(REFIID riid, IPSFactoryBuffer **facbuf)
1717 HRESULT hr;
1718 CLSID clsid;
1720 if ((hr = CoGetPSClsid(riid, &clsid)))
1721 return hr;
1722 return CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL,
1723 &IID_IPSFactoryBuffer, (LPVOID*)facbuf);
1726 static HRESULT init_proxy_entry_point(TMProxyImpl *proxy, unsigned int num)
1728 int j;
1729 /* nrofargs including This */
1730 int nrofargs = 1;
1731 ITypeInfo *tinfo2;
1732 TMAsmProxy *xasm = proxy->asmstubs + num;
1733 HRESULT hres;
1734 const FUNCDESC *fdesc;
1736 hres = get_funcdesc(proxy->tinfo, num, &tinfo2, &fdesc, NULL, NULL, NULL);
1737 if (hres) {
1738 ERR("GetFuncDesc %x should not fail here.\n",hres);
1739 return hres;
1741 ITypeInfo_Release(tinfo2);
1742 /* some args take more than 4 byte on the stack */
1743 for (j=0;j<fdesc->cParams;j++)
1744 nrofargs += _argsize(&fdesc->lprgelemdescParam[j].tdesc, proxy->tinfo);
1746 #ifdef __i386__
1747 if (fdesc->callconv != CC_STDCALL) {
1748 ERR("calling convention is not stdcall????\n");
1749 return E_FAIL;
1751 /* leal 4(%esp),%eax
1752 * pushl %eax
1753 * pushl <nr>
1754 * call xCall
1755 * lret <nr>
1757 xasm->lealeax = 0x0424448d;
1758 xasm->pushleax = 0x50;
1759 xasm->pushlval = 0x68;
1760 xasm->nr = num;
1761 xasm->lcall = 0xe8;
1762 xasm->xcall = (char *)xCall - (char *)&xasm->lret;
1763 xasm->lret = 0xc2;
1764 xasm->bytestopop = nrofargs * 4;
1765 xasm->nop = 0x9090;
1766 proxy->lpvtbl[fdesc->oVft / sizeof(void *)] = xasm;
1767 #else
1768 FIXME("not implemented on non i386\n");
1769 return E_FAIL;
1770 #endif
1771 return S_OK;
1774 static HRESULT WINAPI
1775 PSFacBuf_CreateProxy(
1776 LPPSFACTORYBUFFER iface, IUnknown* pUnkOuter, REFIID riid,
1777 IRpcProxyBuffer **ppProxy, LPVOID *ppv)
1779 HRESULT hres;
1780 ITypeInfo *tinfo;
1781 unsigned int i, nroffuncs, vtbl_size;
1782 TMProxyImpl *proxy;
1783 TYPEATTR *typeattr;
1784 BOOL defer_to_dispatch = FALSE;
1786 TRACE("(...%s...)\n",debugstr_guid(riid));
1787 hres = _get_typeinfo_for_iid(riid,&tinfo);
1788 if (hres) {
1789 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
1790 return hres;
1793 hres = num_of_funcs(tinfo, &nroffuncs, &vtbl_size);
1794 TRACE("Got %d funcs, vtbl size %d\n", nroffuncs, vtbl_size);
1796 if (FAILED(hres)) {
1797 ERR("Cannot get number of functions for typeinfo %s\n",debugstr_guid(riid));
1798 ITypeInfo_Release(tinfo);
1799 return hres;
1802 proxy = CoTaskMemAlloc(sizeof(TMProxyImpl));
1803 if (!proxy) return E_OUTOFMEMORY;
1805 proxy->dispatch = NULL;
1806 proxy->dispatch_proxy = NULL;
1807 proxy->outerunknown = pUnkOuter;
1808 proxy->asmstubs = VirtualAlloc(NULL, sizeof(TMAsmProxy) * nroffuncs, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
1809 if (!proxy->asmstubs) {
1810 ERR("Could not commit pages for proxy thunks\n");
1811 CoTaskMemFree(proxy);
1812 return E_OUTOFMEMORY;
1814 proxy->IRpcProxyBuffer_iface.lpVtbl = &tmproxyvtable;
1815 /* one reference for the proxy */
1816 proxy->ref = 1;
1817 proxy->tinfo = tinfo;
1818 proxy->iid = *riid;
1819 proxy->chanbuf = 0;
1821 InitializeCriticalSection(&proxy->crit);
1822 proxy->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TMProxyImpl.crit");
1824 proxy->lpvtbl = HeapAlloc(GetProcessHeap(), 0, vtbl_size);
1826 /* if we derive from IDispatch then defer to its proxy for its methods */
1827 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
1828 if (hres == S_OK)
1830 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
1832 IPSFactoryBuffer *factory_buffer;
1833 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
1834 if (hres == S_OK)
1836 hres = IPSFactoryBuffer_CreateProxy(factory_buffer, NULL,
1837 &IID_IDispatch, &proxy->dispatch_proxy,
1838 (void **)&proxy->dispatch);
1839 IPSFactoryBuffer_Release(factory_buffer);
1841 if ((hres == S_OK) && (nroffuncs < 7))
1843 ERR("nroffuncs calculated incorrectly (%d)\n", nroffuncs);
1844 hres = E_UNEXPECTED;
1846 if (hres == S_OK)
1848 defer_to_dispatch = TRUE;
1851 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
1854 for (i=0;i<nroffuncs;i++) {
1855 switch (i) {
1856 case 0:
1857 proxy->lpvtbl[i] = ProxyIUnknown_QueryInterface;
1858 break;
1859 case 1:
1860 proxy->lpvtbl[i] = ProxyIUnknown_AddRef;
1861 break;
1862 case 2:
1863 proxy->lpvtbl[i] = ProxyIUnknown_Release;
1864 break;
1865 case 3:
1866 if(!defer_to_dispatch) hres = init_proxy_entry_point(proxy, i);
1867 else proxy->lpvtbl[3] = ProxyIDispatch_GetTypeInfoCount;
1868 break;
1869 case 4:
1870 if(!defer_to_dispatch) hres = init_proxy_entry_point(proxy, i);
1871 else proxy->lpvtbl[4] = ProxyIDispatch_GetTypeInfo;
1872 break;
1873 case 5:
1874 if(!defer_to_dispatch) hres = init_proxy_entry_point(proxy, i);
1875 else proxy->lpvtbl[5] = ProxyIDispatch_GetIDsOfNames;
1876 break;
1877 case 6:
1878 if(!defer_to_dispatch) hres = init_proxy_entry_point(proxy, i);
1879 else proxy->lpvtbl[6] = ProxyIDispatch_Invoke;
1880 break;
1881 default:
1882 hres = init_proxy_entry_point(proxy, i);
1886 if (hres == S_OK)
1888 *ppv = proxy;
1889 *ppProxy = &proxy->IRpcProxyBuffer_iface;
1890 IUnknown_AddRef((IUnknown *)*ppv);
1891 return S_OK;
1893 else
1894 TMProxyImpl_Release(&proxy->IRpcProxyBuffer_iface);
1895 return hres;
1898 typedef struct _TMStubImpl {
1899 IRpcStubBuffer IRpcStubBuffer_iface;
1900 LONG ref;
1902 LPUNKNOWN pUnk;
1903 ITypeInfo *tinfo;
1904 IID iid;
1905 IRpcStubBuffer *dispatch_stub;
1906 BOOL dispatch_derivative;
1907 } TMStubImpl;
1909 static inline TMStubImpl *impl_from_IRpcStubBuffer(IRpcStubBuffer *iface)
1911 return CONTAINING_RECORD(iface, TMStubImpl, IRpcStubBuffer_iface);
1914 static HRESULT WINAPI
1915 TMStubImpl_QueryInterface(LPRPCSTUBBUFFER iface, REFIID riid, LPVOID *ppv)
1917 if (IsEqualIID(riid,&IID_IRpcStubBuffer)||IsEqualIID(riid,&IID_IUnknown)){
1918 *ppv = iface;
1919 IRpcStubBuffer_AddRef(iface);
1920 return S_OK;
1922 FIXME("%s, not supported IID.\n",debugstr_guid(riid));
1923 return E_NOINTERFACE;
1926 static ULONG WINAPI
1927 TMStubImpl_AddRef(LPRPCSTUBBUFFER iface)
1929 TMStubImpl *This = impl_from_IRpcStubBuffer(iface);
1930 ULONG refCount = InterlockedIncrement(&This->ref);
1932 TRACE("(%p)->(ref before=%u)\n", This, refCount - 1);
1934 return refCount;
1937 static ULONG WINAPI
1938 TMStubImpl_Release(LPRPCSTUBBUFFER iface)
1940 TMStubImpl *This = impl_from_IRpcStubBuffer(iface);
1941 ULONG refCount = InterlockedDecrement(&This->ref);
1943 TRACE("(%p)->(ref before=%u)\n", This, refCount + 1);
1945 if (!refCount)
1947 IRpcStubBuffer_Disconnect(iface);
1948 ITypeInfo_Release(This->tinfo);
1949 if (This->dispatch_stub)
1950 IRpcStubBuffer_Release(This->dispatch_stub);
1951 CoTaskMemFree(This);
1953 return refCount;
1956 static HRESULT WINAPI
1957 TMStubImpl_Connect(LPRPCSTUBBUFFER iface, LPUNKNOWN pUnkServer)
1959 TMStubImpl *This = impl_from_IRpcStubBuffer(iface);
1961 TRACE("(%p)->(%p)\n", This, pUnkServer);
1963 IUnknown_AddRef(pUnkServer);
1964 This->pUnk = pUnkServer;
1966 if (This->dispatch_stub)
1967 IRpcStubBuffer_Connect(This->dispatch_stub, pUnkServer);
1969 return S_OK;
1972 static void WINAPI
1973 TMStubImpl_Disconnect(LPRPCSTUBBUFFER iface)
1975 TMStubImpl *This = impl_from_IRpcStubBuffer(iface);
1977 TRACE("(%p)->()\n", This);
1979 if (This->pUnk)
1981 IUnknown_Release(This->pUnk);
1982 This->pUnk = NULL;
1985 if (This->dispatch_stub)
1986 IRpcStubBuffer_Disconnect(This->dispatch_stub);
1989 static HRESULT WINAPI
1990 TMStubImpl_Invoke(
1991 LPRPCSTUBBUFFER iface, RPCOLEMESSAGE* xmsg,IRpcChannelBuffer*rpcchanbuf)
1993 #ifdef __i386__
1994 int i;
1995 const FUNCDESC *fdesc;
1996 TMStubImpl *This = impl_from_IRpcStubBuffer(iface);
1997 HRESULT hres;
1998 DWORD *args = NULL, res, *xargs, nrofargs;
1999 marshal_state buf;
2000 UINT nrofnames = 0;
2001 BSTR names[10];
2002 BSTR iname = NULL;
2003 ITypeInfo *tinfo = NULL;
2005 TRACE("...\n");
2007 if (xmsg->iMethod < 3) {
2008 ERR("IUnknown methods cannot be marshaled by the typelib marshaler\n");
2009 return E_UNEXPECTED;
2012 if (This->dispatch_derivative && xmsg->iMethod < sizeof(IDispatchVtbl)/sizeof(void *))
2014 if (!This->dispatch_stub)
2016 IPSFactoryBuffer *factory_buffer;
2017 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
2018 if (hres == S_OK)
2020 hres = IPSFactoryBuffer_CreateStub(factory_buffer, &IID_IDispatch,
2021 This->pUnk, &This->dispatch_stub);
2022 IPSFactoryBuffer_Release(factory_buffer);
2024 if (hres != S_OK)
2025 return hres;
2027 return IRpcStubBuffer_Invoke(This->dispatch_stub, xmsg, rpcchanbuf);
2030 memset(&buf,0,sizeof(buf));
2031 buf.size = xmsg->cbBuffer;
2032 buf.base = HeapAlloc(GetProcessHeap(), 0, xmsg->cbBuffer);
2033 memcpy(buf.base, xmsg->Buffer, xmsg->cbBuffer);
2034 buf.curoff = 0;
2036 hres = get_funcdesc(This->tinfo,xmsg->iMethod,&tinfo,&fdesc,&iname,NULL,NULL);
2037 if (hres) {
2038 ERR("GetFuncDesc on method %d failed with %x\n",xmsg->iMethod,hres);
2039 return hres;
2042 if (iname && !lstrcmpW(iname, IDispatchW))
2044 ERR("IDispatch cannot be marshaled by the typelib marshaler\n");
2045 hres = E_UNEXPECTED;
2046 SysFreeString (iname);
2047 goto exit;
2050 SysFreeString (iname);
2052 /* Need them for hack below */
2053 memset(names,0,sizeof(names));
2054 ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames);
2055 if (nrofnames > sizeof(names)/sizeof(names[0])) {
2056 ERR("Need more names!\n");
2059 /*dump_FUNCDESC(fdesc);*/
2060 nrofargs = 0;
2061 for (i=0;i<fdesc->cParams;i++)
2062 nrofargs += _argsize(&fdesc->lprgelemdescParam[i].tdesc, tinfo);
2063 args = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(nrofargs+1)*sizeof(DWORD));
2064 if (!args)
2066 hres = E_OUTOFMEMORY;
2067 goto exit;
2070 /* Allocate all stuff used by call. */
2071 xargs = args+1;
2072 for (i=0;i<fdesc->cParams;i++) {
2073 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2075 hres = deserialize_param(
2076 tinfo,
2077 is_in_elem(elem),
2078 FALSE,
2079 TRUE,
2080 &(elem->tdesc),
2081 xargs,
2082 &buf
2084 xargs += _argsize(&elem->tdesc, tinfo);
2085 if (hres) {
2086 ERR("Failed to deserialize param %s, hres %x\n",relaystr(names[i+1]),hres);
2087 break;
2091 args[0] = (DWORD)This->pUnk;
2093 __TRY
2095 res = _invoke(
2096 (*((FARPROC**)args[0]))[fdesc->oVft/4],
2097 fdesc->callconv,
2098 (xargs-args),
2099 args
2102 __EXCEPT_ALL
2104 DWORD dwExceptionCode = GetExceptionCode();
2105 ERR("invoke call failed with exception 0x%08x (%d)\n", dwExceptionCode, dwExceptionCode);
2106 if (FAILED(dwExceptionCode))
2107 hres = dwExceptionCode;
2108 else
2109 hres = HRESULT_FROM_WIN32(dwExceptionCode);
2111 __ENDTRY
2113 if (hres != S_OK)
2114 goto exit;
2116 buf.curoff = 0;
2118 xargs = args+1;
2119 for (i=0;i<fdesc->cParams;i++) {
2120 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2121 hres = serialize_param(
2122 tinfo,
2123 is_out_elem(elem),
2124 FALSE,
2125 TRUE,
2126 &elem->tdesc,
2127 xargs,
2128 &buf
2130 xargs += _argsize(&elem->tdesc, tinfo);
2131 if (hres) {
2132 ERR("Failed to stuballoc param, hres %x\n",hres);
2133 break;
2137 hres = xbuf_add (&buf, (LPBYTE)&res, sizeof(DWORD));
2139 if (hres != S_OK)
2140 goto exit;
2142 xmsg->cbBuffer = buf.curoff;
2143 hres = IRpcChannelBuffer_GetBuffer(rpcchanbuf, xmsg, &This->iid);
2144 if (hres != S_OK)
2145 ERR("IRpcChannelBuffer_GetBuffer failed with error 0x%08x\n", hres);
2147 if (hres == S_OK)
2148 memcpy(xmsg->Buffer, buf.base, buf.curoff);
2150 exit:
2151 for (i = 0; i < nrofnames; i++)
2152 SysFreeString(names[i]);
2154 ITypeInfo_Release(tinfo);
2155 HeapFree(GetProcessHeap(), 0, args);
2157 HeapFree(GetProcessHeap(), 0, buf.base);
2159 TRACE("returning\n");
2160 return hres;
2161 #else
2162 FIXME( "not implemented on non-i386\n" );
2163 return E_FAIL;
2164 #endif
2167 static LPRPCSTUBBUFFER WINAPI
2168 TMStubImpl_IsIIDSupported(LPRPCSTUBBUFFER iface, REFIID riid) {
2169 FIXME("Huh (%s)?\n",debugstr_guid(riid));
2170 return NULL;
2173 static ULONG WINAPI
2174 TMStubImpl_CountRefs(LPRPCSTUBBUFFER iface) {
2175 TMStubImpl *This = impl_from_IRpcStubBuffer(iface);
2177 FIXME("()\n");
2178 return This->ref; /*FIXME? */
2181 static HRESULT WINAPI
2182 TMStubImpl_DebugServerQueryInterface(LPRPCSTUBBUFFER iface, LPVOID *ppv) {
2183 return E_NOTIMPL;
2186 static void WINAPI
2187 TMStubImpl_DebugServerRelease(LPRPCSTUBBUFFER iface, LPVOID ppv) {
2188 return;
2191 static const IRpcStubBufferVtbl tmstubvtbl = {
2192 TMStubImpl_QueryInterface,
2193 TMStubImpl_AddRef,
2194 TMStubImpl_Release,
2195 TMStubImpl_Connect,
2196 TMStubImpl_Disconnect,
2197 TMStubImpl_Invoke,
2198 TMStubImpl_IsIIDSupported,
2199 TMStubImpl_CountRefs,
2200 TMStubImpl_DebugServerQueryInterface,
2201 TMStubImpl_DebugServerRelease
2204 static HRESULT WINAPI
2205 PSFacBuf_CreateStub(
2206 LPPSFACTORYBUFFER iface, REFIID riid,IUnknown *pUnkServer,
2207 IRpcStubBuffer** ppStub
2209 HRESULT hres;
2210 ITypeInfo *tinfo;
2211 TMStubImpl *stub;
2212 TYPEATTR *typeattr;
2214 TRACE("(%s,%p,%p)\n",debugstr_guid(riid),pUnkServer,ppStub);
2216 hres = _get_typeinfo_for_iid(riid,&tinfo);
2217 if (hres) {
2218 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
2219 return hres;
2222 stub = CoTaskMemAlloc(sizeof(TMStubImpl));
2223 if (!stub)
2224 return E_OUTOFMEMORY;
2225 stub->IRpcStubBuffer_iface.lpVtbl = &tmstubvtbl;
2226 stub->ref = 1;
2227 stub->tinfo = tinfo;
2228 stub->dispatch_stub = NULL;
2229 stub->dispatch_derivative = FALSE;
2230 stub->iid = *riid;
2231 hres = IRpcStubBuffer_Connect(&stub->IRpcStubBuffer_iface,pUnkServer);
2232 *ppStub = &stub->IRpcStubBuffer_iface;
2233 TRACE("IRpcStubBuffer: %p\n", stub);
2234 if (hres)
2235 ERR("Connect to pUnkServer failed?\n");
2237 /* if we derive from IDispatch then defer to its stub for some of its methods */
2238 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
2239 if (hres == S_OK)
2241 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
2242 stub->dispatch_derivative = TRUE;
2243 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
2246 return hres;
2249 static const IPSFactoryBufferVtbl psfacbufvtbl = {
2250 PSFacBuf_QueryInterface,
2251 PSFacBuf_AddRef,
2252 PSFacBuf_Release,
2253 PSFacBuf_CreateProxy,
2254 PSFacBuf_CreateStub
2257 /* This is the whole PSFactoryBuffer object, just the vtableptr */
2258 static const IPSFactoryBufferVtbl *lppsfac = &psfacbufvtbl;
2260 /***********************************************************************
2261 * TMARSHAL_DllGetClassObject
2263 HRESULT TMARSHAL_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
2265 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)) {
2266 *ppv = &lppsfac;
2267 return S_OK;
2269 return E_NOINTERFACE;