#include #include #include #include #include #define CHECK_STAT(func, end) \ do { \ PDH_STATUS __zz_stat; \ __zz_stat = (func); \ if(__zz_stat != ERROR_SUCCESS) { \ _ftprintf(stderr, "error! ret=0x%x in line %d\n", __zz_stat, __LINE__); \ goto end; \ } \ } while(0) int main(int argc, char *argv[]) { HQUERY hQuery; HCOUNTER hCounter; PDH_FMT_COUNTERVALUE fmtVal; if(argc != 2) { _ftprintf(stderr, "Usage: %s counter_name\n", argv[0]); return 1; } if(PdhOpenQuery(NULL, 0, &hQuery) != ERROR_SUCCESS) { _ftprintf(stderr, "Something is wrong...\n"); return 2; } CHECK_STAT(PdhAddCounter(hQuery, argv[1], 0, &hCounter), end); CHECK_STAT(PdhCollectQueryData(hQuery), end); CHECK_STAT(PdhGetFormattedCounterValue(hCounter, PDH_FMT_DOUBLE, NULL, &fmtVal), end); _tprintf("%s = %f\n", argv[1], fmtVal.doubleValue); end: PdhCloseQuery(hQuery); return 0; }