RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Access Raw Data with Performance Counters in Visual C++

Though Windows allows you direct access to performance data, using the native C API is unwieldy. James Curran explains a few C++ techniques to help make it more manageable.

indows lets you monitor a large number of internal statistics. Most people only see them in the Performance Monitor utility. This is fine if all you need is to look at graphs, but to fully study and analyze the information, you'll need to get at the raw data. Windows gives you access to the raw data, but only through a cumbersome C API. However, with the proper application of a few C++ techniques, you can make the whole process much simpler.

Working with (and Wrapping) Queries
At the lowest level, the structures used are defined in the Platform SDK header <winperf.h>. Microsoft apparently realized that those were too tedious for most people to use, so they built a new interface on top of that, known as the Performance Data Helper (PDH) interface. Though the PDH was only a slight improvement, you can still build a useable interface with it.

The structure at the heart of performance monitoring is the HQUERY. It is initialized with the API function PdhOpenQuery(), and closed out with PdhCloseQuery(). Clearly this calls for a wrapper class, which I'll call Query, with a constructor and destructor handling these tasks. The main purpose for a HQUERY is to manage a number of HCOUNTER structures. They are attached to a HQUERY by calling PdhAddCounter(). Add a member function to Query to attach them. Also add a cast to access the underlying HQUERY, and you have a functioning minimal interface for Query (see Listing 1). It's fairly simple, and if that were all, I might not have even bothered with the C++ wrapper. But there is more.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date