PDH指定されたNIC速度の取得


#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <pdh.h>
#include <pdhmsg.h>
  
  #pragma comment(lib, "pdh.lib")
  
  CONST ULONG SAMPLE_INTERVAL_MS    = 1000;
  CONST PWSTR BROWSE_DIALOG_CAPTION = L"Select a counter to monitor.";
  
  void wmain(void)
  {
      PDH_STATUS Status;
      HQUERY Query = NULL;
      HCOUNTER Counter;
      PDH_FMT_COUNTERVALUE DisplayValue;
      DWORD CounterType;
      SYSTEMTIME SampleTime;
      PDH_BROWSE_DLG_CONFIG BrowseDlgData;//----Intel[R] Centrino[R] Wireless-N 100-----
      WCHAR CounterPathBuffer[PDH_MAX_COUNTER_PATH]=
		  L"\\Network Interface(Intel[R] Centrino[R] Wireless-N 100)\\Bytes Received/sec";
  
      //
      // Create a query.
      //
  
      Status = PdhOpenQuery(NULL, NULL, &Query);
      if (Status != ERROR_SUCCESS) 
      {
         wprintf(L"
PdhOpenQuery failed with status 0x%x.", Status); goto Cleanup; } // // Add the selected counter to the query. // Status = PdhAddCounter(Query, CounterPathBuffer, 0, &Counter); if (Status != ERROR_SUCCESS) { wprintf(L"
PdhAddCounter failed with status 0x%x.", Status); goto Cleanup; } // // Most counters require two sample values to display a formatted value. // PDH stores the current sample value and the previously collected // sample value. This call retrieves the first value that will be used // by PdhGetFormattedCounterValue in the first iteration of the loop // Note that this value is lost if the counter does not require two // values to compute a displayable value. // Status = PdhCollectQueryData(Query); if (Status != ERROR_SUCCESS) { wprintf(L"
PdhCollectQueryData failed with 0x%x.
", Status); goto Cleanup; } // // Print counter values until a key is pressed. // while (!_kbhit()) { Sleep(SAMPLE_INTERVAL_MS); GetLocalTime(&SampleTime); Status = PdhCollectQueryData(Query); if (Status != ERROR_SUCCESS) { wprintf(L"
PdhCollectQueryData failed with status 0x%x.", Status); } wprintf(L"
\" %2.2d:%2.2d:%2.2\"", SampleTime.wHour, SampleTime.wMinute, SampleTime.wSecond); // // Compute a displayable value for the counter. // Status = PdhGetFormattedCounterValue(Counter, PDH_FMT_DOUBLE, &CounterType, &DisplayValue); if (Status != ERROR_SUCCESS) { wprintf(L"
PdhGetFormattedCounterValue failed with status 0x%x.", Status); goto Cleanup; } WCHAR STR[200]; wsprintf(STR,L"%.20g\/s", DisplayValue.doubleValue); } Cleanup: // // Close the query. // if (Query) { PdhCloseQuery(Query); } }