Leaked source code of windows server 2003
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.

292 lines
6.1 KiB

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2000 Microsoft Corporation
  4. //
  5. // Module Name:
  6. // DiskMonitor.cpp
  7. //
  8. // Description:
  9. // description-for-module
  10. //
  11. // History:
  12. // Xing Jin (i-xingj) 06-Dec-2000
  13. //
  14. //////////////////////////////////////////////////////////////////////////////
  15. #include <windows.h>
  16. #include <stdio.h>
  17. #include <time.h>
  18. #include <locale.h>
  19. #include <wbemidl.h>
  20. #include <initguid.h>
  21. #include "SAEventFactory.h"
  22. #include "SADiskEvent.h"
  23. static HINSTANCE g_hInstance;
  24. LONG g_cObj = 0;
  25. LONG g_cLock= 0;
  26. //////////////////////////////////////////////////////////////////////////////
  27. //
  28. // DllMain
  29. //
  30. // Description:
  31. // Entry point of the module.
  32. //
  33. // Arguments:
  34. // [in] hinstDLLIn
  35. // dwReasonIn
  36. // lpReservedIn
  37. //
  38. // Returns:
  39. // TRUE
  40. // FALSE
  41. //
  42. // History:
  43. // Xing Jin (i-xingj) 06-Dec-2000
  44. //
  45. //////////////////////////////////////////////////////////////////////////////
  46. BOOL
  47. WINAPI
  48. DllMain(
  49. HINSTANCE hinstDLLIn,
  50. DWORD dwReasonIn,
  51. LPVOID lpReservedIn
  52. )
  53. {
  54. if ( dwReasonIn == DLL_PROCESS_ATTACH )
  55. {
  56. g_hInstance = hinstDLLIn;
  57. setlocale( LC_ALL, "" );
  58. }
  59. else if ( dwReasonIn == DLL_PROCESS_DETACH )
  60. {
  61. }
  62. return TRUE;
  63. }
  64. //////////////////////////////////////////////////////////////////////////////
  65. //
  66. // DllGetClassObject
  67. //
  68. // Description:
  69. // Retrieves the class object from the module.
  70. //
  71. // Arguments:
  72. // [in] rclsidIn
  73. // riidIn
  74. // [out] ppvOut
  75. //
  76. // Return:
  77. // S_OK
  78. // CLASS_E_CLASSNOTAVAILABLE
  79. //
  80. // History:
  81. // Xing Jin (i-xingj) 06-Dec-2000
  82. //
  83. //////////////////////////////////////////////////////////////////////////////
  84. extern "C"
  85. HRESULT
  86. APIENTRY
  87. DllGetClassObject(
  88. REFCLSID rclsidIn,
  89. REFIID riidIn,
  90. LPVOID * ppvOut
  91. )
  92. {
  93. HRESULT hr;
  94. CSAEventFactory * pFactory;
  95. //
  96. // Verify the caller is asking for our type of object.
  97. //
  98. if ( CLSID_DiskEventProvider != rclsidIn )
  99. {
  100. return E_FAIL;
  101. }
  102. //
  103. // Check that we can provide the interface.
  104. //
  105. if ( IID_IUnknown != riidIn && IID_IClassFactory != riidIn )
  106. {
  107. return E_NOINTERFACE;
  108. }
  109. //
  110. // Get a new class factory.
  111. //
  112. pFactory = new CSAEventFactory( rclsidIn );
  113. if ( !pFactory )
  114. {
  115. return E_OUTOFMEMORY;
  116. }
  117. //
  118. // Verify we can get an instance.
  119. //
  120. hr = pFactory->QueryInterface( riidIn, ppvOut );
  121. if ( FAILED( hr ) )
  122. {
  123. delete pFactory;
  124. }
  125. return hr;
  126. }
  127. //////////////////////////////////////////////////////////////////////////////
  128. //
  129. // DllCanUnloadNow
  130. //
  131. // Description:
  132. // Retrieves the class object from the module.
  133. //
  134. // Return:
  135. // SA_OK
  136. // SA_FALSE
  137. //
  138. // History:
  139. // Xing Jin (i-xingj) 06-Dec-2000
  140. //
  141. //////////////////////////////////////////////////////////////////////////////
  142. extern "C"
  143. HRESULT
  144. APIENTRY
  145. DllCanUnloadNow(void)
  146. {
  147. SCODE sc = TRUE;
  148. if (g_cObj || g_cLock)
  149. sc = S_FALSE;
  150. return sc;
  151. }
  152. //////////////////////////////////////////////////////////////////////////////
  153. //
  154. // DllRegisterServer
  155. //
  156. // Description:
  157. // Standard OLE entry point for registering the server.
  158. //
  159. // Returns:
  160. // S_OK Registration was successful
  161. // E_FAIL Registration failed.
  162. //
  163. // History:
  164. // Xing Jin (i-xingj) 06-Dec-2000
  165. //
  166. //////////////////////////////////////////////////////////////////////////////
  167. extern "C"
  168. HRESULT
  169. APIENTRY
  170. DllRegisterServer(void)
  171. {
  172. wchar_t Path[1024];
  173. wchar_t *pGuidStr = 0;
  174. wchar_t KeyPath[1024];
  175. //
  176. // Where are we?
  177. //
  178. DWORD dwResult = GetModuleFileNameW(g_hInstance, Path, 1023);
  179. if (0 == dwResult)
  180. {
  181. return (HRESULT_FROM_WIN32 (GetLastError ()));
  182. }
  183. Path[1023] = L'\0';
  184. //
  185. // Convert CLSID to string.
  186. //
  187. StringFromCLSID(CLSID_DiskEventProvider, &pGuidStr);
  188. swprintf(KeyPath, L"CLSID\\\\%s", pGuidStr);
  189. HKEY hKey;
  190. LONG lRes = RegCreateKeyW(HKEY_CLASSES_ROOT, KeyPath, &hKey);
  191. if (lRes)
  192. return E_FAIL;
  193. wchar_t *pName = L"Microsoft Server Appliance Disk Monitor";
  194. RegSetValueExW(hKey, 0, 0, REG_SZ, (const BYTE *) pName, wcslen(pName) * 2 + 2);
  195. HKEY hSubkey;
  196. lRes = RegCreateKeyW(hKey, L"InprocServer32", &hSubkey);
  197. RegSetValueExW(hSubkey, 0, 0, REG_SZ, (const BYTE *) Path, wcslen(Path) * 2 + 2);
  198. RegSetValueExW(hSubkey, L"ThreadingModel", 0, REG_SZ, (const BYTE *) L"Both", wcslen(L"Both") * 2 + 2);
  199. RegCloseKey(hSubkey);
  200. RegCloseKey(hKey);
  201. CoTaskMemFree(pGuidStr);
  202. return S_OK;
  203. }
  204. //////////////////////////////////////////////////////////////////////////////
  205. //
  206. // DllUnregisterServer
  207. //
  208. // Description:
  209. // Standard OLE entry point for unregistering the server.
  210. //
  211. // Returns:
  212. // S_OK Unregistration was successful
  213. // E_FAIL Unregistration failed.
  214. //
  215. // History:
  216. // Xing Jin (i-xingj) 06-Dec-2000
  217. //
  218. //////////////////////////////////////////////////////////////////////////////
  219. extern "C"
  220. HRESULT
  221. APIENTRY
  222. DllUnregisterServer(void)
  223. {
  224. wchar_t *pGuidStr = 0;
  225. HKEY hKey;
  226. wchar_t KeyPath[256];
  227. StringFromCLSID(CLSID_DiskEventProvider, &pGuidStr);
  228. swprintf(KeyPath, L"CLSID\\%s", pGuidStr);
  229. //
  230. // Delete InProcServer32 subkey.
  231. //
  232. LONG lRes = RegOpenKeyW(HKEY_CLASSES_ROOT, KeyPath, &hKey);
  233. if ( lRes )
  234. {
  235. return E_FAIL;
  236. }
  237. RegDeleteKeyW(hKey, L"InprocServer32");
  238. RegCloseKey(hKey);
  239. //
  240. // Delete CLSID GUID key.
  241. //
  242. lRes = RegOpenKeyW(HKEY_CLASSES_ROOT, L"CLSID", &hKey);
  243. if ( lRes )
  244. {
  245. return E_FAIL;
  246. }
  247. RegDeleteKeyW(hKey, pGuidStr);
  248. RegCloseKey(hKey);
  249. CoTaskMemFree(pGuidStr);
  250. return S_OK;
  251. }