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