Question

Does anybody know of a method for creating custom Performance Counters using ordinary unmanaged Visual C++?

I know that it can be done easily using managed C++, but I need to do it using an unmanaged Windows service.

I also know that you can retrieve performance counter data, but I need to create some custom counters and increment them during the applications runtime.

Was it helpful?

Solution

See here: http://msdn.microsoft.com/en-us/library/aa371925.aspx

It is not really hard, but a bit tedious as the API involves extensive usage of self-referential, variable-length structures and has to employ some IPC mechanism to obtain the data from the monitored process.

OTHER TIPS

The support for adding C++ performance counters changed in Vista and beyond. The Performance DLL approach suggested in another answer still works, but the new technique described here is easier to use.

In this approach you write a manifest that describes your counters, run CTRPP, a tool that generates code from your manifest. Compile and link this code with your application, and add a call to initialize the process (it starts a background thread), and add code to update the counters as necessary. The details of publishing the counters are handled by the background thread running the generated code.

You also need to run lodctr /m:[manifest file] to register your counters before they can be used. This must be run as an admin.

BTW: Another program, unlodctr reverse the effect of lodctr and must be used if you make any changes to your counters because there is no "replace" operation, only delete the old, then install the new.

<RANT>Documentation for all the above is just plain awful. For example lodctr was completely reworked for Vista, but the doc in MSDN is all for the XP version and no longer applies. If you visit MSDN please use the "This documentation is not helpful" button liberally and maybe Microsoft will get the message.</RANT>

Don't use the ATL performance monitor classes. I know they are easy to add and they have a wizard and all, but they are hopelessly bugged. I added them to one of my development apps at work, then had to go through and rip the code out 6 months later. All in all about 3 weeks work lost to that noise.

I was looking for something a litte easier to implement. I will probably have to use this approach. I was also shown by a colleague (thanks PJ) that there is a Scribble tutorial that has been modified to show how to add a Performance Counter using ATL classes: PerformanceScribble Sample: Performance Monitoring in an MFC Application

The big drawback here is that currently my application doesn't use MFC or ATL, and I would have to add the support for it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top