#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 getObjects(BOOL bCounter,BOOL bInstance);
void getObjcetItem(LPTSTR ptsObject,BOOL bCounter,BOOL bInstance);
void main(void) {
getObjects(TRUE,FALSE);
getObjects(FALSE,TRUE);
}
void getObjects(BOOL bCounter,BOOL bInstance) {
PDH_STATUS status = ERROR_SUCCESS;
LPTSTR ptsObjectsListBuffer = NULL;
DWORD dwObjectsListSize = 0;
LPTSTR pTemp = NULL;
/*第一次调用,将返回实际的字符数*/
status = PdhEnumObjects(
NULL, // real-time source
NULL, // local machine
ptsObjectsListBuffer, // LPTSTR mszObjectList
&dwObjectsListSize, //LPDWORD pcchBufferLength,
PERF_DETAIL_EXPERT, //DWORD dwDetailLevel
FALSE //BOOL bRefresh
);
/*读取性能对象列表*/
if (status == PDH_MORE_DATA) {
// Allocate the buffers and try the call again.
ptsObjectsListBuffer = (LPTSTR) malloc(dwObjectsListSize * sizeof(TCHAR));
if (NULL != ptsObjectsListBuffer) {
status = PdhEnumObjects(
NULL, // real-time source
NULL, // local machine
ptsObjectsListBuffer, // LPTSTR mszObjectList
&dwObjectsListSize, //LPDWORD pcchBufferLength,
PERF_DETAIL_EXPERT, //DWORD dwDetailLevel
FALSE //BOOL bRefresh
);
if (status == ERROR_SUCCESS) {
for (pTemp = ptsObjectsListBuffer; *pTemp != 0;pTemp += _tcslen(pTemp) + 1)
/*从字符数组中读取字符串的方法*/
{
_tprintf(_T("%s\n"), pTemp);
getObjcetItem(pTemp,bCounter,bInstance);
//wprintf(L"%s\n", pTemp);
}
} else {
_tprintf(_T("Second PdhEnumObjects failed with %0x%x.\n"),
status);
}
} else {
_tprintf(_T("Unable to allocate buffers.\n"));
status = ERROR_OUTOFMEMORY;
}
} else {
_tprintf(_T("\nPdhEnumObjects failed with 0x%x.\n"), status);
}
if (ptsObjectsListBuffer != NULL)
free(ptsObjectsListBuffer);
}
void getObjcetItem(LPTSTR ptsObject,BOOL bCounter,BOOL bInstance) {
PCTSTR strObject =ptsObject;
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, &dwCounterListSize,//如果Buffer为NULL,Size为0
ptsInstanceListBuffer, &dwInstanceListSize,
PERF_DETAIL_WIZARD, // counter detail level
0);
if (status == PDH_MORE_DATA) {
// Allocate the buffers and try the call again.
ptsCounterListBuffer = (LPTSTR) malloc(dwCounterListSize * sizeof(TCHAR));
ptsInstanceListBuffer = (LPTSTR) malloc(dwInstanceListSize * sizeof(TCHAR));
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("_____________________\n"));
_tprintf(_T("Counters ConTain:\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.
if(bCounter){
for (pTemp = ptsCounterListBuffer; *pTemp != 0;pTemp += _tcslen(pTemp) + 1) {
_tprintf(_T("\t"));
_tprintf(_T("%s\n"), pTemp);
//wprintf(L"%s\n", pTemp);
}
}
if(bInstance){
_tprintf(_T("_____________________\n"));
_tprintf(_T("\nInstances Contain:\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 +=_tcslen(pTemp) + 1) {
_tprintf(_T("\t"));
_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性能测试之四-----------获取每个对象的Counter和Instance
最新推荐文章于 2021-10-16 16:23:09 发布