#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);
)
获取PDH性能对象列表之三-------------获取性能对象的属性列表以及实例列表
最新推荐文章于 2015-07-25 08:51:21 发布