Chinaunix首页 | 论坛 | 博客
  • 博客访问: 205882
  • 博文数量: 48
  • 博客积分: 1935
  • 博客等级: 上尉
  • 技术积分: 491
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-29 00:59
文章分类

全部博文(48)

文章存档

2011年(1)

2010年(47)

我的朋友

分类: WINDOWS

2010-10-27 23:10:36

C++里面获取单个进程的CPU利用率和内存利用率的API
 

可以使用以下几个函数获得系统进程的使用情况:GetProcessMemoryInfo, EnumProcesses!具体使用方法如MSDN介绍:

 

#include
#include
#include "psapi.h"

void PrintMemoryInfo( DWORD processID )
{
 HANDLE hProcess;
 PROCESS_MEMORY_COUNTERS pmc;

 // Print the process identifier.

 printf( "\nProcess ID: %u\n", processID );

 // Print information about the memory usage of the process.

 hProcess = OpenProcess(  PROCESS_QUERY_INFORMATION |
  PROCESS_VM_READ,
  FALSE, processID );
 if (NULL == hProcess)
  return;

 if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) )
 {
  printf( "\tPageFaultCount: %d\n", pmc.PageFaultCount );
  printf( "\tPageFaultCount: 0x%08X\n", pmc.PageFaultCount );
  printf( "\tPeakWorkingSetSize: 0x%08X\n",
   pmc.PeakWorkingSetSize );
  printf( "\tWorkingSetSize: 0x%08X\n", pmc.WorkingSetSize );
  printf( "\tQuotaPeakPagedPoolUsage: 0x%08X\n",
   pmc.QuotaPeakPagedPoolUsage );
  printf( "\tQuotaPagedPoolUsage: 0x%08X\n",
   pmc.QuotaPagedPoolUsage );
  printf( "\tQuotaPeakNonPagedPoolUsage: 0x%08X\n",
   pmc.QuotaPeakNonPagedPoolUsage );
  printf( "\tQuotaNonPagedPoolUsage: 0x%08X\n",
   pmc.QuotaNonPagedPoolUsage );
  printf( "\tPagefileUsage: 0x%08X\n", pmc.PagefileUsage );
  printf( "\tPeakPagefileUsage: 0x%08X\n",
   pmc.PeakPagefileUsage );
 }

 CloseHandle( hProcess );
}

void main( )
{
 // Get the list of process identifiers.

 DWORD aProcesses[1024], cbNeeded, cProcesses;
 unsigned int i;

 if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
  return;

 // Calculate how many process identifiers were returned.

 cProcesses = cbNeeded / sizeof(DWORD);

 // Print the memory usage for each process

 for ( i = 0; i < cProcesses; i++ )
  PrintMemoryInfo( aProcesses[i] );
}

 

阅读(8905) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~