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