www.pudn.com > NetMgrSamp1.rar > modAdd.bas


Attribute VB_Name = "modAdd" 
' 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 
' ///////////////////////////////////////////////// 
Private Const SNMP_PDU_GET = ASN_CONTEXT Or ASN_CONSTRUCTOR Or 0        ' 0x0 
Private Const SNMP_PDU_GETNEXT = ASN_CONTEXT Or ASN_CONSTRUCTOR Or 1    ' 0x1 
Private Const SNMP_PDU_RESPONSE = ASN_CONTEXT Or ASN_CONSTRUCTOR Or 2   ' 0x2 
Private Const SNMP_PDU_SET = ASN_CONTEXT Or ASN_CONSTRUCTOR Or 3        ' 0x3 
Private Const SNMP_PDU_GETBULK = ASN_CONTEXT Or ASN_CONSTRUCTOR Or 5    ' 0x5 
Private Const SNMP_PDU_INFORM = ASN_CONTEXT Or ASN_CONSTRUCTOR Or 6     ' 0x6 
 
' ///////////////////////////////////////////////// 
' 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    ' 0x10 
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 
 
' ========================================================== 
' 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 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 Function RtlMoveMemory Lib "kernel32.DLL" (ByRef dest As Any, ByRef src As Any, ByVal Length As Long) As Long 
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long) 
 
' ========================================================== 
' Processes response from agent 
' ========================================================== 
Public 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 
'On Error GoTo errorHandler 
'On Error Resume Next 
 
    ' Local variable declarations 
    ' -------------------------------- 
    Dim vbValue        As vbOctetValue 
    Dim vbValueTmp     As vbOctetValue 
    Dim vbOID          As AsnVar 
    Dim strOctetValueT 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 wParam = SNMP_ERRORSTATUS_NOERROR Then 
        ' Assign RequestID 
        lngRequestID = lParam 
         
        ' Get the message from the agent 
        lngRetVal = SnmpRecvMsg(lSession, lngSrcEntity, lngDstEntity, lngContext, lngPDU) 
 
        ' Process data within the PDU 
        lngRetVal = SnmpGetPduData(lngPDU, lngPduType, lngRequestID, lngErrorStatus, lngErrorIndex, lngVBL) 
         
        ' Get the variable binding list 
        lngRetVal = SnmpGetVb(lngVBL, 1, vbOID, vbValue) 
             
            ' Determine how to process response from agent 
            Select Case vbValue.vbType 
                Case ASN_OCTETSTRING            ' String value 
                    ' Initiliaze variable 
                    strOctetValueT = String$(vbValue.octetRef.len, 0) 
                    lngRetVal = RtlMoveMemory(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 SNMP_SYNTAX_OID            ' OID type 
                    ' Initialize variable 
                    strOctetValueT = String(1408, 0) 
                    lngRetVal = RtlMoveMemory(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 
         
        ' 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 SNMPAPI_SUCCESS 
        SnmpAgentResponse = 1 
    Else 
        ' Set error message 
        strRequestValue = "[NetMgrVB.Mod.SnmpAgentResponse][" & wParam & "]=Request ID " & lParam & " has encountered an error" 
         
        ' Return SNMPAPI_FAILURE 
        SnmpAgentResponse = 0 
    End If 
     
    ' Done! 
    boolSendMsg = False 
'    Exit Function 
     
'errorHandler: 
'    strRequestValue = "[NetMgrVB.Mod.SnmpAgentResponse][" & Err.Number & "]=" & Err.Description 
End Function