首页  编辑  

如何使用C中的不定参数函数呢

Tags: /超级猛料/Language.Object Pascal/内嵌汇编、函数、过程/   Date Created:

如何使用C中的不定参数函数呢?

例如API中有wsprintf和wvsprintf两个函数,其为不定参数函数,在Delphi中如何使用呢?

有两个方法一个是使用不定参数函数声明:

 var

   D: Integer;

   HD: Smallint;

   HU: Word;

   HS: PAnsiChar;

   LS: PWideChar;

   OutStr: array[0..256] of Char;

 function wsprintf(Output: PChar; Format: PChar): Integer; cdecl;

   varargs; external 'user32.dll' name 'wsprintfA';

 begin

   D := 1234;

   HD := -5678;

   HU := 33000;

   HS := 'Hello'; // Ansi!

   LS := 'World'; // Wide!

   wsprintf(OutStr, '%d %hd %hu %hs %ls', D, HD, HU, HS, LS);

   Writeln(OutStr);

   Readln;

 end.

Delphi本身的函数和过程等不支持 varargs 关键字,该关键字只能用于 external 的外部连接函数或过程,例如DLL或者OBJ。

另外一个为使用Delphi本身的声明 + 数组实现:

function sprintf(const Format: string; Args: array of const): string; stdcall;

{

 类似C语言中sprintf的函数,请参考MSDN中的wvsprintf函数

Support Format:

 %[-][#][0][width][.precision]type

type Value Meaning

 c        Single character. This value is interpreted as type WCHAR if the calling

          application defines Unicode and as type __wchar_t otherwise.

 C        Single character. This value is interpreted as type __wchar_t if the calling

          application defines Unicode and as type WCHAR otherwise.

 d        Signed decimal integer. This value is equivalent to i.

 hc, hC   Single character. The wsprintf function ignores character arguments

          with a numeric value of zero. This value is always interpreted as

          type __wchar_t, even when the calling application defines Unicode.

 hd       Signed short integer argument.

 hs, hS   String. This value is always interpreted as type LPSTR, even when the

          calling application defines Unicode.

 hu       Unsigned short integer.

 i        Signed decimal integer. This value is equivalent to d.

 lc, lC   Single character. The wsprintf function ignores character arguments

          with a numeric value of zero. This value is always interpreted as

          type WCHAR, even when the calling application does not define Unicode.

 ld       Long signed integer. This value is equivalent to li.

 li       Long signed integer. This value is equivalent to ld.

 ls, lS   String. This value is always interpreted as type LPWSTR, even when the

          calling application does not define Unicode. This value is equivalent to ws.

 lu       Long unsigned integer.

 lx, lX   Long unsigned hexadecimal integer in lowercase or uppercase.

 p        Windows 2000/XP: Pointer. The address is printed using hexadecimal.

 s        String. This value is interpreted as type LPWSTR when the calling

          application defines Unicode and as type LPSTR otherwise.

 S        String. This value is interpreted as type LPSTR when the calling

          application defines Unicode and as type LPWSTR otherwise.

 u        Unsigned integer argument.

 x, X     Unsigned hexadecimal integer in lowercase or uppercase.

}

var

 OutPutBuffer: array[0..1023] of char;

 ArgsBuffers: array of PChar;

 i: integer;

begin

 Result := Format;

 if Length(Args) = 0 then Exit;

 SetLength(ArgsBuffers, Length(Args));

 for i := Low(Args) to High(Args) do

 begin

   ArgsBuffers[i] := Args[i].VPointer;

 end;

 ZeroMemory(@OutPutBuffer[0], SizeOf(OutPutBuffer));

 SetString(Result, OutPutBuffer, wvsprintf(OutPutBuffer, PChar(Format), @ArgsBuffers[0]));

end;