呵呵呵呵呵

萝莉有三好,柔体 轻音 易推倒。女神有三宝,干嘛 呵呵 去洗澡。宅男有三好:Dota 基友 破电脑。


VC/BCB 编程获取网卡当前的连接速度(带宽)

VC/BCB 编程获取网卡当前的连接速度(带宽)

#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;
}

更多内容:

C++ Builder 2010 创建DLL动态库的方法
五类CAT5、超五类CAT5E、六类CAT6和超六类CAT6A四种布线方式系统性能差异
VC/BCB 获取其它进程HANDLE的三种方法
VC 获取PC的CPU,内存等信息示例代码
VC/BCB中system、WinExec、ShellExecute、CreateProcess示例

本文链接地址:http://www.hehehehehe.cn/i/698.html