获取CPU和内存呢信息
#include <stdio.h>
#include <stdlib.h>
#include <winsock.h>
#pragma comment(lib, "ws2_32.lib")
#include <nb30.h>
#pragma comment(lib,"NETAPI32.LIB")
#include <string>
#include <iostream>
using namespace std;
#include "psapi.h"
#pragma comment(lib, "Psapi.lib") #include "hardcard.h"
//------获取硬盘----------------------------------------------------------------------- typedef BOOL(WINAPI *PGETDISKFREESPACEEX)(LPCSTR,
PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER); BOOL MyGetDiskFreeSpaceEx(LPCSTR pszDrive, float *out)
{
PGETDISKFREESPACEEX pGetDiskFreeSpaceEx;
__int64 i64FreeBytesToCaller, i64TotalBytes, i64FreeBytes; DWORD dwSectPerClust,
dwBytesPerSect,
dwFreeClusters,
dwTotalClusters; BOOL fResult; pGetDiskFreeSpaceEx = (PGETDISKFREESPACEEX) GetProcAddress(
GetModuleHandle("kernel32.dll"),
"GetDiskFreeSpaceExA"); if (pGetDiskFreeSpaceEx)
{
// printf("\n1.%x\n",pGetDiskFreeSpaceEx);
fResult = pGetDiskFreeSpaceEx(pszDrive,
(PULARGE_INTEGER)&i64FreeBytesToCaller,
(PULARGE_INTEGER)&i64TotalBytes,
(PULARGE_INTEGER)&i64FreeBytes); // Process GetDiskFreeSpaceEx results.
if (fResult)
{
// printf("剩余字节 = %I64d\n", i64FreeBytes);
// printf("总共字节 = %I64d\n", i64TotalBytes);
double f = i64FreeBytes, t = i64TotalBytes;
for (int i = ; i < ; ++i)
{
f /= ;
t /= ;
}
out[] = f;
out[] = t;
// printf("%0.2fG,", f);
// printf("%0.2fG", t);
}
/// printf("\n");
return fResult;
}
else
{
//printf("\n2.%x\n",pGetDiskFreeSpaceEx);
fResult = GetDiskFreeSpaceA(pszDrive,
&dwSectPerClust,
&dwBytesPerSect,
&dwFreeClusters,
&dwTotalClusters); // Process GetDiskFreeSpace results.
if (fResult)
{
// printf("剩余字节 = I64d",dwFreeClusters*dwSectPerClust*dwBytesPerSect);
}
//printf("\n");
return fResult;
}
}
__int64 CompareFileTime(FILETIME time1, FILETIME time2)
{
__int64 a = time1.dwHighDateTime << | time1.dwLowDateTime;
__int64 b = time2.dwHighDateTime << | time2.dwLowDateTime; return (b - a);
}
void getcpumemory(int *_cpu, int *_memory)
{
HANDLE hEvent;
BOOL res; FILETIME preidleTime;
FILETIME prekernelTime;
FILETIME preuserTime; FILETIME idleTime;
FILETIME kernelTime;
FILETIME userTime;
MEMORYSTATUS memo; res = GetSystemTimes(&idleTime, &kernelTime, &userTime); preidleTime = idleTime;
prekernelTime = kernelTime;
preuserTime = userTime; hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); // 初始值为 nonsignaled ,并且每次触发后自动设置为nonsignaled //while (1)
{ WaitForSingleObject(hEvent, ); //等待500毫秒
res = GetSystemTimes(&idleTime, &kernelTime, &userTime); int idle = CompareFileTime(preidleTime, idleTime);
int kernel = CompareFileTime(prekernelTime, kernelTime);
int user = CompareFileTime(preuserTime, userTime); int cpu = (kernel + user - idle) * / (kernel + user);//(总的时间-空闲时间)/总的时间=占用cpu的时间就是使用率
int cpuidle = (idle) * / (kernel + user);
GlobalMemoryStatus(&memo);
//system("cls");
cout << "CPU" << cpu << "%,";// << " CPU空闲率:" <<cpuidle << "%" <<endl;
cout << "内存" << memo.dwMemoryLoad << "%;";
preidleTime = idleTime;
prekernelTime = kernelTime;
preuserTime = userTime;
*_cpu = cpu;
*_memory = memo.dwMemoryLoad;
// Sleep(500);
} }
#pragma once BOOL MyGetDiskFreeSpaceEx(LPCSTR pszDrive, float *out);
void getcpumemory(int *_cpu, int *_memory);
获取CPU和内存呢信息的更多相关文章
- Android获取cpu和内存信息、网址的代码
android获取手机cpu并判断是单核还是多核 /** * Gets the number of cores available in this device, across all proce ...
- iOS 获取APP的CPU、内存等信息
目标是开发一个SDK,嵌入到APP里面,用来统计当前APP的实时CPU.内存等信息 2015.11.17 http://stackoverflow.com/questions/12889422/ios ...
- 方法:Linux 下用JAVA获取CPU、内存、磁盘的系统资源信息
CPU使用率: InputStream is = null; InputStreamReader isr = null; BufferedReader brStat = null; StringTok ...
- 主机性能监控之wmi 获取系统信息及内存性能信息
标 题: 主机性能监控之wmi 获取系统信息及内存性能信息作 者: itdef链 接: http://www.cnblogs.com/itdef/p/3990240.html 欢迎转帖 请保持文本完整 ...
- Python获取CPU、内存使用率以及网络使用状态代码
Python获取CPU.内存使用率以及网络使用状态代码_python_脚本之家 http://www.jb51.net/article/134714.htm
- jmeter性能工具 之监控cpu,内存等信息(四)
1.jmeter 本身不支持直接监控 cpu,内存等信息,需要去官网下载控件 JMeterPlugins-Standard-1.4.0.zip 解压好将其中\lib\ext\JMeterPlug ...
- Linux下使用java获取cpu、内存使用率
原文地址:http://www.voidcn.com/article/p-yehrvmep-uo.html 思路如下:Linux系统中可以用top命令查看进程使用CPU和内存情况,通过Runtime类 ...
- Golang获取CPU、内存、硬盘使用率
Golang获取CPU.内存.硬盘使用率 工具包 go get github.com/shirou/gopsutil 实现 func GetCpuPercent() float64 { percent ...
- Java 获取CPU、内存、外网IP等硬件信息
import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.Input ...
随机推荐
- 【转】我是怎么找到电子书的 – IT篇
多读书,提高自己 电子出版物 IT-ebooks http://it-ebooks.info/ 上万本英文原版电子书,大多数为apress和o'relly的.全都是文字版,体积小又清楚.适合懂英文的人 ...
- svn 安装网站
1.svn插件 http://subclipse.tigris.org/servlets/ProjectDocumentList?folderID=2240
- iOS UICollectionView高级用法(长按自由移动cell)-新
[reference]http://www.jianshu.com/p/31d07bf32d62 iOS 9之后: 示例如下 效果 前言: 看完你可以学到哪些呢? 就是文章标题那么多, 只有那么多. ...
- 安卓 canvas
[转]http://blog.sina.com.cn/s/blog_61ef49250100qw9x.html(easy) [转]http://blog.csdn.net/rhljiayou/arti ...
- java名词解释,让你更好理解
Java 开发工具包 (JDK) Java开发工具包是Java环境的核心组件,并提供编译.调试和运行一个Java程序所需的所有工具,可执行文件和二进制文件.JDK是一个平台特定的软件,有针对Windo ...
- Python模块学习:threading 多线程控制和处理
Reference:http://python.jobbole.com/81546/ threading.Thread Thread 是threading模块中最重要的类之一,可以使用它来创建线程.有 ...
- python实现二叉树
初学python,需要实现一个决策树,首先实践一下利用python实现一个二叉树数据结构.建树的时候做了处理,保证建立的二叉树是平衡二叉树. # -*- coding: utf-8 -*- from ...
- iOS 之 关闭键盘
//方法一:关闭整个系统的键盘 [[[UIApplication sharedApplication] keyWindow] endEditing:YES]; //方法二:关闭当前页的键盘 [[sel ...
- Aaron Swartz – 互联网天才开挂的人生历程:每时每刻都问自己,现在这世界有什么最重要的事是我能参与去做的?
Aaron说的一句话让我挺有感触的-- 相信你应该真的每时每刻都问自己,现在这世界有什么最重要的事是我能参与去做的? 如果你没在做那最重要的事,那又是为什么? 1986年11月8日,有个叫Aaron ...
- YII 1.0 分页类
在控制器中 方法1 $criteria = new CDbCriteria();//AR的另一种写法 $model = Article::model(); $total = $model->co ...