#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預(yù)編譯語句
#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使用狀態(tài)
? 參數(shù):無
? 返回值:內(nèi)存占用率
? 作者:牽牛散步
*/
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;
?
}
/*
? 功能:得到內(nèi)存使用狀態(tài)
? 參數(shù):無
? 返回值:內(nèi)存信息結(jié)構(gòu)體[包括總的物理內(nèi)存,還可使用內(nèi)存,虛擬內(nèi)存,單位為K]
? 作者:牽牛散步
*/
MemoryInf MemorySta()
{
?MemoryInf tmp;//在ICE SLICE里定義的信息結(jié)構(gòu)體
?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;
}
/*
?功能:得到硬盤使用情況
?參數(shù):無
?返回值:硬盤信息結(jié)構(gòu)體
?作者:牽牛散步
*/
DiskInf GetDiskSta()
{
?ULARGE_INTEGER FreeAvailable,TotalNum,TotalFreeNum;
?char p[3];
?bool b_flag;
?DiskInf tmp;//ICE SLICE里定義的硬盤信息結(jié)構(gòu)體
?tmp.TotalSpace = 0;
?tmp.FreeSpace = 0;
?//得到有效的驅(qū)動(dòng)器名,即盤符
?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;
}