H file: rasspdm.h
#ifndef __rasspdmon_h_
#define __rasspdmon_h_
Void INITSPEEDMONITOR (VOID);
Void ClearspeedMonitor (Void);
Void getrasspeed (DWORD * PTPS, DWORD * PRPS, DWORD * PTT, DWORD * PRT);
Parameters: send each second, receive, total transmission, total accept
#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 by the DYN_DATA registry under 9x
* /
DWORD GETTOTALRECV (Void)
{
HKEY 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 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, & DewType, (Byte *) & DWValue, & DWLEN
RegcloseKey (HKEY);
}
Return dwvalue;
}
DWORD GETPERSECONDRECV (VOID)
{
HKEY 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 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 it through PER DATA in NT
*********************************************************** *************** /
PPERF_OBJECT_TYPE FIRSTOBJECT (PPERF_DATA_BLOCK Perfdata)
{
Return (pperf_object_type) (PBYTE) Perfdata
Perfdata-> HeaderLength);
}
PPERF_OBJECT_TYPE NEXTOBJECT (PPERF_OBJECT_TYPE Perfobj)
{
Return (PPERF_Object_Type) (PBYTE) PerfoBJ
Perfobj-> Totalbytelength);
}
PPERF_INSTANCE_DEFINITION FIRSTINSTANCE (PPERF_OBJECT_TYPE Perfobj)
{
Return (PPERF_INSTANCE_DEFINITION) (PBYTE) Perfobj
Perfobj-> definitionLENGTH));
}
PPERF_INSTANCE_DEFINITION NEXTINSTANCE
PPERF_INSTANCE_DEFINITION PERFINST)
{
PPERF_COUNTER_BLOCK Perfcntrblk;
Perfcntrblk = (PPERF_COUNTER_BLOCK) (PBYTE) Perfinst
Perfinst-> bytelength;
Return (pperf_instance_definition) (PBYTE) Perfcntrblk
Perfcntrblk-> Bytelength));
}
PPERF_COUNTER_DEFINITION firstCOUNTER (PPERF_Object_Type Perfobj)
{
Return (PPERF_COUNTER_DEFINITION) (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 name from the registry to the *
* Global variable lpnameserserray. *
* ********************************************************** ***************** /
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 (HKEY_LOCAL_MACHINE,
"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,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
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 name Into an Array, by index.
INT IC = 1;
For (lpcurrentstring = lpnameStrings; * lpcurrentstring; lpcurrentstring = (lstrlen (lpcurrentstring) 1))
{
Dwcounter = atol (lpcurrentstring);
LpCurrentString = (lpstrlen (lpcurrentstring) 1);
// Printf ("% d foregth =% D Data =% S / N", IC , DWCUNTER, LPCURRENTSTRING
LPNAMESARRAY [DWCOUNTER] = (lpstr) lpcurrentstring;
}
}
/ ************************************************** *****************
Get RAS speed
*********************************************************** *************** /
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
Parameters: send each second, receive, total transmission, total accept
PPERF_DATA_BLOCK Perfdata = NULL;
PPERF_OBJECT_TYPE Perfobj;
PPERF_INSTANCE_DEFINITION PERFINST;
PPERF_COUNTER_DEFINITION Perfcntr, CURCNTR;
PPERF_COUNTER_BLOCK 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 this is big enough.
Buffersize = byteincrement;
Perfdata = (PPERF_DATA_BLOCK) Realloc (Perfdata, Buffers);
}
// Get the first object type.
Perfobj = firstObject (perfdata);
// Process All Objects.
// Printf ("Object Total =% D / N", Perfdata-> NumObjectTypes);
// getchar (); for (i = 0; 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
{
// display the counter by index and name.
IF ("Bytes Transmitted / Sec", LPNAMESARRAY [Perfcntr-> CounternameTitleIndex]) == 0)
{
* PTPS = * ((DWORD *) ((Byte *) PTRTOCNTR Perfcntr-> counteroffset);
}
IF ("Bytes Received / Sec", LPNAMESARRAY [PerfcnTr-> CounternameTitleIndex]) == 0)
{
* prPs = * ((DWORD *) (Byte *) PTRTOCNTR Perfcntr-> counterOffset);
}
IF (strcmp ("bytes transmitted", lpnameserray [perfcntr-> counternametitleIndex]) == 0)
{
* PTT = * ((DWORD *) (Byte *) PTRTOCNTR Perfcntr-> CounterOffset);
}
IF ("Bytes Received", lpnameserserray [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: send each second, receive, total transmission, total accept
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 Windows
DWORD DWWINDOWSMAJORVERSON = (DWORD)); DWORD DWINDOWSMINORVERSION = (DWORD))
// Get Build Numbers for Windows NT or Win32S
IF (dWVersion >> 31 == 0) // Windows NT
{
GLO_FFROMDYNREG = FALSE;
GetNameStrings ();
}
Else // Windows 95 - 98
GLO_FFROMDYNREG = TRUE;
}
Void ClearspeedMonitor (Void)
{
LPNameStrings
Free (lpnameStrings);
IF (LPNAMESARRAY)
Free (lpnameserray);
}
Instructions:
#include "rasspdm.h"
Bool cyourapp :: initInstance ()
{
// Initialize RAS speed access
INITSPEEDMONITOR ();
...
}
#include "rasspdm.h"
Void cyourWnd :: ONTIMER (uint nidevent)
{
DWORD DWR, DWS, DWPSR, DWPSS; / / Total reception, total transmission, reception per second, send each second
Getrasspeed (& DWPSS, & DWPSR, & DWS, & DWR); // PTPS, DWORD * PRPS, DWORD * PTT, DWORD * PRT)
/ / Parameter: Send per second, receive, total transmission, total reception
...
}
But I found that the transmission per second during use, the two results per second are not very correct, so I use the total number of times to calculate the speed per second.