获取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 ...
随机推荐
- memcache和memcached之间的区别和联系
memcache和memcached区别: Memcache是什么?Memcache是一个自由和开放源代码.高性能.分配的内存对象缓存系统.用于加速动态web应用程序,减轻数据库负载.它可以应对任意多 ...
- ios 闪屏页的设置
ref:http://blog.csdn.net/bianruifeng/article/details/8746549
- UVa 10226 - Hardwood Species
题目大意:给出n棵树(有重复),统计每种树出现的频率.使用STL的map. #include <cstdio> #include <iostream> #include < ...
- Python3基础 用while 循环求解 一个整数的阶乘
镇场诗: 诚听如来语,顿舍世间名与利.愿做地藏徒,广演是经阎浮提. 愿尽吾所学,成就一良心博客.愿诸后来人,重现智慧清净体.-------------------------------------- ...
- Html emed 和 object
<object> 标签用于包含对象,比如图像.音频.视频.Java applets.ActiveX.PDF 以及 Flash. object 的初衷是取代 img 和 applet 元素. ...
- 在asp.net中使用ajax记录
一.问题描述 ajax在mvc中使用频繁,比如cms中的评论功能,但由于涉及到前后端开发,日久容易忘,在此做下记录. 二.内容 控制器中代码示例: /// <summary> /// 在文 ...
- Eclipse 注释
Ctrl + / Ctri + Shift + / Ctrl + Shift + C
- CentOS6+MySQL5.6二进制安装
一般我们安装mysql采用二进制安装的方式就足以满足我们的生产环境了,不过需要我们配置my.cnf文件 从官网下载二进制MySQL,选择Linux-Generic,最后这两个是二进制包 http:// ...
- springMvc获取servletContext
springMvc获取servletContext WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebA ...
- lufylegend库 鼠标事件 循环事件 键盘事件
lufylegend库 鼠标事件 循环事件 键盘事件 <!DOCTYPE html> <html lang="en"> <head> <m ...