wmi.h 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright (c) 2010 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. // WMI (Windows Management and Instrumentation) is a big, complex, COM-based
  5. // API that can be used to perform all sorts of things. Sometimes is the best
  6. // way to accomplish something under windows but its lack of an approachable
  7. // C++ interface prevents its use. This collection of functions is a step in
  8. // that direction.
  9. // There are two classes; WMIUtil and WMIProcessUtil. The first
  10. // one contains generic helpers and the second one contains the only
  11. // functionality that is needed right now which is to use WMI to launch a
  12. // process.
  13. // To use any function on this header you must call CoInitialize or
  14. // CoInitializeEx beforehand.
  15. //
  16. // For more information about WMI programming:
  17. // http://msdn2.microsoft.com/en-us/library/aa384642(VS.85).aspx
  18. #ifndef BASE_WIN_WMI_H_
  19. #define BASE_WIN_WMI_H_
  20. #include <wbemidl.h>
  21. #include <wrl/client.h>
  22. #include "base/base_export.h"
  23. #include "base/strings/string_piece.h"
  24. namespace base {
  25. namespace win {
  26. // Creates an instance of the WMI service connected to the local computer and
  27. // returns its COM interface. If |set_blanket| is set to true, the basic COM
  28. // security blanket is applied to the returned interface. This is almost
  29. // always desirable unless you set the parameter to false and apply a custom
  30. // COM security blanket.
  31. // Returns true if succeeded and |wmi_services|: the pointer to the service.
  32. BASE_EXPORT bool CreateLocalWmiConnection(
  33. bool set_blanket,
  34. Microsoft::WRL::ComPtr<IWbemServices>* wmi_services);
  35. // Creates a WMI method using from a WMI class named |class_name| that
  36. // contains a method named |method_name|. Only WMI classes that are CIM
  37. // classes can be created using this function.
  38. // Returns true if succeeded and |class_instance| returns a pointer to the
  39. // WMI method that you can fill with parameter values using SetParameter.
  40. BASE_EXPORT bool CreateWmiClassMethodObject(
  41. IWbemServices* wmi_services,
  42. WStringPiece class_name,
  43. WStringPiece method_name,
  44. Microsoft::WRL::ComPtr<IWbemClassObject>* class_instance);
  45. // Creates a new process from |command_line|. The advantage over CreateProcess
  46. // is that it allows you to always break out from a Job object that the caller
  47. // is attached to even if the Job object flags prevent that.
  48. // Returns true and the process id in process_id if the process is launched
  49. // successful. False otherwise.
  50. // Note that a fully qualified path must be specified in most cases unless
  51. // the program is not in the search path of winmgmt.exe.
  52. // Processes created this way are children of wmiprvse.exe and run with the
  53. // caller credentials.
  54. // More info: http://msdn2.microsoft.com/en-us/library/aa394372(VS.85).aspx
  55. BASE_EXPORT bool WmiLaunchProcess(const std::wstring& command_line,
  56. int* process_id);
  57. // An encapsulation of information retrieved from the 'Win32_ComputerSystem' and
  58. // 'Win32_Bios' WMI classes; see :
  59. // https://docs.microsoft.com/en-us/windows/desktop/CIMWin32Prov/win32-computersystem
  60. // https://docs.microsoft.com/en-us/windows/desktop/CIMWin32Prov/win32-systembios
  61. class BASE_EXPORT WmiComputerSystemInfo {
  62. public:
  63. static WmiComputerSystemInfo Get();
  64. const std::wstring& manufacturer() const { return manufacturer_; }
  65. const std::wstring& model() const { return model_; }
  66. const std::wstring& serial_number() const { return serial_number_; }
  67. private:
  68. void PopulateModelAndManufacturer(
  69. const Microsoft::WRL::ComPtr<IWbemServices>& services);
  70. void PopulateSerialNumber(
  71. const Microsoft::WRL::ComPtr<IWbemServices>& services);
  72. std::wstring manufacturer_;
  73. std::wstring model_;
  74. std::wstring serial_number_;
  75. };
  76. } // namespace win
  77. } // namespace base
  78. #endif // BASE_WIN_WMI_H_