1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/other-licenses/7zstub/src/Windows/DLL.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,115 @@ 1.4 +// Windows/DLL.cpp 1.5 + 1.6 +#include "StdAfx.h" 1.7 + 1.8 +#include "DLL.h" 1.9 +#include "Defs.h" 1.10 +#ifndef _UNICODE 1.11 +#include "../Common/StringConvert.h" 1.12 +#endif 1.13 + 1.14 +#ifndef _UNICODE 1.15 +extern bool g_IsNT; 1.16 +#endif 1.17 + 1.18 +namespace NWindows { 1.19 +namespace NDLL { 1.20 + 1.21 +CLibrary::~CLibrary() 1.22 +{ 1.23 + Free(); 1.24 +} 1.25 + 1.26 +bool CLibrary::Free() 1.27 +{ 1.28 + if (_module == 0) 1.29 + return true; 1.30 + // MessageBox(0, TEXT(""), TEXT("Free"), 0); 1.31 + // Sleep(5000); 1.32 + if (!::FreeLibrary(_module)) 1.33 + return false; 1.34 + _module = 0; 1.35 + return true; 1.36 +} 1.37 + 1.38 +bool CLibrary::LoadOperations(HMODULE newModule) 1.39 +{ 1.40 + if (newModule == NULL) 1.41 + return false; 1.42 + if(!Free()) 1.43 + return false; 1.44 + _module = newModule; 1.45 + return true; 1.46 +} 1.47 + 1.48 +bool CLibrary::LoadEx(LPCTSTR fileName, DWORD flags) 1.49 +{ 1.50 + // MessageBox(0, fileName, TEXT("LoadEx"), 0); 1.51 + return LoadOperations(::LoadLibraryEx(fileName, NULL, flags)); 1.52 +} 1.53 + 1.54 +bool CLibrary::Load(LPCTSTR fileName) 1.55 +{ 1.56 + // MessageBox(0, fileName, TEXT("Load"), 0); 1.57 + // Sleep(5000); 1.58 + // OutputDebugString(fileName); 1.59 + // OutputDebugString(TEXT("\n")); 1.60 + return LoadOperations(::LoadLibrary(fileName)); 1.61 +} 1.62 + 1.63 +#ifndef _UNICODE 1.64 +static inline UINT GetCurrentCodePage() { return ::AreFileApisANSI() ? CP_ACP : CP_OEMCP; } 1.65 +CSysString GetSysPath(LPCWSTR sysPath) 1.66 + { return UnicodeStringToMultiByte(sysPath, GetCurrentCodePage()); } 1.67 + 1.68 +bool CLibrary::LoadEx(LPCWSTR fileName, DWORD flags) 1.69 +{ 1.70 + if (g_IsNT) 1.71 + return LoadOperations(::LoadLibraryExW(fileName, NULL, flags)); 1.72 + return LoadEx(GetSysPath(fileName), flags); 1.73 +} 1.74 +bool CLibrary::Load(LPCWSTR fileName) 1.75 +{ 1.76 + if (g_IsNT) 1.77 + return LoadOperations(::LoadLibraryW(fileName)); 1.78 + return Load(GetSysPath(fileName)); 1.79 +} 1.80 +#endif 1.81 + 1.82 +bool MyGetModuleFileName(HMODULE hModule, CSysString &result) 1.83 +{ 1.84 + result.Empty(); 1.85 + TCHAR fullPath[MAX_PATH + 2]; 1.86 + DWORD size = ::GetModuleFileName(hModule, fullPath, MAX_PATH + 1); 1.87 + if (size <= MAX_PATH && size != 0) 1.88 + { 1.89 + result = fullPath; 1.90 + return true; 1.91 + } 1.92 + return false; 1.93 +} 1.94 + 1.95 +#ifndef _UNICODE 1.96 +bool MyGetModuleFileName(HMODULE hModule, UString &result) 1.97 +{ 1.98 + result.Empty(); 1.99 + if (g_IsNT) 1.100 + { 1.101 + wchar_t fullPath[MAX_PATH + 2]; 1.102 + DWORD size = ::GetModuleFileNameW(hModule, fullPath, MAX_PATH + 1); 1.103 + if (size <= MAX_PATH && size != 0) 1.104 + { 1.105 + result = fullPath; 1.106 + return true; 1.107 + } 1.108 + return false; 1.109 + } 1.110 + CSysString resultSys; 1.111 + if (!MyGetModuleFileName(hModule, resultSys)) 1.112 + return false; 1.113 + result = MultiByteToUnicodeString(resultSys, GetCurrentCodePage()); 1.114 + return true; 1.115 +} 1.116 +#endif 1.117 + 1.118 +}}