📄 myarpdlg.cpp
字号:
// MyARPDlg.cpp : implementation file
//
#include "stdafx.h"
#include "MyARP.h"
#include "MyARPDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyARPDlg dialog
CMyARPDlg::CMyARPDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyARPDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyARPDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMyARPDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyARPDlg)
DDX_Control(pDX, ID_BUTTON_STOP, m_stop);
DDX_Control(pDX, ID_BUTTON_SCAN, m_start);
DDX_Control(pDX, IDC_LIST, m_ListCtl);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMyARPDlg, CDialog)
//{{AFX_MSG_MAP(CMyARPDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CANCELMODE()
ON_BN_CLICKED(IDC_BUTTON_Send, OnBUTTONSend)
ON_BN_CLICKED(ID_BUTTON_SCAN, OnButtonScan)
ON_BN_CLICKED(ID_BUTTON_STOP, OnButtonStop)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyARPDlg message handlers
BOOL CMyARPDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
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);
}
}
// 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
list_count = 0;
Monitor_flag = false; // 设置监视标志
m_stop.EnableWindow(false);
InitList();
return TRUE; // return TRUE unless you set the focus to a control
}
void CMyARPDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 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 CMyARPDlg::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 CMyARPDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMyARPDlg::OnCancelMode()
{
CDialog::OnCancelMode();
// TODO: Add your message handler code here
}
// 初始化列表控件函数
void CMyARPDlg::InitList(void)
{
char *Column[] = {"操作码", "发送方MAC", "发送方IP地址", "接收方MAC", "接收方IP"};
int width[] = {50, 150, 150, 150, 150};
LVCOLUMN lvc;
lvc.mask = LVCF_FMT|LVCF_WIDTH|LVCF_TEXT|LVCF_SUBITEM;
lvc.fmt = LVCFMT_LEFT;
for(int i=0; i<5; i++){
lvc.pszText = Column[i];
lvc.cx = width[i];
lvc.iSubItem = i;
m_ListCtl.InsertColumn(i, &lvc);
}
}
// 在列表控件中增加一行
void CMyARPDlg::AddRow(char *src_Op, char *src_SendMAC, char *src_SendIP, char *src_TargMAC, char *src_TargIP)
{
LVITEM lvi;
lvi.mask = LVIF_TEXT|LVIF_IMAGE|LVIF_PARAM;
lvi.iSubItem = 0;
lvi.pszText = "";
lvi.iImage = 0;
lvi.iItem = 0;
char * data[] = {src_Op, src_SendMAC, src_SendIP, src_TargMAC, src_TargIP};
lvi.iItem = list_count;
m_ListCtl.InsertItem(&lvi);
for(int i=0; i<5; i++)m_ListCtl.SetItemText(list_count, i, (char *)data[i]);
list_count++;
}
void CMyARPDlg::OnOK()
{
// TODO: Add extra validation here
CDialog::OnOK();
}
void CMyARPDlg::OnBUTTONSend()
{
// TODO: Add your control notification handler code here
}
void CMyARPDlg::OnButtonScan()
{
// TODO: Add your control notification handler code here
TCHAR szOutputBuffer[20];
m_hDevice = CreateFile("\\\\.\\Passthru",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if(m_hDevice == INVALID_HANDLE_VALUE)
{
MyGetLastError();
return;
}
DWORD dwReturn;
char *Event_name = "Scan_Event";
// 创建 Event 对象,初始化状态为FALSE。
m_hEvent = CreateEvent(NULL, false, false, Event_name);
// 把 Event 对象传递给驱动程序
DeviceIoControl(m_hDevice,
IO_REFERENCE_EVENT,
(LPVOID)m_hEvent,
0,
NULL,
0,
&dwReturn,
NULL);
m_start.EnableWindow(false);
m_stop.EnableWindow(true);
// 设置监视标志
Monitor_flag = true;
// 取得共享内存的地址
if(!DeviceIoControl(m_hDevice,
IO_GET_SHAREMEMORY_ADDR,
NULL,
NULL,
szOutputBuffer,
sizeof(szOutputBuffer),
&dwReturn,
NULL
))
{
MessageBox("SHAREMEMORY_ADD Fail!");
MyGetLastError();
}
psharememory = *((PVOID *)szOutputBuffer);
// 启动监视线程
CreateMyThread();
}
unsigned long __stdcall CMyARPDlg::MyThreadEntry(LPVOID lpParam)
{
DWORD dwReturn;
CMyARPDlg* pThis = (CMyARPDlg*)lpParam;
while(pThis->Monitor_flag != false)
{
if(pThis->m_hEvent)
{
WaitForSingleObject(pThis->m_hEvent, INFINITE);
// 在列表控件中显示
PARPPACKET pkt = (PARPPACKET)pThis->psharememory;
struct in_addr temp1,temp2;
temp1.S_un.S_addr = pkt->arpFrame.Send_Prot_Addr;
temp2.S_un.S_addr = pkt->arpFrame.Targ_Prot_Addr;
pThis->AddRow("", "", inet_ntoa(temp1), "", inet_ntoa(temp2));
// 设置事件对象为非信号态
DeviceIoControl(pThis->m_hDevice,
IO_CLEAR_EVENT,
NULL,
0,
NULL,
0,
&dwReturn,
NULL);
}
}
return true;
}
void CMyARPDlg::CreateMyThread()
{
// TODO: Add your control notification handler code here
DWORD dwThreadID;
HANDLE hThread = CreateThread(NULL,
0,
MyThreadEntry,
this,
0,
&dwThreadID);
CloseHandle(hThread);
}
void WINAPI CMyARPDlg::MyGetLastError(void)
{
LPVOID lpMsgBuf = NULL;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
// Display the string.
MessageBox((LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );
// Free the buffer.
LocalFree(lpMsgBuf);
}
void CMyARPDlg::OnButtonStop()
{
// TODO: Add your control notification handler code here
DWORD dwReturn;
Monitor_flag = false;
// 关闭设备句柄,清除事件对象。
if(m_hDevice)
{
if(m_hEvent)
{
DeviceIoControl(m_hDevice,
IO_STOP_MONITOR_EVENT,
NULL,
0,
NULL,
0,
&dwReturn,
NULL);
CloseHandle(m_hEvent);
}
CloseHandle(m_hDevice);
}
// 设置按钮状态
m_start.EnableWindow(true);
m_stop.EnableWindow(false);
}
void CMyARPDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
OnButtonStop();
CDialog::OnClose();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -