1 // Copyright (c) 2006-2008 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 BASE_WMI_UTIL_H__ 21 #define BASE_WMI_UTIL_H__ 22 23 #include <string> 24 #include <wbemidl.h> 25 26 class WMIUtil { 27 public: 28 // Creates an instance of the WMI service connected to the local computer and 29 // returns its COM interface. If 'set-blanket' is set to true, the basic COM 30 // security blanket is applied to the returned interface. This is almost 31 // always desirable unless you set the parameter to false and apply a custom 32 // COM security blanket. 33 // Returns true if succeeded and 'wmi_services': the pointer to the service. 34 // When done with the interface you must call Release(); 35 static bool CreateLocalConnection(bool set_blanket, 36 IWbemServices** wmi_services); 37 38 // Creates a WMI method using from a WMI class named 'class_name' that 39 // contains a method named 'method_name'. Only WMI classes that are CIM 40 // classes can be created using this function. 41 // Returns true if succeeded and 'class_instance' returns a pointer to the 42 // WMI method that you can fill with parameter values using SetParameter. 43 // When done with the interface you must call Release(); 44 static bool CreateClassMethodObject(IWbemServices* wmi_services, 45 const std::wstring& class_name, 46 const std::wstring& method_name, 47 IWbemClassObject** class_instance); 48 49 // Fills a single parameter given an instanced 'class_method'. Returns true 50 // if operation succeeded. When all the parameters are set the method can 51 // be executed using IWbemServices::ExecMethod(). 52 static bool SetParameter(IWbemClassObject* class_method, 53 const std::wstring& parameter_name, 54 VARIANT* parameter); 55 }; 56 57 // This class contains functionality of the WMI class 'Win32_Process' 58 // more info: http://msdn2.microsoft.com/en-us/library/aa394372(VS.85).aspx 59 class WMIProcessUtil { 60 public: 61 // Creates a new process from 'command_line'. The advantage over CreateProcess 62 // is that it allows you to always break out from a Job object that the caller 63 // is attached to even if the Job object flags prevent that. 64 // Returns true and the process id in process_id if the process is launched 65 // successful. False otherwise. 66 // Note that a fully qualified path must be specified in most cases unless 67 // the program is not in the search path of winmgmt.exe. 68 // Processes created this way are children of wmiprvse.exe and run with the 69 // caller credentials. 70 static bool Launch(const std::wstring& command_line, int* process_id); 71 }; 72 73 #endif // BASE_WMI_UTIL_H__ 74