`
standalone
  • 浏览: 597994 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Get information about CPU memory usage

    博客分类:
  • c++
阅读更多
#include <iostream>
#include <iomanip>
#pragma comment (lib, "psapi.lib") // Add 'psapi.lib' to your linker options 

#include <windows.h>
#include <psapi.h> 

using namespace std; 

int main()
{
  // Open current process
  HANDLE hProcess = ::OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, ::GetCurrentProcessId());
  if(hProcess)
  {
    PROCESS_MEMORY_COUNTERS ProcessMemoryCounters; 

    memset(&ProcessMemoryCounters, 0, sizeof(ProcessMemoryCounters)); 

    // Set size of structure
    ProcessMemoryCounters.cb = sizeof(ProcessMemoryCounters); 

    // Get memory usage
    if(::GetProcessMemoryInfo(hProcess,
                              &ProcessMemoryCounters,
                              sizeof(ProcessMemoryCounters)) == TRUE)
    {
      std::cout << std::setfill('0') << std::hex
                << "PageFaultCount: 0x" << std::setw(8)
                << ProcessMemoryCounters.PageFaultCount << std::endl
                << "PeakWorkingSetSize: 0x" << std::setw(8)
                << ProcessMemoryCounters.PeakWorkingSetSize << std::endl
                << "WorkingSetSize: 0x" << std::setw(8)
                << ProcessMemoryCounters.WorkingSetSize << std::endl
                << "QuotaPeakPagedPoolUsage: 0x" << std::setw(8)
                << ProcessMemoryCounters.QuotaPeakPagedPoolUsage << std::endl
                << "QuotaPagedPoolUsage: 0x" << std::setw(8)
                << ProcessMemoryCounters.QuotaPagedPoolUsage << std::endl
                << "QuotaPeakNonPagedPoolUsage: 0x" << std::setw(8)
                << ProcessMemoryCounters.QuotaPeakNonPagedPoolUsage << std::endl
                << "QuotaNonPagedPoolUsage: 0x" << std::setw(8)
                << ProcessMemoryCounters.QuotaNonPagedPoolUsage << std::endl
                << "PagefileUsage: 0x" << std::setw(8)
                << ProcessMemoryCounters.PagefileUsage << std::endl
                << "PeakPagefileUsage: 0x" << std::setw(8)
                << ProcessMemoryCounters.PeakPagefileUsage <<std::endl;
    }
    else
      std::cout << "Could not get memory usage (Error: "
                << ::GetLastError() << ")" << std::endl; 

    // Close process
    ::CloseHandle(hProcess);
  }
  else
    std::cout << "Could not open process (Error "
              << ::GetLastError() << ")" << std::endl; 

  return 0;
} 

 

#pragma   comment(lib,"Ws2_32.lib")表示链接Ws2_32.lib这个库。和在工程设置里写上链入Ws2_32.lib的效果一样,不过这种方法写的程序别人在使用你的代码的时候就不用再设置工程settings了。

分享到:
评论

相关推荐

    System Hardware Information Finder

    In this update I have added the information about CPU vendor (Intel, Cyrix, AMD, etc.) ,CPU speed, and Physical memory status of sustem . CPU Information: To get CPU information I could not find ...

    uCOSII [繁體版]

    task’s priority, suspend and resume a task, and get information about a task. Chapter 5, Time Management This chapter describes how μC/OS-II can suspend a task’s execution until some user specified...

    ora分析脚本

    - pxwplan &lt;sql_id&gt; : get explain plan with work area information of a particular cursor and all connected cursor slave SQL - eplan &lt;sql_id&gt; []: get explain plan with execution statistics - pxeplan ...

    Blender Cycles Lighting and Rendering Cookbook.pdf

    Further on in the book, you will get to know about animation and still shots, and learn how to create advanced materials for realistic rendering, as well cartoon style shaders. This cookbook contains...

    Troubleshooting.with.the.Windows.Sysinternals.Tools.2nd.Edition

    Capture detailed data about processors, memory, and clocks Troubleshoot unbootable devices, file-in-use errors, unexplained communication, and many other problems Understand Windows core concepts that...

    IRPTrace2.00.002.zip

    We strongly recommend that you read the following information about this release. 1) IRPTrace Components ===================== README.TXT - This file HOWTOREG.TXT - How to register IRPTrace and ...

    Practical Windows Forensics

    Get to know how to dump and analyze computer memory. In Detail Over the last few years, the wave of the cybercrime has risen rapidly. We have witnessed many major attacks on the governmental, ...

    MS-DOS 5.0

    information about an updated RCD.SYS device driver. 2.4 Disk Manager ---------------- To upgrade Ontrack Computer System's Disk Manager, do the following: 1. Use the EXPAND command to expand and ...

    Unix编程常见问题解答 高清PDF含书签目录

    1.11 How do I find out about a process' memory usage? 我怎样找出一个进程的存储器使用情况? 1.12 Why do processes never decrease in size? 为什么进程的大小不缩减? 1.13 How do I change the name of my ...

    一个win32下的ARM开源编译器

    flag which can potentially be unclear about what you will get. This is especially important in macros and structures where register numbers may be unknown until instantiated. Both syntaxes support ...

    Mastering.Spring.Cloud

    Running tests with an in-memory database Handling HTTP clients and service discovery Implementing sample tests Integration tests Categorizing tests Capturing HTTP traffic Contract tests Using Pact ...

    EurekaLog_7.5.0.0_Enterprise

    7)....Added: Streaming unpacked debug info into temporal files instead of memory - this greatly reduces run-time application memory usage at cost of slightly slower exception processing. This also ...

    微软内部资料-SQL性能优化3

    For more information about how to decode this value, see also… Inside SQL Server 2000, pages 803 and 806. Key Range Locking Key Range Locking To support SERIALIZABLE transaction semantics, ...

    apktool documentation

    -advance / --advanced will launch advance parameters and information on the usage output -m / --match-original is a new feature for apk analysis. This retains the apk is nearly original format, but ...

    convmv-1.15.tar.gz

    If you convert multiple hundredthousands or millions of files the memory usage of convmv might grow quite high. This option would help you out in that case. --nosmart by default convmv will detect ...

    The Art of Assembly Language Programming

    An Easy Way to Remember the 8086 Memory Addressing Modes 4.6.2.8 - Some Final Comments About 8086 Addressing Modes 4.6.3 - 80386 Register Addressing Modes 4.6.4 - 80386 Memory Addressing ...

    应用Dephi 开发佳能照相机API

    * Description: This is the Sample code to show the usage of EDSDK. * * * * * ******************************************************************************* * * * Written and developed by Camera ...

    Diskeeper 2008 v12.0.759.0

    Diskeeper records information about its activity in two ways. First, general information about previously-run defragmentation job is shown in the Log tab within the Diskeeper console. Additional ...

    ARM® Compiler v5.06 for µVision® armasm User Guide

    Other information 1 Overview of the Assembler 1.1 About the ARM Compiler toolchain assemblers 1.2 Key features of the assembler 1.3 How the assembler works 1.4 Directives that can be omitted in pass 2...

    php.ini-development

    application users can inadvertently leak otherwise secure information. ; This is php.ini-development INI file. ;;;;;;;;;;;;;;;;;;; ; Quick Reference ; ;;;;;;;;;;;;;;;;;;; ; The following are all ...

Global site tag (gtag.js) - Google Analytics