2 wang19940408 wang19940408 于 2017.06.15 11:02 提问

毕业设计 求大神指导代码 基于VC的网络通讯调试助手工具实现

// 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;

}

4个回答

what951006
what951006   2017.06.15 14:07
已采纳

他们都不回答你,分给我吧,我应该可以帮到你~ (-.-)

what951006
what951006 回复wang19940408: 哈哈,你是哪里不懂呢?看你代码表达好像是获取网络设备
2 个月之前 回复
wang19940408
wang19940408 怎么帮助我呀 分多少都有
2 个月之前 回复
shen_wei
shen_wei   Ds   Rxr 2017.06.15 16:32

关键是遇到了什么问题??、贴代码也不知道方法要实现什么。。。。。

wang19940408
wang19940408 帮我解读一下代码就好。。答辩用的
2 个月之前 回复
shen_wei
shen_wei   Ds   Rxr 2017.06.15 16:32
shen_wei
shen_wei   Ds   Rxr 2017.06.16 14:30
 BOOL CAdapaterSelection::OnInitDialog() 
{
    CDialog::OnInitDialog();
    // TODO: Add extra initialization here  

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

    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); //添加设备IP地址
            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)  //添加广播地址IP地址
        {
            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) //添加子网掩码IP地址
        {
            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) //添加目地地址IP地址
        {
            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[]) //解析IP地址
{
    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;
}
Csdn user default icon
上传中...
上传图片
插入图片