#ifdef UNICODE
#ifndef _UNICODE
#define _UNICODE  1
#endif
#define tmain     wmain
#else
#define tmain     main
#endif// This program only needs the essential windows header files.
#define WIN32_LEAN_AND_MEAN 1#include <windows.h>
#include <winperf.h>
#include <malloc.h>
#include <stdio.h>
#include <tchar.h>
#include <pdh.h>
#include <PDHMSG.H>
int
tmain ()
{
         LPTSTR szObjects = NULL;
DWORD dwBufferSize = 0;
DWORD dwDetailLevel = 0;
BOOL bRefresh = 0;
pdhStatus = PdhEnumObjects(NULL, NULL, szObjects, &dwBufferSize, 
dwDetailLevel, bRefresh);
         //问题就在上面这个调用,函数返回ERROR_SUCCESS表示成功了,但在dwBufferSize中返回的值
         //却是1,晕死! if (pdhStatus == ERROR_SUCCESS)
{
szObjects = (LPTSTR) malloc(dwBufferSize * sizeof(TCHAR));
if (szObjects == NULL)
return 0;
pdhStatus = PdhEnumObjects(NULL, NULL, szObjects, &dwBufferSize,
dwDetailLevel, bRefresh);
if (pdhStatus == ERROR_SUCCESS)
{
 
for (LPTSTR lpTmp = szObjects; *lpTmp; 
lpTmp += lstrlen(lpTmp) + 1)
{
printf("\n%s", lpTmp);
}
}
else
{
printf("ErrorCode:%d", GetLastError());
}
}
else 
{
printf("ErrorCode:%d", GetLastError());
}
return 0;}

解决方案 »

  1.   

    你的第五个参数对吗?参考Winperf.h中的相关定义,改程序为下面的测试一下:
    #ifdef UNICODE
    #ifndef _UNICODE
    #define _UNICODE  1
    #endif
    #define tmain     wmain
    #else
    #define tmain     main
    #endif// This program only needs the essential windows header files.
    //#define WIN32_LEAN_AND_MEAN 1#include <windows.h>
    #include <winperf.h>
    #include <malloc.h>
    #include <stdio.h>
    #include <tchar.h>
    #include <pdh.h>
    #include <PDHMSG.H>
    int
    tmain ()
    {
    LPTSTR szObjects = NULL;
    DWORD dwBufferSize = 0;
    DWORD dwDetailLevel = 0;
    BOOL bRefresh = FALSE;
    PDH_STATUS pdhStatus = PdhEnumObjects(NULL, NULL, szObjects, &dwBufferSize, 
    PERF_DETAIL_ADVANCED, bRefresh);
    //问题就在上面这个调用,函数返回ERROR_SUCCESS表示成功了,但在dwBufferSize中返回的值
    //却是1,晕死!
    if(pdhStatus == PDH_MORE_DATA)
    {
    printf("PDH_MORE_DATA\n");
    }
    else if(pdhStatus == PDH_INSUFFICIENT_BUFFER )
    {
    printf("PDH_INSUFFICIENT_BUFFER\n");
    }
    else if(pdhStatus == PDH_INVALID_ARGUMENT)
    {
    printf("PDH_INVALID_ARGUMENT\n");
    }
    if (pdhStatus == ERROR_SUCCESS)
    {
    szObjects = (LPTSTR) malloc(dwBufferSize * sizeof(TCHAR));
    if (szObjects == NULL)
    return 0;
    pdhStatus = PdhEnumObjects(NULL, NULL, szObjects, &dwBufferSize,
    dwDetailLevel, bRefresh);
    if (pdhStatus == ERROR_SUCCESS)
    {

    for (LPTSTR lpTmp = szObjects; *lpTmp; 
    lpTmp += lstrlen(lpTmp) + 1)
    {
    printf("\n%s", lpTmp);
    }
    }
    else
    {
    printf("ErrorCode:%d", GetLastError());
    }
    }
    else 
    {
    printf("ErrorCode:%d\n", GetLastError());
    }
    return 0;

    }
      

  2.   

    参考:
    http://search.cpan.org/src/GLENSMALL/Win32-PerfMon-0.07/PerfMon.xs