zoukankan      html  css  js  c++  java
  • 获取CPU和内存呢信息

      1 #include <stdio.h>   
      2 #include <stdlib.h>   
      3 #include <winsock.h>   
      4 #pragma comment(lib, "ws2_32.lib")   
      5 #include <nb30.h>
      6 #pragma comment(lib,"NETAPI32.LIB")
      7 #include <string>
      8 #include <iostream>
      9 using namespace std;
     10 #include "psapi.h"
     11 #pragma comment(lib, "Psapi.lib") 
     12 
     13 #include "hardcard.h"
     14 //------获取硬盘-----------------------------------------------------------------------
     15 
     16 typedef BOOL(WINAPI *PGETDISKFREESPACEEX)(LPCSTR,
     17     PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER);
     18 
     19 BOOL MyGetDiskFreeSpaceEx(LPCSTR pszDrive, float *out)
     20 {
     21     PGETDISKFREESPACEEX pGetDiskFreeSpaceEx;
     22     __int64 i64FreeBytesToCaller, i64TotalBytes, i64FreeBytes;
     23 
     24     DWORD dwSectPerClust,
     25         dwBytesPerSect,
     26         dwFreeClusters,
     27         dwTotalClusters;
     28 
     29     BOOL fResult;
     30 
     31     pGetDiskFreeSpaceEx = (PGETDISKFREESPACEEX) GetProcAddress(
     32         GetModuleHandle("kernel32.dll"),
     33         "GetDiskFreeSpaceExA");
     34 
     35     if (pGetDiskFreeSpaceEx)
     36     {
     37         // printf("
    1.%x
    ",pGetDiskFreeSpaceEx);
     38         fResult = pGetDiskFreeSpaceEx(pszDrive,
     39             (PULARGE_INTEGER)&i64FreeBytesToCaller,
     40             (PULARGE_INTEGER)&i64TotalBytes,
     41             (PULARGE_INTEGER)&i64FreeBytes);
     42 
     43         // Process GetDiskFreeSpaceEx results.
     44         if (fResult)
     45         {
     46             //  printf("剩余字节 = %I64d
    ", i64FreeBytes);
     47             //  printf("总共字节 = %I64d
    ", i64TotalBytes);
     48             double f = i64FreeBytes, t = i64TotalBytes;
     49             for (int i = 0; i < 3; ++i)
     50             {
     51                 f /= 1024;
     52                 t /= 1024;
     53             }
     54             out[0] = f;
     55             out[1] = t;
     56             //   printf("%0.2fG,", f);
     57             //    printf("%0.2fG", t);
     58         }
     59         ///  printf("
    ");
     60         return fResult;
     61     }
     62     else
     63     {
     64         //printf("
    2.%x
    ",pGetDiskFreeSpaceEx);
     65         fResult = GetDiskFreeSpaceA(pszDrive,
     66             &dwSectPerClust,
     67             &dwBytesPerSect,
     68             &dwFreeClusters,
     69             &dwTotalClusters);
     70 
     71         // Process GetDiskFreeSpace results.
     72         if (fResult)
     73         {
     74             // printf("剩余字节 = I64d",dwFreeClusters*dwSectPerClust*dwBytesPerSect);
     75         }
     76         //printf("
    ");
     77         return fResult;
     78     }
     79 }
     80 __int64 CompareFileTime(FILETIME time1, FILETIME time2)
     81 {
     82     __int64 a = time1.dwHighDateTime << 32 | time1.dwLowDateTime;
     83     __int64 b = time2.dwHighDateTime << 32 | time2.dwLowDateTime;
     84 
     85     return   (b - a);
     86 }
     87 void getcpumemory(int *_cpu, int *_memory)
     88 {
     89     HANDLE hEvent;
     90     BOOL res;
     91 
     92     FILETIME preidleTime;
     93     FILETIME prekernelTime;
     94     FILETIME preuserTime;
     95 
     96     FILETIME idleTime;
     97     FILETIME kernelTime;
     98     FILETIME userTime;
     99     MEMORYSTATUS memo;
    100 
    101     res = GetSystemTimes(&idleTime, &kernelTime, &userTime);
    102 
    103     preidleTime = idleTime;
    104     prekernelTime = kernelTime;
    105     preuserTime = userTime;
    106 
    107     hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); // 初始值为 nonsignaled ,并且每次触发后自动设置为nonsignaled
    108 
    109     //while (1)
    110     {
    111 
    112 
    113         WaitForSingleObject(hEvent, 500); //等待500毫秒
    114         res = GetSystemTimes(&idleTime, &kernelTime, &userTime);
    115 
    116         int idle = CompareFileTime(preidleTime, idleTime);
    117         int kernel = CompareFileTime(prekernelTime, kernelTime);
    118         int user = CompareFileTime(preuserTime, userTime);
    119 
    120         int cpu = (kernel + user - idle) * 100 / (kernel + user);//(总的时间-空闲时间)/总的时间=占用cpu的时间就是使用率
    121         int cpuidle = (idle) * 100 / (kernel + user);
    122         GlobalMemoryStatus(&memo);
    123         //system("cls");
    124         cout << "CPU" << cpu << "%,";// << "      CPU空闲率:" <<cpuidle << "%" <<endl;      
    125         cout << "内存" << memo.dwMemoryLoad << "%;";
    126         preidleTime = idleTime;
    127         prekernelTime = kernelTime;
    128         preuserTime = userTime;
    129         *_cpu = cpu;
    130         *_memory = memo.dwMemoryLoad;
    131         // Sleep(500); 
    132     }
    133 
    134 }
    1 #pragma once
    2 
    3 BOOL MyGetDiskFreeSpaceEx(LPCSTR pszDrive, float *out);
    4 void getcpumemory(int *_cpu, int *_memory);
  • 相关阅读:
    STM32 HAL库学习笔记
    嵌入式Linux学习笔记
    AVR_Interrupt
    shutdown命令用法
    ULINK2 USB电脑无法识别(连接电脑后,设备管理器显示未知设备)
    MDK中编译程序后Program Size详解
    Keil(MDK-ARM)系列教程(三)_工程目标选项配置(Ⅰ)
    第48章 MDK的编译过程及文件类型全解
    Do not access Object.prototype method ‘hasOwnProperty’ from target object no-prototype-builtins
    让vscode按照eslint进行格式化
  • 原文地址:https://www.cnblogs.com/Zblogs/p/3356566.html
Copyright © 2011-2022 走看看