msxml3: Improved checking of failed case.
[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 #ifdef HAVE_LIBXSLT
39 #include <libxslt/xslt.h>
40 #endif
41
42 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
43
44 static HINSTANCE hInstance;
45 static ITypeLib *typelib;
46 static ITypeInfo *typeinfos[LAST_tid];
47
48 static REFIID tid_ids[] = {
49     &IID_IXMLDOMAttribute,
50     &IID_IXMLDOMCDATASection,
51     &IID_IXMLDOMComment,
52     &IID_IXMLDOMDocument2,
53     &IID_IXMLDOMDocumentFragment,
54     &IID_IXMLDOMElement,
55     &IID_IXMLDOMEntityReference,
56     &IID_IXMLDOMImplementation,
57     &IID_IXMLDOMNamedNodeMap,
58     &IID_IXMLDOMNode,
59     &IID_IXMLDOMNodeList,
60     &IID_IXMLDOMParseError,
61     &IID_IXMLDOMProcessingInstruction,
62     &IID_IXMLDOMSchemaCollection,
63     &IID_IXMLDOMText,
64     &IID_IXMLElement,
65     &IID_IXMLDOMDocument,
66     &IID_IVBSAXAttributes,
67     &IID_IVBSAXContentHandler,
68     &IID_IVBSAXDeclHandler,
69     &IID_IVBSAXDTDHandler,
70     &IID_IVBSAXEntityResolver,
71     &IID_IVBSAXErrorHandler,
72     &IID_IVBSAXLexicalHandler,
73     &IID_IVBSAXLocator,
74     &IID_IVBSAXXMLFilter,
75     &IID_IVBSAXXMLReader,
76     &IID_IMXAttributes,
77     &IID_IMXReaderControl,
78     &IID_IMXWriter,
79 };
80
81 HRESULT get_typeinfo(enum tid_t tid, ITypeInfo **typeinfo)
82 {
83     HRESULT hres;
84
85     if(!typelib) {
86         ITypeLib *tl;
87
88         hres = LoadRegTypeLib(&LIBID_MSXML2, 3, 0, LOCALE_SYSTEM_DEFAULT, &tl);
89         if(FAILED(hres)) {
90             ERR("LoadRegTypeLib failed: %08x\n", hres);
91             return hres;
92         }
93
94         if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
95             ITypeLib_Release(tl);
96     }
97
98     if(!typeinfos[tid]) {
99         ITypeInfo *typeinfo;
100
101         hres = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &typeinfo);
102         if(FAILED(hres)) {
103             ERR("GetTypeInfoOfGuid failed: %08x\n", hres);
104             return hres;
105         }
106
107         if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), typeinfo, NULL))
108             ITypeInfo_Release(typeinfo);
109     }
110
111     *typeinfo = typeinfos[tid];
112
113     ITypeInfo_AddRef(typeinfos[tid]);
114     return S_OK;
115 }
116
117 static CRITICAL_SECTION MSXML3_typelib_cs;
118 static CRITICAL_SECTION_DEBUG MSXML3_typelib_cs_debug =
119 {
120     0, 0, &MSXML3_typelib_cs,
121     { &MSXML3_typelib_cs_debug.ProcessLocksList,
122       &MSXML3_typelib_cs_debug.ProcessLocksList },
123       0, 0, { (DWORD_PTR)(__FILE__ ": MSXML3_typelib_cs") }
124 };
125 static CRITICAL_SECTION MSXML3_typelib_cs = { &MSXML3_typelib_cs_debug, -1, 0, 0, 0, 0 };
126
127 ITypeLib *get_msxml3_typelib( LPWSTR *path )
128 {
129     static WCHAR msxml3_path[MAX_PATH];
130
131     EnterCriticalSection( &MSXML3_typelib_cs );
132
133     if (!typelib)
134     {
135         TRACE("loading typelib\n");
136
137         if (GetModuleFileNameW( hInstance, msxml3_path, MAX_PATH ))
138             LoadTypeLib( msxml3_path, &typelib );
139     }
140
141     LeaveCriticalSection( &MSXML3_typelib_cs );
142
143     if (path)
144         *path = msxml3_path;
145
146     if (typelib)
147         ITypeLib_AddRef( typelib );
148
149     return typelib;
150 }
151
152 static void process_detach(void)
153 {
154     if(typelib) {
155         unsigned i;
156
157         for(i=0; i < sizeof(typeinfos)/sizeof(*typeinfos); i++)
158             if(typeinfos[i])
159                 ITypeInfo_Release(typeinfos[i]);
160
161         ITypeLib_Release(typelib);
162     }
163 }
164
165 HRESULT WINAPI DllCanUnloadNow(void)
166 {
167     FIXME("\n");
168     return S_FALSE;
169 }
170
171 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
172 {
173     switch(fdwReason)
174     {
175     case DLL_PROCESS_ATTACH:
176 #ifdef HAVE_LIBXML2
177         xmlInitParser();
178 #endif
179 #ifdef HAVE_XSLTINIT
180         xsltInit();
181 #endif
182         hInstance = hInstDLL;
183         DisableThreadLibraryCalls(hInstDLL);
184         break;
185     case DLL_PROCESS_DETACH:
186 #ifdef HAVE_LIBXSLT
187         xsltCleanupGlobals();
188 #endif
189 #ifdef HAVE_LIBXML2
190         xmlCleanupParser();
191         process_detach();
192 #endif
193         break;
194     }
195     return TRUE;
196 }