Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to declare a function/procedure with a variable number of arguments

344 views
Skip to first unread message

Yanlai Huang

unread,
Aug 12, 2003, 2:12:59 AM8/12/03
to
I want to call a function in a DLL.

This function is writen in C with a variable number of arguments, that likes
"printf".

How do I declare this function in Object Pascal?

Thanks

Peter Thönell

unread,
Aug 12, 2003, 2:28:51 AM8/12/03
to

Oh, and call it like this:

GetDelimToken( [.....] );


Peter Thönell

unread,
Aug 12, 2003, 2:26:28 AM8/12/03
to

function GetDelimToken(Terms: array of const): TToken;
var
i, len : integer;
begin
len := Length(Terms);
for i := 0 to len-1 do
Blabla( Terms[i].VClass );
end;


Ignacio Vazquez

unread,
Aug 12, 2003, 2:28:19 AM8/12/03
to
"Yanlai Huang" <h...@kingstargroup.com> wrote in message
news:3f38...@newsgroups.borland.com...

> This function is writen in C with a variable number of arguments, that
likes
> "printf".
>
> How do I declare this function in Object Pascal?

With "array of const". Look up "Variant open array parameters" in the Delphi
help.

Cheers,
Ignacio

Chris Morgan

unread,
Aug 12, 2003, 4:29:11 AM8/12/03
to

Array of const is the Delphi way of passing a variable number
of arguments to a function, but it is not compatible with C.

If you want to import a function from a DLL written in C
which has a variable number of arguments, use the varargs directive
(in Delphi 6 at least, not surre abt previous versions).

Look it up in the help.

Cheers,

Chris

Peter Below (TeamB)

unread,
Aug 12, 2003, 5:14:26 AM8/12/03
to

Depends on your Delphi version. Since D6 you can use the varargs directive:

function printf(Format: PChar): Integer; cdecl; varargs;
external 'foo.dll';

With this declaration the compiler allows you to call this function with
additional parameters. Varargs *only* works for cdecl functions and only for
external declarations, if the help can be trusted. The compiler accepts a function
pointer declaration like

Var
printf: function (Format: PChar): Integer cdecl varargs;

though, so dynamic loading should also work.

Note that all the advice on using an open array parameter you have received in
the other replies is off target if your aim is to call an existing C function in a
DLL.

--
Peter Below (TeamB)
Use the newsgroup archives :
http://www.mers.com/searchsite.html
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be


Yanlai Huang

unread,
Aug 13, 2003, 3:07:51 AM8/13/03
to
Very Thanks.

I'm trying to embed Python in my delphi application.

I have known that an open array parameter function is not compatible with C,
and known why a "cdecl" caller push parameters from right to left
to make the format string of "printf" at the top of the stack
to let "printf" process variant parameters.

But I haven't known the "varargs" directive. Very Thanks.

Why Borland does not mention it in "what's new" secion of the online help?

"Peter Below (TeamB)" <10011...@compuXXserve.com>
VA.0000a25...@nomail.please...

Peter Below (TeamB)

unread,
Aug 13, 2003, 6:17:51 AM8/13/03
to
In article <3f39...@newsgroups.borland.com>, Yanlai Huang wrote:
> Why Borland does not mention it in "what's new" secion of the online help?
>

No idea, i suppose it just fell through the cracks <g>.

Ignacio Vazquez

unread,
Aug 13, 2003, 10:18:48 AM8/13/03
to
"Yanlai Huang" <h...@kingstargroup.com> wrote in message
3f39...@newsgroups.borland.com...

> I'm trying to embed Python in my delphi application.

http://membres.lycos.fr/marat/delphi/python.htm

Cheers,
Ignacio

--
No, don't send me e-mail directly. No, just don't.


0 new messages