想知道进程读写磁盘的情况,可以获取当前进程或指定进程的IO计数。
#include <Windows.h>
int get_io_bytes(ULONGLONG * read_bytes, ULONGLONG * write_bytes,ULONGLONG * wct,ULONGLONG * rct)
{
IO_COUNTERS io_counter;
HANDLE hProcess=GetCurrentProcess();//获取当前进程句柄
if(GetProcessIoCounters(hProcess, &io_counter))
{
if(read_bytes) *read_bytes = io_counter.ReadTransferCount;//字节数
if(write_bytes) *write_bytes = io_counter.WriteTransferCount;
if(wct) *wct=io_counter.WriteOperationCount;//次数
if(rct) *rct=io_counter.ReadOperationCount;
return ;
}
return -;
}
如果是检查其他进程的话,首先设法拿到进程ID,然后进程句柄需要如下获取。
需要为这个句柄指定查询权限,注意第一个参数:
hProcess=OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ,FALSE,pid);
获取完毕后,将句柄关闭:
CloseHandle(hProcess);