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
35 #include "wine/debug.h"
36 #include "wine/list.h"
37 #include "wine/unicode.h"
39 #include "msxml_private.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
43 #define DISPATCHEX(x) ((IDispatchEx*) &(x)->lpIDispatchExVtbl)
51 struct dispex_data_t {
54 func_info_t **name_table;
64 struct dispex_dynamic_data_t {
67 dynamic_prop_t *props;
70 #define DISPID_DYNPROP_0 0x50000000
71 #define DISPID_DYNPROP_MAX 0x5fffffff
73 static struct list dispex_data_list = LIST_INIT(dispex_data_list);
74 static ITypeLib *typelib;
75 static ITypeInfo *typeinfos[LAST_tid];
77 static REFIID tid_ids[] = {
78 &IID_IXMLDOMAttribute,
79 &IID_IXMLDOMCDATASection,
82 &IID_IXMLDOMDocument2,
83 &IID_IXMLDOMDocumentFragment,
84 &IID_IXMLDOMDocumentType,
86 &IID_IXMLDOMEntityReference,
87 &IID_IXMLDOMImplementation,
88 &IID_IXMLDOMNamedNodeMap,
91 &IID_IXMLDOMParseError,
92 &IID_IXMLDOMProcessingInstruction,
93 &IID_IXMLDOMSchemaCollection,
94 &IID_IXMLDOMSelection,
101 &IID_IVBSAXAttributes,
102 &IID_IVBSAXContentHandler,
103 &IID_IVBSAXDeclHandler,
104 &IID_IVBSAXDTDHandler,
105 &IID_IVBSAXEntityResolver,
106 &IID_IVBSAXErrorHandler,
107 &IID_IVBSAXLexicalHandler,
109 &IID_IVBSAXXMLFilter,
110 &IID_IVBSAXXMLReader,
112 &IID_IMXReaderControl,
116 HRESULT get_typeinfo(enum tid_t tid, ITypeInfo **typeinfo)
123 hres = LoadRegTypeLib(&LIBID_MSXML2, 3, 0, LOCALE_SYSTEM_DEFAULT, &tl);
125 ERR("LoadRegTypeLib failed: %08x\n", hres);
129 if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
130 ITypeLib_Release(tl);
133 if(!typeinfos[tid]) {
136 hres = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &typeinfo);
138 ERR("GetTypeInfoOfGuid failed: %08x\n", hres);
142 if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), typeinfo, NULL))
143 ITypeInfo_Release(typeinfo);
146 *typeinfo = typeinfos[tid];
148 ITypeInfo_AddRef(typeinfos[tid]);
152 void release_typelib(void)
157 while(!list_empty(&dispex_data_list)) {
158 iter = LIST_ENTRY(list_head(&dispex_data_list), dispex_data_t, entry);
159 list_remove(&iter->entry);
161 for(i=0; i < iter->func_cnt; i++)
162 SysFreeString(iter->funcs[i].name);
164 heap_free(iter->funcs);
165 heap_free(iter->name_table);
172 for(i=0; i < sizeof(typeinfos)/sizeof(*typeinfos); i++)
174 ITypeInfo_Release(typeinfos[i]);
176 ITypeLib_Release(typelib);
179 static void add_func_info(dispex_data_t *data, DWORD *size, tid_t tid, DISPID id, ITypeInfo *dti)
183 if(data->func_cnt && data->funcs[data->func_cnt-1].id == id)
186 if(data->func_cnt == *size)
187 data->funcs = heap_realloc(data->funcs, (*size <<= 1)*sizeof(func_info_t));
189 hres = ITypeInfo_GetDocumentation(dti, id, &data->funcs[data->func_cnt].name, NULL, NULL, NULL);
193 data->funcs[data->func_cnt].id = id;
194 data->funcs[data->func_cnt].tid = tid;
199 static int dispid_cmp(const void *p1, const void *p2)
201 return ((const func_info_t*)p1)->id - ((const func_info_t*)p2)->id;
204 static int func_name_cmp(const void *p1, const void *p2)
206 return strcmpiW((*(func_info_t* const*)p1)->name, (*(func_info_t* const*)p2)->name);
209 static dispex_data_t *preprocess_dispex_data(DispatchEx *This)
211 const tid_t *tid = This->data->iface_tids;
218 TRACE("(%p)\n", This);
220 hres = get_typeinfo(This->data->disp_tid, &dti);
222 ERR("Could not get disp type info: %08x\n", hres);
226 data = heap_alloc(sizeof(dispex_data_t));
228 data->funcs = heap_alloc(size*sizeof(func_info_t));
229 list_add_tail(&dispex_data_list, &data->entry);
232 hres = get_typeinfo(*tid, &ti);
238 hres = ITypeInfo_GetFuncDesc(ti, i++, &funcdesc);
242 add_func_info(data, &size, *tid, funcdesc->memid, dti);
243 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
246 ITypeInfo_Release(ti);
250 if(!data->func_cnt) {
251 heap_free(data->funcs);
253 }else if(data->func_cnt != size) {
254 data->funcs = heap_realloc(data->funcs, data->func_cnt * sizeof(func_info_t));
257 qsort(data->funcs, data->func_cnt, sizeof(func_info_t), dispid_cmp);
260 data->name_table = heap_alloc(data->func_cnt * sizeof(func_info_t*));
261 for(i=0; i < data->func_cnt; i++)
262 data->name_table[i] = data->funcs+i;
263 qsort(data->name_table, data->func_cnt, sizeof(func_info_t*), func_name_cmp);
265 data->name_table = NULL;
268 ITypeInfo_Release(dti);
272 static CRITICAL_SECTION cs_dispex_static_data;
273 static CRITICAL_SECTION_DEBUG cs_dispex_static_data_dbg =
275 0, 0, &cs_dispex_static_data,
276 { &cs_dispex_static_data_dbg.ProcessLocksList, &cs_dispex_static_data_dbg.ProcessLocksList },
277 0, 0, { (DWORD_PTR)(__FILE__ ": dispex_static_data") }
279 static CRITICAL_SECTION cs_dispex_static_data = { &cs_dispex_static_data_dbg, -1, 0, 0, 0, 0 };
282 static dispex_data_t *get_dispex_data(DispatchEx *This)
285 return This->data->data;
287 EnterCriticalSection(&cs_dispex_static_data);
289 if(!This->data->data)
290 This->data->data = preprocess_dispex_data(This);
292 LeaveCriticalSection(&cs_dispex_static_data);
294 return This->data->data;
297 static inline BOOL is_custom_dispid(DISPID id)
299 return MSXML_DISPID_CUSTOM_MIN <= id && id <= MSXML_DISPID_CUSTOM_MAX;
302 static inline BOOL is_dynamic_dispid(DISPID id)
304 return DISPID_DYNPROP_0 <= id && id <= DISPID_DYNPROP_MAX;
307 static inline DispatchEx *impl_from_IDispatchEx(IDispatchEx *iface)
309 return (DispatchEx*)((char*)iface - FIELD_OFFSET(DispatchEx, lpIDispatchExVtbl));
312 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
314 DispatchEx *This = impl_from_IDispatchEx(iface);
316 return IUnknown_QueryInterface(This->outer, riid, ppv);
319 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
321 DispatchEx *This = impl_from_IDispatchEx(iface);
323 return IUnknown_AddRef(This->outer);
326 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
328 DispatchEx *This = impl_from_IDispatchEx(iface);
330 return IUnknown_Release(This->outer);
333 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
335 DispatchEx *This = impl_from_IDispatchEx(iface);
337 TRACE("(%p)->(%p)\n", This, pctinfo);
343 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
344 LCID lcid, ITypeInfo **ppTInfo)
346 DispatchEx *This = impl_from_IDispatchEx(iface);
348 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
350 return get_typeinfo(This->data->disp_tid, ppTInfo);
353 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
354 LPOLESTR *rgszNames, UINT cNames,
355 LCID lcid, DISPID *rgDispId)
357 DispatchEx *This = impl_from_IDispatchEx(iface);
361 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
364 for(i=0; i < cNames; i++) {
365 hres = IDispatchEx_GetDispID(DISPATCHEX(This), rgszNames[i], 0, rgDispId+i);
373 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
374 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
375 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
377 DispatchEx *This = impl_from_IDispatchEx(iface);
379 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
380 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
382 return IDispatchEx_InvokeEx(DISPATCHEX(This), dispIdMember, lcid, wFlags,
383 pDispParams, pVarResult, pExcepInfo, NULL);
386 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
388 DispatchEx *This = impl_from_IDispatchEx(iface);
392 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
394 if(grfdex & ~(fdexNameCaseSensitive|fdexNameEnsure|fdexNameImplicit))
395 FIXME("Unsupported grfdex %x\n", grfdex);
397 data = get_dispex_data(This);
402 max = data->func_cnt-1;
407 c = strcmpiW(data->name_table[n]->name, bstrName);
409 if((grfdex & fdexNameCaseSensitive) && strcmpW(data->name_table[n]->name, bstrName))
412 *pid = data->name_table[n]->id;
422 if(This->dynamic_data) {
425 for(i=0; i < This->dynamic_data->prop_cnt; i++) {
426 if(!strcmpW(This->dynamic_data->props[i].name, bstrName)) {
427 *pid = DISPID_DYNPROP_0 + i;
433 if(This->data->vtbl && This->data->vtbl->get_dispid) {
436 hres = This->data->vtbl->get_dispid(This->outer, bstrName, grfdex, pid);
437 if(hres != DISP_E_UNKNOWNNAME)
441 if(grfdex & fdexNameEnsure) {
442 dispex_dynamic_data_t *dynamic_data;
444 TRACE("creating dynamic prop %s\n", debugstr_w(bstrName));
446 if(This->dynamic_data) {
447 dynamic_data = This->dynamic_data;
449 dynamic_data = This->dynamic_data = heap_alloc_zero(sizeof(dispex_dynamic_data_t));
451 return E_OUTOFMEMORY;
454 if(!dynamic_data->buf_size) {
455 dynamic_data->props = heap_alloc(sizeof(dynamic_prop_t)*4);
456 if(!dynamic_data->props)
457 return E_OUTOFMEMORY;
458 dynamic_data->buf_size = 4;
459 }else if(dynamic_data->buf_size == dynamic_data->prop_cnt) {
460 dynamic_prop_t *new_props;
462 new_props = heap_realloc(dynamic_data->props, sizeof(dynamic_prop_t)*(dynamic_data->buf_size<<1));
464 return E_OUTOFMEMORY;
466 dynamic_data->props = new_props;
467 dynamic_data->buf_size <<= 1;
470 dynamic_data->props[dynamic_data->prop_cnt].name = heap_strdupW(bstrName);
471 VariantInit(&dynamic_data->props[dynamic_data->prop_cnt].var);
472 *pid = DISPID_DYNPROP_0 + dynamic_data->prop_cnt++;
477 TRACE("not found %s\n", debugstr_w(bstrName));
478 return DISP_E_UNKNOWNNAME;
481 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
482 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
484 DispatchEx *This = impl_from_IDispatchEx(iface);
492 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
494 if(is_custom_dispid(id) && This->data->vtbl && This->data->vtbl->invoke)
495 return This->data->vtbl->invoke(This->outer, id, lcid, wFlags, pdp, pvarRes, pei);
497 if(wFlags == DISPATCH_CONSTRUCT) {
498 FIXME("DISPATCH_CONSTRUCT not implemented\n");
502 if(is_dynamic_dispid(id)) {
503 DWORD idx = id - DISPID_DYNPROP_0;
506 if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
507 return DISP_E_UNKNOWNNAME;
509 var = &This->dynamic_data->props[idx].var;
512 case INVOKE_PROPERTYGET:
513 return VariantCopy(pvarRes, var);
514 case INVOKE_PROPERTYPUT:
516 return VariantCopy(var, pdp->rgvarg);
518 FIXME("unhandled wFlags %x\n", wFlags);
523 data = get_dispex_data(This);
528 max = data->func_cnt-1;
533 if(data->funcs[n].id == id)
536 if(data->funcs[n].id < id)
543 WARN("invalid id %x\n", id);
544 return DISP_E_UNKNOWNNAME;
547 hres = get_typeinfo(data->funcs[n].tid, &ti);
549 ERR("Could not get type info: %08x\n", hres);
553 hres = IUnknown_QueryInterface(This->outer, tid_ids[data->funcs[n].tid], (void**)&unk);
555 ERR("Could not get iface: %08x\n", hres);
559 hres = ITypeInfo_Invoke(ti, unk, id, wFlags, pdp, pvarRes, pei, &argerr);
561 ITypeInfo_Release(ti);
562 IUnknown_Release(unk);
566 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
568 DispatchEx *This = impl_from_IDispatchEx(iface);
569 TRACE("Not implemented in native msxml3 (%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
573 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
575 DispatchEx *This = impl_from_IDispatchEx(iface);
576 TRACE("Not implemented in native msxml3 (%p)->(%x)\n", This, id);
580 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
582 DispatchEx *This = impl_from_IDispatchEx(iface);
583 TRACE("Not implemented in native msxml3 (%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
587 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
589 DispatchEx *This = impl_from_IDispatchEx(iface);
590 TRACE("Not implemented in native msxml3 (%p)->(%x %p)\n", This, id, pbstrName);
594 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
596 DispatchEx *This = impl_from_IDispatchEx(iface);
597 TRACE(" Not implemented in native msxml3 (%p)->(%x %x %p)\n", This, grfdex, id, pid);
601 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
603 DispatchEx *This = impl_from_IDispatchEx(iface);
604 TRACE("Not implemented in native msxml3 (%p)->(%p)\n", This, ppunk);
608 static IDispatchExVtbl DispatchExVtbl = {
609 DispatchEx_QueryInterface,
612 DispatchEx_GetTypeInfoCount,
613 DispatchEx_GetTypeInfo,
614 DispatchEx_GetIDsOfNames,
616 DispatchEx_GetDispID,
618 DispatchEx_DeleteMemberByName,
619 DispatchEx_DeleteMemberByDispID,
620 DispatchEx_GetMemberProperties,
621 DispatchEx_GetMemberName,
622 DispatchEx_GetNextDispID,
623 DispatchEx_GetNameSpaceParent
626 BOOL dispex_query_interface(DispatchEx *This, REFIID riid, void **ppv)
628 static const IID IID_UndocumentedScriptIface =
629 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa0}};
631 if(IsEqualGUID(&IID_IDispatch, riid)) {
632 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
633 *ppv = DISPATCHEX(This);
634 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
635 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
636 *ppv = DISPATCHEX(This);
637 }else if(IsEqualGUID(&IID_UndocumentedScriptIface, riid)) {
638 TRACE("(%p)->(IID_UndocumentedScriptIface %p) returning NULL\n", This, ppv);
640 }else if (IsEqualGUID(&IID_IObjectIdentity, riid)) {
641 TRACE("IID_IObjectIdentity not supported returning NULL\n");
648 IUnknown_AddRef((IUnknown*)*ppv);
652 void init_dispex(DispatchEx *dispex, IUnknown *outer, dispex_static_data_t *data)
654 dispex->lpIDispatchExVtbl = &DispatchExVtbl;
655 dispex->outer = outer;