Contact Info

Crumbtrail

ActiveXperts.com » Network Component » How to Use Network Component » SMTP Trap Sender » Visual Basic 6

How to use SNMP Trap Sender 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.


Network Component is compliant with SNMP v1 and SNMP v2c. Several SNMP data types are supported, including:

Network Component SNMP traps features:


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 objSnmpTrapManager As AxNetwork.SnmpTrapManager

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 objSnmpTrapManager		 = CreateObject("AxNetwork.SnmpTrapManager")

Appendix: Full source code

Option Explicit
Dim objManagerI As SnmpTrapManager
Dim objManagerO As SnmpTrapManager

Dim objConstants As NwConstants
Dim bListening As Boolean

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 objManagerI = CreateObject("AxNetwork.SnmpTrapManager")  ' Incoming Traps
    Set objManagerO = CreateObject("AxNetwork.SnmpTrapManager")  ' Outgoing Traps
    
    Set objConstants = CreateObject("AxNetwork.NwConstants")
    
    objManagerI.Initialize
    objManagerO.Initialize
    
    cmbVersion.AddItem ("V1")
    cmbVersion.AddItem ("V2C")
    
    cmbVersion.ListIndex = 1
    
    cmbType.AddItem ("ASN_INTEGER")
    cmbType.AddItem ("ASN_BITS")
    cmbType.AddItem ("ASN_OCTETSTRING")
    cmbType.AddItem ("ASN_NULL")
    cmbType.AddItem ("ASN_OBJECTIDENTIFIER")
    cmbType.AddItem ("ASN_INTEGER32")
    cmbType.AddItem ("ASN_SEQUENCE")
    cmbType.AddItem ("ASN_IPADDRESS")
    cmbType.AddItem ("ASN_COUNTER32")
    cmbType.AddItem ("ASN_GAUGE32")
    cmbType.AddItem ("ASN_TIMETICKS")
    cmbType.AddItem ("ASN_OPAQUE")
    cmbType.AddItem ("ASN_COUNTER64")
    cmbType.AddItem ("ASN_UNSIGNED32")
    cmbType.ListIndex = 2

    cmbGenericV1Trap.AddItem ("Cold Start")
    cmbGenericV1Trap.AddItem ("Warm Start")
    cmbGenericV1Trap.AddItem ("Link Down")
    cmbGenericV1Trap.AddItem ("Link Up")
    cmbGenericV1Trap.AddItem ("Authentication Failure")
    cmbGenericV1Trap.AddItem ("egpNeighborLoss")
    cmbGenericV1Trap.AddItem ("Enterprise Specific")
    cmbGenericV1Trap.AddItem ("")
    cmbGenericV1Trap.ListIndex = 0
    
    bListening = False
    
    SetDefaultLogFile
    
    EnableControls
    
End Sub

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

Private Sub btnSend_Click()
    Dim objSnmpTrap As SnmpTrap
    Dim objSnmpObject As SnmpObject
        
    Set objSnmpTrap = CreateObject("AxNetwork.SnmpTrap")
    Set objSnmpObject = CreateObject("AxNetwork.SnmpObject")
    
    objSnmpObject.OID = txtOID.Text
    objSnmpObject.Type = GetType
    objSnmpObject.Value = txtValue.Text
    
    objSnmpTrap.Clear
    objSnmpTrap.Community = txtCommunity.Text
    objSnmpTrap.Host = txtAgent.Text
    objSnmpTrap.AddObject objSnmpObject
    objSnmpTrap.Port = CInt(txtPort.Text)
    
    If (cmbVersion.Text = "V1") Then
        objSnmpTrap.GenericTrap = cmbGenericV1Trap.ListIndex
        objSnmpTrap.SpecificTrap = IsNumeric(txtSpecificV1Trap.Text)
    End If
    
    objManagerO.LogFile = txtLogFile.Text
    objManagerO.ProtocolVersion = cmbVersion.ListIndex + 1
    
    objManagerO.Send objSnmpTrap
        
    Set objSnmpObject = Nothing
    Set objSnmpTrap = Nothing
    
    ShowResultO
End Sub

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

Private Sub btnStartListening_Click()
    objManagerI.LogFile = txtLogFile.Text
    objManagerI.StartListening txtCommunityIn.Text, CInt(txtPortIn.Text)
    If (ShowResultI = 0) Then
        Timer.Enabled = True
        bListening = True
        EnableControls
    End If
    
End Sub

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

Private Sub btnStopListening_Click()
    objManagerI.StopListening
    If (ShowResultI = 0) Then
        Timer.Enabled = False
        bListening = False
        EnableControls
    End If
End Sub

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

Private Sub Timer_Timer()
    Dim objSnmpTrap As SnmpTrap
    Dim objSnmpObject As SnmpObject
    
    On Error Resume Next
    Set objSnmpTrap = objManagerI.GetFirstTrap
    
    While ShowResultI = 0
    
        Set objSnmpObject = objSnmpTrap.GetFirstObject
        
        While objSnmpTrap.LastError = 0
        
            Dim objItem As ListItem
            
            Set objItem = lvTraps.ListItems.Add(, , objSnmpTrap.Host)     ' Add data to list control
            
            objItem.SubItems(1) = objSnmpObject.OID
            objItem.SubItems(2) = GetTypeString(objSnmpObject.Type)
            objItem.SubItems(3) = objSnmpObject.Value
            
            Set objSnmpObject = objSnmpTrap.GetNextObject

        Wend
        
        Set objSnmpTrap = objManagerI.GetNextTrap
    Wend
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 Sub cmbVersion_Click()
    EnableControls
End Sub

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

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) & "SnmpTrap.log"
Else
    txtLogFile.Text = "C:\SnmpTrap.log"
End If
End Function

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

Private Sub cmbGenericV1Trap_Click()
    EnableControls
End Sub

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

Private Function GetType()
    Select Case cmbType.Text
    Case "ASN_INTEGER"
        GetType = objConstants.nwSNMP_TYPE_INTEGER
    Case "ASN_BUTS"
        GetType = objConstants.nwSNMP_TYPE_BITS
    Case "ASN_OCTETSTRING"
        GetType = objConstants.nwSNMP_TYPE_OCTETSTRING
    Case "ASN_NULL"
        GetType = objConstants.nwSNMP_TYPE_NULL
    Case "ASN_OBJECTIDENTIFIER"
        GetType = objConstants.nwSNMP_TYPE_OBJECTIDENTIFIER
    Case "ASN_INTEGER32"
        GetType = objConstants.nwSNMP_TYPE_INTEGER32
    Case "ASN_SEQUENCE"
        GetType = objConstants.nwSNMP_TYPE_SEQUENCE
    Case "ASN_IPADDRESS"
        GetType = objConstants.nwSNMP_TYPE_IPADDRESS
    Case "ASN_COUNTER32"
        GetType = objConstants.nwSNMP_TYPE_COUNTER32
    Case "ASN_GAUGE32"
        GetType = objConstants.nwSNMP_TYPE_GAUGE32
    Case "ASN_TIMETICKS"
        GetType = objConstants.nwSNMP_TYPE_TIMETICKS
    Case "ASN_OPAQUE"
        GetType = objConstants.nwSNMP_TYPE_OPAQUE
    Case "ASN_COUNTER64"
        GetType = objConstants.nwSNMP_TYPE_COUNTER64
    Case "ASN_UNSIGNED32"
        GetType = objConstants.nwSNMP_TYPE_UNSIGNED32
    End Select
End Function

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

Private Function GetTypeString(lType As Long)

    GetTypeString = ""
    
    Select Case lType
    Case objConstants.nwSNMP_TYPE_BITS
        GetTypeString = "ASN_BITS"
    Case objConstants.nwSNMP_TYPE_COUNTER32
        GetTypeString = "ASN_COUNTER32"
    Case objConstants.nwSNMP_TYPE_COUNTER64
        GetTypeString = "ASN_COUNTER64"
    Case objConstants.nwSNMP_TYPE_TIMETICKS
        GetTypeString = "ASN_TIMETICKS"
    Case objConstants.nwSNMP_TYPE_OCTETSTRING
        GetTypeString = "ASN_OCTETSTRING"
    Case objConstants.nwSNMP_TYPE_GAUGE32
        GetTypeString = "ASN_GAUGE32"
    Case objConstants.nwSNMP_TYPE_IPADDRESS
        GetTypeString = "ASN_IPADDRESS"
    Case objConstants.nwSNMP_TYPE_OPAQUE
        GetTypeString = "ASN_OPAQUE"
    Case objConstants.nwSNMP_TYPE_UNSIGNED32
        GetTypeString = "ASN_UNSIGNED32"
    Case objConstants.nwSNMP_TYPE_OBJECTIDENTIFIER
        GetTypeString = "ASN_OBJECTIDENTIFIER"
    Case objConstants.nwSNMP_TYPE_NULL
        GetTypeString = "ASN_NULL"
    Case objConstants.nwSNMP_TYPE_INTEGER
        GetTypeString = "ASN_INTEGER"
    Case objConstants.nwSNMP_TYPE_INTEGER32
        GetTypeString = "ASN_INTEGER32"
    Case objConstants.nwSNMP_TYPE_SEQUENCE
        GetTypeString = "ASN_SEQUENCE"
    End Select
End Function

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

Private Function ShowResultI()
    ShowResultI = objManagerI.LastError
    txtResult.Text = ShowResultI & ": " & objManagerI.GetErrorDescription(ShowResultI)
End Function

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

Private Function ShowResultO()
    ShowResultO = objManagerO.LastError
    txtResult.Text = ShowResultO & ": " & objManagerO.GetErrorDescription(ShowResultO)
End Function

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

Private Sub btnClearList_Click()
    lvTraps.ListItems.Clear
End Sub

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

Private Sub EnableControls()
    btnStartListening.Enabled = Not bListening
    btnStopListening.Enabled = bListening
    
    If (cmbVersion.Text = "V2C") Then
        lblSpecificTrap.Visible = False
        lblGenericV1Trap.Visible = False
        cmbGenericV1Trap.Visible = False
        txtSpecificV1Trap.Visible = False
    Else
        lblGenericV1Trap.Visible = True
        cmbGenericV1Trap.Visible = True
        If (cmbGenericV1Trap.Text = "Enterprise Specific") Then
            lblSpecificTrap.Visible = True
            txtSpecificV1Trap.Visible = True
        Else
            lblSpecificTrap.Visible = False
            txtSpecificV1Trap.Visible = False
        End If
    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.