PdhCollectQueryData和获取系统信息

[code=C/C++]/**************************************************************************************

* Source: cpucpu.c/cpp

* Description:

* This program can used to get the percent of CPU used, number of processes or

* theads, memory used, number of TCP connections ....

* Usage:

* The functions described above all depends on the "szPathBuffer"

*

* strcat(szPathBuffer,"//Processor(_Total)//% Processor Time");

*

* (Funtion):(szPathBuffer)

* CPU used percent: //Processor(_Total)//% Processor Time

* Number of process: //System//Processes

* Number of threads: //System//Threads

* Total Memory (Contains virtual memory): //Memory//Commit Limit

* Memory used (Contains virtual memory): //Memory//Committed Bytes

* Number of TCP connections: //TCP//Connections Active

*

* Other Object Items can get by PdhEnumObjects() and PdhEnumObjectItems()

*

* History:

* Jiazhen<luojiazhen#gmail.com> 26/02/2008

**************************************************************************************/

#include <pdh.h>

#include <CONIO.H>

#pragma comment( lib, "pdh.lib")





int main(int argc, char* argv[])

{



while( !_kbhit() )

{



HQUERY hQuery;

HCOUNTER *pCounterHandle;

PDH_STATUS pdhStatus;

PDH_FMT_COUNTERVALUE fmtValue;

DWORD ctrType;

CHAR szPathBuffer[MAXPATH] = {'/0'};

int nCPUUsage;



// Open the query object.

pdhStatus = PdhOpenQuery (0, 0, &hQuery);



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



// Get the CPU used information

strcat(szPathBuffer,"//Processor(_Total)//% Processor Time");



pdhStatus = PdhAddCounter (hQuery,

szPathBuffer,

0,

pCounterHandle);



pdhStatus = PdhCollectQueryData (hQuery);



// Get the current value of this counter.

pdhStatus = PdhGetFormattedCounterValue (*pCounterHandle,

PDH_FMT_DOUBLE,

&ctrType,

&fmtValue);

nCPUUsage = (int)fmtValue.doubleValue;



//fmtValue.doubleValue

if (pdhStatus == ERROR_SUCCESS) {

printf (TEXT(",/"%.20g/"/n"), fmtValue.doubleValue);

}

else {

// Print the error value.

printf ( TEXT("error./"-1/"") );

}





// Close the query.

pdhStatus = PdhCloseQuery (hQuery);



// Output the informaion

// printf("CPU Usage is %d/n", 100-nCPUUsage );

Sleep( 500 );

}

return 0;

}



[/code]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路漫漫其修远.

你的鼓励是我寻找真相的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值