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.

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