Solved

Callbacks

Posted on 1997-08-06
8
917 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

756 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