Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Callbacks

Posted on 1997-08-06
8
Medium Priority
?
938 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 600 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Screencast - Getting to Know the Pipeline
Suggested Courses

610 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