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

📄 routetracedlg.cpp

📁 RouteTrace_ICMP
💻 CPP
字号:
// RouteTraceDlg.cpp : implementation file
//

#include "stdafx.h"
#include "RouteTrace.h"
#include "RouteTraceDlg.h"
#include "afxmt.h"
#include "Ping.h"

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

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

/////////////////////////////////////////////////////////////////////////////
// CRouteTraceDlg dialog

CRouteTraceDlg::CRouteTraceDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CRouteTraceDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CRouteTraceDlg)
	m_iMaxHops = 20;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CRouteTraceDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CRouteTraceDlg)
	DDX_Control(pDX, IDC_LIST_TRACE, m_TraceList);
	DDX_Control(pDX, IDC_TRACE, m_TraceText);
	DDX_Control(pDX, IDC_COMBO_HOSTADDR, m_Comb_HostAddr);
	DDX_Text(pDX, IDC_MAXHOT, m_iMaxHops);
	DDV_MinMaxInt(pDX, m_iMaxHops, 1, 100);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CRouteTraceDlg, CDialog)
	//{{AFX_MSG_MAP(CRouteTraceDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_TRACE, OnTrace)
	ON_MESSAGE(WM_USER_RECEIVE_MSG, OnReceiveMsg)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRouteTraceDlg message handlers

BOOL CRouteTraceDlg::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
	RECT	rect;
	m_TraceList.InsertColumn(0,"");
	m_TraceList.GetWindowRect(&rect);
	m_TraceList.SetColumnWidth(0, rect.right - rect.left-20);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CRouteTraceDlg::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 CRouteTraceDlg::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 CRouteTraceDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}
//-------------------------------------------------------------
BOOL		bRouteFlag = FALSE;
CString		strRouteState;
int			g_iMaxHops = 30;

/********************************************************
*    在线在线程中实现Tracert
*********************************************************/
struct SubThreadInfo
{
	HWND			m_hwnd;
	CString			strHostAddr;
} sInfo;

UINT ThreadSendIcmp(LPVOID pParam)          
{
	SubThreadInfo* pInfo = (SubThreadInfo*)pParam;
	int		 iTTL = 1;
	int		 iState;
	int	     iRTT;
	CString  strHostIP;
	CString		strOutRouteName;

	strHostIP = pInfo->strHostAddr;

	CPing    SendICMP;
	for (iTTL = 1; iTTL<=g_iMaxHops; iTTL++)
	{
		if( !bRouteFlag )
		{
			break;
		}  
		// 发送ICMP报文
		iState = SendICMP.Ping(strHostIP, iTTL,&strOutRouteName,&iRTT);

		if ( iState == REPLY_ARRIVE )
		{
			strRouteState.Format("%d   %s, 响应时间%d ms,  -- 到达",iTTL,strOutRouteName,iRTT);
		}
		else if( iState == REPLY_ROUTESTATE )
		{
			strRouteState.Format("%d   %s, 响应时间%d ms,   >>",iTTL,strOutRouteName,iRTT);
		}
		else if( iState == REPLY_TIMEOUT )
		{
			strRouteState.Format("%d    -- 超时",iTTL);
		}
		else
		{
			strRouteState.Format("%d    -- 超时",iTTL);
		}

		::PostMessage(pInfo->m_hwnd,WM_USER_RECEIVE_MSG,iTTL,1);

		if ( iState == REPLY_ARRIVE )
		{
			break;
		}
		Sleep(1000);
	}
	return 0;
}

//------------------------- Trace 线程结束 ----------------------

void CRouteTraceDlg::OnTrace() 
{
	CString strTargetHostAddr;
	UpdateData(TRUE);
	CWnd * pWnd;
	pWnd = GetDlgItem(IDC_COMBO_HOSTADDR);
	pWnd->GetWindowText(strTargetHostAddr);
	g_iMaxHops = m_iMaxHops;

	if(strTargetHostAddr.IsEmpty()) 
	{
		MessageBox("请输入地址!");
		pWnd->SetFocus();
		return;
	}
	if (m_Comb_HostAddr.FindStringExact(-1, strTargetHostAddr) == CB_ERR)
	{
	   m_Comb_HostAddr.AddString(strTargetHostAddr);
	}

	CString   strButtonExc;
	m_TraceText.GetWindowText( strButtonExc ) ;
	if( strButtonExc == "跟   踪" )
	{
		bRouteFlag=TRUE;
		m_TraceText.SetWindowText( "停  止" );
		m_TraceList.DeleteAllItems();

		// 起动线程
		sInfo.m_hwnd = this->m_hWnd;
		sInfo.strHostAddr = strTargetHostAddr;
		AfxBeginThread((AFX_THREADPROC)ThreadSendIcmp,&sInfo);		
	}
	else
	{
		bRouteFlag=FALSE;
		m_TraceList.InsertItem(0,"中止");	
		m_TraceText.SetWindowText( "跟   踪" );
	}
}
/***********************************************************
 * 功能:接收线程返回的信息
 ************************************************************/

LRESULT  CRouteTraceDlg::OnReceiveMsg(WPARAM wParam, LPARAM lParam)
{
	int		iTTL = (int)wParam;
	int		iState=(int)lParam;
	CString strThreadMsg;
	
	//显示结果到列表
	int iSub = m_TraceList.InsertItem(0,strRouteState);	 
	return 0;
}

⌨️ 快捷键说明

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