[fpc-pascal] Get TMethod from function reference
    Sven Barth 
    pascaldragon at googlemail.com
       
    Thu Sep 15 07:28:41 CEST 2022
    
    
  
Am 08.09.2022 um 16:55 schrieb Hairy Pixels via fpc-pascal:
> A function reference can call a class method but can you get the TMethod data from function references? It was possible with “is object” to cast to TMethod but that doesn’t seem to be possible with references.
>
> =========
>
> type
>    TMyClass = class
>      constructor Create;
>      procedure DoThis;
>    end;
>
>    constructor TMyClass.Create;
>    var
>      proc: reference to procedure;
>    begin
>      proc := @DoThis;
>      writeln(Assigned(PMethod(proc)^.data)); // FALSE
>    end;
As mentioned elsewhere function references are in fact interfaces. And 
you can't retrieve a method pointer to a interface function, cause 
relying on that would result in either memory leaks or premature release 
of the interface, because TMethod only contains a Pointer for the Data 
field that does not support managed types.
You can get a reference to the raw interface by doing this however:
=== code begin ===
var
   proc: reference to procedure;
   i: IUnknown;
begin
    i := IUnknown(PPointer(@proc)^);
end.
=== code end ===
Though I don't know what you hope to achieve with this...
Regards,
Sven
    
    
More information about the fpc-pascal
mailing list