#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. leetcode--010 Linked List Cycle II

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAApAAAACICAIAAADfzUzYAAANeklEQVR4nO3dQa7bthbG8W4mK/A+so

  2. iOS中UITextField 使用全面解析 分类: ios技术 2015-04-10 14:37 153人阅读 评论(0) 收藏

    //初始化textfield并设置位置及大小   UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 13 ...

  3. [Angular Tutorial] 5-Filtering Repeaters

    在上一步中,我们花了很大功夫来布局应用的基础,所以我们现在做点简单点的吧!我们将会添加一个全文本搜索框(没错,这很简单). ·我们的应用现在会有一个搜索框,注意页面中手机列表的改变取决于用户在搜索框键 ...

  4. 简单的shared_ptr实现

    RT,代码参考了STL中shard_ptr的实现,基本原理是引用计数,利用Ref_cnt类来管理内存,在shared_ptr创建时创建,此后shared_ptr仅是在拷贝复制析构的过程中对引用进行修改 ...

  5. Android开发:View的几种布局及实践

    引言 View的布局显示方式有下面几种:线性布局(Linear Layout).相对布局(Relative Layout).表格布局(Table Layout).网格视图(Grid View).标签布 ...

  6. jmeter接口测试实践

    一.什么是接口测试? 接口测试是测试系统组件间接口的一种测试.接口测试主要用于检测外部系统与系统之间以及内部各个子系统之间的交互点.测试的重点是要检查数据的交换,传递和控制管理过程,以及系统间的相互逻 ...

  7. 其实想要完全理解MVC框架并不是太容易

    完全理解MVC并不是很容易.使用MVC需要精心的计划,由于它的内部原理比较复杂,所以需要花费一些时间去思考.同时由于模型和视图要严格的分离,这样也给调试应用程序带来了一定的困难.每个构件在使用之前都需 ...

  8. bootstrap 表单控件 控件状态 控件大小 help-block

    bootstrap 表单控件 控件状态 控件大小 help-block <!DOCTYPE html> <html lang="en"> <head& ...

  9. 初识SuperSocket

    有一些企业由于以前使用的操作系统是被淘汰的操作系统,例如OpenVMS.现需要将针对openvms开发的通讯程序进行移植到现在的windows操作系统上,通过一段时间的了解,现在需要花时间去找商业性的 ...

  10. C# out的使用 函数例题

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...