CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  VC/MFC >  硬件/系统

用什么API函数获得当前CPU使用状态?有人知道吗

楼主HUSTCPH(花木樱盗)2004-04-04 19:33:26 在 VC/MFC / 硬件/系统 提问

RT,谢谢! 问题点数:0、回复次数:3Top

1 楼mintwlf(Programmer)回复于 2004-04-04 19:35:40 得分 0

我记得是读一个注册表项的信息,你查一下MSDN吧!Top

2 楼code8238(二进制动物)回复于 2004-04-04 19:45:19 得分 0

 
  这是获得系统资源使用情况的源码,看看可能有帮助  
  #include   <windows.h>  
  #include   <conio.h>  
  #include   <stdio.h>  
  //  
  #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;  
   
   
  //   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;  
   
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////  
  ////                                 main                     /////////////////////////////////////////////////////////////////////////////  
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////  
   
  void   main(void)  
   
  {  
   
          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   =   (PROCNTQSI)GetProcAddress(  
                                                                                      GetModuleHandle("ntdll"),  
                                                                                    "NtQuerySystemInformation"  
                                                                                    );  
   
          if   (!NtQuerySystemInformation)  
                  return;  
   
          //   get   number   of   processors   in   the   system  
          status   =   NtQuerySystemInformation(SystemBasicInformation,&SysBaseInfo,sizeof(SysBaseInfo),NULL);  
          if   (status   !=   NO_ERROR)  
                  return;  
   
          printf("\nCPU   Usage   (press   any   key   to   exit):         ");  
   
          while(!_kbhit())  
   
          {  
                  //   get   new   system   time  
                  status   =   NtQuerySystemInformation(SystemTimeInformation,&SysTimeInfo,sizeof(SysTimeInfo),0);  
                  if   (status!=NO_ERROR)  
                          return;  
   
                  //   get   new   CPU's   idle   time  
                  status   =   NtQuerySystemInformation(SystemPerformanceInformation,&SysPerfInfo,sizeof(SysPerfInfo),NULL);  
                  if   (status   !=   NO_ERROR)  
                          return;  
   
                  //   if   it's   a   first   call   -   skip   it  
                if   (liOldIdleTime.QuadPart   !=   0)  
                {  
                          //   CurrentValue   =   NewValue   -   OldValue  
                          dbIdleTime       =   Li2Double(SysPerfInfo.liIdleTime)   -   Li2Double(liOldIdleTime);  
                          dbSystemTime   =   Li2Double(SysTimeInfo.liKeSystemTime)   -   Li2Double(liOldSystemTime);  
                          //   CurrentCpuIdle   =   IdleTime   /   SystemTime  
                          dbIdleTime   =   dbIdleTime   /   dbSystemTime;  
                          //   CurrentCpuUsage%   =     -   (CurrentCpuIdle   *   100)   /   NumberOfProcessors  
                          dbIdleTime   =   100.0   -   dbIdleTime   *   100.0   /   (double)SysBaseInfo.bKeNumberProcessors   +   0.5;  
                          printf("\b\b\b\b%3d%%",(UINT)dbIdleTime);  
                }  
                  //   store   new   CPU's   idle   and   system   time  
                  liOldIdleTime       =   SysPerfInfo.liIdleTime;  
                  liOldSystemTime   =   SysTimeInfo.liKeSystemTime;  
                  //   wait   one   second  
                  Sleep(1000);  
          }  
          printf("\n");  
  }  
  Top

3 楼enoloo(在水一方)回复于 2004-04-04 19:51:01 得分 0

VOID   GetSystemInfo(  
      LPSYSTEM_INFO   lpSystemInfo     //   system   information  
  );  
   
  typedef   struct   _SYSTEM_INFO   {    
      union   {    
          DWORD     dwOemId;    
          struct   {    
              WORD   wProcessorArchitecture;    
              WORD   wReserved;    
          };    
      };    
      DWORD     dwPageSize;    
      LPVOID   lpMinimumApplicationAddress;    
      LPVOID   lpMaximumApplicationAddress;    
      DWORD_PTR   dwActiveProcessorMask;    
      DWORD   dwNumberOfProcessors;    
      DWORD   dwProcessorType;    
      DWORD   dwAllocationGranularity;    
      WORD   wProcessorLevel;    
      WORD   wProcessorRevision;    
  }   SYSTEM_INFO;    
  Top

相关问题

  • 如何使用API函数
  • 如何使用API函数
  • API函数怎么使用
  • 怎样使用API函数
  • API函数如何使用
  • API函数GetWindowRect的使用
  • API函数FillRect的使用
  • 关于使用api函数检测cpu中的运行程序的问题?
  • 请问如何使用该API函数
  • c++builder中如何使用api函数?

关键词

  • ulong
  • large
  • typedef struct
  • integer
  • double
  • define
  • dword
  • include
  • information

得分解答快速导航

  • 帖主:HUSTCPH

相关链接

  • Visual C++类图书
  • Visual C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo