Board logo

标题: [网络连接] 如何用批处理获取网卡当前的连接速度? [打印本页]

作者: hijackle    时间: 2011-7-15 17:21     标题: 如何用批处理获取网卡当前的连接速度?

如何用批处理获取网卡当前的连接速度?


下面这种方法貌似是可以实现的,但是能否用批处理实现呢?
#include <tchar.h>
#include <windows.h>
#include <pdh.h>

#pragma comment(lib, "pdh.lib")

int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpstrCmdLine, int nCmdShow)
{
HQUERY hQuery = NULL ;
PDH_STATUS pdhStatus ;
HCOUNTER * pCounterHandle = NULL ;

pdhStatus = PdhOpenQuery (0, 0, & hQuery) ;
if ( pdhStatus != ERROR_SUCCESS )
{
return 0;
}

pCounterHandle = (HCOUNTER*) GlobalAlloc(GPTR , sizeof(HCOUNTER)) ;

if ( pCounterHandle == NULL )
{
return 0;
}

PDH_FMT_COUNTERVALUE fmtValue ;
DWORD dwctrType ;
DWORD dwCountListLen = NULL;
DWORD dwInstandeListLen = NULL;
LPTSTR lpCounterList = NULL;
LPTSTR lpInstanceList = NULL;
LPTSTR lpString = NULL;
TCHAR szCountPath[256] = {0};
TCHAR szMsg[128] = {0};

PdhEnumObjectItems(NULL, NULL, TEXT("Network Interface"), lpCounterList, &dwCountListLen, lpInstanceList, &dwInstandeListLen, PERF_DETAIL_WIZARD, 0);
lpCounterList = new TCHAR[dwCountListLen];
lpInstanceList = new TCHAR[dwInstandeListLen];

pdhStatus = PdhEnumObjectItems(NULL, NULL, TEXT("Network Interface"), lpCounterList, &dwCountListLen, lpInstanceList, &dwInstandeListLen, PERF_DETAIL_WIZARD, 0);

if ( pdhStatus != ERROR_SUCCESS )
{
return 0;
}

for (lpString=lpInstanceList; *lpString!=0; lpString+=lstrlen(lpString)+1)
{
if (_tcscmp(lpString, TEXT("MS TCP Loopback interface")) == 0)
{
continue;
}

_stprintf_s(szCountPath, _countof(szCountPath), _TEXT ( "\\Network Interface(%s)\\Current Bandwidth"), lpString);
pdhStatus = PdhAddCounter(hQuery, szCountPath, 0, pCounterHandle) ;

if ( pdhStatus != ERROR_SUCCESS )
{
return 0;
}

pdhStatus = PdhCollectQueryData ( hQuery ) ;

if ( pdhStatus != ERROR_SUCCESS )
{
return 0;
}

pdhStatus = PdhGetFormattedCounterValue ( * pCounterHandle , PDH_FMT_LARGE , & dwctrType , & fmtValue ) ;
if ( pdhStatus != ERROR_SUCCESS )
{
return 0;
}

_stprintf_s(szMsg, _countof(szMsg), TEXT("网卡: %s 当前链接速度: %u Mbps"), lpString, fmtValue.largeValue/1000/1000);
MessageBox(GetDesktopWindow(), szMsg, TEXT("网卡链接速度"), MB_ICONASTERISK);
}

delete [] lpCounterList;
delete [] lpInstanceList;
PdhCloseQuery (hQuery) ;

return 1;
}
作者: batman    时间: 2011-7-15 21:04

批处理应该无解。。。
作者: powerbat    时间: 2011-7-15 23:01

  1. wmic path Win32_PerfRawData_Tcpip_NetworkInterface Get Name,CurrentBandwidth /Value
复制代码

作者: batman    时间: 2011-7-15 23:12

楼上的代码可能只适合原生态xp系统。。。
作者: lxzzr    时间: 2011-7-15 23:15

用WMIC从性能计数器中获取网卡的相关数据不一定及时、准确,但是对于批处理而言,好像除此之外别无他法。
作者: broly    时间: 2011-7-15 23:17

3# powerbat


带宽 = 网卡连接速度?




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2