[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

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

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


下面这种方法貌似是可以实现的,但是能否用批处理实现呢?
#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;
}
1

评分人数

    • batman: 请将代码用code标签括起,方便他人复制PB -2

3# powerbat


带宽 = 网卡连接速度?
---学无止境---

TOP

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

TOP

楼上的代码可能只适合原生态xp系统。。。
***共同提高***

TOP

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

TOP

批处理应该无解。。。
***共同提高***

TOP

返回列表