msxml3: Store the DLL instance handle.
[wine/multimedia.git] / dlls / msxml3 / main.c
blob8e612b758d2a40e125deb42431d8fa40bd36a84d
1 /*
2 * MSXML Class Factory
4 * Copyright 2002 Lionel Ulmer
5 * Copyright 2005 Mike McCormack
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.
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.
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
22 #include "config.h"
23 #include "wine/port.h"
25 #define COBJMACROS
27 #include <stdarg.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winuser.h"
31 #include "ole2.h"
32 #include "msxml.h"
33 #include "msxml6.h"
35 #include "wine/unicode.h"
36 #include "wine/debug.h"
37 #include "wine/library.h"
39 #include "msxml_private.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
43 HINSTANCE MSXML_hInstance = NULL;
45 #ifdef HAVE_LIBXML2
47 void wineXmlCallbackLog(char const* caller, xmlErrorLevel lvl, char const* msg, va_list ap)
49 char* buf = NULL;
50 int len = 32, needed;
51 enum __wine_debug_class dbcl = __WINE_DBCL_ERR;
52 switch (lvl)
54 case XML_ERR_NONE:
55 dbcl = __WINE_DBCL_TRACE;
56 break;
57 case XML_ERR_WARNING:
58 dbcl = __WINE_DBCL_WARN;
59 break;
60 default:
61 break;
64 if (ap)
68 heap_free(buf);
69 buf = heap_alloc(len);
70 needed = vsnprintf(buf, len, msg, ap);
71 if (needed == -1)
72 len *= 2;
73 else if (needed >= len)
74 len = needed + 1;
75 else
76 needed = 0;
78 while (needed);
80 wine_dbg_log(dbcl, &__wine_dbch_msxml, caller, buf);
81 heap_free(buf);
83 else
85 wine_dbg_log(dbcl, &__wine_dbch_msxml, caller, msg);
89 /* Support for loading xml files from a Wine Windows drive */
90 static int wineXmlMatchCallback (char const * filename)
92 int nRet = 0;
94 TRACE("%s\n", filename);
97 * We will deal with loading XML files from the file system
98 * We only care about files that linux cannot find.
99 * e.g. C:,D: etc
101 if(isalpha(filename[0]) && filename[1] == ':')
102 nRet = 1;
104 return nRet;
107 static void *wineXmlOpenCallback (char const * filename)
109 BSTR sFilename = bstr_from_xmlChar( (const xmlChar*)filename);
110 HANDLE hFile;
112 TRACE("%s\n", debugstr_w(sFilename));
114 hFile = CreateFileW(sFilename, GENERIC_READ,FILE_SHARE_READ, NULL,
115 OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, NULL);
116 if(hFile == INVALID_HANDLE_VALUE) hFile = 0;
117 SysFreeString(sFilename);
118 return hFile;
121 static int wineXmlReadCallback(void * context, char * buffer, int len)
123 DWORD dwBytesRead;
125 TRACE("%p %s %d\n", context, buffer, len);
127 if ((context == NULL) || (buffer == NULL))
128 return(-1);
130 if(!ReadFile( context, buffer,len, &dwBytesRead, NULL))
132 ERR("Failed to read file\n");
133 return -1;
136 TRACE("Read %d\n", dwBytesRead);
138 return dwBytesRead;
141 static int wineXmlFileCloseCallback (void * context)
143 return CloseHandle(context) ? 0 : -1;
146 #endif
149 HRESULT WINAPI DllCanUnloadNow(void)
151 return S_FALSE;
155 void* libxslt_handle = NULL;
156 #ifdef SONAME_LIBXSLT
157 # define DECL_FUNCPTR(f) typeof(f) * p##f = NULL
158 DECL_FUNCPTR(xsltApplyStylesheet);
159 DECL_FUNCPTR(xsltCleanupGlobals);
160 DECL_FUNCPTR(xsltFreeStylesheet);
161 DECL_FUNCPTR(xsltParseStylesheetDoc);
162 # undef DECL_FUNCPTR
163 #endif
165 static void init_libxslt(void)
167 #ifdef SONAME_LIBXSLT
168 void (*pxsltInit)(void); /* Missing in libxslt <= 1.1.14 */
170 libxslt_handle = wine_dlopen(SONAME_LIBXSLT, RTLD_NOW, NULL, 0);
171 if (!libxslt_handle)
172 return;
174 #define LOAD_FUNCPTR(f, needed) if ((p##f = wine_dlsym(libxslt_handle, #f, NULL, 0)) == NULL && needed) { WARN("Can't find symbol %s\n", #f); goto sym_not_found; }
175 LOAD_FUNCPTR(xsltInit, 0);
176 LOAD_FUNCPTR(xsltApplyStylesheet, 1);
177 LOAD_FUNCPTR(xsltCleanupGlobals, 1);
178 LOAD_FUNCPTR(xsltFreeStylesheet, 1);
179 LOAD_FUNCPTR(xsltParseStylesheetDoc, 1);
180 #undef LOAD_FUNCPTR
182 if (pxsltInit)
183 pxsltInit();
184 return;
186 sym_not_found:
187 wine_dlclose(libxslt_handle, NULL, 0);
188 libxslt_handle = NULL;
189 #endif
193 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
195 MSXML_hInstance = hInstDLL;
197 switch(fdwReason)
199 case DLL_PROCESS_ATTACH:
200 #ifdef HAVE_LIBXML2
201 xmlInitParser();
203 /* Set the default indent character to a single tab,
204 for this thread and as default for new threads */
205 xmlTreeIndentString = "\t";
206 xmlThrDefTreeIndentString("\t");
208 /* Register callbacks for loading XML files */
209 if(xmlRegisterInputCallbacks(wineXmlMatchCallback, wineXmlOpenCallback,
210 wineXmlReadCallback, wineXmlFileCloseCallback) == -1)
211 WARN("Failed to register callbacks\n");
213 #endif
214 init_libxslt();
215 DisableThreadLibraryCalls(hInstDLL);
216 break;
217 case DLL_PROCESS_DETACH:
218 #ifdef SONAME_LIBXSLT
219 if (libxslt_handle)
221 pxsltCleanupGlobals();
222 wine_dlclose(libxslt_handle, NULL, 0);
223 libxslt_handle = NULL;
225 #endif
226 #ifdef HAVE_LIBXML2
227 /* Restore default Callbacks */
228 xmlCleanupInputCallbacks();
229 xmlRegisterDefaultInputCallbacks();
231 xmlCleanupParser();
232 #endif
233 release_typelib();
234 break;
236 return TRUE;