📄 mysnmpdlg.cpp
字号:
// MySnmpDlg.cpp : implementation file
//
#include "stdafx.h"
#include "MySnmp.h"
#include "MySnmpDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define SNMP_MSG WM_USER+100
#define DAY (24L*60L*60L)
#define HOUR (60L*60L)
#define MINUTE (60L)
#define HUNDRED (100L)
#define LIST_SYSTEMINFO 0 //系统信息
#define LIST_ROUTETABLE 1 //路由表
#define LIST_ROUTESUBNET 2 //子网
#define LIST_ROUTETUOPU 3 //拓扑图
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
typedef struct tagTipData
{
CPoint point;
CString strIP;
}TipData;
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()
/////////////////////////////////////////////////////////////////////////////
// CMySnmpDlg dialog
CMySnmpDlg::CMySnmpDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMySnmpDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMySnmpDlg)
// 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);
m_nListType=-1;
m_RightRect.left=m_RightRect.right=m_RightRect.bottom=m_RightRect.top=0;
m_pTipDlg=NULL;
}
void CMySnmpDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMySnmpDlg)
DDX_Control(pDX, IDC_LIST1, m_ctrlListCtrl);
DDX_Control(pDX, IDC_IPADDRESS1, m_ctrlIPAddress);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMySnmpDlg, CDialog)
//{{AFX_MSG_MAP(CMySnmpDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTONSTART, OnButtonstart)
ON_BN_CLICKED(IDC_BUTTONSYSTEMINFO, OnButtonsysteminfo)
ON_BN_CLICKED(IDC_BUTTONROUTETABLE, OnButtonroutetable)
ON_BN_CLICKED(IDC_BUTTONSUBNETINFO, OnButtonsubnetinfo)
ON_BN_CLICKED(IDC_BUTTONTUOPU, OnButtontuopu)
ON_BN_CLICKED(IDC_BUTTONQUIT, OnButtonquit)
ON_BN_CLICKED(IDC_BUTTONABOUT, OnButtonabout)
ON_WM_CLOSE()
ON_NOTIFY(NM_DBLCLK, IDC_LIST1, OnDblclkList1)
ON_WM_LBUTTONDOWN()
ON_WM_TIMER()
//}}AFX_MSG_MAP
ON_MESSAGE(SNMP_MSG,OnReceive)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMySnmpDlg message handlers
BOOL CMySnmpDlg::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
m_ctrlIPAddress.SetAddress(127,0,0,1);
m_ctrlIPAddress.SetFieldFocus(0);
CButton* pButton;
pButton=(CButton*)this->GetDlgItem(IDC_BUTTONSYSTEMINFO);
pButton->EnableWindow(FALSE);
pButton=(CButton*)this->GetDlgItem(IDC_BUTTONROUTETABLE);
pButton->EnableWindow(FALSE);
pButton=(CButton*)this->GetDlgItem(IDC_BUTTONSUBNETINFO);
pButton->EnableWindow(FALSE);
pButton=(CButton*)this->GetDlgItem(IDC_BUTTONTUOPU);
pButton->EnableWindow(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
void CMySnmpDlg::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 CMySnmpDlg::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 CMySnmpDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMySnmpDlg::OnOK()
{
// TODO: Add extra validation here
// CDialog::OnOK();
}
void CMySnmpDlg::OnButtonstart()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
BYTE nField0,nField1,nField2,nField3;
m_ctrlIPAddress.GetAddress(nField0,nField1,nField2,nField3);
sprintf(m_IPAddress,"%d.%d.%d.%d",nField0,nField1,nField2,nField3);
m_Route.m_strAddress=m_IPAddress;
//MessageBox(m_IPAddress);
if(!InitSnmp())
{
return;
}
m_messageDlg.Create(IDD_DIALOG1);
m_messageDlg.CenterWindow();
m_messageDlg.ShowWindow(SW_SHOW);
CButton* pButton=(CButton*)GetDlgItem(IDC_BUTTONSTART);
pButton->EnableWindow(FALSE);
GetRouteInfo();
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
}
//初始化snmp
BOOL CMySnmpDlg::InitSnmp()
{
SNMPAPI_STATUS lStat;
//Initialize WinSNMP
smiUINT32 nMajorVersion,nMinorVersion,nLevel,nTranslateMode,nRetransmitMode;
lStat=SnmpStartup(&nMajorVersion,&nMinorVersion,&nLevel,&nTranslateMode,&nRetransmitMode);
if(lStat==SNMPAPI_FAILURE)
{
MessageBox("SnmpStartup Error!");
return FALSE;
}
//设置传输模式
lStat=SnmpSetTranslateMode(SNMPAPI_UNTRANSLATED_V1);
if(lStat==SNMPAPI_FAILURE)
{
MessageBox("SnmpSetTranslateMode Error!");
return FALSE;
}
//设置重传模式
lStat=SnmpSetRetransmitMode(SNMPAPI_ON);
if (lStat==SNMPAPI_FAILURE)
{
MessageBox("SnmpSetRetransmitMode Error!");
return FALSE;
}
//创建会话
m_hSession=SnmpCreateSession(CMySnmpDlg::m_hWnd,SNMP_MSG,NULL,NULL);
if(m_hSession==SNMPAPI_FAILURE)
{
MessageBox("SnmpCreateSession Error!");
SnmpCleanup();
return FALSE;
}
//创建实体句柄
m_hDstEntity=SnmpStrToEntity(m_hSession,m_IPAddress);
if(m_hDstEntity==SNMPAPI_FAILURE)
{
MessageBox("SnmpStrToEntity Error!");
SnmpCleanup();
return FALSE;
}
//创建上下文句柄
smiOCTETS dcTx;
char community[]="public";
dcTx.len=strlen(community);
dcTx.ptr=(smiLPBYTE)community;
m_hContext=SnmpStrToContext(m_hSession,&dcTx);
if(m_hContext==SNMPAPI_FAILURE)
{
MessageBox("SnmpStrToContext Error!");
SnmpCleanup();
return FALSE;
}
//创建变量捆绑列表
m_hVBL=SnmpCreateVbl(m_hSession,NULL,NULL);
if(m_hVBL==SNMPAPI_FAILURE)
{
MessageBox("SnmpCreateVbl Error!");
SnmpCleanup();
return FALSE;
}
return TRUE;
}
//开始获取路由器的各种信息
void CMySnmpDlg::GetRouteInfo()
{
char sName[100]="1.3.6.1.2.1";
smiOID dName;
if(SnmpStrToOid(sName,&dName)==SNMPAPI_FAILURE)
{
CButton* pButton=(CButton*)GetDlgItem(IDC_BUTTONSTART);
pButton->EnableWindow(TRUE);
m_messageDlg.ShowWindow(SW_HIDE);
m_messageDlg.DestroyWindow();
MessageBox("SnmpStrToOid Error!");
SnmpCleanup();
return;
}
//添加到变量绑定列表
if(SnmpSetVb(m_hVBL,0,&dName,NULL)==SNMPAPI_FAILURE)
{
CButton* pButton=(CButton*)GetDlgItem(IDC_BUTTONSTART);
pButton->EnableWindow(TRUE);
m_messageDlg.ShowWindow(SW_HIDE);
m_messageDlg.DestroyWindow();
MessageBox("SnmpSetVb Error!");
SnmpCleanup();
return;
}
//创建协议数据单元
HSNMP_PDU hPdu=SnmpCreatePdu(m_hSession,SNMP_PDU_GETNEXT,100,0,0,m_hVBL);
if(hPdu==SNMPAPI_FAILURE)
{
CButton* pButton=(CButton*)GetDlgItem(IDC_BUTTONSTART);
pButton->EnableWindow(TRUE);
m_messageDlg.ShowWindow(SW_HIDE);
m_messageDlg.DestroyWindow();
MessageBox("SnmpCreatePdu Error!");
SnmpCleanup();
return;
}
//发送数据
if(SnmpSendMsg(m_hSession,0,m_hDstEntity,m_hContext,hPdu)==SNMPAPI_FAILURE)
{
CButton* pButton=(CButton*)GetDlgItem(IDC_BUTTONSTART);
pButton->EnableWindow(TRUE);
m_messageDlg.ShowWindow(SW_HIDE);
m_messageDlg.DestroyWindow();
MessageBox("SnmpSendMsg Error!");
SnmpCleanup();
return;
}
}
void CMySnmpDlg::OnReceive()
{
//MessageBox("接受到数据");
//接受数据
HSNMP_PDU hPdu;
if(SnmpRecvMsg(m_hSession,NULL,NULL,NULL,&hPdu)==SNMPAPI_FAILURE)
{
CButton* pButton=(CButton*)GetDlgItem(IDC_BUTTONSTART);
pButton->EnableWindow(TRUE);
m_messageDlg.ShowWindow(SW_HIDE);
m_messageDlg.DestroyWindow();
MessageBox("SnmpRecvMsg Error!");
SnmpCleanup();
return;
}
//提取协议数据单元
smiINT32 lType, lErr, lIdx,lReqId;
HSNMP_VBL hVbl;
if(SnmpGetPduData(hPdu,&lType,&lReqId,&lErr,&lIdx,&hVbl)==SNMPAPI_FAILURE)
{
CButton* pButton=(CButton*)GetDlgItem(IDC_BUTTONSTART);
pButton->EnableWindow(TRUE);
m_messageDlg.ShowWindow(SW_HIDE);
m_messageDlg.DestroyWindow();
MessageBox("SnmpGetPduData Error!");
SnmpCleanup();
return;
}
if(lErr!=0)
{
CButton* pButton=(CButton*)GetDlgItem(IDC_BUTTONSTART);
pButton->EnableWindow(TRUE);
m_messageDlg.ShowWindow(SW_HIDE);
m_messageDlg.DestroyWindow();
CString str;
str.Format("SNMP Error for RequestID = %d: Value = %d, Index = %d\n",
lReqId, lErr, lIdx);
MessageBox(str);
SnmpCleanup();
return ;
}
smiUINT32 nVb;
nVb=SnmpCountVbl(hVbl);
smiOID dName;
smiVALUE dValue;
char szName[256];
char szValue[256];
for(smiUINT32 i=1;i<=nVb;i++)
{
SnmpGetVb(hVbl,i,&dName,&dValue);
SnmpOidToStr(&dName,sizeof(szName),szName); //获取变量名
ValToStr(&dValue,sizeof(szValue),szValue); //获取变量值
//////////////////////////////////////////////////////////////////////////
Analyse(szName,szValue); //提取有用的信息,分别保存
if(IsBeginWith(szName,"1.3.6.1.2.1.5"))
{
//表示所需的信息已经全部获取
//////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -