#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和内存呢信息的更多相关文章

  1. Android获取cpu和内存信息、网址的代码

      android获取手机cpu并判断是单核还是多核 /** * Gets the number of cores available in this device, across all proce ...

  2. iOS 获取APP的CPU、内存等信息

    目标是开发一个SDK,嵌入到APP里面,用来统计当前APP的实时CPU.内存等信息 2015.11.17 http://stackoverflow.com/questions/12889422/ios ...

  3. 方法:Linux 下用JAVA获取CPU、内存、磁盘的系统资源信息

    CPU使用率: InputStream is = null; InputStreamReader isr = null; BufferedReader brStat = null; StringTok ...

  4. 主机性能监控之wmi 获取系统信息及内存性能信息

    标 题: 主机性能监控之wmi 获取系统信息及内存性能信息作 者: itdef链 接: http://www.cnblogs.com/itdef/p/3990240.html 欢迎转帖 请保持文本完整 ...

  5. Python获取CPU、内存使用率以及网络使用状态代码

    Python获取CPU.内存使用率以及网络使用状态代码_python_脚本之家 http://www.jb51.net/article/134714.htm

  6. jmeter性能工具 之监控cpu,内存等信息(四)

    1.jmeter 本身不支持直接监控 cpu,内存等信息,需要去官网下载控件 JMeterPlugins-Standard-1.4.0.zip    解压好将其中\lib\ext\JMeterPlug ...

  7. Linux下使用java获取cpu、内存使用率

    原文地址:http://www.voidcn.com/article/p-yehrvmep-uo.html 思路如下:Linux系统中可以用top命令查看进程使用CPU和内存情况,通过Runtime类 ...

  8. Golang获取CPU、内存、硬盘使用率

    Golang获取CPU.内存.硬盘使用率 工具包 go get github.com/shirou/gopsutil 实现 func GetCpuPercent() float64 { percent ...

  9. Java 获取CPU、内存、外网IP等硬件信息

    import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.Input ...

随机推荐

  1. winform总结5> winform程序开发注意事项

    1.全局异常捕获 Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); //处理UI线程异常 Ap ...

  2. ThinkPHP 自动验证实例

    //array(验证字段1,验证规则,错误提示,[验证条件,附加规则,验证时间]),protected $_validate = array( ); ThinkPHP 自动验证定义的附加规则如下: r ...

  3. 如何选择面向移动设备的html5开发框架

    很久以前整理了篇将手机网站做成手机应用的JS框架.时隔一年多,很多新的技术已经出现,下面再来总结下还有哪些框架是适合面向手机设备的开发的. 1.jQuery Mobile jQuery Mobile ...

  4. jquery换肤

    <script src="script/jquery-2.1.0.js"></script>      <link href="style/ ...

  5. 搜索框(SearchView)的功能与用法

    SearchView是搜索框组件,它可以让用户在文本框内输入汉字,并允许通过监听器监控用户输入,当用户用户输入完成后提交搜索按钮时,也通过监听器执行实际的搜索. 使用SearchView时可以使用如下 ...

  6. 部署statspack工具(一)

    禁用sga自动管理机制,分配比较小的数据缓冲区(30m)和共享池(70m)空间 1.1关闭SGA自动管理机制 查看是否开启了ASSM idle>show parameter sga; NAME  ...

  7. easyui message show中msg嵌入一个按钮如何绑定事件

    http://www.oschina.net/question/945028_171927

  8. Hadoop权威指南:通过FileSystem API读取数据

    Hadoop权威指南:通过FileSystem API读取数据 [TOC] 在Hadoop中,FileSystem是一个通用的文件系统API 获取FileSystem实例的几个静态方法 public ...

  9. Linux下使用javac编译

    Linux下使用javac编译Hadoop程序 首先要配置好Hadoop, 给出两个教程 Hadoop安装教程单机/伪分布式配置Hadoop2.6.0/Ubuntu14.04 Hadoop集群安装配置 ...

  10. [转载] HTTP协议状态码详解(HTTP Status Code)

    转载自:http://www.cnblogs.com/shanyou/archive/2012/05/06/2486134.html 使用ASP.NET/PHP/JSP 或者javascript都会用 ...