#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() { PDH_BROWSE_DLG_CONFIG brwConf; TCHAR cntPath[1024]; HQUERY hQuery; HCOUNTER hCounter; PDH_FMT_COUNTERVALUE fmtVal; _tmemset(&brwConf, 0, sizeof(brwConf)); brwConf.bIncludeInstanceIndex = TRUE; brwConf.bSingleCounterPerAdd = FALSE; brwConf.bSingleCounterPerDialog = TRUE; brwConf.bLocalCountersOnly = FALSE; brwConf.bWildCardInstances = FALSE; brwConf.bHideDetailBox = FALSE; brwConf.hWndOwner = NULL; brwConf.szReturnPathBuffer = cntPath; brwConf.cchReturnPathLength = sizeof(cntPath)-1; brwConf.pCallBack = NULL; brwConf.dwDefaultDetailLevel = PERF_DETAIL_WIZARD; brwConf.szDialogBoxCaption = "Select Counter"; if(PdhBrowseCounters(&brwConf) != ERROR_SUCCESS) { return 0; } _tprintf("%s\n", cntPath); if(PdhOpenQuery(NULL, 0, &hQuery) != ERROR_SUCCESS) { _ftprintf(stderr, "Something is wrong...\n"); return 2; } CHECK_STAT(PdhAddCounter(hQuery, cntPath, 0, &hCounter), end); while(1) { CHECK_STAT(PdhCollectQueryData(hQuery), end); CHECK_STAT(PdhGetFormattedCounterValue(hCounter, PDH_FMT_DOUBLE, NULL, &fmtVal), end); _tprintf("%s = %f\n", cntPath, fmtVal.doubleValue); Sleep(1000); } end: PdhCloseQuery(hQuery); return 0; }