2 weixin 39172883 weixin_39172883 于 2017.06.15 10:25 提问

各位大神 毕业答辩 代码不懂 希望有人指点 基于VC的网络通讯调试助手 100C

// AdapaterSelection.cpp : implementation file
//

#include "stdafx.h"
#include "CapturePacket.h"
#include "AdapaterSelection.h"

#ifdef DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE
_;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAdapaterSelection dialog

CAdapaterSelection::CAdapaterSelection(CWnd* pParent /*=NULL*/)
: CDialog(CAdapaterSelection::IDD, pParent)
{
//{{AFX_DATA_INIT(CAdapaterSelection)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}

void CAdapaterSelection::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAdapaterSelection)
DDX_Control(pDX, IDC_TREE_NETWORKADAPTERS, m_treeAdapater);

//}}AFX_DATA_MAP

}

BEGIN_MESSAGE_MAP(CAdapaterSelection, CDialog)
//{{AFX_MSG_MAP(CAdapaterSelection)
ON_NOTIFY(NM_DBLCLK, IDC_TREE_NETWORKADAPTERS, OnDblclkTreeNetworkadapters)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAdapaterSelection message handlers

BOOL CAdapaterSelection::OnInitDialog()
{
CDialog::OnInitDialog();

// TODO: Add extra initialization here  

//初始化树形选择
HTREEITEM root = m_treeAdapater.InsertItem(_T("网络适配器"));

if(pcap_findalldevs(&m_pAllDevs, m_strErrorBuf) == -1)
    return FALSE;

for(m_pDev = m_pAllDevs;m_pDev != NULL;m_pDev = m_pDev->next)
{
    char temp[400];
    CString strText;
    HTREEITEM hSubItem = m_treeAdapater.InsertItem(m_pDev->name,root);
    if(m_pDev->description != NULL)
    {
        strText.Format("网卡描述信息 : %s",m_pDev->description);
        m_treeAdapater.InsertItem(strText,hSubItem);
    }
    else
    {
        m_treeAdapater.InsertItem("网卡描述信息 : 空",hSubItem);
    }

    if(m_pDev->addresses != NULL && m_pDev->addresses->addr != NULL)
    {
        iptos(((struct sockaddr_in *)m_pDev->addresses->addr)->sin_addr.s_addr,temp);
        strText.Format("IP地址 : %s",temp);
        m_treeAdapater.InsertItem(strText,hSubItem);            
    }
    else
    {
        m_treeAdapater.InsertItem("IP地址 :  空",hSubItem);
    }

    if(m_pDev->addresses != NULL && m_pDev->addresses->broadaddr != NULL)
    {
        iptos(((struct sockaddr_in *)m_pDev->addresses->broadaddr)->sin_addr.s_addr,temp);      
        strText.Format("广播地址 : %s",temp);
        m_treeAdapater.InsertItem(strText,hSubItem);    

    }
    else
    {
        m_treeAdapater.InsertItem("广播地址 : 无",hSubItem);
    }

    if(m_pDev->addresses!=NULL && m_pDev->addresses->netmask!=NULL)
    {
        iptos(((struct sockaddr_in *)m_pDev->addresses->netmask)->sin_addr.s_addr,temp);
        strText.Format("子网掩码 : %s",temp);
        m_treeAdapater.InsertItem(strText,hSubItem);    
    }   
    else
    {
        m_treeAdapater.InsertItem("子网掩码 : 空",hSubItem);
    }

    if(m_pDev->addresses!=NULL && m_pDev->addresses->dstaddr!=NULL)
    {
        iptos(((struct sockaddr_in *)m_pDev->addresses->dstaddr)->sin_addr.s_addr,temp);            
        strText.Format("目地地址 : %s",temp);
        m_treeAdapater.InsertItem(strText,hSubItem);    
    }
    else
    {
        m_treeAdapater.InsertItem("目地地址 : 无",hSubItem);
    }       

}
m_pDev = NULL;
m_treeAdapater.Expand(root,TVE_EXPAND);
return TRUE;  // return TRUE unless you set the focus to a control
              // EXCEPTION: OCX Property Pages should return FALSE

}

void CAdapaterSelection::OnOK()
{
// TODO: Add extra validation here
if(m_pDev == NULL)
{
MessageBox("请双击选择网卡!");
return;
}
if(m_pDev->addresses == NULL)
{
MessageBox("不能选择虚拟网卡!");
return;
}
MessageBox("网卡绑定成功!");

CDialog::OnOK();

}

pcap_if_t* CAdapaterSelection::getDevice()
{
if(m_pDev == NULL)
return NULL;
return m_pDev;
}
void CAdapaterSelection::iptos(u_long in, char output[])
{
u_char *p;
p = (u_char *)∈
sprintf(output, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
}

void CAdapaterSelection::OnDblclkTreeNetworkadapters(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
HTREEITEM hSubItem = m_treeAdapater.GetSelectedItem();
if(m_treeAdapater.GetParentItem(hSubItem) == NULL)
return;

else if( m_treeAdapater.GetItemText(m_treeAdapater.GetParentItem(hSubItem)) != "网络适配器")
    hSubItem = m_treeAdapater.GetParentItem(hSubItem);

m_pDev=m_pAllDevs;

while(strcmp(m_pDev->name,m_treeAdapater.GetItemText(hSubItem)) != 0)
{   //找到被选中网卡
    m_pDev = m_pDev->next;
}
*pResult = 0;

}

1个回答

shen_wei
shen_wei   Ds   Rxr 2017.06.15 16:44

这段代码大意 就是树形结构显示本机网卡的数量。。

并根据选择的网卡进行绑定操作。。设置网卡的一些IP地址信息。。

Csdn user default icon
上传中...
上传图片
插入图片