不定参数函数
{ .... }
type
VA_FN = function(const par1, par2{, ...}: Pointer): Boolean;
cdecl varargs;
{ .... }
//
// varargs without "external" keyword
//
function fn(const par1, par2{, ...}: Pointer): Boolean; cdecl;
var
last_arg: Pointer absolute par2;
ptr_args: array[0..MAXLONG shr 2] of Pointer absolute last_arg;
dw_args: array[0..MAXLONG shr 2] of Cardinal absolute last_arg;
s_args: array[0..MAXLONG shr 2] of PChar absolute last_arg;
w_args: array[0..MAXLONG shr 2] of WideString absolute last_arg;
begin
// ptr_args[1] is first optional argument
Result := (ptr_args[1] {par3} = Pointer(1)) and
(dw_args[2] {par4} = 2) and
(string(ptr_args[3]) = 'CHAR') and
(w_args[4] = 'WCHAR');
end;
procedure test_fn;
begin
// VA_FN typecast
VA_FN(@fn)({par1}nil, {par2}nil, {par3}Pointer(1), {par4}2, {par5}'CHAR',
{par6}WideString('WCHAR'));
end;
---------------------------------------
http://www.chami.com/tips/delphi/112696D.html
//
// FunctionWithVarArgs()
//
// skeleton for a function that
// can accept vairable number of
// multi-type variables
//
// here are some examples on how
// to call this function:
//
// FunctionWithVarArgs(
// [ 1, True, 3, '5', '0' ] );
//
// FunctionWithVarArgs(
// [ 'one', 5 ] );
//
// FunctionWithVarArgs( [] );
//
procedure FunctionWithVarArgs(
const ArgsList : array of const );
var
ArgsListTyped :
array[0..$FFF0 div SizeOf(TVarRec)]
of TVarRec absolute ArgsList;
n : integer;
begin
for n := Low( ArgsList ) to
High( ArgsList ) do
begin
with ArgsListTyped[ n ] do
begin
case VType of
vtInteger : begin
{handle VInteger here} end;
vtBoolean : begin
{handle VBoolean here} end;
vtChar : begin
{handle VChar here} end;
vtExtended : begin
{handle VExtended here} end;
vtString : begin
{handle VString here} end;
vtPointer : begin
{handle VPointer here} end;
vtPChar : begin
{handle VPChar here} end;
vtObject : begin
{handle VObject here} end;
vtClass : begin
{handle VClass here} end;
vtWideChar : begin
{handle VWideChar here} end;
vtPWideChar : begin
{handle VPWideChar here} end;
vtAnsiString: begin
{handle VAnsiString here} end;
vtCurrency : begin
{handle VCurrency here} end;
vtVariant : begin
{handle VVariant here} end;
else begin
{handle unknown type here} end;
end;
end;
end;
end;
//
// example function created using
// the above skeleton
//
// AddNumbers() will return the
// sum of all the integers passed
// to it
//
// AddNumbers( [1, 2, 3] )
// will return 6
//
//
function AddNumbers(
const ArgsList : array of const )
: integer;
var
ArgsListTyped :
array[0..$FFF0 div SizeOf(TVarRec)]
of TVarRec absolute ArgsList;
n : integer;
begin
Result := 0;
for n := Low( ArgsList ) to
High( ArgsList ) do
begin
with ArgsListTyped[ n ] do
begin
case VType of
vtInteger : Result := Result + VInteger;
end;
end;
end;
end;