shell32: Update Korean resource.
[wine/multimedia.git] / dlls / msxml3 / dispex.c
blobfbbc35b0503085b6a8587403f3b68d7c2e5b47e6
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 #define COBJMACROS
21 #include "config.h"
23 #include <stdarg.h>
24 #ifdef HAVE_LIBXML2
25 # include <libxml/parser.h>
26 # include <libxml/xmlerror.h>
27 #endif
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winuser.h"
32 #include "winnls.h"
33 #include "ole2.h"
34 #include "msxml6.h"
35 #include "wininet.h"
36 #include "urlmon.h"
37 #include "winreg.h"
38 #include "shlwapi.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 typedef struct {
49 DISPID id;
50 BSTR name;
51 enum tid_t tid;
52 } func_info_t;
54 struct dispex_data_t {
55 DWORD func_cnt;
56 func_info_t *funcs;
57 func_info_t **name_table;
59 struct list entry;
62 typedef struct {
63 VARIANT var;
64 LPWSTR name;
65 } dynamic_prop_t;
67 struct dispex_dynamic_data_t {
68 DWORD buf_size;
69 DWORD prop_cnt;
70 dynamic_prop_t *props;
73 #define DISPID_DYNPROP_0 0x50000000
74 #define DISPID_DYNPROP_MAX 0x5fffffff
76 static struct list dispex_data_list = LIST_INIT(dispex_data_list);
77 static ITypeLib *typelib;
78 static ITypeInfo *typeinfos[LAST_tid];
80 static REFIID tid_ids[] = {
81 &IID_IXMLDOMAttribute,
82 &IID_IXMLDOMCDATASection,
83 &IID_IXMLDOMComment,
84 &IID_IXMLDOMDocument,
85 &IID_IXMLDOMDocument2,
86 &IID_IXMLDOMDocumentFragment,
87 &IID_IXMLDOMDocumentType,
88 &IID_IXMLDOMElement,
89 &IID_IXMLDOMEntityReference,
90 &IID_IXMLDOMImplementation,
91 &IID_IXMLDOMNamedNodeMap,
92 &IID_IXMLDOMNode,
93 &IID_IXMLDOMNodeList,
94 &IID_IXMLDOMParseError,
95 &IID_IXMLDOMProcessingInstruction,
96 &IID_IXMLDOMSchemaCollection,
97 &IID_IXMLDOMSelection,
98 &IID_IXMLDOMText,
99 &IID_IXMLElement,
100 &IID_IXMLDOMDocument,
101 &IID_IXMLHTTPRequest,
102 &IID_IXSLProcessor,
103 &IID_IXSLTemplate,
104 &IID_IVBSAXAttributes,
105 &IID_IVBSAXContentHandler,
106 &IID_IVBSAXDeclHandler,
107 &IID_IVBSAXDTDHandler,
108 &IID_IVBSAXEntityResolver,
109 &IID_IVBSAXErrorHandler,
110 &IID_IVBSAXLexicalHandler,
111 &IID_IVBSAXLocator,
112 &IID_IVBSAXXMLFilter,
113 &IID_IVBSAXXMLReader,
114 &IID_IMXAttributes,
115 &IID_IMXReaderControl,
116 &IID_IMXWriter,
119 HRESULT get_typeinfo(enum tid_t tid, ITypeInfo **typeinfo)
121 HRESULT hres;
123 if(!typelib) {
124 ITypeLib *tl;
126 hres = LoadRegTypeLib(&LIBID_MSXML2, 3, 0, LOCALE_SYSTEM_DEFAULT, &tl);
127 if(FAILED(hres)) {
128 ERR("LoadRegTypeLib failed: %08x\n", hres);
129 return hres;
132 if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
133 ITypeLib_Release(tl);
136 if(!typeinfos[tid]) {
137 ITypeInfo *ti;
139 hres = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &ti);
140 if(FAILED(hres)) {
141 ERR("GetTypeInfoOfGuid failed: %08x\n", hres);
142 return hres;
145 if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), ti, NULL))
146 ITypeInfo_Release(ti);
149 *typeinfo = typeinfos[tid];
151 ITypeInfo_AddRef(typeinfos[tid]);
152 return S_OK;
155 void release_typelib(void)
157 dispex_data_t *iter;
158 unsigned i;
160 while(!list_empty(&dispex_data_list)) {
161 iter = LIST_ENTRY(list_head(&dispex_data_list), dispex_data_t, entry);
162 list_remove(&iter->entry);
164 for(i=0; i < iter->func_cnt; i++)
165 SysFreeString(iter->funcs[i].name);
167 heap_free(iter->funcs);
168 heap_free(iter->name_table);
169 heap_free(iter);
172 if(!typelib)
173 return;
175 for(i=0; i < sizeof(typeinfos)/sizeof(*typeinfos); i++)
176 if(typeinfos[i])
177 ITypeInfo_Release(typeinfos[i]);
179 ITypeLib_Release(typelib);
182 static void add_func_info(dispex_data_t *data, DWORD *size, tid_t tid, DISPID id, ITypeInfo *dti)
184 HRESULT hres;
186 if(data->func_cnt && data->funcs[data->func_cnt-1].id == id)
187 return;
189 if(data->func_cnt == *size)
190 data->funcs = heap_realloc(data->funcs, (*size <<= 1)*sizeof(func_info_t));
192 hres = ITypeInfo_GetDocumentation(dti, id, &data->funcs[data->func_cnt].name, NULL, NULL, NULL);
193 if(FAILED(hres))
194 return;
196 data->funcs[data->func_cnt].id = id;
197 data->funcs[data->func_cnt].tid = tid;
199 data->func_cnt++;
202 static int dispid_cmp(const void *p1, const void *p2)
204 return ((const func_info_t*)p1)->id - ((const func_info_t*)p2)->id;
207 static int func_name_cmp(const void *p1, const void *p2)
209 return strcmpiW((*(func_info_t* const*)p1)->name, (*(func_info_t* const*)p2)->name);
212 static dispex_data_t *preprocess_dispex_data(DispatchEx *This)
214 const tid_t *tid = This->data->iface_tids;
215 FUNCDESC *funcdesc;
216 dispex_data_t *data;
217 DWORD size = 16, i;
218 ITypeInfo *ti, *dti;
219 HRESULT hres;
221 TRACE("(%p)\n", This);
223 hres = get_typeinfo(This->data->disp_tid, &dti);
224 if(FAILED(hres)) {
225 ERR("Could not get disp type info: %08x\n", hres);
226 return NULL;
229 data = heap_alloc(sizeof(dispex_data_t));
230 data->func_cnt = 0;
231 data->funcs = heap_alloc(size*sizeof(func_info_t));
232 list_add_tail(&dispex_data_list, &data->entry);
234 while(*tid) {
235 hres = get_typeinfo(*tid, &ti);
236 if(FAILED(hres))
237 break;
239 i=0;
240 while(1) {
241 hres = ITypeInfo_GetFuncDesc(ti, i++, &funcdesc);
242 if(FAILED(hres))
243 break;
245 add_func_info(data, &size, *tid, funcdesc->memid, dti);
246 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
249 ITypeInfo_Release(ti);
250 tid++;
253 if(!data->func_cnt) {
254 heap_free(data->funcs);
255 data->funcs = NULL;
256 }else if(data->func_cnt != size) {
257 data->funcs = heap_realloc(data->funcs, data->func_cnt * sizeof(func_info_t));
260 qsort(data->funcs, data->func_cnt, sizeof(func_info_t), dispid_cmp);
262 if(data->funcs) {
263 data->name_table = heap_alloc(data->func_cnt * sizeof(func_info_t*));
264 for(i=0; i < data->func_cnt; i++)
265 data->name_table[i] = data->funcs+i;
266 qsort(data->name_table, data->func_cnt, sizeof(func_info_t*), func_name_cmp);
267 }else {
268 data->name_table = NULL;
271 ITypeInfo_Release(dti);
272 return data;
275 static CRITICAL_SECTION cs_dispex_static_data;
276 static CRITICAL_SECTION_DEBUG cs_dispex_static_data_dbg =
278 0, 0, &cs_dispex_static_data,
279 { &cs_dispex_static_data_dbg.ProcessLocksList, &cs_dispex_static_data_dbg.ProcessLocksList },
280 0, 0, { (DWORD_PTR)(__FILE__ ": dispex_static_data") }
282 static CRITICAL_SECTION cs_dispex_static_data = { &cs_dispex_static_data_dbg, -1, 0, 0, 0, 0 };
285 static dispex_data_t *get_dispex_data(DispatchEx *This)
287 if(This->data->data)
288 return This->data->data;
290 EnterCriticalSection(&cs_dispex_static_data);
292 if(!This->data->data)
293 This->data->data = preprocess_dispex_data(This);
295 LeaveCriticalSection(&cs_dispex_static_data);
297 return This->data->data;
300 static inline BOOL is_custom_dispid(DISPID id)
302 return MSXML_DISPID_CUSTOM_MIN <= id && id <= MSXML_DISPID_CUSTOM_MAX;
305 static inline BOOL is_dynamic_dispid(DISPID id)
307 return DISPID_DYNPROP_0 <= id && id <= DISPID_DYNPROP_MAX;
310 static inline DispatchEx *impl_from_IDispatchEx(IDispatchEx *iface)
312 return CONTAINING_RECORD(iface, DispatchEx, IDispatchEx_iface);
315 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
317 DispatchEx *This = impl_from_IDispatchEx(iface);
319 return IUnknown_QueryInterface(This->outer, riid, ppv);
322 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
324 DispatchEx *This = impl_from_IDispatchEx(iface);
326 return IUnknown_AddRef(This->outer);
329 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
331 DispatchEx *This = impl_from_IDispatchEx(iface);
333 return IUnknown_Release(This->outer);
336 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
338 DispatchEx *This = impl_from_IDispatchEx(iface);
340 TRACE("(%p)->(%p)\n", This, pctinfo);
342 *pctinfo = 1;
343 return S_OK;
346 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
347 LCID lcid, ITypeInfo **ppTInfo)
349 DispatchEx *This = impl_from_IDispatchEx(iface);
351 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
353 return get_typeinfo(This->data->disp_tid, ppTInfo);
356 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
357 LPOLESTR *rgszNames, UINT cNames,
358 LCID lcid, DISPID *rgDispId)
360 DispatchEx *This = impl_from_IDispatchEx(iface);
361 UINT i;
362 HRESULT hres;
364 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
365 lcid, rgDispId);
367 for(i=0; i < cNames; i++) {
368 hres = IDispatchEx_GetDispID(&This->IDispatchEx_iface, rgszNames[i], 0, rgDispId+i);
369 if(FAILED(hres))
370 return hres;
373 return S_OK;
376 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
377 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
378 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
380 DispatchEx *This = impl_from_IDispatchEx(iface);
382 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
383 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
385 return IDispatchEx_InvokeEx(&This->IDispatchEx_iface, dispIdMember, lcid, wFlags,
386 pDispParams, pVarResult, pExcepInfo, NULL);
389 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
391 DispatchEx *This = impl_from_IDispatchEx(iface);
392 dispex_data_t *data;
393 int min, max, n, c;
395 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
397 if(grfdex & ~(fdexNameCaseSensitive|fdexNameEnsure|fdexNameImplicit))
398 FIXME("Unsupported grfdex %x\n", grfdex);
400 data = get_dispex_data(This);
401 if(!data)
402 return E_FAIL;
404 min = 0;
405 max = data->func_cnt-1;
407 while(min <= max) {
408 n = (min+max)/2;
410 c = strcmpiW(data->name_table[n]->name, bstrName);
411 if(!c) {
412 if((grfdex & fdexNameCaseSensitive) && strcmpW(data->name_table[n]->name, bstrName))
413 break;
415 *pid = data->name_table[n]->id;
416 return S_OK;
419 if(c > 0)
420 max = n-1;
421 else
422 min = n+1;
425 if(This->dynamic_data) {
426 unsigned i;
428 for(i=0; i < This->dynamic_data->prop_cnt; i++) {
429 if(!strcmpW(This->dynamic_data->props[i].name, bstrName)) {
430 *pid = DISPID_DYNPROP_0 + i;
431 return S_OK;
436 if(This->data->vtbl && This->data->vtbl->get_dispid) {
437 HRESULT hres;
439 hres = This->data->vtbl->get_dispid(This->outer, bstrName, grfdex, pid);
440 if(hres != DISP_E_UNKNOWNNAME)
441 return hres;
444 if(grfdex & fdexNameEnsure) {
445 dispex_dynamic_data_t *dynamic_data;
447 TRACE("creating dynamic prop %s\n", debugstr_w(bstrName));
449 if(This->dynamic_data) {
450 dynamic_data = This->dynamic_data;
451 }else {
452 dynamic_data = This->dynamic_data = heap_alloc_zero(sizeof(dispex_dynamic_data_t));
453 if(!dynamic_data)
454 return E_OUTOFMEMORY;
457 if(!dynamic_data->buf_size) {
458 dynamic_data->props = heap_alloc(sizeof(dynamic_prop_t)*4);
459 if(!dynamic_data->props)
460 return E_OUTOFMEMORY;
461 dynamic_data->buf_size = 4;
462 }else if(dynamic_data->buf_size == dynamic_data->prop_cnt) {
463 dynamic_prop_t *new_props;
465 new_props = heap_realloc(dynamic_data->props, sizeof(dynamic_prop_t)*(dynamic_data->buf_size<<1));
466 if(!new_props)
467 return E_OUTOFMEMORY;
469 dynamic_data->props = new_props;
470 dynamic_data->buf_size <<= 1;
473 dynamic_data->props[dynamic_data->prop_cnt].name = heap_strdupW(bstrName);
474 VariantInit(&dynamic_data->props[dynamic_data->prop_cnt].var);
475 *pid = DISPID_DYNPROP_0 + dynamic_data->prop_cnt++;
477 return S_OK;
480 TRACE("not found %s\n", debugstr_w(bstrName));
481 return DISP_E_UNKNOWNNAME;
484 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
485 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
487 DispatchEx *This = impl_from_IDispatchEx(iface);
488 IUnknown *unk;
489 ITypeInfo *ti;
490 dispex_data_t *data;
491 UINT argerr=0;
492 int min, max, n;
493 HRESULT hres;
495 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
497 if(is_custom_dispid(id) && This->data->vtbl && This->data->vtbl->invoke)
498 return This->data->vtbl->invoke(This->outer, id, lcid, wFlags, pdp, pvarRes, pei);
500 if(wFlags == DISPATCH_CONSTRUCT) {
501 FIXME("DISPATCH_CONSTRUCT not implemented\n");
502 return E_NOTIMPL;
505 if(is_dynamic_dispid(id)) {
506 DWORD idx = id - DISPID_DYNPROP_0;
507 VARIANT *var;
509 if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
510 return DISP_E_UNKNOWNNAME;
512 var = &This->dynamic_data->props[idx].var;
514 switch(wFlags) {
515 case INVOKE_PROPERTYGET:
516 return VariantCopy(pvarRes, var);
517 case INVOKE_PROPERTYPUT:
518 VariantClear(var);
519 return VariantCopy(var, pdp->rgvarg);
520 default:
521 FIXME("unhandled wFlags %x\n", wFlags);
522 return E_NOTIMPL;
526 data = get_dispex_data(This);
527 if(!data)
528 return E_FAIL;
530 min = 0;
531 max = data->func_cnt-1;
533 while(min <= max) {
534 n = (min+max)/2;
536 if(data->funcs[n].id == id)
537 break;
539 if(data->funcs[n].id < id)
540 min = n+1;
541 else
542 max = n-1;
545 if(min > max) {
546 WARN("invalid id %x\n", id);
547 return DISP_E_UNKNOWNNAME;
550 hres = get_typeinfo(data->funcs[n].tid, &ti);
551 if(FAILED(hres)) {
552 ERR("Could not get type info: %08x\n", hres);
553 return hres;
556 hres = IUnknown_QueryInterface(This->outer, tid_ids[data->funcs[n].tid], (void**)&unk);
557 if(FAILED(hres)) {
558 ERR("Could not get iface: %08x\n", hres);
559 return E_FAIL;
562 hres = ITypeInfo_Invoke(ti, unk, id, wFlags, pdp, pvarRes, pei, &argerr);
564 ITypeInfo_Release(ti);
565 IUnknown_Release(unk);
566 return hres;
569 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
571 DispatchEx *This = impl_from_IDispatchEx(iface);
572 TRACE("Not implemented in native msxml3 (%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
573 return E_NOTIMPL;
576 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
578 DispatchEx *This = impl_from_IDispatchEx(iface);
579 TRACE("Not implemented in native msxml3 (%p)->(%x)\n", This, id);
580 return E_NOTIMPL;
583 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
585 DispatchEx *This = impl_from_IDispatchEx(iface);
586 TRACE("Not implemented in native msxml3 (%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
587 return E_NOTIMPL;
590 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
592 DispatchEx *This = impl_from_IDispatchEx(iface);
593 TRACE("Not implemented in native msxml3 (%p)->(%x %p)\n", This, id, pbstrName);
594 return E_NOTIMPL;
597 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
599 DispatchEx *This = impl_from_IDispatchEx(iface);
600 TRACE(" Not implemented in native msxml3 (%p)->(%x %x %p)\n", This, grfdex, id, pid);
601 return E_NOTIMPL;
604 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
606 DispatchEx *This = impl_from_IDispatchEx(iface);
607 TRACE("Not implemented in native msxml3 (%p)->(%p)\n", This, ppunk);
608 return E_NOTIMPL;
611 static IDispatchExVtbl DispatchExVtbl = {
612 DispatchEx_QueryInterface,
613 DispatchEx_AddRef,
614 DispatchEx_Release,
615 DispatchEx_GetTypeInfoCount,
616 DispatchEx_GetTypeInfo,
617 DispatchEx_GetIDsOfNames,
618 DispatchEx_Invoke,
619 DispatchEx_GetDispID,
620 DispatchEx_InvokeEx,
621 DispatchEx_DeleteMemberByName,
622 DispatchEx_DeleteMemberByDispID,
623 DispatchEx_GetMemberProperties,
624 DispatchEx_GetMemberName,
625 DispatchEx_GetNextDispID,
626 DispatchEx_GetNameSpaceParent
629 BOOL dispex_query_interface(DispatchEx *This, REFIID riid, void **ppv)
631 static const IID IID_UndocumentedScriptIface =
632 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa0}};
634 if(IsEqualGUID(&IID_IDispatch, riid)) {
635 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
636 *ppv = &This->IDispatchEx_iface;
637 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
638 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
639 *ppv = &This->IDispatchEx_iface;
640 }else if(IsEqualGUID(&IID_UndocumentedScriptIface, riid)) {
641 TRACE("(%p)->(IID_UndocumentedScriptIface %p) returning NULL\n", This, ppv);
642 *ppv = NULL;
643 }else if (IsEqualGUID(&IID_IObjectIdentity, riid)) {
644 TRACE("IID_IObjectIdentity not supported returning NULL\n");
645 *ppv = NULL;
646 }else {
647 return FALSE;
650 if(*ppv)
651 IUnknown_AddRef((IUnknown*)*ppv);
652 return TRUE;
655 void init_dispex(DispatchEx *dispex, IUnknown *outer, dispex_static_data_t *data)
657 dispex->IDispatchEx_iface.lpVtbl = &DispatchExVtbl;
658 dispex->outer = outer;
659 dispex->data = data;