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 == NULL)
21 return true;
22 if (!::FreeLibrary(_module))
23 return false;
24 _module = NULL;
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 const 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 const 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 const 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 Z7_SFX
94
GetModuleDirPrefix()95 FString GetModuleDirPrefix()
96 {
97 FString s;
98 if (MyGetModuleFileName(s))
99 {
100 const 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 // _WIN32
114
115 #include <dlfcn.h>
116 #include <stdlib.h>
117
118 // FARPROC
GetProcAddress(HMODULE module,LPCSTR procName)119 void *GetProcAddress(HMODULE module, LPCSTR procName)
120 {
121 void *ptr = NULL;
122 if (module)
123 ptr = dlsym(module, procName);
124 return ptr;
125 }
126
127 namespace NWindows {
128 namespace NDLL {
129
Free()130 bool CLibrary::Free() throw()
131 {
132 if (!_module)
133 return true;
134 const int ret = dlclose(_module);
135 if (ret != 0)
136 return false;
137 _module = NULL;
138 return true;
139 }
140
Load(CFSTR path)141 bool CLibrary::Load(CFSTR path) throw()
142 {
143 if (!Free())
144 return false;
145
146 int options = 0;
147
148 #ifdef RTLD_LOCAL
149 options |= RTLD_LOCAL;
150 #endif
151
152 #ifdef RTLD_NOW
153 options |= RTLD_NOW;
154 #endif
155
156 #ifdef RTLD_GROUP
157 #if ! (defined(hpux) || defined(__hpux))
158 options |= RTLD_GROUP; // mainly for solaris but not for HPUX
159 #endif
160 #endif
161
162 _module = dlopen(path, options);
163 return (_module != NULL);
164 }
165
166 /*
167 // FARPROC
168 void * CLibrary::GetProc(LPCSTR procName) const
169 {
170 // return My_GetProcAddress(_module, procName);
171 return local_GetProcAddress(_module, procName);
172 // return NULL;
173 }
174 */
175
176 }}
177
178 #endif
179