d2d1: Implement d2d_d3d_render_target_CreateBitmap().
[wine/multimedia.git] / dlls / oleaut32 / tmarshal.c
blobb812215c36b3b5842972450319ef5d6705451d03
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 REGSAM opposite = (sizeof(void*) == 8) ? KEY_WOW64_32KEY : KEY_WOW64_64KEY;
274 BOOL is_wow64;
275 char tlguid[200],typelibkey[300],interfacekey[300],ver[100];
276 char tlfn[260];
277 OLECHAR tlfnW[260];
278 DWORD tlguidlen, verlen, type;
279 LONG tlfnlen, err;
280 ITypeLib *tl;
282 sprintf( interfacekey, "Interface\\{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\Typelib",
283 riid->Data1, riid->Data2, riid->Data3,
284 riid->Data4[0], riid->Data4[1], riid->Data4[2], riid->Data4[3],
285 riid->Data4[4], riid->Data4[5], riid->Data4[6], riid->Data4[7]
288 err = RegOpenKeyExA(HKEY_CLASSES_ROOT,interfacekey,0,KEY_READ,&ikey);
289 if (err && (opposite == KEY_WOW64_32KEY || (IsWow64Process(GetCurrentProcess(), &is_wow64)
290 && is_wow64))) {
291 err = RegOpenKeyExA(HKEY_CLASSES_ROOT,interfacekey,0,KEY_READ|opposite,&ikey);
293 if (err) {
294 ERR("No %s key found.\n",interfacekey);
295 return E_FAIL;
297 tlguidlen = sizeof(tlguid);
298 if (RegQueryValueExA(ikey,NULL,NULL,&type,(LPBYTE)tlguid,&tlguidlen)) {
299 ERR("Getting typelib guid failed.\n");
300 RegCloseKey(ikey);
301 return E_FAIL;
303 verlen = sizeof(ver);
304 if (RegQueryValueExA(ikey,"Version",NULL,&type,(LPBYTE)ver,&verlen)) {
305 ERR("Could not get version value?\n");
306 RegCloseKey(ikey);
307 return E_FAIL;
309 RegCloseKey(ikey);
310 sprintf(typelibkey,"Typelib\\%s\\%s\\0\\win%u",tlguid,ver,(sizeof(void*) == 8) ? 64 : 32);
311 tlfnlen = sizeof(tlfn);
312 if (RegQueryValueA(HKEY_CLASSES_ROOT,typelibkey,tlfn,&tlfnlen)) {
313 #ifdef _WIN64
314 sprintf(typelibkey,"Typelib\\%s\\%s\\0\\win32",tlguid,ver);
315 tlfnlen = sizeof(tlfn);
316 if (RegQueryValueA(HKEY_CLASSES_ROOT,typelibkey,tlfn,&tlfnlen)) {
317 #endif
318 ERR("Could not get typelib fn?\n");
319 return E_FAIL;
320 #ifdef _WIN64
322 #endif
324 MultiByteToWideChar(CP_ACP, 0, tlfn, -1, tlfnW, sizeof(tlfnW) / sizeof(tlfnW[0]));
325 hres = LoadTypeLib(tlfnW,&tl);
326 if (hres) {
327 ERR("Failed to load typelib for %s, but it should be there.\n",debugstr_guid(riid));
328 return hres;
330 hres = ITypeLib_GetTypeInfoOfGuid(tl,riid,ti);
331 if (hres) {
332 ERR("typelib does not contain info for %s?\n",debugstr_guid(riid));
333 ITypeLib_Release(tl);
334 return hres;
336 ITypeLib_Release(tl);
337 return hres;
341 * Determine the number of functions including all inherited functions
342 * and well as the size of the vtbl.
343 * Note for non-dual dispinterfaces we simply return the size of IDispatch.
345 static HRESULT num_of_funcs(ITypeInfo *tinfo, unsigned int *num,
346 unsigned int *vtbl_size)
348 HRESULT hr;
349 TYPEATTR *attr;
350 ITypeInfo *tinfo2;
351 UINT inherited_funcs = 0, i;
353 *num = 0;
354 if(vtbl_size) *vtbl_size = 0;
356 hr = ITypeInfo_GetTypeAttr(tinfo, &attr);
357 if (hr)
359 ERR("GetTypeAttr failed with %x\n", hr);
360 return hr;
363 if(attr->typekind == TKIND_DISPATCH)
365 if(attr->wTypeFlags & TYPEFLAG_FDUAL)
367 HREFTYPE href;
369 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
370 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, -1, &href);
371 if(FAILED(hr))
373 ERR("Unable to get interface href from dual dispinterface\n");
374 return hr;
376 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
377 if(FAILED(hr))
379 ERR("Unable to get interface from dual dispinterface\n");
380 return hr;
382 hr = num_of_funcs(tinfo2, num, vtbl_size);
383 ITypeInfo_Release(tinfo2);
384 return hr;
386 else /* non-dual dispinterface */
388 /* These will be the size of IDispatchVtbl */
389 *num = attr->cbSizeVft / sizeof(void *);
390 if(vtbl_size) *vtbl_size = attr->cbSizeVft;
391 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
392 return hr;
396 for (i = 0; i < attr->cImplTypes; i++)
398 HREFTYPE href;
399 ITypeInfo *pSubTypeInfo;
400 UINT sub_funcs;
402 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, i, &href);
403 if (FAILED(hr)) goto end;
404 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &pSubTypeInfo);
405 if (FAILED(hr)) goto end;
407 hr = num_of_funcs(pSubTypeInfo, &sub_funcs, NULL);
408 ITypeInfo_Release(pSubTypeInfo);
410 if(FAILED(hr)) goto end;
411 inherited_funcs += sub_funcs;
414 *num = inherited_funcs + attr->cFuncs;
415 if(vtbl_size) *vtbl_size = attr->cbSizeVft;
417 end:
418 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
419 return hr;
422 #ifdef __i386__
424 #include "pshpack1.h"
426 typedef struct _TMAsmProxy {
427 DWORD lealeax;
428 BYTE pushleax;
429 BYTE pushlval;
430 DWORD nr;
431 BYTE lcall;
432 DWORD xcall;
433 BYTE lret;
434 WORD bytestopop;
435 WORD nop;
436 } TMAsmProxy;
438 #include "poppack.h"
440 #else /* __i386__ */
441 # warning You need to implement stubless proxies for your architecture
442 typedef struct _TMAsmProxy {
443 } TMAsmProxy;
444 #endif
446 typedef struct _TMProxyImpl {
447 LPVOID *lpvtbl;
448 IRpcProxyBuffer IRpcProxyBuffer_iface;
449 LONG ref;
451 TMAsmProxy *asmstubs;
452 ITypeInfo* tinfo;
453 IRpcChannelBuffer* chanbuf;
454 IID iid;
455 CRITICAL_SECTION crit;
456 IUnknown *outerunknown;
457 IDispatch *dispatch;
458 IRpcProxyBuffer *dispatch_proxy;
459 } TMProxyImpl;
461 static inline TMProxyImpl *impl_from_IRpcProxyBuffer( IRpcProxyBuffer *iface )
463 return CONTAINING_RECORD(iface, TMProxyImpl, IRpcProxyBuffer_iface);
466 static HRESULT WINAPI
467 TMProxyImpl_QueryInterface(LPRPCPROXYBUFFER iface, REFIID riid, LPVOID *ppv)
469 TRACE("()\n");
470 if (IsEqualIID(riid,&IID_IUnknown)||IsEqualIID(riid,&IID_IRpcProxyBuffer)) {
471 *ppv = iface;
472 IRpcProxyBuffer_AddRef(iface);
473 return S_OK;
475 FIXME("no interface for %s\n",debugstr_guid(riid));
476 return E_NOINTERFACE;
479 static ULONG WINAPI
480 TMProxyImpl_AddRef(LPRPCPROXYBUFFER iface)
482 TMProxyImpl *This = impl_from_IRpcProxyBuffer( iface );
483 ULONG refCount = InterlockedIncrement(&This->ref);
485 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
487 return refCount;
490 static ULONG WINAPI
491 TMProxyImpl_Release(LPRPCPROXYBUFFER iface)
493 TMProxyImpl *This = impl_from_IRpcProxyBuffer( iface );
494 ULONG refCount = InterlockedDecrement(&This->ref);
496 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
498 if (!refCount)
500 if (This->dispatch_proxy) IRpcProxyBuffer_Release(This->dispatch_proxy);
501 This->crit.DebugInfo->Spare[0] = 0;
502 DeleteCriticalSection(&This->crit);
503 if (This->chanbuf) IRpcChannelBuffer_Release(This->chanbuf);
504 VirtualFree(This->asmstubs, 0, MEM_RELEASE);
505 HeapFree(GetProcessHeap(), 0, This->lpvtbl);
506 ITypeInfo_Release(This->tinfo);
507 CoTaskMemFree(This);
509 return refCount;
512 static HRESULT WINAPI
513 TMProxyImpl_Connect(
514 LPRPCPROXYBUFFER iface,IRpcChannelBuffer* pRpcChannelBuffer)
516 TMProxyImpl *This = impl_from_IRpcProxyBuffer( iface );
518 TRACE("(%p)\n", pRpcChannelBuffer);
520 EnterCriticalSection(&This->crit);
522 IRpcChannelBuffer_AddRef(pRpcChannelBuffer);
523 This->chanbuf = pRpcChannelBuffer;
525 LeaveCriticalSection(&This->crit);
527 if (This->dispatch_proxy)
529 IRpcChannelBuffer *pDelegateChannel;
530 HRESULT hr = TMarshalDispatchChannel_Create(pRpcChannelBuffer, &This->iid, &pDelegateChannel);
531 if (FAILED(hr))
532 return hr;
533 hr = IRpcProxyBuffer_Connect(This->dispatch_proxy, pDelegateChannel);
534 IRpcChannelBuffer_Release(pDelegateChannel);
535 return hr;
538 return S_OK;
541 static void WINAPI
542 TMProxyImpl_Disconnect(LPRPCPROXYBUFFER iface)
544 TMProxyImpl *This = impl_from_IRpcProxyBuffer( iface );
546 TRACE("()\n");
548 EnterCriticalSection(&This->crit);
550 IRpcChannelBuffer_Release(This->chanbuf);
551 This->chanbuf = NULL;
553 LeaveCriticalSection(&This->crit);
555 if (This->dispatch_proxy)
556 IRpcProxyBuffer_Disconnect(This->dispatch_proxy);
560 static const IRpcProxyBufferVtbl tmproxyvtable = {
561 TMProxyImpl_QueryInterface,
562 TMProxyImpl_AddRef,
563 TMProxyImpl_Release,
564 TMProxyImpl_Connect,
565 TMProxyImpl_Disconnect
568 /* how much space do we use on stack in DWORD steps. */
569 static int
570 _argsize(TYPEDESC *tdesc, ITypeInfo *tinfo) {
571 switch (tdesc->vt) {
572 case VT_I8:
573 case VT_UI8:
574 return 8/sizeof(DWORD);
575 case VT_R8:
576 return sizeof(double)/sizeof(DWORD);
577 case VT_CY:
578 return sizeof(CY)/sizeof(DWORD);
579 case VT_DATE:
580 return sizeof(DATE)/sizeof(DWORD);
581 case VT_DECIMAL:
582 return (sizeof(DECIMAL)+3)/sizeof(DWORD);
583 case VT_VARIANT:
584 return (sizeof(VARIANT)+3)/sizeof(DWORD);
585 case VT_USERDEFINED:
587 ITypeInfo *tinfo2;
588 TYPEATTR *tattr;
589 HRESULT hres;
590 DWORD ret;
592 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
593 if (FAILED(hres))
594 return 0; /* should fail critically in serialize_param */
595 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
596 ret = (tattr->cbSizeInstance+3)/sizeof(DWORD);
597 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
598 ITypeInfo_Release(tinfo2);
599 return ret;
601 default:
602 return 1;
606 /* how much space do we use on the heap (in bytes) */
607 static int
608 _xsize(const TYPEDESC *td, ITypeInfo *tinfo) {
609 switch (td->vt) {
610 case VT_DATE:
611 return sizeof(DATE);
612 case VT_CY:
613 return sizeof(CY);
614 case VT_VARIANT:
615 return sizeof(VARIANT);
616 case VT_CARRAY: {
617 int i, arrsize = 1;
618 const ARRAYDESC *adesc = td->u.lpadesc;
620 for (i=0;i<adesc->cDims;i++)
621 arrsize *= adesc->rgbounds[i].cElements;
622 return arrsize*_xsize(&adesc->tdescElem, tinfo);
624 case VT_UI8:
625 case VT_I8:
626 case VT_R8:
627 return 8;
628 case VT_UI2:
629 case VT_I2:
630 case VT_BOOL:
631 return 2;
632 case VT_UI1:
633 case VT_I1:
634 return 1;
635 case VT_USERDEFINED:
637 ITypeInfo *tinfo2;
638 TYPEATTR *tattr;
639 HRESULT hres;
640 DWORD ret;
642 hres = ITypeInfo_GetRefTypeInfo(tinfo,td->u.hreftype,&tinfo2);
643 if (FAILED(hres))
644 return 0;
645 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
646 ret = tattr->cbSizeInstance;
647 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
648 ITypeInfo_Release(tinfo2);
649 return ret;
651 default:
652 return 4;
656 /* Whether we pass this type by reference or by value */
657 static BOOL
658 _passbyref(const TYPEDESC *td, ITypeInfo *tinfo) {
659 return (td->vt == VT_USERDEFINED ||
660 td->vt == VT_VARIANT ||
661 td->vt == VT_PTR);
664 static HRESULT
665 serialize_param(
666 ITypeInfo *tinfo,
667 BOOL writeit,
668 BOOL debugout,
669 BOOL dealloc,
670 TYPEDESC *tdesc,
671 DWORD *arg,
672 marshal_state *buf)
674 HRESULT hres = S_OK;
675 VARTYPE vartype;
677 TRACE("(tdesc.vt %s)\n",debugstr_vt(tdesc->vt));
679 vartype = tdesc->vt;
680 if ((vartype & 0xf000) == VT_ARRAY)
681 vartype = VT_SAFEARRAY;
683 switch (vartype) {
684 case VT_DATE:
685 case VT_I8:
686 case VT_UI8:
687 case VT_R8:
688 case VT_CY:
689 hres = S_OK;
690 if (debugout) TRACE_(olerelay)("%x%x\n",arg[0],arg[1]);
691 if (writeit)
692 hres = xbuf_add(buf,(LPBYTE)arg,8);
693 return hres;
694 case VT_ERROR:
695 case VT_INT:
696 case VT_UINT:
697 case VT_I4:
698 case VT_R4:
699 case VT_UI4:
700 hres = S_OK;
701 if (debugout) TRACE_(olerelay)("%x\n",*arg);
702 if (writeit)
703 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
704 return hres;
705 case VT_I2:
706 case VT_UI2:
707 case VT_BOOL:
708 hres = S_OK;
709 if (debugout) TRACE_(olerelay)("%04x\n",*arg & 0xffff);
710 if (writeit)
711 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
712 return hres;
713 case VT_I1:
714 case VT_UI1:
715 hres = S_OK;
716 if (debugout) TRACE_(olerelay)("%02x\n",*arg & 0xff);
717 if (writeit)
718 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
719 return hres;
720 case VT_VARIANT: {
721 if (debugout) TRACE_(olerelay)("%s", debugstr_variant((VARIANT *)arg));
722 if (writeit)
724 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
725 ULONG size = VARIANT_UserSize(&flags, buf->curoff, (VARIANT *)arg);
726 xbuf_resize(buf, size);
727 VARIANT_UserMarshal(&flags, buf->base + buf->curoff, (VARIANT *)arg);
728 buf->curoff = size;
730 if (dealloc)
732 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
733 VARIANT_UserFree(&flags, (VARIANT *)arg);
735 return S_OK;
737 case VT_BSTR: {
738 if (writeit && debugout) {
739 if (*arg)
740 TRACE_(olerelay)("%s",relaystr((WCHAR*)*arg));
741 else
742 TRACE_(olerelay)("<bstr NULL>");
744 if (writeit)
746 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
747 ULONG size = BSTR_UserSize(&flags, buf->curoff, (BSTR *)arg);
748 xbuf_resize(buf, size);
749 BSTR_UserMarshal(&flags, buf->base + buf->curoff, (BSTR *)arg);
750 buf->curoff = size;
752 if (dealloc)
754 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
755 BSTR_UserFree(&flags, (BSTR *)arg);
757 return S_OK;
759 case VT_PTR: {
760 DWORD cookie;
761 BOOL derefhere = TRUE;
763 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
764 ITypeInfo *tinfo2;
765 TYPEATTR *tattr;
767 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
768 if (hres) {
769 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
770 return hres;
772 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
773 switch (tattr->typekind) {
774 case TKIND_ALIAS:
775 if (tattr->tdescAlias.vt == VT_USERDEFINED)
777 DWORD href = tattr->tdescAlias.u.hreftype;
778 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
779 ITypeInfo_Release(tinfo2);
780 hres = ITypeInfo_GetRefTypeInfo(tinfo,href,&tinfo2);
781 if (hres) {
782 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
783 return hres;
785 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
786 derefhere = (tattr->typekind != TKIND_DISPATCH && tattr->typekind != TKIND_INTERFACE);
788 break;
789 case TKIND_ENUM: /* confirmed */
790 case TKIND_RECORD: /* FIXME: mostly untested */
791 break;
792 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
793 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
794 derefhere=FALSE;
795 break;
796 default:
797 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
798 derefhere=FALSE;
799 break;
801 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
802 ITypeInfo_Release(tinfo2);
805 if (debugout) TRACE_(olerelay)("*");
806 /* Write always, so the other side knows when it gets a NULL pointer.
808 cookie = *arg ? 0x42424242 : 0;
809 hres = xbuf_add(buf,(LPBYTE)&cookie,sizeof(cookie));
810 if (hres)
811 return hres;
812 if (!*arg) {
813 if (debugout) TRACE_(olerelay)("NULL");
814 return S_OK;
816 hres = serialize_param(tinfo,writeit,debugout,dealloc,tdesc->u.lptdesc,(DWORD*)*arg,buf);
817 if (derefhere && dealloc) HeapFree(GetProcessHeap(),0,(LPVOID)*arg);
818 return hres;
820 case VT_UNKNOWN:
821 if (debugout) TRACE_(olerelay)("unk(0x%x)",*arg);
822 if (writeit)
823 hres = _marshal_interface(buf,&IID_IUnknown,(LPUNKNOWN)*arg);
824 if (dealloc && *(IUnknown **)arg)
825 IUnknown_Release((LPUNKNOWN)*arg);
826 return hres;
827 case VT_DISPATCH:
828 if (debugout) TRACE_(olerelay)("idisp(0x%x)",*arg);
829 if (writeit)
830 hres = _marshal_interface(buf,&IID_IDispatch,(LPUNKNOWN)*arg);
831 if (dealloc && *(IUnknown **)arg)
832 IUnknown_Release((LPUNKNOWN)*arg);
833 return hres;
834 case VT_VOID:
835 if (debugout) TRACE_(olerelay)("<void>");
836 return S_OK;
837 case VT_USERDEFINED: {
838 ITypeInfo *tinfo2;
839 TYPEATTR *tattr;
841 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
842 if (hres) {
843 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
844 return hres;
846 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
847 switch (tattr->typekind) {
848 case TKIND_DISPATCH:
849 case TKIND_INTERFACE:
850 if (writeit)
851 hres=_marshal_interface(buf,&(tattr->guid),(LPUNKNOWN)arg);
852 if (dealloc)
853 IUnknown_Release((LPUNKNOWN)arg);
854 break;
855 case TKIND_RECORD: {
856 int i;
857 if (debugout) TRACE_(olerelay)("{");
858 for (i=0;i<tattr->cVars;i++) {
859 VARDESC *vdesc;
860 ELEMDESC *elem2;
861 TYPEDESC *tdesc2;
863 hres = ITypeInfo_GetVarDesc(tinfo2, i, &vdesc);
864 if (hres) {
865 ERR("Could not get vardesc of %d\n",i);
866 return hres;
868 elem2 = &vdesc->elemdescVar;
869 tdesc2 = &elem2->tdesc;
870 hres = serialize_param(
871 tinfo2,
872 writeit,
873 debugout,
874 dealloc,
875 tdesc2,
876 (DWORD*)(((LPBYTE)arg)+vdesc->u.oInst),
879 ITypeInfo_ReleaseVarDesc(tinfo2, vdesc);
880 if (hres!=S_OK)
881 return hres;
882 if (debugout && (i<(tattr->cVars-1)))
883 TRACE_(olerelay)(",");
885 if (debugout) TRACE_(olerelay)("}");
886 break;
888 case TKIND_ALIAS:
889 hres = serialize_param(tinfo2,writeit,debugout,dealloc,&tattr->tdescAlias,arg,buf);
890 break;
891 case TKIND_ENUM:
892 hres = S_OK;
893 if (debugout) TRACE_(olerelay)("%x",*arg);
894 if (writeit)
895 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
896 break;
897 default:
898 FIXME("Unhandled typekind %d\n",tattr->typekind);
899 hres = E_FAIL;
900 break;
902 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
903 ITypeInfo_Release(tinfo2);
904 return hres;
906 case VT_CARRAY: {
907 ARRAYDESC *adesc = tdesc->u.lpadesc;
908 int i, arrsize = 1;
910 if (debugout) TRACE_(olerelay)("carr");
911 for (i=0;i<adesc->cDims;i++) {
912 if (debugout) TRACE_(olerelay)("[%d]",adesc->rgbounds[i].cElements);
913 arrsize *= adesc->rgbounds[i].cElements;
915 if (debugout) TRACE_(olerelay)("(vt %s)",debugstr_vt(adesc->tdescElem.vt));
916 if (debugout) TRACE_(olerelay)("[");
917 for (i=0;i<arrsize;i++) {
918 LPBYTE base = _passbyref(&adesc->tdescElem, tinfo) ? (LPBYTE) *arg : (LPBYTE) arg;
919 hres = serialize_param(tinfo, writeit, debugout, dealloc, &adesc->tdescElem, (DWORD*)((LPBYTE)base+i*_xsize(&adesc->tdescElem, tinfo)), buf);
920 if (hres)
921 return hres;
922 if (debugout && (i<arrsize-1)) TRACE_(olerelay)(",");
924 if (debugout) TRACE_(olerelay)("]");
925 if (dealloc)
926 HeapFree(GetProcessHeap(), 0, *(void **)arg);
927 return S_OK;
929 case VT_SAFEARRAY: {
930 if (writeit)
932 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
933 ULONG size = LPSAFEARRAY_UserSize(&flags, buf->curoff, (LPSAFEARRAY *)arg);
934 xbuf_resize(buf, size);
935 LPSAFEARRAY_UserMarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
936 buf->curoff = size;
938 if (dealloc)
940 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
941 LPSAFEARRAY_UserFree(&flags, (LPSAFEARRAY *)arg);
943 return S_OK;
945 default:
946 ERR("Unhandled marshal type %d.\n",tdesc->vt);
947 return S_OK;
951 static HRESULT
952 deserialize_param(
953 ITypeInfo *tinfo,
954 BOOL readit,
955 BOOL debugout,
956 BOOL alloc,
957 TYPEDESC *tdesc,
958 DWORD *arg,
959 marshal_state *buf)
961 HRESULT hres = S_OK;
962 VARTYPE vartype;
964 TRACE("vt %s at %p\n",debugstr_vt(tdesc->vt),arg);
966 vartype = tdesc->vt;
967 if ((vartype & 0xf000) == VT_ARRAY)
968 vartype = VT_SAFEARRAY;
970 while (1) {
971 switch (vartype) {
972 case VT_VARIANT: {
973 if (readit)
975 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
976 unsigned char *buffer;
977 buffer = VARIANT_UserUnmarshal(&flags, buf->base + buf->curoff, (VARIANT *)arg);
978 buf->curoff = buffer - buf->base;
980 return S_OK;
982 case VT_DATE:
983 case VT_I8:
984 case VT_UI8:
985 case VT_R8:
986 case VT_CY:
987 if (readit) {
988 hres = xbuf_get(buf,(LPBYTE)arg,8);
989 if (hres) ERR("Failed to read integer 8 byte\n");
991 if (debugout) TRACE_(olerelay)("%x%x",arg[0],arg[1]);
992 return hres;
993 case VT_ERROR:
994 case VT_I4:
995 case VT_INT:
996 case VT_UINT:
997 case VT_R4:
998 case VT_UI4:
999 if (readit) {
1000 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
1001 if (hres) ERR("Failed to read integer 4 byte\n");
1003 if (debugout) TRACE_(olerelay)("%x",*arg);
1004 return hres;
1005 case VT_I2:
1006 case VT_UI2:
1007 case VT_BOOL:
1008 if (readit) {
1009 DWORD x;
1010 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
1011 if (hres) ERR("Failed to read integer 4 byte\n");
1012 memcpy(arg,&x,2);
1014 if (debugout) TRACE_(olerelay)("%04x",*arg & 0xffff);
1015 return hres;
1016 case VT_I1:
1017 case VT_UI1:
1018 if (readit) {
1019 DWORD x;
1020 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
1021 if (hres) ERR("Failed to read integer 4 byte\n");
1022 memcpy(arg,&x,1);
1024 if (debugout) TRACE_(olerelay)("%02x",*arg & 0xff);
1025 return hres;
1026 case VT_BSTR: {
1027 if (readit)
1029 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
1030 unsigned char *buffer;
1031 buffer = BSTR_UserUnmarshal(&flags, buf->base + buf->curoff, (BSTR *)arg);
1032 buf->curoff = buffer - buf->base;
1033 if (debugout) TRACE_(olerelay)("%s",debugstr_w(*(BSTR *)arg));
1035 return S_OK;
1037 case VT_PTR: {
1038 DWORD cookie;
1039 BOOL derefhere = TRUE;
1041 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
1042 ITypeInfo *tinfo2;
1043 TYPEATTR *tattr;
1045 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
1046 if (hres) {
1047 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
1048 return hres;
1050 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1051 switch (tattr->typekind) {
1052 case TKIND_ALIAS:
1053 if (tattr->tdescAlias.vt == VT_USERDEFINED)
1055 DWORD href = tattr->tdescAlias.u.hreftype;
1056 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
1057 ITypeInfo_Release(tinfo2);
1058 hres = ITypeInfo_GetRefTypeInfo(tinfo,href,&tinfo2);
1059 if (hres) {
1060 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
1061 return hres;
1063 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1064 derefhere = (tattr->typekind != TKIND_DISPATCH && tattr->typekind != TKIND_INTERFACE);
1066 break;
1067 case TKIND_ENUM: /* confirmed */
1068 case TKIND_RECORD: /* FIXME: mostly untested */
1069 break;
1070 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
1071 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
1072 derefhere=FALSE;
1073 break;
1074 default:
1075 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
1076 derefhere=FALSE;
1077 break;
1079 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1080 ITypeInfo_Release(tinfo2);
1082 /* read it in all cases, we need to know if we have
1083 * NULL pointer or not.
1085 hres = xbuf_get(buf,(LPBYTE)&cookie,sizeof(cookie));
1086 if (hres) {
1087 ERR("Failed to load pointer cookie.\n");
1088 return hres;
1090 if (cookie != 0x42424242) {
1091 /* we read a NULL ptr from the remote side */
1092 if (debugout) TRACE_(olerelay)("NULL");
1093 *arg = 0;
1094 return S_OK;
1096 if (debugout) TRACE_(olerelay)("*");
1097 if (alloc) {
1098 /* Allocate space for the referenced struct */
1099 if (derefhere)
1100 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,_xsize(tdesc->u.lptdesc, tinfo));
1102 if (derefhere)
1103 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, (LPDWORD)*arg, buf);
1104 else
1105 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, arg, buf);
1107 case VT_UNKNOWN:
1108 /* FIXME: UNKNOWN is unknown ..., but allocate 4 byte for it */
1109 if (alloc)
1110 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
1111 hres = S_OK;
1112 if (readit)
1113 hres = _unmarshal_interface(buf,&IID_IUnknown,(LPUNKNOWN*)arg);
1114 if (debugout)
1115 TRACE_(olerelay)("unk(%p)",arg);
1116 return hres;
1117 case VT_DISPATCH:
1118 hres = S_OK;
1119 if (readit)
1120 hres = _unmarshal_interface(buf,&IID_IDispatch,(LPUNKNOWN*)arg);
1121 if (debugout)
1122 TRACE_(olerelay)("idisp(%p)",arg);
1123 return hres;
1124 case VT_VOID:
1125 if (debugout) TRACE_(olerelay)("<void>");
1126 return S_OK;
1127 case VT_USERDEFINED: {
1128 ITypeInfo *tinfo2;
1129 TYPEATTR *tattr;
1131 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
1132 if (hres) {
1133 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
1134 return hres;
1136 hres = ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1137 if (hres) {
1138 ERR("Could not get typeattr in VT_USERDEFINED.\n");
1139 } else {
1140 switch (tattr->typekind) {
1141 case TKIND_DISPATCH:
1142 case TKIND_INTERFACE:
1143 if (readit)
1144 hres = _unmarshal_interface(buf,&(tattr->guid),(LPUNKNOWN*)arg);
1145 break;
1146 case TKIND_RECORD: {
1147 int i;
1149 if (debugout) TRACE_(olerelay)("{");
1150 for (i=0;i<tattr->cVars;i++) {
1151 VARDESC *vdesc;
1153 hres = ITypeInfo_GetVarDesc(tinfo2, i, &vdesc);
1154 if (hres) {
1155 ERR("Could not get vardesc of %d\n",i);
1156 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1157 ITypeInfo_Release(tinfo2);
1158 return hres;
1160 hres = deserialize_param(
1161 tinfo2,
1162 readit,
1163 debugout,
1164 alloc,
1165 &vdesc->elemdescVar.tdesc,
1166 (DWORD*)(((LPBYTE)arg)+vdesc->u.oInst),
1169 ITypeInfo_ReleaseVarDesc(tinfo2, vdesc);
1170 if (debugout && (i<tattr->cVars-1)) TRACE_(olerelay)(",");
1172 if (debugout) TRACE_(olerelay)("}");
1173 break;
1175 case TKIND_ALIAS:
1176 hres = deserialize_param(tinfo2,readit,debugout,alloc,&tattr->tdescAlias,arg,buf);
1177 break;
1178 case TKIND_ENUM:
1179 if (readit) {
1180 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
1181 if (hres) ERR("Failed to read enum (4 byte)\n");
1183 if (debugout) TRACE_(olerelay)("%x",*arg);
1184 break;
1185 default:
1186 ERR("Unhandled typekind %d\n",tattr->typekind);
1187 hres = E_FAIL;
1188 break;
1190 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1192 if (hres)
1193 ERR("failed to stuballoc in TKIND_RECORD.\n");
1194 ITypeInfo_Release(tinfo2);
1195 return hres;
1197 case VT_CARRAY: {
1198 /* arg is pointing to the start of the array. */
1199 LPBYTE base = (LPBYTE) arg;
1200 ARRAYDESC *adesc = tdesc->u.lpadesc;
1201 int arrsize,i;
1202 arrsize = 1;
1203 if (adesc->cDims > 1) FIXME("cDims > 1 in VT_CARRAY. Does it work?\n");
1204 for (i=0;i<adesc->cDims;i++)
1205 arrsize *= adesc->rgbounds[i].cElements;
1206 if (_passbyref(&adesc->tdescElem, tinfo))
1208 base = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,_xsize(tdesc->u.lptdesc, tinfo) * arrsize);
1209 *arg = (DWORD) base;
1211 for (i=0;i<arrsize;i++)
1212 deserialize_param(
1213 tinfo,
1214 readit,
1215 debugout,
1216 alloc,
1217 &adesc->tdescElem,
1218 (DWORD*)(base + i*_xsize(&adesc->tdescElem, tinfo)),
1221 return S_OK;
1223 case VT_SAFEARRAY: {
1224 if (readit)
1226 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
1227 unsigned char *buffer;
1228 buffer = LPSAFEARRAY_UserUnmarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
1229 buf->curoff = buffer - buf->base;
1231 return S_OK;
1233 default:
1234 ERR("No handler for VT type %d!\n",tdesc->vt);
1235 return S_OK;
1240 /* Retrieves a function's funcdesc, searching back into inherited interfaces. */
1241 static HRESULT get_funcdesc(ITypeInfo *tinfo, int iMethod, ITypeInfo **tactual, const FUNCDESC **fdesc,
1242 BSTR *iname, BSTR *fname, UINT *num)
1244 HRESULT hr;
1245 UINT i, impl_types;
1246 UINT inherited_funcs = 0;
1247 TYPEATTR *attr;
1249 if (fname) *fname = NULL;
1250 if (iname) *iname = NULL;
1251 if (num) *num = 0;
1252 *tactual = NULL;
1254 hr = ITypeInfo_GetTypeAttr(tinfo, &attr);
1255 if (FAILED(hr))
1257 ERR("GetTypeAttr failed with %x\n",hr);
1258 return hr;
1261 if(attr->typekind == TKIND_DISPATCH)
1263 if(attr->wTypeFlags & TYPEFLAG_FDUAL)
1265 HREFTYPE href;
1266 ITypeInfo *tinfo2;
1268 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, -1, &href);
1269 if(FAILED(hr))
1271 ERR("Cannot get interface href from dual dispinterface\n");
1272 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1273 return hr;
1275 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
1276 if(FAILED(hr))
1278 ERR("Cannot get interface from dual dispinterface\n");
1279 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1280 return hr;
1282 hr = get_funcdesc(tinfo2, iMethod, tactual, fdesc, iname, fname, num);
1283 ITypeInfo_Release(tinfo2);
1284 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1285 return hr;
1287 ERR("Shouldn't be called with a non-dual dispinterface\n");
1288 return E_FAIL;
1291 impl_types = attr->cImplTypes;
1292 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1294 for (i = 0; i < impl_types; i++)
1296 HREFTYPE href;
1297 ITypeInfo *pSubTypeInfo;
1298 UINT sub_funcs;
1300 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, i, &href);
1301 if (FAILED(hr)) return hr;
1302 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &pSubTypeInfo);
1303 if (FAILED(hr)) return hr;
1305 hr = get_funcdesc(pSubTypeInfo, iMethod, tactual, fdesc, iname, fname, &sub_funcs);
1306 inherited_funcs += sub_funcs;
1307 ITypeInfo_Release(pSubTypeInfo);
1308 if(SUCCEEDED(hr)) return hr;
1310 if(iMethod < inherited_funcs)
1312 ERR("shouldn't be here\n");
1313 return E_INVALIDARG;
1316 for(i = inherited_funcs; i <= iMethod; i++)
1318 hr = ITypeInfoImpl_GetInternalFuncDesc(tinfo, i - inherited_funcs, fdesc);
1319 if(FAILED(hr))
1321 if(num) *num = i;
1322 return hr;
1326 /* found it. We don't care about num so zero it */
1327 if(num) *num = 0;
1328 *tactual = tinfo;
1329 ITypeInfo_AddRef(*tactual);
1330 if (fname) ITypeInfo_GetDocumentation(tinfo,(*fdesc)->memid,fname,NULL,NULL,NULL);
1331 if (iname) ITypeInfo_GetDocumentation(tinfo,-1,iname,NULL,NULL,NULL);
1332 return S_OK;
1335 static inline BOOL is_in_elem(const ELEMDESC *elem)
1337 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN || !elem->u.paramdesc.wParamFlags);
1340 static inline BOOL is_out_elem(const ELEMDESC *elem)
1342 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT || !elem->u.paramdesc.wParamFlags);
1345 static DWORD WINAPI xCall(int method, void **args)
1347 TMProxyImpl *tpinfo = args[0];
1348 DWORD *xargs;
1349 const FUNCDESC *fdesc;
1350 HRESULT hres;
1351 int i;
1352 marshal_state buf;
1353 RPCOLEMESSAGE msg;
1354 ULONG status;
1355 BSTR fname,iname;
1356 BSTR names[10];
1357 UINT nrofnames;
1358 DWORD remoteresult = 0;
1359 ITypeInfo *tinfo;
1360 IRpcChannelBuffer *chanbuf;
1362 EnterCriticalSection(&tpinfo->crit);
1364 hres = get_funcdesc(tpinfo->tinfo,method,&tinfo,&fdesc,&iname,&fname,NULL);
1365 if (hres) {
1366 ERR("Did not find typeinfo/funcdesc entry for method %d!\n",method);
1367 LeaveCriticalSection(&tpinfo->crit);
1368 return E_FAIL;
1371 if (!tpinfo->chanbuf)
1373 WARN("Tried to use disconnected proxy\n");
1374 ITypeInfo_Release(tinfo);
1375 LeaveCriticalSection(&tpinfo->crit);
1376 return RPC_E_DISCONNECTED;
1378 chanbuf = tpinfo->chanbuf;
1379 IRpcChannelBuffer_AddRef(chanbuf);
1381 LeaveCriticalSection(&tpinfo->crit);
1383 if (TRACE_ON(olerelay)) {
1384 TRACE_(olerelay)("->");
1385 if (iname)
1386 TRACE_(olerelay)("%s:",relaystr(iname));
1387 if (fname)
1388 TRACE_(olerelay)("%s(%d)",relaystr(fname),method);
1389 else
1390 TRACE_(olerelay)("%d",method);
1391 TRACE_(olerelay)("(");
1394 SysFreeString(iname);
1395 SysFreeString(fname);
1397 memset(&buf,0,sizeof(buf));
1399 /* normal typelib driven serializing */
1401 /* Need them for hack below */
1402 memset(names,0,sizeof(names));
1403 if (ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames))
1404 nrofnames = 0;
1405 if (nrofnames > sizeof(names)/sizeof(names[0]))
1406 ERR("Need more names!\n");
1408 xargs = (DWORD *)(args + 1);
1409 for (i=0;i<fdesc->cParams;i++) {
1410 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1411 if (TRACE_ON(olerelay)) {
1412 if (i) TRACE_(olerelay)(",");
1413 if (i+1<nrofnames && names[i+1])
1414 TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1416 /* No need to marshal other data than FIN and any VT_PTR. */
1417 if (!is_in_elem(elem))
1419 if (elem->tdesc.vt != VT_PTR)
1421 xargs+=_argsize(&elem->tdesc, tinfo);
1422 TRACE_(olerelay)("[out]");
1423 continue;
1425 else
1427 memset( *(void **)xargs, 0, _xsize( elem->tdesc.u.lptdesc, tinfo ) );
1431 hres = serialize_param(
1432 tinfo,
1433 is_in_elem(elem),
1434 TRACE_ON(olerelay),
1435 FALSE,
1436 &elem->tdesc,
1437 xargs,
1438 &buf
1441 if (hres) {
1442 ERR("Failed to serialize param, hres %x\n",hres);
1443 break;
1445 xargs+=_argsize(&elem->tdesc, tinfo);
1447 TRACE_(olerelay)(")");
1449 memset(&msg,0,sizeof(msg));
1450 msg.cbBuffer = buf.curoff;
1451 msg.iMethod = method;
1452 hres = IRpcChannelBuffer_GetBuffer(chanbuf,&msg,&(tpinfo->iid));
1453 if (hres) {
1454 ERR("RpcChannelBuffer GetBuffer failed, %x\n",hres);
1455 goto exit;
1457 memcpy(msg.Buffer,buf.base,buf.curoff);
1458 TRACE_(olerelay)("\n");
1459 hres = IRpcChannelBuffer_SendReceive(chanbuf,&msg,&status);
1460 if (hres) {
1461 ERR("RpcChannelBuffer SendReceive failed, %x\n",hres);
1462 goto exit;
1465 TRACE_(olerelay)(" status = %08x (",status);
1466 if (buf.base)
1467 buf.base = HeapReAlloc(GetProcessHeap(),0,buf.base,msg.cbBuffer);
1468 else
1469 buf.base = HeapAlloc(GetProcessHeap(),0,msg.cbBuffer);
1470 buf.size = msg.cbBuffer;
1471 memcpy(buf.base,msg.Buffer,buf.size);
1472 buf.curoff = 0;
1474 /* generic deserializer using typelib description */
1475 xargs = (DWORD *)(args + 1);
1476 status = S_OK;
1477 for (i=0;i<fdesc->cParams;i++) {
1478 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1480 if (i) TRACE_(olerelay)(",");
1481 if (i+1<nrofnames && names[i+1]) TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1483 /* No need to marshal other data than FOUT and any VT_PTR */
1484 if (!is_out_elem(elem) && (elem->tdesc.vt != VT_PTR)) {
1485 xargs += _argsize(&elem->tdesc, tinfo);
1486 TRACE_(olerelay)("[in]");
1487 continue;
1489 hres = deserialize_param(
1490 tinfo,
1491 is_out_elem(elem),
1492 TRACE_ON(olerelay),
1493 FALSE,
1494 &(elem->tdesc),
1495 xargs,
1496 &buf
1498 if (hres) {
1499 ERR("Failed to unmarshall param, hres %x\n",hres);
1500 status = hres;
1501 break;
1503 xargs += _argsize(&elem->tdesc, tinfo);
1506 hres = xbuf_get(&buf, (LPBYTE)&remoteresult, sizeof(DWORD));
1507 if (hres != S_OK)
1508 goto exit;
1509 TRACE_(olerelay)(") = %08x\n", remoteresult);
1511 hres = remoteresult;
1513 exit:
1514 IRpcChannelBuffer_FreeBuffer(chanbuf,&msg);
1515 for (i = 0; i < nrofnames; i++)
1516 SysFreeString(names[i]);
1517 HeapFree(GetProcessHeap(),0,buf.base);
1518 IRpcChannelBuffer_Release(chanbuf);
1519 ITypeInfo_Release(tinfo);
1520 TRACE("-- 0x%08x\n", hres);
1521 return hres;
1524 static HRESULT WINAPI ProxyIUnknown_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
1526 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1528 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1530 if (proxy->outerunknown)
1531 return IUnknown_QueryInterface(proxy->outerunknown, riid, ppv);
1533 FIXME("No interface\n");
1534 return E_NOINTERFACE;
1537 static ULONG WINAPI ProxyIUnknown_AddRef(IUnknown *iface)
1539 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1541 TRACE("\n");
1543 if (proxy->outerunknown)
1544 return IUnknown_AddRef(proxy->outerunknown);
1546 return 2; /* FIXME */
1549 static ULONG WINAPI ProxyIUnknown_Release(IUnknown *iface)
1551 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1553 TRACE("\n");
1555 if (proxy->outerunknown)
1556 return IUnknown_Release(proxy->outerunknown);
1558 return 1; /* FIXME */
1561 static HRESULT WINAPI ProxyIDispatch_GetTypeInfoCount(LPDISPATCH iface, UINT * pctinfo)
1563 TMProxyImpl *This = (TMProxyImpl *)iface;
1565 TRACE("(%p)\n", pctinfo);
1567 return IDispatch_GetTypeInfoCount(This->dispatch, pctinfo);
1570 static HRESULT WINAPI ProxyIDispatch_GetTypeInfo(LPDISPATCH iface, UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo)
1572 TMProxyImpl *This = (TMProxyImpl *)iface;
1574 TRACE("(%d, %x, %p)\n", iTInfo, lcid, ppTInfo);
1576 return IDispatch_GetTypeInfo(This->dispatch, iTInfo, lcid, ppTInfo);
1579 static HRESULT WINAPI ProxyIDispatch_GetIDsOfNames(LPDISPATCH iface, REFIID riid, LPOLESTR * rgszNames, UINT cNames, LCID lcid, DISPID * rgDispId)
1581 TMProxyImpl *This = (TMProxyImpl *)iface;
1583 TRACE("(%s, %p, %d, 0x%x, %p)\n", debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);
1585 return IDispatch_GetIDsOfNames(This->dispatch, riid, rgszNames,
1586 cNames, lcid, rgDispId);
1589 static HRESULT WINAPI ProxyIDispatch_Invoke(LPDISPATCH iface, DISPID dispIdMember, REFIID riid, LCID lcid,
1590 WORD wFlags, DISPPARAMS * pDispParams, VARIANT * pVarResult,
1591 EXCEPINFO * pExcepInfo, UINT * puArgErr)
1593 TMProxyImpl *This = (TMProxyImpl *)iface;
1595 TRACE("(%d, %s, 0x%x, 0x%x, %p, %p, %p, %p)\n", dispIdMember,
1596 debugstr_guid(riid), lcid, wFlags, pDispParams, pVarResult,
1597 pExcepInfo, puArgErr);
1599 return IDispatch_Invoke(This->dispatch, dispIdMember, riid, lcid,
1600 wFlags, pDispParams, pVarResult, pExcepInfo,
1601 puArgErr);
1604 typedef struct
1606 IRpcChannelBuffer IRpcChannelBuffer_iface;
1607 LONG refs;
1608 /* the IDispatch-derived interface we are handling */
1609 IID tmarshal_iid;
1610 IRpcChannelBuffer *pDelegateChannel;
1611 } TMarshalDispatchChannel;
1613 static inline TMarshalDispatchChannel *impl_from_IRpcChannelBuffer(IRpcChannelBuffer *iface)
1615 return CONTAINING_RECORD(iface, TMarshalDispatchChannel, IRpcChannelBuffer_iface);
1618 static HRESULT WINAPI TMarshalDispatchChannel_QueryInterface(IRpcChannelBuffer *iface, REFIID riid, LPVOID *ppv)
1620 *ppv = NULL;
1621 if (IsEqualIID(riid,&IID_IRpcChannelBuffer) || IsEqualIID(riid,&IID_IUnknown))
1623 *ppv = iface;
1624 IRpcChannelBuffer_AddRef(iface);
1625 return S_OK;
1627 return E_NOINTERFACE;
1630 static ULONG WINAPI TMarshalDispatchChannel_AddRef(LPRPCCHANNELBUFFER iface)
1632 TMarshalDispatchChannel *This = impl_from_IRpcChannelBuffer(iface);
1633 return InterlockedIncrement(&This->refs);
1636 static ULONG WINAPI TMarshalDispatchChannel_Release(LPRPCCHANNELBUFFER iface)
1638 TMarshalDispatchChannel *This = impl_from_IRpcChannelBuffer(iface);
1639 ULONG ref;
1641 ref = InterlockedDecrement(&This->refs);
1642 if (ref)
1643 return ref;
1645 IRpcChannelBuffer_Release(This->pDelegateChannel);
1646 HeapFree(GetProcessHeap(), 0, This);
1647 return 0;
1650 static HRESULT WINAPI TMarshalDispatchChannel_GetBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg, REFIID riid)
1652 TMarshalDispatchChannel *This = impl_from_IRpcChannelBuffer(iface);
1653 TRACE("(%p, %s)\n", olemsg, debugstr_guid(riid));
1654 /* Note: we are pretending to invoke a method on the interface identified
1655 * by tmarshal_iid so that we can re-use the IDispatch proxy/stub code
1656 * without the RPC runtime getting confused by not exporting an IDispatch interface */
1657 return IRpcChannelBuffer_GetBuffer(This->pDelegateChannel, olemsg, &This->tmarshal_iid);
1660 static HRESULT WINAPI TMarshalDispatchChannel_SendReceive(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE *olemsg, ULONG *pstatus)
1662 TMarshalDispatchChannel *This = impl_from_IRpcChannelBuffer(iface);
1663 TRACE("(%p, %p)\n", olemsg, pstatus);
1664 return IRpcChannelBuffer_SendReceive(This->pDelegateChannel, olemsg, pstatus);
1667 static HRESULT WINAPI TMarshalDispatchChannel_FreeBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg)
1669 TMarshalDispatchChannel *This = impl_from_IRpcChannelBuffer(iface);
1670 TRACE("(%p)\n", olemsg);
1671 return IRpcChannelBuffer_FreeBuffer(This->pDelegateChannel, olemsg);
1674 static HRESULT WINAPI TMarshalDispatchChannel_GetDestCtx(LPRPCCHANNELBUFFER iface, DWORD* pdwDestContext, void** ppvDestContext)
1676 TMarshalDispatchChannel *This = impl_from_IRpcChannelBuffer(iface);
1677 TRACE("(%p,%p)\n", pdwDestContext, ppvDestContext);
1678 return IRpcChannelBuffer_GetDestCtx(This->pDelegateChannel, pdwDestContext, ppvDestContext);
1681 static HRESULT WINAPI TMarshalDispatchChannel_IsConnected(LPRPCCHANNELBUFFER iface)
1683 TMarshalDispatchChannel *This = impl_from_IRpcChannelBuffer(iface);
1684 TRACE("()\n");
1685 return IRpcChannelBuffer_IsConnected(This->pDelegateChannel);
1688 static const IRpcChannelBufferVtbl TMarshalDispatchChannelVtbl =
1690 TMarshalDispatchChannel_QueryInterface,
1691 TMarshalDispatchChannel_AddRef,
1692 TMarshalDispatchChannel_Release,
1693 TMarshalDispatchChannel_GetBuffer,
1694 TMarshalDispatchChannel_SendReceive,
1695 TMarshalDispatchChannel_FreeBuffer,
1696 TMarshalDispatchChannel_GetDestCtx,
1697 TMarshalDispatchChannel_IsConnected
1700 static HRESULT TMarshalDispatchChannel_Create(
1701 IRpcChannelBuffer *pDelegateChannel, REFIID tmarshal_riid,
1702 IRpcChannelBuffer **ppChannel)
1704 TMarshalDispatchChannel *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1705 if (!This)
1706 return E_OUTOFMEMORY;
1708 This->IRpcChannelBuffer_iface.lpVtbl = &TMarshalDispatchChannelVtbl;
1709 This->refs = 1;
1710 IRpcChannelBuffer_AddRef(pDelegateChannel);
1711 This->pDelegateChannel = pDelegateChannel;
1712 This->tmarshal_iid = *tmarshal_riid;
1714 *ppChannel = &This->IRpcChannelBuffer_iface;
1715 return S_OK;
1719 static inline HRESULT get_facbuf_for_iid(REFIID riid, IPSFactoryBuffer **facbuf)
1721 HRESULT hr;
1722 CLSID clsid;
1724 if ((hr = CoGetPSClsid(riid, &clsid)))
1725 return hr;
1726 return CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL,
1727 &IID_IPSFactoryBuffer, (LPVOID*)facbuf);
1730 static HRESULT init_proxy_entry_point(TMProxyImpl *proxy, unsigned int num)
1732 int j;
1733 /* nrofargs including This */
1734 int nrofargs = 1;
1735 ITypeInfo *tinfo2;
1736 TMAsmProxy *xasm = proxy->asmstubs + num;
1737 HRESULT hres;
1738 const FUNCDESC *fdesc;
1740 hres = get_funcdesc(proxy->tinfo, num, &tinfo2, &fdesc, NULL, NULL, NULL);
1741 if (hres) {
1742 ERR("GetFuncDesc %x should not fail here.\n",hres);
1743 return hres;
1745 ITypeInfo_Release(tinfo2);
1746 /* some args take more than 4 byte on the stack */
1747 for (j=0;j<fdesc->cParams;j++)
1748 nrofargs += _argsize(&fdesc->lprgelemdescParam[j].tdesc, proxy->tinfo);
1750 #ifdef __i386__
1751 if (fdesc->callconv != CC_STDCALL) {
1752 ERR("calling convention is not stdcall????\n");
1753 return E_FAIL;
1755 /* leal 4(%esp),%eax
1756 * pushl %eax
1757 * pushl <nr>
1758 * call xCall
1759 * lret <nr>
1761 xasm->lealeax = 0x0424448d;
1762 xasm->pushleax = 0x50;
1763 xasm->pushlval = 0x68;
1764 xasm->nr = num;
1765 xasm->lcall = 0xe8;
1766 xasm->xcall = (char *)xCall - (char *)&xasm->lret;
1767 xasm->lret = 0xc2;
1768 xasm->bytestopop = nrofargs * 4;
1769 xasm->nop = 0x9090;
1770 proxy->lpvtbl[fdesc->oVft / sizeof(void *)] = xasm;
1771 #else
1772 FIXME("not implemented on non i386\n");
1773 return E_FAIL;
1774 #endif
1775 return S_OK;
1778 static HRESULT WINAPI
1779 PSFacBuf_CreateProxy(
1780 LPPSFACTORYBUFFER iface, IUnknown* pUnkOuter, REFIID riid,
1781 IRpcProxyBuffer **ppProxy, LPVOID *ppv)
1783 HRESULT hres;
1784 ITypeInfo *tinfo;
1785 unsigned int i, nroffuncs, vtbl_size;
1786 TMProxyImpl *proxy;
1787 TYPEATTR *typeattr;
1788 BOOL defer_to_dispatch = FALSE;
1790 TRACE("(...%s...)\n",debugstr_guid(riid));
1791 hres = _get_typeinfo_for_iid(riid,&tinfo);
1792 if (hres) {
1793 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
1794 return hres;
1797 hres = num_of_funcs(tinfo, &nroffuncs, &vtbl_size);
1798 TRACE("Got %d funcs, vtbl size %d\n", nroffuncs, vtbl_size);
1800 if (FAILED(hres)) {
1801 ERR("Cannot get number of functions for typeinfo %s\n",debugstr_guid(riid));
1802 ITypeInfo_Release(tinfo);
1803 return hres;
1806 proxy = CoTaskMemAlloc(sizeof(TMProxyImpl));
1807 if (!proxy) return E_OUTOFMEMORY;
1809 proxy->dispatch = NULL;
1810 proxy->dispatch_proxy = NULL;
1811 proxy->outerunknown = pUnkOuter;
1812 proxy->asmstubs = VirtualAlloc(NULL, sizeof(TMAsmProxy) * nroffuncs, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
1813 if (!proxy->asmstubs) {
1814 ERR("Could not commit pages for proxy thunks\n");
1815 CoTaskMemFree(proxy);
1816 return E_OUTOFMEMORY;
1818 proxy->IRpcProxyBuffer_iface.lpVtbl = &tmproxyvtable;
1819 /* one reference for the proxy */
1820 proxy->ref = 1;
1821 proxy->tinfo = tinfo;
1822 proxy->iid = *riid;
1823 proxy->chanbuf = 0;
1825 InitializeCriticalSection(&proxy->crit);
1826 proxy->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TMProxyImpl.crit");
1828 proxy->lpvtbl = HeapAlloc(GetProcessHeap(), 0, vtbl_size);
1830 /* if we derive from IDispatch then defer to its proxy for its methods */
1831 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
1832 if (hres == S_OK)
1834 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
1836 IPSFactoryBuffer *factory_buffer;
1837 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
1838 if (hres == S_OK)
1840 hres = IPSFactoryBuffer_CreateProxy(factory_buffer, NULL,
1841 &IID_IDispatch, &proxy->dispatch_proxy,
1842 (void **)&proxy->dispatch);
1843 IPSFactoryBuffer_Release(factory_buffer);
1845 if ((hres == S_OK) && (nroffuncs < 7))
1847 ERR("nroffuncs calculated incorrectly (%d)\n", nroffuncs);
1848 hres = E_UNEXPECTED;
1850 if (hres == S_OK)
1852 defer_to_dispatch = TRUE;
1855 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
1858 for (i=0;i<nroffuncs;i++) {
1859 switch (i) {
1860 case 0:
1861 proxy->lpvtbl[i] = ProxyIUnknown_QueryInterface;
1862 break;
1863 case 1:
1864 proxy->lpvtbl[i] = ProxyIUnknown_AddRef;
1865 break;
1866 case 2:
1867 proxy->lpvtbl[i] = ProxyIUnknown_Release;
1868 break;
1869 case 3:
1870 if(!defer_to_dispatch) hres = init_proxy_entry_point(proxy, i);
1871 else proxy->lpvtbl[3] = ProxyIDispatch_GetTypeInfoCount;
1872 break;
1873 case 4:
1874 if(!defer_to_dispatch) hres = init_proxy_entry_point(proxy, i);
1875 else proxy->lpvtbl[4] = ProxyIDispatch_GetTypeInfo;
1876 break;
1877 case 5:
1878 if(!defer_to_dispatch) hres = init_proxy_entry_point(proxy, i);
1879 else proxy->lpvtbl[5] = ProxyIDispatch_GetIDsOfNames;
1880 break;
1881 case 6:
1882 if(!defer_to_dispatch) hres = init_proxy_entry_point(proxy, i);
1883 else proxy->lpvtbl[6] = ProxyIDispatch_Invoke;
1884 break;
1885 default:
1886 hres = init_proxy_entry_point(proxy, i);
1890 if (hres == S_OK)
1892 *ppv = proxy;
1893 *ppProxy = &proxy->IRpcProxyBuffer_iface;
1894 IUnknown_AddRef((IUnknown *)*ppv);
1895 return S_OK;
1897 else
1898 TMProxyImpl_Release(&proxy->IRpcProxyBuffer_iface);
1899 return hres;
1902 typedef struct _TMStubImpl {
1903 IRpcStubBuffer IRpcStubBuffer_iface;
1904 LONG ref;
1906 LPUNKNOWN pUnk;
1907 ITypeInfo *tinfo;
1908 IID iid;
1909 IRpcStubBuffer *dispatch_stub;
1910 BOOL dispatch_derivative;
1911 } TMStubImpl;
1913 static inline TMStubImpl *impl_from_IRpcStubBuffer(IRpcStubBuffer *iface)
1915 return CONTAINING_RECORD(iface, TMStubImpl, IRpcStubBuffer_iface);
1918 static HRESULT WINAPI
1919 TMStubImpl_QueryInterface(LPRPCSTUBBUFFER iface, REFIID riid, LPVOID *ppv)
1921 if (IsEqualIID(riid,&IID_IRpcStubBuffer)||IsEqualIID(riid,&IID_IUnknown)){
1922 *ppv = iface;
1923 IRpcStubBuffer_AddRef(iface);
1924 return S_OK;
1926 FIXME("%s, not supported IID.\n",debugstr_guid(riid));
1927 return E_NOINTERFACE;
1930 static ULONG WINAPI
1931 TMStubImpl_AddRef(LPRPCSTUBBUFFER iface)
1933 TMStubImpl *This = impl_from_IRpcStubBuffer(iface);
1934 ULONG refCount = InterlockedIncrement(&This->ref);
1936 TRACE("(%p)->(ref before=%u)\n", This, refCount - 1);
1938 return refCount;
1941 static ULONG WINAPI
1942 TMStubImpl_Release(LPRPCSTUBBUFFER iface)
1944 TMStubImpl *This = impl_from_IRpcStubBuffer(iface);
1945 ULONG refCount = InterlockedDecrement(&This->ref);
1947 TRACE("(%p)->(ref before=%u)\n", This, refCount + 1);
1949 if (!refCount)
1951 IRpcStubBuffer_Disconnect(iface);
1952 ITypeInfo_Release(This->tinfo);
1953 if (This->dispatch_stub)
1954 IRpcStubBuffer_Release(This->dispatch_stub);
1955 CoTaskMemFree(This);
1957 return refCount;
1960 static HRESULT WINAPI
1961 TMStubImpl_Connect(LPRPCSTUBBUFFER iface, LPUNKNOWN pUnkServer)
1963 TMStubImpl *This = impl_from_IRpcStubBuffer(iface);
1965 TRACE("(%p)->(%p)\n", This, pUnkServer);
1967 IUnknown_AddRef(pUnkServer);
1968 This->pUnk = pUnkServer;
1970 if (This->dispatch_stub)
1971 IRpcStubBuffer_Connect(This->dispatch_stub, pUnkServer);
1973 return S_OK;
1976 static void WINAPI
1977 TMStubImpl_Disconnect(LPRPCSTUBBUFFER iface)
1979 TMStubImpl *This = impl_from_IRpcStubBuffer(iface);
1981 TRACE("(%p)->()\n", This);
1983 if (This->pUnk)
1985 IUnknown_Release(This->pUnk);
1986 This->pUnk = NULL;
1989 if (This->dispatch_stub)
1990 IRpcStubBuffer_Disconnect(This->dispatch_stub);
1993 static HRESULT WINAPI
1994 TMStubImpl_Invoke(
1995 LPRPCSTUBBUFFER iface, RPCOLEMESSAGE* xmsg,IRpcChannelBuffer*rpcchanbuf)
1997 #ifdef __i386__
1998 int i;
1999 const FUNCDESC *fdesc;
2000 TMStubImpl *This = impl_from_IRpcStubBuffer(iface);
2001 HRESULT hres;
2002 DWORD *args = NULL, res, *xargs, nrofargs;
2003 marshal_state buf;
2004 UINT nrofnames = 0;
2005 BSTR names[10];
2006 BSTR iname = NULL;
2007 ITypeInfo *tinfo = NULL;
2009 TRACE("...\n");
2011 if (xmsg->iMethod < 3) {
2012 ERR("IUnknown methods cannot be marshaled by the typelib marshaler\n");
2013 return E_UNEXPECTED;
2016 if (This->dispatch_derivative && xmsg->iMethod < sizeof(IDispatchVtbl)/sizeof(void *))
2018 if (!This->dispatch_stub)
2020 IPSFactoryBuffer *factory_buffer;
2021 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
2022 if (hres == S_OK)
2024 hres = IPSFactoryBuffer_CreateStub(factory_buffer, &IID_IDispatch,
2025 This->pUnk, &This->dispatch_stub);
2026 IPSFactoryBuffer_Release(factory_buffer);
2028 if (hres != S_OK)
2029 return hres;
2031 return IRpcStubBuffer_Invoke(This->dispatch_stub, xmsg, rpcchanbuf);
2034 memset(&buf,0,sizeof(buf));
2035 buf.size = xmsg->cbBuffer;
2036 buf.base = HeapAlloc(GetProcessHeap(), 0, xmsg->cbBuffer);
2037 memcpy(buf.base, xmsg->Buffer, xmsg->cbBuffer);
2038 buf.curoff = 0;
2040 hres = get_funcdesc(This->tinfo,xmsg->iMethod,&tinfo,&fdesc,&iname,NULL,NULL);
2041 if (hres) {
2042 ERR("GetFuncDesc on method %d failed with %x\n",xmsg->iMethod,hres);
2043 return hres;
2046 if (iname && !lstrcmpW(iname, IDispatchW))
2048 ERR("IDispatch cannot be marshaled by the typelib marshaler\n");
2049 hres = E_UNEXPECTED;
2050 SysFreeString (iname);
2051 goto exit;
2054 SysFreeString (iname);
2056 /* Need them for hack below */
2057 memset(names,0,sizeof(names));
2058 ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames);
2059 if (nrofnames > sizeof(names)/sizeof(names[0])) {
2060 ERR("Need more names!\n");
2063 /*dump_FUNCDESC(fdesc);*/
2064 nrofargs = 0;
2065 for (i=0;i<fdesc->cParams;i++)
2066 nrofargs += _argsize(&fdesc->lprgelemdescParam[i].tdesc, tinfo);
2067 args = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(nrofargs+1)*sizeof(DWORD));
2068 if (!args)
2070 hres = E_OUTOFMEMORY;
2071 goto exit;
2074 /* Allocate all stuff used by call. */
2075 xargs = args+1;
2076 for (i=0;i<fdesc->cParams;i++) {
2077 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2079 hres = deserialize_param(
2080 tinfo,
2081 is_in_elem(elem),
2082 FALSE,
2083 TRUE,
2084 &(elem->tdesc),
2085 xargs,
2086 &buf
2088 xargs += _argsize(&elem->tdesc, tinfo);
2089 if (hres) {
2090 ERR("Failed to deserialize param %s, hres %x\n",relaystr(names[i+1]),hres);
2091 break;
2095 args[0] = (DWORD)This->pUnk;
2097 __TRY
2099 res = _invoke(
2100 (*((FARPROC**)args[0]))[fdesc->oVft/4],
2101 fdesc->callconv,
2102 (xargs-args),
2103 args
2106 __EXCEPT_ALL
2108 DWORD dwExceptionCode = GetExceptionCode();
2109 ERR("invoke call failed with exception 0x%08x (%d)\n", dwExceptionCode, dwExceptionCode);
2110 if (FAILED(dwExceptionCode))
2111 hres = dwExceptionCode;
2112 else
2113 hres = HRESULT_FROM_WIN32(dwExceptionCode);
2115 __ENDTRY
2117 if (hres != S_OK)
2118 goto exit;
2120 buf.curoff = 0;
2122 xargs = args+1;
2123 for (i=0;i<fdesc->cParams;i++) {
2124 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2125 hres = serialize_param(
2126 tinfo,
2127 is_out_elem(elem),
2128 FALSE,
2129 TRUE,
2130 &elem->tdesc,
2131 xargs,
2132 &buf
2134 xargs += _argsize(&elem->tdesc, tinfo);
2135 if (hres) {
2136 ERR("Failed to stuballoc param, hres %x\n",hres);
2137 break;
2141 hres = xbuf_add (&buf, (LPBYTE)&res, sizeof(DWORD));
2143 if (hres != S_OK)
2144 goto exit;
2146 xmsg->cbBuffer = buf.curoff;
2147 hres = IRpcChannelBuffer_GetBuffer(rpcchanbuf, xmsg, &This->iid);
2148 if (hres != S_OK)
2149 ERR("IRpcChannelBuffer_GetBuffer failed with error 0x%08x\n", hres);
2151 if (hres == S_OK)
2152 memcpy(xmsg->Buffer, buf.base, buf.curoff);
2154 exit:
2155 for (i = 0; i < nrofnames; i++)
2156 SysFreeString(names[i]);
2158 ITypeInfo_Release(tinfo);
2159 HeapFree(GetProcessHeap(), 0, args);
2161 HeapFree(GetProcessHeap(), 0, buf.base);
2163 TRACE("returning\n");
2164 return hres;
2165 #else
2166 FIXME( "not implemented on non-i386\n" );
2167 return E_FAIL;
2168 #endif
2171 static LPRPCSTUBBUFFER WINAPI
2172 TMStubImpl_IsIIDSupported(LPRPCSTUBBUFFER iface, REFIID riid) {
2173 FIXME("Huh (%s)?\n",debugstr_guid(riid));
2174 return NULL;
2177 static ULONG WINAPI
2178 TMStubImpl_CountRefs(LPRPCSTUBBUFFER iface) {
2179 TMStubImpl *This = impl_from_IRpcStubBuffer(iface);
2181 FIXME("()\n");
2182 return This->ref; /*FIXME? */
2185 static HRESULT WINAPI
2186 TMStubImpl_DebugServerQueryInterface(LPRPCSTUBBUFFER iface, LPVOID *ppv) {
2187 return E_NOTIMPL;
2190 static void WINAPI
2191 TMStubImpl_DebugServerRelease(LPRPCSTUBBUFFER iface, LPVOID ppv) {
2192 return;
2195 static const IRpcStubBufferVtbl tmstubvtbl = {
2196 TMStubImpl_QueryInterface,
2197 TMStubImpl_AddRef,
2198 TMStubImpl_Release,
2199 TMStubImpl_Connect,
2200 TMStubImpl_Disconnect,
2201 TMStubImpl_Invoke,
2202 TMStubImpl_IsIIDSupported,
2203 TMStubImpl_CountRefs,
2204 TMStubImpl_DebugServerQueryInterface,
2205 TMStubImpl_DebugServerRelease
2208 static HRESULT WINAPI
2209 PSFacBuf_CreateStub(
2210 LPPSFACTORYBUFFER iface, REFIID riid,IUnknown *pUnkServer,
2211 IRpcStubBuffer** ppStub
2213 HRESULT hres;
2214 ITypeInfo *tinfo;
2215 TMStubImpl *stub;
2216 TYPEATTR *typeattr;
2218 TRACE("(%s,%p,%p)\n",debugstr_guid(riid),pUnkServer,ppStub);
2220 hres = _get_typeinfo_for_iid(riid,&tinfo);
2221 if (hres) {
2222 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
2223 return hres;
2226 stub = CoTaskMemAlloc(sizeof(TMStubImpl));
2227 if (!stub)
2228 return E_OUTOFMEMORY;
2229 stub->IRpcStubBuffer_iface.lpVtbl = &tmstubvtbl;
2230 stub->ref = 1;
2231 stub->tinfo = tinfo;
2232 stub->dispatch_stub = NULL;
2233 stub->dispatch_derivative = FALSE;
2234 stub->iid = *riid;
2235 hres = IRpcStubBuffer_Connect(&stub->IRpcStubBuffer_iface,pUnkServer);
2236 *ppStub = &stub->IRpcStubBuffer_iface;
2237 TRACE("IRpcStubBuffer: %p\n", stub);
2238 if (hres)
2239 ERR("Connect to pUnkServer failed?\n");
2241 /* if we derive from IDispatch then defer to its stub for some of its methods */
2242 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
2243 if (hres == S_OK)
2245 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
2246 stub->dispatch_derivative = TRUE;
2247 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
2250 return hres;
2253 static const IPSFactoryBufferVtbl psfacbufvtbl = {
2254 PSFacBuf_QueryInterface,
2255 PSFacBuf_AddRef,
2256 PSFacBuf_Release,
2257 PSFacBuf_CreateProxy,
2258 PSFacBuf_CreateStub
2261 /* This is the whole PSFactoryBuffer object, just the vtableptr */
2262 static const IPSFactoryBufferVtbl *lppsfac = &psfacbufvtbl;
2264 /***********************************************************************
2265 * TMARSHAL_DllGetClassObject
2267 HRESULT TMARSHAL_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
2269 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)) {
2270 *ppv = &lppsfac;
2271 return S_OK;
2273 return E_NOINTERFACE;