void Getsysteminfo(int i ,CString &strinfo)
{
HQUERY hQuery;
HCOUNTER* pCounterHandle;
PDH_STATUS pdhStatus;
PDH_FMT_COUNTERVALUE fmtValue;
DWORD ctrType;
CHAR szPathBuffer[MAXPATH] = {'\0'};
int nRetCode = 0;
CHAR szinfo [MAXPATH] = {'\0'};
strinfo = "\0";
// Open the query object.
pdhStatus = PdhOpenQuery (0, 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()得到
反正我只要用到上面的东西:)
*/
switch (i)
{
case 1 :
strcat(szPathBuffer,"\\System\\Processes");
break;
case 2 :
strcat(szPathBuffer,"\\Processor(_Total)\\% ");
break;
// case 3 :
}
pdhStatus = PdhAddCounter (hQuery,
szPathBuffer,
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) {
sprintf (szinfo, "%d", (int)fmtValue.longValue );
strinfo = (LPTSTR)szinfo ;
}
// else {
// // Print the error value.
// printf (TEXT("error.\"-1\""));
// } // Close the query.
pdhStatus = PdhCloseQuery (hQuery);
{
HQUERY hQuery;
HCOUNTER* pCounterHandle;
PDH_STATUS pdhStatus;
PDH_FMT_COUNTERVALUE fmtValue;
DWORD ctrType;
CHAR szPathBuffer[MAXPATH] = {'\0'};
int nRetCode = 0;
CHAR szinfo [MAXPATH] = {'\0'};
strinfo = "\0";
// Open the query object.
pdhStatus = PdhOpenQuery (0, 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()得到
反正我只要用到上面的东西:)
*/
switch (i)
{
case 1 :
strcat(szPathBuffer,"\\System\\Processes");
break;
case 2 :
strcat(szPathBuffer,"\\Processor(_Total)\\% ");
break;
// case 3 :
}
pdhStatus = PdhAddCounter (hQuery,
szPathBuffer,
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) {
sprintf (szinfo, "%d", (int)fmtValue.longValue );
strinfo = (LPTSTR)szinfo ;
}
// else {
// // Print the error value.
// printf (TEXT("error.\"-1\""));
// } // Close the query.
pdhStatus = PdhCloseQuery (hQuery);
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货