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