* Sync to trunk HEAD (r53473).
[reactos.git] / dll / win32 / oleaut32 / tmarshal.c
1 /*
2 * TYPELIB Marshaler
3 *
4 * Copyright 2002,2005 Marcus Meissner
5 *
6 * The olerelay debug channel allows you to see calls marshalled by
7 * the typelib marshaller. It is not a generic COM relaying system.
8 *
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.
13 *
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.
18 *
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
22 */
23
24 #include "config.h"
25 #include "wine/port.h"
26
27 #include <assert.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <ctype.h>
33
34 #define COBJMACROS
35 #define NONAMELESSUNION
36 #define NONAMELESSSTRUCT
37
38 #include "winerror.h"
39 #include "windef.h"
40 #include "winbase.h"
41 #include "winnls.h"
42 #include "winreg.h"
43 #include "winuser.h"
44
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"
51
52 static const WCHAR IDispatchW[] = { 'I','D','i','s','p','a','t','c','h',0};
53
54 WINE_DEFAULT_DEBUG_CHANNEL(ole);
55 WINE_DECLARE_DEBUG_CHANNEL(olerelay);
56
57 #define ICOM_THIS_MULTI(impl,field,iface) impl* const This=(impl*)((char*)(iface) - offsetof(impl,field))
58
59 static HRESULT TMarshalDispatchChannel_Create(
60 IRpcChannelBuffer *pDelegateChannel, REFIID tmarshal_riid,
61 IRpcChannelBuffer **ppChannel);
62
63 typedef struct _marshal_state {
64 LPBYTE base;
65 int size;
66 int curoff;
67 } marshal_state;
68
69 /* used in the olerelay code to avoid having the L"" stuff added by debugstr_w */
70 static char *relaystr(WCHAR *in) {
71 char *tmp = (char *)debugstr_w(in);
72 tmp += 2;
73 tmp[strlen(tmp)-1] = '\0';
74 return tmp;
75 }
76
77 static HRESULT
78 xbuf_resize(marshal_state *buf, DWORD newsize)
79 {
80 if(buf->size >= newsize)
81 return S_FALSE;
82
83 if(buf->base)
84 {
85 buf->base = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buf->base, newsize);
86 if(!buf->base)
87 return E_OUTOFMEMORY;
88 }
89 else
90 {
91 buf->base = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, newsize);
92 if(!buf->base)
93 return E_OUTOFMEMORY;
94 }
95 buf->size = newsize;
96 return S_OK;
97 }
98
99 static HRESULT
100 xbuf_add(marshal_state *buf, const BYTE *stuff, DWORD size)
101 {
102 HRESULT hr;
103
104 if(buf->size - buf->curoff < size)
105 {
106 hr = xbuf_resize(buf, buf->size + size + 100);
107 if(FAILED(hr)) return hr;
108 }
109 memcpy(buf->base+buf->curoff,stuff,size);
110 buf->curoff += size;
111 return S_OK;
112 }
113
114 static HRESULT
115 xbuf_get(marshal_state *buf, LPBYTE stuff, DWORD size) {
116 if (buf->size < buf->curoff+size) return E_FAIL;
117 memcpy(stuff,buf->base+buf->curoff,size);
118 buf->curoff += size;
119 return S_OK;
120 }
121
122 static HRESULT
123 xbuf_skip(marshal_state *buf, DWORD size) {
124 if (buf->size < buf->curoff+size) return E_FAIL;
125 buf->curoff += size;
126 return S_OK;
127 }
128
129 static HRESULT
130 _unmarshal_interface(marshal_state *buf, REFIID riid, LPUNKNOWN *pUnk) {
131 IStream *pStm;
132 ULARGE_INTEGER newpos;
133 LARGE_INTEGER seekto;
134 ULONG res;
135 HRESULT hres;
136 DWORD xsize;
137
138 TRACE("...%s...\n",debugstr_guid(riid));
139
140 *pUnk = NULL;
141 hres = xbuf_get(buf,(LPBYTE)&xsize,sizeof(xsize));
142 if (hres) {
143 ERR("xbuf_get failed\n");
144 return hres;
145 }
146
147 if (xsize == 0) return S_OK;
148
149 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
150 if (hres) {
151 ERR("Stream create failed %x\n",hres);
152 return hres;
153 }
154
155 hres = IStream_Write(pStm,buf->base+buf->curoff,xsize,&res);
156 if (hres) {
157 ERR("stream write %x\n",hres);
158 return hres;
159 }
160
161 memset(&seekto,0,sizeof(seekto));
162 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
163 if (hres) {
164 ERR("Failed Seek %x\n",hres);
165 return hres;
166 }
167
168 hres = CoUnmarshalInterface(pStm,riid,(LPVOID*)pUnk);
169 if (hres) {
170 ERR("Unmarshalling interface %s failed with %x\n",debugstr_guid(riid),hres);
171 return hres;
172 }
173
174 IStream_Release(pStm);
175 return xbuf_skip(buf,xsize);
176 }
177
178 static HRESULT
179 _marshal_interface(marshal_state *buf, REFIID riid, LPUNKNOWN pUnk) {
180 LPBYTE tempbuf = NULL;
181 IStream *pStm = NULL;
182 STATSTG ststg;
183 ULARGE_INTEGER newpos;
184 LARGE_INTEGER seekto;
185 ULONG res;
186 DWORD xsize;
187 HRESULT hres;
188
189 if (!pUnk) {
190 /* this is valid, if for instance we serialize
191 * a VT_DISPATCH with NULL ptr which apparently
192 * can happen. S_OK to make sure we continue
193 * serializing.
194 */
195 WARN("pUnk is NULL\n");
196 xsize = 0;
197 return xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
198 }
199
200 hres = E_FAIL;
201
202 TRACE("...%s...\n",debugstr_guid(riid));
203
204 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
205 if (hres) {
206 ERR("Stream create failed %x\n",hres);
207 goto fail;
208 }
209
210 hres = CoMarshalInterface(pStm,riid,pUnk,0,NULL,0);
211 if (hres) {
212 ERR("Marshalling interface %s failed with %x\n", debugstr_guid(riid), hres);
213 goto fail;
214 }
215
216 hres = IStream_Stat(pStm,&ststg,STATFLAG_NONAME);
217 if (hres) {
218 ERR("Stream stat failed\n");
219 goto fail;
220 }
221
222 tempbuf = HeapAlloc(GetProcessHeap(), 0, ststg.cbSize.u.LowPart);
223 memset(&seekto,0,sizeof(seekto));
224 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
225 if (hres) {
226 ERR("Failed Seek %x\n",hres);
227 goto fail;
228 }
229
230 hres = IStream_Read(pStm,tempbuf,ststg.cbSize.u.LowPart,&res);
231 if (hres) {
232 ERR("Failed Read %x\n",hres);
233 goto fail;
234 }
235
236 xsize = ststg.cbSize.u.LowPart;
237 xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
238 hres = xbuf_add(buf,tempbuf,ststg.cbSize.u.LowPart);
239
240 HeapFree(GetProcessHeap(),0,tempbuf);
241 IStream_Release(pStm);
242
243 return hres;
244
245 fail:
246 xsize = 0;
247 xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
248 if (pStm) IUnknown_Release(pStm);
249 HeapFree(GetProcessHeap(), 0, tempbuf);
250 return hres;
251 }
252
253 /********************* OLE Proxy/Stub Factory ********************************/
254 static HRESULT WINAPI
255 PSFacBuf_QueryInterface(LPPSFACTORYBUFFER iface, REFIID iid, LPVOID *ppv) {
256 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)||IsEqualIID(iid,&IID_IUnknown)) {
257 *ppv = iface;
258 /* No ref counting, static class */
259 return S_OK;
260 }
261 FIXME("(%s) unknown IID?\n",debugstr_guid(iid));
262 return E_NOINTERFACE;
263 }
264
265 static ULONG WINAPI PSFacBuf_AddRef(LPPSFACTORYBUFFER iface) { return 2; }
266 static ULONG WINAPI PSFacBuf_Release(LPPSFACTORYBUFFER iface) { return 1; }
267
268 static HRESULT
269 _get_typeinfo_for_iid(REFIID riid, ITypeInfo**ti) {
270 HRESULT hres;
271 HKEY ikey;
272 char tlguid[200],typelibkey[300],interfacekey[300],ver[100];
273 char tlfn[260];
274 OLECHAR tlfnW[260];
275 DWORD tlguidlen, verlen, type;
276 LONG tlfnlen;
277 ITypeLib *tl;
278
279 sprintf( interfacekey, "Interface\\{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\Typelib",
280 riid->Data1, riid->Data2, riid->Data3,
281 riid->Data4[0], riid->Data4[1], riid->Data4[2], riid->Data4[3],
282 riid->Data4[4], riid->Data4[5], riid->Data4[6], riid->Data4[7]
283 );
284
285 if (RegOpenKeyA(HKEY_CLASSES_ROOT,interfacekey,&ikey)) {
286 ERR("No %s key found.\n",interfacekey);
287 return E_FAIL;
288 }
289 tlguidlen = sizeof(tlguid);
290 if (RegQueryValueExA(ikey,NULL,NULL,&type,(LPBYTE)tlguid,&tlguidlen)) {
291 ERR("Getting typelib guid failed.\n");
292 RegCloseKey(ikey);
293 return E_FAIL;
294 }
295 verlen = sizeof(ver);
296 if (RegQueryValueExA(ikey,"Version",NULL,&type,(LPBYTE)ver,&verlen)) {
297 ERR("Could not get version value?\n");
298 RegCloseKey(ikey);
299 return E_FAIL;
300 }
301 RegCloseKey(ikey);
302 sprintf(typelibkey,"Typelib\\%s\\%s\\0\\win%u",tlguid,ver,(sizeof(void*) == 8) ? 64 : 32);
303 tlfnlen = sizeof(tlfn);
304 if (RegQueryValueA(HKEY_CLASSES_ROOT,typelibkey,tlfn,&tlfnlen)) {
305 ERR("Could not get typelib fn?\n");
306 return E_FAIL;
307 }
308 MultiByteToWideChar(CP_ACP, 0, tlfn, -1, tlfnW, sizeof(tlfnW) / sizeof(tlfnW[0]));
309 hres = LoadTypeLib(tlfnW,&tl);
310 if (hres) {
311 ERR("Failed to load typelib for %s, but it should be there.\n",debugstr_guid(riid));
312 return hres;
313 }
314 hres = ITypeLib_GetTypeInfoOfGuid(tl,riid,ti);
315 if (hres) {
316 ERR("typelib does not contain info for %s?\n",debugstr_guid(riid));
317 ITypeLib_Release(tl);
318 return hres;
319 }
320 ITypeLib_Release(tl);
321 return hres;
322 }
323
324 /*
325 * Determine the number of functions including all inherited functions.
326 * Note for non-dual dispinterfaces we simply return the size of IDispatch.
327 */
328 static HRESULT num_of_funcs(ITypeInfo *tinfo, unsigned int *num)
329 {
330 HRESULT hres;
331 TYPEATTR *attr;
332 ITypeInfo *tinfo2;
333
334 *num = 0;
335 hres = ITypeInfo_GetTypeAttr(tinfo, &attr);
336 if (hres) {
337 ERR("GetTypeAttr failed with %x\n",hres);
338 return hres;
339 }
340
341 if(attr->typekind == TKIND_DISPATCH && (attr->wTypeFlags & TYPEFLAG_FDUAL))
342 {
343 HREFTYPE href;
344 hres = ITypeInfo_GetRefTypeOfImplType(tinfo, -1, &href);
345 if(FAILED(hres))
346 {
347 ERR("Unable to get interface href from dual dispinterface\n");
348 goto end;
349 }
350 hres = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
351 if(FAILED(hres))
352 {
353 ERR("Unable to get interface from dual dispinterface\n");
354 goto end;
355 }
356 hres = num_of_funcs(tinfo2, num);
357 ITypeInfo_Release(tinfo2);
358 }
359 else
360 {
361 *num = attr->cbSizeVft / 4;
362 }
363
364 end:
365 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
366 return hres;
367 }
368
369 #ifdef __i386__
370
371 #include "pshpack1.h"
372
373 typedef struct _TMAsmProxy {
374 BYTE popleax;
375 BYTE pushlval;
376 DWORD nr;
377 BYTE pushleax;
378 BYTE lcall;
379 DWORD xcall;
380 BYTE lret;
381 WORD bytestopop;
382 BYTE nop;
383 } TMAsmProxy;
384
385 #include "poppack.h"
386
387 #else /* __i386__ */
388 #ifdef _MSC_VER
389 #pragma message("You need to implement stubless proxies for your architecture")
390 #else
391 # warning You need to implement stubless proxies for your architecture
392 #endif
393 typedef struct _TMAsmProxy {
394 char a;
395 } TMAsmProxy;
396 #endif
397
398 typedef struct _TMProxyImpl {
399 LPVOID *lpvtbl;
400 const IRpcProxyBufferVtbl *lpvtbl2;
401 LONG ref;
402
403 TMAsmProxy *asmstubs;
404 ITypeInfo* tinfo;
405 IRpcChannelBuffer* chanbuf;
406 IID iid;
407 CRITICAL_SECTION crit;
408 IUnknown *outerunknown;
409 IDispatch *dispatch;
410 IRpcProxyBuffer *dispatch_proxy;
411 } TMProxyImpl;
412
413 static HRESULT WINAPI
414 TMProxyImpl_QueryInterface(LPRPCPROXYBUFFER iface, REFIID riid, LPVOID *ppv)
415 {
416 TRACE("()\n");
417 if (IsEqualIID(riid,&IID_IUnknown)||IsEqualIID(riid,&IID_IRpcProxyBuffer)) {
418 *ppv = iface;
419 IRpcProxyBuffer_AddRef(iface);
420 return S_OK;
421 }
422 FIXME("no interface for %s\n",debugstr_guid(riid));
423 return E_NOINTERFACE;
424 }
425
426 static ULONG WINAPI
427 TMProxyImpl_AddRef(LPRPCPROXYBUFFER iface)
428 {
429 ICOM_THIS_MULTI(TMProxyImpl,lpvtbl2,iface);
430 ULONG refCount = InterlockedIncrement(&This->ref);
431
432 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
433
434 return refCount;
435 }
436
437 static ULONG WINAPI
438 TMProxyImpl_Release(LPRPCPROXYBUFFER iface)
439 {
440 ICOM_THIS_MULTI(TMProxyImpl,lpvtbl2,iface);
441 ULONG refCount = InterlockedDecrement(&This->ref);
442
443 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
444
445 if (!refCount)
446 {
447 if (This->dispatch_proxy) IRpcProxyBuffer_Release(This->dispatch_proxy);
448 This->crit.DebugInfo->Spare[0] = 0;
449 DeleteCriticalSection(&This->crit);
450 if (This->chanbuf) IRpcChannelBuffer_Release(This->chanbuf);
451 VirtualFree(This->asmstubs, 0, MEM_RELEASE);
452 HeapFree(GetProcessHeap(), 0, This->lpvtbl);
453 ITypeInfo_Release(This->tinfo);
454 CoTaskMemFree(This);
455 }
456 return refCount;
457 }
458
459 static HRESULT WINAPI
460 TMProxyImpl_Connect(
461 LPRPCPROXYBUFFER iface,IRpcChannelBuffer* pRpcChannelBuffer)
462 {
463 ICOM_THIS_MULTI(TMProxyImpl, lpvtbl2, iface);
464
465 TRACE("(%p)\n", pRpcChannelBuffer);
466
467 EnterCriticalSection(&This->crit);
468
469 IRpcChannelBuffer_AddRef(pRpcChannelBuffer);
470 This->chanbuf = pRpcChannelBuffer;
471
472 LeaveCriticalSection(&This->crit);
473
474 if (This->dispatch_proxy)
475 {
476 IRpcChannelBuffer *pDelegateChannel;
477 HRESULT hr = TMarshalDispatchChannel_Create(pRpcChannelBuffer, &This->iid, &pDelegateChannel);
478 if (FAILED(hr))
479 return hr;
480 hr = IRpcProxyBuffer_Connect(This->dispatch_proxy, pDelegateChannel);
481 IRpcChannelBuffer_Release(pDelegateChannel);
482 return hr;
483 }
484
485 return S_OK;
486 }
487
488 static void WINAPI
489 TMProxyImpl_Disconnect(LPRPCPROXYBUFFER iface)
490 {
491 ICOM_THIS_MULTI(TMProxyImpl, lpvtbl2, iface);
492
493 TRACE("()\n");
494
495 EnterCriticalSection(&This->crit);
496
497 IRpcChannelBuffer_Release(This->chanbuf);
498 This->chanbuf = NULL;
499
500 LeaveCriticalSection(&This->crit);
501
502 if (This->dispatch_proxy)
503 IRpcProxyBuffer_Disconnect(This->dispatch_proxy);
504 }
505
506
507 static const IRpcProxyBufferVtbl tmproxyvtable = {
508 TMProxyImpl_QueryInterface,
509 TMProxyImpl_AddRef,
510 TMProxyImpl_Release,
511 TMProxyImpl_Connect,
512 TMProxyImpl_Disconnect
513 };
514
515 /* how much space do we use on stack in DWORD steps. */
516 static int
517 _argsize(TYPEDESC *tdesc, ITypeInfo *tinfo) {
518 switch (tdesc->vt) {
519 case VT_I8:
520 case VT_UI8:
521 return 8/sizeof(DWORD);
522 case VT_R8:
523 return sizeof(double)/sizeof(DWORD);
524 case VT_CY:
525 return sizeof(CY)/sizeof(DWORD);
526 case VT_DATE:
527 return sizeof(DATE)/sizeof(DWORD);
528 case VT_DECIMAL:
529 return (sizeof(DECIMAL)+3)/sizeof(DWORD);
530 case VT_VARIANT:
531 return (sizeof(VARIANT)+3)/sizeof(DWORD);
532 case VT_USERDEFINED:
533 {
534 ITypeInfo *tinfo2;
535 TYPEATTR *tattr;
536 HRESULT hres;
537 DWORD ret;
538
539 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
540 if (FAILED(hres))
541 return 0; /* should fail critically in serialize_param */
542 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
543 ret = (tattr->cbSizeInstance+3)/sizeof(DWORD);
544 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
545 ITypeInfo_Release(tinfo2);
546 return ret;
547 }
548 default:
549 return 1;
550 }
551 }
552
553 /* how much space do we use on the heap (in bytes) */
554 static int
555 _xsize(const TYPEDESC *td, ITypeInfo *tinfo) {
556 switch (td->vt) {
557 case VT_DATE:
558 return sizeof(DATE);
559 case VT_CY:
560 return sizeof(CY);
561 /* FIXME: VT_BOOL should return 2? */
562 case VT_VARIANT:
563 return sizeof(VARIANT)+3; /* FIXME: why the +3? */
564 case VT_CARRAY: {
565 int i, arrsize = 1;
566 const ARRAYDESC *adesc = td->u.lpadesc;
567
568 for (i=0;i<adesc->cDims;i++)
569 arrsize *= adesc->rgbounds[i].cElements;
570 return arrsize*_xsize(&adesc->tdescElem, tinfo);
571 }
572 case VT_UI8:
573 case VT_I8:
574 case VT_R8:
575 return 8;
576 case VT_UI2:
577 case VT_I2:
578 return 2;
579 case VT_UI1:
580 case VT_I1:
581 return 1;
582 case VT_USERDEFINED:
583 {
584 ITypeInfo *tinfo2;
585 TYPEATTR *tattr;
586 HRESULT hres;
587 DWORD ret;
588
589 hres = ITypeInfo_GetRefTypeInfo(tinfo,td->u.hreftype,&tinfo2);
590 if (FAILED(hres))
591 return 0;
592 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
593 ret = tattr->cbSizeInstance;
594 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
595 ITypeInfo_Release(tinfo2);
596 return ret;
597 }
598 default:
599 return 4;
600 }
601 }
602
603 static HRESULT
604 serialize_param(
605 ITypeInfo *tinfo,
606 BOOL writeit,
607 BOOL debugout,
608 BOOL dealloc,
609 TYPEDESC *tdesc,
610 DWORD *arg,
611 marshal_state *buf)
612 {
613 HRESULT hres = S_OK;
614 VARTYPE vartype;
615
616 TRACE("(tdesc.vt %s)\n",debugstr_vt(tdesc->vt));
617
618 vartype = tdesc->vt;
619 if ((vartype & 0xf000) == VT_ARRAY)
620 vartype = VT_SAFEARRAY;
621
622 switch (vartype) {
623 case VT_I8:
624 case VT_UI8:
625 case VT_R8:
626 case VT_CY:
627 hres = S_OK;
628 if (debugout) TRACE_(olerelay)("%x%x\n",arg[0],arg[1]);
629 if (writeit)
630 hres = xbuf_add(buf,(LPBYTE)arg,8);
631 return hres;
632 case VT_BOOL:
633 case VT_ERROR:
634 case VT_INT:
635 case VT_UINT:
636 case VT_I4:
637 case VT_R4:
638 case VT_UI4:
639 hres = S_OK;
640 if (debugout) TRACE_(olerelay)("%x\n",*arg);
641 if (writeit)
642 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
643 return hres;
644 case VT_I2:
645 case VT_UI2:
646 hres = S_OK;
647 if (debugout) TRACE_(olerelay)("%04x\n",*arg & 0xffff);
648 if (writeit)
649 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
650 return hres;
651 case VT_I1:
652 case VT_UI1:
653 hres = S_OK;
654 if (debugout) TRACE_(olerelay)("%02x\n",*arg & 0xff);
655 if (writeit)
656 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
657 return hres;
658 case VT_VARIANT: {
659 if (debugout) TRACE_(olerelay)("Vt(%s%s)(",debugstr_vt(V_VT((VARIANT *)arg)),debugstr_vf(V_VT((VARIANT *)arg)));
660 if (writeit)
661 {
662 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
663 ULONG size = VARIANT_UserSize(&flags, buf->curoff, (VARIANT *)arg);
664 xbuf_resize(buf, size);
665 VARIANT_UserMarshal(&flags, buf->base + buf->curoff, (VARIANT *)arg);
666 buf->curoff = size;
667 }
668 if (dealloc)
669 {
670 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
671 VARIANT_UserFree(&flags, (VARIANT *)arg);
672 }
673 return S_OK;
674 }
675 case VT_BSTR: {
676 if (debugout) {
677 if (*arg)
678 TRACE_(olerelay)("%s",relaystr((WCHAR*)*arg));
679 else
680 TRACE_(olerelay)("<bstr NULL>");
681 }
682 if (writeit)
683 {
684 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
685 ULONG size = BSTR_UserSize(&flags, buf->curoff, (BSTR *)arg);
686 xbuf_resize(buf, size);
687 BSTR_UserMarshal(&flags, buf->base + buf->curoff, (BSTR *)arg);
688 buf->curoff = size;
689 }
690 if (dealloc)
691 {
692 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
693 BSTR_UserFree(&flags, (BSTR *)arg);
694 }
695 return S_OK;
696 }
697 case VT_PTR: {
698 DWORD cookie;
699 BOOL derefhere = TRUE;
700
701 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
702 ITypeInfo *tinfo2;
703 TYPEATTR *tattr;
704
705 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
706 if (hres) {
707 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
708 return hres;
709 }
710 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
711 switch (tattr->typekind) {
712 case TKIND_ALIAS:
713 if (tattr->tdescAlias.vt == VT_USERDEFINED)
714 {
715 DWORD href = tattr->tdescAlias.u.hreftype;
716 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
717 ITypeInfo_Release(tinfo2);
718 hres = ITypeInfo_GetRefTypeInfo(tinfo,href,&tinfo2);
719 if (hres) {
720 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
721 return hres;
722 }
723 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
724 derefhere = (tattr->typekind != TKIND_DISPATCH && tattr->typekind != TKIND_INTERFACE);
725 }
726 break;
727 case TKIND_ENUM: /* confirmed */
728 case TKIND_RECORD: /* FIXME: mostly untested */
729 break;
730 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
731 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
732 derefhere=FALSE;
733 break;
734 default:
735 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
736 derefhere=FALSE;
737 break;
738 }
739 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
740 ITypeInfo_Release(tinfo2);
741 }
742
743 if (debugout) TRACE_(olerelay)("*");
744 /* Write always, so the other side knows when it gets a NULL pointer.
745 */
746 cookie = *arg ? 0x42424242 : 0;
747 hres = xbuf_add(buf,(LPBYTE)&cookie,sizeof(cookie));
748 if (hres)
749 return hres;
750 if (!*arg) {
751 if (debugout) TRACE_(olerelay)("NULL");
752 return S_OK;
753 }
754 hres = serialize_param(tinfo,writeit,debugout,dealloc,tdesc->u.lptdesc,(DWORD*)*arg,buf);
755 if (derefhere && dealloc) HeapFree(GetProcessHeap(),0,(LPVOID)*arg);
756 return hres;
757 }
758 case VT_UNKNOWN:
759 if (debugout) TRACE_(olerelay)("unk(0x%x)",*arg);
760 if (writeit)
761 hres = _marshal_interface(buf,&IID_IUnknown,(LPUNKNOWN)*arg);
762 if (dealloc && *(IUnknown **)arg)
763 IUnknown_Release((LPUNKNOWN)*arg);
764 return hres;
765 case VT_DISPATCH:
766 if (debugout) TRACE_(olerelay)("idisp(0x%x)",*arg);
767 if (writeit)
768 hres = _marshal_interface(buf,&IID_IDispatch,(LPUNKNOWN)*arg);
769 if (dealloc && *(IUnknown **)arg)
770 IUnknown_Release((LPUNKNOWN)*arg);
771 return hres;
772 case VT_VOID:
773 if (debugout) TRACE_(olerelay)("<void>");
774 return S_OK;
775 case VT_USERDEFINED: {
776 ITypeInfo *tinfo2;
777 TYPEATTR *tattr;
778
779 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
780 if (hres) {
781 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
782 return hres;
783 }
784 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
785 switch (tattr->typekind) {
786 case TKIND_DISPATCH:
787 case TKIND_INTERFACE:
788 if (writeit)
789 hres=_marshal_interface(buf,&(tattr->guid),(LPUNKNOWN)arg);
790 if (dealloc)
791 IUnknown_Release((LPUNKNOWN)arg);
792 break;
793 case TKIND_RECORD: {
794 int i;
795 if (debugout) TRACE_(olerelay)("{");
796 for (i=0;i<tattr->cVars;i++) {
797 VARDESC *vdesc;
798 ELEMDESC *elem2;
799 TYPEDESC *tdesc2;
800
801 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
802 if (hres) {
803 ERR("Could not get vardesc of %d\n",i);
804 return hres;
805 }
806 elem2 = &vdesc->elemdescVar;
807 tdesc2 = &elem2->tdesc;
808 hres = serialize_param(
809 tinfo2,
810 writeit,
811 debugout,
812 dealloc,
813 tdesc2,
814 (DWORD*)(((LPBYTE)arg)+vdesc->u.oInst),
815 buf
816 );
817 ITypeInfo_ReleaseVarDesc(tinfo2, vdesc);
818 if (hres!=S_OK)
819 return hres;
820 if (debugout && (i<(tattr->cVars-1)))
821 TRACE_(olerelay)(",");
822 }
823 if (debugout) TRACE_(olerelay)("}");
824 break;
825 }
826 case TKIND_ALIAS:
827 hres = serialize_param(tinfo2,writeit,debugout,dealloc,&tattr->tdescAlias,arg,buf);
828 break;
829 case TKIND_ENUM:
830 hres = S_OK;
831 if (debugout) TRACE_(olerelay)("%x",*arg);
832 if (writeit)
833 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
834 break;
835 default:
836 FIXME("Unhandled typekind %d\n",tattr->typekind);
837 hres = E_FAIL;
838 break;
839 }
840 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
841 ITypeInfo_Release(tinfo2);
842 return hres;
843 }
844 case VT_CARRAY: {
845 ARRAYDESC *adesc = tdesc->u.lpadesc;
846 int i, arrsize = 1;
847
848 if (debugout) TRACE_(olerelay)("carr");
849 for (i=0;i<adesc->cDims;i++) {
850 if (debugout) TRACE_(olerelay)("[%d]",adesc->rgbounds[i].cElements);
851 arrsize *= adesc->rgbounds[i].cElements;
852 }
853 if (debugout) TRACE_(olerelay)("(vt %s)",debugstr_vt(adesc->tdescElem.vt));
854 if (debugout) TRACE_(olerelay)("[");
855 for (i=0;i<arrsize;i++) {
856 hres = serialize_param(tinfo, writeit, debugout, dealloc, &adesc->tdescElem, (DWORD*)((LPBYTE)(*arg)+i*_xsize(&adesc->tdescElem, tinfo)), buf);
857 if (hres)
858 return hres;
859 if (debugout && (i<arrsize-1)) TRACE_(olerelay)(",");
860 }
861 if (debugout) TRACE_(olerelay)("]");
862 if (dealloc)
863 HeapFree(GetProcessHeap(), 0, *(void **)arg);
864 return S_OK;
865 }
866 case VT_SAFEARRAY: {
867 if (writeit)
868 {
869 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
870 ULONG size = LPSAFEARRAY_UserSize(&flags, buf->curoff, (LPSAFEARRAY *)arg);
871 xbuf_resize(buf, size);
872 LPSAFEARRAY_UserMarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
873 buf->curoff = size;
874 }
875 if (dealloc)
876 {
877 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
878 LPSAFEARRAY_UserFree(&flags, (LPSAFEARRAY *)arg);
879 }
880 return S_OK;
881 }
882 default:
883 ERR("Unhandled marshal type %d.\n",tdesc->vt);
884 return S_OK;
885 }
886 }
887
888 static HRESULT
889 deserialize_param(
890 ITypeInfo *tinfo,
891 BOOL readit,
892 BOOL debugout,
893 BOOL alloc,
894 TYPEDESC *tdesc,
895 DWORD *arg,
896 marshal_state *buf)
897 {
898 HRESULT hres = S_OK;
899 VARTYPE vartype;
900
901 TRACE("vt %s at %p\n",debugstr_vt(tdesc->vt),arg);
902
903 vartype = tdesc->vt;
904 if ((vartype & 0xf000) == VT_ARRAY)
905 vartype = VT_SAFEARRAY;
906
907 while (1) {
908 switch (vartype) {
909 case VT_VARIANT: {
910 if (readit)
911 {
912 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
913 unsigned char *buffer;
914 buffer = VARIANT_UserUnmarshal(&flags, buf->base + buf->curoff, (VARIANT *)arg);
915 buf->curoff = buffer - buf->base;
916 }
917 return S_OK;
918 }
919 case VT_I8:
920 case VT_UI8:
921 case VT_R8:
922 case VT_CY:
923 if (readit) {
924 hres = xbuf_get(buf,(LPBYTE)arg,8);
925 if (hres) ERR("Failed to read integer 8 byte\n");
926 }
927 if (debugout) TRACE_(olerelay)("%x%x",arg[0],arg[1]);
928 return hres;
929 case VT_ERROR:
930 case VT_BOOL:
931 case VT_I4:
932 case VT_INT:
933 case VT_UINT:
934 case VT_R4:
935 case VT_UI4:
936 if (readit) {
937 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
938 if (hres) ERR("Failed to read integer 4 byte\n");
939 }
940 if (debugout) TRACE_(olerelay)("%x",*arg);
941 return hres;
942 case VT_I2:
943 case VT_UI2:
944 if (readit) {
945 DWORD x;
946 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
947 if (hres) ERR("Failed to read integer 4 byte\n");
948 memcpy(arg,&x,2);
949 }
950 if (debugout) TRACE_(olerelay)("%04x",*arg & 0xffff);
951 return hres;
952 case VT_I1:
953 case VT_UI1:
954 if (readit) {
955 DWORD x;
956 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
957 if (hres) ERR("Failed to read integer 4 byte\n");
958 memcpy(arg,&x,1);
959 }
960 if (debugout) TRACE_(olerelay)("%02x",*arg & 0xff);
961 return hres;
962 case VT_BSTR: {
963 if (readit)
964 {
965 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
966 unsigned char *buffer;
967 buffer = BSTR_UserUnmarshal(&flags, buf->base + buf->curoff, (BSTR *)arg);
968 buf->curoff = buffer - buf->base;
969 if (debugout) TRACE_(olerelay)("%s",debugstr_w(*(BSTR *)arg));
970 }
971 return S_OK;
972 }
973 case VT_PTR: {
974 DWORD cookie;
975 BOOL derefhere = TRUE;
976
977 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
978 ITypeInfo *tinfo2;
979 TYPEATTR *tattr;
980
981 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
982 if (hres) {
983 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
984 return hres;
985 }
986 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
987 switch (tattr->typekind) {
988 case TKIND_ALIAS:
989 if (tattr->tdescAlias.vt == VT_USERDEFINED)
990 {
991 DWORD href = tattr->tdescAlias.u.hreftype;
992 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
993 ITypeInfo_Release(tinfo2);
994 hres = ITypeInfo_GetRefTypeInfo(tinfo,href,&tinfo2);
995 if (hres) {
996 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
997 return hres;
998 }
999 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1000 derefhere = (tattr->typekind != TKIND_DISPATCH && tattr->typekind != TKIND_INTERFACE);
1001 }
1002 break;
1003 case TKIND_ENUM: /* confirmed */
1004 case TKIND_RECORD: /* FIXME: mostly untested */
1005 break;
1006 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
1007 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
1008 derefhere=FALSE;
1009 break;
1010 default:
1011 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
1012 derefhere=FALSE;
1013 break;
1014 }
1015 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1016 ITypeInfo_Release(tinfo2);
1017 }
1018 /* read it in all cases, we need to know if we have
1019 * NULL pointer or not.
1020 */
1021 hres = xbuf_get(buf,(LPBYTE)&cookie,sizeof(cookie));
1022 if (hres) {
1023 ERR("Failed to load pointer cookie.\n");
1024 return hres;
1025 }
1026 if (cookie != 0x42424242) {
1027 /* we read a NULL ptr from the remote side */
1028 if (debugout) TRACE_(olerelay)("NULL");
1029 *arg = 0;
1030 return S_OK;
1031 }
1032 if (debugout) TRACE_(olerelay)("*");
1033 if (alloc) {
1034 /* Allocate space for the referenced struct */
1035 if (derefhere)
1036 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,_xsize(tdesc->u.lptdesc, tinfo));
1037 }
1038 if (derefhere)
1039 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, (LPDWORD)*arg, buf);
1040 else
1041 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, arg, buf);
1042 }
1043 case VT_UNKNOWN:
1044 /* FIXME: UNKNOWN is unknown ..., but allocate 4 byte for it */
1045 if (alloc)
1046 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
1047 hres = S_OK;
1048 if (readit)
1049 hres = _unmarshal_interface(buf,&IID_IUnknown,(LPUNKNOWN*)arg);
1050 if (debugout)
1051 TRACE_(olerelay)("unk(%p)",arg);
1052 return hres;
1053 case VT_DISPATCH:
1054 hres = S_OK;
1055 if (readit)
1056 hres = _unmarshal_interface(buf,&IID_IDispatch,(LPUNKNOWN*)arg);
1057 if (debugout)
1058 TRACE_(olerelay)("idisp(%p)",arg);
1059 return hres;
1060 case VT_VOID:
1061 if (debugout) TRACE_(olerelay)("<void>");
1062 return S_OK;
1063 case VT_USERDEFINED: {
1064 ITypeInfo *tinfo2;
1065 TYPEATTR *tattr;
1066
1067 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
1068 if (hres) {
1069 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
1070 return hres;
1071 }
1072 hres = ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1073 if (hres) {
1074 ERR("Could not get typeattr in VT_USERDEFINED.\n");
1075 } else {
1076 switch (tattr->typekind) {
1077 case TKIND_DISPATCH:
1078 case TKIND_INTERFACE:
1079 if (readit)
1080 hres = _unmarshal_interface(buf,&(tattr->guid),(LPUNKNOWN*)arg);
1081 break;
1082 case TKIND_RECORD: {
1083 int i;
1084
1085 if (debugout) TRACE_(olerelay)("{");
1086 for (i=0;i<tattr->cVars;i++) {
1087 VARDESC *vdesc;
1088
1089 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
1090 if (hres) {
1091 ERR("Could not get vardesc of %d\n",i);
1092 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1093 ITypeInfo_Release(tinfo2);
1094 return hres;
1095 }
1096 hres = deserialize_param(
1097 tinfo2,
1098 readit,
1099 debugout,
1100 alloc,
1101 &vdesc->elemdescVar.tdesc,
1102 (DWORD*)(((LPBYTE)arg)+vdesc->u.oInst),
1103 buf
1104 );
1105 ITypeInfo2_ReleaseVarDesc(tinfo2, vdesc);
1106 if (debugout && (i<tattr->cVars-1)) TRACE_(olerelay)(",");
1107 }
1108 if (debugout) TRACE_(olerelay)("}");
1109 break;
1110 }
1111 case TKIND_ALIAS:
1112 hres = deserialize_param(tinfo2,readit,debugout,alloc,&tattr->tdescAlias,arg,buf);
1113 break;
1114 case TKIND_ENUM:
1115 if (readit) {
1116 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
1117 if (hres) ERR("Failed to read enum (4 byte)\n");
1118 }
1119 if (debugout) TRACE_(olerelay)("%x",*arg);
1120 break;
1121 default:
1122 ERR("Unhandled typekind %d\n",tattr->typekind);
1123 hres = E_FAIL;
1124 break;
1125 }
1126 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1127 }
1128 if (hres)
1129 ERR("failed to stuballoc in TKIND_RECORD.\n");
1130 ITypeInfo_Release(tinfo2);
1131 return hres;
1132 }
1133 case VT_CARRAY: {
1134 /* arg is pointing to the start of the array. */
1135 ARRAYDESC *adesc = tdesc->u.lpadesc;
1136 int arrsize,i;
1137 arrsize = 1;
1138 if (adesc->cDims > 1) FIXME("cDims > 1 in VT_CARRAY. Does it work?\n");
1139 for (i=0;i<adesc->cDims;i++)
1140 arrsize *= adesc->rgbounds[i].cElements;
1141 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,_xsize(tdesc->u.lptdesc, tinfo) * arrsize);
1142 for (i=0;i<arrsize;i++)
1143 deserialize_param(
1144 tinfo,
1145 readit,
1146 debugout,
1147 alloc,
1148 &adesc->tdescElem,
1149 (DWORD*)((LPBYTE)(*arg)+i*_xsize(&adesc->tdescElem, tinfo)),
1150 buf
1151 );
1152 return S_OK;
1153 }
1154 case VT_SAFEARRAY: {
1155 if (readit)
1156 {
1157 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
1158 unsigned char *buffer;
1159 buffer = LPSAFEARRAY_UserUnmarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
1160 buf->curoff = buffer - buf->base;
1161 }
1162 return S_OK;
1163 }
1164 default:
1165 ERR("No handler for VT type %d!\n",tdesc->vt);
1166 return S_OK;
1167 }
1168 }
1169 }
1170
1171 /* Retrieves a function's funcdesc, searching back into inherited interfaces. */
1172 static HRESULT get_funcdesc(ITypeInfo *tinfo, int iMethod, ITypeInfo **tactual, const FUNCDESC **fdesc,
1173 BSTR *iname, BSTR *fname, UINT *num)
1174 {
1175 HRESULT hr;
1176 UINT i, impl_types;
1177 UINT inherited_funcs = 0;
1178 TYPEATTR *attr;
1179
1180 if (fname) *fname = NULL;
1181 if (iname) *iname = NULL;
1182 if (num) *num = 0;
1183 *tactual = NULL;
1184
1185 hr = ITypeInfo_GetTypeAttr(tinfo, &attr);
1186 if (FAILED(hr))
1187 {
1188 ERR("GetTypeAttr failed with %x\n",hr);
1189 return hr;
1190 }
1191
1192 if(attr->typekind == TKIND_DISPATCH)
1193 {
1194 if(attr->wTypeFlags & TYPEFLAG_FDUAL)
1195 {
1196 HREFTYPE href;
1197 ITypeInfo *tinfo2;
1198
1199 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, -1, &href);
1200 if(FAILED(hr))
1201 {
1202 ERR("Cannot get interface href from dual dispinterface\n");
1203 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1204 return hr;
1205 }
1206 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
1207 if(FAILED(hr))
1208 {
1209 ERR("Cannot get interface from dual dispinterface\n");
1210 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1211 return hr;
1212 }
1213 hr = get_funcdesc(tinfo2, iMethod, tactual, fdesc, iname, fname, num);
1214 ITypeInfo_Release(tinfo2);
1215 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1216 return hr;
1217 }
1218 ERR("Shouldn't be called with a non-dual dispinterface\n");
1219 return E_FAIL;
1220 }
1221
1222 impl_types = attr->cImplTypes;
1223 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1224
1225 for (i = 0; i < impl_types; i++)
1226 {
1227 HREFTYPE href;
1228 ITypeInfo *pSubTypeInfo;
1229 UINT sub_funcs;
1230
1231 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, i, &href);
1232 if (FAILED(hr)) return hr;
1233 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &pSubTypeInfo);
1234 if (FAILED(hr)) return hr;
1235
1236 hr = get_funcdesc(pSubTypeInfo, iMethod, tactual, fdesc, iname, fname, &sub_funcs);
1237 inherited_funcs += sub_funcs;
1238 ITypeInfo_Release(pSubTypeInfo);
1239 if(SUCCEEDED(hr)) return hr;
1240 }
1241 if(iMethod < inherited_funcs)
1242 {
1243 ERR("shouldn't be here\n");
1244 return E_INVALIDARG;
1245 }
1246
1247 for(i = inherited_funcs; i <= iMethod; i++)
1248 {
1249 hr = ITypeInfoImpl_GetInternalFuncDesc(tinfo, i - inherited_funcs, fdesc);
1250 if(FAILED(hr))
1251 {
1252 if(num) *num = i;
1253 return hr;
1254 }
1255 }
1256
1257 /* found it. We don't care about num so zero it */
1258 if(num) *num = 0;
1259 *tactual = tinfo;
1260 ITypeInfo_AddRef(*tactual);
1261 if (fname) ITypeInfo_GetDocumentation(tinfo,(*fdesc)->memid,fname,NULL,NULL,NULL);
1262 if (iname) ITypeInfo_GetDocumentation(tinfo,-1,iname,NULL,NULL,NULL);
1263 return S_OK;
1264 }
1265
1266 static inline BOOL is_in_elem(const ELEMDESC *elem)
1267 {
1268 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN || !elem->u.paramdesc.wParamFlags);
1269 }
1270
1271 static inline BOOL is_out_elem(const ELEMDESC *elem)
1272 {
1273 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT || !elem->u.paramdesc.wParamFlags);
1274 }
1275
1276 static DWORD
1277 xCall(LPVOID retptr, int method, TMProxyImpl *tpinfo /*, args */)
1278 {
1279 DWORD *args = ((DWORD*)&tpinfo)+1, *xargs;
1280 const FUNCDESC *fdesc;
1281 HRESULT hres;
1282 int i, relaydeb = TRACE_ON(olerelay);
1283 marshal_state buf;
1284 RPCOLEMESSAGE msg;
1285 ULONG status;
1286 BSTR fname,iname;
1287 BSTR names[10];
1288 UINT nrofnames;
1289 DWORD remoteresult = 0;
1290 ITypeInfo *tinfo;
1291 IRpcChannelBuffer *chanbuf;
1292
1293 EnterCriticalSection(&tpinfo->crit);
1294
1295 hres = get_funcdesc(tpinfo->tinfo,method,&tinfo,&fdesc,&iname,&fname,NULL);
1296 if (hres) {
1297 ERR("Did not find typeinfo/funcdesc entry for method %d!\n",method);
1298 LeaveCriticalSection(&tpinfo->crit);
1299 return E_FAIL;
1300 }
1301
1302 if (!tpinfo->chanbuf)
1303 {
1304 WARN("Tried to use disconnected proxy\n");
1305 ITypeInfo_Release(tinfo);
1306 LeaveCriticalSection(&tpinfo->crit);
1307 return RPC_E_DISCONNECTED;
1308 }
1309 chanbuf = tpinfo->chanbuf;
1310 IRpcChannelBuffer_AddRef(chanbuf);
1311
1312 LeaveCriticalSection(&tpinfo->crit);
1313
1314 if (relaydeb) {
1315 TRACE_(olerelay)("->");
1316 if (iname)
1317 TRACE_(olerelay)("%s:",relaystr(iname));
1318 if (fname)
1319 TRACE_(olerelay)("%s(%d)",relaystr(fname),method);
1320 else
1321 TRACE_(olerelay)("%d",method);
1322 TRACE_(olerelay)("(");
1323 }
1324
1325 SysFreeString(iname);
1326 SysFreeString(fname);
1327
1328 memset(&buf,0,sizeof(buf));
1329
1330 /* normal typelib driven serializing */
1331
1332 /* Need them for hack below */
1333 memset(names,0,sizeof(names));
1334 if (ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames))
1335 nrofnames = 0;
1336 if (nrofnames > sizeof(names)/sizeof(names[0]))
1337 ERR("Need more names!\n");
1338
1339 xargs = args;
1340 for (i=0;i<fdesc->cParams;i++) {
1341 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1342 if (relaydeb) {
1343 if (i) TRACE_(olerelay)(",");
1344 if (i+1<nrofnames && names[i+1])
1345 TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1346 }
1347 /* No need to marshal other data than FIN and any VT_PTR. */
1348 if (!is_in_elem(elem) && (elem->tdesc.vt != VT_PTR)) {
1349 xargs+=_argsize(&elem->tdesc, tinfo);
1350 if (relaydeb) TRACE_(olerelay)("[out]");
1351 continue;
1352 }
1353 hres = serialize_param(
1354 tinfo,
1355 is_in_elem(elem),
1356 relaydeb,
1357 FALSE,
1358 &elem->tdesc,
1359 xargs,
1360 &buf
1361 );
1362
1363 if (hres) {
1364 ERR("Failed to serialize param, hres %x\n",hres);
1365 break;
1366 }
1367 xargs+=_argsize(&elem->tdesc, tinfo);
1368 }
1369 if (relaydeb) TRACE_(olerelay)(")");
1370
1371 memset(&msg,0,sizeof(msg));
1372 msg.cbBuffer = buf.curoff;
1373 msg.iMethod = method;
1374 hres = IRpcChannelBuffer_GetBuffer(chanbuf,&msg,&(tpinfo->iid));
1375 if (hres) {
1376 ERR("RpcChannelBuffer GetBuffer failed, %x\n",hres);
1377 goto exit;
1378 }
1379 memcpy(msg.Buffer,buf.base,buf.curoff);
1380 if (relaydeb) TRACE_(olerelay)("\n");
1381 hres = IRpcChannelBuffer_SendReceive(chanbuf,&msg,&status);
1382 if (hres) {
1383 ERR("RpcChannelBuffer SendReceive failed, %x\n",hres);
1384 goto exit;
1385 }
1386
1387 if (relaydeb) TRACE_(olerelay)(" status = %08x (",status);
1388 if (buf.base)
1389 buf.base = HeapReAlloc(GetProcessHeap(),0,buf.base,msg.cbBuffer);
1390 else
1391 buf.base = HeapAlloc(GetProcessHeap(),0,msg.cbBuffer);
1392 buf.size = msg.cbBuffer;
1393 memcpy(buf.base,msg.Buffer,buf.size);
1394 buf.curoff = 0;
1395
1396 /* generic deserializer using typelib description */
1397 xargs = args;
1398 status = S_OK;
1399 for (i=0;i<fdesc->cParams;i++) {
1400 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1401
1402 if (relaydeb) {
1403 if (i) TRACE_(olerelay)(",");
1404 if (i+1<nrofnames && names[i+1]) TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1405 }
1406 /* No need to marshal other data than FOUT and any VT_PTR */
1407 if (!is_out_elem(elem) && (elem->tdesc.vt != VT_PTR)) {
1408 xargs += _argsize(&elem->tdesc, tinfo);
1409 if (relaydeb) TRACE_(olerelay)("[in]");
1410 continue;
1411 }
1412 hres = deserialize_param(
1413 tinfo,
1414 is_out_elem(elem),
1415 relaydeb,
1416 FALSE,
1417 &(elem->tdesc),
1418 xargs,
1419 &buf
1420 );
1421 if (hres) {
1422 ERR("Failed to unmarshall param, hres %x\n",hres);
1423 status = hres;
1424 break;
1425 }
1426 xargs += _argsize(&elem->tdesc, tinfo);
1427 }
1428
1429 hres = xbuf_get(&buf, (LPBYTE)&remoteresult, sizeof(DWORD));
1430 if (hres != S_OK)
1431 goto exit;
1432 if (relaydeb) TRACE_(olerelay)(") = %08x\n", remoteresult);
1433
1434 hres = remoteresult;
1435
1436 exit:
1437 IRpcChannelBuffer_FreeBuffer(chanbuf,&msg);
1438 for (i = 0; i < nrofnames; i++)
1439 SysFreeString(names[i]);
1440 HeapFree(GetProcessHeap(),0,buf.base);
1441 IRpcChannelBuffer_Release(chanbuf);
1442 ITypeInfo_Release(tinfo);
1443 TRACE("-- 0x%08x\n", hres);
1444 return hres;
1445 }
1446
1447 static HRESULT WINAPI ProxyIUnknown_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
1448 {
1449 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1450
1451 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1452
1453 if (proxy->outerunknown)
1454 return IUnknown_QueryInterface(proxy->outerunknown, riid, ppv);
1455
1456 FIXME("No interface\n");
1457 return E_NOINTERFACE;
1458 }
1459
1460 static ULONG WINAPI ProxyIUnknown_AddRef(IUnknown *iface)
1461 {
1462 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1463
1464 TRACE("\n");
1465
1466 if (proxy->outerunknown)
1467 return IUnknown_AddRef(proxy->outerunknown);
1468
1469 return 2; /* FIXME */
1470 }
1471
1472 static ULONG WINAPI ProxyIUnknown_Release(IUnknown *iface)
1473 {
1474 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1475
1476 TRACE("\n");
1477
1478 if (proxy->outerunknown)
1479 return IUnknown_Release(proxy->outerunknown);
1480
1481 return 1; /* FIXME */
1482 }
1483
1484 static HRESULT WINAPI ProxyIDispatch_GetTypeInfoCount(LPDISPATCH iface, UINT * pctinfo)
1485 {
1486 TMProxyImpl *This = (TMProxyImpl *)iface;
1487
1488 TRACE("(%p)\n", pctinfo);
1489
1490 return IDispatch_GetTypeInfoCount(This->dispatch, pctinfo);
1491 }
1492
1493 static HRESULT WINAPI ProxyIDispatch_GetTypeInfo(LPDISPATCH iface, UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo)
1494 {
1495 TMProxyImpl *This = (TMProxyImpl *)iface;
1496
1497 TRACE("(%d, %x, %p)\n", iTInfo, lcid, ppTInfo);
1498
1499 return IDispatch_GetTypeInfo(This->dispatch, iTInfo, lcid, ppTInfo);
1500 }
1501
1502 static HRESULT WINAPI ProxyIDispatch_GetIDsOfNames(LPDISPATCH iface, REFIID riid, LPOLESTR * rgszNames, UINT cNames, LCID lcid, DISPID * rgDispId)
1503 {
1504 TMProxyImpl *This = (TMProxyImpl *)iface;
1505
1506 TRACE("(%s, %p, %d, 0x%x, %p)\n", debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);
1507
1508 return IDispatch_GetIDsOfNames(This->dispatch, riid, rgszNames,
1509 cNames, lcid, rgDispId);
1510 }
1511
1512 static HRESULT WINAPI ProxyIDispatch_Invoke(LPDISPATCH iface, DISPID dispIdMember, REFIID riid, LCID lcid,
1513 WORD wFlags, DISPPARAMS * pDispParams, VARIANT * pVarResult,
1514 EXCEPINFO * pExcepInfo, UINT * puArgErr)
1515 {
1516 TMProxyImpl *This = (TMProxyImpl *)iface;
1517
1518 TRACE("(%d, %s, 0x%x, 0x%x, %p, %p, %p, %p)\n", dispIdMember,
1519 debugstr_guid(riid), lcid, wFlags, pDispParams, pVarResult,
1520 pExcepInfo, puArgErr);
1521
1522 return IDispatch_Invoke(This->dispatch, dispIdMember, riid, lcid,
1523 wFlags, pDispParams, pVarResult, pExcepInfo,
1524 puArgErr);
1525 }
1526
1527 typedef struct
1528 {
1529 const IRpcChannelBufferVtbl *lpVtbl;
1530 LONG refs;
1531 /* the IDispatch-derived interface we are handling */
1532 IID tmarshal_iid;
1533 IRpcChannelBuffer *pDelegateChannel;
1534 } TMarshalDispatchChannel;
1535
1536 static HRESULT WINAPI TMarshalDispatchChannel_QueryInterface(LPRPCCHANNELBUFFER iface, REFIID riid, LPVOID *ppv)
1537 {
1538 *ppv = NULL;
1539 if (IsEqualIID(riid,&IID_IRpcChannelBuffer) || IsEqualIID(riid,&IID_IUnknown))
1540 {
1541 *ppv = iface;
1542 IUnknown_AddRef(iface);
1543 return S_OK;
1544 }
1545 return E_NOINTERFACE;
1546 }
1547
1548 static ULONG WINAPI TMarshalDispatchChannel_AddRef(LPRPCCHANNELBUFFER iface)
1549 {
1550 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1551 return InterlockedIncrement(&This->refs);
1552 }
1553
1554 static ULONG WINAPI TMarshalDispatchChannel_Release(LPRPCCHANNELBUFFER iface)
1555 {
1556 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1557 ULONG ref;
1558
1559 ref = InterlockedDecrement(&This->refs);
1560 if (ref)
1561 return ref;
1562
1563 IRpcChannelBuffer_Release(This->pDelegateChannel);
1564 HeapFree(GetProcessHeap(), 0, This);
1565 return 0;
1566 }
1567
1568 static HRESULT WINAPI TMarshalDispatchChannel_GetBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg, REFIID riid)
1569 {
1570 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1571 TRACE("(%p, %s)\n", olemsg, debugstr_guid(riid));
1572 /* Note: we are pretending to invoke a method on the interface identified
1573 * by tmarshal_iid so that we can re-use the IDispatch proxy/stub code
1574 * without the RPC runtime getting confused by not exporting an IDispatch interface */
1575 return IRpcChannelBuffer_GetBuffer(This->pDelegateChannel, olemsg, &This->tmarshal_iid);
1576 }
1577
1578 static HRESULT WINAPI TMarshalDispatchChannel_SendReceive(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE *olemsg, ULONG *pstatus)
1579 {
1580 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1581 TRACE("(%p, %p)\n", olemsg, pstatus);
1582 return IRpcChannelBuffer_SendReceive(This->pDelegateChannel, olemsg, pstatus);
1583 }
1584
1585 static HRESULT WINAPI TMarshalDispatchChannel_FreeBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg)
1586 {
1587 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1588 TRACE("(%p)\n", olemsg);
1589 return IRpcChannelBuffer_FreeBuffer(This->pDelegateChannel, olemsg);
1590 }
1591
1592 static HRESULT WINAPI TMarshalDispatchChannel_GetDestCtx(LPRPCCHANNELBUFFER iface, DWORD* pdwDestContext, void** ppvDestContext)
1593 {
1594 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1595 TRACE("(%p,%p)\n", pdwDestContext, ppvDestContext);
1596 return IRpcChannelBuffer_GetDestCtx(This->pDelegateChannel, pdwDestContext, ppvDestContext);
1597 }
1598
1599 static HRESULT WINAPI TMarshalDispatchChannel_IsConnected(LPRPCCHANNELBUFFER iface)
1600 {
1601 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1602 TRACE("()\n");
1603 return IRpcChannelBuffer_IsConnected(This->pDelegateChannel);
1604 }
1605
1606 static const IRpcChannelBufferVtbl TMarshalDispatchChannelVtbl =
1607 {
1608 TMarshalDispatchChannel_QueryInterface,
1609 TMarshalDispatchChannel_AddRef,
1610 TMarshalDispatchChannel_Release,
1611 TMarshalDispatchChannel_GetBuffer,
1612 TMarshalDispatchChannel_SendReceive,
1613 TMarshalDispatchChannel_FreeBuffer,
1614 TMarshalDispatchChannel_GetDestCtx,
1615 TMarshalDispatchChannel_IsConnected
1616 };
1617
1618 static HRESULT TMarshalDispatchChannel_Create(
1619 IRpcChannelBuffer *pDelegateChannel, REFIID tmarshal_riid,
1620 IRpcChannelBuffer **ppChannel)
1621 {
1622 TMarshalDispatchChannel *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1623 if (!This)
1624 return E_OUTOFMEMORY;
1625
1626 This->lpVtbl = &TMarshalDispatchChannelVtbl;
1627 This->refs = 1;
1628 IRpcChannelBuffer_AddRef(pDelegateChannel);
1629 This->pDelegateChannel = pDelegateChannel;
1630 This->tmarshal_iid = *tmarshal_riid;
1631
1632 *ppChannel = (IRpcChannelBuffer *)&This->lpVtbl;
1633 return S_OK;
1634 }
1635
1636
1637 static inline HRESULT get_facbuf_for_iid(REFIID riid, IPSFactoryBuffer **facbuf)
1638 {
1639 HRESULT hr;
1640 CLSID clsid;
1641
1642 if ((hr = CoGetPSClsid(riid, &clsid)))
1643 return hr;
1644 return CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL,
1645 &IID_IPSFactoryBuffer, (LPVOID*)facbuf);
1646 }
1647
1648 static HRESULT init_proxy_entry_point(TMProxyImpl *proxy, unsigned int num)
1649 {
1650 int j;
1651 /* nrofargs without This */
1652 int nrofargs;
1653 ITypeInfo *tinfo2;
1654 TMAsmProxy *xasm = proxy->asmstubs + num;
1655 HRESULT hres;
1656 const FUNCDESC *fdesc;
1657
1658 hres = get_funcdesc(proxy->tinfo, num, &tinfo2, &fdesc, NULL, NULL, NULL);
1659 if (hres) {
1660 ERR("GetFuncDesc %x should not fail here.\n",hres);
1661 return hres;
1662 }
1663 ITypeInfo_Release(tinfo2);
1664 /* some args take more than 4 byte on the stack */
1665 nrofargs = 0;
1666 for (j=0;j<fdesc->cParams;j++)
1667 nrofargs += _argsize(&fdesc->lprgelemdescParam[j].tdesc, proxy->tinfo);
1668
1669 #ifdef __i386__
1670 if (fdesc->callconv != CC_STDCALL) {
1671 ERR("calling convention is not stdcall????\n");
1672 return E_FAIL;
1673 }
1674 /* popl %eax - return ptr
1675 * pushl <nr>
1676 * pushl %eax
1677 * call xCall
1678 * lret <nr> (+4)
1679 *
1680 *
1681 * arg3 arg2 arg1 <method> <returnptr>
1682 */
1683 xasm->popleax = 0x58;
1684 xasm->pushlval = 0x68;
1685 xasm->nr = num;
1686 xasm->pushleax = 0x50;
1687 xasm->lcall = 0xe8; /* relative jump */
1688 xasm->xcall = (DWORD)xCall;
1689 xasm->xcall -= (DWORD)&(xasm->lret);
1690 xasm->lret = 0xc2;
1691 xasm->bytestopop = (nrofargs+2)*4; /* pop args, This, iMethod */
1692 xasm->nop = 0x90;
1693 proxy->lpvtbl[num] = xasm;
1694 #else
1695 FIXME("not implemented on non i386\n");
1696 return E_FAIL;
1697 #endif
1698 return S_OK;
1699 }
1700
1701 static HRESULT WINAPI
1702 PSFacBuf_CreateProxy(
1703 LPPSFACTORYBUFFER iface, IUnknown* pUnkOuter, REFIID riid,
1704 IRpcProxyBuffer **ppProxy, LPVOID *ppv)
1705 {
1706 HRESULT hres;
1707 ITypeInfo *tinfo;
1708 unsigned int i, nroffuncs;
1709 TMProxyImpl *proxy;
1710 TYPEATTR *typeattr;
1711 BOOL defer_to_dispatch = FALSE;
1712
1713 TRACE("(...%s...)\n",debugstr_guid(riid));
1714 hres = _get_typeinfo_for_iid(riid,&tinfo);
1715 if (hres) {
1716 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
1717 return hres;
1718 }
1719
1720 hres = num_of_funcs(tinfo, &nroffuncs);
1721 if (FAILED(hres)) {
1722 ERR("Cannot get number of functions for typeinfo %s\n",debugstr_guid(riid));
1723 ITypeInfo_Release(tinfo);
1724 return hres;
1725 }
1726
1727 proxy = CoTaskMemAlloc(sizeof(TMProxyImpl));
1728 if (!proxy) return E_OUTOFMEMORY;
1729
1730 assert(sizeof(TMAsmProxy) == 16);
1731
1732 proxy->dispatch = NULL;
1733 proxy->dispatch_proxy = NULL;
1734 proxy->outerunknown = pUnkOuter;
1735 proxy->asmstubs = VirtualAlloc(NULL, sizeof(TMAsmProxy) * nroffuncs, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
1736 if (!proxy->asmstubs) {
1737 ERR("Could not commit pages for proxy thunks\n");
1738 CoTaskMemFree(proxy);
1739 return E_OUTOFMEMORY;
1740 }
1741 proxy->lpvtbl2 = &tmproxyvtable;
1742 /* one reference for the proxy */
1743 proxy->ref = 1;
1744 proxy->tinfo = tinfo;
1745 proxy->iid = *riid;
1746 proxy->chanbuf = 0;
1747
1748 InitializeCriticalSection(&proxy->crit);
1749 proxy->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TMProxyImpl.crit");
1750
1751 proxy->lpvtbl = HeapAlloc(GetProcessHeap(),0,sizeof(LPBYTE)*nroffuncs);
1752
1753 /* if we derive from IDispatch then defer to its proxy for its methods */
1754 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
1755 if (hres == S_OK)
1756 {
1757 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
1758 {
1759 IPSFactoryBuffer *factory_buffer;
1760 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
1761 if (hres == S_OK)
1762 {
1763 hres = IPSFactoryBuffer_CreateProxy(factory_buffer, NULL,
1764 &IID_IDispatch, &proxy->dispatch_proxy,
1765 (void **)&proxy->dispatch);
1766 IPSFactoryBuffer_Release(factory_buffer);
1767 }
1768 if ((hres == S_OK) && (nroffuncs < 7))
1769 {
1770 ERR("nroffuncs calculated incorrectly (%d)\n", nroffuncs);
1771 hres = E_UNEXPECTED;
1772 }
1773 if (hres == S_OK)
1774 {
1775 defer_to_dispatch = TRUE;
1776 }
1777 }
1778 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
1779 }
1780
1781 for (i=0;i<nroffuncs;i++) {
1782 switch (i) {
1783 case 0:
1784 proxy->lpvtbl[i] = ProxyIUnknown_QueryInterface;
1785 break;
1786 case 1:
1787 proxy->lpvtbl[i] = ProxyIUnknown_AddRef;
1788 break;
1789 case 2:
1790 proxy->lpvtbl[i] = ProxyIUnknown_Release;
1791 break;
1792 case 3:
1793 if(!defer_to_dispatch)
1794 {
1795 hres = init_proxy_entry_point(proxy, i);
1796 if(FAILED(hres)) return hres;
1797 }
1798 else proxy->lpvtbl[3] = ProxyIDispatch_GetTypeInfoCount;
1799 break;
1800 case 4:
1801 if(!defer_to_dispatch)
1802 {
1803 hres = init_proxy_entry_point(proxy, i);
1804 if(FAILED(hres)) return hres;
1805 }
1806 else proxy->lpvtbl[4] = ProxyIDispatch_GetTypeInfo;
1807 break;
1808 case 5:
1809 if(!defer_to_dispatch)
1810 {
1811 hres = init_proxy_entry_point(proxy, i);
1812 if(FAILED(hres)) return hres;
1813 }
1814 else proxy->lpvtbl[5] = ProxyIDispatch_GetIDsOfNames;
1815 break;
1816 case 6:
1817 if(!defer_to_dispatch)
1818 {
1819 hres = init_proxy_entry_point(proxy, i);
1820 if(FAILED(hres)) return hres;
1821 }
1822 else proxy->lpvtbl[6] = ProxyIDispatch_Invoke;
1823 break;
1824 default:
1825 hres = init_proxy_entry_point(proxy, i);
1826 if(FAILED(hres)) return hres;
1827 }
1828 }
1829
1830 if (hres == S_OK)
1831 {
1832 *ppv = proxy;
1833 *ppProxy = (IRpcProxyBuffer *)&(proxy->lpvtbl2);
1834 IUnknown_AddRef((IUnknown *)*ppv);
1835 return S_OK;
1836 }
1837 else
1838 TMProxyImpl_Release((IRpcProxyBuffer *)&proxy->lpvtbl2);
1839 return hres;
1840 }
1841
1842 typedef struct _TMStubImpl {
1843 const IRpcStubBufferVtbl *lpvtbl;
1844 LONG ref;
1845
1846 LPUNKNOWN pUnk;
1847 ITypeInfo *tinfo;
1848 IID iid;
1849 IRpcStubBuffer *dispatch_stub;
1850 BOOL dispatch_derivative;
1851 } TMStubImpl;
1852
1853 static HRESULT WINAPI
1854 TMStubImpl_QueryInterface(LPRPCSTUBBUFFER iface, REFIID riid, LPVOID *ppv)
1855 {
1856 if (IsEqualIID(riid,&IID_IRpcStubBuffer)||IsEqualIID(riid,&IID_IUnknown)){
1857 *ppv = iface;
1858 IRpcStubBuffer_AddRef(iface);
1859 return S_OK;
1860 }
1861 FIXME("%s, not supported IID.\n",debugstr_guid(riid));
1862 return E_NOINTERFACE;
1863 }
1864
1865 static ULONG WINAPI
1866 TMStubImpl_AddRef(LPRPCSTUBBUFFER iface)
1867 {
1868 TMStubImpl *This = (TMStubImpl *)iface;
1869 ULONG refCount = InterlockedIncrement(&This->ref);
1870
1871 TRACE("(%p)->(ref before=%u)\n", This, refCount - 1);
1872
1873 return refCount;
1874 }
1875
1876 static ULONG WINAPI
1877 TMStubImpl_Release(LPRPCSTUBBUFFER iface)
1878 {
1879 TMStubImpl *This = (TMStubImpl *)iface;
1880 ULONG refCount = InterlockedDecrement(&This->ref);
1881
1882 TRACE("(%p)->(ref before=%u)\n", This, refCount + 1);
1883
1884 if (!refCount)
1885 {
1886 IRpcStubBuffer_Disconnect(iface);
1887 ITypeInfo_Release(This->tinfo);
1888 if (This->dispatch_stub)
1889 IRpcStubBuffer_Release(This->dispatch_stub);
1890 CoTaskMemFree(This);
1891 }
1892 return refCount;
1893 }
1894
1895 static HRESULT WINAPI
1896 TMStubImpl_Connect(LPRPCSTUBBUFFER iface, LPUNKNOWN pUnkServer)
1897 {
1898 TMStubImpl *This = (TMStubImpl *)iface;
1899
1900 TRACE("(%p)->(%p)\n", This, pUnkServer);
1901
1902 IUnknown_AddRef(pUnkServer);
1903 This->pUnk = pUnkServer;
1904
1905 if (This->dispatch_stub)
1906 IRpcStubBuffer_Connect(This->dispatch_stub, pUnkServer);
1907
1908 return S_OK;
1909 }
1910
1911 static void WINAPI
1912 TMStubImpl_Disconnect(LPRPCSTUBBUFFER iface)
1913 {
1914 TMStubImpl *This = (TMStubImpl *)iface;
1915
1916 TRACE("(%p)->()\n", This);
1917
1918 if (This->pUnk)
1919 {
1920 IUnknown_Release(This->pUnk);
1921 This->pUnk = NULL;
1922 }
1923
1924 if (This->dispatch_stub)
1925 IRpcStubBuffer_Disconnect(This->dispatch_stub);
1926 }
1927
1928 static HRESULT WINAPI
1929 TMStubImpl_Invoke(
1930 LPRPCSTUBBUFFER iface, RPCOLEMESSAGE* xmsg,IRpcChannelBuffer*rpcchanbuf)
1931 {
1932 #ifdef __i386__
1933 int i;
1934 const FUNCDESC *fdesc;
1935 TMStubImpl *This = (TMStubImpl *)iface;
1936 HRESULT hres;
1937 DWORD *args = NULL, res, *xargs, nrofargs;
1938 marshal_state buf;
1939 UINT nrofnames = 0;
1940 BSTR names[10];
1941 BSTR iname = NULL;
1942 ITypeInfo *tinfo = NULL;
1943
1944 TRACE("...\n");
1945
1946 if (xmsg->iMethod < 3) {
1947 ERR("IUnknown methods cannot be marshaled by the typelib marshaler\n");
1948 return E_UNEXPECTED;
1949 }
1950
1951 if (This->dispatch_derivative && xmsg->iMethod < sizeof(IDispatchVtbl)/sizeof(void *))
1952 {
1953 IPSFactoryBuffer *factory_buffer;
1954 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
1955 if (hres == S_OK)
1956 {
1957 hres = IPSFactoryBuffer_CreateStub(factory_buffer, &IID_IDispatch,
1958 This->pUnk, &This->dispatch_stub);
1959 IPSFactoryBuffer_Release(factory_buffer);
1960 }
1961 if (hres != S_OK)
1962 return hres;
1963 return IRpcStubBuffer_Invoke(This->dispatch_stub, xmsg, rpcchanbuf);
1964 }
1965
1966 memset(&buf,0,sizeof(buf));
1967 buf.size = xmsg->cbBuffer;
1968 buf.base = HeapAlloc(GetProcessHeap(), 0, xmsg->cbBuffer);
1969 memcpy(buf.base, xmsg->Buffer, xmsg->cbBuffer);
1970 buf.curoff = 0;
1971
1972 hres = get_funcdesc(This->tinfo,xmsg->iMethod,&tinfo,&fdesc,&iname,NULL,NULL);
1973 if (hres) {
1974 ERR("GetFuncDesc on method %d failed with %x\n",xmsg->iMethod,hres);
1975 return hres;
1976 }
1977
1978 if (iname && !lstrcmpW(iname, IDispatchW))
1979 {
1980 ERR("IDispatch cannot be marshaled by the typelib marshaler\n");
1981 hres = E_UNEXPECTED;
1982 SysFreeString (iname);
1983 goto exit;
1984 }
1985
1986 SysFreeString (iname);
1987
1988 /* Need them for hack below */
1989 memset(names,0,sizeof(names));
1990 ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames);
1991 if (nrofnames > sizeof(names)/sizeof(names[0])) {
1992 ERR("Need more names!\n");
1993 }
1994
1995 /*dump_FUNCDESC(fdesc);*/
1996 nrofargs = 0;
1997 for (i=0;i<fdesc->cParams;i++)
1998 nrofargs += _argsize(&fdesc->lprgelemdescParam[i].tdesc, tinfo);
1999 args = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(nrofargs+1)*sizeof(DWORD));
2000 if (!args)
2001 {
2002 hres = E_OUTOFMEMORY;
2003 goto exit;
2004 }
2005
2006 /* Allocate all stuff used by call. */
2007 xargs = args+1;
2008 for (i=0;i<fdesc->cParams;i++) {
2009 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2010
2011 hres = deserialize_param(
2012 tinfo,
2013 is_in_elem(elem),
2014 FALSE,
2015 TRUE,
2016 &(elem->tdesc),
2017 xargs,
2018 &buf
2019 );
2020 xargs += _argsize(&elem->tdesc, tinfo);
2021 if (hres) {
2022 ERR("Failed to deserialize param %s, hres %x\n",relaystr(names[i+1]),hres);
2023 break;
2024 }
2025 }
2026
2027 args[0] = (DWORD)This->pUnk;
2028
2029 __TRY
2030 {
2031 res = _invoke(
2032 (*((FARPROC**)args[0]))[fdesc->oVft/4],
2033 fdesc->callconv,
2034 (xargs-args),
2035 args
2036 );
2037 }
2038 __EXCEPT_ALL
2039 {
2040 DWORD dwExceptionCode = GetExceptionCode();
2041 ERR("invoke call failed with exception 0x%08x (%d)\n", dwExceptionCode, dwExceptionCode);
2042 if (FAILED(dwExceptionCode))
2043 hres = dwExceptionCode;
2044 else
2045 hres = HRESULT_FROM_WIN32(dwExceptionCode);
2046 }
2047 __ENDTRY
2048
2049 if (hres != S_OK)
2050 goto exit;
2051
2052 buf.curoff = 0;
2053
2054 xargs = args+1;
2055 for (i=0;i<fdesc->cParams;i++) {
2056 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2057 hres = serialize_param(
2058 tinfo,
2059 is_out_elem(elem),
2060 FALSE,
2061 TRUE,
2062 &elem->tdesc,
2063 xargs,
2064 &buf
2065 );
2066 xargs += _argsize(&elem->tdesc, tinfo);
2067 if (hres) {
2068 ERR("Failed to stuballoc param, hres %x\n",hres);
2069 break;
2070 }
2071 }
2072
2073 hres = xbuf_add (&buf, (LPBYTE)&res, sizeof(DWORD));
2074
2075 if (hres != S_OK)
2076 goto exit;
2077
2078 xmsg->cbBuffer = buf.curoff;
2079 hres = IRpcChannelBuffer_GetBuffer(rpcchanbuf, xmsg, &This->iid);
2080 if (hres != S_OK)
2081 ERR("IRpcChannelBuffer_GetBuffer failed with error 0x%08x\n", hres);
2082
2083 if (hres == S_OK)
2084 memcpy(xmsg->Buffer, buf.base, buf.curoff);
2085
2086 exit:
2087 for (i = 0; i < nrofnames; i++)
2088 SysFreeString(names[i]);
2089
2090 ITypeInfo_Release(tinfo);
2091 HeapFree(GetProcessHeap(), 0, args);
2092
2093 HeapFree(GetProcessHeap(), 0, buf.base);
2094
2095 TRACE("returning\n");
2096 return hres;
2097 #else
2098 FIXME( "not implemented on non-i386\n" );
2099 return E_FAIL;
2100 #endif
2101 }
2102
2103 static LPRPCSTUBBUFFER WINAPI
2104 TMStubImpl_IsIIDSupported(LPRPCSTUBBUFFER iface, REFIID riid) {
2105 FIXME("Huh (%s)?\n",debugstr_guid(riid));
2106 return NULL;
2107 }
2108
2109 static ULONG WINAPI
2110 TMStubImpl_CountRefs(LPRPCSTUBBUFFER iface) {
2111 TMStubImpl *This = (TMStubImpl *)iface;
2112
2113 FIXME("()\n");
2114 return This->ref; /*FIXME? */
2115 }
2116
2117 static HRESULT WINAPI
2118 TMStubImpl_DebugServerQueryInterface(LPRPCSTUBBUFFER iface, LPVOID *ppv) {
2119 return E_NOTIMPL;
2120 }
2121
2122 static void WINAPI
2123 TMStubImpl_DebugServerRelease(LPRPCSTUBBUFFER iface, LPVOID ppv) {
2124 return;
2125 }
2126
2127 static const IRpcStubBufferVtbl tmstubvtbl = {
2128 TMStubImpl_QueryInterface,
2129 TMStubImpl_AddRef,
2130 TMStubImpl_Release,
2131 TMStubImpl_Connect,
2132 TMStubImpl_Disconnect,
2133 TMStubImpl_Invoke,
2134 TMStubImpl_IsIIDSupported,
2135 TMStubImpl_CountRefs,
2136 TMStubImpl_DebugServerQueryInterface,
2137 TMStubImpl_DebugServerRelease
2138 };
2139
2140 static HRESULT WINAPI
2141 PSFacBuf_CreateStub(
2142 LPPSFACTORYBUFFER iface, REFIID riid,IUnknown *pUnkServer,
2143 IRpcStubBuffer** ppStub
2144 ) {
2145 HRESULT hres;
2146 ITypeInfo *tinfo;
2147 TMStubImpl *stub;
2148 TYPEATTR *typeattr;
2149
2150 TRACE("(%s,%p,%p)\n",debugstr_guid(riid),pUnkServer,ppStub);
2151
2152 hres = _get_typeinfo_for_iid(riid,&tinfo);
2153 if (hres) {
2154 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
2155 return hres;
2156 }
2157
2158 stub = CoTaskMemAlloc(sizeof(TMStubImpl));
2159 if (!stub)
2160 return E_OUTOFMEMORY;
2161 stub->lpvtbl = &tmstubvtbl;
2162 stub->ref = 1;
2163 stub->tinfo = tinfo;
2164 stub->dispatch_stub = NULL;
2165 stub->dispatch_derivative = FALSE;
2166 stub->iid = *riid;
2167 hres = IRpcStubBuffer_Connect((LPRPCSTUBBUFFER)stub,pUnkServer);
2168 *ppStub = (LPRPCSTUBBUFFER)stub;
2169 TRACE("IRpcStubBuffer: %p\n", stub);
2170 if (hres)
2171 ERR("Connect to pUnkServer failed?\n");
2172
2173 /* if we derive from IDispatch then defer to its stub for some of its methods */
2174 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
2175 if (hres == S_OK)
2176 {
2177 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
2178 stub->dispatch_derivative = TRUE;
2179 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
2180 }
2181
2182 return hres;
2183 }
2184
2185 static const IPSFactoryBufferVtbl psfacbufvtbl = {
2186 PSFacBuf_QueryInterface,
2187 PSFacBuf_AddRef,
2188 PSFacBuf_Release,
2189 PSFacBuf_CreateProxy,
2190 PSFacBuf_CreateStub
2191 };
2192
2193 /* This is the whole PSFactoryBuffer object, just the vtableptr */
2194 static const IPSFactoryBufferVtbl *lppsfac = &psfacbufvtbl;
2195
2196 /***********************************************************************
2197 * TMARSHAL_DllGetClassObject
2198 */
2199 HRESULT TMARSHAL_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
2200 {
2201 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)) {
2202 *ppv = &lppsfac;
2203 return S_OK;
2204 }
2205 return E_NOINTERFACE;
2206 }