示例#1
0
        public bool Listen(string RemoteMachine)
        {
            bool Success = false; // init success to failure

            try
            {
                SNMPAPI_STATUS Result = new SNMPAPI_STATUS(); // set up a result item

                // this is our call-back function
                SnmpAPI.SnmpCallback SnmpCB1 = new SnmpAPI.SnmpCallback(OnSnmpMessage);

                // get the local and remote ip addresses
                this.LocalAddr  = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0].ToString();
                this.RemoteAddr = Dns.GetHostEntry(RemoteMachine).AddressList[0].ToString();

                // initialize the snmp api system
                Result = SnmpAPI.SnmpStartup(out major, out minor, out level, out translate, out retran);
                if (Result != SNMPAPI_STATUS.SNMPAPI_SUCCESS)
                {
                    throw new Exception("SNMP Initialization Error");
                }

                // create the session
                Session = SnmpAPI.SnmpCreateSession(IntPtr.Zero, 0, SnmpCB1, IntPtr.Zero);
                if (Result == SNMPAPI_STATUS.SNMPAPI_FAILURE)
                {
                    throw new Exception("SNMP Create Session Error");
                }

                Result = SnmpRegister(Session, System.IntPtr.Zero, System.IntPtr.Zero, System.IntPtr.Zero, System.IntPtr.Zero, (int)SNMPAPI_STATE.SNMPAPI_ON);
                if (Result != SNMPAPI_STATUS.SNMPAPI_SUCCESS)
                {
                    throw new Exception("SNMP Initialization Error");
                }


                IntPtr     VbL1 = new IntPtr();                 // pointer to variable bindings list
                SMIOID[]   OID  = new SMIOID[1];                // an array of oids
                SMIVALUE[] Val  = new SMIVALUE[1];              // an array of values

                // create entities for the source and destination addresses
                IntPtr Source = SnmpAPI.SnmpStrToEntity(Session, LocalAddr);
                IntPtr Destin = SnmpAPI.SnmpStrToEntity(Session, RemoteAddr);


                VbL1 = SnmpAPI.SnmpCreateVbl(Session, ref OID[0], ref Val[0]);

                VARIABLE_ITEM VariableListItem = new VARIABLE_ITEM();
                VariableListItem.Entity = ".1";
                VariableListItem.Type   = VARIABLE_TYPE.VARIABLE_TYPE_OCTECT;
                VariableListItem.Value  = "GetAlarmDescription(AlarmCode)";
                AddVariableListItem(VariableListItem);

                IntPtr Pdu = SnmpAPI.SnmpCreatePdu(Session, (int)SNMPAPI_PDU.SNMP_PDU_GETBULK, Int16.MinValue, Int16.MinValue, Int16.MinValue, VbL1);

                GCHandle[] PinnedArray = new GCHandle[1];               // an array of pinned memory

                SMIOCTETS DestOct = new SMIOCTETS();                    // the destination octect

                // set the destination octet (we only need to set the first one, the rest will flow accordingly)
                DestOct.size = (uint)(VariableListItem.Value.Length);
                //DestOct.octets = PinnedArray[0].AddrOfPinnedObject();
                IntPtr Context = SnmpAPI.SnmpStrToContext(Session, ref DestOct);

                // set the port to 162
                Result = SnmpAPI.SnmpSetPort(Destin, 162);

                while (!Success)
                {
                    Result = SnmpAPI.SnmpRecvMsg(Session, out Source, out Destin, out Context, out Pdu);
                    if (Result == SNMPAPI_STATUS.SNMPAPI_SUCCESS)

                    //throw new Exception("SNMP Initialization Error");
                    {
                        int    teste1;
                        int    teste0;
                        int    teste3; int teste4;
                        IntPtr vlb;
                        Result = SnmpAPI.SnmpGetPduData(Pdu, out teste0, out teste1, out teste3, out teste4, out vlb);
                        //VARIABLE_BINDING_LIST teste11 = teste;


                        Success = true;
                        break;
                    }
                    Thread.Sleep(100);
                }
            }
            catch (Exception Err)
            {
                ErrMess = Err.Message;
            }
            return(Success);
        }