windows.networking.hostname/tests: Add IHostNameFactory::CreateHostName() tests.
[wine.git] / dlls / msxml3 / dispex.c
blobcd7c2820133f5cad220770ae3eb80b5bbf7bd2a5
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 <stdarg.h>
23 #include "msxml2.h"
24 #include "msxml2did.h"
25 #include "dispex.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 };
43 enum lib_version_t
45 LibXml = 0,
46 LibXml2,
47 LibXml_Last
50 typedef struct {
51 REFIID iid;
52 enum lib_version_t lib;
53 } tid_id_t;
55 typedef struct {
56 REFIID iid;
57 unsigned short major;
58 } lib_id_t;
60 typedef struct {
61 DISPID id;
62 BSTR name;
63 enum tid_t tid;
64 } func_info_t;
66 struct dispex_data_t {
67 DWORD func_cnt;
68 func_info_t *funcs;
69 func_info_t **name_table;
71 struct list entry;
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[] = {
80 { &LIBID_MSXML, 2 },
81 { &LIBID_MSXML2, 3 }
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)
140 HRESULT hres;
142 if(!typelib[lib]) {
143 hres = LoadRegTypeLib(lib_ids[lib].iid, lib_ids[lib].major, 0, LOCALE_SYSTEM_DEFAULT, tl);
144 if(FAILED(hres)) {
145 ERR("LoadRegTypeLib failed, hr %#lx.\n", hres);
146 return hres;
149 if (InterlockedCompareExchangePointer((void**)&typelib[lib], *tl, NULL))
150 ITypeLib_Release(*tl);
153 *tl = typelib[lib];
154 return S_OK;
157 HRESULT get_typeinfo(enum tid_t tid, ITypeInfo **typeinfo)
159 unsigned lib = get_libid_from_tid(tid);
160 ITypeLib *typelib;
161 HRESULT hres;
163 if (FAILED(hres = get_typelib(lib, &typelib)))
164 return hres;
166 if(!typeinfos[tid]) {
167 ITypeInfo *ti;
169 hres = ITypeLib_GetTypeInfoOfGuid(typelib, get_riid_from_tid(tid), &ti);
170 if(FAILED(hres)) {
171 /* try harder with typelib from msxml.dll */
172 if (FAILED(hres = get_typelib(LibXml, &typelib)))
173 return hres;
174 hres = ITypeLib_GetTypeInfoOfGuid(typelib, get_riid_from_tid(tid), &ti);
175 if(FAILED(hres)) {
176 ERR("GetTypeInfoOfGuid failed, hr %#lx.\n", hres);
177 return hres;
181 if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), ti, NULL))
182 ITypeInfo_Release(ti);
185 *typeinfo = typeinfos[tid];
187 ITypeInfo_AddRef(typeinfos[tid]);
188 return S_OK;
191 void release_typelib(void)
193 dispex_data_t *iter;
194 unsigned i;
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);
205 heap_free(iter);
208 for(i=0; i < ARRAY_SIZE(typeinfos); i++)
209 if(typeinfos[i])
210 ITypeInfo_Release(typeinfos[i]);
212 for(i=0; i < ARRAY_SIZE(typelib); i++)
213 if(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)
221 HRESULT hres;
223 if(data->func_cnt && data->funcs[data->func_cnt-1].id == id)
224 return;
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);
230 if(FAILED(hres))
231 return;
233 data->funcs[data->func_cnt].id = id;
234 data->funcs[data->func_cnt].tid = tid;
236 data->func_cnt++;
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;
252 FUNCDESC *funcdesc;
253 dispex_data_t *data;
254 DWORD size = 16, i;
255 ITypeInfo *ti, *dti;
256 HRESULT hres;
258 TRACE("(%p)\n", This);
260 hres = get_typeinfo(This->data->disp_tid, &dti);
261 if(FAILED(hres)) {
262 ERR("Could not get disp type info, hr %#lx.\n", hres);
263 return NULL;
266 data = heap_alloc(sizeof(dispex_data_t));
267 data->func_cnt = 0;
268 data->funcs = heap_alloc(size*sizeof(func_info_t));
269 list_add_tail(&dispex_data_list, &data->entry);
271 while(*tid) {
272 hres = get_typeinfo(*tid, &ti);
273 if(FAILED(hres))
274 break;
276 i=0;
277 while(1) {
278 hres = ITypeInfo_GetFuncDesc(ti, i++, &funcdesc);
279 if(FAILED(hres))
280 break;
282 add_func_info(data, &size, *tid, funcdesc->memid, dti);
283 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
286 ITypeInfo_Release(ti);
287 tid++;
290 if(!data->func_cnt) {
291 heap_free(data->funcs);
292 data->funcs = NULL;
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);
299 if(data->funcs) {
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);
304 }else {
305 data->name_table = NULL;
308 ITypeInfo_Release(dti);
309 return data;
312 static dispex_data_t *get_dispex_data(DispatchEx *This)
314 if(This->data->data)
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);
359 *pctinfo = 1;
360 return S_OK;
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)
377 UINT i;
378 HRESULT hres;
380 TRACE("%p, %s, %p, %u, %lx, %p.\n", iface, debugstr_guid(riid), rgszNames, cNames,
381 lcid, rgDispId);
383 for(i=0; i < cNames; i++) {
384 hres = IDispatchEx_GetDispID(iface, rgszNames[i], 0, rgDispId+i);
385 if(FAILED(hres))
386 return hres;
389 return S_OK;
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);
405 dispex_data_t *data;
406 int min, max, n, c;
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);
414 if(!data)
415 return E_FAIL;
417 min = 0;
418 max = data->func_cnt-1;
420 while(min <= max) {
421 n = (min+max)/2;
423 c = lstrcmpiW(data->name_table[n]->name, bstrName);
424 if(!c) {
425 if((grfdex & fdexNameCaseSensitive) && wcscmp(data->name_table[n]->name, bstrName))
426 break;
428 *pid = data->name_table[n]->id;
429 return S_OK;
432 if(c > 0)
433 max = n-1;
434 else
435 min = n+1;
438 if(This->data->vtbl && This->data->vtbl->get_dispid) {
439 HRESULT hres;
441 hres = This->data->vtbl->get_dispid(This->outer, bstrName, grfdex, pid);
442 if(hres != DISP_E_UNKNOWNNAME)
443 return hres;
446 TRACE("not found %s\n", debugstr_w(bstrName));
447 return DISP_E_UNKNOWNNAME;
450 static BOOL is_propputref_id(DISPID id)
452 switch (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:
468 return TRUE;
469 default:
470 return FALSE;
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);
478 IUnknown *unk;
479 ITypeInfo *ti;
480 dispex_data_t *data;
481 UINT argerr=0;
482 int min, max, n;
483 HRESULT hres;
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");
494 return E_NOTIMPL;
497 data = get_dispex_data(This);
498 if(!data)
499 return E_FAIL;
501 min = 0;
502 max = data->func_cnt-1;
504 while(min <= max) {
505 n = (min+max)/2;
507 if(data->funcs[n].id == id)
508 break;
510 if(data->funcs[n].id < id)
511 min = n+1;
512 else
513 max = n-1;
516 if(min > max) {
517 WARN("invalid id %lx.\n", id);
518 return DISP_E_UNKNOWNNAME;
521 hres = get_typeinfo(data->funcs[n].tid, &ti);
522 if(FAILED(hres)) {
523 ERR("Could not get type info, hr %#lx.\n", hres);
524 return hres;
527 hres = IUnknown_QueryInterface(This->outer, get_riid_from_tid(data->funcs[n].tid), (void**)&unk);
528 if(FAILED(hres)) {
529 ERR("Could not get interface, hr %#lx.\n", hres);
530 ITypeInfo_Release(ti);
531 return E_FAIL;
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);
540 return hres;
543 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
545 TRACE("%p, %s, %lx.\n", iface, debugstr_w(bstrName), grfdex);
546 return E_NOTIMPL;
549 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
551 TRACE("%p, %ld.\n", iface, id);
552 return E_NOTIMPL;
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);
558 return E_NOTIMPL;
561 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
563 TRACE("%p, %ld, %p.\n", iface, id, pbstrName);
564 return E_NOTIMPL;
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);
570 return E_NOTIMPL;
573 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
575 TRACE("%p, %p.\n", iface, ppunk);
576 return E_NOTIMPL;
579 static IDispatchExVtbl DispatchExVtbl = {
580 DispatchEx_QueryInterface,
581 DispatchEx_AddRef,
582 DispatchEx_Release,
583 DispatchEx_GetTypeInfoCount,
584 DispatchEx_GetTypeInfo,
585 DispatchEx_GetIDsOfNames,
586 DispatchEx_Invoke,
587 DispatchEx_GetDispID,
588 DispatchEx_InvokeEx,
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);
612 *ppv = NULL;
613 }else if(IsEqualGUID(&IID_UndocumentedScriptIface, riid)) {
614 TRACE("(%p)->(IID_UndocumentedScriptIface %p) returning NULL\n", This, ppv);
615 *ppv = NULL;
616 }else if (IsEqualGUID(&IID_IObjectIdentity, riid)) {
617 TRACE("IID_IObjectIdentity not supported returning NULL\n");
618 *ppv = NULL;
619 }else {
620 return FALSE;
623 if(*ppv)
624 IUnknown_AddRef((IUnknown*)*ppv);
625 return TRUE;
628 void init_dispex(DispatchEx *dispex, IUnknown *outer, dispex_static_data_t *data)
630 dispex->IDispatchEx_iface.lpVtbl = &DispatchExVtbl;
631 dispex->outer = outer;
632 dispex->data = data;