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.

289 lines
10 KiB

  1. /********************************************************************
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. PCH_Drive.CPP
  5. Abstract:
  6. WBEM provider class implementation for PCH_Drive class
  7. Revision History:
  8. Ghim-Sim Chua (gschua) 04/27/99
  9. - Created
  10. Ghim-Sim Chua (gschua) 05/02/99
  11. - Modified code to use CopyProperty function
  12. - Use CComBSTR instead of USES_CONVERSION
  13. ********************************************************************/
  14. #include "pchealth.h"
  15. #include "PCH_Drive.h"
  16. /////////////////////////////////////////////////////////////////////////////
  17. // tracing stuff
  18. #ifdef THIS_FILE
  19. #undef THIS_FILE
  20. #endif
  21. static char __szTraceSourceFile[] = __FILE__;
  22. #define THIS_FILE __szTraceSourceFile
  23. #define TRACE_ID DCID_DRIVE
  24. #define maxMediaTypes (sizeof(szMediaTypeStrings)/sizeof(*szMediaTypeStrings))
  25. #define KILO 1024
  26. #define MAXSIZE 65
  27. const static LPCTSTR szMediaTypeStrings[] =
  28. {
  29. _T("Format is Unknown "),
  30. _T("5.25\", 1.2MB, 512 bytes/sector "),
  31. _T("3.5\", 1.44MB, 512 bytes/sector "),
  32. _T("3.5\", 2.88MB, 512 bytes/sector "),
  33. _T("3.5\", 20.8MB, 512 bytes/sector "),
  34. _T("3.5\", 720KB, 512 bytes/sector "),
  35. _T("5.25\", 360KB, 512 bytes/sector "),
  36. _T("5.25\", 320KB, 512 bytes/sector "),
  37. _T("5.25\", 320KB, 1024 bytes/sector"),
  38. _T("5.25\", 180KB, 512 bytes/sector "),
  39. _T("5.25\", 160KB, 512 bytes/sector "),
  40. _T("Removable media other than floppy "),
  41. _T("Fixed hard disk media "),
  42. _T("3.5\", 120M Floppy "),
  43. _T("3.5\" , 640KB, 512 bytes/sector "),
  44. _T("5.25\", 640KB, 512 bytes/sector "),
  45. _T("5.25\", 720KB, 512 bytes/sector "),
  46. _T("3.5\" , 1.2Mb, 512 bytes/sector "),
  47. _T("3.5\" , 1.23Mb, 1024 bytes/sector"),
  48. _T("5.25\", 1.23MB, 1024 bytes/sector"),
  49. _T("3.5\" MO 128Mb 512 bytes/sector "),
  50. _T("3.5\" MO 230Mb 512 bytes/sector "),
  51. _T("8\", 256KB, 128 bytes/sector ")
  52. };
  53. CPCH_Drive MyPCH_DriveSet (PROVIDER_NAME_PCH_DRIVE, PCH_NAMESPACE) ;
  54. // Property names
  55. //===============
  56. const static WCHAR* pAvailable = L"Available" ;
  57. const static WCHAR* pTimeStamp = L"TimeStamp" ;
  58. const static WCHAR* pChange = L"Change" ;
  59. const static WCHAR* pDriveLetter = L"DriveLetter" ;
  60. const static WCHAR* pFilesystemType = L"FilesystemType" ;
  61. const static WCHAR* pFree = L"Free" ;
  62. const static WCHAR* pDescription = L"Description";
  63. const static WCHAR* pMediaType = L"MediaType";
  64. /*****************************************************************************
  65. *
  66. * FUNCTION : CPCH_Drive::EnumerateInstances
  67. *
  68. * DESCRIPTION : Returns all the instances of this class.
  69. *
  70. * INPUTS : A pointer to the MethodContext for communication with WinMgmt.
  71. * A long that contains the flags described in
  72. * IWbemServices::CreateInstanceEnumAsync. Note that the following
  73. * flags are handled by (and filtered out by) WinMgmt:
  74. * WBEM_FLAG_DEEP
  75. * WBEM_FLAG_SHALLOW
  76. * WBEM_FLAG_RETURN_IMMEDIATELY
  77. * WBEM_FLAG_FORWARD_ONLY
  78. * WBEM_FLAG_BIDIRECTIONAL
  79. *
  80. * RETURNS : WBEM_S_NO_ERROR if successful
  81. *
  82. * COMMENTS : TO DO: All instances on the machine should be returned here.
  83. * If there are no instances, return WBEM_S_NO_ERROR.
  84. * It is not an error to have no instances.
  85. *
  86. *****************************************************************************/
  87. HRESULT CPCH_Drive::EnumerateInstances(
  88. MethodContext* pMethodContext,
  89. long lFlags
  90. )
  91. {
  92. TraceFunctEnter("CPCH_Drive::EnumerateInstances");
  93. HRESULT hRes = WBEM_S_NO_ERROR;
  94. REFPTRCOLLECTION_POSITION posList;
  95. CComPtr<IEnumWbemClassObject> pEnumInst;
  96. IWbemClassObjectPtr pObj;
  97. ULONG ulRetVal;
  98. int nMediaType;
  99. long lFreeSpace;
  100. long lAvailable;
  101. LONGLONG llFreeSpace;
  102. LONGLONG llAvailable;
  103. TCHAR tchSize[MAXSIZE];
  104. TCHAR tchFreeSpace[MAXSIZE];
  105. CComVariant varMediaType;
  106. CComVariant varMediaTypeStr;
  107. CComVariant varFreeSpace;
  108. CComVariant varFree;
  109. CComVariant varAvailable;
  110. CComVariant varSize;
  111. //
  112. // Get the date and time
  113. //
  114. SYSTEMTIME stUTCTime;
  115. GetSystemTime(&stUTCTime);
  116. //
  117. // Execute the query
  118. //
  119. hRes = ExecWQLQuery(&pEnumInst, CComBSTR("select DeviceID, FileSystem, FreeSpace, Size, Description, MediaType FROM win32_logicalDisk"));
  120. if (FAILED(hRes))
  121. goto END;
  122. //
  123. // enumerate the instances from win32_CodecFile
  124. //
  125. while(WBEM_S_NO_ERROR == pEnumInst->Next(WBEM_INFINITE, 1, &pObj, &ulRetVal))
  126. {
  127. // Create a new instance based on the passed-in MethodContext
  128. CInstancePtr pInstance(CreateNewInstance(pMethodContext), false);
  129. if (!pInstance->SetDateTime(pTimeStamp, WBEMTime(stUTCTime)))
  130. ErrorTrace(TRACE_ID, "SetDateTime on Timestamp Field failed.");
  131. if (!pInstance->SetCHString(pChange, L"Snapshot"))
  132. ErrorTrace(TRACE_ID, "SetCHString on Change Field failed.");
  133. (void)CopyProperty(pObj, L"DeviceID", pInstance, pDriveLetter);
  134. (void)CopyProperty(pObj, L"FileSystem", pInstance, pFilesystemType);
  135. (void)CopyProperty(pObj, L"Description", pInstance, pDescription);
  136. // Get the Available Space
  137. varSize.Clear();
  138. varAvailable.Clear();
  139. hRes = pObj->Get(CComBSTR(L"Size"),0,&varSize,NULL,NULL);
  140. if(FAILED(hRes))
  141. {
  142. // Cannot Get the "Size" Property.
  143. ErrorTrace(TRACE_ID, "GetVariant on Size Field failed.");
  144. }
  145. else
  146. {
  147. // Got the size property.
  148. if(varSize.vt == VT_BSTR)
  149. {
  150. varSize.ChangeType(VT_BSTR, NULL);
  151. {
  152. USES_CONVERSION;
  153. _tcscpy(tchSize,OLE2T(varSize.bstrVal));
  154. }
  155. // Convert this to KB.
  156. // lAvailable = _ttol(tchSize);
  157. llAvailable = _ttoi64(tchSize);
  158. }
  159. else if(varSize.vt == VT_NULL)
  160. {
  161. llAvailable = 0;
  162. }
  163. // lAvailable = lAvailable/KILO;
  164. llAvailable = llAvailable/KILO;
  165. varAvailable = (long)llAvailable;
  166. // Set the Size Property
  167. if (FAILED(pInstance->SetVariant(pAvailable, varAvailable)))
  168. {
  169. // Set Available Space Failed
  170. // Proceed anyway
  171. ErrorTrace(TRACE_ID, "SetVariant on Available Field failed.");
  172. }
  173. }
  174. varFreeSpace.Clear();
  175. varFree.Clear();
  176. hRes = pObj->Get(CComBSTR(L"FreeSpace"),0,&varFreeSpace,NULL,NULL);
  177. if(FAILED(hRes))
  178. {
  179. // Cannot Get the "FreeSpace" Property.
  180. ErrorTrace(TRACE_ID, "GetVariant on Size Field failed.");
  181. }
  182. else
  183. {
  184. // Got the FreeSpace property.
  185. if(varFreeSpace.vt == VT_BSTR)
  186. {
  187. varFreeSpace.ChangeType(VT_BSTR, NULL);
  188. {
  189. USES_CONVERSION;
  190. _tcscpy(tchFreeSpace,OLE2T(varFreeSpace.bstrVal));
  191. }
  192. // Convert this to KB.
  193. // lFreeSpace = _ttol(tchFreeSpace);
  194. llFreeSpace = _ttoi64(tchFreeSpace);
  195. }
  196. else if(varFreeSpace.vt == VT_NULL)
  197. {
  198. llFreeSpace = 0;
  199. }
  200. // lFreeSpace = lFreeSpace/KILO;
  201. llFreeSpace = llFreeSpace/KILO;
  202. // varFreeSpace = (long)llFreeSpace;
  203. // varFree = nFreeSpace;
  204. // varFree = lFreeSpace;
  205. varFree = (long)llFreeSpace;
  206. // Set the Free Property
  207. if (FAILED(pInstance->SetVariant(pFree, varFree)))
  208. {
  209. // Set Free Space Failed
  210. // Proceed anyway
  211. ErrorTrace(TRACE_ID, "SetVariant on Free Field failed.");
  212. }
  213. }
  214. varMediaType = NULL;
  215. hRes = pObj->Get(CComBSTR("MediaType"), 0, &varMediaType, NULL, NULL);
  216. if (FAILED(hRes))
  217. {
  218. // Cannot get MediaType.
  219. ErrorTrace(TRACE_ID, "GetVariant on MediaType Field failed.");
  220. }
  221. else
  222. {
  223. // Got the MediaType
  224. nMediaType = varMediaType.iVal;
  225. if (nMediaType < 0 || nMediaType > maxMediaTypes)
  226. {
  227. //unknown Media Type
  228. nMediaType = 0;
  229. }
  230. varMediaTypeStr = szMediaTypeStrings[nMediaType];
  231. // Set the Media Type Property
  232. if (FAILED(pInstance->SetVariant(pMediaType, varMediaTypeStr)))
  233. {
  234. // Set MediaType Failed
  235. // Proceed anyway
  236. ErrorTrace(TRACE_ID, "SetVariant on MediaType Field failed.");
  237. }
  238. }
  239. hRes = pInstance->Commit();
  240. if (FAILED(hRes))
  241. {
  242. // Could not Set the Change Property
  243. // Continue anyway
  244. ErrorTrace(TRACE_ID, "Commit failed.");
  245. }
  246. }
  247. END :
  248. TraceFunctLeave();
  249. return hRes ;
  250. }