1370b324cSopenharmony_ci// Windows/DLL.cpp
2370b324cSopenharmony_ci
3370b324cSopenharmony_ci#include "StdAfx.h"
4370b324cSopenharmony_ci
5370b324cSopenharmony_ci#include "DLL.h"
6370b324cSopenharmony_ci
7370b324cSopenharmony_ci#ifdef _WIN32
8370b324cSopenharmony_ci
9370b324cSopenharmony_ci#ifndef _UNICODE
10370b324cSopenharmony_ciextern bool g_IsNT;
11370b324cSopenharmony_ci#endif
12370b324cSopenharmony_ci
13370b324cSopenharmony_ciextern HINSTANCE g_hInstance;
14370b324cSopenharmony_ci
15370b324cSopenharmony_cinamespace NWindows {
16370b324cSopenharmony_cinamespace NDLL {
17370b324cSopenharmony_ci
18370b324cSopenharmony_cibool CLibrary::Free() throw()
19370b324cSopenharmony_ci{
20370b324cSopenharmony_ci  if (_module == NULL)
21370b324cSopenharmony_ci    return true;
22370b324cSopenharmony_ci  if (!::FreeLibrary(_module))
23370b324cSopenharmony_ci    return false;
24370b324cSopenharmony_ci  _module = NULL;
25370b324cSopenharmony_ci  return true;
26370b324cSopenharmony_ci}
27370b324cSopenharmony_ci
28370b324cSopenharmony_cibool CLibrary::LoadEx(CFSTR path, DWORD flags) throw()
29370b324cSopenharmony_ci{
30370b324cSopenharmony_ci  if (!Free())
31370b324cSopenharmony_ci    return false;
32370b324cSopenharmony_ci  #ifndef _UNICODE
33370b324cSopenharmony_ci  if (!g_IsNT)
34370b324cSopenharmony_ci  {
35370b324cSopenharmony_ci    _module = ::LoadLibraryEx(fs2fas(path), NULL, flags);
36370b324cSopenharmony_ci  }
37370b324cSopenharmony_ci  else
38370b324cSopenharmony_ci  #endif
39370b324cSopenharmony_ci  {
40370b324cSopenharmony_ci    _module = ::LoadLibraryExW(fs2us(path), NULL, flags);
41370b324cSopenharmony_ci  }
42370b324cSopenharmony_ci  return (_module != NULL);
43370b324cSopenharmony_ci}
44370b324cSopenharmony_ci
45370b324cSopenharmony_cibool CLibrary::Load(CFSTR path) throw()
46370b324cSopenharmony_ci{
47370b324cSopenharmony_ci  if (!Free())
48370b324cSopenharmony_ci    return false;
49370b324cSopenharmony_ci  #ifndef _UNICODE
50370b324cSopenharmony_ci  if (!g_IsNT)
51370b324cSopenharmony_ci  {
52370b324cSopenharmony_ci    _module = ::LoadLibrary(fs2fas(path));
53370b324cSopenharmony_ci  }
54370b324cSopenharmony_ci  else
55370b324cSopenharmony_ci  #endif
56370b324cSopenharmony_ci  {
57370b324cSopenharmony_ci    _module = ::LoadLibraryW(fs2us(path));
58370b324cSopenharmony_ci  }
59370b324cSopenharmony_ci  return (_module != NULL);
60370b324cSopenharmony_ci}
61370b324cSopenharmony_ci
62370b324cSopenharmony_cibool MyGetModuleFileName(FString &path)
63370b324cSopenharmony_ci{
64370b324cSopenharmony_ci  HMODULE hModule = g_hInstance;
65370b324cSopenharmony_ci  path.Empty();
66370b324cSopenharmony_ci  #ifndef _UNICODE
67370b324cSopenharmony_ci  if (!g_IsNT)
68370b324cSopenharmony_ci  {
69370b324cSopenharmony_ci    TCHAR s[MAX_PATH + 2];
70370b324cSopenharmony_ci    s[0] = 0;
71370b324cSopenharmony_ci    DWORD size = ::GetModuleFileName(hModule, s, MAX_PATH + 1);
72370b324cSopenharmony_ci    if (size <= MAX_PATH && size != 0)
73370b324cSopenharmony_ci    {
74370b324cSopenharmony_ci      path = fas2fs(s);
75370b324cSopenharmony_ci      return true;
76370b324cSopenharmony_ci    }
77370b324cSopenharmony_ci  }
78370b324cSopenharmony_ci  else
79370b324cSopenharmony_ci  #endif
80370b324cSopenharmony_ci  {
81370b324cSopenharmony_ci    WCHAR s[MAX_PATH + 2];
82370b324cSopenharmony_ci    s[0] = 0;
83370b324cSopenharmony_ci    DWORD size = ::GetModuleFileNameW(hModule, s, MAX_PATH + 1);
84370b324cSopenharmony_ci    if (size <= MAX_PATH && size != 0)
85370b324cSopenharmony_ci    {
86370b324cSopenharmony_ci      path = us2fs(s);
87370b324cSopenharmony_ci      return true;
88370b324cSopenharmony_ci    }
89370b324cSopenharmony_ci  }
90370b324cSopenharmony_ci  return false;
91370b324cSopenharmony_ci}
92370b324cSopenharmony_ci
93370b324cSopenharmony_ci#ifndef Z7_SFX
94370b324cSopenharmony_ci
95370b324cSopenharmony_ciFString GetModuleDirPrefix()
96370b324cSopenharmony_ci{
97370b324cSopenharmony_ci  FString s;
98370b324cSopenharmony_ci  if (MyGetModuleFileName(s))
99370b324cSopenharmony_ci  {
100370b324cSopenharmony_ci    int pos = s.ReverseFind_PathSepar();
101370b324cSopenharmony_ci    if (pos >= 0)
102370b324cSopenharmony_ci      s.DeleteFrom((unsigned)(pos + 1));
103370b324cSopenharmony_ci  }
104370b324cSopenharmony_ci  if (s.IsEmpty())
105370b324cSopenharmony_ci    s = "." STRING_PATH_SEPARATOR;
106370b324cSopenharmony_ci  return s;
107370b324cSopenharmony_ci}
108370b324cSopenharmony_ci
109370b324cSopenharmony_ci#endif
110370b324cSopenharmony_ci
111370b324cSopenharmony_ci}}
112370b324cSopenharmony_ci
113370b324cSopenharmony_ci#else // _WIN32
114370b324cSopenharmony_ci
115370b324cSopenharmony_ci#include <dlfcn.h>
116370b324cSopenharmony_ci#include <stdlib.h>
117370b324cSopenharmony_ci#include "../Common/Common.h"
118370b324cSopenharmony_ci
119370b324cSopenharmony_ci// FARPROC
120370b324cSopenharmony_civoid *GetProcAddress(HMODULE module, LPCSTR procName)
121370b324cSopenharmony_ci{
122370b324cSopenharmony_ci  void *ptr = NULL;
123370b324cSopenharmony_ci  if (module)
124370b324cSopenharmony_ci    ptr = dlsym(module, procName);
125370b324cSopenharmony_ci  return ptr;
126370b324cSopenharmony_ci}
127370b324cSopenharmony_ci
128370b324cSopenharmony_cinamespace NWindows {
129370b324cSopenharmony_cinamespace NDLL {
130370b324cSopenharmony_ci
131370b324cSopenharmony_cibool CLibrary::Free() throw()
132370b324cSopenharmony_ci{
133370b324cSopenharmony_ci  if (!_module)
134370b324cSopenharmony_ci    return true;
135370b324cSopenharmony_ci  const int ret = dlclose(_module);
136370b324cSopenharmony_ci  if (ret != 0)
137370b324cSopenharmony_ci    return false;
138370b324cSopenharmony_ci  _module = NULL;
139370b324cSopenharmony_ci  return true;
140370b324cSopenharmony_ci}
141370b324cSopenharmony_ci
142370b324cSopenharmony_cibool CLibrary::Load(CFSTR path) throw()
143370b324cSopenharmony_ci{
144370b324cSopenharmony_ci  if (!Free())
145370b324cSopenharmony_ci    return false;
146370b324cSopenharmony_ci
147370b324cSopenharmony_ci  int options = 0;
148370b324cSopenharmony_ci
149370b324cSopenharmony_ci  #ifdef RTLD_LOCAL
150370b324cSopenharmony_ci    options |= RTLD_LOCAL;
151370b324cSopenharmony_ci  #endif
152370b324cSopenharmony_ci
153370b324cSopenharmony_ci  #ifdef RTLD_NOW
154370b324cSopenharmony_ci    options |= RTLD_NOW;
155370b324cSopenharmony_ci  #endif
156370b324cSopenharmony_ci
157370b324cSopenharmony_ci  #ifdef RTLD_GROUP
158370b324cSopenharmony_ci    #if ! (defined(hpux) || defined(__hpux))
159370b324cSopenharmony_ci      options |= RTLD_GROUP; // mainly for solaris but not for HPUX
160370b324cSopenharmony_ci    #endif
161370b324cSopenharmony_ci  #endif
162370b324cSopenharmony_ci
163370b324cSopenharmony_ci  _module = dlopen(path, options);
164370b324cSopenharmony_ci  return (_module != NULL);
165370b324cSopenharmony_ci}
166370b324cSopenharmony_ci
167370b324cSopenharmony_ci/*
168370b324cSopenharmony_ci// FARPROC
169370b324cSopenharmony_civoid * CLibrary::GetProc(LPCSTR procName) const
170370b324cSopenharmony_ci{
171370b324cSopenharmony_ci  // return My_GetProcAddress(_module, procName);
172370b324cSopenharmony_ci  return local_GetProcAddress(_module, procName);
173370b324cSopenharmony_ci  // return NULL;
174370b324cSopenharmony_ci}
175370b324cSopenharmony_ci*/
176370b324cSopenharmony_ci
177370b324cSopenharmony_ci}}
178370b324cSopenharmony_ci
179370b324cSopenharmony_ci#endif
180