SharedLibrary_WIN32U.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // SharedLibrary_WIN32U.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/SharedLibrary_WIN32U.cpp#2 $
  5. //
  6. // Library: Foundation
  7. // Package: SharedLibrary
  8. // Module: SharedLibrary
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/SharedLibrary_WIN32U.h"
  16. #include "Poco/UnicodeConverter.h"
  17. #include "Poco/Path.h"
  18. #include "Poco/UnWindows.h"
  19. namespace Poco {
  20. FastMutex SharedLibraryImpl::_mutex;
  21. SharedLibraryImpl::SharedLibraryImpl()
  22. {
  23. _handle = 0;
  24. }
  25. SharedLibraryImpl::~SharedLibraryImpl()
  26. {
  27. }
  28. void SharedLibraryImpl::loadImpl(const std::string& path, int /*flags*/)
  29. {
  30. FastMutex::ScopedLock lock(_mutex);
  31. if (_handle) throw LibraryAlreadyLoadedException(_path);
  32. DWORD flags(0);
  33. #if !defined(_WIN32_WCE)
  34. Path p(path);
  35. if (p.isAbsolute()) flags |= LOAD_WITH_ALTERED_SEARCH_PATH;
  36. #endif
  37. std::wstring upath;
  38. UnicodeConverter::toUTF16(path, upath);
  39. _handle = LoadLibraryExW(upath.c_str(), 0, flags);
  40. if (!_handle) throw LibraryLoadException(path);
  41. _path = path;
  42. }
  43. void SharedLibraryImpl::unloadImpl()
  44. {
  45. FastMutex::ScopedLock lock(_mutex);
  46. if (_handle)
  47. {
  48. FreeLibrary((HMODULE) _handle);
  49. _handle = 0;
  50. }
  51. _path.clear();
  52. }
  53. bool SharedLibraryImpl::isLoadedImpl() const
  54. {
  55. return _handle != 0;
  56. }
  57. void* SharedLibraryImpl::findSymbolImpl(const std::string& name)
  58. {
  59. FastMutex::ScopedLock lock(_mutex);
  60. if (_handle)
  61. {
  62. #if defined(_WIN32_WCE)
  63. std::wstring uname;
  64. UnicodeConverter::toUTF16(name, uname);
  65. return (void*) GetProcAddressW((HMODULE) _handle, uname.c_str());
  66. #else
  67. return (void*) GetProcAddress((HMODULE) _handle, name.c_str());
  68. #endif
  69. }
  70. else return 0;
  71. }
  72. const std::string& SharedLibraryImpl::getPathImpl() const
  73. {
  74. return _path;
  75. }
  76. std::string SharedLibraryImpl::suffixImpl()
  77. {
  78. #if defined(_DEBUG)
  79. return "d.dll";
  80. #else
  81. return ".dll";
  82. #endif
  83. }
  84. } // namespace Poco