PDH性能测试之五--待续

#define UNICODE
#define _UNICODE

#include <windows.h>
#include <stdio.h>
#include <pdh.h>
#include <pdhmsg.h>
#include<tchar.h>
#pragma comment(lib, "pdh.lib")
	/*
	新C编程总结-------关于C语言错误处理
	通过返回值返回状态,通过输入参数返回需要的值。
	PDH_STATUS PdhOpenQuery(
	_In_   LPCTSTR szDataSource,//NULL--------取得实时的数据源,字符串---------日志文件的名字。
	_In_   DWORD_PTR dwUserData,//用户自定义的值,通过调用PdhGetCounterInfo()返回PDH_COUNTER_INFO结构的dwQueryUserData字段。
	_Out_  PDH_HQUERY *phQuery)//真正的返回值,返回一个标识查询的句柄。
	------------------------------------------------------
	PDH_STATUS PdhAddCounter(
	_In_   PDH_HQUERY hQuery,
	_In_   LPCTSTR szFullCounterPath,//Counter查询路径(\\Computer\PerfObject(ParentInstance/ObjectInstance#InstanceIndex)\Counter)
	_In_   DWORD_PTR dwUserData,
	_Out_  PDH_HCOUNTER *phCounter);//返回值,标识一个添加到查询的句柄。
	-------------------------------------------------------------
	PDH_STATUS PdhCollectQueryData(---------收集当前所有Counter的原始值,并且更显每个Counter的状态代码。
	_Inout_  PDH_HQUERY hQuery);
	________返回指定Counter的可显示的值___________
	-------------------------------------------------------------
	PDH_STATUS PdhGetFormattedCounterValue(
	_In_   PDH_HCOUNTER hCounter,//该Handle由PdhAddCounter()返回。
	_In_   DWORD dwFormat,//判断Counter值的类型,指定以下值
		PDH_FMT_DOUBLE,浮点型
		PDH_FMT_LARGE,64位整型
		PDH_FMT_LONG	长整型
		可以和以下类型混合使用
		PDH_FMT_NOSCALE		不接受计数器的默认缩放因子
		PDH_FMT_NOCAP100	如果Counter的值大于100,将不会被强制设置到100。默认行为是计数器的上限值限制为100.(相当于百分数)
		PDH_FMT_1000		用1000乘以计数器的值。

	_Out_  LPDWORD lpdwType,//返回一个计数器类型,这个参数是可选的。
	_Out_  PPDH_FMT_COUNTERVALUE pValue//返回一个 PDH_FMT_COUNTERVALUE结构体,这个结构体含有计数器的值。
	-----------------------------------------------------------------
	typedef struct _PDH_FMT_COUNTERVALUE {
		DWORD CStatus;
		union {
			LONG     longValue;
			double   doubleValue;
			LONGLONG largeValue;
			LPCSTR   AnsiStringValue;
			LPCWSTR  WideStringValue;
				};
	} PDH_FMT_COUNTERVALUE, *PPDH_FMT_COUNTERVALUE;

);
*/
CONST PTSTR	COUNTER_PATH=_T("\\Processor(0)\\% Processor Time");

void _tmain(int argc, TCHAR **argv)
{
    HQUERY hQuery = NULL;
    HCOUNTER hCounter = NULL;
    PDH_STATUS status = ERROR_SUCCESS;
    DWORD dwFormat = PDH_FMT_DOUBLE;
    PDH_FMT_COUNTERVALUE ItemBuffer;

    status = PdhOpenQuery(NULL, 0, &hQuery);
    if (ERROR_SUCCESS != status)
    {
        _tprintf(_T("PdhOpenQuery failed with 0x%x\n"), status);
        goto cleanup;
    }

    // Add the same counter used when writing the log file.
    status = PdhAddCounter(hQuery, COUNTER_PATH, 0, &hCounter);

    if (ERROR_SUCCESS != status)
    {
        _tprintf(_T("PdhAddCounter failed with 0x%x\n"), status);
        goto cleanup;
    }

    // Read a performance data record.
    status = PdhCollectQueryData(hQuery);

    if (ERROR_SUCCESS != status)
    {
        _tprintf(_T("PdhCollectQueryData failed with 0x%x\n"), status);
        goto cleanup;
    }

    while (ERROR_SUCCESS == status)
    {
        // Read the next record
        status = PdhCollectQueryData(hQuery);

        if (ERROR_SUCCESS == status)
        {
            // Format the performance data record.
            status = PdhGetFormattedCounterValue(hCounter,
                dwFormat,
                (LPDWORD)NULL,
                &ItemBuffer);

            if (ERROR_SUCCESS != status)
            {
                _tprintf(_T("PdhGetFormattedCounterValue failed with 0x%x.\n"), status);
                goto cleanup;
            }

            _tprintf(_T("Formatted counter value = %.20g\n"), ItemBuffer.doubleValue);
        }
        else
        {
            if (PDH_NO_MORE_DATA != status)
            {
                _tprintf(_T("PdhCollectQueryData failed with 0x%x\n"), status);
            }
        }
    }

cleanup:

    // Close the query.
    if (hQuery)
        PdhCloseQuery(hQuery);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值