CPdh
v1.23 A collection of C++ classes to encapsulate the NT Performance Counters
Ever wanted to write some code to determine the amount of bytes sent or received
by Dial-Up Networking, what is the current CPU Usage or how many threads are currently
running on the system. The classes provided allow you do query any of these parameters,
or to query the availability of performance objects and their counters both on the
local machine and any machine on the network. Please note that since Windows 9x
has its own version of performance counters which are incompatible with the NT versions,
these classes will not work on Windows 9x. If you want classes to work with Windows
9x, have a look at the DynData classes which I have developed.
Features
- Simple and clean C++ interface based.
- The classes are fully Unicode compliant.
- Full documentation is provided in the form of a HTML file
The enclosed zip file contains the source code
and a simple console based application which exercises the class.
Copyright
- You are allowed to include the source code in any product (commercial, shareware,
freeware or otherwise) when your product is released in binary form.
- You are allowed to modify the source code in any way you want except you
cannot modify the copyright details at the top of each module.
- If you want to distribute source code with your application, then you are
only allowed to distribute versions released by the author. This is to maintain
a single distribution point for the source code.
Updates
v1.23 (18 March 2022)
- Updated copyright details.
- Updated the code to use C++ uniform initialization for all variable
declarations.
v1.22 (21 March 2020)
- Updated copyright details.
- Fixed more Clang-Tidy static code analysis warnings in the code.
v1.21 (29 December 2019)
- Fixed various Clang-Tidy static code analysis warnings in the code.
v1.20 (21 May 2019)
- Updated copyright details.
- Updated the code to clean compile on VC 2019
v1.19 (1 September 2018)
- Fixed a number of compiler warnings when using VS 2017 15.8.1
v1.18 (15 August 2018)
- Updated copyright details.
- Fixed a number of C++ core guidelines compiler warnings. These changes
mean that the code will now only compile on VC 2017 or later.
- Removed the code which supported the CPDH_MFC_EXTENSIONS preprocessor
define throughout the codebase
- Replaced NULL throughout the codebase with nullptr
- Replaced ATL::CHeapPtr throughout the code with std::vector
- Remove usage of cpdhimpl.cpp module from the codebase. Now all calls to
PDH API are direct.
- Added support for PdhGetDefaultPerfCounterH API.
- Added support for PdhUpdateLogFileCatalog API.
v1.17 (9 December 2017)
- Verified there has been no further additions to PDH from the latest
Windows 10 SDK
- Updated the code to compile cleanly when _ATL_NO_AUTOMATIC_NAMESPACE is
defined.
- Replaced CString::operator LPC*STR() calls throughout the codebase with
CString::GetString calls
30 April 2017
- Updated the demo app to compile cleanly using /permissive-.
V1.16 (25 March 2017)
- Updated copyright details.
- Updated the code to clean compile on VC 2017
V1.15 (22 December 2015)
- Verified there has been no further additions to PDH from the Windows 10
SDK
- Updated the code to clean compile on VC 2015
V1.14 (4 May 2015)
- Updated the code to clean compile on VC 2013
- Updated copyright details
- Reworked the classes to optionally compile without MFC. By default the classes
now use STL classes and idioms but if you define CPDH_MFC_EXTENSIONS the classes
will revert back to the MFC behaviour.
- Added SAL annotations to all the code
- Reworked CPdhDataSource::Update to take a LPCTSTR parameter
- Verified there has been no further additions to PDH from the Windows 8.1
SDK
- Added a new preprocessor macro value of "CPDH_NO_DYNLINK" to control
inclusion of "cpdhimpl.h" in cpdh.cpp.
V1.13 (18 October 2012)
- Updated copyright details
- Updated the code to compile cleanly on VC 2012
- Code no longer uses LoadLibrary without an absolute path when loading PDH
DLL. This avoids DLL planting security issues.
V1.12 (10 April 2010)
- Updated copyright details
- Updated the project settings to more modern default values.
- Reworked all the internal heap allocation code to use ATL::CHeapPtr
- Refactored all the multi-sz parsing code into one method of CPdh
- Reviewed some reported issues with CPdh::ParseInstanceName. The sample app
now uses a string which should parse. Thanks to David Chaplin for reporting
this issue.
- Some of the methods in CPdhCounter and CPdhDataSource can allocate memory.
These details are now encapsulated in AllocateMemory and DeallocateMemory methods,
rather than exposing raw CRT pointers in the client API.
- Verified there has been no further additions to PDH from the Windows 7 SDK
- The code now only supports VC 2005 or later.
V1.11 (12 January 2008)
- Updated copyright details.
- Updated the code and sample app to clean compile on VC 2008
- CPdhException::GetErrorMessage now only calls the FormatMessage function
once. The second call is not required following a review of the FormatMessage
documentation.
- CPdhException::GetErrorMessage now uses the correct return value from FormatMessage.
V1.10 (27 December 2007)
- CPdhException::GetErrorMessage now uses the FORMAT_MESSAGE_IGNORE_INSERTS
flag. For more information please see Raymond Chen's blog at
http://blogs.msdn.com/oldnewthing/archive/2007/11/28/6564257.aspx. Thanks
to Alexey Kuznetsov for reporting this issue.
- Minor update to display HRESULT's correctly in CPdh::ThrowPdhException
- CPDHException::GetErrorMessage now uses Checked::tcsncpy_s if compiled using
VC 2005 or later.
V1.09 (17 March 2007)
- Updated copyright details.
- Updated the documentation for the code to refer to the need for the Platform
SDK if you are compiling on VC 6. Thanks to Nicolas MEURET for reporting this
issue.
V1.08 (27 December 2006)
- Updated copyright details.
- Updated the documentation to use the same style as the web site.
- Code now uses new C++ style casts rather than old style C casts where necessary.
- Optimized CPdhException constructor code
- Removed CPdhException destructor code as it was not really required
- Optimized CPdhCounter constructor code
- Removed CPdhCounter destructor code as it was not really required
- Optimized CPdhQuery constructor code
- Fix a minor copy and paste bug in CPdh::EnumObjects
- Updated code to clean compile on VC 2005
- CPdhQuery::Add now takes an additional BOOL parameter to specify if PdhAddCounter
or PdhAddEnglishCounter should be used
- CPdh::GetDefaultObject now allows the data source to be specified
- CPdh::EnumObjects now allows the data source to be specified
- CPdh::EnumObjectItems now allows the data source to be specified
- CPdhQuery::Open now allows the data source to be specified
- Updated the documentation to provide a more concise description of the various
classes and their functionality.
- Added support for the following PDH functions: PdhAddEnglishCounter,
PdhBindInputDataSource, PdhBrowseCountersH, PdhCloseLog, PdhCollectQueryDataEx,
PdhCollectQueryDataWithTime, PdhEnumLogSetNames, PdhEnumMachinesH,
PdhEnumObjectItemsH, PdhEnumObjectsH, PdhExpandCounterPathH, PdhExpandWildCardPath,
PdhExpandWildCardPathH, PdhFormatFromRawValue, PdhGetCounterTimeBase,
PdhGetDataSourceTimeRange, PdhGetDataSourceTimeRangeH, PdhGetDefaultPerfCounterH,
PdhGetDefaultPerfObjectH, PdhGetFormattedCounterArray, PdhGetLogFileSize,
PdhIsRealTimeQuery, PdhLookupPerfIndexByName, PdhLookupPerfNameByIndex,
PdhOpenLogPdhOpenQueryH, PdhReadRawLogRecord, PdhSelectDataSource,
PdhSetDefaultRealTimeDataSource,
PdhSetQueryTimeRange, PdhUpdateLog &
PdhValidatePathEx
V1.07 (15 February 2003)
- Added some diagnostic code to CPDHQuery::Close() to ASSERT if any counters
are still present in the query when a query object is being destroyed. Some
users of CPdh have reported problems with leaks in some versions of PDH.DLL
when on Windows NT 4/SP6 when do call Add() to add a counter to a query, you
must call Remove() before deleting either the Query or the counter. Failure
to do so give quite a hefty memory leak. The Microsoft docs imply you don't
need to do this. Thanks to Pete Williams for reporting this. Please note that
this is not a bug in the CPDH classes but in PDH.dll itself (or at least some
versions of it.
- Addition of operators in the query and counter class to return the underlying
HCOUNTER / HQUERY.
V1.06 (26 October 2001)
- Fixed a bug in EnumObjectItems, EnumObjects, EnumMachines and ExpandPath
where unitialized data was being parsed. Thanks to Petr Stehlik for spotting
the problem.
- Fixed a number of functions where on Windows XP you get returned PDH_MORE_DATA
even when just asking for the buffer size to allocate.
V1.05 (14 April 2001)
- Fixed a compile problem when statically linking to MFC.
- Updated copyright information.
- Fixed a bug when calling PdhMakeCounterPath which was causing corruption.
- Fixed a memory leak in CPdhCounter::CPdhCounter.
V1.04 (24 March 2000)
- Fixed a problem with size of arrays in CPdh::EnumMachines, CPdh::EnumObjects,
CPdh::EnumObjectItems, CPdh::GetDefaultCounter, CPdh::GetDefaultObject and CPdh::ParseInstanceName
when compiled for UNICODE.
V1.03 (25 January 2000)
- Minor update to improve the trace statements when exceptions are thrown.
V1.02 (13 September 1999)
- Fixed a problem which was causing an unhandled exception being thrown when
the PDH.DLL is not present on client machines.
V1.01 (5 September 1999)
- Fixed a bug in pjPdhState::pjPdhState in CPDHIMPL.CPP which was causing
an access violation
V1.0 (9 February 1999)