DLLDateien.cpp 1.7 KB

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