msctf: Correct handling if NULL for pdimFocus in ITfThreadMgr::SetFocus.
[wine/multimedia.git] / dlls / mshtml / dispex.c
blobb8937e7c29e4549ddef671d17a8639af638a620c
1 /*
2 * Copyright 2008 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>
21 #define COBJMACROS
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "ole2.h"
28 #include "wine/debug.h"
30 #include "mshtml_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
34 typedef struct {
35 DISPID id;
36 BSTR name;
37 tid_t tid;
38 int func_disp_idx;
39 } func_info_t;
41 struct dispex_data_t {
42 DWORD func_cnt;
43 func_info_t *funcs;
44 func_info_t **name_table;
45 DWORD func_disp_cnt;
47 struct list entry;
50 typedef struct {
51 VARIANT var;
52 LPWSTR name;
53 } dynamic_prop_t;
55 typedef struct {
56 DispatchEx dispex;
57 const IUnknownVtbl *lpIUnknownVtbl;
58 DispatchEx *obj;
59 func_info_t *info;
60 } func_disp_t;
62 #define FUNCUNKNOWN(x) ((IUnknown*) &(x)->lpIUnknownVtbl)
64 struct dispex_dynamic_data_t {
65 DWORD buf_size;
66 DWORD prop_cnt;
67 dynamic_prop_t *props;
68 func_disp_t **func_disps;
71 #define DISPID_DYNPROP_0 0x50000000
72 #define DISPID_DYNPROP_MAX 0x5fffffff
74 static ITypeLib *typelib;
75 static ITypeInfo *typeinfos[LAST_tid];
76 static struct list dispex_data_list = LIST_INIT(dispex_data_list);
78 static REFIID tid_ids[] = {
79 &IID_NULL,
80 &DIID_DispCEventObj,
81 &DIID_DispDOMChildrenCollection,
82 &DIID_DispHTMLBody,
83 &DIID_DispHTMLCommentElement,
84 &DIID_DispHTMLCurrentStyle,
85 &DIID_DispHTMLDocument,
86 &DIID_DispHTMLDOMTextNode,
87 &DIID_DispHTMLElementCollection,
88 &DIID_DispHTMLGenericElement,
89 &DIID_DispHTMLIFrame,
90 &DIID_DispHTMLImg,
91 &DIID_DispHTMLInputElement,
92 &DIID_DispHTMLLocation,
93 &DIID_DispHTMLNavigator,
94 &DIID_DispHTMLOptionElement,
95 &DIID_DispHTMLSelectElement,
96 &DIID_DispHTMLStyle,
97 &DIID_DispHTMLTable,
98 &DIID_DispHTMLTableRow,
99 &DIID_DispHTMLUnknownElement,
100 &DIID_DispHTMLWindow2,
101 &IID_IHTMLBodyElement,
102 &IID_IHTMLBodyElement2,
103 &IID_IHTMLCommentElement,
104 &IID_IHTMLCurrentStyle,
105 &IID_IHTMLCurrentStyle2,
106 &IID_IHTMLCurrentStyle3,
107 &IID_IHTMLCurrentStyle4,
108 &IID_IHTMLDocument2,
109 &IID_IHTMLDocument3,
110 &IID_IHTMLDocument4,
111 &IID_IHTMLDocument5,
112 &IID_IHTMLDOMChildrenCollection,
113 &IID_IHTMLDOMNode,
114 &IID_IHTMLDOMNode2,
115 &IID_IHTMLDOMTextNode,
116 &IID_IHTMLElement,
117 &IID_IHTMLElement2,
118 &IID_IHTMLElement3,
119 &IID_IHTMLElement4,
120 &IID_IHTMLElementCollection,
121 &IID_IHTMLEventObj,
122 &IID_IHTMLFrameBase2,
123 &IID_IHTMLGenericElement,
124 &IID_IHTMLImgElement,
125 &IID_IHTMLInputElement,
126 &IID_IHTMLLocation,
127 &IID_IHTMLOptionElement,
128 &IID_IHTMLSelectElement,
129 &IID_IHTMLStyle,
130 &IID_IHTMLStyle2,
131 &IID_IHTMLStyle3,
132 &IID_IHTMLStyle4,
133 &IID_IHTMLTable,
134 &IID_IHTMLTableRow,
135 &IID_IHTMLTextContainer,
136 &IID_IHTMLUniqueName,
137 &IID_IHTMLWindow2,
138 &IID_IHTMLWindow3,
139 &IID_IOmNavigator
142 static HRESULT get_typeinfo(tid_t tid, ITypeInfo **typeinfo)
144 HRESULT hres;
146 if(!typelib) {
147 ITypeLib *tl;
149 hres = LoadRegTypeLib(&LIBID_MSHTML, 4, 0, LOCALE_SYSTEM_DEFAULT, &tl);
150 if(FAILED(hres)) {
151 ERR("LoadRegTypeLib failed: %08x\n", hres);
152 return hres;
155 if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
156 ITypeLib_Release(tl);
159 if(!typeinfos[tid]) {
160 ITypeInfo *typeinfo;
162 hres = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &typeinfo);
163 if(FAILED(hres)) {
164 ERR("GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_guid(tid_ids[tid]), hres);
165 return hres;
168 if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), typeinfo, NULL))
169 ITypeInfo_Release(typeinfo);
172 *typeinfo = typeinfos[tid];
173 return S_OK;
176 void release_typelib(void)
178 dispex_data_t *iter;
179 unsigned i;
181 while(!list_empty(&dispex_data_list)) {
182 iter = LIST_ENTRY(list_head(&dispex_data_list), dispex_data_t, entry);
183 list_remove(&iter->entry);
185 for(i=0; i < iter->func_cnt; i++)
186 SysFreeString(iter->funcs[i].name);
188 heap_free(iter->funcs);
189 heap_free(iter->name_table);
190 heap_free(iter);
193 if(!typelib)
194 return;
196 for(i=0; i < sizeof(typeinfos)/sizeof(*typeinfos); i++)
197 if(typeinfos[i])
198 ITypeInfo_Release(typeinfos[i]);
200 ITypeLib_Release(typelib);
203 static void add_func_info(dispex_data_t *data, DWORD *size, tid_t tid, const FUNCDESC *desc, ITypeInfo *dti)
205 HRESULT hres;
207 if(data->func_cnt && data->funcs[data->func_cnt-1].id == desc->memid)
208 return;
210 if(data->func_cnt == *size)
211 data->funcs = heap_realloc(data->funcs, (*size <<= 1)*sizeof(func_info_t));
213 hres = ITypeInfo_GetDocumentation(dti, desc->memid, &data->funcs[data->func_cnt].name, NULL, NULL, NULL);
214 if(FAILED(hres))
215 return;
217 data->funcs[data->func_cnt].id = desc->memid;
218 data->funcs[data->func_cnt].tid = tid;
219 data->funcs[data->func_cnt].func_disp_idx = desc->invkind == INVOKE_FUNC ? data->func_disp_cnt++ : -1;
221 data->func_cnt++;
224 static int dispid_cmp(const void *p1, const void *p2)
226 return ((func_info_t*)p1)->id - ((func_info_t*)p2)->id;
229 static int func_name_cmp(const void *p1, const void *p2)
231 return strcmpiW((*(func_info_t**)p1)->name, (*(func_info_t**)p2)->name);
234 static dispex_data_t *preprocess_dispex_data(DispatchEx *This)
236 const tid_t *tid = This->data->iface_tids;
237 FUNCDESC *funcdesc;
238 dispex_data_t *data;
239 DWORD size = 16, i;
240 ITypeInfo *ti, *dti;
241 HRESULT hres;
243 TRACE("(%p)\n", This);
245 hres = get_typeinfo(This->data->disp_tid, &dti);
246 if(FAILED(hres)) {
247 ERR("Could not get disp type info: %08x\n", hres);
248 return NULL;
251 data = heap_alloc(sizeof(dispex_data_t));
252 data->func_cnt = 0;
253 data->func_disp_cnt = 0;
254 data->funcs = heap_alloc(size*sizeof(func_info_t));
255 list_add_tail(&dispex_data_list, &data->entry);
257 while(*tid) {
258 hres = get_typeinfo(*tid, &ti);
259 if(FAILED(hres))
260 break;
262 i=7;
263 while(1) {
264 hres = ITypeInfo_GetFuncDesc(ti, i++, &funcdesc);
265 if(FAILED(hres))
266 break;
268 add_func_info(data, &size, *tid, funcdesc, dti);
269 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
272 tid++;
275 if(!data->func_cnt) {
276 heap_free(data->funcs);
277 data->funcs = NULL;
278 }else if(data->func_cnt != size) {
279 data->funcs = heap_realloc(data->funcs, data->func_cnt * sizeof(func_info_t));
282 qsort(data->funcs, data->func_cnt, sizeof(func_info_t), dispid_cmp);
284 if(data->funcs) {
285 data->name_table = heap_alloc(data->func_cnt * sizeof(func_info_t*));
286 for(i=0; i < data->func_cnt; i++)
287 data->name_table[i] = data->funcs+i;
288 qsort(data->name_table, data->func_cnt, sizeof(func_info_t*), func_name_cmp);
289 }else {
290 data->name_table = NULL;
293 return data;
296 static CRITICAL_SECTION cs_dispex_static_data;
297 static CRITICAL_SECTION_DEBUG cs_dispex_static_data_dbg =
299 0, 0, &cs_dispex_static_data,
300 { &cs_dispex_static_data_dbg.ProcessLocksList, &cs_dispex_static_data_dbg.ProcessLocksList },
301 0, 0, { (DWORD_PTR)(__FILE__ ": dispex_static_data") }
303 static CRITICAL_SECTION cs_dispex_static_data = { &cs_dispex_static_data_dbg, -1, 0, 0, 0, 0 };
306 static dispex_data_t *get_dispex_data(DispatchEx *This)
308 if(This->data->data)
309 return This->data->data;
311 EnterCriticalSection(&cs_dispex_static_data);
313 if(!This->data->data)
314 This->data->data = preprocess_dispex_data(This);
316 LeaveCriticalSection(&cs_dispex_static_data);
318 return This->data->data;
321 void call_disp_func(HTMLDocument *doc, IDispatch *disp, IDispatch *this_obj)
323 DISPID named_arg = DISPID_THIS;
324 VARIANTARG arg;
325 DISPPARAMS params = {&arg, &named_arg, 1, 1};
326 EXCEPINFO ei;
327 IDispatchEx *dispex;
328 VARIANT res;
329 HRESULT hres;
331 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
332 if(FAILED(hres)) {
333 FIXME("Could not get IDispatchEx interface: %08x\n", hres);
334 return;
337 V_VT(&arg) = VT_DISPATCH;
338 V_DISPATCH(&arg) = this_obj;
339 VariantInit(&res);
340 memset(&ei, 0, sizeof(ei));
342 hres = IDispatchEx_InvokeEx(dispex, 0, GetUserDefaultLCID(), DISPATCH_METHOD, &params, &res, &ei, NULL);
343 IDispatchEx_Release(dispex);
345 TRACE("%p returned %08x\n", disp, hres);
347 VariantClear(&res);
350 static inline BOOL is_custom_dispid(DISPID id)
352 return MSHTML_DISPID_CUSTOM_MIN <= id && id <= MSHTML_DISPID_CUSTOM_MAX;
355 static inline BOOL is_dynamic_dispid(DISPID id)
357 return DISPID_DYNPROP_0 <= id && id <= DISPID_DYNPROP_MAX;
360 static inline dispex_dynamic_data_t *get_dynamic_data(DispatchEx *This, BOOL alloc)
362 return !alloc || This->dynamic_data
363 ? This->dynamic_data
364 : (This->dynamic_data = heap_alloc_zero(sizeof(dispex_dynamic_data_t)));
367 static HRESULT get_dynamic_prop(DispatchEx *This, const WCHAR *name, BOOL alloc, dynamic_prop_t **ret)
369 dispex_dynamic_data_t *data = get_dynamic_data(This, alloc);
370 unsigned i;
372 if(!data) {
373 if(alloc)
374 return E_OUTOFMEMORY;
376 TRACE("not found %s\n", debugstr_w(name));
377 return DISP_E_UNKNOWNNAME;
380 for(i=0; i < data->prop_cnt; i++) {
381 if(!strcmpW(data->props[i].name, name)) {
382 *ret = data->props+i;
383 return S_OK;
387 TRACE("creating dynamic prop %s\n", debugstr_w(name));
389 if(!data->buf_size) {
390 data->props = heap_alloc(sizeof(dynamic_prop_t)*4);
391 if(!data->props)
392 return E_OUTOFMEMORY;
393 data->buf_size = 4;
394 }else if(data->buf_size == data->prop_cnt) {
395 dynamic_prop_t *new_props;
397 new_props = heap_realloc(data->props, sizeof(dynamic_prop_t)*(data->buf_size<<1));
398 if(!new_props)
399 return E_OUTOFMEMORY;
401 data->props = new_props;
402 data->buf_size <<= 1;
405 data->props[data->prop_cnt].name = heap_strdupW(name);
406 VariantInit(&data->props[data->prop_cnt].var);
407 *ret = data->props + data->prop_cnt++;
409 return S_OK;
412 HRESULT dispex_get_dprop_ref(DispatchEx *This, const WCHAR *name, BOOL alloc, VARIANT **ret)
414 dynamic_prop_t *prop;
415 HRESULT hres;
417 hres = get_dynamic_prop(This, name, alloc, &prop);
418 if(FAILED(hres))
419 return hres;
421 *ret = &prop->var;
422 return S_OK;
425 static HRESULT dispex_value(DispatchEx *This, LCID lcid, WORD flags, DISPPARAMS *params,
426 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
428 static const WCHAR objectW[] = {'[','o','b','j','e','c','t',']',0};
430 if(This->data->vtbl && This->data->vtbl->value)
431 return This->data->vtbl->value(This->outer, lcid, flags, params, res, ei, caller);
433 switch(flags) {
434 case DISPATCH_PROPERTYGET:
435 V_VT(res) = VT_BSTR;
436 V_BSTR(res) = SysAllocString(objectW);
437 if(!V_BSTR(res))
438 return E_OUTOFMEMORY;
439 break;
440 default:
441 FIXME("Unimplemented flags %x\n", flags);
442 return E_NOTIMPL;
445 return S_OK;
448 static HRESULT typeinfo_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
449 EXCEPINFO *ei)
451 ITypeInfo *ti;
452 IUnknown *unk;
453 UINT argerr=0;
454 HRESULT hres;
456 hres = get_typeinfo(func->tid, &ti);
457 if(FAILED(hres)) {
458 ERR("Could not get type info: %08x\n", hres);
459 return hres;
462 hres = IUnknown_QueryInterface(This->outer, tid_ids[func->tid], (void**)&unk);
463 if(FAILED(hres)) {
464 ERR("Could not get iface %s: %08x\n", debugstr_guid(tid_ids[func->tid]), hres);
465 return E_FAIL;
468 hres = ITypeInfo_Invoke(ti, unk, func->id, flags, dp, res, ei, &argerr);
470 IUnknown_Release(unk);
471 return hres;
474 #define FUNCTION_THIS(iface) DEFINE_THIS(func_disp_t, IUnknown, iface)
476 static HRESULT WINAPI Function_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
478 func_disp_t *This = FUNCTION_THIS(iface);
480 if(IsEqualGUID(&IID_IUnknown, riid)) {
481 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
482 *ppv = FUNCUNKNOWN(This);
483 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
484 return *ppv ? S_OK : E_NOINTERFACE;
485 }else {
486 *ppv = NULL;
487 return E_NOINTERFACE;
490 IUnknown_AddRef((IUnknown*)*ppv);
491 return S_OK;
494 static ULONG WINAPI Function_AddRef(IUnknown *iface)
496 func_disp_t *This = FUNCTION_THIS(iface);
498 TRACE("(%p)\n", This);
500 return IDispatchEx_AddRef(DISPATCHEX(This->obj));
503 static ULONG WINAPI Function_Release(IUnknown *iface)
505 func_disp_t *This = FUNCTION_THIS(iface);
507 TRACE("(%p)\n", This);
509 return IDispatchEx_Release(DISPATCHEX(This->obj));
512 static HRESULT function_value(IUnknown *iface, LCID lcid, WORD flags, DISPPARAMS *params,
513 VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
515 func_disp_t *This = FUNCTION_THIS(iface);
516 HRESULT hres;
518 switch(flags) {
519 case DISPATCH_METHOD:
520 hres = typeinfo_invoke(This->obj, This->info, flags, params, res, ei);
521 break;
522 default:
523 FIXME("Unimplemented flags %x\n", flags);
524 hres = E_NOTIMPL;
527 return hres;
530 #undef FUNCTION_THIS
532 static const IUnknownVtbl FunctionUnkVtbl = {
533 Function_QueryInterface,
534 Function_AddRef,
535 Function_Release
538 static const dispex_static_data_vtbl_t function_dispex_vtbl = {
539 function_value,
540 NULL,
541 NULL
544 static dispex_static_data_t function_dispex = {
545 &function_dispex_vtbl,
546 LAST_tid,
547 NULL,
548 NULL
551 static func_disp_t *create_func_disp(DispatchEx *obj, func_info_t *info)
553 func_disp_t *ret;
555 ret = heap_alloc_zero(sizeof(func_disp_t));
556 if(!ret)
557 return NULL;
559 ret->lpIUnknownVtbl = &FunctionUnkVtbl;
560 init_dispex(&ret->dispex, FUNCUNKNOWN(ret), &function_dispex);
561 ret->obj = obj;
562 ret->info = info;
564 return ret;
567 static HRESULT function_invoke(DispatchEx *This, func_info_t *func, WORD flags, DISPPARAMS *dp, VARIANT *res,
568 EXCEPINFO *ei)
570 HRESULT hres;
572 switch(flags) {
573 case DISPATCH_METHOD:
574 hres = typeinfo_invoke(This, func, flags, dp, res, ei);
575 break;
576 case DISPATCH_PROPERTYGET: {
577 dispex_dynamic_data_t *dynamic_data;
579 dynamic_data = get_dynamic_data(This, TRUE);
580 if(!dynamic_data)
581 return E_OUTOFMEMORY;
583 if(!dynamic_data->func_disps) {
584 dynamic_data->func_disps = heap_alloc_zero(This->data->data->func_disp_cnt * sizeof(func_disp_t*));
585 if(!dynamic_data->func_disps)
586 return E_OUTOFMEMORY;
589 if(!dynamic_data->func_disps[func->func_disp_idx]) {
590 dynamic_data->func_disps[func->func_disp_idx] = create_func_disp(This, func);
591 if(!dynamic_data->func_disps[func->func_disp_idx])
592 return E_OUTOFMEMORY;
595 V_VT(res) = VT_DISPATCH;
596 V_DISPATCH(res) = (IDispatch*)DISPATCHEX(&dynamic_data->func_disps[func->func_disp_idx]->dispex);
597 IDispatch_AddRef(V_DISPATCH(res));
598 hres = S_OK;
599 break;
601 default:
602 FIXME("Unimplemented flags %x\n", flags);
603 case DISPATCH_PROPERTYPUT:
604 hres = E_NOTIMPL;
607 return hres;
610 #define DISPATCHEX_THIS(iface) DEFINE_THIS(DispatchEx, IDispatchEx, iface)
612 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
614 DispatchEx *This = DISPATCHEX_THIS(iface);
616 return IUnknown_QueryInterface(This->outer, riid, ppv);
619 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
621 DispatchEx *This = DISPATCHEX_THIS(iface);
623 return IUnknown_AddRef(This->outer);
626 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
628 DispatchEx *This = DISPATCHEX_THIS(iface);
630 return IUnknown_Release(This->outer);
633 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
635 DispatchEx *This = DISPATCHEX_THIS(iface);
637 TRACE("(%p)->(%p)\n", This, pctinfo);
639 *pctinfo = 1;
640 return S_OK;
643 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
644 LCID lcid, ITypeInfo **ppTInfo)
646 DispatchEx *This = DISPATCHEX_THIS(iface);
647 HRESULT hres;
649 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
651 hres = get_typeinfo(This->data->disp_tid, ppTInfo);
652 if(FAILED(hres))
653 return hres;
655 ITypeInfo_AddRef(*ppTInfo);
656 return S_OK;
659 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
660 LPOLESTR *rgszNames, UINT cNames,
661 LCID lcid, DISPID *rgDispId)
663 DispatchEx *This = DISPATCHEX_THIS(iface);
664 UINT i;
665 HRESULT hres;
667 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
668 lcid, rgDispId);
670 for(i=0; i < cNames; i++) {
671 hres = IDispatchEx_GetDispID(DISPATCHEX(This), rgszNames[i], 0, rgDispId+i);
672 if(FAILED(hres))
673 return hres;
676 return S_OK;
679 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
680 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
681 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
683 DispatchEx *This = DISPATCHEX_THIS(iface);
685 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
686 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
688 return IDispatchEx_InvokeEx(DISPATCHEX(This), dispIdMember, lcid, wFlags,
689 pDispParams, pVarResult, pExcepInfo, NULL);
692 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
694 DispatchEx *This = DISPATCHEX_THIS(iface);
695 dynamic_prop_t *dprop;
696 dispex_data_t *data;
697 int min, max, n, c;
698 HRESULT hres;
700 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
702 if(grfdex & ~(fdexNameCaseSensitive|fdexNameEnsure|fdexNameImplicit))
703 FIXME("Unsupported grfdex %x\n", grfdex);
705 data = get_dispex_data(This);
706 if(!data)
707 return E_FAIL;
709 min = 0;
710 max = data->func_cnt-1;
712 while(min <= max) {
713 n = (min+max)/2;
715 c = strcmpiW(data->name_table[n]->name, bstrName);
716 if(!c) {
717 if((grfdex & fdexNameCaseSensitive) && strcmpW(data->name_table[n]->name, bstrName))
718 break;
720 *pid = data->name_table[n]->id;
721 return S_OK;
724 if(c > 0)
725 max = n-1;
726 else
727 min = n+1;
730 if(This->data->vtbl && This->data->vtbl->get_dispid) {
731 HRESULT hres;
733 hres = This->data->vtbl->get_dispid(This->outer, bstrName, grfdex, pid);
734 if(hres != DISP_E_UNKNOWNNAME)
735 return hres;
738 hres = get_dynamic_prop(This, bstrName, grfdex&fdexNameEnsure, &dprop);
739 if(FAILED(hres))
740 return hres;
742 *pid = DISPID_DYNPROP_0 + (dprop - This->dynamic_data->props);
743 return S_OK;
746 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
747 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
749 DispatchEx *This = DISPATCHEX_THIS(iface);
750 dispex_data_t *data;
751 int min, max, n;
752 HRESULT hres;
754 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
756 if(id == DISPID_VALUE)
757 return dispex_value(This, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
759 if(is_custom_dispid(id) && This->data->vtbl && This->data->vtbl->invoke)
760 return This->data->vtbl->invoke(This->outer, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
762 if(wFlags == DISPATCH_CONSTRUCT) {
763 FIXME("DISPATCH_CONSTRUCT not implemented\n");
764 return E_NOTIMPL;
767 if(is_dynamic_dispid(id)) {
768 DWORD idx = id - DISPID_DYNPROP_0;
769 VARIANT *var;
771 if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
772 return DISP_E_UNKNOWNNAME;
774 var = &This->dynamic_data->props[idx].var;
776 switch(wFlags) {
777 case INVOKE_FUNC: {
778 DISPID named_arg = DISPID_THIS;
779 DISPPARAMS dp = {NULL, &named_arg, 0, 1};
780 IDispatchEx *dispex;
782 if(V_VT(var) != VT_DISPATCH) {
783 FIXME("invoke vt %d\n", V_VT(var));
784 return E_NOTIMPL;
787 if(pdp->cNamedArgs) {
788 FIXME("named args not supported\n");
789 return E_NOTIMPL;
792 dp.rgvarg = heap_alloc((pdp->cArgs+1)*sizeof(VARIANTARG));
793 if(!dp.rgvarg)
794 return E_OUTOFMEMORY;
796 dp.cArgs = pdp->cArgs+1;
797 memcpy(dp.rgvarg+1, pdp->rgvarg, pdp->cArgs*sizeof(VARIANTARG));
799 V_VT(dp.rgvarg) = VT_DISPATCH;
800 V_DISPATCH(dp.rgvarg) = (IDispatch*)DISPATCHEX(This);
802 hres = IDispatch_QueryInterface(V_DISPATCH(var), &IID_IDispatchEx, (void**)&dispex);
803 TRACE("%s call\n", debugstr_w(This->dynamic_data->props[idx].name));
804 if(SUCCEEDED(hres)) {
805 hres = IDispatchEx_InvokeEx(dispex, DISPID_VALUE, lcid, wFlags, &dp, pvarRes, pei, pspCaller);
806 IDispatchEx_Release(dispex);
807 }else {
808 ULONG err = 0;
809 hres = IDispatch_Invoke(V_DISPATCH(var), DISPID_VALUE, &IID_NULL, lcid, wFlags, pdp, pvarRes, pei, &err);
811 TRACE("%s ret %08x\n", debugstr_w(This->dynamic_data->props[idx].name), hres);
813 heap_free(dp.rgvarg);
814 return hres;
816 case INVOKE_PROPERTYGET:
817 return VariantCopy(pvarRes, var);
818 case INVOKE_PROPERTYPUT:
819 VariantClear(var);
820 return VariantCopy(var, pdp->rgvarg);
821 default:
822 FIXME("unhandled wFlags %x\n", wFlags);
823 return E_NOTIMPL;
827 data = get_dispex_data(This);
828 if(!data)
829 return E_FAIL;
831 min = 0;
832 max = data->func_cnt-1;
834 while(min <= max) {
835 n = (min+max)/2;
837 if(data->funcs[n].id == id)
838 break;
840 if(data->funcs[n].id < id)
841 min = n+1;
842 else
843 max = n-1;
846 if(min > max) {
847 WARN("invalid id %x\n", id);
848 return DISP_E_UNKNOWNNAME;
851 if(data->funcs[n].func_disp_idx == -1)
852 hres = typeinfo_invoke(This, data->funcs+n, wFlags, pdp, pvarRes, pei);
853 else
854 hres = function_invoke(This, data->funcs+n, wFlags, pdp, pvarRes, pei);
856 return hres;
859 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
861 DispatchEx *This = DISPATCHEX_THIS(iface);
862 FIXME("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
863 return E_NOTIMPL;
866 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
868 DispatchEx *This = DISPATCHEX_THIS(iface);
869 FIXME("(%p)->(%x)\n", This, id);
870 return E_NOTIMPL;
873 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
875 DispatchEx *This = DISPATCHEX_THIS(iface);
876 FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
877 return E_NOTIMPL;
880 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
882 DispatchEx *This = DISPATCHEX_THIS(iface);
883 FIXME("(%p)->(%x %p)\n", This, id, pbstrName);
884 return E_NOTIMPL;
887 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
889 DispatchEx *This = DISPATCHEX_THIS(iface);
890 FIXME("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
891 return E_NOTIMPL;
894 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
896 DispatchEx *This = DISPATCHEX_THIS(iface);
897 FIXME("(%p)->(%p)\n", This, ppunk);
898 return E_NOTIMPL;
901 #undef DISPATCHEX_THIS
903 static IDispatchExVtbl DispatchExVtbl = {
904 DispatchEx_QueryInterface,
905 DispatchEx_AddRef,
906 DispatchEx_Release,
907 DispatchEx_GetTypeInfoCount,
908 DispatchEx_GetTypeInfo,
909 DispatchEx_GetIDsOfNames,
910 DispatchEx_Invoke,
911 DispatchEx_GetDispID,
912 DispatchEx_InvokeEx,
913 DispatchEx_DeleteMemberByName,
914 DispatchEx_DeleteMemberByDispID,
915 DispatchEx_GetMemberProperties,
916 DispatchEx_GetMemberName,
917 DispatchEx_GetNextDispID,
918 DispatchEx_GetNameSpaceParent
921 BOOL dispex_query_interface(DispatchEx *This, REFIID riid, void **ppv)
923 static const IID IID_UndocumentedScriptIface =
924 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa0}};
925 static const IID IID_IDispatchJS =
926 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa6}};
928 if(IsEqualGUID(&IID_IDispatch, riid)) {
929 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
930 *ppv = DISPATCHEX(This);
931 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
932 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
933 *ppv = DISPATCHEX(This);
934 }else if(IsEqualGUID(&IID_IDispatchJS, riid)) {
935 TRACE("(%p)->(IID_IDispatchJS %p) returning NULL\n", This, ppv);
936 *ppv = NULL;
937 }else if(IsEqualGUID(&IID_UndocumentedScriptIface, riid)) {
938 TRACE("(%p)->(IID_UndocumentedScriptIface %p) returning NULL\n", This, ppv);
939 *ppv = NULL;
940 }else {
941 return FALSE;
944 if(*ppv)
945 IUnknown_AddRef((IUnknown*)*ppv);
946 return TRUE;
949 void release_dispex(DispatchEx *This)
951 dynamic_prop_t *prop;
953 if(!This->dynamic_data)
954 return;
956 for(prop = This->dynamic_data->props; prop < This->dynamic_data->props + This->dynamic_data->prop_cnt; prop++) {
957 VariantClear(&prop->var);
958 heap_free(prop->name);
961 heap_free(This->dynamic_data->props);
963 if(This->dynamic_data->func_disps) {
964 unsigned i;
966 for(i=0; i < This->data->data->func_disp_cnt; i++) {
967 if(This->dynamic_data->func_disps[i]) {
968 release_dispex(&This->dynamic_data->func_disps[i]->dispex);
969 heap_free(This->dynamic_data->func_disps[i]);
973 heap_free(This->dynamic_data->func_disps);
976 heap_free(This->dynamic_data);
979 void init_dispex(DispatchEx *dispex, IUnknown *outer, dispex_static_data_t *data)
981 dispex->lpIDispatchExVtbl = &DispatchExVtbl;
982 dispex->outer = outer;
983 dispex->data = data;
984 dispex->dynamic_data = NULL;