mpr: Don't succeed if drive is not remote in WNetGetUniversalName.
[wine/multimedia.git] / dlls / mshtml / dispex.c
blob66dad69fb5dd4f7ef573f16aa55aab60b6801a1b
1 /*
2 * Copyright 2008-2009 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <stdarg.h>
20 #include <assert.h>
22 #define COBJMACROS
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winuser.h"
27 #include "ole2.h"
29 #include "wine/debug.h"
31 #include "mshtml_private.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
35 static CRITICAL_SECTION cs_dispex_static_data;
36 static CRITICAL_SECTION_DEBUG cs_dispex_static_data_dbg =
38 0, 0, &cs_dispex_static_data,
39 { &cs_dispex_static_data_dbg.ProcessLocksList, &cs_dispex_static_data_dbg.ProcessLocksList },
40 0, 0, { (DWORD_PTR)(__FILE__ ": dispex_static_data") }
42 static CRITICAL_SECTION cs_dispex_static_data = { &cs_dispex_static_data_dbg, -1, 0, 0, 0, 0 };
45 static const WCHAR objectW[] = {'[','o','b','j','e','c','t',']',0};
47 typedef struct {
48 DISPID id;
49 BSTR name;
50 tid_t tid;
51 SHORT put_vtbl_off;
52 SHORT get_vtbl_off;
53 SHORT func_disp_idx;
54 VARTYPE prop_vt;
55 } func_info_t;
57 struct dispex_data_t {
58 DWORD func_cnt;
59 func_info_t *funcs;
60 func_info_t **name_table;
61 DWORD func_disp_cnt;
63 struct list entry;
66 typedef struct {
67 VARIANT var;
68 LPWSTR name;
69 DWORD flags;
70 } dynamic_prop_t;
72 #define DYNPROP_DELETED 0x01
74 typedef struct {
75 DispatchEx dispex;
76 IUnknown IUnknown_iface;
77 DispatchEx *obj;
78 func_info_t *info;
79 } func_disp_t;
81 struct dispex_dynamic_data_t {
82 DWORD buf_size;
83 DWORD prop_cnt;
84 dynamic_prop_t *props;
85 func_disp_t **func_disps;
88 #define DISPID_DYNPROP_0 0x50000000
89 #define DISPID_DYNPROP_MAX 0x5fffffff
91 #define FDEX_VERSION_MASK 0xf0000000
93 static ITypeLib *typelib;
94 static ITypeInfo *typeinfos[LAST_tid];
95 static struct list dispex_data_list = LIST_INIT(dispex_data_list);
97 static REFIID tid_ids[] = {
98 #define XIID(iface) &IID_ ## iface,
99 #define XDIID(iface) &DIID_ ## iface,
100 TID_LIST
101 #undef XIID
102 #undef XDIID
105 static HRESULT load_typelib(void)
107 HRESULT hres;
108 ITypeLib *tl;
110 hres = LoadRegTypeLib(&LIBID_MSHTML, 4, 0, LOCALE_SYSTEM_DEFAULT, &tl);
111 if(FAILED(hres)) {
112 ERR("LoadRegTypeLib failed: %08x\n", hres);
113 return hres;
116 if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
117 ITypeLib_Release(tl);
118 return hres;
121 static HRESULT get_typeinfo(tid_t tid, ITypeInfo **typeinfo)
123 HRESULT hres;
125 if (!typelib)
126 hres = load_typelib();
127 if (!typelib)
128 return hres;
130 if(!typeinfos[tid]) {
131 ITypeInfo *ti;
133 hres = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &ti);
134 if(FAILED(hres)) {
135 ERR("GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_guid(tid_ids[tid]), hres);
136 return hres;
139 if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), ti, NULL))
140 ITypeInfo_Release(ti);
143 *typeinfo = typeinfos[tid];
144 return S_OK;
147 void release_typelib(void)
149 dispex_data_t *iter;
150 unsigned i;
152 while(!list_empty(&dispex_data_list)) {
153 iter = LIST_ENTRY(list_head(&dispex_data_list), dispex_data_t, entry);
154 list_remove(&iter->entry);
156 for(i=0; i < iter->func_cnt; i++)
157 SysFreeString(iter->funcs[i].name);
159 heap_free(iter->funcs);
160 heap_free(iter->name_table);
161 heap_free(iter);
164 if(!typelib)
165 return;
167 for(i=0; i < sizeof(typeinfos)/sizeof(*typeinfos); i++)
168 if(typeinfos[i])
169 ITypeInfo_Release(typeinfos[i]);
171 ITypeLib_Release(typelib);
172 DeleteCriticalSection(&cs_dispex_static_data);
175 HRESULT get_htmldoc_classinfo(ITypeInfo **typeinfo)
177 HRESULT hres;
179 if (!typelib)
180 hres = load_typelib();
181 if (!typelib)
182 return hres;
184 hres = ITypeLib_GetTypeInfoOfGuid(typelib, &CLSID_HTMLDocument, typeinfo);
185 if(FAILED(hres))
186 ERR("GetTypeInfoOfGuid failed: %08x\n", hres);
187 return hres;
190 static void add_func_info(dispex_data_t *data, DWORD *size, tid_t tid, const FUNCDESC *desc, ITypeInfo *dti)
192 func_info_t *info;
193 HRESULT hres;
195 if(data->func_cnt && data->funcs[data->func_cnt-1].id == desc->memid) {
196 info = data->funcs+data->func_cnt-1;
197 }else {
198 if(data->func_cnt == *size)
199 data->funcs = heap_realloc(data->funcs, (*size <<= 1)*sizeof(func_info_t));
201 info = data->funcs+data->func_cnt;
202 hres = ITypeInfo_GetDocumentation(dti, desc->memid, &info->name, NULL, NULL, NULL);
203 if(FAILED(hres))
204 return;
206 data->func_cnt++;
208 info->id = desc->memid;
209 info->tid = tid;
210 info->func_disp_idx = -1;
211 info->prop_vt = VT_EMPTY;
212 info->put_vtbl_off = 0;
213 info->get_vtbl_off = 0;
216 if(desc->invkind & DISPATCH_METHOD) {
217 info->func_disp_idx = data->func_disp_cnt++;
218 }else if(desc->invkind & (DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYGET)) {
219 VARTYPE vt = VT_EMPTY;
221 if(desc->invkind & DISPATCH_PROPERTYGET) {
222 vt = desc->elemdescFunc.tdesc.vt;
223 info->get_vtbl_off = desc->oVft/sizeof(void*);
225 if(desc->invkind & DISPATCH_PROPERTYPUT) {
226 assert(desc->cParams == 1);
227 vt = desc->lprgelemdescParam->tdesc.vt;
228 info->put_vtbl_off = desc->oVft/sizeof(void*);
231 assert(info->prop_vt == VT_EMPTY || vt == info->prop_vt);
232 info->prop_vt = vt;
236 static int dispid_cmp(const void *p1, const void *p2)
238 return ((const func_info_t*)p1)->id - ((const func_info_t*)p2)->id;
241 static int func_name_cmp(const void *p1, const void *p2)
243 return strcmpiW((*(func_info_t* const*)p1)->name, (*(func_info_t* const*)p2)->name);
246 static dispex_data_t *preprocess_dispex_data(DispatchEx *This)
248 const tid_t *tid = This->data->iface_tids;
249 FUNCDESC *funcdesc;
250 dispex_data_t *data;
251 DWORD size = 16, i;
252 ITypeInfo *ti, *dti;
253 HRESULT hres;
255 TRACE("(%p)\n", This);
257 if(This->data->disp_tid) {
258 hres = get_typeinfo(This->data->disp_tid, &dti);
259 if(FAILED(hres)) {
260 ERR("Could not get disp type info: %08x\n", hres);
261 return NULL;
265 data = heap_alloc(sizeof(dispex_data_t));
266 data->func_cnt = 0;
267 data->func_disp_cnt = 0;
268 data->funcs = heap_alloc(size*sizeof(func_info_t));
269 list_add_tail(&dispex_data_list, &data->entry);
271 while(*tid) {
272 hres = get_typeinfo(*tid, &ti);
273 if(FAILED(hres))
274 break;
276 i=7;
277 while(1) {
278 hres = ITypeInfo_GetFuncDesc(ti, i++, &funcdesc);
279 if(FAILED(hres))
280 break;
282 add_func_info(data, &size, *tid, funcdesc, dti);
283 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
286 tid++;
289 if(!data->func_cnt) {
290 heap_free(data->funcs);
291 data->name_table = NULL;
292 data->funcs = NULL;
293 return data;
297 data->funcs = heap_realloc(data->funcs, data->func_cnt * sizeof(func_info_t));
298 qsort(data->funcs, data->func_cnt, sizeof(func_info_t), dispid_cmp);
300 data->name_table = heap_alloc(data->func_cnt * sizeof(func_info_t*));
301 for(i=0; i < data->func_cnt; i++)
302 data->name_table[i] = data->funcs+i;
303 qsort(data->name_table, data->func_cnt, sizeof(func_info_t*), func_name_cmp);
305 return data;
308 static int id_cmp(const void *p1, const void *p2)
310 return *(const DISPID*)p1 - *(const DISPID*)p2;
313 HRESULT get_dispids(tid_t tid, DWORD *ret_size, DISPID **ret)
315 unsigned i, func_cnt;
316 FUNCDESC *funcdesc;
317 ITypeInfo *ti;
318 TYPEATTR *attr;
319 DISPID *ids;
320 HRESULT hres;
322 hres = get_typeinfo(tid, &ti);
323 if(FAILED(hres))
324 return hres;
326 hres = ITypeInfo_GetTypeAttr(ti, &attr);
327 if(FAILED(hres)) {
328 ITypeInfo_Release(ti);
329 return hres;
332 func_cnt = attr->cFuncs;
333 ITypeInfo_ReleaseTypeAttr(ti, attr);
335 ids = heap_alloc(func_cnt*sizeof(DISPID));
336 if(!ids) {
337 ITypeInfo_Release(ti);
338 return E_OUTOFMEMORY;
341 for(i=0; i < func_cnt; i++) {
342 hres = ITypeInfo_GetFuncDesc(ti, i, &funcdesc);
343 if(FAILED(hres))
344 break;
346 ids[i] = funcdesc->memid;
347 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
350 ITypeInfo_Release(ti);
351 if(FAILED(hres)) {
352 heap_free(ids);
353 return hres;
356 qsort(ids, func_cnt, sizeof(DISPID), id_cmp);
358 *ret_size = func_cnt;
359 *ret = ids;
360 return S_OK;
363 static dispex_data_t *get_dispex_data(DispatchEx *This)
365 if(This->data->data)
366 return This->data->data;
368 EnterCriticalSection(&cs_dispex_static_data);
370 if(!This->data->data)
371 This->data->data = preprocess_dispex_data(This);
373 LeaveCriticalSection(&cs_dispex_static_data);
375 return This->data->data;
378 static inline BOOL is_custom_dispid(DISPID id)
380 return MSHTML_DISPID_CUSTOM_MIN <= id && id <= MSHTML_DISPID_CUSTOM_MAX;
383 static inline BOOL is_dynamic_dispid(DISPID id)
385 return DISPID_DYNPROP_0 <= id && id <= DISPID_DYNPROP_MAX;
388 static HRESULT variant_copy(VARIANT *dest, VARIANT *src)
390 if(V_VT(src) == VT_BSTR && !V_BSTR(src)) {
391 V_VT(dest) = VT_BSTR;
392 V_BSTR(dest) = NULL;
393 return S_OK;
396 return VariantCopy(dest, src);
399 static inline dispex_dynamic_data_t *get_dynamic_data(DispatchEx *This)
401 if(This->dynamic_data)
402 return This->dynamic_data;
404 This->dynamic_data = heap_alloc_zero(sizeof(dispex_dynamic_data_t));
405 if(!This->dynamic_data)
406 return NULL;
408 if(This->data->vtbl && This->data->vtbl->populate_props)
409 This->data->vtbl->populate_props(This);
411 return This->dynamic_data;
414 static HRESULT get_dynamic_prop(DispatchEx *This, const WCHAR *name, DWORD flags, dynamic_prop_t **ret)
416 const BOOL alloc = flags & fdexNameEnsure;
417 dispex_dynamic_data_t *data;
418 dynamic_prop_t *prop;
420 data = get_dynamic_data(This);
421 if(!data)
422 return E_OUTOFMEMORY;
424 for(prop = data->props; prop < data->props+data->prop_cnt; prop++) {
425 if(flags & fdexNameCaseInsensitive ? !strcmpiW(prop->name, name) : !strcmpW(prop->name, name)) {
426 if(prop->flags & DYNPROP_DELETED) {
427 if(!alloc)
428 return DISP_E_UNKNOWNNAME;
429 prop->flags &= ~DYNPROP_DELETED;
431 *ret = prop;
432 return S_OK;
436 if(!alloc)
437 return DISP_E_UNKNOWNNAME;
439 TRACE("creating dynamic prop %s\n", debugstr_w(name));
441 if(!data->buf_size) {
442 data->props = heap_alloc(sizeof(dynamic_prop_t)*4);
443 if(!data->props)
444 return E_OUTOFMEMORY;
445 data->buf_size = 4;
446 }else if(data->buf_size == data->prop_cnt) {
447 dynamic_prop_t *new_props;
449 new_props = heap_realloc(data->props, sizeof(dynamic_prop_t)*(data->buf_size<<1));
450 if(!new_props)
451 return E_OUTOFMEMORY;
453 data->props = new_props;
454 data->buf_size <<= 1;
457 prop = data->props + data->prop_cnt;
459 prop->name = heap_strdupW(name);
460 if(!prop->name)
461 return E_OUTOFMEMORY;
463 VariantInit(&prop->var);
464 prop->flags = 0;
465 data->prop_cnt++;
466 *ret = prop;
467 return S_OK;
470 HRESULT dispex_get_dprop_ref(DispatchEx *This, const WCHAR *name, BOOL alloc, VARIANT **ret)
472 dynamic_prop_t *prop;
473 HRESULT hres;
475 hres = get_dynamic_prop(This, name, alloc ? fdexNameEnsure : 0, &prop);
476 if(FAILED(hres))
477 return hres;
479 *ret = &prop->var;
480 return S_OK;
483 HRESULT dispex_get_dynid(DispatchEx *This, const WCHAR *name, DISPID *id)
485 dynamic_prop_t *prop;
486 HRESULT hres;
488 hres = get_dynamic_prop(This, name, fdexNameEnsure, &prop);
489 if(FAILED(hres))
490 return hres;
492 *id = DISPID_DYNPROP_0 + (prop - This->dynamic_data->props);
493 return S_OK;
496 static HRESULT dispex_value(DispatchEx *This, LCID lcid, WORD flags, DISPPARAMS *params,
497 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
499 if(This->data->vtbl && This->data->vtbl->value)
500 return This->data->vtbl->value(This, lcid, flags, params, res, ei, caller);
502 switch(flags) {
503 case DISPATCH_PROPERTYGET:
504 V_VT(res) = VT_BSTR;
505 V_BSTR(res) = SysAllocString(objectW);
506 if(!V_BSTR(res))
507 return E_OUTOFMEMORY;
508 break;
509 default:
510 FIXME("Unimplemented flags %x\n", flags);
511 return E_NOTIMPL;
514 return S_OK;
517 static HRESULT typeinfo_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
518 EXCEPINFO *ei)
520 ITypeInfo *ti;
521 IUnknown *unk;
522 UINT argerr=0;
523 HRESULT hres;
525 hres = get_typeinfo(func->tid, &ti);
526 if(FAILED(hres)) {
527 ERR("Could not get type info: %08x\n", hres);
528 return hres;
531 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&unk);
532 if(FAILED(hres)) {
533 ERR("Could not get iface %s: %08x\n", debugstr_guid(tid_ids[func->tid]), hres);
534 return E_FAIL;
537 hres = ITypeInfo_Invoke(ti, unk, func->id, flags, dp, res, ei, &argerr);
539 IUnknown_Release(unk);
540 return hres;
543 static inline func_disp_t *impl_from_IUnknown(IUnknown *iface)
545 return CONTAINING_RECORD(iface, func_disp_t, IUnknown_iface);
548 static HRESULT WINAPI Function_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
550 func_disp_t *This = impl_from_IUnknown(iface);
552 if(IsEqualGUID(&IID_IUnknown, riid)) {
553 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
554 *ppv = &This->IUnknown_iface;
555 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
556 return *ppv ? S_OK : E_NOINTERFACE;
557 }else {
558 *ppv = NULL;
559 return E_NOINTERFACE;
562 IUnknown_AddRef((IUnknown*)*ppv);
563 return S_OK;
566 static ULONG WINAPI Function_AddRef(IUnknown *iface)
568 func_disp_t *This = impl_from_IUnknown(iface);
570 TRACE("(%p)\n", This);
572 return IDispatchEx_AddRef(&This->obj->IDispatchEx_iface);
575 static ULONG WINAPI Function_Release(IUnknown *iface)
577 func_disp_t *This = impl_from_IUnknown(iface);
579 TRACE("(%p)\n", This);
581 return IDispatchEx_Release(&This->obj->IDispatchEx_iface);
584 static const IUnknownVtbl FunctionUnkVtbl = {
585 Function_QueryInterface,
586 Function_AddRef,
587 Function_Release
590 static inline func_disp_t *impl_from_DispatchEx(DispatchEx *iface)
592 return CONTAINING_RECORD(iface, func_disp_t, dispex);
595 static HRESULT function_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *params,
596 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
598 func_disp_t *This = impl_from_DispatchEx(dispex);
599 HRESULT hres;
601 switch(flags) {
602 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
603 if(!res)
604 return E_INVALIDARG;
605 /* fall through */
606 case DISPATCH_METHOD:
607 hres = typeinfo_invoke(This->obj, This->info, flags, params, res, ei);
608 break;
609 default:
610 FIXME("Unimplemented flags %x\n", flags);
611 hres = E_NOTIMPL;
614 return hres;
617 static const dispex_static_data_vtbl_t function_dispex_vtbl = {
618 function_value,
619 NULL,
620 NULL,
621 NULL
624 static const tid_t function_iface_tids[] = {0};
626 static dispex_static_data_t function_dispex = {
627 &function_dispex_vtbl,
628 NULL_tid,
629 NULL,
630 function_iface_tids
633 static func_disp_t *create_func_disp(DispatchEx *obj, func_info_t *info)
635 func_disp_t *ret;
637 ret = heap_alloc_zero(sizeof(func_disp_t));
638 if(!ret)
639 return NULL;
641 ret->IUnknown_iface.lpVtbl = &FunctionUnkVtbl;
642 init_dispex(&ret->dispex, &ret->IUnknown_iface, &function_dispex);
643 ret->obj = obj;
644 ret->info = info;
646 return ret;
649 static HRESULT function_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
650 EXCEPINFO *ei)
652 HRESULT hres;
654 switch(flags) {
655 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
656 if(!res)
657 return E_INVALIDARG;
658 /* fall through */
659 case DISPATCH_METHOD:
660 hres = typeinfo_invoke(This, func, flags, dp, res, ei);
661 break;
662 case DISPATCH_PROPERTYGET: {
663 dispex_dynamic_data_t *dynamic_data;
665 if(func->id == DISPID_VALUE) {
666 BSTR ret;
668 ret = SysAllocString(objectW);
669 if(!ret)
670 return E_OUTOFMEMORY;
672 V_VT(res) = VT_BSTR;
673 V_BSTR(res) = ret;
674 return S_OK;
677 dynamic_data = get_dynamic_data(This);
678 if(!dynamic_data)
679 return E_OUTOFMEMORY;
681 if(!dynamic_data->func_disps) {
682 dynamic_data->func_disps = heap_alloc_zero(This->data->data->func_disp_cnt * sizeof(func_disp_t*));
683 if(!dynamic_data->func_disps)
684 return E_OUTOFMEMORY;
687 if(!dynamic_data->func_disps[func->func_disp_idx]) {
688 dynamic_data->func_disps[func->func_disp_idx] = create_func_disp(This, func);
689 if(!dynamic_data->func_disps[func->func_disp_idx])
690 return E_OUTOFMEMORY;
693 V_VT(res) = VT_DISPATCH;
694 V_DISPATCH(res) = (IDispatch*)&dynamic_data->func_disps[func->func_disp_idx]->dispex.IDispatchEx_iface;
695 IDispatch_AddRef(V_DISPATCH(res));
696 hres = S_OK;
697 break;
699 default:
700 FIXME("Unimplemented flags %x\n", flags);
701 /* fall through */
702 case DISPATCH_PROPERTYPUT:
703 hres = E_NOTIMPL;
706 return hres;
709 static HRESULT get_builtin_func(dispex_data_t *data, DISPID id, func_info_t **ret)
711 int min, max, n;
713 min = 0;
714 max = data->func_cnt-1;
716 while(min <= max) {
717 n = (min+max)/2;
719 if(data->funcs[n].id == id) {
720 *ret = data->funcs+n;
721 return S_OK;
724 if(data->funcs[n].id < id)
725 min = n+1;
726 else
727 max = n-1;
730 WARN("invalid id %x\n", id);
731 return DISP_E_UNKNOWNNAME;
734 static HRESULT get_builtin_id(DispatchEx *This, BSTR name, DWORD grfdex, DISPID *ret)
736 dispex_data_t *data;
737 int min, max, n, c;
739 data = get_dispex_data(This);
740 if(!data)
741 return E_FAIL;
743 min = 0;
744 max = data->func_cnt-1;
746 while(min <= max) {
747 n = (min+max)/2;
749 c = strcmpiW(data->name_table[n]->name, name);
750 if(!c) {
751 if((grfdex & fdexNameCaseSensitive) && strcmpW(data->name_table[n]->name, name))
752 break;
754 *ret = data->name_table[n]->id;
755 return S_OK;
758 if(c > 0)
759 max = n-1;
760 else
761 min = n+1;
764 if(This->data->vtbl && This->data->vtbl->get_dispid) {
765 HRESULT hres;
767 hres = This->data->vtbl->get_dispid(This, name, grfdex, ret);
768 if(hres != DISP_E_UNKNOWNNAME)
769 return hres;
772 return DISP_E_UNKNOWNNAME;
775 /* List all types used by IDispatchEx-based properties */
776 #define BUILTIN_TYPES_SWITCH \
777 CASE_VT(VT_I2, INT16, V_I2); \
778 CASE_VT(VT_I4, INT32, V_I4); \
779 CASE_VT(VT_R4, float, V_R4); \
780 CASE_VT(VT_BSTR, BSTR, V_BSTR); \
781 CASE_VT(VT_BOOL, VARIANT_BOOL, V_BOOL); \
782 CASE_VT(VT_VARIANT, VARIANT, *); \
783 CASE_VT(VT_PTR, void*, V_BYREF); \
784 CASE_VT(VT_UNKNOWN, IUnknown*, V_UNKNOWN); \
785 CASE_VT(VT_DISPATCH, IDispatch*, V_DISPATCH)
787 static HRESULT change_type(VARIANT *dst, VARIANT *src, VARTYPE vt, IServiceProvider *caller)
789 V_VT(dst) = VT_EMPTY;
791 if(caller) {
792 IVariantChangeType *change_type = NULL;
793 HRESULT hres;
795 hres = IServiceProvider_QueryService(caller, &SID_VariantConversion, &IID_IVariantChangeType, (void**)&change_type);
796 if(SUCCEEDED(hres)) {
797 hres = IVariantChangeType_ChangeType(change_type, dst, src, LOCALE_NEUTRAL, vt);
798 IVariantChangeType_Release(change_type);
799 return hres;
803 return VariantChangeType(dst, src, 0, vt);
806 static HRESULT builtin_propget(DispatchEx *This, func_info_t *func, DISPPARAMS *dp, VARIANT *res)
808 IUnknown *iface;
809 HRESULT hres;
811 if(dp->cArgs) {
812 FIXME("cArgs %d\n", dp->cArgs);
813 return E_NOTIMPL;
816 assert(func->get_vtbl_off);
818 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&iface);
819 if(SUCCEEDED(hres)) {
820 switch(func->prop_vt) {
821 #define CASE_VT(vt,type,access) \
822 case vt: { \
823 type val; \
824 hres = ((HRESULT (WINAPI*)(IUnknown*,type*))((void**)iface->lpVtbl)[func->get_vtbl_off])(iface,&val); \
825 if(SUCCEEDED(hres)) \
826 access(res) = val; \
828 break
829 BUILTIN_TYPES_SWITCH;
830 #undef CASE_VT
831 default:
832 FIXME("Unhandled vt %d\n", func->prop_vt);
833 hres = E_NOTIMPL;
835 IUnknown_Release(iface);
838 if(FAILED(hres))
839 return hres;
841 if(func->prop_vt != VT_VARIANT)
842 V_VT(res) = func->prop_vt == VT_PTR ? VT_DISPATCH : func->prop_vt;
843 return S_OK;
846 static HRESULT builtin_propput(DispatchEx *This, func_info_t *func, DISPPARAMS *dp, IServiceProvider *caller)
848 VARIANT *v, tmpv;
849 IUnknown *iface;
850 HRESULT hres;
852 if(dp->cArgs != 1 || (dp->cNamedArgs == 1 && *dp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
853 || dp->cNamedArgs > 1) {
854 FIXME("invalid args\n");
855 return E_INVALIDARG;
858 if(!func->put_vtbl_off) {
859 FIXME("No setter\n");
860 return E_FAIL;
863 v = dp->rgvarg;
864 if(func->prop_vt != VT_VARIANT && V_VT(v) != func->prop_vt) {
865 hres = change_type(&tmpv, v, func->prop_vt, caller);
866 if(FAILED(hres))
867 return hres;
868 v = &tmpv;
871 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&iface);
872 if(SUCCEEDED(hres)) {
873 switch(func->prop_vt) {
874 #define CASE_VT(vt,type,access) \
875 case vt: \
876 hres = ((HRESULT (WINAPI*)(IUnknown*,type))((void**)iface->lpVtbl)[func->put_vtbl_off])(iface,access(v)); \
877 break
878 BUILTIN_TYPES_SWITCH;
879 #undef CASE_VT
880 default:
881 FIXME("Unimplemented vt %d\n", func->prop_vt);
882 hres = E_NOTIMPL;
885 IUnknown_Release(iface);
888 if(v == &tmpv)
889 VariantClear(v);
890 return hres;
893 static HRESULT invoke_builtin_prop(DispatchEx *This, DISPID id, LCID lcid, WORD flags, DISPPARAMS *dp,
894 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
896 dispex_data_t *data;
897 func_info_t *func;
898 HRESULT hres;
900 data = get_dispex_data(This);
901 if(!data)
902 return E_FAIL;
904 hres = get_builtin_func(data, id, &func);
905 if(id == DISPID_VALUE && hres == DISP_E_UNKNOWNNAME)
906 return dispex_value(This, lcid, flags, dp, res, ei, caller);
907 if(FAILED(hres))
908 return hres;
910 if(func->func_disp_idx != -1)
911 return function_invoke(This, func, flags, dp, res, ei);
913 switch(flags) {
914 case DISPATCH_PROPERTYPUT:
915 if(res)
916 V_VT(res) = VT_EMPTY;
917 hres = builtin_propput(This, func, dp, caller);
918 break;
919 case DISPATCH_PROPERTYGET:
920 hres = builtin_propget(This, func, dp, res);
921 break;
922 default:
923 hres = typeinfo_invoke(This, func, flags, dp, res, ei);
926 return hres;
929 HRESULT remove_prop(DispatchEx *This, BSTR name, VARIANT_BOOL *success)
931 dynamic_prop_t *prop;
932 DISPID id;
933 HRESULT hres;
935 hres = get_builtin_id(This, name, 0, &id);
936 if(hres == S_OK) {
937 DISPID named_id = DISPID_PROPERTYPUT;
938 VARIANT var;
939 DISPPARAMS dp = {&var,&named_id,1,1};
940 EXCEPINFO ei;
942 V_VT(&var) = VT_EMPTY;
943 memset(&ei, 0, sizeof(ei));
944 hres = invoke_builtin_prop(This, id, 0, DISPATCH_PROPERTYPUT, &dp, NULL, &ei, NULL);
945 if(FAILED(hres))
946 return hres;
948 *success = VARIANT_TRUE;
949 return S_OK;
952 hres = get_dynamic_prop(This, name, 0, &prop);
953 if(FAILED(hres)) {
954 if(hres != DISP_E_UNKNOWNNAME)
955 return hres;
956 *success = VARIANT_FALSE;
957 return S_OK;
960 VariantClear(&prop->var);
961 prop->flags |= DYNPROP_DELETED;
962 *success = VARIANT_TRUE;
963 return S_OK;
966 static inline DispatchEx *impl_from_IDispatchEx(IDispatchEx *iface)
968 return CONTAINING_RECORD(iface, DispatchEx, IDispatchEx_iface);
971 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
973 DispatchEx *This = impl_from_IDispatchEx(iface);
975 return IUnknown_QueryInterface(This->outer, riid, ppv);
978 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
980 DispatchEx *This = impl_from_IDispatchEx(iface);
982 return IUnknown_AddRef(This->outer);
985 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
987 DispatchEx *This = impl_from_IDispatchEx(iface);
989 return IUnknown_Release(This->outer);
992 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
994 DispatchEx *This = impl_from_IDispatchEx(iface);
996 TRACE("(%p)->(%p)\n", This, pctinfo);
998 *pctinfo = 1;
999 return S_OK;
1002 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
1003 LCID lcid, ITypeInfo **ppTInfo)
1005 DispatchEx *This = impl_from_IDispatchEx(iface);
1006 HRESULT hres;
1008 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
1010 hres = get_typeinfo(This->data->disp_tid, ppTInfo);
1011 if(FAILED(hres))
1012 return hres;
1014 ITypeInfo_AddRef(*ppTInfo);
1015 return S_OK;
1018 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
1019 LPOLESTR *rgszNames, UINT cNames,
1020 LCID lcid, DISPID *rgDispId)
1022 DispatchEx *This = impl_from_IDispatchEx(iface);
1023 UINT i;
1024 HRESULT hres;
1026 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
1027 lcid, rgDispId);
1029 for(i=0; i < cNames; i++) {
1030 hres = IDispatchEx_GetDispID(&This->IDispatchEx_iface, rgszNames[i], 0, rgDispId+i);
1031 if(FAILED(hres))
1032 return hres;
1035 return S_OK;
1038 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
1039 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
1040 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
1042 DispatchEx *This = impl_from_IDispatchEx(iface);
1044 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1045 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1047 return IDispatchEx_InvokeEx(&This->IDispatchEx_iface, dispIdMember, lcid, wFlags, pDispParams,
1048 pVarResult, pExcepInfo, NULL);
1051 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
1053 DispatchEx *This = impl_from_IDispatchEx(iface);
1054 dynamic_prop_t *dprop;
1055 HRESULT hres;
1057 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
1059 if(grfdex & ~(fdexNameCaseSensitive|fdexNameCaseInsensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK))
1060 FIXME("Unsupported grfdex %x\n", grfdex);
1062 hres = get_builtin_id(This, bstrName, grfdex, pid);
1063 if(hres != DISP_E_UNKNOWNNAME)
1064 return hres;
1066 hres = get_dynamic_prop(This, bstrName, grfdex, &dprop);
1067 if(FAILED(hres))
1068 return hres;
1070 *pid = DISPID_DYNPROP_0 + (dprop - This->dynamic_data->props);
1071 return S_OK;
1074 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
1075 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
1077 DispatchEx *This = impl_from_IDispatchEx(iface);
1078 HRESULT hres;
1080 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1082 if(is_custom_dispid(id) && This->data->vtbl && This->data->vtbl->invoke)
1083 return This->data->vtbl->invoke(This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1085 if(wFlags == DISPATCH_CONSTRUCT) {
1086 if(id == DISPID_VALUE) {
1087 if(This->data->vtbl && This->data->vtbl->value) {
1088 return This->data->vtbl->value(This, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1090 FIXME("DISPATCH_CONSTRUCT flag but missing value function\n");
1091 return E_FAIL;
1093 FIXME("DISPATCH_CONSTRUCT flag without DISPID_VALUE\n");
1094 return E_FAIL;
1097 if(is_dynamic_dispid(id)) {
1098 DWORD idx = id - DISPID_DYNPROP_0;
1099 dynamic_prop_t *prop;
1101 if(!get_dynamic_data(This) || This->dynamic_data->prop_cnt <= idx)
1102 return DISP_E_UNKNOWNNAME;
1104 prop = This->dynamic_data->props+idx;
1106 switch(wFlags) {
1107 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
1108 if(!pvarRes)
1109 return E_INVALIDARG;
1110 /* fall through */
1111 case DISPATCH_METHOD: {
1112 DISPID named_arg = DISPID_THIS;
1113 DISPPARAMS dp = {NULL, &named_arg, 0, 1};
1114 IDispatchEx *dispex;
1116 if(V_VT(&prop->var) != VT_DISPATCH) {
1117 FIXME("invoke %s\n", debugstr_variant(&prop->var));
1118 return E_NOTIMPL;
1121 if(pdp->cNamedArgs) {
1122 FIXME("named args not supported\n");
1123 return E_NOTIMPL;
1126 dp.rgvarg = heap_alloc((pdp->cArgs+1)*sizeof(VARIANTARG));
1127 if(!dp.rgvarg)
1128 return E_OUTOFMEMORY;
1130 dp.cArgs = pdp->cArgs+1;
1131 memcpy(dp.rgvarg+1, pdp->rgvarg, pdp->cArgs*sizeof(VARIANTARG));
1133 V_VT(dp.rgvarg) = VT_DISPATCH;
1134 V_DISPATCH(dp.rgvarg) = (IDispatch*)&This->IDispatchEx_iface;
1136 hres = IDispatch_QueryInterface(V_DISPATCH(&prop->var), &IID_IDispatchEx, (void**)&dispex);
1137 TRACE("%s call\n", debugstr_w(This->dynamic_data->props[idx].name));
1138 if(SUCCEEDED(hres)) {
1139 hres = IDispatchEx_InvokeEx(dispex, DISPID_VALUE, lcid, wFlags, &dp, pvarRes, pei, pspCaller);
1140 IDispatchEx_Release(dispex);
1141 }else {
1142 ULONG err = 0;
1143 hres = IDispatch_Invoke(V_DISPATCH(&prop->var), DISPID_VALUE, &IID_NULL, lcid, wFlags, pdp, pvarRes, pei, &err);
1145 TRACE("%s ret %08x\n", debugstr_w(This->dynamic_data->props[idx].name), hres);
1147 heap_free(dp.rgvarg);
1148 return hres;
1150 case DISPATCH_PROPERTYGET:
1151 if(prop->flags & DYNPROP_DELETED)
1152 return DISP_E_UNKNOWNNAME;
1153 V_VT(pvarRes) = VT_EMPTY;
1154 return variant_copy(pvarRes, &prop->var);
1155 case DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYPUTREF:
1156 case DISPATCH_PROPERTYPUT:
1157 if(pdp->cArgs != 1 || (pdp->cNamedArgs == 1 && *pdp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
1158 || pdp->cNamedArgs > 1) {
1159 FIXME("invalid args\n");
1160 return E_INVALIDARG;
1163 TRACE("put %s\n", debugstr_variant(pdp->rgvarg));
1164 VariantClear(&prop->var);
1165 hres = variant_copy(&prop->var, pdp->rgvarg);
1166 if(FAILED(hres))
1167 return hres;
1169 prop->flags &= ~DYNPROP_DELETED;
1170 return S_OK;
1171 default:
1172 FIXME("unhandled wFlags %x\n", wFlags);
1173 return E_NOTIMPL;
1177 return invoke_builtin_prop(This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1180 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
1182 DispatchEx *This = impl_from_IDispatchEx(iface);
1184 TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
1186 /* Not implemented by IE */
1187 return E_NOTIMPL;
1190 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
1192 DispatchEx *This = impl_from_IDispatchEx(iface);
1194 TRACE("(%p)->(%x)\n", This, id);
1196 /* Not implemented by IE */
1197 return E_NOTIMPL;
1200 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
1202 DispatchEx *This = impl_from_IDispatchEx(iface);
1203 FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
1204 return E_NOTIMPL;
1207 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
1209 DispatchEx *This = impl_from_IDispatchEx(iface);
1210 dispex_data_t *data;
1211 func_info_t *func;
1212 HRESULT hres;
1214 TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
1216 if(is_dynamic_dispid(id)) {
1217 DWORD idx = id - DISPID_DYNPROP_0;
1219 if(!get_dynamic_data(This) || This->dynamic_data->prop_cnt <= idx)
1220 return DISP_E_UNKNOWNNAME;
1222 *pbstrName = SysAllocString(This->dynamic_data->props[idx].name);
1223 if(!*pbstrName)
1224 return E_OUTOFMEMORY;
1226 return S_OK;
1229 data = get_dispex_data(This);
1230 if(!data)
1231 return E_FAIL;
1233 hres = get_builtin_func(data, id, &func);
1234 if(FAILED(hres))
1235 return hres;
1237 *pbstrName = SysAllocString(func->name);
1238 if(!*pbstrName)
1239 return E_OUTOFMEMORY;
1240 return S_OK;
1243 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
1245 DispatchEx *This = impl_from_IDispatchEx(iface);
1246 dispex_data_t *data;
1247 func_info_t *func;
1248 HRESULT hres;
1250 TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
1252 if(is_dynamic_dispid(id)) {
1253 DWORD idx = id - DISPID_DYNPROP_0;
1255 if(!get_dynamic_data(This) || This->dynamic_data->prop_cnt <= idx)
1256 return DISP_E_UNKNOWNNAME;
1258 while(++idx < This->dynamic_data->prop_cnt && This->dynamic_data->props[idx].flags & DYNPROP_DELETED);
1260 if(idx == This->dynamic_data->prop_cnt) {
1261 *pid = DISPID_STARTENUM;
1262 return S_FALSE;
1265 *pid = DISPID_DYNPROP_0+idx;
1266 return S_OK;
1269 data = get_dispex_data(This);
1270 if(!data)
1271 return E_FAIL;
1273 if(id == DISPID_STARTENUM) {
1274 func = data->funcs;
1275 }else {
1276 hres = get_builtin_func(data, id, &func);
1277 if(FAILED(hres))
1278 return hres;
1279 func++;
1282 while(func < data->funcs+data->func_cnt) {
1283 /* FIXME: Skip hidden properties */
1284 if(func->func_disp_idx == -1) {
1285 *pid = func->id;
1286 return S_OK;
1288 func++;
1291 if(get_dynamic_data(This) && This->dynamic_data->prop_cnt) {
1292 *pid = DISPID_DYNPROP_0;
1293 return S_OK;
1296 *pid = DISPID_STARTENUM;
1297 return S_FALSE;
1300 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
1302 DispatchEx *This = impl_from_IDispatchEx(iface);
1303 FIXME("(%p)->(%p)\n", This, ppunk);
1304 return E_NOTIMPL;
1307 static IDispatchExVtbl DispatchExVtbl = {
1308 DispatchEx_QueryInterface,
1309 DispatchEx_AddRef,
1310 DispatchEx_Release,
1311 DispatchEx_GetTypeInfoCount,
1312 DispatchEx_GetTypeInfo,
1313 DispatchEx_GetIDsOfNames,
1314 DispatchEx_Invoke,
1315 DispatchEx_GetDispID,
1316 DispatchEx_InvokeEx,
1317 DispatchEx_DeleteMemberByName,
1318 DispatchEx_DeleteMemberByDispID,
1319 DispatchEx_GetMemberProperties,
1320 DispatchEx_GetMemberName,
1321 DispatchEx_GetNextDispID,
1322 DispatchEx_GetNameSpaceParent
1325 BOOL dispex_query_interface(DispatchEx *This, REFIID riid, void **ppv)
1327 static const IID IID_UndocumentedScriptIface =
1328 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa0}};
1329 static const IID IID_IDispatchJS =
1330 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa6}};
1332 if(IsEqualGUID(&IID_IDispatch, riid)) {
1333 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
1334 *ppv = &This->IDispatchEx_iface;
1335 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
1336 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
1337 *ppv = &This->IDispatchEx_iface;
1338 }else if(IsEqualGUID(&IID_IDispatchJS, riid)) {
1339 TRACE("(%p)->(IID_IDispatchJS %p) returning NULL\n", This, ppv);
1340 *ppv = NULL;
1341 }else if(IsEqualGUID(&IID_UndocumentedScriptIface, riid)) {
1342 TRACE("(%p)->(IID_UndocumentedScriptIface %p) returning NULL\n", This, ppv);
1343 *ppv = NULL;
1344 }else {
1345 return FALSE;
1348 if(*ppv)
1349 IUnknown_AddRef((IUnknown*)*ppv);
1350 return TRUE;
1353 void release_dispex(DispatchEx *This)
1355 dynamic_prop_t *prop;
1357 if(!This->dynamic_data)
1358 return;
1360 for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++) {
1361 VariantClear(&prop->var);
1362 heap_free(prop->name);
1365 heap_free(This->dynamic_data->props);
1367 if(This->dynamic_data->func_disps) {
1368 unsigned i;
1370 for(i=0; i < This->data->data->func_disp_cnt; i++) {
1371 if(This->dynamic_data->func_disps[i]) {
1372 release_dispex(&This->dynamic_data->func_disps[i]->dispex);
1373 heap_free(This->dynamic_data->func_disps[i]);
1377 heap_free(This->dynamic_data->func_disps);
1380 heap_free(This->dynamic_data);
1383 void init_dispex(DispatchEx *dispex, IUnknown *outer, dispex_static_data_t *data)
1385 dispex->IDispatchEx_iface.lpVtbl = &DispatchExVtbl;
1386 dispex->outer = outer;
1387 dispex->data = data;
1388 dispex->dynamic_data = NULL;