Skip to content

Instantly share code, notes, and snippets.

@trinary
Created February 24, 2011 02:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trinary/841675 to your computer and use it in GitHub Desktop.
Save trinary/841675 to your computer and use it in GitHub Desktop.
Expand PDH wildcard queries on the cmd line
// pdhexpand.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string>
#include <sstream>
#include <iostream>
#include <vector>
#include <pdh.h>
#include <pdhmsg.h>
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();
}
int _tmain(int argc, char* argv[])
{
std::string wildcard;
if (argc > 1)
{
wildcard = argv[1];
}
else
{
wildcard = "\\Process(*)\\Thread Count";
}
std::cout << "my query: " << wildcard << std::endl;
std::vector<char> buffer(128, 0);
PDH_STATUS status;
DWORD buff_size;
buff_size = buffer.size();
status = PdhExpandCounterPath(wildcard.c_str(), &buffer[0], &buff_size);
// check to see if the buffer is too small (I hate windows)
if (status == PDH_MORE_DATA) {
buffer.resize(buff_size, 0);
buff_size = buffer.size();
status = PdhExpandCounterPath(wildcard.c_str(), &buffer[0], &buff_size);
}
// this is a real error since we know we have the correct size
if (status != PDH_CSTATUS_VALID_DATA) {
std::string message("failed to expand PDH counter path: ");
message += to_string<PDH_STATUS>(status,std::hex);
std::cout << message << std::endl;
}
else
{
std::string out(buffer.begin(),buffer.end());
std::cout << out << std::endl;
}
std::cout << "Press enter.";
std::cin.ignore(1,'\n');
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment