为什么此程序不是TProc?

时间:2019-05-16 18:58:58

标签: delphi

我遇到以下情况:

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;

编译器说我不能使用TTask运行MyProcedure。尝试将MyProcedure强制转换为TProc是错误的。我的问题是1)什么是MyProcedure? 2)我将如何发现MyProcedure的类型?

谢谢

1 个答案:

答案 0 :(得分:5)

TProc不使用stdcall调用约定。它被声明为匿名方法类型,它使用默认的register调用约定:

 TProc = reference to procedure;

 TMyProcedure = procedure of object; stdcall;

匿名方法与声明调用约定而不是标准Delphi register约定的方法不兼容。不要使用stdcall,也不要插入包装器方法或本地匿名方法,即:

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