📄 scanlancomputerdlg.cpp
字号:
// ScanLanComputerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "ScanLanComputer.h"
#include "ScanLanComputerDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define DESTPORT 137 //ScanLanComputer name port
#define MYPORT 4321
/////////////////////////////////////////////////////////////////////////////
// CScanLanComputerDlg dialog
CScanLanComputerDlg* pDlg;
BYTE bs[50] = {0x0,0x00,0x0,0x10,0x0,0x1,0x0,0x0,0x0,0x0,0x0,0x0,0x20,0x43,0x4b,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x0,0x0,0x21,0x0,0x1};
unsigned char B1[4],B2[4];
HANDLE wait_handle;
CString strOldIP = "";
bool bExit = false;
int i;
//------------------------------------------------------------------
CScanLanComputerDlg::CScanLanComputerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CScanLanComputerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CScanLanComputerDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
pDlg = this;
if ( !m_UDPSocket.Create(MYPORT,SOCK_DGRAM) )
{
AfxMessageBox(_T("Failed to create socket!"));
}
}
void CScanLanComputerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CScanLanComputerDlg)
DDX_Control(pDX, IDC_LIST_IP_ADDRESS, m_ListBoxIP);
DDX_Control(pDX, IDC_IPADDRESS2, m_IPEdit2);
DDX_Control(pDX, IDC_LIST_COMPUTER_INFO, m_ListCtrlComputerInfo);
DDX_Control(pDX, IDC_IPADDRESS1, m_IPEdit1);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CScanLanComputerDlg, CDialog)
//{{AFX_MSG_MAP(CScanLanComputerDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BTN_SEND, OnBtnStart)
ON_NOTIFY(IPN_FIELDCHANGED, IDC_IPADDRESS1, OnFieldchangedIpaddress1)
ON_BN_CLICKED(IDC_BTN_EXIT, OnBtnExit)
ON_BN_CLICKED(IDC_BTN_SAVE, OnBtnSave)
ON_NOTIFY(NM_DBLCLK, IDC_LIST3, OnDblclkListView)
ON_BN_CLICKED(IDC_BUTTON_CANCEL, OnButtonCancel)
ON_BN_CLICKED(IDC_BUTTON_CLEAR, OnButtonClear)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CScanLanComputerDlg message handlers
BOOL CScanLanComputerDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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
// Set IP Address segment.
m_IPEdit1.SetAddress( 172, 25, 68, 1 );
m_IPEdit2.SetAddress( 172, 25, 68, 255 );
// Create event.
wait_handle = CreateEvent( NULL,true,false,_T("receive data") );
// Disable exit thread button.
GetDlgItem( IDC_BTN_EXIT )->EnableWindow( FALSE );
// Initialize list control.
DWORD dwStyle = GetWindowLong( m_ListCtrlComputerInfo.GetSafeHwnd(), GWL_STYLE );
dwStyle &= ~LVS_TYPEMASK;
dwStyle |= LVS_REPORT;
SetWindowLong( m_ListCtrlComputerInfo.GetSafeHwnd(), GWL_STYLE, dwStyle );
m_ListCtrlComputerInfo.InsertColumn( 0, _T("MAC地址"), LVCFMT_LEFT, 150 );
m_ListCtrlComputerInfo.InsertColumn( 0, _T("用户"), LVCFMT_LEFT, 160 );
m_ListCtrlComputerInfo.InsertColumn( 0, _T("主机"), LVCFMT_LEFT, 120 );
m_ListCtrlComputerInfo.InsertColumn( 0, _T("工作组"), LVCFMT_LEFT, 120 );
m_ListCtrlComputerInfo.InsertColumn( 0, _T("IP地址"), LVCFMT_LEFT, 100 );
m_ListCtrlComputerInfo.SetExtendedStyle( LVS_EX_GRIDLINES );
::SendMessage( m_ListCtrlComputerInfo.m_hWnd, LVM_SETEXTENDEDLISTVIEWSTYLE,
LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT );
return TRUE; // return TRUE unless you set the focus to a control
}
// 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 CScanLanComputerDlg::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 CScanLanComputerDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
// ScanLanComputer线程
UINT ScanLanComputerThread(LPVOID param)
{
do
{
if ( bExit )
{
AfxMessageBox(_T("暂时停止查找网络邻居!"));
pDlg->GetDlgItem( IDC_BTN_SEND )->EnableWindow( TRUE );
pDlg->GetDlgItem( IDC_IPADDRESS1 )->EnableWindow( TRUE );
pDlg->GetDlgItem( IDC_IPADDRESS2 )->EnableWindow( TRUE );
pDlg->GetDlgItem( IDC_BTN_EXIT )->EnableWindow( FALSE );
bExit = false;
return 1;
}
pDlg->m_strIP.Format( "%d.%d.%d.%d", B1[0], B1[1], B1[2], B1[3] );
pDlg->m_ListBoxIP.InsertString( 0, pDlg->m_strIP );
if ( B1[3] != 0 && B1[2] != 0 )
pDlg->m_UDPSocket.SendTo( (void*)bs, 50, DESTPORT, pDlg->m_strIP, 0 );
if ( B1[2] <= B2[2] )
{
if( B1[3] < B2[3] ) B1[3]++;
else if( B1[2] < B2[2] && B1[3] < 255 )B1[3]++;
else if( B1[2] < B2[2] && B1[3] == 255 )
{
B1[3] = 0;
B1[2]++;
}
}
else break;
if( B1[3] >= B2[3] && B1[2] >= B2[2] ) break;
} while( B1[2] <= 255 && B1[3] <= 255 );
pDlg->m_ListBoxIP.InsertString( 0,_T("-----查找网络邻居结束!-----"));
pDlg->GetDlgItem( IDC_BTN_SEND )->EnableWindow( TRUE );
pDlg->GetDlgItem( IDC_IPADDRESS1 )->EnableWindow( TRUE );
pDlg->GetDlgItem( IDC_IPADDRESS2 )->EnableWindow( TRUE );
pDlg->GetDlgItem( IDC_BTN_EXIT )->EnableWindow( FALSE );
pDlg->GetDlgItem( IDC_BUTTON_CLEAR )->EnableWindow( TRUE );
return 0;
}
void CScanLanComputerDlg::OnBtnStart()
{
GetDlgItem( IDC_BUTTON_CLEAR )->EnableWindow( FALSE );
m_IPEdit1.GetAddress( B1[0], B1[1], B1[2], B1[3] );
m_IPEdit2.GetAddress( B2[0], B2[1], B2[2], B2[3] );
// Check data.
if( B2[2] < B1[2] )
{
AfxMessageBox( _T("终止地址应大于起始地址") );
return;
}
else if( B2[2] == B1[2] && B2[3] < B1[3] )
{
AfxMessageBox( _T("终止地址应大于起始地址") );
return;
}
else
{
// Do nothing.
}
if( B2[0] != B1[0] || B2[1] != B1[1] )
{
AfxMessageBox( _T("不支持A类或B类网") );
return;
}
GetDlgItem( IDC_BTN_SEND )->EnableWindow( false );
GetDlgItem( IDC_IPADDRESS1 )->EnableWindow( false );
GetDlgItem( IDC_IPADDRESS2 )->EnableWindow( false );
GetDlgItem( IDC_BTN_EXIT )->EnableWindow( true );
// 启动线程
AfxBeginThread( ScanLanComputerThread, this->GetSafeHwnd(), THREAD_PRIORITY_NORMAL );
}
void CScanLanComputerDlg::OnReceive()
{
BYTE Buf[500];
CString str,strIP,strHost,strHex,strMac,Host,Group,User;
UINT dport;
m_UDPSocket.ReceiveFrom( Buf,500,strIP,dport,0 );
if( strIP == (char)NULL || strIP == strOldIP ) return;
strOldIP = strIP;
int index = m_ListCtrlComputerInfo.InsertItem( 0,strIP );
strHost = _T("");
strHex = _T("");
User = _T("?");
Host = _T("\\");
int tem = 0,num = 0;
bool bAdd = true;
for ( i = 57; i < 500; i++ )
{
if ( Buf[i] == 0xcc ) break;
if ( bAdd )
{
str.Format( _T("%c"),Buf[i] );
if( Buf[i] >= ' ' ) strHost += str;
str.Format( _T("%02X-"),Buf[i] );
strHex += str;
}
if ( (++tem)%18 == 0 )
{
bAdd = true;
strHost.TrimRight( (char)NULL );
if ( strHost == _T("") )
{
strMac.Delete( 17,strMac.GetLength() - 17 );
m_ListCtrlComputerInfo.SetItem( index,4,LVIF_TEXT,strMac, 0, 0, 0,0 );
break;
}
if ( num == 0 && strHost != _T("") )
{
m_ListCtrlComputerInfo.SetItem( index,2,LVIF_TEXT,strHost, 0, 0, 0,0 );
Host = strHost;
num++;
}
else
{
if( Host != strHost && num == 1 && strHost != _T("") )
{
m_ListCtrlComputerInfo.SetItem( index,1,LVIF_TEXT,strHost, 0, 0, 0,0 );
Group = strHost;
num++;
}
else
{
if( strHost != Host && strHost != Group && num == 2 && strHost != _T(""))
{
User = strHost;
if( User != _T("__MSBROWSE__") )
{
m_ListCtrlComputerInfo.SetItem( index,3,LVIF_TEXT,User, 0, 0, 0,0 );
num++;
}
}
}
}
strMac = strHex;
strHost = _T("");
strHex = _T("");
}
}
SetEvent( wait_handle );
}
void CScanLanComputerDlg::OnFieldchangedIpaddress1( NMHDR* pNMHDR, LRESULT* pResult )
{
unsigned char AddrB[4];
m_IPEdit1.GetAddress( AddrB[0], AddrB[1], AddrB[2], AddrB[3] );
m_IPEdit2.SetAddress( AddrB[0], AddrB[1], AddrB[2], 255 );
*pResult = 0;
}
void CScanLanComputerDlg::OnBtnExit()
{
GetDlgItem( IDC_BUTTON_CLEAR )->EnableWindow( TRUE );
bExit = true;
}
CString MakeLen( CString csDst,int nLength )
{
if( csDst.GetLength() < nLength )
{
for( int i = csDst.GetLength(); i < nLength; i++ )
{
csDst += ' ';
}
}
else
{
csDst += ' ';
}
return csDst;
}
void CScanLanComputerDlg::OnBtnSave()
{
int nCount = m_ListCtrlComputerInfo.GetItemCount();
CFile f;
CFileException e;
CString csFileName = _T("");
if ( 0 == m_ListCtrlComputerInfo.GetItemCount())
{
AfxMessageBox( _T("当前列表中没有内容,保存没有意义!"));
return;
}
CTime t = CTime::GetCurrentTime();
CString csTime = t.Format( _T("%Y年%m月%d日-%H时%M分%S秒"));
csTime = _T("网络邻居_") + csTime;
CFileDialog FileDlg( FALSE,_T("txt"),csTime,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,_T("txt"),NULL );
if ( FileDlg.DoModal() == IDOK )
{
UpdateData( true );
csFileName = FileDlg.GetFileName();
UpdateData( false );
if ( f.Open( csFileName, CFile::modeCreate | CFile::modeWrite, &e) )
{
CString csTitle = _T("");
csTitle = _T("IP地址 工作组 主机 用户 MAC地址\r\n");
csTitle += _T("-----------------------------------------------------------------------------------\r\n");
f.Write( csTitle, csTitle.GetLength() );
for ( int i = 0; i < nCount; i++ )
{
csTitle = MakeLen( m_ListCtrlComputerInfo.GetItemText(i,0),17 )
+MakeLen( m_ListCtrlComputerInfo.GetItemText(i,1),14 )
+MakeLen( m_ListCtrlComputerInfo.GetItemText(i,2),14 )
+MakeLen( m_ListCtrlComputerInfo.GetItemText(i,3),14 )
+MakeLen( m_ListCtrlComputerInfo.GetItemText(i,4),17 );
csTitle += "\r\n\r\n";
f.Write( csTitle, csTitle.GetLength() );
}
f.Close();
}
}
}
void CScanLanComputerDlg::OnDblclkListView( NMHDR* pNMHDR, LRESULT* pResult )
{
if ( 0 == m_ListCtrlComputerInfo.GetItemCount() )
{
AfxMessageBox(_T("没有主机显示!"));
return;
}
POSITION pos = m_ListCtrlComputerInfo.GetFirstSelectedItemPosition();
if ( NULL == pos )
{
AfxMessageBox(_T("请选择要打开的主机!"));
return;
}
int nSelIndex = m_ListCtrlComputerInfo.GetNextSelectedItem( pos );
CString csHostName = _T("");
csHostName = m_ListCtrlComputerInfo.GetItemText( nSelIndex, 2 );
csHostName = "\\\\" + csHostName;
char* host = &char( *csHostName );
::ShellExecute( this->GetSafeHwnd(), _T("open"), host, NULL, _T("c:\\"), SW_SHOW );
*pResult = 0;
}
void CScanLanComputerDlg::OnButtonClear()
{
if ( (0 == m_ListBoxIP.GetCount()) && (0 == m_ListBoxIP.GetCount()) )
{
AfxMessageBox( _T("所有列表都已经为空,清除没有意义!"));
return;
}
// 清空 listbox.
m_ListBoxIP.ResetContent();
// 清空 listctrl.
m_ListCtrlComputerInfo.DeleteAllItems();
}
void CScanLanComputerDlg::OnButtonCancel()
{
CDialog::OnCancel();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -