DLLDateien.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include "DLLDateien.h"
  2. // Inhalt der DLLDateien Klasse aus DLLDateien.h
  3. // Konstruktor
  4. DLLDateien::DLLDateien()
  5. {
  6. dlls = new Array< DLLDatei* >();
  7. ref = 1;
  8. }
  9. // Destruktor
  10. DLLDateien::~DLLDateien()
  11. {
  12. cs.lock();
  13. int anz = dlls->getEintragAnzahl();
  14. for( int i = 0; i < anz; i++ )
  15. {
  16. DLLDatei *tmp = dlls->get( i );
  17. if( tmp )
  18. {
  19. tmp->name->release();
  20. FreeLibrary( tmp->handle );
  21. }
  22. delete tmp;
  23. }
  24. dlls->release();
  25. cs.unlock();
  26. }
  27. // nicht constant
  28. HINSTANCE DLLDateien::ladeDLL( char *name, char *pfad )
  29. {
  30. cs.lock();
  31. int anz = dlls->getEintragAnzahl();
  32. for( int i = 0; i < anz; i++ )
  33. {
  34. DLLDatei *tmp = dlls->get( i );
  35. if( tmp && tmp->name->istGleich( name ) )
  36. {
  37. tmp->ref++;
  38. cs.unlock();
  39. return tmp->handle;
  40. }
  41. }
  42. HINSTANCE h = LoadLibrary( pfad );
  43. if( !h )
  44. return 0;
  45. DLLDatei *dll = new DLLDatei();
  46. dll->name = new Text( name );
  47. dll->handle = h;
  48. dll->ref = 1;
  49. dlls->add( dll );
  50. cs.unlock();
  51. return h;
  52. }
  53. void DLLDateien::releaseDLL( char *name )
  54. {
  55. cs.lock();
  56. int anz = dlls->getEintragAnzahl();
  57. for( int i = 0; i < anz; i++ )
  58. {
  59. DLLDatei *tmp = dlls->get( i );
  60. if( tmp && tmp->name->istGleich( name ) )
  61. {
  62. tmp->ref--;
  63. if( !tmp->ref )
  64. {
  65. tmp->name->release();
  66. FreeLibrary( tmp->handle );
  67. delete tmp;
  68. dlls->remove( i );
  69. }
  70. cs.unlock();
  71. return;
  72. }
  73. }
  74. cs.unlock();
  75. }
  76. // Reference Counting
  77. DLLDateien *DLLDateien::getThis()
  78. {
  79. ref++;
  80. return this;
  81. }
  82. DLLDateien *DLLDateien::release()
  83. {
  84. ref--;
  85. if( !ref )
  86. delete this;
  87. return 0;
  88. }