| OLD | NEW |
| (Empty) |
| 1 // Copyright 2006-2009 Google Inc. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 // you may not use this file except in compliance with the License. | |
| 5 // You may obtain a copy of the License at | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 // ======================================================================== | |
| 15 | |
| 16 | |
| 17 #include "omaha/base/wmi_query.h" | |
| 18 | |
| 19 #include <atlcomcli.h> | |
| 20 #include "omaha/base/debug.h" | |
| 21 #include "omaha/base/logging.h" | |
| 22 #include "omaha/base/utils.h" | |
| 23 | |
| 24 namespace omaha { | |
| 25 | |
| 26 WmiQuery::WmiQuery() : at_end_(true) { | |
| 27 } | |
| 28 | |
| 29 WmiQuery::~WmiQuery() { | |
| 30 } | |
| 31 | |
| 32 HRESULT WmiQuery::Connect(const TCHAR* resource) { | |
| 33 UTIL_LOG(L6, (_T("[WmiQuery::Connect][resource=%s]"), resource)); | |
| 34 ASSERT1(resource && *resource); | |
| 35 | |
| 36 CComBSTR object_path; | |
| 37 HRESULT hr = object_path.Append(resource); | |
| 38 if (FAILED(hr)) { | |
| 39 return hr; | |
| 40 } | |
| 41 | |
| 42 hr = wbem_.CoCreateInstance(__uuidof(WbemLocator)); | |
| 43 if (FAILED(hr)) { | |
| 44 return hr; | |
| 45 } | |
| 46 | |
| 47 // Connect to WMI through the IWbemLocator::ConnectServer method. This | |
| 48 // call can block up to 2 minutes on XP or indefinitely on Windows 2000 if | |
| 49 // the server is broken. | |
| 50 hr = wbem_->ConnectServer(object_path, // object path | |
| 51 NULL, // username | |
| 52 NULL, // password | |
| 53 NULL, // locale | |
| 54 WBEM_FLAG_CONNECT_USE_MAX_WAIT, // security flags | |
| 55 NULL, // authority | |
| 56 0, // context | |
| 57 &service_); // namespace | |
| 58 if (FAILED(hr)) { | |
| 59 return hr; | |
| 60 } | |
| 61 | |
| 62 // Set security levels on the proxy. | |
| 63 hr = ::CoSetProxyBlanket(service_, | |
| 64 RPC_C_AUTHN_WINNT, | |
| 65 RPC_C_AUTHZ_NONE, | |
| 66 NULL, | |
| 67 RPC_C_AUTHN_LEVEL_CALL, | |
| 68 RPC_C_IMP_LEVEL_IMPERSONATE, | |
| 69 NULL, | |
| 70 EOAC_NONE); | |
| 71 if (FAILED(hr)) { | |
| 72 return hr; | |
| 73 } | |
| 74 | |
| 75 return S_OK; | |
| 76 } | |
| 77 | |
| 78 HRESULT WmiQuery::Query(const TCHAR* query) { | |
| 79 UTIL_LOG(L6, (_T("[WmiQuery::Query][query=%s]"), query)); | |
| 80 ASSERT1(query && *query); | |
| 81 | |
| 82 CComBSTR query_language, query_string; | |
| 83 HRESULT hr = query_language.Append(_T("WQL")); | |
| 84 if (FAILED(hr)) { | |
| 85 return hr; | |
| 86 } | |
| 87 hr = query_string.Append(query); | |
| 88 if (FAILED(hr)) { | |
| 89 return hr; | |
| 90 } | |
| 91 | |
| 92 uint32 flags = WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY; | |
| 93 hr = service_->ExecQuery(query_language, | |
| 94 query_string, | |
| 95 flags, | |
| 96 NULL, | |
| 97 &enumerator_); | |
| 98 if (FAILED(hr)) { | |
| 99 return hr; | |
| 100 } | |
| 101 at_end_ = false; | |
| 102 return Next(); | |
| 103 } | |
| 104 | |
| 105 HRESULT WmiQuery::Next() { | |
| 106 UTIL_LOG(L6, (_T("[WmiQuery::Next]"))); | |
| 107 | |
| 108 ASSERT1(!at_end_); | |
| 109 | |
| 110 ULONG ret = 0; | |
| 111 HRESULT hr = enumerator_->Next(WBEM_INFINITE, 1, &obj_, &ret); | |
| 112 if (FAILED(hr)) { | |
| 113 return hr; | |
| 114 } | |
| 115 at_end_ = ret == 0; | |
| 116 return S_OK; | |
| 117 } | |
| 118 | |
| 119 bool WmiQuery::AtEnd() { | |
| 120 return at_end_; | |
| 121 } | |
| 122 | |
| 123 HRESULT WmiQuery::GetValue(const TCHAR* name, CComVariant* value) { | |
| 124 ASSERT1(name && *name); | |
| 125 ASSERT1(value); | |
| 126 ASSERT1(!at_end_ && obj_); | |
| 127 | |
| 128 value->Clear(); | |
| 129 | |
| 130 CComBSTR name_string; | |
| 131 HRESULT hr = name_string.Append(name); | |
| 132 if (FAILED(hr)) { | |
| 133 return hr; | |
| 134 } | |
| 135 hr = obj_->Get(name_string, 0, value, 0, 0); | |
| 136 if (FAILED(hr)) { | |
| 137 return hr; | |
| 138 } | |
| 139 | |
| 140 return S_OK; | |
| 141 } | |
| 142 | |
| 143 HRESULT WmiQuery::GetValue(const TCHAR* name, CString* value) { | |
| 144 ASSERT1(name && *name); | |
| 145 ASSERT1(value); | |
| 146 | |
| 147 CComVariant var; | |
| 148 HRESULT hr = GetValue(name, &var); | |
| 149 if (FAILED(hr)) { | |
| 150 return hr; | |
| 151 } | |
| 152 | |
| 153 ASSERT1(V_VT(&var) == VT_BSTR); | |
| 154 value->SetString(var.bstrVal); | |
| 155 return S_OK; | |
| 156 } | |
| 157 | |
| 158 HRESULT WmiQuery::GetValue(const TCHAR* name, bool* value) { | |
| 159 ASSERT1(name && *name); | |
| 160 ASSERT1(value); | |
| 161 | |
| 162 CComVariant var; | |
| 163 HRESULT hr = GetValue(name, &var); | |
| 164 if (FAILED(hr)) { | |
| 165 return hr; | |
| 166 } | |
| 167 | |
| 168 ASSERT1(V_VT(&var) == VT_BOOL); | |
| 169 *value = var.boolVal != 0; | |
| 170 return S_OK; | |
| 171 } | |
| 172 | |
| 173 HRESULT WmiQuery::GetValue(const TCHAR* name, int* value) { | |
| 174 ASSERT1(name && *name); | |
| 175 ASSERT1(value); | |
| 176 | |
| 177 CComVariant var; | |
| 178 HRESULT hr = GetValue(name, &var); | |
| 179 if (FAILED(hr)) { | |
| 180 return hr; | |
| 181 } | |
| 182 | |
| 183 ASSERT1(V_VT(&var) == VT_I4); | |
| 184 *value = var.lVal; | |
| 185 return S_OK; | |
| 186 } | |
| 187 | |
| 188 HRESULT WmiQuery::GetValue(const TCHAR* name, uint32* value) { | |
| 189 ASSERT1(name && *name); | |
| 190 ASSERT1(value); | |
| 191 | |
| 192 CComVariant var; | |
| 193 HRESULT hr = GetValue(name, &var); | |
| 194 if (FAILED(hr)) { | |
| 195 return hr; | |
| 196 } | |
| 197 | |
| 198 ASSERT1(V_VT(&var) == VT_UI4); | |
| 199 *value = var.ulVal; | |
| 200 return S_OK; | |
| 201 } | |
| 202 | |
| 203 } // namespace omaha | |
| 204 | |
| OLD | NEW |