以下是从MSDN中拷贝下来的示例代码,PdhEnumObjectItems 的调用总是返回PDH_CSTATUS_NO_OBJECT(指定的对象没有找到),于是我打开“管理工具”=》“性能”工具一看,果然根本就没有"Process"这个性能对象(注意不是Processor),只在"Objects"性能对象中找到一个计数器"Processes",晕!怎么会这样啊,MS的示例代码有问题么?????#ifdef UNICODE
#ifndef _UNICODE
#define _UNICODE 1
#endif
#define tmain wmain
#else
#define tmain main
#endif// This program needs only 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 ()
{
PDH_STATUS pdhStatus = ERROR_SUCCESS;
LPTSTR szCounterListBuffer = NULL;
DWORD dwCounterListSize = 0;
LPTSTR szInstanceListBuffer = NULL;
DWORD dwInstanceListSize = 0;
LPTSTR szThisInstance = NULL;
// Determine the required buffer size for the data. pdhStatus = PdhEnumObjectItems (
NULL, // reserved
NULL, // local machine
TEXT("Process"), // object to enumerate
szCounterListBuffer, // pass in NULL buffers
&dwCounterListSize, // 0 to get length
szInstanceListBuffer, // required size
&dwInstanceListSize, // of the buffers in chars
PERF_DETAIL_WIZARD, // counter detail level
0); if (pdhStatus == PDH_MORE_DATA)
{
// Allocate the buffers and try the call again.
szCounterListBuffer = (LPTSTR)malloc (
(dwCounterListSize * sizeof (TCHAR)));
szInstanceListBuffer = (LPTSTR)malloc (
(dwInstanceListSize * sizeof (TCHAR))); if ((szCounterListBuffer != NULL) &&
(szInstanceListBuffer != NULL))
{
pdhStatus = PdhEnumObjectItems (
NULL, // reserved
NULL, // local machine
TEXT("Process"), // object to enumerate
szCounterListBuffer, // passing in NULL buffers
&dwCounterListSize, // passing in 0
szInstanceListBuffer,
&dwInstanceListSize,
PERF_DETAIL_WIZARD, // counter detail level
0);
if (pdhStatus == ERROR_SUCCESS)
{
_tprintf (TEXT("\nRunning Processes:"));
// Walk the return instance list.
for (szThisInstance = szInstanceListBuffer;
*szThisInstance != 0;
szThisInstance += lstrlen(szThisInstance) + 1)
{
_tprintf (TEXT("\n %s"), szThisInstance);
}
}
}
else
{
_tprintf (TEXT("\nPROCLIST: unable to allocate buffers"));
} if (szCounterListBuffer != NULL)
free (szCounterListBuffer); if (szInstanceListBuffer != NULL)
free (szInstanceListBuffer);
}
else
{
_tprintf(TEXT("\nUnable to determine required buffer size."));
}
return 0;
}
#ifndef _UNICODE
#define _UNICODE 1
#endif
#define tmain wmain
#else
#define tmain main
#endif// This program needs only 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 ()
{
PDH_STATUS pdhStatus = ERROR_SUCCESS;
LPTSTR szCounterListBuffer = NULL;
DWORD dwCounterListSize = 0;
LPTSTR szInstanceListBuffer = NULL;
DWORD dwInstanceListSize = 0;
LPTSTR szThisInstance = NULL;
// Determine the required buffer size for the data. pdhStatus = PdhEnumObjectItems (
NULL, // reserved
NULL, // local machine
TEXT("Process"), // object to enumerate
szCounterListBuffer, // pass in NULL buffers
&dwCounterListSize, // 0 to get length
szInstanceListBuffer, // required size
&dwInstanceListSize, // of the buffers in chars
PERF_DETAIL_WIZARD, // counter detail level
0); if (pdhStatus == PDH_MORE_DATA)
{
// Allocate the buffers and try the call again.
szCounterListBuffer = (LPTSTR)malloc (
(dwCounterListSize * sizeof (TCHAR)));
szInstanceListBuffer = (LPTSTR)malloc (
(dwInstanceListSize * sizeof (TCHAR))); if ((szCounterListBuffer != NULL) &&
(szInstanceListBuffer != NULL))
{
pdhStatus = PdhEnumObjectItems (
NULL, // reserved
NULL, // local machine
TEXT("Process"), // object to enumerate
szCounterListBuffer, // passing in NULL buffers
&dwCounterListSize, // passing in 0
szInstanceListBuffer,
&dwInstanceListSize,
PERF_DETAIL_WIZARD, // counter detail level
0);
if (pdhStatus == ERROR_SUCCESS)
{
_tprintf (TEXT("\nRunning Processes:"));
// Walk the return instance list.
for (szThisInstance = szInstanceListBuffer;
*szThisInstance != 0;
szThisInstance += lstrlen(szThisInstance) + 1)
{
_tprintf (TEXT("\n %s"), szThisInstance);
}
}
}
else
{
_tprintf (TEXT("\nPROCLIST: unable to allocate buffers"));
} if (szCounterListBuffer != NULL)
free (szCounterListBuffer); if (szInstanceListBuffer != NULL)
free (szInstanceListBuffer);
}
else
{
_tprintf(TEXT("\nUnable to determine required buffer size."));
}
return 0;
}
原因: 在启动“性能”时运行的测试例程检测到安装的计数器有问题,为防止系统性能下降,这些计数器已被禁用。被禁用的对象和计数器不出现在“添加计数器”对话框中。解决方案: 使用注册表编辑器,将 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Service_name\Performance\Disable Performance Counters 下的值从 1(启用)更改为 0(禁用)。注意,在初始测试之后被禁用的计数器很可能是有问题并可能导致系统性能下降的计数器。有关解决计数器 DLL 问题的详细信息,请参阅 Microsoft Web 站点 (http://msdn.microsoft.com/)。