Contact Info

Crumbtrail

ActiveXperts.com » Network Component » How to Use Network Component » SNMP MIB » Visual Basic 6

How to use SNMP MIB Browser 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:


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':

Visual Basic

(Click on the picture to enlarge)

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 on the picture to enlarge)

Click 'OK' to close the 'References...' dialog.

Then, select the Project form and choose 'View Code' from the context menu:

(Click on the picture to enlarge)

On top of your code, declare the following object:

Public objSnmpMibBrowser As AxNetwork.SnmpMibBrowser

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 objSnmpMibBrowser		 = CreateObject("AxNetwork.SnmpMibBrowser")

Appendix: Full source code

Option Explicit
Public objMibBrowser As SnmpMibBrowser
Public objConstants As NwConstants

'///////////////////////////////////////////////////////////////////////

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 objMibBrowser = CreateObject("AxNetwork.SnmpMibBrowser")
  Set objConstants = CreateObject("AxNetwork.NwConstants")
     
  SetDefaultLogFile
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
  
  txtFile.Text = dlgOpenFile.FileName
End Sub

'///////////////////////////////////////////////////////////////////////

Private Sub btnClose_Click()
  ClearTree
  ClearFields
End Sub

'///////////////////////////////////////////////////////////////////////

Private Sub btnOpen_Click()
  ClearTree
  ClearFields
      
  objMibBrowser.LogFile = txtLogfile.Text
  objMibBrowser.LoadMibFile (txtFile.Text)
  
  If (GetResult() = 0) Then
      BuildMibTree
  End If
End Sub

'///////////////////////////////////////////////////////////////////////

Private Sub BuildMibTree()
    Dim objNode As SnmpObject
    
    Dim t1 As Node
    Dim troot As Node
       
    Dim strParent As String
        
    Set objNode = objMibBrowser.Get("iso.org.dod.internet")
          
    While (objMibBrowser.LastError = 0)
    
        If (InStrRev(objNode.OIDNameShort, ".")) Then
            strParent = Left(objNode.OIDNameShort, InStrRev(objNode.OIDNameShort, ".") - 1)
            Set t1 = tvMIB.Nodes.Add(strParent, tvwChild, objNode.OIDNameShort, objNode.OIDName)
        Else
            Set t1 = tvMIB.Nodes.Add(, , objNode.OIDNameShort, objNode.OIDName)
        End If
    
        t1.Expanded = True
        t1.Tag = objNode.OID
    
        Set objNode = objMibBrowser.GetNext
    Wend
End Sub

'///////////////////////////////////////////////////////////////////////

Private Sub tvMIB_NodeClick(ByVal Node As MSComctlLib.Node)
    Dim objNode As SnmpObject
    
    ClearFields
    
    Set objNode = objMibBrowser.Get(Node.Tag)
    
    txtOid.Text = objNode.OID
    txtDescription.Text = objNode.Description
    txtSyntax.Text = objNode.Syntax
    txtName.Text = objNode.OIDName
        
     If (objNode.Access = objConstants.nwMIB_ACCESS_NOACCESS) Then
            txtAccess.Text = "NOACCESS"
        ElseIf (objNode.Access = objConstants.nwMIB_ACCESS_NOTIFY) Then
            txtAccess.Text = "NOTIFY"
        ElseIf (objNode.Access = objConstants.nwMIB_ACCESS_READONLY) Then
            txtAccess.Text = "READONLY"
        ElseIf (objNode.Access = objConstants.nwMIB_ACCESS_WRITEONLY) Then
            txtAccess.Text = "WRITEONLY"
        ElseIf (objNode.Access = objConstants.nwMIB_ACCESS_READWRITE) Then
            txtAccess.Text = "READWRITE"
        ElseIf (objNode.Access = objConstants.nwMIB_ACCESS_READCREATE) Then
            txtAccess.Text = "READCREATE"
        Else
            txtAccess.Text = "n/a"
        End If

        If (objNode.Status = objConstants.nwMIB_STATUS_CURRENT) Then
            txtStatus.Text = "CURRENT"
        ElseIf (objNode.Status = objConstants.nwMIB_STATUS_DEPRECATED) Then
            txtStatus.Text = "DEPRECATED"
        ElseIf (objNode.Status = objConstants.nwMIB_STATUS_OBSOLETE) Then
            txtStatus.Text = "OBSOLETE"
        ElseIf (objNode.Status = objConstants.nwMIB_STATUS_MANDATORY) Then
            txtStatus.Text = "MANDATORY"
        Else
            txtStatus.Text = "n/a"
        End If
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

'///////////////////////////////////////////////////////////////////////

Private Function GetResult() As Long
    Dim lResult As Long

    lResult = objMibBrowser.LastError
    txtResult.Text = lResult & ": " & objMibBrowser.GetErrorDescription(lResult)
   
    GetResult = lResult
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) & "MibBrowser.log"
Else
    txtLogfile.Text = "C:\MibBrowser.log"
End If
End Function

'///////////////////////////////////////////////////////////////////////

Private Sub ClearTree()
    tvMIB.Nodes.Clear
End Sub

'///////////////////////////////////////////////////////////////////////

Private Sub ClearFields()
    txtOid.Text = ""
    txtDescription.Text = ""
    txtSyntax.Text = ""
    txtAccess.Text = ""
    txtStatus.Text = ""
    txtName.Text = ""
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.