SubNetDlg.cpp
资源名称:NetFinder.rar [点击查看]
上传用户:qiye66692
上传日期:2022-04-25
资源大小:72k
文件大小:3k
源码类别:
SNMP编程
开发平台:
Visual C++
- // SubNetDlg.cpp : 实现文件
- //
- #include "stdafx.h"
- #include "NetFinder.h"
- #include "SubNetDlg.h"
- #include ".subnetdlg.h"
- #include ".func.h"
- #include ".Msg.h"
- #include ".Host.h"
- // CSubNetDlg 对话框
- IMPLEMENT_DYNAMIC(CSubNetDlg, CDialog)
- CSubNetDlg::CSubNetDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CSubNetDlg::IDD, pParent)
- , m_subNet(_T("0.0.0.0"))
- , m_ip(_T(""))
- , m_mask(_T(""))
- , m_community(_T(""))
- {
- bInSearching = FALSE;
- m_pThread = NULL;
- }
- CSubNetDlg::~CSubNetDlg()
- {
- }
- BOOL CSubNetDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- m_StopEvent.ResetEvent();
- m_ExitEvent.ResetEvent();
- m_imageList.Create( 16, 16, ILC_COLOR4, 1, 0 );
- HICON hIcon = ::AfxGetApp()->LoadIcon(IDI_HOST);
- m_imageList.SetBkColor( COLORREF RGB(255,255,255) );
- m_imageList.Add( hIcon );
- m_list.SetImageList( &m_imageList, LVSIL_SMALL );
- m_list.InsertColumn( 0, "主机", LVCFMT_LEFT, 100 );
- m_list.InsertColumn( 1, "系统描述(如果支持SNMP协议)", LVCFMT_LEFT, 400 );
- m_progress.SetRange( 1, 254 );
- m_progress.SetStep( 1 );
- m_progress.SetPos( 0 );
- DWORD dNetMask = inet_addr(m_mask);
- DWORD dNetAddr = inet_addr(m_ip);
- dNetMask = ntohl(dNetMask);
- dNetAddr = ntohl(dNetAddr);
- DWORD dSubNet = dNetAddr & dNetMask;
- BYTE *byte = (BYTE *)&dSubNet;
- m_subNet.Format("%d.%d.%d.%d", //设置子网
- (int)byte[3], (int)byte[2], (int)byte[1], (int)byte[0]);
- UpdateData( FALSE );
- return TRUE; // set focus to first one
- }
- void CSubNetDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- DDX_Text(pDX, IDC_IP_STATIC, m_subNet);
- DDX_Control(pDX, IDC_PROGRESS, m_progress);
- DDX_Control(pDX, IDC_LIST, m_list);
- }
- BEGIN_MESSAGE_MAP(CSubNetDlg, CDialog)
- ON_BN_CLICKED(IDC_SEARCH_BUTTON, OnBnClickedSearchButton)
- ON_MESSAGE(MSG_FOUND_HOST, OnFoundHost)
- ON_WM_DESTROY()
- END_MESSAGE_MAP()
- // CSubNetDlg 消息处理程序
- void CSubNetDlg::OnBnClickedSearchButton()
- {
- // TODO: 在此添加控件通知处理程序代码
- if( bInSearching == FALSE )
- {
- m_list.DeleteAllItems();
- bInSearching = TRUE;
- m_progress.SetPos( 0 );
- GetDlgItem(IDC_SEARCH_BUTTON)->SetWindowText("停止");
- m_StopEvent.ResetEvent();
- m_pThread = AfxBeginThread( PingHost, this ); //新开线程,开始ping主机
- }
- else
- {
- bInSearching = FALSE;
- if(m_pThread != NULL) {
- ::TerminateThread(m_pThread->m_hThread, 0);
- m_pThread = NULL;
- MessageBox(_T("搜索线程被终止!"));
- }
- m_progress.SetPos(0);
- GetDlgItem(IDC_SEARCH_BUTTON)->SetWindowText("开始搜索");
- m_StopEvent.SetEvent();
- }
- }
- LRESULT CSubNetDlg::OnFoundHost(WPARAM wParam, LPARAM lParam)
- {
- int index = m_list.InsertItem( wParam, m_host.m_strHostIp );
- m_list.SetItemText(index, 1, m_host.m_strHostDescr);
- return 0L;
- }
- void CSubNetDlg::OnDestroy()
- {
- CDialog::OnDestroy();
- // TODO: 在此处添加消息处理程序代码
- if(this->m_pThread != NULL) {
- ::TerminateThread(m_pThread->m_hThread, 0);
- m_pThread = NULL;
- }
- //if( bInSearching == TRUE )
- //{
- // m_StopEvent.SetEvent();
- // WaitForSingleObject( m_ExitEvent.m_hObject, INFINITE );
- //}
- }