获取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 ...
随机推荐
- iOS纯代码手动适配 分类: ios技术 2015-05-04 17:14 239人阅读 评论(0) 收藏
首先说下让自己的程序支持iPhone6和6+,第一种使用官方提供的launch screen.xib,这个直接看官方文档即可,这里不再多述:第二种方法是和之前iPhone5的类似,比较简单,为iPho ...
- P4语言编程快速开始 实践一
参考:P4语言快速开始 感谢杨老师的分享! 前言及P4程序请参考原文,本文主要是对文章中的两个动手实例的实践记录. 1.通过behavioral-model运行simple_router样例 执行命令 ...
- NLPIR中文分词器的使用
一.普通java项目 (1)添加项目jar包 File -> Project Structure Libarries 添加jar包jna-4.0.0.jar (2)将Data文件夹复制到 ...
- 利用STM32F唯一96bit序列号实现反拷贝加密的源代码公开(转)
源:利用STM32F唯一96bit序列号实现反拷贝加密的源代码公开 //---------------------------------------------------------------- ...
- 2017-01-11小程序form表单提交
小程序form表单提交 1.小程序相对于之前的WEB+PHP建站来说,个人理解为只是将web放到了微信端,用小程序固定的格式前前端进行布局.事件触发和数据的输送和读取,服务器端可以用任何后端语言写,但 ...
- xml数据传输
- 安卓 ArrayList,LinkedList,HashSet,Vector,TreeSet的区别和使用
java的集合就那么几种 总体为:List,Set,Map (都是接口由其子类去实现具体的方法) ArrayList,LinkedList,Vector都属于List List:元素是有顺序的,元素可 ...
- 学习笔记::LCT
今天听见茹大神20分钟讲完了LCT,10分钟讲完平衡树,5分钟讲完树剖,感觉自己智商还不及他一半... 还有很多不懂:2017/1/15 的理解: access是干什么用的? 不知道,只知道他是用来把 ...
- 使AIX下ksh可以翻查上一条命令
http://blog.csdn.net/wangjialiang/article/details/7106393 http://www.blogjava.net/03081626/archive/2 ...
- C++中定义使用受限的类
1.只能在堆上使用的类 栈上对象通过自动调用析构函数释放空间,将该类的析构函数定义为private就可以阻止其被自动调用,从而阻止在栈上使用该类的对象,为了避免资源泄漏问题,在堆上使用该类的对象时我们 ...