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.

289 lines
6.6 KiB

  1. //----------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 2000.
  5. //
  6. // File: libmain.cxx
  7. //
  8. // Contents: Contains the implementation of DLL entry points.
  9. //
  10. //----------------------------------------------------------------------------
  11. #define INITGUID
  12. #include "cadsxml.hxx"
  13. #include "adsxmlcf.hxx"
  14. #include <ole2.h>
  15. DECLARE_INFOLEVEL(ADs)
  16. CADsXMLCF g_cfADsXML;
  17. CRITICAL_SECTION g_ADsXMLCritSect;
  18. struct CLSCACHE
  19. {
  20. const CLSID *pclsid;
  21. IClassFactory *pCF;
  22. };
  23. DEFINE_GUID(CLSID_ADsXML, 0x800fc136,0xb101,0x46c4,0xa5,0xd9,0x80,0xfb,0x31,0x4e,0x1a,0x13);
  24. CLSCACHE g_aclscache[] =
  25. {
  26. &CLSID_ADsXML, &g_cfADsXML
  27. };
  28. #define ARRAY_SIZE(_a) (sizeof(_a) / sizeof(_a[0]))
  29. #define EXTENSION_CLSID L"SOFTWARE\\Microsoft\\ADs\\Providers\\LDAP\\Extensions\\top\\{800fc136-b101-46c4-a5d9-80fb314e1a13}"
  30. #define EXTENSION_IID L"{91e5c5dc-926b-46ff-9fdb-9fb112bf10e6}"
  31. extern CRITICAL_SECTION g_ExtTypeInfoCritSect;
  32. extern CRITICAL_SECTION g_DispTypeInfoCritSect;
  33. //----------------------------------------------------------------
  34. //
  35. // Function: DllGetClassObject
  36. //
  37. // Synopsis: Standard DLL entrypoint for locating class factories
  38. //
  39. // Arguments:
  40. //
  41. // clsid Class ID of object being created
  42. // iid Interface requested
  43. // ppv Returns interface requested
  44. //
  45. // Returns: S_OK on success, error otherwise
  46. //
  47. // Modifies: *ppv to return interface pointer
  48. //
  49. //----------------------------------------------------------------
  50. STDAPI
  51. DllGetClassObject(
  52. REFCLSID clsid,
  53. REFIID iid,
  54. LPVOID FAR* ppv
  55. )
  56. {
  57. HRESULT hr = S_OK;
  58. size_t i = 0;
  59. ADsAssert(ppv != NULL);
  60. for (i = 0; i < ARRAY_SIZE(g_aclscache); i++)
  61. {
  62. if (IsEqualCLSID(clsid, *g_aclscache[i].pclsid))
  63. {
  64. hr = g_aclscache[i].pCF->QueryInterface(iid, ppv);
  65. RRETURN(hr);
  66. }
  67. }
  68. *ppv = NULL;
  69. RRETURN(E_NOINTERFACE);
  70. }
  71. //----------------------------------------------------------------
  72. //
  73. // Function: DllCanUnloadNow
  74. //
  75. // Synopsis: Standard DLL entrypoint to determine if DLL can be unloaded
  76. //
  77. // Arguments: None
  78. //
  79. // Returns: Returns S_FALSE if the DLL cannot be unloaded, S_OK otherwise.
  80. // The DLL can be unloaded if there are no outstanding objects.
  81. //
  82. // Modifies: Nothing
  83. //
  84. //---------------------------------------------------------------
  85. STDAPI
  86. DllCanUnloadNow(void)
  87. {
  88. HRESULT hr = S_FALSE;
  89. EnterCriticalSection(&g_ADsXMLCritSect);
  90. if(0 == glnObjCnt) {
  91. hr = S_OK;
  92. }
  93. LeaveCriticalSection(&g_ADsXMLCritSect);
  94. return hr;
  95. }
  96. //+---------------------------------------------------------------
  97. //
  98. // Function: DllMain
  99. //
  100. // Synopsis: Standard DLL initialization entrypoint
  101. //
  102. // Arguments:
  103. //
  104. // hDll Handle to the DLL module
  105. // dwReason Reason for calling this entry point
  106. // lpReserved Reserved
  107. //
  108. // Returns: Returns FALSE on failure, TRUE otherwise.
  109. //
  110. // Modifies: Nothing
  111. //
  112. //---------------------------------------------------------------
  113. BOOL
  114. DllMain(HANDLE hDll, DWORD dwReason, LPVOID lpReserved)
  115. {
  116. HRESULT hr;
  117. switch (dwReason) {
  118. case DLL_PROCESS_ATTACH:
  119. //
  120. // Catch case of init crit sect failing.
  121. //
  122. __try {
  123. DisableThreadLibraryCalls((HINSTANCE)hDll);
  124. InitializeCriticalSection(&g_ADsXMLCritSect);
  125. #if DBG==1
  126. #ifndef MSVC
  127. InitializeCriticalSection(&g_csOT);
  128. InitializeCriticalSection(&g_csMem);
  129. #endif
  130. InitializeCriticalSection(&g_csDP);
  131. #endif
  132. InitializeCriticalSection(&g_ExtTypeInfoCritSect);
  133. InitializeCriticalSection(&g_DispTypeInfoCritSect);
  134. }
  135. __except (EXCEPTION_EXECUTE_HANDLER) {
  136. //
  137. // Critical failure
  138. //
  139. return FALSE;
  140. }
  141. break;
  142. case DLL_PROCESS_DETACH:
  143. DeleteCriticalSection(&g_ADsXMLCritSect);
  144. AggregateeFreeTypeInfoTable();
  145. #if DBG==1
  146. #ifndef MSVC
  147. DeleteCriticalSection(&g_csOT);
  148. DeleteCriticalSection(&g_csMem);
  149. #endif
  150. DeleteCriticalSection(&g_csDP);
  151. #endif
  152. DeleteCriticalSection(&g_ExtTypeInfoCritSect);
  153. DeleteCriticalSection(&g_DispTypeInfoCritSect);
  154. break;
  155. default:
  156. return FALSE;
  157. }
  158. return TRUE;
  159. }
  160. //----------------------------------------------------------------
  161. //
  162. // Function: DllRegisterServer
  163. //
  164. // Synopsis: Standard DLL entrypoint to create registry entries for classes
  165. // supported by this DLL.
  166. //
  167. // Arguments: None
  168. //
  169. // Returns: Returns S_OK on success, error otherwise.
  170. //
  171. // Modifies: Nothing
  172. //
  173. //---------------------------------------------------------------
  174. STDAPI DllRegisterServer(void)
  175. {
  176. HRESULT hr = S_OK;
  177. long lRetVal = 0;
  178. HKEY hKey;
  179. lRetVal = RegCreateKeyEx(
  180. HKEY_LOCAL_MACHINE,
  181. EXTENSION_CLSID,
  182. 0,
  183. NULL,
  184. REG_OPTION_NON_VOLATILE,
  185. KEY_ALL_ACCESS,
  186. NULL,
  187. &hKey,
  188. NULL
  189. );
  190. if(lRetVal != ERROR_SUCCESS)
  191. BAIL_ON_FAILURE(hr = HRESULT_FROM_WIN32(lRetVal));
  192. lRetVal = RegSetValueEx(
  193. hKey,
  194. L"Interfaces",
  195. 0,
  196. REG_SZ,
  197. (const BYTE *) EXTENSION_IID,
  198. (wcslen(EXTENSION_IID) + 1) * 2
  199. );
  200. if(lRetVal != ERROR_SUCCESS)
  201. BAIL_ON_FAILURE(hr = HRESULT_FROM_WIN32(lRetVal));
  202. RegCloseKey(hKey);
  203. error:
  204. RRETURN(hr);
  205. }
  206. //----------------------------------------------------------------
  207. //
  208. // Function: DllUnregisterServer
  209. //
  210. // Synopsis: Standard DLL entrypoint to remove registry entries for classes
  211. // supported by this DLL.
  212. //
  213. // Arguments: None
  214. //
  215. // Returns: Returns S_OK on success, error otherwise.
  216. //
  217. // Modifies: Nothing
  218. //
  219. //---------------------------------------------------------------
  220. STDAPI DllUnregisterServer(void)
  221. {
  222. LONG lRetVal = 0;
  223. HRESULT hr = S_OK;
  224. lRetVal = RegDeleteKey(
  225. HKEY_LOCAL_MACHINE,
  226. EXTENSION_CLSID
  227. );
  228. if(lRetVal != ERROR_SUCCESS)
  229. BAIL_ON_FAILURE(hr = HRESULT_FROM_WIN32(lRetVal));
  230. lRetVal = RegDeleteKey(
  231. HKEY_LOCAL_MACHINE,
  232. L"SOFTWARE\\Microsoft\\ADs\\Providers\\LDAP\\Extensions\\top"
  233. );
  234. if(lRetVal != ERROR_SUCCESS)
  235. BAIL_ON_FAILURE(hr = HRESULT_FROM_WIN32(lRetVal));
  236. error:
  237. RRETURN(hr);
  238. }