Delphi “array of const” to “varargs”

后端 未结 4 654
囚心锁ツ
囚心锁ツ 2020-12-16 23:19

Please help! I need this conversion to write wrapper for some C headers for Delphi.

As an example:

function pushfstring(fmt: PAnsiChar): PAnsiChar; c         


        
4条回答
  •  孤街浪徒
    2020-12-17 00:14

    The wrapper you are trying to write is possible in Free Pascal, since Free Pascal supports 2 equvalent declarations for varargs external functions:

    http://www.freepascal.org/docs-html/ref/refsu68.html

    so instead of

    function pushfstring(fmt: PAnsiChar): PAnsiChar; cdecl; varargs; external;
    

    you should write

    function pushfstring(fmt: PAnsiChar; Args: Array of const): PAnsiChar; cdecl; external;
    

    Update: I have tried the same trick in Delphi, but it does not work:

    //function sprintf(S, fmt: PAnsiChar; const args: array of const): Integer;
    //           cdecl; external 'MSVCRT.DLL';
    
    function sprintf(S, fmt: PAnsiChar): Integer;
               cdecl; varargs; external 'MSVCRT.DLL';
    
    procedure TForm1.Button1Click(Sender: TObject);
    var
      S, fmt: Ansistring;
    
    begin
      SetLength(S, 99);
      fmt:= '%d - %d';
    //  sprintf(PAnsiChar(S), PAnsiChar(fmt), [1, 2]);
      sprintf(PAnsiChar(S), PAnsiChar(fmt), 1, 2);
      ShowMessage(S);
    end;
    

提交回复
热议问题