- /**
- * Obtain network traffic
- */
- Int getnetworkpercent ()
- {
- DWORD dwsize = 0;
- DWORD dwretval = 0;
- DWORD speed = 0;
- Mib_iftable * piftable;
- Mib_ifrow * pifrow;
- Piftable = (mib_iftable *) malloc (sizeof (mib_iftable ));
- If (piftable = NULL)
- {
- Return 1;
- }
- /* Call getiftable for the first time to obtain necessary information and write data */
- Dwsize = sizeof (mib_iftable );
- If (getiftable (piftable, & dwsize, false) = error_insufficient_buffer)
- {
- Free (piftable );
- Piftable = (mib_iftable *) malloc (dwsize );
- If (piftable = NULL)
- {
- Return 1;
- }
- }
Performance Counter acquisition
- # Include <windows. h>
- # Include <PDH. h>
- # Include <stdio. h>
- # Pragma comment (Lib, "PDH. lib ")
- # Include <string>
- Int main (INT argc, char * argv []) {
- If (argc <3 ){
- Printf ("performance count program [interval]/n ");
- Return-1;
- }
- Hquery query;
- Hcounter counter;
- Pdh_status status;
- Status = pdhopenquery (0, 0, & query );
- If (status! = Error_success ){
- Printf ("Open query failed! ");
- }
- Status = pdhaddcounter (query, (STD: string ("// process (") + argv [2] + ") // % processor time "). c_str (), 0, & Counter );
- If (status! = Error_success ){
- Printf ("add counter failed! ");
- }
- Pdh_fmt_countervalue * value = (pdh_fmt_countervalue *) globalalloc (gptr, sizeof (pdh_fmt_countervalue ));
- Int COUNT = atoi (argv [1]);
- Int interval = argc = 4? Atoi (argv [3]): 1000;
- Pdhcollectquerydata (query );
- While (count --){
- Sleep (interval );
- DWORD type;
- Pdhcollectquerydata (query );
- Status = pdhgetformattedcountervalue (counter, pdh_fmt_double, & type, value );
- If (status! = Error_success ){
- Printf ("read counter value failed! ");
- Break;
- }
- Printf ("% F/N", value-> doublevalue );
- }
- Globalfree (value );
- Pdhremovecounter (Counter );
- Pdhclosequery (query );
- Return 0;
- }
- This is the code I previously wrote to obtain CPU usage. The network usage method is the same.
- Pdhaddcounter (query, (STD: string ("// process (") + argv [2] + ") // % processor time "). c_str (), 0, & Counter );
- Modify the counter target in this sentence...
- For details, refer to msdn.