DLLRegister.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. FreeLibrary(tmp->handle);
  22. }
  23. delete tmp;
  24. }
  25. dlls->release();
  26. cs.unlock();
  27. }
  28. // non-constant
  29. HINSTANCE DLLRegister::loadDLL(const char* name, const char* pfad)
  30. {
  31. cs.lock();
  32. int anz = dlls->getEntryCount();
  33. for (int i = 0; i < anz; i++)
  34. {
  35. DLLFile* tmp = dlls->get(i);
  36. if (tmp && tmp->name->isEqual(name))
  37. {
  38. tmp->ref++;
  39. cs.unlock();
  40. return tmp->handle;
  41. }
  42. }
  43. HINSTANCE h = LoadLibrary(pfad);
  44. if (!h)
  45. {
  46. cs.unlock();
  47. return 0;
  48. }
  49. DLLFile* dll = new DLLFile();
  50. dll->name = new Text(name);
  51. dll->handle = h;
  52. dll->ref = 1;
  53. dlls->add(dll);
  54. cs.unlock();
  55. return h;
  56. }
  57. void DLLRegister::releaseDLL(const char* name)
  58. {
  59. cs.lock();
  60. int anz = dlls->getEntryCount();
  61. for (int i = 0; i < anz; i++)
  62. {
  63. DLLFile* tmp = dlls->get(i);
  64. if (tmp && tmp->name->isEqual(name))
  65. {
  66. tmp->ref--;
  67. if (!tmp->ref)
  68. {
  69. tmp->name->release();
  70. #ifndef _DEBUG
  71. FreeLibrary(tmp->handle);
  72. #endif
  73. delete tmp;
  74. dlls->remove(i);
  75. }
  76. cs.unlock();
  77. return;
  78. }
  79. }
  80. cs.unlock();
  81. }