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
25 # include <libxml/parser.h>
26 # include <libxml/xmlerror.h>
40 #include "wine/debug.h"
41 #include "wine/list.h"
42 #include "wine/unicode.h"
44 #include "msxml_private.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(msxml
);
48 static CRITICAL_SECTION cs_dispex_static_data
;
49 static CRITICAL_SECTION_DEBUG cs_dispex_static_data_dbg
=
51 0, 0, &cs_dispex_static_data
,
52 { &cs_dispex_static_data_dbg
.ProcessLocksList
, &cs_dispex_static_data_dbg
.ProcessLocksList
},
53 0, 0, { (DWORD_PTR
)(__FILE__
": dispex_static_data") }
55 static CRITICAL_SECTION cs_dispex_static_data
= { &cs_dispex_static_data_dbg
, -1, 0, 0, 0, 0 };
67 enum lib_version_t lib
;
81 struct dispex_data_t
{
84 func_info_t
**name_table
;
94 struct dispex_dynamic_data_t
{
97 dynamic_prop_t
*props
;
100 #define DISPID_DYNPROP_0 0x50000000
101 #define DISPID_DYNPROP_MAX 0x5fffffff
103 static struct list dispex_data_list
= LIST_INIT(dispex_data_list
);
104 static ITypeLib
*typelib
[LibXml_Last
];
105 static ITypeInfo
*typeinfos
[LAST_tid
];
107 /* indexed with lib_version_t values */
108 static lib_id_t lib_ids
[] = {
113 static tid_id_t tid_ids
[] = {
114 { &IID_NULL
, LibXml_Last
},
115 { &IID_IXMLDOMAttribute
, LibXml2
},
116 { &IID_IXMLDOMCDATASection
, LibXml2
},
117 { &IID_IXMLDOMComment
, LibXml2
},
118 { &IID_IXMLDOMDocument
, LibXml2
},
119 { &IID_IXMLDOMDocument2
, LibXml2
},
120 { &IID_IXMLDOMDocument3
, LibXml2
},
121 { &IID_IXMLDOMDocumentFragment
, LibXml2
},
122 { &IID_IXMLDOMDocumentType
, LibXml2
},
123 { &IID_IXMLDOMElement
, LibXml2
},
124 { &IID_IXMLDOMEntityReference
, LibXml2
},
125 { &IID_IXMLDOMImplementation
, LibXml2
},
126 { &IID_IXMLDOMNamedNodeMap
, LibXml2
},
127 { &IID_IXMLDOMNode
, LibXml2
},
128 { &IID_IXMLDOMNodeList
, LibXml2
},
129 { &IID_IXMLDOMParseError
, LibXml2
},
130 { &IID_IXMLDOMProcessingInstruction
, LibXml2
},
131 { &IID_IXMLDOMSchemaCollection
, LibXml2
},
132 { &IID_IXMLDOMSelection
, LibXml2
},
133 { &IID_IXMLDOMText
, LibXml2
},
134 { &IID_IXMLElement
, LibXml
},
135 { &IID_IXMLDocument
, LibXml
},
136 { &IID_IXMLHTTPRequest
, LibXml2
},
137 { &IID_IXSLProcessor
, LibXml2
},
138 { &IID_IXSLTemplate
, LibXml2
},
139 { &IID_IVBSAXAttributes
, LibXml2
},
140 { &IID_IVBSAXContentHandler
, LibXml2
},
141 { &IID_IVBSAXDeclHandler
, LibXml2
},
142 { &IID_IVBSAXDTDHandler
, LibXml2
},
143 { &IID_IVBSAXEntityResolver
, LibXml2
},
144 { &IID_IVBSAXErrorHandler
, LibXml2
},
145 { &IID_IVBSAXLexicalHandler
, LibXml2
},
146 { &IID_IVBSAXLocator
, LibXml2
},
147 { &IID_IVBSAXXMLFilter
, LibXml2
},
148 { &IID_IVBSAXXMLReader
, LibXml2
},
149 { &IID_IMXAttributes
, LibXml2
},
150 { &IID_IMXReaderControl
, LibXml2
},
151 { &IID_IMXWriter
, LibXml2
},
152 { &IID_IVBMXNamespaceManager
, LibXml2
}
155 const IID
*get_riid_from_tid(tid_t tid
)
157 return tid_ids
[tid
].iid
;
160 static inline unsigned get_libid_from_tid(tid_t tid
)
162 return tid_ids
[tid
].lib
;
165 HRESULT
get_typeinfo(enum tid_t tid
, ITypeInfo
**typeinfo
)
167 unsigned lib
= get_libid_from_tid(tid
);
173 hres
= LoadRegTypeLib(lib_ids
[lib
].iid
, lib_ids
[lib
].major
, 0, LOCALE_SYSTEM_DEFAULT
, &tl
);
175 ERR("LoadRegTypeLib failed: %08x\n", hres
);
179 if(InterlockedCompareExchangePointer((void**)&typelib
[lib
], tl
, NULL
))
180 ITypeLib_Release(tl
);
183 if(!typeinfos
[tid
]) {
186 hres
= ITypeLib_GetTypeInfoOfGuid(typelib
[lib
], get_riid_from_tid(tid
), &ti
);
188 /* try harder with typelib from msxml.dll */
189 hres
= ITypeLib_GetTypeInfoOfGuid(typelib
[LibXml
], get_riid_from_tid(tid
), &ti
);
191 ERR("GetTypeInfoOfGuid failed: %08x\n", hres
);
196 if(InterlockedCompareExchangePointer((void**)(typeinfos
+tid
), ti
, NULL
))
197 ITypeInfo_Release(ti
);
200 *typeinfo
= typeinfos
[tid
];
202 ITypeInfo_AddRef(typeinfos
[tid
]);
206 void release_typelib(void)
211 while(!list_empty(&dispex_data_list
)) {
212 iter
= LIST_ENTRY(list_head(&dispex_data_list
), dispex_data_t
, entry
);
213 list_remove(&iter
->entry
);
215 for(i
=0; i
< iter
->func_cnt
; i
++)
216 SysFreeString(iter
->funcs
[i
].name
);
218 heap_free(iter
->funcs
);
219 heap_free(iter
->name_table
);
223 for(i
=0; i
< sizeof(typeinfos
)/sizeof(*typeinfos
); i
++)
225 ITypeInfo_Release(typeinfos
[i
]);
227 for(i
=0; i
< sizeof(typelib
)/sizeof(*typelib
); i
++)
229 ITypeLib_Release(typelib
[i
]);
231 DeleteCriticalSection(&cs_dispex_static_data
);
234 static void add_func_info(dispex_data_t
*data
, DWORD
*size
, tid_t tid
, DISPID id
, ITypeInfo
*dti
)
238 if(data
->func_cnt
&& data
->funcs
[data
->func_cnt
-1].id
== id
)
241 if(data
->func_cnt
== *size
)
242 data
->funcs
= heap_realloc(data
->funcs
, (*size
<<= 1)*sizeof(func_info_t
));
244 hres
= ITypeInfo_GetDocumentation(dti
, id
, &data
->funcs
[data
->func_cnt
].name
, NULL
, NULL
, NULL
);
248 data
->funcs
[data
->func_cnt
].id
= id
;
249 data
->funcs
[data
->func_cnt
].tid
= tid
;
254 static int dispid_cmp(const void *p1
, const void *p2
)
256 return ((const func_info_t
*)p1
)->id
- ((const func_info_t
*)p2
)->id
;
259 static int func_name_cmp(const void *p1
, const void *p2
)
261 return strcmpiW((*(func_info_t
* const*)p1
)->name
, (*(func_info_t
* const*)p2
)->name
);
264 static dispex_data_t
*preprocess_dispex_data(DispatchEx
*This
)
266 const tid_t
*tid
= This
->data
->iface_tids
;
273 TRACE("(%p)\n", This
);
275 hres
= get_typeinfo(This
->data
->disp_tid
, &dti
);
277 ERR("Could not get disp type info: %08x\n", hres
);
281 data
= heap_alloc(sizeof(dispex_data_t
));
283 data
->funcs
= heap_alloc(size
*sizeof(func_info_t
));
284 list_add_tail(&dispex_data_list
, &data
->entry
);
287 hres
= get_typeinfo(*tid
, &ti
);
293 hres
= ITypeInfo_GetFuncDesc(ti
, i
++, &funcdesc
);
297 add_func_info(data
, &size
, *tid
, funcdesc
->memid
, dti
);
298 ITypeInfo_ReleaseFuncDesc(ti
, funcdesc
);
301 ITypeInfo_Release(ti
);
305 if(!data
->func_cnt
) {
306 heap_free(data
->funcs
);
308 }else if(data
->func_cnt
!= size
) {
309 data
->funcs
= heap_realloc(data
->funcs
, data
->func_cnt
* sizeof(func_info_t
));
312 qsort(data
->funcs
, data
->func_cnt
, sizeof(func_info_t
), dispid_cmp
);
315 data
->name_table
= heap_alloc(data
->func_cnt
* sizeof(func_info_t
*));
316 for(i
=0; i
< data
->func_cnt
; i
++)
317 data
->name_table
[i
] = data
->funcs
+i
;
318 qsort(data
->name_table
, data
->func_cnt
, sizeof(func_info_t
*), func_name_cmp
);
320 data
->name_table
= NULL
;
323 ITypeInfo_Release(dti
);
327 static dispex_data_t
*get_dispex_data(DispatchEx
*This
)
330 return This
->data
->data
;
332 EnterCriticalSection(&cs_dispex_static_data
);
334 if(!This
->data
->data
)
335 This
->data
->data
= preprocess_dispex_data(This
);
337 LeaveCriticalSection(&cs_dispex_static_data
);
339 return This
->data
->data
;
342 static inline BOOL
is_dynamic_dispid(DISPID id
)
344 return DISPID_DYNPROP_0
<= id
&& id
<= DISPID_DYNPROP_MAX
;
347 static inline DispatchEx
*impl_from_IDispatchEx(IDispatchEx
*iface
)
349 return CONTAINING_RECORD(iface
, DispatchEx
, IDispatchEx_iface
);
352 static HRESULT WINAPI
DispatchEx_QueryInterface(IDispatchEx
*iface
, REFIID riid
, void **ppv
)
354 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
356 return IUnknown_QueryInterface(This
->outer
, riid
, ppv
);
359 static ULONG WINAPI
DispatchEx_AddRef(IDispatchEx
*iface
)
361 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
363 return IUnknown_AddRef(This
->outer
);
366 static ULONG WINAPI
DispatchEx_Release(IDispatchEx
*iface
)
368 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
370 return IUnknown_Release(This
->outer
);
373 static HRESULT WINAPI
DispatchEx_GetTypeInfoCount(IDispatchEx
*iface
, UINT
*pctinfo
)
375 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
377 TRACE("(%p)->(%p)\n", This
, pctinfo
);
383 static HRESULT WINAPI
DispatchEx_GetTypeInfo(IDispatchEx
*iface
, UINT iTInfo
,
384 LCID lcid
, ITypeInfo
**ppTInfo
)
386 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
388 TRACE("(%p)->(%u %u %p)\n", This
, iTInfo
, lcid
, ppTInfo
);
390 return get_typeinfo(This
->data
->disp_tid
, ppTInfo
);
393 static HRESULT WINAPI
DispatchEx_GetIDsOfNames(IDispatchEx
*iface
, REFIID riid
,
394 LPOLESTR
*rgszNames
, UINT cNames
,
395 LCID lcid
, DISPID
*rgDispId
)
397 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
401 TRACE("(%p)->(%s %p %u %u %p)\n", This
, debugstr_guid(riid
), rgszNames
, cNames
,
404 for(i
=0; i
< cNames
; i
++) {
405 hres
= IDispatchEx_GetDispID(&This
->IDispatchEx_iface
, rgszNames
[i
], 0, rgDispId
+i
);
413 static HRESULT WINAPI
DispatchEx_Invoke(IDispatchEx
*iface
, DISPID dispIdMember
,
414 REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
,
415 VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
417 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
419 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This
, dispIdMember
, debugstr_guid(riid
),
420 lcid
, wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
422 return IDispatchEx_InvokeEx(&This
->IDispatchEx_iface
, dispIdMember
, lcid
, wFlags
,
423 pDispParams
, pVarResult
, pExcepInfo
, NULL
);
426 static HRESULT WINAPI
DispatchEx_GetDispID(IDispatchEx
*iface
, BSTR bstrName
, DWORD grfdex
, DISPID
*pid
)
428 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
432 TRACE("(%p)->(%s %x %p)\n", This
, debugstr_w(bstrName
), grfdex
, pid
);
434 if(grfdex
& ~(fdexNameCaseSensitive
|fdexNameEnsure
|fdexNameImplicit
))
435 FIXME("Unsupported grfdex %x\n", grfdex
);
437 data
= get_dispex_data(This
);
442 max
= data
->func_cnt
-1;
447 c
= strcmpiW(data
->name_table
[n
]->name
, bstrName
);
449 if((grfdex
& fdexNameCaseSensitive
) && strcmpW(data
->name_table
[n
]->name
, bstrName
))
452 *pid
= data
->name_table
[n
]->id
;
462 if(This
->dynamic_data
) {
465 for(i
=0; i
< This
->dynamic_data
->prop_cnt
; i
++) {
466 if(!strcmpW(This
->dynamic_data
->props
[i
].name
, bstrName
)) {
467 *pid
= DISPID_DYNPROP_0
+ i
;
473 if(This
->data
->vtbl
&& This
->data
->vtbl
->get_dispid
) {
476 hres
= This
->data
->vtbl
->get_dispid(This
->outer
, bstrName
, grfdex
, pid
);
477 if(hres
!= DISP_E_UNKNOWNNAME
)
481 if(grfdex
& fdexNameEnsure
) {
482 dispex_dynamic_data_t
*dynamic_data
;
484 TRACE("creating dynamic prop %s\n", debugstr_w(bstrName
));
486 if(This
->dynamic_data
) {
487 dynamic_data
= This
->dynamic_data
;
489 dynamic_data
= This
->dynamic_data
= heap_alloc_zero(sizeof(dispex_dynamic_data_t
));
491 return E_OUTOFMEMORY
;
494 if(!dynamic_data
->buf_size
) {
495 dynamic_data
->props
= heap_alloc(sizeof(dynamic_prop_t
)*4);
496 if(!dynamic_data
->props
)
497 return E_OUTOFMEMORY
;
498 dynamic_data
->buf_size
= 4;
499 }else if(dynamic_data
->buf_size
== dynamic_data
->prop_cnt
) {
500 dynamic_prop_t
*new_props
;
502 new_props
= heap_realloc(dynamic_data
->props
, sizeof(dynamic_prop_t
)*(dynamic_data
->buf_size
<<1));
504 return E_OUTOFMEMORY
;
506 dynamic_data
->props
= new_props
;
507 dynamic_data
->buf_size
<<= 1;
510 dynamic_data
->props
[dynamic_data
->prop_cnt
].name
= heap_strdupW(bstrName
);
511 VariantInit(&dynamic_data
->props
[dynamic_data
->prop_cnt
].var
);
512 *pid
= DISPID_DYNPROP_0
+ dynamic_data
->prop_cnt
++;
517 TRACE("not found %s\n", debugstr_w(bstrName
));
518 return DISP_E_UNKNOWNNAME
;
521 static HRESULT WINAPI
DispatchEx_InvokeEx(IDispatchEx
*iface
, DISPID id
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pdp
,
522 VARIANT
*pvarRes
, EXCEPINFO
*pei
, IServiceProvider
*pspCaller
)
524 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
532 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This
, id
, lcid
, wFlags
, pdp
, pvarRes
, pei
, pspCaller
);
534 if(This
->data
->vtbl
&& This
->data
->vtbl
->invoke
) {
535 hres
= This
->data
->vtbl
->invoke(This
->outer
, id
, lcid
, wFlags
, pdp
, pvarRes
, pei
);
536 if (hres
!= DISP_E_UNKNOWNNAME
) return hres
;
539 if(wFlags
== DISPATCH_CONSTRUCT
) {
540 FIXME("DISPATCH_CONSTRUCT not implemented\n");
544 if(is_dynamic_dispid(id
)) {
545 DWORD idx
= id
- DISPID_DYNPROP_0
;
548 if(!This
->dynamic_data
|| This
->dynamic_data
->prop_cnt
<= idx
)
549 return DISP_E_UNKNOWNNAME
;
551 var
= &This
->dynamic_data
->props
[idx
].var
;
554 case INVOKE_PROPERTYGET
:
555 return VariantCopy(pvarRes
, var
);
556 case INVOKE_PROPERTYPUT
:
558 return VariantCopy(var
, pdp
->rgvarg
);
560 FIXME("unhandled wFlags %x\n", wFlags
);
565 data
= get_dispex_data(This
);
570 max
= data
->func_cnt
-1;
575 if(data
->funcs
[n
].id
== id
)
578 if(data
->funcs
[n
].id
< id
)
585 WARN("invalid id %x\n", id
);
586 return DISP_E_UNKNOWNNAME
;
589 hres
= get_typeinfo(data
->funcs
[n
].tid
, &ti
);
591 ERR("Could not get type info: %08x\n", hres
);
595 hres
= IUnknown_QueryInterface(This
->outer
, get_riid_from_tid(data
->funcs
[n
].tid
), (void**)&unk
);
597 ERR("Could not get iface: %08x\n", hres
);
601 hres
= ITypeInfo_Invoke(ti
, unk
, id
, wFlags
, pdp
, pvarRes
, pei
, &argerr
);
603 ITypeInfo_Release(ti
);
604 IUnknown_Release(unk
);
608 static HRESULT WINAPI
DispatchEx_DeleteMemberByName(IDispatchEx
*iface
, BSTR bstrName
, DWORD grfdex
)
610 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
611 TRACE("Not implemented in native msxml3 (%p)->(%s %x)\n", This
, debugstr_w(bstrName
), grfdex
);
615 static HRESULT WINAPI
DispatchEx_DeleteMemberByDispID(IDispatchEx
*iface
, DISPID id
)
617 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
618 TRACE("Not implemented in native msxml3 (%p)->(%x)\n", This
, id
);
622 static HRESULT WINAPI
DispatchEx_GetMemberProperties(IDispatchEx
*iface
, DISPID id
, DWORD grfdexFetch
, DWORD
*pgrfdex
)
624 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
625 TRACE("Not implemented in native msxml3 (%p)->(%x %x %p)\n", This
, id
, grfdexFetch
, pgrfdex
);
629 static HRESULT WINAPI
DispatchEx_GetMemberName(IDispatchEx
*iface
, DISPID id
, BSTR
*pbstrName
)
631 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
632 TRACE("Not implemented in native msxml3 (%p)->(%x %p)\n", This
, id
, pbstrName
);
636 static HRESULT WINAPI
DispatchEx_GetNextDispID(IDispatchEx
*iface
, DWORD grfdex
, DISPID id
, DISPID
*pid
)
638 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
639 TRACE(" Not implemented in native msxml3 (%p)->(%x %x %p)\n", This
, grfdex
, id
, pid
);
643 static HRESULT WINAPI
DispatchEx_GetNameSpaceParent(IDispatchEx
*iface
, IUnknown
**ppunk
)
645 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
646 TRACE("Not implemented in native msxml3 (%p)->(%p)\n", This
, ppunk
);
650 static IDispatchExVtbl DispatchExVtbl
= {
651 DispatchEx_QueryInterface
,
654 DispatchEx_GetTypeInfoCount
,
655 DispatchEx_GetTypeInfo
,
656 DispatchEx_GetIDsOfNames
,
658 DispatchEx_GetDispID
,
660 DispatchEx_DeleteMemberByName
,
661 DispatchEx_DeleteMemberByDispID
,
662 DispatchEx_GetMemberProperties
,
663 DispatchEx_GetMemberName
,
664 DispatchEx_GetNextDispID
,
665 DispatchEx_GetNameSpaceParent
668 BOOL
dispex_query_interface(DispatchEx
*This
, REFIID riid
, void **ppv
)
670 static const IID IID_UndocumentedScriptIface
=
671 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa0}};
672 static const IID IID_IDispatchJS
=
673 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa6}};
675 if(IsEqualGUID(&IID_IDispatch
, riid
)) {
676 TRACE("(%p)->(IID_IDispatch %p)\n", This
, ppv
);
677 *ppv
= &This
->IDispatchEx_iface
;
678 }else if(IsEqualGUID(&IID_IDispatchEx
, riid
)) {
679 TRACE("(%p)->(IID_IDispatchEx %p)\n", This
, ppv
);
680 *ppv
= &This
->IDispatchEx_iface
;
681 }else if(IsEqualGUID(&IID_IDispatchJS
, riid
)) {
682 TRACE("(%p)->(IID_IDispatchJS %p) returning NULL\n", This
, ppv
);
684 }else if(IsEqualGUID(&IID_UndocumentedScriptIface
, riid
)) {
685 TRACE("(%p)->(IID_UndocumentedScriptIface %p) returning NULL\n", This
, ppv
);
687 }else if (IsEqualGUID(&IID_IObjectIdentity
, riid
)) {
688 TRACE("IID_IObjectIdentity not supported returning NULL\n");
695 IUnknown_AddRef((IUnknown
*)*ppv
);
699 void release_dispex(DispatchEx
*This
)
701 dynamic_prop_t
*prop
;
703 if(!This
->dynamic_data
)
706 for(prop
= This
->dynamic_data
->props
; prop
< This
->dynamic_data
->props
+ This
->dynamic_data
->prop_cnt
; prop
++) {
707 VariantClear(&prop
->var
);
708 heap_free(prop
->name
);
711 heap_free(This
->dynamic_data
->props
);
712 heap_free(This
->dynamic_data
);
715 void init_dispex(DispatchEx
*dispex
, IUnknown
*outer
, dispex_static_data_t
*data
)
717 dispex
->IDispatchEx_iface
.lpVtbl
= &DispatchExVtbl
;
718 dispex
->outer
= outer
;
720 dispex
->dynamic_data
= NULL
;