Home | History | Annotate | Download | only in util
      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 
      5 // WMI (Windows Management and Instrumentation) is a big, complex, COM-based
      6 // API that can be used to perform all sorts of things. Sometimes is the best
      7 // way to accomplish something under windows but its lack of an approachable
      8 // C++ interface prevents its use. This collection of fucntions is a step in
      9 // that direction.
     10 // There are two classes; WMIUtil and WMIProcessUtil. The first
     11 // one contain generic helpers and the second one contains the only
     12 // functionality that is needed right now which is to use WMI to launch a
     13 // process.
     14 // To use any function on this header you must call CoInitialize or
     15 // CoInitializeEx beforehand.
     16 //
     17 // For more information about WMI programming:
     18 // http://msdn2.microsoft.com/en-us/library/aa384642(VS.85).aspx
     19 
     20 #ifndef CHROME_INSTALLER_UTIL_WMI_H_
     21 #define CHROME_INSTALLER_UTIL_WMI_H_
     22 
     23 #include <string>
     24 #include <wbemidl.h>
     25 #include "base/strings/string16.h"
     26 
     27 namespace installer {
     28 
     29 class WMI {
     30  public:
     31   // Creates an instance of the WMI service connected to the local computer and
     32   // returns its COM interface. If 'set-blanket' is set to true, the basic COM
     33   // security blanket is applied to the returned interface. This is almost
     34   // always desirable unless you set the parameter to false and apply a custom
     35   // COM security blanket.
     36   // Returns true if succeeded and 'wmi_services': the pointer to the service.
     37   // When done with the interface you must call Release();
     38   static bool CreateLocalConnection(bool set_blanket,
     39                                     IWbemServices** wmi_services);
     40 
     41   // Creates a WMI method using from a WMI class named 'class_name' that
     42   // contains a method named 'method_name'. Only WMI classes that are CIM
     43   // classes can be created using this function.
     44   // Returns true if succeeded and 'class_instance' returns a pointer to the
     45   // WMI method that you can fill with parameter values using SetParameter.
     46   // When done with the interface you must call Release();
     47   static bool CreateClassMethodObject(IWbemServices* wmi_services,
     48                                       const std::wstring& class_name,
     49                                       const std::wstring& method_name,
     50                                       IWbemClassObject** class_instance);
     51 
     52   // Fills a single parameter given an instanced 'class_method'. Returns true
     53   // if operation succeeded. When all the parameters are set the method can
     54   // be executed using IWbemServices::ExecMethod().
     55   static bool SetParameter(IWbemClassObject* class_method,
     56                            const std::wstring& parameter_name,
     57                            VARIANT* parameter);
     58 };
     59 
     60 // This class contains functionality of the WMI class 'Win32_Process'
     61 // more info: http://msdn2.microsoft.com/en-us/library/aa394372(VS.85).aspx
     62 class WMIProcess {
     63  public:
     64   // Creates a new process from 'command_line'. The advantage over CreateProcess
     65   // is that it allows you to always break out from a Job object that the caller
     66   // is attached to even if the Job object flags prevent that.
     67   // Returns true and the process id in process_id if the process is launched
     68   // successful. False otherwise.
     69   // Note that a fully qualified path must be specified in most cases unless
     70   // the program is not in the search path of winmgmt.exe.
     71   // Processes created this way are children of wmiprvse.exe and run with the
     72   // caller credentials.
     73   static bool Launch(const std::wstring& command_line, int* process_id);
     74 };
     75 
     76 // This class contains functionality of the WMI class 'Win32_ComputerSystem'
     77 // more info: http://msdn.microsoft.com/en-us/library/aa394102(VS.85).aspx
     78 class WMIComputerSystem {
     79  public:
     80   // Returns a human readable string for the model/make of this computer.
     81   static string16 GetModel();
     82 };
     83 
     84 }  // namespace installer
     85 
     86 #endif  // CHROME_INSTALLER_UTIL_WMI_H_
     87