结果视图:
.h代码:
#pragma once
#include "afxwin.h"
#define WMMY_NETWORKER_MESSAGE WM_USER+100
// CMyNetCheckSystemDlg 对话框
class CMyNetCheckSystemDlg : public CDialogEx
{
// 构造
public:
CMyNetCheckSystemDlg(CWnd* pParent = NULL); // 标准构造函数
// 对话框数据
enum { IDD = IDD_MYNETCHECKSYSTEM_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
HICON m_hIcon;
afx_msg LRESULT MYMSGNETWORKER(WPARAM wParam, LPARAM lParam);
// 生成的消息映射函数
virtual BOOL OnInitDialog();
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnBnClickedButton1();
afx_msg void OnBnClickedButton5();
CStatic m_downspeed;
CStatic m_upspeed;
afx_msg void OnTimer(UINT_PTR nIDEvent);
};
.cpp代码:
// MyNetCheckSystemDlg.cpp : 实现文件
//
#include<strsafe.h>
#include<stdio.h>
#include<Windows.h>
#include<objbase.h>
#include<IPHlpApi.h>
#pragma comment(lib,"Iphlpapi.lib")
#include<SetupAPI.h>
#pragma comment(lib,"Setupapi.lib")
unsigned int _stdcall ThreadCheckSpeed(LPVOID lpArgs)
{
CMyNetCheckSystemDlg *pDlg = (CMyNetCheckSystemDlg*)lpArgs;
PMIB_IFTABLE m_pTable = NULL;
DWORD m_dwAdapters = 0;
ULONG uRetCode = GetIfTable(m_pTable,&m_dwAdapters,TRUE);
if (uRetCode == ERROR_NOT_SUPPORTED)
{
//return (-1);
exit(-1);
}
if (uRetCode == ERROR_INSUFFICIENT_BUFFER)
{
m_pTable = (PMIB_IFTABLE)new BYTE[65535]; //假设端口数不超过65535个
}
DWORD dwLastIn = 0; //上一秒钟的接收字节数
DWORD dwLastOut = 0; //上一秒钟的发送字节数
DWORD dwBandIn = 0; //下载速度
DWORD dwBandOut = 0; //上传速度
while (1)
{
GetIfTable(m_pTable, &m_dwAdapters, TRUE);
DWORD dwInOctets = 0;
DWORD dwOutOctets = 0;
//将所有端口的流量进行统计
for (UINT i = 0; i < m_pTable->dwNumEntries; i++)
{
MIB_IFROW Row = m_pTable->table[i];
dwInOctets += Row.dwInOctets;
dwOutOctets += Row.dwOutOctets;
}
dwBandIn = dwInOctets - dwLastIn; //下载速度
dwBandOut = dwOutOctets - dwLastOut; //上床速速
if (dwLastIn <= 0)
{
dwBandIn = 0;
}
else
{
dwBandIn = dwBandIn / 1024; //b转换成kb
}
if (dwLastOut <= 0)
{
dwBandOut = 0;
}
else
{
dwBandOut = dwBandOut / 1024; //b转换成kb
}
dwLastIn = dwInOctets;
dwLastOut = dwOutOctets;
printf("收到字节: %u bytes\n", dwLastIn);
printf("发送字节: %u bytes\n", dwLastOut);
printf("下行速度: %u KB\n", dwBandIn);
printf("上行速度: %u KB\n", dwBandOut);
printf("--------------------------\n");
PostMessage(pDlg->m_hWnd, WMMY_NETWORKER_MESSAGE, dwBandIn, dwBandOut);
Sleep(1000);
//休眠一秒钟
}
delete[] m_pTable;
return 0;
}
LRESULT CMyNetCheckSystemDlg::MYMSGNETWORKER(WPARAM wParam, LPARAM lParam)
{
// 数据的处理
CString dwBandIn;
dwBandIn.Format(_T("下行速度:%u KB"),wParam);
m_downspeed.SetWindowText(dwBandIn);
CString dwBandOut;
dwBandOut.Format(_T("上行速度:%u KB"), lParam);
m_upspeed.SetWindowText(dwBandOut);
return 0;
}
//------------------------------------------------------------------------------
int DisableLANFunc(void)
{
HDEVINFO hDevInfo = INVALID_HANDLE_VALUE;
// 获得一个返回值
hDevInfo = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_PRESENT | DIGCF_ALLCLASSES);
if (INVALID_HANDLE_VALUE==hDevInfo)
{
return GetLastError();
}
SP_DEVINFO_DATA DeviceInfoData = { sizeof(SP_DEVINFO_DATA) };
LPOLESTR guid;
TCHAR devName[128];
TCHAR instanceId[128];
for (int i = 0;SetupDiEnumDeviceInfo(hDevInfo,i,&DeviceInfoData) ; i++)
{
// 将CLSID结构转换为字符串方式
StringFromCLSID(DeviceInfoData.ClassGuid, &guid);
SetupDiClassNameFromGuid(&DeviceInfoData.ClassGuid, devName, 128, NULL);
if (!strcmp(devName,_T("Net")))
{
SetupDiGetDeviceInstanceId(hDevInfo, &DeviceInfoData, instanceId, 128, NULL);
if (!strncmp(instanceId, _T("PCI"), 3))
{
SP_PROPCHANGE_PARAMS params = { sizeof(SP_CLASSINSTALL_HEADER) };
params.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
params.Scope = DICS_FLAG_CONFIGSPECIFIC;
params.StateChange = DICS_DISABLE;
params.HwProfile = 0;
SetupDiSetClassInstallParams(hDevInfo, &DeviceInfoData, (SP_CLASSINSTALL_HEADER*)¶ms, sizeof(SP_PROPCHANGE_PARAMS));
SetupDiChangeState(hDevInfo, &DeviceInfoData);
}
}
CoTaskMemFree(guid);
}
SetupDiDestroyDeviceInfoList(hDevInfo);
return 0;
}
int EnableLANFunc(void)
{
HDEVINFO hDevInfo = INVALID_HANDLE_VALUE;
hDevInfo = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_PRESENT | DIGCF_ALLCLASSES);
if (INVALID_HANDLE_VALUE == hDevInfo)
return GetLastError();
SP_DEVINFO_DATA DeviceInfoData = { sizeof(SP_DEVINFO_DATA) };
LPOLESTR guid;
TCHAR devName[128];
TCHAR instanceId[128];
for (int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++)
{
StringFromCLSID(DeviceInfoData.ClassGuid, &guid);
SetupDiClassNameFromGuid(&DeviceInfoData.ClassGuid, devName, 128, NULL);
if (!strcmp(devName, _T("Net")))
{
SetupDiGetDeviceInstanceId(hDevInfo, &DeviceInfoData, instanceId, 128, NULL);
if (!strncmp(instanceId, _T("PCI"), 3))
{
SP_PROPCHANGE_PARAMS params = { sizeof(SP_CLASSINSTALL_HEADER) };
params.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
params.Scope = DICS_FLAG_CONFIGSPECIFIC;
params.StateChange = CDCS_ENABLED;
params.HwProfile = 0;
SetupDiSetClassInstallParams(hDevInfo, &DeviceInfoData, (SP_CLASSINSTALL_HEADER*)¶ms, sizeof(SP_PROPCHANGE_PARAMS));
SetupDiChangeState(hDevInfo, &DeviceInfoData);
}
}
CoTaskMemFree(guid);
}
SetupDiDestroyDeviceInfoList(hDevInfo);
return 0;
}
//------------------------------------------------------------------------------
BEGIN_MESSAGE_MAP(CMyNetCheckSystemDlg, CDialogEx)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_MESSAGE(WMMY_NETWORKER_MESSAGE, MYMSGNETWORKER)
ON_BN_CLICKED(IDC_BUTTON1, &CMyNetCheckSystemDlg::OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON5, &CMyNetCheckSystemDlg::OnBnClickedButton5)
ON_WM_TIMER()
END_MESSAGE_MAP()
// CMyNetCheckSystemDlg 消息处理程序
BOOL CMyNetCheckSystemDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
// 添加定时器
SetTimer(1, 1000, 0);
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CMyNetCheckSystemDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
HKEY hKey;
LPCSTR data_set = _T("SYSTEM\\CurrentControlSet\\Control\\Class\\{4d36e972-e325-11ce-bfc1-08002be10318}\\0001");
if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, data_set, 0, KEY_ALL_ACCESS, &hKey))
{
CString itemValue = _T("4"); // 定义字符串常量
DWORD size = itemValue.GetLength(); // 获取字符串长度
// 设置项值
if (ERROR_SUCCESS == RegSetValueEx(hKey, _T("*CNetworkCheckSystemDlg"), 0, REG_SZ, (unsigned char*)itemValue.GetBuffer(0), size))
{
DisableLANFunc();
EnableLANFunc();
}
else
{
exit(1);
}
}
else
{
exit(1);
}
RegCloseKey(hKey); //关闭键句柄
}
void CMyNetCheckSystemDlg::OnBnClickedButton5()
{
// TODO: 在此添加控件通知处理程序代码
HKEY hKEY;
LPCTSTR data_Set = _T("SYSTEM\\CurrentControlSet\\Control\\Class\\{4d36e972-e325-11ce-bfc1-08002be10318}\\0001");
if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, data_Set, 0, KEY_ALL_ACCESS, &hKEY))
{
CString itemvalue = _T("6");//定义字符串变量
DWORD size = itemvalue.GetLength();//获取字符串长度
//设置项值
if (ERROR_SUCCESS == RegSetValueEx(hKEY, _T("*CNetworkMonitorDlg"), 0, REG_SZ, (unsigned char*)itemvalue.GetBuffer(0), size))
{
DisableLANFunc();
EnableLANFunc();
}
else
{
exit(1);
}
}
else
{
exit(1);
}
RegCloseKey(hKEY);//关闭键句柄
}
void CMyNetCheckSystemDlg::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
switch (nIDEvent)
{
case 1:
{
HANDLE m_hThread_speed = (HANDLE)_beginthreadex(NULL, 0, ThreadCheckSpeed, this, 0, NULL);
break;
}
default:
break;
}
CDialogEx::OnTimer(nIDEvent);
}