www.pudn.com > findIP.rar > SearchIPDlg.cpp
// SearchIPDlg.cpp : implementation file
//
#include "stdafx.h"
#include "SearchIP.h"
#include "SearchIPDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define destPORT 137 //SearchIP name port
#define myPORT 4321
/////////////////////////////////////////////////////////////////////////////
// CSearchIPDlg dialog
//--------------------------------var-------------------------------
CSearchIPDlg* 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 IP1[4],IP2[4];
HANDLE wait_handle;
CString strOldIP="";
bool bExit=false;
int i;
//------------------------------------------------------------------
CSearchIPDlg::CSearchIPDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSearchIPDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSearchIPDlg)
// 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 CSearchIPDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSearchIPDlg)
DDX_Control(pDX, IDC_LIST1, m_ListBox);
DDX_Control(pDX, IDC_IPADDRESS2, m_IPEnd);
DDX_Control(pDX, IDC_LIST2, m_ListView);
DDX_Control(pDX, IDC_IPADDRESS1, m_IPStart);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSearchIPDlg, CDialog)
//{{AFX_MSG_MAP(CSearchIPDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BTN_START, OnBtnStart)
ON_NOTIFY(IPN_FIELDCHANGED, IDC_IPADDRESS1, OnFieldchangedIpaddress1)
ON_BN_CLICKED(IDC_BTN_EXIT, OnBtnExit)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSearchIPDlg message handlers
BOOL CSearchIPDlg::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
m_IPStart.SetAddress(202,204,120,1);//初始化两个IP Address
m_IPEnd.SetAddress(202,204,120,255);
wait_handle=CreateEvent(NULL,true,false,"receive data");//初始化wait_handle
GetDlgItem(IDC_BTN_EXIT)->EnableWindow(false);
// -----------------对ListView进行初始化---------------------
DWORD dwStyle=GetWindowLong(m_ListView.GetSafeHwnd(),GWL_STYLE);
dwStyle&=~LVS_TYPEMASK;
dwStyle|=LVS_REPORT;
SetWindowLong(m_ListView.GetSafeHwnd(),GWL_STYLE,dwStyle);
m_ListView.SetTextColor(RGB(255,0,0));//设置字的颜色
m_ListView.InsertColumn(0," 用户",LVCFMT_LEFT,100);//对DataList加入列
m_ListView.InsertColumn(0," 主机",LVCFMT_LEFT,100);
m_ListView.InsertColumn(0," 工作组",LVCFMT_LEFT,100);
m_ListView.InsertColumn(0," IP地址",LVCFMT_LEFT,130);
m_ListView.SetExtendedStyle(LVS_EX_GRIDLINES);//设置m_ListView显示格式
::SendMessage(m_ListView.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 CSearchIPDlg::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 CSearchIPDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//--------------------------SearchIP线程----------------------
UINT SearchIPThread(LPVOID param)
{
do
{
if(bExit)
{
AfxMessageBox("搜索终止!");//搜索被终止时,重新设置组件是否可视
pDlg->GetDlgItem(IDC_BTN_START)->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;
}
//m_strIP此时查询的IP
//在m_ListBox中显示正在搜寻的IP地址
pDlg->m_strIP.Format("%d.%d.%d.%d",IP1[0],IP1[1],IP1[2],IP1[3]);
pDlg->m_ListBox.InsertString(0,pDlg->m_strIP);
if(IP1[3]!=0&&IP1[2]!=0)
pDlg->m_UDPSocket.SendTo((void*)bs,50,destPORT,pDlg->m_strIP,0);//查询IP,自动调用UDP中的OnReceive()函数接收.
//AfxMessageBox(pDlg->m_strIP);
int nWait=100;//设置最长等待时间限制为100
WaitForSingleObject(
wait_handle, // handle to object to wait for
nWait // time-out interval in milliseconds
);
ResetEvent(wait_handle);
//=============================================
if(IP1[2]<=IP2[2])
{
//if(IP1[3]==255)AfxMessageBox("here0");
if(IP1[3]=IP2[3]&&IP1[2]>=IP2[2])break;
}while(IP1[2]<=255&&IP1[3]<=255);
pDlg->m_ListBox.InsertString(0,"-----搜索完成!-----");
pDlg->GetDlgItem(IDC_BTN_START)->EnableWindow(true);
pDlg->GetDlgItem(IDC_IPADDRESS1)->EnableWindow(true);
pDlg->GetDlgItem(IDC_IPADDRESS2)->EnableWindow(true);
pDlg->GetDlgItem(IDC_BTN_EXIT)->EnableWindow(false);
return 0;
}
//-----------------------------------------------------------
void CSearchIPDlg::OnBtnStart()
{
// TODO: Add your control notification handler code here
m_ListView.UpdateData(true);//数据更新
m_IPStart.GetAddress(IP1[0],IP1[1],IP1[2],IP1[3]);
m_IPEnd.GetAddress(IP2[0],IP2[1],IP2[2],IP2[3]);
if(IP2[2]EnableWindow(false);
GetDlgItem(IDC_IPADDRESS1)->EnableWindow(false);
GetDlgItem(IDC_IPADDRESS2)->EnableWindow(false);
GetDlgItem(IDC_BTN_EXIT)->EnableWindow(true);
//把对IP地址的搜寻通过进程实现
AfxBeginThread(SearchIPThread,this->GetSafeHwnd(),THREAD_PRIORITY_NORMAL);
}
void CSearchIPDlg::OnReceive()
{
BYTE Buf[500];
//m_UDPSocket.Receive(Buf,500,0);
//AfxMessageBox("here1!!!!");
CString str,strIP,strHost,strHex,strMac,Host,Group,User;
UINT dport;
m_UDPSocket.ReceiveFrom(Buf,500,strIP,dport,0);//查找网络中IP对应的主机相关信息存放在buf中
if(strIP==(char)NULL||strIP==strOldIP)return;
strOldIP=strIP;
int index=m_ListView.InsertItem(0,strIP);
strHost="";
strHex="";
User="?";
Host="\\";
int tem=0,num=0;
bool bAdd=true;
//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;
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!="")
{
m_ListView.SetItem(index,1,LVIF_TEXT,strHost, 0, 0, 0,0);
Group=strHost;
num++;
}
else
{
if(strHost!=Host&&strHost!=Group&&num==2&&strHost!="")
{
User=strHost;
if(User!="__MSBROWSE__")
{
m_ListView.SetItem(index,3,LVIF_TEXT,User, 0, 0, 0,0);
num++;
}
//num++;
}
}
}
strMac=strHex;
strHost="";
strHex="";
}
}
SetEvent(wait_handle);
}
void CSearchIPDlg::OnFieldchangedIpaddress1(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
unsigned char AddrIP[4];
m_IPStart.GetAddress(AddrIP[0],AddrIP[1],AddrIP[2],AddrIP[3]);
m_IPEnd.SetAddress(AddrIP[0],AddrIP[1],AddrIP[2],255);
*pResult = 0;
}
void CSearchIPDlg::OnBtnExit()
{
// TODO: Add your control notification handler code here
bExit=true;
}