oleaut32: Thousands separator support for VarFormat.
[wine/hacks.git] / dlls / msxml3 / dispex.c
blob4156c78a4a76bcf048e5c8b8107e4f2bf520d546
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 #include <assert.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "winnls.h"
29 #include "ole2.h"
30 #include "msxml2.h"
31 #include "wininet.h"
32 #include "urlmon.h"
33 #include "winreg.h"
34 #include "shlwapi.h"
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)
46 typedef struct {
47 DISPID id;
48 BSTR name;
49 enum tid_t tid;
50 } func_info_t;
52 struct dispex_data_t {
53 DWORD func_cnt;
54 func_info_t *funcs;
55 func_info_t **name_table;
57 struct list entry;
60 typedef struct {
61 VARIANT var;
62 LPWSTR name;
63 } dynamic_prop_t;
65 struct dispex_dynamic_data_t {
66 DWORD buf_size;
67 DWORD prop_cnt;
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,
81 &IID_IXMLDOMComment,
82 &IID_IXMLDOMDocument2,
83 &IID_IXMLDOMDocumentFragment,
84 &IID_IXMLDOMElement,
85 &IID_IXMLDOMEntityReference,
86 &IID_IXMLDOMImplementation,
87 &IID_IXMLDOMNamedNodeMap,
88 &IID_IXMLDOMNode,
89 &IID_IXMLDOMNodeList,
90 &IID_IXMLDOMParseError,
91 &IID_IXMLDOMProcessingInstruction,
92 &IID_IXMLDOMSchemaCollection,
93 &IID_IXMLDOMText,
94 &IID_IXMLElement,
95 &IID_IXMLDOMDocument,
96 &IID_IVBSAXAttributes,
97 &IID_IVBSAXContentHandler,
98 &IID_IVBSAXDeclHandler,
99 &IID_IVBSAXDTDHandler,
100 &IID_IVBSAXEntityResolver,
101 &IID_IVBSAXErrorHandler,
102 &IID_IVBSAXLexicalHandler,
103 &IID_IVBSAXLocator,
104 &IID_IVBSAXXMLFilter,
105 &IID_IVBSAXXMLReader,
106 &IID_IMXAttributes,
107 &IID_IMXReaderControl,
108 &IID_IMXWriter,
111 HRESULT get_typeinfo(enum tid_t tid, ITypeInfo **typeinfo)
113 HRESULT hres;
115 if(!typelib) {
116 ITypeLib *tl;
118 hres = LoadRegTypeLib(&LIBID_MSXML2, 3, 0, LOCALE_SYSTEM_DEFAULT, &tl);
119 if(FAILED(hres)) {
120 ERR("LoadRegTypeLib failed: %08x\n", hres);
121 return hres;
124 if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
125 ITypeLib_Release(tl);
128 if(!typeinfos[tid]) {
129 ITypeInfo *typeinfo;
131 hres = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &typeinfo);
132 if(FAILED(hres)) {
133 ERR("GetTypeInfoOfGuid failed: %08x\n", hres);
134 return hres;
137 if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), typeinfo, NULL))
138 ITypeInfo_Release(typeinfo);
141 *typeinfo = typeinfos[tid];
143 ITypeInfo_AddRef(typeinfos[tid]);
144 return S_OK;
147 void release_typelib(void)
149 dispex_data_t *iter;
150 unsigned i;
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);
161 heap_free(iter);
164 if(!typelib)
165 return;
167 for(i=0; i < sizeof(typeinfos)/sizeof(*typeinfos); i++)
168 if(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)
176 HRESULT hres;
178 if(data->func_cnt && data->funcs[data->func_cnt-1].id == id)
179 return;
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);
185 if(FAILED(hres))
186 return;
188 data->funcs[data->func_cnt].id = id;
189 data->funcs[data->func_cnt].tid = tid;
191 data->func_cnt++;
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;
207 FUNCDESC *funcdesc;
208 dispex_data_t *data;
209 DWORD size = 16, i;
210 ITypeInfo *ti, *dti;
211 HRESULT hres;
213 TRACE("(%p)\n", This);
215 hres = get_typeinfo(This->data->disp_tid, &dti);
216 if(FAILED(hres)) {
217 ERR("Could not get disp type info: %08x\n", hres);
218 return NULL;
221 data = heap_alloc(sizeof(dispex_data_t));
222 data->func_cnt = 0;
223 data->funcs = heap_alloc(size*sizeof(func_info_t));
224 list_add_tail(&dispex_data_list, &data->entry);
226 while(*tid) {
227 hres = get_typeinfo(*tid, &ti);
228 if(FAILED(hres))
229 break;
231 i=0;
232 while(1) {
233 hres = ITypeInfo_GetFuncDesc(ti, i++, &funcdesc);
234 if(FAILED(hres))
235 break;
237 add_func_info(data, &size, *tid, funcdesc->memid, dti);
238 ITypeInfo_ReleaseFuncDesc(ti, funcdesc);
241 ITypeInfo_Release(ti);
242 tid++;
245 if(!data->func_cnt) {
246 heap_free(data->funcs);
247 data->funcs = NULL;
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);
254 if(data->funcs) {
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);
259 }else {
260 data->name_table = NULL;
263 ITypeInfo_Release(dti);
264 return data;
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)
279 if(This->data->data)
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);
334 *pctinfo = 1;
335 return S_OK;
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);
353 UINT i;
354 HRESULT hres;
356 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
357 lcid, rgDispId);
359 for(i=0; i < cNames; i++) {
360 hres = IDispatchEx_GetDispID(DISPATCHEX(This), rgszNames[i], 0, rgDispId+i);
361 if(FAILED(hres))
362 return hres;
365 return S_OK;
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);
384 dispex_data_t *data;
385 int min, max, n, c;
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);
393 if(!data)
394 return E_FAIL;
396 min = 0;
397 max = data->func_cnt-1;
399 while(min <= max) {
400 n = (min+max)/2;
402 c = strcmpiW(data->name_table[n]->name, bstrName);
403 if(!c) {
404 if((grfdex & fdexNameCaseSensitive) && strcmpW(data->name_table[n]->name, bstrName))
405 break;
407 *pid = data->name_table[n]->id;
408 return S_OK;
411 if(c > 0)
412 max = n-1;
413 else
414 min = n+1;
417 if(This->dynamic_data) {
418 unsigned i;
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;
423 return S_OK;
428 if(This->data->vtbl && This->data->vtbl->get_dispid) {
429 HRESULT hres;
431 hres = This->data->vtbl->get_dispid(This->outer, bstrName, grfdex, pid);
432 if(hres != DISP_E_UNKNOWNNAME)
433 return hres;
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++;
450 return S_OK;
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);
461 IUnknown *unk;
462 ITypeInfo *ti;
463 dispex_data_t *data;
464 UINT argerr=0;
465 int min, max, n;
466 HRESULT hres;
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");
475 return E_NOTIMPL;
478 if(is_dynamic_dispid(id)) {
479 DWORD idx = id - DISPID_DYNPROP_0;
480 VARIANT *var;
482 if(!This->dynamic_data || This->dynamic_data->prop_cnt <= idx)
483 return DISP_E_UNKNOWNNAME;
485 var = &This->dynamic_data->props[idx].var;
487 switch(wFlags) {
488 case INVOKE_PROPERTYGET:
489 return VariantCopy(pvarRes, var);
490 case INVOKE_PROPERTYPUT:
491 VariantClear(var);
492 return VariantCopy(var, pdp->rgvarg);
493 default:
494 FIXME("unhandled wFlags %x\n", wFlags);
495 return E_NOTIMPL;
499 data = get_dispex_data(This);
500 if(!data)
501 return E_FAIL;
503 min = 0;
504 max = data->func_cnt-1;
506 while(min <= max) {
507 n = (min+max)/2;
509 if(data->funcs[n].id == id)
510 break;
512 if(data->funcs[n].id < id)
513 min = n+1;
514 else
515 max = n-1;
518 if(min > max) {
519 WARN("invalid id %x\n", id);
520 return DISP_E_UNKNOWNNAME;
523 hres = get_typeinfo(data->funcs[n].tid, &ti);
524 if(FAILED(hres)) {
525 ERR("Could not get type info: %08x\n", hres);
526 return hres;
529 hres = IUnknown_QueryInterface(This->outer, tid_ids[data->funcs[n].tid], (void**)&unk);
530 if(FAILED(hres)) {
531 ERR("Could not get iface: %08x\n", hres);
532 return E_FAIL;
535 hres = ITypeInfo_Invoke(ti, unk, id, wFlags, pdp, pvarRes, pei, &argerr);
537 ITypeInfo_Release(ti);
538 IUnknown_Release(unk);
539 return hres;
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);
546 return S_OK;
549 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
551 DispatchEx *This = impl_from_IDispatchEx(iface);
552 FIXME("(%p)->(%x)\n", This, id);
553 return E_NOTIMPL;
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);
560 return E_NOTIMPL;
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);
567 return E_NOTIMPL;
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);
574 return E_NOTIMPL;
577 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
579 DispatchEx *This = impl_from_IDispatchEx(iface);
580 FIXME("(%p)->(%p)\n", This, ppunk);
581 return E_NOTIMPL;
584 static IDispatchExVtbl DispatchExVtbl = {
585 DispatchEx_QueryInterface,
586 DispatchEx_AddRef,
587 DispatchEx_Release,
588 DispatchEx_GetTypeInfoCount,
589 DispatchEx_GetTypeInfo,
590 DispatchEx_GetIDsOfNames,
591 DispatchEx_Invoke,
592 DispatchEx_GetDispID,
593 DispatchEx_InvokeEx,
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);
615 *ppv = NULL;
616 }else {
617 return FALSE;
620 if(*ppv)
621 IUnknown_AddRef((IUnknown*)*ppv);
622 return TRUE;
625 void init_dispex(DispatchEx *dispex, IUnknown *outer, dispex_static_data_t *data)
627 dispex->lpIDispatchExVtbl = &DispatchExVtbl;
628 dispex->outer = outer;
629 dispex->data = data;