1 // Windows/DLL.cpp
2 
3 #include "StdAfx.h"
4 
5 #include "DLL.h"
6 
7 #ifndef _UNICODE
8 extern bool g_IsNT;
9 #endif
10 
11 extern HINSTANCE g_hInstance;
12 
13 namespace NWindows {
14 namespace NDLL {
15 
Free()16 bool CLibrary::Free() throw()
17 {
18   if (_module == 0)
19     return true;
20   if (!::FreeLibrary(_module))
21     return false;
22   _module = 0;
23   return true;
24 }
25 
LoadEx(CFSTR path,DWORD flags)26 bool CLibrary::LoadEx(CFSTR path, DWORD flags) throw()
27 {
28   if (!Free())
29     return false;
30   #ifndef _UNICODE
31   if (!g_IsNT)
32   {
33     _module = ::LoadLibraryEx(fs2fas(path), NULL, flags);
34   }
35   else
36   #endif
37   {
38     _module = ::LoadLibraryExW(fs2us(path), NULL, flags);
39   }
40   return (_module != NULL);
41 }
42 
Load(CFSTR path)43 bool CLibrary::Load(CFSTR path) throw()
44 {
45   if (!Free())
46     return false;
47   #ifndef _UNICODE
48   if (!g_IsNT)
49   {
50     _module = ::LoadLibrary(fs2fas(path));
51   }
52   else
53   #endif
54   {
55     _module = ::LoadLibraryW(fs2us(path));
56   }
57   return (_module != NULL);
58 }
59 
MyGetModuleFileName(FString & path)60 bool MyGetModuleFileName(FString &path)
61 {
62   HMODULE hModule = g_hInstance;
63   path.Empty();
64   #ifndef _UNICODE
65   if (!g_IsNT)
66   {
67     TCHAR s[MAX_PATH + 2];
68     s[0] = 0;
69     DWORD size = ::GetModuleFileName(hModule, s, MAX_PATH + 1);
70     if (size <= MAX_PATH && size != 0)
71     {
72       path = fas2fs(s);
73       return true;
74     }
75   }
76   else
77   #endif
78   {
79     WCHAR s[MAX_PATH + 2];
80     s[0] = 0;
81     DWORD size = ::GetModuleFileNameW(hModule, s, MAX_PATH + 1);
82     if (size <= MAX_PATH && size != 0)
83     {
84       path = us2fs(s);
85       return true;
86     }
87   }
88   return false;
89 }
90 
91 #ifndef _SFX
92 
GetModuleDirPrefix()93 FString GetModuleDirPrefix()
94 {
95   FString s;
96   if (MyGetModuleFileName(s))
97   {
98     int pos = s.ReverseFind_PathSepar();
99     if (pos >= 0)
100     {
101       s.DeleteFrom(pos + 1);
102       return s;
103     }
104   }
105   return FTEXT(".") FSTRING_PATH_SEPARATOR;
106 }
107 
108 #endif
109 
110 }}
111