📄 getsetdlg.cpp
字号:
// getsetDlg.cpp : implementation file
//
#include "stdafx.h"
#include "getset.h"
#include "getsetDlg.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;
PFIXED_INFO finfo=NULL;
unsigned long len1,len2=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()
/////////////////////////////////////////////////////////////////////////////
// CGetsetDlg dialog
CGetsetDlg::CGetsetDlg(CWnd* pParent /*=NULL*/)
: CDialog(CGetsetDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CGetsetDlg)
m_sHostName = _T("");
m_type = _T("");
m_sDsMask = _T("");
m_cardname = _T("");
m_sDns = _T("");
m_pMacAddr = _T("");
m_sIPAddress = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CGetsetDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CGetsetDlg)
DDX_Text(pDX, IDC_EDIT1, m_sHostName);
DDX_Text(pDX, IDC_EDIT2, m_type);
DDX_Text(pDX, IDC_EDIT4, m_sDsMask);
DDX_Text(pDX, IDC_EDIT3, m_cardname);
DDX_Text(pDX, IDC_EDIT5, m_sDns);
DDX_Text(pDX, IDC_EDIT6, m_pMacAddr);
DDX_Text(pDX, IDC_EDIT7, m_sIPAddress);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CGetsetDlg, CDialog)
//{{AFX_MSG_MAP(CGetsetDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CGetsetDlg message handlers
BOOL CGetsetDlg::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 CGetsetDlg::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 CGetsetDlg::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 CGetsetDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CGetsetDlg::OnButton1()
{
// TODO: Add your control notification handler code here
int nRetCode;
nRetCode = StartUp();
nRetCode = GetLocalHostName(m_sHostName);
nRetCode = GetIPAddress(m_sHostName,m_sIPAddress);
GetInfo();
ParseData();
UpdateData(FALSE);
}
int CGetsetDlg::StartUp()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(2,0);
err=WSAStartup(wVersionRequested,&wsaData);
if(err!=0){
//不能找到WinSock DLL.
return err;
}
if(LOBYTE(wsaData.wVersion)!=2||HIBYTE(wsaData.wVersion)!=0)
{
//终止Ws2_32.dll.的使用
WSACleanup();
return WSAVERNOTSUPPORTED;
}
return 0;
}
int CGetsetDlg::GetLocalHostName(CString &sHostName)
{
char szHostName[256];
int nRetCode;
nRetCode = gethostname(szHostName,sizeof(szHostName));
if(nRetCode!=0){
//错误产生
sHostName =_T("Not available");;
return WSAGetLastError();
}
sHostName=szHostName;
return 0;
}
int CGetsetDlg::GetIPAddress(const CString &sHostName, CString &sIPAddress)
{
struct hostent FAR *lpHostEnt = gethostbyname(sHostName);
if(lpHostEnt == NULL){
//错误产生
sIPAddress = _T("");
return WSAGetLastError();
}
//或得IP地址
LPSTR lpAddr = lpHostEnt->h_addr_list[0];
if(lpAddr){
struct in_addr inAddr;
memmove (&inAddr,lpAddr,4);
//转化成标准IP地址形式
sIPAddress = inet_ntoa(inAddr);
if(sIPAddress.IsEmpty())
sIPAddress =_T("Not available");
}
return 0;
}
void CGetsetDlg::GetInfo()
{
pinfo=NULL;
unsigned long nError1;
unsigned long nError2;
nError1 = GetAdaptersInfo(pinfo,&len1);
nError2 = GetNetworkParams(finfo,&len2);
/*由于我们事先不知道本地机器上有多少张网卡,所以也就没法知道应该分配多大的缓存。
好在GetAdaptersInfo函数在缓冲的大小不够时会在第二个参数也就是ulSize中填入应该分配的缓冲的大小。*/
if (nError1==ERROR_NO_DATA)
{
AfxMessageBox("No adapter information exists for the local computer");
}
if (nError1==ERROR_NOT_SUPPORTED)
{
AfxMessageBox("GetAdaptersInfo is not supported by the operating system running on the local computer");
}
if (nError1==ERROR_BUFFER_OVERFLOW)
{
pinfo= (PIP_ADAPTER_INFO)malloc(len1);
nError1 = GetAdaptersInfo(pinfo,&len1);
}
if (nError1==0)
{ if (nError2==0)
{
ParseData();
}
if (nError2==ERROR_NO_DATA)
{
AfxMessageBox("No Network Paramrters exist for the local computer");
}
if (nError2==ERROR_NOT_SUPPORTED)
{
AfxMessageBox("GetNetworkParams is not supported by the operating system running on the local computer");
}
if (nError2==ERROR_BUFFER_OVERFLOW)
{
finfo= (PFIXED_INFO)malloc(len2);
nError2 = GetNetworkParams(finfo,&len2);
if (nError2==0)
{
ParseData();
}
}
}
return;
}
int CGetsetDlg::ParseData()
{
CString m_m_type,name;
if(pinfo!=NULL)
{
m_pMacAddr.Format("%02X:%02X:%02X:%02X:%02X:%02X",pinfo->Address[0],pinfo->Address[1],pinfo->Address[2],pinfo->Address[3],pinfo->Address[4],pinfo->Address[5]);
m_sDsMask.Format("%s",pinfo->IpAddressList.IpMask.String);
m_cardname=pinfo->Description;
m_m_type.Format("%d",pinfo->Type);
if(m_m_type=="6")
{
m_type="以太网适配器";
}
else
{
if(m_m_type=="9")
{
m_type="令牌环适配器";
}
else
{
if(m_m_type=="15")
{
m_type="光纤接口适配器";
}
else
{
if(m_m_type=="23")
{
m_type="点到点协议适配器";
}
else
{
if(m_m_type=="24")
{
m_type="回环(Loopback)适配器";
}
else
{
if(m_m_type=="28")
{
m_type="串行适配器(Serial Line Interface Protocol)";
}
else
m_type="其他类型的适配器";
}
}
}
}
}
PIP_ADDR_STRING DnsServerList = &(finfo->DnsServerList);
m_sDns = DnsServerList->IpAddress.String;
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -