📄 ethernetadapterdlg.cpp
字号:
// EthernetAdapterDlg.cpp : implementation file
//
#include "stdafx.h"
#include "EthernetAdapter.h"
#include "EthernetAdapterDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CEthernetAdapterDlg dialog
CEthernetAdapterDlg::CEthernetAdapterDlg(CWnd* pParent /*=NULL*/)
: CDialog(CEthernetAdapterDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CEthernetAdapterDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CEthernetAdapterDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CEthernetAdapterDlg)
DDX_Control(pDX, IDC_COMBO_NETADPTER, m_combNetAdapter);
DDX_Control(pDX, IDC_STATIC_MAC, m_staMAC);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CEthernetAdapterDlg, CDialog)
//{{AFX_MSG_MAP(CEthernetAdapterDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_CBN_SELCHANGE(IDC_COMBO_NETADPTER, OnSelChangeNetAdapter)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEthernetAdapterDlg message handlers
BOOL CEthernetAdapterDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
NCB ncb;
UCHAR uRetCode;
LANA_ENUM lana_enum;
CString strInfo;
memset( &ncb, 0, sizeof(ncb) );
// 向网卡发送NCBENUM命令,以获取当前机器的网卡信息,如有多少个网卡、每张网卡的编号等
ncb.ncb_command = NCBENUM;
ncb.ncb_buffer = (unsigned char *) &lana_enum;
ncb.ncb_length = sizeof(lana_enum);
uRetCode = Netbios( &ncb );
// 初始化控件
for(int k=0;k<lana_enum.length;k++)
{
strInfo.Format("%d",lana_enum.lana[k]);//k+1);
m_combNetAdapter.InsertString(k,strInfo);
m_combNetAdapter.SetItemData(k,lana_enum.lana[k]);
}
m_combNetAdapter.SetCurSel(0);
// 对每一张网卡,以其网卡编号为输入编号,获取其MAC地址
for ( int i=0; i< lana_enum.length; ++i)
{
GetNetAdapterInfo(lana_enum.lana[i]);
if(i==0)
m_staMAC.SetWindowText(m_strMAC);
}
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CEthernetAdapterDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CEthernetAdapterDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
// 得到指定编号网卡的MAC地址
void CEthernetAdapterDlg::GetNetAdapterInfo(int lana_num)
{
NCB ncb;
UCHAR uRetCode;
// 首先对选定的网卡发送一个NCBRESET命令,以便进行初始化
memset( &ncb, 0, sizeof(ncb) );
ncb.ncb_command = NCBRESET;
ncb.ncb_lana_num = lana_num; // 指定网卡编号
uRetCode = Netbios( &ncb );
// 接着,可以发送NCBASTAT命令以获取网卡的信息
memset( &ncb, 0, sizeof(ncb) );
ncb.ncb_command = NCBASTAT;
ncb.ncb_lana_num = lana_num; // 指定网卡号
strcpy( (char *)ncb.ncb_callname, "* " );
ncb.ncb_buffer = (unsigned char *) &m_NetAdapterInfo;
// 指定返回的信息存放的变量
ncb.ncb_length = sizeof(m_NetAdapterInfo);
uRetCode = Netbios( &ncb );
if ( uRetCode == 0 )
{
// 把网卡MAC地址格式化成常用的16进制形式,如0010-A4E4-5802
m_strMAC.Format("%02X-%02X-%02X-%02X-%02X-%02X",
m_NetAdapterInfo.adapt.adapter_address[0],
m_NetAdapterInfo.adapt.adapter_address[1],
m_NetAdapterInfo.adapt.adapter_address[2],
m_NetAdapterInfo.adapt.adapter_address[3],
m_NetAdapterInfo.adapt.adapter_address[4],
m_NetAdapterInfo.adapt.adapter_address[5] );
}
}
void CEthernetAdapterDlg::OnSelChangeNetAdapter()
{
int nCurNetAdapter=m_combNetAdapter.GetItemData(m_combNetAdapter.GetCurSel());
GetNetAdapterInfo( nCurNetAdapter);
m_staMAC.SetWindowText(m_strMAC);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -