This guide shows how to use Gurux SNMP component (GXSNMP) in the Visual
Basic environment. The development version of GXSNMP comes with a Visual
Basic example. Source code is available at sourceforge.net
Adding GXSNMP component to the project
- Start Visual Basic
- Open the project where you want to add the GXSNMP component, or create a new one
- Select Components from the Project menu
- Select the GXSNMP component and click OK to finish

Using components with Visual Basic
You can either drag the component to the form, or create it dynamically.

Creating component dynamically
To create component in runtime, select References in the Project menu.
The dialog shows available components, select the one(s) to use (in this case Gurux SNMP component). Click
OK to finish. The following example shows how to create a GXSNMP component.
Dim WithEvents GXSNMP1 As GXSNMP 'Create new component Set GXSNMP1= New GXSNMP
Note: WithEvents means that component can receive asynchronous calls.
Setting Media Properties
You can either select appropriate settings in the Properties dialog or edit the settings programmatically.
'Set server port and TCP/IP address. GXSNMP1.SNMPPort = 161 GXSNMP1.HostName = "Localhost" GXSNMP1.Connect Dim pack As New GXSNMPPacket pack.Command = GX_SNMP_COMMAND_GET pack.OID = "1.3.6.1.2.1.1.1.0" GXSNMP1.Send pack, "localhost"
Sending and receiving packets
All data is sent asynchronously. Response is received as a Received event message.
Receiving event messages
Because the GXSMS1 variable was introduced as the WithEvents type, an incoming event
message from the system can be received very easily. The following example shows how to
create a handler for the Received method.
Select GXSNMP1 in the left menu above the code window. In the right menu, select the event message,
on which you want to make the handler (OnReceived). Code window then displays following:
Private Sub GXSNMP1_OnReceived(var As Variant, ByVal SenderInfo As String)
Dim pack As GXSNMPPacket
Set pack = data
Dim pos As Integer
For pos = 0 To pack.DataItems.Count - 1
'Show received data.
MsgBox pack.DataItems(pos).OID & ": "& pack.DataItems(pos).Data
Next
End Sub
Now create handler for OnError event.
Error Handling
It's very important to consider error handling when sending and receiving data.
The connection can drop down, or something else unexpected may happen. If something
unwanted happens, Gurux SNMP component returns error.The following example shows
how to handle errors in Visual Basic.
Sub SendData(Data As Variant)
On Error GoTo GXErrHandler
'Send Data
GXSNMP1.Send Data
Exit Sub
GXErrHandler:
'Show occurred error
MsgBox Err.Description
End Sub
Using the components from development environment
Using from .NET | Using from Visual Basic | Using from C++