⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testdlg.cpp

📁 获取网卡信息
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// TestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Test.h"
#include "TestDlg.h"
#include <afxtempl.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define WM_SOCKET_MSG WM_USER+101
SOCKET sock[2];
CList<CString,CString> IPList;

typedef DWORD(CALLBACK * PGNOINTERFACE)(PDWORD);//GetNumberOfInterfaces

typedef DWORD(CALLBACK * PGAINFO)(PIP_ADAPTER_INFO,PULONG);//GetAdaptersInfo

typedef  DWORD(CALLBACK * PGIINFO)(PIP_INTERFACE_INFO,PULONG );//GetInterfaceInfo

typedef  DWORD(CALLBACK * PGIAT)(PMIB_IPADDRTABLE,PULONG,BOOL);//GetIpAddrTable

typedef  DWORD(CALLBACK * PAIA)(IPAddr,IPMask,DWORD,PULONG,PULONG);//AddIPAddress
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

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()

/////////////////////////////////////////////////////////////////////////////
// CTestDlg dialog

CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTestDlg)
	m_nPortB = 8552;
	m_nPortA = 8551;
	m_sSendA = _T("");
	m_sSendB = _T("");
	m_sRecA = _T("");
	m_sRecB = _T("");
	m_sIPA = _T("");
	m_sIPB = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTestDlg)
	DDX_Text(pDX, IDC_EDT_PORT_B, m_nPortB);
	DDV_MinMaxUInt(pDX, m_nPortB, 1025, 10000);
	DDX_Text(pDX, IDC_EDT_PORT_A, m_nPortA);
	DDV_MinMaxUInt(pDX, m_nPortA, 1025, 10000);
	DDX_Text(pDX, IDC_EDT_SEND_A, m_sSendA);
	DDV_MaxChars(pDX, m_sSendA, 1024);
	DDX_Text(pDX, IDC_EDT_SEND_B, m_sSendB);
	DDV_MaxChars(pDX, m_sSendB, 1024);
	DDX_Text(pDX, IDC_EDT_REC_A, m_sRecA);
	DDX_Text(pDX, IDC_EDT_REC_B, m_sRecB);
	DDX_Text(pDX, IDC_EDT_IP_A, m_sIPA);
	DDX_Text(pDX, IDC_EDT_IP_B, m_sIPB);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
//{{AFX_MSG_MAP(CTestDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CLOSE()
ON_BN_CLICKED(IDC_BTN_SEND_A, OnBtnSendA)
ON_BN_CLICKED(IDC_BTN_SEND_B, OnBtnSendB)
ON_CBN_SELCHANGE(IDC_COM_IP_A, OnSelchangeComIpA)
ON_CBN_SELCHANGE(IDC_COM_IP_B, OnSelchangeComIpB)
ON_EN_CHANGE(IDC_EDT_PORT_A, OnChangeEdtPortA)
ON_EN_CHANGE(IDC_EDT_PORT_B, OnChangeEdtPortB)
//}}AFX_MSG_MAP
ON_MESSAGE(WM_SOCKET_MSG,OnSocketMsg)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestDlg message handlers

BOOL CTestDlg::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
	InitSocket();
	//GetAdapterIP();
	if(!GetAdapterLists())
	{
		MessageBox("取网卡信息失败");
		return false;
	}

	int nAdapterNum=((CComboBox*)GetDlgItem(IDC_COM_IP_A))->GetCount();
	if(nAdapterNum==0)
	{
		MessageBox("没有找到任何网卡!","提示");
		return false;
	}
	else if(nAdapterNum==1)
	{	
		MessageBox("只找到一块网卡!","提示");   
		((CComboBox*)GetDlgItem(IDC_COM_IP_A))->SetCurSel(0);
		((CComboBox*)GetDlgItem(IDC_COM_IP_B))->SetCurSel(0);
	}
	else
	{
		((CComboBox*)GetDlgItem(IDC_COM_IP_A))->SetCurSel(0);
		((CComboBox*)GetDlgItem(IDC_COM_IP_B))->SetCurSel(1);
	}
	OnSelchangeComIpA();
	OnSelchangeComIpB();
//	CreateSocket(0);
//	CreateSocket(1);
	((CComboBox*)GetDlgItem(IDC_COM_IP_A))->SetDroppedWidth(490);
	((CComboBox*)GetDlgItem(IDC_COM_IP_B))->SetDroppedWidth(490);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CTestDlg::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 CTestDlg::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 CTestDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

bool CTestDlg::InitSocket()
{
	//启动套接字
	WSADATA  wsa;
	if(WSAStartup(0x101,&wsa))
	{
		AfxMessageBox("套接字初始化失败!",NULL,MB_OK|MB_ICONSTOP);
		WSACleanup();
		return false;
	}
	/*	if(LOBYTE(wsa.wVersion )!=1||HIBYTE(wsa.wHighVersion) !=1)
	{
	AfxMessageBox("版本匹配错误!",NULL,MB_OK|MB_ICONSTOP);
	WSACleanup();
	return false;
	}
	*/	
	
	return true;
}

bool CTestDlg::GetAdapterIP()
{//取得网卡的IP
	CString strMSG;
	char name[256],bufIP[20];
	hostent *pHostent;
	sockaddr_in sin;
	memset(name,0,sizeof(name));
	memset(bufIP,0,sizeof(bufIP));
	int res=gethostname(name,sizeof(name));
	if (res != 0) 
	{
		strMSG.Format("取主机名字失败,错误码:%s", WSAGetLastError());
		MessageBox(strMSG);
		return false;
	}
	pHostent=gethostbyname(name);
	if (pHostent==NULL) 
	{
		strMSG.Format("取主机信息失败,错误码:%s", WSAGetLastError());
		MessageBox(strMSG);
		return false;
	}
	for (int nAdapter=0; pHostent->h_addr_list[nAdapter]; nAdapter++)
	{
		memcpy ( &sin.sin_addr.s_addr, pHostent->h_addr_list[nAdapter],pHostent->h_length);
		// 输出机器的IP地址.
		TRACE("\nAddress: %s\n", inet_ntoa(sin.sin_addr)); // 显示地址串
		sprintf(bufIP,"%s",inet_ntoa(sin.sin_addr));
		((CComboBox*)GetDlgItem(IDC_COM_IP_A))->AddString(bufIP);
		((CComboBox*)GetDlgItem(IDC_COM_IP_B))->AddString(bufIP);
	}
	m_nAdapterNum=nAdapter;
	if(nAdapter==0)
	{
		MessageBox("没有找到任何网卡!","提示");
	}
	else if(nAdapter==1)
	{
		
		MessageBox("只找到一块网卡!","提示");   
		((CComboBox*)GetDlgItem(IDC_COM_IP_A))->SetCurSel(0);
		((CComboBox*)GetDlgItem(IDC_COM_IP_B))->SetCurSel(0);
	}
	else
	{
		((CComboBox*)GetDlgItem(IDC_COM_IP_A))->SetCurSel(0);
		((CComboBox*)GetDlgItem(IDC_COM_IP_B))->SetCurSel(1);
	}
	return true;
}

bool CTestDlg::CreateSocket(int n)
{
	//创建套接字
	UpdateData(TRUE);
	CString strMsg;
	char buf[20];
	sock[n] = socket(AF_INET, SOCK_DGRAM, 0); 
	if(sock[n]==INVALID_SOCKET)
	{
		strMsg.Format ("创建套接字失败,错误码:%d",WSAGetLastError());
		MessageBox(strMsg);
		return false;
	}
	//绑定
	sockaddr_in sin;
	sin.sin_family =AF_INET;
	if(n==0)
	{
		sin.sin_port =htons(m_nPortA);
		/*GetDlgItem(IDC_EDT_IP_A)->GetWindowText(buf,20);*/
		strcpy(buf,m_sIPA.GetBuffer(20));
		if(strcmp(buf,"")==0)
		{
			MessageBox("错误的IP!","错误");
			closesocket(sock[0]);
			return false;
		}
		sin.sin_addr.s_addr =inet_addr(buf);
	   }
	else
	{
        sin.sin_port =htons(m_nPortB);
		//GetDlgItem(IDC_EDT_IP_B)->GetWindowText(buf,20);
		strcpy(buf,m_sIPB.GetBuffer(20));
		if(strcpy(buf,"")==0)
		{
			MessageBox("错误的IP!","错误");
			closesocket(sock[1]);
			return false;
		}
		sin.sin_addr.s_addr =inet_addr(buf);
	}
	//sin.sin_port =0;
	//	sin.sin_addr.s_addr =inet_addr("218.199.85.200");
	//	sin.sin_addr.s_addr =htonl(INADDR_ANY);
	//	sin.sin_addr.s_addr =0;
	if(bind(sock[n],(LPSOCKADDR)&sin,sizeof(sin))==SOCKET_ERROR)
	{
		CString strMsg;
		strMsg.Format ("建立绑定失败,错误码:%d",WSAGetLastError());

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -