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