📄 nbtstatdlg.cpp
字号:
// NBTSTATDlg.cpp : implementation file
//
#include "stdafx.h"
#include "NBTSTAT.h"
#include "NBTSTATDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define destPORT 137 //nbtstat name port
#define myPORT 4321
/////////////////////////////////////////////////////////////////////////////
// CNBTSTATDlg dialog
//--------------------------------var-------------------------------
CNBTSTATDlg* 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;
//------------------------------------------------------------------
CNBTSTATDlg::CNBTSTATDlg(CWnd* pParent /*=NULL*/)
: CDialog(CNBTSTATDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CNBTSTATDlg)
// 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("Failed Create Socket");
}
//---------------------------------------------
}
void CNBTSTATDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CNBTSTATDlg)
DDX_Control(pDX, IDC_SPIN1, m_spin);
DDX_Control(pDX, IDC_LIST1, m_ListBox);
DDX_Control(pDX, IDC_IPADDRESS2, m_IPEdit2);
DDX_Control(pDX, IDC_LIST3, m_ListView);
DDX_Control(pDX, IDC_IPADDRESS1, m_IPEdit1);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CNBTSTATDlg, CDialog)
//{{AFX_MSG_MAP(CNBTSTATDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BTN_SEND, OnBtnSend)
ON_NOTIFY(IPN_FIELDCHANGED, IDC_IPADDRESS1, OnFieldchangedIpaddress1)
ON_BN_CLICKED(IDC_BTN_EXIT, OnBtnExit)
ON_BN_CLICKED(IDC_BTN_SAVE, OnBtnSave)
ON_BN_CLICKED(IDC_BTN_ABOUT, OnBtnAbout)
ON_NOTIFY(NM_DBLCLK, IDC_LIST3, OnDblclkListView)
ON_BN_CLICKED(IDC_BTN_SEND2, OnBtnSend2)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CNBTSTATDlg message handlers
BOOL CNBTSTATDlg::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
// TODO: Add extra initialization here
CArray<CString,CString> list;
CJahNetCommon::GetLocalIpList(&list);
if ( list.GetSize() > 0 )
{
CString tmp = list.GetAt( 0 );
int pos = tmp.ReverseFind('.');
if ( pos > 0 )
tmp = tmp.Left(pos);
m_IPEdit1.SetWindowText(tmp+".1");
m_IPEdit2.SetWindowText(tmp+".255");
}
else
{
m_IPEdit1.SetAddress(192,168,1,1);
m_IPEdit2.SetAddress(192,168,1,255);
}
wait_handle=CreateEvent(NULL,true,false,"receive data");
GetDlgItem(IDC_BTN_EXIT)->EnableWindow(false);
m_spin.SetRange(100,10000);
m_spin.SetPos(100);
// -----------------ListView initialize---------------------
DWORD dwStyle=GetWindowLong(m_ListView.GetSafeHwnd(),GWL_STYLE);
dwStyle&=~LVS_TYPEMASK;
dwStyle|=LVS_REPORT;
SetWindowLong(m_ListView.GetSafeHwnd(),GWL_STYLE,dwStyle);
m_ListView.InsertColumn(0,"MAC地址",LVCFMT_LEFT,120);
m_ListView.InsertColumn(0,"用户\\其它",LVCFMT_LEFT,100);
m_ListView.InsertColumn(0,"主机",LVCFMT_LEFT,80);
m_ListView.InsertColumn(0,"工作组",LVCFMT_LEFT,80);
m_ListView.InsertColumn(0,"IP地址",LVCFMT_LEFT,100);
m_ListView.SetExtendedStyle(LVS_EX_GRIDLINES);
::SendMessage(m_ListView.m_hWnd, LVM_SETEXTENDEDLISTVIEWSTYLE,
LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT);
::SetWindowPos(m_hWnd,HWND_TOPMOST,0, 0, 0, 0,SWP_NOSIZE | SWP_NOMOVE );
return TRUE; // return TRUE unless you set the focus to a control
}
void CNBTSTATDlg::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 CNBTSTATDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
UINT NbtstatThread(LPVOID param)
{
//bool bFirst=true;
do
{
if(bExit)
{
pDlg->GetDlgItem(IDC_BTN_SEND)->EnableWindow(true);
pDlg->GetDlgItem(IDC_EDIT1)->EnableWindow(true);
pDlg->GetDlgItem(IDC_SPIN1)->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_ListBox.InsertString(0,pDlg->m_strIP);
if(B1[3]!=0&&B1[2]!=0)
pDlg->m_UDPSocket.SendTo((void*)bs,50,destPORT,pDlg->m_strIP,0);
int nWait=pDlg->m_spin.GetPos();
WaitForSingleObject(
wait_handle, // handle to object to wait for
nWait // time-out interval in milliseconds
);
ResetEvent(wait_handle);
if(B1[2]<=B2[2])
{
//if(B1[3]==255)AfxMessageBox("here0");
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)
{
//AfxMessageBox("here");
B1[3]=0;
B1[2]++;
}
}
else break;
if(B1[3]>=B2[3]&&B1[2]>=B2[2])break;
//if(!bFirst)break;
//if(B1[3]=B2[3])bFirst=false;
}while(B1[2]<=255&&B1[3]<=255);
pDlg->m_ListBox.InsertString(0,"-----complete!-----");
pDlg->GetDlgItem(IDC_BTN_SEND)->EnableWindow(true);
pDlg->GetDlgItem(IDC_EDIT1)->EnableWindow(true);
pDlg->GetDlgItem(IDC_SPIN1)->EnableWindow(true);
pDlg->GetDlgItem(IDC_IPADDRESS1)->EnableWindow(true);
pDlg->GetDlgItem(IDC_IPADDRESS2)->EnableWindow(true);
pDlg->GetDlgItem(IDC_BTN_EXIT)->EnableWindow(false);
return 0;
}
//-----------------------------------------------------------
void CNBTSTATDlg::OnBtnSend()
{
// TODO: Add your control notification handler code here
m_IPEdit1.GetAddress(B1[0],B1[1],B1[2],B1[3]);
m_IPEdit2.GetAddress(B2[0],B2[1],B2[2],B2[3]);
if(B2[2]<B1[2])
{
AfxMessageBox("终止地址应大于起始地址!");
return ;
}
else if(B2[2]==B1[2]&&B2[3]<B1[3])
{
AfxMessageBox("终止地址应大于起始地址!");
return ;
}
if(B2[0]!=B1[0]||B2[1]!=B1[1])
{
AfxMessageBox("不支持A类或B类网!");
return ;
}
GetDlgItem(IDC_BTN_SEND)->EnableWindow(false);
GetDlgItem(IDC_EDIT1)->EnableWindow(false);
GetDlgItem(IDC_SPIN1)->EnableWindow(false);
GetDlgItem(IDC_IPADDRESS1)->EnableWindow(false);
GetDlgItem(IDC_IPADDRESS2)->EnableWindow(false);
GetDlgItem(IDC_BTN_EXIT)->EnableWindow(true);
AfxBeginThread(NbtstatThread,this->GetSafeHwnd(),THREAD_PRIORITY_NORMAL);
}
void CNBTSTATDlg::OnReceive()
{
BYTE Buf[500]={0};
//m_UDPSocket.Receive(Buf,500,0);
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)
{
// AfxMessageBox(strIP);
return;
}
strOldIP=strIP;
int index=m_ListView.InsertItem(0,strIP);
strHost="";
strHex="";
User="?";
Host="\\";
int tem=0,num=0;
bool bAdd=true;
CFileComputer fComputer;
fComputer.Load();
int pos = fComputer.Find(inet_addr(strIP));
CFileComputer::ST_DATA data = { 0 };
if ( pos >= 0 )
data = fComputer.m_list.GetAt(pos);
//if(m_strIP==strIP)
for(i=57;i<500;i++) //57-72
{
if(Buf[i]==0xcc)
break;
if(Buf[i]==0x20)
bAdd=false;
if(bAdd)
{
str.Format("%c",Buf[i]);
if(Buf[i]>=' ')strHost+=str;
str.Format("%02x.",Buf[i]);
strHex+=str;
}
if((++tem)%18==0)
{
bAdd=true;
//m_ListBox.AddString(strHost);//
strHost.TrimRight((char)NULL);
if(strHost=="")
{
strMac.Delete(17,strMac.GetLength()-17);
m_ListView.SetItem(index,4,LVIF_TEXT,strMac, 0, 0, 0,0);
break;
}
if(num==0&&strHost!="")
{
m_ListView.SetItem(index,2,LVIF_TEXT,strHost, 0, 0, 0,0);
Host=strHost;
num++;
}
else
{
if(Host!=strHost&&num==1&&strHost!="")
{
if(data.m_group[0] == '\0')
{
m_ListView.SetItem(index,1,LVIF_TEXT,strHost, 0, 0, 0,0);
}
else
{
m_ListView.SetItem(index,1,LVIF_TEXT,data.m_group, 0, 0, 0,0);
Group=strHost;
//AfxMessageBox(strHost);
}
num++;
}
else
{
if(strHost!=Host&&strHost!=Group&&num==2&&strHost!="")
{
User=strHost;
if(User!="__MSBROWSE__")
{
if(data.m_user[0] == '\0')
{
m_ListView.SetItem(index,3,LVIF_TEXT,strHost, 0, 0, 0,0);
}
else
{
m_ListView.SetItem(index,3,LVIF_TEXT,data.m_user, 0, 0, 0,0);
// AfxMessageBox(Host);
}
num++;
}
//num++;
}
}
}
strMac=strHex;
strHost="";
strHex="";
}
}
SetEvent(wait_handle);
}
void CNBTSTATDlg::OnFieldchangedIpaddress1(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
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 CNBTSTATDlg::OnBtnExit()
{
// TODO: Add your control notification handler code here
bExit=true;
}
CString MakeLen(CString str,int len)
{
if(str.GetLength()<len)
{
for(int i=str.GetLength();i<len;i++)str+=' ';
}
else str+=' ';
return str;
}
void CNBTSTATDlg::OnBtnSave()
{
// TODO: Add your control notification handler code here
int Count=m_ListView.GetItemCount();
CFile f;
CFileException e;
CString strFileName;
CTime t=CTime::GetCurrentTime();
CString strTime = t.Format( "%d-%H-%M-%S" );
strTime="NBT_LOG-"+strTime;
//char* filename=(LPSTR)(LPCTSTR)strTime;
CFileDialog fileDlg(FALSE,"txt",strTime,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"txt",NULL); //TRUE 为打开对话框
if(fileDlg.DoModal()==IDOK)
{
UpdateData(true);
strFileName=fileDlg.GetFileName();
UpdateData(false);
if( f.Open(strFileName, CFile::modeCreate | CFile::modeWrite, &e ) )
{
CString str;
str="IP地址 工作组 主机 用户 MAC地址\r\n\r\n";
f.Write( str, str.GetLength() );
for(int i=0;i<Count;i++)
{
str=MakeLen(m_ListView.GetItemText(i,0),17)
+MakeLen(m_ListView.GetItemText(i,1),14)
+MakeLen(m_ListView.GetItemText(i,2),14)
+MakeLen(m_ListView.GetItemText(i,3),14)
+MakeLen(m_ListView.GetItemText(i,4),17);
str+="\r\n\r\n";
f.Write( str, str.GetLength() );
}
f.Close();
}
}
}
void CNBTSTATDlg::OnBtnAbout()
{
// TODO: Add your control notification handler code here
AfxMessageBox("---------查看内网机器信息--------\r\n\r\n waguan_008@163.com");
}
void CNBTSTATDlg::OnDblclkListView(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
*pResult = 0;
}
void CNBTSTATDlg::OnBtnSend2()
{
// TODO: Add your control notification handler code here
pDlg->GetDlgItem(IDC_BTN_SEND)->EnableWindow(false);
pDlg->GetDlgItem(IDC_EDIT1)->EnableWindow(false);
pDlg->GetDlgItem(IDC_SPIN1)->EnableWindow(false);
pDlg->GetDlgItem(IDC_IPADDRESS1)->EnableWindow(false);
pDlg->GetDlgItem(IDC_IPADDRESS2)->EnableWindow(false);
pDlg->GetDlgItem(IDC_BTN_EXIT)->EnableWindow(true);
ULONG startIp,endIp;
CString tmp;
m_IPEdit1.GetWindowText(tmp);
startIp = ntohl(inet_addr(tmp));
m_IPEdit2.GetWindowText(tmp);
endIp = ntohl(inet_addr(tmp));
for( ULONG ip =startIp ; ip <= endIp ; ip ++ )
{
CString mac = CJahNetCommon::GetMacByIp(ip);
CString sIp;
BYTE b[4];
memcpy(b,&ip,sizeof(ULONG));
sIp.Format("%u.%u.%u.%u",b[3],b[2],b[1],b[0]);
m_ListBox.InsertString(0,sIp);
if(mac != "")
{
int index=m_ListView.InsertItem(0,sIp);
m_ListView.SetItem(index,4,LVIF_TEXT,mac, 0, 0, 0,0);
}
UpdateData(false);
}
pDlg->m_ListBox.InsertString(0,"-----启动无条件扫描!-----");
pDlg->GetDlgItem(IDC_BTN_SEND)->EnableWindow(true);
pDlg->GetDlgItem(IDC_EDIT1)->EnableWindow(true);
pDlg->GetDlgItem(IDC_SPIN1)->EnableWindow(true);
pDlg->GetDlgItem(IDC_IPADDRESS1)->EnableWindow(true);
pDlg->GetDlgItem(IDC_IPADDRESS2)->EnableWindow(true);
pDlg->GetDlgItem(IDC_BTN_EXIT)->EnableWindow(false);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -