Stub implementations for CertCreateCRLContext and CertCloseStore.
[wine/gsoc_dplay.git] / dlls / oleaut32 / tmarshal.c
blobb4df2959ca2d088d2e4cc64db2c6d117f156368b
1 /*
2 * TYPELIB Marshaler
4 * Copyright 2002 Marcus Meissner
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
23 #include <assert.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <ctype.h>
30 #define NONAMELESSUNION
31 #define NONAMELESSSTRUCT
32 #include "winerror.h"
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winnls.h"
36 #include "winreg.h"
37 #include "winuser.h"
39 #include "ole2.h"
40 #include "wine/unicode.h"
41 #include "ole2disp.h"
42 #include "typelib.h"
43 #include "wine/debug.h"
44 #include "winternl.h"
46 static const WCHAR riidW[5] = {'r','i','i','d',0};
47 static const WCHAR pdispparamsW[] = {'p','d','i','s','p','p','a','r','a','m','s',0};
48 static const WCHAR ppvObjectW[] = {'p','p','v','O','b','j','e','c','t',0};
50 WINE_DEFAULT_DEBUG_CHANNEL(ole);
51 WINE_DECLARE_DEBUG_CHANNEL(olerelay);
53 typedef struct _marshal_state {
54 LPBYTE base;
55 int size;
56 int curoff;
58 BOOL thisisiid;
59 IID iid; /* HACK: for VT_VOID */
60 } marshal_state;
62 static HRESULT
63 xbuf_add(marshal_state *buf, LPBYTE stuff, DWORD size) {
64 while (buf->size - buf->curoff < size) {
65 if (buf->base) {
66 buf->size += 100;
67 buf->base = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,buf->base,buf->size);
68 if (!buf->base)
69 return E_OUTOFMEMORY;
70 } else {
71 buf->base = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,32);
72 buf->size = 32;
73 if (!buf->base)
74 return E_OUTOFMEMORY;
77 memcpy(buf->base+buf->curoff,stuff,size);
78 buf->curoff += size;
79 return S_OK;
82 static HRESULT
83 xbuf_get(marshal_state *buf, LPBYTE stuff, DWORD size) {
84 if (buf->size < buf->curoff+size) return E_FAIL;
85 memcpy(stuff,buf->base+buf->curoff,size);
86 buf->curoff += size;
87 return S_OK;
90 static HRESULT
91 xbuf_skip(marshal_state *buf, DWORD size) {
92 if (buf->size < buf->curoff+size) return E_FAIL;
93 buf->curoff += size;
94 return S_OK;
97 static HRESULT
98 _unmarshal_interface(marshal_state *buf, REFIID riid, LPUNKNOWN *pUnk) {
99 IStream *pStm;
100 ULARGE_INTEGER newpos;
101 LARGE_INTEGER seekto;
102 ULONG res;
103 HRESULT hres;
104 DWORD xsize;
106 TRACE("...%s...\n",debugstr_guid(riid));
107 *pUnk = NULL;
108 hres = xbuf_get(buf,(LPBYTE)&xsize,sizeof(xsize));
109 if (hres) return hres;
110 if (xsize == 0) return S_OK;
111 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
112 if (hres) {
113 FIXME("Stream create failed %lx\n",hres);
114 return hres;
116 hres = IStream_Write(pStm,buf->base+buf->curoff,xsize,&res);
117 if (hres) { FIXME("stream write %lx\n",hres); return hres; }
118 memset(&seekto,0,sizeof(seekto));
119 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
120 if (hres) { FIXME("Failed Seek %lx\n",hres); return hres;}
121 hres = CoUnmarshalInterface(pStm,riid,(LPVOID*)pUnk);
122 if (hres) {
123 FIXME("Marshalling interface %s failed with %lx\n",debugstr_guid(riid),hres);
124 return hres;
126 IStream_Release(pStm);
127 return xbuf_skip(buf,xsize);
130 static HRESULT
131 _marshal_interface(marshal_state *buf, REFIID riid, LPUNKNOWN pUnk) {
132 LPUNKNOWN newiface;
133 LPBYTE tempbuf;
134 IStream *pStm;
135 STATSTG ststg;
136 ULARGE_INTEGER newpos;
137 LARGE_INTEGER seekto;
138 ULONG res;
139 DWORD xsize;
140 HRESULT hres;
142 hres = S_OK;
143 if (!pUnk)
144 goto fail;
146 TRACE("...%s...\n",debugstr_guid(riid));
147 hres=IUnknown_QueryInterface(pUnk,riid,(LPVOID*)&newiface);
148 if (hres) {
149 TRACE("%p does not support iface %s\n",pUnk,debugstr_guid(riid));
150 goto fail;
152 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
153 if (hres) {
154 FIXME("Stream create failed %lx\n",hres);
155 goto fail;
157 hres = CoMarshalInterface(pStm,riid,newiface,0,NULL,0);
158 IUnknown_Release(newiface);
159 if (hres) {
160 FIXME("Marshalling interface %s failed with %lx\n",
161 debugstr_guid(riid),hres
163 goto fail;
165 hres = IStream_Stat(pStm,&ststg,0);
166 tempbuf = HeapAlloc(GetProcessHeap(), 0, ststg.cbSize.s.LowPart);
167 memset(&seekto,0,sizeof(seekto));
168 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
169 if (hres) { FIXME("Failed Seek %lx\n",hres); goto fail;}
170 hres = IStream_Read(pStm,tempbuf,ststg.cbSize.s.LowPart,&res);
171 if (hres) { FIXME("Failed Read %lx\n",hres); goto fail;}
172 IStream_Release(pStm);
173 xsize = ststg.cbSize.s.LowPart;
174 xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
175 hres = xbuf_add(buf,tempbuf,ststg.cbSize.s.LowPart);
176 HeapFree(GetProcessHeap(),0,tempbuf);
177 return hres;
178 fail:
179 xsize = 0;
180 xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
181 return hres;
184 /********************* OLE Proxy/Stub Factory ********************************/
185 static HRESULT WINAPI
186 PSFacBuf_QueryInterface(LPPSFACTORYBUFFER iface, REFIID iid, LPVOID *ppv) {
187 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)||IsEqualIID(iid,&IID_IUnknown)) {
188 *ppv = (LPVOID)iface;
189 /* No ref counting, static class */
190 return S_OK;
192 FIXME("(%s) unknown IID?\n",debugstr_guid(iid));
193 return E_NOINTERFACE;
196 static ULONG WINAPI PSFacBuf_AddRef(LPPSFACTORYBUFFER iface) { return 2; }
197 static ULONG WINAPI PSFacBuf_Release(LPPSFACTORYBUFFER iface) { return 1; }
199 static HRESULT
200 _get_typeinfo_for_iid(REFIID riid, ITypeInfo**ti) {
201 HRESULT hres;
202 HKEY ikey;
203 char tlguid[200],typelibkey[300],interfacekey[300],ver[100];
204 char tlfn[260];
205 OLECHAR tlfnW[260];
206 DWORD tlguidlen, verlen, type, tlfnlen;
207 ITypeLib *tl;
209 sprintf( interfacekey, "Interface\\{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\Typelib",
210 riid->Data1, riid->Data2, riid->Data3,
211 riid->Data4[0], riid->Data4[1], riid->Data4[2], riid->Data4[3],
212 riid->Data4[4], riid->Data4[5], riid->Data4[6], riid->Data4[7]
215 if (RegOpenKeyA(HKEY_CLASSES_ROOT,interfacekey,&ikey)) {
216 FIXME("No %s key found.\n",interfacekey);
217 return E_FAIL;
219 type = (1<<REG_SZ);
220 tlguidlen = sizeof(tlguid);
221 if (RegQueryValueExA(ikey,NULL,NULL,&type,tlguid,&tlguidlen)) {
222 FIXME("Getting typelib guid failed.\n");
223 RegCloseKey(ikey);
224 return E_FAIL;
226 type = (1<<REG_SZ);
227 verlen = sizeof(ver);
228 if (RegQueryValueExA(ikey,"Version",NULL,&type,ver,&verlen)) {
229 FIXME("Could not get version value?\n");
230 RegCloseKey(ikey);
231 return E_FAIL;
233 RegCloseKey(ikey);
234 sprintf(typelibkey,"Typelib\\%s\\%s\\0\\win32",tlguid,ver);
235 tlfnlen = sizeof(tlfn);
236 if (RegQueryValueA(HKEY_CLASSES_ROOT,typelibkey,tlfn,&tlfnlen)) {
237 FIXME("Could not get typelib fn?\n");
238 return E_FAIL;
240 MultiByteToWideChar(CP_ACP, 0, tlfn, -1, tlfnW, -1);
241 hres = LoadTypeLib(tlfnW,&tl);
242 if (hres) {
243 ERR("Failed to load typelib for %s, but it should be there.\n",debugstr_guid(riid));
244 return hres;
246 hres = ITypeLib_GetTypeInfoOfGuid(tl,riid,ti);
247 if (hres) {
248 ERR("typelib does not contain info for %s?\n",debugstr_guid(riid));
249 ITypeLib_Release(tl);
250 return hres;
252 /* FIXME: do this? ITypeLib_Release(tl); */
253 return hres;
256 /* Determine nr of functions. Since we use the toplevel interface and all
257 * inherited ones have lower numbers, we are ok to not to descent into
258 * the inheritance tree I think.
260 static int _nroffuncs(ITypeInfo *tinfo) {
261 int n, max = 0;
262 FUNCDESC *fdesc;
263 HRESULT hres;
265 n=0;
266 while (1) {
267 hres = ITypeInfo_GetFuncDesc(tinfo,n,&fdesc);
268 if (hres)
269 return max+1;
270 if (fdesc->oVft/4 > max)
271 max = fdesc->oVft/4;
272 n++;
274 /*NOTREACHED*/
277 typedef struct _TMAsmProxy {
278 BYTE popleax;
279 BYTE pushlval;
280 BYTE nr;
281 BYTE pushleax;
282 BYTE lcall;
283 DWORD xcall;
284 BYTE lret;
285 WORD bytestopop;
286 } WINE_PACKED TMAsmProxy;
288 typedef struct _TMProxyImpl {
289 DWORD *lpvtbl;
290 ICOM_VTABLE(IRpcProxyBuffer) *lpvtbl2;
291 DWORD ref;
293 TMAsmProxy *asmstubs;
294 ITypeInfo* tinfo;
295 IRpcChannelBuffer* chanbuf;
296 IID iid;
297 } TMProxyImpl;
299 static HRESULT WINAPI
300 TMProxyImpl_QueryInterface(LPRPCPROXYBUFFER iface, REFIID riid, LPVOID *ppv) {
301 TRACE("()\n");
302 if (IsEqualIID(riid,&IID_IUnknown)||IsEqualIID(riid,&IID_IRpcProxyBuffer)) {
303 *ppv = (LPVOID)iface;
304 IRpcProxyBuffer_AddRef(iface);
305 return S_OK;
307 FIXME("no interface for %s\n",debugstr_guid(riid));
308 return E_NOINTERFACE;
311 static ULONG WINAPI
312 TMProxyImpl_AddRef(LPRPCPROXYBUFFER iface) {
313 ICOM_THIS_MULTI(TMProxyImpl,lpvtbl2,iface);
315 TRACE("()\n");
316 This->ref++;
317 return This->ref;
320 static ULONG WINAPI
321 TMProxyImpl_Release(LPRPCPROXYBUFFER iface) {
322 ICOM_THIS_MULTI(TMProxyImpl,lpvtbl2,iface);
324 TRACE("()\n");
325 This->ref--;
326 if (This->ref) return This->ref;
327 if (This->chanbuf) IRpcChannelBuffer_Release(This->chanbuf);
328 HeapFree(GetProcessHeap(),0,This);
329 return 0;
332 static HRESULT WINAPI
333 TMProxyImpl_Connect(
334 LPRPCPROXYBUFFER iface,IRpcChannelBuffer* pRpcChannelBuffer
336 ICOM_THIS_MULTI(TMProxyImpl,lpvtbl2,iface);
338 TRACE("(%p)\n",pRpcChannelBuffer);
339 This->chanbuf = pRpcChannelBuffer;
340 IRpcChannelBuffer_AddRef(This->chanbuf);
341 return S_OK;
344 static void WINAPI
345 TMProxyImpl_Disconnect(LPRPCPROXYBUFFER iface) {
346 ICOM_THIS_MULTI(TMProxyImpl,lpvtbl2,iface);
348 FIXME("()\n");
349 IRpcChannelBuffer_Release(This->chanbuf);
350 This->chanbuf = NULL;
354 static ICOM_VTABLE(IRpcProxyBuffer) tmproxyvtable = {
355 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
356 TMProxyImpl_QueryInterface,
357 TMProxyImpl_AddRef,
358 TMProxyImpl_Release,
359 TMProxyImpl_Connect,
360 TMProxyImpl_Disconnect
363 /* how much space do we use on stack in DWORD steps. */
364 int const
365 _argsize(DWORD vt) {
366 switch (vt) {
367 case VT_DATE:
368 return sizeof(DATE)/sizeof(DWORD);
369 case VT_VARIANT:
370 return (sizeof(VARIANT)+3)/sizeof(DWORD);
371 default:
372 return 1;
376 static int
377 _xsize(TYPEDESC *td) {
378 switch (td->vt) {
379 case VT_DATE:
380 return sizeof(DATE);
381 case VT_VARIANT:
382 return sizeof(VARIANT)+3;
383 case VT_CARRAY: {
384 int i, arrsize = 1;
385 ARRAYDESC *adesc = td->u.lpadesc;
387 for (i=0;i<adesc->cDims;i++)
388 arrsize *= adesc->rgbounds[i].cElements;
389 return arrsize*_xsize(&adesc->tdescElem);
391 case VT_UI2:
392 case VT_I2:
393 return 2;
394 case VT_UI1:
395 case VT_I1:
396 return 1;
397 default:
398 return 4;
402 static HRESULT
403 serialize_param(
404 ITypeInfo *tinfo,
405 BOOL writeit,
406 BOOL debugout,
407 BOOL dealloc,
408 TYPEDESC *tdesc,
409 DWORD *arg,
410 marshal_state *buf
412 HRESULT hres = S_OK;
414 TRACE("(tdesc.vt %d)\n",tdesc->vt);
416 switch (tdesc->vt) {
417 case VT_EMPTY: /* nothing. empty variant for instance */
418 return S_OK;
419 case VT_BOOL:
420 case VT_ERROR:
421 case VT_UI4:
422 case VT_UINT:
423 case VT_I4:
424 case VT_R4:
425 case VT_UI2:
426 case VT_UI1:
427 hres = S_OK;
428 if (debugout) MESSAGE("%lx",*arg);
429 if (writeit)
430 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
431 return hres;
432 case VT_VARIANT: {
433 TYPEDESC tdesc2;
434 VARIANT *vt = (VARIANT*)arg;
435 DWORD vttype = V_VT(vt);
437 if (debugout) MESSAGE("Vt(%ld)(",vttype);
438 tdesc2.vt = vttype;
439 if (writeit) {
440 hres = xbuf_add(buf,(LPBYTE)&vttype,sizeof(vttype));
441 if (hres) return hres;
443 /* need to recurse since we need to free the stuff */
444 hres = serialize_param(tinfo,writeit,debugout,dealloc,&tdesc2,&(V_I4(vt)),buf);
445 if (debugout) MESSAGE(")");
446 return hres;
448 case VT_BSTR: {
449 if (debugout) {
450 if (arg)
451 MESSAGE("%s",debugstr_w((BSTR)*arg));
452 else
453 MESSAGE("<bstr NULL>");
455 if (writeit) {
456 if (!*arg) {
457 DWORD fakelen = -1;
458 hres = xbuf_add(buf,(LPBYTE)&fakelen,4);
459 if (hres)
460 return hres;
461 } else {
462 DWORD *bstr = ((DWORD*)(*arg))-1;
464 hres = xbuf_add(buf,(LPBYTE)bstr,bstr[0]+4);
465 if (hres)
466 return hres;
470 if (dealloc && arg)
471 SysFreeString((BSTR)*arg);
472 return S_OK;
474 case VT_PTR: {
475 DWORD cookie;
477 if (debugout) MESSAGE("*");
478 if (writeit) {
479 cookie = *arg ? 0x42424242 : 0;
480 hres = xbuf_add(buf,(LPBYTE)&cookie,sizeof(cookie));
481 if (hres)
482 return hres;
484 if (!*arg) {
485 if (debugout) MESSAGE("NULL");
486 return S_OK;
488 hres = serialize_param(tinfo,writeit,debugout,dealloc,tdesc->u.lptdesc,(DWORD*)*arg,buf);
489 if (dealloc) HeapFree(GetProcessHeap(),0,(LPVOID)arg);
490 return hres;
492 case VT_UNKNOWN:
493 if (debugout) MESSAGE("unk(0x%lx)",*arg);
494 if (writeit)
495 hres = _marshal_interface(buf,&IID_IUnknown,(LPUNKNOWN)*arg);
496 return hres;
497 case VT_DISPATCH:
498 if (debugout) MESSAGE("idisp(0x%lx)",*arg);
499 if (writeit)
500 hres = _marshal_interface(buf,&IID_IDispatch,(LPUNKNOWN)*arg);
501 return hres;
502 case VT_VOID:
503 if (debugout) MESSAGE("<void>");
504 return S_OK;
505 case VT_USERDEFINED: {
506 ITypeInfo *tinfo2;
507 TYPEATTR *tattr;
509 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
510 if (hres) {
511 FIXME("Could not get typeinfo of hreftype %lx for VT_USERDEFINED.\n",tdesc->u.hreftype);
512 return hres;
514 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
515 switch (tattr->typekind) {
516 case TKIND_DISPATCH:
517 case TKIND_INTERFACE:
518 if (writeit)
519 hres=_marshal_interface(buf,&(tattr->guid),(LPUNKNOWN)arg);
520 break;
521 case TKIND_RECORD: {
522 int i;
523 if (debugout) MESSAGE("{");
524 for (i=0;i<tattr->cVars;i++) {
525 VARDESC *vdesc;
526 ELEMDESC *elem2;
527 TYPEDESC *tdesc2;
529 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
530 if (hres) {
531 FIXME("Could not get vardesc of %d\n",i);
532 return hres;
534 /* Need them for hack below */
536 memset(names,0,sizeof(names));
537 hres = ITypeInfo_GetNames(tinfo2,vdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames);
538 if (nrofnames > sizeof(names)/sizeof(names[0])) {
539 ERR("Need more names!\n");
541 if (!hres && debugout)
542 MESSAGE("%s=",debugstr_w(names[0]));
544 elem2 = &vdesc->elemdescVar;
545 tdesc2 = &elem2->tdesc;
546 hres = serialize_param(
547 tinfo2,
548 writeit,
549 debugout,
550 dealloc,
551 tdesc2,
552 (DWORD*)(((LPBYTE)arg)+vdesc->u.oInst),
555 if (hres!=S_OK)
556 return hres;
557 if (debugout && (i<(tattr->cVars-1)))
558 MESSAGE(",");
560 if (buf->thisisiid && (tattr->cbSizeInstance==sizeof(GUID)))
561 memcpy(&(buf->iid),arg,sizeof(buf->iid));
562 if (debugout) MESSAGE("}");
563 break;
565 default:
566 FIXME("Unhandled typekind %d\n",tattr->typekind);
567 hres = E_FAIL;
568 break;
570 ITypeInfo_Release(tinfo2);
571 return hres;
573 case VT_CARRAY: {
574 ARRAYDESC *adesc = tdesc->u.lpadesc;
575 int i, arrsize = 1;
577 if (debugout) MESSAGE("carr");
578 for (i=0;i<adesc->cDims;i++) {
579 if (debugout) MESSAGE("[%ld]",adesc->rgbounds[i].cElements);
580 arrsize *= adesc->rgbounds[i].cElements;
582 if (debugout) MESSAGE("[");
583 for (i=0;i<arrsize;i++) {
584 hres = serialize_param(tinfo, writeit, debugout, dealloc, &adesc->tdescElem, (DWORD*)((LPBYTE)arg+i*_xsize(&adesc->tdescElem)), buf);
585 if (hres)
586 return hres;
587 if (debugout && (i<arrsize-1)) MESSAGE(",");
589 if (debugout) MESSAGE("]");
590 return S_OK;
592 default:
593 ERR("Unhandled marshal type %d.\n",tdesc->vt);
594 return S_OK;
598 static HRESULT
599 serialize_LPVOID_ptr(
600 ITypeInfo *tinfo,
601 BOOL writeit,
602 BOOL debugout,
603 BOOL dealloc,
604 TYPEDESC *tdesc,
605 DWORD *arg,
606 marshal_state *buf
608 HRESULT hres;
609 DWORD cookie;
611 if ((tdesc->vt != VT_PTR) ||
612 (tdesc->u.lptdesc->vt != VT_PTR) ||
613 (tdesc->u.lptdesc->u.lptdesc->vt != VT_VOID)
615 FIXME("ppvObject not expressed as VT_PTR -> VT_PTR -> VT_VOID?\n");
616 return E_FAIL;
618 cookie = (*arg) ? 0x42424242: 0x0;
619 if (writeit) {
620 hres = xbuf_add(buf, (LPVOID)&cookie, sizeof(cookie));
621 if (hres)
622 return hres;
624 if (!*arg) {
625 if (debugout) MESSAGE("<lpvoid NULL>");
626 return S_OK;
628 if (debugout)
629 MESSAGE("ppv(%p)",*(LPUNKNOWN*)*arg);
630 if (writeit) {
631 hres = _marshal_interface(buf,&(buf->iid),*(LPUNKNOWN*)*arg);
632 if (hres)
633 return hres;
635 if (dealloc)
636 HeapFree(GetProcessHeap(),0,(LPVOID)*arg);
637 return S_OK;
640 static HRESULT
641 serialize_DISPPARAM_ptr(
642 ITypeInfo *tinfo,
643 BOOL writeit,
644 BOOL debugout,
645 BOOL dealloc,
646 TYPEDESC *tdesc,
647 DWORD *arg,
648 marshal_state *buf
650 DWORD cookie;
651 HRESULT hres;
652 DISPPARAMS *disp;
653 int i;
655 if ((tdesc->vt != VT_PTR) || (tdesc->u.lptdesc->vt != VT_USERDEFINED)) {
656 FIXME("DISPPARAMS not expressed as VT_PTR -> VT_USERDEFINED?\n");
657 return E_FAIL;
660 cookie = *arg ? 0x42424242 : 0x0;
661 if (writeit) {
662 hres = xbuf_add(buf,(LPBYTE)&cookie,sizeof(cookie));
663 if (hres)
664 return hres;
666 if (!*arg) {
667 if (debugout) MESSAGE("<DISPPARAMS NULL>");
668 return S_OK;
670 disp = (DISPPARAMS*)*arg;
671 if (writeit) {
672 hres = xbuf_add(buf,(LPBYTE)&disp->cArgs,sizeof(disp->cArgs));
673 if (hres)
674 return hres;
676 if (debugout) MESSAGE("D{");
677 for (i=0;i<disp->cArgs;i++) {
678 TYPEDESC vtdesc;
680 vtdesc.vt = VT_VARIANT;
681 serialize_param(
682 tinfo,
683 writeit,
684 debugout,
685 dealloc,
686 &vtdesc,
687 (DWORD*)(disp->rgvarg+i),
690 if (debugout && (i<disp->cArgs-1))
691 MESSAGE(",");
693 if (dealloc)
694 HeapFree(GetProcessHeap(),0,disp->rgvarg);
695 if (writeit) {
696 hres = xbuf_add(buf,(LPBYTE)&disp->cNamedArgs,sizeof(disp->cNamedArgs));
697 if (hres)
698 return hres;
700 if (debugout) MESSAGE("}{");
701 for (i=0;i<disp->cNamedArgs;i++) {
702 TYPEDESC vtdesc;
704 vtdesc.vt = VT_UINT;
705 serialize_param(
706 tinfo,
707 writeit,
708 debugout,
709 dealloc,
710 &vtdesc,
711 (DWORD*)(disp->rgdispidNamedArgs+i),
714 if (debugout && (i<disp->cNamedArgs-1))
715 MESSAGE(",");
717 if (debugout) MESSAGE("}");
718 if (dealloc) {
719 HeapFree(GetProcessHeap(),0,disp->rgdispidNamedArgs);
720 HeapFree(GetProcessHeap(),0,disp);
722 return S_OK;
725 static HRESULT
726 deserialize_param(
727 ITypeInfo *tinfo,
728 BOOL readit,
729 BOOL debugout,
730 BOOL alloc,
731 TYPEDESC *tdesc,
732 DWORD *arg,
733 marshal_state *buf
735 HRESULT hres = S_OK;
737 TRACE("vt %d at %p\n",tdesc->vt,arg);
739 while (1) {
740 switch (tdesc->vt) {
741 case VT_EMPTY:
742 if (debugout) MESSAGE("<empty>");
743 return S_OK;
744 case VT_NULL:
745 if (debugout) MESSAGE("<null>");
746 return S_OK;
747 case VT_VARIANT: {
748 VARIANT *vt = (VARIANT*)arg;
750 if (readit) {
751 DWORD vttype;
752 TYPEDESC tdesc2;
753 hres = xbuf_get(buf,(LPBYTE)&vttype,sizeof(vttype));
754 if (hres) {
755 FIXME("vt type not read?\n");
756 return hres;
758 memset(&tdesc2,0,sizeof(tdesc2));
759 tdesc2.vt = vttype;
760 V_VT(vt) = vttype;
761 if (debugout) MESSAGE("Vt(%ld)(",vttype);
762 hres = deserialize_param(tinfo, readit, debugout, alloc, &tdesc2, &(V_I4(vt)), buf);
763 MESSAGE(")");
764 return hres;
765 } else {
766 VariantInit(vt);
767 return S_OK;
770 case VT_ERROR:
771 case VT_BOOL: case VT_I4: case VT_UI4: case VT_UINT: case VT_R4:
772 case VT_UI2:
773 case VT_UI1:
774 if (readit) {
775 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
776 if (hres) FIXME("Failed to read integer 4 byte\n");
778 if (debugout) MESSAGE("%lx",*arg);
779 return hres;
780 case VT_BSTR: {
781 WCHAR *str;
782 DWORD len;
784 if (readit) {
785 hres = xbuf_get(buf,(LPBYTE)&len,sizeof(DWORD));
786 if (hres) {
787 FIXME("failed to read bstr klen\n");
788 return hres;
790 if (len == -1) {
791 *arg = 0;
792 if (debugout) MESSAGE("<bstr NULL>");
793 } else {
794 str = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,len+sizeof(WCHAR));
795 hres = xbuf_get(buf,(LPBYTE)str,len);
796 if (hres) {
797 FIXME("Failed to read BSTR.\n");
798 return hres;
800 *arg = (DWORD)SysAllocStringLen(str,len);
801 if (debugout) MESSAGE("%s",debugstr_w(str));
802 HeapFree(GetProcessHeap(),0,str);
804 } else {
805 *arg = 0;
807 return S_OK;
809 case VT_PTR: {
810 DWORD cookie;
811 BOOL derefhere = 0;
813 derefhere = (tdesc->u.lptdesc->vt != VT_USERDEFINED);
815 if (readit) {
816 hres = xbuf_get(buf,(LPBYTE)&cookie,sizeof(cookie));
817 if (hres) {
818 FIXME("Failed to load pointer cookie.\n");
819 return hres;
821 if (cookie != 0x42424242) {
822 if (debugout) MESSAGE("NULL");
823 *arg = 0;
824 return S_OK;
826 if (debugout) MESSAGE("*");
828 if (alloc) {
829 if (derefhere)
830 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,_xsize(tdesc->u.lptdesc));
832 if (derefhere)
833 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, (LPDWORD)*arg, buf);
834 else
835 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, arg, buf);
837 case VT_UNKNOWN:
838 /* FIXME: UNKNOWN is unknown ..., but allocate 4 byte for it */
839 if (alloc)
840 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
841 hres = S_OK;
842 if (readit)
843 hres = _unmarshal_interface(buf,&IID_IUnknown,(LPUNKNOWN*)arg);
844 if (debugout)
845 MESSAGE("unk(%p)",arg);
846 return hres;
847 case VT_DISPATCH:
848 hres = S_OK;
849 if (readit)
850 hres = _unmarshal_interface(buf,&IID_IDispatch,(LPUNKNOWN*)arg);
851 if (debugout)
852 MESSAGE("idisp(%p)",arg);
853 return hres;
854 case VT_VOID:
855 if (debugout) MESSAGE("<void>");
856 return S_OK;
857 case VT_USERDEFINED: {
858 ITypeInfo *tinfo2;
859 TYPEATTR *tattr;
861 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
862 if (hres) {
863 FIXME("Could not get typeinfo of hreftype %lx for VT_USERDEFINED.\n",tdesc->u.hreftype);
864 return hres;
866 hres = ITypeInfo_GetTypeAttr(tinfo2,&tattr);
867 if (hres) {
868 FIXME("Could not get typeattr in VT_USERDEFINED.\n");
869 } else {
870 if (alloc)
871 *arg = (DWORD)HeapAlloc(GetProcessHeap(),0,tattr->cbSizeInstance);
872 switch (tattr->typekind) {
873 case TKIND_DISPATCH:
874 case TKIND_INTERFACE:
875 if (readit)
876 hres = _unmarshal_interface(buf,&(tattr->guid),(LPUNKNOWN*)arg);
877 break;
878 case TKIND_RECORD: {
879 int i;
881 if (debugout) MESSAGE("{");
882 for (i=0;i<tattr->cVars;i++) {
883 VARDESC *vdesc;
885 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
886 if (hres) {
887 FIXME("Could not get vardesc of %d\n",i);
888 return hres;
890 hres = deserialize_param(
891 tinfo2,
892 readit,
893 debugout,
894 alloc,
895 &vdesc->elemdescVar.tdesc,
896 (DWORD*)(((LPBYTE)*arg)+vdesc->u.oInst),
899 if (debugout && (i<tattr->cVars-1)) MESSAGE(",");
901 if (buf->thisisiid && (tattr->cbSizeInstance==sizeof(GUID)))
902 memcpy(&(buf->iid),(LPBYTE)*arg,sizeof(buf->iid));
903 if (debugout) MESSAGE("}");
904 break;
906 default:
907 ERR("Unhandled typekind %d\n",tattr->typekind);
908 hres = E_FAIL;
909 break;
912 if (hres)
913 FIXME("failed to stuballoc in TKIND_RECORD.\n");
914 ITypeInfo_Release(tinfo2);
915 return hres;
917 case VT_CARRAY: {
918 /* arg is pointing to the start of the array. */
919 ARRAYDESC *adesc = tdesc->u.lpadesc;
920 int arrsize,i;
921 arrsize = 1;
922 if (adesc->cDims > 1) FIXME("cDims > 1 in VT_CARRAY. Does it work?\n");
923 for (i=0;i<adesc->cDims;i++)
924 arrsize *= adesc->rgbounds[i].cElements;
925 for (i=0;i<arrsize;i++)
926 deserialize_param(
927 tinfo,
928 readit,
929 debugout,
930 alloc,
931 &adesc->tdescElem,
932 (DWORD*)((LPBYTE)(arg)+i*_xsize(&adesc->tdescElem)),
935 return S_OK;
937 default:
938 ERR("No handler for VT type %d!\n",tdesc->vt);
939 return S_OK;
944 static HRESULT
945 deserialize_LPVOID_ptr(
946 ITypeInfo *tinfo,
947 BOOL readit,
948 BOOL debugout,
949 BOOL alloc,
950 TYPEDESC *tdesc,
951 DWORD *arg,
952 marshal_state *buf
954 HRESULT hres;
955 DWORD cookie;
957 if ((tdesc->vt != VT_PTR) ||
958 (tdesc->u.lptdesc->vt != VT_PTR) ||
959 (tdesc->u.lptdesc->u.lptdesc->vt != VT_VOID)
961 FIXME("ppvObject not expressed as VT_PTR -> VT_PTR -> VT_VOID?\n");
962 return E_FAIL;
964 if (alloc)
965 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(LPVOID));
966 if (readit) {
967 hres = xbuf_get(buf, (LPVOID)&cookie, sizeof(cookie));
968 if (hres)
969 return hres;
970 if (cookie != 0x42424242) {
971 *(DWORD*)*arg = 0;
972 if (debugout) MESSAGE("<lpvoid NULL>");
973 return S_OK;
976 if (readit) {
977 hres = _unmarshal_interface(buf,&buf->iid,(LPUNKNOWN*)*arg);
978 if (hres)
979 return hres;
981 if (debugout) MESSAGE("ppv(%p)",(LPVOID)*arg);
982 return S_OK;
985 static HRESULT
986 deserialize_DISPPARAM_ptr(
987 ITypeInfo *tinfo,
988 BOOL readit,
989 BOOL debugout,
990 BOOL alloc,
991 TYPEDESC *tdesc,
992 DWORD *arg,
993 marshal_state *buf
995 DWORD cookie;
996 DISPPARAMS *disps;
997 HRESULT hres;
998 int i;
1000 if ((tdesc->vt != VT_PTR) || (tdesc->u.lptdesc->vt != VT_USERDEFINED)) {
1001 FIXME("DISPPARAMS not expressed as VT_PTR -> VT_USERDEFINED?\n");
1002 return E_FAIL;
1004 if (readit) {
1005 hres = xbuf_get(buf,(LPBYTE)&cookie,sizeof(cookie));
1006 if (hres)
1007 return hres;
1008 if (cookie == 0) {
1009 *arg = 0;
1010 if (debugout) MESSAGE("<DISPPARAMS NULL>");
1011 return S_OK;
1014 if (alloc)
1015 *arg = (DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DISPPARAMS));
1016 disps = (DISPPARAMS*)*arg;
1017 if (!readit)
1018 return S_OK;
1019 hres = xbuf_get(buf, (LPBYTE)&disps->cArgs, sizeof(disps->cArgs));
1020 if (hres)
1021 return hres;
1022 if (alloc)
1023 disps->rgvarg = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(VARIANT)*disps->cArgs);
1024 if (debugout) MESSAGE("D{");
1025 for (i=0; i< disps->cArgs; i++) {
1026 TYPEDESC vdesc;
1028 vdesc.vt = VT_VARIANT;
1029 hres = deserialize_param(
1030 tinfo,
1031 readit,
1032 debugout,
1033 alloc,
1034 &vdesc,
1035 (DWORD*)(disps->rgvarg+i),
1039 if (debugout) MESSAGE("}{");
1040 hres = xbuf_get(buf, (LPBYTE)&disps->cNamedArgs, sizeof(disps->cNamedArgs));
1041 if (hres)
1042 return hres;
1043 if (disps->cNamedArgs) {
1044 if (alloc)
1045 disps->rgdispidNamedArgs = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DISPID)*disps->cNamedArgs);
1046 for (i=0; i< disps->cNamedArgs; i++) {
1047 TYPEDESC vdesc;
1049 vdesc.vt = VT_UINT;
1050 hres = deserialize_param(
1051 tinfo,
1052 readit,
1053 debugout,
1054 alloc,
1055 &vdesc,
1056 (DWORD*)(disps->rgdispidNamedArgs+i),
1059 if (debugout && i<(disps->cNamedArgs-1)) MESSAGE(",");
1062 if (debugout) MESSAGE("}");
1063 return S_OK;
1066 /* Searches function, also in inherited interfaces */
1067 static HRESULT
1068 _get_funcdesc(
1069 ITypeInfo *tinfo, int iMethod, FUNCDESC **fdesc, BSTR *iname, BSTR *fname
1071 int i = 0, j = 0;
1072 HRESULT hres;
1074 if (fname) *fname = NULL;
1075 if (iname) *iname = NULL;
1077 while (1) {
1078 hres = ITypeInfo_GetFuncDesc(tinfo, i, fdesc);
1079 if (hres) {
1080 ITypeInfo *tinfo2;
1081 HREFTYPE href;
1082 TYPEATTR *attr;
1084 hres = ITypeInfo_GetTypeAttr(tinfo, &attr);
1085 if (hres) {
1086 FIXME("GetTypeAttr failed with %lx\n",hres);
1087 return hres;
1089 /* Not found, so look in inherited ifaces. */
1090 for (j=0;j<attr->cImplTypes;j++) {
1091 hres = ITypeInfo_GetRefTypeOfImplType(tinfo, j, &href);
1092 if (hres) {
1093 FIXME("Did not find a reftype for interface offset %d?\n",j);
1094 break;
1096 hres = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
1097 if (hres) {
1098 FIXME("Did not find a typeinfo for reftype %ld?\n",href);
1099 continue;
1101 hres = _get_funcdesc(tinfo2,iMethod,fdesc,iname,fname);
1102 ITypeInfo_Release(tinfo2);
1103 if (!hres) return S_OK;
1105 return E_FAIL;
1107 if (((*fdesc)->oVft/4) == iMethod) {
1108 if (fname)
1109 ITypeInfo_GetDocumentation(tinfo,(*fdesc)->memid,fname,NULL,NULL,NULL);
1110 if (iname)
1111 ITypeInfo_GetDocumentation(tinfo,-1,iname,NULL,NULL,NULL);
1112 return S_OK;
1114 i++;
1116 return E_FAIL;
1119 static DWORD
1120 xCall(LPVOID retptr, int method, TMProxyImpl *tpinfo /*, args */) {
1121 DWORD *args = ((DWORD*)&tpinfo)+1, *xargs;
1122 FUNCDESC *fdesc;
1123 HRESULT hres;
1124 int i, relaydeb = TRACE_ON(olerelay);
1125 marshal_state buf;
1126 RPCOLEMESSAGE msg;
1127 ULONG status;
1128 BSTR fname,iname;
1129 BSTR names[10];
1130 int nrofnames;
1132 hres = _get_funcdesc(tpinfo->tinfo,method,&fdesc,&iname,&fname);
1133 if (hres) {
1134 ERR("Did not find typeinfo/funcdesc entry for method %d!\n",method);
1135 return 0;
1138 /*dump_FUNCDESC(fdesc);*/
1139 if (relaydeb) {
1140 TRACE_(olerelay)("->");
1141 if (iname)
1142 MESSAGE("%s:",debugstr_w(iname));
1143 if (fname)
1144 MESSAGE("%s(%d)",debugstr_w(fname),method);
1145 else
1146 MESSAGE("%d",method);
1147 MESSAGE("(");
1148 if (iname) SysFreeString(iname);
1149 if (fname) SysFreeString(fname);
1151 /* Need them for hack below */
1152 memset(names,0,sizeof(names));
1153 if (ITypeInfo_GetNames(tpinfo->tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames))
1154 nrofnames = 0;
1155 if (nrofnames > sizeof(names)/sizeof(names[0]))
1156 ERR("Need more names!\n");
1158 memset(&buf,0,sizeof(buf));
1159 buf.iid = IID_IUnknown;
1160 if (method == 0) {
1161 xbuf_add(&buf,(LPBYTE)args[0],sizeof(IID));
1162 if (relaydeb) MESSAGE("riid=%s,[out]",debugstr_guid((REFIID)args[0]));
1163 } else {
1164 xargs = args;
1165 for (i=0;i<fdesc->cParams;i++) {
1166 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1167 BOOL isserialized = FALSE;
1168 if (relaydeb) {
1169 if (i) MESSAGE(",");
1170 if (i+1<nrofnames && names[i+1])
1171 MESSAGE("%s=",debugstr_w(names[i+1]));
1173 /* No need to marshal other data than FIN */
1174 if (!(elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN)) {
1175 xargs+=_argsize(elem->tdesc.vt);
1176 if (relaydeb) MESSAGE("[out]");
1177 continue;
1179 if (((i+1)<nrofnames) && !IsBadStringPtrW(names[i+1],1)) {
1180 /* If the parameter is 'riid', we use it as interface IID
1181 * for a later ppvObject serialization.
1183 buf.thisisiid = !lstrcmpW(names[i+1],riidW);
1185 /* DISPPARAMS* needs special serializer */
1186 if (!lstrcmpW(names[i+1],pdispparamsW)) {
1187 hres = serialize_DISPPARAM_ptr(
1188 tpinfo->tinfo,
1189 elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN,
1190 relaydeb,
1191 FALSE,
1192 &elem->tdesc,
1193 xargs,
1194 &buf
1196 isserialized = TRUE;
1198 if (!lstrcmpW(names[i+1],ppvObjectW)) {
1199 hres = serialize_LPVOID_ptr(
1200 tpinfo->tinfo,
1201 elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN,
1202 relaydeb,
1203 FALSE,
1204 &elem->tdesc,
1205 xargs,
1206 &buf
1208 if (hres == S_OK)
1209 isserialized = TRUE;
1212 if (!isserialized)
1213 hres = serialize_param(
1214 tpinfo->tinfo,
1215 elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN,
1216 relaydeb,
1217 FALSE,
1218 &elem->tdesc,
1219 xargs,
1220 &buf
1223 if (hres) {
1224 FIXME("Failed to serialize param, hres %lx\n",hres);
1225 break;
1227 xargs+=_argsize(elem->tdesc.vt);
1230 if (relaydeb) MESSAGE(")");
1231 memset(&msg,0,sizeof(msg));
1232 msg.cbBuffer = buf.curoff;
1233 msg.iMethod = method;
1234 hres = IRpcChannelBuffer_GetBuffer(tpinfo->chanbuf,&msg,&(tpinfo->iid));
1235 if (hres) {
1236 FIXME("RpcChannelBuffer GetBuffer failed, %lx\n",hres);
1237 return hres;
1239 memcpy(msg.Buffer,buf.base,buf.curoff);
1240 if (relaydeb) MESSAGE("\n");
1241 hres = IRpcChannelBuffer_SendReceive(tpinfo->chanbuf,&msg,&status);
1242 if (hres) {
1243 FIXME("RpcChannelBuffer SendReceive failed, %lx\n",hres);
1244 return hres;
1246 relaydeb = TRACE_ON(olerelay);
1247 if (relaydeb) MESSAGE(" = %08lx (",status);
1248 if (buf.base)
1249 buf.base = HeapReAlloc(GetProcessHeap(),0,buf.base,msg.cbBuffer);
1250 else
1251 buf.base = HeapAlloc(GetProcessHeap(),0,msg.cbBuffer);
1252 buf.size = msg.cbBuffer;
1253 memcpy(buf.base,msg.Buffer,buf.size);
1254 buf.curoff = 0;
1255 if (method == 0) {
1256 _unmarshal_interface(&buf,(REFIID)args[0],(LPUNKNOWN*)args[1]);
1257 if (relaydeb) MESSAGE("[in],%p",*((DWORD**)args[1]));
1258 } else {
1259 xargs = args;
1260 for (i=0;i<fdesc->cParams;i++) {
1261 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1262 BOOL isdeserialized = FALSE;
1264 if (relaydeb) {
1265 if (i) MESSAGE(",");
1266 if (i+1<nrofnames && names[i+1]) MESSAGE("%s=",debugstr_w(names[i+1]));
1268 /* No need to marshal other data than FOUT I think */
1269 if (!(elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT)) {
1270 xargs += _argsize(elem->tdesc.vt);
1271 if (relaydeb) MESSAGE("[in]");
1272 continue;
1274 if (((i+1)<nrofnames) && !IsBadStringPtrW(names[i+1],1)) {
1275 /* If the parameter is 'riid', we use it as interface IID
1276 * for a later ppvObject serialization.
1278 buf.thisisiid = !lstrcmpW(names[i+1],riidW);
1280 /* deserialize DISPPARAM */
1281 if (!lstrcmpW(names[i+1],pdispparamsW)) {
1282 hres = deserialize_DISPPARAM_ptr(
1283 tpinfo->tinfo,
1284 elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT,
1285 relaydeb,
1286 FALSE,
1287 &(elem->tdesc),
1288 xargs,
1289 &buf
1291 if (hres) {
1292 FIXME("Failed to deserialize DISPPARAM*, hres %lx\n",hres);
1293 break;
1295 isdeserialized = TRUE;
1297 if (!lstrcmpW(names[i+1],ppvObjectW)) {
1298 hres = deserialize_LPVOID_ptr(
1299 tpinfo->tinfo,
1300 elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT,
1301 relaydeb,
1302 FALSE,
1303 &elem->tdesc,
1304 xargs,
1305 &buf
1307 if (hres == S_OK)
1308 isdeserialized = TRUE;
1311 if (!isdeserialized)
1312 hres = deserialize_param(
1313 tpinfo->tinfo,
1314 elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT,
1315 relaydeb,
1316 FALSE,
1317 &(elem->tdesc),
1318 xargs,
1319 &buf
1321 if (hres) {
1322 FIXME("Failed to unmarshall param, hres %lx\n",hres);
1323 break;
1325 xargs += _argsize(elem->tdesc.vt);
1328 if (relaydeb) MESSAGE(")\n\n");
1329 HeapFree(GetProcessHeap(),0,buf.base);
1330 return status;
1333 static HRESULT WINAPI
1334 PSFacBuf_CreateProxy(
1335 LPPSFACTORYBUFFER iface, IUnknown* pUnkOuter, REFIID riid,
1336 IRpcProxyBuffer **ppProxy, LPVOID *ppv
1338 HRESULT hres;
1339 ITypeInfo *tinfo;
1340 int i, nroffuncs;
1341 FUNCDESC *fdesc;
1342 TMProxyImpl *proxy;
1344 TRACE("(...%s...)\n",debugstr_guid(riid));
1345 hres = _get_typeinfo_for_iid(riid,&tinfo);
1346 if (hres) {
1347 FIXME("No typeinfo for %s?\n",debugstr_guid(riid));
1348 return hres;
1350 nroffuncs = _nroffuncs(tinfo);
1351 proxy = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(TMProxyImpl));
1352 if (!proxy) return E_OUTOFMEMORY;
1353 proxy->asmstubs=HeapAlloc(GetProcessHeap(),0,sizeof(TMAsmProxy)*nroffuncs);
1355 assert(sizeof(TMAsmProxy) == 12);
1357 proxy->lpvtbl = HeapAlloc(GetProcessHeap(),0,sizeof(LPBYTE)*nroffuncs);
1358 for (i=0;i<nroffuncs;i++) {
1359 int nrofargs;
1360 TMAsmProxy *xasm = proxy->asmstubs+i;
1362 /* nrofargs without This */
1363 switch (i) {
1364 case 0: nrofargs = 2;
1365 break;
1366 case 1: case 2: nrofargs = 0;
1367 break;
1368 default: {
1369 int j;
1370 hres = _get_funcdesc(tinfo,i,&fdesc,NULL,NULL);
1371 if (hres) {
1372 FIXME("GetFuncDesc %lx should not fail here.\n",hres);
1373 return hres;
1375 /* some args take more than 4 byte on the stack */
1376 nrofargs = 0;
1377 for (j=0;j<fdesc->cParams;j++)
1378 nrofargs += _argsize(fdesc->lprgelemdescParam[j].tdesc.vt);
1380 if (fdesc->callconv != CC_STDCALL) {
1381 ERR("calling convention is not stdcall????\n");
1382 return E_FAIL;
1384 break;
1387 /* popl %eax - return ptr
1388 * pushl <nr>
1389 * pushl %eax
1390 * call xCall
1391 * lret <nr> (+4)
1394 * arg3 arg2 arg1 <method> <returnptr>
1396 xasm->popleax = 0x58;
1397 xasm->pushlval = 0x6a;
1398 xasm->nr = i;
1399 xasm->pushleax = 0x50;
1400 xasm->lcall = 0xe8; /* relative jump */
1401 xasm->xcall = (DWORD)xCall;
1402 xasm->xcall -= (DWORD)&(xasm->lret);
1403 xasm->lret = 0xc2;
1404 xasm->bytestopop= (nrofargs+2)*4; /* pop args, This, iMethod */
1405 proxy->lpvtbl[i] = (DWORD)xasm;
1407 proxy->lpvtbl2 = &tmproxyvtable;
1408 proxy->ref = 2;
1409 proxy->tinfo = tinfo;
1410 memcpy(&proxy->iid,riid,sizeof(*riid));
1411 *ppv = (LPVOID)proxy;
1412 *ppProxy = (IRpcProxyBuffer *)&(proxy->lpvtbl2);
1413 return S_OK;
1416 typedef struct _TMStubImpl {
1417 ICOM_VTABLE(IRpcStubBuffer) *lpvtbl;
1418 DWORD ref;
1420 LPUNKNOWN pUnk;
1421 ITypeInfo *tinfo;
1422 IID iid;
1423 } TMStubImpl;
1425 static HRESULT WINAPI
1426 TMStubImpl_QueryInterface(LPRPCSTUBBUFFER iface, REFIID riid, LPVOID *ppv) {
1427 if (IsEqualIID(riid,&IID_IRpcStubBuffer)||IsEqualIID(riid,&IID_IUnknown)){
1428 *ppv = (LPVOID)iface;
1429 IRpcStubBuffer_AddRef(iface);
1430 return S_OK;
1432 FIXME("%s, not supported IID.\n",debugstr_guid(riid));
1433 return E_NOINTERFACE;
1436 static ULONG WINAPI
1437 TMStubImpl_AddRef(LPRPCSTUBBUFFER iface) {
1438 ICOM_THIS(TMStubImpl,iface);
1440 This->ref++;
1441 return This->ref;
1444 static ULONG WINAPI
1445 TMStubImpl_Release(LPRPCSTUBBUFFER iface) {
1446 ICOM_THIS(TMStubImpl,iface);
1448 This->ref--;
1449 if (This->ref)
1450 return This->ref;
1451 HeapFree(GetProcessHeap(),0,This);
1452 return 0;
1455 static HRESULT WINAPI
1456 TMStubImpl_Connect(LPRPCSTUBBUFFER iface, LPUNKNOWN pUnkServer) {
1457 ICOM_THIS(TMStubImpl,iface);
1459 IUnknown_AddRef(pUnkServer);
1460 This->pUnk = pUnkServer;
1461 return S_OK;
1464 static void WINAPI
1465 TMStubImpl_Disconnect(LPRPCSTUBBUFFER iface) {
1466 ICOM_THIS(TMStubImpl,iface);
1468 IUnknown_Release(This->pUnk);
1469 This->pUnk = NULL;
1470 return;
1473 static HRESULT WINAPI
1474 TMStubImpl_Invoke(
1475 LPRPCSTUBBUFFER iface, RPCOLEMESSAGE* xmsg,IRpcChannelBuffer*rpcchanbuf
1477 int i;
1478 FUNCDESC *fdesc;
1479 ICOM_THIS(TMStubImpl,iface);
1480 HRESULT hres;
1481 DWORD *args, res, *xargs, nrofargs;
1482 marshal_state buf;
1483 int nrofnames;
1484 BSTR names[10];
1486 memset(&buf,0,sizeof(buf));
1487 buf.size = xmsg->cbBuffer;
1488 buf.base = xmsg->Buffer;
1489 buf.curoff = 0;
1490 buf.iid = IID_IUnknown;
1492 TRACE("...\n");
1493 if (xmsg->iMethod == 0) { /* QI */
1494 IID xiid;
1495 /* in: IID, out: <iface> */
1497 xbuf_get(&buf,(LPBYTE)&xiid,sizeof(xiid));
1498 buf.curoff = 0;
1499 hres = _marshal_interface(&buf,&xiid,This->pUnk);
1500 xmsg->Buffer = buf.base; /* Might have been reallocated */
1501 xmsg->cbBuffer = buf.size;
1502 return hres;
1504 hres = _get_funcdesc(This->tinfo,xmsg->iMethod,&fdesc,NULL,NULL);
1505 if (hres) {
1506 FIXME("GetFuncDesc on method %ld failed with %lx\n",xmsg->iMethod,hres);
1507 return hres;
1509 /* Need them for hack below */
1510 memset(names,0,sizeof(names));
1511 ITypeInfo_GetNames(This->tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames);
1512 if (nrofnames > sizeof(names)/sizeof(names[0])) {
1513 ERR("Need more names!\n");
1516 /*dump_FUNCDESC(fdesc);*/
1517 nrofargs = 0;
1518 for (i=0;i<fdesc->cParams;i++)
1519 nrofargs += _argsize(fdesc->lprgelemdescParam[i].tdesc.vt);
1520 args = HeapAlloc(GetProcessHeap(),0,(nrofargs+1)*sizeof(DWORD));
1521 if (!args) return E_OUTOFMEMORY;
1523 /* Allocate all stuff used by call. */
1524 xargs = args+1;
1525 for (i=0;i<fdesc->cParams;i++) {
1526 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1527 BOOL isdeserialized = FALSE;
1529 if (((i+1)<nrofnames) && !IsBadStringPtrW(names[i+1],1)) {
1530 /* If the parameter is 'riid', we use it as interface IID
1531 * for a later ppvObject serialization.
1533 buf.thisisiid = !lstrcmpW(names[i+1],riidW);
1535 /* deserialize DISPPARAM */
1536 if (!lstrcmpW(names[i+1],pdispparamsW)) {
1537 hres = deserialize_DISPPARAM_ptr(
1538 This->tinfo,
1539 elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN,
1540 FALSE,
1541 TRUE,
1542 &(elem->tdesc),
1543 xargs,
1544 &buf
1546 if (hres) {
1547 FIXME("Failed to deserialize DISPPARAM*, hres %lx\n",hres);
1548 break;
1550 isdeserialized = TRUE;
1552 if (!lstrcmpW(names[i+1],ppvObjectW)) {
1553 hres = deserialize_LPVOID_ptr(
1554 This->tinfo,
1555 elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT,
1556 FALSE,
1557 TRUE,
1558 &elem->tdesc,
1559 xargs,
1560 &buf
1562 if (hres == S_OK)
1563 isdeserialized = TRUE;
1566 if (!isdeserialized)
1567 hres = deserialize_param(
1568 This->tinfo,
1569 elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN,
1570 FALSE,
1571 TRUE,
1572 &(elem->tdesc),
1573 xargs,
1574 &buf
1576 xargs += _argsize(elem->tdesc.vt);
1577 if (hres) {
1578 FIXME("Failed to deserialize param %s, hres %lx\n",debugstr_w(names[i+1]),hres);
1579 break;
1582 hres = IUnknown_QueryInterface(This->pUnk,&(This->iid),(LPVOID*)&(args[0]));
1583 if (hres) {
1584 ERR("Does not support iface %s\n",debugstr_guid(&(This->iid)));
1585 return hres;
1587 res = _invoke(
1588 (*((FARPROC**)args[0]))[fdesc->oVft/4],
1589 fdesc->callconv,
1590 (xargs-args),
1591 args
1593 IUnknown_Release((LPUNKNOWN)args[0]);
1594 buf.curoff = 0;
1595 xargs = args+1;
1596 for (i=0;i<fdesc->cParams;i++) {
1597 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
1598 BOOL isserialized = FALSE;
1600 if (((i+1)<nrofnames) && !IsBadStringPtrW(names[i+1],1)) {
1601 /* If the parameter is 'riid', we use it as interface IID
1602 * for a later ppvObject serialization.
1604 buf.thisisiid = !lstrcmpW(names[i+1],riidW);
1606 /* DISPPARAMS* needs special serializer */
1607 if (!lstrcmpW(names[i+1],pdispparamsW)) {
1608 hres = serialize_DISPPARAM_ptr(
1609 This->tinfo,
1610 elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT,
1611 FALSE,
1612 TRUE,
1613 &elem->tdesc,
1614 xargs,
1615 &buf
1617 isserialized = TRUE;
1619 if (!lstrcmpW(names[i+1],ppvObjectW)) {
1620 hres = serialize_LPVOID_ptr(
1621 This->tinfo,
1622 elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT,
1623 FALSE,
1624 TRUE,
1625 &elem->tdesc,
1626 xargs,
1627 &buf
1629 if (hres == S_OK)
1630 isserialized = TRUE;
1633 if (!isserialized)
1634 hres = serialize_param(
1635 This->tinfo,
1636 elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT,
1637 FALSE,
1638 TRUE,
1639 &elem->tdesc,
1640 xargs,
1641 &buf
1643 xargs += _argsize(elem->tdesc.vt);
1644 if (hres) {
1645 FIXME("Failed to stuballoc param, hres %lx\n",hres);
1646 break;
1649 /* might need to use IRpcChannelBuffer_GetBuffer ? */
1650 xmsg->cbBuffer = buf.curoff;
1651 xmsg->Buffer = buf.base;
1652 HeapFree(GetProcessHeap(),0,args);
1653 return res;
1656 static LPRPCSTUBBUFFER WINAPI
1657 TMStubImpl_IsIIDSupported(LPRPCSTUBBUFFER iface, REFIID riid) {
1658 FIXME("Huh (%s)?\n",debugstr_guid(riid));
1659 return NULL;
1662 static ULONG WINAPI
1663 TMStubImpl_CountRefs(LPRPCSTUBBUFFER iface) {
1664 ICOM_THIS(TMStubImpl,iface);
1666 return This->ref; /*FIXME? */
1669 static HRESULT WINAPI
1670 TMStubImpl_DebugServerQueryInterface(LPRPCSTUBBUFFER iface, LPVOID *ppv) {
1671 return E_NOTIMPL;
1674 static void WINAPI
1675 TMStubImpl_DebugServerRelease(LPRPCSTUBBUFFER iface, LPVOID ppv) {
1676 return;
1679 ICOM_VTABLE(IRpcStubBuffer) tmstubvtbl = {
1680 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1681 TMStubImpl_QueryInterface,
1682 TMStubImpl_AddRef,
1683 TMStubImpl_Release,
1684 TMStubImpl_Connect,
1685 TMStubImpl_Disconnect,
1686 TMStubImpl_Invoke,
1687 TMStubImpl_IsIIDSupported,
1688 TMStubImpl_CountRefs,
1689 TMStubImpl_DebugServerQueryInterface,
1690 TMStubImpl_DebugServerRelease
1693 static HRESULT WINAPI
1694 PSFacBuf_CreateStub(
1695 LPPSFACTORYBUFFER iface, REFIID riid,IUnknown *pUnkServer,
1696 IRpcStubBuffer** ppStub
1698 HRESULT hres;
1699 ITypeInfo *tinfo;
1700 TMStubImpl *stub;
1702 TRACE("(%s,%p,%p)\n",debugstr_guid(riid),pUnkServer,ppStub);
1703 hres = _get_typeinfo_for_iid(riid,&tinfo);
1704 if (hres) {
1705 FIXME("No typeinfo for %s?\n",debugstr_guid(riid));
1706 return hres;
1708 stub = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(TMStubImpl));
1709 if (!stub)
1710 return E_OUTOFMEMORY;
1711 stub->lpvtbl = &tmstubvtbl;
1712 stub->ref = 1;
1713 stub->tinfo = tinfo;
1714 memcpy(&(stub->iid),riid,sizeof(*riid));
1715 hres = IRpcStubBuffer_Connect((LPRPCSTUBBUFFER)stub,pUnkServer);
1716 *ppStub = (LPRPCSTUBBUFFER)stub;
1717 if (hres)
1718 FIXME("Connect to pUnkServer failed?\n");
1719 return hres;
1722 static ICOM_VTABLE(IPSFactoryBuffer) psfacbufvtbl = {
1723 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1724 PSFacBuf_QueryInterface,
1725 PSFacBuf_AddRef,
1726 PSFacBuf_Release,
1727 PSFacBuf_CreateProxy,
1728 PSFacBuf_CreateStub
1731 /* This is the whole PSFactoryBuffer object, just the vtableptr */
1732 static ICOM_VTABLE(IPSFactoryBuffer) *lppsfac = &psfacbufvtbl;
1734 /***********************************************************************
1735 * DllGetClassObject [OLE32.63]
1737 HRESULT WINAPI
1738 TypeLibFac_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
1740 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)) {
1741 *ppv = &lppsfac;
1742 return S_OK;
1744 return E_NOINTERFACE;