Recent

Author Topic: Procedure of record  (Read 1886 times)

Blestan

  • Sr. Member
  • ****
  • Posts: 461
Procedure of record
« 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?
Speak postscript or die!
Translate to pdf and live!

Thaddy

  • Hero Member
  • *****
  • Posts: 10705
Re: Procedure of record
« Reply #1 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?
« Last Edit: May 21, 2017, 09:56:35 pm by Thaddy »

Blestan

  • Sr. Member
  • ****
  • Posts: 461
Re: Procedure of record
« Reply #2 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
Speak postscript or die!
Translate to pdf and live!

 

TinyPortal © 2005-2018