Full UNICODE support in libnetxms
[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 #ifdef _WIN32
59 FreeLibrary(hModule);
60 #else /* _WIN32 */
61 dlclose(hModule);
62 #endif
63 }
64
65
66 //
67 // Get symbol address from library
68 //
69
70 void LIBNETXMS_EXPORTABLE *DLGetSymbolAddr(HMODULE hModule,
71 TCHAR *pszSymbol,
72 TCHAR *pszErrorText)
73 {
74 void *pAddr;
75
76 #ifdef _WIN32
77 #if !defined(UNDER_CE) && defined(UNICODE)
78 char szBuffer[256];
79
80 WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK | WC_DEFAULTCHAR,
81 pszSymbol, -1, szBuffer, 256, NULL, NULL);
82 pAddr = GetProcAddress(hModule, szBuffer);
83 #else
84 pAddr = GetProcAddress(hModule, pszSymbol);
85 #endif
86 if (pAddr == NULL)
87 GetSystemErrorText(GetLastError(), pszErrorText, 255);
88 #else /* _WIN32 */
89 pAddr = dlsym(hModule, szSymbol);
90 if (pAddr == NULL)
91 _tscncpy(pszErrorText, dlerror(), 255);
92 #endif
93 return pAddr;
94 }