我正在尝试获取任务管理器中显示的网络适配器利用率。 我现在尝试使用WMI,并且我能够获得每秒每秒接收的字节数/每秒发送的字节数。有没有办法让任务管理器中显示预先计算的值(0-100%),还是可以使用它们来计算该值?
IEnumWbemClassObject* pEnumerator = NULL;
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT * FROM Win32_PerfRawData_Tcpip_NetworkInterface"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);
if (FAILED(hres))
{
cout << "Query for operating system name failed."
<< " Error code = 0x"
<< hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
// Get the data from the query
IWbemClassObject *pclsObj = NULL;
ULONG uReturn = 0;
while (pEnumerator)
{
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
&pclsObj, &uReturn);
if (0 == uReturn)
{
break;
}
VARIANT vtProp;
VariantInit(&vtProp);
hr = pclsObj->Get(L"Name", 0, &vtProp, 0, 0);
wcout << " network adapter : " << vtProp.bstrVal << endl;
VariantClear(&vtProp);
hr = pclsObj->Get(L"BytesReceivedPerSec", 0, &vtProp, 0, 0);
wcout << " BytesReceivedPerSec : " << vtProp.bstrVal << endl;
VariantClear(&vtProp);
hr = pclsObj->Get(L"BytesSentPerSec", 0, &vtProp, 0, 0);
wcout << " BytesSentPerSec : " << vtProp.bstrVal << endl;
VariantClear(&vtProp);
hr = pclsObj->Get(L"Timestamp_Sys100NS", 0, &vtProp, 0, 0);
wcout << " Timestamp_Sys100NS : " << vtProp.bstrVal << endl;
VariantClear(&vtProp);
pclsObj->Release();
}
答案 0 :(得分:0)
您的查询包含字段CurrentBandwidth
,您可以使用该字段计算百分比。
float pct = (BytesReceivedPerSec * 100.f) / (CurrentBandwidth * 800.f)