📄 b04021328_getipdlg.cpp
字号:
// B04021328_GetIPDlg.cpp : implementation file
//
#include "stdafx.h"
#include "B04021328_GetIP.h"
#include "B04021328_GetIPDlg.h"
#include <winsock.h>
#include <wsipx.h>
#include <wsnwlink.h>
#include "Iphlpapi.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
PIP_ADAPTER_INFO pinfo = NULL;
FIXED_INFO * FixedInfo;
unsigned long len=0;
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CB04021328_GetIPDlg dialog
CB04021328_GetIPDlg::CB04021328_GetIPDlg(CWnd* pParent /*=NULL*/)
: CDialog(CB04021328_GetIPDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CB04021328_GetIPDlg)
// NOTE: the ClassWizard will add member initialization here
m_sHostName = _T("");
m_sIPAddress = _T("");
m_sMAC = _T("");
m_sGateWay = _T("");
m_sSubnetMask = _T("");
m_sDNS = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CB04021328_GetIPDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CB04021328_GetIPDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
DDX_Text(pDX, IDC_HostName, m_sHostName);
DDX_Text(pDX, IDC_IPAddress, m_sIPAddress);
DDX_Text(pDX, IDC_GetMAC, m_sMAC);
DDX_Text(pDX, IDC_GateWay, m_sGateWay);
DDX_Text(pDX, IDC_SubnetMask, m_sSubnetMask);
DDX_Text(pDX, IDC_DNS, m_sDNS);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CB04021328_GetIPDlg, CDialog)
//{{AFX_MSG_MAP(CB04021328_GetIPDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDOK, OnGetIP)
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDCANCEL, &CB04021328_GetIPDlg::OnBnClickedCancel)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CB04021328_GetIPDlg message handlers
BOOL CB04021328_GetIPDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 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
return TRUE; // return TRUE unless you set the focus to a control
}
void CB04021328_GetIPDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 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 CB04021328_GetIPDlg::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 CB04021328_GetIPDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
int CB04021328_GetIPDlg::StartUp()
{
// Initialize windows sockets API. Ask for version 1.1
int err;
WORD wVersionRequested = MAKEWORD(1, 1);
WSADATA wsaData;
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
// Couldn't find a usable WinSock DLL
return err;
}
return 0;
}
int CB04021328_GetIPDlg::CleanUp()
{
// Terminate windows sockets API
int nRetCode;
nRetCode = WSACleanup();
if (nRetCode != 0) {
// An error occured.
return WSAGetLastError();
}
return 0;
}
int CB04021328_GetIPDlg::GetHostName(CString& sHostName)
{
char szHostName[256];
int nRetCode;
nRetCode = gethostname(szHostName, sizeof(szHostName));
if (nRetCode != 0) {
// An error has occurred
sHostName = _T("Not available");;
return WSAGetLastError();
}
sHostName = szHostName;
return 0;
}
int CB04021328_GetIPDlg::GetIPAddress(const CString& sHostName, CString& sIPAddress)
{
// hostent& he = *pHostent;
// sockaddr_in sa;
// for (int nAdapter=0; he.h_addr_list[nAdapter]; nAdapter++) {
// memcpy ( &sa.sin_addr.s_addr, he.h_addr_list[nAdapter],he.h_length);
// // Output the machines IP Address.
// }
struct hostent FAR *lpHostEnt = gethostbyname (sHostName);
if (lpHostEnt == NULL) {
// An error occurred.
sIPAddress = _T("");
return WSAGetLastError();
}
LPSTR lpAddr = lpHostEnt->h_addr_list[0];
if (lpAddr) {
struct in_addr inAddr;
memmove (&inAddr, lpAddr, 4);
sIPAddress = inet_ntoa (inAddr);
if (sIPAddress.IsEmpty())
sIPAddress = _T("Not available");
}
return 0;
}
void CB04021328_GetIPDlg::GetOneMac(int AdapterIndex, CString& m_sMAC)
{
NCB ncb;
int uRetCode;
ASTAT Adapter;
memset( &ncb, 0, sizeof(ncb) );
ncb.ncb_command = NCBRESET;
ncb.ncb_lana_num = AdapterIndex; // 指定网卡号
//首先对选定的网卡发送一个NCBRESET命令,以便进行初始化
uRetCode = Netbios( &ncb );
memset( &ncb, 0, sizeof(ncb) );
ncb.ncb_command = NCBASTAT;
ncb.ncb_lana_num = AdapterIndex; // 指定网卡号
strcpy( (char *)ncb.ncb_callname,"*" );
// 指定返回的信息存放的变量
ncb.ncb_buffer = (unsigned char *) &Adapter;
ncb.ncb_length = sizeof(Adapter);
// 发送NCBASTAT命令以获取网卡的信息
uRetCode = Netbios( &ncb );
if ( uRetCode == 0 )
{
// 把网卡MAC地址格式化成常用的16进制形式,如0010-A4E4-5802
CString strMacAddr;
strMacAddr.Format( "%02X-%02X-%02X-%02X-%02X-%02X\n",
Adapter.adapt.adapter_address[0],
Adapter.adapt.adapter_address[1],
Adapter.adapt.adapter_address[2],
Adapter.adapt.adapter_address[3],
Adapter.adapt.adapter_address[4],
Adapter.adapt.adapter_address[5] );
m_sMAC = strMacAddr;
/* //将网卡地址和序号存入数组中
ADPTSTRCT AdptSt;
AdptSt.nIndex = AdapterIndex;
AdptSt.strMac = strMacAddr;
m_arrAdapters.Add(AdptSt);
*/
}
}
void CB04021328_GetIPDlg::GetGateWay_SubNet()
{
if (pinfo!=NULL)
delete (pinfo);
unsigned long nError;
pinfo= (PIP_ADAPTER_INFO)malloc(len);
nError = GetAdaptersInfo(pinfo,&len);
if (nError==0)
{
if (pinfo!=NULL)
{
PIP_ADDR_STRING pAddressList = &(pinfo->IpAddressList);
do
{
pAddressList = pAddressList->Next;
} while (pAddressList != NULL);
m_sSubnetMask.Format("%s",pinfo->IpAddressList.IpMask.String);
m_sGateWay.Format("%s",pinfo->GatewayList.IpAddress.String);
}
}
/*pinfo = (PIP_ADAPTER_INFO *) GlobalAlloc( GPTR, sizeof( PIP_ADAPTER_INFO ) );
len = sizeof( PIP_ADAPTER_INFO );
if( ERROR_BUFFER_OVERFLOW == GetAdaptersInfo(pinfo,&len ) )
{
GlobalFree( pinfo );
pinfo = (PIP_ADAPTER_INFO *) GlobalAlloc( GPTR, len );
}
GetAdaptersInfo(pinfo,&len);
*/
}
void CB04021328_GetIPDlg::GetDNS()
{
// IP_ADDR_STRING * pIPAddr;
FixedInfo = (FIXED_INFO *) GlobalAlloc( GPTR, sizeof( FIXED_INFO ) );
len = sizeof( FIXED_INFO );
if( ERROR_BUFFER_OVERFLOW == GetNetworkParams( FixedInfo, &len ) ) {
GlobalFree( FixedInfo );
FixedInfo = (FIXED_INFO *) GlobalAlloc( GPTR, len );
}
GetNetworkParams( FixedInfo, &len );
m_sDNS.Format("%s",FixedInfo -> DnsServerList.IpAddress.String);
// pIPAddr = FixedInfo -> DnsServerList.Next;
}
void CB04021328_GetIPDlg::OnGetIP()
{
// TODO: Add your control notification handler code here
int nRetCode;
nRetCode = StartUp();
TRACE1("StartUp RetCode: %d\n", nRetCode);
nRetCode = GetHostName(m_sHostName);
TRACE1("GetHostName RetCode: %d\n", nRetCode);
nRetCode = GetIPAddress(m_sHostName, m_sIPAddress);
TRACE1("GetIPAddress RetCode: %d\n", nRetCode);
nRetCode = CleanUp();
TRACE1("CleanUp RetCode: %d\n", nRetCode);
GetOneMac(0, m_sMAC);
GetGateWay_SubNet();
if(m_sGateWay == _T("")&&m_sSubnetMask == _T(""))
GetGateWay_SubNet();
GetDNS();
UpdateData(FALSE);
}
void CB04021328_GetIPDlg::OnBnClickedCancel()
{
// TODO: 在此添加控件通知处理程序代码
OnCancel();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -