用了pdh.lib
#include <windows.h>
#include <winperf.h>
#include <malloc.h>
#include <stdio.h>
#include <tchar.h>
#include <pdh.h>#define MAX 80int main ()
{
HQUERY          hQuery;
HCOUNTER        *pCounterHandle;
PDH_STATUS      pdhStatus;
PDH_FMT_COUNTERVALUE  fmtValue;
DWORD          ctrType;
CHAR            szBuffer[MAX] = {'\0'};
int            nRetCode = 0; char str[50]="\\Processor(_Total)\\% Processor Time";

// Open the query object.
pdhStatus = PdhOpenQuery (NULL, 0, &hQuery);

pCounterHandle = (HCOUNTER *)GlobalAlloc(GPTR, sizeof(HCOUNTER));

/*
\\Processor(_Total)\\% Processor Time CPU使用率
\\System\\Processes 当前系统进程数
\\System\\Threads 当前系统线程数
\\Memory\\Commit Limit 总共内存数K (包括虚拟内存)
\\Memory\\Committed Bytes 已用内存数K (包括虚拟内存)
\\TCP\\Connections Active 系统中已建立的 TCP连接个数
其它Object Items 可以利用PdhEnumObjects()和PdhEnumObjectItems()得到
反正我只要用到上面的东东:)
*/

for(int i=0;i<100;i++)

{
strcpy(szBuffer,str); 
pdhStatus = PdhAddCounter (hQuery,
szBuffer, 
0, 
pCounterHandle);
// "Prime" counters that need two values to display a 
//  formatted value.
pdhStatus = PdhCollectQueryData (hQuery);

// Get the current value of this counter.

pdhStatus = PdhGetFormattedCounterValue (*pCounterHandle,
PDH_FMT_LONG ,
&ctrType,
&fmtValue);

//fmtValue.doubleValue为所要的结果
if (pdhStatus == ERROR_SUCCESS)
{
printf (TEXT("%s %ld\n"),str,fmtValue.longValue);
}
else 
{
// Print the error value.
printf (TEXT("error.\"-1\"")); 
}

printf("\n");
Sleep(1000);
}
// Close the query.
pdhStatus = PdhCloseQuery (hQuery);

return nRetCode;
}