How to use SNMP in a Visual Basic project
Network Component provides an easy-to-use development interface to a variety of IP protocols. By using Network Component, you can very easily create or enhance applications with network features.
Network Component features the following: DNS, FTP, HTTP, HTTPs, ICMP Ping, IP-to-Country, MSN, NTP, RSH, SCP, SFTP, SNMP v1/v2c (Get, GetNext, Set), SNMP Traps, SNMP MIB, SSH, TCP, Telnet, TFTP, UDP, Telnet, Wake-On-LAN and more.
Network Component can be well integrated into any development platform that supports ActiveX objects.
The most important functions of the SFtp object are:
- Connect - connect to the (remote) FTP server on port 22 or any alternate port;
- Disconnect - to diconnect after a connect call;
- GetCurrentDir - retrieve the current directory;
- ChangeDir - change the current directory;
- CreateDir - create a new directory;
- RenameDir - rename a directory;
- DeleteDir - delete a directory;
- FindFile - find a specific file in the current directory;
- FindFirstFile - iterate over all files in the current directory; find the first file;
- FindNextFile - iterate over all files in the current directory; find the next file;
- RenameFile - rename a file in the current directory;
- DeleteFile - delete a file in the current directory;
- GetFile - get (download) a file;
- PutFile - put (upload) a file;
Step 1: Download and install the Network Component
Download Network Component from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.
Step 2: Create a new Visual Basic project
Launch 'Microsoft Visual Basic' from the Start menu, and choose 'New' from the 'File Menu'. The 'New Project' dialog appears.
Select 'Standard Exe' and click 'OK':
Step 3: Refer to the Network Component Library and create the objects
A new Project is created, with a blank form.
First, you must add a reference to Network Component in the project to be able to use the object. To do so, choose 'References...' from the 'Project' menu. In the 'References' dialog that pops up, enable the 'Network Component 3.1 Type Library' reference as shown in the following picture:
Click 'OK' to close the 'References...' dialog.
Then, select the Project form and choose 'View Code' from the context menu:
On top of your code, declare the following object:
Public objSnmp As AxNetwork.Snmp
Step 4: Create the object
From the Code window, select 'Form'. The Private Sub 'Form_Load()' will be displayed now. In the 'Form Load' function, create the object in the following way:
Set objSnmp = CreateObject("AxNetwork.Snmp")
Appendix: Full source code
Option Explicit '/////////////////////////////////////////////////////////////////////// Dim objSnmpManager As SnmpManager Dim objSnmpObject As SnmpObject Dim objConstants As NwConstants '/////////////////////////////////////////////////////////////////////// Dim bOpen As Boolean Dim lCurrentType As Long '/////////////////////////////////////////////////////////////////////// Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long Private Const MAX_PATH = 260 '/////////////////////////////////////////////////////////////////////// Private Sub Form_Load() Set objSnmpManager = CreateObject("AxNetwork.SnmpManager") Set objConstants = CreateObject("AxNetwork.NwConstants") cmbVersion.AddItem ("V1") cmbVersion.AddItem ("V2C") cmbVersion.ListIndex = 1 objSnmpManager.Initialize SetDefaultLogFile EnableControls End Sub '/////////////////////////////////////////////////////////////////////// Private Sub btnClose_Click() objSnmpManager.Close bOpen = False EnableControls End Sub '/////////////////////////////////////////////////////////////////////// Private Sub btnGet_Click() If bOpen = True Then MousePointer = vbHourglass Dim objSnmpObject Set objSnmpObject = objSnmpManager.Get(txtOID.Text) If ShowResult = 0 Then txtOID = objSnmpObject.OID txtOIDName = objSnmpObject.OIDName txtValue = objSnmpObject.Value lCurrentType = objSnmpObject.Type If (optHex.Value = True) Then txtValue = StringToHex(objSnmpObject.Value) End If EnableRadioBoxes (lCurrentType) GetType End If Set objSnmpObject = Nothing MousePointer = vbDefault End If End Sub '/////////////////////////////////////////////////////////////////////// Private Sub btnGetNext_Click() If bOpen = True Then MousePointer = vbHourglass Set objSnmpObject = objSnmpManager.GetNext() If ShowResult = 0 Then txtOID = objSnmpObject.OID txtOIDName = objSnmpObject.OIDName txtValue = objSnmpObject.Value lCurrentType = objSnmpObject.Type If (optHex.Value = True) Then txtValue = StringToHex(objSnmpObject.Value) End If EnableRadioBoxes (lCurrentType) GetType End If MousePointer = vbDefault End If End Sub '/////////////////////////////////////////////////////////////////////// Private Sub btnOpen_Click() objSnmpManager.LogFile = txtLogFile.Text objSnmpManager.ProtocolVersion = cmbVersion.ListIndex + 1 If (txtMibFile.Text <> "") Then objSnmpManager.LoadMibFile (txtMibFile.Text) If (ShowResult <> 0) Then Exit Sub End If End If objSnmpManager.Open txtAgent.Text, txtCommunity.Text, CInt(txtPort.Text) If (ShowResult = 0) Then bOpen = True EnableControls End If End Sub '/////////////////////////////////////////////////////////////////////// Private Sub btnSet_Click() If bOpen = True Then MousePointer = vbHourglass Set objSnmpObject = CreateObject("AxNetwork.SnmpObject") objSnmpObject.Clear objSnmpObject.Type = lCurrentType objSnmpObject.Value = txtNewValue.Text objSnmpObject.OID = txtOID.Text objSnmpObject.OIDName = txtOIDName.Text objSnmpManager.Set objSnmpObject MousePointer = vbDefault ShowResult End If End Sub '/////////////////////////////////////////////////////////////////////// Private Sub btnBrowse_Click() With dlgOpenFile .DefaultExt = "mib" .DialogTitle = "Select MIB file" .InitDir = "C:\" .Filter = "All MIB Files|*.mib" .FilterIndex = 1 .ShowOpen End With txtMibFile.Text = dlgOpenFile.FileName End Sub '/////////////////////////////////////////////////////////////////////// Private Sub btnView_Click() If FileExists(txtLogFile.Text) = True Then Shell "notepad " + txtLogFile.Text, vbNormalFocus End If End Sub '/////////////////////////////////////////////////////////////////////// Public Function FileExists(sFileName As String) As Boolean FileExists = CBool(Len(Dir$(sFileName))) And CBool(Len(sFileName)) End Function '/////////////////////////////////////////////////////////////////////// Public Function StringToHex(ByVal str As String) As String Dim X As Long, hexchar As String For X = 1 To Len(str) hexchar = Hex$(AscW(Mid$(str, X, 1))) If Len(hexchar) = 1 Then hexchar = "0" & hexchar StringToHex = StringToHex & hexchar & " " Next X End Function '/////////////////////////////////////////////////////////////////////// Function EnableRadioBoxes(intType As Integer) If (intType = objConstants.nwSNMP_TYPE_OCTETSTRING) Then optHex.Enabled = True optAscii.Enabled = True Else optHex.Enabled = False optAscii.Enabled = False End If End Function '/////////////////////////////////////////////////////////////////////// Private Function SetDefaultLogFile() Dim Buffer As String Buffer = Space(MAX_PATH) If GetTempPath(MAX_PATH, Buffer) <> 0 Then txtLogFile.Text = Left$(Buffer, InStr(Buffer, vbNullChar) - 1) & "Snmp.log" Else txtLogFile.Text = "C:\Snmp.log" End If End Function '/////////////////////////////////////////////////////////////////////// Private Function ShowResult() ShowResult = objSnmpManager.LastError txtResult.Text = ShowResult & ": " & objSnmpManager.GetErrorDescription(ShowResult) End Function '/////////////////////////////////////////////////////////////////////// Private Sub EnableControls() btnOpen.Enabled = Not bOpen btnClose.Enabled = bOpen txtOID.Enabled = bOpen txtOIDName.Enabled = bOpen txtType.Enabled = bOpen txtValue.Enabled = bOpen txtNewValue.Enabled = bOpen btnGet.Enabled = bOpen btnGetNext.Enabled = bOpen btnSet.Enabled = bOpen optAscii.Enabled = bOpen optHex.Enabled = bOpen End Sub '/////////////////////////////////////////////////////////////////////// Private Sub GetType() txtType.Text = "" Select Case lCurrentType Case objConstants.nwSNMP_TYPE_BITS txtType.Text = "ASN_BITS" Case objConstants.nwSNMP_TYPE_COUNTER32 txtType.Text = "ASN_COUNTER32" Case objConstants.nwSNMP_TYPE_COUNTER64 txtType.Text = "ASN_COUNTER64" Case objConstants.nwSNMP_TYPE_TIMETICKS txtType.Text = "ASN_TIMETICKS" Case objConstants.nwSNMP_TYPE_OCTETSTRING txtType.Text = "ASN_OCTETSTRING" Case objConstants.nwSNMP_TYPE_GAUGE32 txtType.Text = "ASN_GAUGE32" Case objConstants.nwSNMP_TYPE_IPADDRESS txtType.Text = "ASN_IPADDRESS" Case objConstants.nwSNMP_TYPE_OPAQUE txtType.Text = "ASN_OPAQUE" Case objConstants.nwSNMP_TYPE_UNSIGNED32 txtType.Text = "ASN_UNSIGNED32" Case objConstants.nwSNMP_TYPE_OBJECTIDENTIFIER txtType.Text = "ASN_OBJECTIDENTIFIER" Case objConstants.nwSNMP_TYPE_NULL txtType.Text = "ASN_NULL" Case objConstants.nwSNMP_TYPE_INTEGER txtType.Text = "ASN_INTEGER" Case objConstants.nwSNMP_TYPE_INTEGER32 txtType.Text = "ASN_INTEGER32" Case objConstants.nwSNMP_TYPE_SEQUENCE txtType.Text = "ASN_SEQUENCE" End Select End Sub '/////////////////////////////////////////////////////////////////////// Private Sub optAscii_Click() optHex.Value = False If (Trim(txtValue.Text) <> "") Then btnGet_Click End If End Sub '/////////////////////////////////////////////////////////////////////// Private Sub optHex_Click() optAscii.Value = False If (Trim(txtValue.Text) <> "") Then btnGet_Click End If End Sub
You can download the complete samples here. There are many other working Network Component scripts on our site and shipped with the product.
NOTE: Demo Projects are created with Microsoft Visual Studio 2008
The Network Component project ships with a set of Microsoft Visual Studio .NET samples, including samples for Microsoft Visual C# .NET. The projects are created with Microsoft Visual Studio 2008.
Users with a later version of Microsoft Visual Studio can open such a project. The Visual Studio Conversion Wizard will guide you through the process of converting the project to the version used.