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.

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