Solved

Callbacks

Posted on 1997-08-06
8
921 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

737 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