Sign in Join

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

Mailing lists / fpc-pascal / 2016-05

passing "array of const" into cdecl procvar

5 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

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

Seth Grover

I'm having a little trouble figuring out how to do the following:

I have a procedural type defined as such:

===========================
type
Tjson_pack = function(

    fmt : pchar;
    args : array of const)
    : pjson_t; cdecl;

===========================

I have a variable defined as:

===========================
_json_pack : Tjson_pack = nil;
===========================

And, finally, I have a local function declared this way:

===========================
function json_pack(
  fmt : pchar;
  args : array of const)
  : pjson_t;
begin
  if (_json_pack  nil) then begin
    {$warning not sure how to handle this}
    result := _json_pack(fmt, args);
  end else begin
    result := nil;
  end;
end;
===========================

The error message I get when trying to compile is this:

Error: Wrong type "Array Of Const" in array constructor

If I try to make my local json_pack function cdecl, I get this:

Error: VarArgs directive (or '...' in MacPas) without CDecl/CPPDecl/MWPascal and External

The reason I'm using a procedural type is because I'm loading the external library at runtime via dynlibs:

===========================
_json_pack := Tjson_pack(dynlibs.GetProcAddress(JanssonLibHandle,
'json_pack'));
===========================

so I can't declare my function as external, since it's not. I can't declare my procedural type as external, of course:

Error: Procedure directive "EXTERNAL" not allowed in procvar declaration

How can I do this? Is there any way for me to pass an array of const argument through directly to a cdecl'ed procvar call?

(And yes, I know FPC has a built-in json engine... that's besides the point :))

Thanks,

-Seth Grover

Sven Barth
to Seth Grover
"Seth Grover" wrote
How can I do this? Is there any way for me to pass an array of const

argument through directly to a cdecl'ed procvar call?

You can't, because an array of const/vararg of a cdecl function is something completely different from a normal Pascal array of const.

Regards,
Sven

Maciej Izak
to Seth Grover: I'm having a little trouble figuring out how to do the following: I have a procedural…

2016-05-13 17:58 GMT+02:00 Seth Grover :

How can I do this? Is there any way for me to pass an array of const argument through directly to a cdecl'ed procvar call?

Sven is wrong, of course you can, but it requires some work. Maybe not much multi-platform and in few cases limited but works fine for x86 on Windows.

Look my old question and great answer from Barry Kelly:

http://stackoverflow.com/questions/2305962/delphi-array-of-const-to-varargs

It can be ported to other platforms/architectures but not so easy :)

Best regards, Maciej Izak
Seth Grover

Thanks, Sven and Maciej, for the quick answers.

Yikes, that example is a little bit ugly. Hm. Well fortunately there is another API in this library I'm wrapping that does almost the same thing. I think rather than go down that rabbit hole I'll see if I can rework what I'm doing to do using another API and just forget the varargs one for now.

Thanks,

-SG

Seth Grover
Sven Barth
to Maciej Izak: 2016-05-13 17:58 GMT+02:00 Seth Grover : Sven is wrong, of course you can, but it…
"Maciej Izak" wrote
2016-05-13 17:58 GMT+02:00 Seth Grover : > > How can I do this? Is there any way for me to pass an array of const

argument through directly to a cdecl'ed procvar call?

Sven is wrong, of course you can, but it requires some work. Maybe not

much multi-platform and in few cases limited but works fine for x86 on Windows.

I probably should have written "short answer" and "long answer". The short answer is - as I wrote - no it's not possible. The long answer is that it's possible, but only with quote some effort - as shown with the link you provided.

Regards,
Sven