获取PDH性能对象列表之三-------------获取性能对象的属性列表以及实例列表

#define _UNICODE /*主要对C标准库函数,将宏替换为宽字节形式*/
#define UNICODE /*主要对Windows API标准函数,将函数宏替换为宽字节形式*/
#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>
#include <malloc.h>
#include <stdio.h>
#include <pdh.h>
#include <pdhmsg.h> /*存放PDH函数返回的错误代码*/
#include <tchar.h> ///

#pragma comment(lib, "pdh.lib")
void main()
{
	PCTSTR strObject=_T("Process");
	PDH_STATUS status = ERROR_SUCCESS;
	LPTSTR ptsCounterListBuffer = NULL;//属性列表
	DWORD dwCounterListSize = 0;
	LPTSTR ptsInstanceListBuffer = NULL;//实例列表
	DWORD dwInstanceListSize = 0;
	LPTSTR pTemp = NULL;

	// Determine the required buffer size for the data.
	status = PdhEnumObjectItems(
	NULL,                   // real-time source
			NULL,                   // local machine
			strObject,         // object to enumerate
			ptsCounterListBuffer,   // pass NULL and 0
			&dwCounterListSize,     // to get required buffer size
			ptsInstanceListBuffer, &dwInstanceListSize,
			PERF_DETAIL_WIZARD,     // counter detail level
			0);

	if (status == PDH_MORE_DATA) {
		// Allocate the buffers and try the call again.
		ptsCounterListBuffer = (LPWSTR) malloc(
				dwCounterListSize * sizeof(WCHAR));
		ptsInstanceListBuffer = (LPWSTR) malloc(
				dwInstanceListSize * sizeof(WCHAR));

		if (NULL != ptsCounterListBuffer && NULL != ptsInstanceListBuffer) {
			status = PdhEnumObjectItems(
					NULL,                   // real-time source
					NULL,                   // local machine
					strObject,         // object to enumerate
					ptsCounterListBuffer, &dwCounterListSize,
					ptsInstanceListBuffer, &dwInstanceListSize,
					PERF_DETAIL_WIZARD,     // counter detail level
					0);

			if (status == ERROR_SUCCESS) {
				_tprintf(_T("Counters that the Process objects defines:\n\n"));
				//wprintf(L"Counters that the Process objects defines:\n\n");

				// Walk the counters list. The list can contain one
				// or more null-terminated strings. The list is terminated
				// using two null-terminator characters.
				for (pTemp = ptsCounterListBuffer; *pTemp != 0;
						pTemp += wcslen(pTemp) + 1) {
					_tprintf(_T("%s\n"),pTemp);
					//wprintf(L"%s\n", pTemp);
				}
				_tprintf(_T("\nInstances of the Process object:\n\n"));
				//wprintf(L"\nInstances of the Process object:\n\n");

				// Walk the instance list. The list can contain one
				// or more null-terminated strings. The list is terminated
				// using two null-terminator characters.
				for (pTemp = ptsInstanceListBuffer; *pTemp != 0;
						pTemp += wcslen(pTemp) + 1) {
					_tprintf(_T("%s\n"),pTemp);
					//wprintf(L"%s\n", pTemp);
				}
			} else {
				_tprintf(_T("Second PdhEnumObjectItems failed with %0x%x.\n"),status);
				//wprintf(L"Second PdhEnumObjectItems failed with %0x%x.\n",status);
			}
		} else {
			_tprintf(_T("Unable to allocate buffers.\n"));
			//wprintf(L"Unable to allocate buffers.\n");
			status = ERROR_OUTOFMEMORY;
		}
	} else {
		_tprintf(_T("\nPdhEnumObjectItems failed with 0x%x.\n"),status);
		//wprintf(L"\nPdhEnumObjectItems failed with 0x%x.\n", status);
	}

	if (ptsCounterListBuffer != NULL)
		free(ptsCounterListBuffer);

	if (ptsInstanceListBuffer != NULL)
		free(ptsInstanceListBuffer);
)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值