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