Solved

Callbacks

Posted on 1997-08-06
8
899 Views
Last Modified: 2006-11-17
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.
0
Comment
Question by:Dazza051197
  • 4
  • 2
  • 2
8 Comments
 
LVL 2

Expert Comment

by:icampbe1
ID: 1340745
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.
0
 
LVL 1

Author Comment

by:Dazza051197
ID: 1340746
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.
0
 

Accepted Solution

by:
gnom earned 150 total points
ID: 1340747
maybe as you know there is an important difference between methods and normal procedures/functions this is the class information that the methods have and that makes it impossible to pass a method as a normal callback function. that means you have to define a normal procedure for the callback, and in this procedure you have to find out for which instance of your object you have to make a call and then call it. often you have only one instance of an object and it's easy to write such a procedure. if you have more instances of an object, you need to put all instances into a list at creation time of your objects and then decide in the callback to which of the instances in the list you will pass the callback (maybe to all). fell free to ask for code details (but then i must know a little bit more of your code).

good luck
  dejan as gnom


0
 
LVL 2

Expert Comment

by:icampbe1
ID: 1340748
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:Dazza051197
ID: 1340749
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
0
 
LVL 1

Author Comment

by:Dazza051197
ID: 1340750
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.
0
 

Expert Comment

by:gnom
ID: 1340751
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

0
 
LVL 1

Author Comment

by:Dazza051197
ID: 1340752
Thank you very much for the example.
Darren
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now