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.

160 lines
5.8 KiB

  1. /********************************************************************
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. PCH_RunningTask.CPP
  5. Abstract:
  6. WBEM provider class implementation for PCH_RunningTask class
  7. Revision History:
  8. Ghim-Sim Chua (gschua) 04/27/99
  9. - Created
  10. Jim Martin (a-jammar) 04/30/99
  11. - Updated to retrieve file info from CIM_DataFile
  12. ********************************************************************/
  13. #include "pchealth.h"
  14. #include "PCH_RunningTask.h"
  15. #include <tlhelp32.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_RUNNINGTASK
  24. CPCH_RunningTask MyPCH_RunningTaskSet (PROVIDER_NAME_PCH_RUNNINGTASK, PCH_NAMESPACE) ;
  25. // Property names
  26. //===============
  27. const static WCHAR * pAddress = L"Address" ;
  28. const static WCHAR * pTimeStamp = L"TimeStamp" ;
  29. const static WCHAR * pChange = L"Change" ;
  30. const static WCHAR * pDate = L"Date" ;
  31. const static WCHAR * pDescription = L"Description" ;
  32. const static WCHAR * pManufacturer = L"Manufacturer" ;
  33. const static WCHAR * pName = L"Name" ;
  34. const static WCHAR * pPartOf = L"PartOf" ;
  35. const static WCHAR * pPath = L"Path" ;
  36. const static WCHAR * pSize = L"Size" ;
  37. const static WCHAR * pType = L"Type" ;
  38. const static WCHAR * pVersion = L"Version" ;
  39. //-----------------------------------------------------------------------------
  40. // The EnumerateInstances member function is responsible for reporting each
  41. // instance of the PCH_RunningTask class. This is done by performing a query
  42. // against CIMV2 for all of the Win32_Process instances. Each process instance
  43. // corresponds to a running task, and is used to find a CIM_DataFile instance
  44. // to report file information for each running task.
  45. //-----------------------------------------------------------------------------
  46. typedef HANDLE (*CTH32)(DWORD, DWORD);
  47. HRESULT CPCH_RunningTask::EnumerateInstances(MethodContext* pMethodContext, long lFlags)
  48. {
  49. USES_CONVERSION;
  50. TraceFunctEnter("CPCH_RunningTask::EnumerateInstances");
  51. CComPtr<IEnumWbemClassObject> pEnumInst;
  52. IWbemClassObjectPtr pObj;
  53. CFileVersionInfo fileversioninfo;
  54. SYSTEMTIME stUTCTime;
  55. CComBSTR bstrQuery("SELECT Caption, ExecutablePath FROM Win32_Process");
  56. HRESULT hRes = WBEM_S_NO_ERROR;
  57. LPSTR szFile;
  58. ULONG ulRetVal;
  59. // Get the date and time for the time stamp.
  60. GetSystemTime(&stUTCTime);
  61. // Execute the query against the Win32_Process class. This will give us the
  62. // list of processes running - then we'll get file information for each of
  63. // the processes.
  64. hRes = ExecWQLQuery(&pEnumInst, bstrQuery);
  65. if (FAILED(hRes))
  66. goto END;
  67. // Enumerate each instance of the Win32_Process query.
  68. // CODEWORK: this shouldn't really use WBEM_INFINITE
  69. while (WBEM_S_NO_ERROR == pEnumInst->Next(WBEM_INFINITE, 1, &pObj, &ulRetVal))
  70. {
  71. CInstancePtr pInstance(CreateNewInstance(pMethodContext), false);
  72. // Use the system time to set the timestamp property, and set
  73. // the "Change" field to "Snapshot".
  74. if (!pInstance->SetDateTime(pTimeStamp, WBEMTime(stUTCTime)))
  75. ErrorTrace(TRACE_ID, "SetDateTime on Timestamp Field failed.");
  76. if (!pInstance->SetCHString(pChange, L"Snapshot"))
  77. ErrorTrace(TRACE_ID, "SetCHString on Change Field failed.");
  78. // Copy each property which transfers directly from the source
  79. // class object to the destination CInstance object.
  80. CopyProperty(pObj, L"Caption", pInstance, pName);
  81. CopyProperty(pObj, L"ExecutablePath", pInstance, pPath);
  82. // Get the "ExecutablePath" property, which we'll use to find the
  83. // appropriate CIM_DataFile object.
  84. CComVariant varValue;
  85. CComBSTR bstrExecutablePath("ExecutablePath");
  86. if (FAILED(pObj->Get(bstrExecutablePath, 0, &varValue, NULL, NULL)))
  87. ErrorTrace(TRACE_ID, "GetVariant on ExecutablePath field failed.");
  88. else
  89. {
  90. CComPtr<IWbemClassObject> pFileObj;
  91. CComBSTR ccombstrValue(V_BSTR(&varValue));
  92. if (SUCCEEDED(GetCIMDataFile(ccombstrValue, &pFileObj)))
  93. {
  94. // Using the CIM_DataFile object, copy over the appropriate properties.
  95. CopyProperty(pFileObj, L"Version", pInstance, pVersion);
  96. CopyProperty(pFileObj, L"FileSize", pInstance, pSize);
  97. CopyProperty(pFileObj, L"CreationDate", pInstance, pDate);
  98. CopyProperty(pFileObj, L"Manufacturer", pInstance, pManufacturer);
  99. }
  100. // Use the CFileVersionInfo object to get version attributes.
  101. if (SUCCEEDED(fileversioninfo.QueryFile(ccombstrValue)))
  102. {
  103. if (!pInstance->SetCHString(pDescription, fileversioninfo.GetDescription()))
  104. ErrorTrace(TRACE_ID, "SetCHString on description field failed.");
  105. if (!pInstance->SetCHString(pPartOf, fileversioninfo.GetProduct()))
  106. ErrorTrace(TRACE_ID, "SetCHString on partof field failed.");
  107. }
  108. }
  109. // After all the properties are set, release the instance of the
  110. // class we're getting data from, and commit the new instance.
  111. hRes = pInstance->Commit();
  112. if (FAILED(hRes))
  113. ErrorTrace(TRACE_ID, "Commit on Instance failed.");
  114. }
  115. END:
  116. TraceFunctLeave();
  117. return hRes;
  118. }