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
24 #include "msxml2did.h"
27 #include "wine/debug.h"
28 #include "wine/heap.h"
30 #include "msxml_dispex.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(msxml
);
34 static CRITICAL_SECTION cs_dispex_static_data
;
35 static CRITICAL_SECTION_DEBUG cs_dispex_static_data_dbg
=
37 0, 0, &cs_dispex_static_data
,
38 { &cs_dispex_static_data_dbg
.ProcessLocksList
, &cs_dispex_static_data_dbg
.ProcessLocksList
},
39 0, 0, { (DWORD_PTR
)(__FILE__
": dispex_static_data") }
41 static CRITICAL_SECTION cs_dispex_static_data
= { &cs_dispex_static_data_dbg
, -1, 0, 0, 0, 0 };
52 enum lib_version_t lib
;
66 struct dispex_data_t
{
69 func_info_t
**name_table
;
74 static struct list dispex_data_list
= LIST_INIT(dispex_data_list
);
75 static ITypeLib
*typelib
[LibXml_Last
];
76 static ITypeInfo
*typeinfos
[LAST_tid
];
78 /* indexed with lib_version_t values */
79 static lib_id_t lib_ids
[] = {
84 static tid_id_t tid_ids
[] = {
85 { &IID_NULL
, LibXml_Last
},
86 { &IID_IXMLDOMAttribute
, LibXml2
},
87 { &IID_IXMLDOMCDATASection
, LibXml2
},
88 { &IID_IXMLDOMComment
, LibXml2
},
89 { &IID_IXMLDOMDocument
, LibXml2
},
90 { &IID_IXMLDOMDocument2
, LibXml2
},
91 { &IID_IXMLDOMDocument3
, LibXml2
},
92 { &IID_IXMLDOMDocumentFragment
, LibXml2
},
93 { &IID_IXMLDOMDocumentType
, LibXml2
},
94 { &IID_IXMLDOMElement
, LibXml2
},
95 { &IID_IXMLDOMEntityReference
, LibXml2
},
96 { &IID_IXMLDOMImplementation
, LibXml2
},
97 { &IID_IXMLDOMNamedNodeMap
, LibXml2
},
98 { &IID_IXMLDOMNode
, LibXml2
},
99 { &IID_IXMLDOMNodeList
, LibXml2
},
100 { &IID_IXMLDOMParseError2
, LibXml2
},
101 { &IID_IXMLDOMProcessingInstruction
, LibXml2
},
102 { &IID_IXMLDOMSchemaCollection
, LibXml2
},
103 { &IID_IXMLDOMSchemaCollection2
, LibXml2
},
104 { &IID_IXMLDOMSelection
, LibXml2
},
105 { &IID_IXMLDOMText
, LibXml2
},
106 { &IID_IXMLElement
, LibXml
},
107 { &IID_IXMLDocument
, LibXml
},
108 { &IID_IXMLHTTPRequest
, LibXml2
},
109 { &IID_IXSLProcessor
, LibXml2
},
110 { &IID_IXSLTemplate
, LibXml2
},
111 { &IID_IVBSAXAttributes
, LibXml2
},
112 { &IID_IVBSAXContentHandler
, LibXml2
},
113 { &IID_IVBSAXDeclHandler
, LibXml2
},
114 { &IID_IVBSAXDTDHandler
, LibXml2
},
115 { &IID_IVBSAXEntityResolver
, LibXml2
},
116 { &IID_IVBSAXErrorHandler
, LibXml2
},
117 { &IID_IVBSAXLexicalHandler
, LibXml2
},
118 { &IID_IVBSAXLocator
, LibXml2
},
119 { &IID_IVBSAXXMLFilter
, LibXml2
},
120 { &IID_IVBSAXXMLReader
, LibXml2
},
121 { &IID_IMXAttributes
, LibXml2
},
122 { &IID_IMXReaderControl
, LibXml2
},
123 { &IID_IMXWriter
, LibXml2
},
124 { &IID_IVBMXNamespaceManager
, LibXml2
},
125 { &IID_IServerXMLHTTPRequest
, LibXml2
}
128 const IID
*get_riid_from_tid(tid_t tid
)
130 return tid_ids
[tid
].iid
;
133 static inline unsigned get_libid_from_tid(tid_t tid
)
135 return tid_ids
[tid
].lib
;
138 static HRESULT
get_typelib(unsigned lib
, ITypeLib
**tl
)
143 hres
= LoadRegTypeLib(lib_ids
[lib
].iid
, lib_ids
[lib
].major
, 0, LOCALE_SYSTEM_DEFAULT
, tl
);
145 ERR("LoadRegTypeLib failed, hr %#lx.\n", hres
);
149 if (InterlockedCompareExchangePointer((void**)&typelib
[lib
], *tl
, NULL
))
150 ITypeLib_Release(*tl
);
157 HRESULT
get_typeinfo(enum tid_t tid
, ITypeInfo
**typeinfo
)
159 unsigned lib
= get_libid_from_tid(tid
);
163 if (FAILED(hres
= get_typelib(lib
, &typelib
)))
166 if(!typeinfos
[tid
]) {
169 hres
= ITypeLib_GetTypeInfoOfGuid(typelib
, get_riid_from_tid(tid
), &ti
);
171 /* try harder with typelib from msxml.dll */
172 if (FAILED(hres
= get_typelib(LibXml
, &typelib
)))
174 hres
= ITypeLib_GetTypeInfoOfGuid(typelib
, get_riid_from_tid(tid
), &ti
);
176 ERR("GetTypeInfoOfGuid failed, hr %#lx.\n", hres
);
181 if(InterlockedCompareExchangePointer((void**)(typeinfos
+tid
), ti
, NULL
))
182 ITypeInfo_Release(ti
);
185 *typeinfo
= typeinfos
[tid
];
187 ITypeInfo_AddRef(typeinfos
[tid
]);
191 void release_typelib(void)
196 while(!list_empty(&dispex_data_list
)) {
197 iter
= LIST_ENTRY(list_head(&dispex_data_list
), dispex_data_t
, entry
);
198 list_remove(&iter
->entry
);
200 for(i
=0; i
< iter
->func_cnt
; i
++)
201 SysFreeString(iter
->funcs
[i
].name
);
203 heap_free(iter
->funcs
);
204 heap_free(iter
->name_table
);
208 for(i
=0; i
< ARRAY_SIZE(typeinfos
); i
++)
210 ITypeInfo_Release(typeinfos
[i
]);
212 for(i
=0; i
< ARRAY_SIZE(typelib
); i
++)
214 ITypeLib_Release(typelib
[i
]);
216 DeleteCriticalSection(&cs_dispex_static_data
);
219 static void add_func_info(dispex_data_t
*data
, DWORD
*size
, tid_t tid
, DISPID id
, ITypeInfo
*dti
)
223 if(data
->func_cnt
&& data
->funcs
[data
->func_cnt
-1].id
== id
)
226 if(data
->func_cnt
== *size
)
227 data
->funcs
= heap_realloc(data
->funcs
, (*size
<<= 1)*sizeof(func_info_t
));
229 hres
= ITypeInfo_GetDocumentation(dti
, id
, &data
->funcs
[data
->func_cnt
].name
, NULL
, NULL
, NULL
);
233 data
->funcs
[data
->func_cnt
].id
= id
;
234 data
->funcs
[data
->func_cnt
].tid
= tid
;
239 static int __cdecl
dispid_cmp(const void *p1
, const void *p2
)
241 return ((const func_info_t
*)p1
)->id
- ((const func_info_t
*)p2
)->id
;
244 static int __cdecl
func_name_cmp(const void *p1
, const void *p2
)
246 return lstrcmpiW((*(func_info_t
* const*)p1
)->name
, (*(func_info_t
* const*)p2
)->name
);
249 static dispex_data_t
*preprocess_dispex_data(DispatchEx
*This
)
251 const tid_t
*tid
= This
->data
->iface_tids
;
258 TRACE("(%p)\n", This
);
260 hres
= get_typeinfo(This
->data
->disp_tid
, &dti
);
262 ERR("Could not get disp type info, hr %#lx.\n", hres
);
266 data
= heap_alloc(sizeof(dispex_data_t
));
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
->memid
, dti
);
283 ITypeInfo_ReleaseFuncDesc(ti
, funcdesc
);
286 ITypeInfo_Release(ti
);
290 if(!data
->func_cnt
) {
291 heap_free(data
->funcs
);
293 }else if(data
->func_cnt
!= size
) {
294 data
->funcs
= heap_realloc(data
->funcs
, data
->func_cnt
* sizeof(func_info_t
));
297 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
);
305 data
->name_table
= NULL
;
308 ITypeInfo_Release(dti
);
312 static dispex_data_t
*get_dispex_data(DispatchEx
*This
)
315 return This
->data
->data
;
317 EnterCriticalSection(&cs_dispex_static_data
);
319 if(!This
->data
->data
)
320 This
->data
->data
= preprocess_dispex_data(This
);
322 LeaveCriticalSection(&cs_dispex_static_data
);
324 return This
->data
->data
;
327 static inline DispatchEx
*impl_from_IDispatchEx(IDispatchEx
*iface
)
329 return CONTAINING_RECORD(iface
, DispatchEx
, IDispatchEx_iface
);
332 static HRESULT WINAPI
DispatchEx_QueryInterface(IDispatchEx
*iface
, REFIID riid
, void **ppv
)
334 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
336 return IUnknown_QueryInterface(This
->outer
, riid
, ppv
);
339 static ULONG WINAPI
DispatchEx_AddRef(IDispatchEx
*iface
)
341 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
343 return IUnknown_AddRef(This
->outer
);
346 static ULONG WINAPI
DispatchEx_Release(IDispatchEx
*iface
)
348 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
350 return IUnknown_Release(This
->outer
);
353 static HRESULT WINAPI
DispatchEx_GetTypeInfoCount(IDispatchEx
*iface
, UINT
*pctinfo
)
355 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
357 TRACE("(%p)->(%p)\n", This
, pctinfo
);
363 static HRESULT WINAPI
DispatchEx_GetTypeInfo(IDispatchEx
*iface
, UINT iTInfo
,
364 LCID lcid
, ITypeInfo
**ppTInfo
)
366 DispatchEx
*dispex
= impl_from_IDispatchEx(iface
);
368 TRACE("%p, %u, %lx, %p.\n", iface
, iTInfo
, lcid
, ppTInfo
);
370 return get_typeinfo(dispex
->data
->disp_tid
, ppTInfo
);
373 static HRESULT WINAPI
DispatchEx_GetIDsOfNames(IDispatchEx
*iface
, REFIID riid
,
374 LPOLESTR
*rgszNames
, UINT cNames
,
375 LCID lcid
, DISPID
*rgDispId
)
380 TRACE("%p, %s, %p, %u, %lx, %p.\n", iface
, debugstr_guid(riid
), rgszNames
, cNames
,
383 for(i
=0; i
< cNames
; i
++) {
384 hres
= IDispatchEx_GetDispID(iface
, rgszNames
[i
], 0, rgDispId
+i
);
392 static HRESULT WINAPI
DispatchEx_Invoke(IDispatchEx
*iface
, DISPID dispIdMember
,
393 REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
,
394 VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
396 TRACE("%p, %ld, %s, %lx, %x, %p, %p, %p, %p.\n", iface
, dispIdMember
, debugstr_guid(riid
),
397 lcid
, wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
399 return IDispatchEx_InvokeEx(iface
, dispIdMember
, lcid
, wFlags
, pDispParams
, pVarResult
, pExcepInfo
, NULL
);
402 static HRESULT WINAPI
DispatchEx_GetDispID(IDispatchEx
*iface
, BSTR bstrName
, DWORD grfdex
, DISPID
*pid
)
404 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
408 TRACE("%p, %s, %lx, %p.\n", iface
, debugstr_w(bstrName
), grfdex
, pid
);
410 if(grfdex
& ~(fdexNameCaseSensitive
|fdexNameEnsure
|fdexNameImplicit
))
411 FIXME("Unsupported grfdex %lx.\n", grfdex
);
413 data
= get_dispex_data(This
);
418 max
= data
->func_cnt
-1;
423 c
= lstrcmpiW(data
->name_table
[n
]->name
, bstrName
);
425 if((grfdex
& fdexNameCaseSensitive
) && wcscmp(data
->name_table
[n
]->name
, bstrName
))
428 *pid
= data
->name_table
[n
]->id
;
438 if(This
->data
->vtbl
&& This
->data
->vtbl
->get_dispid
) {
441 hres
= This
->data
->vtbl
->get_dispid(This
->outer
, bstrName
, grfdex
, pid
);
442 if(hres
!= DISP_E_UNKNOWNNAME
)
446 TRACE("not found %s\n", debugstr_w(bstrName
));
447 return DISP_E_UNKNOWNNAME
;
450 static BOOL
is_propputref_id(DISPID id
)
454 case DISPID_DOM_DOCUMENT_DOCUMENTELEMENT
:
455 case DISPID_XMLDOM_DOCUMENT2_SCHEMAS
:
456 case DISPID_XMLDOM_SELECTION_CONTEXT
:
457 case DISPID_XMLDOM_TEMPLATE_STYLESHEET
:
458 case DISPID_SAX_CONTENTHANDLER_DOCUMENTLOCATOR
:
459 case DISPID_SAX_XMLFILTER_PARENT
:
460 case DISPID_SAX_XMLREADER_ENTITYRESOLVER
:
461 case DISPID_SAX_XMLREADER_CONTENTHANDLER
:
462 case DISPID_SAX_XMLREADER_DTDHANDLER
:
463 case DISPID_SAX_XMLREADER_ERRORHANDLER
:
464 case DISPID_MXXML_FILTER_ENTITYRESOLVER
:
465 case DISPID_MXXML_FILTER_CONTENTHANDLER
:
466 case DISPID_MXXML_FILTER_DTDHANDLER
:
467 case DISPID_MXXML_FILTER_ERRORHANDLER
:
474 static HRESULT WINAPI
DispatchEx_InvokeEx(IDispatchEx
*iface
, DISPID id
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pdp
,
475 VARIANT
*pvarRes
, EXCEPINFO
*pei
, IServiceProvider
*pspCaller
)
477 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
485 TRACE("%p, %ld, %lx, %x, %p, %p, %p, %p.\n", iface
, id
, lcid
, wFlags
, pdp
, pvarRes
, pei
, pspCaller
);
487 if(This
->data
->vtbl
&& This
->data
->vtbl
->invoke
) {
488 hres
= This
->data
->vtbl
->invoke(This
->outer
, id
, lcid
, wFlags
, pdp
, pvarRes
, pei
);
489 if (hres
!= DISP_E_UNKNOWNNAME
) return hres
;
492 if(wFlags
== DISPATCH_CONSTRUCT
) {
493 FIXME("DISPATCH_CONSTRUCT not implemented\n");
497 data
= get_dispex_data(This
);
502 max
= data
->func_cnt
-1;
507 if(data
->funcs
[n
].id
== id
)
510 if(data
->funcs
[n
].id
< id
)
517 WARN("invalid id %lx.\n", id
);
518 return DISP_E_UNKNOWNNAME
;
521 hres
= get_typeinfo(data
->funcs
[n
].tid
, &ti
);
523 ERR("Could not get type info, hr %#lx.\n", hres
);
527 hres
= IUnknown_QueryInterface(This
->outer
, get_riid_from_tid(data
->funcs
[n
].tid
), (void**)&unk
);
529 ERR("Could not get interface, hr %#lx.\n", hres
);
530 ITypeInfo_Release(ti
);
534 if (is_propputref_id(id
) && wFlags
== DISPATCH_PROPERTYPUT
)
535 wFlags
= DISPATCH_PROPERTYPUTREF
;
536 hres
= ITypeInfo_Invoke(ti
, unk
, id
, wFlags
, pdp
, pvarRes
, pei
, &argerr
);
538 ITypeInfo_Release(ti
);
539 IUnknown_Release(unk
);
543 static HRESULT WINAPI
DispatchEx_DeleteMemberByName(IDispatchEx
*iface
, BSTR bstrName
, DWORD grfdex
)
545 TRACE("%p, %s, %lx.\n", iface
, debugstr_w(bstrName
), grfdex
);
549 static HRESULT WINAPI
DispatchEx_DeleteMemberByDispID(IDispatchEx
*iface
, DISPID id
)
551 TRACE("%p, %ld.\n", iface
, id
);
555 static HRESULT WINAPI
DispatchEx_GetMemberProperties(IDispatchEx
*iface
, DISPID id
, DWORD grfdexFetch
, DWORD
*pgrfdex
)
557 TRACE("%p, %ld, %lx, %p.\n", iface
, id
, grfdexFetch
, pgrfdex
);
561 static HRESULT WINAPI
DispatchEx_GetMemberName(IDispatchEx
*iface
, DISPID id
, BSTR
*pbstrName
)
563 TRACE("%p, %ld, %p.\n", iface
, id
, pbstrName
);
567 static HRESULT WINAPI
DispatchEx_GetNextDispID(IDispatchEx
*iface
, DWORD grfdex
, DISPID id
, DISPID
*pid
)
569 TRACE("%p, %lx, %ld, %p.\n", iface
, grfdex
, id
, pid
);
573 static HRESULT WINAPI
DispatchEx_GetNameSpaceParent(IDispatchEx
*iface
, IUnknown
**ppunk
)
575 TRACE("%p, %p.\n", iface
, ppunk
);
579 static IDispatchExVtbl DispatchExVtbl
= {
580 DispatchEx_QueryInterface
,
583 DispatchEx_GetTypeInfoCount
,
584 DispatchEx_GetTypeInfo
,
585 DispatchEx_GetIDsOfNames
,
587 DispatchEx_GetDispID
,
589 DispatchEx_DeleteMemberByName
,
590 DispatchEx_DeleteMemberByDispID
,
591 DispatchEx_GetMemberProperties
,
592 DispatchEx_GetMemberName
,
593 DispatchEx_GetNextDispID
,
594 DispatchEx_GetNameSpaceParent
597 BOOL
dispex_query_interface(DispatchEx
*This
, REFIID riid
, void **ppv
)
599 static const IID IID_UndocumentedScriptIface
=
600 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa0}};
601 static const IID IID_IDispatchJS
=
602 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa6}};
604 if(IsEqualGUID(&IID_IDispatch
, riid
)) {
605 TRACE("(%p)->(IID_IDispatch %p)\n", This
, ppv
);
606 *ppv
= &This
->IDispatchEx_iface
;
607 }else if(IsEqualGUID(&IID_IDispatchEx
, riid
)) {
608 TRACE("(%p)->(IID_IDispatchEx %p)\n", This
, ppv
);
609 *ppv
= &This
->IDispatchEx_iface
;
610 }else if(IsEqualGUID(&IID_IDispatchJS
, riid
)) {
611 TRACE("(%p)->(IID_IDispatchJS %p) returning NULL\n", This
, ppv
);
613 }else if(IsEqualGUID(&IID_UndocumentedScriptIface
, riid
)) {
614 TRACE("(%p)->(IID_UndocumentedScriptIface %p) returning NULL\n", This
, ppv
);
616 }else if (IsEqualGUID(&IID_IObjectIdentity
, riid
)) {
617 TRACE("IID_IObjectIdentity not supported returning NULL\n");
624 IUnknown_AddRef((IUnknown
*)*ppv
);
628 void init_dispex(DispatchEx
*dispex
, IUnknown
*outer
, dispex_static_data_t
*data
)
630 dispex
->IDispatchEx_iface
.lpVtbl
= &DispatchExVtbl
;
631 dispex
->outer
= outer
;