C++编程中,利用WINDOWS API获得系统状态信息[CPU占用率,硬盘使用情况,内存使用情况]

来源:互联网 发布:瓦尔特数控机床编程 编辑:程序博客网 时间:2024/04/30 12:54

#include <Ice/Ice.h>
#include <iostream>
#include <GetWinSysState.h>
#include <Winbase.h>
#include <conio.h>
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <string>
#include <direct.h>

#define SystemBasicInformation       0
#define SystemPerformanceInformation 2
#define SystemTimeInformation        3

#define Li2Double(x) ((double)((x).HighPart) * 4.294967296E9 + (double)((x).LowPart))
//ICE预编译语句

#ifdef _DEBUG
#pragma comment(lib, "iced.lib")
#pragma comment(lib, "iceutild.lib")
#else
#pragma comment(lib, "ice.lib")
#pragma comment(lib, "iceutil.lib")
#endif

typedef struct
{
    DWORD   dwUnknown1;
    ULONG   uKeMaximumIncrement;
    ULONG   uPageSize;
    ULONG   uMmNumberOfPhysicalPages;
    ULONG   uMmLowestPhysicalPage;
    ULONG   uMmHighestPhysicalPage;
    ULONG   uAllocationGranularity;
    PVOID   pLowestUserAddress;
    PVOID   pMmHighestUserAddress;
    ULONG   uKeActiveProcessors;
    BYTE    bKeNumberProcessors;
    BYTE    bUnknown2;
    WORD    wUnknown3;
} SYSTEM_BASIC_INFORMATION;

typedef struct
{
    LARGE_INTEGER   liIdleTime;
    DWORD           dwSpare[76];
} SYSTEM_PERFORMANCE_INFORMATION;

typedef struct
{
    LARGE_INTEGER liKeBootTime;
    LARGE_INTEGER liKeSystemTime;
    LARGE_INTEGER liExpTimeZoneBias;
    ULONG         uCurrentTimeZoneId;
    DWORD         dwReserved;
} SYSTEM_TIME_INFORMATION;


// ntdll!NtQuerySystemInformation (NT specific!)
//
// The function copies the system information of the
// specified type into a buffer
//
// NTSYSAPI
// NTSTATUS
// NTAPI
// NtQuerySystemInformation(
//    IN UINT SystemInformationClass,    // information type
//    OUT PVOID SystemInformation,       // pointer to buffer
//    IN ULONG SystemInformationLength,  // buffer size in bytes
//    OUT PULONG ReturnLength OPTIONAL   // pointer to a 32-bit
//                                       // variable that receives
//                                       // the number of bytes
//                                       // written to the buffer
// );
typedef LONG (WINAPI *PROCNTQSI)(UINT,PVOID,ULONG,PULONG);

PROCNTQSI NtQuerySystemInformation;

/*
  功能:得到CPU使用状态
  参数:无
  返回值:内存占用率
  作者:牵牛散步
*/
int GetCpuStat()
{
 SYSTEM_PERFORMANCE_INFORMATION SysPerfInfo;
    SYSTEM_TIME_INFORMATION        SysTimeInfo;
    SYSTEM_BASIC_INFORMATION       SysBaseInfo;
    double                         dbIdleTime;
    double                         dbSystemTime;
    LONG                           status;
    LARGE_INTEGER                  liOldIdleTime = {0,0};
    LARGE_INTEGER                  liOldSystemTime = {0,0};
 int UsageCpu = 0;
    NtQuerySystemInformation = (PROCNTQSI)GetProcAddress(
  GetModuleHandle("ntdll"),
  "NtQuerySystemInformation"
  );
 
    if (!NtQuerySystemInformation)
        return 0;
 
    status = NtQuerySystemInformation(SystemBasicInformation,&SysBaseInfo,sizeof(SysBaseInfo),NULL);
    if (status != NO_ERROR)
        return 0;
    
 for( int t = 0 ; t < 2 ; t++ )
    {
  status = NtQuerySystemInformation(SystemTimeInformation,&SysTimeInfo,sizeof(SysTimeInfo),0);
        if (status!=NO_ERROR)
            return 0;
  
        status = NtQuerySystemInformation(SystemPerformanceInformation,&SysPerfInfo,sizeof(SysPerfInfo),NULL);
        if (status != NO_ERROR)
            return 0;
  
  if (liOldIdleTime.QuadPart != 0)
  {
            dbIdleTime = Li2Double(SysPerfInfo.liIdleTime) - Li2Double(liOldIdleTime);
            dbSystemTime = Li2Double(SysTimeInfo.liKeSystemTime) - Li2Double(liOldSystemTime);
   
            dbIdleTime = dbIdleTime / dbSystemTime;
   

            dbIdleTime = 100.0 - dbIdleTime * 100.0 / (double)SysBaseInfo.bKeNumberProcessors + 0.5;
   UsageCpu = (int)dbIdleTime;

  }
  
        // store new CPU's idle and system time
        liOldIdleTime = SysPerfInfo.liIdleTime;
        liOldSystemTime = SysTimeInfo.liKeSystemTime;
  
        // wait one second
        Sleep(500);
    }
 return UsageCpu;
 
}


/*
  功能:得到内存使用状态
  参数:无
  返回值:内存信息结构体[包括总的物理内存,还可使用内存,虚拟内存,单位为K]
  作者:牵牛散步
*/
MemoryInf MemorySta()
{
 MemoryInf tmp;//在ICE SLICE里定义的信息结构体
 MEMORYSTATUS memStatus;
 GlobalMemoryStatus(&memStatus);
 DWORD tom=memStatus.dwTotalPhys/1024;
 DWORD mem=memStatus.dwAvailPhys/1024;
 DWORD res=memStatus.dwAvailVirtual/1024;
 tmp.TotalMem = (int)tom;
 tmp.ValidMem = (int)mem;
 tmp.VirtualMem = (int)res;
 return tmp;
}


/*
 功能:得到硬盘使用情况
 参数:无
 返回值:硬盘信息结构体
 作者:牵牛散步
*/
DiskInf GetDiskSta()
{
 ULARGE_INTEGER FreeAvailable,TotalNum,TotalFreeNum;

 char p[3];
 bool b_flag;
 DiskInf tmp;//ICE SLICE里定义的硬盘信息结构体
 tmp.TotalSpace = 0;
 tmp.FreeSpace = 0;
 //得到有效的驱动器名,即盘符
 for( int drive = 1; drive <= 26; drive++ )
 {
  if( !_chdrive( drive ) )
  {
   memset( p , 0 , sizeof(p));
   p[0] = drive + 'A' - 1;
   p[1] = ':';
   p[2] = '/0';
   b_flag = GetDiskFreeSpaceEx( p ,&FreeAvailable,&TotalNum,&TotalFreeNum );
   if( b_flag )
   {
    tmp.TotalSpace += (int)(TotalNum.QuadPart/(1024*1024));
    tmp.FreeSpace += (int)(FreeAvailable.QuadPart/(1024*1024));
   }
  }
 }
 return tmp;
}

int main()
{
   return 1;
}

(摘自 http://www.cppblog.com/klsmlzm/archive/2006/04/14/5522.html) 
原创粉丝点击