Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

304 lines
11 KiB

  1. // Ruler
  2. // 1 2 3 4 5 6 7 8
  3. //345678901234567890123456789012345678901234567890123456789012345678901234567890
  4. /********************************************************************/
  5. /* */
  6. /* The standard layout. */
  7. /* */
  8. /* The standard layout for 'cpp' files in this code is as */
  9. /* follows: */
  10. /* */
  11. /* 1. Include files. */
  12. /* 2. Constants local to the class. */
  13. /* 3. Data structures local to the class. */
  14. /* 4. Data initializations. */
  15. /* 5. Static functions. */
  16. /* 6. Class functions. */
  17. /* */
  18. /* The constructor is typically the first function, class */
  19. /* member functions appear in alphabetical order with the */
  20. /* destructor appearing at the end of the file. Any section */
  21. /* or function this is not required is simply omitted. */
  22. /* */
  23. /********************************************************************/
  24. #include "LibraryPCH.hpp"
  25. #include "Dll.hpp"
  26. /********************************************************************/
  27. /* */
  28. /* Constants local to the class. */
  29. /* */
  30. /* The constants supplied here control various debug settings. */
  31. /* */
  32. /********************************************************************/
  33. CONST SBIT32 MaxDebugFileName = 128;
  34. /********************************************************************/
  35. /* */
  36. /* Static member initialization. */
  37. /* */
  38. /* Static member initialization sets the initial value for all */
  39. /* static members. */
  40. /* */
  41. /********************************************************************/
  42. #pragma init_seg(lib)
  43. LIST DLL::ActiveClasses;
  44. SPINLOCK DLL::Spinlock;
  45. /********************************************************************/
  46. /* */
  47. /* Class constructor. */
  48. /* */
  49. /* Create a new entry so we can notify the class when a DLL */
  50. /* event occurs. */
  51. /* */
  52. /********************************************************************/
  53. DLL::DLL( FUNCTION NewFunction,VOID *NewParameter )
  54. {
  55. //
  56. // Setup class values.
  57. //
  58. Function = NewFunction;
  59. Parameter = NewParameter;
  60. //
  61. // Claim a lock to ensure the list does
  62. // not become corrupt.
  63. //
  64. Spinlock.ClaimLock();
  65. //
  66. // Add the current instance into the active
  67. // list so it will be notified of all future
  68. // events.
  69. //
  70. Insert( & ActiveClasses );
  71. //
  72. // Release the lock.
  73. //
  74. Spinlock.ReleaseLock();
  75. }
  76. /********************************************************************/
  77. /* */
  78. /* Standard DLL processing. */
  79. /* */
  80. /* Automatically delete the private per thread heap on thread */
  81. /* exit when Rockall is compiled as a DLL. */
  82. /* */
  83. /********************************************************************/
  84. BOOL WINAPI DllMain
  85. (
  86. HINSTANCE Module,
  87. DWORD Reason,
  88. LPVOID Reserved
  89. )
  90. {
  91. REGISTER DLL *Current;
  92. //
  93. // Claim a lock to ensure the list does
  94. // not become corrupt.
  95. //
  96. DLL::ClaimLock();
  97. //
  98. // When Rockall is built for a DLL we use the
  99. // detach notification to delete the private
  100. // per thread heap.
  101. //
  102. switch( Reason )
  103. {
  104. case DLL_PROCESS_ATTACH:
  105. {
  106. #ifdef ENABLE_DEBUG_FILE
  107. AUTO CHAR FileName[ MaxDebugFileName ];
  108. //
  109. // We will register the DLL name with the
  110. // debug trace code just in case any messages
  111. // are generated.
  112. //
  113. if ( GetModuleFileName( Module,FileName,MaxDebugFileName ) != 0 )
  114. { DebugFileName( FileName ); }
  115. #endif
  116. //
  117. // Notify all interested parties that
  118. // a process has attached.
  119. //
  120. for
  121. (
  122. Current = ((DLL*) DLL::GetActiveClasses());
  123. Current != NULL;
  124. Current = ((DLL*) Current -> Next())
  125. )
  126. { Current -> ProcessAttach(); }
  127. break;
  128. }
  129. case DLL_THREAD_ATTACH:
  130. {
  131. //
  132. // Notify all interested parties that
  133. // a thread has attached.
  134. //
  135. for
  136. (
  137. Current = ((DLL*) DLL::GetActiveClasses());
  138. Current != NULL;
  139. Current = ((DLL*) Current -> Next())
  140. )
  141. { Current -> ThreadAttach(); }
  142. break;
  143. }
  144. case DLL_THREAD_DETACH:
  145. {
  146. //
  147. // Notify all interested parties that
  148. // a thread has dettached.
  149. //
  150. for
  151. (
  152. Current = ((DLL*) DLL::GetActiveClasses());
  153. Current != NULL;
  154. Current = ((DLL*) Current -> Next())
  155. )
  156. { Current -> ThreadDetach(); }
  157. break;
  158. }
  159. case DLL_PROCESS_DETACH:
  160. {
  161. //
  162. // Notify all interested parties that
  163. // a process has dettached.
  164. //
  165. for
  166. (
  167. Current = ((DLL*) DLL::GetActiveClasses());
  168. Current != NULL;
  169. Current = ((DLL*) Current -> Next())
  170. )
  171. { Current -> ProcessDetach(); }
  172. break;
  173. }
  174. }
  175. //
  176. // Release the lock.
  177. //
  178. DLL::ReleaseLock();
  179. return TRUE;
  180. }
  181. /********************************************************************/
  182. /* */
  183. /* Process attach callback. */
  184. /* */
  185. /* When a process attach occurs the following callback is */
  186. /* executed. */
  187. /* */
  188. /********************************************************************/
  189. VOID DLL::ProcessAttach( VOID )
  190. {
  191. if ( Function != NULL )
  192. { Function( Parameter,DLL_PROCESS_ATTACH ); }
  193. }
  194. /********************************************************************/
  195. /* */
  196. /* Thread attach callback. */
  197. /* */
  198. /* When a thread attach occurs the following callback is */
  199. /* executed. */
  200. /* */
  201. /********************************************************************/
  202. VOID DLL::ThreadAttach( VOID )
  203. {
  204. if ( Function != NULL )
  205. { Function( Parameter,DLL_THREAD_ATTACH ); }
  206. }
  207. /********************************************************************/
  208. /* */
  209. /* Thread dettach callback. */
  210. /* */
  211. /* When a thread dettach occurs the following callback is */
  212. /* executed. */
  213. /* */
  214. /********************************************************************/
  215. VOID DLL::ThreadDetach( VOID )
  216. {
  217. if ( Function != NULL )
  218. { Function( Parameter,DLL_THREAD_DETACH ); }
  219. }
  220. /********************************************************************/
  221. /* */
  222. /* Process dettach callback. */
  223. /* */
  224. /* When a process dettach occurs the following callback is */
  225. /* executed. */
  226. /* */
  227. /********************************************************************/
  228. VOID DLL::ProcessDetach( VOID )
  229. {
  230. if ( Function != NULL )
  231. { Function( Parameter,DLL_PROCESS_DETACH ); }
  232. }
  233. /********************************************************************/
  234. /* */
  235. /* Class destructor. */
  236. /* */
  237. /* Destory a DLL. This call is not thread safe and should */
  238. /* only be made in a single thread environment. */
  239. /* */
  240. /********************************************************************/
  241. DLL::~DLL( VOID )
  242. {
  243. //
  244. // Claim a lock to ensure the list does
  245. // not become corrupt.
  246. //
  247. Spinlock.ClaimLock();
  248. //
  249. // Delete the current instance from the active
  250. // list so it will not be notified of future
  251. // events.
  252. //
  253. Delete( & ActiveClasses );
  254. //
  255. // Release the lock.
  256. //
  257. Spinlock.ReleaseLock();
  258. //
  259. // Delete class values.
  260. //
  261. Parameter = NULL;
  262. Function = NULL;
  263. }