Created
February 24, 2011 02:58
-
-
Save trinary/841675 to your computer and use it in GitHub Desktop.
Expand PDH wildcard queries on the cmd line
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
// 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