DLLRegister.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include "DLLRegister.h"
  2. using namespace Framework;
  3. // Content of the DLLFiles class from DLLFiles.h
  4. // Constructor
  5. DLLRegister::DLLRegister()
  6. : ReferenceCounter()
  7. {
  8. dlls = new Array<DLLFile*>();
  9. }
  10. // Destructor
  11. DLLRegister::~DLLRegister()
  12. {
  13. cs.lock();
  14. int anz = dlls->getEntryCount();
  15. for (int i = 0; i < anz; i++)
  16. {
  17. DLLFile* tmp = dlls->get(i);
  18. if (tmp)
  19. {
  20. tmp->name->release();
  21. #ifndef _DEBUG
  22. FreeLibrary(tmp->handle);
  23. #endif
  24. }
  25. delete tmp;
  26. }
  27. dlls->release();
  28. cs.unlock();
  29. }
  30. // non-constant
  31. HINSTANCE DLLRegister::loadDLL(const char* name, const char* pfad)
  32. {
  33. cs.lock();
  34. int anz = dlls->getEntryCount();
  35. for (int i = 0; i < anz; i++)
  36. {
  37. DLLFile* tmp = dlls->get(i);
  38. if (tmp && tmp->name->isEqual(name))
  39. {
  40. tmp->ref++;
  41. cs.unlock();
  42. return tmp->handle;
  43. }
  44. }
  45. HINSTANCE h = LoadLibrary(pfad);
  46. if (!h)
  47. {
  48. cs.unlock();
  49. return 0;
  50. }
  51. DLLFile* dll = new DLLFile();
  52. dll->name = new Text(name);
  53. dll->handle = h;
  54. dll->ref = 1;
  55. dlls->add(dll);
  56. cs.unlock();
  57. return h;
  58. }
  59. void DLLRegister::releaseDLL(const char* name)
  60. {
  61. cs.lock();
  62. int anz = dlls->getEntryCount();
  63. for (int i = 0; i < anz; i++)
  64. {
  65. DLLFile* tmp = dlls->get(i);
  66. if (tmp && tmp->name->isEqual(name))
  67. {
  68. tmp->ref--;
  69. if (!tmp->ref)
  70. {
  71. tmp->name->release();
  72. #ifndef _DEBUG
  73. FreeLibrary(tmp->handle);
  74. #endif
  75. delete tmp;
  76. dlls->remove(i);
  77. }
  78. cs.unlock();
  79. return;
  80. }
  81. }
  82. cs.unlock();
  83. }