| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <windows.h> | 5 #include <windows.h> |
| 6 #include <atlbase.h> | 6 #include "base/basictypes.h" |
| 7 #include "base/scoped_bstr_win.h" |
| 8 #include "base/scoped_comptr_win.h" |
| 7 | 9 |
| 8 #pragma comment(lib, "wbemuuid.lib") | 10 #pragma comment(lib, "wbemuuid.lib") |
| 9 | 11 |
| 10 #include "base/wmi_util.h" | 12 #include "base/wmi_util.h" |
| 11 | 13 |
| 14 namespace { |
| 15 // Simple class to manage the lifetime of a variant. |
| 16 // TODO(tommi): Replace this for a more useful class. |
| 17 class VariantHelper : public VARIANT { |
| 18 public: |
| 19 VariantHelper() { |
| 20 vt = VT_EMPTY; |
| 21 } |
| 22 explicit VariantHelper(VARTYPE type) { |
| 23 vt = type; |
| 24 } |
| 25 ~VariantHelper() { |
| 26 ::VariantClear(this); |
| 27 } |
| 28 private: |
| 29 DISALLOW_COPY_AND_ASSIGN(VariantHelper); |
| 30 }; |
| 31 |
| 32 } // namespace |
| 33 |
| 12 bool WMIUtil::CreateLocalConnection(bool set_blanket, | 34 bool WMIUtil::CreateLocalConnection(bool set_blanket, |
| 13 IWbemServices** wmi_services) { | 35 IWbemServices** wmi_services) { |
| 14 CComPtr<IWbemLocator> wmi_locator; | 36 ScopedComPtr<IWbemLocator> wmi_locator; |
| 15 HRESULT hr = wmi_locator.CoCreateInstance(CLSID_WbemLocator, NULL, | 37 HRESULT hr = wmi_locator.CreateInstance(CLSID_WbemLocator, NULL, |
| 16 CLSCTX_INPROC_SERVER); | 38 CLSCTX_INPROC_SERVER); |
| 17 if (FAILED(hr)) | 39 if (FAILED(hr)) |
| 18 return false; | 40 return false; |
| 19 | 41 |
| 20 CComPtr<IWbemServices> wmi_services_r; | 42 ScopedComPtr<IWbemServices> wmi_services_r; |
| 21 hr = wmi_locator->ConnectServer(CComBSTR(L"ROOT\\CIMV2"), NULL, NULL, 0, NULL, | 43 hr = wmi_locator->ConnectServer(StackBstr(L"ROOT\\CIMV2"), NULL, NULL, 0, |
| 22 0, 0, &wmi_services_r); | 44 NULL, 0, 0, wmi_services_r.Receive()); |
| 23 if (FAILED(hr)) | 45 if (FAILED(hr)) |
| 24 return false; | 46 return false; |
| 25 | 47 |
| 26 if (set_blanket) { | 48 if (set_blanket) { |
| 27 hr = ::CoSetProxyBlanket(wmi_services_r, | 49 hr = ::CoSetProxyBlanket(wmi_services_r, |
| 28 RPC_C_AUTHN_WINNT, | 50 RPC_C_AUTHN_WINNT, |
| 29 RPC_C_AUTHZ_NONE, | 51 RPC_C_AUTHZ_NONE, |
| 30 NULL, | 52 NULL, |
| 31 RPC_C_AUTHN_LEVEL_CALL, | 53 RPC_C_AUTHN_LEVEL_CALL, |
| 32 RPC_C_IMP_LEVEL_IMPERSONATE, | 54 RPC_C_IMP_LEVEL_IMPERSONATE, |
| 33 NULL, | 55 NULL, |
| 34 EOAC_NONE); | 56 EOAC_NONE); |
| 35 if (FAILED(hr)) | 57 if (FAILED(hr)) |
| 36 return false; | 58 return false; |
| 37 } | 59 } |
| 38 | 60 |
| 39 *wmi_services = wmi_services_r.Detach(); | 61 *wmi_services = wmi_services_r.Detach(); |
| 40 return true; | 62 return true; |
| 41 } | 63 } |
| 42 | 64 |
| 43 bool WMIUtil::CreateClassMethodObject(IWbemServices* wmi_services, | 65 bool WMIUtil::CreateClassMethodObject(IWbemServices* wmi_services, |
| 44 const std::wstring& class_name, | 66 const std::wstring& class_name, |
| 45 const std::wstring& method_name, | 67 const std::wstring& method_name, |
| 46 IWbemClassObject** class_instance) { | 68 IWbemClassObject** class_instance) { |
| 47 // We attempt to instantiate a COM object that represents a WMI object plus | 69 // We attempt to instantiate a COM object that represents a WMI object plus |
| 48 // a method rolled into one entity. | 70 // a method rolled into one entity. |
| 49 CComBSTR b_class_name(class_name.c_str()); | 71 ScopedBstr b_class_name(class_name.c_str()); |
| 50 CComBSTR b_method_name(method_name.c_str()); | 72 ScopedBstr b_method_name(method_name.c_str()); |
| 51 CComPtr<IWbemClassObject> class_object = NULL; | 73 ScopedComPtr<IWbemClassObject> class_object; |
| 52 HRESULT hr; | 74 HRESULT hr; |
| 53 hr = wmi_services->GetObject(b_class_name, 0, NULL, &class_object, NULL); | 75 hr = wmi_services->GetObject(b_class_name, 0, NULL, |
| 76 class_object.Receive(), NULL); |
| 54 if (FAILED(hr)) | 77 if (FAILED(hr)) |
| 55 return false; | 78 return false; |
| 56 | 79 |
| 57 CComPtr<IWbemClassObject> params_def = NULL; | 80 ScopedComPtr<IWbemClassObject> params_def; |
| 58 hr = class_object->GetMethod(b_method_name, 0, ¶ms_def, NULL); | 81 hr = class_object->GetMethod(b_method_name, 0, params_def.Receive(), NULL); |
| 59 if (FAILED(hr)) | 82 if (FAILED(hr)) |
| 60 return false; | 83 return false; |
| 61 | 84 |
| 62 if (NULL == params_def) { | 85 if (NULL == params_def) { |
| 63 // You hit this special case if the WMI class is not a CIM class. MSDN | 86 // You hit this special case if the WMI class is not a CIM class. MSDN |
| 64 // sometimes tells you this. Welcome to WMI hell. | 87 // sometimes tells you this. Welcome to WMI hell. |
| 65 return false; | 88 return false; |
| 66 } | 89 } |
| 67 | 90 |
| 68 hr = params_def->SpawnInstance(0, class_instance); | 91 hr = params_def->SpawnInstance(0, class_instance); |
| 69 return(SUCCEEDED(hr)); | 92 return(SUCCEEDED(hr)); |
| 70 } | 93 } |
| 71 | 94 |
| 72 bool SetParameter(IWbemClassObject* class_method, | 95 bool SetParameter(IWbemClassObject* class_method, |
| 73 const std::wstring& parameter_name, VARIANT* parameter) { | 96 const std::wstring& parameter_name, VARIANT* parameter) { |
| 74 HRESULT hr = class_method->Put(parameter_name.c_str(), 0, parameter, 0); | 97 HRESULT hr = class_method->Put(parameter_name.c_str(), 0, parameter, 0); |
| 75 return SUCCEEDED(hr); | 98 return SUCCEEDED(hr); |
| 76 } | 99 } |
| 77 | 100 |
| 78 | 101 |
| 79 // The code in Launch() basically calls the Create Method of the Win32_Process | 102 // The code in Launch() basically calls the Create Method of the Win32_Process |
| 80 // CIM class is documented here: | 103 // CIM class is documented here: |
| 81 // http://msdn2.microsoft.com/en-us/library/aa389388(VS.85).aspx | 104 // http://msdn2.microsoft.com/en-us/library/aa389388(VS.85).aspx |
| 82 | 105 |
| 83 bool WMIProcessUtil::Launch(const std::wstring& command_line, int* process_id) { | 106 bool WMIProcessUtil::Launch(const std::wstring& command_line, int* process_id) { |
| 84 CComPtr<IWbemServices> wmi_local; | 107 ScopedComPtr<IWbemServices> wmi_local; |
| 85 if (!WMIUtil::CreateLocalConnection(true, &wmi_local)) | 108 if (!WMIUtil::CreateLocalConnection(true, wmi_local.Receive())) |
| 86 return false; | 109 return false; |
| 87 | 110 |
| 88 const wchar_t class_name[] = L"Win32_Process"; | 111 const wchar_t class_name[] = L"Win32_Process"; |
| 89 const wchar_t method_name[] = L"Create"; | 112 const wchar_t method_name[] = L"Create"; |
| 90 CComPtr<IWbemClassObject> process_create; | 113 ScopedComPtr<IWbemClassObject> process_create; |
| 91 if (!WMIUtil::CreateClassMethodObject(wmi_local, class_name, method_name, | 114 if (!WMIUtil::CreateClassMethodObject(wmi_local, class_name, method_name, |
| 92 &process_create)) | 115 process_create.Receive())) |
| 93 return false; | 116 return false; |
| 94 | 117 |
| 95 CComVariant b_command_line(command_line.c_str()); | 118 VariantHelper b_command_line(VT_BSTR); |
| 119 b_command_line.bstrVal = ::SysAllocString(command_line.c_str()); |
| 120 |
| 96 if (!SetParameter(process_create, L"CommandLine", &b_command_line)) | 121 if (!SetParameter(process_create, L"CommandLine", &b_command_line)) |
| 97 return false; | 122 return false; |
| 98 | 123 |
| 99 CComPtr<IWbemClassObject> out_params; | 124 ScopedComPtr<IWbemClassObject> out_params; |
| 100 HRESULT hr = wmi_local->ExecMethod(CComBSTR(class_name), | 125 HRESULT hr = wmi_local->ExecMethod(StackBstr(class_name), |
| 101 CComBSTR(method_name), 0, NULL, | 126 StackBstr(method_name), 0, NULL, |
| 102 process_create, &out_params, NULL); | 127 process_create, out_params.Receive(), |
| 128 NULL); |
| 103 if (FAILED(hr)) | 129 if (FAILED(hr)) |
| 104 return false; | 130 return false; |
| 105 | 131 |
| 106 CComVariant ret_value; | 132 VariantHelper ret_value; |
| 107 hr = out_params->Get(L"ReturnValue", 0, &ret_value, NULL, 0); | 133 hr = out_params->Get(L"ReturnValue", 0, &ret_value, NULL, 0); |
| 108 if (FAILED(hr) || (0 != ret_value.uintVal)) | 134 if (FAILED(hr) || (0 != ret_value.uintVal)) |
| 109 return false; | 135 return false; |
| 110 | 136 |
| 111 CComVariant pid; | 137 VariantHelper pid; |
| 112 hr = out_params->Get(L"ProcessId", 0, &pid, NULL, 0); | 138 hr = out_params->Get(L"ProcessId", 0, &pid, NULL, 0); |
| 113 if (FAILED(hr) || (0 == pid.intVal)) | 139 if (FAILED(hr) || (0 == pid.intVal)) |
| 114 return false; | 140 return false; |
| 115 | 141 |
| 116 if (process_id) | 142 if (process_id) |
| 117 *process_id = pid.intVal; | 143 *process_id = pid.intVal; |
| 118 | 144 |
| 119 return true; | 145 return true; |
| 120 } | 146 } |
| 121 | 147 |
| OLD | NEW |