天天看點

C++程式設計中,利用WINDOWS API獲得系統狀态資訊[CPU占用率,硬碟使用情況,記憶體使用情況]

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

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;

}

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)