vc中獲取磁盤IO統計計數

想知道進程讀寫磁盤的狀況,能夠獲取當前進程或指定進程的IO計數。spa

#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 0;
    }
    return -1;
}

若是是檢查其餘進程的話,首先設法拿到進程ID,而後進程句柄須要以下獲取。
須要爲這個句柄指定查詢權限,注意第一個參數:
 hProcess=OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ,FALSE,pid); 
獲取完畢後,將句柄關閉:
 CloseHandle(hProcess); code

相關文章
相關標籤/搜索