牵牛散步
仗剑走天涯

#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;
}

posted on 2006-04-14 11:35 牵牛散步 阅读(10416) 评论(3)  编辑 收藏 引用 所属分类: C++学习资料
Comments
  • # re: C++编程中,利用WINDOWS API获得系统状态信息[CPU占用率,硬盘使用情况,内存使用情况]
    javaer
    Posted @ 2007-03-30 18:40
    #include <Ice/Ice.h>
    #include <GetWinSysState.h>

    我想问你一下,缺这两个库,怎么办?那里有?  回复  更多评论   
  • # re: C++编程中,利用WINDOWS API获得系统状态信息[CPU占用率,硬盘使用情况,内存使用情况]
    SHI
    Posted @ 2007-12-24 16:10
    GetWinSysState.h这个找不到怎么办
      回复  更多评论   
  • # re: C++编程中,利用WINDOWS API获得系统状态信息[CPU占用率,硬盘使用情况,内存使用情况]
    好看网
    Posted @ 2010-08-05 11:19
    总的虚拟内存大小不知道怎么获得 哎。

    那结构体 里的 TOTALMEMORY已经不是虚拟内存了。

    在windows核心编程里面写的。  回复  更多评论   

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理