Lazarus

Programming => General => Topic started by: Blestan on May 21, 2017, 09:19:25 pm

Title: Procedure of record
Post by: Blestan on May 21, 2017, 09:19:25 pm
Hi!
i have the following problem - cannot fugure how to call a procedure of advanced record as a callback...
TEnsureDataCallback=function(const DataPtr: Pointer; MaxLen: Integer): Integer of object;

and latter triing to assign a procdure of record to a parameter in proc call gives me compile error

any ideas?
Title: Re: Procedure of record
Post by: Thaddy on May 21, 2017, 09:30:10 pm
If you want it as a callback it needs to be a  class function and static; And the proper calling convention added: cdecl or winapi/stdcall;

Or you should unhide the hidden self parameter in a function prototype.
Code: Pascal  [Select][+][-]
  1. // no of object, but exposed self. You can assign an object method to it. (w/o the self exposed)
  2. TEnsureDataCallback=function(_self:pointer;const DataPtr: Pointer; MaxLen: Integer): Integer;stdcall;  
  3.  
  4. //or:
  5. type
  6.  TEnsureDataCallback=function(const DataPtr: Pointer; MaxLen: Integer): Integer;stdcall;
  7.  
  8.  TMytype = record
  9.  public
  10.    class function EnsureDataCallBack(const DataPtr: Pointer; MaxLen: Integer): Integer;stdcall;static; // Now it's assignable
  11.  end;

The choice depends on what you want to achieve. Interoperability with C code,maybe? Or your own Pascal code?
Title: Re: Procedure of record
Post by: Blestan on May 21, 2017, 10:18:47 pm
ok ... i also found a dirty hack with a TMethod field because i need the Instance Ptr ( Self)

  M.Code:=@Self.ReadBuffer;
  M.Data:=@Self;
  FBuffer^.OnRead:=M;       

 ... thanks for specifiing the calling convension
TinyPortal © 2005-2018