1 // Windows/DLL.cpp
2
3 #include "StdAfx.h"
4
5 #include "DLL.h"
6
7 #ifdef _WIN32
8
9 #ifndef _UNICODE
10 extern bool g_IsNT;
11 #endif
12
13 extern HINSTANCE g_hInstance;
14
15 namespace NWindows {
16 namespace NDLL {
17
Free()18 bool CLibrary::Free() throw()
19 {
20 if (_module == 0)
21 return true;
22 if (!::FreeLibrary(_module))
23 return false;
24 _module = 0;
25 return true;
26 }
27
LoadEx(CFSTR path,DWORD flags)28 bool CLibrary::LoadEx(CFSTR path, DWORD flags) throw()
29 {
30 if (!Free())
31 return false;
32 #ifndef _UNICODE
33 if (!g_IsNT)
34 {
35 _module = ::LoadLibraryEx(fs2fas(path), NULL, flags);
36 }
37 else
38 #endif
39 {
40 _module = ::LoadLibraryExW(fs2us(path), NULL, flags);
41 }
42 return (_module != NULL);
43 }
44
Load(CFSTR path)45 bool CLibrary::Load(CFSTR path) throw()
46 {
47 if (!Free())
48 return false;
49 #ifndef _UNICODE
50 if (!g_IsNT)
51 {
52 _module = ::LoadLibrary(fs2fas(path));
53 }
54 else
55 #endif
56 {
57 _module = ::LoadLibraryW(fs2us(path));
58 }
59 return (_module != NULL);
60 }
61
MyGetModuleFileName(FString & path)62 bool MyGetModuleFileName(FString &path)
63 {
64 HMODULE hModule = g_hInstance;
65 path.Empty();
66 #ifndef _UNICODE
67 if (!g_IsNT)
68 {
69 TCHAR s[MAX_PATH + 2];
70 s[0] = 0;
71 DWORD size = ::GetModuleFileName(hModule, s, MAX_PATH + 1);
72 if (size <= MAX_PATH && size != 0)
73 {
74 path = fas2fs(s);
75 return true;
76 }
77 }
78 else
79 #endif
80 {
81 WCHAR s[MAX_PATH + 2];
82 s[0] = 0;
83 DWORD size = ::GetModuleFileNameW(hModule, s, MAX_PATH + 1);
84 if (size <= MAX_PATH && size != 0)
85 {
86 path = us2fs(s);
87 return true;
88 }
89 }
90 return false;
91 }
92
93 #ifndef _SFX
94
GetModuleDirPrefix()95 FString GetModuleDirPrefix()
96 {
97 FString s;
98 if (MyGetModuleFileName(s))
99 {
100 int pos = s.ReverseFind_PathSepar();
101 if (pos >= 0)
102 s.DeleteFrom((unsigned)(pos + 1));
103 }
104 if (s.IsEmpty())
105 s = "." STRING_PATH_SEPARATOR;
106 return s;
107 }
108
109 #endif
110
111 }}
112
113 #else
114
115 #include <dlfcn.h>
116 #include <stdlib.h>
117
118 namespace NWindows {
119 namespace NDLL {
120
Free()121 bool CLibrary::Free() throw()
122 {
123 if (_module == NULL)
124 return true;
125 int ret = dlclose(_module);
126 if (ret != 0)
127 return false;
128 _module = NULL;
129 return true;
130 }
131
132 static
133 // FARPROC
134 void *
local_GetProcAddress(HMODULE module,LPCSTR procName)135 local_GetProcAddress(HMODULE module, LPCSTR procName)
136 {
137 void *ptr = NULL;
138 if (module)
139 {
140 ptr = dlsym(module, procName);
141 }
142 return ptr;
143 }
144
Load(CFSTR path)145 bool CLibrary::Load(CFSTR path) throw()
146 {
147 if (!Free())
148 return false;
149
150 int options = 0;
151
152 #ifdef RTLD_LOCAL
153 options |= RTLD_LOCAL;
154 #endif
155
156 #ifdef RTLD_NOW
157 options |= RTLD_NOW;
158 #endif
159
160 #ifdef RTLD_GROUP
161 #if ! (defined(hpux) || defined(__hpux))
162 options |= RTLD_GROUP; // mainly for solaris but not for HPUX
163 #endif
164 #endif
165
166 void *handler = dlopen(path, options);
167
168 if (handler)
169 {
170 // here we can transfer some settings to DLL
171 }
172 else
173 {
174 }
175
176 _module = handler;
177
178 return (_module != NULL);
179 }
180
181 // FARPROC
GetProc(LPCSTR procName) const182 void * CLibrary::GetProc(LPCSTR procName) const
183 {
184 // return My_GetProcAddress(_module, procName);
185 return local_GetProcAddress(_module, procName);
186 // return NULL;
187 }
188
189 }}
190
191 #endif
192