访问冲突错误SNMP

时间:2014-04-11 07:21:08

标签: delphi snmp access-violation

我正在编写一个简单的SNMP应用程序来从comtech调制解调器CDM-570L获取信息 (系统解密,功率电平和符号率)。

unit Unit1;

interface

uses
  System.UITypes, Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, IdUDPBase, IdUDPClient, idSNMP;

type
    TForm1 = class(TForm)
      Nhap: TGroupBox;
      Label1: TLabel;
      Label2: TLabel;
      ButtonEnter: TButton;
      Xuat: TGroupBox;
      PowerLevel: TLabel;
    PowerLevelValue: TEdit;
      SymbolRate: TLabel;
    SymbolRateValue: TEdit;
      ButtonSave: TButton;
      dBm: TLabel;
      sps: TLabel;
      DeviceName: TLabel;
    HostIP: TEdit;
    ComString: TEdit;
    procedure BtnEnterClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  SNMP: TidSNMP;
implementation

{$R *.dfm}

procedure TForm1.BtnEnterClick(Sender: TObject);
var dn, OID: string; //dn = device name, I mean system descryption

begin
  if Trim(HostIP.Text) = '' then begin
    MessageDlg('Chưa nhập IP của thiết bị!', mtError, [mbOK], 0);
    Exit;
    end;
  if Trim(ComString.Text) = '' then begin
    MessageDlg('Chưa nhập SNMP read community string', mtError, [mbOK], 0);
    Exit;
    end;
  PowerLevelValue.Clear;
  SymbolRateValue.Clear;
  SNMP.Host:= HostIP.Text;
  SNMP.Community:= ComString.Text;
  SNMP.ReceiveTimeout:=1000;
  if SNMP.QuickSend('1.3.6.1.2.1.1.1.0',SNMP.Community,SNMP.Host,dn)
  then DeviceName.Caption:=dn;
end;

end.

发生错误:

  

$ 005D39C9的第一次机会异常。异常类$ C0000005,消息'访问冲突位于0x005d39c9:读取地址0x00000000'。处理Project1.exe(8156)

enter image description here

~~我在这里的第一篇文章,感谢阅读~~

编辑:对不起,没有“如果那么”,我忘了编辑

SNMP.QuickSend('1.3.6.1.2.1.1.1.0',SNMP.Community,SNMP.Host,dn);
DeviceName.Caption:=dn;

1 个答案:

答案 0 :(得分:1)

声明SNMP全局变量但从未分配给它。因此它是nil。尝试引用它将导致访问冲突。您必须在使用之前实例化该对象。

更广泛地说,我想知道为什么你在这里使用全局变量。一般而言,最好避免使用全局变量。