- Remove redundant QueryInterface in marshal_interface - the object is
[wine.git] / dlls / oleaut32 / tmarshal.c
blobeded5c64df561e2431414bc1d677676f166a8c9d
1 /*
2 * TYPELIB Marshaler
4 * Copyright 2002,2005 Marcus Meissner
6 * The olerelay debug channel allows you to see calls marshalled by
7 * the typelib marshaller. It is not a generic COM relaying system.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "config.h"
26 #include <assert.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <ctype.h>
33 #define COBJMACROS
34 #define NONAMELESSUNION
35 #define NONAMELESSSTRUCT
37 #include "winerror.h"
38 #include "windef.h"
39 #include "winbase.h"
40 #include "winnls.h"
41 #include "winreg.h"
42 #include "winuser.h"
44 #include "ole2.h"
45 #include "typelib.h"
46 #include "wine/debug.h"
48 static const WCHAR riidW[5] = {'r','i','i','d',0};
49 static const WCHAR pdispparamsW[] = {'p','d','i','s','p','p','a','r','a','m','s',0};
50 static const WCHAR ppvObjectW[] = {'p','p','v','O','b','j','e','c','t',0};
51 static const WCHAR IDispatchW[] = { 'I','D','i','s','p','a','t','c','h',0};
52 static const WCHAR GetIDsOfNamesW[] = { 'G','e','t','I','D','s','O','f','N','a','m','e','s',0};
54 WINE_DEFAULT_DEBUG_CHANNEL(ole);
55 WINE_DECLARE_DEBUG_CHANNEL(olerelay);
57 #define ICOM_THIS_MULTI(impl,field,iface) impl* const This=(impl*)((char*)(iface) - offsetof(impl,field))
59 typedef struct _marshal_state {
60 LPBYTE base;
61 int size;
62 int curoff;
64 BOOL thisisiid;
65 IID iid; /* HACK: for VT_VOID */
66 } marshal_state;
68 /* used in the olerelay code to avoid having the L"" stuff added by debugstr_w */
69 static char *relaystr(WCHAR *in) {
70 char *tmp = (char *)debugstr_w(in);
71 tmp += 2;
72 tmp[strlen(tmp)-1] = '\0';
73 return tmp;
76 static HRESULT
77 xbuf_add(marshal_state *buf, LPBYTE stuff, DWORD size) {
78 while (buf->size - buf->curoff < size) {
79 if (buf->base) {
80 buf->size += 100;
81 buf->base = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,buf->base,buf->size);
82 if (!buf->base)
83 return E_OUTOFMEMORY;
84 } else {
85 buf->base = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,32);
86 buf->size = 32;
87 if (!buf->base)
88 return E_OUTOFMEMORY;
91 memcpy(buf->base+buf->curoff,stuff,size);
92 buf->curoff += size;
93 return S_OK;
96 static HRESULT
97 xbuf_get(marshal_state *buf, LPBYTE stuff, DWORD size) {
98 if (buf->size < buf->curoff+size) return E_FAIL;
99 memcpy(stuff,buf->base+buf->curoff,size);
100 buf->curoff += size;
101 return S_OK;
104 static HRESULT
105 xbuf_skip(marshal_state *buf, DWORD size) {
106 if (buf->size < buf->curoff+size) return E_FAIL;
107 buf->curoff += size;
108 return S_OK;
111 static HRESULT
112 _unmarshal_interface(marshal_state *buf, REFIID riid, LPUNKNOWN *pUnk) {
113 IStream *pStm;
114 ULARGE_INTEGER newpos;
115 LARGE_INTEGER seekto;
116 ULONG res;
117 HRESULT hres;
118 DWORD xsize;
120 TRACE("...%s...\n",debugstr_guid(riid));
122 *pUnk = NULL;
123 hres = xbuf_get(buf,(LPBYTE)&xsize,sizeof(xsize));
124 if (hres) {
125 ERR("xbuf_get failed\n");
126 return hres;
129 if (xsize == 0) return S_OK;
131 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
132 if (hres) {
133 ERR("Stream create failed %lx\n",hres);
134 return hres;
137 hres = IStream_Write(pStm,buf->base+buf->curoff,xsize,&res);
138 if (hres) {
139 ERR("stream write %lx\n",hres);
140 return hres;
143 memset(&seekto,0,sizeof(seekto));
144 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
145 if (hres) {
146 ERR("Failed Seek %lx\n",hres);
147 return hres;
150 hres = CoUnmarshalInterface(pStm,riid,(LPVOID*)pUnk);
151 if (hres) {
152 ERR("Unmarshalling interface %s failed with %lx\n",debugstr_guid(riid),hres);
153 return hres;
156 IStream_Release(pStm);
157 return xbuf_skip(buf,xsize);
160 static HRESULT
161 _marshal_interface(marshal_state *buf, REFIID riid, LPUNKNOWN pUnk) {
162 LPBYTE tempbuf = NULL;
163 IStream *pStm = NULL;
164 STATSTG ststg;
165 ULARGE_INTEGER newpos;
166 LARGE_INTEGER seekto;
167 ULONG res;
168 DWORD xsize;
169 HRESULT hres;
171 if (!pUnk) {
172 /* this is valid, if for instance we serialize
173 * a VT_DISPATCH with NULL ptr which apparently
174 * can happen. S_OK to make sure we continue
175 * serializing.
177 ERR("pUnk is NULL?\n");
178 xsize = 0;
179 return xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
182 hres = E_FAIL;
184 TRACE("...%s...\n",debugstr_guid(riid));
186 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
187 if (hres) {
188 ERR("Stream create failed %lx\n",hres);
189 goto fail;
192 hres = CoMarshalInterface(pStm,riid,pUnk,0,NULL,0);
193 if (hres) {
194 ERR("Marshalling interface %s failed with %lx\n", debugstr_guid(riid), hres);
195 goto fail;
198 hres = IStream_Stat(pStm,&ststg,0);
199 if (hres) {
200 ERR("Stream stat failed\n");
201 goto fail;
204 tempbuf = HeapAlloc(GetProcessHeap(), 0, ststg.cbSize.u.LowPart);
205 memset(&seekto,0,sizeof(seekto));
206 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
207 if (hres) {
208 ERR("Failed Seek %lx\n",hres);
209 goto fail;
212 hres = IStream_Read(pStm,tempbuf,ststg.cbSize.u.LowPart,&res);
213 if (hres) {
214 ERR("Failed Read %lx\n",hres);
215 goto fail;
218 xsize = ststg.cbSize.u.LowPart;
219 xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
220 hres = xbuf_add(buf,tempbuf,ststg.cbSize.u.LowPart);
222 HeapFree(GetProcessHeap(),0,tempbuf);
223 IStream_Release(pStm);
225 return hres;
227 fail:
228 xsize = 0;
229 xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
230 if (pStm) IUnknown_Release(pStm);
231 HeapFree(GetProcessHeap(), 0, tempbuf);
232 return hres;
235 /********************* OLE Proxy/Stub Factory ********************************/
236 static HRESULT WINAPI
237 PSFacBuf_QueryInterface(LPPSFACTORYBUFFER iface, REFIID iid, LPVOID *ppv) {
238 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)||IsEqualIID(iid,&IID_IUnknown)) {
239 *ppv = (LPVOID)iface;
240 /* No ref counting, static class */
241 return S_OK;
243 FIXME("(%s) unknown IID?\n",debugstr_guid(iid));
244 return E_NOINTERFACE;
247 static ULONG WINAPI PSFacBuf_AddRef(LPPSFACTORYBUFFER iface) { return 2; }
248 static ULONG WINAPI PSFacBuf_Release(LPPSFACTORYBUFFER iface) { return 1; }
250 static HRESULT
251 _get_typeinfo_for_iid(REFIID riid, ITypeInfo**ti) {
252 HRESULT hres;
253 HKEY ikey;
254 char tlguid[200],typelibkey[300],interfacekey[300],ver[100];
255 char tlfn[260];
256 OLECHAR tlfnW[260];
257 DWORD tlguidlen, verlen, type;
258 LONG tlfnlen;
259 ITypeLib *tl;
261 sprintf( interfacekey, "Interface\\{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\Typelib",
262 riid->Data1, riid->Data2, riid->Data3,
263 riid->Data4[0], riid->Data4[1], riid->Data4[2], riid->Data4[3],
264 riid->Data4[4], riid->Data4[5], riid->Data4[6], riid->Data4[7]
267 if (RegOpenKeyA(HKEY_CLASSES_ROOT,interfacekey,&ikey)) {
268 ERR("No %s key found.\n",interfacekey);
269 return E_FAIL;
271 type = (1<<REG_SZ);
272 tlguidlen = sizeof(tlguid);
273 if (RegQueryValueExA(ikey,NULL,NULL,&type,(LPBYTE)tlguid,&tlguidlen)) {
274 ERR("Getting typelib guid failed.\n");
275 RegCloseKey(ikey);
276 return E_FAIL;
278 type = (1<<REG_SZ);
279 verlen = sizeof(ver);
280 if (RegQueryValueExA(ikey,"Version",NULL,&type,(LPBYTE)ver,&verlen)) {
281 ERR("Could not get version value?\n");
282 RegCloseKey(ikey);
283 return E_FAIL;
285 RegCloseKey(ikey);
286 sprintf(typelibkey,"Typelib\\%s\\%s\\0\\win32",tlguid,ver);
287 tlfnlen = sizeof(tlfn);
288 if (RegQueryValueA(HKEY_CLASSES_ROOT,typelibkey,tlfn,&tlfnlen)) {
289 ERR("Could not get typelib fn?\n");
290 return E_FAIL;
292 MultiByteToWideChar(CP_ACP, 0, tlfn, -1, tlfnW, -1);
293 hres = LoadTypeLib(tlfnW,&tl);
294 if (hres) {
295 ERR("Failed to load typelib for %s, but it should be there.\n",debugstr_guid(riid));
296 return hres;
298 hres = ITypeLib_GetTypeInfoOfGuid(tl,riid,ti);
299 if (hres) {
300 ERR("typelib does not contain info for %s?\n",debugstr_guid(riid));
301 ITypeLib_Release(tl);
302 return hres;
304 /* FIXME: do this? ITypeLib_Release(tl); */
305 return hres;
308 /* Determine nr of functions. Since we use the toplevel interface and all
309 * inherited ones have lower numbers, we are ok to not to descent into
310 * the inheritance tree I think.
312 static int _nroffuncs(ITypeInfo *tinfo) {
313 int n, max = 0;
314 FUNCDESC *fdesc;
315 HRESULT hres;
317 n=0;
318 while (1) {
319 hres = ITypeInfo_GetFuncDesc(tinfo,n,&fdesc);
320 if (hres)
321 return max+1;
322 if (fdesc->oVft/4 > max)
323 max = fdesc->oVft/4;
324 n++;
326 /*NOTREACHED*/
329 #ifdef __i386__
331 #include "pshpack1.h"
333 typedef struct _TMAsmProxy {
334 BYTE popleax;
335 BYTE pushlval;
336 BYTE nr;
337 BYTE pushleax;
338 BYTE lcall;
339 DWORD xcall;
340 BYTE lret;
341 WORD bytestopop;
342 } TMAsmProxy;
344 #include "poppack.h"
346 #else /* __i386__ */
347 # error You need to implement stubless proxies for your architecture
348 #endif
350 typedef struct _TMProxyImpl {
351 LPVOID *lpvtbl;
352 const IRpcProxyBufferVtbl *lpvtbl2;
353 LONG ref;
355 TMAsmProxy *asmstubs;
356 ITypeInfo* tinfo;
357 IRpcChannelBuffer* chanbuf;
358 IID iid;
359 CRITICAL_SECTION crit;
360 IUnknown *outerunknown;
361 } TMProxyImpl;
363 static HRESULT WINAPI
364 TMProxyImpl_QueryInterface(LPRPCPROXYBUFFER iface, REFIID riid, LPVOID *ppv)
366 TRACE("()\n");
367 if (IsEqualIID(riid,&IID_IUnknown)||IsEqualIID(riid,&IID_IRpcProxyBuffer)) {
368 *ppv = (LPVOID)iface;
369 IRpcProxyBuffer_AddRef(iface);
370 return S_OK;
372 FIXME("no interface for %s\n",debugstr_guid(riid));
373 return E_NOINTERFACE;
376 static ULONG WINAPI
377 TMProxyImpl_AddRef(LPRPCPROXYBUFFER iface)
379 ICOM_THIS_MULTI(TMProxyImpl,lpvtbl2,iface);
380 ULONG refCount = InterlockedIncrement(&This->ref);
382 TRACE("(%p)->(ref before=%lu)\n",This, refCount - 1);
384 return refCount;
387 static ULONG WINAPI
388 TMProxyImpl_Release(LPRPCPROXYBUFFER iface)
390 ICOM_THIS_MULTI(TMProxyImpl,lpvtbl2,iface);
391 ULONG refCount = InterlockedDecrement(&This->ref);
393 TRACE("(%p)->(ref before=%lu)\n",This, refCount + 1);
395 if (!refCount)
397 DeleteCriticalSection(&This->crit);
398 if (This->chanbuf) IRpcChannelBuffer_Release(This->chanbuf);
399 VirtualFree(This->asmstubs, 0, MEM_RELEASE);
400 CoTaskMemFree(This);
402 return refCount;
405 static HRESULT WINAPI
406 TMProxyImpl_Connect(
407 LPRPCPROXYBUFFER iface,IRpcChannelBuffer* pRpcChannelBuffer)
409 ICOM_THIS_MULTI(TMProxyImpl, lpvtbl2, iface);
411 TRACE("(%p)\n", pRpcChannelBuffer);
413 EnterCriticalSection(&This->crit);
415 IRpcChannelBuffer_AddRef(pRpcChannelBuffer);
416 This->chanbuf = pRpcChannelBuffer;
418 LeaveCriticalSection(&This->crit);
420 return S_OK;
423 static void WINAPI
424 TMProxyImpl_Disconnect(LPRPCPROXYBUFFER iface)
426 ICOM_THIS_MULTI(TMProxyImpl, lpvtbl2, iface);
428 TRACE("()\n");
430 EnterCriticalSection(&This->crit);
432 IRpcChannelBuffer_Release(This->chanbuf);
433 This->chanbuf = NULL;
435 LeaveCriticalSection(&This->crit);
439 static const IRpcProxyBufferVtbl tmproxyvtable = {
440 TMProxyImpl_QueryInterface,
441 TMProxyImpl_AddRef,
442 TMProxyImpl_Release,
443 TMProxyImpl_Connect,
444 TMProxyImpl_Disconnect
447 /* how much space do we use on stack in DWORD steps. */
449 _argsize(DWORD vt) {
450 switch (vt) {
451 case VT_R8:
452 return sizeof(double)/sizeof(DWORD);
453 case VT_CY:
454 return sizeof(CY)/sizeof(DWORD);
455 case VT_DATE:
456 return sizeof(DATE)/sizeof(DWORD);
457 case VT_VARIANT:
458 return (sizeof(VARIANT)+3)/sizeof(DWORD);
459 default:
460 return 1;
464 static int
465 _xsize(TYPEDESC *td) {
466 switch (td->vt) {
467 case VT_DATE:
468 return sizeof(DATE);
469 case VT_VARIANT:
470 return sizeof(VARIANT)+3;
471 case VT_CARRAY: {
472 int i, arrsize = 1;
473 ARRAYDESC *adesc = td->u.lpadesc;
475 for (i=0;i<adesc->cDims;i++)
476 arrsize *= adesc->rgbounds[i].cElements;
477 return arrsize*_xsize(&adesc->tdescElem);
479 case VT_UI2:
480 case VT_I2:
481 return 2;
482 case VT_UI1:
483 case VT_I1:
484 return 1;
485 default:
486 return 4;
490 static HRESULT
491 serialize_param(
492 ITypeInfo *tinfo,
493 BOOL writeit,
494 BOOL debugout,
495 BOOL dealloc,
496 TYPEDESC *tdesc,
497 DWORD *arg,
498 marshal_state *buf)
500 HRESULT hres = S_OK;
502 TRACE("(tdesc.vt %d)\n",tdesc->vt);
504 switch (tdesc->vt) {
505 case VT_EMPTY: /* nothing. empty variant for instance */
506 return S_OK;
507 case VT_BOOL:
508 case VT_ERROR:
509 case VT_UINT:
510 case VT_I4:
511 case VT_R4:
512 case VT_UI4:
513 hres = S_OK;
514 if (debugout) TRACE_(olerelay)("%lx",*arg);
515 if (writeit)
516 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
517 return hres;
518 case VT_I2:
519 case VT_UI2:
520 hres = S_OK;
521 if (debugout) TRACE_(olerelay)("%04lx",*arg & 0xffff);
522 if (writeit)
523 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
524 return hres;
525 case VT_I1:
526 case VT_UI1:
527 hres = S_OK;
528 if (debugout) TRACE_(olerelay)("%02lx",*arg & 0xff);
529 if (writeit)
530 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
531 return hres;
532 case VT_I4|VT_BYREF:
533 hres = S_OK;
534 if (debugout) TRACE_(olerelay)("&0x%lx",*arg);
535 if (writeit)
536 hres = xbuf_add(buf,(LPBYTE)(DWORD*)*arg,sizeof(DWORD));
537 /* do not dealloc at this time */
538 return hres;
539 case VT_VARIANT: {
540 TYPEDESC tdesc2;
541 VARIANT *vt = (VARIANT*)arg;
542 DWORD vttype = V_VT(vt);
544 if (debugout) TRACE_(olerelay)("Vt(%ld)(",vttype);
545 tdesc2.vt = vttype;
546 if (writeit) {
547 hres = xbuf_add(buf,(LPBYTE)&vttype,sizeof(vttype));
548 if (hres) return hres;
550 /* need to recurse since we need to free the stuff */
551 hres = serialize_param(tinfo,writeit,debugout,dealloc,&tdesc2,(DWORD*)&(V_I4(vt)),buf);
552 if (debugout) TRACE_(olerelay)(")");
553 return hres;
555 case VT_BSTR|VT_BYREF: {
556 if (debugout) TRACE_(olerelay)("[byref]'%s'", *(BSTR*)*arg ? relaystr(*((BSTR*)*arg)) : "<bstr NULL>");
557 if (writeit) {
558 /* ptr to ptr to magic widestring, basically */
559 BSTR *bstr = (BSTR *) *arg;
560 DWORD len;
561 if (!*bstr) {
562 /* -1 means "null string" which is equivalent to empty string */
563 len = -1;
564 hres = xbuf_add(buf, (LPBYTE)&len,sizeof(DWORD));
565 if (hres) return hres;
566 } else {
567 len = *((DWORD*)*bstr-1)/sizeof(WCHAR);
568 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
569 if (hres) return hres;
570 hres = xbuf_add(buf,(LPBYTE)*bstr,len * sizeof(WCHAR));
571 if (hres) return hres;
575 if (dealloc && arg) {
576 BSTR *str = *((BSTR **)arg);
577 SysFreeString(*str);
579 return S_OK;
582 case VT_BSTR: {
583 if (debugout) {
584 if (*arg)
585 TRACE_(olerelay)("%s",relaystr((WCHAR*)*arg));
586 else
587 TRACE_(olerelay)("<bstr NULL>");
589 if (writeit) {
590 BSTR bstr = (BSTR)*arg;
591 DWORD len;
592 if (!bstr) {
593 len = -1;
594 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
595 if (hres) return hres;
596 } else {
597 len = *((DWORD*)bstr-1)/sizeof(WCHAR);
598 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
599 if (hres) return hres;
600 hres = xbuf_add(buf,(LPBYTE)bstr,len * sizeof(WCHAR));
601 if (hres) return hres;
605 if (dealloc && arg)
606 SysFreeString((BSTR)*arg);
607 return S_OK;
609 case VT_PTR: {
610 DWORD cookie;
611 BOOL derefhere;
613 derefhere = (tdesc->u.lptdesc->vt != VT_USERDEFINED);
615 if (debugout) TRACE_(olerelay)("*");
616 /* Write always, so the other side knows when it gets a NULL pointer.
618 cookie = *arg ? 0x42424242 : 0;
619 hres = xbuf_add(buf,(LPBYTE)&cookie,sizeof(cookie));
620 if (hres)
621 return hres;
622 if (!*arg) {
623 if (debugout) TRACE_(olerelay)("NULL");
624 return S_OK;
626 hres = serialize_param(tinfo,writeit,debugout,dealloc,tdesc->u.lptdesc,(DWORD*)*arg,buf);
627 if (derefhere && dealloc) HeapFree(GetProcessHeap(),0,(LPVOID)*arg);
628 return hres;
630 case VT_UNKNOWN:
631 if (debugout) TRACE_(olerelay)("unk(0x%lx)",*arg);
632 if (writeit)
633 hres = _marshal_interface(buf,&IID_IUnknown,(LPUNKNOWN)*arg);
634 return hres;
635 case VT_DISPATCH:
636 if (debugout) TRACE_(olerelay)("idisp(0x%lx)",*arg);
637 if (writeit)
638 hres = _marshal_interface(buf,&IID_IDispatch,(LPUNKNOWN)*arg);
639 return hres;
640 case VT_VOID:
641 if (debugout) TRACE_(olerelay)("<void>");
642 return S_OK;
643 case VT_USERDEFINED: {
644 ITypeInfo *tinfo2;
645 TYPEATTR *tattr;
647 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
648 if (hres) {
649 ERR("Could not get typeinfo of hreftype %lx for VT_USERDEFINED.\n",tdesc->u.hreftype);
650 return hres;
652 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
653 switch (tattr->typekind) {
654 case TKIND_DISPATCH:
655 case TKIND_INTERFACE:
656 if (writeit)
657 hres=_marshal_interface(buf,&(tattr->guid),(LPUNKNOWN)arg);
658 if (dealloc)
659 IUnknown_Release((LPUNKNOWN)arg);
660 break;
661 case TKIND_RECORD: {
662 int i;
663 if (debugout) TRACE_(olerelay)("{");
664 for (i=0;i<tattr->cVars;i++) {
665 VARDESC *vdesc;
666 ELEMDESC *elem2;
667 TYPEDESC *tdesc2;
669 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
670 if (hres) {
671 ERR("Could not get vardesc of %d\n",i);
672 return hres;
674 /* Need them for hack below */
676 memset(names,0,sizeof(names));
677 hres = ITypeInfo_GetNames(tinfo2,vdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames);
678 if (nrofnames > sizeof(names)/sizeof(names[0])) {
679 ERR("Need more names!\n");
681 if (!hres && debugout)
682 TRACE_(olerelay)("%s=",relaystr(names[0]));
684 elem2 = &vdesc->elemdescVar;
685 tdesc2 = &elem2->tdesc;
686 hres = serialize_param(
687 tinfo2,
688 writeit,
689 debugout,
690 dealloc,
691 tdesc2,
692 (DWORD*)(((LPBYTE)arg)+vdesc->u.oInst),
695 ITypeInfo_ReleaseVarDesc(tinfo2, vdesc);
696 if (hres!=S_OK)
697 return hres;
698 if (debugout && (i<(tattr->cVars-1)))
699 TRACE_(olerelay)(",");
701 if (buf->thisisiid && (tattr->cbSizeInstance==sizeof(GUID)))
702 memcpy(&(buf->iid),arg,sizeof(buf->iid));
703 if (debugout) TRACE_(olerelay)("}");
704 break;
706 case TKIND_ALIAS:
707 return serialize_param(tinfo2,writeit,debugout,dealloc,&tattr->tdescAlias,arg,buf);
708 case TKIND_ENUM:
709 hres = S_OK;
710 if (debugout) TRACE_(olerelay)("%lx",*arg);
711 if (writeit)
712 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
713 return hres;
714 default:
715 FIXME("Unhandled typekind %d\n",tattr->typekind);
716 hres = E_FAIL;
717 break;
719 ITypeInfo_Release(tinfo2);
720 return hres;
722 case VT_CARRAY: {
723 ARRAYDESC *adesc = tdesc->u.lpadesc;
724 int i, arrsize = 1;
726 if (debugout) TRACE_(olerelay)("carr");
727 for (i=0;i<adesc->cDims;i++) {
728 if (debugout) TRACE_(olerelay)("[%ld]",adesc->rgbounds[i].cElements);
729 arrsize *= adesc->rgbounds[i].cElements;
731 if (debugout) TRACE_(olerelay)("(vt %d)",adesc->tdescElem.vt);
732 if (debugout) TRACE_(olerelay)("[");
733 for (i=0;i<arrsize;i++) {
734 hres = serialize_param(tinfo, writeit, debugout, dealloc, &adesc->tdescElem, (DWORD*)((LPBYTE)arg+i*_xsize(&adesc->tdescElem)), buf);
735 if (hres)
736 return hres;
737 if (debugout && (i<arrsize-1)) TRACE_(olerelay)(",");
739 if (debugout) TRACE_(olerelay)("]");
740 return S_OK;
742 default:
743 ERR("Unhandled marshal type %d.\n",tdesc->vt);
744 return S_OK;
748 /* IDL desc:
749 * HRESULT GetIDsOfNames(
750 * [in] REFIID riid, args[1]
751 * [in, size_is(cNames)] LPOLESTR *rgszNames, args[2]
752 * [in] UINT cNames, args[3]
753 * [in] LCID lcid, args[4]
754 * [out, size_is(cNames)] DISPID *rgDispId); args[5]
756 * line format:
757 * IID iid;
758 * DWORD cNames;
759 * LPOLESTR rgszNames[cNames];
760 * DWORD bytestrlen (incl 0)
761 * BYTE data[bytestrlen] (incl 0)
762 * LCID
764 static HRESULT
765 serialize_IDispatch_GetIDsOfNames(
766 BOOL inputparams,
767 BOOL debugout,
768 DWORD *args,
769 marshal_state *buf)
771 HRESULT hres;
772 DWORD cNames = args[2];
773 LPOLESTR *rgszNames = (LPOLESTR*)args[1];
774 int i;
776 if (inputparams) {
777 if (debugout) TRACE_(olerelay)("riid=%s,",debugstr_guid((REFIID)args[0]));
778 hres = xbuf_add(buf, (LPBYTE)args[0], sizeof(IID));
779 if (hres) {
780 FIXME("serialize of IID failed.\n");
781 return hres;
783 if (debugout) TRACE_(olerelay)("cNames=%ld,",cNames);
784 hres = xbuf_add(buf, (LPBYTE)&cNames, sizeof(DWORD));
785 if (hres) {
786 FIXME("serialize of cNames failed.\n");
787 return hres;
789 if (debugout) TRACE_(olerelay)("rgszNames=[");
790 for (i=0;i<cNames;i++) {
791 DWORD len = 2*(lstrlenW(rgszNames[i])+1);
793 if (debugout) TRACE_(olerelay)("%s,",relaystr(rgszNames[i]));
794 hres = xbuf_add(buf, (LPBYTE)&len, sizeof(DWORD));
795 if (hres) {
796 FIXME("serialize of len failed.\n");
797 return hres;
799 hres = xbuf_add(buf, (LPBYTE)rgszNames[i], len);
800 if (hres) {
801 FIXME("serialize of rgszNames[i] failed.\n");
802 return hres;
805 if (debugout) TRACE_(olerelay)("],lcid=%04lx)",args[3]);
806 hres = xbuf_add(buf, (LPBYTE)&args[3], sizeof(DWORD));
807 if (hres) {
808 FIXME("serialize of lcid failed.\n");
809 return hres;
811 } else {
812 DISPID *rgDispId = (DISPID*)args[4];
814 hres = xbuf_add(buf, (LPBYTE)rgDispId, sizeof(DISPID) * cNames);
815 if (hres) {
816 FIXME("serialize of rgDispId failed.\n");
817 return hres;
819 if (debugout) {
820 TRACE_(olerelay)("riid=[in],rgszNames=[in],cNames=[in],rgDispId=[");
821 for (i=0;i<cNames;i++)
822 TRACE_(olerelay)("%08lx,",rgDispId[i]);
823 TRACE_(olerelay)("])");
825 HeapFree(GetProcessHeap(),0,(IID*)args[0]);
826 rgszNames = (LPOLESTR*)args[1];
827 for (i=0;i<cNames;i++) HeapFree(GetProcessHeap(),0,rgszNames[i]);
828 HeapFree(GetProcessHeap(),0,rgszNames);
829 HeapFree(GetProcessHeap(),0,rgDispId);
831 return S_OK;
834 static HRESULT
835 deserialize_IDispatch_GetIDsOfNames(
836 BOOL inputparams,
837 BOOL debugout,
838 DWORD *args,
839 marshal_state *buf)
841 HRESULT hres;
842 DWORD cNames;
843 LPOLESTR *rgszNames;
844 int i;
846 if (inputparams) {
847 args[0] = (DWORD)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IID));
848 if (!args[0]) return E_FAIL;
849 hres = xbuf_get(buf, (LPBYTE)args[0], sizeof(IID));
850 if (hres) {
851 FIXME("deserialize of IID failed.\n");
852 return hres;
854 if (debugout) TRACE_(olerelay)("riid=%s,",debugstr_guid((REFIID)args[0]));
856 hres = xbuf_get(buf, (LPBYTE)&cNames, sizeof(DWORD));
857 if (hres) {
858 FIXME("deserialize of cNames failed.\n");
859 return hres;
861 args[2] = cNames;
862 if (debugout) TRACE_(olerelay)("cNames=%ld,",cNames);
863 if (debugout) TRACE_(olerelay)("rgszNames=[");
864 rgszNames = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(LPOLESTR) * cNames);
865 if (!rgszNames) return E_FAIL;
866 args[1] = (DWORD)rgszNames;
867 for (i=0;i<cNames;i++) {
868 DWORD len;
870 hres = xbuf_get(buf, (LPBYTE)&len, sizeof(DWORD));
871 if (hres) {
872 FIXME("serialize of len failed.\n");
873 return hres;
875 rgszNames[i] = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
876 if (!rgszNames[i]) {
877 FIXME("heapalloc of %ld bytes failed\n", len);
878 return E_FAIL;
880 hres = xbuf_get(buf, (LPBYTE)rgszNames[i], len);
881 if (hres) {
882 FIXME("serialize of rgszNames[i] failed.\n");
883 return hres;
885 if (debugout) TRACE_(olerelay)("%s,",relaystr(rgszNames[i]));
887 hres = xbuf_get(buf, (LPBYTE)&args[3], sizeof(DWORD));
888 if (hres) {
889 FIXME("deserialize of lcid failed.\n");
890 return hres;
892 if (debugout) TRACE_(olerelay)("],lcid=%04lx,rgDispId=[out])",args[3]);
893 args[4] = (DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DISPID) * cNames);
894 } else {
895 hres = xbuf_get(buf, (LPBYTE)args[4], sizeof(DISPID) * args[2]);
896 if (hres) {
897 FIXME("serialize of rgDispId failed.\n");
898 return hres;
900 if (debugout) {
901 TRACE_(olerelay)("dispid=[");
902 for (i=0;i<args[2];i++)
903 TRACE_(olerelay)("%08lx,",((DISPID*)args[4])[i]);
904 TRACE_(olerelay)("])");
907 return S_OK;
910 static HRESULT
911 serialize_LPVOID_ptr(
912 ITypeInfo *tinfo,
913 BOOL writeit,
914 BOOL debugout,
915 BOOL dealloc,
916 TYPEDESC *tdesc,
917 DWORD *arg,
918 marshal_state *buf)
920 HRESULT hres;
921 DWORD cookie;
923 if ((tdesc->vt != VT_PTR) ||
924 (tdesc->u.lptdesc->vt != VT_PTR) ||
925 (tdesc->u.lptdesc->u.lptdesc->vt != VT_VOID)
927 FIXME("ppvObject not expressed as VT_PTR -> VT_PTR -> VT_VOID?\n");
928 return E_FAIL;
930 cookie = (*(DWORD*)*arg) ? 0x42424242: 0x0;
931 if (writeit) {
932 hres = xbuf_add(buf, (LPVOID)&cookie, sizeof(cookie));
933 if (hres)
934 return hres;
936 if (!*(DWORD*)*arg) {
937 if (debugout) TRACE_(olerelay)("<lpvoid NULL>");
938 return S_OK;
940 if (debugout)
941 TRACE_(olerelay)("ppv(%p)",*(LPUNKNOWN*)*arg);
942 if (writeit) {
943 hres = _marshal_interface(buf,&(buf->iid),*(LPUNKNOWN*)*arg);
944 if (hres)
945 return hres;
947 if (dealloc)
948 HeapFree(GetProcessHeap(),0,(LPVOID)*arg);
949 return S_OK;
952 static HRESULT
953 serialize_DISPPARAM_ptr(
954 ITypeInfo *tinfo,
955 BOOL writeit,
956 BOOL debugout,
957 BOOL dealloc,
958 TYPEDESC *tdesc,
959 DWORD *arg,
960 marshal_state *buf)
962 DWORD cookie;
963 HRESULT hres;
964 DISPPARAMS *disp;
965 int i;
967 if ((tdesc->vt != VT_PTR) || (tdesc->u.lptdesc->vt != VT_USERDEFINED)) {
968 FIXME("DISPPARAMS not expressed as VT_PTR -> VT_USERDEFINED?\n");
969 return E_FAIL;
972 cookie = *arg ? 0x42424242 : 0x0;
973 if (writeit) {
974 hres = xbuf_add(buf,(LPBYTE)&cookie,sizeof(cookie));
975 if (hres)
976 return hres;
978 if (!*arg) {
979 if (debugout) TRACE_(olerelay)("<DISPPARAMS NULL>");
980 return S_OK;
982 disp = (DISPPARAMS*)*arg;
983 if (writeit) {
984 hres = xbuf_add(buf,(LPBYTE)&disp->cArgs,sizeof(disp->cArgs));
985 if (hres)
986 return hres;
988 if (debugout) TRACE_(olerelay)("D{");
989 for (i=0;i<disp->cArgs;i++) {
990 TYPEDESC vtdesc;
992 vtdesc.vt = VT_VARIANT;
993 serialize_param(
994 tinfo,
995 writeit,
996 debugout,
997 dealloc,
998 &vtdesc,
999 (DWORD*)(disp->rgvarg+i),
1002 if (debugout && (i<disp->cArgs-1))
1003 TRACE_(olerelay)(",");
1005 if (dealloc)
1006 HeapFree(GetProcessHeap(),0,disp->rgvarg);
1007 if (writeit) {
1008 hres = xbuf_add(buf,(LPBYTE)&disp->cNamedArgs,sizeof(disp->cNamedArgs));
1009 if (hres)
1010 return hres;
1012 if (debugout) TRACE_(olerelay)("}{");
1013 for (i=0;i<disp->cNamedArgs;i++) {
1014 TYPEDESC vtdesc;
1016 vtdesc.vt = VT_UINT;
1017 serialize_param(
1018 tinfo,
1019 writeit,
1020 debugout,
1021 dealloc,
1022 &vtdesc,
1023 (DWORD*)(disp->rgdispidNamedArgs+i),
1026 if (debugout && (i<disp->cNamedArgs-1))
1027 TRACE_(olerelay)(",");
1029 if (debugout) TRACE_(olerelay)("}");
1030 if (dealloc) {
1031 HeapFree(GetProcessHeap(),0,disp->rgdispidNamedArgs);
1032 HeapFree(GetProcessHeap(),0,disp);
1034 return S_OK;
1037 static HRESULT
1038 deserialize_param(
1039 ITypeInfo *tinfo,
1040 BOOL readit,
1041 BOOL debugout,
1042 BOOL alloc,
1043 TYPEDESC *tdesc,
1044 DWORD *arg,
1045 marshal_state *buf)
1047 HRESULT hres = S_OK;
1049 TRACE("vt %d at %p\n",tdesc->vt,arg);
1051 while (1) {
1052 switch (tdesc->vt) {
1053 case VT_EMPTY:
1054 if (debugout) TRACE_(olerelay)("<empty>");
1055 return S_OK;
1056 case VT_NULL:
1057 if (debugout) TRACE_(olerelay)("<null>");
1058 return S_OK;
1059 case VT_VARIANT: {
1060 VARIANT *vt = (VARIANT*)arg;
1062 if (readit) {
1063 DWORD vttype;
1064 TYPEDESC tdesc2;
1065 hres = xbuf_get(buf,(LPBYTE)&vttype,sizeof(vttype));
1066 if (hres) {
1067 FIXME("vt type not read?\n");
1068 return hres;
1070 memset(&tdesc2,0,sizeof(tdesc2));
1071 tdesc2.vt = vttype;
1072 V_VT(vt) = vttype;
1073 if (debugout) TRACE_(olerelay)("Vt(%ld)(",vttype);
1074 hres = deserialize_param(tinfo, readit, debugout, alloc, &tdesc2, (DWORD*)&(V_I4(vt)), buf);
1075 TRACE_(olerelay)(")");
1076 return hres;
1077 } else {
1078 VariantInit(vt);
1079 return S_OK;
1082 case VT_ERROR:
1083 case VT_BOOL:
1084 case VT_I4:
1085 case VT_UINT:
1086 case VT_R4:
1087 case VT_UI4:
1088 if (readit) {
1089 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
1090 if (hres) ERR("Failed to read integer 4 byte\n");
1092 if (debugout) TRACE_(olerelay)("%lx",*arg);
1093 return hres;
1094 case VT_I2:
1095 case VT_UI2:
1096 if (readit) {
1097 DWORD x;
1098 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
1099 if (hres) ERR("Failed to read integer 4 byte\n");
1100 memcpy(arg,&x,2);
1102 if (debugout) TRACE_(olerelay)("%04lx",*arg & 0xffff);
1103 return hres;
1104 case VT_I1:
1105 case VT_UI1:
1106 if (readit) {
1107 DWORD x;
1108 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
1109 if (hres) ERR("Failed to read integer 4 byte\n");
1110 memcpy(arg,&x,1);
1112 if (debugout) TRACE_(olerelay)("%02lx",*arg & 0xff);
1113 return hres;
1114 case VT_I4|VT_BYREF:
1115 hres = S_OK;
1116 if (alloc)
1117 *arg = (DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
1118 if (readit) {
1119 hres = xbuf_get(buf,(LPBYTE)*arg,sizeof(DWORD));
1120 if (hres) ERR("Failed to read integer 4 byte\n");
1122 if (debugout) TRACE_(olerelay)("&0x%lx",*(DWORD*)*arg);
1123 return hres;
1124 case VT_BSTR|VT_BYREF: {
1125 BSTR **bstr = (BSTR **)arg;
1126 WCHAR *str;
1127 DWORD len;
1129 if (readit) {
1130 hres = xbuf_get(buf,(LPBYTE)&len,sizeof(DWORD));
1131 if (hres) {
1132 ERR("failed to read bstr klen\n");
1133 return hres;
1135 if (len == -1) {
1136 *bstr = CoTaskMemAlloc(sizeof(BSTR *));
1137 **bstr = NULL;
1138 if (debugout) TRACE_(olerelay)("<bstr NULL>");
1139 } else {
1140 str = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(len+1)*sizeof(WCHAR));
1141 hres = xbuf_get(buf,(LPBYTE)str,len*sizeof(WCHAR));
1142 if (hres) {
1143 ERR("Failed to read BSTR.\n");
1144 return hres;
1146 *bstr = CoTaskMemAlloc(sizeof(BSTR *));
1147 **bstr = SysAllocStringLen(str,len);
1148 if (debugout) TRACE_(olerelay)("%s",relaystr(str));
1149 HeapFree(GetProcessHeap(),0,str);
1151 } else {
1152 *bstr = NULL;
1154 return S_OK;
1156 case VT_BSTR: {
1157 WCHAR *str;
1158 DWORD len;
1160 if (readit) {
1161 hres = xbuf_get(buf,(LPBYTE)&len,sizeof(DWORD));
1162 if (hres) {
1163 ERR("failed to read bstr klen\n");
1164 return hres;
1166 if (len == -1) {
1167 *arg = 0;
1168 if (debugout) TRACE_(olerelay)("<bstr NULL>");
1169 } else {
1170 str = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(len+1)*sizeof(WCHAR));
1171 hres = xbuf_get(buf,(LPBYTE)str,len*sizeof(WCHAR));
1172 if (hres) {
1173 ERR("Failed to read BSTR.\n");
1174 return hres;
1176 *arg = (DWORD)SysAllocStringLen(str,len);
1177 if (debugout) TRACE_(olerelay)("%s",relaystr(str));
1178 HeapFree(GetProcessHeap(),0,str);
1180 } else {
1181 *arg = 0;
1183 return S_OK;
1185 case VT_PTR: {
1186 DWORD cookie;
1187 BOOL derefhere = 0;
1189 derefhere = (tdesc->u.lptdesc->vt != VT_USERDEFINED);
1190 /* read it in all cases, we need to know if we have
1191 * NULL pointer or not.
1193 hres = xbuf_get(buf,(LPBYTE)&cookie,sizeof(cookie));
1194 if (hres) {
1195 ERR("Failed to load pointer cookie.\n");
1196 return hres;
1198 if (cookie != 0x42424242) {
1199 /* we read a NULL ptr from the remote side */
1200 if (debugout) TRACE_(olerelay)("NULL");
1201 *arg = 0;
1202 return S_OK;
1204 if (debugout) TRACE_(olerelay)("*");
1205 if (alloc) {
1206 /* Allocate space for the referenced struct */
1207 if (derefhere)
1208 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,_xsize(tdesc->u.lptdesc));
1210 if (derefhere)
1211 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, (LPDWORD)*arg, buf);
1212 else
1213 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, arg, buf);
1215 case VT_UNKNOWN:
1216 /* FIXME: UNKNOWN is unknown ..., but allocate 4 byte for it */
1217 if (alloc)
1218 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
1219 hres = S_OK;
1220 if (readit)
1221 hres = _unmarshal_interface(buf,&IID_IUnknown,(LPUNKNOWN*)arg);
1222 if (debugout)
1223 TRACE_(olerelay)("unk(%p)",arg);
1224 return hres;
1225 case VT_DISPATCH:
1226 hres = S_OK;
1227 if (readit)
1228 hres = _unmarshal_interface(buf,&IID_IDispatch,(LPUNKNOWN*)arg);
1229 if (debugout)
1230 TRACE_(olerelay)("idisp(%p)",arg);
1231 return hres;
1232 case VT_VOID:
1233 if (debugout) TRACE_(olerelay)("<void>");
1234 return S_OK;
1235 case VT_USERDEFINED: {
1236 ITypeInfo *tinfo2;
1237 TYPEATTR *tattr;
1239 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
1240 if (hres) {
1241 ERR("Could not get typeinfo of hreftype %lx for VT_USERDEFINED.\n",tdesc->u.hreftype);
1242 return hres;
1244 hres = ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1245 if (hres) {
1246 ERR("Could not get typeattr in VT_USERDEFINED.\n");
1247 } else {
1248 switch (tattr->typekind) {
1249 case TKIND_DISPATCH:
1250 case TKIND_INTERFACE:
1251 if (readit)
1252 hres = _unmarshal_interface(buf,&(tattr->guid),(LPUNKNOWN*)arg);
1253 break;
1254 case TKIND_RECORD: {
1255 int i;
1257 if (alloc)
1258 *arg = (DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,tattr->cbSizeInstance);
1260 if (debugout) TRACE_(olerelay)("{");
1261 for (i=0;i<tattr->cVars;i++) {
1262 VARDESC *vdesc;
1264 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
1265 if (hres) {
1266 ERR("Could not get vardesc of %d\n",i);
1267 return hres;
1269 hres = deserialize_param(
1270 tinfo2,
1271 readit,
1272 debugout,
1273 alloc,
1274 &vdesc->elemdescVar.tdesc,
1275 (DWORD*)(((LPBYTE)*arg)+vdesc->u.oInst),
1278 if (debugout && (i<tattr->cVars-1)) TRACE_(olerelay)(",");
1280 if (buf->thisisiid && (tattr->cbSizeInstance==sizeof(GUID)))
1281 memcpy(&(buf->iid),(LPBYTE)*arg,sizeof(buf->iid));
1282 if (debugout) TRACE_(olerelay)("}");
1283 break;
1285 case TKIND_ALIAS:
1286 return deserialize_param(tinfo2,readit,debugout,alloc,&tattr->tdescAlias,arg,buf);
1287 case TKIND_ENUM:
1288 if (readit) {
1289 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
1290 if (hres) ERR("Failed to read enum (4 byte)\n");
1292 if (debugout) TRACE_(olerelay)("%lx",*arg);
1293 return hres;
1294 default:
1295 ERR("Unhandled typekind %d\n",tattr->typekind);
1296 hres = E_FAIL;
1297 break;
1300 if (hres)
1301 ERR("failed to stuballoc in TKIND_RECORD.\n");
1302 ITypeInfo_Release(tinfo2);
1303 return hres;
1305 case VT_CARRAY: {
1306 /* arg is pointing to the start of the array. */
1307 ARRAYDESC *adesc = tdesc->u.lpadesc;
1308 int arrsize,i;
1309 arrsize = 1;
1310 if (adesc->cDims > 1) FIXME("cDims > 1 in VT_CARRAY. Does it work?\n");
1311 for (i=0;i<adesc->cDims;i++)
1312 arrsize *= adesc->rgbounds[i].cElements;
1313 for (i=0;i<arrsize;i++)
1314 deserialize_param(
1315 tinfo,
1316 readit,
1317 debugout,
1318 alloc,
1319 &adesc->tdescElem,
1320 (DWORD*)((LPBYTE)(arg)+i*_xsize(&adesc->tdescElem)),
1323 return S_OK;
1325 default:
1326 ERR("No handler for VT type %d!\n",tdesc->vt);
1327 return S_OK;
1332 static HRESULT
1333 deserialize_LPVOID_ptr(
1334 ITypeInfo *tinfo,
1335 BOOL readit,
1336 BOOL debugout,
1337 BOOL alloc,
1338 TYPEDESC *tdesc,
1339 DWORD *arg,
1340 marshal_state *buf
1342 HRESULT hres;
1343 DWORD cookie;
1345 if ((tdesc->vt != VT_PTR) ||
1346 (tdesc->u.lptdesc->vt != VT_PTR) ||
1347 (tdesc->u.lptdesc->u.lptdesc->vt != VT_VOID)
1349 FIXME("ppvObject not expressed as VT_PTR -> VT_PTR -> VT_VOID?\n");
1350 return E_FAIL;
1352 if (alloc)
1353 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(LPVOID));
1354 if (readit) {
1355 hres = xbuf_get(buf, (LPVOID)&cookie, sizeof(cookie));
1356 if (hres)
1357 return hres;
1358 if (cookie != 0x42424242) {
1359 *(DWORD*)*arg = 0;
1360 if (debugout) TRACE_(olerelay)("<lpvoid NULL>");
1361 return S_OK;
1364 if (readit) {
1365 hres = _unmarshal_interface(buf,&buf->iid,(LPUNKNOWN*)*arg);
1366 if (hres) {
1367 FIXME("_unmarshal_interface of %s , %p failed with %lx\n", debugstr_guid(&buf->iid), (LPUNKNOWN*)*arg, hres);
1368 return hres;
1371 if (debugout) TRACE_(olerelay)("ppv(%p)",(LPVOID)*arg);
1372 return S_OK;
1375 static HRESULT
1376 deserialize_DISPPARAM_ptr(
1377 ITypeInfo *tinfo,
1378 BOOL readit,
1379 BOOL debugout,
1380 BOOL alloc,
1381 TYPEDESC *tdesc,
1382 DWORD *arg,
1383 marshal_state *buf)
1385 DWORD cookie;
1386 DISPPARAMS *disps;
1387 HRESULT hres;
1388 int i;
1390 if ((tdesc->vt != VT_PTR) || (tdesc->u.lptdesc->vt != VT_USERDEFINED)) {
1391 FIXME("DISPPARAMS not expressed as VT_PTR -> VT_USERDEFINED?\n");
1392 return E_FAIL;
1394 if (readit) {
1395 hres = xbuf_get(buf,(LPBYTE)&cookie,sizeof(cookie));
1396 if (hres)
1397 return hres;
1398 if (cookie == 0) {
1399 *arg = 0;
1400 if (debugout) TRACE_(olerelay)("<DISPPARAMS NULL>");
1401 return S_OK;
1404 if (alloc)
1405 *arg = (DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DISPPARAMS));
1406 disps = (DISPPARAMS*)*arg;
1407 if (!readit)
1408 return S_OK;
1409 hres = xbuf_get(buf, (LPBYTE)&disps->cArgs, sizeof(disps->cArgs));
1410 if (hres)
1411 return hres;
1412 if (alloc)
1413 disps->rgvarg = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(VARIANT)*disps->cArgs);
1414 if (debugout) TRACE_(olerelay)("D{");
1415 for (i=0; i< disps->cArgs; i++) {
1416 TYPEDESC vdesc;
1418 vdesc.vt = VT_VARIANT;
1419 hres = deserialize_param(
1420 tinfo,
1421 readit,
1422 debugout,
1423 alloc,
1424 &vdesc,
1425 (DWORD*)(disps->rgvarg+i),
1429 if (debugout) TRACE_(olerelay)("}{");
1430 hres = xbuf_get(buf, (LPBYTE)&disps->cNamedArgs, sizeof(disps->cNamedArgs));
1431 if (hres)
1432 return hres;
1433 if (disps->cNamedArgs) {
1434 if (alloc)
1435 disps->rgdispidNamedArgs = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DISPID)*disps->cNamedArgs);
1436 for (i=0; i< disps->cNamedArgs; i++) {
1437 TYPEDESC vdesc;
1439 vdesc.vt = VT_UINT;
1440 hres = deserialize_param(
1441 tinfo,
1442 readit,
1443 debugout,
1444 alloc,
1445 &vdesc,
1446 (DWORD*)(disps->rgdispidNamedArgs+i),
1449 if (debugout && i<(disps->cNamedArgs-1)) TRACE_(olerelay)(",");
1452 if (debugout) TRACE_(olerelay)("}");
1453 return S_OK;
1456 /* Searches function, also in inherited interfaces */
1457 static HRESULT
1458 _get_funcdesc(
1459 ITypeInfo *tinfo, int iMethod, ITypeInfo **tactual, FUNCDESC **fdesc, BSTR *iname, BSTR *fname)
1461 int i = 0, j = 0;
1462 HRESULT hres;
1464 if (fname) *fname = NULL;
1465 if (iname) *iname = NULL;
1467 *tactual = tinfo;
1468 ITypeInfo_AddRef(*tactual);
1470 while (1) {
1471 hres = ITypeInfo_GetFuncDesc(tinfo, i, fdesc);
1472 if (hres) {
1473 ITypeInfo *tinfo2;
1474 HREFTYPE href;
1475 TYPEATTR *attr;
1477 hres = ITypeInfo_GetTypeAttr(tinfo, &attr);
1478 if (hres) {
1479 ERR("GetTypeAttr failed with %lx\n",hres);
1480 return hres;
1482 /* Not found, so look in inherited ifaces. */
1483 for (j=0;j<attr->cImplTypes;j++) {
1484 hres = ITypeInfo_GetRefTypeOfImplType(tinfo, j, &href);
1485 if (hres) {
1486 ERR("Did not find a reftype for interface offset %d?\n",j);
1487 break;
1489 hres = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
1490 if (hres) {
1491 ERR("Did not find a typeinfo for reftype %ld?\n",href);
1492 continue;
1494 hres = _get_funcdesc(tinfo2,iMethod,tactual,fdesc,iname,fname);
1495 ITypeInfo_Release(tinfo2);
1496 if (!hres) return S_OK;
1498 return hres;
1500 if (((*fdesc)->oVft/4) == iMethod) {
1501 if (fname)
1502 ITypeInfo_GetDocumentation(tinfo,(*fdesc)->memid,fname,NULL,NULL,NULL);
1503 if (iname)
1504 ITypeInfo_GetDocumentation(tinfo,-1,iname,NULL,NULL,NULL);
1505 return S_OK;
1507 i++;
1511 static DWORD
1512 xCall(LPVOID retptr, int method, TMProxyImpl *tpinfo /*, args */)
1514 DWORD *args = ((DWORD*)&tpinfo)+1, *xargs;
1515 FUNCDESC *fdesc;
1516 HRESULT hres;
1517 int i, relaydeb = TRACE_ON(olerelay);
1518 marshal_state buf;
1519 RPCOLEMESSAGE msg;
1520 ULONG status;
1521 BSTR fname,iname;
1522 BSTR names[10];
1523 UINT nrofnames;
1524 int is_idispatch_getidsofnames = 0;
1525 DWORD remoteresult = 0;
1526 ITypeInfo *tinfo;
1528 EnterCriticalSection(&tpinfo->crit);
1530 hres = _get_funcdesc(tpinfo->tinfo,method,&tinfo,&fdesc,&iname,&fname);
1531 if (hres) {
1532 ERR("Did not find typeinfo/funcdesc entry for method %d!\n",method);
1533 ITypeInfo_Release(tinfo);
1534 LeaveCriticalSection(&tpinfo->crit);
1535 return E_FAIL;
1538 if (!tpinfo->chanbuf)
1540 WARN("Tried to use disconnected proxy\n");
1541 ITypeInfo_Release(tinfo);
1542 LeaveCriticalSection(&tpinfo->crit);
1543 return RPC_E_DISCONNECTED;
1546 if (relaydeb) {
1547 TRACE_(olerelay)("->");
1548 if (iname)
1549 TRACE_(olerelay)("%s:",relaystr(iname));
1550 if (fname)
1551 TRACE_(olerelay)("%s(%d)",relaystr(fname),method);
1552 else
1553 TRACE_(olerelay)("%d",method);
1554 TRACE_(olerelay)("(");
1556 if (iname && fname && !lstrcmpW(iname,IDispatchW) && !lstrcmpW(fname,GetIDsOfNamesW))
1557 is_idispatch_getidsofnames = 1;
1559 if (iname) SysFreeString(iname);
1560 if (fname) SysFreeString(fname);
1562 memset(&buf,0,sizeof(buf));
1563 buf.iid = IID_IUnknown;
1565 /* Special IDispatch::GetIDsOfNames() serializer */
1566 if (is_idispatch_getidsofnames) {
1567 hres = serialize_IDispatch_GetIDsOfNames(TRUE,relaydeb,args,&buf);
1568 if (hres != S_OK) {
1569 FIXME("serialize of IDispatch::GetIDsOfNames failed!\n");
1570 ITypeInfo_Release(tinfo);
1571 LeaveCriticalSection(&tpinfo->crit);
1572 return hres;
1574 goto afterserialize;
1577 /* special QueryInterface serialize */
1578 if (method == 0) {
1579 xbuf_add(&buf,(LPBYTE)args[0],sizeof(IID));
1580 if (relaydeb) TRACE_(olerelay)("riid=%s,[out])",debugstr_guid((REFIID)args[0]));
1581 goto afterserialize;
1584 /* normal typelib driven serializing */
1586 /* Need them for hack below */
1587 memset(names,0,sizeof(names));
1588 if (ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames))
1589 nrofnames = 0;
1590 if (nrofnames > sizeof(names)/sizeof(names[0]))
1591 ERR("Need more names!\n");
1593 xargs = args;
1594 for (i=0;i<fdesc->cParams;i++) {
1595 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1596 BOOL isserialized = FALSE;
1597 if (relaydeb) {
1598 if (i) TRACE_(olerelay)(",");
1599 if (i+1<nrofnames && names[i+1])
1600 TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1602 /* No need to marshal other data than FIN and any VT_PTR. */
1603 if (!(elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN) && (elem->tdesc.vt != VT_PTR)) {
1604 xargs+=_argsize(elem->tdesc.vt);
1605 if (relaydeb) TRACE_(olerelay)("[out]");
1606 continue;
1608 if (((i+1)<nrofnames) && !IsBadStringPtrW(names[i+1],1)) {
1609 /* If the parameter is 'riid', we use it as interface IID
1610 * for a later ppvObject serialization.
1612 buf.thisisiid = !lstrcmpW(names[i+1],riidW);
1614 /* DISPPARAMS* needs special serializer */
1615 if (!lstrcmpW(names[i+1],pdispparamsW)) {
1616 hres = serialize_DISPPARAM_ptr(
1617 tinfo,
1618 elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN,
1619 relaydeb,
1620 FALSE,
1621 &elem->tdesc,
1622 xargs,
1623 &buf
1625 isserialized = TRUE;
1627 if (!lstrcmpW(names[i+1],ppvObjectW)) {
1628 hres = serialize_LPVOID_ptr(
1629 tinfo,
1630 elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN,
1631 relaydeb,
1632 FALSE,
1633 &elem->tdesc,
1634 xargs,
1635 &buf
1637 if (hres == S_OK)
1638 isserialized = TRUE;
1641 if (!isserialized)
1642 hres = serialize_param(
1643 tinfo,
1644 elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN,
1645 relaydeb,
1646 FALSE,
1647 &elem->tdesc,
1648 xargs,
1649 &buf
1652 if (hres) {
1653 ERR("Failed to serialize param, hres %lx\n",hres);
1654 break;
1656 xargs+=_argsize(elem->tdesc.vt);
1658 if (relaydeb) TRACE_(olerelay)(")");
1660 afterserialize:
1661 memset(&msg,0,sizeof(msg));
1662 msg.cbBuffer = buf.curoff;
1663 msg.iMethod = method;
1664 hres = IRpcChannelBuffer_GetBuffer(tpinfo->chanbuf,&msg,&(tpinfo->iid));
1665 if (hres) {
1666 ERR("RpcChannelBuffer GetBuffer failed, %lx\n",hres);
1667 LeaveCriticalSection(&tpinfo->crit);
1668 return hres;
1670 memcpy(msg.Buffer,buf.base,buf.curoff);
1671 if (relaydeb) TRACE_(olerelay)("\n");
1672 hres = IRpcChannelBuffer_SendReceive(tpinfo->chanbuf,&msg,&status);
1673 if (hres) {
1674 ERR("RpcChannelBuffer SendReceive failed, %lx\n",hres);
1675 LeaveCriticalSection(&tpinfo->crit);
1676 return hres;
1679 if (relaydeb) TRACE_(olerelay)(" status = %08lx (",status);
1680 if (buf.base)
1681 buf.base = HeapReAlloc(GetProcessHeap(),0,buf.base,msg.cbBuffer);
1682 else
1683 buf.base = HeapAlloc(GetProcessHeap(),0,msg.cbBuffer);
1684 buf.size = msg.cbBuffer;
1685 memcpy(buf.base,msg.Buffer,buf.size);
1686 buf.curoff = 0;
1688 /* Special IDispatch::GetIDsOfNames() deserializer */
1689 if (is_idispatch_getidsofnames) {
1690 hres = deserialize_IDispatch_GetIDsOfNames(FALSE,relaydeb,args,&buf);
1691 if (hres != S_OK) {
1692 FIXME("deserialize of IDispatch::GetIDsOfNames failed!\n");
1693 return hres;
1695 goto after_deserialize;
1697 /* Special QueryInterface deserializer */
1698 if (method == 0) {
1699 _unmarshal_interface(&buf,(REFIID)args[0],(LPUNKNOWN*)args[1]);
1700 if (relaydeb) TRACE_(olerelay)("[in],%p",*((DWORD**)args[1]));
1701 goto after_deserialize;
1704 /* generic deserializer using typelib description */
1705 xargs = args;
1706 status = S_OK;
1707 for (i=0;i<fdesc->cParams;i++) {
1708 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1709 BOOL isdeserialized = FALSE;
1711 if (relaydeb) {
1712 if (i) TRACE_(olerelay)(",");
1713 if (i+1<nrofnames && names[i+1]) TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1715 /* No need to marshal other data than FOUT and any VT_PTR */
1716 if (!(elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT) && (elem->tdesc.vt != VT_PTR)) {
1717 xargs += _argsize(elem->tdesc.vt);
1718 if (relaydeb) TRACE_(olerelay)("[in]");
1719 continue;
1721 if (((i+1)<nrofnames) && !IsBadStringPtrW(names[i+1],1)) {
1722 /* If the parameter is 'riid', we use it as interface IID
1723 * for a later ppvObject serialization.
1725 buf.thisisiid = !lstrcmpW(names[i+1],riidW);
1727 /* deserialize DISPPARAM */
1728 if (!lstrcmpW(names[i+1],pdispparamsW)) {
1729 hres = deserialize_DISPPARAM_ptr(
1730 tinfo,
1731 elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT,
1732 relaydeb,
1733 FALSE,
1734 &(elem->tdesc),
1735 xargs,
1736 &buf
1738 if (hres) {
1739 ERR("Failed to deserialize DISPPARAM*, hres %lx\n",hres);
1740 break;
1742 isdeserialized = TRUE;
1744 if (!lstrcmpW(names[i+1],ppvObjectW)) {
1745 hres = deserialize_LPVOID_ptr(
1746 tinfo,
1747 elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT,
1748 relaydeb,
1749 FALSE,
1750 &elem->tdesc,
1751 xargs,
1752 &buf
1754 if (hres == S_OK)
1755 isdeserialized = TRUE;
1758 if (!isdeserialized)
1759 hres = deserialize_param(
1760 tinfo,
1761 elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT,
1762 relaydeb,
1763 FALSE,
1764 &(elem->tdesc),
1765 xargs,
1766 &buf
1768 if (hres) {
1769 ERR("Failed to unmarshall param, hres %lx\n",hres);
1770 status = hres;
1771 break;
1773 xargs += _argsize(elem->tdesc.vt);
1775 after_deserialize:
1776 hres = xbuf_get(&buf, (LPBYTE)&remoteresult, sizeof(DWORD));
1777 if (hres != S_OK)
1778 return hres;
1779 if (relaydeb) TRACE_(olerelay)(") = %08lx\n", remoteresult);
1781 if (status != S_OK) /* OLE/COM internal error */
1782 return status;
1784 HeapFree(GetProcessHeap(),0,buf.base);
1785 ITypeInfo_Release(tinfo);
1786 LeaveCriticalSection(&tpinfo->crit);
1787 return remoteresult;
1790 HRESULT WINAPI ProxyIUnknown_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
1792 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1794 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1796 if (proxy->outerunknown)
1797 return IUnknown_QueryInterface(proxy->outerunknown, riid, ppv);
1799 FIXME("No interface\n");
1800 return E_NOINTERFACE;
1803 ULONG WINAPI ProxyIUnknown_AddRef(IUnknown *iface)
1805 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1807 TRACE("\n");
1809 if (proxy->outerunknown)
1810 return IUnknown_AddRef(proxy->outerunknown);
1812 return 2; /* FIXME */
1815 ULONG WINAPI ProxyIUnknown_Release(IUnknown *iface)
1817 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1819 TRACE("\n");
1821 if (proxy->outerunknown)
1822 return IUnknown_Release(proxy->outerunknown);
1824 return 1; /* FIXME */
1827 static HRESULT WINAPI
1828 PSFacBuf_CreateProxy(
1829 LPPSFACTORYBUFFER iface, IUnknown* pUnkOuter, REFIID riid,
1830 IRpcProxyBuffer **ppProxy, LPVOID *ppv)
1832 HRESULT hres;
1833 ITypeInfo *tinfo;
1834 int i, nroffuncs;
1835 FUNCDESC *fdesc;
1836 TMProxyImpl *proxy;
1838 TRACE("(...%s...)\n",debugstr_guid(riid));
1839 hres = _get_typeinfo_for_iid(riid,&tinfo);
1840 if (hres) {
1841 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
1842 return hres;
1844 nroffuncs = _nroffuncs(tinfo);
1845 proxy = CoTaskMemAlloc(sizeof(TMProxyImpl));
1846 if (!proxy) return E_OUTOFMEMORY;
1848 assert(sizeof(TMAsmProxy) == 12);
1850 proxy->outerunknown = pUnkOuter;
1851 proxy->asmstubs = VirtualAlloc(NULL, sizeof(TMAsmProxy) * nroffuncs, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
1852 if (!proxy->asmstubs) {
1853 ERR("Could not commit pages for proxy thunks\n");
1854 CoTaskMemFree(proxy);
1855 return E_OUTOFMEMORY;
1858 InitializeCriticalSection(&proxy->crit);
1860 proxy->lpvtbl = HeapAlloc(GetProcessHeap(),0,sizeof(LPBYTE)*nroffuncs);
1861 for (i=0;i<nroffuncs;i++) {
1862 TMAsmProxy *xasm = proxy->asmstubs+i;
1864 switch (i) {
1865 case 0:
1866 proxy->lpvtbl[i] = ProxyIUnknown_QueryInterface;
1867 break;
1868 case 1:
1869 proxy->lpvtbl[i] = ProxyIUnknown_AddRef;
1870 break;
1871 case 2:
1872 proxy->lpvtbl[i] = ProxyIUnknown_Release;
1873 break;
1874 default: {
1875 int j;
1876 /* nrofargs without This */
1877 int nrofargs;
1878 ITypeInfo *tinfo2;
1879 hres = _get_funcdesc(tinfo,i,&tinfo2,&fdesc,NULL,NULL);
1880 ITypeInfo_Release(tinfo2);
1881 if (hres) {
1882 ERR("GetFuncDesc %lx should not fail here.\n",hres);
1883 return hres;
1885 /* some args take more than 4 byte on the stack */
1886 nrofargs = 0;
1887 for (j=0;j<fdesc->cParams;j++)
1888 nrofargs += _argsize(fdesc->lprgelemdescParam[j].tdesc.vt);
1890 if (fdesc->callconv != CC_STDCALL) {
1891 ERR("calling convention is not stdcall????\n");
1892 return E_FAIL;
1894 /* popl %eax - return ptr
1895 * pushl <nr>
1896 * pushl %eax
1897 * call xCall
1898 * lret <nr> (+4)
1901 * arg3 arg2 arg1 <method> <returnptr>
1903 xasm->popleax = 0x58;
1904 xasm->pushlval = 0x6a;
1905 xasm->nr = i;
1906 xasm->pushleax = 0x50;
1907 xasm->lcall = 0xe8; /* relative jump */
1908 xasm->xcall = (DWORD)xCall;
1909 xasm->xcall -= (DWORD)&(xasm->lret);
1910 xasm->lret = 0xc2;
1911 xasm->bytestopop= (nrofargs+2)*4; /* pop args, This, iMethod */
1912 proxy->lpvtbl[i] = xasm;
1913 break;
1917 proxy->lpvtbl2 = &tmproxyvtable;
1918 /* 1 reference for the proxy and 1 for the object */
1919 proxy->ref = 2;
1920 proxy->tinfo = tinfo;
1921 memcpy(&proxy->iid,riid,sizeof(*riid));
1922 proxy->chanbuf = 0;
1923 *ppv = (LPVOID)proxy;
1924 *ppProxy = (IRpcProxyBuffer *)&(proxy->lpvtbl2);
1925 return S_OK;
1928 typedef struct _TMStubImpl {
1929 const IRpcStubBufferVtbl *lpvtbl;
1930 LONG ref;
1932 LPUNKNOWN pUnk;
1933 ITypeInfo *tinfo;
1934 IID iid;
1935 } TMStubImpl;
1937 static HRESULT WINAPI
1938 TMStubImpl_QueryInterface(LPRPCSTUBBUFFER iface, REFIID riid, LPVOID *ppv)
1940 if (IsEqualIID(riid,&IID_IRpcStubBuffer)||IsEqualIID(riid,&IID_IUnknown)){
1941 *ppv = (LPVOID)iface;
1942 IRpcStubBuffer_AddRef(iface);
1943 return S_OK;
1945 FIXME("%s, not supported IID.\n",debugstr_guid(riid));
1946 return E_NOINTERFACE;
1949 static ULONG WINAPI
1950 TMStubImpl_AddRef(LPRPCSTUBBUFFER iface)
1952 TMStubImpl *This = (TMStubImpl *)iface;
1953 ULONG refCount = InterlockedIncrement(&This->ref);
1955 TRACE("(%p)->(ref before=%lu)\n", This, refCount - 1);
1957 return refCount;
1960 static ULONG WINAPI
1961 TMStubImpl_Release(LPRPCSTUBBUFFER iface)
1963 TMStubImpl *This = (TMStubImpl *)iface;
1964 ULONG refCount = InterlockedDecrement(&This->ref);
1966 TRACE("(%p)->(ref before=%lu)\n", This, refCount + 1);
1968 if (!refCount)
1970 IRpcStubBuffer_Disconnect(iface);
1971 ITypeInfo_Release(This->tinfo);
1972 CoTaskMemFree(This);
1974 return refCount;
1977 static HRESULT WINAPI
1978 TMStubImpl_Connect(LPRPCSTUBBUFFER iface, LPUNKNOWN pUnkServer)
1980 TMStubImpl *This = (TMStubImpl *)iface;
1982 TRACE("(%p)->(%p)\n", This, pUnkServer);
1984 IUnknown_AddRef(pUnkServer);
1985 This->pUnk = pUnkServer;
1986 return S_OK;
1989 static void WINAPI
1990 TMStubImpl_Disconnect(LPRPCSTUBBUFFER iface)
1992 TMStubImpl *This = (TMStubImpl *)iface;
1994 TRACE("(%p)->()\n", This);
1996 if (This->pUnk)
1998 IUnknown_Release(This->pUnk);
1999 This->pUnk = NULL;
2003 static HRESULT WINAPI
2004 TMStubImpl_Invoke(
2005 LPRPCSTUBBUFFER iface, RPCOLEMESSAGE* xmsg,IRpcChannelBuffer*rpcchanbuf)
2007 int i;
2008 FUNCDESC *fdesc;
2009 TMStubImpl *This = (TMStubImpl *)iface;
2010 HRESULT hres;
2011 DWORD *args, res, *xargs, nrofargs;
2012 marshal_state buf;
2013 UINT nrofnames;
2014 BSTR names[10];
2015 BSTR fname = NULL,iname = NULL;
2016 BOOL is_idispatch_getidsofnames = 0;
2017 ITypeInfo *tinfo;
2019 memset(&buf,0,sizeof(buf));
2020 buf.size = xmsg->cbBuffer;
2021 buf.base = HeapAlloc(GetProcessHeap(), 0, xmsg->cbBuffer);
2022 memcpy(buf.base, xmsg->Buffer, xmsg->cbBuffer);
2023 buf.curoff = 0;
2024 buf.iid = IID_IUnknown;
2026 TRACE("...\n");
2027 if (xmsg->iMethod == 0) { /* QI */
2028 IID xiid;
2029 /* in: IID, out: <iface> */
2031 xbuf_get(&buf,(LPBYTE)&xiid,sizeof(xiid));
2032 buf.curoff = 0;
2033 hres = _marshal_interface(&buf,&xiid,This->pUnk);
2034 xmsg->Buffer = buf.base; /* Might have been reallocated */
2035 xmsg->cbBuffer = buf.size;
2036 return hres;
2038 hres = _get_funcdesc(This->tinfo,xmsg->iMethod,&tinfo,&fdesc,&iname,&fname);
2039 if (hres) {
2040 ERR("GetFuncDesc on method %ld failed with %lx\n",xmsg->iMethod,hres);
2041 return hres;
2044 if (iname && fname && !lstrcmpW(iname, IDispatchW) && !lstrcmpW(fname, GetIDsOfNamesW))
2045 is_idispatch_getidsofnames = 1;
2047 if (iname) SysFreeString (iname);
2048 if (fname) SysFreeString (fname);
2050 /* Need them for hack below */
2051 memset(names,0,sizeof(names));
2052 ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames);
2053 if (nrofnames > sizeof(names)/sizeof(names[0])) {
2054 ERR("Need more names!\n");
2057 /*dump_FUNCDESC(fdesc);*/
2058 nrofargs = 0;
2059 for (i=0;i<fdesc->cParams;i++)
2060 nrofargs += _argsize(fdesc->lprgelemdescParam[i].tdesc.vt);
2061 args = HeapAlloc(GetProcessHeap(),0,(nrofargs+1)*sizeof(DWORD));
2062 if (!args) return E_OUTOFMEMORY;
2064 if (is_idispatch_getidsofnames) {
2065 hres = deserialize_IDispatch_GetIDsOfNames(TRUE,FALSE,args+1,&buf);
2066 if (hres != S_OK) {
2067 FIXME("deserialize_IDispatch_GetIDsOfNames failed!\n");
2068 return hres;
2070 xargs = args+1+5;
2071 goto afterdeserialize;
2074 /* Allocate all stuff used by call. */
2075 xargs = args+1;
2076 for (i=0;i<fdesc->cParams;i++) {
2077 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2078 BOOL isdeserialized = FALSE;
2080 if (((i+1)<nrofnames) && !IsBadStringPtrW(names[i+1],1)) {
2081 /* If the parameter is 'riid', we use it as interface IID
2082 * for a later ppvObject serialization.
2084 buf.thisisiid = !lstrcmpW(names[i+1],riidW);
2086 /* deserialize DISPPARAM */
2087 if (!lstrcmpW(names[i+1],pdispparamsW)) {
2088 hres = deserialize_DISPPARAM_ptr(
2089 tinfo,
2090 elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN,
2091 FALSE,
2092 TRUE,
2093 &(elem->tdesc),
2094 xargs,
2095 &buf
2097 if (hres) {
2098 ERR("Failed to deserialize DISPPARAM*, hres %lx\n",hres);
2099 break;
2101 isdeserialized = TRUE;
2103 if (!lstrcmpW(names[i+1],ppvObjectW)) {
2104 hres = deserialize_LPVOID_ptr(
2105 tinfo,
2106 elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN,
2107 FALSE,
2108 TRUE,
2109 &elem->tdesc,
2110 xargs,
2111 &buf
2113 if (hres == S_OK)
2114 isdeserialized = TRUE;
2117 if (!isdeserialized)
2118 hres = deserialize_param(
2119 tinfo,
2120 elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN,
2121 FALSE,
2122 TRUE,
2123 &(elem->tdesc),
2124 xargs,
2125 &buf
2127 xargs += _argsize(elem->tdesc.vt);
2128 if (hres) {
2129 ERR("Failed to deserialize param %s, hres %lx\n",relaystr(names[i+1]),hres);
2130 break;
2133 afterdeserialize:
2134 hres = IUnknown_QueryInterface(This->pUnk,&(This->iid),(LPVOID*)&(args[0]));
2135 if (hres) {
2136 ERR("Does not support iface %s, returning %lx\n",debugstr_guid(&(This->iid)), hres);
2137 return hres;
2139 res = _invoke(
2140 (*((FARPROC**)args[0]))[fdesc->oVft/4],
2141 fdesc->callconv,
2142 (xargs-args),
2143 args
2145 IUnknown_Release((LPUNKNOWN)args[0]);
2146 buf.curoff = 0;
2148 /* special IDispatch::GetIDsOfNames serializer */
2149 if (is_idispatch_getidsofnames) {
2150 hres = serialize_IDispatch_GetIDsOfNames(FALSE,FALSE,args+1,&buf);
2151 if (hres != S_OK) {
2152 FIXME("serialize of IDispatch::GetIDsOfNames failed!\n");
2153 return hres;
2155 goto afterserialize;
2157 xargs = args+1;
2158 for (i=0;i<fdesc->cParams;i++) {
2159 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2160 BOOL isserialized = FALSE;
2162 if (((i+1)<nrofnames) && !IsBadStringPtrW(names[i+1],1)) {
2163 /* If the parameter is 'riid', we use it as interface IID
2164 * for a later ppvObject serialization.
2166 buf.thisisiid = !lstrcmpW(names[i+1],riidW);
2168 /* DISPPARAMS* needs special serializer */
2169 if (!lstrcmpW(names[i+1],pdispparamsW)) {
2170 hres = serialize_DISPPARAM_ptr(
2171 tinfo,
2172 elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT,
2173 FALSE,
2174 TRUE,
2175 &elem->tdesc,
2176 xargs,
2177 &buf
2179 isserialized = TRUE;
2181 if (!lstrcmpW(names[i+1],ppvObjectW)) {
2182 hres = serialize_LPVOID_ptr(
2183 tinfo,
2184 elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT,
2185 FALSE,
2186 TRUE,
2187 &elem->tdesc,
2188 xargs,
2189 &buf
2191 if (hres == S_OK)
2192 isserialized = TRUE;
2195 if (!isserialized)
2196 hres = serialize_param(
2197 tinfo,
2198 elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT,
2199 FALSE,
2200 TRUE,
2201 &elem->tdesc,
2202 xargs,
2203 &buf
2205 xargs += _argsize(elem->tdesc.vt);
2206 if (hres) {
2207 ERR("Failed to stuballoc param, hres %lx\n",hres);
2208 break;
2211 afterserialize:
2212 hres = xbuf_add (&buf, (LPBYTE)&res, sizeof(DWORD));
2213 if (hres != S_OK)
2214 return hres;
2216 ITypeInfo_Release(tinfo);
2217 xmsg->cbBuffer = buf.curoff;
2218 I_RpcGetBuffer((RPC_MESSAGE *)xmsg);
2219 memcpy(xmsg->Buffer, buf.base, buf.curoff);
2220 HeapFree(GetProcessHeap(),0,args);
2221 return S_OK;
2224 static LPRPCSTUBBUFFER WINAPI
2225 TMStubImpl_IsIIDSupported(LPRPCSTUBBUFFER iface, REFIID riid) {
2226 FIXME("Huh (%s)?\n",debugstr_guid(riid));
2227 return NULL;
2230 static ULONG WINAPI
2231 TMStubImpl_CountRefs(LPRPCSTUBBUFFER iface) {
2232 TMStubImpl *This = (TMStubImpl *)iface;
2234 return This->ref; /*FIXME? */
2237 static HRESULT WINAPI
2238 TMStubImpl_DebugServerQueryInterface(LPRPCSTUBBUFFER iface, LPVOID *ppv) {
2239 return E_NOTIMPL;
2242 static void WINAPI
2243 TMStubImpl_DebugServerRelease(LPRPCSTUBBUFFER iface, LPVOID ppv) {
2244 return;
2247 static const IRpcStubBufferVtbl tmstubvtbl = {
2248 TMStubImpl_QueryInterface,
2249 TMStubImpl_AddRef,
2250 TMStubImpl_Release,
2251 TMStubImpl_Connect,
2252 TMStubImpl_Disconnect,
2253 TMStubImpl_Invoke,
2254 TMStubImpl_IsIIDSupported,
2255 TMStubImpl_CountRefs,
2256 TMStubImpl_DebugServerQueryInterface,
2257 TMStubImpl_DebugServerRelease
2260 static HRESULT WINAPI
2261 PSFacBuf_CreateStub(
2262 LPPSFACTORYBUFFER iface, REFIID riid,IUnknown *pUnkServer,
2263 IRpcStubBuffer** ppStub
2265 HRESULT hres;
2266 ITypeInfo *tinfo;
2267 TMStubImpl *stub;
2269 TRACE("(%s,%p,%p)\n",debugstr_guid(riid),pUnkServer,ppStub);
2270 hres = _get_typeinfo_for_iid(riid,&tinfo);
2271 if (hres) {
2272 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
2273 return hres;
2275 stub = CoTaskMemAlloc(sizeof(TMStubImpl));
2276 if (!stub)
2277 return E_OUTOFMEMORY;
2278 stub->lpvtbl = &tmstubvtbl;
2279 stub->ref = 1;
2280 stub->tinfo = tinfo;
2281 memcpy(&(stub->iid),riid,sizeof(*riid));
2282 hres = IRpcStubBuffer_Connect((LPRPCSTUBBUFFER)stub,pUnkServer);
2283 *ppStub = (LPRPCSTUBBUFFER)stub;
2284 TRACE("IRpcStubBuffer: %p\n", stub);
2285 if (hres)
2286 ERR("Connect to pUnkServer failed?\n");
2287 return hres;
2290 static const IPSFactoryBufferVtbl psfacbufvtbl = {
2291 PSFacBuf_QueryInterface,
2292 PSFacBuf_AddRef,
2293 PSFacBuf_Release,
2294 PSFacBuf_CreateProxy,
2295 PSFacBuf_CreateStub
2298 /* This is the whole PSFactoryBuffer object, just the vtableptr */
2299 static const IPSFactoryBufferVtbl *lppsfac = &psfacbufvtbl;
2301 /***********************************************************************
2302 * TMARSHAL_DllGetClassObject
2304 HRESULT TMARSHAL_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
2306 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)) {
2307 *ppv = &lppsfac;
2308 return S_OK;
2310 return E_NOINTERFACE;