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