我处于这样一种情况,我必须使用 C++ 和内联 ASM 模拟一个 _stdcall 函数,但它使用可变数量的参数。通常它不知道在将控制权返回给它的父级时从堆栈中弹出多少参数,所以这是行不通的,但我希望通过一个全局变量告诉它它应该有多少参数然后得到它像那样弹出它们。
这真的可能吗?如果是这样,有人可以让我朝着正确的方向出发吗?我特别坚持我需要的结尾代码。
我的目标是制作一个函数,该函数可用作任何需要回调的函数(如 EnumWindows)的回调,只要用户在运行时告诉它 args 列表必须有多长。这个想法是让它与其他地方的一些代码集成,所以它基本上在每次调用回调时运行一个触发器,并提供一个链接到一个地方,用户可以在这个地方读取和查看返回的变量。
这有意义吗?
最佳答案
没有意义。 __stdcall
不允许可变参数,因为所有参数的总大小被修饰到函数名称 ( from msdn ) 中:
Name-decoration convention
An underscore (_) is prefixed to the name. The name is followed by the at sign (@) followed by the number of bytes (in decimal) in the argument list. Therefore, the function declared as
int func( int a, double b )
is decorated as follows:_func@12
这段引述告诉您可变参数 __stdcall
函数是如何实现的:
The __stdcall calling convention is used to call Win32 API functions. The callee cleans the stack, so the compiler makes vararg functions __cdecl. Functions that use this calling convention require a function prototype.
(强调我的)
因此,没有带有可变参数的 __stdcall
函数,它们会悄悄地更改为 __cdecl
。 :)
关于c++ - _stdcall 中的可变参数,C++/内联 ASM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5612260/