#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);
}
PDH性能测试之五--待续
最新推荐文章于 2021-10-16 10:39:52 发布