| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/installer/util/wmi.h" | 5 #include "chrome/installer/util/wmi.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/win/scoped_bstr.h" | 10 #include "base/win/scoped_bstr.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 return false; | 26 return false; |
| 27 | 27 |
| 28 base::win::ScopedComPtr<IWbemServices> wmi_services_r; | 28 base::win::ScopedComPtr<IWbemServices> wmi_services_r; |
| 29 hr = wmi_locator->ConnectServer(base::win::ScopedBstr(L"ROOT\\CIMV2"), | 29 hr = wmi_locator->ConnectServer(base::win::ScopedBstr(L"ROOT\\CIMV2"), |
| 30 NULL, NULL, 0, NULL, 0, 0, | 30 NULL, NULL, 0, NULL, 0, 0, |
| 31 wmi_services_r.Receive()); | 31 wmi_services_r.Receive()); |
| 32 if (FAILED(hr)) | 32 if (FAILED(hr)) |
| 33 return false; | 33 return false; |
| 34 | 34 |
| 35 if (set_blanket) { | 35 if (set_blanket) { |
| 36 hr = ::CoSetProxyBlanket(wmi_services_r, | 36 hr = ::CoSetProxyBlanket(wmi_services_r.get(), RPC_C_AUTHN_WINNT, |
| 37 RPC_C_AUTHN_WINNT, | 37 RPC_C_AUTHZ_NONE, NULL, RPC_C_AUTHN_LEVEL_CALL, |
| 38 RPC_C_AUTHZ_NONE, | 38 RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE); |
| 39 NULL, | |
| 40 RPC_C_AUTHN_LEVEL_CALL, | |
| 41 RPC_C_IMP_LEVEL_IMPERSONATE, | |
| 42 NULL, | |
| 43 EOAC_NONE); | |
| 44 if (FAILED(hr)) | 39 if (FAILED(hr)) |
| 45 return false; | 40 return false; |
| 46 } | 41 } |
| 47 | 42 |
| 48 *wmi_services = wmi_services_r.Detach(); | 43 *wmi_services = wmi_services_r.Detach(); |
| 49 return true; | 44 return true; |
| 50 } | 45 } |
| 51 | 46 |
| 52 bool WMI::CreateClassMethodObject(IWbemServices* wmi_services, | 47 bool WMI::CreateClassMethodObject(IWbemServices* wmi_services, |
| 53 const std::wstring& class_name, | 48 const std::wstring& class_name, |
| 54 const std::wstring& method_name, | 49 const std::wstring& method_name, |
| 55 IWbemClassObject** class_instance) { | 50 IWbemClassObject** class_instance) { |
| 56 // We attempt to instantiate a COM object that represents a WMI object plus | 51 // We attempt to instantiate a COM object that represents a WMI object plus |
| 57 // a method rolled into one entity. | 52 // a method rolled into one entity. |
| 58 base::win::ScopedBstr b_class_name(class_name.c_str()); | 53 base::win::ScopedBstr b_class_name(class_name.c_str()); |
| 59 base::win::ScopedBstr b_method_name(method_name.c_str()); | 54 base::win::ScopedBstr b_method_name(method_name.c_str()); |
| 60 base::win::ScopedComPtr<IWbemClassObject> class_object; | 55 base::win::ScopedComPtr<IWbemClassObject> class_object; |
| 61 HRESULT hr; | 56 HRESULT hr; |
| 62 hr = wmi_services->GetObject(b_class_name, 0, NULL, | 57 hr = wmi_services->GetObject(b_class_name, 0, NULL, |
| 63 class_object.Receive(), NULL); | 58 class_object.Receive(), NULL); |
| 64 if (FAILED(hr)) | 59 if (FAILED(hr)) |
| 65 return false; | 60 return false; |
| 66 | 61 |
| 67 base::win::ScopedComPtr<IWbemClassObject> params_def; | 62 base::win::ScopedComPtr<IWbemClassObject> params_def; |
| 68 hr = class_object->GetMethod(b_method_name, 0, params_def.Receive(), NULL); | 63 hr = class_object->GetMethod(b_method_name, 0, params_def.Receive(), NULL); |
| 69 if (FAILED(hr)) | 64 if (FAILED(hr)) |
| 70 return false; | 65 return false; |
| 71 | 66 |
| 72 if (NULL == params_def) { | 67 if (NULL == params_def.get()) { |
| 73 // You hit this special case if the WMI class is not a CIM class. MSDN | 68 // You hit this special case if the WMI class is not a CIM class. MSDN |
| 74 // sometimes tells you this. Welcome to WMI hell. | 69 // sometimes tells you this. Welcome to WMI hell. |
| 75 return false; | 70 return false; |
| 76 } | 71 } |
| 77 | 72 |
| 78 hr = params_def->SpawnInstance(0, class_instance); | 73 hr = params_def->SpawnInstance(0, class_instance); |
| 79 return(SUCCEEDED(hr)); | 74 return(SUCCEEDED(hr)); |
| 80 } | 75 } |
| 81 | 76 |
| 82 bool SetParameter(IWbemClassObject* class_method, | 77 bool SetParameter(IWbemClassObject* class_method, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 94 // the values in the returned out_params, are VT_I4, which is int32. | 89 // the values in the returned out_params, are VT_I4, which is int32. |
| 95 | 90 |
| 96 bool WMIProcess::Launch(const std::wstring& command_line, int* process_id) { | 91 bool WMIProcess::Launch(const std::wstring& command_line, int* process_id) { |
| 97 base::win::ScopedComPtr<IWbemServices> wmi_local; | 92 base::win::ScopedComPtr<IWbemServices> wmi_local; |
| 98 if (!WMI::CreateLocalConnection(true, wmi_local.Receive())) | 93 if (!WMI::CreateLocalConnection(true, wmi_local.Receive())) |
| 99 return false; | 94 return false; |
| 100 | 95 |
| 101 const wchar_t class_name[] = L"Win32_Process"; | 96 const wchar_t class_name[] = L"Win32_Process"; |
| 102 const wchar_t method_name[] = L"Create"; | 97 const wchar_t method_name[] = L"Create"; |
| 103 base::win::ScopedComPtr<IWbemClassObject> process_create; | 98 base::win::ScopedComPtr<IWbemClassObject> process_create; |
| 104 if (!WMI::CreateClassMethodObject(wmi_local, class_name, method_name, | 99 if (!WMI::CreateClassMethodObject(wmi_local.get(), class_name, method_name, |
| 105 process_create.Receive())) | 100 process_create.Receive())) |
| 106 return false; | 101 return false; |
| 107 | 102 |
| 108 ScopedVariant b_command_line(command_line.c_str()); | 103 ScopedVariant b_command_line(command_line.c_str()); |
| 109 | 104 |
| 110 if (!SetParameter(process_create, L"CommandLine", b_command_line.AsInput())) | 105 if (!SetParameter(process_create.get(), L"CommandLine", |
| 106 b_command_line.AsInput())) |
| 111 return false; | 107 return false; |
| 112 | 108 |
| 113 base::win::ScopedComPtr<IWbemClassObject> out_params; | 109 base::win::ScopedComPtr<IWbemClassObject> out_params; |
| 114 HRESULT hr = wmi_local->ExecMethod(base::win::ScopedBstr(class_name), | 110 HRESULT hr = wmi_local->ExecMethod( |
| 115 base::win::ScopedBstr(method_name), | 111 base::win::ScopedBstr(class_name), base::win::ScopedBstr(method_name), 0, |
| 116 0, NULL, process_create, | 112 NULL, process_create.get(), out_params.Receive(), NULL); |
| 117 out_params.Receive(), NULL); | |
| 118 if (FAILED(hr)) | 113 if (FAILED(hr)) |
| 119 return false; | 114 return false; |
| 120 | 115 |
| 121 // We're only expecting int32 or uint32 values, so no need for ScopedVariant. | 116 // We're only expecting int32 or uint32 values, so no need for ScopedVariant. |
| 122 VARIANT ret_value = {VT_EMPTY}; | 117 VARIANT ret_value = {VT_EMPTY}; |
| 123 hr = out_params->Get(L"ReturnValue", 0, &ret_value, NULL, 0); | 118 hr = out_params->Get(L"ReturnValue", 0, &ret_value, NULL, 0); |
| 124 if (FAILED(hr) || 0 != V_I4(&ret_value)) | 119 if (FAILED(hr) || 0 != V_I4(&ret_value)) |
| 125 return false; | 120 return false; |
| 126 | 121 |
| 127 VARIANT pid = {VT_EMPTY}; | 122 VARIANT pid = {VT_EMPTY}; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 140 if (!WMI::CreateLocalConnection(true, services.Receive())) | 135 if (!WMI::CreateLocalConnection(true, services.Receive())) |
| 141 return base::string16(); | 136 return base::string16(); |
| 142 | 137 |
| 143 base::win::ScopedBstr query_language(L"WQL"); | 138 base::win::ScopedBstr query_language(L"WQL"); |
| 144 base::win::ScopedBstr query(L"SELECT * FROM Win32_ComputerSystem"); | 139 base::win::ScopedBstr query(L"SELECT * FROM Win32_ComputerSystem"); |
| 145 base::win::ScopedComPtr<IEnumWbemClassObject> enumerator; | 140 base::win::ScopedComPtr<IEnumWbemClassObject> enumerator; |
| 146 HRESULT hr = services->ExecQuery( | 141 HRESULT hr = services->ExecQuery( |
| 147 query_language, query, | 142 query_language, query, |
| 148 WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, | 143 WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, |
| 149 enumerator.Receive()); | 144 enumerator.Receive()); |
| 150 if (FAILED(hr) || !enumerator) | 145 if (FAILED(hr) || !enumerator.get()) |
| 151 return base::string16(); | 146 return base::string16(); |
| 152 | 147 |
| 153 base::win::ScopedComPtr<IWbemClassObject> class_object; | 148 base::win::ScopedComPtr<IWbemClassObject> class_object; |
| 154 ULONG items_returned = 0; | 149 ULONG items_returned = 0; |
| 155 hr = enumerator->Next(WBEM_INFINITE, 1, class_object.Receive(), | 150 hr = enumerator->Next(WBEM_INFINITE, 1, class_object.Receive(), |
| 156 &items_returned); | 151 &items_returned); |
| 157 if (!items_returned) | 152 if (!items_returned) |
| 158 return base::string16(); | 153 return base::string16(); |
| 159 | 154 |
| 160 base::win::ScopedVariant manufacturer; | 155 base::win::ScopedVariant manufacturer; |
| 161 class_object->Get(L"Manufacturer", 0, manufacturer.Receive(), 0, 0); | 156 class_object->Get(L"Manufacturer", 0, manufacturer.Receive(), 0, 0); |
| 162 base::win::ScopedVariant model; | 157 base::win::ScopedVariant model; |
| 163 class_object->Get(L"Model", 0, model.Receive(), 0, 0); | 158 class_object->Get(L"Model", 0, model.Receive(), 0, 0); |
| 164 | 159 |
| 165 base::string16 model_string; | 160 base::string16 model_string; |
| 166 if (manufacturer.type() == VT_BSTR) { | 161 if (manufacturer.type() == VT_BSTR) { |
| 167 model_string = V_BSTR(&manufacturer); | 162 model_string = V_BSTR(&manufacturer); |
| 168 if (model.type() == VT_BSTR) | 163 if (model.type() == VT_BSTR) |
| 169 model_string += L" "; | 164 model_string += L" "; |
| 170 } | 165 } |
| 171 if (model.type() == VT_BSTR) | 166 if (model.type() == VT_BSTR) |
| 172 model_string += V_BSTR(&model); | 167 model_string += V_BSTR(&model); |
| 173 | 168 |
| 174 return model_string; | 169 return model_string; |
| 175 } | 170 } |
| 176 | 171 |
| 177 } // namespace installer | 172 } // namespace installer |
| OLD | NEW |