Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

callback functions

tj29tj29 Posts: 45Member
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

Comments

  • zibadianzibadian Posts: 6,349Member
    : 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:
    [code]
    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;
    [/code]
Sign In or Register to comment.