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
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};
57 struct dispex_data_t {
60 func_info_t **name_table;
72 #define DYNPROP_DELETED 0x01
76 IUnknown IUnknown_iface;
81 struct dispex_dynamic_data_t {
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,
105 static HRESULT load_typelib(void)
110 hres = LoadRegTypeLib(&LIBID_MSHTML, 4, 0, LOCALE_SYSTEM_DEFAULT, &tl);
112 ERR("LoadRegTypeLib failed: %08x\n", hres);
116 if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
117 ITypeLib_Release(tl);
121 static HRESULT get_typeinfo(tid_t tid, ITypeInfo **typeinfo)
126 hres = load_typelib();
130 if(!typeinfos[tid]) {
133 hres = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &ti);
135 ERR("GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_guid(tid_ids[tid]), hres);
139 if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), ti, NULL))
140 ITypeInfo_Release(ti);
143 *typeinfo = typeinfos[tid];
147 void release_typelib(void)
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);
167 for(i=0; i < sizeof(typeinfos)/sizeof(*typeinfos); i++)
169 ITypeInfo_Release(typeinfos[i]);
171 ITypeLib_Release(typelib);
172 DeleteCriticalSection(&cs_dispex_static_data);
175 HRESULT get_htmldoc_classinfo(ITypeInfo **typeinfo)
180 hres = load_typelib();
184 hres = ITypeLib_GetTypeInfoOfGuid(typelib, &CLSID_HTMLDocument, typeinfo);
186 ERR("GetTypeInfoOfGuid failed: %08x\n", hres);
190 static void add_func_info(dispex_data_t *data, DWORD *size, tid_t tid, const FUNCDESC *desc, ITypeInfo *dti)
195 if(data->func_cnt && data->funcs[data->func_cnt-1].id == desc->memid) {
196 info = data->funcs+data->func_cnt-1;
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);
208 info->id = desc->memid;
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);
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;
255 TRACE("(%p)\n", This);
257 if(This->data->disp_tid) {
258 hres = get_typeinfo(This->data->disp_tid, &dti);
260 ERR("Could not get disp type info: %08x\n", hres);
265 data = heap_alloc(sizeof(dispex_data_t));
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);
272 hres = get_typeinfo(*tid, &ti);
278 hres = ITypeInfo_GetFuncDesc(ti, i++, &funcdesc);
282 add_func_info(data, &size, *tid, funcdesc, dti);
283 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
289 if(!data->func_cnt) {
290 heap_free(data->funcs);
291 data->name_table = NULL;
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);
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;
322 hres = get_typeinfo(tid, &ti);
326 hres = ITypeInfo_GetTypeAttr(ti, &attr);
328 ITypeInfo_Release(ti);
332 func_cnt = attr->cFuncs;
333 ITypeInfo_ReleaseTypeAttr(ti, attr);
335 ids = heap_alloc(func_cnt*sizeof(DISPID));
337 ITypeInfo_Release(ti);
338 return E_OUTOFMEMORY;
341 for(i=0; i < func_cnt; i++) {
342 hres = ITypeInfo_GetFuncDesc(ti, i, &funcdesc);
346 ids[i] = funcdesc->memid;
347 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
350 ITypeInfo_Release(ti);
356 qsort(ids, func_cnt, sizeof(DISPID), id_cmp);
358 *ret_size = func_cnt;
363 static dispex_data_t *get_dispex_data(DispatchEx *This)
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;
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)
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);
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) {
428 return DISP_E_UNKNOWNNAME;
429 prop->flags &= ~DYNPROP_DELETED;
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);
444 return E_OUTOFMEMORY;
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));
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);
461 return E_OUTOFMEMORY;
463 VariantInit(&prop->var);
470 HRESULT dispex_get_dprop_ref(DispatchEx *This, const WCHAR *name, BOOL alloc, VARIANT **ret)
472 dynamic_prop_t *prop;
475 hres = get_dynamic_prop(This, name, alloc ? fdexNameEnsure : 0, &prop);
483 static HRESULT dispex_value(DispatchEx *This, LCID lcid, WORD flags, DISPPARAMS *params,
484 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
486 if(This->data->vtbl && This->data->vtbl->value)
487 return This->data->vtbl->value(This, lcid, flags, params, res, ei, caller);
490 case DISPATCH_PROPERTYGET:
492 V_BSTR(res) = SysAllocString(objectW);
494 return E_OUTOFMEMORY;
497 FIXME("Unimplemented flags %x\n", flags);
504 static HRESULT typeinfo_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
512 hres = get_typeinfo(func->tid, &ti);
514 ERR("Could not get type info: %08x\n", hres);
518 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&unk);
520 ERR("Could not get iface %s: %08x\n", debugstr_guid(tid_ids[func->tid]), hres);
524 hres = ITypeInfo_Invoke(ti, unk, func->id, flags, dp, res, ei, &argerr);
526 IUnknown_Release(unk);
530 static inline func_disp_t *impl_from_IUnknown(IUnknown *iface)
532 return CONTAINING_RECORD(iface, func_disp_t, IUnknown_iface);
535 static HRESULT WINAPI Function_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
537 func_disp_t *This = impl_from_IUnknown(iface);
539 if(IsEqualGUID(&IID_IUnknown, riid)) {
540 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
541 *ppv = &This->IUnknown_iface;
542 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
543 return *ppv ? S_OK : E_NOINTERFACE;
546 return E_NOINTERFACE;
549 IUnknown_AddRef((IUnknown*)*ppv);
553 static ULONG WINAPI Function_AddRef(IUnknown *iface)
555 func_disp_t *This = impl_from_IUnknown(iface);
557 TRACE("(%p)\n", This);
559 return IDispatchEx_AddRef(&This->obj->IDispatchEx_iface);
562 static ULONG WINAPI Function_Release(IUnknown *iface)
564 func_disp_t *This = impl_from_IUnknown(iface);
566 TRACE("(%p)\n", This);
568 return IDispatchEx_Release(&This->obj->IDispatchEx_iface);
571 static const IUnknownVtbl FunctionUnkVtbl = {
572 Function_QueryInterface,
577 static inline func_disp_t *impl_from_DispatchEx(DispatchEx *iface)
579 return CONTAINING_RECORD(iface, func_disp_t, dispex);
582 static HRESULT function_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *params,
583 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
585 func_disp_t *This = impl_from_DispatchEx(dispex);
589 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
593 case DISPATCH_METHOD:
594 hres = typeinfo_invoke(This->obj, This->info, flags, params, res, ei);
597 FIXME("Unimplemented flags %x\n", flags);
604 static const dispex_static_data_vtbl_t function_dispex_vtbl = {
611 static const tid_t function_iface_tids[] = {0};
613 static dispex_static_data_t function_dispex = {
614 &function_dispex_vtbl,
620 static func_disp_t *create_func_disp(DispatchEx *obj, func_info_t *info)
624 ret = heap_alloc_zero(sizeof(func_disp_t));
628 ret->IUnknown_iface.lpVtbl = &FunctionUnkVtbl;
629 init_dispex(&ret->dispex, &ret->IUnknown_iface, &function_dispex);
636 static HRESULT function_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
642 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
646 case DISPATCH_METHOD:
647 hres = typeinfo_invoke(This, func, flags, dp, res, ei);
649 case DISPATCH_PROPERTYGET: {
650 dispex_dynamic_data_t *dynamic_data;
652 if(func->id == DISPID_VALUE) {
655 ret = SysAllocString(objectW);
657 return E_OUTOFMEMORY;
664 dynamic_data = get_dynamic_data(This);
666 return E_OUTOFMEMORY;
668 if(!dynamic_data->func_disps) {
669 dynamic_data->func_disps = heap_alloc_zero(This->data->data->func_disp_cnt * sizeof(func_disp_t*));
670 if(!dynamic_data->func_disps)
671 return E_OUTOFMEMORY;
674 if(!dynamic_data->func_disps[func->func_disp_idx]) {
675 dynamic_data->func_disps[func->func_disp_idx] = create_func_disp(This, func);
676 if(!dynamic_data->func_disps[func->func_disp_idx])
677 return E_OUTOFMEMORY;
680 V_VT(res) = VT_DISPATCH;
681 V_DISPATCH(res) = (IDispatch*)&dynamic_data->func_disps[func->func_disp_idx]->dispex.IDispatchEx_iface;
682 IDispatch_AddRef(V_DISPATCH(res));
687 FIXME("Unimplemented flags %x\n", flags);
689 case DISPATCH_PROPERTYPUT:
696 static HRESULT get_builtin_func(dispex_data_t *data, DISPID id, func_info_t **ret)
701 max = data->func_cnt-1;
706 if(data->funcs[n].id == id) {
707 *ret = data->funcs+n;
711 if(data->funcs[n].id < id)
717 WARN("invalid id %x\n", id);
718 return DISP_E_UNKNOWNNAME;
721 static HRESULT get_builtin_id(DispatchEx *This, BSTR name, DWORD grfdex, DISPID *ret)
726 data = get_dispex_data(This);
731 max = data->func_cnt-1;
736 c = strcmpiW(data->name_table[n]->name, name);
738 if((grfdex & fdexNameCaseSensitive) && strcmpW(data->name_table[n]->name, name))
741 *ret = data->name_table[n]->id;
751 if(This->data->vtbl && This->data->vtbl->get_dispid) {
754 hres = This->data->vtbl->get_dispid(This, name, grfdex, ret);
755 if(hres != DISP_E_UNKNOWNNAME)
759 return DISP_E_UNKNOWNNAME;
762 /* List all types used by IDispatchEx-based properties */
763 #define BUILTIN_TYPES_SWITCH \
764 CASE_VT(VT_I2, INT16, V_I2); \
765 CASE_VT(VT_I4, INT32, V_I4); \
766 CASE_VT(VT_R4, float, V_R4); \
767 CASE_VT(VT_BSTR, BSTR, V_BSTR); \
768 CASE_VT(VT_BOOL, VARIANT_BOOL, V_BOOL); \
769 CASE_VT(VT_VARIANT, VARIANT, *); \
770 CASE_VT(VT_PTR, void*, V_BYREF); \
771 CASE_VT(VT_UNKNOWN, IUnknown*, V_UNKNOWN); \
772 CASE_VT(VT_DISPATCH, IDispatch*, V_DISPATCH)
774 static HRESULT change_type(VARIANT *dst, VARIANT *src, VARTYPE vt, IServiceProvider *caller)
776 V_VT(dst) = VT_EMPTY;
779 IVariantChangeType *change_type = NULL;
782 hres = IServiceProvider_QueryService(caller, &SID_VariantConversion, &IID_IVariantChangeType, (void**)&change_type);
783 if(SUCCEEDED(hres)) {
784 hres = IVariantChangeType_ChangeType(change_type, dst, src, LOCALE_NEUTRAL, vt);
785 IVariantChangeType_Release(change_type);
790 return VariantChangeType(dst, src, 0, vt);
793 static HRESULT builtin_propget(DispatchEx *This, func_info_t *func, DISPPARAMS *dp, VARIANT *res)
799 FIXME("cArgs %d\n", dp->cArgs);
803 assert(func->get_vtbl_off);
805 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&iface);
806 if(SUCCEEDED(hres)) {
807 switch(func->prop_vt) {
808 #define CASE_VT(vt,type,access) \
811 hres = ((HRESULT (WINAPI*)(IUnknown*,type*))((void**)iface->lpVtbl)[func->get_vtbl_off])(iface,&val); \
812 if(SUCCEEDED(hres)) \
816 BUILTIN_TYPES_SWITCH;
819 FIXME("Unhandled vt %d\n", func->prop_vt);
822 IUnknown_Release(iface);
828 if(func->prop_vt != VT_VARIANT)
829 V_VT(res) = func->prop_vt == VT_PTR ? VT_DISPATCH : func->prop_vt;
833 static HRESULT builtin_propput(DispatchEx *This, func_info_t *func, DISPPARAMS *dp, IServiceProvider *caller)
839 if(dp->cArgs != 1 || (dp->cNamedArgs == 1 && *dp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
840 || dp->cNamedArgs > 1) {
841 FIXME("invalid args\n");
845 if(!func->put_vtbl_off) {
846 FIXME("No setter\n");
851 if(func->prop_vt != VT_VARIANT && V_VT(v) != func->prop_vt) {
852 hres = change_type(&tmpv, v, func->prop_vt, caller);
858 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&iface);
859 if(SUCCEEDED(hres)) {
860 switch(func->prop_vt) {
861 #define CASE_VT(vt,type,access) \
863 hres = ((HRESULT (WINAPI*)(IUnknown*,type))((void**)iface->lpVtbl)[func->put_vtbl_off])(iface,access(v)); \
865 BUILTIN_TYPES_SWITCH;
868 FIXME("Unimplemented vt %d\n", func->prop_vt);
872 IUnknown_Release(iface);
880 static HRESULT invoke_builtin_prop(DispatchEx *This, DISPID id, LCID lcid, WORD flags, DISPPARAMS *dp,
881 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
887 data = get_dispex_data(This);
891 hres = get_builtin_func(data, id, &func);
892 if(id == DISPID_VALUE && hres == DISP_E_UNKNOWNNAME)
893 return dispex_value(This, lcid, flags, dp, res, ei, caller);
897 if(func->func_disp_idx != -1)
898 return function_invoke(This, func, flags, dp, res, ei);
901 case DISPATCH_PROPERTYPUT:
903 V_VT(res) = VT_EMPTY;
904 hres = builtin_propput(This, func, dp, caller);
906 case DISPATCH_PROPERTYGET:
907 hres = builtin_propget(This, func, dp, res);
910 hres = typeinfo_invoke(This, func, flags, dp, res, ei);
916 HRESULT remove_prop(DispatchEx *This, BSTR name, VARIANT_BOOL *success)
918 dynamic_prop_t *prop;
922 hres = get_builtin_id(This, name, 0, &id);
924 DISPID named_id = DISPID_PROPERTYPUT;
926 DISPPARAMS dp = {&var,&named_id,1,1};
929 V_VT(&var) = VT_EMPTY;
930 memset(&ei, 0, sizeof(ei));
931 hres = invoke_builtin_prop(This, id, 0, DISPATCH_PROPERTYPUT, &dp, NULL, &ei, NULL);
935 *success = VARIANT_TRUE;
939 hres = get_dynamic_prop(This, name, 0, &prop);
941 if(hres != DISP_E_UNKNOWNNAME)
943 *success = VARIANT_FALSE;
947 VariantClear(&prop->var);
948 prop->flags |= DYNPROP_DELETED;
949 *success = VARIANT_TRUE;
953 static inline DispatchEx *impl_from_IDispatchEx(IDispatchEx *iface)
955 return CONTAINING_RECORD(iface, DispatchEx, IDispatchEx_iface);
958 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
960 DispatchEx *This = impl_from_IDispatchEx(iface);
962 return IUnknown_QueryInterface(This->outer, riid, ppv);
965 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
967 DispatchEx *This = impl_from_IDispatchEx(iface);
969 return IUnknown_AddRef(This->outer);
972 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
974 DispatchEx *This = impl_from_IDispatchEx(iface);
976 return IUnknown_Release(This->outer);
979 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
981 DispatchEx *This = impl_from_IDispatchEx(iface);
983 TRACE("(%p)->(%p)\n", This, pctinfo);
989 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
990 LCID lcid, ITypeInfo **ppTInfo)
992 DispatchEx *This = impl_from_IDispatchEx(iface);
995 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
997 hres = get_typeinfo(This->data->disp_tid, ppTInfo);
1001 ITypeInfo_AddRef(*ppTInfo);
1005 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
1006 LPOLESTR *rgszNames, UINT cNames,
1007 LCID lcid, DISPID *rgDispId)
1009 DispatchEx *This = impl_from_IDispatchEx(iface);
1013 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
1016 for(i=0; i < cNames; i++) {
1017 hres = IDispatchEx_GetDispID(&This->IDispatchEx_iface, rgszNames[i], 0, rgDispId+i);
1025 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
1026 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
1027 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
1029 DispatchEx *This = impl_from_IDispatchEx(iface);
1031 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1032 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1034 return IDispatchEx_InvokeEx(&This->IDispatchEx_iface, dispIdMember, lcid, wFlags, pDispParams,
1035 pVarResult, pExcepInfo, NULL);
1038 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
1040 DispatchEx *This = impl_from_IDispatchEx(iface);
1041 dynamic_prop_t *dprop;
1044 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
1046 if(grfdex & ~(fdexNameCaseSensitive|fdexNameCaseInsensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK))
1047 FIXME("Unsupported grfdex %x\n", grfdex);
1049 hres = get_builtin_id(This, bstrName, grfdex, pid);
1050 if(hres != DISP_E_UNKNOWNNAME)
1053 hres = get_dynamic_prop(This, bstrName, grfdex, &dprop);
1057 *pid = DISPID_DYNPROP_0 + (dprop - This->dynamic_data->props);
1061 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
1062 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
1064 DispatchEx *This = impl_from_IDispatchEx(iface);
1067 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1069 if(is_custom_dispid(id) && This->data->vtbl && This->data->vtbl->invoke)
1070 return This->data->vtbl->invoke(This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1072 if(wFlags == DISPATCH_CONSTRUCT) {
1073 if(id == DISPID_VALUE) {
1074 if(This->data->vtbl && This->data->vtbl->value) {
1075 return This->data->vtbl->value(This, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1077 FIXME("DISPATCH_CONSTRUCT flag but missing value function\n");
1080 FIXME("DISPATCH_CONSTRUCT flag without DISPID_VALUE\n");
1084 if(is_dynamic_dispid(id)) {
1085 DWORD idx = id - DISPID_DYNPROP_0;
1086 dynamic_prop_t *prop;
1088 if(!get_dynamic_data(This) || This->dynamic_data->prop_cnt <= idx)
1089 return DISP_E_UNKNOWNNAME;
1091 prop = This->dynamic_data->props+idx;
1094 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
1096 return E_INVALIDARG;
1098 case DISPATCH_METHOD: {
1099 DISPID named_arg = DISPID_THIS;
1100 DISPPARAMS dp = {NULL, &named_arg, 0, 1};
1101 IDispatchEx *dispex;
1103 if(V_VT(&prop->var) != VT_DISPATCH) {
1104 FIXME("invoke %s\n", debugstr_variant(&prop->var));
1108 if(pdp->cNamedArgs) {
1109 FIXME("named args not supported\n");
1113 dp.rgvarg = heap_alloc((pdp->cArgs+1)*sizeof(VARIANTARG));
1115 return E_OUTOFMEMORY;
1117 dp.cArgs = pdp->cArgs+1;
1118 memcpy(dp.rgvarg+1, pdp->rgvarg, pdp->cArgs*sizeof(VARIANTARG));
1120 V_VT(dp.rgvarg) = VT_DISPATCH;
1121 V_DISPATCH(dp.rgvarg) = (IDispatch*)&This->IDispatchEx_iface;
1123 hres = IDispatch_QueryInterface(V_DISPATCH(&prop->var), &IID_IDispatchEx, (void**)&dispex);
1124 TRACE("%s call\n", debugstr_w(This->dynamic_data->props[idx].name));
1125 if(SUCCEEDED(hres)) {
1126 hres = IDispatchEx_InvokeEx(dispex, DISPID_VALUE, lcid, wFlags, &dp, pvarRes, pei, pspCaller);
1127 IDispatchEx_Release(dispex);
1130 hres = IDispatch_Invoke(V_DISPATCH(&prop->var), DISPID_VALUE, &IID_NULL, lcid, wFlags, pdp, pvarRes, pei, &err);
1132 TRACE("%s ret %08x\n", debugstr_w(This->dynamic_data->props[idx].name), hres);
1134 heap_free(dp.rgvarg);
1137 case DISPATCH_PROPERTYGET:
1138 if(prop->flags & DYNPROP_DELETED)
1139 return DISP_E_UNKNOWNNAME;
1140 V_VT(pvarRes) = VT_EMPTY;
1141 return variant_copy(pvarRes, &prop->var);
1142 case DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYPUTREF:
1143 case DISPATCH_PROPERTYPUT:
1144 if(pdp->cArgs != 1 || (pdp->cNamedArgs == 1 && *pdp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
1145 || pdp->cNamedArgs > 1) {
1146 FIXME("invalid args\n");
1147 return E_INVALIDARG;
1150 TRACE("put %s\n", debugstr_variant(pdp->rgvarg));
1151 VariantClear(&prop->var);
1152 hres = variant_copy(&prop->var, pdp->rgvarg);
1156 prop->flags &= ~DYNPROP_DELETED;
1159 FIXME("unhandled wFlags %x\n", wFlags);
1164 return invoke_builtin_prop(This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1167 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
1169 DispatchEx *This = impl_from_IDispatchEx(iface);
1171 TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
1173 /* Not implemented by IE */
1177 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
1179 DispatchEx *This = impl_from_IDispatchEx(iface);
1181 TRACE("(%p)->(%x)\n", This, id);
1183 /* Not implemented by IE */
1187 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
1189 DispatchEx *This = impl_from_IDispatchEx(iface);
1190 FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
1194 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
1196 DispatchEx *This = impl_from_IDispatchEx(iface);
1197 dispex_data_t *data;
1201 TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
1203 if(is_dynamic_dispid(id)) {
1204 DWORD idx = id - DISPID_DYNPROP_0;
1206 if(!get_dynamic_data(This) || This->dynamic_data->prop_cnt <= idx)
1207 return DISP_E_UNKNOWNNAME;
1209 *pbstrName = SysAllocString(This->dynamic_data->props[idx].name);
1211 return E_OUTOFMEMORY;
1216 data = get_dispex_data(This);
1220 hres = get_builtin_func(data, id, &func);
1224 *pbstrName = SysAllocString(func->name);
1226 return E_OUTOFMEMORY;
1230 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
1232 DispatchEx *This = impl_from_IDispatchEx(iface);
1233 dispex_data_t *data;
1237 TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
1239 if(is_dynamic_dispid(id)) {
1240 DWORD idx = id - DISPID_DYNPROP_0;
1242 if(!get_dynamic_data(This) || This->dynamic_data->prop_cnt <= idx)
1243 return DISP_E_UNKNOWNNAME;
1245 while(++idx < This->dynamic_data->prop_cnt && This->dynamic_data->props[idx].flags & DYNPROP_DELETED);
1247 if(idx == This->dynamic_data->prop_cnt) {
1248 *pid = DISPID_STARTENUM;
1252 *pid = DISPID_DYNPROP_0+idx;
1256 data = get_dispex_data(This);
1260 if(id == DISPID_STARTENUM) {
1263 hres = get_builtin_func(data, id, &func);
1269 while(func < data->funcs+data->func_cnt) {
1270 /* FIXME: Skip hidden properties */
1271 if(func->func_disp_idx == -1) {
1278 if(get_dynamic_data(This) && This->dynamic_data->prop_cnt) {
1279 *pid = DISPID_DYNPROP_0;
1283 *pid = DISPID_STARTENUM;
1287 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
1289 DispatchEx *This = impl_from_IDispatchEx(iface);
1290 FIXME("(%p)->(%p)\n", This, ppunk);
1294 static IDispatchExVtbl DispatchExVtbl = {
1295 DispatchEx_QueryInterface,
1298 DispatchEx_GetTypeInfoCount,
1299 DispatchEx_GetTypeInfo,
1300 DispatchEx_GetIDsOfNames,
1302 DispatchEx_GetDispID,
1303 DispatchEx_InvokeEx,
1304 DispatchEx_DeleteMemberByName,
1305 DispatchEx_DeleteMemberByDispID,
1306 DispatchEx_GetMemberProperties,
1307 DispatchEx_GetMemberName,
1308 DispatchEx_GetNextDispID,
1309 DispatchEx_GetNameSpaceParent
1312 BOOL dispex_query_interface(DispatchEx *This, REFIID riid, void **ppv)
1314 static const IID IID_UndocumentedScriptIface =
1315 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa0}};
1316 static const IID IID_IDispatchJS =
1317 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa6}};
1319 if(IsEqualGUID(&IID_IDispatch, riid)) {
1320 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
1321 *ppv = &This->IDispatchEx_iface;
1322 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
1323 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
1324 *ppv = &This->IDispatchEx_iface;
1325 }else if(IsEqualGUID(&IID_IDispatchJS, riid)) {
1326 TRACE("(%p)->(IID_IDispatchJS %p) returning NULL\n", This, ppv);
1328 }else if(IsEqualGUID(&IID_UndocumentedScriptIface, riid)) {
1329 TRACE("(%p)->(IID_UndocumentedScriptIface %p) returning NULL\n", This, ppv);
1336 IUnknown_AddRef((IUnknown*)*ppv);
1340 void release_dispex(DispatchEx *This)
1342 dynamic_prop_t *prop;
1344 if(!This->dynamic_data)
1347 for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++) {
1348 VariantClear(&prop->var);
1349 heap_free(prop->name);
1352 heap_free(This->dynamic_data->props);
1354 if(This->dynamic_data->func_disps) {
1357 for(i=0; i < This->data->data->func_disp_cnt; i++) {
1358 if(This->dynamic_data->func_disps[i]) {
1359 release_dispex(&This->dynamic_data->func_disps[i]->dispex);
1360 heap_free(This->dynamic_data->func_disps[i]);
1364 heap_free(This->dynamic_data->func_disps);
1367 heap_free(This->dynamic_data);
1370 void init_dispex(DispatchEx *dispex, IUnknown *outer, dispex_static_data_t *data)
1372 dispex->IDispatchEx_iface.lpVtbl = &DispatchExVtbl;
1373 dispex->outer = outer;
1374 dispex->data = data;
1375 dispex->dynamic_data = NULL;