// GetProcessCpu.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include #include "pdh.h" #pragma comment(lib, "Pdh.lib") void main(void) { PDH_STATUS status = ERROR_SUCCESS; HQUERY hQuery = NULL; HCOUNTER hCounter = NULL; HANDLE hEvent = NULL; DWORD dwWaitResult = 0; PDH_FMT_COUNTERVALUE DisplayValue; DWORD dwCounterType = 0; status = PdhOpenQuery(NULL, 0, &hQuery); if (ERROR_SUCCESS == status) { status = PdhAddCounter(hQuery, "\\Processor(0)\\% Processor Time", 0, &hCounter); if (ERROR_SUCCESS == status) { PdhCollectQueryData(hQuery); while(true) { PdhCollectQueryData(hQuery); status = PdhGetFormattedCounterValue(hCounter, PDH_FMT_DOUBLE, &dwCounterType, &DisplayValue); printf("CPUÕ¼ÓÃ %.2f\n", DisplayValue.doubleValue); Sleep(1000); } } PdhCloseQuery(hQuery); } }