winDlibrary.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #define NOMINMAX
  23. #define WIN32_LEAN_AND_MEAN
  24. #include <windows.h>
  25. #include "platform/types.h"
  26. #include "platform/platformDlibrary.h"
  27. class Win32DLibrary: public DLibrary
  28. {
  29. HMODULE _handle;
  30. public:
  31. Win32DLibrary();
  32. ~Win32DLibrary();
  33. bool open(const char* file);
  34. void close();
  35. void *bind(const char *name);
  36. };
  37. Win32DLibrary::Win32DLibrary()
  38. {
  39. _handle = 0;
  40. }
  41. Win32DLibrary::~Win32DLibrary()
  42. {
  43. close();
  44. }
  45. bool Win32DLibrary::open(const char* file)
  46. {
  47. // dlopen should also include the RTLD_LOCAL flag, but it seems to be
  48. // missing from cygwin
  49. _handle = LoadLibraryA(file);
  50. if (!_handle)
  51. return false;
  52. bool (*open)() = (bool(*)())bind("dllopen");
  53. if (open && !(*open)()) {
  54. FreeLibrary(_handle);
  55. _handle = 0;
  56. return false;
  57. }
  58. return true;
  59. }
  60. void Win32DLibrary::close()
  61. {
  62. if (_handle) {
  63. void (*close)() = (void(*)())bind("dllclose");
  64. if (close)
  65. (*close)();
  66. FreeLibrary(_handle);
  67. _handle = 0;
  68. }
  69. }
  70. void* Win32DLibrary::bind(const char *name)
  71. {
  72. return _handle? (void*)GetProcAddress(_handle,name): 0;
  73. }
  74. DLibraryRef OsLoadLibrary(const char* file)
  75. {
  76. Win32DLibrary* library = new Win32DLibrary();
  77. if (!library->open(file)) {
  78. delete library;
  79. return 0;
  80. }
  81. return library;
  82. }