| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -44,6 +44,7 @@ struct _query_data { | |||
| 44 | 44 | LPCWSTR query; | |
| 45 | 45 | HANDLE writePipe; | |
| 46 | 46 | HANDLE readPipe; | |
| 47 | + HANDLE initEvent; | ||
| 47 | 48 | HANDLE connectEvent; | |
| 48 | 49 | }; | |
| 49 | 50 | ||
@@ -81,13 +82,16 @@ _query_thread(LPVOID param) | |||
| 81 | 82 | IID_IWbemLocator, (LPVOID *)&locator | |
| 82 | 83 | ); | |
| 83 | 84 | } | |
| 85 | + if (SUCCEEDED(hr) && !SetEvent(data->initEvent)) { | ||
| 86 | + hr = HRESULT_FROM_WIN32(GetLastError()); | ||
| 87 | + } | ||
| 84 | 88 | if (SUCCEEDED(hr)) { | |
| 85 | 89 | hr = locator->ConnectServer( | |
| 86 | 90 | bstr_t(L"ROOT\\CIMV2"), | |
| 87 | 91 | NULL, NULL, 0, NULL, 0, 0, &services | |
| 88 | 92 | ); | |
| 89 | 93 | } | |
| 90 | - if (!SetEvent(data->connectEvent)) { | ||
| 94 | + if (SUCCEEDED(hr) && !SetEvent(data->connectEvent)) { | ||
| 91 | 95 | hr = HRESULT_FROM_WIN32(GetLastError()); | |
| 92 | 96 | } | |
| 93 | 97 | if (SUCCEEDED(hr)) { | |
@@ -193,6 +197,24 @@ _query_thread(LPVOID param) | |||
| 193 | 197 | } | |
| 194 | 198 | ||
| 195 | 199 | ||
| 200 | + static DWORD | ||
| 201 | + wait_event(HANDLE event, DWORD timeout) | ||
| 202 | + { | ||
| 203 | + DWORD err = 0; | ||
| 204 | + switch (WaitForSingleObject(event, timeout)) { | ||
| 205 | + case WAIT_OBJECT_0: | ||
| 206 | + break; | ||
| 207 | + case WAIT_TIMEOUT: | ||
| 208 | + err = WAIT_TIMEOUT; | ||
| 209 | + break; | ||
| 210 | + default: | ||
| 211 | + err = GetLastError(); | ||
| 212 | + break; | ||
| 213 | + } | ||
| 214 | + return err; | ||
| 215 | + } | ||
| 216 | + | ||
| 217 | + | ||
| 196 | 218 | /*[clinic input] | |
| 197 | 219 | _wmi.exec_query | |
| 198 | 220 | ||
@@ -235,8 +257,11 @@ _wmi_exec_query_impl(PyObject *module, PyObject *query) | |||
| 235 | 257 | ||
| 236 | 258 | Py_BEGIN_ALLOW_THREADS | |
| 237 | 259 | ||
| 260 | + data.initEvent = CreateEvent(NULL, TRUE, FALSE, NULL); | ||
| 238 | 261 | data.connectEvent = CreateEvent(NULL, TRUE, FALSE, NULL); | |
| 239 | - if (!data.connectEvent || !CreatePipe(&data.readPipe, &data.writePipe, NULL, 0)) { | ||
| 262 | + if (!data.initEvent || !data.connectEvent || | ||
| 263 | + !CreatePipe(&data.readPipe, &data.writePipe, NULL, 0)) | ||
| 264 | + { | ||
| 240 | 265 | err = GetLastError(); | |
| 241 | 266 | } else { | |
| 242 | 267 | hThread = CreateThread(NULL, 0, _query_thread, (LPVOID*)&data, 0, NULL); | |
@@ -251,16 +276,12 @@ _wmi_exec_query_impl(PyObject *module, PyObject *query) | |||
| 251 | 276 | // gh-112278: If current user doesn't have permission to query the WMI, the | |
| 252 | 277 | // function IWbemLocator::ConnectServer will hang for 5 seconds, and there | |
| 253 | 278 | // is no way to specify the timeout. So we use an Event object to simulate | |
| 254 | - // a timeout. | ||
| 255 | - switch (WaitForSingleObject(data.connectEvent, 100)) { | ||
| 256 | - case WAIT_OBJECT_0: | ||
| 257 | - break; | ||
| 258 | - case WAIT_TIMEOUT: | ||
| 259 | - err = WAIT_TIMEOUT; | ||
| 260 | - break; | ||
| 261 | - default: | ||
| 262 | - err = GetLastError(); | ||
| 263 | - break; | ||
| 279 | + // a timeout. The initEvent will be set after COM initialization, it will | ||
| 280 | + // take a longer time when first initialized. The connectEvent will be set | ||
| 281 | + // after connected to WMI. | ||
| 282 | + err = wait_event(data.initEvent, 1000); | ||
| 283 | + if (!err) { | ||
| 284 | + err = wait_event(data.connectEvent, 100); | ||
| 264 | 285 | } | |
| 265 | 286 | ||
| 266 | 287 | while (!err) { | |
@@ -306,6 +327,7 @@ _wmi_exec_query_impl(PyObject *module, PyObject *query) | |||
| 306 | 327 | } | |
| 307 | 328 | ||
| 308 | 329 | CloseHandle(hThread); | |
| 330 | + CloseHandle(data.initEvent); | ||
| 309 | 331 | CloseHandle(data.connectEvent); | |
| 310 | 332 | hThread = NULL; | |
| 311 | 333 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments