www.pudn.com > NetMgrSamp1.rar > SnmpV1.cls


VERSION 1.0 CLASS 
BEGIN 
  MultiUse = -1  'True 
  Persistable = 0  'NotPersistable 
  DataBindingBehavior = 0  'vbNone 
  DataSourceBehavior  = 0  'vbNone 
  MTSTransactionMode  = 0  'NotAnMTSObject 
END 
Attribute VB_Name = "SnmpV1" 
Attribute VB_GlobalNameSpace = False 
Attribute VB_Creatable = True 
Attribute VB_PredeclaredId = False 
Attribute VB_Exposed = True 
Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes" 
Attribute VB_Ext_KEY = "Top_Level" ,"Yes" 
' ========================================================== 
' Module Name: SnmpV1 
' Create Date: 08/02/2002 
' Module Goal: Expose modSNMPAPI functions through class 
'              interface 
'  Created By: Benjamin Garcia 
'  Add'l Info: Please feel free to contact me with any 
'              questions or comments at bgarcia@personify.ws 
'              Module Reference: NetMgrVB.SnmpV1 
' ========================================================== 
 
Private lcSession As Long 
 
' ========================================================== 
' "Open" session to agent 
' ========================================================== 
Public Function SnmpMgrOpen() As String 
    ' Validate session isn't open and appropriate data has 
    ' been assigned to agentSettings struct 
    
    Select Case True 
        ' Properties haven't been initialized 
        Case (agentSettings.agtAddr = "" Or agentSettings.CommStr = "") 
            SnmpMgrOpen = "[NetMgrVB.SnmpV1.SnmpMgrOpen][MV]=You must set both the Agent and CommStr properties before you can open a session." 
         
        ' Pre-existing session encountered 
        Case (lSession <> 0) 
            SnmpMgrOpen = "[NetMgrVB.SnmpV1.SnmpMgrOpen][OS]=You must close your existing session before opening another one." 
         
        ' Good to go! 
        Case Else 
            SnmpMgrOpen = SnmpOpenSession(lcSession) 
    End Select 
End Function 
 
' ========================================================== 
' "Close" session to agent 
' ========================================================== 
Public Function SnmpMgrClose() As String 
    ' Verify if the session is already closed 
    If lcSession = 0 Then 
        ' Session is already closed -- no need to run through process 
        SnmpMgrClose = "[NetMgrVB.SnmpV1.SnmpMgrClose][CS]=No session currently initialized to close." 
    Else 
        ' Session is active -- run SnmpCloseSession 
        SnmpMgrClose = SnmpCloseSession(lcSession) 
    End If 
End Function 
 
' ========================================================== 
' Request OID value from Agent and return response 
' ========================================================== 
Public Function SnmpGet(ByVal strOID As String) As String 
    ' Local variable declarations 
    ' --------------------------- 
    Dim strRetVal As String 
 
    ' Make request to agent 
    strRetVal = initAgentProc(lcSession, SNMP_PDU_GET, strOID, "") 
     
    ' Return agent response 
    SnmpGet = strRetVal 
End Function 
 
' ========================================================== 
' Set OID value to Agent and return response 
' ========================================================== 
Public Function SnmpGetNext(strOID As String) As String 
    ' Local variable declarations 
    ' --------------------------- 
    Dim strRetVal As String 
 
    ' Make request to agent 
    strRetVal = initAgentProc(lcSession, SNMP_PDU_GETNEXT, strOID, "") 
     
    ' =Re-Assign Next OID 
    ' == If preservation of the incoming oid is an issue then you can do something like: 
    '    SnmpGetNext(byVal strInOID as String, strOutOID as String) as String 
    strOID = strReturnOID 
     
    ' Return agent response 
    SnmpGetNext = strRetVal 
End Function 
 
' ========================================================== 
' Request that OID value change and return response 
' ========================================================== 
Public Function SnmpSet(ByVal strOID As String, ByVal varOIDValue As Variant, Optional ByVal intSetTypeOverride As Integer = 99) As String 
On Error GoTo errorHandler 
 
    ' Local variable declarations 
    ' --------------------------- 
    Dim strRetVal As String 
     
    ' Make request to agent 
    Select Case intSetTypeOverride 
        Case 0      ' Setting oid set value to string 
            strRetVal = initAgentProc(lcSession, SNMP_PDU_SET, strOID, CStr(varOIDValue)) 
         
        Case 1      ' Setting oid set value to numeric 
            strRetVal = initAgentProc(lcSession, SNMP_PDU_SET, strOID, CLng(varOIDValue)) 
         
        Case 99     ' Auto oid set value detection put in place 
            strRetVal = initAgentProc(lcSession, SNMP_PDU_SET, strOID, varOIDValue) 
         
        Case Else   ' Invalid argument passed 
            strRetVal = "[NetMgrVB.SnmpV1.SnmpSet][IST]=An invalid Set Type Override value has been encountered.  Valid Values are 0 for String Override and 1 for Numeric Override." 
    End Select 
     
    ' Return agent response 
    SnmpSet = strRetVal 
     
    ' Done! 
    Exit Function 
     
errorHandler: 
    SnmpSet = "[NetMgrVB.SnmpV1.SnmpSet][" & Err.Number & "]=" & Err.Description 
End Function 
 
' ========================================================== 
' Total number of retries before returning error 
' ========================================================== 
Public Property Let Retry(ByVal vData As Integer) 
    agentSettings.Retries = vData 
End Property 
 
Public Property Get Retry() As Integer 
    Retry = agentSettings.Retries 
End Property 
 
' ========================================================== 
' Total number of ms before returning error 
' ========================================================== 
Public Property Let Timeout(ByVal vData As Integer) 
    agentSettings.Timeout = vData 
End Property 
 
Public Property Get Timeout() As Integer 
    Timeout = agentSettings.Timeout 
End Property 
 
' ========================================================== 
' Community string to be used to communicate with Agent 
' ========================================================== 
Public Property Let CommStr(ByVal vData As String) 
    agentSettings.CommStr = vData 
End Property 
 
Public Property Get CommStr() As String 
    CommStr = agentSettings.CommStr 
End Property 
 
' ========================================================== 
' Address of Agent (i.e. "localhost", "10.1.15.8") 
' ========================================================== 
Public Property Let Agent(ByVal vData As String) 
    agentSettings.agtAddr = vData 
End Property 
 
Public Property Get Agent() As String 
    Agent = agentSettings.agtAddr 
End Property 
 
' ========================================================== 
' Class Initialization statements 
' ========================================================== 
Private Sub Class_Initialize() 
    With agentSettings 
        .Timeout = 2000 
        .Retries = 3 
        .mgrAddr = "127.0.0.1" 
    End With 
End Sub