IdIRC中的PrivateMessages出错

时间:2014-11-27 14:16:50

标签: delphi indy

您好我的以下代码有问题:

program test;

{$APPTYPE CONSOLE}
{$R *.res}

uses
  System.SysUtils, idIRC, idContext, idGlobal;


// procedure TForm1.IdIRC1PrivateMessage(ASender: TIdContext; const ANickname,
// AHost, ATarget, AMessage: string);

// procedure TForm1.IdIRC1Raw(ASender: TIdContext; AIn: Boolean;
// const AMessage: string);

procedure IrcPrivateMessage(ASelf: Pointer; const ANickname, AHost, ATarget,
  AMessage: string);
begin
  Writeln('[+] Message ' + ANickname + ' > ' + AMessage);
end;

procedure IrcRaw(ASelf: Pointer; ASender: TIdContext; AIn: Boolean;
  const AMessage: String);
begin
  Writeln(iif(AIn, '[Recv] ', '[Sent] ') + AMessage);
end;

var
  irc: TIdIRC;
  m1: TMethod;
  m2: TMethod;

begin
  try
    irc := TIdIRC.Create(nil);
    try
      irc.Host := 'localhost';
      irc.Port := 6667;
      irc.Nickname := 'tester';
      irc.Username := 'tester';

      m1.Code := @IrcRaw;
      m1.Data := irc;
      irc.OnRaw := TIdIRCRawEvent(m1);

      m2.Code := @IrcPrivateMessage;
      m2.Data := irc;
      irc.OnPrivateMessage := TIdIRCPrivMessageEvent(m2);

      try
        irc.Connect;
      except
        Writeln('Nay');
        Exit;
      end;

      Writeln('Yeah');

      irc.Join('#locos');

      while ('1' = '1') do
      begin
        //
      end;

    finally
      irc.Free;
    end;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;

end.

问题是当我收到私人消息时,ANickname条目给出了以下错误:

项目test.exe引发了exeption类#C00000005,并在0x00404673处显示了访问冲突:读取地址0x03cf4e58

我做错了什么?

1 个答案:

答案 0 :(得分:1)

TIdIRCPrivMessageEvent是"对象"的过程,它隐式引用该对象。您为此添加了Self参数,但是您省略了TIdContext参数,该参数也是该事件的一部分:

procedure IrcPrivateMessage(ASelf: Pointer; Sender: TIdContext; const ANickname, AHost, ATarget, AMessage: string);
begin
   ....
end;