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