用了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;
}
#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;
}
解决方案 »
- Win32 SDK创建的子窗口怎么响应消息??
- CListCtrl怎么去掉显示文字的部分
- 100分 VC 遍历所有窗口问题
- COM自动化接口的问题,郁闷死我了,分全给了~~~
- 有关象listbox一样的排序功能
- 请问,我用connect()函数连接一个IP地址,却总是返回不成功,可是QQ传文件的时候怎么一连就通呢?
- 枚举线程窗口的问题
- 请教:服务器等待各个客户机的UDP消息该如何设计?
- 一个关于动态改变CDialogBar中控件文字的问题?
- office都为我们提供了什么组件可以使用啊?哪里能找到资料?我想在我的软件中加入VBA功能,Office有这样的组件可用吗?
- 画一个饼形图,显示学生成绩在0~59,60~79,8~89及90分以上的百分比例。
- 在 DOS 下弹出 Windows 窗口
你应该将检测CPU占用情况的代码放到一个线程中,这样一来它占用的CPU资源就不为99%了。