Newsgroups : Borland : borland.public.delphi.internet.winsock : 2008 Jun : Re: Console App with Indy10 FTP client - event handlers

www.cryer.info
Managed Newsgroup Archive

Re: Console App with Indy10 FTP client - event handlers

Subject:Re: Console App with Indy10 FTP client - event handlers
Posted by:"Remy Lebeau (TeamB)" (no.spam@no.spam.com)
Date:Thu, 19 Jun 2008 14:06:54

"millerb" <bcmiller@prairiecomm.net> wrote in message
news:485ac074$1@newsgroups.borland.com...

> I'd like to make use of the TIdFTP event handlers but don't
> know how to set it up in a formless app.

VCL event handlers have to be members of a class.  You have two options to
make this work in a console project:

1) (the easy way) define a class to hold the desired handlers, ie:

    type
        TFTPEvents = class
        public
            procedure FTPAfterClientLogin(ASender: TObject);
            procedure FTPAfterGet(ASender: TObject; AStream: TStream);
        end;

    var
        FTP: TIdFTP;

        FTPEvent: TFTPEvents;
        ...

    procedure Main;
    begin
        FTPEvents := TFTPEvents.Create;
        FTP := TIdFTP.Create(nil);

        FTP.OnAfterClientLogin := FTPEvents.FTPAfterClientLogin;
        FTP.OnAfterGet := FTPEvents.FTPAfterGet;

        ...
    end;

    procedure TFTPEvents.FTPAfterClientLogin(ASender: TObject);
    begin
        ...
    end;

    procedure TFTPEvents.FTPAfterGet(ASender: TObject; AStream: TStream);
    begin
        ...
    end;


2) (dirty hack) use TMethod to force non-class functions to act like class
methods, ie:

    var
        FTP: TIdFTP;
        ...

    // notice the extra parameter in front...
    procedure FTPAfterClientLogin(AFTP: TIdFTP; ASender: TObject);
    procedure FTPAfterGet(AFTP: TIdFTP; ASender: TObject; AStream: TStream);

    procedure Main;
    var
        M: TMethod;
    begin
        FTP := TIdFTP.Create(nil);

        M.Code := @FTPAfterClientLogin;
        M.Data := FTP;
        FTP.OnAfterClientLogin := TOnAfterClientLogin(M);
        // alternatively:
        // Typinfo.SetMethodProp(FTP, 'OnAfterClientLogin', M);

        M.Code := @FTPAfterClientLogin;
        M.Data := FTP;
        FTP.OnAfterGet := TIdFtpAfterGet(M);
        // alternatively:
        // Typinfo.SetMethodProp(FTP, 'OnAfterGet', M);

        ...
    end;

    procedure FTPAfterClientLogin(AFTP: TIdFTP; ASender: TObject);
    begin
        ...
    end;


    procedure FTPAfterGet(AFTP: TIdFTP; ASender: TObject; AStream: TStream);
    begin
        ...
    end;


Gambit

Replies:

none

In response to:

www.cryer.info
Managed Newsgroup Archive