DLL.cpp 2.4 KB

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