轻松取得winnt下的系统资源信息(cpu利用率,内存使用情况,线程数 )

看到好多朋友询问怎样得到系统信息(象任务管理器一样),前段时间研究了一番,有所领悟,现与大家共享。

  下面是部分测试代码:

#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#include <pdh.h>

#define MAXPATH 80

int __cdecl _tmain (int argc, TCHAR **argv)
{

  HQUERY          hQuery;
  HCOUNTER        *pCounterHandle;
  PDH_STATUS      pdhStatus;
  PDH_FMT_COUNTERVALUE  fmtValue;
  DWORD          ctrType;
  CHAR            szPathBuffer[MAXPATH] = {'/0'};
  int            nRetCode = 0;

  // Open the query object.
  pdhStatus = PdhOpenQuery (0, 0, &hQuery);

  pCounterHandle = (HCOUNTER *)GlobalAlloc(GPTR, sizeof(HCOUNTER));

/*
//Processor(_Total)//% Processor Time CPU使用率
//System//Processes 当前系统进程数
//System//Threads 当前系统线程数
//Memory//Commit Limit 总共内存数K (包括虚拟内存)
//Memory//Committed Bytes 已用内存数K (包括虚拟内存)
//TCP//Connections Active 系统中已建立的 TCP连接个数
其它Object Items 可以利用PdhEnumObjects()和PdhEnumObjectItems()得到
反正我只要用到上面的东东:)
*/
  strcat(szPathBuffer,"//System//Processes");

  pdhStatus = PdhAddCounter (hQuery,
  szPathBuffer,
  0,
  pCounterHandle);
  // "Prime" counters that need two values to display a
  //  formatted value.
  pdhStatus = PdhCollectQueryData (hQuery);

  // Get the current value of this counter.
  pdhStatus = PdhGetFormattedCounterValue (*pCounterHandle,
                                              PDH_FMT_DOUBLE,
                                              &ctrType,
                                              &fmtValue);

    //fmtValue.doubleValue为所要的结果
    if (pdhStatus == ERROR_SUCCESS) {
        printf (TEXT(",/"%.20g/"/n"), fmtValue.doubleValue);
    }
else {
    // Print the error value.
        printf (TEXT("error./"-1/""));
  }

  // Close the query.
  pdhStatus = PdhCloseQuery (hQuery);

  return nRetCode;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
Sigar 是一个跨平台的系统信息收集库,可以获取系统的 CPU内存、磁盘、网络等信息。以下是获取系统信息的示例代码: ```java import org.hyperic.sigar.*; public class SigarDemo { public static void main(String[] args) throws SigarException { Sigar sigar = new Sigar(); System.out.println("CPU信息:"); CpuInfo[] cpuInfos = sigar.getCpuInfoList(); for (CpuInfo cpuInfo : cpuInfos) { System.out.println("CPU型号:" + cpuInfo.getModel()); System.out.println("CPU频率:" + cpuInfo.getMhz() + "MHz"); System.out.println("CPU:" + cpuInfo.getTotalCores()); } System.out.println("内存信息:"); Mem mem = sigar.getMem(); System.out.println("总内存:" + mem.getTotal() / 1024 / 1024 + "MB"); System.out.println("已用内存:" + mem.getUsed() / 1024 / 1024 + "MB"); System.out.println("剩余内存:" + mem.getFree() / 1024 / 1024 + "MB"); System.out.println("磁盘信息:"); FileSystem[] fileSystems = sigar.getFileSystemList(); for (FileSystem fileSystem : fileSystems) { System.out.println("盘符:" + fileSystem.getDirName()); System.out.println("盘符类型:" + fileSystem.getTypeName()); FileSystemUsage usage = sigar.getFileSystemUsage(fileSystem.getDirName()); System.out.println("总大小:" + usage.getTotal() / 1024 / 1024 + "MB"); System.out.println("已用大小:" + usage.getUsed() / 1024 / 1024 + "MB"); System.out.println("剩余大小:" + usage.getFree() / 1024 / 1024 + "MB"); } System.out.println("网络信息:"); NetInterfaceConfig[] netConfigs = sigar.getNetInterfaceConfigList(); for (NetInterfaceConfig netConfig : netConfigs) { System.out.println("网络设备名:" + netConfig.getName()); System.out.println("IP地址:" + netConfig.getAddress()); System.out.println("子网掩码:" + netConfig.getNetmask()); } } } ``` 注意:需要引入 sigar.jar 和 sigar-amd64-winnt.dll(或其它平台对应的库文件)。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

evelan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值