PerfCtrv2Mgr v1.06
Welcome to PerfCtrv2Mgr, A collection of freeware C++ classes to encapsulate the
Performance Counters v2 infrastructure available in Windows Vista or later.
Usage
- To use the classes in your code, just include "PerfCtrlv2Mgr.h" in
which ever module requires it in your project.
- As of v1.03, the classes are designed for VC 2017 or later. They will not
compile on earlier releases of VC.
- The sample app included in the download is a VC 2017 console app which
demonstrates the functionality.
- The code is UNICODE enabled and build configurations are provided for in the
sample app.
- To generate a collection of counters you start with a xml manifest file
which defines the counters you want. In the sample this is
"PerfCountersv2.man".
- You then need to compile this manifest file using the SDK tool ctrpp.exe using for
example the command line: ctrpp -o perfcountersv2.h -rc perfcountersv2.rc
perfcountersv2.man. Ideally you should create a simple resource only DLL
project which just includes the perfcountersv2.rc (or your equivalent) file.
This file will be loaded by Windows when it needs the strings for a specific
set of counters. For
the included sample project the rc file is simply included in the main exe
project for convenience.
- You should then register the xml manifest file from an elevated command prompt: lodctr "/M:c:\....\PerfCountersv2.man" Info: Successfully
installed performance counters in c:\....\PerfCountersv2.man. Lodctr is a
standard Windows executable for registering performance counters. At this point
if you bring up Windows Performance Monitor (aka PerfMon) to show the counters it will fail to show the
counters and instead you will get two guids which are the guids of the
sample counter sets. This is because the PerfCountersv2.exe has not been
created yet.
- Add the "PerfCtrv2Mg.h" header file to your project
- Initialize and create whatever counters you need along the lines
of the code in the "perfcountersv2.cpp" file. This sample code registers the two
counter sets and then randomly changes each counter up or down every
second for a total of thirty seconds before the app exits.
- Build your project and ensure that the DLL or exe which includes the rc
strings from the earlier step is in the same directory as where you
registered the man file from. In the included sample app this would entail
coping the PerfCountersv2.exe up one level.
- If you now bring up Perfmon, you should see two
new counters called "PerfCountersv2 (CounterSet1)" and "PerfCounterv2
(CounterSet2)".
- If you now run the sample exe, and add the two counters in Perfmon you
should now see the two counters change randomly up or down every second.
- Hopefully the tutorial above should provide a quick start for you to add
a simple performance counters infrastructure to your apps. Happing coding!
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.
History
v1.06 (17 March 2022)
- Updated copyright details
- Updated the code to use C++ uniform initialization for all variable
declarations.
v1.05 (21 September 2021)
- Reinstated a previous GetInstance method.
15 August 2021
- Updated copyright details.
- Updated compiler settings to more modern
defaults.
v1.04 (12 April 2020)
- Updated copyright details.
- Fixed more Clang-Tidy static code analysis warnings in the code.
v1.03 (19 September 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 all code which uses LoadLibrary \ GetProcAddress.
v1.02 (10 February 2016)
- Updated the code to use LoadLibrary rather than relying
on client applications already having a implicit dependency on ADVAPI32.dll.
This bug / issue was spotted when you compile the sample app in release mode
which ships with the wrapper classes.
v1.01 (2 January 2016)
- Updated copyright details
- Added SAL annotations to all the code
- Removed unnecessary CStringW instance from
CPerfProvider::QueryInstance method
- CPerfCounterSet::CreateInstance now uses a PCWSTR parameter
instead of a LPCTSTR parameter. This avoids the need to use of CStringW
local variable
- Updated the parameter values names for the classes to be
consistent with the Windows 10 SDK perflib.h header.
- Updated the code to compile without taking a dependency on MFC.
v1.0 (11 October 2012)
API Reference
- There are 2 main classes included. They are:
CPerfProvider which is a simple C++ wrapper for anything which requires a
HANDLE in the Win32 Performance counters API e.g. PerfStartProvider,
PerfStopProvider, etc.
CPerfCounterSet encapsulates a so called performance counter set. This
represents a logical grouping of counters and provides methods to create and
destroy instances of a counter as well as modifying the actual values of a
counter.
- The classes provide a simple wrapping over their SDK equivalents and you
should refer to the MSDN documentation for further details. For example the
method CPerfCounterSet::CreateInstance wraps the function PerfCreateInstance.
Contacting the Author
PJ Naughter
Email: pjna@naughter.com
Web: http://ww.naughter.com
17 March 2022