VC语言

本类阅读TOP10

·VC++ 学习笔记(二)
·用Visual C++打造IE浏览器(1)
·每个开发人员现在应该下载的十种必备工具
·教你用VC6做QQ对对碰外挂程序
·Netmsg 局域网聊天程序
·Windows消息大全
·VC++下使用ADO编写数据库程序
·VC++学习笔记(四)
·非法探取密码的原理及其防范
·怎样在VC++中访问、修改注册表

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
轻松取得winnt下的系统资源信息(cpu利用率,内存使用情况,线程数 )

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

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

  下面是部分测试代码:

#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;
}

 




相关文章

相关软件