Added check for NULL hModule to DLClose
[public/netxms.git] / src / libnetxms / dload.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Utility Library
4 ** Copyright (C) 2003, 2004 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 #ifndef _WIN32
27 #include <dlfcn.h>
28 #endif
29
30
31 //
32 // Load DLL/shared library
33 //
34
35 HMODULE LIBNETXMS_EXPORTABLE DLOpen(TCHAR *szLibName, TCHAR *pszErrorText)
36 {
37 HMODULE hModule;
38
39 #ifdef _WIN32
40 hModule = LoadLibrary(szLibName);
41 if (hModule == NULL)
42 GetSystemErrorText(GetLastError(), pszErrorText, 255);
43 #else /* _WIN32 */
44 hModule = dlopen(szLibName, RTLD_NOW | RTLD_GLOBAL);
45 if (hModule == NULL)
46 strncpy(pszErrorText, dlerror(), 255);
47 #endif
48 return hModule;
49 }
50
51
52 //
53 // Unload DLL/shared library
54 //
55
56 void LIBNETXMS_EXPORTABLE DLClose(HMODULE hModule)
57 {
58 if (hModule != NULL)
59 {
60 #ifdef _WIN32
61 FreeLibrary(hModule);
62 #else /* _WIN32 */
63 dlclose(hModule);
64 #endif
65 }
66 }
67
68
69 //
70 // Get symbol address from library
71 //
72
73 void LIBNETXMS_EXPORTABLE *DLGetSymbolAddr(HMODULE hModule,
74 TCHAR *pszSymbol,
75 TCHAR *pszErrorText)
76 {
77 void *pAddr;
78
79 #ifdef _WIN32
80 #if !defined(UNDER_CE) && defined(UNICODE)
81 char szBuffer[256];
82
83 WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK | WC_DEFAULTCHAR,
84 pszSymbol, -1, szBuffer, 256, NULL, NULL);
85 pAddr = GetProcAddress(hModule, szBuffer);
86 #else
87 pAddr = GetProcAddress(hModule, pszSymbol);
88 #endif
89 if (pAddr == NULL)
90 GetSystemErrorText(GetLastError(), pszErrorText, 255);
91 #else /* _WIN32 */
92 pAddr = dlsym(hModule, pszSymbol);
93 if (pAddr == NULL)
94 _tcsncpy(pszErrorText, dlerror(), 255);
95 #endif
96 return pAddr;
97 }