Created
July 20, 2011 00:51
-
-
Save trinary/1094102 to your computer and use it in GitHub Desktop.
Again, I am doing PDH crap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <windows.h> | |
#include <stdio.h> | |
#include <pdh.h> | |
#include <pdhmsg.h> | |
#include <vector> | |
#include <iostream> | |
#pragma comment(lib, "pdh.lib") | |
CONST ULONG SAMPLE_INTERVAL_MS = 1000; | |
void printerror(DWORD code, HMODULE library) | |
{ | |
LPWSTR message = NULL; | |
if (!FormatMessage(FORMAT_MESSAGE_FROM_HMODULE | | |
FORMAT_MESSAGE_ALLOCATE_BUFFER | | |
/*FORMAT_MESSAGE_IGNORE_INSERTS |*/ | |
FORMAT_MESSAGE_ARGUMENT_ARRAY, | |
library, | |
code, | |
0, | |
(LPWSTR)&message, | |
0, | |
NULL)) | |
//(va_list*)pArgs)) | |
{ | |
std::cout << "Format message failed with 0x%x\n" << GetLastError(); | |
return; | |
} | |
wprintf(L"Formatted message: %s\n", message); | |
LocalFree(message); | |
} | |
template <class T> std::string to_string(T t, std::ios_base & (*f)(std::ios_base&)) | |
{ | |
std::ostringstream oss; | |
oss << f << t; | |
return oss.str(); | |
} | |
void main() | |
{ | |
LPWSTR *commandline; | |
int nargs; | |
PDH_HQUERY hQuery = NULL; | |
HMODULE hPdhLibrary; | |
PDH_STATUS status = ERROR_SUCCESS; | |
std::vector<PDH_HCOUNTER> hCounter; | |
std::vector<wchar_t> wc_buff(128,0); | |
DWORD wc_buff_size = wc_buff.size(); | |
DWORD dwBufferSize = 0; // Size of the pItems buffer | |
DWORD dwItemCount = 0; // Number of items in the pItems buffer | |
PDH_FMT_COUNTERVALUE_ITEM *pItems = NULL; // Array of PDH_FMT_COUNTERVALUE_ITEM structures | |
hPdhLibrary = LoadLibrary(L"pdh.dll"); | |
commandline = CommandLineToArgvW(GetCommandLineW(), &nargs); //ffffuuckck YOUU | |
if (status = PdhOpenQuery(NULL, 0, &hQuery)) | |
{ | |
printerror(status, hPdhLibrary); | |
} | |
// Specify a counter object with a wildcard for the instance. | |
for (int i = 1; i < nargs; ++i) | |
{ | |
PDH_HCOUNTER thisCounter; | |
if (status = PdhExpandCounterPath(commandline[i],&wc_buff[0], &wc_buff_size)) | |
{ | |
if (status == PDH_MORE_DATA) | |
{ | |
wc_buff.resize(wc_buff_size,0); | |
wc_buff_size = wc_buff.size(); | |
status = PdhExpandCounterPath(commandline[i], &wc_buff[0], &wc_buff_size); | |
} | |
if (status != PDH_CSTATUS_VALID_DATA) | |
{ | |
wprintf(L"Failed expanding wildcard %s\n",commandline[i]); | |
printerror(status, hPdhLibrary); | |
} | |
} | |
if (status = PdhAddCounter(hQuery, commandline[i], 0, &thisCounter)) | |
{ | |
wprintf(L"Failed adding %s\n",commandline[i]); | |
printerror(status, hPdhLibrary); | |
} | |
hCounter.push_back(thisCounter); | |
} | |
// Some counters need two sample in order to format a value, so | |
// make this call to get the first value before entering the loop. | |
if (status = PdhCollectQueryData(hQuery)) | |
{ | |
printerror(status,hPdhLibrary); | |
wprintf(L"PdhCollectQueryData failed with 0x%x.\n", status); | |
} | |
Sleep(SAMPLE_INTERVAL_MS); | |
if (status = PdhCollectQueryData(hQuery)) | |
{ | |
printerror(status, hPdhLibrary); | |
wprintf(L"PdhCollectQueryData failed with 0x%x.\n", status); | |
} | |
std::vector<PDH_HCOUNTER>::const_iterator front = hCounter.begin(); | |
std::vector<PDH_HCOUNTER>::const_iterator back = hCounter.end(); | |
for(; front != back ; ++front) | |
{ | |
// Get the required size of the pItems buffer. | |
PDH_HCOUNTER thatCounter = *front; | |
status = PdhGetFormattedCounterArray(thatCounter, PDH_FMT_DOUBLE, &dwBufferSize, &dwItemCount, pItems); | |
if (PDH_MORE_DATA == status) | |
{ | |
pItems = (PDH_FMT_COUNTERVALUE_ITEM *) malloc(dwBufferSize); | |
if (pItems) | |
{ | |
status = PdhGetFormattedCounterArray(thatCounter, PDH_FMT_DOUBLE, &dwBufferSize, &dwItemCount, pItems); | |
if (ERROR_SUCCESS == status) | |
{ | |
// Loop through the array and print the instance name and counter value. | |
for (DWORD i = 0; i < dwItemCount; i++) | |
{ | |
PDH_COUNTER_INFO *infobuffer = NULL; | |
DWORD infobufsize = 0; | |
status = PdhGetCounterInfo(thatCounter,false,&infobufsize,infobuffer); | |
if (status == PDH_MORE_DATA) | |
{ | |
infobuffer = (PPDH_COUNTER_INFO)malloc(infobufsize); | |
status = PdhGetCounterInfo(thatCounter,false,&infobufsize,infobuffer); | |
} | |
else | |
{ | |
printerror(status,hPdhLibrary); | |
} | |
if (infobuffer->szInstanceName == NULL) | |
{ | |
wprintf(L"App|SharePoint|%s|%s\t%.20g\t0\n", infobuffer->szObjectName, infobuffer->szCounterName, pItems[i].FmtValue.doubleValue); | |
} | |
else | |
{ | |
wprintf(L"App|SharePoint|%s|%s|%s\t%.20g\t0\n", infobuffer->szObjectName, pItems[i].szName ,infobuffer->szCounterName, pItems[i].FmtValue.doubleValue); | |
} | |
free(infobuffer); | |
} | |
} | |
else | |
{ | |
printerror(status, hPdhLibrary); | |
wprintf(L"Second PdhGetFormattedCounterArray call failed with 0x%x.\n", status); | |
} | |
} | |
else | |
{ | |
wprintf(L"malloc for PdhGetFormattedCounterArray failed.\n"); | |
return; | |
} | |
} | |
else | |
{ | |
printerror(status, hPdhLibrary); | |
wprintf(L"PdhGetFormattedCounterArray failed with 0x%x.\n", status); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment