1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| typedef HRESULT (*WMICALLBACK)(VARIANT& vtInfo);
HRESULT WmiQueryInfo(LPCWSTR wszCmd, LPCWSTR wszName, WMICALLBACK Callback) { if ((wszCmd == NULL) || (wszName == NULL) || (Callback == NULL)) return S_FALSE; CoInitializeEx(0, COINIT_MULTITHREADED); HRESULT hr = CoInitializeSecurity( NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL); if ((hr != S_OK) && (hr != RPC_E_TOO_LATE)) return hr; IWbemLocator *pLocator = NULL; hr = CoCreateInstance( CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *)&pLocator); if (hr != S_OK) return hr; IWbemServices *pServices = NULL; hr = pLocator->ConnectServer( _bstr_t(L"ROOT\\CIMV2"), NULL, NULL, 0, NULL, 0, 0, &pServices); if (hr != S_OK) { pLocator->Release(); return hr; } hr = CoSetProxyBlanket( pServices, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE); if (hr != S_OK) { pServices->Release(); pLocator->Release(); return hr; } IEnumWbemClassObject* pEnumerator = NULL; hr = pServices->ExecQuery( _bstr_t(L"WQL"), _bstr_t(wszCmd), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator); if (hr != S_OK) { pServices->Release(); pLocator->Release(); return hr; } ULONG uReturn = 0; VARIANT vtInfo = { 0 }; IWbemClassObject* pClassObject = NULL; while (pEnumerator != NULL) { hr = pEnumerator->Next(WBEM_INFINITE, 1, &pClassObject, &uReturn); if ((hr != S_OK) || (uReturn == 0) || (pClassObject == NULL)) break; VariantInit(&vtInfo); hr = pClassObject->Get(wszName, 0, &vtInfo, NULL, NULL); if (hr != S_OK) break; hr = Callback(vtInfo); if (hr != S_OK) break; VariantClear(&vtInfo); } if (pClassObject != NULL) pClassObject->Release(); if (pEnumerator != NULL) pEnumerator->Release(); pServices->Release(); pLocator->Release(); return hr; }
|