www.pudn.com > NetMgrSamp1.rar > modSNMPAPI.bas
Attribute VB_Name = "modSNMPAPI"
' ==========================================================
' Module Name: modSNMPAPI
' Create Date: 08/02/2002
' Module Goal: Expose functions and header definitions for
' both the SnmpApi.dll and MgmtApi.dll to the
' VB environment and illustrate how to use
' them (mimic functionality of NetMgr component
' 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.Mod
' ==========================================================
' ==========================================================
' ::= Begin Snippets from Snmp.h header file
' ==========================================================
' /////////////////////////////////////////////////
' ASN/BER Base Types
' /////////////////////////////////////////////////
Private Const ASN_UNIVERSAL = &H0
Private Const ASN_APPLICATION = &H40
Private Const ASN_CONTEXT = &H80
Private Const ASN_PRIVATE = &HC0
Private Const ASN_PRIMITIVE = &H0
Private Const ASN_CONSTRUCTOR = &H20
' /////////////////////////////////////////////////
' PDU Types -- Public Definition
' /////////////////////////////////////////////////
Public Const SNMP_PDU_GET = ASN_CONTEXT Or ASN_CONSTRUCTOR Or 0 ' 0x0
Public Const SNMP_PDU_GETNEXT = ASN_CONTEXT Or ASN_CONSTRUCTOR Or 1 ' 0x1
Public Const SNMP_PDU_SET = ASN_CONTEXT Or ASN_CONSTRUCTOR Or 3 ' 0x3
' /////////////////////////////////////////////////
' SNMP Simple Syntax Values
' /////////////////////////////////////////////////
Private Const ASN_INTEGER = ASN_UNIVERSAL Or ASN_PRIMITIVE Or 2 ' 0x02
Private Const ASN_BITS = ASN_UNIVERSAL Or ASN_PRIMITIVE Or 3 ' 0x03
Private Const ASN_OCTETSTRING = ASN_UNIVERSAL Or ASN_PRIMITIVE Or 4 ' 0x04
Private Const ASN_NULL = ASN_UNIVERSAL Or ASN_PRIMITIVE Or 5 ' 0x05
Private Const ASN_OBJECTIDENTIFIER = ASN_UNIVERSAL Or ASN_PRIMITIVE Or 6 ' 0x06
Private Const ASN_INTEGER32 = ASN_INTEGER
' /////////////////////////////////////////////////
' SNMP Constructor Syntax Values
' /////////////////////////////////////////////////
Private Const ASN_SEQUENCE = ASN_UNIVERSAL Or ASN_CONSTRUCTOR Or &H10
Private Const ASN_SEQUENCEOF = ASN_SEQUENCE
' /////////////////////////////////////////////////
' SNMP Application Syntax Values
' /////////////////////////////////////////////////
Private Const ASN_IPADDRESS = ASN_APPLICATION Or ASN_PRIMITIVE Or 0 ' 0x00
Private Const ASN_COUNTER32 = ASN_APPLICATION Or ASN_PRIMITIVE Or 1 ' 0x01
Private Const ASN_GAUGE32 = ASN_APPLICATION Or ASN_PRIMITIVE Or 2 ' 0x02
Private Const ASN_TIMETICKS = ASN_APPLICATION Or ASN_PRIMITIVE Or 3 ' 0x03
Private Const ASN_OPAQUE = ASN_APPLICATION Or ASN_PRIMITIVE Or 4 ' 0x04
Private Const ASN_COUNTER64 = ASN_APPLICATION Or ASN_PRIMITIVE Or 6 ' 0x06
Private Const ASN_UNSIGNED32 = ASN_APPLICATION Or ASN_PRIMITIVE Or 7 ' 0x07
' /////////////////////////////////////////////////
' SNMP Exception Values
' /////////////////////////////////////////////////
Private Const SNMP_EXCEPTION_NOSUCHOBJECT = ASN_CONTEXT Or ASN_PRIMITIVE Or 0
Private Const SNMP_EXCEPTION_NOSUCHINSTANCE = ASN_CONTEXT Or ASN_PRIMITIVE Or 1
Private Const SNMP_EXCEPTION_ENDOFMIBVIEW = ASN_CONTEXT Or ASN_PRIMITIVE Or 2
' /////////////////////////////////////////////////
' SNMP Error Codes
' /////////////////////////////////////////////////
Private Const SNMP_ERRORSTATUS_NOERROR = 0
Private Const SNMP_ERRORSTATUS_TOOBIG = 1
Private Const SNMP_ERRORSTATUS_NOSUCHNAME = 2
Private Const SNMP_ERRORSTATUS_BADVALUE = 3
Private Const SNMP_ERRORSTATUS_READONLY = 4
Private Const SNMP_ERRORSTATUS_GENERR = 5
Private Const SNMP_ERRORSTATUS_NOACCESS = 6
Private Const SNMP_ERRORSTATUS_WRONGTYPE = 7
Private Const SNMP_ERRORSTATUS_WRONGLENGTH = 8
Private Const SNMP_ERRORSTATUS_WRONGENCODING = 9
Private Const SNMP_ERRORSTATUS_WRONGVALUE = 10
Private Const SNMP_ERRORSTATUS_NOCREATION = 11
Private Const SNMP_ERRORSTATUS_INCONSISTENTVALUE = 12
Private Const SNMP_ERRORSTATUS_RESOURCEUNAVAILABLE = 13
Private Const SNMP_ERRORSTATUS_COMMITFAILED = 14
Private Const SNMP_ERRORSTATUS_UNDOFAILED = 15
Private Const SNMP_ERRORSTATUS_AUTHORIZATIONERROR = 16
Private Const SNMP_ERRORSTATUS_NOTWRITABLE = 17
Private Const SNMP_ERRORSTATUS_INCONSISTENTNAME = 18
' /////////////////////////////////////////////////
' Type Definitions -- Please note that these will
' not match up exactly because the struct in snmp.h
' could not be mirrored
' /////////////////////////////////////////////////
Private Type AsnVar
len As Long
ptr As Long
End Type
Private Type ContextOctetValue
len As Long
ptr As String
End Type
Private Type vbIntValue
vbType As Long
lngVal As Long
void As Long
End Type
Private Type sdCounter64
hiPart As Long
loPart As Long
End Type
Private Type vbOctetValue
vbType As Long
octetRef As AsnVar
End Type
Private Type vbOIDValue
vbType As Long
oidRef As AsnVar
End Type
Private Type vbCounter64Value
vbType As Long
intLarge As sdCounter64
End Type
' ==========================================================
' ::= End Snippets from Snmp.h header file
' ==========================================================
' ==========================================================
' ::= Begin Snippets from WinSnmp.h header file
' ==========================================================
' /////////////////////////////////////////////////
' "Utility" Constants
' /////////////////////////////////////////////////
Private Const SNMPAPI_SUCCESS = 1
Private Const SNMPAPI_FAILURE = 0
' /////////////////////////////////////////////////
' WinSnmp API Specific Error Codes returned from
' SnmpGetLastError
' /////////////////////////////////////////////////
Private Const SNMPAPI_ALLOC_ERROR = 2 ' Error allocating memory
Private Const SNMPAPI_CONTEXT_INVALID = 3 ' Invalid context parameter
Private Const SNMPAPI_CONTEXT_UNKNOWN = 4 ' Unknown context parameter
Private Const SNMPAPI_ENTITY_INVALID = 5 ' Invalid entity parameter
Private Const SNMPAPI_ENTITY_UNKNOWN = 6 ' Unknown entity parameter
Private Const SNMPAPI_INDEX_INVALID = 7 ' Invalid VBL index parameter
Private Const SNMPAPI_NOOP = 8 ' No operation performed
Private Const SNMPAPI_OID_INVALID = 9 ' Invalid OID parameter
Private Const SNMPAPI_OPERATION_INVALID = 10 ' Invalid/unsupported operation
Private Const SNMPAPI_OUTPUT_TRUNCATED = 11 ' Insufficient output buf len
Private Const SNMPAPI_PDU_INVALID = 12 ' Invalid PDU parameter
Private Const SNMPAPI_SESSION_INVALID = 13 ' Invalid session parameter
Private Const SNMPAPI_SYNTAX_INVALID = 14 ' Invalid syntax in smiVALUE
Private Const SNMPAPI_VBL_INVALID = 15 ' Invalid VBL parameter
Private Const SNMPAPI_MODE_INVALID = 16 ' Invalid mode parameter
Private Const SNMPAPI_SIZE_INVALID = 17 ' Invalid size/length parameter
Private Const SNMPAPI_NOT_INITIALIZED = 18 ' SnmpStartup failed/not called
Private Const SNMPAPI_MESSAGE_INVALID = 19 ' Invalid SNMP message format
Private Const SNMPAPI_HWND_INVALID = 20 ' Invalid Window handle
Private Const SNMPAPI_OTHER_ERROR = 99 ' For internal/undefined errors
' /////////////////////////////////////////////////
' Generic Transport Layer (TL) Errors
' /////////////////////////////////////////////////
Private Const SNMPAPI_TL_NOT_INITIALIZED = 100 ' TL not initialized
Private Const SNMPAPI_TL_NOT_SUPPORTED = 101 ' TL does not support protocol
Private Const SNMPAPI_TL_NOT_AVAILABLE = 102 ' Network subsystem has failed
Private Const SNMPAPI_TL_RESOURCE_ERROR = 103 ' TL resource error
Private Const SNMPAPI_TL_UNDELIVERABLE = 104 ' Destination unreachable
Private Const SNMPAPI_TL_SRC_INVALID = 105 ' Source endpoint invalid
Private Const SNMPAPI_TL_INVALID_PARAM = 106 ' Input parameter invalid
Private Const SNMPAPI_TL_IN_USE = 107 ' Source endpoint in use
Private Const SNMPAPI_TL_TIMEOUT = 108 ' No response before timeout
Private Const SNMPAPI_TL_PDU_TOO_BIG = 109 ' PDU too big for send/receive
Private Const SNMPAPI_TL_OTHER = 199 ' Undefined TL error
' ==========================================================
' ::= End Snippets from WindSnmp.h header file
' ==========================================================
' ==========================================================
' Local Application Specific Constants
' ==========================================================
Private Const APP_RESPONSE_TIME = 100
' ==========================================================
' Local Type Definitions
' ==========================================================
Private Type SnmpAgentSettings
mgrAddr As String
agtAddr As String
CommStr As String
Timeout As Integer
Retries As Integer
End Type
' ==========================================================
' API Function Declarations -- WSnmp32.dll
' ==========================================================
Private Declare Function SnmpGetTranslateMode Lib "wsnmp32.dll" (nTranslateMode As Long) As Long
Private Declare Function SnmpSetTranslateMode Lib "wsnmp32.dll" (ByVal nTranslateMode As Long) As Long
Private Declare Function SnmpGetRetransmitMode Lib "wsnmp32.dll" (nRetransmitMode As Long) As Long
Private Declare Function SnmpSetRetransmitMode Lib "wsnmp32.dll" (ByVal nRetransmitMode As Long) As Long
Private Declare Function SnmpGetTimeout Lib "wsnmp32.dll" (ByVal hEntity As Long, nPolicyTimeout As Long, nActualTimeout As Long) As Long
Private Declare Function SnmpSetTimeout Lib "wsnmp32.dll" (ByVal hEntity As Long, ByVal nPolicyTimeout As Long) As Long
Private Declare Function SnmpGetRetry Lib "wsnmp32.dll" (ByVal hEntity As Long, nPolicyRetry As Long, nActualRetry As Long) As Long
Private Declare Function SnmpSetRetry Lib "wsnmp32.dll" (ByVal hEntity As Long, ByVal nPolicyRetry As Long) As Long
Private Declare Function SnmpStartup Lib "wsnmp32.dll" (nMajorVersion As Long, nMinorVersion As Long, nLevel As Long, nTranslateMode As Long, nRetransmitMode As Long) As Long
Private Declare Function SnmpClose Lib "wsnmp32.dll" (ByVal session As Long) As Long
Private Declare Function SnmpCleanup Lib "wsnmp32.dll" () As Long
Private Declare Function SnmpSendMsg Lib "wsnmp32.dll" (ByVal session As Long, ByVal srcEntity As Long, ByVal dstEntity As Long, ByVal context As Long, ByVal pdu As Long) As Long
Private Declare Function SnmpRecvMsg Lib "wsnmp32.dll" (ByVal session As Long, srcEntity As Long, dstEntity As Long, context As Long, pdu As Long) As Long
Private Declare Function SnmpCreateSession Lib "wsnmp32.dll" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal fCallBack As Long, ByVal lpClientData As Long) As Long
Private Declare Function SnmpStrToEntity Lib "wsnmp32.dll" (ByVal session As Long, ByVal strEntity As String) As Long
Private Declare Function SnmpFreeEntity Lib "wsnmp32.dll" (ByVal entity As Long) As Long
Private Declare Function SnmpStrToContext Lib "wsnmp32.dll" (ByVal session As Long, strOctet As ContextOctetValue) As Long
Private Declare Function SnmpContextToStr Lib "wsnmp32.dll" (ByVal hContext As Long, smiOctets As AsnVar) As Long
Private Declare Function SnmpFreeContext Lib "wsnmp32.dll" (ByVal context As Long) As Long
Private Declare Function SnmpCreatePdu Lib "wsnmp32.dll" (ByVal session As Long, ByVal PDU_type As Long, ByVal request_id As Long, ByVal error_status As Long, ByVal error_index As Long, ByVal varbindlist As Long) As Long
Private Declare Function SnmpGetPduData Lib "wsnmp32.dll" (ByVal pdu As Long, PDU_type As Long, request_id As Long, error_status As Long, error_index As Long, varbindlist As Long) As Long
Private Declare Function SnmpSetPduData Lib "wsnmp32.dll" (ByVal pdu As Long, PDU_type As Long, request_id As Long, error_status As Long, error_index As Long, varbindlist As Long) As Long
Private Declare Function SnmpFreePdu Lib "wsnmp32.dll" (ByVal pdu As Long) As Long
Private Declare Function SnmpCreateVbl Lib "wsnmp32.dll" (ByVal session As Long, name As AsnVar, value As Any) As Long
Private Declare Function SnmpFreeVbl Lib "wsnmp32.dll" (ByVal vbl As Long) As Long
Private Declare Function SnmpCountVbl Lib "wsnmp32.dll" (ByVal vbl As Long) As Long
Private Declare Function SnmpGetVb Lib "wsnmp32.dll" (ByVal vbl As Long, ByVal index As Long, name As AsnVar, value As vbOctetValue) As Long
Private Declare Function SnmpSetVb Lib "wsnmp32.dll" (ByVal vbl As Long, ByVal index As Long, name As AsnVar, value As Any) As Long
Private Declare Function SnmpDeleteVb Lib "wsnmp32.dll" (ByVal vbl As Long, ByVal index As Long) As Long
Private Declare Function SnmpGetLastError Lib "wsnmp32.dll" (ByVal session As Long) As Long
Private Declare Function SnmpStrToOid Lib "wsnmp32.dll" (ByVal strOID As String, dstOID As AsnVar) As Long
Private Declare Function SnmpOidToStr Lib "wsnmp32.dll" (srcOID As AsnVar, ByVal size As Long, ByVal strOID As String) As Long
Private Declare Function SnmpEncodeMsg Lib "wsnmp32.dll" (ByVal session As Long, ByVal srcEntity As Long, ByVal dstEntity As Long, ByVal context As Long, ByVal pdu As Long, msgBuffDesc As AsnVar) As Long
Private Declare Function SnmpDecodeMsg Lib "wsnmp32.dll" (ByVal session As Long, srcEntity As Long, dstEntity As Long, context As Long, pdu As Long, msgBuffDesc As AsnVar) As Long
Private Declare Function SnmpFreeDescriptor Lib "wsnmp32.dll" (ByVal syntax As Long, Descriptor As Any) As Long
Private Declare Function SnmpFreeDescVar Lib "wsnmp32.dll" Alias "SnmpFreeDescriptor" (ByVal syntax As Long, Descriptor As AsnVar) As Long
' ==========================================================
' API Function Declarations -- Kernel32.dll
' ==========================================================
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (anyDest As Any, anySrc As Any, ByVal lngLen As Long)
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
' /////////////////////////////////////////////////
' Use below with DBMON if you need to debug
' -- It is not recommended to use in CallBacks
' /////////////////////////////////////////////////
'Public Declare Sub OutputDebugString Lib "kernel32" Alias "OutputDebugStringA" (ByVal strOutput As String)
' ==========================================================
' Global Variable Declarations
' ==========================================================
Public agentSettings As SnmpAgentSettings ' Global Reference -- Agent Settings
Public strReturnOID As String ' Module Reference -- GetNext Next OID
Private lngWParam As Long ' Module Reference -- Notification Type
Private lngLParam As Long ' Module Reference -- Request ID for PDU
Private boolResponse As Boolean ' Module Reference -- Indicates agent response
' ==========================================================
' Adheres to the SNMPAPI_Callback function layout but does
' NOT HANDLE API CALLS because of limitations of Visual
' Basic 6.0 -- If an API function is placed in this function
' an Access Violation error will occur within any
' stand-alone application that tries to use this componnent
' ==========================================================
Private Function SnmpAgentResponse(ByVal lSession As Long, ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long, ByVal lpClientData As Long) As Long
' Assign values to global variables
lngWParam = wParam ' If wParam = 0 then a SNMP message is available
lngLParam = lParam ' Specifies the request id for the pdu being processed
boolResponse = True ' Set global to recognize response has been received
' Return Success -- must be returned
SnmpAgentResponse = SNMPAPI_SUCCESS
End Function
' ==========================================================
' Initalizes WinSNMP API and SNMP session
' ==========================================================
Public Function SnmpOpenSession(ByRef lSession As Long) As String
On Error GoTo errorHandler
' Local variable declarations
' ---------------------------
Dim lngRetVal As Long
Dim LPVOID As Long
' "Startup" API
lngRetVal = SnmpStartup(LPVOID, LPVOID, LPVOID, LPVOID, LPVOID)
' Set translate mode to Translated
lngRetVal = SnmpSetTranslateMode(0)
' Set to v1
lngRetVal = SnmpSetRetransmitMode(1)
' Create session
lSession = SnmpCreateSession(0, 0, AddressOf SnmpAgentResponse, 0)
' Done!
SnmpOpenSession = "Successful"
Exit Function
errorHandler:
SnmpOpenSession = "[NetMgrVB.Mod.SnmpOpenSession][WAO" & Err.Number & "]=" & Err.Description
End Function
' ==========================================================
' Closes session
' ==========================================================
Public Function SnmpCloseSession(ByRef lSession As Long) As String
On Error GoTo errorHandler
' Local variable declarations
' ---------------------------
Dim lngRetVal As Long
' Close SNMP Session
lngRetVal = SnmpClose(lSession)
lSession = 0
' Initiate CleanUp mode
lngRetVal = SnmpCleanup()
' Done!
SnmpCloseSession = "Successful"
Exit Function
errorHandler:
SnmpCloseSession = "[NetMgrVB.Mod.SnmpCloseSession][WA0" & Err.Number & "]=" & Err.Description
End Function
' ==========================================================
' Generates and sends the SNMP packet to the agent
' ==========================================================
Public Function SnmpAgentRequest(ByVal lSession As Long, ByVal lngPDUType As Long, ByVal strOID As String, ByVal varOIDValue As Variant) As String
On Error GoTo errorHandler
' Local variable declarations
' ------------------------------------
Dim vbListOctValue As vbOctetValue
Dim vbListIntValue As vbIntValue
Dim vbOID As AsnVar
Dim ctxOctetVal As ContextOctetValue
Dim lngRetVal As Long
Dim lngSrcEntity As Long
Dim lngDstEntity As Long
Dim lngVBL As Long
Dim lngPDU As Long
Dim lngContext As Long
' Recycle to false
boolResponse = False
' Set Translate mode to Translated
lngRetVal = SnmpSetTranslateMode(0)
' Convert source (mgr) string to entity (long)
lngSrcEntity = SnmpStrToEntity(lSession, agentSettings.mgrAddr)
' Verify that conversion happened appropriately
If lngSrcEntity = SNMPAPI_FAILURE Then
' Translation did not occur -- set translation to
' Untranslated for v1
lngRetVal = SnmpSetTranslateMode(1)
' Retry assignment
lngSrcEntity = SnmpStrToEntity(lSession, agentSettings.mgrAddr)
' Verify that assignment occured
If lngSrcEntity = SNMPAPI_FAILURE Then
' Translation did not occur -- return error
SnmpAgentRequest = "[NetMgrVB.Mod.SnmpAgentRequest][US]=Unknown Source"
Exit Function
End If
End If
' Reset translation mode to translated
lngRetVal = SnmpSetTranslateMode(0)
' Convert target (agent) string to entity (long
lngDstEntity = SnmpStrToEntity(lSession, agentSettings.agtAddr)
' Verify that conversation happened appropriately
If lngDstEntity = SNMPAPI_FAILURE Then
' Translation did not occur -- set translation to
' Untranslated for v1
lngRetVal = SnmpSetTranslateMode(1)
' Retry assignment
lngDstEntity = SnmpStrToEntity(lSession, agentSettings.agtAddr)
' Verify that assignment occured
If lngDstEntity = SNMPAPI_FAILURE Then
' Free up any resources up until this point
lngRetVal = SnmpFreeEntity(lngSrcEntity)
' Translation did not occur -- return error
SnmpAgentRequest = "[NetMgrVB.Mod.SnmpAgentRequest][UT]=Unknown Target"
Exit Function
End If
End If
' Assign community string to context type
ctxOctetVal.len = Len(agentSettings.CommStr)
ctxOctetVal.ptr = agentSettings.CommStr
' Reset translation mode to translated
lngRetVal = SnmpSetTranslateMode(0)
' Convert context from string to long
lngContext = SnmpStrToContext(lSession, ctxOctetVal)
' Verify that assignment occured
If lngContext = SNMPAPI_FAILURE Then
' Translation did not occur -- set translation to
' Untranslated for v1
lngRetVal = SnmpSetTranslateMode(1)
' Reassign context
lngContext = SnmpStrToContext(lSession, ctxOctetVal)
' Verify that assignment occured
If lngContext = SNMPAPI_FAILURE Then
' Free any resources up until this point
lngRetVal = SnmpFreeEntity(lngSrcEntity)
lngRetVal = SnmpFreeEntity(lngDstEntity)
'Translation did not occur -- return error
SnmpAgentRequest = "[NetMgrVB.Mod.SnmpAgentRequest][CSTR]=Invalid community string"
Exit Function
End If
End If
' Convert OID string to vbOID
lngRetVal = SnmpStrToOid(strOID, vbOID)
' Check to see if any errors may have been encountered
If lngRetVal = SNMPAPI_FAILURE Then
' Free any resources up until this point
lngRetVal = SnmpFreeContext(lngContext)
' Return Error
SnmpAgentRequest = "[NetMgrVB.Mod.SnmpAgentRequest][AP0" & SnmpGetLastError(lSession) & "]=Error during String to OID conversion"
' No more processing is necessary
Exit Function
End If
' Create variable binding list
' Initialize null value
'OutputDebugString CStr(IsNumeric(varOIDValue)) & ":" & CStr(lngPDUType) & ":" & CStr(SNMP_PDU_SET)
If lngPDUType = SNMP_PDU_SET Then
If IsNumeric(varOIDValue) Then
vbListIntValue.vbType = ASN_INTEGER32
CopyMemory vbListIntValue.lngVal, CLng(varOIDValue), 4
lngVBL = SnmpCreateVbl(lSession, vbOID, vbListIntValue)
Else
vbListOctValue.vbType = ASN_OCTETSTRING
vbListOctValue.octetRef.len = Len(varOIDValue)
CopyMemory vbListOctValue.octetRef.ptr, CStr(varOIDValue), Len(varOIDValue)
lngVBL = SnmpCreateVbl(lSession, vbOID, vbListOctValue)
End If
Else
vbListIntValue.vbType = ASN_NULL
lngVBL = SnmpCreateVbl(lSession, vbOID, vbListIntValue)
End If
' Check to see if any errors may have been encountered
If lngVBL = SNMPAPI_FAILURE Then
' Return Error
SnmpAgentRequest = "[NetMgrVB.Mod.SnmpAgentRequest][AP0" & SnmpGetLastError(lSession) & "]=Error during VariableBind object creation"
' Free any resources up until this point
lngRetVal = SnmpFreeDescVar(ASN_OBJECTIDENTIFIER, vbOID)
lngRetVal = SnmpFreeEntity(lngSrcEntity)
lngRetVal = SnmpFreeEntity(lngDstEntity)
lngRetVal = SnmpFreeContext(lngContext)
' No more processing is necessary
Exit Function
End If
' Randomize Timer for Request ID Generation
Randomize Timer
' Generate PDU
lngPDU = SnmpCreatePdu(lSession, lngPDUType, 0, 0, 0, lngVBL)
' Check to see if any errors may have been encountered
If lngPDU = SNMPAPI_FAILURE Then
' Return Error
SnmpAgentRequest = "[NetMgrVB.Mod.SnmpAgentRequest][AP0" & SnmpGetLastError(lSession) & "]=Error during PDU creation"
' Free any resources up until this point
lngRetVal = SnmpFreeDescVar(ASN_OBJECTIDENTIFIER, vbOID)
lngRetVal = SnmpFreeEntity(lngSrcEntity)
lngRetVal = SnmpFreeEntity(lngDstEntity)
lngRetVal = SnmpFreeVbl(lngVBL)
lngRetVal = SnmpFreeContext(lngContext)
' No more processing is necessary
Exit Function
End If
' Send request to Agent
lngRetVal = SnmpSendMsg(lSession, lngSrcEntity, lngDstEntity, lngContext, lngPDU)
' Verify that no errors occured
If lngRetVal = SNMPAPI_FAILURE Then
' Report error
SnmpAgentRequest = "[NetMgrVB.Mod.SnmpAgentRequest][WAPI]=" & SnmpGetLastError(lSession)
End If
' Free local variables
lngRetVal = SnmpFreeDescVar(ASN_OBJECTIDENTIFIER, vbOID)
lngRetVal = SnmpFreeEntity(lngSrcEntity)
lngRetVal = SnmpFreeEntity(lngDstEntity)
lngRetVal = SnmpFreeVbl(lngVBL)
lngRetVal = SnmpFreePdu(lngPDU)
lngRetVal = SnmpFreeContext(lngContext)
' Done!
Exit Function
errorHandler:
SnmpAgentRequest = "[NetMgrVB.Mod.SnmpAgentRequest][WA0" & Err.Number & "]=" & Err.Description
End Function
' ==========================================================
' Processes response from agent
' ==========================================================
Public Function SnmpResponseProcess(ByVal lSession As Long) As String
On Error GoTo errorHandler
' Local variable declarations
' --------------------------------
Dim vbValue As vbOctetValue
Dim vbValueTmp As vbOctetValue
Dim vbOID As AsnVar
Dim strOctetValueT As String
Dim strRequestValue As String
Dim lngRetVal As Long
Dim lngSrcEntity As Long
Dim lngDstEntity As Long
Dim lngContext As Long
Dim lngPDU As Long
Dim lngVBL As Long
Dim lngPDUType As Long
Dim lngRequestID As Long
Dim lngErrorStatus As Long
Dim lngErrorIndex As Long
' Verify that no error is being passed
If lngWParam = SNMP_ERRORSTATUS_NOERROR Then
' Assign RequestID
lngRequestID = lngLParam
' Get the message from the agent
lngRetVal = SnmpRecvMsg(lSession, lngSrcEntity, lngDstEntity, lngContext, lngPDU)
' Check to see if any errors may have been encountered
If lngRetVal = SNMPAPI_FAILURE Then
' Return Error
SnmpResponseProcess = "[NetMgrVB.Mod.SnmpResponseProcess][AP0" & SnmpGetLastError(lSession) & "]=Error during message retrieval"
' No more processing is necessary
Exit Function
End If
' Process data within the PDU
lngRetVal = SnmpGetPduData(lngPDU, lngPDUType, lngRequestID, lngErrorStatus, lngErrorIndex, lngVBL)
' =Check to see if any errors may have been encountered
' ==Check return value from SnmpRecvMsg
If lngRetVal = SNMPAPI_FAILURE Then
' Return Error
SnmpResponseProcess = "[NetMgrVB.Mod.SnmpResponseProcess][AP0" & SnmpGetLastError(lSession) & "]=Error during PDU parsing"
' No more processing is necessary
Exit Function
End If
' ==Check ErrorStatus to get Agent Specific error
If lngErrorStatus > 0 Then
SnmpResponseProcess = "[NetMgrVB.Mod.SnmpResponseProcess][AP0" & CStr(lngErrorStatus) & "C" & CStr(lngErrorIndex) & "]=Error encountered during Agent Process"
' No more processing is necessary
Exit Function
End If
' Get the variable binding list
lngRetVal = SnmpGetVb(lngVBL, 1, vbOID, vbValue)
' Check to see if any errors may have been encountered
If lngRetVal = SNMPAPI_FAILURE Then
' Return Error
SnmpResponseProcess = "[NetMgrVB.Mod.SnmpResponseProcess][AP0" & SnmpGetLastError(lSession) & "]=Error during VariableBind parsing"
' No more processing is necessary
Exit Function
End If
' Determine how to process response from agent
Select Case vbValue.vbType
Case ASN_OCTETSTRING ' String value
' Initiliaze variable
strOctetValueT = String$(vbValue.octetRef.len, 0)
CopyMemory ByVal strOctetValueT, ByVal vbValue.octetRef.ptr, vbValue.octetRef.len
' What kind of data are we dealing with?
If Asc(Mid(strOctetValueT, 1, 1)) > 0 Then
' Strict string -- return value
strRequestValue = strOctetValueT
Else
' loop through octet stream and apply appropriate
' conversions
For X = 1 To vbValue.octetRef.len
If Hex(Asc(Mid(strOctetValueT, X, 1))) = "0" Then
strRequestValue = strRequestValue & "0"
End If
strRequestValue = strRequestValue & Hex(Asc(Mid(strOctetValueT, X, 1)))
Next
End If
Case ASN_OBJECTIDENTIFIER ' OID type
' Initialize variable
strOctetValueT = String(1408, 0)
CopyMemory vbValueTmp, vbValue, 12
' Convert OID to String
lngRetVal = SnmpOidToStr(vbValueTmp.octetRef, 1408, strOctetValueT)
' Accommodate \0 character
If lngRetVal > 0 Then lngRetVal = lngRetVal - 1
' Return agent response value
strRequestValue = Left(strOctetValueT, lngRetVal)
Case Else
' All others
strRequestValue = CStr(vbValue.octetRef.len)
End Select
' This is used primarily for GetNext operations
strReturnOID = String$(1408, 0)
lngRetVal = SnmpOidToStr(vbOID, 1408, strReturnOID)
If lngRetVal = SNMPAPI_FAILURE Then
strReturnOID = "[NetMgrVB.Mod.SnmpResponseProcess][AP2" & lngLParam & "]=Unable to retrieve OID from VariableBinding List"
End If
strReturnOID = Trim(Replace(strReturnOID, Chr(0), ""))
' Free local variables
lngRetVal = SnmpFreeDescVar(ASN_OBJECTIDENTIFIER, vbOID)
lngRetVal = SnmpFreeDescVar(vbValue.vbType, vbValue.octetRef)
lngRetVal = SnmpFreeVbl(lngVBL)
lngRetVal = SnmpFreePdu(lngPDU)
lngRetVal = SnmpFreeEntity(lngSrcEntity)
lngRetVal = SnmpFreeEntity(lngDstEntity)
lngRetVal = SnmpFreeContext(lngContext)
' Return Value
SnmpResponseProcess = strRequestValue
Else
' Return Error Message
SnmpResponseProcess = "[NetMgrVB.Mod.SnmpAgentResponse][AP1" & lngWParam & "]=Request ID " & lngLParam & " has encountered an error"
strReturnOID = ""
End If
' Done!
Exit Function
errorHandler:
strRequestValue = "[NetMgrVB.Mod.SnmpResponseProcess][WA0" & Err.Number & "]=" & Err.Description
strReturnOID = ""
End Function
' ==========================================================
' Centralized point for request/response between module and
' class
' ==========================================================
Public Function initAgentProc(ByVal lSession As Long, ByVal lngPDUType As Long, ByVal strOID As String, ByVal varOIDValue As Variant) As String
' Local variable declarations
' --------------------------------
Dim strRetVal As String
Dim lngRetVal As String
' Make request to agent
strRetVal = SnmpAgentRequest(lSession, lngPDUType, strOID, varOIDValue)
' Check to see if an error was returned
If InStr(1, strRetVal, "[NetMgrVB.Mod.", 1) > 0 Then
' Return error message
initAgentProc = strRetVal
Else
' Wait for SnmpAgentResponse to be executed
Do
DoEvents
Sleep APP_RESPONSE_TIME
Loop Until (boolResponse)
' Return response for request value
initAgentProc = SnmpResponseProcess(lSession)
End If
End Function