You are here:

ActiveXperts.com > ActiveSocket > How to Use ActiveSocket > SNMP > Visual Basic 5.x/6.x

ActiveSocket Toolkit Add network capabilities to any Windows or .NET application

Quicklinks


Visual Basic 5.x/6.x SNMP Sample Source Code

ActiveSocket provides an easy-to-use development interface to a variety of IP protocols. By using ActiveSocket, you can very easily create or enhance applications with network features.

ActiveSocket 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.

SNMP can be well integrated into Borland Visual Basic environments. This document describes how ActiveSocket's SNMP objects can be integrated into Visual Basic projects.

ActiveSocket is compliant with SNMP v1 and SNMP v2c. Different SNMP data types are supported, including:

  • String types (also called "octet strings");
  • Integer types (16bit, 32bit, 64bit and unsigned integers);
  • IP Address types;
  • Timetick types;
  • Counter types (32bit and 64bit counters);
  • OID types (also called "Object ID's");
  • Other, less frequently used datatypes.

The following operations are supported:

  • Get - retrieve an object variable from the (remote) agent;
  • GetNext - retrieve the next object variable from a table or list within an agent;
  • Set - set values for object variables within an agent.

Step 1: Download and install the ActiveSocket Toolkit

Download the ActiveSocket Toolkit 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 ActiveSocket Library and create the objects

A new Project is created, with a blank form.

First, you must add a reference to ActiveSocket 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 'ActiveSocket 3.1 Type Library' reference as shown in the following picture:

Visual Basic

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

Visual Basic

(Click on the picture to enlarge)

On top of your code, declare the following object:

Public objSnmpManager As SnmpManager
Public objConstants As SocketConstants

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 objSnmpManager = CreateObject("ActiveXperts.SnmpManager")
Set objConstants   = CreateObject("ActiveXperts.ASConstants")

Step 5: Creating a simple SNMP manager application

When the objects have been declared and created, you can add the rest of the code to build a simple SNMP manager application. You can find the sourcecode of this application below:

Option Explicit

Dim objSnmpManager As SnmpManager
Dim objSnmpObject As SnmpObject
Dim objConstants As SocketConstants

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 CommandClose_Click()
    objSnmpManager.Close
    
    bOpen = False
    
    EnableControls
End Sub

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

Private Sub CommandGet_Click()
    
    If bOpen = True Then
        MousePointer = vbHourglass
        Set objSnmpObject = objSnmpManager.Get(TextOID.Text)
        If ShowResult = 0 Then
            TextOID = objSnmpObject.OID
            TextValue = objSnmpObject.Value
            lCurrentType = objSnmpObject.Type
            GetType
        End If
        Set objSnmpObject = Nothing
        MousePointer = vbDefault
    End If
    
End Sub

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

Private Sub CommandGetNext_Click()
    If bOpen = True Then
        MousePointer = vbHourglass
        Set objSnmpObject = objSnmpManager.GetNext()
        If ShowResult = 0 Then
            TextOID      = objSnmpObject.OID
            TextValue    = objSnmpObject.Value
            lCurrentType = objSnmpObject.Type
            GetType
        End If
        MousePointer = vbDefault
    End If
    
End Sub

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

Private Sub CommandOpen_Click()
    objSnmpManager.LogFile = TextLogFile.Text
    objSnmpManager.Open TextAgent.Text, TextCommunity.Text, CInt(TextPort.Text)
    objSnmpManager.ProtocolVersion = ComboVersion.ListIndex + 1
    If ShowResult = 0 Then bOpen = True
    
    EnableControls
End Sub

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

Private Sub CommandSet_Click()
    If bOpen = True Then
        MousePointer = vbHourglass
        Set objSnmpObject = CreateObject("ActiveXperts.SnmpObject")
        
        objSnmpObject.Clear
        objSnmpObject.Type = lCurrentType
        objSnmpObject.Value = TextNewValue.Text
        objSnmpObject.OID = TextOID.Text
        
        objSnmpManager.Set objSnmpObject
        MousePointer = vbDefault
        ShowResult
    End If
End Sub

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

Private Sub CommandView_Click()
    If FileExists(TextLogFile.Text) = True Then
        Shell "notepad " + TextLogFile.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 SetDefaultLogFile()

Dim Buffer As String
Buffer = Space(MAX_PATH)

If GetTempPath(MAX_PATH, Buffer) <> 0 Then
    TextLogFile.Text = Left$(Buffer, InStr(Buffer, vbNullChar) - 1) & "SnmpLog.txt"
Else
    TextLogFile.Text = "C:\SnmpLog.txt"
End If
End Function

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

Private Sub Form_Load()
    Set objSnmpManager = CreateObject("ActiveXperts.SnmpManager")
    Set objConstants = CreateObject("ActiveXperts.ASConstants")
    
    ComboVersion.AddItem ("V1")
    ComboVersion.AddItem ("V2C")
    ComboVersion.ListIndex = 1
    
    objSnmpManager.Initialize
    
    SetDefaultLogFile
    
    EnableControls
    
End Sub

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

Private Function ShowResult()
    ShowResult = objSnmpManager.LastError
    TextResult.Text = ShowResult & " : " & objSnmpManager.GetErrorDescription(ShowResult)
End Function

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

Private Sub EnableControls()
    CommandOpen.Enabled = Not bOpen
    CommandClose.Enabled = bOpen
    TextOID.Enabled = bOpen
    TextType.Enabled = bOpen
    TextValue.Enabled = bOpen
    TextNewValue.Enabled = bOpen
    CommandGet.Enabled = bOpen
    CommandGetNext.Enabled = bOpen
    CommandSet.Enabled = bOpen
End Sub

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

Private Sub GetType()
    TextType.Text = ""
    
    Select Case lCurrentType
    Case objConstants.asSNMP_TYPE_BITS
        TextType.Text = "ASN_BITS"
    Case objConstants.asSNMP_TYPE_COUNTER32
        TextType.Text = "ASN_COUNTER32"
    Case objConstants.asSNMP_TYPE_COUNTER64
        TextType.Text = "ASN_COUNTER64"
    Case objConstants.asSNMP_TYPE_TIMETICKS
        TextType.Text = "ASN_TIMETICKS"
    Case objConstants.asSNMP_TYPE_OCTETSTRING
        TextType.Text = "ASN_OCTETSTRING"
    Case objConstants.asSNMP_TYPE_GAUGE32
        TextType.Text = "ASN_GAUGE32"
    Case objConstants.asSNMP_TYPE_IPADDRESS
        TextType.Text = "ASN_IPADDRESS"
    Case objConstants.asSNMP_TYPE_OPAQUE
        TextType.Text = "ASN_OPAQUE"
    Case objConstants.asSNMP_TYPE_UNSIGNED32
        TextType.Text = "ASN_UNSIGNED32"
    Case objConstants.asSNMP_TYPE_OBJECTIDENTIFIER
        TextType.Text = "ASN_OBJECTIDENTIFIER"
    Case objConstants.asSNMP_TYPE_NULL
        TextType.Text = "ASN_NULL"
    Case objConstants.asSNMP_TYPE_INTEGER
        TextType.Text = "ASN_INTEGER"
    Case objConstants.asSNMP_TYPE_INTEGER32
        TextType.Text = "ASN_INTEGER32"
    Case objConstants.asSNMP_TYPE_SEQUENCE
        TextType.Text = "ASN_SEQUENCE"
    End Select
End Sub

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

You can download the complete sample on our ftp site ftp.activexperts-labs.com/samples/network-component. There are many other working ActiveSocket scripts on our site and shipped with the product.