David U
David U

Reputation: 993

Why is this procedure not a TProc?

I have a situation like the following:

interface

type
    IMyInterface = interface
    [GUID]
        procedure MyProcedure; stdcall;
    end;

    TMyOBject = class(TInterfacedObject, IMyInterface)
        procedure MyProcedure; virtual; stdcall; abstract;
    end;

    TDerivedObject = class(TMyOBject)
        procedure MyProcedure; override; stdcall;
        procedure SomeOtherProcedure;
    end;

implementation

uses
    System.Threading;

procedure TDerivedObject.MyProcedure;
begin
    //DoStuff;
end;

procedure TDerivedObject.SomeOtherProcedure;
begin
    TTask.Run(MyProcedure); //Error: Run can't be called with this parameter
end;

The compiler says I can't use a TTask to run MyProcedure. It is an error to try and cast MyProcedure to a TProc. My questions are 1) What type is MyProcedure? 2) How would I go about discovering the type of MyProcedure?

Thanks

Upvotes: 4

Views: 848

Answers (1)

J...
J...

Reputation: 31403

TProc doesn't use the stdcall calling convention. It is declared an as anonymous method type that uses the default register calling convention instead:

 TProc = reference to procedure;

whereas

 TMyProcedure = procedure of object; stdcall;

Anonymous methods are not compatible with methods declaring calling conventions other than the standard Delphi register convention. Either don't use stdcall, or insert a wrapper method or local anonymous method, ie:

procedure TDerivedObject.SomeOtherProcedure;
begin
  TTask.Run(procedure begin MyProcedure; end); 
end;

Upvotes: 6

Related Questions