VC獲取網絡卡流量
BOOL CTSysWatch::GetNetAdapterFlux(LPNETADAPTER_FLUX pAdapterFlux, int nIndex )
{
// Declare and initialize variables
DWORD dwRetVal = 0;
// Make an initial call to GetIfTable to get the
// necessary size into the dwSize variable
if(m_ifTable == NULL)
{
if (GetIfTable(m_ifTable, &m_dwTableSize, 0) == ERROR_INSUFFICIENT_BUFFER)
{
m_ifTable = (MIB_IFTABLE *) malloc (m_dwTableSize);
}
}
// Make a second call to GetIfTable to get the
// actual data we want
if ((dwRetVal = GetIfTable(m_ifTable, &m_dwTableSize, 0)) != NO_ERROR)
{
return FALSE;
}
int nPos = 0;
for(int i = 0; i < (int)m_ifTable->dwNumEntries; i++)
{
if(m_ifTable->table[i].dwIndex == (DWORD)nIndex)
{
nPos = i;
break;
}
}
if(nPos == (int)m_ifTable->dwNumEntries)
return FALSE;
if(m_parrAdapterTable == NULL)
{
m_parrAdapterTable = new ADAPTER_TABLE[m_ifTable->dwNumEntries];
memset(m_parrAdapterTable,0,m_ifTable->dwNumEntries * sizeof(ADAPTER_TABLE));
m_parrAdapterTable[nPos].dwOldInOctets = m_ifTable->table[nPos].dwInOctets;
m_parrAdapterTable[nPos].dwOldOutOctets = m_ifTable->table[nPos].dwOutOctets;
return TRUE;
}
//in
pAdapterFlux->fInCurrentFlux = (float)((m_ifTable->table[nPos].dwInOctets - m_parrAdapterTable[nPos].dwOldInOctets) * 8.0/1000.0);
m_parrAdapterTable[nPos].dwOldInOctets = m_ifTable->table[nPos].dwInOctets;
if(pAdapterFlux->fInCurrentFlux > m_parrAdapterTable[nPos].fInMaxFlux)
m_parrAdapterTable[nPos].fInMaxFlux = pAdapterFlux->fInCurrentFlux;
pAdapterFlux->fInMaxFlux = m_parrAdapterTable[nPos].fInMaxFlux;
m_parrAdapterTable[nPos].fInAverageFlux = (float)(m_parrAdapterTable[nPos].fInAverageFlux + (pAdapterFlux->fInCurrentFlux - m_parrAdapterTable[nPos].fInAverageFlux) / (m_parrAdapterTable[nPos].dwTimes + 1.0));
pAdapterFlux->fInAverageFlux = m_parrAdapterTable[nPos].fInAverageFlux;
//out
pAdapterFlux->fOutCurrentFlux = (float)((m_ifTable->table[nPos].dwOutOctets - m_parrAdapterTable[nPos].dwOldOutOctets) * 8.0/1000.0);
m_parrAdapterTable[nPos].dwOldOutOctets = m_ifTable->table[nPos].dwOutOctets;
if(pAdapterFlux->fOutCurrentFlux > m_parrAdapterTable[nPos].fOutMaxFlux)
m_parrAdapterTable[nPos].fOutMaxFlux = pAdapterFlux->fOutCurrentFlux;
pAdapterFlux->fOutMaxFlux = m_parrAdapterTable[nPos].fOutMaxFlux;
m_parrAdapterTable[nPos].fOutAverageFlux = (float)(m_parrAdapterTable[nPos].fOutAverageFlux + (pAdapterFlux->fOutCurrentFlux - m_parrAdapterTable[nPos].fOutAverageFlux) / (m_parrAdapterTable[nPos].dwTimes + 1.0));
pAdapterFlux->fOutAverageFlux = m_parrAdapterTable[nPos].fOutAverageFlux;
m_parrAdapterTable[nPos].dwTimes++;
return TRUE;
}
//return 0 success
//return 1 ERROR_INSUFFICIENT_BUFFER
//return -1 failed
BOOL CTSysWatch::GetNetAdapterInfo(LPNETINTERFACE_INFO pIfInfo,PDWORD pdwCount)
{
// Declare and initialize variables
DWORD dwRetVal = 0;
// Make an initial call to GetIfTable to get the
// necessary size into the dwSize variable
if(m_ifTable == NULL)
{
if (GetIfTable(m_ifTable, &m_dwTableSize, 0) == ERROR_INSUFFICIENT_BUFFER)
{
m_ifTable = (MIB_IFTABLE *) malloc (m_dwTableSize);
}
}
// Make a second call to GetIfTable to get the
// actual data we want
if ((dwRetVal = GetIfTable(m_ifTable, &m_dwTableSize, 0)) != NO_ERROR)
{
return -1;
}
if(*pdwCount < m_ifTable->dwNumEntries)
{
*pdwCount = m_ifTable->dwNumEntries;
return 1;
}
for(int i = 0; i < (int)m_ifTable->dwNumEntries; i++)
{
pIfInfo[i].dwIndex = m_ifTable->table[i].dwIndex;
pIfInfo[i].dwType = m_ifTable->table[i].dwType;
memcpy(pIfInfo[i].bDescr,m_ifTable->table[i].bDescr,MAXLEN_IFDESCR);
}
return 0;
}
呼叫:
char szTmp[25] = {0};
NETADAPTER_FLUX AdapterFlux = {0};
GetNetAdapterFlux(&AdapterFlux);
memset(szTmp,0x00,25);
sprintf(szTmp,"%.2fk/%.2fk",AdapterFlux.fInCurrentFlux,AdapterFlux.fOutCurrentFlux);