⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cupagent.cpp

📁 一个网络监视的程序
💻 CPP
字号:
// CupAgent.cpp: implementation of the CupAgent class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "CCAMSC.h"
#include "CupAgent.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CupAgent::CupAgent()
{
	SleepTime=1000;
	State=false;
    //liOldIdleTime = {0,0};
   // liOldSystemTime = {0,0};
}
bool CupAgent::StartAgent()
{
	
    NtQuerySystemInformation = (PROCNTQSI)GetProcAddress(
		GetModuleHandle("ntdll"),
		"NtQuerySystemInformation"
		);
	
    if (!NtQuerySystemInformation){
		AfxMessageBox("CupAgent::StartAgent: GetProcAddress NtQuerySystemInformation  Error");
		return false;
	}
    // get number of processors in the system
    status = NtQuerySystemInformation(SystemBasicInformation,&SysBaseInfo,sizeof(SysBaseInfo),NULL);
    if (status != NO_ERROR){
		AfxMessageBox("CupAgent::StartAgent: NtQuerySystemInformation  Error");
		return false;
	}
	State=true;
	return true;
	
}
double	CupAgent::getUsage()
{
	if (State) {
		status = NtQuerySystemInformation(SystemTimeInformation,&SysTimeInfo,sizeof(SysTimeInfo),0);
        if (status!=NO_ERROR)
            return -1;
		
        // get new CPU's idle time
        status = NtQuerySystemInformation(SystemPerformanceInformation,&SysPerfInfo,sizeof(SysPerfInfo),NULL);
        if (status != NO_ERROR)
            return -1;
		
        // if it's a first call - skip it
		if (liOldIdleTime.QuadPart != 0)
		{
            // CurrentValue = NewValue - OldValue
            dbIdleTime = Li2Double(SysPerfInfo.liIdleTime) - Li2Double(liOldIdleTime);
            dbSystemTime = Li2Double(SysTimeInfo.liKeSystemTime) - Li2Double(liOldSystemTime);
			
            // CurrentCpuIdle = IdleTime / SystemTime
            dbIdleTime = dbIdleTime / dbSystemTime;
			
            // CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors
            dbIdleTime = 100.0 - dbIdleTime * 100.0 / (double)SysBaseInfo.bKeNumberProcessors + 0.5;
			
            
		}
		
        // store new CPU's idle and system time
        liOldIdleTime = SysPerfInfo.liIdleTime;
        liOldSystemTime = SysTimeInfo.liKeSystemTime;
		
		return dbIdleTime;
	}
	
}


























CupAgent::StopAgent()
{
	State=false;
	
}

CupAgent::~CupAgent()
{

}





















⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -