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
36 #include "wine/debug.h"
37 #include "wine/list.h"
38 #include "wine/unicode.h"
40 #include "msxml_private.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(msxml
);
44 #define DISPATCHEX(x) ((IDispatchEx*) &(x)->lpIDispatchExVtbl)
52 struct dispex_data_t
{
55 func_info_t
**name_table
;
65 struct dispex_dynamic_data_t
{
68 dynamic_prop_t
*props
;
71 #define DISPID_DYNPROP_0 0x50000000
72 #define DISPID_DYNPROP_MAX 0x5fffffff
74 static struct list dispex_data_list
= LIST_INIT(dispex_data_list
);
75 static ITypeLib
*typelib
;
76 static ITypeInfo
*typeinfos
[LAST_tid
];
78 static REFIID tid_ids
[] = {
79 &IID_IXMLDOMAttribute
,
80 &IID_IXMLDOMCDATASection
,
82 &IID_IXMLDOMDocument2
,
83 &IID_IXMLDOMDocumentFragment
,
85 &IID_IXMLDOMEntityReference
,
86 &IID_IXMLDOMImplementation
,
87 &IID_IXMLDOMNamedNodeMap
,
90 &IID_IXMLDOMParseError
,
91 &IID_IXMLDOMProcessingInstruction
,
92 &IID_IXMLDOMSchemaCollection
,
96 &IID_IVBSAXAttributes
,
97 &IID_IVBSAXContentHandler
,
98 &IID_IVBSAXDeclHandler
,
99 &IID_IVBSAXDTDHandler
,
100 &IID_IVBSAXEntityResolver
,
101 &IID_IVBSAXErrorHandler
,
102 &IID_IVBSAXLexicalHandler
,
104 &IID_IVBSAXXMLFilter
,
105 &IID_IVBSAXXMLReader
,
107 &IID_IMXReaderControl
,
111 HRESULT
get_typeinfo(enum tid_t tid
, ITypeInfo
**typeinfo
)
118 hres
= LoadRegTypeLib(&LIBID_MSXML2
, 3, 0, LOCALE_SYSTEM_DEFAULT
, &tl
);
120 ERR("LoadRegTypeLib failed: %08x\n", hres
);
124 if(InterlockedCompareExchangePointer((void**)&typelib
, tl
, NULL
))
125 ITypeLib_Release(tl
);
128 if(!typeinfos
[tid
]) {
131 hres
= ITypeLib_GetTypeInfoOfGuid(typelib
, tid_ids
[tid
], &typeinfo
);
133 ERR("GetTypeInfoOfGuid failed: %08x\n", hres
);
137 if(InterlockedCompareExchangePointer((void**)(typeinfos
+tid
), typeinfo
, NULL
))
138 ITypeInfo_Release(typeinfo
);
141 *typeinfo
= typeinfos
[tid
];
143 ITypeInfo_AddRef(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
);
174 static void add_func_info(dispex_data_t
*data
, DWORD
*size
, tid_t tid
, DISPID id
, ITypeInfo
*dti
)
178 if(data
->func_cnt
&& data
->funcs
[data
->func_cnt
-1].id
== id
)
181 if(data
->func_cnt
== *size
)
182 data
->funcs
= heap_realloc(data
->funcs
, (*size
<<= 1)*sizeof(func_info_t
));
184 hres
= ITypeInfo_GetDocumentation(dti
, id
, &data
->funcs
[data
->func_cnt
].name
, NULL
, NULL
, NULL
);
188 data
->funcs
[data
->func_cnt
].id
= id
;
189 data
->funcs
[data
->func_cnt
].tid
= tid
;
194 static int dispid_cmp(const void *p1
, const void *p2
)
196 return ((func_info_t
*)p1
)->id
- ((func_info_t
*)p2
)->id
;
199 static int func_name_cmp(const void *p1
, const void *p2
)
201 return strcmpiW((*(func_info_t
**)p1
)->name
, (*(func_info_t
**)p2
)->name
);
204 static dispex_data_t
*preprocess_dispex_data(DispatchEx
*This
)
206 const tid_t
*tid
= This
->data
->iface_tids
;
213 TRACE("(%p)\n", This
);
215 hres
= get_typeinfo(This
->data
->disp_tid
, &dti
);
217 ERR("Could not get disp type info: %08x\n", hres
);
221 data
= heap_alloc(sizeof(dispex_data_t
));
223 data
->funcs
= heap_alloc(size
*sizeof(func_info_t
));
224 list_add_tail(&dispex_data_list
, &data
->entry
);
227 hres
= get_typeinfo(*tid
, &ti
);
233 hres
= ITypeInfo_GetFuncDesc(ti
, i
++, &funcdesc
);
237 add_func_info(data
, &size
, *tid
, funcdesc
->memid
, dti
);
238 ITypeInfo_ReleaseFuncDesc(ti
, funcdesc
);
241 ITypeInfo_Release(ti
);
245 if(!data
->func_cnt
) {
246 heap_free(data
->funcs
);
248 }else if(data
->func_cnt
!= size
) {
249 data
->funcs
= heap_realloc(data
->funcs
, data
->func_cnt
* sizeof(func_info_t
));
252 qsort(data
->funcs
, data
->func_cnt
, sizeof(func_info_t
), dispid_cmp
);
255 data
->name_table
= heap_alloc(data
->func_cnt
* sizeof(func_info_t
*));
256 for(i
=0; i
< data
->func_cnt
; i
++)
257 data
->name_table
[i
] = data
->funcs
+i
;
258 qsort(data
->name_table
, data
->func_cnt
, sizeof(func_info_t
*), func_name_cmp
);
260 data
->name_table
= NULL
;
263 ITypeInfo_Release(dti
);
267 static CRITICAL_SECTION cs_dispex_static_data
;
268 static CRITICAL_SECTION_DEBUG cs_dispex_static_data_dbg
=
270 0, 0, &cs_dispex_static_data
,
271 { &cs_dispex_static_data_dbg
.ProcessLocksList
, &cs_dispex_static_data_dbg
.ProcessLocksList
},
272 0, 0, { (DWORD_PTR
)(__FILE__
": dispex_static_data") }
274 static CRITICAL_SECTION cs_dispex_static_data
= { &cs_dispex_static_data_dbg
, -1, 0, 0, 0, 0 };
277 static dispex_data_t
*get_dispex_data(DispatchEx
*This
)
280 return This
->data
->data
;
282 EnterCriticalSection(&cs_dispex_static_data
);
284 if(!This
->data
->data
)
285 This
->data
->data
= preprocess_dispex_data(This
);
287 LeaveCriticalSection(&cs_dispex_static_data
);
289 return This
->data
->data
;
292 static inline BOOL
is_custom_dispid(DISPID id
)
294 return MSXML_DISPID_CUSTOM_MIN
<= id
&& id
<= MSXML_DISPID_CUSTOM_MAX
;
297 static inline BOOL
is_dynamic_dispid(DISPID id
)
299 return DISPID_DYNPROP_0
<= id
&& id
<= DISPID_DYNPROP_MAX
;
302 static inline DispatchEx
*impl_from_IDispatchEx(IDispatchEx
*iface
)
304 return (DispatchEx
*)((char*)iface
- FIELD_OFFSET(DispatchEx
, lpIDispatchExVtbl
));
307 static HRESULT WINAPI
DispatchEx_QueryInterface(IDispatchEx
*iface
, REFIID riid
, void **ppv
)
309 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
311 return IUnknown_QueryInterface(This
->outer
, riid
, ppv
);
314 static ULONG WINAPI
DispatchEx_AddRef(IDispatchEx
*iface
)
316 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
318 return IUnknown_AddRef(This
->outer
);
321 static ULONG WINAPI
DispatchEx_Release(IDispatchEx
*iface
)
323 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
325 return IUnknown_Release(This
->outer
);
328 static HRESULT WINAPI
DispatchEx_GetTypeInfoCount(IDispatchEx
*iface
, UINT
*pctinfo
)
330 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
332 TRACE("(%p)->(%p)\n", This
, pctinfo
);
338 static HRESULT WINAPI
DispatchEx_GetTypeInfo(IDispatchEx
*iface
, UINT iTInfo
,
339 LCID lcid
, ITypeInfo
**ppTInfo
)
341 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
343 TRACE("(%p)->(%u %u %p)\n", This
, iTInfo
, lcid
, ppTInfo
);
345 return get_typeinfo(This
->data
->disp_tid
, ppTInfo
);
348 static HRESULT WINAPI
DispatchEx_GetIDsOfNames(IDispatchEx
*iface
, REFIID riid
,
349 LPOLESTR
*rgszNames
, UINT cNames
,
350 LCID lcid
, DISPID
*rgDispId
)
352 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
356 TRACE("(%p)->(%s %p %u %u %p)\n", This
, debugstr_guid(riid
), rgszNames
, cNames
,
359 for(i
=0; i
< cNames
; i
++) {
360 hres
= IDispatchEx_GetDispID(DISPATCHEX(This
), rgszNames
[i
], 0, rgDispId
+i
);
368 static HRESULT WINAPI
DispatchEx_Invoke(IDispatchEx
*iface
, DISPID dispIdMember
,
369 REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
,
370 VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
372 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
374 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This
, dispIdMember
, debugstr_guid(riid
),
375 lcid
, wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
377 return IDispatchEx_InvokeEx(DISPATCHEX(This
), dispIdMember
, lcid
, wFlags
,
378 pDispParams
, pVarResult
, pExcepInfo
, NULL
);
381 static HRESULT WINAPI
DispatchEx_GetDispID(IDispatchEx
*iface
, BSTR bstrName
, DWORD grfdex
, DISPID
*pid
)
383 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
387 TRACE("(%p)->(%s %x %p)\n", This
, debugstr_w(bstrName
), grfdex
, pid
);
389 if(grfdex
& ~(fdexNameCaseSensitive
|fdexNameEnsure
|fdexNameImplicit
))
390 FIXME("Unsupported grfdex %x\n", grfdex
);
392 data
= get_dispex_data(This
);
397 max
= data
->func_cnt
-1;
402 c
= strcmpiW(data
->name_table
[n
]->name
, bstrName
);
404 if((grfdex
& fdexNameCaseSensitive
) && strcmpW(data
->name_table
[n
]->name
, bstrName
))
407 *pid
= data
->name_table
[n
]->id
;
417 if(This
->dynamic_data
) {
420 for(i
=0; i
< This
->dynamic_data
->prop_cnt
; i
++) {
421 if(!strcmpW(This
->dynamic_data
->props
[i
].name
, bstrName
)) {
422 *pid
= DISPID_DYNPROP_0
+ i
;
428 if(This
->data
->vtbl
&& This
->data
->vtbl
->get_dispid
) {
431 hres
= This
->data
->vtbl
->get_dispid(This
->outer
, bstrName
, grfdex
, pid
);
432 if(hres
!= DISP_E_UNKNOWNNAME
)
436 if(grfdex
& fdexNameEnsure
) {
437 TRACE("creating dynamic prop %s\n", debugstr_w(bstrName
));
439 if(!This
->dynamic_data
) {
440 This
->dynamic_data
= heap_alloc_zero(sizeof(dispex_dynamic_data_t
));
441 This
->dynamic_data
->props
= heap_alloc(This
->dynamic_data
->buf_size
= 4);
442 }else if(This
->dynamic_data
->buf_size
== This
->dynamic_data
->prop_cnt
) {
443 This
->dynamic_data
->props
= heap_realloc(This
->dynamic_data
->props
, This
->dynamic_data
->buf_size
<<=1);
446 This
->dynamic_data
->props
[This
->dynamic_data
->prop_cnt
].name
= heap_strdupW(bstrName
);
447 VariantInit(&This
->dynamic_data
->props
[This
->dynamic_data
->prop_cnt
].var
);
448 *pid
= DISPID_DYNPROP_0
+ This
->dynamic_data
->prop_cnt
++;
453 TRACE("not found %s\n", debugstr_w(bstrName
));
454 return DISP_E_UNKNOWNNAME
;
457 static HRESULT WINAPI
DispatchEx_InvokeEx(IDispatchEx
*iface
, DISPID id
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pdp
,
458 VARIANT
*pvarRes
, EXCEPINFO
*pei
, IServiceProvider
*pspCaller
)
460 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
468 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This
, id
, lcid
, wFlags
, pdp
, pvarRes
, pei
, pspCaller
);
470 if(is_custom_dispid(id
) && This
->data
->vtbl
&& This
->data
->vtbl
->invoke
)
471 return This
->data
->vtbl
->invoke(This
->outer
, id
, lcid
, wFlags
, pdp
, pvarRes
, pei
);
473 if(wFlags
== DISPATCH_CONSTRUCT
) {
474 FIXME("DISPATCH_CONSTRUCT not implemented\n");
478 if(is_dynamic_dispid(id
)) {
479 DWORD idx
= id
- DISPID_DYNPROP_0
;
482 if(!This
->dynamic_data
|| This
->dynamic_data
->prop_cnt
<= idx
)
483 return DISP_E_UNKNOWNNAME
;
485 var
= &This
->dynamic_data
->props
[idx
].var
;
488 case INVOKE_PROPERTYGET
:
489 return VariantCopy(pvarRes
, var
);
490 case INVOKE_PROPERTYPUT
:
492 return VariantCopy(var
, pdp
->rgvarg
);
494 FIXME("unhandled wFlags %x\n", wFlags
);
499 data
= get_dispex_data(This
);
504 max
= data
->func_cnt
-1;
509 if(data
->funcs
[n
].id
== id
)
512 if(data
->funcs
[n
].id
< id
)
519 WARN("invalid id %x\n", id
);
520 return DISP_E_UNKNOWNNAME
;
523 hres
= get_typeinfo(data
->funcs
[n
].tid
, &ti
);
525 ERR("Could not get type info: %08x\n", hres
);
529 hres
= IUnknown_QueryInterface(This
->outer
, tid_ids
[data
->funcs
[n
].tid
], (void**)&unk
);
531 ERR("Could not get iface: %08x\n", hres
);
535 hres
= ITypeInfo_Invoke(ti
, unk
, id
, wFlags
, pdp
, pvarRes
, pei
, &argerr
);
537 ITypeInfo_Release(ti
);
538 IUnknown_Release(unk
);
542 static HRESULT WINAPI
DispatchEx_DeleteMemberByName(IDispatchEx
*iface
, BSTR bstrName
, DWORD grfdex
)
544 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
545 FIXME("(%p)->(%s %x)\n", This
, debugstr_w(bstrName
), grfdex
);
549 static HRESULT WINAPI
DispatchEx_DeleteMemberByDispID(IDispatchEx
*iface
, DISPID id
)
551 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
552 FIXME("(%p)->(%x)\n", This
, id
);
556 static HRESULT WINAPI
DispatchEx_GetMemberProperties(IDispatchEx
*iface
, DISPID id
, DWORD grfdexFetch
, DWORD
*pgrfdex
)
558 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
559 FIXME("(%p)->(%x %x %p)\n", This
, id
, grfdexFetch
, pgrfdex
);
563 static HRESULT WINAPI
DispatchEx_GetMemberName(IDispatchEx
*iface
, DISPID id
, BSTR
*pbstrName
)
565 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
566 FIXME("(%p)->(%x %p)\n", This
, id
, pbstrName
);
570 static HRESULT WINAPI
DispatchEx_GetNextDispID(IDispatchEx
*iface
, DWORD grfdex
, DISPID id
, DISPID
*pid
)
572 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
573 FIXME("(%p)->(%x %x %p)\n", This
, grfdex
, id
, pid
);
577 static HRESULT WINAPI
DispatchEx_GetNameSpaceParent(IDispatchEx
*iface
, IUnknown
**ppunk
)
579 DispatchEx
*This
= impl_from_IDispatchEx(iface
);
580 FIXME("(%p)->(%p)\n", This
, ppunk
);
584 static IDispatchExVtbl DispatchExVtbl
= {
585 DispatchEx_QueryInterface
,
588 DispatchEx_GetTypeInfoCount
,
589 DispatchEx_GetTypeInfo
,
590 DispatchEx_GetIDsOfNames
,
592 DispatchEx_GetDispID
,
594 DispatchEx_DeleteMemberByName
,
595 DispatchEx_DeleteMemberByDispID
,
596 DispatchEx_GetMemberProperties
,
597 DispatchEx_GetMemberName
,
598 DispatchEx_GetNextDispID
,
599 DispatchEx_GetNameSpaceParent
602 BOOL
dispex_query_interface(DispatchEx
*This
, REFIID riid
, void **ppv
)
604 static const IID IID_UndocumentedScriptIface
=
605 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa0}};
607 if(IsEqualGUID(&IID_IDispatch
, riid
)) {
608 TRACE("(%p)->(IID_IDispatch %p)\n", This
, ppv
);
609 *ppv
= DISPATCHEX(This
);
610 }else if(IsEqualGUID(&IID_IDispatchEx
, riid
)) {
611 TRACE("(%p)->(IID_IDispatchEx %p)\n", This
, ppv
);
612 *ppv
= DISPATCHEX(This
);
613 }else if(IsEqualGUID(&IID_UndocumentedScriptIface
, riid
)) {
614 TRACE("(%p)->(IID_UndocumentedScriptIface %p) returning NULL\n", This
, ppv
);
621 IUnknown_AddRef((IUnknown
*)*ppv
);
625 void init_dispex(DispatchEx
*dispex
, IUnknown
*outer
, dispex_static_data_t
*data
)
627 dispex
->lpIDispatchExVtbl
= &DispatchExVtbl
;
628 dispex
->outer
= outer
;