- 帖子
- 76
- 积分
- 115
- 技术
- 0
- 捐助
- 0
- 注册时间
- 2010-12-18
|
如何用批处理获取网卡当前的连接速度?
下面这种方法貌似是可以实现的,但是能否用批处理实现呢?
#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
评分人数
-
|