在Delphi中通过其内存地址调用可变参数C函数

Tor*_*bio 2 delphi typedef variadic-functions memory-address

假设我在C++中有一个函数,我在其中使用指向其内存地址的指针来调用它typedef.现在,我怎样才能在Delphi中做同样的事情?

例如:

typedef void (*function_t)(char *format, ...);
function_t Function;
Function = (function_t)0x00477123;
Run Code Online (Sandbox Code Playgroud)

然后,我可以用:Function("string", etc);.

在Delphi中,有没有办法在不使用汇编指令的情况下执行此操作?

请注意,它是一个可变参数函数.

Bar*_*lly 17

这是一个惯用的翻译:

typedef void (*function_t)(char *format, ...);
function_t Function;
Function = (function_t)0x00477123;
Run Code Online (Sandbox Code Playgroud)

这是:

type
  TFunction = procedure(Format: PAnsiChar) cdecl varargs;
var
  Function: TFunction;
// ...
  Function := TFunction($00477123);
Run Code Online (Sandbox Code Playgroud)

'cdecl varargs'需要获得C调用约定(调用者弹出堆栈)和可变参数支持(仅支持C调用约定).仅支持Varargs作为调用C的方法; Delphi中没有内置支持来实现C风格的可变参数列表.相反,Format程序和朋友使用了不同的机制:

function Format(const Fmt: string; const Args: array of const): string;
Run Code Online (Sandbox Code Playgroud)

但你可以在其他地方找到更多相关信息.