PDH可以被用来获取windows各种姿态信息,比如:内存、cpu、硬盘、IO等信息;
其中Counter名称格式不好写,有时会写错,一个比较好的办法是调用 PdhEnumObjectItems, 它会弹出一个窗口让你选择,并根据选择返回counter名称格式,很不错,
参考:https://msdn.microsoft.com/en-us/library/windows/desktop/aa372151%28v=vs.85%29.aspx
//参考代码:
//CONST ULONG SAMPLE_INTERVAL_MS = 1000;
int getWin_SystemInfo_pdh(const char* pCounterPathBuffer, double& dValue)
{
int dRet = 0;
PDH_STATUS Status;
HQUERY Query = NULL;
HCOUNTER Counter;
PDH_FMT_COUNTERVALUE DisplayValue;
DWORD CounterType;
CHAR CounterPathBuffer[PDH_MAX_COUNTER_PATH];
//
// Create a query.
//
Status = PdhOpenQuery(NULL, NULL, &Query);
if (Status != ERROR_SUCCESS)
{
printf("\nPdhOpenQuery failed with status 0x%x.", Status);
dRet = -1;
goto Cleanup;
}
//
// Add the selected counter to the query.
//
//sprintf_s(CounterPathBuffer, "\\Network Interface(*)\\Bytes Received/sec"); //ÍøÂçIO ÊÕ
// "\\Network Interface(*)\\Bytes Sent/sec" //ÍøÂçIO ·¢
// "\\Network Interface(*)\\Bytes Total/sec" //ÍøÂçIO ×Ü
// "\\PhysicalDisk(*)\\Disk Read Bytes/sec" //ŽÅÅÌIO ¶Á
// "\\PhysicalDisk(*)\\Disk Write Bytes/sec" //ŽÅÅÌIO ¶Á
// "\\Processor Information(*)\\% Processor Time" //CPUÕŒÓÃÂÊ
// "\\Memory\\Available MBytes" //ÄÚŽæ Ê£Óà
strcpy_s(CounterPathBuffer, PDH_MAX_COUNTER_PATH, pCounterPathBuffer);
Status = PdhAddCounter(Query, CounterPathBuffer, 0, &Counter);
if (Status != ERROR_SUCCESS)
{
printf("\nPdhAddCounter failed with status 0x%x.", Status);
dRet = -2;
goto Cleanup;
}
//
// Most counters require two sample values to display a formatted value.
// PDH stores the current sample value and the previously collected
// sample value. This call retrieves the first value that will be used
// by PdhGetFormattedCounterValue in the first iteration of the loop
// Note that this value is lost if the counter does not require two
// values to compute a displayable value.
//
Status = PdhCollectQueryData(Query);
if (Status != ERROR_SUCCESS)
{
printf("\nPdhCollectQueryData failed with 0x%x.\n", Status);
dRet = -3;
goto Cleanup;
}
//
// Print counter values until a key is pressed.
//
{
Sleep(1000);
Status = PdhCollectQueryData(Query);
if (Status != ERROR_SUCCESS)
{
printf("\nPdhCollectQueryData failed with status 0x%x.", Status);
dRet = -4;
}
// Compute a displayable value for the counter.
Status = PdhGetFormattedCounterValue(Counter, PDH_FMT_DOUBLE, &CounterType, &DisplayValue);
if (Status != ERROR_SUCCESS)
{
printf("\nPdhGetFormattedCounterValue failed with status 0x%x.", Status);
dRet = -5;
goto Cleanup;
}
printf(",\"%.20g\"", DisplayValue.doubleValue);
dValue = DisplayValue.doubleValue;
dRet = 0;
}
Cleanup:
// Close the query.
if (Query)
{
PdhCloseQuery(Query);
}
return dRet;
}
double getWin_CPU_Usage()
{
int nRet = 0;
double dValue;
nRet = getWin_SystemInfo_pdh("\\Processor Information(*)\\% Processor Time", dValue);
if(nRet == 0)
return dValue;
else
return -1.00;
}
double getWin_DiskRead_Speed()
{
int nRet = 0;
double dValue;
nRet = getWin_SystemInfo_pdh("\\PhysicalDisk(*)\\Disk Read Bytes/sec", dValue);
if(nRet == 0)
return dValue;
else
return -1.00;
}
double getWin_DiskWrite_Speed()
{
int nRet = 0;
double dValue;
nRet = getWin_SystemInfo_pdh("\\PhysicalDisk(*)\\Disk Write Bytes/sec", dValue);
if(nRet == 0)
return dValue;
else
return -1.00;
}
double getWin_NetRead_Speed()
{
int nRet = 0;
double dValue;
nRet = getWin_SystemInfo_pdh("\\Network Interface(*)\\Bytes Received/sec", dValue);
if(nRet == 0)
return dValue;
else
return -1.00;
}
double getWin_NetWrite_Speed()
{
int nRet = 0;
double dValue;
nRet = getWin_SystemInfo_pdh("\\Network Interface(*)\\Bytes Sent/sec", dValue);
if(nRet == 0)
return dValue;
else
return -1.00;
}