Delphi:如何获得呼叫功能或功能输入? [英] Delphi: How to get the Calling-function or function-I-am-in?

查看:88
本文介绍了Delphi:如何获得呼叫功能或功能输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何方法可以获取呼叫功能(最佳)或功能I-am-in(第二个)? (用于设置该函数的回调)
我宁愿不必每次执行时都指定要从中调用的函数的名称。请注意,这些函数是类方法。

Is there any way to get the Calling-function (best) or function-I-am-in (2nd)? (for setting callbacks to that function) I would rather not have to specify the name of the function I am calling from, everytime I do it. Note that these functions are class methods.

这是我在做什么:(不喜欢)

Here is what I am doing: (and don't like)

procedure TX.SetMeTimer(FAnimate:Tproc);
begin
  Timer1.OnTimer:=FAnimate;
end; 

procedure TX.Animate1;
begin
  SetMeTimer(Animate1);
  DoSomething;
end;
procedure TX.Animate2;
begin
  SetMeTimer(Animate2);
  DoSomething;
end;
// to TXAnimate500, until you are sick of copying the function name....

这是我想要的样子:(调用函数方法)

This is what I would like it to look like: (Calling function approach)

procedure TX.StartReEntrantTimer(FAnimate:Tproc);
begin
  Timer1.OnTimer:=CallingFunction;
end; 

procedure TX.Animate;
begin
  SetMeTimer();
  DoSomething;
end;

或函数式输入法

procedure TX.StartReEntrantTimer(FAnimate:Tproc);
begin
  Timer1.OnTimer:=F;
end; 

procedure TX.Animate;
begin
  SetMeTimer(FunctionIamIn);
  DoSomething;
end;

为什么?对使用此工具的用户进行动态研究,发现错误的函数名称是导致错误的重要原因,我希望将其删除。

Why? Doing a time-and-motion study on user working with this tool, putting wrong function name is a significant error cause, and I would like to remove it.

推荐答案

Delphi附带的DUnit测试代码的TestFramework.pas单元具有一个CallerAddr函数,该函数在Delphi 2005中是这样实现的:

The unit TestFramework.pas of the DUnit test code that comes with Delphi has a function CallerAddr which (in Delphi 2005) is implemented like this:

function CallerAddr: Pointer; {$IFNDEF CLR} assembler; {$ENDIF}
{$IFDEF CLR}
begin
  Result := nil;
 end;
{$ELSE}
const
  CallerIP = $4;
asm
   mov   eax, ebp
   call  IsBadPointer
   test  eax,eax
   jne   @@Error

   mov   eax, [ebp].CallerIP
   sub   eax, 5   // 5 bytes for call

   push  eax
   call  IsBadPointer
   test  eax,eax
   pop   eax
   je    @@Finish

@@Error:
   xor eax, eax
@@Finish:
end;
{$ENDIF}

(新版本已被修改,因此您应该检查实现

(Later versions have been modified, so you should check the implementation of your own Delphi version.)

它用于在DUnit测试中在调用方法的地址处引发异常。

It is used to raise exceptions at the address of the calling method in DUnit tests. It should be possible to use it or at least adapt it to your purpose.

请注意,这可能仅适用于Win32程序。

Note that this will likely only work for Win32 programs.

这篇关于Delphi:如何获得呼叫功能或功能输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆