msxml3: Support functions for typelib.
[wine] / dlls / msxml3 / main.c
1 /*
2  *    MSXML Class Factory
3  *
4  * Copyright 2002 Lionel Ulmer
5  * Copyright 2005 Mike McCormack
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23
24 #define COBJMACROS
25
26 #include <stdarg.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winuser.h"
30 #include "ole2.h"
31 #include "msxml.h"
32 #include "msxml2.h"
33
34 #include "wine/debug.h"
35
36 #include "msxml_private.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
39
40
41 static ITypeLib *typelib;
42 static ITypeInfo *typeinfos[LAST_tid];
43
44 static REFIID tid_ids[] = {
45     &IID_IXMLDOMAttribute,
46     &IID_IXMLDOMCDATASection,
47     &IID_IXMLDOMComment,
48     &IID_IXMLDOMDocument2,
49     &IID_IXMLDOMDocumentFragment,
50     &IID_IXMLDOMElement,
51     &IID_IXMLDOMEntityReference,
52     &IID_IXMLDOMNamedNodeMap,
53     &IID_IXMLDOMNodeList,
54     &IID_IXMLDOMParseError,
55     &IID_IXMLDOMProcessingInstruction,
56     &IID_IXMLDOMSchemaCollection,
57     &IID_IXMLDOMText,
58     &IID_IXMLElement,
59     &IID_IXMLDOMDocument
60 };
61
62 HRESULT get_typeinfo(enum tid_t tid, ITypeInfo **typeinfo)
63 {
64     HRESULT hres;
65
66     if(!typelib) {
67         ITypeLib *tl;
68
69         hres = LoadRegTypeLib(&LIBID_MSXML2, 3, 0, LOCALE_SYSTEM_DEFAULT, &tl);
70         if(FAILED(hres)) {
71             ERR("LoadRegTypeLib failed: %08x\n", hres);
72             return hres;
73         }
74
75         if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
76             ITypeLib_Release(tl);
77     }
78
79     if(!typeinfos[tid]) {
80         ITypeInfo *typeinfo;
81
82         hres = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &typeinfo);
83         if(FAILED(hres)) {
84             ERR("GetTypeInfoOfGuid failed: %08x\n", hres);
85             return hres;
86         }
87
88         if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), typeinfo, NULL))
89             ITypeInfo_Release(typeinfo);
90     }
91
92     *typeinfo = typeinfos[tid];
93
94     ITypeInfo_AddRef(typeinfos[tid]);
95     return S_OK;
96 }
97
98 static void process_detach(void)
99 {
100     if(typelib) {
101         unsigned i;
102
103         for(i=0; i < sizeof(typeinfos)/sizeof(*typeinfos); i++)
104             if(typeinfos[i])
105                 ITypeInfo_Release(typeinfos[i]);
106
107         ITypeLib_Release(typelib);
108     }
109 }
110
111 HRESULT WINAPI DllCanUnloadNow(void)
112 {
113     FIXME("\n");
114     return S_FALSE;
115 }
116
117 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
118 {
119     switch(fdwReason)
120     {
121     case DLL_PROCESS_ATTACH:
122 #ifdef HAVE_LIBXML2
123         xmlInitParser();
124 #endif
125         DisableThreadLibraryCalls(hInstDLL);
126         break;
127     case DLL_PROCESS_DETACH:
128 #ifdef HAVE_LIBXML2
129         xmlCleanupParser();
130         process_detach();
131 #endif
132         break;
133     }
134     return TRUE;
135 }