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.

471 lines
13 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 "ldap.hxx"
  16. #pragma hdrstop
  17. BOOL fInitializeCritSect = FALSE;
  18. HINSTANCE g_hInst = NULL;
  19. extern HMODULE g_hActiveDs;
  20. typedef DWORD (*PF_DllGetClassObject) (
  21. REFCLSID clsid,
  22. REFIID iid,
  23. LPVOID FAR* ppverved
  24. );
  25. //---------------------------------------------------------------------------
  26. // ADs debug print, mem leak and object tracking-related stuff
  27. //---------------------------------------------------------------------------
  28. DECLARE_INFOLEVEL(ADs)
  29. //+---------------------------------------------------------------------------
  30. //
  31. // Function: ShutDown
  32. //
  33. // Synopsis: Function to handle printing out heap debugging display
  34. //
  35. //----------------------------------------------------------------------------
  36. inline VOID ShutDown()
  37. {
  38. #if DBG==1
  39. #ifndef MSVC
  40. DUMP_TRACKING_INFO_DELETE();
  41. DeleteCriticalSection(&g_csOT);
  42. #endif // ifndef MSVC
  43. DeleteCriticalSection(&g_csDP);
  44. #endif
  45. }
  46. extern "C" DWORD heapInfoLevel;
  47. extern "C" DWORD OtInfoLevel;
  48. extern "C" DWORD ADsInfoLevel;
  49. extern CRITICAL_SECTION g_RootDSECritSect;
  50. extern CRITICAL_SECTION g_ExtCritSect;
  51. extern CRITICAL_SECTION g_TypeInfoCritSect;
  52. extern CRITICAL_SECTION g_DispTypeInfoCritSect;
  53. extern CRITICAL_SECTION g_SystemAPICritSect;
  54. CRITICAL_SECTION g_LockCritSect;
  55. //+---------------------------------------------------------------------------
  56. //
  57. // Function: GetINIHeapInfoLevel
  58. //
  59. // Synopsis: Gets various infolevel values from win.ini
  60. //
  61. //----------------------------------------------------------------------------
  62. inline VOID GetINIHeapInfoLevel()
  63. {
  64. #if DBG==1
  65. const INT MAXINFOLEN=11;
  66. TCHAR awcs[MAXINFOLEN];
  67. #ifndef MSVC
  68. if (GetProfileString(TEXT("LDAP"),TEXT("heapInfoLevel"), TEXT("00000003"), awcs,MAXINFOLEN))
  69. heapInfoLevel = _tcstoul(awcs, NULL, 16);
  70. if (GetProfileString(TEXT("LDAP"),TEXT("Ot"), TEXT("00000003"), awcs, MAXINFOLEN))
  71. OtInfoLevel = _tcstoul(awcs, NULL, 16);
  72. #endif // MSVC
  73. if (GetProfileString(TEXT("LDAP"),TEXT("ADsInfoLevel"), TEXT("00000003"), awcs,MAXINFOLEN))
  74. ADsInfoLevel = _tcstoul(awcs, NULL, 16);
  75. #endif
  76. }
  77. // Globals
  78. ULONG g_ulObjCount = 0; // Number of objects alive in oleds.dll
  79. ULONG g_ulLocks = 0; // Number of Provider objects alive
  80. CLDAPProviderCF g_cfProvider;
  81. CLDAPNamespaceCF g_cfNamespace;
  82. CADSystemInfoCF g_cfADSystemInfo;
  83. CNameTranslateCF g_cfNameTranslate;
  84. //+------------------------------------------------------------------------
  85. //
  86. // oleds class factories
  87. //
  88. //-------------------------------------------------------------------------
  89. struct CLSCACHE
  90. {
  91. const CLSID * pclsid;
  92. IClassFactory * pCF;
  93. };
  94. CLSCACHE g_aclscache[] =
  95. {
  96. &CLSID_LDAPProvider, &g_cfProvider,
  97. &CLSID_LDAPNamespace, &g_cfNamespace,
  98. &CLSID_NameTranslate, &g_cfNameTranslate,
  99. &CLSID_ADSystemInfo, &g_cfADSystemInfo
  100. };
  101. extern PCLASS_ENTRY gpClassHead;
  102. //+---------------------------------------------------------------
  103. //
  104. // Function: DllGetClassObject
  105. //
  106. // Synopsis: Standard DLL entrypoint for locating class factories
  107. //
  108. //----------------------------------------------------------------
  109. STDAPI
  110. DllGetClassObject(REFCLSID clsid, REFIID iid, LPVOID FAR* ppv)
  111. {
  112. HRESULT hr = E_NOINTERFACE;
  113. size_t i;
  114. HKEY hKey = NULL;
  115. HINSTANCE hDll = NULL ;
  116. if (ppv)
  117. *ppv = NULL;
  118. for (i = 0; i < ARRAY_SIZE(g_aclscache); i++)
  119. {
  120. if (IsEqualCLSID(clsid, *g_aclscache[i].pclsid))
  121. {
  122. hr = g_aclscache[i].pCF->QueryInterface(iid, ppv);
  123. RRETURN(hr);
  124. }
  125. }
  126. //
  127. // This workaround is for the special case where an old version of ADSI
  128. // is installed in the system. Installing that will overwrite the registry
  129. // with the old setting for pathcracker. The pathcracker object used to live
  130. // on adsldp.
  131. // The following code redirects the call to the DllGetClassObject in
  132. // activeds if the Pathname object is being requested. It also fixes the
  133. // registry to point to the correct DLL.
  134. //
  135. if (IsEqualCLSID(clsid, CLSID_Pathname)) {
  136. PF_DllGetClassObject pfDllGetClassObject= NULL ;
  137. WCHAR szPathDescriptor[] = L"ADs Pathname Object";
  138. WCHAR szDllName[] = L"activeds.dll";
  139. DWORD WinError;
  140. if (!(hDll = LoadLibraryHelper(szDllName))) {
  141. BAIL_ON_FAILURE(hr = HRESULT_FROM_WIN32(GetLastError()));
  142. }
  143. if (!(pfDllGetClassObject = (PF_DllGetClassObject)GetProcAddress(hDll, "DllGetClassObject"))) {
  144. BAIL_ON_FAILURE(hr = HRESULT_FROM_WIN32(GetLastError()));
  145. }
  146. hr = (*pfDllGetClassObject)(clsid,
  147. iid,
  148. ppv);
  149. BAIL_ON_FAILURE(hr);
  150. //
  151. // Setting the general description
  152. // Even if any of the operations below fails, we'll just bail with the
  153. // hr from DllGetClassObject.
  154. //
  155. WinError = RegOpenKeyEx(HKEY_CLASSES_ROOT,
  156. L"CLSID\\{080d0d78-f421-11d0-a36e-00c04fb950dc}",
  157. NULL,
  158. KEY_ALL_ACCESS,
  159. &hKey);
  160. if (WinError != ERROR_SUCCESS) {
  161. goto error;
  162. }
  163. WinError = RegSetValueEx(hKey,
  164. NULL,
  165. 0,
  166. REG_SZ,
  167. (BYTE *)szPathDescriptor,
  168. (wcslen(szPathDescriptor)+1) * sizeof(WCHAR));
  169. if (WinError != ERROR_SUCCESS) {
  170. goto error;
  171. }
  172. RegCloseKey(hKey);
  173. hKey = NULL;
  174. //
  175. // Setting the inprocserver
  176. //
  177. WinError = RegOpenKeyEx(HKEY_CLASSES_ROOT,
  178. L"CLSID\\{080d0d78-f421-11d0-a36e-00c04fb950dc}\\InprocServer32",
  179. NULL,
  180. KEY_ALL_ACCESS,
  181. &hKey);
  182. if (WinError != ERROR_SUCCESS) {
  183. goto error;
  184. }
  185. WinError = RegSetValueEx(hKey,
  186. NULL,
  187. 0,
  188. REG_SZ,
  189. (BYTE *)szDllName,
  190. (wcslen(szDllName)+1) * sizeof(WCHAR));
  191. if (WinError != ERROR_SUCCESS) {
  192. goto error;
  193. }
  194. }
  195. //
  196. // Add Debugging Code to indicate that the oleds.DllGetClassObject has been called with an unknown CLSID.
  197. //
  198. error:
  199. if (hDll) {
  200. FreeLibrary(hDll);
  201. }
  202. if (hKey) {
  203. RegCloseKey(hKey);
  204. }
  205. return hr;
  206. }
  207. //+---------------------------------------------------------------
  208. //
  209. // Function: DllCanUnloadNow
  210. //
  211. // Synopsis: Standard DLL entrypoint to determine if DLL can be unloaded
  212. //
  213. //---------------------------------------------------------------
  214. STDAPI
  215. DllCanUnloadNow(void)
  216. {
  217. HRESULT hr;
  218. hr = S_FALSE;
  219. //
  220. // Both the ldap and utils\cdispmgr count need to be 0
  221. //
  222. if (AggregatorDllCanUnload() && DllReadyToUnload()) {
  223. EnterCriticalSection(&g_LockCritSect);
  224. if(0 == g_ulLocks)
  225. {
  226. hr = S_OK;
  227. }
  228. LeaveCriticalSection(&g_LockCritSect);
  229. }
  230. return hr;
  231. }
  232. //+---------------------------------------------------------------
  233. //
  234. // Function: LibMain
  235. //
  236. // Synopsis: Standard DLL initialization entrypoint
  237. //
  238. //---------------------------------------------------------------
  239. EXTERN_C BOOL __cdecl
  240. LibMain(HINSTANCE hInst, ULONG ulReason, LPVOID pvReserved)
  241. {
  242. HRESULT hr;
  243. DWORD dwCritSectIniStage = 0;
  244. switch (ulReason)
  245. {
  246. case DLL_PROCESS_ATTACH:
  247. //
  248. // In try to catch possibily of init crit sects failing.
  249. //
  250. __try {
  251. DisableThreadLibraryCalls(hInst);
  252. g_hInst = hInst;
  253. g_hActiveDs = GetModuleHandle(TEXT("activeds.dll"));
  254. // Maybe we should check the handle.
  255. InitializeCriticalSection(&g_RootDSECritSect);
  256. dwCritSectIniStage = 1;
  257. InitializeCriticalSection(&g_ExtCritSect);
  258. dwCritSectIniStage = 2;
  259. InitializeCriticalSection(&g_TypeInfoCritSect);
  260. dwCritSectIniStage = 3;
  261. InitializeCriticalSection(&g_DispTypeInfoCritSect);
  262. dwCritSectIniStage = 4;
  263. InitializeCriticalSection(&g_csLoadLibsCritSect);
  264. dwCritSectIniStage = 5;
  265. InitializeCriticalSection(&g_LockCritSect);
  266. dwCritSectIniStage = 6;
  267. InitializeCriticalSection(&g_SystemAPICritSect);
  268. dwCritSectIniStage = 7;
  269. #if DBG==1
  270. InitializeCriticalSection(&g_csDP);
  271. dwCritSectIniStage = 8;
  272. #ifndef MSVC
  273. InitializeCriticalSection(&g_csOT);
  274. dwCritSectIniStage = 9;
  275. InitializeCriticalSection(&g_csMem);
  276. dwCritSectIniStage = 10;
  277. #endif
  278. #endif
  279. fInitializeCritSect = TRUE;
  280. }
  281. __except (EXCEPTION_EXECUTE_HANDLER) {
  282. //
  283. // Something went wrong
  284. //
  285. switch(dwCritSectIniStage)
  286. {
  287. #if DBG==1
  288. #ifndef MSVC
  289. case 10:
  290. DeleteCriticalSection(&g_csMem);
  291. case 9:
  292. DeleteCriticalSection(&g_csOT);
  293. #endif
  294. case 8:
  295. DeleteCriticalSection(&g_csDP);
  296. #endif
  297. case 7:
  298. DeleteCriticalSection(&g_SystemAPICritSect);
  299. case 6:
  300. DeleteCriticalSection(&g_LockCritSect);
  301. case 5:
  302. DeleteCriticalSection(&g_csLoadLibsCritSect);
  303. case 4:
  304. DeleteCriticalSection(&g_DispTypeInfoCritSect);
  305. case 3:
  306. DeleteCriticalSection(&g_TypeInfoCritSect);
  307. case 2:
  308. DeleteCriticalSection(&g_ExtCritSect);
  309. case 1:
  310. DeleteCriticalSection(&g_RootDSECritSect);
  311. }
  312. return FALSE;
  313. }
  314. break;
  315. case DLL_PROCESS_DETACH:
  316. //
  317. // free global list of class entries for 3rd party ext
  318. //
  319. if (gpClassHead) {
  320. FreeClassesList(gpClassHead);
  321. }
  322. if (gpszStickyServerName) {
  323. FreeADsStr(gpszStickyServerName);
  324. gpszStickyServerName = NULL;
  325. }
  326. if (gpszStickyDomainName) {
  327. FreeADsStr(gpszStickyDomainName);
  328. gpszStickyDomainName = NULL;
  329. }
  330. //
  331. // Good idea to delete all the critical sections
  332. //
  333. if(fInitializeCritSect)
  334. {
  335. FreeServerType();
  336. #if DBG==1
  337. #ifndef MSVC
  338. DeleteCriticalSection(&g_csOT);
  339. DeleteCriticalSection(&g_csMem);
  340. #endif
  341. DeleteCriticalSection(&g_csDP);
  342. #endif
  343. DeleteCriticalSection(&g_RootDSECritSect);
  344. DeleteCriticalSection(&g_ExtCritSect);
  345. DeleteCriticalSection(&g_TypeInfoCritSect);
  346. DeleteCriticalSection(&g_DispTypeInfoCritSect);
  347. DeleteCriticalSection(&g_csLoadLibsCritSect);
  348. DeleteCriticalSection(&g_LockCritSect);
  349. DeleteCriticalSection(&g_SystemAPICritSect);
  350. }
  351. //
  352. // Should be ok to free the dynamically loaded libs.
  353. //
  354. if (g_hDllNtdsapi) {
  355. FreeLibrary((HMODULE) g_hDllNtdsapi);
  356. g_hDllNtdsapi = NULL;
  357. }
  358. if (g_hDllSecur32) {
  359. FreeLibrary((HMODULE) g_hDllSecur32);
  360. }
  361. break;
  362. default:
  363. break;
  364. }
  365. return TRUE;
  366. }
  367. //+---------------------------------------------------------------------------
  368. //
  369. // Function: DllMain
  370. //
  371. // Synopsis: entry point for NT - post .546
  372. //
  373. //----------------------------------------------------------------------------
  374. BOOL
  375. DllMain(HANDLE hDll, DWORD dwReason, LPVOID lpReserved)
  376. {
  377. return LibMain((HINSTANCE)hDll, dwReason, lpReserved);
  378. }