Hfile: rasspdm. h
# Ifndef _ rasspdmon_h _ # DEFINE _ rasspdmon_h_void initspeedmonitor (void); void clearspeedmonitor (void); void getrasspeed (DWORD * PTPs, DWORD * PRPs, DWORD * PTT, DWORD * PRT); // parameter: sent per second, received per second, total sent, total accepted # endif
CPP file rasspdm. cpp
# Include "stdafx. H" # include "rasspdm. H" # include
# Define totalbytes 4096 # define byteincrement 1024 lpstr lpnamestrings = NULL; lpstr * lpnamesarray = NULL; bool glo_ffromdynreg = true; /* Get the speed through the dyn_data registry in 9x */DWORD gettotalrecv (void) {hkey; DWORD dwvalue = (DWORD)-1; if (0 = regopenkey (hkey_dyn_data, "perfstats // statdata", & hkey) {DWORD dwtype, dwlen = sizeof (DWORD); regqueryvalueex (hkey, "dial-up adapter // totalbytesrecvd", null, & dwtype, (byte *) & dwvalue, & dwlen); regclosekey (hkey);} return dwvalue;} DWORD gettotalxmit (void) {hkey; DWORD dwvalue = (DWORD) -1; if (0 = regopenkey (hkey_dyn_data, "perfstats // statdata", & hkey) {DWORD dwtype, dwlen = sizeof (DWORD); regqueryvalueex (hkey, "dial-up adapter // totalbytesxmit", null, & dwtype, (byte *) & dwvalue, & dwlen); regclosekey (hkey);} return dwvalue ;} DWORD getpersecondrecv (void) {hkey; DWORD dwvalue = (DWORD)-1; if (0 = regopenkey (hkey_dyn_data, "perfstats // statdata", & hkey )) {DWORD dwtype, dwlen = sizeof (DWORD); regqueryvalueex (hkey, "dial-up adapter // bytesrecvd", null, & dwtype, (byte *) & dwvalue, & dwlen); regclosekey (hkey);} return dwvalue;} DWORD getpersecondxmit (void) {hkey; DWORD dwvalue = (DWORD)-1; if (0 = regopenkey (hkey_dyn_data, "perfstats // statdata", & hkey) {DWORD dwtype, dwlen = sizeof (DWORD); regqueryvalueex (hkey, "dial-up adapter // bytesxmit", null, & dwtype, (byte *) & dwvalue, & dwlen); regclosekey (hkey);} return dwvalue ;} /*************************************** * ************************* get ******* through per data in NT ******* **************************************** * ****************/pperf_object_type firstobject (pperf_data_block perfdata) {return (pperf_object_type) (pbyte) perfdata + perfdata-> headerlength);} else nextobject (pperf_object_type perfobj) {return (pperf_object_type) (pbyte) perfobj + perfobj-> totalbytelength);} specify firstinstance (pperf_object_type perfobj) {return (response) (pbyte) perfobj + perfobj-> definitionlength ));} extends nextinstance (extends perfinst) {pperf_counter_block perfcntrblk; perfcntrblk = (bytes) (pbyte) perfinst + perfinst-> bytelength); Return (bytes) (pbyte) perfcntrblk + perfcntrblk-> bytelength);} specify firstcounter (pperf_object_type perfobj) {return (response) (pbyte) perfobj + perfobj-> headerlength ));} pperf_counter_definition nextcounter (pperf_counter_definition perfcntr) {return (pperf_counter_definition) (pbyte) perfcntr + perfcntr-> bytelength ));} /*************************************** * *************************** load the counter and object names from the registry to ** global variable lpnamesarray. **************************************** * **************************/void getnamestrings () {hkey hkeyperflib; // handle to registry key hkey hkeyperflib009; // handle to registry key DWORD dwmaxvaluelen; // maximum size of key values DWORD dwbuffer; // bytes to allocate for buffers DWORD dwbuffersize; // size of dwbuffer lpstr lpcurrentstring; // pointer for enumerating data strings DWORD dwcounter; // current counter index // get the number of counter items. regopenkeyex (plugin, "software // Microsoft // Windows NT // CurrentVersion // Perflib", 0, key_read, & hkeyperflib); dwbuffersize = sizeof (dwbuffer); regqueryvalueex (hkeyperflib, "Last counter", null, null, (lpbyte) & dwbuffer, & dwbuffersize); regclosekey (hkeyperflib); // allocate memory for the names array. lpnamesarray = (char **) malloc (dwbuffer + 1) * sizeof (lpstr); // open key containing counter and object names. regopenkeyex (HKEY_LOCAL_MACHINE, "software // Microsoft // Windows NT // CurrentVersion // Perflib // 009", 0, key_read, & hkeyperflib009 ); // get the size of the largest value in the key (counter or help ). regqueryinfokey (hkeyperflib009, null, & dwmaxvaluelen, null, null); // allocate memory for the counter and object names. dwbuffer = dwmaxvaluelen + 1; lpnamestrings = (char *) malloc (dwbuffer * sizeof (char); // read counter value. regqueryvalueex (hkeyperflib009, "counter", null, null, (byte *) lpnamestrings, & dwbuffer); // load names into an array, by index. int Ic = 1; for (maid string = lpnamestrings; * maid string; maid string + = (lstrlen (maid string) + 1) {dwcounter = atol (maid string ); lpcurrentstring + = (lstrlen (lpcurrentstring) + 1); // printf ("% d length = % d Data = % s/n", IC ++, dwcounter, lpcurrentstring ); lpnamesarray [dwcounter] = (lpstr) lpcurrentstring ;}} /*************************************** ********** **************************************** * *************/void getrasspeedon9x (DWORD * PTPs, DWORD * PRPs, DWORD * PTT, DWORD * PRT) {// under 9x * PTPs = getpersecondxmit (); * PRPs = getpersecondrecv (); * PTT = gettotalxmit (); * PRT = gettotalrecv ();} void getrasspeedonnt (DWORD * PTPs, DWORD * PRPs, DWORD * PTT, DWORD * PRT) {// under NT // parameter: sent per second, received per second, total sent, total accepted bytes perfdata = NULL; pperf_object_type perfobj; bytes perfinst; average perfcntr, curcntr; average ptrtocntr; DWORD buffersize = totalbytes; DWORD I, j, k; // get the name strings through the registry. // return; // allocate the buffer for the performance data. perfdata = (pperf_data_block) malloc (buffersize); While (regqueryvalueex (hkey_performance_data, "906", // "Global", null, null, (lpbyte) perfdata, & buffersize) = error_more_data) {// get a buffer that is big enough. buffersize + = byteincrement; perfdata = (pperf_data_block) realloc (perfdata, buffersize);} // get the first object type. perfobj = firstobject (perfdata); // process all objects. // printf ("Object Total = % d/N", perfdata-> numobjecttypes); // getchar (); for (I = 0; I <perfdata-> numobjecttypes; I ++) {// display the object by index and name. // get the first counter. perfcntr = firstcounter (perfobj); If (perfobj-> numinstances> 0) {// exit (0);} else {// get the counter block. ptrtocntr = (pperf_counter_block) (pbyte) perfobj + perfobj-> definitionlength); // retrieve all counters. for (j = 0; j <perfobj-> numcounters; j ++) {// display the counter by index and name. if (strcmp ("bytes transmitted/sec", lpnamesarray [perfcntr-> counternametitleindex]) = 0) {* PTPs = * (DWORD *) (byte *) ptrtocntr + perfcntr-> counteroffset);} If (strcmp ("bytes encoded ED/sec", lpnamesarray [perfcntr-> counternametitleindex]) = 0) {* PRPs = * (DWORD *) (byte *) ptrtocntr + perfcntr-> counteroffset);} If (strcmp ("bytes transmitted ", lpnamesarray [perfcntr-> counternametitleindex]) = 0) {* PTT = * (DWORD *) (byte *) ptrtocntr + perfcntr-> counteroffset ));} if (strcmp ("bytes received", lpnamesarray [perfcntr-> counternametitleindex]) = 0) {* PRT = * (DWORD *) (byte *) ptrtocntr + perfcntr-> counteroffset);} // get the next counter. perfcntr = nextcounter (perfcntr) ;}// get the next object type. perfobj = nextobject (perfobj) ;}} void getrasspeed (DWORD * PTPs, DWORD * PRPs, DWORD * PTT, DWORD * PRT) {// parameter: sent per second, received per second, total sent, total accepted if (glo_ffromdynreg = false) {getrasspeedonnt (PTPs, PRPs, PTT, PRT);} else {getrasspeedon9x (PTPs, PRPs, PTT, PRT) ;}} void initspeedmonitor (void) {DWORD dwversion = getversion (); // get major and minor version numbers of windowsdword dwwindowsmajorversion = (DWORD) (lobyte (loword (dwversion); DWORD dwwindowsminorversion = (DWORD) (hibyte (loword (dwversion ))); // get build numbers for Windows NT or win32sif (dwversion> 31 = 0) // Windows NT {glo_ffromdynreg = false; getnamestrings ();} else // Windows 95-98glo_ffromdynreg = true;} void clearspeedmonitor (void) {If (lpnamestrings) Free (lpnamestrings); If (lpnamesarray) Free (lpnamesarray );}
Usage:
# Include "rasspdm. H "bool cyourapp: initinstance () {// initialize the RAS speed to access initspeedmonitor ();...} # include "rasspdm. H "Void cyourwnd: ontimer (uint nidevent) {dword dwr, DWS, dwpsr, dwpss; // total reception, total sending, receiving per second, getrasspeed (& dwpss, & dwpsr, & DWS, & DWR); // PTPs, DWORD * PRPs, DWORD * PTT, DWORD * PRT) // parameter: Send per second, receive per second, total send, total reception ...}
However, I found that the results sent per second and received per second are not very correct, so I calculated the speed per second by reducing the total number.