Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 5.02 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2.  
  3. #include <windows.h>
  4. #include <stdio.h>
  5. #include <pdh.h>
  6. #include <pdhmsg.h>
  7. #include <vector>
  8. #include <iostream>
  9.  
  10. #pragma comment(lib, "pdh.lib")
  11.  
  12. CONST ULONG SAMPLE_INTERVAL_MS = 1000;
  13.  
  14.  
  15. void printerror(DWORD code, HMODULE library)
  16. {
  17.         LPWSTR message = NULL;
  18.  
  19.         if (!FormatMessage(FORMAT_MESSAGE_FROM_HMODULE |
  20.                        FORMAT_MESSAGE_ALLOCATE_BUFFER |
  21.                        /*FORMAT_MESSAGE_IGNORE_INSERTS |*/
  22.                        FORMAT_MESSAGE_ARGUMENT_ARRAY,
  23.                        library,
  24.                                            code,
  25.                        0,  
  26.                        (LPWSTR)&message,
  27.                        0,
  28.                        NULL))
  29.                        //(va_list*)pArgs))
  30.     {
  31.                 std::cout << "Format message failed with 0x%x\n" <<  GetLastError();
  32.         return;
  33.     }
  34.  
  35.     wprintf(L"Formatted message: %s\n", message);
  36.     LocalFree(message);
  37. }
  38.  
  39.  
  40. template <class T> std::string to_string(T t, std::ios_base & (*f)(std::ios_base&))
  41. {
  42.   std::ostringstream oss;
  43.   oss << f << t;
  44.   return oss.str();
  45. }
  46.  
  47.  
  48. void main()
  49. {
  50.         LPWSTR *commandline;
  51.         int nargs;
  52.     PDH_HQUERY hQuery = NULL;
  53.         HMODULE hPdhLibrary;
  54.        
  55.     PDH_STATUS status = ERROR_SUCCESS;
  56.         std::vector<PDH_HCOUNTER> hCounter;
  57.         std::vector<wchar_t> wc_buff(128,0);
  58.         DWORD wc_buff_size = wc_buff.size();
  59.     DWORD dwBufferSize = 0;         // Size of the pItems buffer
  60.     DWORD dwItemCount = 0;          // Number of items in the pItems buffer
  61.     PDH_FMT_COUNTERVALUE_ITEM *pItems = NULL;  // Array of PDH_FMT_COUNTERVALUE_ITEM structures
  62.        
  63.         hPdhLibrary = LoadLibrary(L"pdh.dll");
  64.  
  65.         commandline = CommandLineToArgvW(GetCommandLineW(), &nargs); //ffffuuckck YOUU
  66.     if (status = PdhOpenQuery(NULL, 0, &hQuery))
  67.     {
  68.                 printerror(status, hPdhLibrary);
  69.     }
  70.  
  71.     // Specify a counter object with a wildcard for the instance.
  72.         for (int i = 1; i < nargs; ++i)
  73.         {
  74.                 PDH_HCOUNTER thisCounter;
  75.                 if (status = PdhExpandCounterPath(commandline[i],&wc_buff[0], &wc_buff_size))
  76.                 {
  77.                         if (status == PDH_MORE_DATA)
  78.                         {
  79.                                 wc_buff.resize(wc_buff_size,0);
  80.                                 wc_buff_size = wc_buff.size();
  81.                                 status = PdhExpandCounterPath(commandline[i], &wc_buff[0], &wc_buff_size);
  82.                         }
  83.                         if (status != PDH_CSTATUS_VALID_DATA)
  84.                         {
  85.                                 wprintf(L"Failed expanding wildcard %s\n",commandline[i]);
  86.                                 printerror(status, hPdhLibrary);
  87.                         }
  88.                 }
  89.  
  90.                 if (status = PdhAddCounter(hQuery, commandline[i], 0, &thisCounter))
  91.                 {
  92.                         wprintf(L"Failed adding %s\n",commandline[i]);
  93.                         printerror(status, hPdhLibrary);
  94.                 }
  95.                 hCounter.push_back(thisCounter);
  96.         }
  97.  
  98.     // Some counters need two sample in order to format a value, so
  99.     // make this call to get the first value before entering the loop.
  100.     if (status = PdhCollectQueryData(hQuery))
  101.     {
  102.                 printerror(status,hPdhLibrary);
  103.         wprintf(L"PdhCollectQueryData failed with 0x%x.\n", status);
  104.    }
  105.  
  106.        
  107.         Sleep(SAMPLE_INTERVAL_MS);
  108.  
  109.     if (status = PdhCollectQueryData(hQuery))
  110.     {
  111.                 printerror(status, hPdhLibrary);
  112.         wprintf(L"PdhCollectQueryData failed with 0x%x.\n", status);
  113.     }
  114.  
  115.         std::vector<PDH_HCOUNTER>::const_iterator front = hCounter.begin();
  116.         std::vector<PDH_HCOUNTER>::const_iterator back  = hCounter.end();
  117.        
  118.         for(; front != back ; ++front)
  119.         {
  120.                
  121.     // Get the required size of the pItems buffer.
  122.         PDH_HCOUNTER thatCounter = *front;
  123.     status = PdhGetFormattedCounterArray(thatCounter, PDH_FMT_DOUBLE, &dwBufferSize, &dwItemCount, pItems);
  124.     if (PDH_MORE_DATA == status)
  125.     {
  126.         pItems = (PDH_FMT_COUNTERVALUE_ITEM *) malloc(dwBufferSize);
  127.         if (pItems)
  128.         {
  129.             status = PdhGetFormattedCounterArray(thatCounter, PDH_FMT_DOUBLE, &dwBufferSize, &dwItemCount, pItems);
  130.             if (ERROR_SUCCESS == status)
  131.             {
  132.                 // Loop through the array and print the instance name and counter value.
  133.                 for (DWORD i = 0; i < dwItemCount; i++)
  134.                 {
  135.                                         PDH_COUNTER_INFO *infobuffer = NULL;
  136.                                         DWORD infobufsize = 0;
  137.                                         status = PdhGetCounterInfo(thatCounter,false,&infobufsize,infobuffer);
  138.                                         if (status == PDH_MORE_DATA)
  139.                                         {
  140.                                                 infobuffer = (PPDH_COUNTER_INFO)malloc(infobufsize);
  141.                                                 status = PdhGetCounterInfo(thatCounter,false,&infobufsize,infobuffer);
  142.                                         }
  143.                                         else
  144.                                         {
  145.                                                 printerror(status,hPdhLibrary);
  146.                                         }
  147.                                         if (infobuffer->szInstanceName == NULL)
  148.                                         {
  149.                                                 wprintf(L"App|SharePoint|%s|%s\t%.20g\t0\n", infobuffer->szObjectName, infobuffer->szCounterName, pItems[i].FmtValue.doubleValue);
  150.                                         }
  151.                                         else
  152.                                         {                                              
  153.                                                 wprintf(L"App|SharePoint|%s|%s|%s\t%.20g\t0\n", infobuffer->szObjectName, pItems[i].szName ,infobuffer->szCounterName, pItems[i].FmtValue.doubleValue);
  154.                                         }
  155.                                         free(infobuffer);
  156.                 }
  157.             }
  158.             else
  159.             {
  160.                                 printerror(status, hPdhLibrary);
  161.                 wprintf(L"Second PdhGetFormattedCounterArray call failed with 0x%x.\n", status);
  162.             }
  163.  
  164.         }
  165.         else
  166.         {
  167.             wprintf(L"malloc for PdhGetFormattedCounterArray failed.\n");
  168.                         return;
  169.         }
  170.     }
  171.     else
  172.     {
  173.                                         printerror(status, hPdhLibrary);
  174.         wprintf(L"PdhGetFormattedCounterArray failed with 0x%x.\n", status);
  175.     }
  176. }
  177.  
  178. }