获取PDH性能对象的列表之二------------获取Windows所有性能对象

来源:互联网 发布:windows无法访问共享 编辑:程序博客网 时间:2024/06/03 19:48
#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 sourceNULL,// local machinestrBuffer,         // LPTSTR mszObjectList&BufferLength,   //LPDWORD pcchBufferLength,PERF_DETAIL_EXPERT,     //DWORD dwDetailLevelFALSE//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);}

0 0
原创粉丝点击