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.

310 lines
8.1 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1995.
  5. //
  6. // File: libmain.cxx
  7. //
  8. // Contents: LibMain for nds.dll
  9. //
  10. // Functions: LibMain, DllGetClassObject
  11. //
  12. // History: 25-Oct-94 KrishnaG Created.
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "ldapc.hxx"
  16. #pragma hdrstop
  17. BOOL fInitializeCritSect = FALSE;
  18. #if DBG==1
  19. #include "formtrck.hxx"
  20. extern "C" {
  21. #include "caiheap.h"
  22. }
  23. extern CRITICAL_SECTION g_csDP; // for debugprint
  24. extern CRITICAL_SECTION g_csOT; // for otracker
  25. extern CRITICAL_SECTION g_csMem; // for MemAlloc
  26. #endif
  27. extern CRITICAL_SECTION g_DomainDnsCache;
  28. extern CRITICAL_SECTION BindCacheCritSect ;
  29. extern CRITICAL_SECTION g_SchemaCritSect;
  30. extern CRITICAL_SECTION g_DefaultSchemaCritSect;
  31. extern CRITICAL_SECTION g_SubSchemaCritSect;
  32. //
  33. // External references to handles for libs we load dynamically.
  34. //
  35. extern HANDLE g_hDllNetApi32;
  36. extern HANDLE g_hDllSecur32;
  37. extern DWORD LsaDeregisterLogonProcessWrapper(
  38. IN HANDLE LsaHandle
  39. );
  40. //
  41. // Variables needed for localized strings.
  42. //
  43. WCHAR g_szNT_Authority[100];
  44. BOOL g_fStringsLoaded = FALSE;
  45. HINSTANCE g_hInst = NULL;
  46. //
  47. // LSA handle (needed by GetUserDomainFlatName)
  48. //
  49. extern HANDLE g_hLsa;
  50. //---------------------------------------------------------------------------
  51. // ADs debug print, mem leak and object tracking-related stuff
  52. //---------------------------------------------------------------------------
  53. DECLARE_INFOLEVEL(ADs)
  54. //+---------------------------------------------------------------------------
  55. //
  56. // Function: ShutDown
  57. //
  58. // Synopsis: Function to handle printing out heap debugging display
  59. //
  60. //----------------------------------------------------------------------------
  61. inline VOID ShutDown()
  62. {
  63. #if DBG==1
  64. #ifndef MSVC
  65. DUMP_TRACKING_INFO_DELETE();
  66. AllocArenaDump( NULL );
  67. DeleteCriticalSection(&g_csOT);
  68. #endif // ifndef MSVC
  69. DeleteCriticalSection(&g_csDP);
  70. #endif
  71. }
  72. //+---------------------------------------------------------------
  73. //
  74. // Function: LibMain
  75. //
  76. // Synopsis: Standard DLL initialization entrypoint
  77. //
  78. //---------------------------------------------------------------
  79. EXTERN_C BOOL __cdecl
  80. LibMain(HINSTANCE hInst, ULONG ulReason, LPVOID pvReserved)
  81. {
  82. HRESULT hr;
  83. DWORD dwCritSectIniStage = 0;
  84. switch (ulReason)
  85. {
  86. case DLL_PROCESS_ATTACH:
  87. //
  88. // Need to handle case of crit sect init failing.
  89. //
  90. __try {
  91. InitADsMem() ;
  92. //
  93. // Initialize the error records
  94. //
  95. memset(&ADsErrorRecList, 0, sizeof(ERROR_RECORD));
  96. InitializeCriticalSection(&ADsErrorRecCritSec);
  97. dwCritSectIniStage = 1;
  98. BindCacheInit() ;
  99. InitializeCriticalSection(&BindCacheCritSect) ;
  100. dwCritSectIniStage = 2;
  101. InitializeCriticalSection(&g_SchemaCritSect);
  102. dwCritSectIniStage = 3;
  103. InitializeCriticalSection(&g_DefaultSchemaCritSect);
  104. dwCritSectIniStage = 4;
  105. InitializeCriticalSection(&g_SubSchemaCritSect);
  106. dwCritSectIniStage = 5;
  107. InitializeCriticalSection(&g_DomainDnsCache);
  108. dwCritSectIniStage = 6;
  109. g_hInst = hInst;
  110. #if DBG==1
  111. InitializeCriticalSection(&g_csDP); // Used by ADsDebug
  112. dwCritSectIniStage = 7;
  113. InitializeCriticalSection(&ADsMemCritSect) ;
  114. dwCritSectIniStage = 8;
  115. #ifndef MSVC
  116. InitializeCriticalSection(&g_csOT); // Used by Object Tracker
  117. dwCritSectIniStage = 9;
  118. InitializeCriticalSection(&g_csMem); // Used by Object Tracker
  119. dwCritSectIniStage = 10;
  120. #endif
  121. #endif
  122. fInitializeCritSect = TRUE;
  123. }
  124. __except (EXCEPTION_EXECUTE_HANDLER) {
  125. //
  126. // Critical faliure
  127. //
  128. switch(dwCritSectIniStage)
  129. {
  130. #if DBG==1
  131. #ifndef MSVC
  132. case 10:
  133. DeleteCriticalSection(&g_csMem);
  134. case 9:
  135. DeleteCriticalSection(&g_csOT);
  136. #endif
  137. case 8:
  138. DeleteCriticalSection(&ADsMemCritSect);
  139. case 7:
  140. DeleteCriticalSection(&g_csDP);
  141. #endif
  142. case 6:
  143. DeleteCriticalSection(&g_DomainDnsCache);
  144. case 5:
  145. DeleteCriticalSection(&g_SubSchemaCritSect);
  146. case 4:
  147. DeleteCriticalSection(&g_DefaultSchemaCritSect);
  148. case 3:
  149. DeleteCriticalSection(&g_SchemaCritSect);
  150. case 2:
  151. DeleteCriticalSection(&BindCacheCritSect) ;
  152. case 1:
  153. DeleteCriticalSection(&ADsErrorRecCritSec);
  154. }
  155. return FALSE;
  156. }
  157. //
  158. // Time to load localized strings if applicable.
  159. //
  160. if (!g_fStringsLoaded) {
  161. //
  162. // Load NT AUthority
  163. //
  164. if (!LoadStringW(
  165. g_hInst,
  166. LDAPC_NT_AUTHORITY,
  167. g_szNT_Authority,
  168. sizeof( g_szNT_Authority ) / sizeof( WCHAR )
  169. )
  170. ) {
  171. wcscpy(g_szNT_Authority, L"NT AUTHORITY");
  172. }
  173. g_fStringsLoaded = TRUE;
  174. }
  175. break;
  176. case DLL_PROCESS_DETACH:
  177. ADsFreeAllErrorRecords();
  178. SchemaCleanup();
  179. BindCacheCleanup();
  180. #if (!defined WIN95)
  181. if (g_hLsa != INVALID_HANDLE_VALUE) {
  182. LsaDeregisterLogonProcessWrapper(g_hLsa);
  183. }
  184. #endif
  185. if (g_hDllNetApi32) {
  186. FreeLibrary((HMODULE)g_hDllNetApi32);
  187. g_hDllNetApi32 = NULL;
  188. }
  189. if (g_hDllSecur32) {
  190. FreeLibrary((HMODULE)g_hDllSecur32);
  191. g_hDllSecur32 = NULL;
  192. }
  193. if(fInitializeCritSect)
  194. {
  195. DeleteCriticalSection(&ADsErrorRecCritSec);
  196. DeleteCriticalSection(&g_DomainDnsCache);
  197. //
  198. // Delete critsects initialized in SchemaInit
  199. //
  200. DeleteCriticalSection(&g_SchemaCritSect);
  201. DeleteCriticalSection(&g_SubSchemaCritSect);
  202. DeleteCriticalSection(&g_DefaultSchemaCritSect);
  203. //
  204. // Delte the critical section initialized in BindCacheInit
  205. //
  206. DeleteCriticalSection(&BindCacheCritSect) ;
  207. #if DBG==1
  208. #ifndef MSVC
  209. DeleteCriticalSection(&g_csOT); // Used by Object Tracker
  210. DeleteCriticalSection(&g_csMem); // Used by Object Tracker
  211. #endif
  212. DeleteCriticalSection(&g_csDP); // Used by ADsDebug
  213. #endif
  214. }
  215. if (gpszServerName) {
  216. FreeADsStr(gpszServerName);
  217. gpszServerName = NULL;
  218. }
  219. if (gpszDomainName) {
  220. FreeADsStr(gpszDomainName);
  221. gpszDomainName = NULL;
  222. }
  223. #if DBG==1
  224. DumpMemoryTracker();
  225. #endif
  226. #if DBG==1
  227. if(fInitializeCritSect)
  228. {
  229. DeleteCriticalSection(&ADsMemCritSect);
  230. }
  231. #endif
  232. break;
  233. case DLL_THREAD_DETACH:
  234. ADsFreeThreadErrorRecords();
  235. break;
  236. default:
  237. break;
  238. }
  239. return TRUE;
  240. }
  241. //+---------------------------------------------------------------------------
  242. //
  243. // Function: DllMain
  244. //
  245. // Synopsis: entry point for NT - post .546
  246. //
  247. //----------------------------------------------------------------------------
  248. BOOL
  249. DllMain(HANDLE hDll, DWORD dwReason, LPVOID lpReserved)
  250. {
  251. return LibMain((HINSTANCE)hDll, dwReason, lpReserved);
  252. }