mountmgr: Name DosDevices constants consistently.
[wine.git] / dlls / msxml3 / dispex.c
blobd0520f3577832421af47eded9eaf3c7301cbbfa8
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 "msxml6did.h"
36 #include "wininet.h"
37 #include "urlmon.h"
38 #include "winreg.h"
39 #include "shlwapi.h"
41 #include "wine/debug.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 };
58 enum lib_version_t
60 LibXml = 0,
61 LibXml2,
62 LibXml_Last
65 typedef struct {
66 REFIID iid;
67 enum lib_version_t lib;
68 } tid_id_t;
70 typedef struct {
71 REFIID iid;
72 unsigned short major;
73 } lib_id_t;
75 typedef struct {
76 DISPID id;
77 BSTR name;
78 enum tid_t tid;
79 } func_info_t;
81 struct dispex_data_t {
82 DWORD func_cnt;
83 func_info_t *funcs;
84 func_info_t **name_table;
86 struct list entry;
89 static struct list dispex_data_list = LIST_INIT(dispex_data_list);
90 static ITypeLib *typelib[LibXml_Last];
91 static ITypeInfo *typeinfos[LAST_tid];
93 /* indexed with lib_version_t values */
94 static lib_id_t lib_ids[] = {
95 { &LIBID_MSXML, 2 },
96 { &LIBID_MSXML2, 3 }
99 static tid_id_t tid_ids[] = {
100 { &IID_NULL, LibXml_Last },
101 { &IID_IXMLDOMAttribute, LibXml2 },
102 { &IID_IXMLDOMCDATASection, LibXml2 },
103 { &IID_IXMLDOMComment, LibXml2 },
104 { &IID_IXMLDOMDocument, LibXml2 },
105 { &IID_IXMLDOMDocument2, LibXml2 },
106 { &IID_IXMLDOMDocument3, LibXml2 },
107 { &IID_IXMLDOMDocumentFragment, LibXml2 },
108 { &IID_IXMLDOMDocumentType, LibXml2 },
109 { &IID_IXMLDOMElement, LibXml2 },
110 { &IID_IXMLDOMEntityReference, LibXml2 },
111 { &IID_IXMLDOMImplementation, LibXml2 },
112 { &IID_IXMLDOMNamedNodeMap, LibXml2 },
113 { &IID_IXMLDOMNode, LibXml2 },
114 { &IID_IXMLDOMNodeList, LibXml2 },
115 { &IID_IXMLDOMParseError2, LibXml2 },
116 { &IID_IXMLDOMProcessingInstruction, LibXml2 },
117 { &IID_IXMLDOMSchemaCollection, LibXml2 },
118 { &IID_IXMLDOMSchemaCollection2, LibXml2 },
119 { &IID_IXMLDOMSelection, LibXml2 },
120 { &IID_IXMLDOMText, LibXml2 },
121 { &IID_IXMLElement, LibXml },
122 { &IID_IXMLDocument, LibXml },
123 { &IID_IXMLHTTPRequest, LibXml2 },
124 { &IID_IXSLProcessor, LibXml2 },
125 { &IID_IXSLTemplate, LibXml2 },
126 { &IID_IVBSAXAttributes, LibXml2 },
127 { &IID_IVBSAXContentHandler, LibXml2 },
128 { &IID_IVBSAXDeclHandler, LibXml2 },
129 { &IID_IVBSAXDTDHandler, LibXml2 },
130 { &IID_IVBSAXEntityResolver, LibXml2 },
131 { &IID_IVBSAXErrorHandler, LibXml2 },
132 { &IID_IVBSAXLexicalHandler, LibXml2 },
133 { &IID_IVBSAXLocator, LibXml2 },
134 { &IID_IVBSAXXMLFilter, LibXml2 },
135 { &IID_IVBSAXXMLReader, LibXml2 },
136 { &IID_IMXAttributes, LibXml2 },
137 { &IID_IMXReaderControl, LibXml2 },
138 { &IID_IMXWriter, LibXml2 },
139 { &IID_IVBMXNamespaceManager, LibXml2 },
140 { &IID_IServerXMLHTTPRequest, LibXml2 }
143 const IID *get_riid_from_tid(tid_t tid)
145 return tid_ids[tid].iid;
148 static inline unsigned get_libid_from_tid(tid_t tid)
150 return tid_ids[tid].lib;
153 HRESULT get_typeinfo(enum tid_t tid, ITypeInfo **typeinfo)
155 unsigned lib = get_libid_from_tid(tid);
156 HRESULT hres;
158 if(!typelib[lib]) {
159 ITypeLib *tl;
161 hres = LoadRegTypeLib(lib_ids[lib].iid, lib_ids[lib].major, 0, LOCALE_SYSTEM_DEFAULT, &tl);
162 if(FAILED(hres)) {
163 ERR("LoadRegTypeLib failed: %08x\n", hres);
164 return hres;
167 if(InterlockedCompareExchangePointer((void**)&typelib[lib], tl, NULL))
168 ITypeLib_Release(tl);
171 if(!typeinfos[tid]) {
172 ITypeInfo *ti;
174 hres = ITypeLib_GetTypeInfoOfGuid(typelib[lib], get_riid_from_tid(tid), &ti);
175 if(FAILED(hres)) {
176 /* try harder with typelib from msxml.dll */
177 hres = ITypeLib_GetTypeInfoOfGuid(typelib[LibXml], get_riid_from_tid(tid), &ti);
178 if(FAILED(hres)) {
179 ERR("GetTypeInfoOfGuid failed: %08x\n", hres);
180 return hres;
184 if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), ti, NULL))
185 ITypeInfo_Release(ti);
188 *typeinfo = typeinfos[tid];
190 ITypeInfo_AddRef(typeinfos[tid]);
191 return S_OK;
194 void release_typelib(void)
196 dispex_data_t *iter;
197 unsigned i;
199 while(!list_empty(&dispex_data_list)) {
200 iter = LIST_ENTRY(list_head(&dispex_data_list), dispex_data_t, entry);
201 list_remove(&iter->entry);
203 for(i=0; i < iter->func_cnt; i++)
204 SysFreeString(iter->funcs[i].name);
206 heap_free(iter->funcs);
207 heap_free(iter->name_table);
208 heap_free(iter);
211 for(i=0; i < sizeof(typeinfos)/sizeof(*typeinfos); i++)
212 if(typeinfos[i])
213 ITypeInfo_Release(typeinfos[i]);
215 for(i=0; i < sizeof(typelib)/sizeof(*typelib); i++)
216 if(typelib[i])
217 ITypeLib_Release(typelib[i]);
219 DeleteCriticalSection(&cs_dispex_static_data);
222 static void add_func_info(dispex_data_t *data, DWORD *size, tid_t tid, DISPID id, ITypeInfo *dti)
224 HRESULT hres;
226 if(data->func_cnt && data->funcs[data->func_cnt-1].id == id)
227 return;
229 if(data->func_cnt == *size)
230 data->funcs = heap_realloc(data->funcs, (*size <<= 1)*sizeof(func_info_t));
232 hres = ITypeInfo_GetDocumentation(dti, id, &data->funcs[data->func_cnt].name, NULL, NULL, NULL);
233 if(FAILED(hres))
234 return;
236 data->funcs[data->func_cnt].id = id;
237 data->funcs[data->func_cnt].tid = tid;
239 data->func_cnt++;
242 static int dispid_cmp(const void *p1, const void *p2)
244 return ((const func_info_t*)p1)->id - ((const func_info_t*)p2)->id;
247 static int func_name_cmp(const void *p1, const void *p2)
249 return strcmpiW((*(func_info_t* const*)p1)->name, (*(func_info_t* const*)p2)->name);
252 static dispex_data_t *preprocess_dispex_data(DispatchEx *This)
254 const tid_t *tid = This->data->iface_tids;
255 FUNCDESC *funcdesc;
256 dispex_data_t *data;
257 DWORD size = 16, i;
258 ITypeInfo *ti, *dti;
259 HRESULT hres;
261 TRACE("(%p)\n", This);
263 hres = get_typeinfo(This->data->disp_tid, &dti);
264 if(FAILED(hres)) {
265 ERR("Could not get disp type info: %08x\n", hres);
266 return NULL;
269 data = heap_alloc(sizeof(dispex_data_t));
270 data->func_cnt = 0;
271 data->funcs = heap_alloc(size*sizeof(func_info_t));
272 list_add_tail(&dispex_data_list, &data->entry);
274 while(*tid) {
275 hres = get_typeinfo(*tid, &ti);
276 if(FAILED(hres))
277 break;
279 i=0;
280 while(1) {
281 hres = ITypeInfo_GetFuncDesc(ti, i++, &funcdesc);
282 if(FAILED(hres))
283 break;
285 add_func_info(data, &size, *tid, funcdesc->memid, dti);
286 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
289 ITypeInfo_Release(ti);
290 tid++;
293 if(!data->func_cnt) {
294 heap_free(data->funcs);
295 data->funcs = NULL;
296 }else if(data->func_cnt != size) {
297 data->funcs = heap_realloc(data->funcs, data->func_cnt * sizeof(func_info_t));
300 qsort(data->funcs, data->func_cnt, sizeof(func_info_t), dispid_cmp);
302 if(data->funcs) {
303 data->name_table = heap_alloc(data->func_cnt * sizeof(func_info_t*));
304 for(i=0; i < data->func_cnt; i++)
305 data->name_table[i] = data->funcs+i;
306 qsort(data->name_table, data->func_cnt, sizeof(func_info_t*), func_name_cmp);
307 }else {
308 data->name_table = NULL;
311 ITypeInfo_Release(dti);
312 return data;
315 static dispex_data_t *get_dispex_data(DispatchEx *This)
317 if(This->data->data)
318 return This->data->data;
320 EnterCriticalSection(&cs_dispex_static_data);
322 if(!This->data->data)
323 This->data->data = preprocess_dispex_data(This);
325 LeaveCriticalSection(&cs_dispex_static_data);
327 return This->data->data;
330 static inline DispatchEx *impl_from_IDispatchEx(IDispatchEx *iface)
332 return CONTAINING_RECORD(iface, DispatchEx, IDispatchEx_iface);
335 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
337 DispatchEx *This = impl_from_IDispatchEx(iface);
339 return IUnknown_QueryInterface(This->outer, riid, ppv);
342 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
344 DispatchEx *This = impl_from_IDispatchEx(iface);
346 return IUnknown_AddRef(This->outer);
349 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
351 DispatchEx *This = impl_from_IDispatchEx(iface);
353 return IUnknown_Release(This->outer);
356 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
358 DispatchEx *This = impl_from_IDispatchEx(iface);
360 TRACE("(%p)->(%p)\n", This, pctinfo);
362 *pctinfo = 1;
363 return S_OK;
366 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
367 LCID lcid, ITypeInfo **ppTInfo)
369 DispatchEx *This = impl_from_IDispatchEx(iface);
371 TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
373 return get_typeinfo(This->data->disp_tid, ppTInfo);
376 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
377 LPOLESTR *rgszNames, UINT cNames,
378 LCID lcid, DISPID *rgDispId)
380 DispatchEx *This = impl_from_IDispatchEx(iface);
381 UINT i;
382 HRESULT hres;
384 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
385 lcid, rgDispId);
387 for(i=0; i < cNames; i++) {
388 hres = IDispatchEx_GetDispID(&This->IDispatchEx_iface, rgszNames[i], 0, rgDispId+i);
389 if(FAILED(hres))
390 return hres;
393 return S_OK;
396 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
397 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
398 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
400 DispatchEx *This = impl_from_IDispatchEx(iface);
402 TRACE("(%p)->(%x %s %x %x %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
403 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
405 return IDispatchEx_InvokeEx(&This->IDispatchEx_iface, dispIdMember, lcid, wFlags,
406 pDispParams, pVarResult, pExcepInfo, NULL);
409 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
411 DispatchEx *This = impl_from_IDispatchEx(iface);
412 dispex_data_t *data;
413 int min, max, n, c;
415 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
417 if(grfdex & ~(fdexNameCaseSensitive|fdexNameEnsure|fdexNameImplicit))
418 FIXME("Unsupported grfdex %x\n", grfdex);
420 data = get_dispex_data(This);
421 if(!data)
422 return E_FAIL;
424 min = 0;
425 max = data->func_cnt-1;
427 while(min <= max) {
428 n = (min+max)/2;
430 c = strcmpiW(data->name_table[n]->name, bstrName);
431 if(!c) {
432 if((grfdex & fdexNameCaseSensitive) && strcmpW(data->name_table[n]->name, bstrName))
433 break;
435 *pid = data->name_table[n]->id;
436 return S_OK;
439 if(c > 0)
440 max = n-1;
441 else
442 min = n+1;
445 if(This->data->vtbl && This->data->vtbl->get_dispid) {
446 HRESULT hres;
448 hres = This->data->vtbl->get_dispid(This->outer, bstrName, grfdex, pid);
449 if(hres != DISP_E_UNKNOWNNAME)
450 return hres;
453 TRACE("not found %s\n", debugstr_w(bstrName));
454 return DISP_E_UNKNOWNNAME;
457 static BOOL is_propputref_id(DISPID id)
459 switch (id)
461 case DISPID_DOM_DOCUMENT_DOCUMENTELEMENT:
462 case DISPID_XMLDOM_DOCUMENT2_SCHEMAS:
463 case DISPID_XMLDOM_SELECTION_CONTEXT:
464 case DISPID_XMLDOM_TEMPLATE_STYLESHEET:
465 case DISPID_SAX_CONTENTHANDLER_DOCUMENTLOCATOR:
466 case DISPID_SAX_XMLFILTER_PARENT:
467 case DISPID_SAX_XMLREADER_ENTITYRESOLVER:
468 case DISPID_SAX_XMLREADER_CONTENTHANDLER:
469 case DISPID_SAX_XMLREADER_DTDHANDLER:
470 case DISPID_SAX_XMLREADER_ERRORHANDLER:
471 case DISPID_MXXML_FILTER_ENTITYRESOLVER:
472 case DISPID_MXXML_FILTER_CONTENTHANDLER:
473 case DISPID_MXXML_FILTER_DTDHANDLER:
474 case DISPID_MXXML_FILTER_ERRORHANDLER:
475 return TRUE;
476 default:
477 return FALSE;
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);
485 IUnknown *unk;
486 ITypeInfo *ti;
487 dispex_data_t *data;
488 UINT argerr=0;
489 int min, max, n;
490 HRESULT hres;
492 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
494 if(This->data->vtbl && This->data->vtbl->invoke) {
495 hres = This->data->vtbl->invoke(This->outer, id, lcid, wFlags, pdp, pvarRes, pei);
496 if (hres != DISP_E_UNKNOWNNAME) return hres;
499 if(wFlags == DISPATCH_CONSTRUCT) {
500 FIXME("DISPATCH_CONSTRUCT not implemented\n");
501 return E_NOTIMPL;
504 data = get_dispex_data(This);
505 if(!data)
506 return E_FAIL;
508 min = 0;
509 max = data->func_cnt-1;
511 while(min <= max) {
512 n = (min+max)/2;
514 if(data->funcs[n].id == id)
515 break;
517 if(data->funcs[n].id < id)
518 min = n+1;
519 else
520 max = n-1;
523 if(min > max) {
524 WARN("invalid id %x\n", id);
525 return DISP_E_UNKNOWNNAME;
528 hres = get_typeinfo(data->funcs[n].tid, &ti);
529 if(FAILED(hres)) {
530 ERR("Could not get type info: %08x\n", hres);
531 return hres;
534 hres = IUnknown_QueryInterface(This->outer, get_riid_from_tid(data->funcs[n].tid), (void**)&unk);
535 if(FAILED(hres)) {
536 ERR("Could not get iface: %08x\n", hres);
537 ITypeInfo_Release(ti);
538 return E_FAIL;
541 if (is_propputref_id(id) && wFlags == DISPATCH_PROPERTYPUT)
542 wFlags = DISPATCH_PROPERTYPUTREF;
543 hres = ITypeInfo_Invoke(ti, unk, id, wFlags, pdp, pvarRes, pei, &argerr);
545 ITypeInfo_Release(ti);
546 IUnknown_Release(unk);
547 return hres;
550 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
552 DispatchEx *This = impl_from_IDispatchEx(iface);
553 TRACE("Not implemented in native msxml3 (%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
554 return E_NOTIMPL;
557 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
559 DispatchEx *This = impl_from_IDispatchEx(iface);
560 TRACE("Not implemented in native msxml3 (%p)->(%x)\n", This, id);
561 return E_NOTIMPL;
564 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
566 DispatchEx *This = impl_from_IDispatchEx(iface);
567 TRACE("Not implemented in native msxml3 (%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
568 return E_NOTIMPL;
571 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
573 DispatchEx *This = impl_from_IDispatchEx(iface);
574 TRACE("Not implemented in native msxml3 (%p)->(%x %p)\n", This, id, pbstrName);
575 return E_NOTIMPL;
578 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
580 DispatchEx *This = impl_from_IDispatchEx(iface);
581 TRACE(" Not implemented in native msxml3 (%p)->(%x %x %p)\n", This, grfdex, id, pid);
582 return E_NOTIMPL;
585 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
587 DispatchEx *This = impl_from_IDispatchEx(iface);
588 TRACE("Not implemented in native msxml3 (%p)->(%p)\n", This, ppunk);
589 return E_NOTIMPL;
592 static IDispatchExVtbl DispatchExVtbl = {
593 DispatchEx_QueryInterface,
594 DispatchEx_AddRef,
595 DispatchEx_Release,
596 DispatchEx_GetTypeInfoCount,
597 DispatchEx_GetTypeInfo,
598 DispatchEx_GetIDsOfNames,
599 DispatchEx_Invoke,
600 DispatchEx_GetDispID,
601 DispatchEx_InvokeEx,
602 DispatchEx_DeleteMemberByName,
603 DispatchEx_DeleteMemberByDispID,
604 DispatchEx_GetMemberProperties,
605 DispatchEx_GetMemberName,
606 DispatchEx_GetNextDispID,
607 DispatchEx_GetNameSpaceParent
610 BOOL dispex_query_interface(DispatchEx *This, REFIID riid, void **ppv)
612 static const IID IID_UndocumentedScriptIface =
613 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa0}};
614 static const IID IID_IDispatchJS =
615 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa6}};
617 if(IsEqualGUID(&IID_IDispatch, riid)) {
618 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
619 *ppv = &This->IDispatchEx_iface;
620 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
621 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
622 *ppv = &This->IDispatchEx_iface;
623 }else if(IsEqualGUID(&IID_IDispatchJS, riid)) {
624 TRACE("(%p)->(IID_IDispatchJS %p) returning NULL\n", This, ppv);
625 *ppv = NULL;
626 }else if(IsEqualGUID(&IID_UndocumentedScriptIface, riid)) {
627 TRACE("(%p)->(IID_UndocumentedScriptIface %p) returning NULL\n", This, ppv);
628 *ppv = NULL;
629 }else if (IsEqualGUID(&IID_IObjectIdentity, riid)) {
630 TRACE("IID_IObjectIdentity not supported returning NULL\n");
631 *ppv = NULL;
632 }else {
633 return FALSE;
636 if(*ppv)
637 IUnknown_AddRef((IUnknown*)*ppv);
638 return TRUE;
641 void init_dispex(DispatchEx *dispex, IUnknown *outer, dispex_static_data_t *data)
643 dispex->IDispatchEx_iface.lpVtbl = &DispatchExVtbl;
644 dispex->outer = outer;
645 dispex->data = data;