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:
The following operations are supported:
Download the ActiveSocket Toolkit from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.
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':
(Click on the picture to enlarge)
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:
(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 objSnmpManager As SnmpManager Public objConstants As SocketConstants
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")
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/asocket. There are many other working ActiveSocket scripts on our site and shipped with the product.