Fixed incorrect usage of strncpy
[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 #if defined(_NETWARE)
27 #include <netware.h>
28 #elif !defined(_WIN32)
29 #include <dlfcn.h>
30 #endif
31
32
33 //
34 // Load DLL/shared library
35 //
36
37 HMODULE LIBNETXMS_EXPORTABLE DLOpen(TCHAR *szLibName, TCHAR *pszErrorText)
38 {
39 HMODULE hModule;
40
41 #if defined(_WIN32)
42 hModule = LoadLibrary(szLibName);
43 if (hModule == NULL)
44 GetSystemErrorText(GetLastError(), pszErrorText, 255);
45 #elif defined(_NETWARE)
46 TCHAR szBuffer[MAX_PATH + 4];
47
48 nx_strncpy(&szBuffer[4], szLibName, MAX_PATH);
49 if (LoadModule(getscreenhandle(), &szBuffer[4], LO_RETURN_HANDLE) == 0)
50 {
51 hModule = *((HMODULE *)szBuffer);
52 }
53 else
54 {
55 hModule = NULL;
56 *pszErrorText = 0;
57 }
58 #else /* _WIN32 */
59 hModule = dlopen(szLibName, RTLD_NOW | RTLD_GLOBAL);
60 if (hModule == NULL)
61 nx_strncpy(pszErrorText, dlerror(), 255);
62 #endif
63 return hModule;
64 }
65
66
67 //
68 // Unload DLL/shared library
69 //
70
71 void LIBNETXMS_EXPORTABLE DLClose(HMODULE hModule)
72 {
73 if (hModule != NULL)
74 {
75 #if defined(_WIN32)
76 FreeLibrary(hModule);
77 #elif defined(_NETWARE)
78 char szName[MAX_PATH], szTemp[1024];
79
80 GetNLMNames(hModule, szName, szTemp);
81 szName[szName[0] + 1] = 0;
82 UnloadModule(getscreenhandle(), &szName[1]);
83 #else
84 dlclose(hModule);
85 #endif
86 }
87 }
88
89
90 //
91 // Get symbol address from library
92 //
93
94 void LIBNETXMS_EXPORTABLE *DLGetSymbolAddr(HMODULE hModule,
95 TCHAR *pszSymbol,
96 TCHAR *pszErrorText)
97 {
98 void *pAddr;
99
100 #if defined(_WIN32)
101 #if !defined(UNDER_CE) && defined(UNICODE)
102 char szBuffer[256];
103
104 WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK | WC_DEFAULTCHAR,
105 pszSymbol, -1, szBuffer, 256, NULL, NULL);
106 pAddr = GetProcAddress(hModule, szBuffer);
107 #else
108 pAddr = GetProcAddress(hModule, pszSymbol);
109 #endif
110 if (pAddr == NULL)
111 GetSystemErrorText(GetLastError(), pszErrorText, 255);
112 #elif defined(_NETWARE)
113 pAddr = ImportPublicObject(hModule, pszSymbol);
114 *pszErrorText = 0;
115 #else /* _WIN32 */
116 pAddr = dlsym(hModule, pszSymbol);
117 if (pAddr == NULL)
118 nx_strncpy(pszErrorText, dlerror(), 255);
119 #endif
120 return pAddr;
121 }