CPU占用率

下面是头文件
ExpandedBlockStart.gif ContractedBlock.gif /**/ /************************************************************************/
ExpandedBlockStart.gifContractedBlock.gif
/**/ /* 
InBlock.gif* 作者:cpp
InBlock.gif* 日期:2008-09-05
InBlock.gif* 内容:提供了一个CSystemInformation类,该类可以获取CPU厂家,
InBlock.gif*                CPU频率,CPU个数,当前使用的CPU,系统运行时占用的
InBlock.gif*                CPU比率,系统运行的Idle时间,User时间以及Kernel时间
InBlock.gif*                获取某个进程的句柄个数,获取随机数(比编译器自带更灵活),
InBlock.gif*                以及将长整数(long long )转换为浮点数,宽字节与窄字节之间的互换
InBlock.gif*
InBlock.gif* 注意:有几个函数仅可以用在windows xp系统上,在其他系统上将失效或不起作用
InBlock.gif*                因此,调用时需要注意,另外,如果转入到其他编译器_WIN32_WINNT用来
InBlock.gif*                指示需要windows xp系统
ExpandedBlockEnd.gif
*/

ExpandedBlockStart.gifContractedBlock.gif
/**/ /************************************************************************/
None.gif
None.gif
#pragma  once
None.gif
None.gif#include
< sstream >
None.gif#include 
< cassert >
None.gif
None.gif#ifndef _WINBASE_
None.gif#include 
< WinBase.h >
None.gif
#endif   //  _WINBASE_H
None.gif
None.gif
// #define  _WIN32_WINNT  //  该值已经在stdafx.h中被声明过,只能够用在winxp系统上
None.gif

ExpandedBlockStart.gifContractedBlock.giftypedef 
struct  _SYSTEM_BASIC_INFORMATION  dot.gif {
InBlock.gif    BYTE Reserved1[
24];
InBlock.gif    PVOID Reserved2[
4];
InBlock.gif    CCHAR NumberOfProcessors;
ExpandedBlockEnd.gif}
 SYSTEM_BASIC_INFORMATION;   //  结构等同于SYSTEM_INFO
None.gif

None.giftypedef 
struct
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
long long             IdleTime;
InBlock.gif    DWORD           dwReversed[
76];
ExpandedBlockEnd.gif}
 SYSTEM_PERFORMANCE_INFORMATION;
None.gif
ExpandedBlockStart.gifContractedBlock.giftypedef 
struct  _SYSTEM_PROCESS_INFORMATION  dot.gif {
InBlock.gif    ULONG NextEntryOffset;
InBlock.gif    BYTE Reserved1[
52];
InBlock.gif    PVOID Reserved2[
3];
InBlock.gif    HANDLE UniqueProcessId;
InBlock.gif    PVOID Reserved3;
InBlock.gif    ULONG HandleCount;
InBlock.gif    BYTE Reserved4[
4];
InBlock.gif    PVOID Reserved5[
11];
InBlock.gif    SIZE_T PeakPagefileUsage;
InBlock.gif    SIZE_T PrivatePageCount;
InBlock.gif    LARGE_INTEGER Reserved6[
6];
ExpandedBlockEnd.gif}
 SYSTEM_PROCESS_INFORMATION;
None.gif
None.giftypedef 
struct
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    LARGE_INTEGER liKeBootTime;
InBlock.gif    LARGE_INTEGER liKeSystemTime;
InBlock.gif    LARGE_INTEGER liExpTimeZoneBias;
InBlock.gif    ULONG         uCurrentTimeZoneId;
InBlock.gif    DWORD         dwReserved;
ExpandedBlockEnd.gif}
 SYSTEM_TIME_INFORMATION;   //  等同于GetSystemTimes获取到的三个数据
None.gif

None.gif
class  CSystemInformation
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
public:
InBlock.gif
// 定义基础数据类型
InBlock.gif
    typedef std::basic_string<TCHAR> string;
InBlock.gif    typedef std::basic_stringstream
<TCHAR> stringstream;
InBlock.gif    typedef std::basic_string
<BYTE>  byteString;
InBlock.gif
InBlock.gif
public:
InBlock.gif    
enum SYSTEM_INFORMATION
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        SystemBasicInformation 
= 0,
InBlock.gif        SystemPerformanceInformation 
= 2,
InBlock.gif        SystemTimeInformation  
= 3,
ExpandedSubBlockEnd.gif    }
;
InBlock.gif
protected:
InBlock.gif    typedef DWORD (WINAPI
* NTQUERYSYSTEMINFOMATION) (         
InBlock.gif        SYSTEM_INFORMATION SystemInformationClass,
InBlock.gif        PVOID SystemInformation,
InBlock.gif        ULONG SystemInformationLength,
InBlock.gif        PULONG ReturnLength );
InBlock.gif
InBlock.gif
public:
InBlock.gif    CSystemInformation(
void);
InBlock.gif
public:
InBlock.gif    
~CSystemInformation(void);
InBlock.gif
public:
InBlock.gif    
// 获取基础信息系统,并返回处理
InBlock.gif
    const LPSYSTEM_INFO GetSystemInfo(void);
InBlock.gif
InBlock.gif
protected:
InBlock.gif    
// 处理错误消息
InBlock.gif
    void ProcessErrorMessage(TCHAR* ErrorText);
InBlock.gif
public:
InBlock.gif    
// 产生给定长度的随机数,由windows系统产生,比C编译器产生的更加随机
InBlock.gif
    byteString GenRandom(int len);
InBlock.gif
InBlock.gif
public:
InBlock.gif    
// 返回转换后的wchar_t指针,并返回长度
InBlock.gif
    wchar_t* ToChar(const char* str, int* len);
InBlock.gif
public:
InBlock.gif    
// 返回转换后的char指针,并返回长度
InBlock.gif
    char* ToChar(const wchar_t* str, int* len);
InBlock.gif
public:
InBlock.gif    
// 获取本地机器的CPU硬件信息
InBlock.gif
    string GetCPUCompany(void);
InBlock.gif
public:
InBlock.gif    
// 获取CPU个数
InBlock.gif
    int GetCPUNumbers(void);
InBlock.gif
public:
InBlock.gif    
// 获取当前CPU编号-即当前使用的是哪个CPU
InBlock.gif
    int GetCurrentCPU(void);
InBlock.gif
public:
InBlock.gif    
// 获取某个打开进程的句柄个数
InBlock.gif
    long GetProcessHandleCount(HANDLE hProcess);
InBlock.gif
public:
InBlock.gif    
// 获取系统空闲时间
InBlock.gif
    LPFILETIME GetIdleTime(void);
InBlock.gif
InBlock.gif
public:
InBlock.gif    
// 获取系统核心运行时间
InBlock.gif
    LPFILETIME GetKernelTime(void);
InBlock.gif    
// 获取用户运行时间
InBlock.gif
    LPFILETIME GetUserTime(void);
InBlock.gif
public:
InBlock.gif    
// 获取系统(空闲,核心以及用户)运行时间
InBlock.gif
    void GetSystemTimes(LPFILETIME IdleTime, LPFILETIME KernelTime, LPFILETIME UserTime,  bool ImmediateRet=false);
InBlock.gif
public:
InBlock.gif    
// 获取CPU占用率
InBlock.gif
    double GetCPURate(void);
InBlock.gif
public:
InBlock.gif    
// 将64位的长整数转换为64位的实数
InBlock.gif
    double LongInt2Double(LARGE_INTEGER want);
InBlock.gif
public:
InBlock.gif    
double LongInt2Double(long long want)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        LARGE_INTEGER _want;
InBlock.gif        memcpy( 
&_want, &want, sizeof(LARGE_INTEGER) );
InBlock.gif        
return LongInt2Double(  _want );
ExpandedSubBlockEnd.gif    }

InBlock.gif    
double LongInt2Double(FILETIME want)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{        
InBlock.gif        LARGE_INTEGER _want;
InBlock.gif        memcpy( 
&_want, &want, sizeof(LARGE_INTEGER) );
InBlock.gif        
return LongInt2Double( _want);
ExpandedSubBlockEnd.gif    }

InBlock.gif
public:
InBlock.gif    
// 获取CPU的频率,高字节部分为主频,低字节部分为次频,以GHz为单位
InBlock.gif
    int GetCPUFrenquence(void);
InBlock.gif
public:
InBlock.gif    
// 获取当前使用的CPU编号
InBlock.gif
    int GetCurrentUseCPU(void);
InBlock.gif
InBlock.gif
public:
InBlock.gif    
// 获取注册表所能够达到的最大尺寸,以字节为单位
InBlock.gif
    long GetMaxRegisterSize(void);
InBlock.gif
public:
InBlock.gif    
// 获取当前注册表的尺寸,以字节为单位
InBlock.gif
    long GetCurrRegisterSize(void);
InBlock.gif
InBlock.gif
private:
InBlock.gif    
long long  m_IdleTime;
InBlock.gif    
long long m_KernelTime;
InBlock.gif    
long long  m_UserTime;
InBlock.gif    
// 用来表示是否已经获取了系统的信息,true为已经获取,false为没有获取
InBlock.gif
    bool m_bHasSystemInfo;
InBlock.gif    wchar_t    
*m_pWChar;
InBlock.gif    
char    *m_pchar;
InBlock.gif
private:
InBlock.gif    
// 系统信息
InBlock.gif
    SYSTEM_INFO m_system_info;
InBlock.gif
private:
InBlock.gif    
// NTDLL.DLL 句柄
InBlock.gif
    HMODULE m_hNtDll;
InBlock.gif    
// NtQuerySystemInformation 函数地址
InBlock.gif
    NTQUERYSYSTEMINFOMATION NtQuerySystemInformation; 
ExpandedBlockEnd.gif}
;
None.gif
下面是实现文件:
 
ExpandedBlockStart.gif ContractedBlock.gif /**/ /************************************************************************/
ExpandedBlockStart.gifContractedBlock.gif
/**/ /* 
InBlock.gif* 作者:cpp
InBlock.gif* 日期:2008-09-05
InBlock.gif* 内容:提供了一个CSystemInformation类,该类可以获取CPU厂家,
InBlock.gif*                CPU频率,CPU个数,当前使用的CPU,系统运行时占用的
InBlock.gif*                CPU比率,系统运行的Idle时间,User时间以及Kernel时间
InBlock.gif*                获取某个进程的句柄个数,获取随机数(比编译器自带更灵活),
InBlock.gif*                将长整数(long long )转换为浮点数,宽字节与窄字节之间的互换
InBlock.gif*                注册表最大容量,当前注册表容量
ExpandedBlockEnd.gif
*/

ExpandedBlockStart.gifContractedBlock.gif
/**/ /************************************************************************/
None.gif
None.gif
// #include "stdafx.h"
None.gif
#include  < windows.h >
None.gif#include 
" SystemInformation.h "
None.gif#include 
< WinCrypt.h >
None.gif#include 
< winbase.h >
None.gif
None.gifCSystemInformation::CSystemInformation()
None.gif: m_bHasSystemInfo(
false )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    m_pWChar 
= NULL;
InBlock.gif    m_pchar 
= NULL;
InBlock.gif    m_hNtDll 
= NULL;
InBlock.gif
InBlock.gif    m_bHasSystemInfo 
= false;
InBlock.gif
InBlock.gif    ::memset( 
&m_system_info, 0sizeof(m_system_info));
InBlock.gif
InBlock.gif    m_hNtDll 
= ::LoadLibrary(TEXT("ntdll.dll"));
InBlock.gif    
if( NULL == m_hNtDll)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
this->ProcessErrorMessage(TEXT("没有载入ntdll.dll文件!"));
InBlock.gif        
return ;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
this->NtQuerySystemInformation = (NTQUERYSYSTEMINFOMATION) GetProcAddress(
InBlock.gif            m_hNtDll, 
"NtQuerySystemInformation"
InBlock.gif        );
InBlock.gif    
if( NULL== NtQuerySystemInformation )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
this->ProcessErrorMessage(TEXT("没有找到NtQuerySystemInformation地址!"));
InBlock.gif        
return ;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
ExpandedBlockEnd.gif}

None.gif
None.gifCSystemInformation::
~ CSystemInformation()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
if( NULL != m_pWChar)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        delete []m_pWChar;
InBlock.gif        m_pWChar 
= NULL;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
if( NULL != m_pchar )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        delete []m_pchar;
InBlock.gif        m_pchar 
= NULL;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif    
if( NULL != m_hNtDll )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        FreeLibrary(m_hNtDll);
InBlock.gif        m_hNtDll 
= NULL;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gif
void  CSystemInformation::ProcessErrorMessage( TCHAR *  ErrorText )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    LPVOID lpMsgBuf;
InBlock.gif
InBlock.gif    FormatMessage( 
InBlock.gif        FORMAT_MESSAGE_ALLOCATE_BUFFER 
| FORMAT_MESSAGE_FROM_SYSTEM,
InBlock.gif        NULL,
InBlock.gif        GetLastError(),
InBlock.gif        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 
// Default language
InBlock.gif
        (LPTSTR) &lpMsgBuf,
InBlock.gif        
0,
InBlock.gif        NULL 
InBlock.gif        );
InBlock.gif    stringstream ss;
InBlock.gif    ss 
<< TEXT("警告:"<< ErrorText << TEXT("存在下面的错误:\n")
InBlock.gif        
<< lpMsgBuf << std::endl;
InBlock.gif    MessageBox(NULL, ss.str().c_str(), TEXT(
"程序错误"), MB_ICONSTOP);
InBlock.gif    LocalFree(lpMsgBuf);
ExpandedBlockEnd.gif}

None.gif
None.gif
const  LPSYSTEM_INFO CSystemInformation::GetSystemInfo(  void  )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    ::GetSystemInfo( 
&m_system_info );
InBlock.gif    m_bHasSystemInfo 
= true;
InBlock.gif    
return &m_system_info;
ExpandedBlockEnd.gif}

None.gif
None.gifCSystemInformation::byteString CSystemInformation::GenRandom( 
int  len )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    HCRYPTPROV hCryptProc;
InBlock.gif    TCHAR
*     UserName = TEXT("MyKeyContainer");
InBlock.gif    
if! CryptAcquireContext(&hCryptProc, UserName, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET ) )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        ProcessErrorMessage(TEXT(
"没有得到加密锁!"));
InBlock.gif        
return byteString();
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    byteString bstr;
InBlock.gif    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        BYTE 
*pbBuffer = new BYTE[len];
InBlock.gif        
if!CryptGenRandom( hCryptProc, len, pbBuffer) )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ProcessErrorMessage(TEXT(
"没有产生得到随机数!"));
ExpandedSubBlockEnd.gif        }

InBlock.gif        bstr 
= pbBuffer;
ExpandedSubBlockEnd.gif    }
catch(dot.gif)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        ProcessErrorMessage(TEXT(
"GenRandom--没有足够内存!"));
ExpandedSubBlockEnd.gif    }

InBlock.gif    CryptReleaseContext(hCryptProc,
0);
InBlock.gif    
return bstr;
ExpandedBlockEnd.gif}

None.gif
None.gifwchar_t
*  CSystemInformation::ToChar(  const   char *  str,  int *  len )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
int strLen = strlen(str);
InBlock.gif    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if ( NULL != m_pWChar)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
ifsizeof(m_pWChar)/sizeof(m_pWChar[0]) < strLen+1 )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                delete[] m_pWChar;
InBlock.gif                m_pWChar 
= new wchar_t[strLen+1 ];
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }
 
InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif                m_pWChar 
= new wchar_t[strLen+1 ];
ExpandedSubBlockEnd.gif        }

InBlock.gif        
*len = strLen + 1;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
catch(dot.gif)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        ProcessErrorMessage(TEXT(
"没有足够内存!"));
InBlock.gif        
*len = 0;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
return m_pWChar;
ExpandedBlockEnd.gif}

None.gif
None.gif
char *  CSystemInformation::ToChar(  const  wchar_t *  str,  int *  len )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
int wStrLen = ::wcslen(str);
InBlock.gif    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if ( sizeof(m_pchar)/sizeof(m_pchar[0]) < wStrLen+1 )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            delete [] m_pchar;
InBlock.gif            m_pchar 
= new char [ wStrLen+1];
ExpandedSubBlockEnd.gif        }
 
InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            m_pchar 
= new char [ wStrLen+1];
ExpandedSubBlockEnd.gif        }

InBlock.gif        
*len = wStrLen;        
ExpandedSubBlockEnd.gif    }

InBlock.gif    
catch(dot.gif)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        ProcessErrorMessage(TEXT(
"没有足够内存!"));
InBlock.gif        
*len = 0;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
return m_pchar;
ExpandedBlockEnd.gif}

None.gif
//  获取本地机器的CPU类型
None.gif
CSystemInformation:: string  CSystemInformation::GetCPUCompany(  void  )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
if! m_bHasSystemInfo )
InBlock.gif        
this->GetSystemInfo();
InBlock.gif
InBlock.gif    WORD processor 
= HIWORD( m_system_info.dwOemId );
InBlock.gif
InBlock.gif    stringstream ss;
InBlock.gif    
switch ( processor )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif    
case PROCESSOR_ARCHITECTURE_INTEL:
InBlock.gif        ss 
<< TEXT("Intel CPU")   ; 
InBlock.gif            
break;
InBlock.gif    
case PROCESSOR_ARCHITECTURE_AMD64:
InBlock.gif            ss 
<<TEXT("Amd64 CPU") ;
InBlock.gif            
break;
InBlock.gif    
case PROCESSOR_ARCHITECTURE_ARM:
InBlock.gif            ss 
<<TEXT("Arm CPU")  ;            
InBlock.gif            
break;
InBlock.gif    
case PROCESSOR_ARCHITECTURE_ALPHA:
InBlock.gif            ss 
<<TEXT("Alpha CPU") ;        
InBlock.gif            
break;
InBlock.gif    
case PROCESSOR_ARCHITECTURE_IA64:
InBlock.gif            ss 
<<TEXT("IA64 CPU") ;
InBlock.gif            
break;
InBlock.gif    
default:
InBlock.gif            ss 
<<  TEXT("unknown CPU") ;
InBlock.gif            
break;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
return ss.str();
ExpandedBlockEnd.gif}

None.gif
None.gif
//  获取CPU个数
None.gif
int  CSystemInformation::GetCPUNumbers( void )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
if!m_bHasSystemInfo )
InBlock.gif        
this->GetSystemInfo();
InBlock.gif
InBlock.gif    
return m_system_info.dwNumberOfProcessors;
ExpandedBlockEnd.gif}

None.gif
None.gif
//  获取当前CPU编号-即当前使用的是哪个CPU
None.gif
//  系统最多支持32个CPU
None.gif
int  CSystemInformation::GetCurrentCPU( void )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
if!m_bHasSystemInfo )
InBlock.gif        
this->GetSystemInfo();
InBlock.gif
InBlock.gif    
int current_cpu = 1;
InBlock.gif    
for(int i=0; i< 32; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if( m_system_info.dwActiveProcessorMask & ( 1<<i) )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            current_cpu 
= i;
InBlock.gif            
break;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
return current_cpu+1;
ExpandedBlockEnd.gif}

None.gif
None.gif
//  获取某个打开进程的句柄个数
None.gif
long  CSystemInformation::GetProcessHandleCount(HANDLE hProcess)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    DWORD dwProcessCount 
= 0;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*    if( !::GetProcessHandleCount(hProcess,&dwProcessCount) )
InBlock.gif    {
InBlock.gif        ProcessErrorMessage(TEXT("获取进程内句柄个数失败!"));
InBlock.gif        dwProcessCount = 0;
InBlock.gif    }
ExpandedSubBlockEnd.gif
*/

InBlock.gif    
return dwProcessCount;
ExpandedBlockEnd.gif}

None.gif
None.gif
//  获取系统空闲时间
None.gif
LPFILETIME CSystemInformation::GetIdleTime( void )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*
InBlock.gif    if( !::GetSystemTimes((LPFILETIME)&m_IdleTime,(LPFILETIME) &m_KernelTime, (LPFILETIME)&m_UserTime))
InBlock.gif    {
InBlock.gif            ProcessErrorMessage(TEXT("获取系统运行时间错误!"));
InBlock.gif    }
ExpandedSubBlockEnd.gif
*/

InBlock.gif    
return (LPFILETIME)&m_IdleTime;
ExpandedBlockEnd.gif}

None.gif
None.gif
//  获取系统核心运行时间
None.gif
LPFILETIME CSystemInformation::GetKernelTime( void )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*
InBlock.gif    if(! ::GetSystemTimes((LPFILETIME)&m_IdleTime,(LPFILETIME) &m_KernelTime, (LPFILETIME)&m_UserTime))
InBlock.gif    {
InBlock.gif            ProcessErrorMessage(TEXT("获取系统运行时间错误!"));
InBlock.gif    }
ExpandedSubBlockEnd.gif
*/

InBlock.gif    
return (LPFILETIME)&m_KernelTime;
ExpandedBlockEnd.gif}

None.gif
None.gif
//  获取用户运行时间
None.gif
LPFILETIME CSystemInformation::GetUserTime( void )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//*
InBlock.gif    if( ! ::GetSystemTimes((LPFILETIME)&m_IdleTime, (LPFILETIME)&m_KernelTime, (LPFILETIME)&m_UserTime) )
InBlock.gif    {
InBlock.gif            ProcessErrorMessage(TEXT("获取系统运行时间错误!"));
InBlock.gif    }
ExpandedSubBlockEnd.gif  
*/

InBlock.gif    
return (LPFILETIME)&m_UserTime;
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
//  同时获取获取系统(空闲,核心以及用户)运行时间
ExpandedBlockStart.gifContractedBlock.gif
void  CSystemInformation::GetSystemTimes(LPFILETIME IdleTime, LPFILETIME KernelTime, LPFILETIME UserTime,  bool  ImmediateRet  /**/ /*=false*/ )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*
InBlock.gif    if( ! ::GetSystemTimes( IdleTime, KernelTime, UserTime) )
InBlock.gif    {
InBlock.gif            ProcessErrorMessage(TEXT("获取系统运行时间错误!"));
InBlock.gif    }
ExpandedSubBlockEnd.gif
*/

InBlock.gif    
if( ImmediateRet )
InBlock.gif        
return ;
InBlock.gif
InBlock.gif    memcpy( 
&m_IdleTime, IdleTime, sizeof( FILETIME) );
InBlock.gif    memcpy( 
&m_KernelTime, KernelTime, sizeof( FILETIME) );
InBlock.gif    memcpy( 
&m_UserTime, UserTime, sizeof(FILETIME) );
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gif
//  获取CPU占用率
None.gif
double  CSystemInformation::GetCPURate( void )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    SYSTEM_PERFORMANCE_INFORMATION  SysPerfInfo;
InBlock.gif    SYSTEM_TIME_INFORMATION        SysTimeInfo;
InBlock.gif    SYSTEM_BASIC_INFORMATION       SysBaseInfo;
InBlock.gif
InBlock.gif    
double IdleTime = 0 ;
InBlock.gif    
static double OldIdleTime = 0;   
InBlock.gif    
double SystemTime = 0;
InBlock.gif    
static double OldSystemTime = 0;
InBlock.gif
InBlock.gif    
if( NtQuerySystemInformation )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        DWORD status 
= NtQuerySystemInformation(SystemBasicInformation, &SysBaseInfo, 
InBlock.gif                
sizeof(SYSTEM_BASIC_INFORMATION), NULL);
InBlock.gif        
//GetSystemInfo();
InBlock.gif

InBlock.gif        
if ( NO_ERROR != status)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ProcessErrorMessage(TEXT(
"没有获取到系统基础信息!"));
InBlock.gif            
return 0;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//GetIdleTime();    // 获取系统的三个时间
InBlock.gif
        status = NtQuerySystemInformation(SystemTimeInformation, &SysTimeInfo,
InBlock.gif        
sizeof(SYSTEM_TIME_INFORMATION), NULL);
InBlock.gif        
if ( NO_ERROR != status)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ProcessErrorMessage(TEXT(
"没有获取到系统基础信息!"));
InBlock.gif            
return 0;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        status 
= NtQuerySystemInformation(SystemPerformanceInformation, &SysPerfInfo, 
InBlock.gif                        
sizeof(SYSTEM_PERFORMANCE_INFORMATION), NULL);
InBlock.gif        
if ( NO_ERROR != status)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif                ProcessErrorMessage(TEXT(
"没有获取到系统基础信息!"));
InBlock.gif                
return 0;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
if ( 0 != OldIdleTime )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            IdleTime 
= LongInt2Double(SysPerfInfo.IdleTime)  - OldIdleTime;
InBlock.gif            SystemTime 
= LongInt2Double(SysTimeInfo.liKeSystemTime) - OldSystemTime;
InBlock.gif            
// 采用这种方式获取的CPU占用率,获取的值与任务管理相差较大
InBlock.gif            
//SystemTime = LongInt2Double(m_KernelTime) - OldSystemTime; 
InBlock.gif
            IdleTime = IdleTime/SystemTime;
InBlock.gif            
InBlock.gif            IdleTime 
= 100 -  IdleTime*100.0/(double)SysBaseInfo.NumberOfProcessors;
ExpandedSubBlockEnd.gif        }

InBlock.gif        OldIdleTime 
= LongInt2Double(SysPerfInfo.IdleTime);
InBlock.gif        OldSystemTime 
=LongInt2Double( SysTimeInfo.liKeSystemTime );
InBlock.gif        
//OldSystemTime = LongInt2Double(m_KernelTime);
InBlock.gif        
//Sleep(100);
ExpandedSubBlockEnd.gif
    }

InBlock.gif    
return IdleTime;
ExpandedBlockEnd.gif}

None.gif
None.gif
//  将64位的长整数转换为64位的实数
None.gif
double  CSystemInformation::LongInt2Double(LARGE_INTEGER want)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
return ((double)(want.HighPart) * 4.294967296E9 + (double)(want.LowPart) );
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
//  获取CPU的频率,高word部分为主频,低word部分为次频,以GHz为单位
None.gif
int  CSystemInformation::GetCPUFrenquence( void )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
if ( !m_bHasSystemInfo )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
this->GetSystemInfo();
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//************************************************************************
InBlock.gif#ifdef _DEBUG
InBlock.gif    stringstream ss;
InBlock.gif    ss << HIBYTE(m_system_info.wProcessorRevision) <<LOBYTE(m_system_info.wProcessorRevision) << std::endl;
InBlock.gif    AfxMessageBox( ss.str().c_str() );
InBlock.gif#endif // _DEBUG
ExpandedSubBlockEnd.gif
*/

InBlock.gif    
return m_system_info.wProcessorRevision;
ExpandedBlockEnd.gif}

None.gif
None.gif
//  获取注册表所能够达到的最大尺寸,以字节为单位
None.gif
long  CSystemInformation::GetMaxRegisterSize( void )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    unsigned 
long Max = 0;
InBlock.gif    unsigned 
long Curr = 0;
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*    if ( !GetSystemRegistryQuota(&Max, &Curr) )
InBlock.gif    {
InBlock.gif        ProcessErrorMessage(TEXT("没有获取到系统的注册表存储容量信息!"));
InBlock.gif        return 0;
InBlock.gif    }
ExpandedSubBlockEnd.gif
*/

InBlock.gif    
return Max;
ExpandedBlockEnd.gif}

None.gif
None.gif
//  获取当前注册表的尺寸,以字节为单位
None.gif
long  CSystemInformation::GetCurrRegisterSize( void )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    unsigned 
long Max = 0;
InBlock.gif    unsigned 
long Curr = 0;
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*
InBlock.gif    if ( !GetSystemRegistryQuota(&Max, &Curr) )
InBlock.gif    {
InBlock.gif        ProcessErrorMessage(TEXT("没有获取到系统的注册表存储容量信息!"));
InBlock.gif        return 0;
InBlock.gif    }
ExpandedSubBlockEnd.gif
*/

InBlock.gif    
return Curr;    
ExpandedBlockEnd.gif}

None.gif
需要注意的是,其中还有部分成员函数不能够良好工作,但CPU占用率已经可以非常良好的运行,与windows的任务管理器基本上没有什么大的差别,但未对百分比数据进行修订,以使得数据位数达到一致!

转载于:https://www.cnblogs.com/ubunoon/archive/2008/11/08/System_Information_CPU_Rate.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值