license headers in libnetxms changed to LGPL
[public/netxms.git] / src / libnetxms / dload.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Utility Library
4 ** Copyright (C) 2003-2010 Victor Kirhenshtein
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU Lesser General Public License as published
8 ** by the Free Software Foundation; either version 3 of the License, or
9 ** (at your option) any later version.
10 **
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
15 **
16 ** You should have received a copy of the GNU Lesser General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 **
20 ** File: dload.cpp
21 **
22 **/
23
24 #include "libnetxms.h"
25
26 #if defined(_NETWARE)
27 #undef SEVERITY_CRITICAL
28 #include <netware.h>
29 #elif !defined(_WIN32)
30 #include <dlfcn.h>
31 #ifndef RTLD_GLOBAL
32 #define RTLD_GLOBAL 0
33 #endif
34 #endif
35
36
37 //
38 // Error texts for NetWare LoadModule()
39 //
40
41 #ifdef _NETWARE
42
43 static char *m_pszErrorText[] =
44 {
45 "No error",
46 "File not found",
47 "Error reading file",
48 "File not in NLM format",
49 "Wrong NLM file version",
50 "Reentrant initialization failure",
51 "Cannot load multiple copies",
52 "Operation already in progress",
53 "Not enough memory",
54 "Initialization failure",
55 "Inconsistent file format",
56 "Cannot load module at startup",
57 "Unresolved dependencies",
58 "Unresolved external symbols",
59 "Public symbol already defined",
60 "XDC data error",
61 "Module must be loaded in the kernel",
62 "Module is NIOS compatible only and cannot be loaded on NetWare",
63 "Cannot create specified address space",
64 "Module initialization failed"
65 };
66
67 #endif
68
69
70 //
71 // Load DLL/shared library
72 //
73
74 HMODULE LIBNETXMS_EXPORTABLE DLOpen(const TCHAR *pszLibName, TCHAR *pszErrorText)
75 {
76 HMODULE hModule;
77
78 #if defined(_WIN32)
79 hModule = LoadLibrary(pszLibName);
80 if ((hModule == NULL) && (pszErrorText != NULL))
81 GetSystemErrorText(GetLastError(), pszErrorText, 255);
82 #elif defined(_NETWARE)
83 TCHAR szBuffer[MAX_PATH + 4];
84 int nError;
85
86 nx_strncpy(&szBuffer[4], pszLibName, MAX_PATH);
87 nError = LoadModule(getnetwarelogger(), &szBuffer[4], LO_RETURN_HANDLE);
88 if (nError == 0)
89 {
90 hModule = *((HMODULE *)szBuffer);
91 if (pszErrorText != NULL)
92 *pszErrorText = 0;
93 }
94 else
95 {
96 hModule = NULL;
97 if (pszErrorText != NULL)
98 nx_strncpy(pszErrorText, (nError <= 19) ? m_pszErrorText[nError] : "Unknown error code", 255);
99 }
100 #else /* not _WIN32 and not _NETWARE */
101 #ifdef UNICODE
102 char *mbbuffer = MBStringFromWideString(pszLibName);
103 hModule = dlopen(mbbuffer, RTLD_NOW | RTLD_GLOBAL);
104 if ((hModule == NULL) && (pszErrorText != NULL))
105 {
106 WCHAR *wbuffer = WideStringFromMBString(dlerror());
107 nx_strncpy(pszErrorText, wbuffer, 255);
108 free(wbuffer);
109 }
110 free(mbbuffer);
111 #else
112 hModule = dlopen(pszLibName, RTLD_NOW | RTLD_GLOBAL);
113 if ((hModule == NULL) && (pszErrorText != NULL))
114 nx_strncpy(pszErrorText, dlerror(), 255);
115 #endif
116 #endif
117 return hModule;
118 }
119
120
121 //
122 // Unload DLL/shared library
123 //
124
125 void LIBNETXMS_EXPORTABLE DLClose(HMODULE hModule)
126 {
127 if (hModule != NULL)
128 {
129 #if defined(_WIN32)
130 FreeLibrary(hModule);
131 #elif defined(_NETWARE)
132 char szName[MAX_PATH], szTemp[1024];
133
134 GetNLMNames(hModule, szName, szTemp);
135 szName[szName[0] + 1] = 0;
136 UnloadModule(getscreenhandle(), &szName[1]);
137 #else
138 dlclose(hModule);
139 #endif
140 }
141 }
142
143
144 //
145 // Get symbol address from library
146 //
147
148 void LIBNETXMS_EXPORTABLE *DLGetSymbolAddr(HMODULE hModule,
149 const TCHAR *pszSymbol,
150 TCHAR *pszErrorText)
151 {
152 void *pAddr;
153
154 #if defined(_WIN32)
155 #if !defined(UNDER_CE) && defined(UNICODE)
156 char szBuffer[256];
157
158 WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK | WC_DEFAULTCHAR,
159 pszSymbol, -1, szBuffer, 256, NULL, NULL);
160 pAddr = GetProcAddress(hModule, szBuffer);
161 #else
162 pAddr = GetProcAddress(hModule, pszSymbol);
163 #endif
164 if ((pAddr == NULL) && (pszErrorText != NULL))
165 GetSystemErrorText(GetLastError(), pszErrorText, 255);
166 #elif defined(_NETWARE)
167 pAddr = ImportPublicObject(hModule, pszSymbol);
168 if (pszErrorText != NULL)
169 *pszErrorText = 0;
170 #else /* _WIN32 */
171 #ifdef UNICODE
172 char mbbuffer[256];
173
174 WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK | WC_DEFAULTCHAR,
175 pszSymbol, -1, mbbuffer, 256, NULL, NULL);
176 pAddr = dlsym(hModule, mbbuffer);
177 if ((pAddr == NULL) && (pszErrorText != NULL))
178 {
179 WCHAR *wbuffer = WideStringFromMBString(dlerror());
180 nx_strncpy(pszErrorText, wbuffer, 255);
181 free(wbuffer);
182 }
183 #else
184 pAddr = dlsym(hModule, pszSymbol);
185 if ((pAddr == NULL) && (pszErrorText != NULL))
186 nx_strncpy(pszErrorText, dlerror(), 255);
187 #endif
188 #endif
189 return pAddr;
190 }