/ * Introduction to use windows performance counters under vc * // * Author: wlzqin * // * Microsoft Windwos NT / 2000 provides a powerful set of API to access a number of system events and performance counter data. We can get the values of the counter in real time or read counter data from a log file. The function can be powerful, and it is easy to use. Below I will simply talk about how to use Windows performance counters in VC. Ok, nonsense, we start: We use a simple example to illustrate the use of performance counters. For example: How do we get the CPU usage of a process that is currently running? You will say: "This is not simple, there are many ways." Of course, I admit that this is not difficult, and there is a lot of ways. But which method is the easiest? Is the highest efficiency? I guess it probably uses performance counters. * // The basic step to use Performance counters is: // 1. Open counter PDHOPENQUERY; // 2. Assign space for counter handle; // 3. Add the resemble of interest to PDHADDCOUNTER; // 4. Collect data PDHCOLLECTQUERYDATA / / 4. Get the value of the counter PDHGETFORMATTEDCOUNTERVALUE; // 5. Turn off the counter PDHCloseQuery.
/ / The following is the step // of the code // first step: // In the header file #include
The above is said, you want to get other technical information only need to change the counter address (the second parameter in the PDHADDCUNTER function "/ process ((" QQ ")) /% processor time"), each counter address contains three parts. (Counter Object Process, Counter, Counter QQ), let's know which counter objects are in your system, each counter object contains which counters, what counter instances have each counter, followed above You can get all count information you want. Microsoft provides us with easy access to counter objects, counters, instance information - sticks. Need to take the following API: 1. PennumObjects (null, // [in] data source, NT4.0 must be null szmachinename, // [in] machine name. Local machine NULL must be NULL SZMACHINENAME, / / [in] machine name. SzObjectListBuffer, // [OUT] Receives buffers of the counter list, if the counter list is 0, then the item is empty & dwobjectListSize, // [in / out] set or receive the counter list length dwdetailLevel, // Get information level // perf_detail_novice primary level // perf_detail_advance Advanced Level (including Proseclas) // perf_detail_expert expert level (including primary and advanced) // perf_detail_wizard system level (contains all levels) true); 2. Crew fresher and counter instance PdhenumObjectItems (null, // [in] data source, NT4.0 must be null szmachinename, // [in] machine name.
The local machine is a null pctCounter, // [in] counter name szcountristbuffer, // [out] to receive buffers of the counter list, if the counter list is 0, the item is empty & dwcounterlistsize, // [in / out] setting Or receive the counter list length szinstancelistbuffer, // [out] receive buffers of the instance list, if the counter list is 0, then the item is empty & dwinstanceListSize, // [in / out] set or receive the instance list length Dwdetaillevel, / / Property level // perf_detail_novice primary level // perf_detail_advance Advanced Level (including primary) // perf_detail_expert expert level (including primary and advanced) // perf_detail_wizard system level (contain all levels) 0); // Last parameter system For more information, please refer to the basic step of the MSDN * /// Crew plug-in counter object is: // 1. Get the counter object list size // 2. Assign buffers for the counter list // 3. Start the stick // below Is a program implementation: // First: Get the list size of the counter object list LPTSTSTSTSTSZOBJECTLISTBUFER = NULL; DWORD DWOBJECTLISTSIZE = 0; LPTSTSTSTHISOBJECT = NULL; __TRY {// First call this function to get the buffer size of the reception performance counter object list PDHSTATUS = PDHENUMOBJECTS (null, // [in] data source, NT4.0 must be null null, // [in] machine name. Local machine is null szobjectlistbuffer, // [out] to receive buffers of the counter list, if the counter The list length is 0, then the item is empty & dwobjectListSize, // [in / out] setting or receive counter list length perf_detail_wizard, // Get information level // perf_detail_novice primary level // perf_d ETAIL_ADVANCE Advanced Level (including primary) // perf_detail_expert expert level (including primary and advanced) // perf_detail_wizard system level (contains all levels) true); if (PDHSTATUS! = Error_Success) {__leave;} // Depending on the buffer size Object list buffer memory assignment counter szObjectListBuffer = (LPTSTR) malloc ((dwObjectListSize * sizeof (TCHAR))); if (szObjectListBuffer == NULL) {__leave;} // this function gets the second call counter object pdhStatus = PdhEnumObjects ( NULL, / / [IN] Data source, NT4.0 must be null null, // [in] machine name.
The local machine is NULL SZOBJECTLISTBUFFER, // [out] to receive buffers of the counter list, if the counter list is 0, the item is empty & dwobjectLisTsize, // [in / out] setting or receive the counter list length perf_detail_wizard, // Get information level // perf_detail_novice primary level // perf_detail_advance Advanced Level (including primary) // perf_detail_expert expert level (including primary and advanced) // perf_detail_wizard system level (contain all levels) True); if (PDHSTATUS! = Error_Success) { __leave;} szThisObject = szObjectListBuffer; // start lifting rod for (; * szThisObject = 0;! szThisObject = (lstrlen (szThisObject) 1)) {// each cycle szThisObject rod is held counter to the object}} __ finally {If (abnormaltermination ()) {// If the fail (SzObjectListBuffer! = Null) {free (szObjectlistbuff); szobjectListbuffer = null;}} else {// If success}} //, don't forget free / * Crew to get the counter object to continue the stick to raise the counter and counter instances, methods, and basic interests above, interested friends can do it yourself, limited to the space, I will not repeat. I am talking about how to know the description information of the counter (but Chinese!), That is, what mean each counter represents? What is it used? To know that each counter description information needs to be used to use the PDHGetCounterInfo function (all in the API started at the beginning of the PDH). * /// Basic steps are as follows: // 1. Formatting a counter address (string) / * Here you need to explain: There are many counters without instance. There is a slightly different form of examples and formatting without instances. For example: (have an example) Get the byte speed transmitted to the disk when the current write operation: you need to use the "PhysicalDisk" counter object, the "Disk Write Bytes / Sec" counter under the counter object, and the counter instance (in The instance of my machine is on the main hard disk is "0 c: D: E: F:"), then get the counter address I transmit to the byte speed on the main hard drive is: "/ PhysicalDisk (" 0 C: D: E : F: ") / disk write bytes / sec". (No case) Get the time (in seconds) of the computer after the last startup: The "System Up Time" counter under the "System" counter object, the cover counter object, no instance, then this address is : "/ System / System Up Time".