使用PDH获取windows状态

本文介绍如何使用PDH(Performance Data Helper)来获取Windows系统的性能数据,包括CPU使用率、磁盘读写速度及网络收发速度等关键指标。通过具体代码示例展示了如何正确配置并收集这些性能计数器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值