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