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

来源:互联网 发布:湘潭大学网络教育 编辑:程序博客网 时间:2024/05/22 06:08
#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 sourceNULL,                   // local machinestrObject,         // object to enumerateptsCounterListBuffer,   // pass NULL and 0&dwCounterListSize,     // to get required buffer sizeptsInstanceListBuffer, &dwInstanceListSize,PERF_DETAIL_WIZARD,     // counter detail level0);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 sourceNULL,                   // local machinestrObject,         // object to enumerateptsCounterListBuffer, &dwCounterListSize,ptsInstanceListBuffer, &dwInstanceListSize,PERF_DETAIL_WIZARD,     // counter detail level0);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