Delphi and Kylix

Moderators: pritaeas
Number of threads: 7244
Number of posts: 19051

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
callback functions Posted by tj29 on 13 Oct 2006 at 6:37 PM
Hi, I have questions on using callbacks functions in Delphi. I am using a dll. In this DLL there is a call to a function that returns the response to a callback function. everything works great.

My question is how can I access that information return to the callback function from an existing class in Delphi.


For example:
//Delphi class

Type
TfrmMain = class(TForm)
private
fObjectFieldList: TStringList;
public
Procedure writetoscreen(abuffer:PChar);
end;

//call back function
Type
TGetHttpResponse = procedure(Buffer : Pchar; Id:integer ); stdCall;

Implementation

//call to dll
function HttpRequest(url_ptr:PCHAR;
ip_ptr:PCHAR;
postdata_ptr:PCHAR;
requestheader_ptr:PCHAR;
port:integer;
timeout:integer;
Func:TGetHttpResponse; <---callback
id:integer): integer; stdcall; external 'winhttp_dll.Dll' name '_HttpRequest@32';


//definition to callback
procedure getresponse(aBuffer : Pchar; aId:integer ); stdCall;
begin
// I would like to pass abuffer to writetoscreen procedure that
belongs to TFrmMain
Writetoscreen(abuffer);
end;

PS: i know i can use a global variable but thats out of the question because this is a threaded application where i am trying to used this in.. thanks for your help

Report
Re: callback functions Posted by zibadian on 13 Oct 2006 at 11:23 PM
: Hi, I have questions on using callbacks functions in Delphi. I am using a dll. In this DLL there is a call to a function that returns the response to a callback function. everything works great.
:
: My question is how can I access that information return to the callback function from an existing class in Delphi.
:
:
: For example:
: //Delphi class
:
: Type
: TfrmMain = class(TForm)
: private
: fObjectFieldList: TStringList;
: public
: Procedure writetoscreen(abuffer:PChar);
: end;
:
: //call back function
: Type
: TGetHttpResponse = procedure(Buffer : Pchar; Id:integer ); stdCall;
:
: Implementation
:
: //call to dll
: function HttpRequest(url_ptr:PCHAR;
: ip_ptr:PCHAR;
: postdata_ptr:PCHAR;
: requestheader_ptr:PCHAR;
: port:integer;
: timeout:integer;
: Func:TGetHttpResponse; <---callback
: id:integer): integer; stdcall; external 'winhttp_dll.Dll' name '_HttpRequest@32';
:
:
: //definition to callback
: procedure getresponse(aBuffer : Pchar; aId:integer ); stdCall;
: begin
: // I would like to pass abuffer to writetoscreen procedure that
: belongs to TFrmMain
: Writetoscreen(abuffer);
: end;
:
: PS: i know i can use a global variable but thats out of the question because this is a threaded application where i am trying to used this in.. thanks for your help
:
:
You should pass the object's address in the ID parameter. Then in the callback you can typecast it to get access to the object's memory space, and its methods:
begin
  HTTPRequest(...., getresponse, integer(frmMain));
   // call to HTTPRequest, left out all the irrelevant parameters
end;

procedure getresponse(aBuffer : Pchar; aId:integer ); stdCall;
begin
  TfrmMain(aID).Writetoscreen(abuffer);
    // aID "points" to frmMain
end;




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.