杨
杨深
Unregistered / Unconfirmed
GUEST, unregistred user!
VC 中定义一可变参数的动态链接库函数Test.dll
int WINAPI TestParam(char * sFormat,...)
{
int first=3;
int count = 0, sum = 0, i = 0;
va_list marker;
va_start( marker, sFormat )
while( i != -1 )
{
i = va_arg( marker, int);
}
va_end( marker )
return 0;
}
在Delphi中调用
Delphi 中定义
unit Test;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, StdCtrls;
function TestParam(sDbNamechar
const Args: array of const): integer
stdcall;
implementation
const
TDLL = 'Test.dll';
function TestParam
external TDLL name 'TestParam';
end.
执行
TestParam('%d,%d,%d',[1,2,4]);
出错,在VC中调试看到参数取不正确。
请问原因
其实delphi中可以定义
function testParam(sFormatchar;const args: array of const):extended;
var
i:integer;
begin
Result := 0;
for i := low(args) to high(args) do
case args.vtype of
vtinteger: Result := Result + args.vinteger;
vtboolean: if args.vboolean then Result := Result + Ord(args.vBoolean);
vtchar: Result := Result + Ord(args.vchar);
vtextended: Result := Result + args.vextended^;
vtstring: Result := Result + strtointdef((args.vstring^),0);
vtansistring: Result := Result + strtointdef(string(args.vansistring),0);
vtcurrency: Result := Result + args.vcurrency^;
end;
……
end;
请问变参在Delphi 中还可用别的办法吗?除了用const args: array of const
int WINAPI TestParam(char * sFormat,...)
{
int first=3;
int count = 0, sum = 0, i = 0;
va_list marker;
va_start( marker, sFormat )
while( i != -1 )
{
i = va_arg( marker, int);
}
va_end( marker )
return 0;
}
在Delphi中调用
Delphi 中定义
unit Test;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, StdCtrls;
function TestParam(sDbNamechar
const Args: array of const): integer
stdcall;
implementation
const
TDLL = 'Test.dll';
function TestParam
external TDLL name 'TestParam';
end.
执行
TestParam('%d,%d,%d',[1,2,4]);
出错,在VC中调试看到参数取不正确。
请问原因
其实delphi中可以定义
function testParam(sFormatchar;const args: array of const):extended;
var
i:integer;
begin
Result := 0;
for i := low(args) to high(args) do
case args.vtype of
vtinteger: Result := Result + args.vinteger;
vtboolean: if args.vboolean then Result := Result + Ord(args.vBoolean);
vtchar: Result := Result + Ord(args.vchar);
vtextended: Result := Result + args.vextended^;
vtstring: Result := Result + strtointdef((args.vstring^),0);
vtansistring: Result := Result + strtointdef(string(args.vansistring),0);
vtcurrency: Result := Result + args.vcurrency^;
end;
……
end;
请问变参在Delphi 中还可用别的办法吗?除了用const args: array of const