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.

364 lines
8.3 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 "nds.hxx"
  16. #pragma hdrstop
  17. HINSTANCE g_hInst = NULL;
  18. WCHAR * szNDSPrefix = L"@NDS!";
  19. //---------------------------------------------------------------------------
  20. // ADs debug print, mem leak and object tracking-related stuff
  21. //---------------------------------------------------------------------------
  22. DECLARE_INFOLEVEL(ADs)
  23. //+---------------------------------------------------------------------------
  24. //
  25. // Function: ShutDown
  26. //
  27. // Synopsis: Function to handle printing out heap debugging display
  28. //
  29. //----------------------------------------------------------------------------
  30. inline VOID ShutDown()
  31. {
  32. #if DBG==1
  33. #ifndef MSVC
  34. DUMP_TRACKING_INFO_DELETE();
  35. DeleteCriticalSection(&g_csOT);
  36. #endif // ifndef MSVC
  37. DeleteCriticalSection(&g_csDP);
  38. #endif
  39. }
  40. extern "C" DWORD heapInfoLevel;
  41. extern "C" DWORD OtInfoLevel;
  42. extern "C" DWORD ADsInfoLevel;
  43. extern CRITICAL_SECTION g_DispTypeInfoCritSect;
  44. //+---------------------------------------------------------------------------
  45. //
  46. // Function: GetINIHeapInfoLevel
  47. //
  48. // Synopsis: Gets various infolevel values from win.ini
  49. //
  50. //----------------------------------------------------------------------------
  51. inline VOID GetINIHeapInfoLevel()
  52. {
  53. #if DBG==1
  54. const INT MAXINFOLEN=11;
  55. WCHAR awcs[MAXINFOLEN];
  56. #ifndef MSVC
  57. if (GetProfileString(L"NDS",L"heapInfoLevel", L"00000003", awcs,MAXINFOLEN))
  58. heapInfoLevel = wcstoul(awcs, NULL, 16);
  59. if (GetProfileString(L"NDS",L"Ot", L"00000003", awcs, MAXINFOLEN))
  60. OtInfoLevel = wcstoul(awcs, NULL, 16);
  61. #endif // MSVC
  62. if (GetProfileString(L"NDS",L"ADsInfoLevel", L"00000003", awcs,MAXINFOLEN))
  63. ADsInfoLevel = wcstoul(awcs, NULL, 16);
  64. #endif
  65. }
  66. // Globals
  67. ULONG g_ulObjCount = 0; // Number of objects alive in oleds.dll
  68. //+------------------------------------------------------------------------
  69. //
  70. // Macro that calculates the number of elements in a statically-defined
  71. // array.
  72. //
  73. // Note - I swiped this from ADsary.cxx - A type-safe array class. Remember
  74. // to swipe the whole thing as required.
  75. //-------------------------------------------------------------------------
  76. #define ARRAY_SIZE(_a) (sizeof(_a) / sizeof(_a[0]))
  77. CNDSProviderCF g_cfProvider;
  78. CNDSNamespaceCF g_cfNamespace;
  79. //+------------------------------------------------------------------------
  80. //
  81. // oleds class factories
  82. //
  83. //-------------------------------------------------------------------------
  84. struct CLSCACHE
  85. {
  86. const CLSID * pclsid;
  87. IClassFactory * pCF;
  88. };
  89. CLSCACHE g_aclscache[] =
  90. {
  91. &CLSID_NDSProvider, &g_cfProvider,
  92. &CLSID_NDSNamespace, &g_cfNamespace
  93. };
  94. //+---------------------------------------------------------------
  95. //
  96. // Function: DllGetClassObject
  97. //
  98. // Synopsis: Standard DLL entrypoint for locating class factories
  99. //
  100. //----------------------------------------------------------------
  101. STDAPI
  102. DllGetClassObject(REFCLSID clsid, REFIID iid, LPVOID FAR* ppv)
  103. {
  104. HRESULT hr;
  105. size_t i;
  106. for (i = 0; i < ARRAY_SIZE(g_aclscache); i++)
  107. {
  108. if (IsEqualCLSID(clsid, *g_aclscache[i].pclsid))
  109. {
  110. hr = g_aclscache[i].pCF->QueryInterface(iid, ppv);
  111. RRETURN(hr);
  112. }
  113. }
  114. *ppv = NULL;
  115. //
  116. // Add Debugging Code to indicate that the oleds.DllGetClassObject has been called with an unknown CLSID.
  117. //
  118. return E_NOINTERFACE;
  119. }
  120. //+---------------------------------------------------------------
  121. //
  122. // Function: DllCanUnloadNow
  123. //
  124. // Synopsis: Standard DLL entrypoint to determine if DLL can be unloaded
  125. //
  126. //---------------------------------------------------------------
  127. STDAPI
  128. DllCanUnloadNow(void)
  129. {
  130. HRESULT hr;
  131. hr = S_FALSE;
  132. //
  133. // BugBug - Fix this Pronto!!
  134. //
  135. /*
  136. if (GET_OBJECT_COUNT() > 0)
  137. hr = S_FALSE;
  138. else
  139. hr = S_OK;
  140. */
  141. return hr;
  142. }
  143. //+---------------------------------------------------------------
  144. //
  145. // Function: LibMain
  146. //
  147. // Synopsis: Standard DLL initialization entrypoint
  148. //
  149. //---------------------------------------------------------------
  150. EXTERN_C BOOL __cdecl
  151. LibMain(HINSTANCE hInst, ULONG ulReason, LPVOID pvReserved)
  152. {
  153. HRESULT hr;
  154. switch (ulReason)
  155. {
  156. case DLL_PROCESS_ATTACH:
  157. //
  158. // Catch case of init crit sect failing.
  159. //
  160. __try {
  161. DisableThreadLibraryCalls(hInst);
  162. g_hInst = hInst;
  163. #if DBG==1
  164. #ifndef MSVC
  165. InitializeCriticalSection(&g_csOT);
  166. InitializeCriticalSection(&g_csMem);
  167. #endif
  168. InitializeCriticalSection(&g_csDP);
  169. #endif
  170. InitializeCriticalSection(&g_DispTypeInfoCritSect);
  171. InitializeCriticalSection(&g_ContextCritSect);
  172. BindCacheInit();
  173. //
  174. // Build the global object class cache
  175. //
  176. hr = CClassCache::CreateClassCache(
  177. &pgClassCache
  178. );
  179. if (FAILED(hr)) {
  180. return(FALSE);
  181. }
  182. InitializeNWLibrary();
  183. }
  184. __except (EXCEPTION_EXECUTE_HANDLER) {
  185. //
  186. // Critical Failure.
  187. //
  188. return FALSE;
  189. }
  190. break;
  191. case DLL_PROCESS_DETACH:
  192. delete pgClassCache;
  193. #if DBG==1
  194. #ifndef MSVC
  195. DeleteCriticalSection(&g_csOT);
  196. DeleteCriticalSection(&g_csMem);
  197. #endif
  198. DeleteCriticalSection(&g_csDP);
  199. #endif
  200. DeleteCriticalSection(&g_DispTypeInfoCritSect);
  201. DeleteCriticalSection(&g_ContextCritSect);
  202. // FreeTypeInfoTable();
  203. BindCacheCleanup();
  204. break;
  205. default:
  206. break;
  207. }
  208. return TRUE;
  209. }
  210. //+---------------------------------------------------------------------------
  211. //
  212. // Function: DllMain
  213. //
  214. // Synopsis: entry point for NT - post .546
  215. //
  216. //----------------------------------------------------------------------------
  217. BOOL
  218. DllMain(HANDLE hDll, DWORD dwReason, LPVOID lpReserved)
  219. {
  220. return LibMain((HINSTANCE)hDll, dwReason, lpReserved);
  221. }
  222. //+------------------------------------------------------------------------
  223. //
  224. // Function: GetCachedClsidIndex
  225. //
  226. // Synopsis: Returns the index of the given CLSID in the cache, or
  227. // -1 if the CLSID is not present in the cache
  228. //
  229. // Arguments: [clsid]
  230. //
  231. // Returns: int
  232. //
  233. //-------------------------------------------------------------------------
  234. int
  235. GetCachedClsidIndex(REFCLSID clsid)
  236. {
  237. int i;
  238. CLSCACHE * pclscache;
  239. for (i = 0, pclscache = g_aclscache;
  240. i < ARRAY_SIZE(g_aclscache);
  241. i ++, pclscache++)
  242. {
  243. if (IsEqualCLSID(*pclscache->pclsid, clsid))
  244. return i;
  245. }
  246. return -1;
  247. }
  248. //+------------------------------------------------------------------------
  249. //
  250. // Function: GetCachedClassFactory
  251. //
  252. // Synopsis: Returns the cached class factory with the given index.
  253. // The pointer returned has been AddRef'd.
  254. //
  255. // Arguments: [iclsid]
  256. //
  257. // Returns: IClassFactory *
  258. //
  259. //-------------------------------------------------------------------------
  260. IClassFactory *
  261. GetCachedClassFactory(int iclsid)
  262. {
  263. IClassFactory * pCF;
  264. // Assert(iclsid >= 0);
  265. // Assert(iclsid < ARRAY_SIZE(g_aclscache));
  266. pCF = g_aclscache[iclsid].pCF;
  267. pCF->AddRef();
  268. return pCF;
  269. }
  270. //+------------------------------------------------------------------------
  271. //
  272. // Function: GetCachedClsid
  273. //
  274. // Synopsis: Returns the CLSID corresponding to the given index.
  275. // Normally, code should call GetCachedClassFactory to get
  276. // the class factory directly.
  277. //
  278. // Arguments: [iclsid] -- Clsid index
  279. // [pclsid] -- Matching clsid returned in *pclsid
  280. //
  281. //-------------------------------------------------------------------------
  282. void
  283. GetCachedClsid(int iclsid, CLSID * pclsid)
  284. {
  285. // Assert(iclsid >= 0);
  286. // Assert(iclsid < ARRAY_SIZE(g_aclscache));
  287. *pclsid = *g_aclscache[iclsid].pclsid;
  288. }