msxml3: Register msxml3 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 static HINSTANCE hInstance;
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_IXMLDOMImplementation,
53     &IID_IXMLDOMNamedNodeMap,
54     &IID_IXMLDOMNodeList,
55     &IID_IXMLDOMParseError,
56     &IID_IXMLDOMProcessingInstruction,
57     &IID_IXMLDOMSchemaCollection,
58     &IID_IXMLDOMText,
59     &IID_IXMLElement,
60     &IID_IXMLDOMDocument
61 };
62
63 HRESULT get_typeinfo(enum tid_t tid, ITypeInfo **typeinfo)
64 {
65     HRESULT hres;
66
67     if(!typelib) {
68         ITypeLib *tl;
69
70         hres = LoadRegTypeLib(&LIBID_MSXML2, 3, 0, LOCALE_SYSTEM_DEFAULT, &tl);
71         if(FAILED(hres)) {
72             ERR("LoadRegTypeLib failed: %08x\n", hres);
73             return hres;
74         }
75
76         if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
77             ITypeLib_Release(tl);
78     }
79
80     if(!typeinfos[tid]) {
81         ITypeInfo *typeinfo;
82
83         hres = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &typeinfo);
84         if(FAILED(hres)) {
85             ERR("GetTypeInfoOfGuid failed: %08x\n", hres);
86             return hres;
87         }
88
89         if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), typeinfo, NULL))
90             ITypeInfo_Release(typeinfo);
91     }
92
93     *typeinfo = typeinfos[tid];
94
95     ITypeInfo_AddRef(typeinfos[tid]);
96     return S_OK;
97 }
98
99 static CRITICAL_SECTION MSXML3_typelib_cs;
100 static CRITICAL_SECTION_DEBUG MSXML3_typelib_cs_debug =
101 {
102     0, 0, &MSXML3_typelib_cs,
103     { &MSXML3_typelib_cs_debug.ProcessLocksList,
104       &MSXML3_typelib_cs_debug.ProcessLocksList },
105       0, 0, { (DWORD_PTR)(__FILE__ ": MSXML3_typelib_cs") }
106 };
107 static CRITICAL_SECTION MSXML3_typelib_cs = { &MSXML3_typelib_cs_debug, -1, 0, 0, 0, 0 };
108
109 ITypeLib *get_msxml3_typelib( LPWSTR *path )
110 {
111     static WCHAR msxml3_path[MAX_PATH];
112
113     EnterCriticalSection( &MSXML3_typelib_cs );
114
115     if (!typelib)
116     {
117         TRACE("loading typelib\n");
118
119         if (GetModuleFileNameW( hInstance, msxml3_path, MAX_PATH ))
120             LoadTypeLib( msxml3_path, &typelib );
121     }
122
123     LeaveCriticalSection( &MSXML3_typelib_cs );
124
125     if (path)
126         *path = msxml3_path;
127
128     if (typelib)
129         ITypeLib_AddRef( typelib );
130
131     return typelib;
132 }
133
134 static void process_detach(void)
135 {
136     if(typelib) {
137         unsigned i;
138
139         for(i=0; i < sizeof(typeinfos)/sizeof(*typeinfos); i++)
140             if(typeinfos[i])
141                 ITypeInfo_Release(typeinfos[i]);
142
143         ITypeLib_Release(typelib);
144     }
145 }
146
147 HRESULT WINAPI DllCanUnloadNow(void)
148 {
149     FIXME("\n");
150     return S_FALSE;
151 }
152
153 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
154 {
155     switch(fdwReason)
156     {
157     case DLL_PROCESS_ATTACH:
158 #ifdef HAVE_LIBXML2
159         xmlInitParser();
160 #endif
161         hInstance = hInstDLL;
162         DisableThreadLibraryCalls(hInstDLL);
163         break;
164     case DLL_PROCESS_DETACH:
165 #ifdef HAVE_LIBXML2
166         xmlCleanupParser();
167         process_detach();
168 #endif
169         break;
170     }
171     return TRUE;
172 }