Source code of Windows XP (NT5)
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.

495 lines
12 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1995.
  5. //
  6. // File: libmain.cxx
  7. //
  8. // Contents: LibMain for oleds.dll
  9. //
  10. // Functions: LibMain, DllGetClassObject
  11. //
  12. // History: 25-Oct-94 KrishnaG Created.
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "winnt.hxx"
  16. #pragma hdrstop
  17. HINSTANCE g_hInst = NULL;
  18. extern HMODULE g_hActiveDs;
  19. WCHAR * szWinNTPrefix = L"@WinNT!";
  20. HANDLE FpnwLoadLibSemaphore = NULL;
  21. //
  22. // Strings that are loaded depending on locality
  23. //
  24. WCHAR g_szBuiltin[100];
  25. WCHAR g_szNT_Authority[100];
  26. WCHAR g_szEveryone[100];
  27. BOOL g_fStringsLoaded = FALSE;
  28. //
  29. // 3rd party extension
  30. //
  31. extern PCLASS_ENTRY gpClassHead;
  32. extern CRITICAL_SECTION g_ExtCritSect;
  33. extern CRITICAL_SECTION g_TypeInfoCritSect;
  34. //
  35. // Disabled as it causes link warnings
  36. // extern CRITICAL_SECTION g_DispTypeInfoCritSect;
  37. //
  38. //---------------------------------------------------------------------------
  39. // ADs debug print, mem leak and object tracking-related stuff
  40. //---------------------------------------------------------------------------
  41. DECLARE_INFOLEVEL(ADs)
  42. //+---------------------------------------------------------------------------
  43. //
  44. // Function: ShutDown
  45. //
  46. // Synopsis: Function to handle printing out heap debugging display
  47. //
  48. //----------------------------------------------------------------------------
  49. inline VOID ShutDown()
  50. {
  51. #if DBG==1
  52. #ifndef MSVC
  53. DUMP_TRACKING_INFO_DELETE();
  54. DeleteCriticalSection(&g_csOT);
  55. #endif // ifndef MSVC
  56. DeleteCriticalSection(&g_csDP);
  57. #endif
  58. }
  59. extern "C" DWORD heapInfoLevel;
  60. extern "C" DWORD OtInfoLevel;
  61. extern "C" DWORD ADsInfoLevel;
  62. //+---------------------------------------------------------------------------
  63. //
  64. // Function: GetINIHeapInfoLevel
  65. //
  66. // Synopsis: Gets various infolevel values from win.ini
  67. //
  68. //----------------------------------------------------------------------------
  69. inline VOID GetINIHeapInfoLevel()
  70. {
  71. #if DBG==1
  72. const INT MAXINFOLEN=11;
  73. WCHAR awcs[MAXINFOLEN];
  74. #ifndef MSVC
  75. if (GetProfileString(L"winnt",L"heapInfoLevel", L"00000003", awcs,MAXINFOLEN))
  76. heapInfoLevel = wcstoul(awcs, NULL, 16);
  77. if (GetProfileString(L"winnt",L"Ot", L"00000003", awcs, MAXINFOLEN))
  78. OtInfoLevel = wcstoul(awcs, NULL, 16);
  79. #endif // MSVC
  80. if (GetProfileString(L"winnt",L"ADsInfoLevel", L"00000003", awcs,MAXINFOLEN))
  81. ADsInfoLevel = wcstoul(awcs, NULL, 16);
  82. #endif
  83. }
  84. // Globals
  85. ULONG g_ulObjCount = 0; // Number of objects alive in oleds.dll
  86. //+------------------------------------------------------------------------
  87. //
  88. // Macro that calculates the number of elements in a statically-defined
  89. // array.
  90. //
  91. // Note - I swiped this from ADsary.cxx - A type-safe array class. Remember
  92. // to swipe the whole thing as required.
  93. //-------------------------------------------------------------------------
  94. #define ARRAY_SIZE(_a) (sizeof(_a) / sizeof(_a[0]))
  95. CWinNTProviderCF g_cfProvider;
  96. CWinNTNamespaceCF g_cfNamespace;
  97. CWinNTSystemInfoCF g_cfWinNTSystemInfo;
  98. CUmiConnectionCF g_cfWinNTUmiConn;
  99. //CWinNTDomainCF g_cfDomain;
  100. //+------------------------------------------------------------------------
  101. //
  102. // oleds class factories
  103. //
  104. //-------------------------------------------------------------------------
  105. struct CLSCACHE
  106. {
  107. const CLSID * pclsid;
  108. IClassFactory * pCF;
  109. };
  110. CLSCACHE g_aclscache[] =
  111. {
  112. &CLSID_WinNTProvider, &g_cfProvider,
  113. &CLSID_WinNTNamespace, &g_cfNamespace,
  114. &CLSID_WinNTSystemInfo, &g_cfWinNTSystemInfo,
  115. &CLSID_WinNTConnectionObject, &g_cfWinNTUmiConn
  116. };
  117. //+---------------------------------------------------------------
  118. //
  119. // Function: DllGetClassObject
  120. //
  121. // Synopsis: Standard DLL entrypoint for locating class factories
  122. //
  123. //----------------------------------------------------------------
  124. STDAPI
  125. DllGetClassObject(REFCLSID clsid, REFIID iid, LPVOID FAR* ppv)
  126. {
  127. HRESULT hr;
  128. size_t i;
  129. for (i = 0; i < ARRAY_SIZE(g_aclscache); i++)
  130. {
  131. if (IsEqualCLSID(clsid, *g_aclscache[i].pclsid))
  132. {
  133. hr = g_aclscache[i].pCF->QueryInterface(iid, ppv);
  134. RRETURN(hr);
  135. }
  136. }
  137. *ppv = NULL;
  138. //
  139. // Add Debugging Code to indicate that the oleds.DllGetClassObject has been called with an unknown CLSID.
  140. //
  141. return E_NOINTERFACE;
  142. }
  143. //+---------------------------------------------------------------
  144. //
  145. // Function: DllCanUnloadNow
  146. //
  147. // Synopsis: Standard DLL entrypoint to determine if DLL can be unloaded
  148. //
  149. //---------------------------------------------------------------
  150. STDAPI
  151. DllCanUnloadNow(void)
  152. {
  153. HRESULT hr;
  154. hr = S_FALSE;
  155. if (AggregatorDllCanUnload() ) {
  156. hr = S_OK;
  157. }
  158. return hr;
  159. }
  160. void LoadLocalizedStrings()
  161. {
  162. if (g_fStringsLoaded) {
  163. return;
  164. }
  165. if (!LoadStringW(
  166. g_hInst,
  167. ADS_WINNT_BUILTIN,
  168. g_szBuiltin,
  169. sizeof( g_szBuiltin ) / sizeof( WCHAR )
  170. )
  171. ) {
  172. wcscpy(g_szBuiltin, L"BUILTIN");
  173. }
  174. if (!LoadStringW(
  175. g_hInst,
  176. ADS_WINNT_NT_AUTHORITY,
  177. g_szNT_Authority,
  178. sizeof( g_szNT_Authority ) / sizeof( WCHAR )
  179. )
  180. ) {
  181. wcscpy(g_szNT_Authority, L"NT AUTHORITY");
  182. }
  183. if (!LoadStringW(
  184. g_hInst,
  185. ADS_WINNT_EVERYONE,
  186. g_szEveryone,
  187. sizeof( g_szEveryone ) / sizeof( WCHAR )
  188. )
  189. ) {
  190. wcscpy(g_szEveryone, L"Everyone");
  191. }
  192. }
  193. //+---------------------------------------------------------------
  194. //
  195. // Function: LibMain
  196. //
  197. // Synopsis: Standard DLL initialization entrypoint
  198. //
  199. //---------------------------------------------------------------
  200. EXTERN_C BOOL __cdecl
  201. LibMain(HINSTANCE hInst, ULONG ulReason, LPVOID pvReserved)
  202. {
  203. HRESULT hr;
  204. switch (ulReason)
  205. {
  206. case DLL_PROCESS_ATTACH:
  207. //
  208. // Catch case of init crit sect failing.
  209. //
  210. __try {
  211. DisableThreadLibraryCalls(hInst);
  212. g_hInst = hInst;
  213. g_hActiveDs = GetModuleHandle(TEXT("activeds.dll"));
  214. #if DBG==1
  215. #ifndef MSVC
  216. InitializeCriticalSection(&g_csOT);
  217. InitializeCriticalSection(&g_csMem);
  218. #endif
  219. InitializeCriticalSection(&g_csDP);
  220. #endif
  221. InitializeCriticalSection(&g_TypeInfoCritSect);
  222. //
  223. // Disabled to avoid linker warnings
  224. // InitializeCriticalSection(&g_DispTypeInfoCritSect);
  225. //
  226. //
  227. // for 3rd party extension
  228. //
  229. InitializeCriticalSection(&g_ExtCritSect);
  230. //
  231. // Initialize the loadlibs critsect.
  232. //
  233. InitializeCriticalSection(&g_csLoadLibs);
  234. //
  235. // Load up localized strings.
  236. //
  237. LoadLocalizedStrings();
  238. gpClassHead = BuildClassesList();
  239. //
  240. // Build the global object class cache
  241. //
  242. hr = CObjNameCache::CreateClassCache(
  243. &pgPDCNameCache
  244. );
  245. if (FAILED(hr)) {
  246. return(FALSE);
  247. }
  248. //
  249. // create semaphore used to protect global data (DLL handles and
  250. // function pointers.
  251. //
  252. if ((FpnwLoadLibSemaphore = CreateSemaphore( NULL,1,1,NULL ))
  253. == NULL)
  254. {
  255. return FALSE ;
  256. }
  257. g_pRtlEncryptMemory = (FRTLENCRYPTMEMORY) LoadAdvapi32Function(
  258. STRINGIZE(RtlEncryptMemory));
  259. g_pRtlDecryptMemory = (FRTLDECRYPTMEMORY) LoadAdvapi32Function(
  260. STRINGIZE(RtlDecryptMemory));
  261. if( (NULL == g_pRtlEncryptMemory) || (NULL == g_pRtlDecryptMemory) )
  262. g_pRtlEncryptMemory = g_pRtlDecryptMemory = NULL;
  263. }
  264. __except (EXCEPTION_EXECUTE_HANDLER) {
  265. //
  266. // Critical failure
  267. //
  268. return FALSE;
  269. }
  270. break;
  271. case DLL_PROCESS_DETACH:
  272. //
  273. // Release semaphor if applicable.
  274. //
  275. if (FpnwLoadLibSemaphore) {
  276. CloseHandle(FpnwLoadLibSemaphore);
  277. FpnwLoadLibSemaphore = NULL;
  278. }
  279. //
  280. // Del the name cache - delte should handle NULL btw.
  281. //
  282. delete pgPDCNameCache;
  283. //
  284. // free global list of class entries for 3rd party ext
  285. //
  286. FreeClassesList(gpClassHead);
  287. //
  288. // Delete the critical sections
  289. //
  290. #if DBG==1
  291. #ifndef MSVC
  292. DeleteCriticalSection(&g_csOT);
  293. DeleteCriticalSection(&g_csMem);
  294. #endif
  295. DeleteCriticalSection(&g_csDP);
  296. #endif
  297. DeleteCriticalSection(&g_TypeInfoCritSect);
  298. //
  299. // Causes link warnings if enabled
  300. // DeleteCriticalSection(&g_DispTypeInfoCritSect);
  301. //
  302. DeleteCriticalSection(&g_ExtCritSect);
  303. DeleteCriticalSection(&g_csLoadLibs);
  304. //
  305. // Free libs we may have loaded dynamically.
  306. //
  307. if (g_hDllNetapi32) {
  308. FreeLibrary((HMODULE) g_hDllNetapi32);
  309. g_hDllNetapi32 = NULL;
  310. }
  311. if (g_hDllAdvapi32) {
  312. FreeLibrary((HMODULE) g_hDllAdvapi32);
  313. }
  314. break;
  315. default:
  316. break;
  317. }
  318. return TRUE;
  319. }
  320. //+---------------------------------------------------------------------------
  321. //
  322. // Function: DllMain
  323. //
  324. // Synopsis: entry point for NT - post .546
  325. //
  326. //----------------------------------------------------------------------------
  327. BOOL
  328. DllMain(HANDLE hDll, DWORD dwReason, LPVOID lpReserved)
  329. {
  330. return LibMain((HINSTANCE)hDll, dwReason, lpReserved);
  331. }
  332. //+------------------------------------------------------------------------
  333. //
  334. // Function: GetCachedClsidIndex
  335. //
  336. // Synopsis: Returns the index of the given CLSID in the cache, or
  337. // -1 if the CLSID is not present in the cache
  338. //
  339. // Arguments: [clsid]
  340. //
  341. // Returns: int
  342. //
  343. //-------------------------------------------------------------------------
  344. int
  345. GetCachedClsidIndex(REFCLSID clsid)
  346. {
  347. int i;
  348. CLSCACHE * pclscache;
  349. for (i = 0, pclscache = g_aclscache;
  350. i < ARRAY_SIZE(g_aclscache);
  351. i ++, pclscache++)
  352. {
  353. if (IsEqualCLSID(*pclscache->pclsid, clsid))
  354. return i;
  355. }
  356. return -1;
  357. }
  358. //+------------------------------------------------------------------------
  359. //
  360. // Function: GetCachedClassFactory
  361. //
  362. // Synopsis: Returns the cached class factory with the given index.
  363. // The pointer returned has been AddRef'd.
  364. //
  365. // Arguments: [iclsid]
  366. //
  367. // Returns: IClassFactory *
  368. //
  369. //-------------------------------------------------------------------------
  370. IClassFactory *
  371. GetCachedClassFactory(int iclsid)
  372. {
  373. IClassFactory * pCF;
  374. // Assert(iclsid >= 0);
  375. // Assert(iclsid < ARRAY_SIZE(g_aclscache));
  376. pCF = g_aclscache[iclsid].pCF;
  377. pCF->AddRef();
  378. return pCF;
  379. }
  380. //+------------------------------------------------------------------------
  381. //
  382. // Function: GetCachedClsid
  383. //
  384. // Synopsis: Returns the CLSID corresponding to the given index.
  385. // Normally, code should call GetCachedClassFactory to get
  386. // the class factory directly.
  387. //
  388. // Arguments: [iclsid] -- Clsid index
  389. // [pclsid] -- Matching clsid returned in *pclsid
  390. //
  391. //-------------------------------------------------------------------------
  392. void
  393. GetCachedClsid(int iclsid, CLSID * pclsid)
  394. {
  395. // Assert(iclsid >= 0);
  396. // Assert(iclsid < ARRAY_SIZE(g_aclscache));
  397. *pclsid = *g_aclscache[iclsid].pclsid;
  398. }