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.

230 lines
8.3 KiB

  1. /********************************************************************
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. PCH_Device.CPP
  5. Abstract:
  6. WBEM provider class implementation for PCH_Device class
  7. Revision History:
  8. Ghim-Sim Chua (gschua) 04/27/99
  9. - Created
  10. ********************************************************************/
  11. #include "pchealth.h"
  12. #include "PCH_Device.h"
  13. #include "confgmgr.h"
  14. #include <cregcls.h>
  15. /////////////////////////////////////////////////////////////////////////////
  16. // tracing stuff
  17. #ifdef THIS_FILE
  18. #undef THIS_FILE
  19. #endif
  20. static char __szTraceSourceFile[] = __FILE__;
  21. #define THIS_FILE __szTraceSourceFile
  22. #define TRACE_ID DCID_DEVICE
  23. CPCH_Device MyPCH_DeviceSet (PROVIDER_NAME_PCH_DEVICE, PCH_NAMESPACE) ;
  24. // Property names
  25. //===============
  26. const static WCHAR* pCategory = L"Category" ;
  27. const static WCHAR* pTimeStamp = L"TimeStamp" ;
  28. const static WCHAR* pChange = L"Change" ;
  29. const static WCHAR* pDescription = L"Description" ;
  30. const static WCHAR* pDriveLetter = L"DriveLetter" ;
  31. const static WCHAR* pHWRevision = L"HWRevision" ;
  32. const static WCHAR* pName = L"Name" ;
  33. const static WCHAR* pRegkey = L"Regkey" ;
  34. /*****************************************************************************
  35. *
  36. * FUNCTION : CPCH_Device::EnumerateInstances
  37. *
  38. * DESCRIPTION : Returns all the instances of this class.
  39. *
  40. * INPUTS : A pointer to the MethodContext for communication with WinMgmt.
  41. * A long that contains the flags described in
  42. * IWbemServices::CreateInstanceEnumAsync. Note that the following
  43. * flags are handled by (and filtered out by) WinMgmt:
  44. * WBEM_FLAG_DEEP
  45. * WBEM_FLAG_SHALLOW
  46. * WBEM_FLAG_RETURN_IMMEDIATELY
  47. * WBEM_FLAG_FORWARD_ONLY
  48. * WBEM_FLAG_BIDIRECTIONAL
  49. *
  50. * RETURNS : WBEM_S_NO_ERROR if successful
  51. *
  52. * COMMENTS : TO DO: All instances on the machine should be returned here.
  53. * If there are no instances, return WBEM_S_NO_ERROR.
  54. * It is not an error to have no instances.
  55. *
  56. *****************************************************************************/
  57. HRESULT CPCH_Device::EnumerateInstances(
  58. MethodContext* pMethodContext,
  59. long lFlags
  60. )
  61. {
  62. TraceFunctEnter("CPCH_Device::EnumerateInstances");
  63. CConfigManager cfgManager;
  64. CDeviceCollection deviceList;
  65. HRESULT hRes = WBEM_S_NO_ERROR;
  66. //
  67. // Get the date and time
  68. //
  69. SYSTEMTIME stUTCTime;
  70. GetSystemTime(&stUTCTime);
  71. if ( cfgManager.GetDeviceList( deviceList ) )
  72. {
  73. REFPTR_POSITION pos;
  74. if ( deviceList.BeginEnum( pos ) )
  75. {
  76. try
  77. {
  78. CConfigMgrDevice* pDevice = NULL;
  79. // Walk the list
  80. while ( (NULL != ( pDevice = deviceList.GetNext( pos ) ) ) )
  81. {
  82. try
  83. {
  84. if(IsOneOfMe(pDevice))
  85. {
  86. // Create a new instance based on the passed-in MethodContext
  87. CInstancePtr pInstance(CreateNewInstance(pMethodContext), false);
  88. CHString chstrVar;
  89. if (!pInstance->SetDateTime(pTimeStamp, WBEMTime(stUTCTime)))
  90. ErrorTrace(TRACE_ID, "SetDateTime on Timestamp Field failed.");
  91. if (!pInstance->SetCHString(pChange, L"Snapshot"))
  92. ErrorTrace(TRACE_ID, "SetCHString on Change Field failed.");
  93. // Description
  94. if (pDevice->GetDeviceDesc(chstrVar))
  95. if (!pInstance->SetCHString(pDescription, chstrVar))
  96. ErrorTrace(TRACE_ID, "SetCHString on Description field failed.");
  97. // Name & Regkey
  98. if (pDevice->GetDeviceID(chstrVar))
  99. {
  100. // Name
  101. if (!pInstance->SetCHString(pName, chstrVar))
  102. ErrorTrace(TRACE_ID, "SetCHString on Name field failed.");
  103. // Regkey
  104. CHString chstrTemp("HKEY_LOCAL_MACHINE\\enum\\");
  105. chstrTemp += chstrVar;
  106. if (!pInstance->SetCHString(pRegkey, chstrTemp))
  107. ErrorTrace(TRACE_ID, "SetCHString on Category field failed.");
  108. // try to get the HW Revision
  109. {
  110. CHString chstrKey("enum\\");
  111. chstrKey += chstrVar;
  112. // Open the key in the registry and get the value
  113. CRegistry RegInfo;
  114. CHString strHWRevision;
  115. if (RegInfo.Open(HKEY_LOCAL_MACHINE, chstrKey, KEY_READ) == ERROR_SUCCESS)
  116. {
  117. try
  118. {
  119. if (RegInfo.GetCurrentKeyValue(L"HWRevision", strHWRevision) == ERROR_SUCCESS)
  120. {
  121. if (!pInstance->SetCHString(pHWRevision, strHWRevision))
  122. ErrorTrace(TRACE_ID, "SetCHString on HWRevision field failed.");
  123. }
  124. }
  125. catch(...)
  126. {
  127. RegInfo.Close();
  128. throw;
  129. }
  130. RegInfo.Close();
  131. }
  132. }
  133. }
  134. // Category
  135. if (pDevice->GetClass(chstrVar))
  136. if (!pInstance->SetCHString(pCategory, chstrVar))
  137. ErrorTrace(TRACE_ID, "SetCHString on Category field failed.");
  138. hRes = pInstance->Commit();
  139. if (FAILED(hRes))
  140. ErrorTrace(TRACE_ID, "Commit on Instance failed.");
  141. }
  142. }
  143. catch(...)
  144. {
  145. // GetNext() AddRefs
  146. pDevice->Release();
  147. throw;
  148. }
  149. // GetNext() AddRefs
  150. pDevice->Release();
  151. }
  152. }
  153. catch(...)
  154. {
  155. // Always call EndEnum(). For all Beginnings, there must be an End
  156. deviceList.EndEnum();
  157. throw;
  158. }
  159. // Always call EndEnum(). For all Beginnings, there must be an End
  160. deviceList.EndEnum();
  161. }
  162. }
  163. TraceFunctLeave();
  164. return hRes ;
  165. }
  166. bool CPCH_Device::IsOneOfMe
  167. (
  168. void* pv
  169. )
  170. {
  171. DWORD dwStatus;
  172. CConfigMgrDevice* pDevice = (CConfigMgrDevice*)pv;
  173. // This logic is what the nt5 device manager uses to
  174. // hide what it calls 'hidden' devices. These devices
  175. // can be viewed by using the View/Show Hidden Devices.
  176. if (pDevice->GetConfigFlags( dwStatus ) && // If we can read the status
  177. ((dwStatus & DN_NO_SHOW_IN_DM) == 0) && // Not marked as hidden
  178. ( !(pDevice->IsClass(L"Legacy")) ) // Not legacy
  179. )
  180. {
  181. return true;
  182. }
  183. else
  184. {
  185. // Before we disqualify this device, see if it has any resources.
  186. CResourceCollection resourceList;
  187. pDevice->GetResourceList(resourceList);
  188. return resourceList.GetSize() != 0;
  189. }
  190. }