Delphi thiscall调用约定 [英] Delphi thiscall calling convention

查看:182
本文介绍了Delphi thiscall调用约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要调用非静态C ++成员函数

I need to call non-static C++ member functions

因此,我需要使用 thiscall 调用约定

Hence i need to use the thiscall calling convention.

Delphi不支持此调用约定。

Delphi doesn't support this calling convention.

所以在我可以调用任何成员函数之前我必须手动推动堆栈中的参数。

So before i can call any of the member functions i have to manually push the parameters in the stack.

  asm
    mov ecx, myClassPointer
  end;

这工作正常,但我正在寻找一个更好的方式来做到这一点。

This works fine but i am looking for a better way to do this.

目前,我在调用函数之前用classpointer调用asm代码,这不是很好。

At the moment i am calling the asm code everytime with the classpointer before calling the function, which isn't nice.

因为我我不是Delphi专家,我想知道你是否可以声明自己的调用约定或自动调用这些这个类的方法。

As i am no Delphi expert i am wondering if you can declare your own calling convention or automate the calling of those thiscall class methods.

推荐答案

Delphi的注册调用约定使用前三个参数的寄存器。前两个是 EAX EDX ,它们在 thiscall 调用约定,可以设置为任何你喜欢的。这些寄存器的第三个是 ECX

Delphi's register calling convention uses registers for the first three parameters. The first two are EAX and EDX, which are unused in the thiscall calling convention and can be set to anything you like. The third of those registers is ECX.

thiscall 传递堆栈上的所有参数,寄存器将堆栈中的剩余参数传递给它。 注册 thiscall 将被调用程序清除堆栈上传递的参数。

thiscall passes all arguments on the stack, and register passes remaining arguments on the stack. Both register and thiscall have the callee clean up arguments passed on the stack.

所以你可以做的是声明一个注册函数,虚拟参数为 EAX EDX 。您仍然可以将其包装在一个不同的函数中以使其更容易调用,但至少现在可以将包装器函数声明为 inline

So what you can do is declare a register function, with dummy parameters for EAX and EDX. You can still wrap this in a different function to make it easier to call, but at least now that wrapper function can be declared inline.

注意,在 thiscall 注册,所以你需要反转参数匹配。

Note though that the order in which arguments get pushed on the stack is different between thiscall and register, so you need to reverse parameters to match.

这篇关于Delphi thiscall调用约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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