Chinaunix首页 | 论坛 | 博客
  • 博客访问: 554442
  • 博文数量: 104
  • 博客积分: 4131
  • 博客等级: 上校
  • 技术积分: 1137
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-31 15:05
文章分类

全部博文(104)

文章存档

2011年(13)

2010年(23)

2009年(68)

我的朋友

分类: WINDOWS

2009-12-08 16:41:39

转载:
#include
#include
#define SystemBasicInformation       0
#define SystemPerformanceInformation 2
#define SystemTimeInformation        3
#define Li2Double(x) ((double)((x).HighPart) * 4.294967296E9 + (double)((x).LowPart))
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;
char*GetCPUType(DWORD type)
{
 switch(type)
 {
 case PROCESSOR_INTEL_386 :
  return "Intel386";break;
 case PROCESSOR_INTEL_486:
  return "Intel486";break;
 case PROCESSOR_INTEL_PENTIUM :
  return "Intel Pentium";break;
 case PROCESSOR_MIPS_R4000 :
  return "MIPS_R4000";break;
 case PROCESSOR_HITACHI_SH3:
  return "Hitachi_sh3";break;
 default:
  return "Unknown";
 }
}
const char*GetOSName(const DWORD MajorVersion,const DWORD MinorVersion)
{
 const char *OSNames[20]={"Windows 95","Windows 98","Windows Me","Windows NT 3.51",
   "Windows NT 4.0","Windows 2000","Windows XP","Windows Server 2003 family","Unknown"};
 switch(MajorVersion)
 {
  case 4:
  {
   switch(MinorVersion)
   {
    case 0:return OSNames[0];
    case 10:return OSNames[1];
    case 90:return OSNames[2];
    default:return OSNames[8];
   }
  }
  case 3:
   return OSNames[3];
  case 5:
  {
   switch(MinorVersion)
   {
    case 0:return OSNames[5];
    case 1:return OSNames[6];
    case 2:return OSNames[7];
    default:return OSNames[8];
   }
  }
  default:return OSNames[8];
 }
 return NULL;
}
void ShowDriveInfo(const char*str)
{
 UINT type=GetDriveType(str);
 printf("%s\t",str);
 //printf("【Drive Type:】 ");
 switch(type)
 {
  case DRIVE_UNKNOWN:
   printf("Unknown\t");break;
  case DRIVE_NO_ROOT_DIR:
   printf("No_Root_Dir\t");break;
  case DRIVE_REMOVABLE:
   printf("Removeable Drive\t");break;
  case DRIVE_FIXED:
   printf("Fixed Drive\t");break;
  case DRIVE_REMOTE:
   printf("Remote Drive\t");break;
  case DRIVE_CDROM:
   printf("CD_ROM Drive\t");break;
  case DRIVE_RAMDISK:
   printf("RAM_Drive\t");break;
 }
 DWORD SectorsPerCluster,BytesPerSector,NumberOfFreeClusters ,TotalNumberOfClusters ;
 if(GetDiskFreeSpace(str,&SectorsPerCluster,&BytesPerSector,&NumberOfFreeClusters,&TotalNumberOfClusters))
 {
  printf("%6d MB\t",((SectorsPerCluster*BytesPerSector)/(1024))*TotalNumberOfClusters/(1024));
  printf("%6d MB\t",((SectorsPerCluster*BytesPerSector)/(1024))*NumberOfFreeClusters/(1024));
  printf("%d%%\n",(TotalNumberOfClusters-NumberOfFreeClusters)*100/TotalNumberOfClusters);
 }
 else
 {
  LPVOID lpMsgBuf;
  FormatMessage(  FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | 
  FORMAT_MESSAGE_IGNORE_INSERTS,
  NULL,GetLastError(), 0,(LPTSTR) &lpMsgBuf,0, NULL  );
  printf("Error:%s",(char*)lpMsgBuf);
  return;
 }
}
void main()
{
 ////////////////System Type////////////////////////////////////////////
 printf("========================CUP Infomation=============\n");
 SYSTEM_INFO si;
 memset(&si,0,sizeof(SYSTEM_INFO));
 GetSystemInfo(&si);
 printf("【CPU Number:】%d\n",si.dwNumberOfProcessors);
 printf("【CPU Type:】%s\n",GetCPUType(si.dwProcessorType));
 //////////////////////////////OS Type////////////////////////////
 printf("==================OS System Infomation=============\n");
 OSVERSIONINFO osi;
 memset(&osi,0,sizeof(OSVERSIONINFO));
 osi.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
 if(GetVersionEx(&osi))
 {
  printf("【OS Name】:%s\t%s\n",GetOSName(osi.dwMajorVersion,osi.dwMinorVersion),osi.szCSDVersion);
 }
 else
 {
  printf("Error Code:%d\n",GetLastError());
 } 
 printf("===================Drive Infomation================\n");
 printf("Number\tName\tType\t\tTotal Space\tFree Space\tPercent Of Use\n");
 LPSTR Drivestr=new char[256];
 memset(Drivestr,0,256);
 DWORD len=GetLogicalDriveStrings(256,Drivestr);
 if(len==0)
 {
  printf("Fail To GetLogicalDriveStrings");
 }
 else
 {
  // printf("Drive Number:%d\n",len/4);
  char strTemp[4]={0};
  char*pStr=Drivestr;
  for(int i=0;i<(int)len/4;i++)
  {
   printf( "%d\t",i+1);
   strTemp[0]=pStr[0+i*4];
   strTemp[1]=pStr[1+i*4];
   strTemp[2]=pStr[2+i*4];
   ShowDriveInfo(strTemp);
  }
 }
 /////////////////////////////////////////CUP Use Percent////////////////
   
  printf("===================CUP Use Percent==============\n");
 HINSTANCE handle=LoadLibrary("ntdll.dll");
 if(handle==NULL)//
 {
  printf("Load ntdll.dll Failed!");
  return;
 }
 typedef LONG (WINAPI *PROCNTQSI)(UINT,PVOID,ULONG,PULONG);
    PROCNTQSI NtQuerySystemInformation=
         (PROCNTQSI)GetProcAddress(handle, "NtQuerySystemInformation");
 if(NtQuerySystemInformation==NULL)
 {
  printf("Failed To GetProcAddress!");
  return;
 }
 printf("【Current CUP Use】:\t");
 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};
 NtQuerySystemInformation(SystemBasicInformation,&SysBaseInfo,sizeof(SysBaseInfo),NULL);
 while(true) 
    {
        
    status = NtQuerySystemInformation(SystemTimeInformation,&SysTimeInfo,sizeof(SysTimeInfo),0);
        if (status!=NO_ERROR)
            return;
       
        status = NtQuerySystemInformation(SystemPerformanceInformation,&SysPerfInfo,sizeof(SysPerfInfo),NULL);
        if (status != NO_ERROR)
            return;
        
       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;
            printf("\b\b\b\b%3d%%",(UINT)dbIdleTime);
       }
         
        liOldIdleTime = SysPerfInfo.liIdleTime;
        liOldSystemTime = SysTimeInfo.liKeSystemTime;
        
        Sleep(1000);
    }
}
 
阅读(641) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~