www.pudn.com > NetMgrSamp1.rar > modWinSNMP.bas
Attribute VB_Name = "modWinSNMP"
' WinSNMP/Manager API Version 2.0
' Declarations & Definitions
' 1 May 1995 -- David Hall (original file)
' 15 Feb 1997 -- Bob Natale (ACE*COMM mods for VB-32)
' 9 Sep 1997 -- Doug Hunt (corrected SNMPAPI_*TRANSLATED* constants)
' 2 Dec 1997 -- Doug Hunt v2.0 modifications
' 2 Feb 1998 -- Doug Hunt added padding to VALUEINT to 12 bytes
' 18 Mar 1998 -- Doug Hunt changed OidToStr size parameter to ByVal
' Limits
Global Const MAXOBJIDSIZ% = 128
Global Const MAXOBJIDSTRSIZE = 1408
' ASN/BER base types
Global Const ASN_UNIVERSAL = &H0
Global Const ASN_APPLICATION = &H40
Global Const ASN_CONTEXT = &H80
Global Const ASN_PRIVATE = &HC0
Global Const ASN_PRIMITIVE = &H0
Global Const ASN_CONSTRUCTOR = &H20
' SNMP ObjectSyntax values
Global Const SNMP_SYNTAX_SEQUENCE = ASN_UNIVERSAL Or ASN_CONSTRUCTOR Or &H10
' 0xB0
' These values are used in the "syntax" member of VALUEs
Global Const SNMP_SYNTAX_INT = ASN_UNIVERSAL Or ASN_PRIMITIVE Or 2
' 0x02
Global Const SNMP_SYNTAX_BITS = ASN_UNIVERSAL Or ASN_PRIMITIVE Or 3
' 0x03
Global Const SNMP_SYNTAX_OCTETS = ASN_UNIVERSAL Or ASN_PRIMITIVE Or 4
' 0x04
Global Const SNMP_SYNTAX_NULL = ASN_UNIVERSAL Or ASN_PRIMITIVE Or 5
' 0x05
Global Const SNMP_SYNTAX_OID = ASN_UNIVERSAL Or ASN_PRIMITIVE Or 6
' 0x06
Global Const SNMP_SYNTAX_INT32 = SNMP_SYNTAX_INT
Global Const SNMP_SYNTAX_IPADDR = ASN_APPLICATION Or ASN_PRIMITIVE Or 0
' 0x40
Global Const SNMP_SYNTAX_CNTR = ASN_APPLICATION Or ASN_PRIMITIVE Or 1
' 0x41
Global Const SNMP_SYNTAX_GAUGE = ASN_APPLICATION Or ASN_PRIMITIVE Or 2
' 0x42
Global Const SNMP_SYNTAX_TIMETICKS = ASN_APPLICATION Or ASN_PRIMITIVE Or 3
' 0x43
Global Const SNMP_SYNTAX_OPAQUE = ASN_APPLICATION Or ASN_PRIMITIVE Or 4
' 0x44
Global Const SNMP_SYNTAX_NSAPADDR = ASN_APPLICATION Or ASN_PRIMITIVE Or 5
' 0x45
Global Const SNMP_SYNTAX_CNTR64 = ASN_APPLICATION Or ASN_PRIMITIVE Or 6
' 0x46
Global Const SNMP_SYNTAX_UINT32 = ASN_APPLICATION Or ASN_PRIMITIVE Or 7
' 0x47
' Exception conditions in response PDUs for SNMPv2
Global Const SNMP_SYNTAX_NOSUCHOBJECT = ASN_CONTEXT Or ASN_PRIMITIVE Or 0
' 0x80
Global Const SNMP_SYNTAX_NOSUCHINSTANCE = ASN_CONTEXT Or ASN_PRIMITIVE Or 1
' 0x81
Global Const SNMP_SYNTAX_ENDOFMIBVIEW = ASN_CONTEXT Or ASN_PRIMITIVE Or 2
' 0x82
' PDU choices:
Global Const SNMP_PDU_GET = ASN_CONTEXT Or ASN_CONSTRUCTOR Or 0
' 0xA0
Global Const SNMP_PDU_GETNEXT = ASN_CONTEXT Or ASN_CONSTRUCTOR Or 1
' 0xA1
Global Const SNMP_PDU_RESPONSE = ASN_CONTEXT Or ASN_CONSTRUCTOR Or 2
' 0xA2
Global Const SNMP_PDU_SET = ASN_CONTEXT Or ASN_CONSTRUCTOR Or 3
' 0xA3
' SNMP_PDU_V1TRAP is obsolete in SNMPv2
Global Const SNMP_PDU_V1TRAP = ASN_CONTEXT Or ASN_CONSTRUCTOR Or 4
' 0xA4
Global Const SNMP_PDU_GETBULK = ASN_CONTEXT Or ASN_CONSTRUCTOR Or 5
' 0xA5
Global Const SNMP_PDU_INFORM = ASN_CONTEXT Or ASN_CONSTRUCTOR Or 6
' 0xA6
Global Const SNMP_PDU_TRAP = ASN_CONTEXT Or ASN_CONSTRUCTOR Or 7
' 0xA7
' SNMP error codes returned in Error_Status field of PDU
' (these are NOT WinSNMP API error codes!)
Global Const SNMP_ERROR_NOERROR = 0
Global Const SNMP_ERROR_TOOBIG = 1
Global Const SNMP_ERROR_NOSUCHNAME = 2
Global Const SNMP_ERROR_BADVALUE = 3
Global Const SNMP_ERROR_READONLY = 4
Global Const SNMP_ERROR_GENERR = 5
' Error codes added for SNMPv2
Global Const SNMP_ERROR_NOACCESS = 6
Global Const SNMP_ERROR_WRONGTYPE = 7
Global Const SNMP_ERROR_WRONGLENGTH = 8
Global Const SNMP_ERROR_WRONGENCODING = 9
Global Const SNMP_ERROR_WRONGVALUE = 10
Global Const SNMP_ERROR_NOCREATION = 11
Global Const SNMP_ERROR_INCONSISTENTVALUE = 12
Global Const SNMP_ERROR_RESOURCEUNAVAILABLE = 13
Global Const SNMP_ERROR_COMMITFAILED = 14
Global Const SNMP_ERROR_UNDOFAILED = 15
Global Const SNMP_ERROR_AUTHORIZATIONERROR = 16
Global Const SNMP_ERROR_NOTWRITABLE = 17
Global Const SNMP_ERROR_INCONSISTENTNAME = 18
' Trap generic types:
Global Const SNMP_TRAP_COLDSTART = 0
Global Const SNMP_TRAP_WARMSTART = 1
Global Const SNMP_TRAP_LINKDOWN = 2
Global Const SNMP_TRAP_LINKUP = 3
Global Const SNMP_TRAP_AUTHFAIL = 4
Global Const SNMP_TRAP_EGPNEIGHBORLOSS = 5
Global Const SNMP_TRAP_ENTERPRISESPECIFIC = 6
' Type declarations:
'
' WinSNMP Descriptors
Type OID
len As Long
ptr As Long
End Type
Type OCTET ' Also: BITS, IPADDR, OPAQUE & NSAPADDR
len As Long
ptr As Long
End Type
Type OCTETSTRING
len As Long
ptr As String
End Type
Type CNTR64
' This is not a "Descriptor", but is a structure
' which can be used in a VALUE
hiPart As Long
loPart As Long
End Type
' WinSNMP Variable Binding "VALUE" definitions
Type VALUEINT
' Also INT32, UINT32, CNTR32, GAUGE32 & TIMETICKS
syntax As Long
long As Long
filler As Long 'To bring length up to 12 bytes
End Type
Type VALUECNTR64
syntax As Long
hNumber As CNTR64
End Type
Type VALUEOCTET
' Also BITS, OPAQUE, IPADDR & NSAPADDR
syntax As Long
octetv As OCTET
End Type
Type VALUEOID
syntax As Long
oidv As OID
End Type
' WinSNMP API values
'
' Values used to indicate entity/context translation modes
Global Const SNMPAPI_TRANSLATED = 0
Global Const SNMPAPI_UNTRANSLATED_V1 = 1
Global Const SNMPAPI_UNTRANSLATED_V2 = 2
' Values used to indicate SNMP "communications level"
' supported by the implementation
Global Const SNMPAPI_NO_SUPPORT = 0
Global Const SNMPAPI_V1_SUPPORT = 1
Global Const SNMPAPI_V2_SUPPORT = 2
Global Const SNMPAPI_M2M_SUPPORT = 3
' Values used to indicate retransmit mode in the implementation
Global Const SNMPAPI_OFF = 0
Global Const SNMPAPI_ON = 1
' WinSNMP API Function Return Codes
Global Const SNMPAPI_FAILURE = 0
Global Const SNMPAPI_SUCCESS = 1
' WinSNMP API Error Codes (for SnmpGetLastError())
' (NOT PDU error status codes)
Global Const SNMPAPI_ALLOC_ERROR = 2 'Error allocating memory
Global Const SNMPAPI_CONTEXT_INVALID = 3 'Invalid context parameter
Global Const SNMPAPI_CONTEXT_UNKNOWN = 4 'Unknown context parameter
Global Const SNMPAPI_ENTITY_INVALID = 5 'Invalid entity parameter
Global Const SNMPAPI_ENTITY_UNKOWN = 6 'Unknown entity parameter
Global Const SNMPAPI_INDEX_INVALID = 7 'Invalid VBL index parameter
Global Const SNMPAPI_NOOP = 8 'No operation performed
Global Const SNMPAPI_OID_INVALID = 9 'Invalid OID parameter
Global Const SNMPAPI_OPERATION_INVALID = 10 'Invalid/unsupported operation
Global Const SNMPAPI_OUTPUT_TRUNCATED = 11 'Insufficient output buffer length
Global Const SNMPAPI_PDU_INVALID = 12 'Invalid PDU parameter
Global Const SNMPAPI_SESSION_INVALID = 13 'Invalid session parameter
Global Const SNMPAPI_SYNTAX_INVALID = 14 'Invalid syntax in smiVALUE variable
Global Const SNMPAPI_VBL_INVALID = 15 'Invalid VBL parameter
Global Const SNMPAPI_MODE_INVALID = 16 'Invalid mode parameter
Global Const SNMPAPI_SIZE_INVALID = 17 'Invalid size/length parameter
Global Const SNMPAPI_NOT_INITIALIZED = 18 'SnmpStartup failed/not called
Global Const SNMPAPI_MESSAGE_INVALID = 19 'Invalid SNMP message format
Global Const SNMPAPI_HWND_INVALID = 20 'Invalid Window handle
Global Const SNMPAPI_OTHER_ERROR = 99 'For internal/undefined errors
' Generic Transport Layer (TL) Errors
Global Const SNMPAPI_TL_NOT_INITIALIZED = 100 'TL not initialized
Global Const SNMPAPI_TL_NOT_SUPPORTED = 101 'TL does not support protocol
Global Const SNMPAPI_TL_NOT_AVAILABLE = 102 'Network sub-system has failed
Global Const SNMPAPI_TL_RESOURCE_ERROR = 103 'TL resource error
Global Const SNMPAPI_TL_UNDELIVERABLE = 104 'Destination unreachable
Global Const SNMPAPI_TL_SRC_INVALID = 105 'Source endpoint invalid
Global Const SNMPAPI_TL_INVALID_PARAM = 106 'Input parameter invalid
Global Const SNMPAPI_TL_IN_USE = 107 'Source endpoint in use
Global Const SNMPAPI_TL_TIMEOUT = 108 'No response before timeout
Global Const SNMPAPI_TL_PDU_TOO_BIG = 109 'PDU too big for send/receive
Global Const SNMPAPI_TL_OTHER = 199 'Undefined TL error
' v2.0: Structure for SnmpGetVendorInfo() output
Global Const MAXVENDORINFO = 32
Type VENDORINFO
vendorName As String * 64
vendorContact As String * 64
vendorVersionId As String * 32
vendorVersionData As String * 32
vendorEnterprise As Long
End Type
' Local Database Functions
' The following declarations have been tested
' SnmpGetTranslateMode()
' SnmpSetTranslateMode()
' SnmpGetRetransmitMode()
' SnmpSetRetransmitMode() - only mode 0 supported at this time
Declare Function SnmpGetTranslateMode Lib "wsnmp32.dll" (nTransMode As Long) As Long
Declare Function SnmpSetTranslateMode Lib "wsnmp32.dll" (ByVal nTransMode As Long) As Long
Declare Function SnmpGetRetransmitMode Lib "wsnmp32.dll" (nRxTxMode As Long) As Long
Declare Function SnmpSetRetransmitMode Lib "wsnmp32.dll" (ByVal nRxTxMode As Long) As Long
Declare Function SnmpGetTimeout Lib "wsnmp32.dll" (ByVal hEntity As Long, nPolicyTO As Long, nActualTO As Long) As Long
Declare Function SnmpSetTimeout Lib "wsnmp32.dll" (ByVal hEntity As Long, ByVal nPolicyTO As Long) As Long
Declare Function SnmpGetRetry Lib "wsnmp32.dll" (ByVal hEntity As Long, nPolicyRetry As Long, nActualRetry As Long) As Long
Declare Function SnmpSetRetry Lib "wsnmp32.dll" (ByVal hEntity As Long, ByVal nPolicyRetry As Long) As Long
' Following Local Database Functions added in v2.0
Declare Function SnmpGetVendorInfo Lib "wsnmp32.dll" (ByRef vendInfo As VENDORINFO) As Long
Declare Function SnmpStartup Lib "wsnmp32.dll" (nVerMaj As Long, nVerMin As Long, nLevel As Long, nTMode As Long, nRMode As Long) As Long
Declare Function SnmpCleanup Lib "wsnmp32.dll" () As Long
Declare Function SnmpCreateSession Lib "wsnmp32.dll" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal fCallBack As Long, ByVal lpClientData As Long) As Long
Declare Function SnmpSendMsg Lib "wsnmp32.dll" (ByVal hSession As Long, ByVal sEntity As Long, ByVal dEntity As Long, ByVal hContext As Long, ByVal hPDU As Long) As Long
Declare Function SnmpRecvMsg Lib "wsnmp32.dll" (ByVal hSession As Long, sEntity As Long, dEntity As Long, hContext As Long, hPDU As Long) As Long
Declare Function SnmpStrToEntity Lib "wsnmp32.dll" (ByVal hSession As Long, ByVal entity As String) As Long
Declare Function SnmpEntityToStr Lib "wsnmp32.dll" (ByVal hEntity As Long, ByVal lenEntity As Long, ByVal strEntity As String) As Long
Declare Function SnmpFreeEntity Lib "wsnmp32.dll" (ByVal hEntity As Long) As Long
Declare Function SnmpStrToContext Lib "wsnmp32.dll" (ByVal hSession As Long, smiOctets As OCTETSTRING) As Long
Declare Function SnmpContextToStr Lib "wsnmp32.dll" (ByVal hContext As Long, smiOctets As OCTET) As Long
Declare Function SnmpFreeContext Lib "wsnmp32.dll" (ByVal hContext As Long) As Long
Declare Function SnmpCreatePdu Lib "wsnmp32.dll" (ByVal hSession As Long, ByVal typePDU As Long, ByVal ReqID As Long, ByVal errorStat As Long, ByVal errorIdx As Long, ByVal hVBL As Long) As Long
Declare Function SnmpGetPduData Lib "wsnmp32.dll" (ByVal hPDU As Long, typePDU As Long, ReqID As Long, errorStat As Long, errorIdx As Long, hVBL As Long) As Long
Declare Function SnmpSetPduData Lib "wsnmp32.dll" (ByVal hPDU As Long, typePDU As Long, ReqID As Long, errorStat As Long, errorIdx As Long, hVBL As Long) As Long
Declare Function SnmpFreePdu Lib "wsnmp32.dll" (ByVal hPDU As Long) As Long
' Variable Binding Functions
' The following declarations have been tested
' SnmpCreateVbl()
' SnmpFreeVbl()
' SnmpCountVbl()
' SnmpGetVb()
' SnmpSetVb()
Declare Function SnmpCreateVbl Lib "wsnmp32.dll" (ByVal hSession As Long, VarOID As OID, VarVal As Any) As Long
Declare Function SnmpFreeVbl Lib "wsnmp32.dll" (ByVal hVBL As Long) As Long
Declare Function SnmpCountVbl Lib "wsnmp32.dll" (ByVal hVBL As Long) As Long
Declare Function SnmpGetVb Lib "wsnmp32.dll" (ByVal hVBL As Long, ByVal index As Long, VarOID As OID, VarVal As VALUEOCTET) As Long
Declare Function SnmpSetVb Lib "wsnmp32.dll" (ByVal hVBL As Long, ByVal index As Long, VarOID As OID, VarVal As Any) As Long
Declare Function SnmpDeleteVb Lib "wsnmp32.dll" (ByVal hVBL As Long, ByVal index As Long) As Long
' SnmpCreateVbl() needs to be aliased to SnmpCreateIntVbl(),
' SnmpCreateOctVbl(), SnmpCreateOidVbl(), SnmpCreateC64Vbl() for "TYPE SAFE" usage
Declare Function SnmpCreateIntVbl Lib "wsnmp32.dll" Alias "SnmpCreateVbl" (ByVal hSession As Long, VarOID As OID, VarVal As VALUEINT) As Long
Declare Function SnmpCreateOctVbl Lib "wsnmp32.dll" Alias "SnmpCreateVbl" (ByVal hSession As Long, VarOID As OID, VarVal As VALUEOCTET) As Long
Declare Function SnmpCreateOidVbl Lib "wsnmp32.dll" Alias "SnmpCreateVbl" (ByVal hSession As Long, VarOID As OID, VarVal As VALUEOID) As Long
Declare Function SnmpCreateC64Vbl Lib "wsnmp32.dll" Alias "SnmpCreateVbl" (ByVal hSession As Long, VarOID As OID, VarVal As VALUECNTR64) As Long
' SnmpSetVb() needs to be aliased to SnmpSetIntVb(), SnmpSetOctVb(),
' SnmpSetOidVb(), SnmpSetC64Vb() for "TYPE SAFE" usage
Declare Function SnmpSetIntVb Lib "wsnmp32.dll" Alias "SnmpSetVb" (ByVal hVBL As Long, ByVal index As Long, VarOID As OID, VarVal As VALUEINT) As Long
Declare Function SnmpSetOctVb Lib "wsnmp32.dll" Alias "SnmpSetVb" (ByVal hVBL As Long, ByVal index As Long, VarOID As OID, VarVal As VALUEOCTET) As Long
Declare Function SnmpSetOidVb Lib "wsnmp32.dll" Alias "SnmpSetVb" (ByVal hVBL As Long, ByVal index As Long, VarOID As OID, VarVal As VALUEOID) As Long
Declare Function SnmpSetC64Vb Lib "wsnmp32.dll" Alias "SnmpSetVb" (ByVal hVBL As Long, ByVal index As Long, VarOID As OID, VarVal As VALUECNTR64) As Long
' Utility Functions
' The following declarations have been tested
' SnmpGetLastError()
' SnmpStrToOid()
' SnmpOidToStr()
' SnmpFreeDescriptor()
Declare Function SnmpGetLastError Lib "wsnmp32.dll" (ByVal hSession As Long) As Long
Declare Function SnmpStrToOid Lib "wsnmp32.dll" (ByVal strOID As String, dstOID As OID) As Long
Declare Function SnmpOidToStr Lib "wsnmp32.dll" (srcOID As OID, ByVal size As Long, ByVal strOID As String) As Long
Declare Function SnmpOidCopy Lib "wsnmp32.dll" (srcOID As OID, dstOID As OID) As Long
Declare Function SnmpOidCompare Lib "wsnmp32.dll" (xOID As OID, yOID As OID, ByVal maxLen As Long, Result As Long) As Long
Declare Function SnmpEncodeMsg Lib "wsnmp32.dll" (ByVal hSession As Long, ByVal srcEntity As Long, ByVal dstEntity As Long, ByVal hContext As Long, ByVal hPDU As Long, msgBuff As OCTET) As Long
Declare Function SnmpDecodeMsg Lib "wsnmp32.dll" (ByVal hSession As Long, srcEntity As Long, dstEntity As Long, hContext As Long, hPDU As Long, msgBuff As OCTET) As Long
Declare Function SnmpFreeDescriptor Lib "wsnmp32.dll" (ByVal syntax As Long, descriptor As Any) As Long
' SnmpFreeDescriptor needs to be Aliased to create SnmpFreeDescOct() &
' SnmpFreeDescOid() for "TYPE SAFE" usage
Declare Function SnmpFreeDescOct Lib "wsnmp32.dll" Alias "SnmpFreeDescriptor" (ByVal syntax As Long, descriptor As OCTET) As Long
Declare Function SnmpFreeDescOid Lib "wsnmp32.dll" Alias "SnmpFreeDescriptor" (ByVal syntax As Long, descriptor As OID) As Long
Public Declare Function RtlMoveMemory Lib "KERNEL32.DLL" (ByRef dest As Any, ByRef src As Any, ByVal length As Long) As Long
Public hSession As Long
Public hAgentSession As Long
Public hAgentEntity As Long
Public Sub AgentOpen()
Dim Result As Long
Dim bogus As Long
nl = " "
Form1.txSampleOids.Text = "Sample OIDS:" & nl & "1.3.6.1.2.1.1.1.0:sysDescr.0" & nl & "1.3.6.1.2.1.1.2.0:sysObjectID.0" & nl & "1.3.6.1.2.1.1.3.0:sysUpTime.0" & nl & "1.3.6.1.2.1.1.4.0:sysContact.0" & nl & "1.3.6.1.2.1.1.5.0:sysName.0"
Result = SnmpStartup(bogus, bogus, bogus, bogus, bogus)
Result = SnmpSetTranslateMode(SNMPAPI_TRANSLATED)
Result = SnmpSetRetransmitMode(SNMPAPI_ON)
hSession = SnmpCreateSession(0, 0, AddressOf trMsg, 0)
End Sub
Public Sub trMsg()
Dim srcEntity As Long, dstEntity As Long
Dim context As Long
Dim pdu As Long
Dim vbl As Long
Dim pdutype As Long, ReqID As Long, errStat As Long, errInd As Long
Dim getOid As OID
Dim getValue As VALUEOCTET
Result = SnmpRecvMsg(hSession, srcEntity, dstEntity, context, pdu)
Result = SnmpGetPduData(pdu, pdutype, ReqID, errStat, errInd, vbl)
Result = SnmpGetVb(vbl, 1, getOid, getValue)
Select Case getValue.syntax
Case SNMP_SYNTAX_OCTETS
Dim vname As String
vname = String$(getValue.octetv.len, 0)
Call RtlMoveMemory(ByVal vname, ByVal getValue.octetv.ptr, getValue.octetv.len)
Form1.txResponse.Text = Form1.txResponse.Text & " : """ & vname & """"
Case SNMP_SYNTAX_OID
Dim txOid As String
Dim oidVal As VALUEOID
txOid = String(MAXOBJIDSTRSIZE, 0) 'Pad out string
'Copy VALUEOCTET to VALUEOID
Call RtlMoveMemory(oidVal, getValue, 12)
Result = SnmpOidToStr(oidVal.oidv, MAXOBJIDSTRSIZE, txOid)
Result = IIf(Result = SNMPAPI_FAILURE, 0, Result - 1)
Form1.txResponse.Text = Form1.txResponse.Text & " : " & Left(txOid, Result)
Case Else
Form1.txResponse.Text = Form1.txResponse.Text & " : " & getValue.octetv.len
End Select
Result = SnmpFreeDescOid(SNMP_SYNTAX_OID, getOid)
Result = SnmpFreeDescOct(getValue.syntax, getValue.octetv)
Result = SnmpFreeVbl(vbl)
Result = SnmpFreePdu(pdu)
Result = SnmpFreeEntity(srcEntity)
Result = SnmpFreeEntity(dstEntity)
Result = SnmpFreeContext(context)
End Sub
Public Sub recvMsg(hSession As Long, isAgent As Boolean)
Dim srcEntity As Long, dstEntity As Long
Dim context As Long
Dim pdu As Long
Dim vbl As Long
Dim pdutype As Long, ReqID As Long, errStat As Long, errInd As Long
Dim getOid As OID
Dim getValue As VALUEOCTET
Result = SnmpRecvMsg(hSession, srcEntity, dstEntity, context, pdu)
Result = SnmpGetPduData(pdu, pdutype, ReqID, errStat, errInd, vbl)
Result = SnmpGetVb(vbl, 1, getOid, getValue)
If isAgent Then
Dim tx As String
Dim oidVal As VALUEOID
tx = String(255, 0) 'Pad out string
Result = SnmpEntityToStr(srcEntity, 255, tx)
Result = IIf(Result = SNMPAPI_FAILURE, 0, Result - 1)
Form1.txAgentSource.Text = Left(tx, Result)
tx = String(255, 0) 'Pad out string
Result = SnmpEntityToStr(dstEntity, 255, tx)
Result = IIf(Result = SNMPAPI_FAILURE, 0, Result - 1)
Form1.txAgentSource.Text = Left(tx, Result)
tx = String(MAXOBJIDSTRSIZE, 0) 'Pad out string
Result = SnmpOidToStr(getOid, MAXOBJIDSTRSIZE, tx)
Result = IIf(Result = SNMPAPI_FAILURE, 0, Result - 1)
txAgentOid = Left(tx, Result)
'Send back error message
pdutype = SNMP_PDU_RESPONSE
errStat = SNMP_ERROR_NOSUCHNAME
errInd = 1
Result = SnmpSetPduData(pdu, pdutype, ReqID, errStat, errInd, vbl)
Result = SnmpSendMsg(hSession, dstEntity, srcEntity, context, pdu)
Else
Select Case getValue.syntax
Case SNMP_SYNTAX_OCTETS
Dim vname As String
vname = String$(getValue.octetv.len, 0)
Call RtlMoveMemory(ByVal vname, ByVal getValue.octetv.ptr, getValue.octetv.len)
txResponse.Text = txResponse.Text & " : """ & vname & """"
Case SNMP_SYNTAX_OID
Dim txOid As String
Dim oidVal As VALUEOID
txOid = String(MAXOBJIDSTRSIZE, 0) 'Pad out string
'Copy VALUEOCTET to VALUEOID
Call RtlMoveMemory(oidVal, getValue, 12)
Result = SnmpOidToStr(oidVal.oidv, MAXOBJIDSTRSIZE, txOid)
Result = IIf(Result = SNMPAPI_FAILURE, 0, Result - 1)
txResponse.Text = txResponse.Text & " : " & Left(txOid, Result)
Case Else
txResponse.Text = txResponse.Text & " : " & getValue.octetv.len
End Select
End If
Result = SnmpFreeDescOid(SNMP_SYNTAX_OID, getOid)
Result = SnmpFreeDescOct(getValue.syntax, getValue.octetv)
Result = SnmpFreeVbl(vbl)
Result = SnmpFreePdu(pdu)
Result = SnmpFreeEntity(srcEntity)
Result = SnmpFreeEntity(dstEntity)
Result = SnmpFreeContext(context)
End Sub
Public Sub SendRequest()
Dim Result As Long
Dim srcEntity As Long
Dim dstEntity As Long
txResponse = ""
Result = SnmpSetTranslateMode(SNMPAPI_TRANSLATED)
srcEntity = SnmpStrToEntity(hSession, Form1.txSource.Text)
If srcEntity = 0 Then
Result = SnmpSetTranslateMode(SNMPAPI_UNTRANSLATED_V1)
srcEntity = SnmpStrToEntity(hSession, Form1.txSource.Text)
End If
If srcEntity = 0 Then
MsgBox "Unknown Source Entity"
Exit Sub
End If
Result = SnmpSetTranslateMode(SNMPAPI_TRANSLATED)
dstEntity = SnmpStrToEntity(hSession, Form1.txDest.Text)
If dstEntity = 0 Then
Result = SnmpSetTranslateMode(SNMPAPI_UNTRANSLATED_V1)
dstEntity = SnmpStrToEntity(hSession, Form1.txDest.Text)
End If
If dstEntity = 0 Then
MsgBox "Unknown Dest Entity"
Exit Sub
End If
Dim getOid As OID
Dim vbl As Long
Dim pdu As Long
Dim NullValue As VALUEINT
Dim contextOct As OCTETSTRING
NullValue.syntax = SNMP_SYNTAX_NULL
contextOct.len = Len(Form1.txContext.Text)
contextOct.ptr = Form1.txContext.Text
Result = SnmpSetTranslateMode(SNMPAPI_TRANSLATED)
context = SnmpStrToContext(hSession, contextOct)
If context = 0 Then
Result = SnmpSetTranslateMode(SNMPAPI_UNTRANSLATED_V1)
context = SnmpStrToContext(hSession, contextOct)
End If
If context = 0 Then
MsgBox "Invalid context"
Exit Sub
End If
Result = SnmpStrToOid(Form1.txOid.Text, getOid)
vbl = SnmpCreateVbl(hSession, getOid, NullValue)
pdu = SnmpCreatePdu(hSession, SNMP_PDU_GET, 1234, 0, 0, vbl)
Result = SnmpSendMsg(hSession, srcEntity, dstEntity, context, pdu)
If Result = SNMPAPI_FAILURE Then
Form1.txResponse.Text = "WinSnmp error " & SnmpGetLastError(hSession)
Else
Form1.txResponse.Text = "Message Sent. . ."
End If
Result = SnmpFreeDescOid(SNMP_SYNTAX_OID, getOid)
Result = SnmpFreeEntity(srcEntity)
Result = SnmpFreeEntity(dstEntity)
Result = SnmpFreePdu(pdu)
Result = SnmpFreeContext(context)
End Sub
Public Sub closeAgent()
Dim Result As Long
Result = SnmpClose(hSession)
Result = SnmpCleanup()
End Sub