Link to home
Start Free TrialLog in
Avatar of Dazza051197
Dazza051197

asked on

Callbacks

How do I use a callback function from within a component.  I can define my procedure outside of my component class but within the same unit and it works, but I need to be able to call a component event from within the callback procedure.  I'm attempting to use the InternetStatusCallBack from the Wininet routines.
Avatar of icampbe1
icampbe1

Clarify a bit.  You start by saying you want to call a procedure (the callback) from within a component....  Then, you say you must call a component event from the procedure..  I'm not sure I understand.  Also, do you meant to call a component's method (or do you really mean to invoke an event) from the callback.

Try to be clear,
Ian C.
Avatar of Dazza051197

ASKER

I can get the callback routine to work if the function is defined outside of my component class, but I want the function to be a member of my component so I can then pass status information on to the owner of the component via the OnXXXXX Events if they've been assigned.Basically, I want to be able to use the StatusCallback Function from my component but if I've defined the procedure outside of the component class then it doesnt know about my component, so I need to make it a method within my class.I hope this is a little clearer.
ASKER CERTIFIED SOLUTION
Avatar of gnom
gnom

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Although I can only comment now, I think this is your answer.  If you want to pass a procedure as a callback, don't make it a method of  your class.  That doesn't mean that you have to declare it outside of your class declaration.  You can declare it as a procedure (You must remember StdCall or FarProc as necessary) inside the method that is making the external call and pass it as necessary.  Your object must persist for the duration of the callback of course.

You don't have to do it externally with lists etc.. as previously suggested.  If you like this answer, I'll write it up in a code example for you in my 'Answer'.  I'll need to know if it is Delphi 1 or a 32-bit app.

Ian C.
Thanks.
I can guarantee to only have 1 instance of my object and would be grateful if you could show me how to access my component from my callback function, assuming that I've declared the procedure from outside of the component class, but still within the same unit.

Thanks again.
Darren
Aggghh I think I've messed up by commenting before accepting the answer.  Can someone high up please credit gnom and icampbe1 with 100 points each. I'm sure they won't mind sharing a little.  I look forward to the help on how to access my component from the callback function.  Oh yes, it's Delphi 2 I'm using.Thanks again everyone.
unit Unit2;

interface

type
  TCallBackType = procedure of object;

  TMyObject = class
  private
    { Private declarations }
    FOnCallBack:TCallBackType;

  public
    { Public declarations }
    procedure DoTheCallback;

  published
    property OnCallBack:TCallBackType read FOnCallBack write FOnCallBack;

  end;

var
  MyObject:TMyObject;

procedure TheCallBack; stdcall;

implementation

procedure TheCallBack; stdcall;
begin
  if Assigned(MyObject) then MyObject.DoTheCallback;
end;

procedure TMyObject.DoTheCallback;
begin
  if Assigned(FOnCallBack) then FOnCallBack;
end;

end.

when you pass then callback, pass it like this

      SetCallBack( @TheCallBack );

and be shure to have MyObject created

have a nice day
  dejan as gnom

Thank you very much for the example.
Darren