Upload 2.0.2
[physicsfs] / lzma / CPP / Windows / DLL.cpp
1 // Windows/DLL.cpp
2
3 #include "StdAfx.h"
4
5 #include "DLL.h"
6 #include "Defs.h"
7 #ifndef _UNICODE
8 #include "../Common/StringConvert.h"
9 #endif
10
11 #ifndef _UNICODE
12 extern bool g_IsNT;
13 #endif
14
15 namespace NWindows {
16 namespace NDLL {
17
18 CLibrary::~CLibrary()
19 {
20   Free();
21 }
22
23 bool CLibrary::Free()
24 {
25   if (_module == 0)
26     return true;
27   // MessageBox(0, TEXT(""), TEXT("Free"), 0);
28   // Sleep(5000);
29   if (!::FreeLibrary(_module))
30     return false;
31   _module = 0;
32   return true;
33 }
34
35 bool CLibrary::LoadOperations(HMODULE newModule)
36 {
37   if (newModule == NULL)
38     return false;
39   if(!Free())
40     return false;
41   _module = newModule;
42   return true;
43 }
44
45 bool CLibrary::LoadEx(LPCTSTR fileName, DWORD flags)
46 {
47   // MessageBox(0, fileName, TEXT("LoadEx"), 0);
48   return LoadOperations(::LoadLibraryEx(fileName, NULL, flags));
49 }
50
51 bool CLibrary::Load(LPCTSTR fileName)
52 {
53   // MessageBox(0, fileName, TEXT("Load"), 0);
54   // Sleep(5000);
55   // OutputDebugString(fileName);
56   // OutputDebugString(TEXT("\n"));
57   return LoadOperations(::LoadLibrary(fileName));
58 }
59
60 #ifndef _UNICODE
61 static inline UINT GetCurrentCodePage() { return ::AreFileApisANSI() ? CP_ACP : CP_OEMCP; } 
62 CSysString GetSysPath(LPCWSTR sysPath)
63   { return UnicodeStringToMultiByte(sysPath, GetCurrentCodePage()); }
64
65 bool CLibrary::LoadEx(LPCWSTR fileName, DWORD flags)
66 {
67   if (g_IsNT)
68     return LoadOperations(::LoadLibraryExW(fileName, NULL, flags));
69   return LoadEx(GetSysPath(fileName), flags);
70 }
71 bool CLibrary::Load(LPCWSTR fileName)
72 {
73   if (g_IsNT)
74     return LoadOperations(::LoadLibraryW(fileName));
75   return Load(GetSysPath(fileName));
76 }
77 #endif
78
79 bool MyGetModuleFileName(HMODULE hModule, CSysString &result)
80 {
81   result.Empty();
82   TCHAR fullPath[MAX_PATH + 2];
83   DWORD size = ::GetModuleFileName(hModule, fullPath, MAX_PATH + 1);
84   if (size <= MAX_PATH && size != 0)
85   {
86     result = fullPath;
87     return true;
88   }
89   return false;
90 }
91
92 #ifndef _UNICODE
93 bool MyGetModuleFileName(HMODULE hModule, UString &result)
94 {
95   result.Empty();
96   if (g_IsNT)
97   {
98     wchar_t fullPath[MAX_PATH + 2];
99     DWORD size = ::GetModuleFileNameW(hModule, fullPath, MAX_PATH + 1);
100     if (size <= MAX_PATH && size != 0)
101     {
102       result = fullPath;
103       return true;
104     }
105     return false;
106   }
107   CSysString resultSys;
108   if (!MyGetModuleFileName(hModule, resultSys))
109     return false;
110   result = MultiByteToUnicodeString(resultSys, GetCurrentCodePage());
111   return true;
112 }
113 #endif
114
115 }}