在Delphi中创建类似于Printf这样可变参数的函数

Delphi本身是不直接支持写Printf这样的变参函数,Format这样的函数类似于Printf,但是还是要多写两个括号,

我觉得太麻烦,其实我们可以间接的实现这样的函数,懒得解释,贴了段程序

program TestVar;

{$APPTYPE CONSOLE}

uses
  SysUtils, Windows;

type
  VA_FN = function(const par1{, ...}: PAnsiChar): Boolean; cdecl varargs;

procedure MyPrintf(const Msg: PAnsiChar);cdecl;
var
  last_arg: Pointer absolute Msg;
  ptr_args: array[0..1024] of Pointer absolute last_arg;
  Output: array [0 .. 1023] of AnsiChar;
  arr:array [0..1024] of Pointer;
  I: Integer;

begin
  try
    FillChar(Output, SizeOf(Output), 0);

    wvsprintfA(Output,Msg, @ptr_args[1]);

    Writeln(string(Output));
  except
    //mask some weird invalid floating point exception
  end;
end;

procedure test_fn;
begin
  // VA_FN typecast
  VA_FN(@Myprintf)({par1}'%d %d %s', 1, 2, PAnsichar('abc'));
  VA_FN(@Myprintf)({par1}'%d %d %s', 3, 4, PAnsichar('abcd'));
end;

begin
  try
    test_fn();

  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.