📄 ipquerydlg.cpp
字号:
// IpQueryDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "IpQuery.h"
#include "IpQueryDlg.h"
#include ".\ipquerydlg.h"
#include "IcmpSendEcho.h"
#include "ipexport.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// 对话框数据
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
// CIpQueryDlg 对话框
CIpQueryDlg::CIpQueryDlg(CWnd* pParent /*=NULL*/)
: CDialog(CIpQueryDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CIpQueryDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_IPADDRESS_BEGIN, ipAddressBegin);
DDX_Control(pDX, IDC_IPADDRESS_END, ipAddressEnd);
}
BEGIN_MESSAGE_MAP(CIpQueryDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_SEARCH, OnBnClickedSearch)
END_MESSAGE_MAP()
// CIpQueryDlg 消息处理程序
BOOL CIpQueryDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 将\“关于...\”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
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);
}
}
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
ShowWindow(SW_MINIMIZE);
// TODO: 在此添加额外的初始化代码
WSADATA wsa;
if (WSAStartup(MAKEWORD(1, 1), &wsa) != 0)
{
TRACE(_T("Could not negotiate a correct version of WinSock\n"));
return FALSE;
}
//Load up the ICMP library
m_hDLL = LoadLibrary(_T("icmp.dll"));
if (m_hDLL == NULL)
{
TRACE(_T("Could not load up the ICMP DLL\n"));
return FALSE;
}
//Retrieve pointers to the functions in the ICMP dll
sm_pIcmpCreateFile = (lpIcmpCreateFile) GetProcAddress(m_hDLL, "IcmpCreateFile");
sm_pIcmpSendEcho = (lpIcmpSendEcho) GetProcAddress(m_hDLL, "IcmpSendEcho" );
sm_pIcmpCloseHandle = (lpIcmpCloseHandle) GetProcAddress(m_hDLL, "IcmpCloseHandle");
if (sm_pIcmpCreateFile == NULL || sm_pIcmpSendEcho == NULL || sm_pIcmpCloseHandle == NULL)
{
TRACE(_T("Could not find ICMP functions in the ICMP DLL\n"));
return FALSE;
}
hIcmpFile = sm_pIcmpCreateFile();
if (hIcmpFile == INVALID_HANDLE_VALUE) {
printf("\tUnable to open handle.\n");
printf("IcmpCreatefile returned error: %ld\n", GetLastError() );
return 0;
}
ip_Saved = NULL;
// ip_Saved.RemoveAll();
return TRUE; // 除非设置了控件的焦点,否则返回 TRUE
}
void CIpQueryDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CIpQueryDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作矩形中居中
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;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标显示。
HCURSOR CIpQueryDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CIpQueryDlg::OnBnClickedSearch()
{
// TODO: Add your control notification handler code here
if (ipAddressBegin.IsBlank()||ipAddressEnd.IsBlank())
{
AfxMessageBox("Please input the ip!",MB_OK,0);
return;
}
BYTE ip_bgn[4],ip_end[4],ip_tmp[4];
CString ip_address_end,ip_address_tmp;
ipAddressBegin.GetAddress(ip_bgn[0],ip_bgn[1],ip_bgn[2],ip_bgn[3]);
ip_address_tmp.Format("%d%s%d%s%d%s%d",ip_bgn[0],".",ip_bgn[1],".",ip_bgn[2],".",ip_bgn[3]);
for (int m=0;m<4;m++)
{
ip_tmp[m] = ip_bgn[m];
}
ipAddressEnd.GetAddress(ip_end[0],ip_end[1],ip_end[2],ip_end[3]);
ip_address_end.Format("%d%s%d%s%d%s%d",ip_end[0],".",ip_end[1],".",ip_end[2],".",ip_end[3]);
while (CompareString(LOCALE_SYSTEM_DEFAULT,0,ip_address_tmp,-1,ip_address_end,-1)!=CSTR_EQUAL)
{
if(Ping(ip_address_tmp))
{
IpNode * ip,*first;
ip =(IpNode *)malloc(sizeof(IpNode));
memset(ip,0,sizeof(IpNode));
strcpy(ip->ip,ip_address_tmp.GetBuffer(ip_address_tmp.GetLength()));
first = ip_Saved;
if (ip_Saved ==NULL)
{
ip_Saved =ip;
}
else
{
while(first->next !=NULL) first =first->next;
first->next= ip;
}
AfxMessageBox("ping succeed \n"+ip_address_tmp);
}
else AfxMessageBox("ping fail"+ip_address_tmp);
if (ip_tmp[3]<255)
{
ip_tmp[3]++;
}
else
{
ip_tmp[2]++;
ip_tmp[3]=1;
}
if (ip_tmp[2]>255)
{
ip_tmp[1]++;
ip_tmp[2]=1;
}
ip_address_tmp.Format("%d%s%d%s%d%s%d",ip_tmp[0],".",ip_tmp[1],".",ip_tmp[2],".",ip_tmp[3]);
}
if(Ping(ip_address_end))
{
IpNode * ip,*first;
ip =(IpNode *)malloc(sizeof(IpNode));
memset(ip,0,sizeof(IpNode));
strcpy(ip->ip,ip_address_tmp.GetBuffer(ip_address_tmp.GetLength()));
first = ip_Saved;
if (ip_Saved ==NULL)
{
ip_Saved =ip;
}
else
{
while(first->next !=NULL) first =first->next;
first->next = ip;
}
AfxMessageBox("ping succeed \n"+ip_address_end);
}
else AfxMessageBox("ping fail \n"+ip_address_tmp);
int num=0;
IpNode *pt =ip_Saved;
while(pt !=NULL)
{
pt =pt->next;
num++;
}
CString show;
show.Format("%d",num);
AfxMessageBox("the ping succeed num is :"+show);
FreeLibrary(m_hDLL);
pt=ip_Saved;;
if(ip_Saved !=NULL)
{
while (ip_Saved->next !=NULL)
{
pt =ip_Saved->next;
free(ip_Saved);
ip_Saved =pt;
}
free(ip_Saved);
ip_Saved =NULL;
pt =NULL;
}
}
bool CIpQueryDlg::Ping(CString ip)
{
DWORD dwRetVal = 0;
char SendData[] = "Data Buffer";
LPVOID ReplyBuffer = NULL;
DWORD ReplySize = 0;
int k;
IPAddr ipaddr;
ipaddr =inet_addr(ip);
ReplySize = sizeof(ICMP_ECHO_REPLY) + sizeof(SendData);
ReplyBuffer = (VOID*) malloc(ReplySize);
if (ReplyBuffer == NULL) {
printf("\tUnable to allocate memory\n");
return FALSE;
}
for (k=0;k<2;k++)
{
dwRetVal = sm_pIcmpSendEcho(hIcmpFile, ipaddr, SendData, sizeof(SendData),
NULL, ReplyBuffer, ReplySize, 1000);
if(dwRetVal != 0)
{/*printf("k = %d\n",k);*/break;}
}
if (dwRetVal != 0) {
//PICMP_ECHO_REPLY pEchoReply = (PICMP_ECHO_REPLY)ReplyBuffer;
//struct in_addr Reply_Addr;
//Reply_Addr.S_un.S_addr = pEchoReply->Address;
////printf("\tSent icmp message to %s\n", argv[1]);
//printf("Send icmp message already \n");
//if (dwRetVal > 1) {
// printf("\tReceived %ld icmp message responses\n", dwRetVal);
// printf("\tInformation from the first response:\n");
//}
//else {
// printf("\tReceived %ld icmp message response\n", dwRetVal);
// printf("\tInformation from this response:\n");
//}
//printf("\t Received from %s\n", inet_ntoa( Reply_Addr ) );
//printf("\t Status = %ld\n",
// pEchoReply->Status);
//printf("\t Roundtrip time = %ld milliseconds\n",
// pEchoReply->RoundTripTime);
return TRUE;
}
else {
/*printf("\tCall to IcmpSendEcho failed.\n");
printf("\tIcmpSendEcho returned error: %ld\n", GetLastError() );*/
return FALSE;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -