#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(void)
{
LPTSTR pTemp = NULL;
PDH_STATUS status = ERROR_SUCCESS;
LPTSTR strBuffer=NULL;
DWORD BufferLength=0;
/*第一次调用,将返回实际的字符数*/
status = PdhEnumObjects(
NULL, // real-time source
NULL, // local machine
strBuffer, // LPTSTR mszObjectList
&BufferLength, //LPDWORD pcchBufferLength,
PERF_DETAIL_EXPERT, //DWORD dwDetailLevel
FALSE//BOOL bRefresh
);
/*读取性能对象列表*/
if (status == PDH_MORE_DATA){
// Allocate the buffers and try the call again.
strBuffer=(LPTSTR)malloc(BufferLength * sizeof(TCHAR));
if (NULL != strBuffer ){
status = PdhEnumObjects(
NULL,// real-time source
NULL,// local machine
strBuffer, // LPTSTR mszObjectList
&BufferLength, //LPDWORD pcchBufferLength,
PERF_DETAIL_EXPERT, //DWORD dwDetailLevel
FALSE//BOOL bRefresh
);
if (status == ERROR_SUCCESS){
for (pTemp =strBuffer; *pTemp != 0; pTemp +=_tcslen(pTemp)+1)
/*从字符数组中读取字符串的方法*/
{
_tprintf(_T("%s\n"),pTemp);
//wprintf(L"%s\n", pTemp);
}
}else{
_tprintf(_T("Second PdhEnumObjects failed with %0x%x.\n"), status);
//wprintf(L"Second PdhEnumObjects 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("\nPdhEnumObjects failed with 0x%x.\n"), status);
//wprintf(L"\nPdhEnumObjects failed with 0x%x.\n", status);
}
if (strBuffer != NULL)
free (strBuffer);
}
获取PDH性能对象的列表之二------------获取Windows所有性能对象
最新推荐文章于 2023-01-12 16:59:30 发布