- libnxcscp merged into libnetxms
[public/netxms.git] / src / libnetxms / dload.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Utility Library
4 ** Copyright (C) 2003, 2004, 2005, 2006 Victor Kirhenshtein
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2 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 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 ** $module: 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(TCHAR *szLibName, TCHAR *pszErrorText)
75 {
76 HMODULE hModule;
77
78 #if defined(_WIN32)
79 hModule = LoadLibrary(szLibName);
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], szLibName, 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 /* _WIN32 */
101 hModule = dlopen(szLibName, RTLD_NOW | RTLD_GLOBAL);
102 if ((hModule == NULL) && (pszErrorText != NULL))
103 nx_strncpy(pszErrorText, dlerror(), 255);
104 #endif
105 return hModule;
106 }
107
108
109 //
110 // Unload DLL/shared library
111 //
112
113 void LIBNETXMS_EXPORTABLE DLClose(HMODULE hModule)
114 {
115 if (hModule != NULL)
116 {
117 #if defined(_WIN32)
118 FreeLibrary(hModule);
119 #elif defined(_NETWARE)
120 char szName[MAX_PATH], szTemp[1024];
121
122 GetNLMNames(hModule, szName, szTemp);
123 szName[szName[0] + 1] = 0;
124 UnloadModule(getscreenhandle(), &szName[1]);
125 #else
126 dlclose(hModule);
127 #endif
128 }
129 }
130
131
132 //
133 // Get symbol address from library
134 //
135
136 void LIBNETXMS_EXPORTABLE *DLGetSymbolAddr(HMODULE hModule,
137 TCHAR *pszSymbol,
138 TCHAR *pszErrorText)
139 {
140 void *pAddr;
141
142 #if defined(_WIN32)
143 #if !defined(UNDER_CE) && defined(UNICODE)
144 char szBuffer[256];
145
146 WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK | WC_DEFAULTCHAR,
147 pszSymbol, -1, szBuffer, 256, NULL, NULL);
148 pAddr = GetProcAddress(hModule, szBuffer);
149 #else
150 pAddr = GetProcAddress(hModule, pszSymbol);
151 #endif
152 if ((pAddr == NULL) && (pszErrorText != NULL))
153 GetSystemErrorText(GetLastError(), pszErrorText, 255);
154 #elif defined(_NETWARE)
155 pAddr = ImportPublicObject(hModule, pszSymbol);
156 if (pszErrorText != NULL)
157 *pszErrorText = 0;
158 #else /* _WIN32 */
159 pAddr = dlsym(hModule, pszSymbol);
160 if ((pAddr == NULL) && (pszErrorText != NULL))
161 nx_strncpy(pszErrorText, dlerror(), 255);
162 #endif
163 return pAddr;
164 }