blob: 4a5ad89649c073031540e20211dfcab0a3dd060c [file] [log] [blame]
Marcus Meissner0749fc22002-02-05 18:11:17 +00001/*
2 * TYPELIB Marshaler
3 *
Marcus Meissner41079862005-03-29 13:06:44 +00004 * Copyright 2002,2005 Marcus Meissner
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00005 *
Mike Hearnd440ca42004-06-17 23:03:26 +00006 * The olerelay debug channel allows you to see calls marshalled by
7 * the typelib marshaller. It is not a generic COM relaying system.
8 *
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00009 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
Jonathan Ernst360a3f92006-05-18 14:49:52 +020021 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Marcus Meissner0749fc22002-02-05 18:11:17 +000022 */
23
24#include "config.h"
25
26#include <assert.h>
27#include <stdlib.h>
28#include <string.h>
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000029#include <stdarg.h>
Marcus Meissner0749fc22002-02-05 18:11:17 +000030#include <stdio.h>
31#include <ctype.h>
32
Francois Gouget486d0202004-10-07 03:06:48 +000033#define COBJMACROS
Dimitrie O. Paun297f3d82003-01-07 20:36:20 +000034#define NONAMELESSUNION
35#define NONAMELESSSTRUCT
Francois Gouget486d0202004-10-07 03:06:48 +000036
Marcus Meissner0749fc22002-02-05 18:11:17 +000037#include "winerror.h"
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000038#include "windef.h"
Alexandre Julliard864ca062003-08-20 18:22:31 +000039#include "winbase.h"
Marcus Meissner0749fc22002-02-05 18:11:17 +000040#include "winnls.h"
41#include "winreg.h"
42#include "winuser.h"
43
44#include "ole2.h"
Huw Davies86d94572006-05-10 12:11:22 +010045#include "propidl.h" /* for LPSAFEARRAY_User* functions */
Marcus Meissner0749fc22002-02-05 18:11:17 +000046#include "typelib.h"
Robert Shearman799ebfc2006-01-27 12:54:03 +010047#include "variant.h"
Alexandre Julliard0799c1a2002-03-09 23:29:33 +000048#include "wine/debug.h"
Robert Shearmana05672e2006-07-17 20:19:27 +010049#include "wine/exception.h"
Marcus Meissner0749fc22002-02-05 18:11:17 +000050
Marcus Meissnere6260a32005-03-27 17:51:59 +000051static const WCHAR IDispatchW[] = { 'I','D','i','s','p','a','t','c','h',0};
Marcus Meissner0749fc22002-02-05 18:11:17 +000052
Alexandre Julliard0799c1a2002-03-09 23:29:33 +000053WINE_DEFAULT_DEBUG_CHANNEL(ole);
54WINE_DECLARE_DEBUG_CHANNEL(olerelay);
Marcus Meissner0749fc22002-02-05 18:11:17 +000055
Alexandre Julliard936198c2004-08-13 00:44:22 +000056#define ICOM_THIS_MULTI(impl,field,iface) impl* const This=(impl*)((char*)(iface) - offsetof(impl,field))
57
Rob Shearman30f2b442007-02-06 19:19:56 +000058static HRESULT TMarshalDispatchChannel_Create(
59 IRpcChannelBuffer *pDelegateChannel, REFIID tmarshal_riid,
60 IRpcChannelBuffer **ppChannel);
61
Marcus Meissner0749fc22002-02-05 18:11:17 +000062typedef struct _marshal_state {
63 LPBYTE base;
64 int size;
65 int curoff;
Marcus Meissner0749fc22002-02-05 18:11:17 +000066} marshal_state;
67
Mike Hearnd440ca42004-06-17 23:03:26 +000068/* used in the olerelay code to avoid having the L"" stuff added by debugstr_w */
69static char *relaystr(WCHAR *in) {
70 char *tmp = (char *)debugstr_w(in);
71 tmp += 2;
72 tmp[strlen(tmp)-1] = '\0';
73 return tmp;
74}
75
Marcus Meissner0749fc22002-02-05 18:11:17 +000076static HRESULT
Huw Daviesd0bdf682006-05-10 11:55:37 +010077xbuf_resize(marshal_state *buf, DWORD newsize)
78{
79 if(buf->size >= newsize)
80 return S_FALSE;
81
82 if(buf->base)
83 {
84 buf->base = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buf->base, newsize);
85 if(!buf->base)
86 return E_OUTOFMEMORY;
87 }
88 else
89 {
90 buf->base = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, newsize);
91 if(!buf->base)
92 return E_OUTOFMEMORY;
93 }
Huw Davies3dce3232006-05-15 10:02:48 +010094 buf->size = newsize;
Huw Daviesd0bdf682006-05-10 11:55:37 +010095 return S_OK;
96}
97
98static HRESULT
Andrew Talbotf73821c2007-07-25 21:37:23 +010099xbuf_add(marshal_state *buf, const BYTE *stuff, DWORD size)
Huw Daviesd0bdf682006-05-10 11:55:37 +0100100{
101 HRESULT hr;
102
103 if(buf->size - buf->curoff < size)
104 {
105 hr = xbuf_resize(buf, buf->size + size + 100);
106 if(FAILED(hr)) return hr;
Marcus Meissner0749fc22002-02-05 18:11:17 +0000107 }
108 memcpy(buf->base+buf->curoff,stuff,size);
109 buf->curoff += size;
110 return S_OK;
111}
112
113static HRESULT
114xbuf_get(marshal_state *buf, LPBYTE stuff, DWORD size) {
115 if (buf->size < buf->curoff+size) return E_FAIL;
116 memcpy(stuff,buf->base+buf->curoff,size);
117 buf->curoff += size;
118 return S_OK;
119}
120
121static HRESULT
122xbuf_skip(marshal_state *buf, DWORD size) {
123 if (buf->size < buf->curoff+size) return E_FAIL;
124 buf->curoff += size;
125 return S_OK;
126}
127
128static HRESULT
129_unmarshal_interface(marshal_state *buf, REFIID riid, LPUNKNOWN *pUnk) {
130 IStream *pStm;
131 ULARGE_INTEGER newpos;
132 LARGE_INTEGER seekto;
133 ULONG res;
134 HRESULT hres;
135 DWORD xsize;
136
137 TRACE("...%s...\n",debugstr_guid(riid));
Mike Hearn64eac752005-01-27 11:11:32 +0000138
Marcus Meissner0749fc22002-02-05 18:11:17 +0000139 *pUnk = NULL;
140 hres = xbuf_get(buf,(LPBYTE)&xsize,sizeof(xsize));
Mike Hearn64eac752005-01-27 11:11:32 +0000141 if (hres) {
142 ERR("xbuf_get failed\n");
143 return hres;
144 }
145
Marcus Meissner0749fc22002-02-05 18:11:17 +0000146 if (xsize == 0) return S_OK;
Mike Hearn64eac752005-01-27 11:11:32 +0000147
Marcus Meissner0749fc22002-02-05 18:11:17 +0000148 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
149 if (hres) {
Hans Leidekkerce66f5d2006-10-12 20:57:10 +0200150 ERR("Stream create failed %x\n",hres);
Marcus Meissner0749fc22002-02-05 18:11:17 +0000151 return hres;
152 }
Mike Hearn64eac752005-01-27 11:11:32 +0000153
Marcus Meissner0749fc22002-02-05 18:11:17 +0000154 hres = IStream_Write(pStm,buf->base+buf->curoff,xsize,&res);
Mike Hearn64eac752005-01-27 11:11:32 +0000155 if (hres) {
Hans Leidekkerce66f5d2006-10-12 20:57:10 +0200156 ERR("stream write %x\n",hres);
Mike Hearn64eac752005-01-27 11:11:32 +0000157 return hres;
158 }
159
Marcus Meissner0749fc22002-02-05 18:11:17 +0000160 memset(&seekto,0,sizeof(seekto));
161 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
Mike Hearn64eac752005-01-27 11:11:32 +0000162 if (hres) {
Hans Leidekkerce66f5d2006-10-12 20:57:10 +0200163 ERR("Failed Seek %x\n",hres);
Mike Hearn64eac752005-01-27 11:11:32 +0000164 return hres;
165 }
166
Marcus Meissner0749fc22002-02-05 18:11:17 +0000167 hres = CoUnmarshalInterface(pStm,riid,(LPVOID*)pUnk);
168 if (hres) {
Hans Leidekkerce66f5d2006-10-12 20:57:10 +0200169 ERR("Unmarshalling interface %s failed with %x\n",debugstr_guid(riid),hres);
Marcus Meissner0749fc22002-02-05 18:11:17 +0000170 return hres;
171 }
Mike Hearn64eac752005-01-27 11:11:32 +0000172
Marcus Meissner0749fc22002-02-05 18:11:17 +0000173 IStream_Release(pStm);
174 return xbuf_skip(buf,xsize);
175}
176
177static HRESULT
178_marshal_interface(marshal_state *buf, REFIID riid, LPUNKNOWN pUnk) {
Mike Hearn64eac752005-01-27 11:11:32 +0000179 LPBYTE tempbuf = NULL;
180 IStream *pStm = NULL;
Marcus Meissner0749fc22002-02-05 18:11:17 +0000181 STATSTG ststg;
182 ULARGE_INTEGER newpos;
183 LARGE_INTEGER seekto;
184 ULONG res;
185 DWORD xsize;
186 HRESULT hres;
187
Mike Hearn64eac752005-01-27 11:11:32 +0000188 if (!pUnk) {
Marcus Meissner41079862005-03-29 13:06:44 +0000189 /* this is valid, if for instance we serialize
190 * a VT_DISPATCH with NULL ptr which apparently
191 * can happen. S_OK to make sure we continue
192 * serializing.
193 */
Robert Shearman89846942006-01-18 11:29:01 +0100194 WARN("pUnk is NULL\n");
Marcus Meissner41079862005-03-29 13:06:44 +0000195 xsize = 0;
196 return xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
Mike Hearn64eac752005-01-27 11:11:32 +0000197 }
Marcus Meissner0749fc22002-02-05 18:11:17 +0000198
Marcus Meissner41079862005-03-29 13:06:44 +0000199 hres = E_FAIL;
200
Marcus Meissner0749fc22002-02-05 18:11:17 +0000201 TRACE("...%s...\n",debugstr_guid(riid));
Mike Hearn64eac752005-01-27 11:11:32 +0000202
Marcus Meissner0749fc22002-02-05 18:11:17 +0000203 hres = CreateStreamOnHGlobal(0,TRUE,&pStm);
204 if (hres) {
Hans Leidekkerce66f5d2006-10-12 20:57:10 +0200205 ERR("Stream create failed %x\n",hres);
Marcus Meissner0749fc22002-02-05 18:11:17 +0000206 goto fail;
207 }
Mike Hearn64eac752005-01-27 11:11:32 +0000208
Robert Shearman4519b1b2005-08-24 09:44:41 +0000209 hres = CoMarshalInterface(pStm,riid,pUnk,0,NULL,0);
Marcus Meissner0749fc22002-02-05 18:11:17 +0000210 if (hres) {
Hans Leidekkerce66f5d2006-10-12 20:57:10 +0200211 ERR("Marshalling interface %s failed with %x\n", debugstr_guid(riid), hres);
Marcus Meissner0749fc22002-02-05 18:11:17 +0000212 goto fail;
213 }
Mike Hearn64eac752005-01-27 11:11:32 +0000214
Marcus Meissner0749fc22002-02-05 18:11:17 +0000215 hres = IStream_Stat(pStm,&ststg,0);
Mike Hearn64eac752005-01-27 11:11:32 +0000216 if (hres) {
217 ERR("Stream stat failed\n");
218 goto fail;
219 }
220
Ge van Geldorp399901e2004-01-23 01:51:33 +0000221 tempbuf = HeapAlloc(GetProcessHeap(), 0, ststg.cbSize.u.LowPart);
Marcus Meissner0749fc22002-02-05 18:11:17 +0000222 memset(&seekto,0,sizeof(seekto));
223 hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
Mike Hearn64eac752005-01-27 11:11:32 +0000224 if (hres) {
Hans Leidekkerce66f5d2006-10-12 20:57:10 +0200225 ERR("Failed Seek %x\n",hres);
Mike Hearn64eac752005-01-27 11:11:32 +0000226 goto fail;
227 }
228
Ge van Geldorp399901e2004-01-23 01:51:33 +0000229 hres = IStream_Read(pStm,tempbuf,ststg.cbSize.u.LowPart,&res);
Mike Hearn64eac752005-01-27 11:11:32 +0000230 if (hres) {
Hans Leidekkerce66f5d2006-10-12 20:57:10 +0200231 ERR("Failed Read %x\n",hres);
Mike Hearn64eac752005-01-27 11:11:32 +0000232 goto fail;
233 }
234
Ge van Geldorp399901e2004-01-23 01:51:33 +0000235 xsize = ststg.cbSize.u.LowPart;
Marcus Meissner0749fc22002-02-05 18:11:17 +0000236 xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
Ge van Geldorp399901e2004-01-23 01:51:33 +0000237 hres = xbuf_add(buf,tempbuf,ststg.cbSize.u.LowPart);
Mike Hearn64eac752005-01-27 11:11:32 +0000238
Marcus Meissner0749fc22002-02-05 18:11:17 +0000239 HeapFree(GetProcessHeap(),0,tempbuf);
Mike Hearn64eac752005-01-27 11:11:32 +0000240 IStream_Release(pStm);
241
Marcus Meissner0749fc22002-02-05 18:11:17 +0000242 return hres;
Mike Hearn64eac752005-01-27 11:11:32 +0000243
Marcus Meissner0749fc22002-02-05 18:11:17 +0000244fail:
245 xsize = 0;
246 xbuf_add(buf,(LPBYTE)&xsize,sizeof(xsize));
Mike Hearn64eac752005-01-27 11:11:32 +0000247 if (pStm) IUnknown_Release(pStm);
Mike Hearn64eac752005-01-27 11:11:32 +0000248 HeapFree(GetProcessHeap(), 0, tempbuf);
Marcus Meissner0749fc22002-02-05 18:11:17 +0000249 return hres;
250}
251
252/********************* OLE Proxy/Stub Factory ********************************/
253static HRESULT WINAPI
254PSFacBuf_QueryInterface(LPPSFACTORYBUFFER iface, REFIID iid, LPVOID *ppv) {
255 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)||IsEqualIID(iid,&IID_IUnknown)) {
Vincent Béron9a624912002-05-31 23:06:46 +0000256 *ppv = (LPVOID)iface;
Marcus Meissner0749fc22002-02-05 18:11:17 +0000257 /* No ref counting, static class */
258 return S_OK;
259 }
260 FIXME("(%s) unknown IID?\n",debugstr_guid(iid));
261 return E_NOINTERFACE;
262}
263
264static ULONG WINAPI PSFacBuf_AddRef(LPPSFACTORYBUFFER iface) { return 2; }
265static ULONG WINAPI PSFacBuf_Release(LPPSFACTORYBUFFER iface) { return 1; }
266
267static HRESULT
268_get_typeinfo_for_iid(REFIID riid, ITypeInfo**ti) {
269 HRESULT hres;
270 HKEY ikey;
271 char tlguid[200],typelibkey[300],interfacekey[300],ver[100];
272 char tlfn[260];
273 OLECHAR tlfnW[260];
Mike McCormackd7b9a232005-07-06 19:08:05 +0000274 DWORD tlguidlen, verlen, type;
275 LONG tlfnlen;
Marcus Meissner0749fc22002-02-05 18:11:17 +0000276 ITypeLib *tl;
277
Michael Stefaniuc15765dd2006-10-15 12:27:34 +0200278 sprintf( interfacekey, "Interface\\{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\Typelib",
Marcus Meissner0749fc22002-02-05 18:11:17 +0000279 riid->Data1, riid->Data2, riid->Data3,
280 riid->Data4[0], riid->Data4[1], riid->Data4[2], riid->Data4[3],
281 riid->Data4[4], riid->Data4[5], riid->Data4[6], riid->Data4[7]
282 );
283
284 if (RegOpenKeyA(HKEY_CLASSES_ROOT,interfacekey,&ikey)) {
Mike Hearn64eac752005-01-27 11:11:32 +0000285 ERR("No %s key found.\n",interfacekey);
Marcus Meissner0749fc22002-02-05 18:11:17 +0000286 return E_FAIL;
287 }
Marcus Meissner0749fc22002-02-05 18:11:17 +0000288 tlguidlen = sizeof(tlguid);
Mike McCormackd7b9a232005-07-06 19:08:05 +0000289 if (RegQueryValueExA(ikey,NULL,NULL,&type,(LPBYTE)tlguid,&tlguidlen)) {
Mike Hearn64eac752005-01-27 11:11:32 +0000290 ERR("Getting typelib guid failed.\n");
Marcus Meissner0749fc22002-02-05 18:11:17 +0000291 RegCloseKey(ikey);
292 return E_FAIL;
293 }
Marcus Meissner0749fc22002-02-05 18:11:17 +0000294 verlen = sizeof(ver);
Mike McCormackd7b9a232005-07-06 19:08:05 +0000295 if (RegQueryValueExA(ikey,"Version",NULL,&type,(LPBYTE)ver,&verlen)) {
Mike Hearn64eac752005-01-27 11:11:32 +0000296 ERR("Could not get version value?\n");
Marcus Meissner0749fc22002-02-05 18:11:17 +0000297 RegCloseKey(ikey);
298 return E_FAIL;
299 }
300 RegCloseKey(ikey);
301 sprintf(typelibkey,"Typelib\\%s\\%s\\0\\win32",tlguid,ver);
302 tlfnlen = sizeof(tlfn);
303 if (RegQueryValueA(HKEY_CLASSES_ROOT,typelibkey,tlfn,&tlfnlen)) {
Mike Hearn64eac752005-01-27 11:11:32 +0000304 ERR("Could not get typelib fn?\n");
Marcus Meissner0749fc22002-02-05 18:11:17 +0000305 return E_FAIL;
306 }
Thomas Weidenmueller05d62922006-05-12 00:07:59 +0200307 MultiByteToWideChar(CP_ACP, 0, tlfn, -1, tlfnW, sizeof(tlfnW) / sizeof(tlfnW[0]));
Marcus Meissner0749fc22002-02-05 18:11:17 +0000308 hres = LoadTypeLib(tlfnW,&tl);
309 if (hres) {
310 ERR("Failed to load typelib for %s, but it should be there.\n",debugstr_guid(riid));
311 return hres;
312 }
313 hres = ITypeLib_GetTypeInfoOfGuid(tl,riid,ti);
314 if (hres) {
315 ERR("typelib does not contain info for %s?\n",debugstr_guid(riid));
316 ITypeLib_Release(tl);
317 return hres;
318 }
Rob Shearmand1b15c82006-10-23 11:38:20 +0100319 ITypeLib_Release(tl);
Marcus Meissner0749fc22002-02-05 18:11:17 +0000320 return hres;
321}
322
Huw Daviesc24a53d2007-05-17 15:29:41 +0100323/*
324 * Determine the number of functions including all inherited functions.
325 * Note for non-dual dispinterfaces we simply return the size of IDispatch.
Marcus Meissner0749fc22002-02-05 18:11:17 +0000326 */
Huw Daviesc24a53d2007-05-17 15:29:41 +0100327static HRESULT num_of_funcs(ITypeInfo *tinfo, unsigned int *num)
328{
329 HRESULT hres;
Rob Shearmanecfe54d2007-01-02 15:39:48 +0000330 TYPEATTR *attr;
331 ITypeInfo *tinfo2;
Marcus Meissner0749fc22002-02-05 18:11:17 +0000332
Huw Daviesc24a53d2007-05-17 15:29:41 +0100333 *num = 0;
Rob Shearmanecfe54d2007-01-02 15:39:48 +0000334 hres = ITypeInfo_GetTypeAttr(tinfo, &attr);
335 if (hres) {
336 ERR("GetTypeAttr failed with %x\n",hres);
337 return hres;
338 }
Huw Daviesc24a53d2007-05-17 15:29:41 +0100339
340 if(attr->typekind == TKIND_DISPATCH && (attr->wTypeFlags & TYPEFLAG_FDUAL))
341 {
Rob Shearmanecfe54d2007-01-02 15:39:48 +0000342 HREFTYPE href;
Huw Daviesc24a53d2007-05-17 15:29:41 +0100343 hres = ITypeInfo_GetRefTypeOfImplType(tinfo, -1, &href);
344 if(FAILED(hres))
345 {
346 ERR("Unable to get interface href from dual dispinterface\n");
347 goto end;
Rob Shearmanecfe54d2007-01-02 15:39:48 +0000348 }
349 hres = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
Huw Daviesc24a53d2007-05-17 15:29:41 +0100350 if(FAILED(hres))
351 {
352 ERR("Unable to get interface from dual dispinterface\n");
353 goto end;
Rob Shearmanecfe54d2007-01-02 15:39:48 +0000354 }
Huw Daviesc24a53d2007-05-17 15:29:41 +0100355 hres = num_of_funcs(tinfo2, num);
Rob Shearmanecfe54d2007-01-02 15:39:48 +0000356 ITypeInfo_Release(tinfo2);
357 }
Huw Daviesc24a53d2007-05-17 15:29:41 +0100358 else
359 {
360 *num = attr->cbSizeVft / 4;
Marcus Meissner0749fc22002-02-05 18:11:17 +0000361 }
Huw Daviesc24a53d2007-05-17 15:29:41 +0100362
363 end:
364 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
365 return hres;
Marcus Meissner0749fc22002-02-05 18:11:17 +0000366}
367
Robert Shearman2a54aac2004-09-07 20:42:26 +0000368#ifdef __i386__
369
Alexandre Julliarde8283082004-01-09 22:18:49 +0000370#include "pshpack1.h"
371
Marcus Meissner0749fc22002-02-05 18:11:17 +0000372typedef struct _TMAsmProxy {
373 BYTE popleax;
374 BYTE pushlval;
375 BYTE nr;
376 BYTE pushleax;
377 BYTE lcall;
378 DWORD xcall;
379 BYTE lret;
380 WORD bytestopop;
Alexandre Julliarde8283082004-01-09 22:18:49 +0000381} TMAsmProxy;
382
383#include "poppack.h"
Marcus Meissner0749fc22002-02-05 18:11:17 +0000384
Robert Shearman2a54aac2004-09-07 20:42:26 +0000385#else /* __i386__ */
Pierre d'Herbemontf26e8092005-10-19 19:06:49 +0000386# warning You need to implement stubless proxies for your architecture
387typedef struct _TMAsmProxy {
388} TMAsmProxy;
Robert Shearman2a54aac2004-09-07 20:42:26 +0000389#endif
390
Marcus Meissner0749fc22002-02-05 18:11:17 +0000391typedef struct _TMProxyImpl {
Dmitry Timoshkoveba47f12005-06-06 19:50:35 +0000392 LPVOID *lpvtbl;
393 const IRpcProxyBufferVtbl *lpvtbl2;
Mike McCormackfc200d02005-07-11 10:25:19 +0000394 LONG ref;
Marcus Meissner0749fc22002-02-05 18:11:17 +0000395
396 TMAsmProxy *asmstubs;
397 ITypeInfo* tinfo;
398 IRpcChannelBuffer* chanbuf;
399 IID iid;
Robert Shearman2a54aac2004-09-07 20:42:26 +0000400 CRITICAL_SECTION crit;
Robert Shearmanac23f2c2005-03-11 10:18:31 +0000401 IUnknown *outerunknown;
Robert Shearman5c2a7082005-10-26 10:12:30 +0000402 IDispatch *dispatch;
Robert Shearmanb0218db2006-02-07 16:26:23 +0100403 IRpcProxyBuffer *dispatch_proxy;
Marcus Meissner0749fc22002-02-05 18:11:17 +0000404} TMProxyImpl;
405
406static HRESULT WINAPI
Robert Shearman2a54aac2004-09-07 20:42:26 +0000407TMProxyImpl_QueryInterface(LPRPCPROXYBUFFER iface, REFIID riid, LPVOID *ppv)
408{
Marcus Meissner0749fc22002-02-05 18:11:17 +0000409 TRACE("()\n");
410 if (IsEqualIID(riid,&IID_IUnknown)||IsEqualIID(riid,&IID_IRpcProxyBuffer)) {
Robert Shearman2a54aac2004-09-07 20:42:26 +0000411 *ppv = (LPVOID)iface;
412 IRpcProxyBuffer_AddRef(iface);
413 return S_OK;
Marcus Meissner0749fc22002-02-05 18:11:17 +0000414 }
415 FIXME("no interface for %s\n",debugstr_guid(riid));
416 return E_NOINTERFACE;
417}
418
419static ULONG WINAPI
Robert Shearman2a54aac2004-09-07 20:42:26 +0000420TMProxyImpl_AddRef(LPRPCPROXYBUFFER iface)
421{
Marcus Meissner0749fc22002-02-05 18:11:17 +0000422 ICOM_THIS_MULTI(TMProxyImpl,lpvtbl2,iface);
James Hawkinsba92d292005-01-14 16:02:20 +0000423 ULONG refCount = InterlockedIncrement(&This->ref);
Marcus Meissner0749fc22002-02-05 18:11:17 +0000424
Hans Leidekkerce66f5d2006-10-12 20:57:10 +0200425 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
Robert Shearman2a54aac2004-09-07 20:42:26 +0000426
James Hawkinsba92d292005-01-14 16:02:20 +0000427 return refCount;
Marcus Meissner0749fc22002-02-05 18:11:17 +0000428}
429
430static ULONG WINAPI
Robert Shearman2a54aac2004-09-07 20:42:26 +0000431TMProxyImpl_Release(LPRPCPROXYBUFFER iface)
432{
Marcus Meissner0749fc22002-02-05 18:11:17 +0000433 ICOM_THIS_MULTI(TMProxyImpl,lpvtbl2,iface);
James Hawkinsba92d292005-01-14 16:02:20 +0000434 ULONG refCount = InterlockedDecrement(&This->ref);
Marcus Meissner0749fc22002-02-05 18:11:17 +0000435
Hans Leidekkerce66f5d2006-10-12 20:57:10 +0200436 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
Robert Shearman2a54aac2004-09-07 20:42:26 +0000437
James Hawkinsba92d292005-01-14 16:02:20 +0000438 if (!refCount)
Robert Shearman2a54aac2004-09-07 20:42:26 +0000439 {
Robert Shearmanb0218db2006-02-07 16:26:23 +0100440 if (This->dispatch_proxy) IRpcProxyBuffer_Release(This->dispatch_proxy);
Jan Zerebecki1e176b42007-03-10 22:09:58 +0100441 This->crit.DebugInfo->Spare[0] = 0;
Robert Shearman2a54aac2004-09-07 20:42:26 +0000442 DeleteCriticalSection(&This->crit);
443 if (This->chanbuf) IRpcChannelBuffer_Release(This->chanbuf);
444 VirtualFree(This->asmstubs, 0, MEM_RELEASE);
Robert Shearmancc21d232006-09-27 15:51:26 +0100445 HeapFree(GetProcessHeap(), 0, This->lpvtbl);
Rob Shearmand1b15c82006-10-23 11:38:20 +0100446 ITypeInfo_Release(This->tinfo);
Robert Shearman2a54aac2004-09-07 20:42:26 +0000447 CoTaskMemFree(This);
448 }
James Hawkinsba92d292005-01-14 16:02:20 +0000449 return refCount;
Marcus Meissner0749fc22002-02-05 18:11:17 +0000450}
451
452static HRESULT WINAPI
453TMProxyImpl_Connect(
Robert Shearman2a54aac2004-09-07 20:42:26 +0000454 LPRPCPROXYBUFFER iface,IRpcChannelBuffer* pRpcChannelBuffer)
455{
456 ICOM_THIS_MULTI(TMProxyImpl, lpvtbl2, iface);
Marcus Meissner0749fc22002-02-05 18:11:17 +0000457
Robert Shearman2a54aac2004-09-07 20:42:26 +0000458 TRACE("(%p)\n", pRpcChannelBuffer);
459
460 EnterCriticalSection(&This->crit);
461
462 IRpcChannelBuffer_AddRef(pRpcChannelBuffer);
Marcus Meissner0749fc22002-02-05 18:11:17 +0000463 This->chanbuf = pRpcChannelBuffer;
Robert Shearman2a54aac2004-09-07 20:42:26 +0000464
465 LeaveCriticalSection(&This->crit);
466
Robert Shearmanb0218db2006-02-07 16:26:23 +0100467 if (This->dispatch_proxy)
Rob Shearman30f2b442007-02-06 19:19:56 +0000468 {
469 IRpcChannelBuffer *pDelegateChannel;
470 HRESULT hr = TMarshalDispatchChannel_Create(pRpcChannelBuffer, &This->iid, &pDelegateChannel);
471 if (FAILED(hr))
472 return hr;
473 return IRpcProxyBuffer_Connect(This->dispatch_proxy, pDelegateChannel);
474 }
Robert Shearmanb0218db2006-02-07 16:26:23 +0100475
Marcus Meissner0749fc22002-02-05 18:11:17 +0000476 return S_OK;
477}
478
479static void WINAPI
Robert Shearman2a54aac2004-09-07 20:42:26 +0000480TMProxyImpl_Disconnect(LPRPCPROXYBUFFER iface)
481{
482 ICOM_THIS_MULTI(TMProxyImpl, lpvtbl2, iface);
Marcus Meissner0749fc22002-02-05 18:11:17 +0000483
Robert Shearman2a54aac2004-09-07 20:42:26 +0000484 TRACE("()\n");
485
486 EnterCriticalSection(&This->crit);
487
Marcus Meissner0749fc22002-02-05 18:11:17 +0000488 IRpcChannelBuffer_Release(This->chanbuf);
489 This->chanbuf = NULL;
Robert Shearman2a54aac2004-09-07 20:42:26 +0000490
491 LeaveCriticalSection(&This->crit);
Robert Shearmanb0218db2006-02-07 16:26:23 +0100492
493 if (This->dispatch_proxy)
494 IRpcProxyBuffer_Disconnect(This->dispatch_proxy);
Marcus Meissner0749fc22002-02-05 18:11:17 +0000495}
496
497
Dmitry Timoshkoveba47f12005-06-06 19:50:35 +0000498static const IRpcProxyBufferVtbl tmproxyvtable = {
Marcus Meissner0749fc22002-02-05 18:11:17 +0000499 TMProxyImpl_QueryInterface,
500 TMProxyImpl_AddRef,
501 TMProxyImpl_Release,
502 TMProxyImpl_Connect,
503 TMProxyImpl_Disconnect
504};
505
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000506/* how much space do we use on stack in DWORD steps. */
Marcus Meissner5539e282004-09-27 20:33:27 +0000507int
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000508_argsize(DWORD vt) {
509 switch (vt) {
Marcus Meissner2045ed22005-11-04 11:13:55 +0000510 case VT_UI8:
511 return 8/sizeof(DWORD);
Robert Shearman1329f872005-06-09 09:43:38 +0000512 case VT_R8:
513 return sizeof(double)/sizeof(DWORD);
514 case VT_CY:
515 return sizeof(CY)/sizeof(DWORD);
Marcus Meissnerc18a6aa2002-06-14 00:36:42 +0000516 case VT_DATE:
517 return sizeof(DATE)/sizeof(DWORD);
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000518 case VT_VARIANT:
519 return (sizeof(VARIANT)+3)/sizeof(DWORD);
520 default:
521 return 1;
522 }
523}
524
525static int
Andrew Talbotf73821c2007-07-25 21:37:23 +0100526_xsize(const TYPEDESC *td) {
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000527 switch (td->vt) {
Marcus Meissnerc18a6aa2002-06-14 00:36:42 +0000528 case VT_DATE:
529 return sizeof(DATE);
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000530 case VT_VARIANT:
531 return sizeof(VARIANT)+3;
532 case VT_CARRAY: {
533 int i, arrsize = 1;
Andrew Talbotf73821c2007-07-25 21:37:23 +0100534 const ARRAYDESC *adesc = td->u.lpadesc;
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000535
536 for (i=0;i<adesc->cDims;i++)
537 arrsize *= adesc->rgbounds[i].cElements;
538 return arrsize*_xsize(&adesc->tdescElem);
539 }
Marcus Meissner2045ed22005-11-04 11:13:55 +0000540 case VT_UI8:
541 case VT_I8:
542 return 8;
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000543 case VT_UI2:
544 case VT_I2:
545 return 2;
546 case VT_UI1:
547 case VT_I1:
548 return 1;
549 default:
550 return 4;
551 }
552}
553
Marcus Meissner0749fc22002-02-05 18:11:17 +0000554static HRESULT
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000555serialize_param(
556 ITypeInfo *tinfo,
557 BOOL writeit,
558 BOOL debugout,
559 BOOL dealloc,
560 TYPEDESC *tdesc,
561 DWORD *arg,
Robert Shearman2a54aac2004-09-07 20:42:26 +0000562 marshal_state *buf)
563{
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000564 HRESULT hres = S_OK;
Marcus Meissner0749fc22002-02-05 18:11:17 +0000565
Robert Shearman799ebfc2006-01-27 12:54:03 +0100566 TRACE("(tdesc.vt %s)\n",debugstr_vt(tdesc->vt));
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000567
Marcus Meissner0749fc22002-02-05 18:11:17 +0000568 switch (tdesc->vt) {
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000569 case VT_EMPTY: /* nothing. empty variant for instance */
Marcus Meissner0749fc22002-02-05 18:11:17 +0000570 return S_OK;
Marcus Meissner2045ed22005-11-04 11:13:55 +0000571 case VT_I8:
572 case VT_UI8:
Robert Shearman878af662006-01-14 17:47:25 +0100573 case VT_CY:
Marcus Meissner2045ed22005-11-04 11:13:55 +0000574 hres = S_OK;
Francois Gouget1f67fa02006-10-18 21:21:57 +0200575 if (debugout) TRACE_(olerelay)("%x%x\n",arg[0],arg[1]);
Marcus Meissner2045ed22005-11-04 11:13:55 +0000576 if (writeit)
577 hres = xbuf_add(buf,(LPBYTE)arg,8);
578 return hres;
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000579 case VT_BOOL:
580 case VT_ERROR:
Robert Shearmanfd81d9c2006-02-07 16:26:02 +0100581 case VT_INT:
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000582 case VT_UINT:
583 case VT_I4:
Marcus Meissnerd3448492002-12-05 19:12:50 +0000584 case VT_R4:
Alexandre Julliard232760d2005-03-14 10:09:11 +0000585 case VT_UI4:
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000586 hres = S_OK;
Francois Gouget1f67fa02006-10-18 21:21:57 +0200587 if (debugout) TRACE_(olerelay)("%x\n",*arg);
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000588 if (writeit)
589 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
590 return hres;
Marcus Meissnerc91df672005-03-27 18:17:19 +0000591 case VT_I2:
592 case VT_UI2:
593 hres = S_OK;
Francois Gouget1f67fa02006-10-18 21:21:57 +0200594 if (debugout) TRACE_(olerelay)("%04x\n",*arg & 0xffff);
Marcus Meissnerc91df672005-03-27 18:17:19 +0000595 if (writeit)
596 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
597 return hres;
598 case VT_I1:
599 case VT_UI1:
600 hres = S_OK;
Francois Gouget1f67fa02006-10-18 21:21:57 +0200601 if (debugout) TRACE_(olerelay)("%02x\n",*arg & 0xff);
Marcus Meissnerc91df672005-03-27 18:17:19 +0000602 if (writeit)
603 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
604 return hres;
Marcus Meissner4b5f3c62005-03-29 11:29:11 +0000605 case VT_I4|VT_BYREF:
606 hres = S_OK;
Francois Gouget1f67fa02006-10-18 21:21:57 +0200607 if (debugout) TRACE_(olerelay)("&0x%x\n",*arg);
Marcus Meissner4b5f3c62005-03-29 11:29:11 +0000608 if (writeit)
609 hres = xbuf_add(buf,(LPBYTE)(DWORD*)*arg,sizeof(DWORD));
610 /* do not dealloc at this time */
611 return hres;
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000612 case VT_VARIANT: {
613 TYPEDESC tdesc2;
614 VARIANT *vt = (VARIANT*)arg;
615 DWORD vttype = V_VT(vt);
Marcus Meissner0749fc22002-02-05 18:11:17 +0000616
Robert Shearman799ebfc2006-01-27 12:54:03 +0100617 if (debugout) TRACE_(olerelay)("Vt(%s%s)(",debugstr_vt(vttype),debugstr_vf(vttype));
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000618 tdesc2.vt = vttype;
619 if (writeit) {
620 hres = xbuf_add(buf,(LPBYTE)&vttype,sizeof(vttype));
621 if (hres) return hres;
622 }
623 /* need to recurse since we need to free the stuff */
Mike McCormacka04b77d2005-08-08 18:38:29 +0000624 hres = serialize_param(tinfo,writeit,debugout,dealloc,&tdesc2,(DWORD*)&(V_I4(vt)),buf);
Mike Hearnd440ca42004-06-17 23:03:26 +0000625 if (debugout) TRACE_(olerelay)(")");
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000626 return hres;
627 }
Alexandre Julliard232760d2005-03-14 10:09:11 +0000628 case VT_BSTR|VT_BYREF: {
Marcus Meissner4b5f3c62005-03-29 11:29:11 +0000629 if (debugout) TRACE_(olerelay)("[byref]'%s'", *(BSTR*)*arg ? relaystr(*((BSTR*)*arg)) : "<bstr NULL>");
Alexandre Julliard232760d2005-03-14 10:09:11 +0000630 if (writeit) {
631 /* ptr to ptr to magic widestring, basically */
632 BSTR *bstr = (BSTR *) *arg;
Robert Shearmand8780c72005-07-19 19:42:50 +0000633 DWORD len;
Marcus Meissner4b5f3c62005-03-29 11:29:11 +0000634 if (!*bstr) {
Alexandre Julliard232760d2005-03-14 10:09:11 +0000635 /* -1 means "null string" which is equivalent to empty string */
Robert Shearmand8780c72005-07-19 19:42:50 +0000636 len = -1;
637 hres = xbuf_add(buf, (LPBYTE)&len,sizeof(DWORD));
638 if (hres) return hres;
Alexandre Julliard232760d2005-03-14 10:09:11 +0000639 } else {
Robert Shearmand8780c72005-07-19 19:42:50 +0000640 len = *((DWORD*)*bstr-1)/sizeof(WCHAR);
641 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
642 if (hres) return hres;
643 hres = xbuf_add(buf,(LPBYTE)*bstr,len * sizeof(WCHAR));
644 if (hres) return hres;
Alexandre Julliard232760d2005-03-14 10:09:11 +0000645 }
646 }
647
648 if (dealloc && arg) {
649 BSTR *str = *((BSTR **)arg);
650 SysFreeString(*str);
651 }
652 return S_OK;
653 }
654
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000655 case VT_BSTR: {
656 if (debugout) {
Mike Hearnedcbd132005-03-27 18:20:10 +0000657 if (*arg)
658 TRACE_(olerelay)("%s",relaystr((WCHAR*)*arg));
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000659 else
Mike Hearnd440ca42004-06-17 23:03:26 +0000660 TRACE_(olerelay)("<bstr NULL>");
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000661 }
662 if (writeit) {
Robert Shearmand8780c72005-07-19 19:42:50 +0000663 BSTR bstr = (BSTR)*arg;
664 DWORD len;
665 if (!bstr) {
666 len = -1;
667 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
668 if (hres) return hres;
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000669 } else {
Robert Shearmand8780c72005-07-19 19:42:50 +0000670 len = *((DWORD*)bstr-1)/sizeof(WCHAR);
671 hres = xbuf_add(buf,(LPBYTE)&len,sizeof(DWORD));
672 if (hres) return hres;
673 hres = xbuf_add(buf,(LPBYTE)bstr,len * sizeof(WCHAR));
674 if (hres) return hres;
Marcus Meissner5b63cc22002-05-06 20:12:11 +0000675 }
Marcus Meissner0749fc22002-02-05 18:11:17 +0000676 }
Marcus Meissner236ad0d2003-12-01 22:36:49 +0000677
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000678 if (dealloc && arg)
Marcus Meissner236ad0d2003-12-01 22:36:49 +0000679 SysFreeString((BSTR)*arg);
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000680 return S_OK;
Marcus Meissner0749fc22002-02-05 18:11:17 +0000681 }
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000682 case VT_PTR: {
683 DWORD cookie;
Marcus Meissner6f823db2005-09-07 09:23:04 +0000684 BOOL derefhere = TRUE;
Robert Shearman1329f872005-06-09 09:43:38 +0000685
Marcus Meissner6f823db2005-09-07 09:23:04 +0000686 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
687 ITypeInfo *tinfo2;
688 TYPEATTR *tattr;
689
690 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
691 if (hres) {
Hans Leidekkerce66f5d2006-10-12 20:57:10 +0200692 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
Marcus Meissner6f823db2005-09-07 09:23:04 +0000693 return hres;
694 }
695 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
696 switch (tattr->typekind) {
697 case TKIND_ENUM: /* confirmed */
698 case TKIND_RECORD: /* FIXME: mostly untested */
699 derefhere=TRUE;
700 break;
701 case TKIND_ALIAS: /* FIXME: untested */
702 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
703 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
704 derefhere=FALSE;
705 break;
706 default:
707 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
708 derefhere=FALSE;
709 break;
710 }
Rob Shearmand1b15c82006-10-23 11:38:20 +0100711 ITypeInfo_ReleaseTypeAttr(tinfo, tattr);
Marcus Meissner6f823db2005-09-07 09:23:04 +0000712 ITypeInfo_Release(tinfo2);
713 }
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000714
Mike Hearnd440ca42004-06-17 23:03:26 +0000715 if (debugout) TRACE_(olerelay)("*");
Marcus Meissnere6260a32005-03-27 17:51:59 +0000716 /* Write always, so the other side knows when it gets a NULL pointer.
717 */
718 cookie = *arg ? 0x42424242 : 0;
719 hres = xbuf_add(buf,(LPBYTE)&cookie,sizeof(cookie));
720 if (hres)
721 return hres;
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000722 if (!*arg) {
Mike Hearnd440ca42004-06-17 23:03:26 +0000723 if (debugout) TRACE_(olerelay)("NULL");
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000724 return S_OK;
725 }
726 hres = serialize_param(tinfo,writeit,debugout,dealloc,tdesc->u.lptdesc,(DWORD*)*arg,buf);
Robert Shearman1329f872005-06-09 09:43:38 +0000727 if (derefhere && dealloc) HeapFree(GetProcessHeap(),0,(LPVOID)*arg);
Marcus Meissner0749fc22002-02-05 18:11:17 +0000728 return hres;
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000729 }
730 case VT_UNKNOWN:
Hans Leidekkerce66f5d2006-10-12 20:57:10 +0200731 if (debugout) TRACE_(olerelay)("unk(0x%x)",*arg);
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000732 if (writeit)
733 hres = _marshal_interface(buf,&IID_IUnknown,(LPUNKNOWN)*arg);
Robert Shearman9030ff22006-02-14 11:33:03 +0100734 if (dealloc && *(IUnknown **)arg)
Robert Shearmanec5d7392006-02-07 16:25:42 +0100735 IUnknown_Release((LPUNKNOWN)*arg);
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000736 return hres;
737 case VT_DISPATCH:
Hans Leidekkerce66f5d2006-10-12 20:57:10 +0200738 if (debugout) TRACE_(olerelay)("idisp(0x%x)",*arg);
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000739 if (writeit)
740 hres = _marshal_interface(buf,&IID_IDispatch,(LPUNKNOWN)*arg);
Robert Shearman9030ff22006-02-14 11:33:03 +0100741 if (dealloc && *(IUnknown **)arg)
Robert Shearmanec5d7392006-02-07 16:25:42 +0100742 IUnknown_Release((LPUNKNOWN)*arg);
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000743 return hres;
744 case VT_VOID:
Mike Hearnd440ca42004-06-17 23:03:26 +0000745 if (debugout) TRACE_(olerelay)("<void>");
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000746 return S_OK;
Marcus Meissner0749fc22002-02-05 18:11:17 +0000747 case VT_USERDEFINED: {
748 ITypeInfo *tinfo2;
749 TYPEATTR *tattr;
750
Marcus Meissner0749fc22002-02-05 18:11:17 +0000751 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
752 if (hres) {
Hans Leidekkerce66f5d2006-10-12 20:57:10 +0200753 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
Marcus Meissner0749fc22002-02-05 18:11:17 +0000754 return hres;
755 }
756 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
757 switch (tattr->typekind) {
Marcus Meissner26ffca42002-12-12 22:25:07 +0000758 case TKIND_DISPATCH:
Marcus Meissner0749fc22002-02-05 18:11:17 +0000759 case TKIND_INTERFACE:
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000760 if (writeit)
761 hres=_marshal_interface(buf,&(tattr->guid),(LPUNKNOWN)arg);
Robert Shearman4519b1b2005-08-24 09:44:41 +0000762 if (dealloc)
763 IUnknown_Release((LPUNKNOWN)arg);
Marcus Meissner0749fc22002-02-05 18:11:17 +0000764 break;
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000765 case TKIND_RECORD: {
766 int i;
Mike Hearnd440ca42004-06-17 23:03:26 +0000767 if (debugout) TRACE_(olerelay)("{");
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000768 for (i=0;i<tattr->cVars;i++) {
769 VARDESC *vdesc;
770 ELEMDESC *elem2;
771 TYPEDESC *tdesc2;
772
773 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
774 if (hres) {
Mike Hearn64eac752005-01-27 11:11:32 +0000775 ERR("Could not get vardesc of %d\n",i);
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000776 return hres;
777 }
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000778 elem2 = &vdesc->elemdescVar;
779 tdesc2 = &elem2->tdesc;
780 hres = serialize_param(
781 tinfo2,
782 writeit,
783 debugout,
784 dealloc,
785 tdesc2,
786 (DWORD*)(((LPBYTE)arg)+vdesc->u.oInst),
787 buf
788 );
Robert Shearmanc6296c42005-06-01 11:03:40 +0000789 ITypeInfo_ReleaseVarDesc(tinfo2, vdesc);
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000790 if (hres!=S_OK)
791 return hres;
792 if (debugout && (i<(tattr->cVars-1)))
Mike Hearnd440ca42004-06-17 23:03:26 +0000793 TRACE_(olerelay)(",");
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000794 }
Mike Hearnd440ca42004-06-17 23:03:26 +0000795 if (debugout) TRACE_(olerelay)("}");
Marcus Meissner0749fc22002-02-05 18:11:17 +0000796 break;
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000797 }
Robert Shearmand2e2e852005-07-19 19:13:23 +0000798 case TKIND_ALIAS:
Rob Shearmand1b15c82006-10-23 11:38:20 +0100799 hres = serialize_param(tinfo2,writeit,debugout,dealloc,&tattr->tdescAlias,arg,buf);
800 break;
Robert Shearmand2e2e852005-07-19 19:13:23 +0000801 case TKIND_ENUM:
802 hres = S_OK;
Hans Leidekkerce66f5d2006-10-12 20:57:10 +0200803 if (debugout) TRACE_(olerelay)("%x",*arg);
Robert Shearmand2e2e852005-07-19 19:13:23 +0000804 if (writeit)
805 hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
Rob Shearmand1b15c82006-10-23 11:38:20 +0100806 break;
Marcus Meissner26ffca42002-12-12 22:25:07 +0000807 default:
808 FIXME("Unhandled typekind %d\n",tattr->typekind);
809 hres = E_FAIL;
Marcus Meissner0749fc22002-02-05 18:11:17 +0000810 break;
811 }
Rob Shearmand1b15c82006-10-23 11:38:20 +0100812 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
Marcus Meissner0749fc22002-02-05 18:11:17 +0000813 ITypeInfo_Release(tinfo2);
814 return hres;
815 }
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000816 case VT_CARRAY: {
817 ARRAYDESC *adesc = tdesc->u.lpadesc;
818 int i, arrsize = 1;
819
Mike Hearnd440ca42004-06-17 23:03:26 +0000820 if (debugout) TRACE_(olerelay)("carr");
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000821 for (i=0;i<adesc->cDims;i++) {
Hans Leidekkerce66f5d2006-10-12 20:57:10 +0200822 if (debugout) TRACE_(olerelay)("[%d]",adesc->rgbounds[i].cElements);
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000823 arrsize *= adesc->rgbounds[i].cElements;
824 }
Robert Shearman799ebfc2006-01-27 12:54:03 +0100825 if (debugout) TRACE_(olerelay)("(vt %s)",debugstr_vt(adesc->tdescElem.vt));
Mike Hearnd440ca42004-06-17 23:03:26 +0000826 if (debugout) TRACE_(olerelay)("[");
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000827 for (i=0;i<arrsize;i++) {
828 hres = serialize_param(tinfo, writeit, debugout, dealloc, &adesc->tdescElem, (DWORD*)((LPBYTE)arg+i*_xsize(&adesc->tdescElem)), buf);
829 if (hres)
830 return hres;
Mike Hearnd440ca42004-06-17 23:03:26 +0000831 if (debugout && (i<arrsize-1)) TRACE_(olerelay)(",");
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000832 }
Mike Hearnd440ca42004-06-17 23:03:26 +0000833 if (debugout) TRACE_(olerelay)("]");
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000834 return S_OK;
835 }
Huw Davies86d94572006-05-10 12:11:22 +0100836 case VT_SAFEARRAY: {
837 if (writeit)
838 {
Michael Stefaniuc9d625ad2006-11-08 23:46:13 +0100839 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
840 ULONG size = LPSAFEARRAY_UserSize(&flags, buf->curoff, (LPSAFEARRAY *)arg);
Huw Davies86d94572006-05-10 12:11:22 +0100841 xbuf_resize(buf, size);
842 LPSAFEARRAY_UserMarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
843 buf->curoff = size;
844 }
845 return S_OK;
846 }
Marcus Meissner0749fc22002-02-05 18:11:17 +0000847 default:
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000848 ERR("Unhandled marshal type %d.\n",tdesc->vt);
849 return S_OK;
850 }
851}
852
Marcus Meissner0749fc22002-02-05 18:11:17 +0000853static HRESULT
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000854deserialize_param(
855 ITypeInfo *tinfo,
856 BOOL readit,
857 BOOL debugout,
858 BOOL alloc,
859 TYPEDESC *tdesc,
860 DWORD *arg,
Robert Shearman2a54aac2004-09-07 20:42:26 +0000861 marshal_state *buf)
862{
Marcus Meissner0749fc22002-02-05 18:11:17 +0000863 HRESULT hres = S_OK;
Marcus Meissner0749fc22002-02-05 18:11:17 +0000864
Robert Shearman799ebfc2006-01-27 12:54:03 +0100865 TRACE("vt %s at %p\n",debugstr_vt(tdesc->vt),arg);
Marcus Meissner0749fc22002-02-05 18:11:17 +0000866
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000867 while (1) {
868 switch (tdesc->vt) {
869 case VT_EMPTY:
Francois Gouget1f67fa02006-10-18 21:21:57 +0200870 if (debugout) TRACE_(olerelay)("<empty>\n");
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000871 return S_OK;
872 case VT_NULL:
Francois Gouget1f67fa02006-10-18 21:21:57 +0200873 if (debugout) TRACE_(olerelay)("<null>\n");
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000874 return S_OK;
875 case VT_VARIANT: {
876 VARIANT *vt = (VARIANT*)arg;
877
878 if (readit) {
879 DWORD vttype;
880 TYPEDESC tdesc2;
881 hres = xbuf_get(buf,(LPBYTE)&vttype,sizeof(vttype));
882 if (hres) {
883 FIXME("vt type not read?\n");
884 return hres;
885 }
886 memset(&tdesc2,0,sizeof(tdesc2));
887 tdesc2.vt = vttype;
888 V_VT(vt) = vttype;
Robert Shearman799ebfc2006-01-27 12:54:03 +0100889 if (debugout) TRACE_(olerelay)("Vt(%s%s)(",debugstr_vt(vttype),debugstr_vf(vttype));
Mike McCormacka04b77d2005-08-08 18:38:29 +0000890 hres = deserialize_param(tinfo, readit, debugout, alloc, &tdesc2, (DWORD*)&(V_I4(vt)), buf);
Mike Hearnd440ca42004-06-17 23:03:26 +0000891 TRACE_(olerelay)(")");
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000892 return hres;
893 } else {
894 VariantInit(vt);
895 return S_OK;
896 }
897 }
Marcus Meissner2045ed22005-11-04 11:13:55 +0000898 case VT_I8:
899 case VT_UI8:
Robert Shearman878af662006-01-14 17:47:25 +0100900 case VT_CY:
Marcus Meissner2045ed22005-11-04 11:13:55 +0000901 if (readit) {
902 hres = xbuf_get(buf,(LPBYTE)arg,8);
903 if (hres) ERR("Failed to read integer 8 byte\n");
904 }
Hans Leidekkerce66f5d2006-10-12 20:57:10 +0200905 if (debugout) TRACE_(olerelay)("%x%x",arg[0],arg[1]);
Marcus Meissner2045ed22005-11-04 11:13:55 +0000906 return hres;
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000907 case VT_ERROR:
Alexandre Julliard232760d2005-03-14 10:09:11 +0000908 case VT_BOOL:
909 case VT_I4:
Robert Shearmanfd81d9c2006-02-07 16:26:02 +0100910 case VT_INT:
Alexandre Julliard232760d2005-03-14 10:09:11 +0000911 case VT_UINT:
912 case VT_R4:
913 case VT_UI4:
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000914 if (readit) {
915 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
Mike Hearn64eac752005-01-27 11:11:32 +0000916 if (hres) ERR("Failed to read integer 4 byte\n");
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000917 }
Hans Leidekkerce66f5d2006-10-12 20:57:10 +0200918 if (debugout) TRACE_(olerelay)("%x",*arg);
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000919 return hres;
Marcus Meissnerc91df672005-03-27 18:17:19 +0000920 case VT_I2:
921 case VT_UI2:
922 if (readit) {
923 DWORD x;
924 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
925 if (hres) ERR("Failed to read integer 4 byte\n");
926 memcpy(arg,&x,2);
927 }
Hans Leidekkerce66f5d2006-10-12 20:57:10 +0200928 if (debugout) TRACE_(olerelay)("%04x",*arg & 0xffff);
Marcus Meissnerc91df672005-03-27 18:17:19 +0000929 return hres;
930 case VT_I1:
931 case VT_UI1:
932 if (readit) {
933 DWORD x;
934 hres = xbuf_get(buf,(LPBYTE)&x,sizeof(DWORD));
935 if (hres) ERR("Failed to read integer 4 byte\n");
936 memcpy(arg,&x,1);
937 }
Hans Leidekkerce66f5d2006-10-12 20:57:10 +0200938 if (debugout) TRACE_(olerelay)("%02x",*arg & 0xff);
Marcus Meissnerc91df672005-03-27 18:17:19 +0000939 return hres;
Marcus Meissner4b5f3c62005-03-29 11:29:11 +0000940 case VT_I4|VT_BYREF:
941 hres = S_OK;
942 if (alloc)
943 *arg = (DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
944 if (readit) {
945 hres = xbuf_get(buf,(LPBYTE)*arg,sizeof(DWORD));
946 if (hres) ERR("Failed to read integer 4 byte\n");
947 }
Hans Leidekkerce66f5d2006-10-12 20:57:10 +0200948 if (debugout) TRACE_(olerelay)("&0x%x",*(DWORD*)*arg);
Marcus Meissner4b5f3c62005-03-29 11:29:11 +0000949 return hres;
Alexandre Julliard232760d2005-03-14 10:09:11 +0000950 case VT_BSTR|VT_BYREF: {
951 BSTR **bstr = (BSTR **)arg;
952 WCHAR *str;
953 DWORD len;
954
955 if (readit) {
956 hres = xbuf_get(buf,(LPBYTE)&len,sizeof(DWORD));
957 if (hres) {
958 ERR("failed to read bstr klen\n");
959 return hres;
960 }
961 if (len == -1) {
Marcus Meissner4b5f3c62005-03-29 11:29:11 +0000962 *bstr = CoTaskMemAlloc(sizeof(BSTR *));
963 **bstr = NULL;
Alexandre Julliard232760d2005-03-14 10:09:11 +0000964 if (debugout) TRACE_(olerelay)("<bstr NULL>");
965 } else {
Robert Shearmand8780c72005-07-19 19:42:50 +0000966 str = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(len+1)*sizeof(WCHAR));
967 hres = xbuf_get(buf,(LPBYTE)str,len*sizeof(WCHAR));
Alexandre Julliard232760d2005-03-14 10:09:11 +0000968 if (hres) {
969 ERR("Failed to read BSTR.\n");
Andrew Talbotedbcf7c2007-10-01 21:33:07 +0100970 HeapFree(GetProcessHeap(),0,str);
Alexandre Julliard232760d2005-03-14 10:09:11 +0000971 return hres;
972 }
973 *bstr = CoTaskMemAlloc(sizeof(BSTR *));
974 **bstr = SysAllocStringLen(str,len);
975 if (debugout) TRACE_(olerelay)("%s",relaystr(str));
976 HeapFree(GetProcessHeap(),0,str);
977 }
978 } else {
979 *bstr = NULL;
980 }
981 return S_OK;
982 }
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000983 case VT_BSTR: {
984 WCHAR *str;
985 DWORD len;
986
987 if (readit) {
988 hres = xbuf_get(buf,(LPBYTE)&len,sizeof(DWORD));
989 if (hres) {
Mike Hearn64eac752005-01-27 11:11:32 +0000990 ERR("failed to read bstr klen\n");
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000991 return hres;
992 }
993 if (len == -1) {
994 *arg = 0;
Mike Hearnd440ca42004-06-17 23:03:26 +0000995 if (debugout) TRACE_(olerelay)("<bstr NULL>");
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000996 } else {
Robert Shearmand8780c72005-07-19 19:42:50 +0000997 str = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(len+1)*sizeof(WCHAR));
998 hres = xbuf_get(buf,(LPBYTE)str,len*sizeof(WCHAR));
Marcus Meissnere81f10c2002-06-10 02:30:20 +0000999 if (hres) {
Mike Hearn64eac752005-01-27 11:11:32 +00001000 ERR("Failed to read BSTR.\n");
Andrew Talbotedbcf7c2007-10-01 21:33:07 +01001001 HeapFree(GetProcessHeap(),0,str);
Marcus Meissnere81f10c2002-06-10 02:30:20 +00001002 return hres;
1003 }
1004 *arg = (DWORD)SysAllocStringLen(str,len);
Mike Hearnd440ca42004-06-17 23:03:26 +00001005 if (debugout) TRACE_(olerelay)("%s",relaystr(str));
Marcus Meissnere81f10c2002-06-10 02:30:20 +00001006 HeapFree(GetProcessHeap(),0,str);
1007 }
1008 } else {
1009 *arg = 0;
1010 }
1011 return S_OK;
1012 }
1013 case VT_PTR: {
1014 DWORD cookie;
Marcus Meissner6f823db2005-09-07 09:23:04 +00001015 BOOL derefhere = TRUE;
Marcus Meissnere81f10c2002-06-10 02:30:20 +00001016
Marcus Meissner6f823db2005-09-07 09:23:04 +00001017 if (tdesc->u.lptdesc->vt == VT_USERDEFINED) {
1018 ITypeInfo *tinfo2;
1019 TYPEATTR *tattr;
1020
1021 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.lptdesc->u.hreftype,&tinfo2);
1022 if (hres) {
Hans Leidekkerce66f5d2006-10-12 20:57:10 +02001023 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.lptdesc->u.hreftype);
Marcus Meissner6f823db2005-09-07 09:23:04 +00001024 return hres;
1025 }
1026 ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1027 switch (tattr->typekind) {
1028 case TKIND_ENUM: /* confirmed */
1029 case TKIND_RECORD: /* FIXME: mostly untested */
1030 derefhere=TRUE;
1031 break;
1032 case TKIND_ALIAS: /* FIXME: untested */
1033 case TKIND_DISPATCH: /* will be done in VT_USERDEFINED case */
1034 case TKIND_INTERFACE: /* will be done in VT_USERDEFINED case */
1035 derefhere=FALSE;
1036 break;
1037 default:
1038 FIXME("unhandled switch cases tattr->typekind %d\n", tattr->typekind);
1039 derefhere=FALSE;
1040 break;
1041 }
Rob Shearmand1b15c82006-10-23 11:38:20 +01001042 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
Marcus Meissner6f823db2005-09-07 09:23:04 +00001043 ITypeInfo_Release(tinfo2);
1044 }
Marcus Meissnere6260a32005-03-27 17:51:59 +00001045 /* read it in all cases, we need to know if we have
1046 * NULL pointer or not.
1047 */
1048 hres = xbuf_get(buf,(LPBYTE)&cookie,sizeof(cookie));
1049 if (hres) {
1050 ERR("Failed to load pointer cookie.\n");
1051 return hres;
Marcus Meissnere81f10c2002-06-10 02:30:20 +00001052 }
Marcus Meissnere6260a32005-03-27 17:51:59 +00001053 if (cookie != 0x42424242) {
1054 /* we read a NULL ptr from the remote side */
1055 if (debugout) TRACE_(olerelay)("NULL");
1056 *arg = 0;
1057 return S_OK;
1058 }
1059 if (debugout) TRACE_(olerelay)("*");
Marcus Meissnere81f10c2002-06-10 02:30:20 +00001060 if (alloc) {
Marcus Meissnere6260a32005-03-27 17:51:59 +00001061 /* Allocate space for the referenced struct */
Marcus Meissnere81f10c2002-06-10 02:30:20 +00001062 if (derefhere)
1063 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,_xsize(tdesc->u.lptdesc));
1064 }
1065 if (derefhere)
1066 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, (LPDWORD)*arg, buf);
1067 else
1068 return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, arg, buf);
1069 }
1070 case VT_UNKNOWN:
1071 /* FIXME: UNKNOWN is unknown ..., but allocate 4 byte for it */
1072 if (alloc)
1073 *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
1074 hres = S_OK;
1075 if (readit)
1076 hres = _unmarshal_interface(buf,&IID_IUnknown,(LPUNKNOWN*)arg);
1077 if (debugout)
Mike Hearnd440ca42004-06-17 23:03:26 +00001078 TRACE_(olerelay)("unk(%p)",arg);
Marcus Meissnere81f10c2002-06-10 02:30:20 +00001079 return hres;
1080 case VT_DISPATCH:
1081 hres = S_OK;
1082 if (readit)
1083 hres = _unmarshal_interface(buf,&IID_IDispatch,(LPUNKNOWN*)arg);
1084 if (debugout)
Mike Hearnd440ca42004-06-17 23:03:26 +00001085 TRACE_(olerelay)("idisp(%p)",arg);
Marcus Meissnere81f10c2002-06-10 02:30:20 +00001086 return hres;
1087 case VT_VOID:
Mike Hearnd440ca42004-06-17 23:03:26 +00001088 if (debugout) TRACE_(olerelay)("<void>");
Marcus Meissnere81f10c2002-06-10 02:30:20 +00001089 return S_OK;
1090 case VT_USERDEFINED: {
1091 ITypeInfo *tinfo2;
1092 TYPEATTR *tattr;
1093
1094 hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
1095 if (hres) {
Hans Leidekkerce66f5d2006-10-12 20:57:10 +02001096 ERR("Could not get typeinfo of hreftype %x for VT_USERDEFINED.\n",tdesc->u.hreftype);
Marcus Meissnere81f10c2002-06-10 02:30:20 +00001097 return hres;
1098 }
1099 hres = ITypeInfo_GetTypeAttr(tinfo2,&tattr);
1100 if (hres) {
Mike Hearn64eac752005-01-27 11:11:32 +00001101 ERR("Could not get typeattr in VT_USERDEFINED.\n");
Marcus Meissnere81f10c2002-06-10 02:30:20 +00001102 } else {
Marcus Meissnere81f10c2002-06-10 02:30:20 +00001103 switch (tattr->typekind) {
Marcus Meissner26ffca42002-12-12 22:25:07 +00001104 case TKIND_DISPATCH:
Marcus Meissnere81f10c2002-06-10 02:30:20 +00001105 case TKIND_INTERFACE:
1106 if (readit)
1107 hres = _unmarshal_interface(buf,&(tattr->guid),(LPUNKNOWN*)arg);
1108 break;
1109 case TKIND_RECORD: {
1110 int i;
1111
Alexandre Julliard232760d2005-03-14 10:09:11 +00001112 if (alloc)
1113 *arg = (DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,tattr->cbSizeInstance);
1114
Mike Hearnd440ca42004-06-17 23:03:26 +00001115 if (debugout) TRACE_(olerelay)("{");
Marcus Meissnere81f10c2002-06-10 02:30:20 +00001116 for (i=0;i<tattr->cVars;i++) {
1117 VARDESC *vdesc;
1118
1119 hres = ITypeInfo2_GetVarDesc(tinfo2, i, &vdesc);
1120 if (hres) {
Mike Hearn64eac752005-01-27 11:11:32 +00001121 ERR("Could not get vardesc of %d\n",i);
Rob Shearmand1b15c82006-10-23 11:38:20 +01001122 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
1123 ITypeInfo_Release(tinfo2);
Marcus Meissnere81f10c2002-06-10 02:30:20 +00001124 return hres;
1125 }
1126 hres = deserialize_param(
1127 tinfo2,
1128 readit,
1129 debugout,
1130 alloc,
1131 &vdesc->elemdescVar.tdesc,
1132 (DWORD*)(((LPBYTE)*arg)+vdesc->u.oInst),
1133 buf
1134 );
Robert Shearmanb0d8e06c2005-12-06 21:24:19 +01001135 ITypeInfo2_ReleaseVarDesc(tinfo2, vdesc);
Mike Hearnd440ca42004-06-17 23:03:26 +00001136 if (debugout && (i<tattr->cVars-1)) TRACE_(olerelay)(",");
Marcus Meissnere81f10c2002-06-10 02:30:20 +00001137 }
Mike Hearnd440ca42004-06-17 23:03:26 +00001138 if (debugout) TRACE_(olerelay)("}");
Marcus Meissnere81f10c2002-06-10 02:30:20 +00001139 break;
1140 }
Robert Shearmand2e2e852005-07-19 19:13:23 +00001141 case TKIND_ALIAS:
Rob Shearmand1b15c82006-10-23 11:38:20 +01001142 hres = deserialize_param(tinfo2,readit,debugout,alloc,&tattr->tdescAlias,arg,buf);
1143 break;
Robert Shearmand2e2e852005-07-19 19:13:23 +00001144 case TKIND_ENUM:
1145 if (readit) {
1146 hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
1147 if (hres) ERR("Failed to read enum (4 byte)\n");
1148 }
Hans Leidekkerce66f5d2006-10-12 20:57:10 +02001149 if (debugout) TRACE_(olerelay)("%x",*arg);
Rob Shearmand1b15c82006-10-23 11:38:20 +01001150 break;
Marcus Meissner26ffca42002-12-12 22:25:07 +00001151 default:
1152 ERR("Unhandled typekind %d\n",tattr->typekind);
1153 hres = E_FAIL;
Marcus Meissnere81f10c2002-06-10 02:30:20 +00001154 break;
1155 }
Rob Shearmand1b15c82006-10-23 11:38:20 +01001156 ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
Marcus Meissnere81f10c2002-06-10 02:30:20 +00001157 }
1158 if (hres)
Mike Hearn64eac752005-01-27 11:11:32 +00001159 ERR("failed to stuballoc in TKIND_RECORD.\n");
Marcus Meissnere81f10c2002-06-10 02:30:20 +00001160 ITypeInfo_Release(tinfo2);
Marcus Meissner0749fc22002-02-05 18:11:17 +00001161 return hres;
1162 }
Marcus Meissnere81f10c2002-06-10 02:30:20 +00001163 case VT_CARRAY: {
1164 /* arg is pointing to the start of the array. */
1165 ARRAYDESC *adesc = tdesc->u.lpadesc;
1166 int arrsize,i;
1167 arrsize = 1;
1168 if (adesc->cDims > 1) FIXME("cDims > 1 in VT_CARRAY. Does it work?\n");
1169 for (i=0;i<adesc->cDims;i++)
1170 arrsize *= adesc->rgbounds[i].cElements;
1171 for (i=0;i<arrsize;i++)
1172 deserialize_param(
1173 tinfo,
1174 readit,
1175 debugout,
1176 alloc,
1177 &adesc->tdescElem,
1178 (DWORD*)((LPBYTE)(arg)+i*_xsize(&adesc->tdescElem)),
1179 buf
1180 );
1181 return S_OK;
Marcus Meissner0749fc22002-02-05 18:11:17 +00001182 }
Huw Davies86d94572006-05-10 12:11:22 +01001183 case VT_SAFEARRAY: {
1184 if (readit)
1185 {
Michael Stefaniuc9d625ad2006-11-08 23:46:13 +01001186 ULONG flags = MAKELONG(MSHCTX_DIFFERENTMACHINE, NDR_LOCAL_DATA_REPRESENTATION);
Huw Davies86d94572006-05-10 12:11:22 +01001187 unsigned char *buffer;
1188 buffer = LPSAFEARRAY_UserUnmarshal(&flags, buf->base + buf->curoff, (LPSAFEARRAY *)arg);
1189 buf->curoff = buffer - buf->base;
1190 }
1191 return S_OK;
1192 }
Marcus Meissner0749fc22002-02-05 18:11:17 +00001193 default:
Marcus Meissnere81f10c2002-06-10 02:30:20 +00001194 ERR("No handler for VT type %d!\n",tdesc->vt);
1195 return S_OK;
Marcus Meissner0749fc22002-02-05 18:11:17 +00001196 }
Marcus Meissnere81f10c2002-06-10 02:30:20 +00001197 }
1198}
1199
Huw Davies294b3eb2007-04-26 11:56:44 +01001200/* Retrieves a function's funcdesc, searching back into inherited interfaces. */
1201static HRESULT get_funcdesc(ITypeInfo *tinfo, int iMethod, ITypeInfo **tactual, const FUNCDESC **fdesc,
1202 BSTR *iname, BSTR *fname, UINT *num)
Robert Shearman2a54aac2004-09-07 20:42:26 +00001203{
Huw Davies294b3eb2007-04-26 11:56:44 +01001204 HRESULT hr;
1205 UINT i, impl_types;
1206 UINT inherited_funcs = 0;
1207 TYPEATTR *attr;
Marcus Meissner0749fc22002-02-05 18:11:17 +00001208
1209 if (fname) *fname = NULL;
1210 if (iname) *iname = NULL;
Huw Davies294b3eb2007-04-26 11:56:44 +01001211 if (num) *num = 0;
1212 *tactual = NULL;
Marcus Meissner0749fc22002-02-05 18:11:17 +00001213
Huw Davies294b3eb2007-04-26 11:56:44 +01001214 hr = ITypeInfo_GetTypeAttr(tinfo, &attr);
1215 if (FAILED(hr))
1216 {
1217 ERR("GetTypeAttr failed with %x\n",hr);
1218 return hr;
Marcus Meissner0749fc22002-02-05 18:11:17 +00001219 }
Huw Daviesc24a53d2007-05-17 15:29:41 +01001220
1221 if(attr->typekind == TKIND_DISPATCH)
1222 {
1223 if(attr->wTypeFlags & TYPEFLAG_FDUAL)
1224 {
1225 HREFTYPE href;
1226 ITypeInfo *tinfo2;
1227
1228 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, -1, &href);
1229 if(FAILED(hr))
1230 {
1231 ERR("Cannot get interface href from dual dispinterface\n");
1232 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1233 return hr;
1234 }
1235 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &tinfo2);
1236 if(FAILED(hr))
1237 {
1238 ERR("Cannot get interface from dual dispinterface\n");
1239 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1240 return hr;
1241 }
1242 hr = get_funcdesc(tinfo2, iMethod, tactual, fdesc, iname, fname, num);
1243 ITypeInfo_Release(tinfo2);
1244 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1245 return hr;
1246 }
1247 ERR("Shouldn't be called with a non-dual dispinterface\n");
1248 return E_FAIL;
1249 }
1250
Huw Davies294b3eb2007-04-26 11:56:44 +01001251 impl_types = attr->cImplTypes;
1252 ITypeInfo_ReleaseTypeAttr(tinfo, attr);
1253
1254 for (i = 0; i < impl_types; i++)
1255 {
1256 HREFTYPE href;
1257 ITypeInfo *pSubTypeInfo;
1258 UINT sub_funcs;
1259
1260 hr = ITypeInfo_GetRefTypeOfImplType(tinfo, i, &href);
1261 if (FAILED(hr)) return hr;
1262 hr = ITypeInfo_GetRefTypeInfo(tinfo, href, &pSubTypeInfo);
1263 if (FAILED(hr)) return hr;
1264
1265 hr = get_funcdesc(pSubTypeInfo, iMethod, tactual, fdesc, iname, fname, &sub_funcs);
1266 inherited_funcs += sub_funcs;
1267 ITypeInfo_Release(pSubTypeInfo);
1268 if(SUCCEEDED(hr)) return hr;
1269 }
1270 if(iMethod < inherited_funcs)
1271 {
1272 ERR("shouldn't be here\n");
1273 return E_INVALIDARG;
1274 }
1275
1276 for(i = inherited_funcs; i <= iMethod; i++)
1277 {
1278 hr = ITypeInfoImpl_GetInternalFuncDesc(tinfo, i - inherited_funcs, fdesc);
1279 if(FAILED(hr))
1280 {
1281 if(num) *num = i;
1282 return hr;
1283 }
1284 }
1285
1286 /* found it. We don't care about num so zero it */
1287 if(num) *num = 0;
1288 *tactual = tinfo;
1289 ITypeInfo_AddRef(*tactual);
1290 if (fname) ITypeInfo_GetDocumentation(tinfo,(*fdesc)->memid,fname,NULL,NULL,NULL);
1291 if (iname) ITypeInfo_GetDocumentation(tinfo,-1,iname,NULL,NULL,NULL);
1292 return S_OK;
Marcus Meissner0749fc22002-02-05 18:11:17 +00001293}
1294
Rob Shearman0f45b2b2007-02-07 19:19:26 +00001295static inline BOOL is_in_elem(const ELEMDESC *elem)
1296{
1297 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FIN || !elem->u.paramdesc.wParamFlags);
1298}
1299
1300static inline BOOL is_out_elem(const ELEMDESC *elem)
1301{
1302 return (elem->u.paramdesc.wParamFlags & PARAMFLAG_FOUT || !elem->u.paramdesc.wParamFlags);
1303}
1304
Marcus Meissner0749fc22002-02-05 18:11:17 +00001305static DWORD
Robert Shearman2a54aac2004-09-07 20:42:26 +00001306xCall(LPVOID retptr, int method, TMProxyImpl *tpinfo /*, args */)
1307{
Marcus Meissner0749fc22002-02-05 18:11:17 +00001308 DWORD *args = ((DWORD*)&tpinfo)+1, *xargs;
Robert Shearmanb0d8e06c2005-12-06 21:24:19 +01001309 const FUNCDESC *fdesc;
Marcus Meissner0749fc22002-02-05 18:11:17 +00001310 HRESULT hres;
1311 int i, relaydeb = TRACE_ON(olerelay);
1312 marshal_state buf;
1313 RPCOLEMESSAGE msg;
1314 ULONG status;
1315 BSTR fname,iname;
1316 BSTR names[10];
Mike McCormacka04b77d2005-08-08 18:38:29 +00001317 UINT nrofnames;
Marcus Meissner89feaca2005-03-28 10:00:49 +00001318 DWORD remoteresult = 0;
Robert Shearmanc6296c42005-06-01 11:03:40 +00001319 ITypeInfo *tinfo;
Robert Shearmanefe606f2005-11-02 20:00:00 +00001320 IRpcChannelBuffer *chanbuf;
Marcus Meissner0749fc22002-02-05 18:11:17 +00001321
Robert Shearman2a54aac2004-09-07 20:42:26 +00001322 EnterCriticalSection(&tpinfo->crit);
1323
Huw Davies294b3eb2007-04-26 11:56:44 +01001324 hres = get_funcdesc(tpinfo->tinfo,method,&tinfo,&fdesc,&iname,&fname,NULL);
Marcus Meissner0749fc22002-02-05 18:11:17 +00001325 if (hres) {
Robert Shearmanefe606f2005-11-02 20:00:00 +00001326 ERR("Did not find typeinfo/funcdesc entry for method %d!\n",method);
Robert Shearman2a54aac2004-09-07 20:42:26 +00001327 LeaveCriticalSection(&tpinfo->crit);
Robert Shearmanefe606f2005-11-02 20:00:00 +00001328 return E_FAIL;
Marcus Meissner0749fc22002-02-05 18:11:17 +00001329 }
1330
Robert Shearman67942792004-12-14 11:44:11 +00001331 if (!tpinfo->chanbuf)
1332 {
1333 WARN("Tried to use disconnected proxy\n");
Robert Shearmanc6296c42005-06-01 11:03:40 +00001334 ITypeInfo_Release(tinfo);
Robert Shearman67942792004-12-14 11:44:11 +00001335 LeaveCriticalSection(&tpinfo->crit);
1336 return RPC_E_DISCONNECTED;
1337 }
Robert Shearmanefe606f2005-11-02 20:00:00 +00001338 chanbuf = tpinfo->chanbuf;
1339 IRpcChannelBuffer_AddRef(chanbuf);
1340
1341 LeaveCriticalSection(&tpinfo->crit);
Robert Shearman67942792004-12-14 11:44:11 +00001342
Marcus Meissner0749fc22002-02-05 18:11:17 +00001343 if (relaydeb) {
Mike Hearn86cc2d52004-06-23 00:10:29 +00001344 TRACE_(olerelay)("->");
Marcus Meissner0749fc22002-02-05 18:11:17 +00001345 if (iname)
Mike Hearnd440ca42004-06-17 23:03:26 +00001346 TRACE_(olerelay)("%s:",relaystr(iname));
Marcus Meissner0749fc22002-02-05 18:11:17 +00001347 if (fname)
Mike Hearnd440ca42004-06-17 23:03:26 +00001348 TRACE_(olerelay)("%s(%d)",relaystr(fname),method);
Marcus Meissner0749fc22002-02-05 18:11:17 +00001349 else
Mike Hearnd440ca42004-06-17 23:03:26 +00001350 TRACE_(olerelay)("%d",method);
1351 TRACE_(olerelay)("(");
Marcus Meissner0749fc22002-02-05 18:11:17 +00001352 }
Marcus Meissnere6260a32005-03-27 17:51:59 +00001353
1354 if (iname) SysFreeString(iname);
1355 if (fname) SysFreeString(fname);
1356
1357 memset(&buf,0,sizeof(buf));
Marcus Meissnere6260a32005-03-27 17:51:59 +00001358
1359 /* normal typelib driven serializing */
1360
Marcus Meissner0749fc22002-02-05 18:11:17 +00001361 /* Need them for hack below */
1362 memset(names,0,sizeof(names));
Robert Shearmanc6296c42005-06-01 11:03:40 +00001363 if (ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames))
Marcus Meissnere81f10c2002-06-10 02:30:20 +00001364 nrofnames = 0;
1365 if (nrofnames > sizeof(names)/sizeof(names[0]))
Marcus Meissner0749fc22002-02-05 18:11:17 +00001366 ERR("Need more names!\n");
Marcus Meissnere81f10c2002-06-10 02:30:20 +00001367
Marcus Meissnere6260a32005-03-27 17:51:59 +00001368 xargs = args;
1369 for (i=0;i<fdesc->cParams;i++) {
1370 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
Marcus Meissnere6260a32005-03-27 17:51:59 +00001371 if (relaydeb) {
1372 if (i) TRACE_(olerelay)(",");
1373 if (i+1<nrofnames && names[i+1])
1374 TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1375 }
1376 /* No need to marshal other data than FIN and any VT_PTR. */
Rob Shearman0f45b2b2007-02-07 19:19:26 +00001377 if (!is_in_elem(elem) && (elem->tdesc.vt != VT_PTR)) {
Marcus Meissnere6260a32005-03-27 17:51:59 +00001378 xargs+=_argsize(elem->tdesc.vt);
1379 if (relaydeb) TRACE_(olerelay)("[out]");
1380 continue;
1381 }
Robert Shearman5c2a7082005-10-26 10:12:30 +00001382 hres = serialize_param(
1383 tinfo,
Rob Shearman0f45b2b2007-02-07 19:19:26 +00001384 is_in_elem(elem),
Robert Shearman5c2a7082005-10-26 10:12:30 +00001385 relaydeb,
1386 FALSE,
1387 &elem->tdesc,
1388 xargs,
1389 &buf
1390 );
Marcus Meissnere6260a32005-03-27 17:51:59 +00001391
1392 if (hres) {
Hans Leidekkerce66f5d2006-10-12 20:57:10 +02001393 ERR("Failed to serialize param, hres %x\n",hres);
Marcus Meissnere6260a32005-03-27 17:51:59 +00001394 break;
1395 }
1396 xargs+=_argsize(elem->tdesc.vt);
Marcus Meissner0749fc22002-02-05 18:11:17 +00001397 }
Mike Hearnd440ca42004-06-17 23:03:26 +00001398 if (relaydeb) TRACE_(olerelay)(")");
Marcus Meissnere6260a32005-03-27 17:51:59 +00001399
Marcus Meissner0749fc22002-02-05 18:11:17 +00001400 memset(&msg,0,sizeof(msg));
1401 msg.cbBuffer = buf.curoff;
1402 msg.iMethod = method;
Robert Shearmanefe606f2005-11-02 20:00:00 +00001403 hres = IRpcChannelBuffer_GetBuffer(chanbuf,&msg,&(tpinfo->iid));
Marcus Meissner0749fc22002-02-05 18:11:17 +00001404 if (hres) {
Hans Leidekkerce66f5d2006-10-12 20:57:10 +02001405 ERR("RpcChannelBuffer GetBuffer failed, %x\n",hres);
Robert Shearmanefe606f2005-11-02 20:00:00 +00001406 goto exit;
Marcus Meissner0749fc22002-02-05 18:11:17 +00001407 }
1408 memcpy(msg.Buffer,buf.base,buf.curoff);
Mike Hearnd440ca42004-06-17 23:03:26 +00001409 if (relaydeb) TRACE_(olerelay)("\n");
Robert Shearmanefe606f2005-11-02 20:00:00 +00001410 hres = IRpcChannelBuffer_SendReceive(chanbuf,&msg,&status);
Marcus Meissner0749fc22002-02-05 18:11:17 +00001411 if (hres) {
Hans Leidekkerce66f5d2006-10-12 20:57:10 +02001412 ERR("RpcChannelBuffer SendReceive failed, %x\n",hres);
Robert Shearmanefe606f2005-11-02 20:00:00 +00001413 goto exit;
Marcus Meissner0749fc22002-02-05 18:11:17 +00001414 }
Mike Hearnd440ca42004-06-17 23:03:26 +00001415
Hans Leidekkerce66f5d2006-10-12 20:57:10 +02001416 if (relaydeb) TRACE_(olerelay)(" status = %08x (",status);
Marcus Meissner0749fc22002-02-05 18:11:17 +00001417 if (buf.base)
1418 buf.base = HeapReAlloc(GetProcessHeap(),0,buf.base,msg.cbBuffer);
1419 else
1420 buf.base = HeapAlloc(GetProcessHeap(),0,msg.cbBuffer);
1421 buf.size = msg.cbBuffer;
1422 memcpy(buf.base,msg.Buffer,buf.size);
1423 buf.curoff = 0;
Marcus Meissnere6260a32005-03-27 17:51:59 +00001424
Marcus Meissnere6260a32005-03-27 17:51:59 +00001425 /* generic deserializer using typelib description */
1426 xargs = args;
Marcus Meissner89feaca2005-03-28 10:00:49 +00001427 status = S_OK;
Marcus Meissnere6260a32005-03-27 17:51:59 +00001428 for (i=0;i<fdesc->cParams;i++) {
1429 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
Marcus Meissnere81f10c2002-06-10 02:30:20 +00001430
Marcus Meissnere6260a32005-03-27 17:51:59 +00001431 if (relaydeb) {
1432 if (i) TRACE_(olerelay)(",");
1433 if (i+1<nrofnames && names[i+1]) TRACE_(olerelay)("%s=",relaystr(names[i+1]));
1434 }
1435 /* No need to marshal other data than FOUT and any VT_PTR */
Rob Shearman0f45b2b2007-02-07 19:19:26 +00001436 if (!is_out_elem(elem) && (elem->tdesc.vt != VT_PTR)) {
Marcus Meissnere6260a32005-03-27 17:51:59 +00001437 xargs += _argsize(elem->tdesc.vt);
1438 if (relaydeb) TRACE_(olerelay)("[in]");
1439 continue;
1440 }
Robert Shearman5c2a7082005-10-26 10:12:30 +00001441 hres = deserialize_param(
1442 tinfo,
Rob Shearman0f45b2b2007-02-07 19:19:26 +00001443 is_out_elem(elem),
Robert Shearman5c2a7082005-10-26 10:12:30 +00001444 relaydeb,
1445 FALSE,
1446 &(elem->tdesc),
1447 xargs,
1448 &buf
1449 );
Marcus Meissnere6260a32005-03-27 17:51:59 +00001450 if (hres) {
Hans Leidekkerce66f5d2006-10-12 20:57:10 +02001451 ERR("Failed to unmarshall param, hres %x\n",hres);
Marcus Meissnere6260a32005-03-27 17:51:59 +00001452 status = hres;
1453 break;
1454 }
1455 xargs += _argsize(elem->tdesc.vt);
Marcus Meissner0749fc22002-02-05 18:11:17 +00001456 }
Robert Shearman5c2a7082005-10-26 10:12:30 +00001457
Marcus Meissner89feaca2005-03-28 10:00:49 +00001458 hres = xbuf_get(&buf, (LPBYTE)&remoteresult, sizeof(DWORD));
1459 if (hres != S_OK)
Robert Shearmanefe606f2005-11-02 20:00:00 +00001460 goto exit;
Hans Leidekkerce66f5d2006-10-12 20:57:10 +02001461 if (relaydeb) TRACE_(olerelay)(") = %08x\n", remoteresult);
Marcus Meissner89feaca2005-03-28 10:00:49 +00001462
Robert Shearmanefe606f2005-11-02 20:00:00 +00001463 hres = remoteresult;
Marcus Meissner89feaca2005-03-28 10:00:49 +00001464
Robert Shearmanefe606f2005-11-02 20:00:00 +00001465exit:
Rob Shearman0fa33bb2006-10-23 11:29:09 +01001466 for (i = 0; i < nrofnames; i++)
1467 SysFreeString(names[i]);
Marcus Meissner0749fc22002-02-05 18:11:17 +00001468 HeapFree(GetProcessHeap(),0,buf.base);
Robert Shearmanefe606f2005-11-02 20:00:00 +00001469 IRpcChannelBuffer_Release(chanbuf);
Robert Shearmanc6296c42005-06-01 11:03:40 +00001470 ITypeInfo_Release(tinfo);
Hans Leidekkerce66f5d2006-10-12 20:57:10 +02001471 TRACE("-- 0x%08x\n", hres);
Robert Shearmanefe606f2005-11-02 20:00:00 +00001472 return hres;
Marcus Meissner0749fc22002-02-05 18:11:17 +00001473}
1474
Alexandre Julliardcfc6c482006-06-10 11:59:33 +02001475static HRESULT WINAPI ProxyIUnknown_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
Robert Shearmanac23f2c2005-03-11 10:18:31 +00001476{
1477 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1478
1479 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1480
1481 if (proxy->outerunknown)
1482 return IUnknown_QueryInterface(proxy->outerunknown, riid, ppv);
1483
1484 FIXME("No interface\n");
1485 return E_NOINTERFACE;
1486}
1487
Alexandre Julliardcfc6c482006-06-10 11:59:33 +02001488static ULONG WINAPI ProxyIUnknown_AddRef(IUnknown *iface)
Robert Shearmanac23f2c2005-03-11 10:18:31 +00001489{
1490 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1491
1492 TRACE("\n");
1493
1494 if (proxy->outerunknown)
1495 return IUnknown_AddRef(proxy->outerunknown);
1496
1497 return 2; /* FIXME */
1498}
1499
Alexandre Julliardcfc6c482006-06-10 11:59:33 +02001500static ULONG WINAPI ProxyIUnknown_Release(IUnknown *iface)
Robert Shearmanac23f2c2005-03-11 10:18:31 +00001501{
1502 TMProxyImpl *proxy = (TMProxyImpl *)iface;
1503
1504 TRACE("\n");
1505
1506 if (proxy->outerunknown)
1507 return IUnknown_Release(proxy->outerunknown);
1508
1509 return 1; /* FIXME */
1510}
1511
Robert Shearman5c2a7082005-10-26 10:12:30 +00001512static HRESULT WINAPI ProxyIDispatch_GetTypeInfoCount(LPDISPATCH iface, UINT * pctinfo)
1513{
1514 TMProxyImpl *This = (TMProxyImpl *)iface;
Robert Shearman5c2a7082005-10-26 10:12:30 +00001515
1516 TRACE("(%p)\n", pctinfo);
1517
Robert Shearmanb0218db2006-02-07 16:26:23 +01001518 return IDispatch_GetTypeInfoCount(This->dispatch, pctinfo);
Robert Shearman5c2a7082005-10-26 10:12:30 +00001519}
1520
1521static HRESULT WINAPI ProxyIDispatch_GetTypeInfo(LPDISPATCH iface, UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo)
1522{
1523 TMProxyImpl *This = (TMProxyImpl *)iface;
Robert Shearman5c2a7082005-10-26 10:12:30 +00001524
Hans Leidekkerce66f5d2006-10-12 20:57:10 +02001525 TRACE("(%d, %x, %p)\n", iTInfo, lcid, ppTInfo);
Robert Shearman5c2a7082005-10-26 10:12:30 +00001526
Robert Shearmanb0218db2006-02-07 16:26:23 +01001527 return IDispatch_GetTypeInfo(This->dispatch, iTInfo, lcid, ppTInfo);
Robert Shearman5c2a7082005-10-26 10:12:30 +00001528}
1529
1530static HRESULT WINAPI ProxyIDispatch_GetIDsOfNames(LPDISPATCH iface, REFIID riid, LPOLESTR * rgszNames, UINT cNames, LCID lcid, DISPID * rgDispId)
1531{
1532 TMProxyImpl *This = (TMProxyImpl *)iface;
Robert Shearman5c2a7082005-10-26 10:12:30 +00001533
Hans Leidekkerce66f5d2006-10-12 20:57:10 +02001534 TRACE("(%s, %p, %d, 0x%x, %p)\n", debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);
Robert Shearman5c2a7082005-10-26 10:12:30 +00001535
Robert Shearmanb0218db2006-02-07 16:26:23 +01001536 return IDispatch_GetIDsOfNames(This->dispatch, riid, rgszNames,
1537 cNames, lcid, rgDispId);
Robert Shearman5c2a7082005-10-26 10:12:30 +00001538}
1539
1540static HRESULT WINAPI ProxyIDispatch_Invoke(LPDISPATCH iface, DISPID dispIdMember, REFIID riid, LCID lcid,
1541 WORD wFlags, DISPPARAMS * pDispParams, VARIANT * pVarResult,
1542 EXCEPINFO * pExcepInfo, UINT * puArgErr)
1543{
1544 TMProxyImpl *This = (TMProxyImpl *)iface;
Robert Shearman5c2a7082005-10-26 10:12:30 +00001545
Hans Leidekkerce66f5d2006-10-12 20:57:10 +02001546 TRACE("(%d, %s, 0x%x, 0x%x, %p, %p, %p, %p)\n", dispIdMember,
Robert Shearmanb0218db2006-02-07 16:26:23 +01001547 debugstr_guid(riid), lcid, wFlags, pDispParams, pVarResult,
1548 pExcepInfo, puArgErr);
Robert Shearman5c2a7082005-10-26 10:12:30 +00001549
Robert Shearmanb0218db2006-02-07 16:26:23 +01001550 return IDispatch_Invoke(This->dispatch, dispIdMember, riid, lcid,
1551 wFlags, pDispParams, pVarResult, pExcepInfo,
1552 puArgErr);
1553}
Robert Shearman5c2a7082005-10-26 10:12:30 +00001554
Rob Shearman30f2b442007-02-06 19:19:56 +00001555typedef struct
1556{
1557 const IRpcChannelBufferVtbl *lpVtbl;
1558 LONG refs;
1559 /* the IDispatch-derived interface we are handling */
1560 IID tmarshal_iid;
1561 IRpcChannelBuffer *pDelegateChannel;
1562} TMarshalDispatchChannel;
1563
1564static HRESULT WINAPI TMarshalDispatchChannel_QueryInterface(LPRPCCHANNELBUFFER iface, REFIID riid, LPVOID *ppv)
1565{
1566 *ppv = NULL;
1567 if (IsEqualIID(riid,&IID_IRpcChannelBuffer) || IsEqualIID(riid,&IID_IUnknown))
1568 {
1569 *ppv = (LPVOID)iface;
1570 IUnknown_AddRef(iface);
1571 return S_OK;
1572 }
1573 return E_NOINTERFACE;
1574}
1575
1576static ULONG WINAPI TMarshalDispatchChannel_AddRef(LPRPCCHANNELBUFFER iface)
1577{
1578 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1579 return InterlockedIncrement(&This->refs);
1580}
1581
1582static ULONG WINAPI TMarshalDispatchChannel_Release(LPRPCCHANNELBUFFER iface)
1583{
1584 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1585 ULONG ref;
1586
1587 ref = InterlockedDecrement(&This->refs);
1588 if (ref)
1589 return ref;
1590
1591 IRpcChannelBuffer_Release(This->pDelegateChannel);
1592 HeapFree(GetProcessHeap(), 0, This);
1593 return 0;
1594}
1595
1596static HRESULT WINAPI TMarshalDispatchChannel_GetBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg, REFIID riid)
1597{
1598 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1599 TRACE("(%p, %s)\n", olemsg, debugstr_guid(riid));
1600 /* Note: we are pretending to invoke a method on the interface identified
1601 * by tmarshal_iid so that we can re-use the IDispatch proxy/stub code
1602 * without the RPC runtime getting confused by not exporting an IDispatch interface */
1603 return IRpcChannelBuffer_GetBuffer(This->pDelegateChannel, olemsg, &This->tmarshal_iid);
1604}
1605
1606static HRESULT WINAPI TMarshalDispatchChannel_SendReceive(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE *olemsg, ULONG *pstatus)
1607{
1608 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1609 TRACE("(%p, %p)\n", olemsg, pstatus);
1610 return IRpcChannelBuffer_SendReceive(This->pDelegateChannel, olemsg, pstatus);
1611}
1612
1613static HRESULT WINAPI TMarshalDispatchChannel_FreeBuffer(LPRPCCHANNELBUFFER iface, RPCOLEMESSAGE* olemsg)
1614{
1615 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1616 TRACE("(%p)\n", olemsg);
1617 return IRpcChannelBuffer_FreeBuffer(This->pDelegateChannel, olemsg);
1618}
1619
1620static HRESULT WINAPI TMarshalDispatchChannel_GetDestCtx(LPRPCCHANNELBUFFER iface, DWORD* pdwDestContext, void** ppvDestContext)
1621{
1622 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1623 TRACE("(%p,%p)\n", pdwDestContext, ppvDestContext);
1624 return IRpcChannelBuffer_GetDestCtx(This->pDelegateChannel, pdwDestContext, ppvDestContext);
1625}
1626
1627static HRESULT WINAPI TMarshalDispatchChannel_IsConnected(LPRPCCHANNELBUFFER iface)
1628{
1629 TMarshalDispatchChannel *This = (TMarshalDispatchChannel *)iface;
1630 TRACE("()\n");
1631 return IRpcChannelBuffer_IsConnected(This->pDelegateChannel);
1632}
1633
1634static const IRpcChannelBufferVtbl TMarshalDispatchChannelVtbl =
1635{
1636 TMarshalDispatchChannel_QueryInterface,
1637 TMarshalDispatchChannel_AddRef,
1638 TMarshalDispatchChannel_Release,
1639 TMarshalDispatchChannel_GetBuffer,
1640 TMarshalDispatchChannel_SendReceive,
1641 TMarshalDispatchChannel_FreeBuffer,
1642 TMarshalDispatchChannel_GetDestCtx,
1643 TMarshalDispatchChannel_IsConnected
1644};
1645
1646static HRESULT TMarshalDispatchChannel_Create(
1647 IRpcChannelBuffer *pDelegateChannel, REFIID tmarshal_riid,
1648 IRpcChannelBuffer **ppChannel)
1649{
1650 TMarshalDispatchChannel *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1651 if (!This)
1652 return E_OUTOFMEMORY;
1653
1654 This->lpVtbl = &TMarshalDispatchChannelVtbl;
1655 This->refs = 1;
1656 IRpcChannelBuffer_AddRef(pDelegateChannel);
1657 This->pDelegateChannel = pDelegateChannel;
1658 This->tmarshal_iid = *tmarshal_riid;
1659
1660 *ppChannel = (IRpcChannelBuffer *)&This->lpVtbl;
1661 return S_OK;
1662}
1663
1664
Robert Shearmanb0218db2006-02-07 16:26:23 +01001665static inline HRESULT get_facbuf_for_iid(REFIID riid, IPSFactoryBuffer **facbuf)
1666{
1667 HRESULT hr;
1668 CLSID clsid;
1669
1670 if ((hr = CoGetPSClsid(riid, &clsid)))
1671 return hr;
1672 return CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL,
1673 &IID_IPSFactoryBuffer, (LPVOID*)facbuf);
Robert Shearman5c2a7082005-10-26 10:12:30 +00001674}
1675
Huw Daviesf1da6722007-05-17 15:23:51 +01001676static HRESULT init_proxy_entry_point(TMProxyImpl *proxy, unsigned int num)
1677{
1678 int j;
1679 /* nrofargs without This */
1680 int nrofargs;
1681 ITypeInfo *tinfo2;
1682 TMAsmProxy *xasm = proxy->asmstubs + num;
1683 HRESULT hres;
1684 const FUNCDESC *fdesc;
1685
1686 hres = get_funcdesc(proxy->tinfo, num, &tinfo2, &fdesc, NULL, NULL, NULL);
1687 if (hres) {
1688 ERR("GetFuncDesc %x should not fail here.\n",hres);
1689 return hres;
1690 }
1691 ITypeInfo_Release(tinfo2);
1692 /* some args take more than 4 byte on the stack */
1693 nrofargs = 0;
1694 for (j=0;j<fdesc->cParams;j++)
1695 nrofargs += _argsize(fdesc->lprgelemdescParam[j].tdesc.vt);
1696
1697#ifdef __i386__
1698 if (fdesc->callconv != CC_STDCALL) {
1699 ERR("calling convention is not stdcall????\n");
1700 return E_FAIL;
1701 }
1702/* popl %eax - return ptr
1703 * pushl <nr>
1704 * pushl %eax
1705 * call xCall
1706 * lret <nr> (+4)
1707 *
1708 *
1709 * arg3 arg2 arg1 <method> <returnptr>
1710 */
1711 xasm->popleax = 0x58;
1712 xasm->pushlval = 0x6a;
1713 xasm->nr = num;
1714 xasm->pushleax = 0x50;
1715 xasm->lcall = 0xe8; /* relative jump */
1716 xasm->xcall = (DWORD)xCall;
1717 xasm->xcall -= (DWORD)&(xasm->lret);
1718 xasm->lret = 0xc2;
1719 xasm->bytestopop = (nrofargs+2)*4; /* pop args, This, iMethod */
1720 proxy->lpvtbl[num] = xasm;
1721#else
1722 FIXME("not implemented on non i386\n");
1723 return E_FAIL;
1724#endif
1725 return S_OK;
1726}
1727
Marcus Meissner0749fc22002-02-05 18:11:17 +00001728static HRESULT WINAPI
1729PSFacBuf_CreateProxy(
1730 LPPSFACTORYBUFFER iface, IUnknown* pUnkOuter, REFIID riid,
Robert Shearman2a54aac2004-09-07 20:42:26 +00001731 IRpcProxyBuffer **ppProxy, LPVOID *ppv)
1732{
Marcus Meissner0749fc22002-02-05 18:11:17 +00001733 HRESULT hres;
1734 ITypeInfo *tinfo;
Huw Daviesc24a53d2007-05-17 15:29:41 +01001735 unsigned int i, nroffuncs;
Marcus Meissner0749fc22002-02-05 18:11:17 +00001736 TMProxyImpl *proxy;
Robert Shearman5c2a7082005-10-26 10:12:30 +00001737 TYPEATTR *typeattr;
Huw Daviesc24a53d2007-05-17 15:29:41 +01001738 BOOL defer_to_dispatch = FALSE;
Vincent Béron9a624912002-05-31 23:06:46 +00001739
Marcus Meissner0749fc22002-02-05 18:11:17 +00001740 TRACE("(...%s...)\n",debugstr_guid(riid));
1741 hres = _get_typeinfo_for_iid(riid,&tinfo);
1742 if (hres) {
Mike Hearn64eac752005-01-27 11:11:32 +00001743 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
Marcus Meissner0749fc22002-02-05 18:11:17 +00001744 return hres;
1745 }
Huw Daviesc24a53d2007-05-17 15:29:41 +01001746
1747 hres = num_of_funcs(tinfo, &nroffuncs);
1748 if (FAILED(hres)) {
1749 ERR("Cannot get number of functions for typeinfo %s\n",debugstr_guid(riid));
1750 ITypeInfo_Release(tinfo);
1751 return hres;
1752 }
1753
Robert Shearman2a54aac2004-09-07 20:42:26 +00001754 proxy = CoTaskMemAlloc(sizeof(TMProxyImpl));
Marcus Meissner0749fc22002-02-05 18:11:17 +00001755 if (!proxy) return E_OUTOFMEMORY;
Marcus Meissner0749fc22002-02-05 18:11:17 +00001756
1757 assert(sizeof(TMAsmProxy) == 12);
1758
Robert Shearman5c2a7082005-10-26 10:12:30 +00001759 proxy->dispatch = NULL;
Robert Shearmanb0218db2006-02-07 16:26:23 +01001760 proxy->dispatch_proxy = NULL;
Robert Shearmanac23f2c2005-03-11 10:18:31 +00001761 proxy->outerunknown = pUnkOuter;
Mike Hearn3186bd22004-06-28 23:51:24 +00001762 proxy->asmstubs = VirtualAlloc(NULL, sizeof(TMAsmProxy) * nroffuncs, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
1763 if (!proxy->asmstubs) {
1764 ERR("Could not commit pages for proxy thunks\n");
Robert Shearman2a54aac2004-09-07 20:42:26 +00001765 CoTaskMemFree(proxy);
Mike Hearn3186bd22004-06-28 23:51:24 +00001766 return E_OUTOFMEMORY;
1767 }
Rob Shearmanecfe54d2007-01-02 15:39:48 +00001768 proxy->lpvtbl2 = &tmproxyvtable;
1769 /* one reference for the proxy */
1770 proxy->ref = 1;
1771 proxy->tinfo = tinfo;
1772 memcpy(&proxy->iid,riid,sizeof(*riid));
1773 proxy->chanbuf = 0;
Mike Hearn3186bd22004-06-28 23:51:24 +00001774
Robert Shearman2a54aac2004-09-07 20:42:26 +00001775 InitializeCriticalSection(&proxy->crit);
Jan Zerebecki1e176b42007-03-10 22:09:58 +01001776 proxy->crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TMProxyImpl.crit");
Robert Shearman2a54aac2004-09-07 20:42:26 +00001777
Marcus Meissner0749fc22002-02-05 18:11:17 +00001778 proxy->lpvtbl = HeapAlloc(GetProcessHeap(),0,sizeof(LPBYTE)*nroffuncs);
Robert Shearman5c2a7082005-10-26 10:12:30 +00001779
1780 /* if we derive from IDispatch then defer to its proxy for its methods */
1781 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
1782 if (hres == S_OK)
1783 {
1784 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
1785 {
Robert Shearmanb0218db2006-02-07 16:26:23 +01001786 IPSFactoryBuffer *factory_buffer;
1787 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
1788 if (hres == S_OK)
1789 {
1790 hres = IPSFactoryBuffer_CreateProxy(factory_buffer, NULL,
1791 &IID_IDispatch, &proxy->dispatch_proxy,
1792 (void **)&proxy->dispatch);
1793 IPSFactoryBuffer_Release(factory_buffer);
1794 }
Rob Shearmanecfe54d2007-01-02 15:39:48 +00001795 if ((hres == S_OK) && (nroffuncs < 7))
1796 {
1797 ERR("nroffuncs calculated incorrectly (%d)\n", nroffuncs);
1798 hres = E_UNEXPECTED;
1799 }
Robert Shearmanb0218db2006-02-07 16:26:23 +01001800 if (hres == S_OK)
1801 {
Huw Daviesc24a53d2007-05-17 15:29:41 +01001802 defer_to_dispatch = TRUE;
Robert Shearmanb0218db2006-02-07 16:26:23 +01001803 }
Robert Shearman5c2a7082005-10-26 10:12:30 +00001804 }
1805 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
1806 }
1807
Huw Daviesc24a53d2007-05-17 15:29:41 +01001808 for (i=0;i<nroffuncs;i++) {
1809 switch (i) {
1810 case 0:
1811 proxy->lpvtbl[i] = ProxyIUnknown_QueryInterface;
1812 break;
1813 case 1:
1814 proxy->lpvtbl[i] = ProxyIUnknown_AddRef;
1815 break;
1816 case 2:
1817 proxy->lpvtbl[i] = ProxyIUnknown_Release;
1818 break;
1819 case 3:
1820 if(!defer_to_dispatch)
1821 {
1822 hres = init_proxy_entry_point(proxy, i);
1823 if(FAILED(hres)) return hres;
1824 }
1825 else proxy->lpvtbl[3] = ProxyIDispatch_GetTypeInfoCount;
1826 break;
1827 case 4:
1828 if(!defer_to_dispatch)
1829 {
1830 hres = init_proxy_entry_point(proxy, i);
1831 if(FAILED(hres)) return hres;
1832 }
1833 else proxy->lpvtbl[4] = ProxyIDispatch_GetTypeInfo;
1834 break;
1835 case 5:
1836 if(!defer_to_dispatch)
1837 {
1838 hres = init_proxy_entry_point(proxy, i);
1839 if(FAILED(hres)) return hres;
1840 }
1841 else proxy->lpvtbl[5] = ProxyIDispatch_GetIDsOfNames;
1842 break;
1843 case 6:
1844 if(!defer_to_dispatch)
1845 {
1846 hres = init_proxy_entry_point(proxy, i);
1847 if(FAILED(hres)) return hres;
1848 }
1849 else proxy->lpvtbl[6] = ProxyIDispatch_Invoke;
1850 break;
1851 default:
1852 hres = init_proxy_entry_point(proxy, i);
1853 if(FAILED(hres)) return hres;
1854 }
1855 }
1856
Robert Shearmanb0218db2006-02-07 16:26:23 +01001857 if (hres == S_OK)
1858 {
1859 *ppv = (LPVOID)proxy;
1860 *ppProxy = (IRpcProxyBuffer *)&(proxy->lpvtbl2);
1861 IUnknown_AddRef((IUnknown *)*ppv);
1862 return S_OK;
1863 }
1864 else
1865 TMProxyImpl_Release((IRpcProxyBuffer *)&proxy->lpvtbl2);
1866 return hres;
Marcus Meissner0749fc22002-02-05 18:11:17 +00001867}
1868
1869typedef struct _TMStubImpl {
Dmitry Timoshkoveba47f12005-06-06 19:50:35 +00001870 const IRpcStubBufferVtbl *lpvtbl;
Mike McCormackfc200d02005-07-11 10:25:19 +00001871 LONG ref;
Marcus Meissner0749fc22002-02-05 18:11:17 +00001872
1873 LPUNKNOWN pUnk;
1874 ITypeInfo *tinfo;
1875 IID iid;
Robert Shearmanb0218db2006-02-07 16:26:23 +01001876 IRpcStubBuffer *dispatch_stub;
Robert Shearman4d0b1e32006-08-29 21:12:16 +01001877 BOOL dispatch_derivative;
Marcus Meissner0749fc22002-02-05 18:11:17 +00001878} TMStubImpl;
1879
1880static HRESULT WINAPI
Robert Shearman2a54aac2004-09-07 20:42:26 +00001881TMStubImpl_QueryInterface(LPRPCSTUBBUFFER iface, REFIID riid, LPVOID *ppv)
1882{
Marcus Meissner0749fc22002-02-05 18:11:17 +00001883 if (IsEqualIID(riid,&IID_IRpcStubBuffer)||IsEqualIID(riid,&IID_IUnknown)){
1884 *ppv = (LPVOID)iface;
1885 IRpcStubBuffer_AddRef(iface);
1886 return S_OK;
1887 }
1888 FIXME("%s, not supported IID.\n",debugstr_guid(riid));
1889 return E_NOINTERFACE;
1890}
1891
1892static ULONG WINAPI
Robert Shearman2a54aac2004-09-07 20:42:26 +00001893TMStubImpl_AddRef(LPRPCSTUBBUFFER iface)
1894{
Alexandre Julliard241a4c32004-09-09 21:03:58 +00001895 TMStubImpl *This = (TMStubImpl *)iface;
James Hawkinsba92d292005-01-14 16:02:20 +00001896 ULONG refCount = InterlockedIncrement(&This->ref);
Mike Hearn6545d992004-07-30 18:43:27 +00001897
Hans Leidekkerce66f5d2006-10-12 20:57:10 +02001898 TRACE("(%p)->(ref before=%u)\n", This, refCount - 1);
Marcus Meissner0749fc22002-02-05 18:11:17 +00001899
James Hawkinsba92d292005-01-14 16:02:20 +00001900 return refCount;
Marcus Meissner0749fc22002-02-05 18:11:17 +00001901}
1902
1903static ULONG WINAPI
Robert Shearman2a54aac2004-09-07 20:42:26 +00001904TMStubImpl_Release(LPRPCSTUBBUFFER iface)
1905{
Alexandre Julliard241a4c32004-09-09 21:03:58 +00001906 TMStubImpl *This = (TMStubImpl *)iface;
James Hawkinsba92d292005-01-14 16:02:20 +00001907 ULONG refCount = InterlockedDecrement(&This->ref);
Marcus Meissner0749fc22002-02-05 18:11:17 +00001908
Hans Leidekkerce66f5d2006-10-12 20:57:10 +02001909 TRACE("(%p)->(ref before=%u)\n", This, refCount + 1);
Mike Hearn6545d992004-07-30 18:43:27 +00001910
James Hawkinsba92d292005-01-14 16:02:20 +00001911 if (!refCount)
Robert Shearman2a54aac2004-09-07 20:42:26 +00001912 {
1913 IRpcStubBuffer_Disconnect(iface);
Robert Shearman6bfae1a2005-07-19 20:00:07 +00001914 ITypeInfo_Release(This->tinfo);
Robert Shearman4d0b1e32006-08-29 21:12:16 +01001915 if (This->dispatch_stub)
1916 IRpcStubBuffer_Release(This->dispatch_stub);
Robert Shearman2a54aac2004-09-07 20:42:26 +00001917 CoTaskMemFree(This);
1918 }
James Hawkinsba92d292005-01-14 16:02:20 +00001919 return refCount;
Marcus Meissner0749fc22002-02-05 18:11:17 +00001920}
1921
1922static HRESULT WINAPI
Robert Shearman2a54aac2004-09-07 20:42:26 +00001923TMStubImpl_Connect(LPRPCSTUBBUFFER iface, LPUNKNOWN pUnkServer)
1924{
Alexandre Julliard241a4c32004-09-09 21:03:58 +00001925 TMStubImpl *This = (TMStubImpl *)iface;
Marcus Meissner0749fc22002-02-05 18:11:17 +00001926
Mike Hearn6545d992004-07-30 18:43:27 +00001927 TRACE("(%p)->(%p)\n", This, pUnkServer);
1928
Marcus Meissner0749fc22002-02-05 18:11:17 +00001929 IUnknown_AddRef(pUnkServer);
1930 This->pUnk = pUnkServer;
Robert Shearmanb0218db2006-02-07 16:26:23 +01001931
1932 if (This->dispatch_stub)
1933 IRpcStubBuffer_Connect(This->dispatch_stub, pUnkServer);
1934
Marcus Meissner0749fc22002-02-05 18:11:17 +00001935 return S_OK;
1936}
1937
1938static void WINAPI
Robert Shearman2a54aac2004-09-07 20:42:26 +00001939TMStubImpl_Disconnect(LPRPCSTUBBUFFER iface)
1940{
Alexandre Julliard241a4c32004-09-09 21:03:58 +00001941 TMStubImpl *This = (TMStubImpl *)iface;
Marcus Meissner0749fc22002-02-05 18:11:17 +00001942
Mike Hearn6545d992004-07-30 18:43:27 +00001943 TRACE("(%p)->()\n", This);
1944
Robert Shearman6bfae1a2005-07-19 20:00:07 +00001945 if (This->pUnk)
1946 {
1947 IUnknown_Release(This->pUnk);
1948 This->pUnk = NULL;
1949 }
Robert Shearmanb0218db2006-02-07 16:26:23 +01001950
1951 if (This->dispatch_stub)
1952 IRpcStubBuffer_Disconnect(This->dispatch_stub);
Marcus Meissner0749fc22002-02-05 18:11:17 +00001953}
1954
Marcus Meissner0749fc22002-02-05 18:11:17 +00001955static HRESULT WINAPI
1956TMStubImpl_Invoke(
Robert Shearman2a54aac2004-09-07 20:42:26 +00001957 LPRPCSTUBBUFFER iface, RPCOLEMESSAGE* xmsg,IRpcChannelBuffer*rpcchanbuf)
1958{
Marcus Meissner0749fc22002-02-05 18:11:17 +00001959 int i;
Robert Shearmanb0d8e06c2005-12-06 21:24:19 +01001960 const FUNCDESC *fdesc;
Alexandre Julliard241a4c32004-09-09 21:03:58 +00001961 TMStubImpl *This = (TMStubImpl *)iface;
Marcus Meissner0749fc22002-02-05 18:11:17 +00001962 HRESULT hres;
Rob Shearmand1b15c82006-10-23 11:38:20 +01001963 DWORD *args = NULL, res, *xargs, nrofargs;
Marcus Meissner0749fc22002-02-05 18:11:17 +00001964 marshal_state buf;
James Hawkinsc8eb8072007-01-16 04:15:20 -06001965 UINT nrofnames = 0;
Marcus Meissner0749fc22002-02-05 18:11:17 +00001966 BSTR names[10];
Robert Shearman5c2a7082005-10-26 10:12:30 +00001967 BSTR iname = NULL;
Rob Shearmand1b15c82006-10-23 11:38:20 +01001968 ITypeInfo *tinfo = NULL;
Marcus Meissner0749fc22002-02-05 18:11:17 +00001969
Marcus Meissner0749fc22002-02-05 18:11:17 +00001970 TRACE("...\n");
Marcus Meissner0749fc22002-02-05 18:11:17 +00001971
Robert Shearman5c2a7082005-10-26 10:12:30 +00001972 if (xmsg->iMethod < 3) {
1973 ERR("IUnknown methods cannot be marshaled by the typelib marshaler\n");
1974 return E_UNEXPECTED;
Marcus Meissner0749fc22002-02-05 18:11:17 +00001975 }
Robert Shearman5c2a7082005-10-26 10:12:30 +00001976
Robert Shearman4d0b1e32006-08-29 21:12:16 +01001977 if (This->dispatch_derivative && xmsg->iMethod < sizeof(IDispatchVtbl)/sizeof(void *))
1978 {
1979 IPSFactoryBuffer *factory_buffer;
1980 hres = get_facbuf_for_iid(&IID_IDispatch, &factory_buffer);
1981 if (hres == S_OK)
1982 {
1983 hres = IPSFactoryBuffer_CreateStub(factory_buffer, &IID_IDispatch,
1984 This->pUnk, &This->dispatch_stub);
1985 IPSFactoryBuffer_Release(factory_buffer);
1986 }
1987 if (hres != S_OK)
1988 return hres;
Robert Shearmanb0218db2006-02-07 16:26:23 +01001989 return IRpcStubBuffer_Invoke(This->dispatch_stub, xmsg, rpcchanbuf);
Robert Shearman4d0b1e32006-08-29 21:12:16 +01001990 }
Robert Shearmanb0218db2006-02-07 16:26:23 +01001991
1992 memset(&buf,0,sizeof(buf));
1993 buf.size = xmsg->cbBuffer;
1994 buf.base = HeapAlloc(GetProcessHeap(), 0, xmsg->cbBuffer);
1995 memcpy(buf.base, xmsg->Buffer, xmsg->cbBuffer);
1996 buf.curoff = 0;
1997
Huw Davies294b3eb2007-04-26 11:56:44 +01001998 hres = get_funcdesc(This->tinfo,xmsg->iMethod,&tinfo,&fdesc,&iname,NULL,NULL);
Marcus Meissner0749fc22002-02-05 18:11:17 +00001999 if (hres) {
Hans Leidekkerce66f5d2006-10-12 20:57:10 +02002000 ERR("GetFuncDesc on method %d failed with %x\n",xmsg->iMethod,hres);
Marcus Meissner0749fc22002-02-05 18:11:17 +00002001 return hres;
2002 }
Marcus Meissnere6260a32005-03-27 17:51:59 +00002003
Robert Shearman5c2a7082005-10-26 10:12:30 +00002004 if (iname && !lstrcmpW(iname, IDispatchW))
2005 {
2006 ERR("IDispatch cannot be marshaled by the typelib marshaler\n");
Rob Shearmand1b15c82006-10-23 11:38:20 +01002007 hres = E_UNEXPECTED;
2008 SysFreeString (iname);
2009 goto exit;
Robert Shearman5c2a7082005-10-26 10:12:30 +00002010 }
Marcus Meissnere6260a32005-03-27 17:51:59 +00002011
2012 if (iname) SysFreeString (iname);
Marcus Meissnere6260a32005-03-27 17:51:59 +00002013
Marcus Meissner0749fc22002-02-05 18:11:17 +00002014 /* Need them for hack below */
2015 memset(names,0,sizeof(names));
Robert Shearmanc6296c42005-06-01 11:03:40 +00002016 ITypeInfo_GetNames(tinfo,fdesc->memid,names,sizeof(names)/sizeof(names[0]),&nrofnames);
Marcus Meissner0749fc22002-02-05 18:11:17 +00002017 if (nrofnames > sizeof(names)/sizeof(names[0])) {
2018 ERR("Need more names!\n");
2019 }
2020
2021 /*dump_FUNCDESC(fdesc);*/
2022 nrofargs = 0;
2023 for (i=0;i<fdesc->cParams;i++)
2024 nrofargs += _argsize(fdesc->lprgelemdescParam[i].tdesc.vt);
2025 args = HeapAlloc(GetProcessHeap(),0,(nrofargs+1)*sizeof(DWORD));
Rob Shearmand1b15c82006-10-23 11:38:20 +01002026 if (!args)
2027 {
2028 hres = E_OUTOFMEMORY;
2029 goto exit;
2030 }
Marcus Meissner0749fc22002-02-05 18:11:17 +00002031
2032 /* Allocate all stuff used by call. */
2033 xargs = args+1;
2034 for (i=0;i<fdesc->cParams;i++) {
2035 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
2036
Robert Shearman5c2a7082005-10-26 10:12:30 +00002037 hres = deserialize_param(
2038 tinfo,
Rob Shearman0f45b2b2007-02-07 19:19:26 +00002039 is_in_elem(elem),
Robert Shearman5c2a7082005-10-26 10:12:30 +00002040 FALSE,
2041 TRUE,
2042 &(elem->tdesc),
2043 xargs,
2044 &buf
2045 );
Marcus Meissner0749fc22002-02-05 18:11:17 +00002046 xargs += _argsize(elem->tdesc.vt);
2047 if (hres) {
Hans Leidekkerce66f5d2006-10-12 20:57:10 +02002048 ERR("Failed to deserialize param %s, hres %x\n",relaystr(names[i+1]),hres);
Marcus Meissner0749fc22002-02-05 18:11:17 +00002049 break;
2050 }
2051 }
Robert Shearman5c2a7082005-10-26 10:12:30 +00002052
Robert Shearman69562b02005-10-27 12:08:58 +00002053 args[0] = (DWORD)This->pUnk;
Robert Shearmana05672e2006-07-17 20:19:27 +01002054
2055 __TRY
2056 {
2057 res = _invoke(
2058 (*((FARPROC**)args[0]))[fdesc->oVft/4],
2059 fdesc->callconv,
2060 (xargs-args),
2061 args
2062 );
2063 }
2064 __EXCEPT(NULL)
2065 {
2066 DWORD dwExceptionCode = GetExceptionCode();
Hans Leidekkerce66f5d2006-10-12 20:57:10 +02002067 ERR("invoke call failed with exception 0x%08x (%d)\n", dwExceptionCode, dwExceptionCode);
Robert Shearmana05672e2006-07-17 20:19:27 +01002068 if (FAILED(dwExceptionCode))
2069 hres = dwExceptionCode;
2070 else
2071 hres = HRESULT_FROM_WIN32(dwExceptionCode);
2072 }
2073 __ENDTRY
2074
2075 if (hres != S_OK)
Rob Shearmand1b15c82006-10-23 11:38:20 +01002076 goto exit;
Robert Shearmana05672e2006-07-17 20:19:27 +01002077
Marcus Meissner0749fc22002-02-05 18:11:17 +00002078 buf.curoff = 0;
Marcus Meissnere6260a32005-03-27 17:51:59 +00002079
Marcus Meissner0749fc22002-02-05 18:11:17 +00002080 xargs = args+1;
2081 for (i=0;i<fdesc->cParams;i++) {
2082 ELEMDESC *elem = fdesc->lprgelemdescParam+i;
Robert Shearman5c2a7082005-10-26 10:12:30 +00002083 hres = serialize_param(
2084 tinfo,
Rob Shearman0f45b2b2007-02-07 19:19:26 +00002085 is_out_elem(elem),
Robert Shearman5c2a7082005-10-26 10:12:30 +00002086 FALSE,
2087 TRUE,
2088 &elem->tdesc,
2089 xargs,
2090 &buf
2091 );
Marcus Meissner0749fc22002-02-05 18:11:17 +00002092 xargs += _argsize(elem->tdesc.vt);
2093 if (hres) {
Hans Leidekkerce66f5d2006-10-12 20:57:10 +02002094 ERR("Failed to stuballoc param, hres %x\n",hres);
Marcus Meissner0749fc22002-02-05 18:11:17 +00002095 break;
2096 }
2097 }
Robert Shearman5c2a7082005-10-26 10:12:30 +00002098
Marcus Meissner89feaca2005-03-28 10:00:49 +00002099 hres = xbuf_add (&buf, (LPBYTE)&res, sizeof(DWORD));
Rob Shearman0fa33bb2006-10-23 11:29:09 +01002100
Marcus Meissner89feaca2005-03-28 10:00:49 +00002101 if (hres != S_OK)
Rob Shearmand1b15c82006-10-23 11:38:20 +01002102 goto exit;
Robert Shearman44d6e392005-08-29 21:46:51 +00002103
Marcus Meissner0749fc22002-02-05 18:11:17 +00002104 xmsg->cbBuffer = buf.curoff;
Robert Shearman70c34cc2006-06-09 17:25:50 +01002105 hres = IRpcChannelBuffer_GetBuffer(rpcchanbuf, xmsg, &This->iid);
2106 if (hres != S_OK)
Hans Leidekkerce66f5d2006-10-12 20:57:10 +02002107 ERR("IRpcChannelBuffer_GetBuffer failed with error 0x%08x\n", hres);
Robert Shearman44d6e392005-08-29 21:46:51 +00002108
2109 if (hres == S_OK)
2110 memcpy(xmsg->Buffer, buf.base, buf.curoff);
2111
Rob Shearmand1b15c82006-10-23 11:38:20 +01002112exit:
2113 for (i = 0; i < nrofnames; i++)
2114 SysFreeString(names[i]);
2115
2116 ITypeInfo_Release(tinfo);
2117 HeapFree(GetProcessHeap(), 0, args);
2118
Robert Shearman44d6e392005-08-29 21:46:51 +00002119 HeapFree(GetProcessHeap(), 0, buf.base);
2120
2121 TRACE("returning\n");
2122 return hres;
Marcus Meissner0749fc22002-02-05 18:11:17 +00002123}
2124
2125static LPRPCSTUBBUFFER WINAPI
2126TMStubImpl_IsIIDSupported(LPRPCSTUBBUFFER iface, REFIID riid) {
2127 FIXME("Huh (%s)?\n",debugstr_guid(riid));
2128 return NULL;
2129}
2130
2131static ULONG WINAPI
2132TMStubImpl_CountRefs(LPRPCSTUBBUFFER iface) {
Alexandre Julliard241a4c32004-09-09 21:03:58 +00002133 TMStubImpl *This = (TMStubImpl *)iface;
Marcus Meissner0749fc22002-02-05 18:11:17 +00002134
Robert Shearman44d6e392005-08-29 21:46:51 +00002135 FIXME("()\n");
Marcus Meissner0749fc22002-02-05 18:11:17 +00002136 return This->ref; /*FIXME? */
2137}
2138
2139static HRESULT WINAPI
2140TMStubImpl_DebugServerQueryInterface(LPRPCSTUBBUFFER iface, LPVOID *ppv) {
2141 return E_NOTIMPL;
2142}
2143
2144static void WINAPI
2145TMStubImpl_DebugServerRelease(LPRPCSTUBBUFFER iface, LPVOID ppv) {
2146 return;
2147}
2148
Dmitry Timoshkoveba47f12005-06-06 19:50:35 +00002149static const IRpcStubBufferVtbl tmstubvtbl = {
Marcus Meissner0749fc22002-02-05 18:11:17 +00002150 TMStubImpl_QueryInterface,
2151 TMStubImpl_AddRef,
2152 TMStubImpl_Release,
2153 TMStubImpl_Connect,
2154 TMStubImpl_Disconnect,
2155 TMStubImpl_Invoke,
2156 TMStubImpl_IsIIDSupported,
2157 TMStubImpl_CountRefs,
2158 TMStubImpl_DebugServerQueryInterface,
2159 TMStubImpl_DebugServerRelease
2160};
2161
2162static HRESULT WINAPI
2163PSFacBuf_CreateStub(
2164 LPPSFACTORYBUFFER iface, REFIID riid,IUnknown *pUnkServer,
2165 IRpcStubBuffer** ppStub
2166) {
2167 HRESULT hres;
2168 ITypeInfo *tinfo;
2169 TMStubImpl *stub;
Robert Shearmanb0218db2006-02-07 16:26:23 +01002170 TYPEATTR *typeattr;
Marcus Meissner0749fc22002-02-05 18:11:17 +00002171
2172 TRACE("(%s,%p,%p)\n",debugstr_guid(riid),pUnkServer,ppStub);
Robert Shearmanb0218db2006-02-07 16:26:23 +01002173
Marcus Meissner0749fc22002-02-05 18:11:17 +00002174 hres = _get_typeinfo_for_iid(riid,&tinfo);
2175 if (hres) {
Mike Hearn64eac752005-01-27 11:11:32 +00002176 ERR("No typeinfo for %s?\n",debugstr_guid(riid));
Marcus Meissner0749fc22002-02-05 18:11:17 +00002177 return hres;
2178 }
Robert Shearmanb0218db2006-02-07 16:26:23 +01002179
Robert Shearman2a54aac2004-09-07 20:42:26 +00002180 stub = CoTaskMemAlloc(sizeof(TMStubImpl));
Marcus Meissner0749fc22002-02-05 18:11:17 +00002181 if (!stub)
2182 return E_OUTOFMEMORY;
2183 stub->lpvtbl = &tmstubvtbl;
2184 stub->ref = 1;
2185 stub->tinfo = tinfo;
Robert Shearmanb0218db2006-02-07 16:26:23 +01002186 stub->dispatch_stub = NULL;
Robert Shearman4d0b1e32006-08-29 21:12:16 +01002187 stub->dispatch_derivative = FALSE;
Marcus Meissner0749fc22002-02-05 18:11:17 +00002188 memcpy(&(stub->iid),riid,sizeof(*riid));
2189 hres = IRpcStubBuffer_Connect((LPRPCSTUBBUFFER)stub,pUnkServer);
2190 *ppStub = (LPRPCSTUBBUFFER)stub;
Mike Hearn6545d992004-07-30 18:43:27 +00002191 TRACE("IRpcStubBuffer: %p\n", stub);
Marcus Meissner0749fc22002-02-05 18:11:17 +00002192 if (hres)
Mike Hearn64eac752005-01-27 11:11:32 +00002193 ERR("Connect to pUnkServer failed?\n");
Robert Shearmanb0218db2006-02-07 16:26:23 +01002194
2195 /* if we derive from IDispatch then defer to its stub for some of its methods */
2196 hres = ITypeInfo_GetTypeAttr(tinfo, &typeattr);
2197 if (hres == S_OK)
2198 {
2199 if (typeattr->wTypeFlags & TYPEFLAG_FDISPATCHABLE)
Robert Shearman4d0b1e32006-08-29 21:12:16 +01002200 stub->dispatch_derivative = TRUE;
Robert Shearmanb0218db2006-02-07 16:26:23 +01002201 ITypeInfo_ReleaseTypeAttr(tinfo, typeattr);
2202 }
2203
Marcus Meissner0749fc22002-02-05 18:11:17 +00002204 return hres;
2205}
2206
Dmitry Timoshkoveba47f12005-06-06 19:50:35 +00002207static const IPSFactoryBufferVtbl psfacbufvtbl = {
Marcus Meissner0749fc22002-02-05 18:11:17 +00002208 PSFacBuf_QueryInterface,
2209 PSFacBuf_AddRef,
2210 PSFacBuf_Release,
2211 PSFacBuf_CreateProxy,
2212 PSFacBuf_CreateStub
2213};
2214
2215/* This is the whole PSFactoryBuffer object, just the vtableptr */
Dmitry Timoshkoveba47f12005-06-06 19:50:35 +00002216static const IPSFactoryBufferVtbl *lppsfac = &psfacbufvtbl;
Marcus Meissner0749fc22002-02-05 18:11:17 +00002217
2218/***********************************************************************
Alexandre Julliardb8d0f4c2005-08-09 10:38:54 +00002219 * TMARSHAL_DllGetClassObject
Marcus Meissner0749fc22002-02-05 18:11:17 +00002220 */
Alexandre Julliardb8d0f4c2005-08-09 10:38:54 +00002221HRESULT TMARSHAL_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv)
Marcus Meissner0749fc22002-02-05 18:11:17 +00002222{
2223 if (IsEqualIID(iid,&IID_IPSFactoryBuffer)) {
2224 *ppv = &lppsfac;
2225 return S_OK;
2226 }
2227 return E_NOINTERFACE;
2228}