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 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include "base/win/scoped_bstr.h" | 10 #include "base/win/scoped_bstr.h" |
11 #include "base/win/scoped_comptr.h" | 11 #include "base/win/scoped_comptr.h" |
12 #include "base/win/scoped_variant.h" | 12 #include "base/win/scoped_variant.h" |
13 | 13 |
14 using base::win::ScopedVariant; | 14 using base::win::ScopedVariant; |
15 | 15 |
16 namespace installer { | 16 namespace installer { |
17 | 17 |
18 bool WMI::CreateLocalConnection(bool set_blanket, | 18 bool WMI::CreateLocalConnection(bool set_blanket, |
19 IWbemServices** wmi_services) { | 19 IWbemServices** wmi_services) { |
20 base::win::ScopedComPtr<IWbemLocator> wmi_locator; | 20 base::win::ScopedComPtr<IWbemLocator> wmi_locator; |
21 HRESULT hr = wmi_locator.CreateInstance(CLSID_WbemLocator, NULL, | 21 HRESULT hr = wmi_locator.CreateInstance(CLSID_WbemLocator, NULL, |
22 CLSCTX_INPROC_SERVER); | 22 CLSCTX_INPROC_SERVER); |
23 if (FAILED(hr)) | 23 if (FAILED(hr)) |
24 return false; | 24 return false; |
25 | 25 |
26 base::win::ScopedComPtr<IWbemServices> wmi_services_r; | 26 base::win::ScopedComPtr<IWbemServices> wmi_services_r; |
27 hr = wmi_locator->ConnectServer(base::win::ScopedBstr(L"ROOT\\CIMV2"), | 27 hr = wmi_locator->ConnectServer(base::win::ScopedBstr(L"ROOT\\CIMV2"), NULL, |
28 NULL, NULL, 0, NULL, 0, 0, | 28 NULL, 0, NULL, 0, 0, |
29 wmi_services_r.Receive()); | 29 wmi_services_r.GetAddressOf()); |
30 if (FAILED(hr)) | 30 if (FAILED(hr)) |
31 return false; | 31 return false; |
32 | 32 |
33 if (set_blanket) { | 33 if (set_blanket) { |
34 hr = ::CoSetProxyBlanket(wmi_services_r.Get(), RPC_C_AUTHN_WINNT, | 34 hr = ::CoSetProxyBlanket(wmi_services_r.Get(), RPC_C_AUTHN_WINNT, |
35 RPC_C_AUTHZ_NONE, NULL, RPC_C_AUTHN_LEVEL_CALL, | 35 RPC_C_AUTHZ_NONE, NULL, RPC_C_AUTHN_LEVEL_CALL, |
36 RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE); | 36 RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE); |
37 if (FAILED(hr)) | 37 if (FAILED(hr)) |
38 return false; | 38 return false; |
39 } | 39 } |
40 | 40 |
41 *wmi_services = wmi_services_r.Detach(); | 41 *wmi_services = wmi_services_r.Detach(); |
42 return true; | 42 return true; |
43 } | 43 } |
44 | 44 |
45 bool WMI::CreateClassMethodObject(IWbemServices* wmi_services, | 45 bool WMI::CreateClassMethodObject(IWbemServices* wmi_services, |
46 const std::wstring& class_name, | 46 const std::wstring& class_name, |
47 const std::wstring& method_name, | 47 const std::wstring& method_name, |
48 IWbemClassObject** class_instance) { | 48 IWbemClassObject** class_instance) { |
49 // We attempt to instantiate a COM object that represents a WMI object plus | 49 // We attempt to instantiate a COM object that represents a WMI object plus |
50 // a method rolled into one entity. | 50 // a method rolled into one entity. |
51 base::win::ScopedBstr b_class_name(class_name.c_str()); | 51 base::win::ScopedBstr b_class_name(class_name.c_str()); |
52 base::win::ScopedBstr b_method_name(method_name.c_str()); | 52 base::win::ScopedBstr b_method_name(method_name.c_str()); |
53 base::win::ScopedComPtr<IWbemClassObject> class_object; | 53 base::win::ScopedComPtr<IWbemClassObject> class_object; |
54 HRESULT hr; | 54 HRESULT hr; |
55 hr = wmi_services->GetObject(b_class_name, 0, NULL, | 55 hr = wmi_services->GetObject(b_class_name, 0, NULL, |
56 class_object.Receive(), NULL); | 56 class_object.GetAddressOf(), NULL); |
57 if (FAILED(hr)) | 57 if (FAILED(hr)) |
58 return false; | 58 return false; |
59 | 59 |
60 base::win::ScopedComPtr<IWbemClassObject> params_def; | 60 base::win::ScopedComPtr<IWbemClassObject> params_def; |
61 hr = class_object->GetMethod(b_method_name, 0, params_def.Receive(), NULL); | 61 hr = class_object->GetMethod(b_method_name, 0, params_def.GetAddressOf(), |
| 62 NULL); |
62 if (FAILED(hr)) | 63 if (FAILED(hr)) |
63 return false; | 64 return false; |
64 | 65 |
65 if (NULL == params_def.Get()) { | 66 if (NULL == params_def.Get()) { |
66 // You hit this special case if the WMI class is not a CIM class. MSDN | 67 // You hit this special case if the WMI class is not a CIM class. MSDN |
67 // sometimes tells you this. Welcome to WMI hell. | 68 // sometimes tells you this. Welcome to WMI hell. |
68 return false; | 69 return false; |
69 } | 70 } |
70 | 71 |
71 hr = params_def->SpawnInstance(0, class_instance); | 72 hr = params_def->SpawnInstance(0, class_instance); |
72 return(SUCCEEDED(hr)); | 73 return(SUCCEEDED(hr)); |
73 } | 74 } |
74 | 75 |
75 bool SetParameter(IWbemClassObject* class_method, | 76 bool SetParameter(IWbemClassObject* class_method, |
76 const std::wstring& parameter_name, VARIANT* parameter) { | 77 const std::wstring& parameter_name, VARIANT* parameter) { |
77 HRESULT hr = class_method->Put(parameter_name.c_str(), 0, parameter, 0); | 78 HRESULT hr = class_method->Put(parameter_name.c_str(), 0, parameter, 0); |
78 return SUCCEEDED(hr); | 79 return SUCCEEDED(hr); |
79 } | 80 } |
80 | 81 |
81 | 82 |
82 // The code in Launch() basically calls the Create Method of the Win32_Process | 83 // The code in Launch() basically calls the Create Method of the Win32_Process |
83 // CIM class is documented here: | 84 // CIM class is documented here: |
84 // http://msdn2.microsoft.com/en-us/library/aa389388(VS.85).aspx | 85 // http://msdn2.microsoft.com/en-us/library/aa389388(VS.85).aspx |
85 // NOTE: The documentation for the Create method suggests that the ProcessId | 86 // NOTE: The documentation for the Create method suggests that the ProcessId |
86 // parameter and return value are of type uint32_t, but when we call the method | 87 // parameter and return value are of type uint32_t, but when we call the method |
87 // the values in the returned out_params, are VT_I4, which is int32_t. | 88 // the values in the returned out_params, are VT_I4, which is int32_t. |
88 | 89 |
89 bool WMIProcess::Launch(const std::wstring& command_line, int* process_id) { | 90 bool WMIProcess::Launch(const std::wstring& command_line, int* process_id) { |
90 base::win::ScopedComPtr<IWbemServices> wmi_local; | 91 base::win::ScopedComPtr<IWbemServices> wmi_local; |
91 if (!WMI::CreateLocalConnection(true, wmi_local.Receive())) | 92 if (!WMI::CreateLocalConnection(true, wmi_local.GetAddressOf())) |
92 return false; | 93 return false; |
93 | 94 |
94 const wchar_t class_name[] = L"Win32_Process"; | 95 const wchar_t class_name[] = L"Win32_Process"; |
95 const wchar_t method_name[] = L"Create"; | 96 const wchar_t method_name[] = L"Create"; |
96 base::win::ScopedComPtr<IWbemClassObject> process_create; | 97 base::win::ScopedComPtr<IWbemClassObject> process_create; |
97 if (!WMI::CreateClassMethodObject(wmi_local.Get(), class_name, method_name, | 98 if (!WMI::CreateClassMethodObject(wmi_local.Get(), class_name, method_name, |
98 process_create.Receive())) | 99 process_create.GetAddressOf())) |
99 return false; | 100 return false; |
100 | 101 |
101 ScopedVariant b_command_line(command_line.c_str()); | 102 ScopedVariant b_command_line(command_line.c_str()); |
102 | 103 |
103 if (!SetParameter(process_create.Get(), L"CommandLine", | 104 if (!SetParameter(process_create.Get(), L"CommandLine", |
104 b_command_line.AsInput())) | 105 b_command_line.AsInput())) |
105 return false; | 106 return false; |
106 | 107 |
107 base::win::ScopedComPtr<IWbemClassObject> out_params; | 108 base::win::ScopedComPtr<IWbemClassObject> out_params; |
108 HRESULT hr = wmi_local->ExecMethod( | 109 HRESULT hr = wmi_local->ExecMethod( |
109 base::win::ScopedBstr(class_name), base::win::ScopedBstr(method_name), 0, | 110 base::win::ScopedBstr(class_name), base::win::ScopedBstr(method_name), 0, |
110 NULL, process_create.Get(), out_params.Receive(), NULL); | 111 NULL, process_create.Get(), out_params.GetAddressOf(), NULL); |
111 if (FAILED(hr)) | 112 if (FAILED(hr)) |
112 return false; | 113 return false; |
113 | 114 |
114 // We're only expecting int32_t or uint32_t values, so no need for | 115 // We're only expecting int32_t or uint32_t values, so no need for |
115 // ScopedVariant. | 116 // ScopedVariant. |
116 VARIANT ret_value = {{{VT_EMPTY}}}; | 117 VARIANT ret_value = {{{VT_EMPTY}}}; |
117 hr = out_params->Get(L"ReturnValue", 0, &ret_value, NULL, 0); | 118 hr = out_params->Get(L"ReturnValue", 0, &ret_value, NULL, 0); |
118 if (FAILED(hr) || 0 != V_I4(&ret_value)) | 119 if (FAILED(hr) || 0 != V_I4(&ret_value)) |
119 return false; | 120 return false; |
120 | 121 |
121 VARIANT pid = {{{VT_EMPTY}}}; | 122 VARIANT pid = {{{VT_EMPTY}}}; |
122 hr = out_params->Get(L"ProcessId", 0, &pid, NULL, 0); | 123 hr = out_params->Get(L"ProcessId", 0, &pid, NULL, 0); |
123 if (FAILED(hr) || 0 == V_I4(&pid)) | 124 if (FAILED(hr) || 0 == V_I4(&pid)) |
124 return false; | 125 return false; |
125 | 126 |
126 if (process_id) | 127 if (process_id) |
127 *process_id = V_I4(&pid); | 128 *process_id = V_I4(&pid); |
128 | 129 |
129 return true; | 130 return true; |
130 } | 131 } |
131 | 132 |
132 base::string16 WMIComputerSystem::GetModel() { | 133 base::string16 WMIComputerSystem::GetModel() { |
133 base::win::ScopedComPtr<IWbemServices> services; | 134 base::win::ScopedComPtr<IWbemServices> services; |
134 if (!WMI::CreateLocalConnection(true, services.Receive())) | 135 if (!WMI::CreateLocalConnection(true, services.GetAddressOf())) |
135 return base::string16(); | 136 return base::string16(); |
136 | 137 |
137 base::win::ScopedBstr query_language(L"WQL"); | 138 base::win::ScopedBstr query_language(L"WQL"); |
138 base::win::ScopedBstr query(L"SELECT * FROM Win32_ComputerSystem"); | 139 base::win::ScopedBstr query(L"SELECT * FROM Win32_ComputerSystem"); |
139 base::win::ScopedComPtr<IEnumWbemClassObject> enumerator; | 140 base::win::ScopedComPtr<IEnumWbemClassObject> enumerator; |
140 HRESULT hr = services->ExecQuery( | 141 HRESULT hr = |
141 query_language, query, | 142 services->ExecQuery(query_language, query, |
142 WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, | 143 WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, |
143 enumerator.Receive()); | 144 NULL, enumerator.GetAddressOf()); |
144 if (FAILED(hr) || !enumerator.Get()) | 145 if (FAILED(hr) || !enumerator.Get()) |
145 return base::string16(); | 146 return base::string16(); |
146 | 147 |
147 base::win::ScopedComPtr<IWbemClassObject> class_object; | 148 base::win::ScopedComPtr<IWbemClassObject> class_object; |
148 ULONG items_returned = 0; | 149 ULONG items_returned = 0; |
149 hr = enumerator->Next(WBEM_INFINITE, 1, class_object.Receive(), | 150 hr = enumerator->Next(WBEM_INFINITE, 1, class_object.GetAddressOf(), |
150 &items_returned); | 151 &items_returned); |
151 if (!items_returned) | 152 if (!items_returned) |
152 return base::string16(); | 153 return base::string16(); |
153 | 154 |
154 base::win::ScopedVariant manufacturer; | 155 base::win::ScopedVariant manufacturer; |
155 class_object->Get(L"Manufacturer", 0, manufacturer.Receive(), 0, 0); | 156 class_object->Get(L"Manufacturer", 0, manufacturer.Receive(), 0, 0); |
156 base::win::ScopedVariant model; | 157 base::win::ScopedVariant model; |
157 class_object->Get(L"Model", 0, model.Receive(), 0, 0); | 158 class_object->Get(L"Model", 0, model.Receive(), 0, 0); |
158 | 159 |
159 base::string16 model_string; | 160 base::string16 model_string; |
160 if (manufacturer.type() == VT_BSTR) { | 161 if (manufacturer.type() == VT_BSTR) { |
161 model_string = V_BSTR(manufacturer.ptr()); | 162 model_string = V_BSTR(manufacturer.ptr()); |
162 if (model.type() == VT_BSTR) | 163 if (model.type() == VT_BSTR) |
163 model_string += L" "; | 164 model_string += L" "; |
164 } | 165 } |
165 if (model.type() == VT_BSTR) | 166 if (model.type() == VT_BSTR) |
166 model_string += V_BSTR(model.ptr()); | 167 model_string += V_BSTR(model.ptr()); |
167 | 168 |
168 return model_string; | 169 return model_string; |
169 } | 170 } |
170 | 171 |
171 } // namespace installer | 172 } // namespace installer |
OLD | NEW |