Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2004-09

Variable argument counts...

11 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Gene Buckle

I'm working on wrapping a C library that has a function that can handle a variable number of parameters. What mechanism can I use in FPC to call this correctly?

The C prototype for the function looks like this:

void QMCall(char * subrname, short int argc, ...);

The function can handle up to 20 parameters after the 'argc' parameter. Would I have to define all 20 possible parameters? Each of the optional parameters is a string type.

Thanks for the help!

g.

"I'm not crazy, I'm plausibly off-nominal!" Proud owner of 80-0007 http://www.f15sim.com - The only one of its kind.

Reply by email

Jonas Maebe
to Gene Buckle
Gene Buckle wrote 8 lines
I'm working on wrapping a C library that has a function that can handle a variable number of parameters. What mechanism can I use in FPC to call this correctly? The C prototype for the function looks like this: void QMCall(char * subrname, short int argc, ...);
procedure QMCall(subrname: pchar; argc: shortint; rest: array of
const); cdecl; external;

You can then call it using

QMCall('test',1,['para1','para2',5,null]);

(I know you said it only needed strings, but it's just to show that you can pass anything that way, just like with "...").

Jonas

Reply by email

Jonas Maebe
to Gene Buckle: Jonas, thanks for the quick response! Do you know if this same trick will work with…
Gene Buckle wrote 14 lines
> procedure QMCall(subrname: pchar; argc: shortint; rest: array of > const); cdecl; external; > > You can then call it using > > QMCall('test',1,['para1','para2',5,null]); > > (I know you said it only needed strings, but it's just to show that > you > can pass anything that way, just like with "..."). Jonas, thanks for the quick response! Do you know if this same trick will work with Delphi 7?

I have no idea. I've never programmed in Delphi in my entire life.

Jonas

Reply by email

Gene Buckle
to Jonas Maebe: procedure QMCall(subrname: pchar; argc: shortint; rest: array of const); cdecl; external;…
Show quoted text 11 lines
procedure QMCall(subrname: pchar; argc: shortint; rest: array of const); cdecl; external; You can then call it using QMCall('test',1,['para1','para2',5,null]); (I know you said it only needed strings, but it's just to show that you can pass anything that way, just like with "...").

Jonas, thanks for the quick response! Do you know if this same trick will work with Delphi 7?

Thanks again!

g.

"I'm not crazy, I'm plausibly off-nominal!" Proud owner of 80-0007 http://www.f15sim.com - The only one of its kind.

Reply by email

Jonas Maebe
to Gene Buckle: My declaration reads like this: procedure QMCall(SubrName : PChar; ArgCount : shortint;…
Gene Buckle wrote 8 lines
procedure QMCall(SubrName : PChar; ArgCount : shortint; Args : array of const); cdecl; external 'QMClient'; However, when I build it, I get an error: QMClient.pas(25,73) Error: Type identifier expected QMClient.pas(25,73) Fatal: Syntax error, ) expected but CONST found

Sorry, it should be

procedure QMCall(SubrName : PChar; ArgCount : shortint; const Args :
array of
const); cdecl; external 'QMClient';

(i.e., another "const" in front of "Args").

Jonas

Reply by email

Gene Buckle
to Jonas Maebe: procedure QMCall(subrname: pchar; argc: shortint; rest: array of const); cdecl; external;…
Show quoted text 7 lines
procedure QMCall(subrname: pchar; argc: shortint; rest: array of const); cdecl; external; You can then call it using QMCall('test',1,['para1','para2',5,null]);

My declaration reads like this:

procedure QMCall(SubrName : PChar; ArgCount : shortint; Args : array of
const); cdecl;
  external 'QMClient';

However, when I build it, I get an error:

QMClient.pas(25,73) Error: Type identifier expected
QMClient.pas(25,73) Fatal: Syntax error, ) expected but CONST found

I'm using FPC 1.0.10 dated 2003/06/26.

tnx!

g.

"I'm not crazy, I'm plausibly off-nominal!" Proud owner of 80-0007 http://www.f15sim.com - The only one of its kind.

Reply by email

Gene Buckle
to Jonas Maebe: Sorry, it should be procedure QMCall(SubrName : PChar; ArgCount : shortint; const Args :…
Show quoted text 10 lines
Sorry, it should be procedure QMCall(SubrName : PChar; ArgCount : shortint; const Args : array of const); cdecl; external 'QMClient'; (i.e., another "const" in front of "Args").
Ok, I made the change:
procedure QMCall(SubrName : PChar; ArgCount : shortint; const Args : array
of const); cdecl; external 'QMClient';
However, I still get the same error:
QMClient.pas(30,79) Error: Type identifier expected
QMClient.pas(30,79) Fatal: Syntax error, ) expected but CONST found

Ideas?

Thanks for the help!

g.

"I'm not crazy, I'm plausibly off-nominal!" Proud owner of 80-0007 http://www.f15sim.com - The only one of its kind.

Reply by email

Jonas Maebe
to Gene Buckle
Gene Buckle wrote 10 lines
Ok, I made the change: procedure QMCall(SubrName : PChar; ArgCount : shortint; const Args : array of const); cdecl; external 'QMClient'; However, I still get the same error: QMClient.pas(30,79) Error: Type identifier expected QMClient.pas(30,79) Fatal: Syntax error, ) expected but CONST found Ideas?

You have to be in delphi or objfpc mode for this to work apparently. Sorry I didn't look that up earlier.

Jonas

Reply by email

Jonas Maebe
to Gene Buckle: That did the trick, thanks! It does kick a warning though: Warning: cdecl'ared functions…
Gene Buckle wrote 7 lines
That did the trick, thanks! It does kick a warning though: Warning: cdecl'ared functions have no high parameter I wasn't able to find this in the documentation or the online forum. Is this directly due to the declaration and is it safe to ignore?

Yes, it's safe to ignore. It's a bug that that this warning is shown when you declare the routine as external, it's only relevant if you program the routine itself in Pascal (since then you can't use "for i := low(args) to high(args) do ...", like you can for Pascal array-of-const parameters).

Jonas

Reply by email

Gene Buckle
to Jonas Maebe: You have to be in delphi or objfpc mode for this to work apparently. Sorry I didn't look…
Show quoted text 5 lines
You have to be in delphi or objfpc mode for this to work apparently. Sorry I didn't look that up earlier.

That did the trick, thanks! It does kick a warning though:

Warning: cdecl'ared functions have no high parameter

I wasn't able to find this in the documentation or the online forum. Is this directly due to the declaration and is it safe to ignore?

tnx.

g.

"I'm not crazy, I'm plausibly off-nominal!" Proud owner of 80-0007 http://www.f15sim.com - The only one of its kind.

Reply by email

Gene Buckle
to Jonas Maebe: Yes, it's safe to ignore. It's a bug that that this warning is shown when you declare the…
Show quoted text 7 lines
Yes, it's safe to ignore. It's a bug that that this warning is shown when you declare the routine as external, it's only relevant if you program the routine itself in Pascal (since then you can't use "for i := low(args) to high(args) do ...", like you can for Pascal array-of-const parameters).

Ok, that's good to know. Thanks again for the fast responses!

g.

"I'm not crazy, I'm plausibly off-nominal!" Proud owner of 80-0007 http://www.f15sim.com - The only one of its kind.

Reply by email