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 HRESULT dispex_get_dynid(DispatchEx *This, const WCHAR *name, DISPID *id)
485 dynamic_prop_t *prop;
488 hres = get_dynamic_prop(This, name, fdexNameEnsure, &prop);
492 *id = DISPID_DYNPROP_0 + (prop - This->dynamic_data->props);
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);
503 case DISPATCH_PROPERTYGET:
505 V_BSTR(res) = SysAllocString(objectW);
507 return E_OUTOFMEMORY;
510 FIXME("Unimplemented flags %x\n", flags);
517 static HRESULT typeinfo_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
525 hres = get_typeinfo(func->tid, &ti);
527 ERR("Could not get type info: %08x\n", hres);
531 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&unk);
533 ERR("Could not get iface %s: %08x\n", debugstr_guid(tid_ids[func->tid]), hres);
537 hres = ITypeInfo_Invoke(ti, unk, func->id, flags, dp, res, ei, &argerr);
539 IUnknown_Release(unk);
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;
559 return E_NOINTERFACE;
562 IUnknown_AddRef((IUnknown*)*ppv);
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,
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);
602 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
606 case DISPATCH_METHOD:
607 hres = typeinfo_invoke(This->obj, This->info, flags, params, res, ei);
610 FIXME("Unimplemented flags %x\n", flags);
617 static const dispex_static_data_vtbl_t function_dispex_vtbl = {
624 static const tid_t function_iface_tids[] = {0};
626 static dispex_static_data_t function_dispex = {
627 &function_dispex_vtbl,
633 static func_disp_t *create_func_disp(DispatchEx *obj, func_info_t *info)
637 ret = heap_alloc_zero(sizeof(func_disp_t));
641 ret->IUnknown_iface.lpVtbl = &FunctionUnkVtbl;
642 init_dispex(&ret->dispex, &ret->IUnknown_iface, &function_dispex);
649 static HRESULT function_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
655 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
659 case DISPATCH_METHOD:
660 hres = typeinfo_invoke(This, func, flags, dp, res, ei);
662 case DISPATCH_PROPERTYGET: {
663 dispex_dynamic_data_t *dynamic_data;
665 if(func->id == DISPID_VALUE) {
668 ret = SysAllocString(objectW);
670 return E_OUTOFMEMORY;
677 dynamic_data = get_dynamic_data(This);
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));
700 FIXME("Unimplemented flags %x\n", flags);
702 case DISPATCH_PROPERTYPUT:
709 static HRESULT get_builtin_func(dispex_data_t *data, DISPID id, func_info_t **ret)
714 max = data->func_cnt-1;
719 if(data->funcs[n].id == id) {
720 *ret = data->funcs+n;
724 if(data->funcs[n].id < id)
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)
739 data = get_dispex_data(This);
744 max = data->func_cnt-1;
749 c = strcmpiW(data->name_table[n]->name, name);
751 if((grfdex & fdexNameCaseSensitive) && strcmpW(data->name_table[n]->name, name))
754 *ret = data->name_table[n]->id;
764 if(This->data->vtbl && This->data->vtbl->get_dispid) {
767 hres = This->data->vtbl->get_dispid(This, name, grfdex, ret);
768 if(hres != DISP_E_UNKNOWNNAME)
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;
792 IVariantChangeType *change_type = NULL;
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);
803 return VariantChangeType(dst, src, 0, vt);
806 static HRESULT builtin_propget(DispatchEx *This, func_info_t *func, DISPPARAMS *dp, VARIANT *res)
812 FIXME("cArgs %d\n", dp->cArgs);
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) \
824 hres = ((HRESULT (WINAPI*)(IUnknown*,type*))((void**)iface->lpVtbl)[func->get_vtbl_off])(iface,&val); \
825 if(SUCCEEDED(hres)) \
829 BUILTIN_TYPES_SWITCH;
832 FIXME("Unhandled vt %d\n", func->prop_vt);
835 IUnknown_Release(iface);
841 if(func->prop_vt != VT_VARIANT)
842 V_VT(res) = func->prop_vt == VT_PTR ? VT_DISPATCH : func->prop_vt;
846 static HRESULT builtin_propput(DispatchEx *This, func_info_t *func, DISPPARAMS *dp, IServiceProvider *caller)
852 if(dp->cArgs != 1 || (dp->cNamedArgs == 1 && *dp->rgdispidNamedArgs != DISPID_PROPERTYPUT)
853 || dp->cNamedArgs > 1) {
854 FIXME("invalid args\n");
858 if(!func->put_vtbl_off) {
859 FIXME("No setter\n");
864 if(func->prop_vt != VT_VARIANT && V_VT(v) != func->prop_vt) {
865 hres = change_type(&tmpv, v, func->prop_vt, caller);
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) \
876 hres = ((HRESULT (WINAPI*)(IUnknown*,type))((void**)iface->lpVtbl)[func->put_vtbl_off])(iface,access(v)); \
878 BUILTIN_TYPES_SWITCH;
881 FIXME("Unimplemented vt %d\n", func->prop_vt);
885 IUnknown_Release(iface);
893 static HRESULT invoke_builtin_prop(DispatchEx *This, DISPID id, LCID lcid, WORD flags, DISPPARAMS *dp,
894 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
900 data = get_dispex_data(This);
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);
910 if(func->func_disp_idx != -1)
911 return function_invoke(This, func, flags, dp, res, ei);
914 case DISPATCH_PROPERTYPUT:
916 V_VT(res) = VT_EMPTY;
917 hres = builtin_propput(This, func, dp, caller);
919 case DISPATCH_PROPERTYGET:
920 hres = builtin_propget(This, func, dp, res);
923 hres = typeinfo_invoke(This, func, flags, dp, res, ei);
929 HRESULT remove_prop(DispatchEx *This, BSTR name, VARIANT_BOOL *success)
931 dynamic_prop_t *prop;
935 hres = get_builtin_id(This, name, 0, &id);
937 DISPID named_id = DISPID_PROPERTYPUT;
939 DISPPARAMS dp = {&var,&named_id,1,1};
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);
948 *success = VARIANT_TRUE;
952 hres = get_dynamic_prop(This, name, 0, &prop);
954 if(hres != DISP_E_UNKNOWNNAME)
956 *success = VARIANT_FALSE;
960 VariantClear(&prop->var);
961 prop->flags |= DYNPROP_DELETED;
962 *success = VARIANT_TRUE;
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);
1002 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
1003 LCID lcid, ITypeInfo **ppTInfo)
1005 DispatchEx *This = impl_from_IDispatchEx(iface);
1008 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
1010 hres = get_typeinfo(This->data->disp_tid, ppTInfo);
1014 ITypeInfo_AddRef(*ppTInfo);
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);
1026 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
1029 for(i=0; i < cNames; i++) {
1030 hres = IDispatchEx_GetDispID(&This->IDispatchEx_iface, rgszNames[i], 0, rgDispId+i);
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;
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)
1066 hres = get_dynamic_prop(This, bstrName, grfdex, &dprop);
1070 *pid = DISPID_DYNPROP_0 + (dprop - This->dynamic_data->props);
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);
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");
1093 FIXME("DISPATCH_CONSTRUCT flag without DISPID_VALUE\n");
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;
1107 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
1109 return E_INVALIDARG;
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));
1121 if(pdp->cNamedArgs) {
1122 FIXME("named args not supported\n");
1126 dp.rgvarg = heap_alloc((pdp->cArgs+1)*sizeof(VARIANTARG));
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);
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);
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);
1169 prop->flags &= ~DYNPROP_DELETED;
1172 FIXME("unhandled wFlags %x\n", wFlags);
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 */
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 */
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);
1207 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
1209 DispatchEx *This = impl_from_IDispatchEx(iface);
1210 dispex_data_t *data;
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);
1224 return E_OUTOFMEMORY;
1229 data = get_dispex_data(This);
1233 hres = get_builtin_func(data, id, &func);
1237 *pbstrName = SysAllocString(func->name);
1239 return E_OUTOFMEMORY;
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;
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;
1265 *pid = DISPID_DYNPROP_0+idx;
1269 data = get_dispex_data(This);
1273 if(id == DISPID_STARTENUM) {
1276 hres = get_builtin_func(data, id, &func);
1282 while(func < data->funcs+data->func_cnt) {
1283 /* FIXME: Skip hidden properties */
1284 if(func->func_disp_idx == -1) {
1291 if(get_dynamic_data(This) && This->dynamic_data->prop_cnt) {
1292 *pid = DISPID_DYNPROP_0;
1296 *pid = DISPID_STARTENUM;
1300 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
1302 DispatchEx *This = impl_from_IDispatchEx(iface);
1303 FIXME("(%p)->(%p)\n", This, ppunk);
1307 static IDispatchExVtbl DispatchExVtbl = {
1308 DispatchEx_QueryInterface,
1311 DispatchEx_GetTypeInfoCount,
1312 DispatchEx_GetTypeInfo,
1313 DispatchEx_GetIDsOfNames,
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);
1341 }else if(IsEqualGUID(&IID_UndocumentedScriptIface, riid)) {
1342 TRACE("(%p)->(IID_UndocumentedScriptIface %p) returning NULL\n", This, ppv);
1349 IUnknown_AddRef((IUnknown*)*ppv);
1353 void release_dispex(DispatchEx *This)
1355 dynamic_prop_t *prop;
1357 if(!This->dynamic_data)
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) {
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;